22.3 任务通知的数据结构

下面第51-53行

  1. typedef struct tskTaskControlBlock
  2. {
  3. volatile StackType_t *pxTopOfStack; /*< Points to the location of the last item placed on the tasks stack. THIS MUST BE THE FIRST MEMBER OF THE TCB STRUCT. */
  4. #if ( portUSING_MPU_WRAPPERS == 1 )
  5. xMPU_SETTINGS xMPUSettings; /*< The MPU settings are defined as part of the port layer. THIS MUST BE THE SECOND MEMBER OF THE TCB STRUCT. */
  6. #endif
  7. ListItem_t xStateListItem; /*< The list that the state list item of a task is reference from denotes the state of that task (Ready, Blocked, Suspended ). */
  8. ListItem_t xEventListItem; /*< Used to reference a task from an event list. */
  9. UBaseType_t uxPriority; /*< The priority of the task. 0 is the lowest priority. */
  10. StackType_t *pxStack; /*< Points to the start of the stack. */
  11. char pcTaskName[ configMAX_TASK_NAME_LEN ];/*< Descriptive name given to the task when created. Facilitates debugging only. */ /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
  12. #if ( portSTACK_GROWTH > 0 )
  13. StackType_t *pxEndOfStack; /*< Points to the end of the stack on architectures where the stack grows up from low memory. */
  14. #endif
  15. #if ( portCRITICAL_NESTING_IN_TCB == 1 )
  16. UBaseType_t uxCriticalNesting; /*< Holds the critical section nesting depth for ports that do not maintain their own count in the port layer. */
  17. #endif
  18. #if ( configUSE_TRACE_FACILITY == 1 )
  19. UBaseType_t uxTCBNumber; /*< Stores a number that increments each time a TCB is created. It allows debuggers to determine when a task has been deleted and then recreated. */
  20. UBaseType_t uxTaskNumber; /*< Stores a number specifically for use by third party trace code. */
  21. #endif
  22. #if ( configUSE_MUTEXES == 1 )
  23. UBaseType_t uxBasePriority; /*< The priority last assigned to the task - used by the priority inheritance mechanism. */
  24. UBaseType_t uxMutexesHeld;
  25. #endif
  26. #if ( configUSE_APPLICATION_TASK_TAG == 1 )
  27. TaskHookFunction_t pxTaskTag;
  28. #endif
  29. #if( configNUM_THREAD_LOCAL_STORAGE_POINTERS > 0 )
  30. void *pvThreadLocalStoragePointers[ configNUM_THREAD_LOCAL_STORAGE_POINTERS ];
  31. #endif
  32. #if( configGENERATE_RUN_TIME_STATS == 1 )
  33. uint32_t ulRunTimeCounter; /*< Stores the amount of time the task has spent in the Running state. */
  34. #endif
  35. #if ( configUSE_NEWLIB_REENTRANT == 1 )
  36. struct _reent xNewLib_reent;
  37. #endif
  38. #if( configUSE_TASK_NOTIFICATIONS == 1 )
  39. // 任务通知的值, 可以保存一个 32 位整数或指针值。
  40. volatile uint32_t ulNotifiedValue;
  41. // 任务通知状态, 用于标识任务是否在等待通知。
  42. volatile uint8_t ucNotifyState;
  43. #endif
  44. #if( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
  45. uint8_t ucStaticallyAllocated; /*< Set to pdTRUE if the task is a statically allocated to ensure no attempt is made to free the memory. */
  46. #endif
  47. #if( INCLUDE_xTaskAbortDelay == 1 )
  48. uint8_t ucDelayAborted;
  49. #endif
  50. } tskTCB;

22.4 任务通知的函数接口讲解

功能 普通 中断
+1 操作 xTaskGenericNotify vTaskNotifyGiveFromISR
其它操作 xTaskNotify xTaskNotifyFromISR
带返回值的(之前的通知) xTaskNotifyAndQuery xTaskNotifyAndQueryFromISR

22.4.1 发送任务通知函数xTaskGenericNotify()

发送通知 API 函数。这类函数比较多,有 6 个。但仔细分析会发现它们只能完成 3 种操作,每种操作有两个 API 函数,分别为带中断保护版本和不带中断保护版本。
所有的函数都是一个宏定义, 在任务中发送任务通知的函数均是调用 xTaskGenericNotify()函数进行发送通知。

