FreeRTOS API Reference 7.0.0 r0

Task Creation

xTaskHandle

task. h

Type by which tasks are referenced. For example, a call to xTaskCreate returns (via a pointer parameter) an xTaskHandle variable that can then be used as a parameter to vTaskDelete to delete the task.

xTaskCreate

task. h

 portBASE_TYPE xTaskCreate(
         pdTASK_CODE pvTaskCode,
         const char * const pcName,
         unsigned short usStackDepth,
         void *pvParameters,
         unsigned portBASE_TYPE uxPriority,
         xTaskHandle *pvCreatedTask
        );

Create a new task and add it to the list of tasks that are ready to run.

xTaskCreate() can only be used to create a task that has unrestricted access to the entire microcontroller memory map. Systems that include MPU support can alternatively create an MPU constrained task using xTaskCreateRestricted().

Parameters:
pvTaskCodePointer to the task entry function. Tasks must be implemented to never return (i.e. continuous loop).
pcNameA descriptive name for the task. This is mainly used to facilitate debugging. Max length defined by tskMAX_TASK_NAME_LEN - default is 16.
usStackDepthThe size of the task stack specified as the number of variables the stack can hold - not the number of bytes. For example, if the stack is 16 bits wide and usStackDepth is defined as 100, 200 bytes will be allocated for stack storage.
pvParametersPointer that will be used as the parameter for the task being created.
uxPriorityThe priority at which the task should run. Systems that include MPU support can optionally create tasks in a privileged (system) mode by setting bit portPRIVILEGE_BIT of the priority parameter. For example, to create a privileged task at priority 2 the uxPriority parameter should be set to ( 2 | portPRIVILEGE_BIT ).
pvCreatedTaskUsed to pass back a handle by which the created task can be referenced.
Returns:
pdPASS if the task was successfully created and added to a ready list, otherwise an error code defined in the file errors. h

Example usage:

 // Task to be created.
 void vTaskCode( void * pvParameters )
 {
  for( ;; )
  {
   // Task code goes here.
  }
 }

 // Function that creates a task.
 void vOtherFunction( void )
 {
 static unsigned char ucParameterToPass;
 xTaskHandle xHandle;

  // Create the task, storing the handle.  Note that the passed parameter ucParameterToPass
  // must exist for the lifetime of the task, so in this case is declared static.  If it was just an
  // an automatic stack variable it might no longer exist, or at least have been corrupted, by the time
  // the new task attempts to access it.
  xTaskCreate( vTaskCode, "NAME", STACK_SIZE, &ucParameterToPass, tskIDLE_PRIORITY, &xHandle );

  // Use the handle to delete the task.
  vTaskDelete( xHandle );
 }

vTaskDelete

task. h

 void vTaskDelete( xTaskHandle pxTask ); 

INCLUDE_vTaskDelete must be defined as 1 for this function to be available. See the configuration section for more information.

Remove a task from the RTOS real time kernels management. The task being deleted will be removed from all ready, blocked, suspended and event lists.

NOTE: The idle task is responsible for freeing the kernel allocated memory from tasks that have been deleted. It is therefore important that the idle task is not starved of microcontroller processing time if your application makes any calls to vTaskDelete (). Memory allocated by the task code is not automatically freed, and should be freed before the task is deleted.

See the demo application file death.c for sample code that utilises vTaskDelete ().

Parameters:
pxTaskThe handle of the task to be deleted. Passing NULL will cause the calling task to be deleted.

Example usage:

 void vOtherFunction( void )
 {
 xTaskHandle xHandle;

  // Create the task, storing the handle.
  xTaskCreate( vTaskCode, "NAME", STACK_SIZE, NULL, tskIDLE_PRIORITY, &xHandle );

  // Use the handle to delete the task.
  vTaskDelete( xHandle );
 }
 All Data Structures