mirror of
https://github.com/espressif/esp-idf.git
synced 2025-07-30 10:47:19 +02:00
fix(i2c): Fix i2c slave auto selection issue,
Closes https://github.com/espressif/esp-idf/issues/15644
This commit is contained in:
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
|
* SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*/
|
*/
|
||||||
@ -202,6 +202,7 @@ esp_err_t i2c_new_slave_device(const i2c_slave_config_t *slave_config, i2c_slave
|
|||||||
ESP_RETURN_ON_FALSE(i2c_slave, ESP_ERR_NO_MEM, TAG, "no memory for i2c slave bus");
|
ESP_RETURN_ON_FALSE(i2c_slave, ESP_ERR_NO_MEM, TAG, "no memory for i2c slave bus");
|
||||||
|
|
||||||
ESP_GOTO_ON_ERROR(i2c_acquire_bus_handle(i2c_port_num, &i2c_slave->base, I2C_BUS_MODE_SLAVE), err, TAG, "I2C bus acquire failed");
|
ESP_GOTO_ON_ERROR(i2c_acquire_bus_handle(i2c_port_num, &i2c_slave->base, I2C_BUS_MODE_SLAVE), err, TAG, "I2C bus acquire failed");
|
||||||
|
i2c_port_num = i2c_slave->base->port_num;
|
||||||
|
|
||||||
i2c_hal_context_t *hal = &i2c_slave->base->hal;
|
i2c_hal_context_t *hal = &i2c_slave->base->hal;
|
||||||
i2c_slave->base->scl_num = slave_config->scl_io_num;
|
i2c_slave->base->scl_num = slave_config->scl_io_num;
|
||||||
@ -293,8 +294,10 @@ err:
|
|||||||
static esp_err_t i2c_slave_bus_destroy(i2c_slave_dev_handle_t i2c_slave)
|
static esp_err_t i2c_slave_bus_destroy(i2c_slave_dev_handle_t i2c_slave)
|
||||||
{
|
{
|
||||||
if (i2c_slave) {
|
if (i2c_slave) {
|
||||||
i2c_ll_disable_intr_mask(i2c_slave->base->hal.dev, I2C_LL_SLAVE_EVENT_INTR);
|
if (i2c_slave->base) {
|
||||||
i2c_common_deinit_pins(i2c_slave->base);
|
i2c_ll_disable_intr_mask(i2c_slave->base->hal.dev, I2C_LL_SLAVE_EVENT_INTR);
|
||||||
|
i2c_common_deinit_pins(i2c_slave->base);
|
||||||
|
}
|
||||||
if (i2c_slave->slv_rx_mux) {
|
if (i2c_slave->slv_rx_mux) {
|
||||||
vSemaphoreDeleteWithCaps(i2c_slave->slv_rx_mux);
|
vSemaphoreDeleteWithCaps(i2c_slave->slv_rx_mux);
|
||||||
i2c_slave->slv_rx_mux = NULL;
|
i2c_slave->slv_rx_mux = NULL;
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
|
* SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*/
|
*/
|
||||||
@ -207,7 +207,12 @@ IRAM_ATTR static void i2c_slave_isr_handler(void *arg)
|
|||||||
|
|
||||||
static esp_err_t i2c_slave_device_destroy(i2c_slave_dev_handle_t i2c_slave)
|
static esp_err_t i2c_slave_device_destroy(i2c_slave_dev_handle_t i2c_slave)
|
||||||
{
|
{
|
||||||
i2c_ll_disable_intr_mask(i2c_slave->base->hal.dev, I2C_LL_SLAVE_EVENT_INTR);
|
esp_err_t ret = ESP_OK;
|
||||||
|
if (i2c_slave->base) {
|
||||||
|
i2c_ll_disable_intr_mask(i2c_slave->base->hal.dev, I2C_LL_SLAVE_EVENT_INTR);
|
||||||
|
i2c_common_deinit_pins(i2c_slave->base);
|
||||||
|
ret = i2c_release_bus_handle(i2c_slave->base);
|
||||||
|
}
|
||||||
if (i2c_slave->rx_ring_buf) {
|
if (i2c_slave->rx_ring_buf) {
|
||||||
vRingbufferDeleteWithCaps(i2c_slave->rx_ring_buf);
|
vRingbufferDeleteWithCaps(i2c_slave->rx_ring_buf);
|
||||||
i2c_slave->rx_ring_buf = NULL;
|
i2c_slave->rx_ring_buf = NULL;
|
||||||
@ -223,7 +228,6 @@ static esp_err_t i2c_slave_device_destroy(i2c_slave_dev_handle_t i2c_slave)
|
|||||||
if (i2c_slave->receive_desc.buffer) {
|
if (i2c_slave->receive_desc.buffer) {
|
||||||
free(i2c_slave->receive_desc.buffer);
|
free(i2c_slave->receive_desc.buffer);
|
||||||
}
|
}
|
||||||
esp_err_t ret = i2c_release_bus_handle(i2c_slave->base);
|
|
||||||
|
|
||||||
free(i2c_slave);
|
free(i2c_slave);
|
||||||
return ret;
|
return ret;
|
||||||
@ -256,7 +260,7 @@ esp_err_t i2c_new_slave_device(const i2c_slave_config_t *slave_config, i2c_slave
|
|||||||
i2c_slave->base->sda_num = slave_config->sda_io_num;
|
i2c_slave->base->sda_num = slave_config->sda_io_num;
|
||||||
i2c_slave->base->pull_up_enable = slave_config->flags.enable_internal_pullup;
|
i2c_slave->base->pull_up_enable = slave_config->flags.enable_internal_pullup;
|
||||||
i2c_slave->rx_data_count = 0;
|
i2c_slave->rx_data_count = 0;
|
||||||
int i2c_port_num = slave_config->i2c_port;
|
i2c_port_num_t i2c_port_num = i2c_slave->base->port_num;
|
||||||
ESP_GOTO_ON_ERROR(i2c_common_set_pins(i2c_slave->base), err, TAG, "i2c slave set pins failed");
|
ESP_GOTO_ON_ERROR(i2c_common_set_pins(i2c_slave->base), err, TAG, "i2c slave set pins failed");
|
||||||
|
|
||||||
i2c_slave->rx_ring_buf = xRingbufferCreateWithCaps(slave_config->receive_buf_depth, RINGBUF_TYPE_BYTEBUF, I2C_MEM_ALLOC_CAPS);
|
i2c_slave->rx_ring_buf = xRingbufferCreateWithCaps(slave_config->receive_buf_depth, RINGBUF_TYPE_BYTEBUF, I2C_MEM_ALLOC_CAPS);
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
|
* SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: Unlicense OR CC0-1.0
|
* SPDX-License-Identifier: Unlicense OR CC0-1.0
|
||||||
*/
|
*/
|
||||||
@ -19,6 +19,47 @@
|
|||||||
|
|
||||||
#if SOC_I2C_SLAVE_CAN_GET_STRETCH_CAUSE
|
#if SOC_I2C_SLAVE_CAN_GET_STRETCH_CAUSE
|
||||||
|
|
||||||
|
TEST_CASE("I2C peripheral allocate slave all", "[i2c]")
|
||||||
|
{
|
||||||
|
i2c_slave_dev_handle_t dev_handle[SOC_HP_I2C_NUM];
|
||||||
|
for (int i = 0; i < SOC_HP_I2C_NUM; i++) {
|
||||||
|
i2c_slave_config_t i2c_slv_config_1 = {
|
||||||
|
.clk_source = I2C_CLK_SRC_DEFAULT,
|
||||||
|
.i2c_port = -1,
|
||||||
|
.scl_io_num = I2C_SLAVE_SCL_IO,
|
||||||
|
.sda_io_num = I2C_SLAVE_SDA_IO,
|
||||||
|
.slave_addr = ESP_SLAVE_ADDR,
|
||||||
|
.send_buf_depth = DATA_LENGTH,
|
||||||
|
.receive_buf_depth = DATA_LENGTH,
|
||||||
|
.flags.enable_internal_pullup = true,
|
||||||
|
};
|
||||||
|
|
||||||
|
TEST_ESP_OK(i2c_new_slave_device(&i2c_slv_config_1, &dev_handle[i]));
|
||||||
|
}
|
||||||
|
i2c_slave_config_t i2c_slv_config_1 = {
|
||||||
|
.clk_source = I2C_CLK_SRC_DEFAULT,
|
||||||
|
.i2c_port = -1,
|
||||||
|
.scl_io_num = I2C_SLAVE_SCL_IO,
|
||||||
|
.sda_io_num = I2C_SLAVE_SDA_IO,
|
||||||
|
.slave_addr = ESP_SLAVE_ADDR,
|
||||||
|
.send_buf_depth = DATA_LENGTH,
|
||||||
|
.receive_buf_depth = DATA_LENGTH,
|
||||||
|
.flags.enable_internal_pullup = true,
|
||||||
|
};
|
||||||
|
i2c_slave_dev_handle_t dev_handle_2;
|
||||||
|
|
||||||
|
TEST_ESP_ERR(ESP_ERR_NOT_FOUND, i2c_new_slave_device(&i2c_slv_config_1, &dev_handle_2));
|
||||||
|
|
||||||
|
for (int i = 0; i < SOC_HP_I2C_NUM; i++) {
|
||||||
|
TEST_ESP_OK(i2c_del_slave_device(dev_handle[i]));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get another one
|
||||||
|
|
||||||
|
TEST_ESP_OK(i2c_new_slave_device(&i2c_slv_config_1, &dev_handle_2));
|
||||||
|
TEST_ESP_OK(i2c_del_slave_device(dev_handle_2));
|
||||||
|
}
|
||||||
|
|
||||||
static QueueHandle_t event_queue;
|
static QueueHandle_t event_queue;
|
||||||
static uint8_t *temp_data;
|
static uint8_t *temp_data;
|
||||||
static size_t temp_len = 0;
|
static size_t temp_len = 0;
|
||||||
@ -91,7 +132,7 @@ static void i2c_slave_read_test_v2(void)
|
|||||||
unity_wait_for_signal("master write");
|
unity_wait_for_signal("master write");
|
||||||
|
|
||||||
i2c_slave_event_t evt;
|
i2c_slave_event_t evt;
|
||||||
if (xQueueReceive(event_queue, &evt, 1) == pdTRUE) {
|
if (xQueueReceive(event_queue, &evt, portMAX_DELAY) == pdTRUE) {
|
||||||
if (evt == I2C_SLAVE_EVT_RX) {
|
if (evt == I2C_SLAVE_EVT_RX) {
|
||||||
disp_buf(temp_data, temp_len);
|
disp_buf(temp_data, temp_len);
|
||||||
printf("length is %x\n", temp_len);
|
printf("length is %x\n", temp_len);
|
||||||
@ -286,7 +327,6 @@ static void i2c_master_write_test_with_customize_api(void)
|
|||||||
|
|
||||||
TEST_ESP_OK(i2c_del_master_bus(bus_handle));
|
TEST_ESP_OK(i2c_del_master_bus(bus_handle));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE_MULTIPLE_DEVICES("I2C master write slave with customize api", "[i2c][test_env=generic_multi_device][timeout=150]", i2c_master_write_test_with_customize_api, i2c_slave_read_test_v2);
|
TEST_CASE_MULTIPLE_DEVICES("I2C master write slave with customize api", "[i2c][test_env=generic_multi_device][timeout=150]", i2c_master_write_test_with_customize_api, i2c_slave_read_test_v2);
|
||||||
|
|
||||||
#endif // SOC_I2C_SLAVE_CAN_GET_STRETCH_CAUSE
|
#endif // SOC_I2C_SLAVE_CAN_GET_STRETCH_CAUSE
|
||||||
|
Reference in New Issue
Block a user