0
0
FreeRTOSprogramming~5 mins

Why task notifications are lightweight in FreeRTOS

Choose your learning style9 modes available
Introduction

Task notifications let tasks send simple messages quickly without using extra memory. They are fast and use little CPU power.

When a task needs to quickly tell another task that an event happened.
When you want to avoid using bigger communication tools like queues or semaphores.
When you want to save memory in small embedded systems.
When you want to keep your program responsive by reducing waiting time.
When you want simple signaling between tasks without complex data.
Syntax
FreeRTOS
BaseType_t xTaskNotify(
    TaskHandle_t xTaskToNotify,
    uint32_t ulValue,
    eNotifyAction eAction
);

xTaskToNotify is the task you want to notify.

ulValue is the number or flag you send with the notification.

Examples
Send a notification without changing the value.
FreeRTOS
xTaskNotify(taskHandle, 0, eNoAction);
Set bit 1 in the notification value to signal an event.
FreeRTOS
xTaskNotify(taskHandle, 2, eSetBits);
Overwrite the notification value with 5.
FreeRTOS
xTaskNotify(taskHandle, 5, eSetValueWithOverwrite);
Sample Program

This program creates two tasks. Task1 sends a notification with value 42 to Task2. Task2 waits for the notification and prints the value it received.

FreeRTOS
#include "FreeRTOS.h"
#include "task.h"
#include <stdio.h>

TaskHandle_t Task1Handle = NULL;
TaskHandle_t Task2Handle = NULL;

void Task1(void *pvParameters) {
    printf("Task1: Sending notification to Task2\n");
    xTaskNotify(Task2Handle, 42, eSetValueWithOverwrite);
    vTaskDelete(NULL);
}

void Task2(void *pvParameters) {
    uint32_t ulNotificationValue = 0;
    xTaskNotifyWait(0x00, 0xFFFFFFFF, &ulNotificationValue, portMAX_DELAY);
    printf("Task2: Received notification with value %lu\n", ulNotificationValue);
    vTaskDelete(NULL);
}

int main(void) {
    xTaskCreate(Task2, "Task2", 1000, NULL, 1, &Task2Handle);
    xTaskCreate(Task1, "Task1", 1000, NULL, 1, &Task1Handle);
    vTaskStartScheduler();
    return 0;
}
OutputSuccess
Important Notes

Task notifications use the task's own memory, so no extra memory is needed.

They are faster than queues because they avoid copying data.

Only one notification value is stored per task, so they are best for simple signals.

Summary

Task notifications are a quick way for tasks to send simple signals.

They use less memory and CPU than other communication methods.

Best for small messages or flags, not for large data.