码迷,mamicode.com
首页 > 其他好文 > 详细

4-FreeRTOS 静态任务的创建/删除

时间:2020-12-24 12:00:47      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:cal   src   bsp   code   cat   count   com   font   函数   

需求:静态创建任务(任务堆栈、控制块由用户自己指定)

①用一个任务创建两个任务Task1和Task2后,将自身任务删除

②在Task1执行5次后,用Task1将Task2删除

③Task1执行10次后,将自身删除

 

代码:

①需要将该宏打开:

#define configSUPPORT_STATIC_ALLOCATION              1      //静态内存

如果使用静态方法 的 话 需 要 用 户 实 现 两 个 函 数 vApplicationGetIdleTaskMemory() vApplicationGetTimerTaskMemory()。 通过这两个函数来给空闲任务和定时器服务任务的任务堆栈和任务控制块分配内存

技术图片

 

 

 1 //空闲任务所需内存
 2 void vApplicationGetIdleTaskMemory( StaticTask_t ** ppxIdleTaskTCBBuffer,
 3                                     StackType_t ** ppxIdleTaskStackBuffer,
 4                                     uint32_t * pulIdleTaskStackSize )
 5 {
 6     
 7     * ppxIdleTaskTCBBuffer = &IdleTaskTCB;//空闲任务控制块
 8     * ppxIdleTaskStackBuffer= IdleTaskStack; //空闲任务任务堆栈
 9     * pulIdleTaskStackSize = configMINIMAL_STACK_SIZE; //堆栈大小
10   
11 }
12 
13 
14 //定时器任务所需内存
15 void vApplicationGetTimerTaskMemory( StaticTask_t ** ppxTimerTaskTCBBuffer,
16                                      StackType_t ** ppxTimerTaskStackBuffer,
17                                      uint32_t * pulTimerTaskStackSize )
18 {
19     
20     * ppxTimerTaskTCBBuffer = &TimerTaskTCB;//任务控制块
21     * ppxTimerTaskStackBuffer= TimerTaskStack; //任务堆栈
22     * pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH; //堆栈大小
23   
24 }

 

②静态创建过程

  1 //----------------------------------------任务优先级
  2 #define START_TASK_PRIO 1 
  3 #define TASK1_PRIO 2 
  4 #define TASK2_PRIO 3 //优先级高
  5 
  6 //----------------------------------------任务堆栈大小
  7 #define START_STK_SIZE 128
  8 #define TASK1_STK_SIZE 128 
  9 #define TASK2_STK_SIZE 128 
 10 
 11 //----------------------------------------任务堆栈
 12 StackType_t StartTaskStack[START_STK_SIZE]; 
 13 StackType_t Task1TaskStack[TASK1_STK_SIZE];
 14 StackType_t Task2TaskStack[TASK2_STK_SIZE];
 15 
 16 //----------------------------------------任务控制块
 17 StaticTask_t StartTaskTCB; 
 18 StaticTask_t Task1TaskTCB;
 19 StaticTask_t Task2TaskTCB;
 20 
 21 
 22 //----------------------------------------任务句柄
 23 TaskHandle_t Task1_Handler; 
 24 TaskHandle_t Task2_Handler; 
 25 TaskHandle_t StartTask_Handler; 
 26 
 27 
 28 //----------------------------------------任务函数
 29 void start_task(void *pvParameters); 
 30 void task1_task(void *pvParameters); 
 31 void task2_task(void *pvParameters); 
 32 
 33 
 34 
 35 //定时器任务所需内存
 36 void vApplicationGetTimerTaskMemory( StaticTask_t ** ppxTimerTaskTCBBuffer,
 37                                      StackType_t ** ppxTimerTaskStackBuffer,
 38                                      uint32_t * pulTimerTaskStackSize )
 39 {
 40     
 41     * ppxTimerTaskTCBBuffer = &TimerTaskTCB;//任务控制块
 42     * ppxTimerTaskStackBuffer= TimerTaskStack; //任务堆栈
 43     * pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH; //堆栈大小
 44   
 45 }
 46 
 47 
 48 
 49 int main(void)
 50 {
 51   User_GPIO_Init();
 52     Delay_init();
 53     USART_Config();
 54     
 55 //    TIM6_Time_Init(9999,7199);//定时1s
 56 //  TIM7_Time_Init(9999,7199);//定时1s
 57 
 58     
 59     StartTask_Handler= xTaskCreateStatic( (TaskFunction_t) start_task,
 60                          (const char * ) "start_task",     
 61                          (uint32_t) START_STK_SIZE,  //任务堆栈大小
 62                          (void * ) NULL,
 63                          (UBaseType_t) START_TASK_PRIO,
 64                          (StackType_t * ) StartTaskStack,
 65                          (StaticTask_t *) &StartTaskTCB);
 66                                  
 67     if(StartTask_Handler==NULL) //创建成功
 68          GPIO_SetBits(GPIOA, GPIO_Pin_8);
 69         
 70     vTaskStartScheduler(); //开启任务调度
 71     
 72 }
 73 
 74 void start_task( void * pvParameters )
 75 {
 76     taskENTER_CRITICAL(); //进入临界区
 77     
 78    //创建任务Task1
 79     Task1_Handler= xTaskCreateStatic( (TaskFunction_t ) task1_task,
 80                                          (const char *   ) "task1_task",     
 81                                          (uint32_t       ) TASK1_STK_SIZE,  //任务堆栈大小
 82                                          (void *         ) NULL,
 83                                          (UBaseType_t    ) TASK1_PRIO,
 84                                          (StackType_t   *) Task1TaskStack,
 85                                          (StaticTask_t  *) &Task1TaskTCB );
 86                                          
 87                                          
 88   //创建任务Task2
 89     Task2_Handler= xTaskCreateStatic( (TaskFunction_t) task2_task,
 90                                          (const char   *) "task2_task",     
 91                                          (uint32_t      ) TASK2_STK_SIZE,  //任务堆栈大小
 92                                          (void *        ) NULL,
 93                                          (UBaseType_t   ) TASK2_PRIO,
 94                                          (StackType_t  *) Task2TaskStack,
 95                                          (StaticTask_t *) &Task2TaskTCB );
 96                                          
 97                                          
 98                                                  
 99         vTaskDelete(StartTask_Handler); //vTaskDelete(NULL)也可以   删除开始任务
100         taskEXIT_CRITICAL();            //退出临界区                                                 
101 }
102 
103 
104 
105 void task1_task(void *pvParameters)
106 {  
107     uint8_t count_num=0; 
108    
109     while(1)
110     {
111       count_num++;
112         printf("任务1执行:%d次\n", count_num);
113         
114         if(count_num==5) 
115         {
116           vTaskDelete(Task2_Handler);
117             printf("任务2被任务1删除\n");
118         }            
119         if(count_num==10)  
120         {
121             printf("任务1被删除\n");
122           vTaskDelete(NULL);  //将自身任务删除
123             
124         }
125         vTaskDelay(1000);  //延时1000个时钟节拍,就是1s   configTICK_RATE_HZ
126     }
127         
128 }
129  
130 void task2_task(void *pvParameters)
131 {  
132     uint8_t count_num1=0;
133    while(1)
134    {
135      count_num1++;
136      printf("任务2执行:%d次\n", count_num1);
137          vTaskDelay(1000);
138   }
139 }

 

执行结果:

技术图片

 

 

 

 

4-FreeRTOS 静态任务的创建/删除

标签:cal   src   bsp   code   cat   count   com   font   函数   

原文地址:https://www.cnblogs.com/darren-pty/p/14160323.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!