你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

【经验分享】STM32G0学习手册——FreeRTOS任务通知

[复制链接]
STMCU小助手 发布时间:2021-11-14 22:04
概述
        每个任务都有一个32位的通知值,可以替代二值信号量,计数信号量,事件组,也可以代替长度为1的队列。

        有以下几种方式发送通知给任务:

1.有通知未读,则不覆盖通知值

2.直接覆盖通知值

3.设置通知值一个或者多个位,当做事件组来使用

4.递增/递减通知,当做信号量来使用

        有以下缺点:

1.只能有一个任务接收通知消息

2.只有等待通知的任务可以被阻塞,发送通知的任务都不会因为发送失败而进入阻塞。

代替二值信号量
在FreeRTOSConfig.h里开启任务通知
  1. /*Turn on task notifications.*/
  2. #define configUSE_TASK_NOTIFICATIONS      1
复制代码

在main.c文件里面修改代码

  1. /* USER CODE BEGIN Includes */

  2. #include "FreeRTOS.h"
  3. #include "task.h"
  4. #include <stdio.h>
  5. #include <string.h>

  6. /* USER CODE END Includes */

  7. /* USER CODE BEGIN PV */
  8. static TaskHandle_t AppTask_Handle = NULL;
  9. static TaskHandle_t KeyTask_Handle = NULL;
  10. static TaskHandle_t LedTask_Handle = NULL;

  11. extern int f_printf(const char *format, ...);
  12. /* USER CODE END PV */

  13. /* USER CODE BEGIN PFP */
  14. static void AppTaskCreate(void);
  15. static void KeyTaskCreate(void);
  16. static void LedTaskCreate(void);
  17. /* USER CODE END PFP */

  18. int main()
  19. {

  20.   /* USER CODE BEGIN WHILE */
  21.   BaseType_t xReturn = pdFAIL;
  22.         
  23.         xReturn = xTaskCreate((TaskFunction_t)AppTaskCreate,
  24.                         (const char*)   "APPTASKCREATE",
  25.                         (uint16_t)      128,
  26.                         (void *)        NULL,
  27.                         (UBaseType_t)   1 ,
  28.                         (TaskHandle_t*) &AppTask_Handle);
  29.         
  30.   if(xReturn == pdPASS)
  31.         {
  32.     f_printf("AppTask Create PASS.\r\n");
  33.                 vTaskStartScheduler();
  34.         }
  35.                                                                                                 
  36.   while (1)
  37.   {
  38.     /* USER CODE END WHILE */
  39.     /* USER CODE BEGIN 3 */
  40.   }
  41.   /* USER CODE END 3 */
  42. }

  43. /* USER CODE BEGIN 4 */
  44. static void AppTaskCreate(void)
  45. {
  46.   BaseType_t xReturn = pdFAIL;

  47.         xReturn = xTaskCreate((TaskFunction_t)LedTaskCreate,
  48.                         (const char *)  "LEDTASKCREATE",
  49.                         (uint16_t)      128,
  50.                         (void *)        NULL,
  51.   /*If the priority here is the same as the key task, printf will not print it out.        */
  52.                         (UBaseType_t)   2,      
  53.                         (TaskHandle_t *)&LedTask_Handle);
  54.   if(xReturn == pdPASS)
  55.                 f_printf("LedTask Create PASS.\r\n");
  56.         
  57.         xReturn = xTaskCreate((TaskFunction_t)KeyTaskCreate,
  58.                         (const char *)  "KEYTASKCREATE",
  59.                         (uint16_t)      128,
  60.                         (void *)        NULL,
  61.                         (UBaseType_t)   1,
  62.                         (TaskHandle_t *)&KeyTask_Handle);
  63.   if(xReturn == pdPASS)
  64.         {
  65.                 f_printf("KeyTask Create PASS.\r\n");
  66.     vTaskDelete(AppTask_Handle);
  67.         }

  68. }
  69. static void KeyTaskCreate(void)
  70. {
  71.         while(1)
  72.         {
  73.     if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0))
  74.           {
  75.       vTaskDelay(50);
  76.       if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0))
  77.                   {
  78.         xTaskNotifyGive(LedTask_Handle);
  79.                           f_printf("Give LedTask_Handle PASS.\r\n");
  80.                   }
  81.           }
  82.           vTaskDelay(100);
  83.   }
  84. }
  85. static void LedTaskCreate(void)
  86. {
  87.         uint32_t ulreturn=0;
  88.         while(1)
  89.         {
  90.                
  91.     ulreturn=ulTaskNotifyTake(pdTRUE,portMAX_DELAY);
  92.                 if(ulreturn==1)
  93.                 {
  94.                         /*It won’t be printed out at the same level*/
  95.                   f_printf("LedTask TAKE PASS.\r\n");
  96.             HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_1);
  97.                         
  98.                 }
  99.                 else
  100.                         f_printf("LedTask TAKE FAIL.\r\n");
  101.     vTaskDelay(100);
  102.   }
  103. }
  104. /* USER CODE END 4 */
  105. 代替计数信号量
  106. 在FreeRTOSConfig.h里开启任务通知

  107. /*Turn on task notifications.*/
  108. #define configUSE_TASK_NOTIFICATIONS      1
  109. 在main.c文件里面修改代码

  110. /* USER CODE BEGIN Includes */

  111. #include "FreeRTOS.h"
  112. #include "task.h"
  113. #include <stdio.h>
  114. #include <string.h>

  115. /* USER CODE END Includes */

  116. /* USER CODE BEGIN PV */
  117. static TaskHandle_t AppTask_Handle = NULL;
  118. static TaskHandle_t KeyTask_Handle = NULL;
  119. static TaskHandle_t LedTask_Handle = NULL;

  120. extern int f_printf(const char *format, ...);

  121. /* USER CODE END PV */

  122. /* USER CODE BEGIN PFP */
  123. static void AppTaskCreate(void);
  124. static void KeyTaskCreate(void);
  125. static void LedTaskCreate(void);
  126. /* USER CODE END PFP */

  127. int main()
  128. {
  129.   /* USER CODE BEGIN WHILE */
  130.   BaseType_t xReturn = pdFAIL;        

  131.   xReturn = xTaskCreate((TaskFunction_t)AppTaskCreate,
  132.                         (const char *)  "APPTASKCREATE",
  133.                         (uint16_t)      128,
  134.                         (void *)        NULL,
  135.                         (UBaseType_t)   1,
  136.                         (TaskHandle_t*) &AppTask_Handle);

  137.   if(xReturn == pdPASS)
  138.         {
  139.     f_printf("AppTask Create PASS.\r\n");
  140.     vTaskStartScheduler();
  141.         }
  142.   while (1)
  143.   {
  144.     /* USER CODE END WHILE */

  145.     /* USER CODE BEGIN 3 */
  146.   }
  147.   /* USER CODE END 3 */
  148. }


  149. /* USER CODE BEGIN 4 */
  150. static void AppTaskCreate(void)
  151. {
  152.         BaseType_t xReturn = pdFAIL;

  153.   xReturn = xTaskCreate((TaskFunction_t)LedTaskCreate,
  154.                         (const char *)  "LEDTASKCREATE",
  155.                         (uint16_t)      128,
  156.                         (void*)         NULL,
  157.                         (UBaseType_t)   1,
  158.                         (TaskHandle_t *)&LedTask_Handle);
  159.   if( xReturn == pdPASS)
  160.           f_printf("LED Task Create PASS.\r\n");
  161.         
  162.         xReturn = xTaskCreate((TaskFunction_t)KeyTaskCreate,
  163.                         (const char *)  "KEYTASKCREATE",
  164.                         (uint16_t )     128,
  165.                         (void *)        NULL,
  166.                         (UBaseType_t)   2,
  167.                         (TaskHandle_t *)&KeyTask_Handle);
  168.   if( xReturn == pdPASS)
  169.         {
  170.     f_printf("KEY Task Create PASS.\r\n");
  171.     vTaskDelete(AppTask_Handle);
  172.         }
  173. }
  174. static void KeyTaskCreate(void)
  175. {
  176.   while(1)
  177.         {
  178.     if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0))
  179.                 {
  180.                         vTaskDelay(40);
  181.       if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0))
  182.                         {
  183.                                 xTaskNotifyGive(LedTask_Handle);
  184.                                 f_printf("LED TASK GIVE PASS.\r\n");
  185.                         }
  186.                 }
  187.     vTaskDelay(40);
  188.         }
  189.                
  190. }
  191. static void LedTaskCreate(void)
  192. {
  193.         uint32_t ulValue = 0 ;
  194.         while(1)
  195.         {
  196.     ulValue = ulTaskNotifyTake( pdFALSE , 0);
  197.                
  198.                 /*Greater than 0 proves that there is still a notification value that can be
  199.          *reduced by 1. Equal to 0 indicates that no notification is available
  200.          */
  201.                 if(ulValue >0)
  202.                 {
  203.                         HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_1);
  204.                         f_printf("LED Task PASS.\r\n");
  205.                 }               
  206.                 vTaskDelay(1500);
  207.         }
  208. }

  209. /* USER CODE END 4 */