参数:
TaskHandle_t xTaskToNotify:被通知的任务句柄,指定通知的任务。
uint32_t ulValue:发送的通知值。
eNotifyAction eAction:枚举类型,指明更新通知值的方式。
uint32_t *pulPreviousNotificationValue:任务原本的通知值返回。

  1. #if( configUSE_TASK_NOTIFICATIONS == 1 )
  2. BaseType_t xTaskGenericNotify( TaskHandle_t xTaskToNotify,
  3. uint32_t ulValue,
  4. eNotifyAction eAction,
  5. uint32_t *pulPreviousNotificationValue )
  6. {
  7. TCB_t * pxTCB;
  8. BaseType_t xReturn = pdPASS;
  9. uint8_t ucOriginalNotifyState;
  10. configASSERT( xTaskToNotify );
  11. // 获取要通知的任务
  12. pxTCB = ( TCB_t * ) xTaskToNotify;
  13. taskENTER_CRITICAL();
  14. {
  15. if( pulPreviousNotificationValue != NULL )
  16. {
  17. // 回传任务原本的任务通值,保存在 pulPreviousNotificationValue 中。
  18. *pulPreviousNotificationValue = pxTCB->ulNotifiedValue;
  19. }
  20. /* 获取任务通知的状态,看看任务是否在等待通知,方便在发送通知后恢复任务 */
  21. ucOriginalNotifyState = pxTCB->ucNotifyState;
  22. /* 标记任务为taskNOTIFICATION_RECEIVED有通知 标记位 */
  23. pxTCB->ucNotifyState = taskNOTIFICATION_RECEIVED;
  24. /* 指定更新任务通知的方式 */
  25. switch( eAction )
  26. {
  27. /*通知值按位或上 ulValue。
  28. 使用这种方法可以某些场景下代替事件组,但执行速度更快。 */
  29. case eSetBits :
  30. pxTCB->ulNotifiedValue |= ulValue;
  31. break;
  32. /* 被通知任务的通知值增加 1,这种发送通知方式,参数 ulValue 未使用 */
  33. case eIncrement :
  34. ( pxTCB->ulNotifiedValue )++;
  35. break;
  36. /* 将被通知任务的通知值设置为 ulValue。无论任务是否还有通知,
  37. 都覆盖当前任务通知值。使用这种方法,
  38. 可以在某些场景下代替 xQueueoverwrite()函数,但执行速度更快。 */
  39. case eSetValueWithOverwrite :
  40. pxTCB->ulNotifiedValue = ulValue;
  41. break;
  42. /* 如果被通知任务当前没有通知,则被通知任务的通知值设置为 ulValue;
  43. 在某些场景下替代长度为 1 的 xQueuesend(),但速度更快。 */
  44. case eSetValueWithoutOverwrite :
  45. if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
  46. {
  47. pxTCB->ulNotifiedValue = ulValue;
  48. }
  49. else
  50. {
  51. /*如果被通知任务还没取走上一个通知,本次发送通知
  52. 任务又接收到了一个通知,则这次通知值丢弃,
  53. 在这种情况下,函数调用失败并返回 pdFALSE。*/
  54. xReturn = pdFAIL;
  55. }
  56. break;
  57. /* 发送通知但不更新通知值,这意味着参数 ulValue 未使用。 */
  58. case eNoAction:
  59. break;
  60. }
  61. traceTASK_NOTIFY();
  62. /* 如果被通知任务由于等待任务通知而挂起 */
  63. if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
  64. {
  65. /* 唤醒任务,将任务从阻塞列表中移除,添加到就绪列表中 */
  66. ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
  67. prvAddTaskToReadyList( pxTCB );
  68. configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
  69. #if( configUSE_TICKLESS_IDLE != 0 )
  70. {
  71. prvResetNextTaskUnblockTime();
  72. }
  73. #endif
  74. // 刚刚唤醒的任务优先级比当前任务高
  75. if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
  76. {
  77. //任务切换
  78. taskYIELD_IF_USING_PREEMPTION();
  79. }
  80. else
  81. {
  82. mtCOVERAGE_TEST_MARKER();
  83. }
  84. }
  85. else
  86. {
  87. mtCOVERAGE_TEST_MARKER();
  88. }
  89. }
  90. taskEXIT_CRITICAL();
  91. return xReturn;
  92. }
  93. #endif /* configUSE_TASK_NOTIFICATIONS */

1.xTaskNotifyGive()

代替:二值信号量
xTaskNotifyGive()是一个宏,宏展开是调用函数 xTaskNotify( ( xTaskToNotify ), ( 0 ),eIncrement ), 即向一个任务发送通知,并将对方的任务通知值加 1。该函数可以作为二值信号量和计数信号量的一种轻量型的实现,速度更快, 在这种情况下对象任务在等待任务通 知 的 时 候 应 该 是 使 用 函 数 ulTaskNotifyTake() 而 不 是 xTaskNotifyWait() 。 xTaskNotifyGive() 不能在中断里面使用,而是使用具有中断保护功能的
vTaskNotifyGiveFromISR()来代替。
image.png
应用:

  1. /* 函数声明 */
  2. static void prvTask1( void *pvParameters );
  3. static void prvTask2( void *pvParameters );
  4. /*定义任务句柄 */
  5. static TaskHandle_t xTask1 = NULL, xTask2 = NULL;
  6. /* 主函数:创建两个任务,然后开始任务调度 */
  7. void main( void )
  8. {
  9. xTaskCreate(prvTask1, "Task1", 200, NULL, tskIDLE_PRIORITY, &xTask1);
  10. xTaskCreate(prvTask2, "Task2", 200, NULL, tskIDLE_PRIORITY, &xTask2);
  11. vTaskStartScheduler();
  12. }
  13. static void prvTask1( void *pvParameters )
  14. {
  15. for ( ;; ) {
  16. /* 向 prvTask2()发送一个任务通知,让其退出阻塞状态 */
  17. xTaskNotifyGive( xTask2 );
  18. /* 阻塞在 prvTask2()的任务通知上
  19. 如果没有收到通知,则一直等待*/
  20. ulTaskNotifyTake( pdTRUE, portMAX_DELAY );
  21. }
  22. }
  23. static void prvTask2( void *pvParameters )
  24. {
  25. for ( ;; ) {
  26. /* 阻塞在 prvTask1()的任务通知上
  27. 如果没有收到通知,则一直等待*/
  28. ulTaskNotifyTake( pdTRUE, portMAX_DELAY );
  29. xTaskNotifyGive( xTask1 );
  30. }
  31. }

