Skip to content

Commit 6b735e0

Browse files
committed
Merge branch 'fix/enhance_i2c_slave_test_stability' into 'master'
fix(i2c): Fixed I2C return without release the lock & enhance lp i2c lock souce selection See merge request espressif/esp-idf!35025
2 parents d1c0d6e + b9183aa commit 6b735e0

File tree

5 files changed

+69
-1
lines changed

5 files changed

+69
-1
lines changed

components/esp_driver_i2c/i2c_common.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,11 @@ esp_err_t i2c_acquire_bus_handle(i2c_port_num_t port_num, i2c_bus_handle_t *i2c_
169169
break;
170170
}
171171
}
172-
ESP_RETURN_ON_FALSE((bus_found == true), ESP_ERR_NOT_FOUND, TAG, "acquire bus failed, no free bus");
172+
if (bus_found == false) {
173+
ESP_LOGE(TAG, "acquire bus failed, no free bus");
174+
_lock_release(&s_i2c_platform.mutex);
175+
return ESP_ERR_NOT_FOUND;
176+
}
173177
} else {
174178
ret = s_i2c_bus_handle_acquire(port_num, i2c_new_bus, mode);
175179
if (ret != ESP_OK) {

components/esp_driver_i2c/i2c_master.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -966,6 +966,19 @@ esp_err_t i2c_new_master_bus(const i2c_master_bus_config_t *bus_config, i2c_mast
966966
}
967967
#if SOC_LP_I2C_SUPPORTED
968968
else {
969+
970+
soc_periph_lp_i2c_clk_src_t clk_srcs[] = SOC_LP_I2C_CLKS;
971+
bool lp_clock_match = false;
972+
for (int i = 0; i < sizeof(clk_srcs) / sizeof(clk_srcs[0]); i++) {
973+
if ((int)clk_srcs[i] == (int)i2c_master->base->clk_src) {
974+
/* Clock source matches. Override the source clock type with the user configured value */
975+
lp_clock_match = true;
976+
break;
977+
}
978+
}
979+
980+
ESP_GOTO_ON_FALSE(lp_clock_match, ESP_ERR_NOT_SUPPORTED, err, TAG, "the clock source does not support lp i2c, please check");
981+
969982
LP_I2C_SRC_CLK_ATOMIC() {
970983
lp_i2c_ll_set_source_clk(hal->dev, i2c_master->base->clk_src);
971984
}

components/esp_driver_i2c/test_apps/i2c_test_apps/main/test_i2c_common.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,41 @@ TEST_CASE("I2C device add & remove check", "[i2c]")
146146
TEST_ESP_OK(i2c_del_master_bus(bus_handle));
147147
}
148148

149+
TEST_CASE("I2C peripheral allocate all", "[i2c]")
150+
{
151+
i2c_master_bus_handle_t bus_handle[SOC_HP_I2C_NUM];
152+
for (int i = 0; i < SOC_HP_I2C_NUM; i++) {
153+
i2c_master_bus_config_t i2c_mst_config_1 = {
154+
.clk_source = I2C_CLK_SRC_DEFAULT,
155+
.i2c_port = -1,
156+
.scl_io_num = I2C_MASTER_SCL_IO,
157+
.sda_io_num = I2C_MASTER_SDA_IO,
158+
.flags.enable_internal_pullup = true,
159+
};
160+
161+
TEST_ESP_OK(i2c_new_master_bus(&i2c_mst_config_1, &bus_handle[i]));
162+
}
163+
i2c_master_bus_config_t i2c_mst_config_1 = {
164+
.clk_source = I2C_CLK_SRC_DEFAULT,
165+
.i2c_port = -1,
166+
.scl_io_num = I2C_MASTER_SCL_IO,
167+
.sda_io_num = I2C_MASTER_SDA_IO,
168+
.flags.enable_internal_pullup = true,
169+
};
170+
i2c_master_bus_handle_t bus_handle_2;
171+
172+
TEST_ESP_ERR(ESP_ERR_NOT_FOUND, i2c_new_master_bus(&i2c_mst_config_1, &bus_handle_2));
173+
174+
for (int i = 0; i < SOC_HP_I2C_NUM; i++) {
175+
TEST_ESP_OK(i2c_del_master_bus(bus_handle[i]));
176+
}
177+
178+
// Get another one
179+
180+
TEST_ESP_OK(i2c_new_master_bus(&i2c_mst_config_1, &bus_handle_2));
181+
TEST_ESP_OK(i2c_del_master_bus(bus_handle_2));
182+
}
183+
149184
TEST_CASE("I2C master probe device test", "[i2c]")
150185
{
151186
// 0x22,33,44,55 does not exist on the I2C bus, so it's expected to return `not found` error

components/esp_driver_i2c/test_apps/i2c_test_apps/main/test_i2c_slave_v2.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ static void i2c_slave_read_test_v2(void)
7474
.slave_addr = ESP_SLAVE_ADDR,
7575
.send_buf_depth = DATA_LENGTH,
7676
.receive_buf_depth = DATA_LENGTH,
77+
.flags.enable_internal_pullup = true,
7778
};
7879

7980
TEST_ESP_OK(i2c_new_slave_device(&i2c_slv_config, &handle));
@@ -200,6 +201,7 @@ static void slave_write_buffer_test_v2(void)
200201
.slave_addr = ESP_SLAVE_ADDR,
201202
.send_buf_depth = DATA_LENGTH,
202203
.receive_buf_depth = DATA_LENGTH,
204+
.flags.enable_internal_pullup = true,
203205
};
204206

205207
TEST_ESP_OK(i2c_new_slave_device(&i2c_slv_config, &handle));

components/esp_driver_i2c/test_apps/i2c_test_apps/main/test_lp_i2c.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,20 @@ TEST_CASE("LP I2C initialize with wrong IO", "[i2c]")
6464

6565
#endif
6666

67+
TEST_CASE("LP I2C initialize with wrong clock source", "[i2c]")
68+
{
69+
i2c_master_bus_config_t i2c_mst_config = {
70+
.lp_source_clk = I2C_CLK_SRC_DEFAULT,
71+
.i2c_port = LP_I2C_NUM_0,
72+
.scl_io_num = LP_I2C_SCL_IO,
73+
.sda_io_num = LP_I2C_SDA_IO,
74+
.flags.enable_internal_pullup = true,
75+
};
76+
i2c_master_bus_handle_t bus_handle;
77+
78+
TEST_ESP_ERR(ESP_ERR_NOT_SUPPORTED, i2c_new_master_bus(&i2c_mst_config, &bus_handle));
79+
}
80+
6781
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)
6882
{
6983
BaseType_t high_task_wakeup = pdFALSE;

0 commit comments

Comments
 (0)