复制代码

代替事件组
在FreeRTOSConfig.h里开启任务通知

  1. /*Turn on task notifications.*/
  2. #define configUSE_TASK_NOTIFICATIONS      1
复制代码

在main.c文件里面修改代码

  1. /* USER CODE BEGIN Includes */

  2. #include "FreeRTOS.h"
  3. #include "task.h"

  4. #include <stdio.h>
  5. #include <string.h>

  6. /* USER CODE END Includes */

  7. /* USER CODE BEGIN PV */
  8. static TaskHandle_t AppTask_Handle = NULL;
  9. static TaskHandle_t KeyTask_Handle = NULL;
  10. static TaskHandle_t LedTask_Handle = NULL;

  11. extern int f_printf(const char *format, ...);
  12. /* USER CODE END PV */

  13. /* USER CODE BEGIN PFP */

  14. static void AppTaskCreate(void);
  15. static void KeyTaskCreate(void);
  16. static void LedTaskCreate(void);

  17. /* USER CODE END PFP */

  18. int main()
  19. {
  20.   /* USER CODE BEGIN WHILE */
  21.   BaseType_t xReturn = pdFAIL;
  22.         
  23.   xReturn = xTaskCreate((TaskFunction_t)AppTaskCreate,
  24.                             (const char *)  "",
  25.                                                 (uint16_t)      128,
  26.                                                 (void *)        NULL,
  27.                                                 (UBaseType_t)  1,
  28.                                                 (TaskHandle_t *)&AppTask_Handle);
  29.         if( xReturn == pdPASS)
  30.         {
  31.                 f_printf("APP Task Start.\r\n");
  32.                 vTaskStartScheduler();
  33.         }

  34.   while (1)
  35.   {
  36.     /* USER CODE END WHILE */

  37.     /* USER CODE BEGIN 3 */
  38.   }
  39.   /* USER CODE END 3 */
  40. }

  41. /* USER CODE BEGIN 4 */
  42. static void AppTaskCreate(void)
  43. {
  44.         BaseType_t xReturn = pdFAIL;
  45.         
  46.         xReturn = xTaskCreate((TaskFunction_t)LedTaskCreate,
  47.                               (const char *)  "",
  48.                                                                                                 (uint16_t)      128,
  49.                                                                                                 (void *)        NULL,
  50.                                                                                                 (UBaseType_t)   1,
  51.                                                                                                 (TaskHandle_t*) &LedTask_Handle);
  52.   if( xReturn == pdPASS)
  53.                 f_printf("LED TASK PASS.\r\n");
  54.         
  55.         xReturn = xTaskCreate((TaskFunction_t)KeyTaskCreate,
  56.                               (const char *)   "",
  57.                                                                                                 (uint16_t )      128,
  58.                                                                                                 (void *)         NULL,
  59.                                                                                                 (UBaseType_t)   2,
  60.                                                                                                 (TaskHandle_t *) &KeyTask_Handle);
  61.   if( xReturn == pdPASS)
  62.         {
  63.     f_printf("KEY TASK PASS.\r\n");
  64.                 vTaskDelete(AppTask_Handle);
  65.         }                                                                                
  66. }
  67. static void KeyTaskCreate(void)
  68. {
  69.   while(1)
  70.         {
  71.     if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0))
  72.                 {
  73.                         vTaskDelay(40);
  74.                         if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0))
  75.                         {
  76.                                 xTaskNotify(LedTask_Handle,0x00000001,eSetBits);
  77.                                 f_printf("PA0.\r\n");
  78.                         }
  79.                 }
  80.                 else if(HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_12) == 0)
  81.                 {
  82.                         vTaskDelay(40);
  83.                         if(HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_12) == 0)
  84.                         {
  85.                                 xTaskNotify(LedTask_Handle,0x00000002,eSetBits);
  86.                                 f_printf("PC12.\r\n");
  87.                         }
  88.                 }
  89.                
  90.                 vTaskDelay(50);
  91.         }
  92. }
  93. static void LedTaskCreate(void)
  94. {
  95.         uint32_t ulReturn = 0;
  96.         BaseType_t xReturn = pdFAIL;
  97.         while(1)
  98.         {
  99.                 xReturn = xTaskNotifyWait( 0 , 0x00000003 , &ulReturn, portMAX_DELAY);
  100.                
  101.                 if(xReturn == pdTRUE)
  102.                 {
  103.                         if( (ulReturn & 0x1)==1)
  104.                         {
  105.                                 HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_2);
  106.                                 f_printf("LED1.\r\n");
  107.                         }
  108.                         else if( (ulReturn & 0x02)==2)
  109.                         {
  110.                                 HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_1);
  111.                                 f_printf("LED2.\r\n");
  112.                         }
  113.                 }
  114.                 else
  115.                         f_printf("Read Notify FAIL.\r\n");

  116.                 vTaskDelay(1500);
  117.         }
  118. }
  119. /* USER CODE END 4 */