2.vTaskNotifyGiveFromISR()

vTaskNotifyGiveFromISR()是 vTaskNotifyGive()的中断保护版本。 用于在中断中向指定任务发送任务通知,并更新对方的任务通知值(加 1 操作) ,在某些场景中可以替代信号量操作,因为这两个通知都是不带有通知值的。
image.png
从上面的函数说明我们大概知道 vTaskNotifyGiveFromISR()函数作用, 每次调用该函数都会增加任务的通知值, 任务通过接收函数返回值是否大于零,判断是否获取到了通知,任务通知值初始化为 0, (如果与信号量做对比)则对应为信号量无效。当中断调用vTaskNotifyGiveFromISR()通知函数给任务的时候,任务的通知值增加,使其大于零,使其表示的通知值变为有效,任务获取有效的通知值将会被恢复。

  1. #if( configUSE_TASK_NOTIFICATIONS == 1 )
  2. void vTaskNotifyGiveFromISR( TaskHandle_t xTaskToNotify,
  3. BaseType_t *pxHigherPriorityTaskWoken )
  4. {
  5. TCB_t * pxTCB;
  6. uint8_t ucOriginalNotifyState;
  7. UBaseType_t uxSavedInterruptStatus;
  8. // 获取通知的任务块
  9. pxTCB = ( TCB_t * ) xTaskToNotify;
  10. //进入中断
  11. uxSavedInterruptSta tus = portSET_INTERRUPT_MASK_FROM_ISR();
  12. {
  13. //保存任务通知的原始状态,
  14. //看看任务是否在等待通知,方便在发送通知后恢复任务
  15. ucOriginalNotifyState = pxTCB->ucNotifyState;
  16. pxTCB->ucNotifyState = taskNOTIFICATION_RECEIVED;
  17. /* 通知值自加,类似于信号量的释放 */
  18. ( pxTCB->ulNotifiedValue )++;
  19. traceTASK_NOTIFY_GIVE_FROM_ISR();
  20. /* 如果任务在阻塞等待通知 */
  21. if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
  22. {
  23. //如果任务调度器运行中
  24. configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
  25. if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
  26. {
  27. /* 唤醒任务,将任务从阻塞列表中移除,添加到就绪列表中 */
  28. ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
  29. prvAddTaskToReadyList( pxTCB );
  30. }
  31. else
  32. {
  33. /* 调度器处于挂起状态,中断依然正常发生,但是不能直接操作就绪列表
  34. 将任务加入到就绪挂起列表,任务调度恢复后会移动到就绪列表 */
  35. vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
  36. }
  37. // 判断优先级 是否需要切换上下文
  38. if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
  39. {
  40. if( pxHigherPriorityTaskWoken != NULL )
  41. {
  42. *pxHigherPriorityTaskWoken = pdTRUE;
  43. }
  44. else
  45. {
  46. // 切换标志
  47. xYieldPending = pdTRUE;
  48. }
  49. }
  50. else
  51. {
  52. mtCOVERAGE_TEST_MARKER();
  53. }
  54. }
  55. }
  56. portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
  57. }
  58. #endif /* configUSE_TASK_NOTIFICATIONS */
  59. /*-----------------------------------------------------------*/

