FreeRTOS(三)——任务创建与删除

1 任务创建和删除的API函数

函数 描述
xTaskCreate() 使用动态方法创建一个任务
xTaskCreateStaitic() 使用静态方法创建一个任务
xTaskCreateRestricted() 创建一个使用MPU进行限制的任务,相关内存使用动态内存分配
vTaskDelete() 删除一个任务

2 任务创建和删除(动态方法)

API的使用方法以及各个参数均在task.h文件中

/**
 * task. h
 *<pre>
 BaseType_t xTaskCreate(
                              TaskFunction_t pvTaskCode,
                              const char * const pcName,
                              configSTACK_DEPTH_TYPE usStackDepth,
                              void *pvParameters,
                              UBaseType_t uxPriority,
                              TaskHandle_t *pvCreatedTask
                          );</pre>
 *
 * Create a new task and add it to the list of tasks that are ready to run.
 *
 * Internally, within the FreeRTOS implementation, tasks use two blocks of
 * memory.  The first block is used to hold the task's data structures.  The
 * second block is used by the task as its stack.  If a task is created using
 * xTaskCreate() then both blocks of memory are automatically dynamically
 * allocated inside the xTaskCreate() function.  (see
 * http://www.freertos.org/a00111.html).  If a task is created using
 * xTaskCreateStatic() then the application writer must provide the required
 * memory.  xTaskCreateStatic() therefore allows a task to be created without
 * using any dynamic memory allocation.
 *
 * See xTaskCreateStatic() for a version that does not use any dynamic memory
 * allocation.
 *
 * xTaskCreate() can only be used to create a task that has unrestricted
 * access to the entire microcontroller memory map.  Systems that include MPU
 * support can alternatively create an MPU constrained task using
 * xTaskCreateRestricted().
 *
 * @param pvTaskCode Pointer to the task entry function.  Tasks
 * must be implemented to never return (i.e. continuous loop).
 *
 * @param pcName A descriptive name for the task.  This is mainly used to
 * facilitate debugging.  Max length defined by configMAX_TASK_NAME_LEN - default
 * is 16.
 *
 * @param usStackDepth The size of the task stack specified as the number of
 * variables the stack can hold - not the number of bytes.  For example, if
 * the stack is 16 bits wide and usStackDepth is defined as 100, 200 bytes
 * will be allocated for stack storage.
 *
 * @param pvParameters Pointer that will be used as the parameter for the task
 * being created.
 *
 * @param uxPriority The priority at which the task should run.  Systems that
 * include MPU support can optionally create tasks in a privileged (system)
 * mode by setting bit portPRIVILEGE_BIT of the priority parameter.  For
 * example, to create a privileged task at priority 2 the uxPriority parameter
 * should be set to ( 2 | portPRIVILEGE_BIT ).
 *
 * @param pvCreatedTask Used to pass back a handle by which the created task
 * can be referenced.
 *
 * @return pdPASS if the task was successfully created and added to a ready
 * list, otherwise an error code defined in the file projdefs.h
 *
 * Example usage:
   <pre>
 // Task to be created.
 void vTaskCode( void * pvParameters )
 {
     for( ;; )
     {
         // Task code goes here.
     }
 }

 // Function that creates a task.
 void vOtherFunction( void )
 {
 static uint8_t ucParameterToPass;
 TaskHandle_t xHandle = NULL;

     // Create the task, storing the handle.  Note that the passed parameter ucParameterToPass
     // must exist for the lifetime of the task, so in this case is declared static.  If it was just an
     // an automatic stack variable it might no longer exist, or at least have been corrupted, by the time
     // the new task attempts to access it.
     xTaskCreate( vTaskCode, "NAME", STACK_SIZE, &ucParameterToPass, tskIDLE_PRIORITY, &xHandle );
     configASSERT( xHandle );

     // Use the handle to delete the task.
     if( xHandle != NULL )
     {
         vTaskDelete( xHandle );
     }
 }
   </pre>
 * \defgroup xTaskCreate xTaskCreate
 * \ingroup Tasks
 */

xTaskCreate()

创建一个新任务并将其添加到准备运行的任务列表中。

在FreeRTOS实施内部,任务使用两块内存。第一个块用于保存任务的数据结构。第二块被任务用作堆栈。如果任务是使用创建的 xTaskCreate()则这两个内存块都会自动动态调整 在xTaskCreate()函数内分配。如果任务是使用创建的
xTaskCreateStatic()然后应用程序编写者必须提供所需的内存。 xTaskCreateStatic()因此允许创建一个任务 使用任何动态内存分配。 对于不使用任何动态内存的版本,请参阅xTaskCreateStatic() 分配。
xTaskCreate()只能用于创建不受限制的任务访问整个单片机内存映射。包含MPU的系统支持也可以使用创建MPU约束任务 xTaskCreateRestricted()。
MPU:Memory Protection Unit (内存保护单元)

参数 描述
pvTaskCode: 任务函数
const pcName: 任务名字,一般用于追踪与调试,长度不能超过configMAX_TASK_STACK_SIZE
usStackDepth: 任务堆栈大小,注意实际申请到的堆栈是usStackDepth的4倍,其中空闲任务的堆栈大小为configMINMAL_STACK_SIZE
pvParameters: 传递给任务函数参数
uxPriority: 任务优先级,范围 0 ~ configMAX_PRIORITIES-1
pvCreatedTask: 任务句柄,任务创建成功后会返回此任务的句柄,这个句柄其实就是任务的任务堆栈,此参数就用来保存这个任务句柄,其他API可能会使用这个句柄
返回值 描述
pdPASS: 任务创建成功
errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY: 任务创建失败,堆内存不足

vTaskDelete()

删除一个用函数xTaskCreate()xTaskCreateStatic创建的任务,被删除了的任务不再存在,也就是说再不会进入运行态。任务被删除后就不能再使用该任务的句柄!如果此任务是使用动态方法创建的,也就是使用xTaskCreate()创建的,那么在此任务被删除以后此任务之前申请的堆栈和控制块和控制内存会在空闲任务中被释放掉,因此当调用函数vTaskDelete()删除任务以后必须给空闲任务一定的运行时间。
只有那些由内核分配给任务的内存才会在任务被删除以后自动的释放掉,用户分配给任务的内存需要用户自行释放掉,比如某个任务中用户调用函数pvPortMalloc()分配了500字节的内存,那么在此任务被删除以后用户也必须调用函数vPortFree()将这500字节的内存释放掉,否则会导致内存泄露。此函数原型如下:
vTaskDelete(TaskHandle_t xTaskDelete)

参数 描述
xTaskDelete: 要删除的任务的任务句柄
返回值 描述
无:

注意:在某个具体的任务中,如使用vTaskDelete(NULL);则代表删除该任务。

猜你喜欢

转载自blog.csdn.net/qq_30650153/article/details/80882147