FreeRTOS task notification | FreeRTOS twelve

Table of contents

illustrate:

1. Task notification

1.1. What is a task notification?

1.2. Advantages and Disadvantages of Task Notification

1.3. How to update task notification value

1.4. Task notification value status

1.5. Task notification status

1.6. Task notification method type

2. Task notification related API functions

2.1. Commonly used notification API functions

2.2. Send notification function with notification value

2.3. Commonly used API functions for receiving notifications

2.4. ulTaskGenericNotifyTake function

2.5. xTaskGenericNotifyWait function

2.6. Best usage occasions


illustrate:

About the content:

    1) The following contents are mostly conceptual understanding and step analysis

    2) There is no personal sample code yet. The official sample code of FreeRTOS is used.

    3) If you want to transplant the code for testing, please look elsewhere. There is no personal sample code for testing in the following content.

About others:

    1) Operating system: win 10

    2) Platform: keil 5 mdk

    3) Language: c language

    4) Board: STM32 series transplanted to FreeRTOS
 

1. Task notification

1.1. What is a task notification?

       Used to notify tasks , the structure member ulNotifiedValue in the task control block is the notification value.

1.2. Advantages and Disadvantages of Task Notification

Advantage:

        1) More efficient, using task notifications to send events or data to tasks is much faster than using queues, event flag groups or semaphores;

        2) The memory used is small, and the corresponding structure needs to be created when using other methods. There is no need to create a structure when using task notification.

Disadvantages:

        1) Data cannot be sent to the ISR (interruption). The ISR has no structure (through the structure member ulNotifiedValue), so data cannot be sent to the ISR. But ISR can use task notifications to send data to tasks;

        2) Multiple tasks cannot be broadcast, and task notifications can only be received and processed by a specified task;

        3) Multiple data cannot be cached. Task notifications send data by updating the task notification value. There is only one task notification value in the task structure and only one data can be saved;

        4) Blocked sending is not supported, and the sender cannot enter the blocking state to wait.

1.3. How to update task notification value

        1) Does not overwrite the notification value of the accepted task;

        2) Override the notification value of the accepted task;

        3) Update one or more bits of the accepted task notification value;

        4) Increase the notification value of accepting tasks.

The types are as follows:

        1) Count value (numeric accumulation, type semaphore)

        2) Corresponding bit, set to one (similar to event flag group)

        3) Any value (supports overwriting or not, similar to a queue)

The above update methods, as long as they are reasonable and flexibly utilize the characteristics of task notifications, can replace queues, semaphores, and event flag groups in some situations.

1.4. Task notification value status

Each task has a structure: task control block TCB, and there are two structure member variables, as shown in Figure 1 below:

figure 1

Note: The uint32_t type is used to represent the notification value; the uint8_t type is used to represent the notification status;
 

1.5. Task notification status

There are three values ​​for task notification status, as shown in Figure 2 below:

figure 2

 Name, the task is not waiting for notification, meaning: the default initialization state of task notification

 Name, waiting for notification, meaning: the receiver is ready (the receiving task notification function has been called at this time), waiting for notification from the sender

 Name, waiting to receive, meaning: the sender has sent (the sending task notification function has been called at this time), waiting for the receiver to receive

1.6. Task notification method type

Code:

typedef enum
{
    eNoAction = 0,            /* Notify the task without updating its notify value. */
    eSetBits,                 /* Set bits in the task's notification value. */
    eIncrement,               /* Increment the task's notification value. */
    eSetValueWithOverwrite,   /* Set the task's notification value to a specific value even if the previous value has not yet been read by the task. */
    eSetValueWithoutOverwrite /* Set the task's notification value if the previous value has been read by the task. */
} eNotifyAction;

Parameter meaning:

Name, eNoAction, meaning: no operation

Name, eSetBits, meaning: update the specified bit

Name, eIncrement, meaning: notification value +1

Name, eSetValueWithOverwrite, meaning: update notification value in overwrite mode

Name, eSetValueWithoutOverwrite, meaning: update notification value without overwriting

2. Task notification related API functions

2.1. Commonly used notification API functions

As shown in Figure 4 below:

 Figure 4

2.2. Send notification function with notification value

Code:

BaseType_t xTaskGenericNotify( TaskHandle_t xTaskToNotify,
                               UBaseType_t uxIndexToNotify,
                               uint32_t ulValue,
                               eNotifyAction eAction,
                               uint32_t * pulPreviousNotificationValue ) PRIVILEGED_FUNCTION;

Parameter meaning:

Name, xTaskToNotify, meaning: task handle to receive task notifications

Name, uxIndexToNotify, meaning: specified notification of the task

Name, ulValue, meaning: task notification value

Name, eAction, meaning: notification method (notification value relationship method)

Name, pulPreviousNotificationValue, meaning: used to save the task notification value before update (NULL is not saved)

2.3. Commonly used API functions for receiving notifications

Function name: ulTaskNotifyTask()

Function: Get task notification, you can set the task notification value to be cleared to zero or -1 when exiting this function; when the task notification is used as a binary semaphore or counting semaphore, use this function to obtain the semaphore.

Function name: xTaskNotifyWait()

Function: Get task notification, which is more complex than ulTaskNotifyTask. It can get the notification value and clear the specified bit of the notification value.

2.4. ulTaskGenericNotifyTake function

Code:

#define ulTaskNotifyTake( xClearCountOnExit, xTicksToWait ) \
    ulTaskGenericNotifyTake( ( tskDEFAULT_INDEX_TO_NOTIFY ), ( xClearCountOnExit ), ( xTicksToWait ) )

Parameter meaning:

Name, tskDEFAULT_INDEX_TO_NOTIFY, meaning: specified notification of the task

Name, xClearCountOnExit, meaning: After the specified task successfully receives the notification, it will clear the notification value to zero or -1; pdTRUE-->clear the notification value to zero, pdFALSE-->set the notification value to -1

Name, xTicksToWait, meaning: maximum time to block waiting for task notification value

Return value meaning:

Return, 0, meaning: reception failed

Return, non-0, meaning: successful reception, return the notification value of the task notification

2.5. xTaskGenericNotifyWait function

Code:

#define xTaskNotifyWait( ulBitsToClearOnEntry, ulBitsToClearOnExit, pulNotificationValue, xTicksToWait ) \
    xTaskGenericNotifyWait( tskDEFAULT_INDEX_TO_NOTIFY, ( ulBitsToClearOnEntry ), ( ulBitsToClearOnExit ), ( pulNotificationValue ), ( xTicksToWait ) )

Parameter meaning:

Name, tskDEFAULT_INDEX_TO_NOTIFY, meaning: specified notification of the task

Name, ulBitsToClearOnEntry, meaning: Waiting to clear the bits notified by the specified task

Name, ulBitsToClearOnExit, meaning: clear the specified task notification value bit after successful waiting.

Name, pulNotificationValue, meaning: used to retrieve the notification value (set to NULL if not used)

Name, xTicksToWait, meaning: maximum time to block waiting for task notification value

Return value meaning:

Return, pdTRUE, meaning: wait for task notification success

Return, pdFALSE, meaning: Failure to wait for task notification

Note: This function is used to obtain the specified bit value of the notification value and clear the notification value. It is suitable for the simulation queue and event flag group. Use this function to obtain the task.

2.6. Best usage occasions

        1) When task notification is used as a semaphore, use the function to obtain the semaphore: ulTaskNotifyTask()

        2) When task notification is used as an event flag group or queue, use the function to obtain: xTaskNotifyWait()

Guess you like

Origin blog.csdn.net/qq_57663276/article/details/128994307