freertos vTaskDelete(NULL)没有空闲内存

时间:2014-12-26 17:15:40

标签: cortex-m3 freertos

我开始学习FreeRTOS。刚才我正在尝试用这段代码创建一个打印任务函数:

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

但在14次致电之后:

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

皮层M3内存耗尽:

************** 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

我如何释放记忆?

1 个答案:

答案 0 :(得分:5)

在任务中删除时,分配给任务的内存将从空闲任务中释放。因此,您必须让空闲任务在某个时刻运行。您在呼叫的功能的文档中明确说明了这一点:http://www.freertos.org/a00126.html

在大多数应用程序中,空闲任务将是最常运行的任务,因此您的测试有点人为。

顺便说一下:从一个关键部分调用像printf这样的长函数真的不是一个好主意,而你是通过从关键部分调用xPortGetFreeHeapSize()来打破FreeRTOS API使用规则(尽管你可能会得到)在这种情况下远离它,一般的经验法则是不从关键部分或调度程序被锁定时调用RTOS API函数。有关此内容的常见问题解答页面位于:http://www.freertos.org/FAQHelp.html

有很多信息,加上FreeRTOS专家等待的专用支持论坛,所有这些都可以在FreeRTOS网站上免费使用,所以我不明白为什么人们会在其他任何地方询问FreeRTOS问题。