Preface
In the article on FreeRTOS porting to Cortex-M3 hardware platform, we have seen task creation API, but the focus of that article is on how to port FreeRTOS. This article will focus on the creation and deletion of API functions of tasks.
Task creation and deletion API functions are located in the file and need to include header files.
1. Task creation
1.1 Function description
BaseType_t xTaskCreate( TaskFunction_t pvTaskCode, const char * const pcName, unsigned short usStackDepth, void *pvParameters, UBaseType_t uxPriority, TaskHandle_t * pvCreatedTask );
Create a new task and add it to the task-ready list.
If you use FreeRTOS-MPU (in the official download package, two porting solutions were written for the Cortex-M3 kernel, one is the ordinary FreeRTOS port layer, and the other is the FreeRTOS-MPU port layer. The latter contains complete memory protection), it is recommended to use the function xTaskCreateRestricted() instead of xTaskCreate(). In the case of FreeRTOS-MPU, using the xTaskCreate() function can create tasks running in privileged mode or user mode (see the description of the function parameter uxPriority below). When running in privileged mode, the task can access the entire memory map; when in user mode, the task can only access its own stack. In any mode, the MPU will not automatically capture stack overflow, so the standard FreeRTOS stack overflow detection mechanism will still be used. The xTaskCreateRestricted() function has greater flexibility.
1.2 Parameter Description
pvTaskCode
: Pointer, pointing to the entry of the task function. The task will never return (located in the dead loop). The parameter type TaskFunction_t is defined in the file and is defined as: typedefvoid (*TaskFunction_t)( void * ).
pcName
: Task description. Mainly used for debugging. The maximum length of a string is specified by the macro configMAX_TASK_NAME_LEN, which is located in the file.
usStackDepth
: Specify the task stack size, the number of stack variables that can be supported, rather than the number of bytes. For example, if the usStackDepth is defined as 100 on a 16-bit width stack, then 200 bytes of stack storage space is actually used. The width of the stack multiplied by the depth must not exceed the maximum value that the size_t type can represent. For example, if size_t is 16 bits, the maximum value that can be represented is 65535.
pvParameters
: Pointer, passed as a parameter to the task when the task is created.
uxPriority
: Priority of tasks. Systems with MPU support can create tasks in privileged (system) mode at will by asserting the portPRIVILEGE_BIT bit of the priority parameter. For example, create a privileged task with priority 2, and the parameter uxPriority can be set to (2 | portPRIVILEGE_BIT ).
pvCreatedTask
: Used to return a handle (ID). After creating a task, you can use this handle to refer to the task.
1.3 Return value
&&If the task is successfully created and added to the ready list function returns pdPASS, otherwise the function returns an error code, see for details.
1.4 Examples of usage
/* Create a task. */ void vTaskCode( void * pvParameters ) { for( ;; ) { /* Task code is placed here */ } } /* Create task function */ void vOtherFunction( void ) { static unsigned char ucParameterToPass; xTaskHandlexHandle; /* Create a task and store the handle. Note: The passed parameter ucParameterToPass must have the same survival cycle as the task. Therefore, it is defined here as a static variable. If it's just an automatic variable, it may not have a long lifetime because It may be used with interrupts and high-priority tasks. */ xTaskCreate( vTaskCode, "NAME", STACK_SIZE,&ucParameterToPass, tskIDLE_PRIORITY, &xHandle ); /* Use handle to delete the task. */ if( xHandle !=NULL ) { vTaskDelete( xHandle ); } }
2. Task deletion
2.1 Task Description
voidvTaskDelete( TaskHandle_t xTask );
Remove a task from the RTOS kernel manager. Tasks are removed from the Ready, Blocking, Pause, and Event lists. In the file, the macro INCLUDE_vTaskDelete must be defined as 1 for this function to be valid.
Note: The deleted task, the storage space allocated by the kernel when the task is created, will be released by the idle task. If an application calls xTaskDelete(), it is necessary to ensure that the idle task obtains a certain microcontroller processing time. The memory allocated by the task code itself will not be automatically released, so these memory should be released before deleting the task.
2.2 Parameter description
xTask: The handle to the deleted task. NULL means deleting the current task.
The above is the detailed content of task creation and deletion of FreeRTOS real-time operating system. For more information about FreeRTOS task creation and deletion, please follow my other related articles!