Timers
Contents
Introduction
Timers can be used to control periodic tasks as well as implement sophisticated algorithms. The implementation of each type of timer is platform-dependent and has different properties that make them useful in specific situations; some timers have low granularity (seconds) and overflow once in tens of years, and others provide high granularity (microseconds), but overflow rapidly. There are 5 types of timers provided by Contiki:
- timer
- stimer
- ctimer
- etimer
- rtimer
Source code
The functions of all types of timers are located inside folder core/sys/{timer, stimer, ctimer, etimer, rtimer}.{c,h}. A complete documentation of Timers in Contiki can be found here.
The timer and stimer are the most basic types of timers and are used to check if a time interval has passed. They do not notice when the time period has elapsed, so the application needs to check periodically if they have expired. The difference between them is the resolution: timers use system clock ticks, which gives high granularity (order of microseconds) but short overflow periods (order of seconds). On the other hand, stimers use seconds to allow much longer time periods (order of years), but has lesser granularity.
Unlike the other timers, the timer and stimer libraries can be safely used from interrupts which makes them especially useful in low level drivers.
The etimer library provides event timers and are used to schedule events to Contiki processes after a period of time. They are used in Contiki processes to wait for a time period while the rest of the system can work or enter low power mode.
The ctimer library provides callback timers and are used to schedule calls to callback functions after a period of time. Like event timers, they are used to wait for some time while the rest of the system can work or enter low power mode. Since the callback timers call a function when a timer expires, they are especially useful in any code that do not have an explicit Contiki process such as protocol implementations.
The rtimer library provides scheduling of real-time tasks. The rtimer library preempts any running Contiki process in order to let the real-time tasks execute at the scheduled time. The real-time tasks are used in time critical codes.