下载 FreeRTOS
 

出色的 RTOS & 嵌入式软件

内核
最新资讯
简化任何设备的身份验证云连接。
利用 CoAP 设计节能型云连接 IoT 解决方案。
11.0.0 版 FreeRTOS 内核简介:
FreeRTOS 路线图和代码贡献流程。
使用 FreeRTOS 实现 OPC-UA over TSN。

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 中的 configMAX_TASK_NAME_LEN 定义。

ulStackDepth puxStackBuffer 参数用于将 StackType_t 变量数组传递给 xTaskCreateStatic()。必须将 ulStackDepth 设置为数组中的索引数。

请参阅常见问题:堆栈应有多大?

pvParameters 传递给已创建任务的参数值。

如果将 pvParameters 设置为变量的地址,则在创建的任务执行时变量必须仍然存在,因此传递堆栈变量的地址无效。

uxPriority 所创建任务执行的优先级

包含 MPU支持的系统可选择通过在 uxPriority 中设置位 portPRIVILEGE_BIT,以特权(系统)模式创建任务。例如,要创建优先级为 2 的特权任务,请将 uxPriority 设置为 (2 | portPRIVILEGE_BIT)。

断言优先级低于 configMAX_priority。如果未定义 configASSERT,则优先级会被静默限制为 (configMAX_PRIORITIES - 1)。

puxStackBuffer 必须指向至少具有 ulStackDepth 索引的 StackType_t 数组(请参阅上面的 ulStackDepth 参数),该数组用作任务的堆栈,因此必须是永久性的(而不是在函数的堆栈上声明)。
pxTaskBuffer 必须指向 StaticTask_t 类型的变量。该变量用于保存新任务的数据结构体 (TCB) ,因此必须是持久的(而不是在函数的堆栈中声明)。
返回:
如果 puxStackBufferpxTaskBuffer 均不为 NULL,则创建任务,并返回任务的句柄。如果 puxStackBufferpxTaskBuffer 为 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 );
}

 

 

 

Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.