Skip to content

Commit 4b612a7

Browse files
committed
feat: support manual selection of i2c driver in idf v5.3 and above
1 parent 224d004 commit 4b612a7

File tree

6 files changed

+37
-14
lines changed

6 files changed

+37
-14
lines changed

components/i2c_bus/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# ChangeLog
22

3+
## v1.1.0 - 2024-11-22
4+
5+
### Enhancements:
6+
7+
- Support manual selection of ``driver/i2c`` or ``esp_driver_i2c`` in idf v5.3 and above.
8+
39
## v1.0.0 - 2024-9-19
410

511
### Enhancements:

components/i2c_bus/CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
if("${IDF_VERSION_MAJOR}.${IDF_VERSION_MINOR}" VERSION_GREATER_EQUAL "5.3")
2-
set(SRC_FILE "i2c_bus_v2.c")
3-
set(REQ esp_driver_i2c)
4-
else()
1+
if("${IDF_VERSION_MAJOR}.${IDF_VERSION_MINOR}" VERSION_LESS "5.3" OR CONFIG_I2C_BUS_BACKWARD_CONFIG)
52
set(SRC_FILE "i2c_bus.c")
63
set(REQ driver)
4+
else()
5+
set(SRC_FILE "i2c_bus_v2.c")
6+
set(REQ esp_driver_i2c driver)
77
endif()
88

99
idf_component_register(SRCS ${SRC_FILE}

components/i2c_bus/Kconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
menu "Bus Options"
22

33
menu "I2C Bus Options"
4+
config ESP_IDF_VERSION
5+
string
6+
option env="ESP_IDF_VERSION"
7+
48
config I2C_BUS_DYNAMIC_CONFIG
59
bool "enable dynamic configuration"
610
default y
@@ -14,6 +18,13 @@ menu "Bus Options"
1418
range 50 5000
1519
help
1620
task block time when try to take the bus, unit:milliseconds
21+
22+
config I2C_BUS_BACKWARD_CONFIG
23+
bool "Enable backward compatibility for the I2C driver (force use of the old i2c_driver above v5.3)"
24+
default n
25+
depends on ESP_IDF_VERSION >= 5.3
26+
help
27+
Enable this option for backward compatibility with the old I2C driver
1728
endmenu
1829

1930
endmenu

components/i2c_bus/idf_component.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: "1.0.0"
1+
version: "1.1.0"
22
description: I2C bus driver
33
url: https://github.com/espressif/esp-iot-solution/tree/master/components/i2c_bus
44
repository: https://github.com/espressif/esp-iot-solution.git

components/i2c_bus/include/i2c_bus.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
#include "esp_idf_version.h"
1111

1212
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0)
13+
#if CONFIG_I2C_BUS_BACKWARD_CONFIG
14+
#include "driver/i2c.h"
15+
#else
1316
#include "driver/i2c_master.h"
17+
#endif
1418
#else
1519
#include "driver/i2c.h"
1620
#endif
@@ -32,6 +36,7 @@ extern "C"
3236
#endif
3337

3438
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0)
39+
#if !CONFIG_I2C_BUS_BACKWARD_CONFIG
3540
/**
3641
* @brief I2C initialization parameters
3742
*/
@@ -46,6 +51,7 @@ typedef struct {
4651
} master; /*!< I2C master config */
4752
uint32_t clk_flags; /*!< Bitwise of ``I2C_SCLK_SRC_FLAG_**FOR_DFS**`` for clk source choice*/
4853
} i2c_config_t;
54+
#endif
4955

5056
typedef void *i2c_cmd_handle_t; /*!< I2C command handle */
5157
#endif

components/i2c_bus/test_apps/main/test_i2c_bus.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ static size_t before_free_32bit;
4141

4242
#define ESP_SLAVE_ADDR 0x28 /*!< ESP32 slave address, you can set any 7bit value */
4343

44-
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0)
44+
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0) && !CONFIG_I2C_BUS_BACKWARD_CONFIG
4545
static QueueHandle_t s_receive_queue;
4646

4747
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)
134134
i2c_bus_device_handle_t i2c_device1 = i2c_bus_device_create(i2c0_bus, ESP_SLAVE_ADDR, 0);
135135
TEST_ASSERT(i2c_device1 != NULL);
136136

137-
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0)
137+
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0) && !CONFIG_I2C_BUS_BACKWARD_CONFIG
138138
unity_wait_for_signal("i2c slave init finish");
139139
unity_send_signal("master write");
140140
#endif
@@ -146,7 +146,7 @@ static void i2c_master_write_test(void)
146146
i2c_bus_write_bytes(i2c_device1, NULL_I2C_MEM_ADDR, DATA_LENGTH / 2, data_wr);
147147
disp_buf(data_wr, i);
148148
free(data_wr);
149-
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0)
149+
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0) && !CONFIG_I2C_BUS_BACKWARD_CONFIG
150150
unity_wait_for_signal("ready to delete");
151151
#endif
152152
i2c_bus_device_delete(&i2c_device1);
@@ -159,7 +159,7 @@ static void i2c_slave_read_test(void)
159159
{
160160
uint8_t *data_rd = (uint8_t *) malloc(DATA_LENGTH);
161161

162-
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 3, 0)
162+
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 3, 0) || CONFIG_I2C_BUS_BACKWARD_CONFIG
163163
int len = 0;
164164
int size_rd = 0;
165165

@@ -250,7 +250,7 @@ static void master_read_slave_test(void)
250250
i2c_bus_device_handle_t i2c_device1 = i2c_bus_device_create(i2c0_bus, ESP_SLAVE_ADDR, 0);
251251
TEST_ASSERT(i2c_device1 != NULL);
252252

253-
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0)
253+
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0) && !CONFIG_I2C_BUS_BACKWARD_CONFIG
254254
unity_send_signal("i2c master init finish");
255255
unity_wait_for_signal("slave write");
256256
#endif
@@ -259,7 +259,7 @@ static void master_read_slave_test(void)
259259
vTaskDelay(100 / portTICK_RATE_MS);
260260

261261
disp_buf(data_rd, RW_TEST_LENGTH);
262-
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0)
262+
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0) && !CONFIG_I2C_BUS_BACKWARD_CONFIG
263263
unity_send_signal("ready to delete");
264264
#endif
265265
i2c_bus_device_delete(&i2c_device1);
@@ -273,7 +273,7 @@ static void slave_write_buffer_test(void)
273273
{
274274
uint8_t *data_wr = (uint8_t *) malloc(RW_TEST_LENGTH);
275275

276-
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 3, 0)
276+
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 3, 0) || CONFIG_I2C_BUS_BACKWARD_CONFIG
277277
i2c_config_t conf_slave = {
278278
.mode = I2C_MODE_SLAVE,
279279
.sda_io_num = I2C_SLAVE_SDA_IO,
@@ -320,7 +320,7 @@ static void slave_write_buffer_test(void)
320320
disp_buf(data_wr, RW_TEST_LENGTH);
321321
free(data_wr);
322322

323-
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 3, 0)
323+
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 3, 0) || CONFIG_I2C_BUS_BACKWARD_CONFIG
324324
i2c_driver_delete(I2C_SLAVE_NUM);
325325
#else
326326
unity_wait_for_signal("ready to delete");
@@ -414,7 +414,7 @@ void tearDown(void)
414414

415415
void app_main(void)
416416
{
417-
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 3, 0)
417+
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 3, 0) || CONFIG_I2C_BUS_BACKWARD_CONFIG
418418
printf("I2C BUS TEST \n");
419419
#else
420420
printf("I2C BUS V2 TEST \n");

0 commit comments

Comments
 (0)