Consider the following FreeRTOS code snippet where a task is notified with a value. What will be the value printed by the task?
#include "FreeRTOS.h" #include "task.h" #include <stdio.h> TaskHandle_t xTaskHandle = NULL; void vTaskFunction(void *pvParameters) { uint32_t ulNotificationValue = 0; xTaskNotifyWait(0x00, 0xffffffff, &ulNotificationValue, portMAX_DELAY); printf("Notification value: %lu\n", ulNotificationValue); vTaskDelete(NULL); } int main(void) { xTaskCreate(vTaskFunction, "Task", configMINIMAL_STACK_SIZE, NULL, 1, &xTaskHandle); vTaskStartScheduler(); vTaskDelay(10); xTaskNotify(xTaskHandle, 1234, eSetValueWithOverwrite); return 0; }
Remember that xTaskNotifyWait receives the notification value sent by xTaskNotify when using eSetValueWithOverwrite.
The xTaskNotify function with eSetValueWithOverwrite sets the notification value directly. The task waits for the notification and receives the value 1234, which is then printed.
In FreeRTOS, xTaskNotify() can perform different actions on the task's notification value. Which action adds the new value to the current notification value?
Think about bitwise operations and how notification values can be combined.
The eSetBits action performs a bitwise OR between the current notification value and the new value, effectively adding bits. This is different from incrementing or overwriting.
Given a task with a current notification value of 5, what will be the notification value after calling xTaskNotify() with value 10 and action eSetValueWithoutOverwrite?
uint32_t ulValue = 5; // Assume task notification value is currently 5 // Call: xTaskNotify(xTaskHandle, 10, eSetValueWithoutOverwrite); // Then task reads notification value
Check what eSetValueWithoutOverwrite does if the notification value is not zero.
The eSetValueWithoutOverwrite action only sets the notification value if it is zero. Since the current value is 5 (non-zero), the notification value remains unchanged.
Examine the code below. The task waits for a notification, but never prints the notification value. What is the likely cause?
void vTaskFunction(void *pvParameters) {
uint32_t ulNotificationValue = 0;
xTaskNotifyWait(0x00, 0xffffffff, &ulNotificationValue, 1000 / portTICK_PERIOD_MS);
printf("Notification value: %lu\n", ulNotificationValue);
vTaskDelete(NULL);
}
// In main or another task:
xTaskNotify(xTaskHandle, 42, eSetValueWithOverwrite);
// But task never prints anythingConsider the timing of when the notification is sent versus when the task waits.
If the notification is sent before the task calls xTaskNotifyWait, the notification is lost because the task was not yet waiting. The task then times out and prints zero.
You want to safely increment a task's notification value by 1 and unblock the task if it is waiting. Which xTaskNotify() action should you use?
Think about which action increments the notification value atomically.
The eIncrement action atomically increments the task's notification value by 1 and unblocks the task if it was waiting. This is the correct choice for incrementing safely.