How FreeRTOS Reduces RAM Usage and Speeds Up Execution

How FreeRTOS Reduces RAM Usage and Speeds Up Execution

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.

2. What Are Direct Task Notifications?
Most methods of inter-task communication use intermediary objects, such as queues, semaphores, or event groups. The sending task writes to the communication object, while the receiving task reads from it.
For example, in FreeRTOS queue communication, you must first define a queue before creating it:
QueueHandle_t  xQueue;
xQueue = xQueueCreate(10,  sizeof( /* Length */ ) );
This queue contains many intermediary objects:

How FreeRTOS Reduces RAM Usage and Speeds Up Execution

Can you calculate how much RAM space this “intermediary object” will occupy?

Understanding intermediary object communication through a code illustration:

How FreeRTOS Reduces RAM Usage and Speeds Up Execution

Direct Task Notification:
When using direct task notifications, as the name suggests, the sending task directly sends notifications to the receiving task without an intermediary object.
Understanding through a code illustration:

How FreeRTOS Reduces RAM Usage and Speeds Up Execution

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.

3. Further Analysis of Direct Task Notifications
By comparing FreeRTOS V10.4.0 with previous versions, you will find that V10.4.0 has added some APIs, such as ulTaskNotifyTake / ulTaskNotifyTakeIndexed:

How FreeRTOS Reduces RAM Usage and Speeds Up Execution

The official website also has detailed introductions and explanations for these APIs, as well as application code examples:

How FreeRTOS Reduces RAM Usage and Speeds Up Execution

Direct Task Communication API Documentation:

https://www.freertos.org/RTOS-task-notification-API.html

(The public account does not support external links, please copy the link to your browser to open)
4. Performance Advantages and Limitations of Using Direct Task Notifications
The flexibility of task notifications allows them to be used when separate queues, binary semaphores, counting semaphores, or event groups need to be created.
Compared to using intermediary objects (such as semaphores) to unblock tasks, using direct notifications can unblock RTOS tasks 45% faster(according to official data) and uses less RAM.
Of course, with these performance advantages, there are also some limitations:
  • 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.

5. Usage
The usage is actually quite simple; as long as you know how to use RTOS queues and semaphores, you can basically look at the official examples to use it.
Here, I will also explain with an official example:
/* 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 );    }}
This concludes the article; for more content, please refer to the official documentation:

https://www.freertos.org/2020/09/decrease-ram-footprint-and-accelerate-execution-with-freertos-notifications.html

How FreeRTOS Reduces RAM Usage and Speeds Up Execution

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!

How FreeRTOS Reduces RAM Usage and Speeds Up Execution

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.

Leave a Comment