I2C crash when last command is "read"

Northeastrader
Posts: 2
Joined: Wed Nov 02, 2022 7:49 am

I2C crash when last command is "read"

Postby Northeastrader » Wed Nov 02, 2022 8:05 am

  1. i2c_cmd_handle_t cmd_link = NULL;
  2. cmd_link = i2c_cmd_link_create();
  3.  
  4. if(cmd_link == NULL)
  5.  {
  6.     ESP_LOGE(TAG,"measured data read : failed to create cmd link");
  7.     return ESP_FAIL;
  8. }
  9.     uint8_t data = 0;
  10.     err_code = i2c_master_start(cmd_link);                                                            
  11.     err_code = i2c_master_write_byte(cmd_link,(MLX90393_I2C_ADDR << 1)|I2C_MASTER_WRITE,ACK_CHECK_EN);    
  12.     err_code = i2c_master_write_byte(cmd_link,MLX90393_I2C_CMD_READ_MEASURED_DATA,ACK_CHECK_EN);        
  13.     err_code = i2c_master_start(cmd_link);                                                          
  14.     err_code = i2c_master_write_byte(cmd_link,(MLX90393_I2C_ADDR << 1)|I2C_MASTER_READ,ACK_CHECK_EN);
  15.     err_code = i2c_master_read(cmd_link,&data ,1,I2C_MASTER_ACK);                        
  16.                                                  
  17. err_code = i2c_master_cmd_begin(MLX90393_I2C_NUM,cmd_link,pdMS_TO_TICKS(1000));

when "i2c_master_read" or "i2c_master_read_byte" is the last command, it crashes at "i2c_master_cmd_begin"
if add "i2c_master_stop" to the end or move "i2c_master_read" up one line it works well

i need to judge the value of "data" to determine either to end communication or read more bytes
here is error infor
  1. Guru Meditation Error: Core  0 panic'ed (StoreProhibited). Exception was unhandled.
  2.  
  3. Core  0 register dump:
  4. PC      : 0x40377024  PS      : 0x00060031  A0      : 0x803772ed  A1      : 0x3fc92880
  5. 0x40377024: i2c_master_cmd_begin_static at E:/ESP32_ENV/esp-idf/components/driver/i2c.c:1289
  6.  
  7. A2      : 0x00000000  A3      : 0x3fce0734  A4      : 0x00000000  A5      : 0x3fcf46a0
  8. A6      : 0x3fc946b4  A7      : 0x3fcf46c0  A8      : 0x00000000  A9      : 0x3fc94b64  
  9. A10     : 0x00000093  A11     : 0x3fce09b8  A12     : 0x00000001  A13     : 0x3fc94b64
  10. A14     : 0x60013000  A15     : 0x00000000  SAR     : 0x00000000  EXCCAUSE: 0x0000001d
  11. EXCVADDR: 0x00000008  LBEG    : 0x00000000  LEND    : 0x00000000  LCOUNT  : 0x00000000
  12.  
  13.  
  14. Backtrace:0x40377021:0x3fc928800x403772ea:0x3fc928b0 0x40376916:0x3fc928f0 0x42018a97:0x3fcf46f0 0x42001f33:0x3fcf4710 0x4037b841:0x3fcf4730 0x4037cd71:0x3fcf4750
  15. 0x40377021: i2c_master_cmd_begin_static at E:/ESP32_ENV/esp-idf/components/driver/i2c.c:1288
  16.  
  17. 0x403772ea: i2c_isr_handler_default at E:/ESP32_ENV/esp-idf/components/driver/i2c.c:497
  18.  
  19. 0x40376916: _xt_lowint1 at E:/ESP32_ENV/esp-idf/components/freertos/port/xtensa/xtensa_vectors.S:1111
  20.  
  21. 0x42018a97: cpu_ll_waiti at E:/ESP32_ENV/esp-idf/components/hal/esp32s3/include/hal/cpu_ll.h:182
  22.  (inlined by) esp_pm_impl_waiti at E:/ESP32_ENV/esp-idf/components/esp_pm/pm_impl.c:837
  23.  
  24. 0x42001f33: esp_vApplicationIdleHook at E:/ESP32_ENV/esp-idf/components/esp_system/freertos_hooks.c:63
  25.  
  26. 0x4037b841: prvIdleTask at E:/ESP32_ENV/esp-idf/components/freertos/tasks.c:3973 (discriminator 1)
  27.  
  28. 0x4037cd71: vPortTaskWrapper at E:/ESP32_ENV/esp-idf/components/freertos/port/xtensa/port.c:131

Northeastrader
Posts: 2
Joined: Wed Nov 02, 2022 7:49 am

Re: I2C crash when last command is "read"

Postby Northeastrader » Thu Nov 03, 2022 9:09 am

i have found out the reason for the problem, this may be a bug of esp-idf

the error occurs when you make "i2c_master_read" as the last command while receive more data than you read
e.g.
i2c receive 3 bytes data and you set "i2c_master_read" as the last command to read 2 bytes
what you expect is read two bytes first and there remains one byte to read next time, but it will crash because of array bounds write

the solution is to provide a large enough buff for "i2c_master_read"

Who is online

Users browsing this forum: No registered users and 104 guests