[AT32L021] 【AT-START-L021测评】FreeRTOS挂起和恢复任务

[复制链接]
1413|0
 楼主| 比神乐 发表于 2024-11-30 18:03 | 显示全部楼层 |阅读模式
代码:
  1. #include "at32l021_board.h"
  2. #include "at32l021_clock.h"
  3. #include "FreeRTOS.h"
  4. #include "task.h"

  5. /** @addtogroup UTILITIES_examples
  6.   * @{
  7.   */

  8. /** @addtogroup FreeRTOS_demo
  9.   * @{
  10.   */
  11. TaskHandle_t led2_handler;
  12. TaskHandle_t led3_handler;

  13. /* led2 task */
  14. void led2_task_function(void *pvParameters);
  15. /* led3 task */
  16. void led3_task_function(void *pvParameters);

  17. /**
  18.   * [url=home.php?mod=space&uid=247401]@brief[/url]  main function.
  19.   * @param  none
  20.   * @retval none
  21.   */
  22. int main(void)
  23. {
  24.   system_clock_config();

  25.   /* init led2 and led3 */
  26.   at32_led_init(LED2);
  27.   at32_led_init(LED3);

  28.   /* init usart1 */
  29.   uart_print_init(115200);

  30.   /* enter critical */
  31.   taskENTER_CRITICAL();

  32.   /* create led2 task */
  33.   if(xTaskCreate((TaskFunction_t )led2_task_function,
  34.                  (const char*    )"LED2_task",
  35.                  (uint16_t       )512,
  36.                  (void*          )NULL,
  37.                  (UBaseType_t    )2,
  38.                  (TaskHandle_t*  )&led2_handler) != pdPASS)
  39.   {
  40.     printf("LED2 task could not be created as there was insufficient heap memory remaining.\r\n");
  41.   }
  42.   else
  43.   {
  44.     printf("LED2 task was created successfully.\r\n");
  45.   }
  46.   /* create led3 task */
  47.   if(xTaskCreate((TaskFunction_t )led3_task_function,
  48.                  (const char*    )"LED3_task",
  49.                  (uint16_t       )512,
  50.                  (void*          )NULL,
  51.                  (UBaseType_t    )2,
  52.                  (TaskHandle_t*  )&led3_handler) != pdPASS)
  53.   {
  54.     printf("LED3 task could not be created as there was insufficient heap memory remaining.\r\n");
  55.   }
  56.   else
  57.   {
  58.     printf("LED3 task was created successfully.\r\n");
  59.   }

  60.   /* exit critical */
  61.   taskEXIT_CRITICAL();

  62.   /* start scheduler */
  63.   vTaskStartScheduler();
  64. }

  65. /* led2 task function */
  66. void led2_task_function(void *pvParameters)
  67. {
  68.         static int i=0;
  69.         static int num=0;
  70.   while(1)
  71.   {
  72.                 i++;
  73.                 if(i==5)
  74.                 {
  75.                         printf("在任务中挂起task3\r\n");
  76.                                                 vTaskSuspend(led3_handler);
  77.                 }
  78.                 else if(i==10)
  79.                 {
  80.                         printf("在任务中恢复task3\r\n");
  81.                                                 vTaskResume(led3_handler);
  82.                 }
  83.     printf("task2_num:%d\r\n",++num);/* 打印数值 */
  84.     vTaskDelay(1000);
  85.   }
  86. }

  87. /* led3 task function */
  88. void led3_task_function(void *pvParameters)
  89. {
  90.         static int num=0;
  91.   while(1)
  92.   {
  93.     printf("task3_num:%d\r\n",++num);/* 打印数值 */
  94.     vTaskDelay(500);
  95.   }
  96. }
运行效果图:
10.jpg 11.jpg
您需要登录后才可以回帖 登录 | 注册

本版积分规则

470

主题

3537

帖子

7

粉丝
快速回复 在线客服 返回列表 返回顶部