Date: abril 10, 2025
Author: Guillermo Garcia
Categories: RTOS Tags: FreeRTOS
Task Notifications in FreeRTOS is crucial for performance and resource management. FreeRTOS provides various mechanisms for task synchronization, such as queues, semaphores, and event groups. However, Task Notifications offer a lightweight and fast alternative for simple inter-task communication. This article explores how Task Notifications work in FreeRTOS.
Table of Contents
Task Notifications allow a FreeRTOS task to send and receive signals without the overhead of traditional synchronization mechanisms. Each task has a 32-bit notification value that can be updated by another task or an interrupt service routine (ISR).
Task notifications can be handled with the following APIs:
The next API function is the basic function to send a task notification.
BaseType_t xTaskNotify( TaskHandle_t xTaskToNotify, /* Handle of the freeRTOS task that going to receive the notification */ uint32_t ulValue, /* Used to change the notification value of the target task */ eNotifyAction eAction /* Set the action to do with the notification value, actions is defined in the enum eNotifyAction */ );
When the task uses the next API function, to receive the notification, the task can change the state to “not pending“.
BaseType_t xTaskNotifyWait( uint32_t ulBitsToClearOnEntry, /* Clear specific bits of task notification value when task start to waiting */ uint32_t ulBitsToClearOnExit, /* Clear specific bits of task notification value when task finish to waiting */ uint32_t *pulNotificationValue, /* The variable were the Task notification value will be passed */ TickType_t xTicksToWait /* The maximum time to wait in the Blocked state for a notification to be received */ );
To use Task notification remember to enable it setting in 1 the definition value:
#define configUSE_TASK_NOTIFICATIONS 1
Each RTOS task has an array of task notifications. configTASK_NOTIFICATION_ARRAY_ENTRIES
sets the number of indexes in the array.
#define configTASK_NOTIFICATION_ARRAY_ENTRIES 3
Previously was explained as a basic example but Direct task notification can be used in other ways, for example:
Task Notification is a mechanism that enables tasks to communicate by sending data to each other. However, there are specific restrictions to be aware of. Each notification can only carry a single 32-bit data value at a time. This means that you cannot send multiple values simultaneously or use other data types than 32-bit integers.
To illustrate how Task Notification works, we will use the basic example provided on the main page. We will modify the example to demonstrate how task notifications can be used with data values.
#include "Notifications.h" TaskHandle_t TaskH1 = NULL; TaskHandle_t TaskH2 = NULL; static void vTask1(void* NotUsed) { UNUSED(NotUsed); uint32_t MsgToSend = 0; while(1) { /* Send the notification with the message */ xTaskNotify(TaskH2, MsgToSend, eSetValueWithOverwrite); /* Increment the Value to send another data the next execution */ MsgToSend++; vTaskDelay(1000); } } static void vTask2(void* NotUsed) { UNUSED(NotUsed); uint32_t ulNotifiedValue; while(1) { /* Receive the task notification message */ xTaskNotifyWait(0, 0, &ulNotifiedValue, portMAX_DELAY); SEGGER_SYSVIEW_PrintfHost("Message Received: %d", ulNotifiedValue); vTaskDelay(100); } } void Task_Communication_fast(void) { /* Create tasks */ xTaskCreate(vTask1,"Tarea 1",configMINIMAL_STACK_SIZE, NULL,tskIDLE_PRIORITY+1,&TaskH1); xTaskCreate(vTask2,"Tarea 2",configMINIMAL_STACK_SIZE, NULL,tskIDLE_PRIORITY+1,&TaskH1); } int main() { HAL_SYSTICK_Config(SystemCoreClock / (1000U / (uint32_t)uwTickFreq)); HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0U); HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4); traceSTART(); Task_Communication_fast(); vTaskStartScheduler(); }
Task 1 sends data via notification to task 2 which enters execution mode when it receives the data.
Task 2 enters blocked mode waiting for a new notification from task 1.
After the blocking time, task 2 receives another piece of data again via notification.
Task Notifications in FreeRTOS provide an efficient, lightweight alternative for task synchronization. With low overhead and high-speed execution, they are ideal for real-time applications. Developers should use these tailored features before older FreeRTOS features because they are smaller and faster, but new FreeRTOS developers often overlook them because the concepts don’t appear in standard OS texts
Deja una respuesta