From 2ca1b78b04fb93aa1caa170b340c6cbf7b38b628 Mon Sep 17 00:00:00 2001 From: H-sw123 <1150857014@qq.com> Date: Fri, 18 Oct 2024 10:27:08 +0800 Subject: [PATCH] Update CH422G and add a new example Closes https://github.com/esp-arduino-libs/ESP32_IO_Expander/pull/7 Closes https://github.com/esp-arduino-libs/ESP32_IO_Expander/issues/5 --- CHANGELOG.md | 6 + examples/TestCH422G/TestCH422G.ino | 102 +++++++++++++ examples/TestFunctions/TestFunctions.ino | 8 ++ library.properties | 4 +- src/chip/CH422G.cpp | 176 +++++++++++++++++------ src/chip/CH422G.h | 59 ++++++-- test_apps/main/test_ESP_IOExpander.cpp | 20 ++- 7 files changed, 306 insertions(+), 69 deletions(-) create mode 100644 examples/TestCH422G/TestCH422G.ino diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b88de5..6c4e4d1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # ChangeLog +## v0.0.4 - 2024-10-18 + +### Enhancements: + +* Update CH422G and add a new example to show how to use it (@H-sw123) + ## v0.0.3 - 2024-05-07 ### Enhancements: diff --git a/examples/TestCH422G/TestCH422G.ino b/examples/TestCH422G/TestCH422G.ino new file mode 100644 index 0000000..016acdf --- /dev/null +++ b/examples/TestCH422G/TestCH422G.ino @@ -0,0 +1,102 @@ +/** + * | Supported IO Expanders | CH422G | + * | ------------------------- | ------ | + * + * # CH422G Test Example + * + * The hardware device used in this example is waveshare ESP32-S3-Touch-LCD-4.3B-BOX. To test the simultaneous use of I/O input and OC output, connect DO0 to DI0, and connect DO1 to DI1. + * + * ## How to use + * + * 1. Enable USB CDC. + * 2. Verify and upload the example to your board. + * + * ## Serial Output + * + * ``` + * ... + * Test begin + * Set the OC pin to push-pull output mode. + * Set the IO0-7 pin to input mode. + * Set pint 8 and 9 to:0, 1 + * + * Read pin 0 and 5 level: 0, 1 + * + * Set pint 8 and 9 to:1, 0 + * + * Read pin 0 and 5 level: 1, 0 + * ... + * ``` + * + * ## Troubleshooting + * + * The driver initialization by default sets CH422G's IO0-7 to output high-level mode. + Since the input/output mode of CH422G's IO0-7 must remain consistent, the driver will only set IO0-7 to + input mode when it determines that all pins are configured as input. + Using pinMode and multiPinMode will be invalid. You can only set the pin working mode through enableAllIO_Input, enableAllIO_Output, enableOC_PushPull and enableOC_OpenDrain + * + */ + +#include +#include + +#define EXAMPLE_I2C_NUM (0) +#define EXAMPLE_I2C_SDA_PIN (8) +#define EXAMPLE_I2C_SCL_PIN (9) +#define EXAMPLE_I2C_ADDR (ESP_IO_EXPANDER_I2C_CH422G_ADDRESS) + +ESP_IOExpander_CH422G *expander = NULL; + +void setup() { + Serial.begin(115200); + delay(1000); + Serial.println("Test begin"); + + expander = new ESP_IOExpander_CH422G((i2c_port_t)EXAMPLE_I2C_NUM, EXAMPLE_I2C_ADDR, EXAMPLE_I2C_SCL_PIN, EXAMPLE_I2C_SDA_PIN); + expander->init(); + expander->begin(); + + /* For CH422G */ + Serial.println("Set the OC pin to push-pull output mode."); + expander->enableOC_PushPull(); + + // Serial.println("Set the OC pin to open_drain output mode."); + // expander->enableOC_OpenDrain(); + + Serial.println("Set the IO0-7 pin to input mode."); + expander->enableAllIO_Input(); + + // Serial.println("Set the IO0-7 pin to output mode."); + // expander->enableAllIO_Output(); +} + +int level[2] = { 0, 0 }; + +void loop() { + for (int i = 0; i < 100; i++) { + bool toggle = i % 2; + + Serial.print("Set pint 8 and 9 to:"); + Serial.print(toggle); + Serial.print(", "); + Serial.println(!toggle); + Serial.println(); + + // Set pin 8 and 9 level + expander->digitalWrite(8, toggle); + expander->digitalWrite(9, !toggle); + delay(1); + + // Read pin 0 and 5 level + level[0] = expander->digitalRead(0); + level[1] = expander->digitalRead(5); + + Serial.print("Read pin 0 and 5 level: "); + Serial.print(level[0]); + Serial.print(", "); + Serial.println(level[1]); + Serial.println(); + + delay(1000); + } +} diff --git a/examples/TestFunctions/TestFunctions.ino b/examples/TestFunctions/TestFunctions.ino index 39f33a5..a30791d 100644 --- a/examples/TestFunctions/TestFunctions.ino +++ b/examples/TestFunctions/TestFunctions.ino @@ -12,6 +12,8 @@ #define EXAMPLE_I2C_NUM (0) #define EXAMPLE_I2C_SDA_PIN (8) #define EXAMPLE_I2C_SCL_PIN (18) +#define EXAMPLE_I2C_ADDR (ESP_IO_EXPANDER_I2C_TCA9554_ADDRESS_000) // Modify this value according to the + // hardware address #define _EXAMPLE_CHIP_CLASS(name, ...) ESP_IOExpander_##name(__VA_ARGS__) #define EXAMPLE_CHIP_CLASS(name, ...) _EXAMPLE_CHIP_CLASS(name, ##__VA_ARGS__) @@ -29,6 +31,12 @@ void setup() expander->init(); expander->begin(); + /* For CH422G */ + // static_cast(expander)->enableOC_PushPull(); + // static_cast(expander)->enableOC_OpenDrain(); + // static_cast(expander)->enableAllIO_Input(); + // static_cast(expander)->enableAllIO_Output(); + Serial.println("Original status:"); expander->printStatus(); diff --git a/library.properties b/library.properties index 5c080fb..cc76fb7 100644 --- a/library.properties +++ b/library.properties @@ -1,6 +1,6 @@ name=ESP32_IO_Expander -version=0.0.3 -author=lzw655 +version=0.0.4 +author=espressif maintainer=espressif sentence=ESP32_IO_Expander is a library designed for driving IO expander chips using ESP32 SoCs paragraph=Currently support TCA95xx(8bit), TCA95xx(16bit), HT8574, CH422G diff --git a/src/chip/CH422G.cpp b/src/chip/CH422G.cpp index 56abfba..5a9019f 100644 --- a/src/chip/CH422G.cpp +++ b/src/chip/CH422G.cpp @@ -19,15 +19,30 @@ /* Timeout of each I2C communication */ #define I2C_TIMEOUT_MS (10) -#define IO_COUNT (8) +#define IO_COUNT (12) /* Register address */ -#define CH422G_REG_IN (0x26) -#define CH422G_REG_OUT (0x38) - -/* Default register value on power-up */ -#define DIR_REG_DEFAULT_VAL (0xff) -#define OUT_REG_DEFAULT_VAL (0xdf) +#define CH422G_REG_WR_SET (0x48 >> 1) +#define CH422G_REG_WR_OC (0x46 >> 1) +#define CH422G_REG_WR_IO (0x70 >> 1) +#define CH422G_REG_RD_IO (0x4D >> 1) + +/* Default register value when reset */ +// *INDENT-OFF* +#define REG_WR_SET_DEFAULT_VAL (0x01UL) // Bit: | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | + // | --- | --- | --- | --- | ------- | ------- | -------- | ------- | + // Value: | / | / | / | / | [SLEEP] | [OD_EN] | [A_SCAN] | [IO_OE] | + // | --- | --- | --- | --- | ------- | ------- | -------- | ------- | + // Default: | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | + +// *INDENT-OFF* +#define REG_WR_OC_DEFAULT_VAL (0x0FUL) +#define REG_WR_IO_DEFAULT_VAL (0xFFUL) +#define REG_OUT_DEFAULT_VAL ((REG_WR_OC_DEFAULT_VAL << 8) | REG_WR_IO_DEFAULT_VAL) +#define REG_DIR_DEFAULT_VAL (0xFFFUL) + +#define REG_WR_SET_BIT_IO_OE (1 << 0) +#define REG_WR_SET_BIT_OD_EN (1 << 2) /** * @brief Device Structure Type @@ -38,14 +53,22 @@ typedef struct { i2c_port_t i2c_num; uint32_t i2c_address; struct { - uint8_t direction; - uint8_t output; + uint8_t wr_set; + uint8_t wr_oc; + uint8_t wr_io; } regs; } esp_io_expander_ch422g_t; static const char *TAG = "ch422g"; static esp_err_t esp_io_expander_new_i2c_ch422g(i2c_port_t i2c_num, uint32_t i2c_address, esp_io_expander_handle_t *handle); +static esp_err_t read_input_reg(esp_io_expander_handle_t handle, uint32_t *value); +static esp_err_t write_output_reg(esp_io_expander_handle_t handle, uint32_t value); +static esp_err_t read_output_reg(esp_io_expander_handle_t handle, uint32_t *value); +static esp_err_t write_direction_reg(esp_io_expander_handle_t handle, uint32_t value); +static esp_err_t read_direction_reg(esp_io_expander_handle_t handle, uint32_t *value); +static esp_err_t reset(esp_io_expander_t *handle); +static esp_err_t del(esp_io_expander_t *handle); ESP_IOExpander_CH422G::~ESP_IOExpander_CH422G() { @@ -62,13 +85,55 @@ void ESP_IOExpander_CH422G::begin(void) CHECK_ERROR_RETURN(esp_io_expander_new_i2c_ch422g(i2c_id, i2c_address, &handle)); } -static esp_err_t read_input_reg(esp_io_expander_handle_t handle, uint32_t *value); -static esp_err_t write_output_reg(esp_io_expander_handle_t handle, uint32_t value); -static esp_err_t read_output_reg(esp_io_expander_handle_t handle, uint32_t *value); -static esp_err_t write_direction_reg(esp_io_expander_handle_t handle, uint32_t value); -static esp_err_t read_direction_reg(esp_io_expander_handle_t handle, uint32_t *value); -static esp_err_t reset(esp_io_expander_t *handle); -static esp_err_t del(esp_io_expander_t *handle); +void ESP_IOExpander_CH422G::enableOC_OpenDrain(void) +{ + esp_io_expander_ch422g_t *ch422g = (esp_io_expander_ch422g_t *)__containerof(handle, esp_io_expander_ch422g_t, base); + uint8_t data = (uint8_t)(ch422g->regs.wr_set | REG_WR_SET_BIT_OD_EN); + + // WR-SET + CHECK_ERROR_RETURN( + i2c_master_write_to_device(ch422g->i2c_num, CH422G_REG_WR_SET, &data, sizeof(data), pdMS_TO_TICKS(I2C_TIMEOUT_MS)) + ); + ch422g->regs.wr_set = data; +} + +void ESP_IOExpander_CH422G::enableOC_PushPull(void) +{ + esp_io_expander_ch422g_t *ch422g = (esp_io_expander_ch422g_t *)__containerof(handle, esp_io_expander_ch422g_t, base); + uint8_t data = (uint8_t)(ch422g->regs.wr_set & ~REG_WR_SET_BIT_OD_EN); + + // WR-SET + CHECK_ERROR_RETURN( + i2c_master_write_to_device(ch422g->i2c_num, CH422G_REG_WR_SET, &data, sizeof(data), pdMS_TO_TICKS(I2C_TIMEOUT_MS)) + ); + ch422g->regs.wr_set = data; +} + +void ESP_IOExpander_CH422G::enableAllIO_Input(void) +{ + esp_io_expander_ch422g_t *ch422g = (esp_io_expander_ch422g_t *)__containerof(handle, esp_io_expander_ch422g_t, base); + uint8_t data = (uint8_t)(ch422g->regs.wr_set & ~REG_WR_SET_BIT_IO_OE); + + // WR-SET + CHECK_ERROR_RETURN( + i2c_master_write_to_device(ch422g->i2c_num, CH422G_REG_WR_SET, &data, sizeof(data), pdMS_TO_TICKS(I2C_TIMEOUT_MS)) + ); + ch422g->regs.wr_set = data; + // Delay 1ms to wait for the IO expander to switch to input mode + vTaskDelay(pdMS_TO_TICKS(2)); +} + +void ESP_IOExpander_CH422G::enableAllIO_Output(void) +{ + esp_io_expander_ch422g_t *ch422g = (esp_io_expander_ch422g_t *)__containerof(handle, esp_io_expander_ch422g_t, base); + uint8_t data = (uint8_t)(ch422g->regs.wr_set | REG_WR_SET_BIT_IO_OE); + + // WR-SET + CHECK_ERROR_RETURN( + i2c_master_write_to_device(ch422g->i2c_num, CH422G_REG_WR_SET, &data, sizeof(data), pdMS_TO_TICKS(I2C_TIMEOUT_MS)) + ); + ch422g->regs.wr_set = data; +} static esp_err_t esp_io_expander_new_i2c_ch422g(i2c_port_t i2c_num, uint32_t i2c_address, esp_io_expander_handle_t *handle) { @@ -79,10 +144,11 @@ static esp_err_t esp_io_expander_new_i2c_ch422g(i2c_port_t i2c_num, uint32_t i2c ESP_RETURN_ON_FALSE(ch422g, ESP_ERR_NO_MEM, TAG, "Malloc failed"); ch422g->base.config.io_count = IO_COUNT; - ch422g->base.config.flags.dir_out_bit_zero = 1; ch422g->i2c_num = i2c_num; ch422g->i2c_address = i2c_address; - ch422g->regs.output = OUT_REG_DEFAULT_VAL; + ch422g->regs.wr_set = REG_WR_SET_DEFAULT_VAL; + ch422g->regs.wr_oc = REG_WR_OC_DEFAULT_VAL; + ch422g->regs.wr_io = REG_WR_IO_DEFAULT_VAL; ch422g->base.read_input_reg = read_input_reg; ch422g->base.write_output_reg = write_output_reg; ch422g->base.read_output_reg = read_output_reg; @@ -105,37 +171,42 @@ static esp_err_t esp_io_expander_new_i2c_ch422g(i2c_port_t i2c_num, uint32_t i2c static esp_err_t read_input_reg(esp_io_expander_handle_t handle, uint32_t *value) { esp_io_expander_ch422g_t *ch422g = (esp_io_expander_ch422g_t *)__containerof(handle, esp_io_expander_ch422g_t, base); - uint8_t temp = 0; ESP_RETURN_ON_ERROR( - i2c_master_read_from_device(ch422g->i2c_num, ch422g->i2c_address, &temp, 1, pdMS_TO_TICKS(I2C_TIMEOUT_MS)), - TAG, "Read input reg failed"); - - // *INDENT-OFF* - ESP_RETURN_ON_ERROR( - i2c_master_read_from_device(ch422g->i2c_num, CH422G_REG_IN, &temp, 1, pdMS_TO_TICKS(I2C_TIMEOUT_MS)), - TAG, "Read input reg failed"); - // *INDENT-ON* + i2c_master_read_from_device(ch422g->i2c_num, CH422G_REG_RD_IO, &temp, 1, pdMS_TO_TICKS(I2C_TIMEOUT_MS)), + TAG, "Read RD-IO reg failed" + ); *value = temp; + return ESP_OK; } static esp_err_t write_output_reg(esp_io_expander_handle_t handle, uint32_t value) { esp_io_expander_ch422g_t *ch422g = (esp_io_expander_ch422g_t *)__containerof(handle, esp_io_expander_ch422g_t, base); - value &= 0xff; - uint8_t out_temp = 0x01; - ESP_RETURN_ON_ERROR( - i2c_master_write_to_device(ch422g->i2c_num, ch422g->i2c_address, &out_temp, 1, pdMS_TO_TICKS(I2C_TIMEOUT_MS)), - TAG, "Write output reg failed"); + uint8_t wr_oc_data = (value & 0xF00) >> 8; + uint8_t wr_io_data = value & 0xFF; + + // WR-OC + if (wr_oc_data) { + ESP_RETURN_ON_ERROR( + i2c_master_write_to_device(ch422g->i2c_num, CH422G_REG_WR_OC, &wr_oc_data, sizeof(wr_oc_data), pdMS_TO_TICKS(I2C_TIMEOUT_MS)), + TAG, "Write WR-OC reg failed" + ); + ch422g->regs.wr_oc = wr_oc_data; + } + + // WR-IO + if (wr_io_data) { + ESP_RETURN_ON_ERROR( + i2c_master_write_to_device(ch422g->i2c_num, CH422G_REG_WR_IO, &wr_io_data, sizeof(wr_io_data), pdMS_TO_TICKS(I2C_TIMEOUT_MS)), + TAG, "Write WR-IO reg failed" + ); + ch422g->regs.wr_io = wr_io_data; + } - uint8_t data = (uint8_t)value; - ESP_RETURN_ON_ERROR( - i2c_master_write_to_device(ch422g->i2c_num, CH422G_REG_OUT, &data, 1, pdMS_TO_TICKS(I2C_TIMEOUT_MS)), - TAG, "Write output reg failed"); - ch422g->regs.output = value; return ESP_OK; } @@ -143,29 +214,50 @@ static esp_err_t read_output_reg(esp_io_expander_handle_t handle, uint32_t *valu { esp_io_expander_ch422g_t *ch422g = (esp_io_expander_ch422g_t *)__containerof(handle, esp_io_expander_ch422g_t, base); - *value = ch422g->regs.output; + *value = ch422g->regs.wr_io | (((uint32_t)ch422g->regs.wr_oc) << 8); + return ESP_OK; } static esp_err_t write_direction_reg(esp_io_expander_handle_t handle, uint32_t value) { esp_io_expander_ch422g_t *ch422g = (esp_io_expander_ch422g_t *)__containerof(handle, esp_io_expander_ch422g_t, base); - value &= 0xff; - ch422g->regs.direction = value; + uint8_t data = ch422g->regs.wr_set; + + value &= 0xFF; + if (value != 0) { + data |= REG_WR_SET_BIT_IO_OE; + } else { + data &= ~REG_WR_SET_BIT_IO_OE; + } + + // WR-SET + ESP_RETURN_ON_ERROR( + i2c_master_write_to_device(ch422g->i2c_num, CH422G_REG_WR_SET, &data, sizeof(data), pdMS_TO_TICKS(I2C_TIMEOUT_MS)), + TAG, "Write WR_SET reg failed" + ); + ch422g->regs.wr_set = data; + return ESP_OK; } +#define DIR_OUT_VALUE (0xFFF) +#define DIR_IN_VALUE (0xF00) + static esp_err_t read_direction_reg(esp_io_expander_handle_t handle, uint32_t *value) { esp_io_expander_ch422g_t *ch422g = (esp_io_expander_ch422g_t *)__containerof(handle, esp_io_expander_ch422g_t, base); - *value = ch422g->regs.direction; + *value = (ch422g->regs.wr_set & REG_WR_SET_BIT_IO_OE) ? DIR_OUT_VALUE : DIR_IN_VALUE; + return ESP_OK; } static esp_err_t reset(esp_io_expander_t *handle) { - ESP_RETURN_ON_ERROR(write_output_reg(handle, OUT_REG_DEFAULT_VAL), TAG, "Write output reg failed"); + ESP_RETURN_ON_ERROR(write_direction_reg(handle, REG_DIR_DEFAULT_VAL), TAG, "Write direction reg (WR_SET) failed"); + ESP_RETURN_ON_ERROR(write_output_reg(handle, REG_OUT_DEFAULT_VAL), TAG, "Write output reg (WR_OC & WR_IO) failed"); + return ESP_OK; } diff --git a/src/chip/CH422G.h b/src/chip/CH422G.h index 1153009..114d909 100644 --- a/src/chip/CH422G.h +++ b/src/chip/CH422G.h @@ -13,6 +13,13 @@ #include "../ESP_IOExpander.h" +/** + * Pin mapping: + * + * | Pin Number | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | + * | ------------ | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | + * | Function | IO0 | IO1 | IO2 | IO3 | IO4 | IO5 | IO6 | IO7 | OC0 | OC1 | OC2 | OC3 | + */ class ESP_IOExpander_CH422G: public ESP_IOExpander { public: /** @@ -21,11 +28,10 @@ class ESP_IOExpander_CH422G: public ESP_IOExpander { * @note After using this function, call `init()` will initialize I2C bus. * * @param id I2C port number - * @param address I2C device address. Should be like `ESP_IO_EXPANDER_I2C_*`. - * Can be found in the header file of each IO expander.h. + * @param address I2C device address. Just to keep the same with other IO expanders, but it is ignored. * @param config Pointer to I2C bus configuration */ - ESP_IOExpander_CH422G(i2c_port_t id, uint8_t address, const i2c_config_t *config): ESP_IOExpander(id, address, config) { }; + ESP_IOExpander_CH422G(i2c_port_t id, uint8_t address, const i2c_config_t *config): ESP_IOExpander(id, 0xFF, config) { }; /** * @brief Constructor to create ESP_IOExpander object @@ -33,12 +39,11 @@ class ESP_IOExpander_CH422G: public ESP_IOExpander { * @note After using this function, call `init()` will initialize I2C bus. * * @param id I2C port number - * @param address I2C device address. Should be like `ESP_IO_EXPANDER_I2C_*`. - * Can be found in the header file of each IO expander.h. + * @param address I2C device address. Just to keep the same with other IO expanders, but it is ignored. * @param scl SCL pin number * @param sda SDA pin number */ - ESP_IOExpander_CH422G(i2c_port_t id, uint8_t address, int scl, int sda): ESP_IOExpander(id, address, scl, sda) { }; + ESP_IOExpander_CH422G(i2c_port_t id, uint8_t address, int scl, int sda): ESP_IOExpander(id, 0xFF, scl, sda) { }; /** * @brief Constructor to create ESP_IOExpander object @@ -46,10 +51,9 @@ class ESP_IOExpander_CH422G: public ESP_IOExpander { * @note If use this function, should initialize I2C bus before call `init()`. * * @param id I2C port number - * @param address I2C device address. Should be like `ESP_IO_EXPANDER_I2C_*`. - * Can be found in the header file of each IO expander.h. + * @param address I2C device address. Just to keep the same with other IO expanders, but it is ignored. */ - ESP_IOExpander_CH422G(i2c_port_t id, uint8_t address): ESP_IOExpander(id, address) { }; + ESP_IOExpander_CH422G(i2c_port_t id, uint8_t address): ESP_IOExpander(id, 0xFF) { }; /** * @brief Destructor @@ -61,15 +65,42 @@ class ESP_IOExpander_CH422G: public ESP_IOExpander { /** * @brief Begin IO expander * + * @note The driver initialization by default sets CH422G's IO0-7 to output high-level mode. + * */ void begin(void) override; + + /** + * @brief Enable OC0-OC3 output open-drain + * + */ + void enableOC_OpenDrain(void); + + /** + * @brief Enable OC0-OC3 output push-pull (default mode when power-on) + * + */ + void enableOC_PushPull(void); + + /** + * @brief Enable IO0-7 input mode + * + * @note The driver initialization by default sets CH422G's IO0-7 to output high-level mode. + * @note Since the input/output mode of CH422G's IO0-7 must remain consistent, the driver will only set IO0-7 to + * input mode when it determines that all pins are configured as input. + * + */ + void enableAllIO_Input(void); + + /** + * @brief Enable IO0-7 output mode + * + */ + void enableAllIO_Output(void); }; /** - * @brief I2C address of the ch422g + * @brief I2C address of the ch422g. Just to keep the same with other IO expanders, but it is ignored. * - * And the 7-bit slave address is the most important data for users. - * For example, if a chip's A0,A1,A2 are connected to GND, it's 7-bit slave address is 1001000b(0x48). - * Then users can use `ESP_IO_EXPANDER_I2C_CH422G_ADDRESS_000` to init it. */ -#define ESP_IO_EXPANDER_I2C_CH422G_ADDRESS_000 (0x24) +#define ESP_IO_EXPANDER_I2C_CH422G_ADDRESS (0x24) diff --git a/test_apps/main/test_ESP_IOExpander.cpp b/test_apps/main/test_ESP_IOExpander.cpp index 5d9d87a..9a1b0fa 100644 --- a/test_apps/main/test_ESP_IOExpander.cpp +++ b/test_apps/main/test_ESP_IOExpander.cpp @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -16,19 +16,17 @@ #include "ESP_IOExpander_Library.h" -// Refer to `esp32-hal-gpio.h` -#define INPUT 0x01 -#define OUTPUT 0x03 -#define LOW 0x0 -#define HIGH 0x1 - static const char *TAG = "ESP_IOxpander_test"; +#define CHIP_NAME TCA95xx_8bit #define I2C_HOST (I2C_NUM_0) #define I2C_SDA_PIN (8) #define I2C_SCL_PIN (18) -TEST_CASE("test ESP IO expander for TCA9554", "[tca9554]") +#define _EXAMPLE_CHIP_CLASS(name, ...) ESP_IOExpander_##name(__VA_ARGS__) +#define EXAMPLE_CHIP_CLASS(name, ...) _EXAMPLE_CHIP_CLASS(name, ##__VA_ARGS__) + +TEST_CASE("test ESP IO expander functions", "[io_expander]") { ESP_IOExpander *expander = NULL; const i2c_config_t i2c_config = EXPANDER_I2C_CONFIG_DEFAULT(I2C_SCL_PIN, I2C_SDA_PIN); @@ -36,7 +34,7 @@ TEST_CASE("test ESP IO expander for TCA9554", "[tca9554]") ESP_LOGI(TAG, "Test initialization with external I2C"); TEST_ASSERT_EQUAL(i2c_param_config(I2C_HOST, &i2c_config), ESP_OK); TEST_ASSERT_EQUAL(i2c_driver_install(I2C_HOST, i2c_config.mode, 0, 0, 0), ESP_OK); - expander = new ESP_IOExpander_TCA95xx_8bit(I2C_HOST, ESP_IO_EXPANDER_I2C_TCA9554_ADDRESS_000); + expander = new EXAMPLE_CHIP_CLASS(CHIP_NAME, I2C_HOST, ESP_IO_EXPANDER_I2C_TCA9554_ADDRESS_000); expander->init(); expander->begin(); expander->reset(); @@ -45,7 +43,7 @@ TEST_CASE("test ESP IO expander for TCA9554", "[tca9554]") i2c_driver_delete(I2C_HOST); ESP_LOGI(TAG, "Test initialization with internal I2C (with config)"); - expander = new ESP_IOExpander_TCA95xx_8bit(I2C_HOST, ESP_IO_EXPANDER_I2C_TCA9554_ADDRESS_000, &i2c_config); + expander = new EXAMPLE_CHIP_CLASS(CHIP_NAME, I2C_HOST, ESP_IO_EXPANDER_I2C_TCA9554_ADDRESS_000, &i2c_config); expander->init(); expander->begin(); expander->reset(); @@ -53,7 +51,7 @@ TEST_CASE("test ESP IO expander for TCA9554", "[tca9554]") delete expander; ESP_LOGI(TAG, "Test initialization with internal I2C (without config)"); - expander = new ESP_IOExpander_TCA95xx_8bit(I2C_HOST, ESP_IO_EXPANDER_I2C_TCA9554_ADDRESS_000, I2C_SCL_PIN, I2C_SDA_PIN); + expander = new EXAMPLE_CHIP_CLASS(CHIP_NAME, I2C_HOST, ESP_IO_EXPANDER_I2C_TCA9554_ADDRESS_000, I2C_SCL_PIN, I2C_SDA_PIN); expander->init(); expander->begin(); expander->reset(); pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy