Bird
Raised Fist0
FreeRTOSprogramming~5 mins

xTaskNotify() with value in FreeRTOS

Choose your learning style10 modes available
Introduction

xTaskNotify() lets one task send a simple number or signal to another task. This helps tasks talk to each other without complex tools.

When you want to tell a task to start or stop doing something.
When you want to send a small number or code to another task quickly.
When you want to signal a task that an event happened.
When you want to update a task with a new value without using queues.
When you want simple task-to-task communication with low overhead.
Syntax
FreeRTOS
BaseType_t xTaskNotify(
    TaskHandle_t xTaskToNotify,
    uint32_t ulValue,
    eNotifyAction eAction
);

xTaskToNotify is the task that will receive the notification.

ulValue is the number you want to send.

eAction decides how the value is sent (set, increment, bitwise OR, etc.).

Examples
This sends the value 10 to the task, replacing any old value.
FreeRTOS
xTaskNotify(taskHandle, 10, eSetValueWithOverwrite);
This adds 1 to the current notification value of the task.
FreeRTOS
xTaskNotify(taskHandle, 1, eIncrement);
This sets bit 0 of the task's notification value.
FreeRTOS
xTaskNotify(taskHandle, 0x01, eSetBits);
Sample Program

This program creates two tasks. The SenderTask waits 1 second, then sends the number 42 to ReceiverTask using xTaskNotify(). ReceiverTask waits for the notification and prints the received value.

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

TaskHandle_t taskHandle;

void ReceiverTask(void *params) {
    uint32_t notifiedValue = 0;
    while (1) {
        // Wait for notification and get the value
        xTaskNotifyWait(0x00, 0xFFFFFFFF, &notifiedValue, portMAX_DELAY);
        printf("Received notification with value: %lu\n", notifiedValue);
    }
}

void SenderTask(void *params) {
    vTaskDelay(pdMS_TO_TICKS(1000)); // Wait 1 second
    xTaskNotify(taskHandle, 42, eSetValueWithOverwrite);
    vTaskDelete(NULL);
}

int main(void) {
    xTaskCreate(ReceiverTask, "Receiver", 1000, NULL, 1, &taskHandle);
    xTaskCreate(SenderTask, "Sender", 1000, NULL, 1, NULL);
    vTaskStartScheduler();
    return 0;
}
OutputSuccess
Important Notes

Use eSetValueWithOverwrite to replace the old value, or eIncrement to add to it.

Notifications are faster and use less memory than queues for simple signals.

Always check the return value of xTaskNotify() to ensure the notification was sent.

Summary

xTaskNotify() sends a number or signal from one task to another.

You choose how the number is sent using eNotifyAction.

This is a simple and fast way for tasks to communicate small data.

Practice

(1/5)
1.

What does the xTaskNotify() function primarily do in FreeRTOS?

easy
A. Creates a new task in the system
B. Deletes a task from the scheduler
C. Sends a notification value from one task to another
D. Suspends the current task indefinitely

Solution

  1. Step 1: Understand the purpose of xTaskNotify()

    The function is designed to send a notification value or signal from one task to another in FreeRTOS.
  2. Step 2: Compare with other task functions

    Creating, deleting, or suspending tasks are done by other FreeRTOS functions, not xTaskNotify().
  3. Final Answer:

    Sends a notification value from one task to another -> Option C
  4. Quick Check:

    xTaskNotify() sends notification value [OK]
Hint: Remember: xTaskNotify() sends signals between tasks [OK]
Common Mistakes:
  • Confusing xTaskNotify() with task creation functions
  • Thinking it suspends or deletes tasks
  • Assuming it handles task priorities
2.

Which of the following is the correct syntax to send a notification with a value using xTaskNotify()?

BaseType_t xTaskNotify( TaskHandle_t xTaskToNotify, uint32_t ulValue, eNotifyAction eAction );
easy
A. xTaskNotify(taskHandle, 10, eSetBits);
B. xTaskNotify(10, taskHandle, eSetValueWithOverwrite);
C. xTaskNotify(taskHandle, eSetValueWithoutOverwrite, 10);
D. xTaskNotify(eSetValueWithOverwrite, taskHandle, 10);

