Previously shared the article “What Features Were Updated in FreeRTOS V10.4.0?” and today we will elaborate on one of the knowledge points: FreeRTOS’s direct task (message) notification, aimed at reducing RAM usage and speeding up execution.
1. Introduction
Almost all RTOS operating systems provide queue and semaphore functionalities, which are essential skills for most beginners.
However, in most cases, they communicate using “intermediary objects” rather than “direct task messages“.
When communicating through “intermediary objects”, each set of queues or semaphores allocates a segment of memory (message buffer and stream buffer). This leads to a problem: if there are many queues or semaphores, it inevitably results in greater memory overhead.
However, if communication is done through the “direct messages” discussed in this article, it will save a lot of memory.
QueueHandle_t xQueue;
xQueue = xQueueCreate(10, sizeof( /* Length */ ) );
Can you calculate how much RAM space this “intermediary object” will occupy?
Understanding intermediary object communication through a code illustration:
Since FreeRTOS V10.4.0, each task has a series of notifications. Each notification contains a 32-bit value and a boolean state, which together consume only5 bytesof RAM.
Just as tasks can block on a binary semaphore waiting for the semaphore to become “available”, tasks can block on notifications waiting for the notification’s state to become “pending”. Similarly, just as tasks can block on a counting semaphore waiting for the semaphore’s count to become non-zero, tasks can block on notifications waiting for the notification’s value to become non-zero. The first example below demonstrates this situation.
Notifications can convey not only events but also data in various ways.
https://www.freertos.org/RTOS-task-notification-API.html
-
RTOS task notifications can only be used when there is only one task that can act as the receiver of the event. However, this condition can be met in most practical use cases, such as when an interrupt interrupts the task that is processing the data received from that interrupt.
-
Only in cases where RTOS task notifications replace queues: the receiving task can wait for notifications in a “blocked” state (thus not consuming any CPU time), while the sending task cannot wait for messages in a “blocked” state. If sending cannot be completed immediately, sending is finished.
/* Prototypes of the two tasks created by main() */static void prvTask1( void *pvParameters );static void prvTask2( void *pvParameters );
/* Handles for the tasks created by main() */static TaskHandle_t xTask1 = NULL, xTask2 = NULL;
/* Create two tasks to send notifications back and forth, then start the RTOS scheduler */void main( void ){ xTaskCreate( prvTask1, "Task1", 200, NULL, tskIDLE_PRIORITY, &xTask1 ); xTaskCreate( prvTask2, "Task2", 200, NULL, tskIDLE_PRIORITY, &xTask2 ); vTaskStartScheduler();}/*———————————————————–*/
/* prvTask1() using the "indexed" version of the API */static void prvTask1( void *pvParameters ){ for( ;; ) { /* Send notification to prvTask2() to unblock it.*/ xTaskNotifyGiveIndexed( xTask2, 0 );
/* Block waiting for prvTask2() to notify this task */ ulTaskNotifyTakeIndexed( 0, pdTRUE, portMAX_DELAY ); }}/*———————————————————–*/
/* prvTask2() using the original version of the API (without "index") */static void prvTask2( void *pvParameters ){ for( ;; ) { /* Wait for prvTask1() to notify this task */ ulTaskNotifyTake( pdTRUE, portMAX_DELAY );
/* Send notification to prvTask1() to unblock it */ xTaskNotifyGive( xTask1 ); }}
https://www.freertos.org/2020/09/decrease-ram-footprint-and-accelerate-execution-with-freertos-notifications.html
1.The 2nd Domestic Embedded Operating System Technology and Industry Development Forum will be held in October by the Qiantang River in Hangzhou.
2.Because he was dissatisfied with existing RTOS, the author wrote it—ChibiOS!
3.The causes and handling strategies of deadlock in operating systems.
4.Linux usage tips you didn’t know~
5.After abandoning Windows, can open-source operating systems become mainstream desktop systems?
6.The mathematician who just won the Physics Nobel Prize: Reality is a distorted response to perfect mathematical truths, and the human brain is the ultimate quantum computer!
Disclaimer: This article is a network reprint, and the copyright belongs to the original author. If there are copyright issues, please contact us, and we will confirm the copyright based on the copyright certificate you provide and pay for the manuscript or delete the content.