We are new to ESP32 and are just getting started. We have an ESP32-C6-DevKiC-1-N8 and I'm doing a simple i2c test.
I can successfully read from an I2C device (its an RTC clock) and it works. However, if the device is disconnected from the bus, the `i2c_master_transmit_receive()` function always returns `ESP_OK`.
Question: How can I detect if the device failed to respond?
Here is the code snippet which is almost completely copied from the documentation example:
Code: Select all
i2c_device_config_t dev_cfg = {
.dev_addr_length = I2C_ADDR_BIT_LEN_7,
.device_address = 0x68,
.scl_speed_hz = 10000,
};
i2c_master_dev_handle_t dev_handle;
ESP_ERROR_CHECK(i2c_master_bus_add_device(i2c_bus_handle, &dev_cfg, &dev_handle));
status = i2c_master_transmit_receive(dev_handle, &targetAddress, 1, rxBuff, numBytes, -1);
printf("read result: %d\r\n", status); // <---- always returns ESP_OK (0) !?!?!
ESP_ERROR_CHECK(i2c_master_bus_rm_device(dev_handle));
Code: Select all
i2c_master_bus_handle_t i2c_bus_handle;
i2c_master_bus_config_t i2c_mst_config = {
.clk_source = I2C_CLK_SRC_DEFAULT,
.i2c_port = -1,
.scl_io_num = I2C_SCL, // IO 11
.sda_io_num = I2C_SDA, // IO 10
.glitch_ignore_cnt = 7,
.flags.enable_internal_pullup = true,
};
ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_mst_config, &i2c_bus_handle));