Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement Task Notification in FreeRTOS Real-time operating system

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "how to realize the task notification of the FreeRTOS real-time operating system". The editor shows you the operation process through the actual case, and the operation method is simple, fast and practical. I hope this article "how to realize the task notification of the FreeRTOS real-time operating system" can help you solve the problem.

Preface

Note: this article introduces the basics of task notification. For detailed source code analysis, see FreeRTOS Advanced 8---FreeRTOS Task Notification Analysis

Each RTOS task has a 32-bit notification value, which is initialized to 0 when the task is created. RTOS task notification is equivalent to sending an event directly to the task, and the task that receives the notification can unblock the state, provided that the blocking event is caused by waiting for the notification. When sending a notification, you can optionally change the notification value of the receiving task.

You can notify the receiving task of updates in the following ways:

Do not override the notification value of the received task

Override the notification value of the received task

Set some bits that receive task notification values

Increase the notification value of receiving tasks

Using task notifications is obviously more flexible than when you have to create queues, binary semaphores, count semaphores, or event groups before using them. Better yet, using task notifications can be 45% faster and use less RAM (using the GCC compiler,-O2 optimization level) than using semaphores to unblock tasks.

Send notifications using the API functions xTaskNotify () and xTaskNotifyGive () (interrupt protection equivalents are xTaskNotifyFromISR () and vTaskNotifyGiveFromISR ()), which are held until the receiving RTOS task calls the API function xTaskNotifyWait () or ulTaskNotifyTake (). If the receiving RTOS task has entered the blocking state because it is waiting for the notification, the task unblocks and clears the notification after receiving the notification.

The RTOS task notification feature is enabled by default and can be disabled by setting the macro configUSE _ TASK_NOTIFICATIONS to 0 in the file FreeRTOSConfig.h, saving 8 bytes of memory for each task.

Although RTOS task notifications are faster and use less memory, it also has some limitations:

Only one task can receive notification events.

The task that receives the notification can enter the blocking state because it is waiting for the notification, but the task that sends the notification cannot enter the blocking state even if it cannot finish sending the notification immediately.

1. Send notification-method 11.1 function describes BaseType_t xTaskNotify (TaskHandle_txTaskToNotify, uint32_t ulValue, eNotifyAction eAction)

Sends the specified notification value to the specified task. If you plan to use RTOS task notification to implement lightweight binary or count semaphores, it is recommended that you use the API function xTaskNotifyGive () instead of this function.

This function cannot be called in an interrupt service routine, and the interrupt protection equivalent function is xTaskNotifyFromISR ().

1.2 Parameter description

XTaskToNotify: the task handle that was notified.

UlValue: notification update value eAction: enumerates the type indicating how to update the notification value

Enumerate variable members and roles as shown in the following table.

1.3 return value

If the parameter eAction is eSetValueWithoutOverwrite, if the notified task has not taken the last notification and received another notification, the notification value cannot be updated and pdFALSE is returned, otherwise pdPASS is returned.

two。 Send Notification-method 22.1 function description

BaseType_t xTaskNotifyGive (TaskHandle_t xTaskToNotify)

This is actually a macro, which is essentially equivalent to xTaskNotify ((xTaskToNotify), (0), eIncrement). You can use this API function instead of binary or counting semaphores, but faster. In this case, you should use the API function ulTaskNotifyTake () to wait for notifications, rather than the API function xTaskNotifyWait ().

This function cannot be called in an interrupt service routine, and the interrupt protection equivalent function is vTaskNotifyGiveFromISR ().

2.2 Parameter description

XTaskToNotify: the task handle that was notified.

