Loading...
Searching...
No Matches
timer.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2014-2015 Freie Universität Berlin
3 * SPDX-License-Identifier: LGPL-2.1-only
4 */
5
6#pragma once
7
31
32#include <limits.h>
33#include <stdint.h>
34#include <stdbool.h>
35
36#include "architecture.h"
37#include "periph_cpu.h"
38#include "periph_conf.h"
39
40#ifdef __cplusplus
41extern "C" {
42#endif
43
49#ifndef TIMER_DEV
50#define TIMER_DEV(x) (x)
51#endif
52
56#ifndef TIMER_UNDEF
57#define TIMER_UNDEF (UINT_FAST8_MAX)
58#endif
59
66#ifndef HAVE_TIMER_T
67typedef uint_fast8_t tim_t;
68#endif
69
75#ifndef TIM_FLAG_RESET_ON_SET
76#define TIM_FLAG_RESET_ON_SET (0x01)
77#endif
78
86#ifndef TIM_FLAG_RESET_ON_MATCH
87#define TIM_FLAG_RESET_ON_MATCH (0x02)
88#endif
89
96#ifndef TIM_FLAG_SET_STOPPED
97#define TIM_FLAG_SET_STOPPED (0x04)
98#endif
99
106typedef void (*timer_cb_t)(void *arg, int channel);
107
111#ifndef HAVE_TIMER_ISR_CTX_T
112typedef struct {
114 void *arg;
116#endif
117
138int timer_init(tim_t dev, uint32_t freq, timer_cb_t cb, void *arg);
139
154int timer_set(tim_t dev, int channel, unsigned int timeout);
155
170int timer_set_absolute(tim_t dev, int channel, unsigned int value);
171
190int timer_set_periodic(tim_t dev, int channel, unsigned int value, uint8_t flags);
191
201int timer_clear(tim_t dev, int channel);
202
210unsigned int timer_read(tim_t dev);
211
220
232
246__attribute__((pure))
248
260__attribute__((pure))
262
293uint32_t timer_query_freqs(tim_t dev, uword_t index);
294
311uint32_t timer_get_closest_freq(tim_t dev, uint32_t target);
312
313#if defined(DOXYGEN)
333/* As this function is polled, it needs to be inlined, so it is typically
334 * provided through timer_arch.h. If a platform ever does not need to go
335 * through static inline here, this declaration's condition can be extended to
336 * be `(defined(MODULE_PERIPH_TIMER_POLL) &&
337 * !defined(PERIPH_TIMER_PROVIDES_INLINE_POLL_CHANNEL) || defined(DOXYGEN)` or
338 * similar. */
339bool timer_poll_channel(tim_t dev, int channel);
340#endif
341
342#if defined(MODULE_PERIPH_TIMER_POLL)
343#include "timer_arch.h" /* IWYU pragma: export */
344#endif
345
346#ifdef __cplusplus
347}
348#endif
349
Platform-independent access to architecture details.
uword_t timer_query_freqs_numof(tim_t dev)
Get the number of different frequencies supported by the given timer.
unsigned int timer_read(tim_t dev)
Read the current value of the given timer device.
bool timer_poll_channel(tim_t dev, int channel)
Check whether a compare channel has matched.
void(* timer_cb_t)(void *arg, int channel)
Signature of event callback functions triggered from interrupts.
Definition timer.h:106
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.
uint32_t timer_get_closest_freq(tim_t dev, uint32_t target)
Search the frequency supported by the timer that is closest to a given target frequency,...
int timer_clear(tim_t dev, int channel)
Clear the given channel of the given timer device.
uint_fast8_t tim_t
Default timer type.
Definition timer.h:67
int timer_init(tim_t dev, uint32_t freq, timer_cb_t cb, void *arg)
Initialize the given timer.
uint32_t timer_query_freqs(tim_t dev, uword_t index)
Iterate over supported frequencies.
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.
void timer_stop(tim_t dev)
Stop the given timer.
uword_t timer_query_channel_numof(tim_t dev)
Get the number of timer channels for the given timer.
int timer_set(tim_t dev, int channel, unsigned int timeout)
Set a given timer channel for the given timer device.
void timer_start(tim_t dev)
Start the given timer.
uint< NUM > _t uword_t
Word sized unsigned integer.
Default interrupt context entry holding callback and argument.
Definition timer.h:112
void * arg
optional argument given to that callback
Definition timer.h:114
timer_cb_t cb
callback executed from timer interrupt
Definition timer.h:113