Solution

  1. Step 1: Identify parameter order in xTaskNotify()

    The correct order is: TaskHandle_t, uint32_t value, eNotifyAction.
  2. Step 2: Match options to correct order

    xTaskNotify(taskHandle, 10, eSetBits); matches the correct order: taskHandle, 10, eSetBits. Others mix parameter order incorrectly.
  3. Final Answer:

    xTaskNotify(taskHandle, 10, eSetBits); -> Option A
  4. Quick Check:

    Correct parameter order: task, value, action [OK]
Hint: Remember parameter order: task, value, action [OK]
Common Mistakes:
  • Swapping value and task handle parameters
  • Using wrong enum for eNotifyAction
  • Passing parameters in wrong order
3.

Consider the following code snippet:

uint32_t notifiedValue = 0;

// Task A sends notification
xTaskNotify(taskBHandle, 0x04, eSetBits);

// Task B waits for notification
xTaskNotifyWait(0x00, 0xFFFFFFFF, ¬ifiedValue, portMAX_DELAY);

// What is the value of notifiedValue after notification?
medium
A. 0x04
B. 0x00
C. 0xFFFFFFFF
D. Undefined

Solution

  1. Step 1: Understand eSetBits action

    eSetBits sets bits in the notification value. Since initial is 0, setting bit 0x04 results in notifiedValue = 0x04.
  2. Step 2: Check xTaskNotifyWait parameters

    It clears bits 0x00 (none) and waits indefinitely, storing the notified bits in notifiedValue.
  3. Final Answer:

    0x04 -> Option A
  4. Quick Check:

    eSetBits sets bits = 0x04 [OK]
Hint: eSetBits adds bits to notified value [OK]
Common Mistakes:
  • Assuming notifiedValue remains zero
  • Confusing eSetBits with eSetValueWithoutOverwrite
  • Ignoring the effect of xTaskNotifyWait
4.

Identify the error in the following code snippet using xTaskNotify():

BaseType_t status;
status = xTaskNotify(NULL, 5, eSetValueWithOverwrite);
if(status == pdPASS) {
    // Notification sent
}
medium
A. Missing return statement in function
B. Using eSetValueWithOverwrite is invalid
C. Notification value must be zero
D. Passing NULL as task handle causes error

Solution

  1. Step 1: Check task handle parameter

    xTaskNotify requires a valid task handle; NULL is invalid and causes failure.
  2. Step 2: Validate other parameters

    eSetValueWithOverwrite and value 5 are valid; no return statement needed here.
  3. Final Answer:

    Passing NULL as task handle causes error -> Option D
  4. Quick Check:

    Task handle NULL invalid [OK]
Hint: Task handle must never be NULL [OK]
Common Mistakes:
  • Passing NULL as task handle
  • Thinking notification value must be zero
  • Confusing eNotifyAction enums
5.

You want to send a notification value from Task A to Task B, but only if Task B's current notification value is zero. Which eNotifyAction should you use with xTaskNotify() to achieve this?

hard
A. eSetValueWithOverwrite
B. eSetValueWithoutOverwrite
C. eIncrement
D. eNoAction

Solution

  1. Step 1: Understand eNotifyAction options

    eSetValueWithoutOverwrite sets the notification value only if it is currently zero.
  2. Step 2: Compare with other actions

    eSetValueWithOverwrite always overwrites; eIncrement adds 1; eNoAction sends no value.
  3. Final Answer:

    eSetValueWithoutOverwrite -> Option B
  4. Quick Check:

    Set only if zero = eSetValueWithoutOverwrite [OK]
Hint: Use eSetValueWithoutOverwrite to avoid overwriting [OK]
Common Mistakes:
  • Using eSetValueWithOverwrite and overwriting value
  • Choosing eIncrement which changes value incorrectly
  • Selecting eNoAction which sends no value
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard