Software Timers

FreeRTOS includes a mechanism called Software Timers that is very similar to what other RTOS call ‘Clocks’. In this section we will provide a brief introduction to FreeRTOS Software Timers. For all the details, please visit FreeRTOS Software Timers and the FreeRTOS API Reference.

The basics

A Software Timer allows you to execute a function some time in the future. To make use of this feature, you will need to:

  • Create a software timer
  • Define the software timer’s callback function (i.e. the function that you want to execute in the future)
  • Define the software timer’s period (i.e. the point in the future when you want it executed)

Warning

As indicated in FreeRTOS Software Timers, it is imperative that a timer’s callback does not attempt to block. That is, it cannot call vTaskDelay(), vTaskDelayUntil(), or specify a non-zero block time when accessing a queue or a semaphore.

You can select a Software Timer to be one-shot or auto-reload during creation. See more details about this in section Creating and Starting a Software Timer below. You may also want to check the FreeRTOS documentation, specifically FreeRTOS Software Timer One-Shot vs Autoreload and the description of xTaskCreate().

Using Software Timers

Please note that Software Timers are not part of the core FreeRTOS kernel. In order to use them, you must first take the following two steps:

  1. Add the FreeRTOS/Source/timers.c source file to your project, and…
  2. Define a few constants in the FreeRTOSConfig.h header file.

FreeRTOSConfig.h is part of the freertos_builds project.

See all the details at FreeRTOS Software Timer Configuration.

Details on Implementation

FreeRTOS Software Timers are provided by a timer service (or daemon) task. Your application will interact with FreeRTOS Timers through API functions, which will allow you to do operations such as create a timer, start and stop a timer, etc. These functions use a standard FreeRTOS queue to send commands to the timer service task. This queue is called the ‘timer command queue’ and cannot be accessed directly. Learn more about this at FreeRTOS Software Timers Service Task.

Creating and Starting a Software Timer

In this section we will provide an example of how to create and start a Software Timer. See more examples of usage in the FreeRTOS API Reference.

Listing 25. Create a FreeRTOS Software Timer
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <FreeRTOS.h>
#include <stdarg.h>


TimerHandle_t xTimer;

void main( void ) {

    BaseType_t xReturned;

    /* Create and start the timer. Recommended to do before starting the scheduler */
    xTimer = xTimerCreate(
            "Timer 1",                      /* Textual name, helpful during debugging! */
            pdMS_TO_TICKS( 500 ),           /* Timer period: 500 ms */
            pdTRUE,                         /* Autoreload */
            ( void * ) 1,                   /* Timer ID. Can be used to identify which
                                               timer expired if multiple timers share a
                                               timer callback */
            &vTimerCallback );              /* Function to call when timer expires. When
                                               invoked by FreeRTOS, the Timer ID will be
                                               passed as a parameter */

    if(xTimer != NULL)
    {
        BaseType_t started;
        started = xTimerStart(
               xTimer,                      /* Handle returned by xTimerCreate */
               0);                          /* Time to block in ticks: 0 (Do not block) */
        if (pdPASS != sarted) {
            /* Failed to start the timer! */
        }
     } else {
       /* Failed to create the timer! */
     }

    /* Start the FreeRTOS scheduler */
    vTaskStartScheduler();
}

/* Timer Callback Function (this may be reused by multiple timers if desired) */
void vTimerCallback( TimerHandle_t xTimer )
{
    /* *** Enter your application code here *** */
    /* Ex. */
    static uint32_t timer_count__500ms = 0;
    timer_count__500ms++;
}