Add files via upload

This commit is contained in:
Alexander 2018-08-13 11:26:05 +03:00 committed by GitHub
parent 30545f4ccc
commit 0696024fde
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 344 additions and 0 deletions

View File

@ -0,0 +1,9 @@
#
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
# project subdirectory.
#
PROJECT_NAME := i2c
include $(IDF_PATH)/make/project.mk

View File

@ -0,0 +1,13 @@
# TCA9535 Example
* This example will show you how to use TCA9535 I2C I/O Expander:
* read register data
* write register data
* Pin assignment:
* GPIO33 is assigned as the data signal of i2c slave port
* GPIO9 is assigned as the clock signal of i2c slave port
* GPIO39 is assigned to INT output of TCA9535. This functionality is not used in this example

View File

@ -0,0 +1,176 @@
#include "TCA9535.h"
#include "driver/i2c.h"
#include "esp_err.h"
#include "esp_log.h"
#include "string.h"
// ****************************************************************************
//! @brief Initializes the I2C interface
//! @param None
//! @return
//! - ESP_OK if erase operation was successful
//! - i2c driver error
// ****************************************************************************
esp_err_t TCA9535Init(void)
{
esp_err_t ret;
int i2c_master_port = I2C_MASTER_NUM;
i2c_config_t conf;
conf.mode = I2C_MODE_MASTER;
conf.sda_io_num = I2C_MASTER_SDA_IO;
conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
conf.scl_io_num = I2C_MASTER_SCL_IO;
conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
conf.master.clk_speed = I2C_MASTER_FREQ_HZ;
i2c_param_config(i2c_master_port, &conf);
ret = i2c_driver_install(i2c_master_port, conf.mode,
I2C_MASTER_RX_BUF_DISABLE,
I2C_MASTER_TX_BUF_DISABLE, 0);
return ret;
}
// ****************************************************************************
//! @brief Reads single byte from specified register
//! @param Register address
//! @return Byte from register
// ****************************************************************************
/* _________________________________________________________________________________________________________
* | start | slave_addr + wr_bit +ack | reg_addr + ack | start | slave_addr + rd_bit +nack | reg_data |stop |
* --------|--------------------------|----------------|-------|---------------------------|----------|-----|
*/
unsigned char TCA9535ReadSingleRegister(tca9535_reg_t address)
{
uint8_t reg_data = 0;
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, ( TCA9535_ADDRESS << 1 ) | WRITE_BIT, ACK_CHECK_EN);
i2c_master_write_byte(cmd, address, ACK_CHECK_EN);
i2c_master_start(cmd);
i2c_master_write_byte(cmd, ( TCA9535_ADDRESS << 1 ) | READ_BIT, ACK_CHECK_EN);
i2c_master_read_byte(cmd, &reg_data, NACK_VAL);
i2c_master_stop(cmd);
i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 100 / portTICK_RATE_MS);
i2c_cmd_link_delete(cmd);
return reg_data;
}
// ****************************************************************************
//! @brief Writes single byte to specified register
//! @param Register address
//! @return
//! - ESP_OK if erase operation was successful
//! - i2c driver error
// ****************************************************************************
/* ____________________________________________________________________________________
* | start | slave_addr + wr_bit + ack | reg_addr + ack | write reg_data + ack | stop |
* --------|---------------------------|----------------|-----------------------|------|
*/
esp_err_t TCA9535WriteSingleRegister(tca9535_reg_t address, unsigned short regVal)
{
esp_err_t ret;
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, ( TCA9535_ADDRESS << 1 ) | WRITE_BIT, ACK_CHECK_EN);
i2c_master_write_byte(cmd, address, ACK_CHECK_EN);
i2c_master_write_byte(cmd, regVal, ACK_CHECK_EN);
i2c_master_stop(cmd);
ret = i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 100 / portTICK_RATE_MS);
i2c_cmd_link_delete(cmd);
return ret;
}
// ****************************************************************************
//! @brief Reads whole register and puts result into struct
//! @param reg: struct pointer
//! reg_num: register type
//! @return
//! - ESP_OK if erase operation was successful
//! - i2c driver error
// ****************************************************************************
esp_err_t TCA9535ReadStruct(TCA9535_Register *reg, tca9535_reg_t reg_num)
{
esp_err_t ret;
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, ( TCA9535_ADDRESS << 1 ) | WRITE_BIT, ACK_CHECK_EN);
i2c_master_write_byte(cmd, reg_num, ACK_CHECK_EN);
i2c_master_start(cmd);
i2c_master_write_byte(cmd, ( TCA9535_ADDRESS << 1 ) | READ_BIT, ACK_CHECK_EN);
i2c_master_read(cmd, (uint8_t*) &reg->asInt, 2, NACK_VAL);
i2c_master_stop(cmd);
ret = i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 100 / portTICK_RATE_MS);
i2c_cmd_link_delete(cmd);
return ret;
}
// ****************************************************************************
//! @brief Writes whole register with data from struct
//! @param reg: struct pointer
//! reg_num: register type
//! @return
//! - ESP_OK if erase operation was successful
//! - i2c driver error
// ****************************************************************************
esp_err_t TCA9535WriteStruct(TCA9535_Register *reg, tca9535_reg_t reg_num)
{
esp_err_t ret;
uint8_t reg_data[2];
memcpy(reg_data, reg, sizeof(reg_data));
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, ( TCA9535_ADDRESS << 1 ) | WRITE_BIT, ACK_CHECK_EN);
i2c_master_write_byte(cmd, reg_num, ACK_CHECK_EN);
i2c_master_write(cmd, reg_data, 2, ACK_VAL);
i2c_master_stop(cmd);
ret = i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 100 / portTICK_RATE_MS);
i2c_cmd_link_delete(cmd);
return ret;
}
esp_err_t TCA9535WriteOutput(TCA9535_Register *reg)
{
return TCA9535WriteStruct(reg, TCA9535_OUTPUT_REG0);
}
esp_err_t TCA9535WritePolarity(TCA9535_Register *reg)
{
return TCA9535WriteStruct(reg, TCA9535_POLARITY_REG0);
}
esp_err_t TCA9535WriteConfig(TCA9535_Register *reg)
{
return TCA9535WriteStruct(reg, TCA9535_CONFIG_REG0);
}
esp_err_t TCA9535ReadInput(TCA9535_Register *reg)
{
return TCA9535ReadStruct(reg, TCA9535_INPUT_REG0);
}
esp_err_t TCA9535ReadOutput(TCA9535_Register *reg)
{
return TCA9535ReadStruct(reg, TCA9535_OUTPUT_REG0);
}
esp_err_t TCA9535ReadPolarity(TCA9535_Register *reg)
{
return TCA9535ReadStruct(reg, TCA9535_POLARITY_REG0);
}
esp_err_t TCA9535ReadConfig(TCA9535_Register *reg)
{
return TCA9535ReadStruct(reg, TCA9535_CONFIG_REG0);
}

