New I2C Master Driver messing with FreeRTOS?

demayn
Posts: 3
Joined: Mon Nov 11, 2024 9:11 am

New I2C Master Driver messing with FreeRTOS?

Postby demayn » Mon Nov 11, 2024 12:46 pm

Hello everyone,

I've been using ESP-IDF for a while but facing problems that I cannot solve myself anymore.
I'm woking in ESP IDF V5.3.1 with a custom Board based on ESP32-S3-1 N8.
Connected to my board I have an LCD Screen controlled via I2C over a PCF8574 IO expander. I wrote the lib for that myself, using the latest I2C Master driver.
My issue is, that my code (or the driver itself?) seems to be messing with RTOS queues, when I transmit I2C data and try to read from a queue, the ESP crashes (see error log below).
I have also attached my code as well as my I2C Master config. Interestingly, when I rem,ove all I2C related commands, the queue works falwlessly, on the other hand the Screen (and thus all I2C commands) work flawlessly when no queue is being used.
Unfortunately, I need both.

I have ensured,m that enough stack and heap are available. I have also tried a dynamically allocated queue. I have also tried making the queue more lightweight by passing pointers in the queue.

Could the latest I2C driver at fault? What can I do? Sadly, I'm at the end of my knowledge.
Thanks for reading.
  1. I (398) GP_HW: LCD Task started
  2. I (2148) GP_HW: queue receive
  3. Guru Meditation Error: Core  0 panic'ed (Interrupt wdt timeout on CPU0).
  4.  
  5. Core  0 register dump:
  6. PC      : 0x4037b2e8  PS      : 0x00060d34  A0      : 0x8037adf6  A1      : 0x3fc9d820
  7. 0x4037b2e8: spinlock_acquire at C:/Espressif/frameworks/esp-idf-v5.3/components/esp_hw_support/include/spinlock.h:131
  8.  (inlined by) xPortEnterCriticalTimeout at C:/Espressif/frameworks/esp-idf-v5.3/components/freertos/FreeRTOS-Kernel/portable/xtensa/port.c:473
  9.  
  10. A2      : 0x3fc9dc20  A3      : 0xffffffff  A4      : 0x147108eb  A5      : 0x00060d23
  11. A6      : 0xb33fffff  A7      : 0x0000cdcd  A8      : 0x8037b2e8  A9      : 0x3fc9d800
  12. A10     : 0x00000000  A11     : 0x53cb9a98  A12     : 0x5f746567  A13     : 0x0000000c
  13. A14     : 0x00060023  A15     : 0x00000003  SAR     : 0x00000004  EXCCAUSE: 0x00000005
  14. EXCVADDR: 0x00000000  LBEG    : 0x400556d5  LEND    : 0x400556e5  LCOUNT  : 0xfffffffe
  15. 0x400556d5: strlen in ROM
  16. 0x400556e5: strlen in ROM
  17.  
  18.  
  19.  
  20. Backtrace: 0x4037b2e5:0x3fc9d820 0x4037adf3:0x3fc9d850 0x42009001:0x3fc9d890 0x4037b08d:0x3fc9d910
  21. 0x4037b2e5: spinlock_acquire at C:/Espressif/frameworks/esp-idf-v5.3/components/esp_hw_support/include/spinlock.h:130
  22.  (inlined by) xPortEnterCriticalTimeout at C:/Espressif/frameworks/esp-idf-v5.3/components/freertos/FreeRTOS-Kernel/portable/xtensa/port.c:473
  23. 0x4037adf3: vPortEnterCritical at C:/Espressif/frameworks/esp-idf-v5.3/components/freertos/FreeRTOS-Kernel/portable/xtensa/include/freertos/portmacro.h:567
  24.  (inlined by) xQueueReceive at C:/Espressif/frameworks/esp-idf-v5.3/components/freertos/FreeRTOS-Kernel/queue.c:1549
  25. 0x42009001: lcd_task at C:/Users/damia/Desktop/SSW/SW/ssw_esp/espidf/ssw2opta/main/main.c:76
  26. 0x4037b08d: vPortTaskWrapper at C:/Espressif/frameworks/esp-idf-v5.3/components/freertos/FreeRTOS-Kernel/portable/xtensa/port.c:134
  27.  
  28.  
  29.  
  30. Core  1 register dump:
  31. PC      : 0x40378cf2  PS      : 0x00060934  A0      : 0x82002c39  A1      : 0x3fc9b550
  32. 0x40378cf2: esp_cpu_wait_for_intr at C:/Espressif/frameworks/esp-idf-v5.3/components/esp_hw_support/cpu.c:64
  33.  
  34. A2      : 0x00000000  A3      : 0x00000000  A4      : 0x3fc995a0  A5      : 0x3fc99580
  35. A6      : 0x40375d98  A7      : 0x00000001  A8      : 0x8200ba2a  A9      : 0x3fc9b510
  36. 0x40375d98: ipc_task at C:/Espressif/frameworks/esp-idf-v5.3/components/esp_system/esp_ipc.c:53
  37.  
  38. A10     : 0x00000000  A11     : 0x00000000  A12     : 0x3fc99580  A13     : 0x3fc99560
  39. A14     : 0x00000001  A15     : 0x3fc9b724  SAR     : 0x00000000  EXCCAUSE: 0x00000005
  40. EXCVADDR: 0x00000000  LBEG    : 0x00000000  LEND    : 0x00000000  LCOUNT  : 0x00000000
  41.  
  42.  
  43. Backtrace: 0x40378cef:0x3fc9b550 0x42002c36:0x3fc9b570 0x4037c025:0x3fc9b590 0x4037b08d:0x3fc9b5b0
  44. 0x40378cef: xt_utils_wait_for_intr at C:/Espressif/frameworks/esp-idf-v5.3/components/xtensa/include/xt_utils.h:82
  45.  (inlined by) esp_cpu_wait_for_intr at C:/Espressif/frameworks/esp-idf-v5.3/components/esp_hw_support/cpu.c:55
  46. 0x42002c36: esp_vApplicationIdleHook at C:/Espressif/frameworks/esp-idf-v5.3/components/esp_system/freertos_hooks.c:58
  47. 0x4037c025: prvIdleTask at C:/Espressif/frameworks/esp-idf-v5.3/components/freertos/FreeRTOS-Kernel/tasks.c:4344 (discriminator 1)
  48. 0x4037b08d: vPortTaskWrapper at C:/Espressif/frameworks/esp-idf-v5.3/components/freertos/FreeRTOS-Kernel/portable/xtensa/port.c:134
  1. #define QUEUE_LENGTH 3
  2. #define ITEM_SIZE sizeof(lcd_data)
  3. StaticQueue_t xQueueBuffer;
  4. uint8_t ucQueueStorage[QUEUE_LENGTH * ITEM_SIZE];
  5.  
  6. QueueHandle_t lcd_queue;
  7.  
  8. rtc_pcf8563 lcd;
  9.  
  10. void lcd_task(void *args);
  11.  
  12. void app_main(void)
  13. {
  14.     gpio_init();
  15.     i2c_init();
  16.  
  17.     esp_err_t ret;
  18.     ret = nvs_flash_init();
  19.     if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
  20.     {
  21.         ESP_ERROR_CHECK(nvs_flash_erase());
  22.         ret = nvs_flash_init();
  23.     }
  24.     ESP_ERROR_CHECK(ret);
  25.  
  26.     // init LCD queue (will be used to send data to LCD print task)
  27.     lcd_queue = xQueueCreateStatic(QUEUE_LENGTH,         // The number of items the queue can hold.
  28.                                    ITEM_SIZE,            // The size of each item in the queue
  29.                                    &(ucQueueStorage[0]), // The buffer that will hold the items in the queue.
  30.                                    &xQueueBuffer);       // The buffer that will hold the queue structure.
  31.     if (lcd_queue == NULL)
  32.         ESP_LOGE(TAG, "queuecreate failed");
  33.     TaskHandle_t lcd_tsk_hdl;
  34.     xTaskCreate(lcd_task, "LCD_TSK", 5000, NULL, tskIDLE_PRIORITY, &lcd_tsk_hdl);
  35.  
  36.     lcd_data info = {
  37.         .x_pos = 0,
  38.         .y_pos = 0,
  39.         .msg = "testmsg",
  40.     };
  41.  
  42.     xQueueSend(lcd_queue, &info, portMAX_DELAY);
  43.  
  44.     vTaskDelay(portMAX_DELAY);
  45. }
  46.  
  47. void lcd_task(void *args)
  48. {
  49.     ESP_LOGI(TAG, "LCD Task started");
  50.     lcd_init(&lcd, LCD_ADDR, bus_handle);
  51.     while (lcd_begin(&lcd, LCD_COLS, LCD_ROWS) != ESP_OK)
  52.     {
  53.         ESP_LOGE(TAG, "LCD init fail");
  54.         vTaskDelay(5000 / portTICK_PERIOD_MS);
  55.     }
  56.  
  57.     // Turn on the backlight
  58.     lcd_set_backlight(&lcd, true);
  59.  
  60.     // Print a message
  61.     vTaskDelay(1);
  62.     lcd_set_cursor(&lcd, 0, 0);
  63.     vTaskDelay(1);
  64.     lcd_print(&lcd, LCD_START_MSG);
  65.     // vTaskDelay(2000 / portTICK_PERIOD_MS);
  66.  
  67.     lcd_data lcd_buf;
  68.  
  69.     while (1)
  70.     {
  71.         ESP_LOGI(TAG, "queue receive");
  72.         if (xQueueReceive(lcd_queue, &lcd_buf, portMAX_DELAY))
  73.             // ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
  74.             ESP_LOGI(TAG, "queue event");
  75.  
  76.         ESP_LOGI(TAG, "%s", lcd_buf.msg);
  77.        
  78.                 vTaskDelay(1);
  79.                 lcd_clear(&lcd);
  80.                 vTaskDelay(1);
  81.                 lcd_set_cursor(&lcd, lcd_buf.x_pos, lcd_buf.y_pos);
  82.                 vTaskDelay(1);
  83.                 lcd_print(&lcd, lcd_buf.msg);
  84.                
  85.     }
  86. }
