i2c read timeout issue while reading Pb hub through atom lite in ESP-IDF



  • hey there, i am using ESP-IDF to program the atom lite, i have a Pb hub and i want to configure it with atom lite using the espidf, but the issue is i am getting read timeout error (error code 0x107) below is the code i am using.

    #include <stdio.h>
    #include "esp_log.h"
    #include "driver/i2c.h"

    static const char *TAG = "i2c-simple-example";

    #define I2C_MASTER_SCL_IO 32 /*!< GPIO number used for I2C master clock /
    #define I2C_MASTER_SDA_IO 26 /
    !< GPIO number used for I2C master data /
    #define I2C_MASTER_NUM 0 /
    !< I2C master i2c port number, the number of i2c peripheral interfaces available will depend on the chip /
    #define I2C_MASTER_FREQ_HZ 400000 /
    !< I2C master clock frequency /
    #define I2C_MASTER_TX_BUF_DISABLE 0 /
    !< I2C master doesn't need buffer /
    #define I2C_MASTER_RX_BUF_DISABLE 0 /
    !< I2C master doesn't need buffer */
    #define I2C_MASTER_TIMEOUT_MS 1000

    #define MPU9250_SENSOR_ADDR 0x61 /*!< Slave address of the MPU9250 sensor /
    #define MPU9250_WHO_AM_I_REG_ADDR 0x61 /
    !< Register addresses of the "who am I" register */

    #define MPU9250_PWR_MGMT_1_REG_ADDR 0x6B /*!< Register addresses of the power managment register */
    #define MPU9250_RESET_BIT 7

    /**

    • @brief Read a sequence of bytes from a MPU9250 sensor registers
      */
      static esp_err_t mpu9250_register_read(uint8_t reg_addr, uint8_t *data, size_t len)
      {
      esp_err_t err = ESP_OK;
      err = i2c_master_write_read_device(I2C_NUM_0, MPU9250_SENSOR_ADDR, &reg_addr, 1, data, len, I2C_MASTER_TIMEOUT_MS / portTICK_RATE_MS);
      return err;
      }
      static esp_err_t i2c_master_init(void)
      {
      i2c_config_t conf = {
      .mode = I2C_MODE_MASTER,
      .sda_io_num = I2C_MASTER_SDA_IO,
      .scl_io_num = I2C_MASTER_SCL_IO,
      .sda_pullup_en = GPIO_PULLUP_ENABLE,
      .scl_pullup_en = GPIO_PULLUP_ENABLE,
      .master.clk_speed = I2C_MASTER_FREQ_HZ,
      };

      i2c_param_config(I2C_NUM_0, &conf);
      i2c_set_timeout(I2C_NUM_0, 400000);
      return i2c_driver_install(I2C_NUM_0, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0);
      }

    void app_main(void)
    {
    uint8_t data[2];
    ESP_ERROR_CHECK(i2c_master_init());
    ESP_LOGI(TAG, "I2C initialized successfully");

    while (1)
    {
        esp_err_t err = mpu9250_register_read(0x00, data, 1);
        if (err != ESP_OK)
        {
            int timevalue;
            i2c_get_timeout(I2C_NUM_0, &timevalue);
            ESP_LOGI(TAG, "time value is : %d  err[%d]", timevalue, err);
        }
        ESP_LOGI(TAG, "WHO_AM_I = %X", data[0]);
        vTaskDelay(2000 / portTICK_PERIOD_MS);
    }
    
    ESP_ERROR_CHECK(i2c_driver_delete(I2C_NUM_0));
    ESP_LOGI(TAG, "I2C unitialized successfully");
    

    }

    Note : interestingly i am able to read and write the Pb hub using the arduino ide wire library so i am confused what could be the issue.
    i have go through all the documentation and help i could get online to resolve the issue but i am unable to solve it. any help in this regard would be highly appreciated.



  • can you try adding ESP_ERROR_CHECK(esp_event_loop_create_default()); and see what errors you get?

    When I compile and deploy my project, I see the following warning

    i2c: This driver is an old driver; please migrate your application code to adapt driver/i2c_master.h,

    which causes my system to never finish initialization. In my case, the M5Unified library needs to be updated to use driver/i2c_master.h

    Perhaps your timeout is related to the loop never initializing. At this point, I am only guessing what the issue might be.