Quality RTOS & Embedded Software

KERNEL

xTaskNotify / xTaskNotifyIndexed
[RTOS Task Notification API]


task.h

 BaseType_t xTaskNotify( TaskHandle_t xTaskToNotify,
                         uint32_t ulValue,
                         eNotifyAction eAction );

BaseType_t xTaskNotifyIndexed( TaskHandle_t xTaskToNotify, UBaseType_t uxIndexToNotify, uint32_t ulValue, eNotifyAction eAction );

[If you are using RTOS task notifications to implement binary or counting semaphore type behaviour then use the simpler xTaskNotifyGive() API function instead of xTaskNotify()]

Each task has an array of ‘task notifications’ (or just ‘notifications’), each of which has a state and a 32-bit value. A direct to task notification is an event sent directly to a task that can unblock the receiving task, and optionally update one of the receiving task’s notification values in a number of different ways. For example, a notification may overwrite one of the receiving task’s notification values, or just set one or more bits in one of the receiving task’s notification values.

xTaskNotify() is used to send an event directly to and potentially unblock an RTOS task, and optionally update one of the receiving task’s notification values in one of the following ways:

  • Write a 32-bit number to the notification value
  • Add one (increment) the notification value
  • Set one or more bits in the notification value
  • Leave the notification value unchanged

xTaskNotify() and xTaskNotifyIndexed() are equivalent functions – the only difference being xTaskNotifyIndexed() can operate on any task notification within the array and xTaskNotify() always operates on the task notification at array index 0.

This function must not be called from an interrupt service routine (ISR). Use xTaskNotifyFromISR() instead.

configUSE_TASK_NOTIFICATIONS must set to 1 in FreeRTOSConfig.h (or be left undefined) for these functions to be available. The constant configTASK_NOTIFICATION_ARRAY_ENTRIES sets the number of indexes in each task’s array of task notifications.

Backward compatibility information:
Prior to FreeRTOS V10.4.0 each task had a single “notification value”, and all task notification API functions operated on that value. Replacing the single notification value with an array of notification values necessitated a new set of API functions that could address specific notifications within the array. xTaskNotify() is the original API function, and remains backward compatible by always operating on the notification value at index 0 in the array. Calling xTaskNotify() is equivalent to calling xTaskNotifyIndexed() with the uxIndexToNotify parameter set to 0.

Parameters:
xTaskToNotify   The handle of the RTOS task being notified. This is the target task.

To obtain a task’s handle create the task using xTaskCreate() and make use of the pxCreatedTask parameter, or create the task using xTaskCreateStatic() and store the returned value, or use the task’s name in a call to xTaskGetHandle().

The handle of the currently executing RTOS task is returned by the xTaskGetCurrentTaskHandle() API function.

uxIndexToNotify   The index within the target task’s array of notification values to which the notification is to be sent.

uxIndexToNotify must be less than configTASK_NOTIFICATION_ARRAY_ENTRIES.

xTaskNotify() does not have this parameter and always sends notifications to index 0.

ulValue   Used to update the notification value of the target task. See the description of the eAction parameter below.
eAction   An enumerated type that can take one of the values documented in the table below in order to perform the associated action.
eAction Setting Action Performed
eNoAction The target task receives the event, but its notification value is not updated. In this case ulValue is not used.

eSetBits The notification value of the target task will be bitwise ORed with ulValue. For example, if ulValue is set to 0x01, then bit 0 will get set within the target task’s notification value. Likewise if ulValue is 0x04 then bit 2 will get set in the target task’s notification value. In this way the RTOS task notification mechanism can be used as a light weight alternative to an event group.

eIncrement The notification value of the target task will be incremented by one, making the call to xTaskNotify() equivalent to a call to xTaskNotifyGive(). In this case ulValue is not used.
eSetValueWithOverwrite The notification value of the target task is unconditionally set to ulValue. In this way the RTOS task notification mechanism is being used as a light weight alternative to xQueueOverwrite().
eSetValueWithoutOverwrite If the target task does not already have a notification pending then its notification value will be set to ulValue.

If the target task already has a notification pending then its notification value is not updated as to do so would overwrite the previous value before it was used. In this case the call to xTaskNotify() fails and pdFALSE is returned.

In this way the RTOS task notification mechanism is being used as a light weight alternative to xQueueSend() on a queue of length 1.

Returns:
pdPASS is returned in all cases other than when eAction is set to eSetValueWithoutOverwrite and the target task’s notification value cannot be updated because the target task already had a notification pending.


Example usage:

[More examples are referenced from the main RTOS task notifications page]



/* Set bit 8 in the 0th notification value of the task referenced by
xTask1Handle. */

xTaskNotifyIndexed( xTask1Handle, 0, ( 1UL << 8UL ), eSetBits );

/* Send a notification to the task referenced by xTask2Handle, potentially
removing the task from the Blocked state, but without updating the task’s
notification value. */

xTaskNotify( xTask2Handle, 0, eNoAction );

/* Set the notification value of the task referenced by xTask3Handle to 0x50,
even if the task had not read its previous notification value. */

xTaskNotify( xTask3Handle, 0x50, eSetValueWithOverwrite );

/* Set the notification value of the task referenced by xTask4Handle to 0xfff,
but only if to do so would not overwrite the task’s existing notification
value before the task had obtained it (by a call to xTaskNotifyWait()
or ulTaskNotifyTake()). */

if( xTaskNotify( xTask4Handle, 0xfff, eSetValueWithoutOverwrite ) == pdPASS )
{
/* The task’s notification value was updated. */
}
else
{
/* The task’s notification value was not updated. */
}






Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.