使用示例:
DMA使用

  1. static TaskHandle_t xTaskToNotify = NULL;
  2. /* 外设驱动的数据传输函数 */
  3. void StartTransmission( uint8_t *pcData, size_t xDataLength )
  4. {
  5. /* 在这个时候, xTaskToNotify 应为 NULL,因为发送并没有进行。
  6. 如果有必要,对外设的访问可以用互斥量来保护*/
  7. configASSERT( xTaskToNotify == NULL );
  8. /* 获取调用函数 StartTransmission()的任务的句柄 */
  9. xTaskToNotify = xTaskGetCurrentTaskHandle();
  10. /* 开始传输,当数据传输完成时产生一个中断 */
  11. // 一般这个是DMA发送的 DMA会产生一个中断
  12. vStartTransmit( pcData, xDatalength );
  13. }
  14. /* 数据传输完成中断 */
  15. void vTransmitEndISR( void )
  16. {
  17. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  18. /* 这个时候不应该为 NULL,因为数据传输已经开始 */
  19. configASSERT( xTaskToNotify != NULL );
  20. /* 通知任务传输已经完成 */
  21. vTaskNotifyGiveFromISR( xTaskToNotify, &xHigherPriorityTaskWoken );
  22. /* 传输已经完成,所以没有任务需要通知 */
  23. xTaskToNotify = NULL;
  24. /* 如果为 pdTRUE,则进行一次上下文切换 */
  25. portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
  26. }
  27. /* 任务:启动数据传输,然后进入阻塞态,直到数据传输完成 */
  28. void vAFunctionCalledFromATask( uint8_t ucDataToTransmit,
  29. size_t xDataLength )
  30. {
  31. uint32_t ulNotificationValue;
  32. const TickType_t xMaxBlockTime = pdMS_TO_TICKS( 200 );
  33. /* 调用上面的函数 StartTransmission()启动传输 */
  34. StartTransmission( ucDataToTransmit, xDataLength );
  35. /* 等待任务传输完成发出通知,进入阻塞态。注意这里第一个参数被
  36. 设置为 pdTRUE ,意味着通知在被获取之后会被清0,使得与二值信号量
  37. 有相同的特性。*/
  38. lNotificationValue = ulTaskNotifyTake( pdFALSE, xMaxBlockTime );
  39. /* 当传输完成时,会产生一个中断
  40. 在中断服务函数中调用 vTaskNotifyGiveFromISR()向启动数据
  41. 传输的任务发送一个任务通知,并将对象任务的任务通知值加 1
  42. 任务通知值在任务创建的时候是初始化为 0 的,当接收到任务后就变成 1 */
  43. if ( ulNotificationValue == 1 ) {
  44. /* 传输按预期完成 */
  45. } else {
  46. /* 调用函数 ulTaskNotifyTake()超时 */
  47. }
  48. }

3. xTaskNotify()

推荐:代替事件组
FreeRTOS 每个任务都有一个 32 位的变量用于实现任务通知,在任务创建的时候初始化为 0。 这个 32 位的通知值在任务控制块 TCB 里面定义,具体见代码清单 22-6。xTaskNotify()用于在任务中直接向另外一个任务发送一个事件, 接收到该任务通知的任务有可能解锁。 如果你想使用任务通知来实现二值信号量和计数信号量,那么应该使用更加简单的函数 xTaskNotifyGive() , 而不是使用 xTaskNotify(), xTaskNotify()函数在发送任务通知的时候会指定一个通知值, 并且用户可以指定通知值发送的方式。
注意:该函数不能在中断里面使用, 而是使用具体中断保护功能的版本函数xTaskNotifyFromISR()。
任务通知在任务控制块中的定义:

  1. #if( configUSE_TASK_NOTIFICATIONS == 1 )
  2. volatile uint32_t ulNotifiedValue;
  3. volatile uint8_t ucNotifyState;
  4. #endif

image.png
image.png
示例:

  1. /* 设置任务 xTask1Handle 的任务通知值的位 8 为 1*/
  2. xTaskNotify( xTask1Handle, ( 1UL << 8UL ), eSetBits );
  3. /* 向任务 xTask2Handle 发送一个任务通知
  4. 有可能会解除该任务的阻塞状态,但是并不会更新该任务自身的任务通知值 */
  5. xTaskNotify( xTask2Handle, 0, eNoAction );
  6. /* 向任务 xTask3Handle 发送一个任务通知
  7. 并把该任务自身的任务通知值更新为 0x50
  8. 即使该任务的上一次的任务通知都没有读取的情况下
  9. 即覆盖写 */
  10. xTaskNotify( xTask3Handle, 0x50, eSetValueWithOverwrite );
  11. /* 向任务 xTask4Handle 发送一个任务通知
  12. 并把该任务自身的任务通知值更新为 0xfff
  13. 但是并不会覆盖该任务之前接收到的任务通知值*/
  14. if(xTaskNotify(xTask4Handle,0xfff,eSetValueWithoutOverwrite)==pdPASS )
  15. {
  16. /* 任务 xTask4Handle 的任务通知值已经更新 */
  17. } else {
  18. /* 任务 xTask4Handle 的任务通知值没有更新
  19. 即上一次的通知值还没有被取走*/
  20. }

4.xTaskNotifyFrmoISR()

xTaskNotifyFromISR()是 xTaskNotify()的中断保护版本,真正起作用的函数是中断发送任务通知通用函数 xTaskGenericNotifyFromISR(),而 xTaskNotifyFromISR()是一个宏定义,.

  1. #define xTaskNotifyFromISR( xTaskToNotify, \
  2. ulValue, \
  3. eAction, \
  4. pxHigherPriorityTaskWoken ) \
  5. xTaskGenericNotifyFromISR( ( xTaskToNotify ), \
  6. ( ulValue ), \
  7. ( eAction ), \
  8. NULL, \
  9. ( pxHigherPriorityTaskWoken ) )


image.png

5.中断中发送任务通知通用函数 xTaskGenericNotifyFromISR()

