Embedded applications need to schedule future events. This can be accomplished using timers and timers' services.
Timers are an integral part of any real-time system. A timer can be seen as an event scheduling according to a predefined time value in the future, exactly like setting an alarm clock.
Embedded systems that have time-sensitive activities use 2 types of timers; hard-timers and soft-timers. Hard timers are based on physical timers on the chip that directly interrupt the CPU. A hard-timer is a must in case of an application demanding high precision and predication. On the other hand soft-timers are scheduled through a software facility that enables soft-timer's maintenance, installation, and removal.
But if hard-timers are more accurate than their soft counterparts, why are they used? The answer is in their nature. The can be programmed with timeouts of course granularity. In addition, the high-precision is not always needed in most of embedded systems. Another prevailing reason is reducing system interrupt overhead. Facilities of soft-timers are built using only 1 hardware-timer.
SW Timers in FreeRTOS
Starting from FreeRTOS V7.0.0, FreeRTOS added the soft-timers feature. FreeRTOS timers have 2 modes of operations; autorelaod and 1-shot modes. Whenever a timer timeouts, a callback function is executed. This service is implemented as a FreeRTOS kernel task and a FreeRTOS queue. This task is assigned a stack size and priority defined the macros configTIMER_TASK_STACK_DEPTH and configTIMER_TASK_PRIORITY respectively. The timers APIs are implemented through the queue. Whenever a task calls a timer API it send s a command to the timer task through the timers queue. This task continually gets the next expire time, either process the expired timer or blocks until a timer expires or it receives a command and processes it. Like any other FreeRTOS service, this service is highly configurable. In order to use it you have to:
- Define configUSE_TIMERS to 1
- Define configTIMER_TASK_PRIORITY and configTIMER_STACK_DEPTH
- Define configTIMER_QUEUE_LENGTH
- Add FreeRTOS/Source/timers.c to your application.
- Use the timers in your application
Demoing the SW Timers under MS Windows
Last year, I showed how to run the industrial PC port under MS Windows. Recently, FreeRTOS team made a windows simulator. Unlike the PC port that used Open Watcom and 80x86 assembly, it uses MS VC or MingW and Windows APIs.
The following steps shows how to test the SW timers with the FreeRTOS MS Windows simulator:
- Download MS VC++ 2010 Express edition from here.
- After Installing the compiler, download FreeRTOS from here.
- Open the project using MS VC and here you go.