复制代码

代替长度为1的消息队列
在FreeRTOSConfig.h里开启任务通知
  1. /*Turn on task notifications.*/
  2. #define configUSE_TASK_NOTIFICATIONS      1
复制代码

在main.c文件里面修改代码

  1. /* USER CODE BEGIN Includes */

  2. #include "FreeRTOS.h"
  3. #include "task.h"
  4. #include <stdio.h>
  5. #include <string.h>

  6. /* USER CODE END Includes */

  7. /* USER CODE BEGIN PV */
  8. static TaskHandle_t AppTask_Handle = NULL;
  9. static TaskHandle_t KeyTask_Handle = NULL;
  10. static TaskHandle_t QueueTask_Handle = NULL;
  11. static uint32_t Message = 0;
  12. static uint32_t *printf_value=&Message;
  13. extern int f_printf(const char *format, ...);

  14. /* USER CODE END PV */

  15. /* USER CODE BEGIN PFP */
  16. static void AppTaskCreate(void);
  17. static void KeyTaskCreate(void);
  18. static void QueueTaskCreate(void);
  19. /* USER CODE END PFP */

  20. int main()
  21. {
  22.   /* USER CODE BEGIN WHILE */
  23.   BaseType_t xReturn = pdFAIL;
  24.         
  25.         xReturn = xTaskCreate((TaskFunction_t )AppTaskCreate,
  26.                               (const char *)   "",
  27.                                                   (uint16_t)       128,
  28.                                                   (void *)         NULL,
  29.                                                   (UBaseType_t)    1,
  30.                                                   (TaskHandle_t *) &AppTask_Handle);
  31.                                                                                                 
  32.   if( xReturn == pdPASS)
  33.         {
  34.     f_printf("AppTask Start.\r\n");
  35.                 vTaskStartScheduler();
  36.         }
  37.         
  38.   while (1)
  39.   {
  40.     /* USER CODE END WHILE */

  41.     /* USER CODE BEGIN 3 */
  42.   }
  43.   /* USER CODE END 3 */
  44. }

  45. /* USER CODE BEGIN 4 */
  46. static void AppTaskCreate(void)
  47. {
  48.         BaseType_t xReturn ;
  49.         
  50.         xReturn = xTaskCreate( (TaskFunction_t)QueueTaskCreate,
  51.                          (const char *)  "",
  52.                                                                                                  (uint16_t)      128,
  53.                                                                                                  (void *)        NULL,
  54.                                                                                                  (UBaseType_t)   2,
  55.                                                                                                  (TaskHandle_t *)&QueueTask_Handle);
  56.   if( xReturn ==pdPASS )
  57.                 f_printf("QueueTask Create PASS.\r\n");
  58.         
  59.         xReturn = xTaskCreate( (TaskFunction_t)KeyTaskCreate,
  60.                          (const char *)  "",
  61.                          (uint16_t)      128,
  62.                                                                                                  (void *)        NULL,
  63.                                                                                                  (UBaseType_t)   1,
  64.                                                                                                  (TaskHandle_t *)&KeyTask_Handle);
  65.   if (xReturn == pdPASS)
  66.         {
  67.           f_printf("KeyTask Create PASS.\r\n");
  68.                 vTaskDelete(AppTask_Handle);
  69.         }
  70. }
  71. static void KeyTaskCreate(void)
  72. {
  73.         while(1)
  74.         {
  75.                 if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0))
  76.                 {
  77.                         vTaskDelay(80);
  78.                         if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0))
  79.                         {
  80.                                 xTaskNotify(QueueTask_Handle, (uint32_t)Message, eSetValueWithOverwrite);
  81.                         }
  82.                 }
  83.                 vTaskDelay(80);
  84.         }
  85. }
  86. static void QueueTaskCreate(void)
  87. {
  88.         while(1)
  89.         {
  90.                 /*work.*/
  91.                 xTaskNotifyWait(0, 0xFFFFFFFF, printf_value, portMAX_DELAY);
  92.                 f_printf("Message is :%d\r\n",*printf_value);
  93.                 Message=*printf_value;
  94.                
  95.                 /*The second method works too*/
  96.         /*
  97.                 xTaskNotifyWait(0, 0xFFFFFFFF, &Message, portMAX_DELAY);
  98.                 f_printf("Message is :%d\r\n",Message);
  99.         */
  100.                 Message++;
  101.                 if(Message > 10)
  102.                         Message = 0;
  103.                 vTaskDelay(1000);
  104.         }
  105. }
  106. /* USER CODE END 4 */
复制代码



收藏 评论0 发布时间:2021-11-14 22:04

举报

0个回答

所属标签

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版