xTaskGenericNotifyFromISR() 是 一 个 在 中 断 中 发 送 任 务 通 知 的 通 用 函 数 ,xTaskNotifyFromISR()、 xTaskNotifyAndQueryFromISR()等函数都是以其为基础,采用宏定义的方式实现。

参数:
TaskHandle_t xTaskToNotify:指定接收通知的任务句柄
uint32_t ulValue:用于更新接收任务通知值, 具体如何更新由形参 eAction 决定。
eNotifyAction eAction:任务通知值更新方式。
uint32_t pulPreviousNotificationValue:用于保存任务上一个通知值。
BaseType_t
pxHigherPriorityTaskWoken:中断切换上下文

  1. #if( configUSE_TASK_NOTIFICATIONS == 1 )
  2. BaseType_t xTaskGenericNotifyFromISR( TaskHandle_t xTaskToNotify,
  3. uint32_t ulValue,
  4. eNotifyAction eAction,
  5. uint32_t *pulPreviousNotificationValue,
  6. BaseType_t *pxHigherPriorityTaskWoken )
  7. {
  8. TCB_t * pxTCB;
  9. uint8_t ucOriginalNotifyState;
  10. BaseType_t xReturn = pdPASS;
  11. UBaseType_t uxSavedInterruptStatus;
  12. configASSERT( xTaskToNotify );
  13. portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
  14. // 获取要通知任务的PCB
  15. pxTCB = ( TCB_t * ) xTaskToNotify;
  16. // 临界区
  17. uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
  18. {
  19. if( pulPreviousNotificationValue != NULL )
  20. {
  21. // 保存之前的通知值
  22. *pulPreviousNotificationValue = pxTCB->ulNotifiedValue;
  23. }
  24. // 获取当前通知的状态
  25. ucOriginalNotifyState = pxTCB->ucNotifyState;
  26. // 设置标记位
  27. pxTCB->ucNotifyState = taskNOTIFICATION_RECEIVED;
  28. // 根据情况进行处理 到这里基本和普通任务是一样的
  29. switch( eAction )
  30. {
  31. case eSetBits :
  32. pxTCB->ulNotifiedValue |= ulValue;
  33. break;
  34. case eIncrement :
  35. ( pxTCB->ulNotifiedValue )++;
  36. break;
  37. case eSetValueWithOverwrite :
  38. pxTCB->ulNotifiedValue = ulValue;
  39. break;
  40. case eSetValueWithoutOverwrite :
  41. if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
  42. {
  43. pxTCB->ulNotifiedValue = ulValue;
  44. }
  45. else
  46. {
  47. xReturn = pdFAIL;
  48. }
  49. break;
  50. case eNoAction :
  51. break;
  52. }
  53. // 退出
  54. traceTASK_NOTIFY_FROM_ISR();
  55. /* 如果任务在阻塞等待通知 */
  56. if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
  57. {
  58. //如果任务调度器运行中,表示可用操作就绪级列表
  59. configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
  60. /* 唤醒任务,将任务从阻塞列表中移除,添加到就绪列表中 */
  61. if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
  62. {
  63. ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
  64. prvAddTaskToReadyList( pxTCB );
  65. }
  66. else
  67. {
  68. // 否则放入挂起列表
  69. vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
  70. }
  71. // 刚刚唤醒的任务优先级比当前任务高
  72. if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
  73. {
  74. if( pxHigherPriorityTaskWoken != NULL )
  75. {
  76. // 设置为TRUE 中断处理完要进行切换
  77. *pxHigherPriorityTaskWoken = pdTRUE;
  78. }
  79. else
  80. {
  81. xYieldPending = pdTRUE;
  82. }
  83. }
  84. else
  85. {
  86. mtCOVERAGE_TEST_MARKER();
  87. }
  88. }
  89. }
  90. portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
  91. return xReturn;
  92. }
  93. #endif /* configUSE_TASK_NOTIFICATIONS */
  94. /*-----------------------------------------------------------*/


