Create your first FreeRTOS task

1. Preamble

In fact, FreeRTOS is not as difficult as everyone thinks, and it is actually quite easy to get started following the blogger's ideas. The previous blogs have taught you how to build FreeRTOS. This article is to teach you actual combat and create your first mission. The so-called practice is the only sign of testing theory. Let's get started then.

2. Create a task

2.1 handle

What is a handle, and what is the function of the handle? My understanding is that before creating a task, we need to define a handle, and then when we create a task, we bind the task to the handle. In the subsequent sequence, we can operate the task by manipulating this handle (such as deleting a task, we only need to delete the handle). The handle is defined as follows:

TaskHandle_t Demo1_Task_Handle=NULL;

2.2 Create task function

The task creation function is as follows. There are 5 parameters here, which are task function, task name, task stack size, task priority, and task handle.

xTaskCreate((TaskFunction_t)Demo1_Task, //任务函数
                        (const char *)"Demo1TaskCreate", //任务名
                        (uint16_t)128,(void *)NULL,  //任务栈大小
                        (UBaseType_t)1,                //任务优先级
                        (TaskHandle_t *)&Demo2_Task_Handle); //任务句柄
                        

3. Code example

Here's a simple code example by creating two tasks. One task is to switch flag back and forth between 1 and 0, and one task is to switch flag2 back and forth between 0 and 1. Here is also an explanation of the delay of the vTaskDelay() function compared to bare metal. vTaskDelay() in FreeRTOS will switch tasks during the delay process, and switch to the current task after the delay is over. So although the Demo1_Task task has a lower priority, it can still be executed.
Note : Since I did not redirect the printf function here, there will be many warnings about printf, but it will not affect the experimental results.

#include "stm32f10x.h" 
#include "FreeRTOS.h"
#include "task.h"


static TaskHandle_t Demo2_Task_Handle=NULL;
static TaskHandle_t Demo1_Task_Handle=NULL;

static void Demo2_Task(void * pvParameters);
static void Demo1_Task(void * pvParameters);

//static void BSP_init(void );

BaseType_t flag=1;
BaseType_t flag2=1;

int main(void)
{
    
    
    BaseType_t xReturn =pdPASS; //创建任务返回值
    
    xReturn=xTaskCreate((TaskFunction_t)Demo2_Task, //任务函数
                        (const char *)"Demo2TaskCreate", //任务名
                        (uint16_t)128,(void *)NULL,  //任务栈大小
                        (UBaseType_t)1,                //任务优先级
                        (TaskHandle_t *)&Demo1_Task_Handle); //任务控制块指针
    
    xReturn=xTaskCreate((TaskFunction_t)Demo1_Task, //任务函数
                        (const char *)"Demo1TaskCreate", //任务名
                        (uint16_t)128,(void *)NULL,  //任务栈大小
                        (UBaseType_t)1,                //任务优先级
                        (TaskHandle_t *)&Demo2_Task_Handle); //任务控制块指针
                        
     
     if(xReturn==pdPASS) //如果创建成功
     {
    
         
       vTaskStartScheduler(); //启动任务,开启调度
     
     }
     else return -1;
     while(1);
        
}

static void Demo2_Task(void *parameter)
{
    
    
  while(1)
  {
    
    

   flag2=0;
   vTaskDelay(500);
   flag2=1;
   vTaskDelay(500); 
  
  }

}

static void Demo1_Task(void * parameter)
{
    
    


 while(1)
 {
    
    

   flag=1;
   vTaskDelay(500);   
   flag=0;   
   vTaskDelay(500);

 }

}

4. Experimental phenomenon

How do we observe the experimental phenomenon? In fact, there are two methods, one is to print through the printf serial port (you need to add the serial port initialization code yourself), and the other is to observe the values ​​​​of the flag and flag2 variables through software simulation. Here I choose software simulation (you can read my article: This article teaches you how to learn keil software simulation ). The experimental phenomenon is that flag and flag2 change between 0 and 1 at the same time.

FreeRTOS experimental phenomenon

Guess you like

Origin blog.csdn.net/qq_62553914/article/details/131330060