FreeRTOS create task

1, FreeRTOS task related API functions

1. Task create and delete API functions
The most basic function of FreeRTOS is task management, and the most basic operation of task management is to create and delete tasks. The task creation and deletion API functions of FreeRTOS are as follows:

functiondescribe
xTaskCreate()Create a task using a dynamic method
xTaskCreateStatic()Create a task using a static method
xTaskCrearteRestricted()Create a task that uses MPU for restriction, and the related memory uses dynamic memory allocation
vTaskDelete()Delete a task

1. Function xTaskCreate()
This function is used to create a task. The task needs ram to save the status information related to the task (task control block), and the task also needs a certain amount of RAM as the task stack. If you use the function xTaskCreate() to create a task, the required RAM will be automatically allocated from the FreeRTOS heap. Therefore, you must provide a memory management file, which we use by default
heap_4.c this memory management file,
And the macro configSUPPORT_DYNAMIC_ALLOCATION must be 1. as
If it is created using the function xTaskCreateStatic(), the RAM needs to be provided by the user. The newly created task is in ready status by default. If no task with higher priority is running, the task will immediately enter the running status and start running. You can create a task before or after the task scheduler is started. The function prototype is as follows:

BaseType_t xTaskCreate(	TaskFunction_t pxTaskCode,
						const char * const pcName,
						const uint16_t usStackDepth,
						void * const pvParameters,
						UBaseType_t uxPriority,
						TaskHandle_t * const pxCreatedTask ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */

Parameters:

parameterdescribe
pxTaskCodeTask function
pcNameTask name is generally used for tracking and debugging. The length of task name cannot exceed configMAX_TASK_NAME_LEN
usStackDepthThe task stack size. Note that the stack actually applied is 4 times that of ussackdepth. The task stack size of idle tasks is configMINIMAL_STACK_SIZE
pvParametersParameters passed to the task function
uxPriotiryTask priority, range 0~configMAX_PRIORITIES-1
pxCreatedTaskTask handle. After the task is created successfully, the task handle of the task will be returned. In fact, this handle is the task stack of the task. This parameter is used to save the task handle. Other API functions may use this handle
BaseType_tReturn value pdPASS: creation succeeded, others failed

2. Function xtask createstatic()
This function has the same function as xTaskCreate() and is also used to create tasks, but the RAM required for tasks created with this function needs to be provided by the user. If you want to use this function, you need to set the macro
configSUPPORT STATIC ALLOCATION is defined as 1. The function prototype is as follows:

TaskHandle_t xTaskCreateStatic(	TaskFunction_t pxTaskCode,
									const char * const pcName,
									const uint32_t ulStackDepth,
									void * const pvParameters,
									UBaseType_t uxPriority,
									StackType_t * const puxStackBuffer,
									StaticTask_t * const pxTaskBuffer ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */

Parameters:

parameterdescribe
pxTaskCodeTask function
pcNameTask name is generally used for tracking and debugging. The length of task name cannot exceed configMAX_TASK_NAME_LEN
usStackDepthThe task stack size. Note that the stack actually applied is 4 times that of ussackdepth. The task stack size of idle tasks is configMINIMAL_STACK_SIZE
pvParametersParameters passed to the task function
uxPriotiryTask priority, range 0~configMAX_PRIORITIES-1
puxStackBufferThe task stack is generally an array, and the array type should be defined as StackType_t type
pxTaskBufferTask control block
TaskHandle_tReturn value. NULL indicates that the task creation failed. Other values indicate that the task creation succeeded. Return the task handle of the task

3. Function xTaskCreateRestricted()
This function is also used to create tasks, but the MCU used in this function requires MPU (memory protection unit), and the tasks created with this function will be protected by MPU. Other functions are the same as the function xTaxkCreate(). Function prototype:

BaseType_t xTaskCreateRestricted( const TaskParameters_t * const pxTaskDefinition, TaskHandle_t *pxCreatedTask )
parameterdescribe
pxTaskDefinitionPoint to a structure TaskParameters_t, which describes the task function, stack size, priority, etc. of the task, which is defined in the task.h file
pxCreatedTasktask handle
BaseType_tReturn value: pdPASS: task creation succeeded, other values: task creation failed

4. Function vTaskDelete()
Delete a task created with the function xTaskCreate() or xTaskCreateStatic(). The deleted task no longer exists, that is, it will never enter the running state. After the task is deleted, the handle of the task can no longer be used! If the task is created with a dynamic method, that is, use the function xTaskCreate() After the task is deleted, the stack and control block memory applied before the task will be released in the idle task. Therefore, when calling the function vtask delete() After deleting a task, the idle task must be given a certain running time. Only the memory allocated to the task by the kernel will be automatically released after the task is deleted. The memory allocated to the task by the user needs to be released by the user, such as the user call function pvPortMalloc() in a task If 500 bytes of memory are allocated, after this task is deleted, the user must also call the function vPortFree() to release the 500 bytes of memory, otherwise memory leakage will occur. The prototype of this function is as follows:

vTaskDelete(TaskHandle_t xTaskToDelete)
parameterdescribe
xTaskToDeleteThe task handle of the task to delete

2, Code presentation of task creation and deletion

#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "FreeRTOS.h"
#include "task.h"
#include "lcd.h"


//Task priority
#define START_TASK_PRIO		1
//Task stack size	
#define START_STK_SIZE 		128  
//task handle 
TaskHandle_t StartTask_Handler;
//Task function
void start_task(void *pvParameters);

//Task priority
#define LED_TASK_PRIO		2
//Task stack size	
#define LED_STK_SIZE 		128  
//task handle 
TaskHandle_t LEDTask_Handler;
//Task function
void led_task(void *pvParameters);

//Task priority
#define LCD_TASK_PRIO		3
//Task stack size	
#define LCD_STK_SIZE 		128  
//task handle 
TaskHandle_t LCDTask_Handler;
//Task function
void lcd_task(void *pvParameters);

//Color used for LCD screen brushing
int lcd_discolor[14] = {  WHITE,BLACK,BLUE,BRED,
													GRED,GBLUE,RED,MAGENTA,
													GREEN,CYAN,YELLOW,BROWN,
													BRRED,GRAY };

int main(void)
{ 
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);//Set system interrupt priority group 4
	delay_init(168);		//Initialization delay function
	uart_init(115200);     	//Initialize serial port
	LED_Init();		        //Initialize LED port
	LCD_Init();
	POINT_COLOR = RED;
	LCD_ShowString(30,10,200,16,16,"LIU YI NIU BI");
	LCD_ShowString(30,40,200,16,16,"2021/10/18");
	//Create start task
    xTaskCreate((TaskFunction_t )start_task,            //Task function
                (const char*    )"start_task",          //Task name
                (uint16_t       )START_STK_SIZE,        //Task stack size
                (void*          )NULL,                  //Parameters passed to the task function
                (UBaseType_t    )START_TASK_PRIO,       //Task priority
                (TaskHandle_t*  )&StartTask_Handler);   //task handle               
    vTaskStartScheduler();          //Turn on task scheduling
}
 
//Start task function
void start_task(void *pvParameters)
{
    taskENTER_CRITICAL();           //Enter critical zone
    //Create LED0 task
    xTaskCreate((TaskFunction_t )led_task,     	
                (const char*    )"led_task",   	
                (uint16_t       )LED_STK_SIZE, 
                (void*          )NULL,				
                (UBaseType_t    )LED_TASK_PRIO,	
                (TaskHandle_t*  )&LEDTask_Handler);   
    //Create LED1 task
    xTaskCreate((TaskFunction_t )lcd_task,     
                (const char*    )"lcd_task",   
                (uint16_t       )LCD_STK_SIZE, 
                (void*          )NULL,
                (UBaseType_t    )LCD_TASK_PRIO,
                (TaskHandle_t*  )&LCDTask_Handler);        
    vTaskDelete(StartTask_Handler); //Delete start task
    taskEXIT_CRITICAL();            //Exit critical zone
}

//LED task function 
void led_task(void *pvParameters)
{
    u8 led_num = 0;
		POINT_COLOR = BLACK;
		LCD_DrawRectangle(5,110,115,314);
		LCD_DrawLine(5,130,115,130);
		POINT_COLOR=BLUE;
		LCD_ShowString(6,111,110,16,16,"led run:000");
		while(1)
    {
				led_num++;
        LED0=~LED0;
				LCD_Fill(6,131,114,313,lcd_discolor[led_num % 14]);
				LCD_ShowxNum(86,111,led_num,3,16,0x80);
        vTaskDelay(1000);
    }
}   

//LCD task function
void lcd_task(void *pvParameters)
{
		u8 lcd_num = 0;
		POINT_COLOR=BLACK;
		LCD_DrawRectangle(125,110,234,314);
		LCD_DrawLine(125,130,234,130);
		POINT_COLOR= BLUE;
		LCD_ShowString(126,111,110,16,16,"lcd run:000");
    while(1)
    {
        lcd_num++;
				LED1=!LED1;
//        vTaskDelay(200);
				LCD_ShowxNum(206,111,lcd_num,3,16,0x80);
				LCD_Fill(126,131,233,313,lcd_discolor[13-lcd_num%14]);
        vTaskDelay(1000);
    }
}

That's all for the FreeRTOS creation task!!! Thank you for visiting!!! I hope you like it more!!! Thank you!!!

Keywords: Embedded system FreeRTOS

Added by Fatboy on Mon, 18 Oct 2021 22:03:32 +0300