Select your cookie preferences

We use essential cookies and similar tools that are necessary to provide our site and services. We use performance cookies to collect anonymous statistics, so we can understand how customers use our site and make improvements. Essential cookies cannot be deactivated, but you can choose “Customize” or “Decline” to decline performance cookies.

If you agree, AWS and approved third parties will also use cookies to provide useful site features, remember your preferences, and display relevant content, including relevant advertising. To accept or decline all non-essential cookies, choose “Accept” or “Decline.” To make more detailed choices, choose “Customize.”

Updated Feb 2025

FreeRTOS scheduling (single-core, AMP and SMP)

[More about tasks...]

Implementing a Task

A task should have the following structure:

1void vATaskFunction( void *pvParameters )
2{
3 for( ;; )
4 {
5 -- Task application code here. --
6 }
7
8 /* Tasks must not attempt to return from their implementing
9 function or otherwise exit. In newer FreeRTOS port
10 attempting to do so will result in an configASSERT() being
11 called if it is defined. If it is necessary for a task to
12 exit then have the task call vTaskDelete( NULL ) to ensure
13 its exit is clean. */
14 vTaskDelete( NULL );
15}

The type TaskFunction_t is defined as a function that returns void and takes a void pointer as its 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. However, as noted on the page that describes the RTOS scheduling algorithm, normally it is best to create tasks that are event-driven so as not to starve lower priority tasks of processing time, making the structure:

1void vATaskFunction( void *pvParameters )
2{
3 for( ;; )
4 {
5 /* Psudeo code showing a task waiting for an event
6 with a block time. If the event occurs, process it.
7 If the timeout expires before the event occurs, then
8 the system may be in an error state, so handle the
9 error. Here the pseudo code "WaitForEvent()" could
10 replaced with xQueueReceive(), ulTaskNotifyTake(),
11 xEventGroupWaitBits(), or any of the other FreeRTOS
12 communication and synchronisation primitives. */
13 if( WaitForEvent( EventObject, TimeOut ) == pdPASS )
14 {
15 -- Handle event here. --
16 }
17 else
18 {
19 -- Clear errors, or take actions here. --
20 }
21 }
22
23 /* As per the first code listing above. */
24 vTaskDelete( NULL );
25}

Again, see the RTOS demo application for numerous examples.

Tasks are created by calling xTaskCreate() or xTaskCreateStatic(), 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:

1void vATaskFunction( void *pvParameters );

Or,

1portTASK_FUNCTION_PROTO( vATaskFunction, pvParameters );

Likewise the function above could equally be written as:

1portTASK_FUNCTION( vATaskFunction, pvParameters )
2{
3 for( ;; )
4 {
5 -- Task application code here. --
6 }
7}