使用示例:

  1. /* 中断:向一个任务发送任务通知,并根据不同的中断将目标任务的
  2. 任务通知值的相应位置 1 */
  3. void vANInterruptHandler( void )
  4. {
  5. BaseType_t xHigherPriorityTaskWoken;
  6. uint32_t ulStatusRegister;
  7. /* 读取中断状态寄存器,判断到来的是哪个中断
  8. 这里假设了 Rx、 Tx 和 buffer overrun 三个中断 */
  9. ulStatusRegister = ulReadPeripheralInterruptStatus();
  10. /* 清除中断标志位 */
  11. vClearPeripheralInterruptStatus( ulStatusRegister );
  12. /* xHigherPriorityTaskWoken 在使用之前必须初始化为 pdFALSE */
  13. xHigherPriorityTaskWoken = pdFALSE;
  14. /* 向任务 xHandlingTask 发送任务通知,并将其任务通知值
  15. 与 ulStatusRegister 的值相或,这样可以不改变任务通知其它位的值*/
  16. xTaskNotifyFromISR( xHandlingTask,
  17. ulStatusRegister,
  18. eSetBits,
  19. &xHigherPriorityTaskWoken );
  20. /* 如果 xHigherPriorityTaskWoken 的值为 pdRTUE
  21. 则执行一次上下文切换*/
  22. portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
  23. }
  24. /* 任务:等待任务通知,然后处理相关的事情 */
  25. void vHandlingTask( void *pvParameters )
  26. {
  27. uint32_t ulInterruptStatus;
  28. for ( ;; ) {
  29. /* 等待任务通知,无限期阻塞(没有超时,所以没有必要检查函数返回值) */
  30. xTaskNotifyWait( 0x00, /* 在进入的时候不清除通知值的任何位 */
  31. ULONG_MAX, /* 在退出的时候复位通知值为 0 */
  32. &ulNotifiedValue, /* 任务通知值传递到变量ulNotifiedValue 中*/
  33. portMAX_DELAY ); /* 无限期等待 */
  34. /* 根据任务通知值里面的各个位的值处理事情 */
  35. if ( ( ulInterruptStatus & 0x01 ) != 0x00 ) {
  36. /* Rx 中断 */
  37. prvProcessRxInterrupt();
  38. }
  39. if ( ( ulInterruptStatus & 0x02 ) != 0x00 ) {
  40. /* Tx 中断 */
  41. prvProcessTxInterrupt();
  42. }
  43. if ( ( ulInterruptStatus & 0x04 ) != 0x00 ) {
  44. /* 缓冲区溢出中断 */
  45. prvClearBufferOverrun();
  46. }
  47. }
  48. }

6.xTaskNotifyAndQuery()

xTaskNotifyAndQuery()与 xTaskNotify()很像,都是调用通用的任务通知发送函数xTaskGenericNotify() 来 实 现通知的发送,不同的是多了一个附加的参数pulPreviousNotifyValue 用于回传接收任务的上一个通知值。

  1. #define xTaskNotifyAndQuery( xTaskToNotify, \
  2. ulValue, \
  3. eAction, \
  4. pulPreviousNotifyValue ) \
  5. xTaskGenericNotify( ( xTaskToNotify ), \
  6. ( ulValue ), \
  7. ( eAction ), \
  8. ( pulPreviousNotifyValue ) )

image.png

  1. uint32_t ulPreviousValue;
  2. /* 设置对象任务 xTask1Handle 的任务通知值的位 8 为 1
  3. 在更新位 8 的值之前把任务通知值回传存储在变量 ulPreviousValue 中*/
  4. xTaskNotifyAndQuery( xTask1Handle, ( 1UL << 8UL ), eSetBits,&ulPreviousValue );
  5. /* 向对象任务 xTask2Handle 发送一个任务通知,有可能解除对象任务的阻塞状态
  6. 但是不更新对象任务的通知值,并将对象任务的通知值存储在变量 ulPreviousValue 中 */
  7. xTaskNotifyAndQuery( xTask2Handle, 0, eNoAction, &ulPreviousValue );
  8. /* 覆盖式设置对象任务的任务通知值为 0x50
  9. 且对象任务的任务通知值不用回传,则最后一个形参设置为 NULL */
  10. xTaskNotifyAndQuery( xTask3Handle, 0x50, eSetValueWithOverwrite, NULL );
  11. /* 设置对象任务的任务通知值为 0xfff,但是并不会覆盖对象任务通过
  12. xTaskNotifyWait()和 ulTaskNotifyTake()这两个函数获取到的已经存在
  13. 的任务通知值。对象任务的前一个任务通知值存储在变量 ulPreviousValue 中*/
  14. if ( xTaskNotifyAndQuery( xTask4Handle,
  15. 0xfff,
  16. eSetValueWithoutOverwrite,
  17. &ulPreviousValue ) == pdPASS)
  18. {
  19. /* 任务通知值已经更新 */
  20. } else {
  21. /* 任务通知值没有更新 */
  22. }

7.xTaskNotifyAndQueryFromISR()

image.png

  1. void vAnISR( void )
  2. {
  3. /* xHigherPriorityTaskWoken 在使用之前必须设置为 pdFALSE */
  4. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  5. uint32_t ulPreviousValue;
  6. /* 设置目标任务 xTask1Handle 的任务通知值的位 8 为 1
  7. 在任务通知值的位 8 被更新之前把上一次的值存储在变量 ulPreviousValue 中*/
  8. xTaskNotifyAndQueryFromISR( xTask1Handle,
  9. ( 1UL << 8UL ),
  10. eSetBits,
  11. &ulPreviousValue,
  12. &xHigherPriorityTaskWoken);
  13. portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
  14. }

22.4.2 获取任务通知函数

获取任务通知函数只能用在任 中,没有带中断保护版本,因 此只有两 个 API 函 数: ulTaskNotifyTake() 和
xTaskNotifyWait ()。前者是为代替二值信号量和计数信号量而专门设计的,它和发送通知API 函数 xTaskNotifyGive()、 vTaskNotifyGiveFromISR()配合使用;后者是全功能版的等待通知,可以根据不同的参数实现轻量级二值信号量、计数信号量、事件组和长度为 1 的队列。

