Skip to content

Commit

Permalink
Merge branch 'feat/i2c_bus_driver_selection' into 'master'
Browse files Browse the repository at this point in the history
feat: support manual selection of i2c driver in idf v5.3 and above

Closes AEG-2046

See merge request ae_group/esp-iot-solution!1151
  • Loading branch information
YanKE01 committed Nov 27, 2024
2 parents c78fce9 + 4b612a7 commit b7c99f5
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 14 deletions.
6 changes: 6 additions & 0 deletions components/i2c_bus/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
8 changes: 4 additions & 4 deletions components/i2c_bus/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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}
Expand Down
11 changes: 11 additions & 0 deletions components/i2c_bus/Kconfig
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
2 changes: 1 addition & 1 deletion components/i2c_bus/idf_component.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 6 additions & 0 deletions components/i2c_bus/include/i2c_bus.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
*/
Expand All @@ -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
Expand Down
18 changes: 9 additions & 9 deletions components/i2c_bus/test_apps/main/test_i2c_bus.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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;

Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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,
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit b7c99f5

Please sign in to comment.