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
35
36#include <limits.h>
37#include <stdint.h>
38#include <stdbool.h>
39
40#include "architecture.h"
41#include "periph_cpu.h"
42#include "periph_conf.h"
43
44#ifdef __cplusplus
45extern "C" {
46#endif
47
53#ifndef TIMER_DEV
54# define TIMER_DEV(x) (x)
55#endif
56
60#ifndef TIMER_UNDEF
61# define TIMER_UNDEF (UINT_FAST8_MAX)
62#endif
63
70#ifndef HAVE_TIMER_T
71typedef uint_fast8_t tim_t;
72#endif
73
80#ifndef TIM_FLAG_RESET_ON_SET
81# define TIM_FLAG_RESET_ON_SET (0x01)
82#endif
83
91#ifndef TIM_FLAG_RESET_ON_MATCH
92# define TIM_FLAG_RESET_ON_MATCH (0x02)
93#endif
94
101#ifndef TIM_FLAG_SET_STOPPED
102# define TIM_FLAG_SET_STOPPED (0x04)
103#endif
104
111typedef void (*timer_cb_t)(void *arg, int channel);
112
116#ifndef HAVE_TIMER_ISR_CTX_T
117typedef struct {
119 void *arg;
121#endif
122
148int timer_init(tim_t dev, uint32_t freq, timer_cb_t cb, void *arg);
149
168int timer_set(tim_t dev, int channel, unsigned int timeout);
169
188int timer_set_absolute(tim_t dev, int channel, unsigned int value);
189
209int timer_set_periodic(tim_t dev, int channel, unsigned int value, uint8_t flags);
210
220int timer_clear(tim_t dev, int channel);
221
229unsigned int timer_read(tim_t dev);
230
239
251
268__attribute__((pure))
270
285__attribute__((pure))
287
317uint32_t timer_query_freqs(tim_t dev, uword_t index);
318
335uint32_t timer_get_closest_freq(tim_t dev, uint32_t target);
336
337#if defined(DOXYGEN)
356/* As this function is polled, it needs to be inlined, so it is typically
357 * provided through `timer_arch.h`. If a platform ever does not need to go
358 * through static inline here, this declaration's condition can be extended to
359 * be `(defined(MODULE_PERIPH_TIMER_POLL) &&
360 * !defined(PERIPH_TIMER_PROVIDES_INLINE_POLL_CHANNEL) || defined(DOXYGEN)` or
361 * similar. */
362bool timer_poll_channel(tim_t dev, int channel);
363#endif
364
365#if defined(MODULE_PERIPH_TIMER_POLL)
366# include "timer_arch.h" /* IWYU pragma: export */
367#endif
368
369#ifdef __cplusplus
370}
371#endif
372
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:111
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:71
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:117
void * arg
optional argument given to that callback
Definition timer.h:119
timer_cb_t cb
callback executed from timer interrupt
Definition timer.h:118