2.3 usage examples staticvoid prvTask1 (void * pvParameters); staticvoid prvTask2 (void * pvParameters); / * Save task handle * / staticTaskHandle_t xTask1 = NULL, xTask2 = NULL;/* create two tasks, send notifications between them repeatedly, start the RTOS scheduler * / voidmain (void) {xTaskCreate (prvTask1, "Task1", 200, NULL, tskIDLE_PRIORITY, & xTask1); xTaskCreate (prvTask2, "Task2", 200, NULL, tskIDLE_PRIORITY, & xTask2) VTaskStartScheduler ();} / *-- * / staticvoid prvTask1 (void * pvParameters) {for (;;) {/ * send a notification to prvTask2 () to unblock state * / xTaskNotifyGive (xTask2) / * wait for the notification of prvTask2 () and enter the blocking * / ulTaskNotifyTake (pdTRUE, portMAX_DELAY);}} / *-- * / staticvoid prvTask2 (void * pvParameters) {for ( ) {/ * wait for the notification of prvTask1 () and enter the blocking * / ulTaskNotifyTake (pdTRUE, portMAX_DELAY); / * send a notification to prvTask1 () to unblock the state * / xTaskNotifyGive (xTask1);}} 3. Get Notification 3.1 function description uint32_t ulTaskNotifyTake (BaseType_txClearCountOnExit, TickType_txTicksToWait)

UlTaskNotifyTake () is tailored to use lighter and faster methods instead of binary or counting semaphores. The API function for FreeRTOS to get the semaphore is xSemaphoreTake (), which can be replaced by using the ulTaskNotifyTake () function equivalently.

When a task uses notification values to implement binary or count semaphores, other tasks either use the API function xTaskNotifyGive () or use the API function xTaskNotify () with the argument eAction to eIncrement. The xTaskNotifyGive () function is recommended (it's actually a macro, which we think of as an API function here). It is also important to note that if used in interrupts, use their interrupt protection equivalent functions: vTaskNotifyGiveFromISR () and xTaskNotifyFromISR ().

The API function xTaskNotifyTake () has two methods to deal with the notification value of a task, one is to zero the notification value when the function exits, which is suitable for implementing binary semaphores, and the other is to subtract the notification value by 1 when the function exits, which is suitable for counting semaphores.

If the notification value of the RTOS task is 0, use xTaskNotifyTake () to optionally put the task into a blocking state until the notification value of the task is not 0. Tasks that enter blocking do not consume CPU time.

3.2 Parameter description

XClearCountOnExit: if this parameter is set to pdFALSE, the notification value of the task is reduced by 1 before the API function xTaskNotifyTake () exits; if this parameter is set to pdTRUE, the task notification value is cleared before the API function xTaskNotifyTake () exits.

XTicksToWait: the maximum time to enter the blocking state while waiting for a notification. The unit of time is the system beat cycle. The macro PDMS _ TO_TICKS is used to convert the specified millisecond time to the corresponding number of system beats.

3.3 return value

Returns the current notification value of the task, which is 0 or minus 1 for the notification value before calling the API function xTaskNotifyTake ().

3.4 examples of usage

/ * interrupt handler. * / voidvANInterruptHandler (void) {BaseType_txHigherPriorityTaskWoken; prvClearInterruptSource (); / * xHigherPriorityTaskWoken must be initialized to pdFALSE. If a call to vTaskNotifyGiveFromISR () unblocks the vHandlingTask task, and the vHandlingTask task takes precedence over the currently running task, xHigherPriorityTaskWoken is automatically set to pdTRUE. * / xHigherPriorityTaskWoken = pdFALSE; / * sends a notification to a task to which xHandlingTask is the handle. * / vTaskNotifyGiveFromISR (xHandlingTask,&xHigherPriorityTaskWoken); / * if xHigherPriorityTaskWoken is pdTRUE, force context switching. The implementation of this macro depends on the migration layer and may call portEND_SWITCHING_ISR * / portYIELD_FROM_ISR (xHigherPriorityTaskWoken) } / *-* / * A task that is blocked by waiting for a notification. * / voidvHandlingTask (void * pvParameters) {BaseType_txEvent; for (;;) {/ * wait for notification and block indefinitely. The parameter pdTRUE indicates that the zero notification value is cleared before the function exits. This is obviously an alternative to binary semaphores. It is important to note that real programs generally do not block indefinitely. * / ulTaskNotifyTake (pdTRUE, portMAX_DELAY); / * after all events have been processed, still wait for the next notification * / do {xEvent = xQueryPeripheral (); if (xEvent! = NO_MORE_EVENTS) {vProcessPeripheralEvent (xEvent);}} while (xEvent! = NO_MORE_EVENTS);}} 4. Wait for Notification 4.1 function description BaseType_t xTaskNotifyWait (uint32_tulBitsToClearOnEntry, uint32_tulBitsToClearOnExit, uint32_t*pulNotificationValue, TickType_txTicksToWait)

If you plan to use RTOS task notification to implement lightweight binary or count semaphores, it is recommended that you use the API function ulTaskNotifyTake () instead of this function.

4.2 Parameter description

UlBitsToClearOnEntry: before using notification, the notification value of the task and the bitwise inverse value of the parameter ulBitsToClearOnEntry are bitwise and operated. Set the parameter ulBitsToClearOnEntry to 0xFFFFFFFF (ULONG_MAX), which indicates the zero task notification value.

UlBitsToClearOnExit: before the function xTaskNotifyWait () exits, the notification value of the task and the bitwise inverse value of the parameter ulBitsToClearOnExit are bitwise and operated.

Set the parameter ulBitsToClearOnExit to 0xFFFFFFFF (ULONG_MAX), which indicates the zero task notification value.

PulNotificationValue: the notification value used to send back the task. This notification value copies the notification value to * pulNotificationValue before the parameter ulBitsToClearOnExit takes effect. If you do not need to return the notification value for the task, set it to NULL.

XTicksToWait: the maximum time to enter the blocking state while waiting for a notification. The unit of time is the system beat cycle. The macro PDMS _ TO_TICKS is used to convert the specified millisecond time to the corresponding number of system beats.

4.3 return value

Returns pdTRUE if the notification is received, and pdFALSE if the API function xTaskNotifyWait () waits for a timeout.

4.4 usage example / * this task uses the bits of the task notification value to pass different events, which can be used instead of event groups in some cases. * / voidvAnEventProcessingTask (void * pvParameters) {uint32_tulNotifiedValue; for (;;) {/ * wait for notification and block indefinitely (there is no timeout, so there is no need to check the function return value). Different bits in the notification values set by other tasks or interrupts represent different events. Parameter 0x00 indicates that the notification value bit of the task is not cleared before the notification is used. Parameter ULONG_MAX indicates that the task notification value is set to 0 notify / xTaskNotifyWait (0x00, ULONG_MAX,&ulNotifiedValue, portMAX_DELAY) before the function xTaskNotifyWait () exits; / * the event is handled according to the notification value * / if ((ulNotifiedValue & 0x01)! = 0) {prvProcessBit0Event () } if ((ulNotifiedValue & 0x02)! = 0) {prvProcessBit1Event ();} if ((ulNotifiedValue & 0x04)! = 0) {prvProcessBit2Event ();} / *. * /} 5 Task notification and query 5.1function description BaseType_t xTaskNotifyAndQuery (TaskHandle_t xTaskToNotify, uint32_tulValue, eNotifyActioneAction, uint32_t*pulPreviousNotifyValue)

This function is very similar to the task notification API function xTaskNotify (), except that it has an additional parameter that returns the current notification value of the task, and then updates the notification value of the task based on the parameters ulValue and eAction.

This function cannot be used in interrupt service routines, where the xTaskNotifyAndQueryFromISR () function is used.

5.2 Parameter description

XTaskToNotify: the task handle that was notified.

UlValue: notification update valu

EAction: enumerates the type, indicates how to update the notification value, enumerates variable members and functions, see the xTaskNotify () section.

PulPreviousNotifyValue: returns task notification values that have not been updated. If you do not need to return unupdated task notification values, set this to NULL, which is equivalent to calling the xTaskNotify () function.

5.3 return value

If the parameter eAction is eSetValueWithoutOverwrite, if the notified task has not taken the last notification and received another notification, the notification value cannot be updated and pdFALSE is returned, otherwise pdPASS is returned.

This is the end of the content about "how to realize the task notification of the FreeRTOS real-time operating system". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report