Direct Task Notification是FreeRTOS 10版本以后的最重要的一个功能。他可以实现大部分二进制信号量,计数信号量,事件组,邮箱等等的功能。而且速度快45%,并且占用更少的内存,所以我们应该尽量使用任务通知这个功能。本集会深层次的讲解什么是notification,以及对他的读,写 和等待。一个任务将会有多个通知,一个通知包含值(value)以及状态(status)这两个内容,值占4个字节,状态占一个字节。
/* Clear the interrupt. */ prvClearInterruptSource();
/* xHigherPriorityTaskWoken must be initialised to pdFALSE. If calling vTaskNotifyGiveFromISR() unblocks the handling task, and the priority of the handling task is higher than the priority of the currently running task, then xHigherPriorityTaskWoken will automatically get set to pdTRUE. */ xHigherPriorityTaskWoken = pdFALSE;
/* Unblock the handling task so the task can perform any processing necessitated by the interrupt. xHandlingTask is the task's handle, which was obtained when the task was created. */ vTaskNotifyGiveIndexedFromISR( xHandlingTask, 0, &xHigherPriorityTaskWoken );
/* Force a context switch if xHigherPriorityTaskWoken is now set to pdTRUE. The macro used to do this is dependent on the port and may be called portEND_SWITCHING_ISR. */ portYIELD_FROM_ISR( xHigherPriorityTaskWoken ); } /*-----------------------------------------------------------*/
/* A task that blocks waiting to be notified that the peripheral needs servicing, processing all the events pending in the peripheral each time it is notified to do so. */ voidvHandlingTask( void *pvParameters ) { BaseType_t xEvent;
for( ;; ) { /* Block indefinitely (without a timeout, so no need to check the function's return value) to wait for a notification. Here the RTOS task notification is being used as a binary semaphore, so the notification value is cleared to zero on exit. NOTE! Real applications should not block indefinitely, but instead time out occasionally in order to handle error conditions that may prevent the interrupt from sending any more notifications. */ ulTaskNotifyTakeIndexed( 0, /* Use the 0th notification */ pdTRUE, /* Clear the notification value before exiting. */ portMAX_DELAY ); /* Block indefinitely. */
/* The RTOS task notification is used as a binary (as opposed to a counting) semaphore, so only go back to wait for further notifications when all events pending in the peripheral have been processed. */ do { xEvent = xQueryPeripheral();