Why does i2c_master_transmit_receive return ESP_OK when the device is unplugged?

KJ7NLL
Posts: 1
Joined: Wed Feb 07, 2024 4:08 am

Why does i2c_master_transmit_receive return ESP_OK when the device is unplugged?

Postby KJ7NLL » Wed Feb 07, 2024 4:25 am

Hello,

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));      
This is how we initialize the bus:

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));  

ESP_Sprite
Posts: 9730
Joined: Thu Nov 26, 2015 4:08 am

Re: Why does i2c_master_transmit_receive return ESP_OK when the device is unplugged?

Postby ESP_Sprite » Wed Feb 07, 2024 6:26 am

Hm, seems that at this moment you can only get that information when you register a callback. Note that that makes the API asynchronous and as such you'll probably need to change your code a fair bit.

Who is online

Users browsing this forum: Majestic-12 [Bot] and 128 guests