Embedded FreeRTOS learning four, the state machine of FreeRTOS tasks

3. The state machine of the task

Suspended: suspended state
Running: Running state
Blocked: blocking state/waiting state
Ready: Ready state
The official figure above does not list the situation where the task is interrupted. If the interrupted task returns from the interruption, the interrupted task will be returned. When the next clock tick is reached, CPU resources are given to the task with the highest priority among the ready tasks

3.1 Sample code

3.1.1 Same priority
code example
int main(void) 
{

/* 创建app_task1任务 */
 xTaskCreate(
 (TaskFunction_t )app_task1,         /* 任务入口函数 */
 (const char* )"app_task1",          /* 任务名字 */
 (uint16_t )512,                     /* 任务栈大小 */
 (void* )NULL,                       /* 任务入口函数参数 */
 (UBaseType_t )5,                    /* 任务的优先级 */
 (TaskHandle_t* )&app_task1_handle); /* 任务控制块指针 */

 /* 创建app_task2任务 */ 
 xTaskCreate(
 (TaskFunction_t )app_task2,         /* 任务入口函数 */
 (const char* )"app_task2",          /* 任务名字 */
 (uint16_t )512,                     /* 任务栈大小 */
 (void* )NULL,                       /* 任务入口函数参数 */
 (UBaseType_t )5,                    /* 任务的优先级 */
 (TaskHandle_t* )&app_task2_handle); /* 任务控制块指针 */

 /* 开启任务调度 */
 vTaskStartScheduler();
 
 while(1);

 }
 static void app_task1(void* pvParameters)
 {
        for(;;)
          {
               printf("app_task1 is running ...\r\n");
               vTaskDelay(20);
          }
 } 

static void app_task2(void* pvParameters)
 {
        for(;;)
         {
                printf("app_task2 is running ...\r\n");
                vTaskDelay(30);
         }
 }

operation result: 

 3.1.1 Different priorities

code example

int main(void)  
{  /* 创建app_task1任务 */
   xTaskCreate((TaskFunction_t )app_task1, /* 任务入口函数 */
  (const char* )"app_task1",               /* 任务名字 */
  (uint16_t )512,                          /* 任务栈大小 */
  (void* )NULL,                            /* 任务入口函数参数 */
  (UBaseType_t )6,                         /* 任务的优先级 */
  (TaskHandle_t* )&app_task1_handle);      /* 任务控制块指针 */

 /* 创建app_task2任务 */ 
 xTaskCreate((TaskFunction_t )app_task2,   /* 任务入口函数 */
 (const char* )"app_task2",                /* 任务名字 */
 (uint16_t )512,                           /* 任务栈大小 */
 (void* )NULL,                             /* 任务入口函数参数 */
 (UBaseType_t )5,                          /* 任务的优先级 */
 (TaskHandle_t* )&app_task2_handle);       /* 任务控制块指针 */
 /* 开启任务调度 */
 vTaskStartScheduler();
 
 while(1);

}
 static void app_task1(void* pvParameters)
 {
         for(;;)
           {
              printf("app_task1 is running ...\r\n");
              vTaskDelay(20);
           }
 } 

 static void app_task2(void* pvParameters)
 {
        for(;;)
          {
             printf("app_task2 is running ...\r\n");
             vTaskDelay(30);
          }
 }

operation result: 

 

 

Guess you like

Origin blog.csdn.net/weixin_44651073/article/details/127223755