View File

@ -0,0 +1,74 @@
#include "esp_err.h"
/************************** I2C Address ***************************************/
#define TCA9535_ADDRESS 0x23 /*!< I2C Address */
/************************** I2C Registers *************************************/
typedef enum {
TCA9535_INPUT_REG0 = 0x00, /*!< Input status register */
TCA9535_INPUT_REG1 = 0x01, /*!< Input status register */
TCA9535_OUTPUT_REG0 = 0x02, /*!< Output register to change state of output BIT set to 1, output set HIGH */
TCA9535_OUTPUT_REG1 = 0x03, /*!< Output register to change state of output BIT set to 1, output set HIGH */
TCA9535_POLARITY_REG0 = 0x04, /*!< Polarity inversion register. BIT '1' inverts input polarity of register 0x00 */
TCA9535_POLARITY_REG1 = 0x05, /*!< Polarity inversion register. BIT '1' inverts input polarity of register 0x00 */
TCA9535_CONFIG_REG0 = 0x06, /*!< Configuration register. BIT = '1' sets port to input BIT = '0' sets port to output */
TCA9535_CONFIG_REG1 = 0x07 /*!< Configuration register. BIT = '1' sets port to input BIT = '0' sets port to output */
} tca9535_reg_t;
// Please assign this to actual pins on your board
#define I2C_MASTER_SCL_IO 9 /*!< gpio number for I2C master clock */
#define I2C_MASTER_SDA_IO 33 /*!< gpio number for I2C master data */
#define TCA9535_INT_GPIO 39
#define I2C_MASTER_NUM I2C_NUM_1 /*!< I2C port number for master dev */
#define I2C_MASTER_TX_BUF_DISABLE 0 /*!< I2C master do not need buffer */
#define I2C_MASTER_RX_BUF_DISABLE 0 /*!< I2C master do not need buffer */
#define I2C_MASTER_FREQ_HZ 100000 /*!< I2C master clock frequency */
#define WRITE_BIT I2C_MASTER_WRITE /*!< I2C master write */
#define READ_BIT I2C_MASTER_READ /*!< I2C master read */
#define ACK_CHECK_EN 0x1 /*!< I2C master will check ack from slave*/
#define ACK_CHECK_DIS 0x0 /*!< I2C master will not check ack from slave */
#define ACK_VAL 0x0 /*!< I2C ack value */
#define NACK_VAL 0x1 /*!< I2C nack value */
#define I2C_LOG_TAG "[tca9535_i2c]" /*!< LOG tag */
struct TCA9535_sBit{
uint8_t B0:1;
uint8_t B1:1;
uint8_t B2:1;
uint8_t B3:1;
uint8_t B4:1;
uint8_t B5:1;
uint8_t B6:1;
uint8_t B7:1;
};
union TCA9535_uInputPort{
uint8_t asInt; /*!< Port data as unsigned integer */
struct TCA9535_sBit bit; /*!< Port data as separate bits */
};
struct TCA9535_sRegister{
union TCA9535_uInputPort P0;
union TCA9535_uInputPort P1;
};
typedef union {
uint16_t asInt; /*!< Register data as unsigned integer */
struct TCA9535_sRegister Port; /*!< Register data as separate ports */
} TCA9535_Register;
esp_err_t TCA9535Init();
unsigned char TCA9535ReadSingleRegister(tca9535_reg_t address);
esp_err_t TCA9535WriteSingleRegister(tca9535_reg_t address, unsigned short regVal);
esp_err_t TCA9535WriteOutput(TCA9535_Register *reg);
esp_err_t TCA9535WritePolarity(TCA9535_Register *reg);
esp_err_t TCA9535WriteConfig(TCA9535_Register *reg);
esp_err_t TCA9535ReadInput(TCA9535_Register *reg);
esp_err_t TCA9535ReadOutput(TCA9535_Register *reg);
esp_err_t TCA9535ReadPolarity(TCA9535_Register *reg);
esp_err_t TCA9535ReadConfig(TCA9535_Register *reg);

