0
0
FreeRTOSprogramming~20 mins

xTaskNotify() with value in FreeRTOS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FreeRTOS Notification Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this FreeRTOS task notification example?

Consider the following FreeRTOS code snippet where a task is notified with a value. What will be the value printed by the task?

FreeRTOS
#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;
}
ANotification value: 0
BNotification value: 1234
CNotification value: 1
DNotification value: 4294967295
Attempts:
2 left
💡 Hint

Remember that xTaskNotifyWait receives the notification value sent by xTaskNotify when using eSetValueWithOverwrite.

🧠 Conceptual
intermediate
1:30remaining
Which notification action updates the task notification value by adding the new value?

In FreeRTOS, xTaskNotify() can perform different actions on the task's notification value. Which action adds the new value to the current notification value?

AeSetBits
BeSetValueWithoutOverwrite
CeSetValueWithOverwrite
DeIncrement
Attempts:
2 left
💡 Hint

Think about bitwise operations and how notification values can be combined.

Predict Output
advanced
2:00remaining
What happens if you call xTaskNotify() with eSetValueWithoutOverwrite when the notification value is already non-zero?

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?

FreeRTOS
uint32_t ulValue = 5;
// Assume task notification value is currently 5
// Call:
xTaskNotify(xTaskHandle, 10, eSetValueWithoutOverwrite);
// Then task reads notification value
ANotification value remains 5
BNotification value becomes 10
CNotification value becomes 15
DNotification value resets to 0
Attempts:
2 left
💡 Hint

Check what eSetValueWithoutOverwrite does if the notification value is not zero.

🔧 Debug
advanced
2:30remaining
Why does this code cause the task to never unblock after xTaskNotify()?

Examine the code below. The task waits for a notification, but never prints the notification value. What is the likely cause?

FreeRTOS
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 anything
AThe notification value is overwritten with zero inside the task
BThe notification wait timeout is too long, blocking indefinitely
CThe task handle passed to xTaskNotify is NULL
DThe task notification is sent before the task starts waiting, so it misses the notification
Attempts:
2 left
💡 Hint

Consider the timing of when the notification is sent versus when the task waits.

🚀 Application
expert
3:00remaining
How to atomically increment a task's notification value and unblock the task?

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?

AeSetBits
BeSetValueWithOverwrite
CeIncrement
DeSetValueWithoutOverwrite
Attempts:
2 left
💡 Hint

Think about which action increments the notification value atomically.