1.ulTaskNotifyTake()

ulTaskNotifyTake()作为二值信号量和计数信号量的一种轻量级实现,速度更快。 如果FreeRTOS 中使用函数 xSemaphoreTake() 来获取信号量, 这个时候则可以试试使用函数ulTaskNotifyTake()来代替。
对于这个函数,任务通知值为 0,对应信号量无效,如果任务设置了阻塞等待,任务被阻塞挂起。当其他任务或中断发送了通知值使其不为 0 后,通知变为有效,等待通知的任务将获取到通知, 并且在退出时候根据用户传递的第一个参数 xClearCountOnExit 选择清零通知值或者执行减一操作。
xTaskNotifyTake()在退出的时候处理任务的通知值的时候有两种方法:

  1. 在函数退出时将通知值清零,这种方法适用于实现二值信号量
  2. 在函数退出时将通知值减 1, 这种方法适用于实现计数信号量

当一个任务使用其自身的任务通知值作为二值信号量或者计数信号量时, 其他任务应该使用函数 xTaskNotifyGive()或者 xTaskNotify( ( xTaskToNotify ), ( 0 ), eIncrement )来向其发送信号量。
image.png
ulTaskNotifyTake()

  1. #if( configUSE_TASK_NOTIFICATIONS == 1 )
  2. uint32_t ulTaskNotifyTake( BaseType_t xClearCountOnExit, TickType_t xTicksToWait )
  3. {
  4. uint32_t ulReturn;
  5. taskENTER_CRITICAL();
  6. {
  7. // 如果通知值为 0 ,阻塞任务
  8. // 默认初始化通知值为 0, 说明没有未读通知
  9. if( pxCurrentTCB->ulNotifiedValue == 0UL )
  10. {
  11. /* 标记任务状态 : 等待消息通知 */
  12. pxCurrentTCB->ucNotifyState = taskWAITING_NOTIFICATION;
  13. //用户指定超时时间了,那就进入等待状态
  14. if( xTicksToWait > ( TickType_t ) 0 )
  15. {
  16. //根据用户指定超时时间将任务添加到延时列表
  17. prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
  18. traceTASK_NOTIFY_TAKE_BLOCK();
  19. // 切换任务
  20. portYIELD_WITHIN_API();
  21. }
  22. else
  23. {
  24. mtCOVERAGE_TEST_MARKER();
  25. }
  26. }
  27. else
  28. {
  29. mtCOVERAGE_TEST_MARKER();
  30. }
  31. }
  32. taskEXIT_CRITICAL();
  33. // 到这里说明其它任务或中断向这个任务发送了通知,或者任务阻塞超时,现在继续处理
  34. taskENTER_CRITICAL();
  35. {
  36. traceTASK_NOTIFY_TAKE();
  37. // 获取任务通知值
  38. ulReturn = pxCurrentTCB->ulNotifiedValue;
  39. // 看看任务通知是否有效,有效则返回
  40. if( ulReturn != 0UL )
  41. {
  42. //是否需要清除通知
  43. if( xClearCountOnExit != pdFALSE )
  44. {
  45. // 清除
  46. pxCurrentTCB->ulNotifiedValue = 0UL;
  47. }
  48. else
  49. {
  50. // -1
  51. pxCurrentTCB->ulNotifiedValue = ulReturn - 1;
  52. }
  53. }
  54. else
  55. {
  56. mtCOVERAGE_TEST_MARKER();
  57. }
  58. //恢复任务通知状态变量
  59. pxCurrentTCB->ucNotifyState = taskNOT_WAITING_NOTIFICATION;
  60. }
  61. taskEXIT_CRITICAL();
  62. return ulReturn;
  63. }
  64. #endif /* configUSE_TASK_NOTIFICATIONS */
  65. /*-----------------------------------------------------------*/

使用示例:

  1. /* 中断服务程序:向一个任务发送任务通知 */
  2. void vANInterruptHandler( void )
  3. {
  4. BaseType_t xHigherPriorityTaskWoken;
  5. /* 清除中断 */
  6. prvClearInterruptSource();
  7. xHigherPriorityTaskWoken = pdFALSE;
  8. /* 发送任务通知,并解锁阻塞在该任务通知下的任务 */
  9. vTaskNotifyGiveFromISR( xHandlingTask, &xHigherPriorityTaskWoken );
  10. portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
  11. }
  12. /* 任务:阻塞在一个任务通知上 */
  13. void vHandlingTask( void *pvParameters )
  14. {
  15. for ( ;; ) {
  16. ulTaskNotifyTake( pdTRUE, /* 在退出前清 0 任务通知值 */
  17. portMAX_DELAY ); /* 无限阻塞 */
  18. /* RTOS 任务通知被当作二值信号量使用
  19. 当处理完所有的事情后继续等待下一个任务通知*/
  20. do {
  21. xEvent = xQueryPeripheral();
  22. if ( xEvent != NO_MORE_EVENTS ) {
  23. vProcessPeripheralEvent( xEvent );
  24. }
  25. }while ( xEvent != NO_MORE_EVENTS );
  26. }
  27. }

