In the article FreeRTOS ported to the CORTEX-M3 hardware platform, we've seen the task creation API, but the article focuses on porting the FreeRTOS, which focuses on the task creation and deletion API functions.
Task creation and deletion API functions are located in file task.c and need to contain task.h header files.
1. Task Creation1.1 Function Description
Basetype_txtaskcreate ( taskfunction_tpvtaskcode, const char * constpcname, unsigned shortusstackdepth, void *pvparameters, Ubasetype_tuxpriority, taskhandle_t*pvcreatedtask );
Create a new task and join the task-ready list.
If you use Freertos-mpu (in the official download package, write two porting protocols for the CORTEX-M3 kernel, one for the normal FreeRTOS transplant layer, and one for the freertos-mpu transplant layer. The latter contains complete memory protection), it is recommended to use the function xtaskcreaterestricted () instead of xtaskcreate (). With Freertos-mpu, you can use the Xtaskcreate () function to create a task that runs in either privileged mode or user mode (see below for a description of the function parameter uxpriority). When running in privileged mode, the task can access the entire memory map, and when in user mode, the task can only access its own stack. The standard FreeRTOS stack overflow detection mechanism is still used, regardless of the mode in which the MPU does not automatically capture stack overflows. The xtaskcreaterestricted () function has greater flexibility.
1.2 Parameter Description
- pvtaskcode: pointer, which points to the entry of the task function. The task never returns (in the Dead Loop). The parameter type taskfunction_t defined in the file projdefs.h typedefvoid (*taskfunction_ T) (void *)
- Pcname : Task description. Mainly used for debugging. The maximum length of a string is specified by macro Configmax_task_name_len , which is located in the FreeRTOSConfig.h file.
- usstackdepth: Specifies the task stack size, the number of stack variables that can be supported, and is not a number of bytes . For example, under a 16-bit-width stack, usstackdepth is defined as 100, which actually uses 200-byte stack storage space. 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 the size_t is 16 bits, the maximum value that can be represented is 65535.
- Pvparameters: pointer, which is passed to the task as a parameter when the task is created.
- uxpriority: The priority of the task. HaveMPUsupported systems, which can be set by the set priority parameterPortprivilege_bitbits, arbitrarily creating tasks in privileged (System) mode. For example, create a priority class that has a2the privileged tasks, parametersuxprioritycan be set to(2 | portprivilege_bit).
- Pvcreatedtask: used to callback a handle ( ID ), you can use this handle to reference tasks after you create the task.
1.3 return value
If the task is successfully created and the Join Ready list function returns Pdpass , or the function returns an error code, see Projdefs.h .
1.4 Usage Examples
/* Create a task. */void vtaskcode (void * pvparameters) {for (;;) { /* The task code is put here * /}}/* Create Task function */void votherfunction (void) { staticunsigned char ucparametertopass; Xtaskhandlexhandle; /* Create a task to store the handle. Note: The passed parameter ucparametertopass must have the same life cycle as the task, so this is defined as a static variable. If it is just an automatic variable, it may not have a long life cycle, because interrupts and high-priority tasks may be used. * /Xtaskcreate (Vtaskcode, "NAME", Stack_size,&ucparametertopass, Tskidle_priority, &xhandle); /* Use the handle to delete the task. * /if (Xhandle!=null) { vtaskdelete (xhandle); }}
2. Task Deletion2.1 Task Description
Voidvtaskdelete (taskhandle_t xtask);
Removes a task from the RTOs kernel manager. When a task is deleted, it is removed from the ready, blocked, paused, and list of events. In file FreeRTOSConfig.h, the macro include_vtaskdelete must be defined as 1 and this function is valid.
Note: The deleted task, which is allocated by the kernel when the task is created, is freed by the idle task. If an application calls Xtaskdelete (), the idle task must be guaranteed to obtain a certain amount of microcontroller processing time. The memory allocated by the task code itself is not automatically freed, so the memory should be freed before the task is deleted.
2.2 Parameter Description
- Xtask: Handle to the deleted task. Null indicates that the current task is deleted.
FreeRTOS Series 10th---freertos Task creation and deletion