Low-level timer peripheral driver. More...
Low-level timer peripheral driver.
After calling timer_init(), the underlying hardware timer should be powered on and running. When a timer is explicitly stopped by calling timer_stop(), the timer should be stopped and powered down (e.g. by peripheral clock gating). Once the timer is started again (by calling timer_start()), it should transparently continue its previously configured operation.
While the timer is active, the implementation might need to block certain power modes on specific CPU implementation.
Files | |
file | timer_arch.h |
CPU specific part of the timer API. | |
file | timer.h |
Low-level timer peripheral driver interface definitions. | |
Data Structures | |
struct | timer_isr_ctx_t |
Default interrupt context entry holding callback and argument. More... | |
Macros | |
#define | TIMER_DEV(x) (x) |
Default timer definition macro. | |
#define | TIMER_UNDEF (UINT_FAST8_MAX) |
Default value for timer not defined. | |
#define | TIM_FLAG_RESET_ON_SET (0x01) |
Reset the timer when the set() function is called. | |
#define | TIM_FLAG_RESET_ON_MATCH (0x02) |
Reset the timer on match. | |
#define | TIM_FLAG_SET_STOPPED (0x04) |
Keep the timer stopped after setting alarm. | |
Typedefs | |
typedef uint_fast8_t | tim_t |
Default timer type. | |
typedef void(* | timer_cb_t) (void *arg, int channel) |
Signature of event callback functions triggered from interrupts. | |
Functions | |
int | timer_init (tim_t dev, uint32_t freq, timer_cb_t cb, void *arg) |
Initialize the given timer. | |
int | timer_set (tim_t dev, int channel, unsigned int timeout) |
Set a given timer channel for the given timer device. | |
int | timer_set_absolute (tim_t dev, int channel, unsigned int value) |
Set an absolute timeout value for the given channel of the given timer. | |
int | timer_set_periodic (tim_t dev, int channel, unsigned int value, uint8_t flags) |
Set an absolute timeout value for the given channel of the given timer. | |
int | timer_clear (tim_t dev, int channel) |
Clear the given channel of the given timer device. | |
unsigned int | timer_read (tim_t dev) |
Read the current value of the given timer device. | |
void | timer_start (tim_t dev) |
Start the given timer. | |
void | timer_stop (tim_t dev) |
Stop the given timer. | |
uword_t | timer_query_freqs_numof (tim_t dev) |
Get the number of different frequencies supported by the given timer. | |
uword_t | timer_query_channel_numof (tim_t dev) |
Get the number of timer channels for the given timer. | |
uint32_t | timer_query_freqs (tim_t dev, uword_t index) |
Iterate over supported frequencies. | |
bool | timer_poll_channel (tim_t dev, int channel) |
Check whether a compare channel has matched. | |
#define TIM_FLAG_RESET_ON_MATCH (0x02) |
#define TIM_FLAG_RESET_ON_SET (0x01) |
Reset the timer when the set() function is called.
When set, calling the timer_set_periodic() function resets the timer count value.
#define TIM_FLAG_SET_STOPPED (0x04) |
Keep the timer stopped after setting alarm.
When set, the timer will remained stopped after a timer_set_periodic() and can be started manually with timer_start().
#define TIMER_DEV | ( | x | ) | (x) |
#define TIMER_UNDEF (UINT_FAST8_MAX) |
typedef uint_fast8_t tim_t |
typedef void(* timer_cb_t) (void *arg, int channel) |
int timer_clear | ( | tim_t | dev, |
int | channel | ||
) |
Clear the given channel of the given timer device.
[in] | dev | the timer device to clear |
[in] | channel | the channel on the given device to clear |
int timer_init | ( | tim_t | dev, |
uint32_t | freq, | ||
timer_cb_t | cb, | ||
void * | arg | ||
) |
Initialize the given timer.
Each timer device is running with the given speed. Each can contain one or more channels as defined in periph_conf.h. The timer is configured in up-counting mode and will count until TIMER_x_MAX_VALUE as defined in used board's periph_conf.h until overflowing.
The timer will be started automatically after initialization with interrupts enabled.
[in] | dev | the timer to initialize |
[in] | freq | requested number of ticks per second |
[in] | cb | this callback is called in interrupt context, the emitting channel is passed as argument |
[in] | arg | argument to the callback |
bool timer_poll_channel | ( | tim_t | dev, |
int | channel | ||
) |
Check whether a compare channel has matched.
It is currently not defined whether this keeps returning true after a channel has been polled until that channel is set, or whether later calls return false.
This is typically used in spin loops that wait for a timer's completion:
This function is only available on platforms that implement the periph_timer_poll
peripheral in addition to periph_timer
.
Get the number of timer channels for the given timer.
This function is marked with attribute pure to tell the compiler that this function has no side affects and will return the same value when called with the same timer as parameter.
There is a weak default implementation that returns the value of TIMER_CHANNEL_NUMOF
. For some MCUs the number of supported channels depends on dev
- those are expected to provide there own implementation of this function.
Iterate over supported frequencies.
dev | Timer to get the next supported frequency of |
index | Index of the frequency to get |
index
highest frequency supported by the timer 0 | index is too high |
FEATURES_REQUIRED += periph_timer_query_freqs
to your Makefile
.When called with a value of 0 for index
, the highest supported frequency is returned. For a value 1 the second highest is returned, and so on. For values out of range, 0 is returned. A program hence can iterate over all supported frequencies using:
Or alternatively:
Get the number of different frequencies supported by the given timer.
If calling timer_query_freqs_numof for the same timer with an index smaller this number, it hence MUST return a frequency (and not zero).
This function is marked with attribute pure to tell the compiler that this function has no side affects and will return the same value when called with the same parameter. (E.g. to not call this function in every loop iteration when iterating over all supported frequencies.)
unsigned int timer_read | ( | tim_t | dev | ) |
Read the current value of the given timer device.
[in] | dev | the timer to read the current value from |
int timer_set | ( | tim_t | dev, |
int | channel, | ||
unsigned int | timeout | ||
) |
Set a given timer channel for the given timer device.
The callback given during initialization is called when timeout ticks have passed after calling this function
[in] | dev | the timer device to set |
[in] | channel | the channel to set |
[in] | timeout | timeout in ticks after that the registered callback is executed |
int timer_set_absolute | ( | tim_t | dev, |
int | channel, | ||
unsigned int | value | ||
) |
Set an absolute timeout value for the given channel of the given timer.
Timers that are less wide than unsigned int
accept and truncate overflown values.
[in] | dev | the timer device to set |
[in] | channel | the channel to set |
[in] | value | the absolute compare value when the callback will be triggered |
int timer_set_periodic | ( | tim_t | dev, |
int | channel, | ||
unsigned int | value, | ||
uint8_t | flags | ||
) |
Set an absolute timeout value for the given channel of the given timer.
The timeout will be called periodically for each iteration.
TIM_FLAG_RESET_ON_MATCH
can be active. Some platforms (Atmel) only allow to use the first channel as TOP value.FEATURES_REQUIRED += periph_timer_periodic
.[in] | dev | the timer device to set |
[in] | channel | the channel to set |
[in] | value | the absolute compare value when the callback will be triggered |
[in] | flags | options |
void timer_start | ( | tim_t | dev | ) |
Start the given timer.
This function is only needed if the timer was stopped manually before.
[in] | dev | the timer device to start |
void timer_stop | ( | tim_t | dev | ) |
Stop the given timer.
This will effect all of the timer's channels.
When the timer is stopped, the underlying timer peripheral should be completely powered off.
[in] | dev | the timer to stop |