Task notifications let tasks send simple messages quickly without using extra memory. They are fast and use little CPU power.
Why task notifications are lightweight in 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.
xTaskNotify(taskHandle, 0, eNoAction);xTaskNotify(taskHandle, 2, eSetBits);xTaskNotify(taskHandle, 5, eSetValueWithOverwrite);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.
#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; }
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.
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.
Practice
Solution
Step 1: Understand task notification purpose
Task notifications are designed to send simple signals or flags between tasks.Step 2: Analyze resource usage
They avoid heavy data copying and complex synchronization, using minimal CPU and memory.Final Answer:
Because they use minimal CPU and memory resources -> Option BQuick Check:
Lightweight means low resource use = C [OK]
- Thinking task notifications handle large data
- Confusing with hardware interrupts
- Assuming complex synchronization is needed
Solution
Step 1: Recall FreeRTOS notification API
The functionxTaskNotify()sends a notification with a value and action.Step 2: Identify correct syntax
xTaskNotify(taskHandle, value, eSetBits);is the correct call to send a notification with bits set.Final Answer:
xTaskNotify(taskHandle, value, eSetBits); -> Option AQuick Check:
Sending notification uses xTaskNotify with action [OK]
- Using xTaskNotifyGive without parameters
- Confusing send and receive functions
- Using non-existent functions like xTaskNotifySend
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
xTaskNotifyFromISR(taskHandle, 0x01, eSetBits, &xHigherPriorityTaskWoken);
if(xHigherPriorityTaskWoken) {
portYIELD_FROM_ISR();
}
What is the main reason this notification method is lightweight?Solution
Step 1: Analyze notification method in ISR
The functionxTaskNotifyFromISR()sends a notification directly to a task without using queues.Step 2: Understand why this is lightweight
Direct notification avoids overhead of queue management and data copying, making it fast and low resource.Final Answer:
It uses direct-to-task notification without queues -> Option AQuick Check:
Direct notification avoids queues = A [OK]
- Thinking it copies large data buffers
- Assuming interrupts are disabled fully
- Believing it creates new tasks
void notifyTask() {
xTaskNotify(taskHandle, 0x01, eSetValueWithOverwrite);
}
But the task never receives the notification. What is the likely problem?Solution
Step 1: Check task handle validity
IftaskHandleis NULL or invalid, notification cannot be delivered.Step 2: Verify notification action and function
eSetValueWithOverwriteis valid, andxTaskNotifyis correct for sending notifications.Final Answer:
The taskHandle is NULL or invalid -> Option CQuick Check:
Invalid task handle blocks notification = A [OK]
- Using wrong notification action
- Assuming queues are needed for notifications
- Confusing xTaskNotifyGive with xTaskNotify
Solution
Step 1: Understand notification capabilities
Task notifications can hold a 32-bit value, ideal for bitwise flags representing multiple events.Step 2: Choose efficient notification method
UsingeSetBitsaction sets bits without overwriting, allowing multiple event flags in one notification.Final Answer:
Use a single notification value with bitwise flags and eSetBits action -> Option DQuick Check:
Bitwise flags + eSetBits = efficient lightweight signaling [OK]
- Overwriting notification value losing events
- Using queues which add overhead
- Using global variables risking race conditions
Ready to prove it?
Pass the test and this pattern moves unknown