this is my I2C config
  1. i2c_master_bus_config_t i2c_mst_config = {
  2.     .i2c_port = I2C_NUM_0,
  3.     .sda_io_num = GPIO_NUM_6,
  4.     .scl_io_num = GPIO_NUM_5,
  5.     .clk_source = I2C_CLK_SRC_DEFAULT,
  6.     .glitch_ignore_cnt = 7,
  7.     .intr_priority = 0,
  8.     .trans_queue_depth = 0,
  9.     .flags.enable_internal_pullup = true, // enable internal pullups
  10. };
  11. i2c_master_bus_handle_t bus_handle;
  12.  
  13. void lcd_init(i2c_lcd_pcf8574_handle_t *lcd, uint8_t i2c_addr, i2c_master_bus_handle_t bus_hdl)
  14. {
  15.     lcd->i2c_addr = i2c_addr;
  16.     lcd->backlight = true;                 // sometimes not supported (PCF5874 P3 is NC on some modules)
  17.     lcd->entrymode = ENTRYMODE_LEFT2RIGHT; // Init the LCD with an internal reset
  18.     lcd->displaycontrol = DISPLAY_ON & ~CURSOR_ON & ~CURSOR_BLINK;
  19.     lcd->bus_hdl = bus_hdl;
  20. }

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

