FreeRTOS mutex example crashes

Mat Py
Posts: 2
Joined: Sat Feb 27, 2021 1:21 pm

FreeRTOS mutex example crashes

Postby Mat Py » Sat Feb 27, 2021 1:37 pm

Hi there,
I'm using the ESP-IDF with PlatformIO for a small project on ESP32. I should use mutexes but I keep getting a runtime error. To understand FreeRTOS mutexes from scratch, I created a test project with code from the freeRTOS book:
  1. #include "freertos/FreeRTOS.h"
  2. #include "freertos/task.h"
  3. #include "freertos/semphr.h"
  4. #include "stdio.h"
  5.  
  6. SemaphoreHandle_t xMutex = NULL;
  7. static void prvNewPrintString( const char *pcString );
  8. static void prvPrintTask( void *pvParameters );
  9.  
  10. static void prvPrintTask( void *pvParameters )
  11. {
  12.     char *pcStringToPrint;
  13.     const TickType_t xMaxBlockTimeTicks = 0x20;
  14.     pcStringToPrint = ( char * ) pvParameters;
  15.     for( ;; )
  16.     {
  17.         prvNewPrintString( pcStringToPrint );
  18.         vTaskDelay( ( rand() % xMaxBlockTimeTicks ) );
  19.     }
  20. }
  21.  
  22. static void prvNewPrintString( const char *pcString )
  23. {
  24.     xSemaphoreTake( xMutex, portMAX_DELAY );
  25.     {
  26.         printf( "%s", pcString );
  27.         fflush( stdout );
  28.     }
  29.     xSemaphoreGive( xMutex );
  30. }
  31.  
  32. void app_main() {
  33.     xMutex = xSemaphoreCreateMutex();
  34.     if( xMutex != NULL )
  35.     {
  36.         xTaskCreate( prvPrintTask, "Print1", 1000,
  37.         "Task 1 ***************************************\r\n", 1, NULL );
  38.         xTaskCreate( prvPrintTask, "Print2", 1000,
  39.         "Task 2 ---------------------------------------\r\n", 2, NULL );
  40.     }
  41.     for( ;; );
  42. }
The only modification I made is removing

Code: Select all

vTaskStartScheduler();
since app_main is already running with the scheduler.
Unfortunately, this code crashes. Here is the panic message:
  1. I (0) cpu_start: Starting scheduler on APP CPU.
  2. Guru Meditation Error: Core  1 panic'ed (IllegalInstruction). Exception was unhandled.
  3. Memory dump at 0x400824e8: e5f875a1 f01d0227 52006136
  4. Core  1 register dump:
  5. PC      : 0x400824ee  PS      : 0x00060230  A0      : 0x3ffb6ed4  A1      : 0x3ffb6650
  6. A2      : 0x3ffae978  A3      : 0x00000004  A4      : 0x00060e20  A5      : 0x00060e23  
  7. A6      : 0x00000001  A7      : 0x00000000  A8      : 0x800824ee  A9      : 0x3ffb6630  
  8. A10     : 0x3ffb21e4  A11     : 0x00060023  A12     : 0x00060020  A13     : 0x00000000
  9. A14     : 0x00000000  A15     : 0x00000000  SAR     : 0x00000000  EXCCAUSE: 0x00000000
  10. EXCVADDR: 0x00000000  LBEG    : 0x00000000  LEND    : 0x00000000  LCOUNT  : 0x00000000  
  11.  
  12. Backtrace:0x400824eb:0x3ffb6650 0x3ffb6ed1:0x3ffb6670 |<-CORRUPTED
Even if the ESP-IDF FreeRTOS implementation is slightly different than the original to handle both cores, this example shouldn't run properly?
I have also no idea how to decode the register dump and the backtrace is not very helpful.

Can you tell me what's wrong here?
Thanks,
MatPy

burtrum
Posts: 20
Joined: Mon Dec 28, 2015 1:34 pm

Re: FreeRTOS mutex example crashes

Postby burtrum » Sat Feb 27, 2021 8:07 pm

Hi,
When using printf, a task stack of 1000 is considered small. Try a bigger stack, 2048.

Burtrum

Mat Py
Posts: 2
Joined: Sat Feb 27, 2021 1:21 pm

Re: FreeRTOS mutex example crashes

Postby Mat Py » Mon Mar 01, 2021 5:32 pm

Thank you, this is the solution. I added some additional "debug" printf then the stack overflow showed clearly in the backtrace, unlike what I got before with no information. Adding more stack space to the tasks solved the problem.
And the error in my project was not related to this one...

Who is online

Users browsing this forum: Bing [Bot] and 124 guests