Why we need Software Timers ?
As you know , the digital circuit elements acts with clock and SoC(System on Chip) used to have provision of hardware timers to trigger various events. Similarly operating system achieve performance with parallel processing concept using timers. However it requires a large number of timers as compared to available hardware timers. This is achieved by the concept of software timers.
Software Timer provide Functionality of keeping Time track and execute a sub routine (Timer Handler/ Timer Call back) after a particular set time. Set Time refers to the timers time period which is set at the time of software timer object creation.
Important information on writing timer callback functions
Timer callback functions may execute in the context of the timer service task or Interrupt. It is therefore essential that timer callback functions never attempt to block or perform any delay operation because it will affect timing operation and other software timers too.
Software Timers In RTOS (FreeRTOS)
FreeRTOS used as a software stack for embedded controllers like ARM. This stack provides software timer functionality with software timers API.
In a Arm Controller , “Systick” Hardware timer is dedicated to FreeRTOS software stack for scheduler reference and it is also used for software Timer reference. Systick timer is configurable to customize scheduling time period. Every time when Systick timer overflow scheduler gets invoked and Systick count increments.
Let’s say Systick timer period is 1ms. After every 1ms systick count will increment. When a software timer object is created with some timing value for ex – TMR_new(2000); “2000 ms is the timer Value”. At the time of object initialization, it will capture the Systick value and at every overflow , it compares ( 2000 == (Current Systick – capture Systick)) . Whenever this condition is fulfilled, Timer Expiry Handler/callback will be Invoked.
- Timer Value comparison and handler/callback calling technique might be different in
different software stacks.
- TMR_new(2000) is just a dummy API. Actual FreeRTOS API is as shown below.
TimerHandle_t xTimerCreate( const char *pcTimerName,
const TickType_t xTimerPeriod,
const UBaseType_t uxAutoReload,
void * const pvTimerID,
TimerCallbackFunction_t pxCallbackFunction );
Refer below Picture for more Clarity.
Software Timers in GPOS (General Purpose Operating System) Linux
Linux Software timer can be configured by using “timer_create” API.. It creates a POSIX per-
process timer which returns an unique timerId .Timer start/stop/delete operation can be performed by using this timerid.
#include <signal.h> #include <time.h> int timer_create(clockid_t clockid, struct sigevent *sevp, timer_t *timerid);
An argument ClockId is used to set the reference clock for software timer. Linux OS has provision of multiple clock references. Sigevent structure holds the timer attributes like handler time. It also specifies how the caller should be notified when the timer expires. Third parameter holds the unique value of timerId assigned by kernel .
Software Timers Advantages and Disadvantages
- Multiple Timer instances can be created
- Timer Functionality is widely available
- Additional Software overhead
- Precision and efficiency is slightly less as compared to hardware timer
Embedded Software EngineerSkills: C Programming, Embedded System DesigningQualification: Electrical Engineering, PGd- Embedded Systems designing
Thanks for reading till end. If you liked it, Please share in your network.
Have a look at Embedkari Low Cost Products