Re: New I2C Master Driver messing with FreeRTOS?

Postby ESP_Sprite » Tue Nov 12, 2024 2:04 am

There's nothing in the code that you posted that seems suspect to me. Any chance of you uploading the full project so we can have a look?

demayn
Posts: 3
Joined: Mon Nov 11, 2024 9:11 am

Re: New I2C Master Driver messing with FreeRTOS?

Postby demayn » Tue Nov 12, 2024 8:10 am

ESP_Sprite wrote: There's nothing in the code that you posted that seems suspect to me. Any chance of you uploading the full project so we can have a look?
Sure, I've got nothing to hide (apart from spaghetti code). How would I do so? Github?

demayn
Posts: 3
Joined: Mon Nov 11, 2024 9:11 am

Re: New I2C Master Driver messing with FreeRTOS?

Postby demayn » Tue Nov 12, 2024 8:30 am

Apparently I'm just a moron.
While cleaning up the code and removing libs that are not necessary for simplicity, I removed the library for the i2c Real time clock. Turns out I had declared my lcd as rtc (real time clock, see line 8 in my code snippet), which has some members in common, so it wouldn't throw an error in C as it's being passed via pointer (just a warning, incompataible pointer type).
Yet the differenmce between the two structs is big enough to scramble my stack :D
That threw a compile error as soon as the other lib was removed.
Thank you very much for making me clean this up, it was just what I needed it seems. I have learned my lesson.

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

Re: New I2C Master Driver messing with FreeRTOS?

Postby ESP_Sprite » Wed Nov 13, 2024 12:04 am

Haha :) you have no idea how often asking people for their (stripped-down) code solves things here.

Who is online

Users browsing this forum: No registered users and 320 guests