第一章 FreeRTOS任务创建

目录

xTaskCreate[任务创建 ]

xTaskCreateStatic

vTaskDelete


详细说明

  • TaskHandle_t

引用任务的类型。例如,对xTaskCreate的调用(通过指针参数)返回TaskHandle_t变量,然后可以将该变量用作vTaskDelete的参数以删除任务。


xTaskCreate[任务创建 ]

 
BaseType_t xTaskCreate( TaskFunction_t pvTaskCode,
                        const char * const pcName,
                        configSTACK_DEPTH_TYPE usStackDepth, 
                        void* pvParameters,
                        UBaseType_t uxPriority,
                        TaskHandle_t * pxCreatedTask );

 

 

创建一个新任务,并将其添加到准备运行的任务列表中。 要使 此RTOS API函数可用,必须在FreeRTOSConfig.h 中将configSUPPORT_DYNAMIC_ALLOCATION设置为1,或将其保留为未定义状态(在这种情况下,它将默认为1)。

每个任务都需要用于保持任务状态的RAM,并由任务用作其堆栈。如果使用xTaskCreate()创建任务,则从FreeRTOS堆中自动分配所需的RAM 。如果使用xTaskCreateStatic()创建任务,则RAM由应用程序编写器提供,因此可以在编译时静态分配。有关 更多信息,请参见 静态与动态分配页面。

如果您使用的是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设置为变量的地址,则在创建的任务执行时该变量必须仍然存在–因此传递堆栈变量的地址无效。

优先级   创建的任务将执行 的优先级。

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

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

xTaskCreateStatic

 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设置为变量的地址,则在创建的任务执行时该变量必须仍然存在–因此传递堆栈变量的地址无效。

优先级   创建的任务将执行的优先级。

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

puxStackBuffer   必须指向至少具有ulStackDepth索引的StackType_t数组(请参见上面的ulStackDepth参数)–该数组将用作任务的堆栈,因此必须是持久的(未在函数的堆栈上声明)。
pxTaskBuffer   必须指向StaticTask_t类型的变量。该变量将用于保存新任务的数据结构(TCB),因此它必须是持久性的(未在函数堆栈上声明)。

返回值:

如果puxStackBuffer或pxTaskBuffer都不为NULL,则将创建任务,并返回任务的句柄。如果puxStackBuffer或pxTaskBuffer为NULL,则不会创建任务,并且将返回NULL。

用法示例:

    /* Dimensions 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 );
    }

​​​​​​​vTaskDelete

void vTaskDelete( TaskHandle_t xTask );

 

必须将INCLUDE_vTaskDelete定义为1才能使用此功能。有关更多信息,请参见RTOS配置文档。

从RTOS内核管理中删除任务。要删除的任务将从所有准备就绪,阻止,暂停和事件列表中删除。

注意:空闲任务负责从已删除的任务中释放RTOS内核分配的内存。因此,重要的是,如果您的应用程序对vTaskDelete()进行了任何调用,请不要使空闲任务耗费微控制器的处理时间。任务代码分配的内存不会自动释放,应在删除任务之前释放。

请参阅演示应用程序文件死亡。c表示使用vTaskDelete()的示例代码。

参数:

任务  要删除的任务的句柄。传递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 );
     }
 }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章