Homepage  

Tasks
[FreeRTOS Fundamentals]

Task States


A task can exist in one of the following states:

Valid task state transitions



Task Priorities

Each task is assigned a priority from 0 to ( configMAX_PRIORITIES - 1 ). configMAX_PRIORITIES is defined within FreeRTOSConfig.h and can be set on an application by application basis. The higher the value given to configMAX_PRIORITIES the more RAM the FreeRTOS kernel will consume.

Low priority numbers denote low priority tasks, with the default idle priority defined by tskIDLE_PRIORITY as being zero.

The scheduler will ensure that a task in the ready or running state will always be given processor time in preference to tasks of a lower priority that are also in the ready state. In other words, the task given processing time will always be the highest priority task that is able to run.



Implementing a Task

A task should have the following structure:
    void vATaskFunction( void *pvParameters )
    {
        for( ;; )
        {
            -- Task application code here. --
        }
    }
 
The type pdTASK_CODE is defined as a function that returns void and takes a void pointer as it's only parameter. All functions that implement a task should be of this type. The parameter can be used to pass information of any type into the task - this is demonstrated by several of the standard demo application tasks.

Task functions should never return so are typically implemented as a continuous loop. Again, see the RTOS demo application for numerous examples.

Tasks are created by calling xTaskCreate() and deleted by calling vTaskDelete().



Task Creation Macros

Task functions can optionally be defined using the portTASK_FUNCTION and portTASK_FUNCTION_PROTO macros. These macro are provided to allow compiler specific syntax to be added to the function definition and prototype respectively. Their use is not required unless specifically stated in documentation for the port being used (currently only the PIC18 fedC port).

The prototype for the function shown above can be written as:

void vATaskFunction( void *pvParameters );
Or,
portTASK_FUNCTION_PROTO( vATaskFunction, pvParameters );
Likewise the function above could equally be written as:
    portTASK_FUNCTION( vATaskFunction, pvParameters )
    {
        for( ;; )
        {
            -- Task application code here. --
        }
    }
 



The Idle Task

The idle task is created automatically when the scheduler is started.

The idle task is responsible for freeing memory allocated by the RTOS to tasks that have since been deleted. It is therefore important in applications that make use of the vTaskDelete() function to ensure the idle task is not starved of processing time. The activity visualisation utility can be used to check the microcontroller time allocated to the idle task.

The idle task has no other active functions so can legitimately be starved of microcontroller time under all other conditions.

It is possible for application tasks to share the idle task priority. (tskIDLE_PRIORITY).



The Idle Task Hook

An idle task hook is a function that is called during each cycle of the idle task. If you want application functionality to run at the idle priority then there are two options:
  1. Implement the functionality in an idle task hook.

    There must always be at least one task that is ready to run. It is therefore imperative that the hook function does not call any API functions that might cause the task to block (vTaskDelay() for example. It is permissible for co-routines to block within the hook function).

  2. Create an idle priority task to implement the functionality.

    This is a more flexible solution but has a higher RAM usage overhead.

See the Embedded software application design section for more information on using an idle hook.

To create an idle hook:

  1. Set configUSE_IDLE_HOOK to 1 within FreeRTOSConfig.h.

  2. Define a function that has the following prototype:

    void vApplicationIdleHook( void );

A common use for an idle hook is to simply put the processor into a power saving mode.





Copyright (C) 2003 - 2008 Richard Barry
Any and all data, files, source code, html content and documentation included in the FreeRTOS distribution or available on this site are the exclusive property of Richard Barry. See the files license.txt (included in the distribution) and this copyright notice for more information. FreeRTOSTM and FreeRTOS.orgTM are trade marks of Richard Barry.