Skip to content

Commit f298eac

Browse files
committed
Merge branch 'feat/i2c_bus_support_bus_handle' into 'master'
feat(i2c_bus): support the bus_handle provided by esp_driver_i2c See merge request ae_group/esp-iot-solution!1224
2 parents 252d34d + 0bee4e6 commit f298eac

File tree

5 files changed

+79
-1
lines changed

5 files changed

+79
-1
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.3.0 - 2025-2-13
4+
5+
### Enhancements:
6+
7+
- ``i2c_bus_v2`` supports initialization using the bus_handle provided by ``esp_driver_i2c``, and also supports returning the internal bus_handle of ``esp_driver_i2c``.
8+
39
## v1.2.0 - 2025-1-14
410

511
### Enhancements:

components/i2c_bus/i2c_bus_v2.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,18 @@ i2c_bus_handle_t i2c_bus_create(i2c_port_t port, const i2c_config_t *conf)
100100
I2C_BUS_CHECK(conf != NULL, "pointer = NULL error", NULL);
101101
I2C_BUS_CHECK(conf->mode == I2C_MODE_MASTER, "i2c_bus only supports master mode", NULL);
102102

103+
if (i2c_master_get_bus_handle(port, &s_i2c_bus[port].bus_handle) == ESP_OK) {
104+
s_i2c_bus[port].is_init = true;
105+
s_i2c_bus[port].conf_activate = *conf;
106+
s_i2c_bus[port].bus_config.i2c_port = port;
107+
s_i2c_bus[port].device_config.scl_speed_hz = conf->master.clk_speed;
108+
s_i2c_bus[port].mutex = xSemaphoreCreateMutex();
109+
I2C_BUS_CHECK(s_i2c_bus[port].mutex != NULL, "i2c_bus xSemaphoreCreateMutex failed", NULL);
110+
s_i2c_bus[port].ref_counter = 0;
111+
ESP_LOGI(TAG, "I2C Bus V2 uses the externally initialized bus handle");
112+
return (i2c_bus_handle_t)&s_i2c_bus[port];
113+
}
114+
103115
if (s_i2c_bus[port].is_init) {
104116
// if i2c_bus has been inited and configs not changed, return the handle directly
105117
if (i2c_config_compare(port, conf)) {
@@ -141,6 +153,14 @@ esp_err_t i2c_bus_delete(i2c_bus_handle_t *p_bus)
141153
return ESP_OK;
142154
}
143155

156+
i2c_master_bus_handle_t i2c_bus_get_internal_bus_handle(i2c_bus_handle_t bus_handle)
157+
{
158+
I2C_BUS_CHECK(bus_handle != NULL, "Null Bus Handle", NULL);
159+
i2c_bus_t *i2c_bus = (i2c_bus_t *)bus_handle;
160+
I2C_BUS_INIT_CHECK(i2c_bus->is_init, NULL);
161+
return i2c_bus->bus_handle;
162+
}
163+
144164
uint32_t i2c_bus_get_current_clk_speed(i2c_bus_handle_t bus_handle)
145165
{
146166
I2C_BUS_CHECK(bus_handle != NULL, "Null Bus Handle", 0);

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.2.0"
1+
version: "1.3.0"
22
description: The I2C Bus Driver supports both hardware and software I2C.
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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,18 @@ i2c_bus_handle_t i2c_bus_create(i2c_port_t port, const i2c_config_t *conf);
9898
*/
9999
esp_err_t i2c_bus_delete(i2c_bus_handle_t *p_bus_handle);
100100

101+
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0)
102+
#if !CONFIG_I2C_BUS_BACKWARD_CONFIG
103+
/**
104+
* @brief Get internal idf bus_handle from i2c_bus_handle
105+
*
106+
* @param bus_handle I2C bus handle
107+
* @return i2c_master_bus_handle_t Return the idf bus_handle if obtained successfully, return NULL if failed.
108+
*/
109+
i2c_master_bus_handle_t i2c_bus_get_internal_bus_handle(i2c_bus_handle_t bus_handle);
110+
#endif
111+
#endif
112+
101113
/**
102114
* @brief Scan i2c devices attached on i2c bus
103115
*

components/i2c_bus/test_apps/main/test_i2c_bus.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,46 @@ TEST_CASE("i2c bus init-deinit test", "[bus][i2c_bus]")
372372
i2c_bus_device_add_test();
373373
}
374374

375+
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0) && !CONFIG_I2C_BUS_BACKWARD_CONFIG
376+
TEST_CASE("I2C bus uses external bus handle test", "[bus][i2c_bus]")
377+
{
378+
uint8_t *data_wr = (uint8_t *) malloc(RW_TEST_LENGTH);
379+
for (int i = 0; i < RW_TEST_LENGTH; i++) {
380+
data_wr[i] = i;
381+
}
382+
383+
i2c_master_bus_config_t i2c_mst_config = {
384+
.clk_source = I2C_CLK_SRC_DEFAULT,
385+
.i2c_port = I2C_MASTER_NUM,
386+
.scl_io_num = I2C_MASTER_SCL_IO,
387+
.sda_io_num = I2C_MASTER_SDA_IO,
388+
.flags.enable_internal_pullup = true,
389+
};
390+
i2c_master_bus_handle_t bus_handle;
391+
392+
TEST_ESP_OK(i2c_new_master_bus(&i2c_mst_config, &bus_handle));
393+
394+
i2c_config_t conf = {
395+
.mode = I2C_MODE_MASTER,
396+
.sda_io_num = I2C_MASTER_SDA_IO,
397+
.sda_pullup_en = GPIO_PULLUP_ENABLE,
398+
.scl_io_num = I2C_MASTER_SCL_IO,
399+
.scl_pullup_en = GPIO_PULLUP_ENABLE,
400+
.master.clk_speed = I2C_MASTER_FREQ_HZ,
401+
};
402+
403+
i2c_bus_handle_t i2c_bus = i2c_bus_create(I2C_MASTER_NUM, &conf);
404+
405+
i2c_bus_device_handle_t i2c_device1 = i2c_bus_device_create(i2c_bus, 0x01, 400000);
406+
TEST_ASSERT(i2c_device1 != NULL);
407+
i2c_bus_write_bytes(i2c_device1, NULL_I2C_MEM_ADDR, RW_TEST_LENGTH, data_wr);
408+
i2c_bus_device_delete(&i2c_device1);
409+
free(data_wr);
410+
TEST_ASSERT(ESP_OK == i2c_bus_delete(&i2c_bus));
411+
TEST_ASSERT(i2c_bus == NULL);
412+
}
413+
#endif
414+
375415
TEST_CASE("I2C bus scan test", "[i2c_bus][scan]")
376416
{
377417
uint8_t addrs[I2C_SCAN_ADDR_NUM] = {0};

0 commit comments

Comments
 (0)