How to Improve FreeRTOS Performance and Reduce RAM Usage

Follow+Star Public Account Number, don’t miss wonderful content

How to Improve FreeRTOS Performance and Reduce RAM Usage

Author | strongerHuang

WeChat Public Account | Embedded Column

Previously shared “What Features Have Been Updated in FreeRTOS V10.4.0?“, today we will detail one knowledge point: the direct task (message) notification of FreeRTOS, which aims to reduce RAM usage and speed up execution.

Embedded Column

1

Introduction
Almost all RTOS operating systems provide queue and semaphore functions; for most beginners, using queues and semaphores is essential skills.

However, in most cases, they communicate using “intermediary objects” rather than “direct task messages“.

Communicating through “intermediary objects” allocates a segment of memory (message buffer and stream buffer) for each set of queues or semaphores. The problem is that if there are many queues or semaphores, it will inevitably lead to greater memory expenditure.

However, if communication is done through the “direct messages” described in this article, a lot of memory can be saved.

Embedded Column

2

What is Direct Task Notification?
Most inter-task communication methods useintermediary objects, such as queues, semaphores, or event groups. The sending task writes to the communication object, and the receiving task reads from the communication object.
For example, in FreeRTOS queue communication, a queue must be defined before creating the queue:
QueueHandle_t  xQueue;
xQueue = xQueueCreate(10,  sizeof( /* length */ ) );
This queue contains many intermediary objects:

How to Improve FreeRTOS Performance and Reduce RAM Usage

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

Understand intermediary object communication through a code diagram:

How to Improve FreeRTOS Performance and Reduce RAM Usage

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

How to Improve FreeRTOS Performance and Reduce RAM Usage

Since FreeRTOS V10.4.0, each task has a series of notifications. Each notification contains a 32-bit value and a boolean status, consuming only5 bytesof RAM.

Just as a task can block a binary semaphore waiting for that semaphore to become “available”, a task can block a notification waiting for the notification’s status to become “pending”. Similarly, just as a task can block a counting semaphore waiting for the count of that semaphore to become non-zero, a task can block a notification waiting for the value of that notification to become non-zero. The first example below demonstrates this situation.

Notifications can convey not only events but also data in various ways.

Embedded Column

3

Further Analysis of Direct Task Notification
By comparingFreeRTOS V10.4.0with previous versions, you will find thatV10.4.0has added some APIs, such as ulTaskNotifyTake / ulTaskNotifyTakeIndexed:

How to Improve FreeRTOS Performance and Reduce RAM Usage

There are detailed introductions and explanations for these APIs on the official website, as well as application code examples:

How to Improve FreeRTOS Performance and Reduce RAM Usage

Direct task communication API description address:

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

(Public account does not support external links, please copy the link to the browser to open)

Embedded Column

4

Performance Advantages and Usage Limitations of Direct Task Notification
The flexibility of task notifications allows them to be used when there is a need to create separate queues, binary semaphores, counting semaphores, or event groups.
Using direct notifications to unblock RTOS tasks is 45% faster(from official data)compared to using intermediary objects (such as semaphores), and it uses less RAM.
Of course, with these performance advantages, there are certainly some limitations:
  • RTOS task notifications can only be used when only one task 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 processing, and that task will handle the data received from the interrupt.

  • Only when using RTOS task notifications instead of queues: the receiving task can wait for a notification in a “blocked” state (thus not consuming any CPU time), while the sending task cannot wait for a message in a “blocked” state. If the sending cannot be completed immediately, the sending completes.

Embedded Column

5

Usage Method
The usage method 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.
I will also explain with an official example:
/* Prototype of the two tasks created by main() */static void prvTask1( void *pvParameters );static void prvTask2( void *pvParameters );
/* Handles for 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() uses 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() uses 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 article ends here; 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

———— END ————

Reply in the background with 『RTOS』『FreeRTOS』『Microcontroller』 to read more related articles.

FollowWeChat Public Account『Embedded Column』, view more content in the bottom menu, reply “Join Group” to join the technical exchange group according to the rules.

How to Improve FreeRTOS Performance and Reduce RAM Usage

Click “Read the original” to see more shares, and welcome to share, collect, like, and view.

Leave a Comment

×