Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

freertos vTaskDelete(NULL) no free memory

I am starting to learn FreeRTOS. Just now I am trying to make a print task function with this code:

static void vTaskPrint(void *pvParameters) {
    taskENTER_CRITICAL();
    printf("%s", (char *)pvParameters);
    printf("xPortGetFreeHeapSize: %d\r\n", xPortGetFreeHeapSize());
    taskEXIT_CRITICAL();
    vTaskDelete(NULL);
}

But after 14 calls to:

xTaskCreate(vTaskPrint, (char *)"vTaskPrint", configMINIMAL_STACK_SIZE, (void *)buffer, 3, (xTaskHandle *)NULL);

The cortex M3 run out of memory:

************** TEST GPIO & LEDS FREERTOS OPEN103Z EVB **************
vTaskLeds
xPortGetFreeHeapSize: 7832
vTaskReadKeys
xPortGetFreeHeapSize: 7232
[INFO] vTaskPrint created successful
Key pressed RIGHT
xPortGetFreeHeapSize: 6632
[INFO] vTaskPrint created successful
Key pressed CENTER KEY
xPortGetFreeHeapSize: 6032
[INFO] vTaskPrint created successful
Key pressed RIGHT
xPortGetFreeHeapSize: 5432
[INFO] vTaskPrint created successful
Key pressed LEFT
xPortGetFreeHeapSize: 4832
[INFO] vTaskPrint created successful
Key pressed RIGHT
xPortGetFreeHeapSize: 4232
[INFO] vTaskPrint created successful
Key pressed LEFT
xPortGetFreeHeapSize: 3632
[INFO] vTaskPrint created successful
Key pressed RIGHT
xPortGetFreeHeapSize: 3032
[INFO] vTaskPrint created successful
Key pressed LEFT
xPortGetFreeHeapSize: 2432
[INFO] vTaskPrint created successful
Key pressed RIGHT
xPortGetFreeHeapSize: 1832
[INFO] vTaskPrint created successful
Key pressed LEFT
xPortGetFreeHeapSize: 1232
[INFO] vTaskPrint created successful
Key pressed RIGHT
xPortGetFreeHeapSize: 632
[INFO] vTaskPrint created successful
Key pressed LEFT
xPortGetFreeHeapSize: 32
[INFO] vTaskPrint created successful
[ERROR] vTaskPrint not created successful

How can I free memory?

like image 354
oscargomezf Avatar asked Dec 26 '14 17:12

oscargomezf


Video Answer


1 Answers

When you delete at task the memory allocated to the task is freed from the Idle task. Therefore you have to let the idle task run at some point. This is clearly stated in the documentation for the function you are calling: http://www.freertos.org/a00126.html

In most applications the idle task will be the task that runs most, so your test is somewhat artificial.

As an aside: It is really not a good idea to call such long functions as printf from a critical section, and you are breaking the FreeRTOS API usage rules by calling xPortGetFreeHeapSize() from a critical section (although you will probably get away with it in this case, the general rule of thumb is not to call RTOS API functions from a critical section or when the scheduler is locked). The FAQ page covering this is here: http://www.freertos.org/FAQHelp.html

There is lots of information, plus a dedicated support forum with FreeRTOS experts waiting, all available for your free use on the FreeRTOS website, so I never understand why people ask FreeRTOS questions anywhere else.

like image 193
Richard Avatar answered Dec 20 '22 22:12

Richard