FreeRTOSyuki xTaskCreate ()

xTaskCreate () function analysis

task. h

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

to sum up

Create a new task instance.
Each task requires RAM to save the task state (task control block, or TCB), and is used by the task as its stack. If the task was created using xTaskCreate (), you need to automatically allocate RAM from the FreeRTOS heap. If you use xTaskCreateStatic () to create a task, the application writer provides RAM, which results in two additional function parameters, but allows static RAM allocation at compile time.
The newly created task is initially in a ready state, but if no higher priority task can run, it will immediately become a running task.
The task can be created before and after starting the scheduler.

parameter

parameter Features
pvTaskCode Task function
pcName Task name
usStackDepth Task stack size
pvParameters Task parameter
uxPriority Task priority
pxCreatedTask Task handle

return value

return value significance
pdPass Indicates that the task has been successfully created.
errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY Indicates that the task cannot be created because FreeRTOS does not have enough heap memory to allocate the task data structure and stack. If the project contains heap_1.c, heap_2.c or heap_4.c, configTOTAL_HEAP_SIZE in FreeRTOSConfig defines the total amount of available heap. Use the vApplicationMallocFailedHook () callback function (or "hook") to catch the failure to allocate memory, and use the xPortGetFreeHeapSize () API function to query the remaining amount of free heap memory. If heap_3.c is included in the project, the linker configuration will define the total heap size.

Use routines

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

Explanation

In FreeRTOSConfig.h, configSUPPORT_DYNAMIC_ALLOCATION must be set to 1 (support dynamic memory application), or simply undefined to make this function available.

Freertos more exciting

发布了163 篇原创文章 · 获赞 183 · 访问量 12万+

Guess you like

Origin blog.csdn.net/qq_31339221/article/details/101351398