(一)FreeRTOS任务创建及删除

目录

        1.任务创建xTaskCreate

        2.任务创建xTaskCreateStatic

        3.任务删除vTaskDelete


1.任务创建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 由应用程序编写者提供,因此可以在编译时进行静态分配。

参数:

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 );
    }
}

2.任务创建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。

参数:

pxTaskCode指向任务入口函数的指针(即实现任务的函数名称,请参阅如下示例)。

任务通常以无限循环的形式实现;实现任务的函数决不能尝试返回或退出。但是,任务可以自行删除

pcName任务的描述性名称。此参数主要用于方便调试,但也可用于获取任务句柄

任务名称的最大长度由 FreeRTOSConfig.h 中的 configMAX_TASK_NAME_LEN 定义。

ulStackDepthpuxStackBuffer 参数用于将 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) ,因此必须是持久的(而不是在函数的堆栈中声明)。

返回:

如果 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 );
    }

3.任务删除vTaskDelete

//task. h

void vTaskDelete( TaskHandle_t xTask );

        INCLUDE_vTaskDelete 必须定义为 1 才能使用此函数。此函数的作用为从 RTOS 内核管理中移除任务。被删除的任务将从所有的就绪、阻塞、挂起和事件的列表中移除。

        请注意,空闲任务负责从已删除任务中释放 RTOS 内核分配的内存。因此,重要的是,如果您的应用程序调用了 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 );
     }
 }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值