I2c driver fail after esp_restart()

lucasfernandez
Posts: 1
Joined: Wed Dec 20, 2023 5:53 pm

I2c driver fail after esp_restart()

Postby lucasfernandez » Wed Dec 20, 2023 6:10 pm

Hello,

I am working on a sensor project implemented on an ESP32-H2 using zigbee and lightsleep.

I noticed that I have an issue when I call the function esp_zb_factory_reset() to reset my zigbee settings and reset the ESP32. Following the reboot my i2c bus seem unresponsive. I downsized my code to a minimum to be able to reproduce the bug and noticed that I have the same behavior after using esp_restart() for a reset. Here my code using an i2c-scan instead of reading values from the sensor (the issue is the same).
  1. void app_main(void) {
  2.     i2c_config_t i2c_cfg = {
  3.         .mode = I2C_MODE_MASTER,
  4.         .sda_io_num = GPIO_NUM_15,
  5.         .scl_io_num = GPIO_NUM_23,
  6.         .sda_pullup_en = false,
  7.         .scl_pullup_en = false,
  8.  
  9.         .master = {
  10.             .clk_speed = 100000
  11.         }
  12.     };
  13.  
  14.     ESP_ERROR_CHECK(i2c_param_config(I2C_NUM_0, &i2c_cfg));
  15.     ESP_ERROR_CHECK(i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, 0, 0, 0));
  16.  
  17.     printf("i2c scan: \n");
  18.     for (uint8_t i = 1; i < 127; i++) {
  19.         int ret;
  20.         i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  21.         i2c_master_start(cmd);
  22.         i2c_master_write_byte(cmd, (i << 1) | I2C_MASTER_WRITE, 1);
  23.         i2c_master_stop(cmd);
  24.         ret = i2c_master_cmd_begin(I2C_NUM_0, cmd, 100 / portTICK_PERIOD_MS);
  25.         i2c_cmd_link_delete(cmd);
  26.    
  27.         if (ret == ESP_OK) {
  28.             printf("Found device at: 0x%2x\n", i);
  29.         }
  30.     }
  31.    
  32.     ESP_ERROR_CHECK(i2c_driver_delete(I2C_NUM_0));
  33.    
  34.     esp_restart();
  35. }
The first time my code run the i2c bus is properly initialized and the sensor is responding. After the restart the bus is initialized and the functions return no errors. But then when the commands get sent over the i2c bus nothing happen like if the bus is not properly initialized and a timeout is awaiting to happen.

I did a few things to try troubleshooting:

* If I do not delete the driver before resetting the ESP32 I have no issue. The driver is properly loaded after reset and the sensor is responsive over the i2c bus. Nonetheless I need to delete the driver. My sensor is running on battery and read values over i2c every 5min. I want to save every bit of energy I can
* I tried changing the pins, the sensor, using an ESP32-C6 instead of an ESP32-H2 I have exactly the same issue
* I tried to reset the fifo and deactivate/activate the i2c module before deleting the driver. That changes nothing

Code: Select all

i2c_reset_tx_fifo(I2C_NUM_0);
i2c_reset_rx_fifo(I2C_NUM_0);
periph_module_disable(PERIPH_I2C0_MODULE);
periph_module_enable(PERIPH_I2C0_MODULE); 
Any idea? Thank you very much for the help.

MicroController
Posts: 1708
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: I2c driver fail after esp_restart()

Postby MicroController » Thu Dec 21, 2023 9:59 pm

Try calling i2c_master_bus_reset(...) before starting the first I2C transaction after a (re-)boot.

Who is online

Users browsing this forum: No registered users and 75 guests