diff --git a/components/i2c_bus/CHANGELOG.md b/components/i2c_bus/CHANGELOG.md index 2a467ec9f..74bce7e53 100644 --- a/components/i2c_bus/CHANGELOG.md +++ b/components/i2c_bus/CHANGELOG.md @@ -1,5 +1,11 @@ # ChangeLog +## v1.1.0 - 2024-11-22 + +### Enhancements: + +- Support manual selection of ``driver/i2c`` or ``esp_driver_i2c`` in idf v5.3 and above. + ## v1.0.0 - 2024-9-19 ### Enhancements: diff --git a/components/i2c_bus/CMakeLists.txt b/components/i2c_bus/CMakeLists.txt index f2395edc2..b1bdcb438 100644 --- a/components/i2c_bus/CMakeLists.txt +++ b/components/i2c_bus/CMakeLists.txt @@ -1,9 +1,9 @@ -if("${IDF_VERSION_MAJOR}.${IDF_VERSION_MINOR}" VERSION_GREATER_EQUAL "5.3") - set(SRC_FILE "i2c_bus_v2.c") - set(REQ esp_driver_i2c) -else() +if("${IDF_VERSION_MAJOR}.${IDF_VERSION_MINOR}" VERSION_LESS "5.3" OR CONFIG_I2C_BUS_BACKWARD_CONFIG) set(SRC_FILE "i2c_bus.c") set(REQ driver) +else() + set(SRC_FILE "i2c_bus_v2.c") + set(REQ esp_driver_i2c driver) endif() idf_component_register(SRCS ${SRC_FILE} diff --git a/components/i2c_bus/Kconfig b/components/i2c_bus/Kconfig index 402c21741..2ca16b2d0 100644 --- a/components/i2c_bus/Kconfig +++ b/components/i2c_bus/Kconfig @@ -1,6 +1,10 @@ menu "Bus Options" menu "I2C Bus Options" + config ESP_IDF_VERSION + string + option env="ESP_IDF_VERSION" + config I2C_BUS_DYNAMIC_CONFIG bool "enable dynamic configuration" default y @@ -14,6 +18,13 @@ menu "Bus Options" range 50 5000 help task block time when try to take the bus, unit:milliseconds + + config I2C_BUS_BACKWARD_CONFIG + bool "Enable backward compatibility for the I2C driver (force use of the old i2c_driver above v5.3)" + default n + depends on ESP_IDF_VERSION >= 5.3 + help + Enable this option for backward compatibility with the old I2C driver endmenu endmenu diff --git a/components/i2c_bus/idf_component.yml b/components/i2c_bus/idf_component.yml index 8ff55f91d..91c9e2887 100644 --- a/components/i2c_bus/idf_component.yml +++ b/components/i2c_bus/idf_component.yml @@ -1,4 +1,4 @@ -version: "1.0.0" +version: "1.1.0" description: I2C bus driver url: https://github.com/espressif/esp-iot-solution/tree/master/components/i2c_bus repository: https://github.com/espressif/esp-iot-solution.git diff --git a/components/i2c_bus/include/i2c_bus.h b/components/i2c_bus/include/i2c_bus.h index 77dde6f39..8a80f7e9b 100644 --- a/components/i2c_bus/include/i2c_bus.h +++ b/components/i2c_bus/include/i2c_bus.h @@ -10,7 +10,11 @@ #include "esp_idf_version.h" #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0) +#if CONFIG_I2C_BUS_BACKWARD_CONFIG +#include "driver/i2c.h" +#else #include "driver/i2c_master.h" +#endif #else #include "driver/i2c.h" #endif @@ -32,6 +36,7 @@ extern "C" #endif #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0) +#if !CONFIG_I2C_BUS_BACKWARD_CONFIG /** * @brief I2C initialization parameters */ @@ -46,6 +51,7 @@ typedef struct { } master; /*!< I2C master config */ uint32_t clk_flags; /*!< Bitwise of ``I2C_SCLK_SRC_FLAG_**FOR_DFS**`` for clk source choice*/ } i2c_config_t; +#endif typedef void *i2c_cmd_handle_t; /*!< I2C command handle */ #endif diff --git a/components/i2c_bus/test_apps/main/test_i2c_bus.c b/components/i2c_bus/test_apps/main/test_i2c_bus.c index f5161b980..dbf12847b 100644 --- a/components/i2c_bus/test_apps/main/test_i2c_bus.c +++ b/components/i2c_bus/test_apps/main/test_i2c_bus.c @@ -41,7 +41,7 @@ static size_t before_free_32bit; #define ESP_SLAVE_ADDR 0x28 /*!< ESP32 slave address, you can set any 7bit value */ -#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0) +#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0) && !CONFIG_I2C_BUS_BACKWARD_CONFIG static QueueHandle_t s_receive_queue; static IRAM_ATTR bool test_i2c_rx_done_callback(i2c_slave_dev_handle_t channel, const i2c_slave_rx_done_event_data_t *edata, void *user_data) @@ -134,7 +134,7 @@ static void i2c_master_write_test(void) i2c_bus_device_handle_t i2c_device1 = i2c_bus_device_create(i2c0_bus, ESP_SLAVE_ADDR, 0); TEST_ASSERT(i2c_device1 != NULL); -#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0) +#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0) && !CONFIG_I2C_BUS_BACKWARD_CONFIG unity_wait_for_signal("i2c slave init finish"); unity_send_signal("master write"); #endif @@ -146,7 +146,7 @@ static void i2c_master_write_test(void) i2c_bus_write_bytes(i2c_device1, NULL_I2C_MEM_ADDR, DATA_LENGTH / 2, data_wr); disp_buf(data_wr, i); free(data_wr); -#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0) +#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0) && !CONFIG_I2C_BUS_BACKWARD_CONFIG unity_wait_for_signal("ready to delete"); #endif i2c_bus_device_delete(&i2c_device1); @@ -159,7 +159,7 @@ static void i2c_slave_read_test(void) { uint8_t *data_rd = (uint8_t *) malloc(DATA_LENGTH); -#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 3, 0) +#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 3, 0) || CONFIG_I2C_BUS_BACKWARD_CONFIG int len = 0; int size_rd = 0; @@ -250,7 +250,7 @@ static void master_read_slave_test(void) i2c_bus_device_handle_t i2c_device1 = i2c_bus_device_create(i2c0_bus, ESP_SLAVE_ADDR, 0); TEST_ASSERT(i2c_device1 != NULL); -#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0) +#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0) && !CONFIG_I2C_BUS_BACKWARD_CONFIG unity_send_signal("i2c master init finish"); unity_wait_for_signal("slave write"); #endif @@ -259,7 +259,7 @@ static void master_read_slave_test(void) vTaskDelay(100 / portTICK_RATE_MS); disp_buf(data_rd, RW_TEST_LENGTH); -#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0) +#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0) && !CONFIG_I2C_BUS_BACKWARD_CONFIG unity_send_signal("ready to delete"); #endif i2c_bus_device_delete(&i2c_device1); @@ -273,7 +273,7 @@ static void slave_write_buffer_test(void) { uint8_t *data_wr = (uint8_t *) malloc(RW_TEST_LENGTH); -#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 3, 0) +#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 3, 0) || CONFIG_I2C_BUS_BACKWARD_CONFIG i2c_config_t conf_slave = { .mode = I2C_MODE_SLAVE, .sda_io_num = I2C_SLAVE_SDA_IO, @@ -320,7 +320,7 @@ static void slave_write_buffer_test(void) disp_buf(data_wr, RW_TEST_LENGTH); free(data_wr); -#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 3, 0) +#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 3, 0) || CONFIG_I2C_BUS_BACKWARD_CONFIG i2c_driver_delete(I2C_SLAVE_NUM); #else unity_wait_for_signal("ready to delete"); @@ -414,7 +414,7 @@ void tearDown(void) void app_main(void) { -#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 3, 0) +#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 3, 0) || CONFIG_I2C_BUS_BACKWARD_CONFIG printf("I2C BUS TEST \n"); #else printf("I2C BUS V2 TEST \n");