xTaskCreate
[任务创建]
task. h
BaseType_t xTaskCreate( TaskFunction_t pvTaskCode,
const char * const pcName,
configSTACK_DEPTH_TYPE usStackDepth,
void *pvParameters,
UBaseType_t uxPriority,
TaskHandle_t *pxCreatedTask
);
创建一个新任务并将其添加到准备运行的任务列表中。
configSUPPORT_DYNAMIC_ALLOCATION
必须在 FreeRTOSConfig.h 中被设置为 1,或保留未定义状态(此时,它默认
默认为 1) ,才能使用此 RTOS API 函数。
每个任务都需要 RAM 来保存任务状态,并由任务用作其
堆栈。 如果使用 xTaskCreate() 创建任务,则所需的 RAM 将自动
从 FreeRTOS 堆中分配。 如果创建任务
使用了 xTaskCreateStatic(),则 RAM
由应用程序编写者提供,因此可以在编译时进行静态分配。 请参阅
静态分配 Vs 动态分配
页面,了解更多信息。
如果您使用的是 FreeRTOS-MPU,则
我们建议您使用 xTaskCreateRestricted()
而不是 xTaskCreate() 。
- 参数:
-
pvTaskCode
|
指向任务入口函数的指针(即
实现任务的函数名称,请参阅如下示例)。
任务通常
以
无限循环的形式实现;实现任务的函数决不能试图返回
或退出。 但是,任务可以
自我删除。
|
pcName
|
任务的描述性名称。主要是为了方便
调试,但也可用于
获取任务句柄。
任务名称的最大长度由
FreeRTOSConfig.h 中的 configMAX_TASK_NAME_LEN 定义。
|
usStackDepth
|
要分配用于
任务堆栈的
字数(不是字节)。例如,如果堆栈的宽度为 16 位,usStackDepth 为
100,则将分配 200 字节用作该任务的堆栈。
再举一例,如果堆栈的宽度为 32 位,usStackDepth 为
400,则将分配 1600 字节用作该任务的堆栈。
堆栈深度与堆栈宽度的乘积不得超过
size_t 类型变量所能包含的最大值。
请参阅常见问题:堆栈应有多大?
|
pvParameters
|
作为参数传递给创建的任务的一个值。
如果 pvParameters 设置为变量的地址,
则在执行创建的任务时该变量必须仍然存在——因此
传递堆栈变量的地址是无效的。
|
uxPriority
|
创建任务执行的优先级
。
包含 MPU 支持的系统可在特权(系统)模式下选择性地创建任务,
方法是在 uxPriority 中设置 portPRIVILEGE_BIT 位。
例如,要创建一个优先级为 2 的特权任务,可将 uxPriority 设置为
( 2 | portPRIVILEGE_BIT )。
断言优先级低于 configMAX_priority 。
如果未定义 configASSERT ,则优先级会被静默限制为 ( configMAX_priority - 1)。
|
pxCreatedTask
|
用于将句柄传递至由 xTaskCreate() 函数创建的任务
。 pxCreatedTask 是可选的,可设置为 NULL。
|
-
返回:
-
如果任务创建成功,则返回
pdPASS 。 否则
返回 errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY 。
用法示例:
/* Task to be 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 xTaskCreate() below.
configASSERT( ( ( uint32_t ) pvParameters ) == 1 );
for( ;; )
{
/* Task code goes here. */
}
}
/* Function that creates a task. */
void vOtherFunction( void )
{
BaseType_t xReturned;
TaskHandle_t xHandle = NULL;
/* Create the task, storing the handle. */
xReturned = xTaskCreate(
vTaskCode, /* Function that implements the task. */
"NAME", /* Text name for the task. */
STACK_SIZE, /* Stack size in words, not bytes. */
( void * ) 1, /* Parameter passed into the task. */
tskIDLE_PRIORITY,/* Priority at which the task is created. */
&xHandle ); /* Used to pass out the created task's handle. */
if( xReturned == pdPASS )
{
/* The task was created. Use the task's handle to delete the task. */
vTaskDelete( xHandle );
}
}
Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.
|