View File

@ -0,0 +1,3 @@
#
# Main Makefile. This is basically the same as a component makefile.
#

View File

@ -0,0 +1,69 @@
#include <stdio.h>
#include "driver/i2c.h"
#include "esp_log.h"
#include "TCA9535.h"
void app_main()
{
// Configure TCA9535's INT output as input with pullup
gpio_pad_select_gpio(TCA9535_INT_GPIO);
gpio_set_direction(TCA9535_INT_GPIO, GPIO_MODE_INPUT);
gpio_set_pull_mode(TCA9535_INT_GPIO, GPIO_PULLUP_ONLY);
// Init i2c driver
esp_err_t ret = TCA9535Init();
if (ret == ESP_OK)
ESP_LOGI(I2C_LOG_TAG, "Initialization done");
else
ESP_LOGI(I2C_LOG_TAG, "Initialization failed");
// Configure TCA9535's PORT0 as input port
TCA9535WriteSingleRegister(TCA9535_CONFIG_REG0, 0xFF);
while (1)
{
// Example of reading single register data
uint8_t reg_data = TCA9535ReadSingleRegister(TCA9535_INPUT_REG0);
printf("INPUT0_REG_DATA = %d\n", reg_data);
reg_data = TCA9535ReadSingleRegister(TCA9535_INPUT_REG1);
printf("INPUT1_REG_DATA = %d\n", reg_data);
reg_data = TCA9535ReadSingleRegister(TCA9535_OUTPUT_REG0);
printf("OUTPUT0_REG_DATA = %d\n", reg_data);
reg_data = TCA9535ReadSingleRegister(TCA9535_OUTPUT_REG1);
printf("OUTPUT1_REG_DATA = %d\n", reg_data);
reg_data = TCA9535ReadSingleRegister(TCA9535_POLARITY_REG0);
printf("POL0_REG_DATA = %d\n", reg_data);
reg_data = TCA9535ReadSingleRegister(TCA9535_POLARITY_REG1);
printf("POL1_REG_DATA = %d\n", reg_data);
reg_data = TCA9535ReadSingleRegister(TCA9535_CONFIG_REG0);
printf("CONF0_REG_DATA = %d\n", reg_data);
reg_data = TCA9535ReadSingleRegister(TCA9535_CONFIG_REG1);
printf("CONF1_REG_DATA = %d\n", reg_data);
// Example of reading data to struct
TCA9535_Register inputRegister;
TCA9535ReadInput(&inputRegister);
printf("Input0_reg_ing = %d\n", inputRegister.Port.P0.asInt);
printf("Input0_reg_ing = %d\n", inputRegister.Port.P1.asInt);
printf("Input_P0.2 = %d\n", inputRegister.Port.P0.bit.B2);
// Example of setting register data with struct
TCA9535_Register configRegister;
// Configure TCA9535's PORT0 as input port
configRegister.Port.P0.asInt = 255;
// Configure TCA9535's PORT1 bit 3 as input port
configRegister.Port.P1.bit.B3 = 1;
// Write data to TCA9535
TCA9535WriteConfig(&configRegister);
vTaskDelay(2000 / portTICK_PERIOD_MS);
}
}