任务创建
[API]
模块
详细描述
TaskHandle_t
task. h
任务引用的类型。例如,调用 xTaskCreate(通过指针参数)返回 TaskHandle_t 变量,然后可以将该变量用作 vTaskDelete 的参数来删除任务。
xTaskCreate
[任务创建]
task. h
创建一个新任务并将其添加到准备运行的任务列表中。 configSUPPORT_DYNAMIC_ALLOCATION 必须在 FreeRTOSConfig.h 中被设置为 1,或保留未定义状态(此时,它默认 默认为 1) ,才能使用此 RTOS API 函数。
每个任务都需要 RAM 来保存任务状态,并由任务用作其 堆栈。 如果使用 xTaskCreate()
创建任务,则所需的 RAM 将自动 从 FreeRTOS 堆中分配。 如果创建任务 使用了 xTaskCreateStatic(),则 RAM 由应用程序编写者提供,因此可以在编译时进行静态分配。 请参阅 静态分配 Vs 动态分配 页面,了解更多信息。
如果您使用的是 FreeRTOS-MPU,则 我们建议您使用 xTaskCreateRestricted() 而不是 xTaskCreate()
。
参数:
pvTaskCode | 指向任务入口函数的指针(即 实现任务的函数名称,请参阅如下示例)。 任务通常 以 无限循环的形式实现;实现任务的函数决不能试图返回 或退出。 但是,任务可以 自我删除。 |
pcName | 任务的描述性名称。主要是为了方便 调试,但也可用于 获取任务句柄。 任务名称的最大长度由 中的 FreeRTOSConfig.hconfigMAX_TASK_NAME_LEN 定义。 |
usStackDepth | 要分配用于 任务堆栈的 字数(不是字节)。例如,如果堆栈的宽度为 16 位,usStackDepth 为 100,则将分配 200 字节用作该任务的堆栈。 再举一例,如果堆栈的宽度为 32 位,usStackDepth 为 400,则将分配 1600 字节用作该任务的堆栈。 堆栈深度与堆栈宽度的乘积不得超过 请参阅常见问题:堆栈应有多大? |
pvParameters | 作为参数传递给创建的任务的一个值。 如果 |
uxPriority | 创建任务执行的优先级 。 包含 MPU 支持的系统可在特权(系统)模式下选择性地创建任务, 方法是在 断言优先级低于 |
pxCreatedTask | 用于将句柄传递至由 xTaskCreate() 函数创建的任务 。 pxCreatedTask 是可选的,可设置为 NULL。 |
Returns:
如果任务创建成功,则返回 pdPASS
。 否则 返回 errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY
。
用法示例:
<span style="color:#202020"><span style="background-color:#ffffff"><span style="background-color:#f3f3f3"><strong><span style="color:green">/* Task to be created. */</span>
void vTaskCode( void * pvParameters )
{
<span style="color:green">/* The parameter value is expected to be 1 as 1 is passed in the
pvParameters value in the call to xTaskCreate() below. </span>
<a data-cke-saved-href="https://www.freertos.org/zh-cn-cmn-s/a00110.html#configASSERT" href="https://www.freertos.org/zh-cn-cmn-s/a00110.html#configASSERT">configASSERT</a>( ( ( uint32_t ) pvParameters ) == 1 );
for( ;; )
{
<span style="color:green">/* Task code goes here. */</span>
}
}
<span style="color:green">/* Function that creates a task. */</span>
void vOtherFunction( void )
{
BaseType_t xReturned;
TaskHandle_t xHandle = NULL;
<span style="color:green">/* Create the task, storing the handle. */</span>
xReturned = xTaskCreate(
vTaskCode, <span style="color:green">/* Function that implements the task. */</span>
"NAME", <span style="color:green">/* Text name for the task. */</span>
STACK_SIZE, <span style="color:green">/* Stack size in words, not bytes. */</span>
( void * ) 1, <span style="color:green">/* Parameter passed into the task. */</span>
tskIDLE_PRIORITY,<span style="color:green">/* Priority at which the task is created. */</span>
&xHandle ); <span style="color:green">/* Used to pass out the created task's handle. */</span>
if( xReturned == pdPASS )
{
<span style="color:green">/* The task was created. Use the task's handle to delete the task. */</span>
<a data-cke-saved-href="https://www.freertos.org/zh-cn-cmn-s/a00126.html" href="https://www.freertos.org/zh-cn-cmn-s/a00126.html">vTaskDelete</a>( xHandle );
}
}</strong></span></span></span>
xTaskCreateStatic
[任务创建]
task. h 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 );
创建新的任务并将其添加到准备运行的任务列表中。 必须在 FreeRTOSConfig.h
中将configSUPPORT_STATIC_ALLOCATION 设置为 1,此 RTOS API 函数才可用。
每个任务都需要 RAM 来保存任务状态,并由任务用作其堆栈。如果使用 xTaskCreate() 创建任务,则会从 FreeRTOS 堆中自动分配所需的 RAM。 如果使用 xTaskCreateStatic()
创建任务,则 RAM 由应用程序编写者提供,这会产生更多的参数,但允许在编译时静态分配 RAM。有关详细信息,请参阅静态分配与动态分配页面。
如果目前使用的是 FreeRTOS-MPU,建议使用 xTaskCreateRestricted(),而不是 xTaskCreateStatic()
。
参数:
pxTaskCode | 指向任务入口函数的指针(即实现任务的函数名称,请参阅如下示例)。 |
pcName | 任务的描述性名称。此参数主要用于方便调试,但也可用于获取任务句柄。 任务名称的最大长度由 FreeRTOSConfig.h 中的 |
ulStackDepth | puxStackBuffer 参数用于将 StackType_t 变量数组传递给 xTaskCreateStatic() 。必须将 ulStackDepth 设置为数组中的索引数。 请参阅常见问题:堆栈应有多大? |
pvParameters | 传递给已创建任务的参数值。 如果将 |
uxPriority | 所创建任务执行的优先级。 包含 MPU支持的系统可选择通过在 断言优先级低于 |
puxStackBuffer | 必须指向至少具有 ulStackDepth 索引的 StackType_t 数组(请参阅上面的 ulStackDepth 参数),该数组用作任务的堆栈,因此必须是永久性的(而不是在函数的堆栈上声明)。 |
pxTaskBuffer | 必须指向 StaticTask_t 类型的变量。该变量用于保存新任务的数据结构体 (TCB) ,因此必须是持久的(而不是在函数的堆栈中声明)。 |
返回:
如果 puxStackBuffer
和 pxTaskBuffer
均不为 NULL,则创建任务,并返回任务的句柄。如果 puxStackBuffer
或 pxTaskBuffer
为 NULL,则不会创建任务,并返回 NULL。
用法示例:
/* Dimensions of the buffer that the task being created will use as its stack.
NOTE: This is the number of words the stack will hold, not the number of
bytes. For example, if each stack item is 32-bits, and this is set to 100,
then 400 bytes (100 * 32-bits) will be allocated. */
#define STACK_SIZE 200
/* Structure that will hold the TCB of the task being created. */
StaticTask_t xTaskBuffer;
/* Buffer that the task being created will use as its stack. Note this is
an array of StackType_t variables. The size of StackType_t is dependent on
the RTOS port. */
StackType_t xStack[ STACK_SIZE ];
/* Function that implements the task being created. */
void vTaskCode( void * pvParameters )
{
/* The parameter value is expected to be 1 as 1 is passed in the
pvParameters value in the call to xTaskCreateStatic(). */
configASSERT( ( uint32_t ) pvParameters == 1UL );
for( ;; )
{
/* Task code goes here. */
}
}
/* Function that creates a task. */
void vOtherFunction( void )
{
TaskHandle_t xHandle = NULL;
/* Create the task without using any dynamic memory allocation. */
xHandle = xTaskCreateStatic(
vTaskCode, /* Function that implements the task. */
"NAME", /* Text name for the task. */
STACK_SIZE, /* Number of indexes in the xStack array. */
( void * ) 1, /* Parameter passed into the task. */
tskIDLE_PRIORITY,/* Priority at which the task is created. */
xStack, /* Array to use as the task's stack. */
&xTaskBuffer ); /* Variable to hold the task's data structure. */
/* puxStackBuffer and pxTaskBuffer were not NULL, so the task will have
been created, and xHandle will be the task's handle. Use the handle
to suspend the task. */
vTaskSuspend( xHandle );
}
xTaskCreateRestrictedStatic
[FreeRTOS-MPU 特定]
task. h
BaseType_t xTaskCreateRestrictedStatic( TaskParameters_t *pxTaskDefinition, TaskHandle_t *pxCreatedTask );
创建一个新的内存保护单元 (MPU) 受限任务并将其添加到准备运行的任务列表中。configSUPPORT_STATIC_ALLOCATION 必须 在 FreeRTOSConfig.h
中设置为 1,以使此 RTOS API 函数可用。
在内部,在 FreeRTOS 实现中,每个任务需要两个内存块。 第一个内存块 用于保存任务的数据结构体。 第二个内存块用作任务的堆栈。 如果 使用 xTaskCreateRestricted() 创建了任务,则任务堆栈的内存由应用程序编写者提供,并且 任务数据结构体所需内存会自动从 FreeRTOS 堆中分配。 如果使用 xTaskCreateRestrictedStatic() 创建了任务,则 应用程序编写者也必须提供任务数据结构体的内存。因此 xTaskCreateRestrictedStatic() 允许 在不使用任何动态内存分配的情况下创建内存保护任务。
xTaskCreateRestrictedStatic() 旨在 与 FreeRTOS-MPU 一起使用, 即演示应用程序,其中包含 有关如何使用相似函数 xTaskCreateRestricted() 的全面和记录示例。
参数:
pxTaskDefinition |
INCLUDE_vTaskDelete 必须定义为 1 才能使用此函数。详情请参阅 RTOS 配置文档。
此函数的作用为从 RTOS 内核管理中移除任务。被删除的任务将从所有的就绪、阻塞、挂起和事件的列表中移除。
请注意,空闲任务负责从已删除任务中释放 RTOS 内核分配的内存。因此,重要的是,如果您的应用程序调用了 vTaskDelete (),空闲任务不会失去微控制器处理时间。任务代码分配的内存不会自动释放,并且应在删除任务之前释放。
请参阅演示应用程序文件 death. c,获取使用 vTaskDelete () 的代码示例。
参数:
xTask | 待删除的任务的句柄。传递 NULL 将导致调用任务被删除。 |
用法示例:
void vOtherFunction( void )
{
TaskHandle_t xHandle = NULL;
// Create the task, storing the handle.
xTaskCreate( vTaskCode, "NAME", STACK_SIZE, NULL, tskIDLE_PRIORITY, &xHandle );
// Use the handle to delete the task.
if( xHandle != NULL )
{
vTaskDelete( xHandle );
}
}