mirror of
https://github.com/espressif/esp-idf.git
synced 2025-12-15 19:34:03 +00:00
fix(i2c_master): Fix issue that initialize esp32 and using i2c_master_probe issue,
and probe might failed. Fixed I2C cannot return err code when nack detected Closes https://github.com/espressif/esp-idf/issues/13213, Closes https://github.com/espressif/esp-idf/issues/12929, Closes https://github.com/espressif/esp-idf/issues/13398,
This commit is contained in:
@@ -26,6 +26,24 @@
|
||||
|
||||
static const char TAG[] = "test-i2c";
|
||||
|
||||
// Make it as a local test, because don't know where there happened to be any pull-up on CI.
|
||||
TEST_CASE("I2C master initialize without pins pull-up ", "[i2c][ignore]")
|
||||
{
|
||||
i2c_master_bus_config_t i2c_mst_config_1 = {
|
||||
.clk_source = I2C_CLK_SRC_DEFAULT,
|
||||
.i2c_port = TEST_I2C_PORT,
|
||||
.scl_io_num = I2C_MASTER_SCL_IO,
|
||||
.sda_io_num = I2C_MASTER_SDA_IO,
|
||||
.flags.enable_internal_pullup = false, // no pull-up
|
||||
};
|
||||
i2c_master_bus_handle_t bus_handle;
|
||||
|
||||
TEST_ESP_OK(i2c_new_master_bus(&i2c_mst_config_1, &bus_handle));
|
||||
TEST_ESP_ERR(ESP_ERR_TIMEOUT, i2c_master_probe(bus_handle, 0x22, 20));
|
||||
vTaskDelay(1000);
|
||||
TEST_ESP_OK(i2c_del_master_bus(bus_handle));
|
||||
}
|
||||
|
||||
TEST_CASE("I2C bus install-uninstall test", "[i2c]")
|
||||
{
|
||||
i2c_master_bus_config_t i2c_mst_config_1 = {
|
||||
@@ -142,10 +160,10 @@ TEST_CASE("I2C master probe device test", "[i2c]")
|
||||
i2c_master_bus_handle_t bus_handle;
|
||||
|
||||
TEST_ESP_OK(i2c_new_master_bus(&i2c_mst_config_1, &bus_handle));
|
||||
TEST_ESP_ERR(i2c_master_probe(bus_handle, 0x22, -1), ESP_ERR_NOT_FOUND);
|
||||
TEST_ESP_ERR(i2c_master_probe(bus_handle, 0x33, -1), ESP_ERR_NOT_FOUND);
|
||||
TEST_ESP_ERR(i2c_master_probe(bus_handle, 0x44, -1), ESP_ERR_NOT_FOUND);
|
||||
TEST_ESP_ERR(i2c_master_probe(bus_handle, 0x55, -1), ESP_ERR_NOT_FOUND);
|
||||
TEST_ESP_ERR(ESP_ERR_NOT_FOUND, i2c_master_probe(bus_handle, 0x22, -1));
|
||||
TEST_ESP_ERR(ESP_ERR_NOT_FOUND, i2c_master_probe(bus_handle, 0x33, -1));
|
||||
TEST_ESP_ERR(ESP_ERR_NOT_FOUND, i2c_master_probe(bus_handle, 0x44, -1));
|
||||
TEST_ESP_ERR(ESP_ERR_NOT_FOUND, i2c_master_probe(bus_handle, 0x55, -1));
|
||||
TEST_ESP_OK(i2c_del_master_bus(bus_handle));
|
||||
}
|
||||
|
||||
@@ -187,6 +205,7 @@ TEST_CASE("I2C master transaction non-blocking mode with large amount of transac
|
||||
.dev_addr_length = I2C_ADDR_BIT_LEN_7,
|
||||
.device_address = 0x58,
|
||||
.scl_speed_hz = 400000,
|
||||
.flags.disable_ack_check = true,
|
||||
};
|
||||
|
||||
i2c_master_dev_handle_t dev_handle;
|
||||
@@ -229,3 +248,65 @@ TEST_CASE("I2C master transaction non-blocking mode with large amount of transac
|
||||
TEST_ESP_OK(i2c_master_bus_rm_device(dev_handle));
|
||||
TEST_ESP_OK(i2c_del_master_bus(bus_handle));
|
||||
}
|
||||
|
||||
static void _test_i2c_new_bus_device(i2c_master_bus_handle_t *bus_handle, i2c_master_dev_handle_t *dev_handle)
|
||||
{
|
||||
i2c_master_bus_config_t i2c_mst_config = {
|
||||
.clk_source = I2C_CLK_SRC_DEFAULT,
|
||||
.i2c_port = TEST_I2C_PORT,
|
||||
.scl_io_num = I2C_MASTER_SCL_IO,
|
||||
.sda_io_num = I2C_MASTER_SDA_IO,
|
||||
.flags.enable_internal_pullup = true,
|
||||
};
|
||||
|
||||
TEST_ESP_OK(i2c_new_master_bus(&i2c_mst_config, bus_handle));
|
||||
|
||||
i2c_device_config_t dev_cfg = {
|
||||
.dev_addr_length = I2C_ADDR_BIT_LEN_7,
|
||||
.device_address = 0x58,
|
||||
.scl_speed_hz = 100000,
|
||||
};
|
||||
|
||||
TEST_ESP_OK(i2c_master_bus_add_device(*bus_handle, &dev_cfg, dev_handle));
|
||||
}
|
||||
|
||||
static void _test_i2c_del_bus_device(i2c_master_bus_handle_t bus_handle, i2c_master_dev_handle_t dev_handle)
|
||||
{
|
||||
TEST_ESP_OK(i2c_master_bus_rm_device(dev_handle));
|
||||
TEST_ESP_OK(i2c_del_master_bus(bus_handle));
|
||||
}
|
||||
|
||||
TEST_CASE("I2C master transaction transmit check nack return value", "[i2c]")
|
||||
{
|
||||
uint8_t data_wr[DATA_LENGTH] = { 0 };
|
||||
|
||||
i2c_master_bus_handle_t bus_handle;
|
||||
i2c_master_dev_handle_t dev_handle;
|
||||
_test_i2c_new_bus_device(&bus_handle, &dev_handle);
|
||||
TEST_ESP_ERR(ESP_ERR_INVALID_STATE, i2c_master_transmit(dev_handle, data_wr, DATA_LENGTH, -1));
|
||||
_test_i2c_del_bus_device(bus_handle, dev_handle);
|
||||
}
|
||||
|
||||
TEST_CASE("I2C master transaction transmit receive check nack return value", "[i2c]")
|
||||
{
|
||||
uint8_t data_wr[DATA_LENGTH] = { 0 };
|
||||
uint8_t data_rd[DATA_LENGTH] = { 0 };
|
||||
|
||||
i2c_master_bus_handle_t bus_handle;
|
||||
i2c_master_dev_handle_t dev_handle;
|
||||
_test_i2c_new_bus_device(&bus_handle, &dev_handle);
|
||||
TEST_ESP_ERR(ESP_ERR_INVALID_STATE, i2c_master_transmit_receive(dev_handle, data_wr, DATA_LENGTH, data_rd, DATA_LENGTH, -1));
|
||||
_test_i2c_del_bus_device(bus_handle, dev_handle);
|
||||
}
|
||||
|
||||
TEST_CASE("I2C master transaction receive check nack return value", "[i2c]")
|
||||
{
|
||||
uint8_t data_rd[DATA_LENGTH] = { 0 };
|
||||
|
||||
i2c_master_bus_handle_t bus_handle;
|
||||
i2c_master_dev_handle_t dev_handle;
|
||||
|
||||
_test_i2c_new_bus_device(&bus_handle, &dev_handle);
|
||||
TEST_ESP_ERR(ESP_ERR_INVALID_STATE, i2c_master_receive(dev_handle, data_rd, DATA_LENGTH, -1));
|
||||
_test_i2c_del_bus_device(bus_handle, dev_handle);
|
||||
}
|
||||
|
||||
@@ -552,6 +552,60 @@ static void slave_write_buffer_1b_test(void)
|
||||
|
||||
TEST_CASE_MULTIPLE_DEVICES("I2C master read slave 1 byte test", "[i2c][test_env=generic_multi_device][timeout=150]", master_read_slave_1b_test, slave_write_buffer_1b_test);
|
||||
|
||||
static void master_probe_slave(void)
|
||||
{
|
||||
i2c_master_bus_config_t i2c_mst_config = {
|
||||
.clk_source = I2C_CLK_SRC_DEFAULT,
|
||||
.i2c_port = TEST_I2C_PORT,
|
||||
.scl_io_num = I2C_MASTER_SCL_IO,
|
||||
.sda_io_num = I2C_MASTER_SDA_IO,
|
||||
.flags.enable_internal_pullup = true,
|
||||
};
|
||||
i2c_master_bus_handle_t bus_handle;
|
||||
TEST_ESP_OK(i2c_new_master_bus(&i2c_mst_config, &bus_handle));
|
||||
|
||||
unity_wait_for_signal("i2c slave init finish");
|
||||
|
||||
esp_err_t ret = ESP_OK;
|
||||
for (uint8_t i = 0x01; i < 0x7F; i++) {
|
||||
ret = i2c_master_probe(bus_handle, i, -1);
|
||||
if (ret == ESP_OK) {
|
||||
printf("The slave has been found, the address is %x\n", i);
|
||||
TEST_ASSERT(i == 0x58);
|
||||
break;
|
||||
}
|
||||
TEST_ASSERT(ret == ESP_ERR_NOT_FOUND);
|
||||
}
|
||||
|
||||
unity_send_signal("probe finish");
|
||||
|
||||
TEST_ESP_OK(i2c_del_master_bus(bus_handle));
|
||||
}
|
||||
|
||||
static void slave_init_for_probe(void)
|
||||
{
|
||||
i2c_slave_config_t i2c_slv_config = {
|
||||
.addr_bit_len = I2C_ADDR_BIT_LEN_7,
|
||||
.clk_source = I2C_CLK_SRC_DEFAULT,
|
||||
.i2c_port = TEST_I2C_PORT,
|
||||
.send_buf_depth = 256,
|
||||
.scl_io_num = I2C_SLAVE_SCL_IO,
|
||||
.sda_io_num = I2C_SLAVE_SDA_IO,
|
||||
.slave_addr = 0x58,
|
||||
};
|
||||
|
||||
i2c_slave_dev_handle_t slave_handle;
|
||||
TEST_ESP_OK(i2c_new_slave_device(&i2c_slv_config, &slave_handle));
|
||||
|
||||
unity_send_signal("i2c slave init finish");
|
||||
|
||||
unity_wait_for_signal("probe finish");
|
||||
|
||||
TEST_ESP_OK(i2c_del_slave_device(slave_handle));
|
||||
}
|
||||
|
||||
TEST_CASE_MULTIPLE_DEVICES("I2C master probe slave test", "[i2c][test_env=generic_multi_device][timeout=150]", master_probe_slave, slave_init_for_probe);
|
||||
|
||||
#if SOC_I2C_NUM > 1
|
||||
// Now chips with mutiple I2C controllers are up to 2, can change this to interation when we have more I2C controllers.
|
||||
static void i2c_master_write_test_more_port(void)
|
||||
|
||||
Reference in New Issue
Block a user