2.xTaskNotifyWait()

xTaskNotifyWait()函数用于实现全功能版的等待任务通知,根据用户指定的参数的不同,可以灵活的用于实现轻量级的消息队列队列、二值信号量、计数信号量和事件组功能,并带有超时等待。
image.png

  1. #if( configUSE_TASK_NOTIFICATIONS == 1 )
  2. BaseType_t xTaskNotifyWait( uint32_t ulBitsToClearOnEntry,
  3. uint32_t ulBitsToClearOnExit,
  4. uint32_t *pulNotificationValue,
  5. TickType_t xTicksToWait )
  6. {
  7. BaseType_t xReturn;
  8. taskENTER_CRITICAL(); // 进入临界区
  9. {
  10. /* 只有任务当前没有收到任务通知,才会将任务阻塞 */
  11. if( pxCurrentTCB->ucNotifyState != taskNOTIFICATION_RECEIVED )
  12. {
  13. /* 使用任务通知值之前,根据用户指定参数 ulBitsToClearOnEntryClear
  14. 将通知值的某些或全部位清零 */
  15. pxCurrentTCB->ulNotifiedValue &= ~ulBitsToClearOnEntry;
  16. /* 设置任务状态标识:等待通知 */
  17. pxCurrentTCB->ucNotifyState = taskWAITING_NOTIFICATION;
  18. /* 设置阻塞,添加到延迟列表 */
  19. if( xTicksToWait > ( TickType_t ) 0 )
  20. {
  21. prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
  22. traceTASK_NOTIFY_WAIT_BLOCK();
  23. portYIELD_WITHIN_API();
  24. }
  25. else
  26. {
  27. mtCOVERAGE_TEST_MARKER();
  28. }
  29. }
  30. else
  31. {
  32. mtCOVERAGE_TEST_MARKER();
  33. }
  34. }
  35. taskEXIT_CRITICAL();
  36. /* 等到通知 */
  37. taskENTER_CRITICAL();
  38. {
  39. traceTASK_NOTIFY_WAIT();
  40. /* 判断是不是无效的参数 */
  41. if( pulNotificationValue != NULL )
  42. {
  43. /* 返回当前通知值,通过指针参数传递 */
  44. *pulNotificationValue = pxCurrentTCB->ulNotifiedValue;
  45. }
  46. /* 判断是否是因为任务阻塞超时,因为如果有
  47. 任务发送了通知的话,任务通知状态会被改变 */
  48. if( pxCurrentTCB->ucNotifyState == taskWAITING_NOTIFICATION )
  49. {
  50. /* 没有收到任务通知,是阻塞超时 */
  51. xReturn = pdFALSE;
  52. }
  53. else
  54. {
  55. /* 收到任务值,先将参数 ulBitsToClearOnExit 取反后与通知值位做按位与运算
  56. 在退出函数前,将通知值的某些或者全部位清零. */
  57. pxCurrentTCB->ulNotifiedValue &= ~ulBitsToClearOnExit;
  58. xReturn = pdTRUE;
  59. }
  60. // 设置任务的通知状态
  61. pxCurrentTCB->ucNotifyState = taskNOT_WAITING_NOTIFICATION;
  62. }
  63. taskEXIT_CRITICAL();
  64. return xReturn;
  65. }
  66. #endif /* configUSE_TASK_NOTIFICATIONS */
  67. /*-----------------------------------------------------------*/


使用示例:
这个通知的话

  1. /* 这个任务展示使用任务通知值的位来传递不同的事件
  2. 这在某些情况下可以代替事件标志组。 */
  3. void vAnEventProcessingTask( void *pvParameters )
  4. {
  5. uint32_t ulNotifiedValue;
  6. /* 等待任务通知,无限期阻塞(没有超时,所以没有必要检查函数返回值)
  7. 这个任务的任务通知值的位由标志事件发生的任务或者中断来设置*/
  8. xTaskNotifyWait( 0x00, /* 在进入的时候不清除通知值的任何位 */
  9. ULONG_MAX, /* 在退出的时候复位通知值为 0 */
  10. &ulNotifiedValue, /* 任务通知值传递到变量 */
  11. portMAX_DELAY ); /* 无限期等待 */
  12. /* 根据任务通知值里面的各个位的值处理事情 */
  13. if ( ( ulNotifiedValue & 0x01 ) != 0 ) {
  14. /* 位 0 被置 1 */
  15. prvProcessBit0Event();
  16. }
  17. if ( ( ulNotifiedValue & 0x02 ) != 0 ) {
  18. /* 位 1 被置 1 */
  19. prvProcessBit1Event();
  20. }
  21. if ( ( ulNotifiedValue & 0x04 ) != 0 ) {
  22. /* 位 1 被置 1 */
  23. prvProcessBit2Event();
  24. }
  25. }