Loading...
Searching...
No Matches
xtimer_compat.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2019 Kaspar Schleiser <kaspar@schleiser.de>
3 *
4 * This file is subject to the terms and conditions of the GNU Lesser
5 * General Public License v2.1. See the file LICENSE in the top level
6 * directory for more details.
7 */
8
20#ifndef ZTIMER64_XTIMER_COMPAT_H
21#define ZTIMER64_XTIMER_COMPAT_H
22
23#include <assert.h>
24#include <stdbool.h>
25#include <stdint.h>
26
27/* make sure to overwrite potentially conflicting XTIMER_WIDTH definition from
28 * board.h by eagerly including it */
29#include "board.h"
30#include "div.h"
31#include "timex.h"
32#ifdef MODULE_CORE_MSG
33#include "msg.h"
34#endif /* MODULE_CORE_MSG */
35#include "mutex.h"
36#include "sched.h"
37
38#include "ztimer.h"
39#include "ztimer64.h"
40
41#ifdef __cplusplus
42extern "C" {
43#endif
44
45/* the xtimer API is documented elsewhere. This is just an (incomplete) wrapper,
46 * so skip doxygen.
47 */
48#ifndef DOXYGEN
49
50/* ztimer clocks with width lower than 32 bit get extended to 32 bit in software
51 * via ztimer_extend. So no matter what was defined elsewhere, we overwrite it
52 */
53#ifdef XTIMER_WIDTH
54#undef XTIMER_WIDTH
55#endif
56
57#define XTIMER_WIDTH (32)
58#define XTIMER_MASK (0)
59
64#ifndef XTIMER_BACKOFF
65#define XTIMER_BACKOFF 1
66#endif
67
68typedef ztimer64_t xtimer_t;
69typedef uint32_t xtimer_ticks32_t;
70typedef uint64_t xtimer_ticks64_t;
71typedef void (*xtimer_callback_t)(void *);
72
73static inline void xtimer_init(void)
74{
76}
77
78static inline xtimer_ticks32_t xtimer_ticks(uint32_t ticks)
79{
80 return ticks;
81}
82
83static inline xtimer_ticks32_t xtimer_now(void)
84{
86}
87
88static inline uint32_t _xtimer_now(void)
89{
91}
92
93static inline xtimer_ticks64_t xtimer_now64(void)
94{
96}
97
98static inline void xtimer_usleep64(uint64_t microseconds)
99{
100 ztimer64_sleep(ZTIMER64_USEC, microseconds);
101}
102
103static inline uint32_t xtimer_now_usec(void)
104{
106}
107
108static inline uint64_t xtimer_now_usec64(void)
109{
111}
112
113static inline void xtimer_sleep(uint32_t seconds)
114{
115 /* TODO: use ZTIMER64_SEC */
116 if (IS_ACTIVE(MODULE_ZTIMER64_MSEC)) {
117 ztimer64_sleep(ZTIMER64_MSEC, ((uint64_t)seconds) * 1000LLU);
118 }
119 else {
120 ztimer64_sleep(ZTIMER64_USEC, ((uint64_t)seconds) * 1000000LLU);
121 }
122}
123
124static inline void xtimer_msleep(uint32_t milliseconds)
125{
126 if (IS_ACTIVE(MODULE_ZTIMER_MSEC)) {
127 ztimer_sleep(ZTIMER_MSEC, milliseconds);
128 }
129 else {
130 ztimer64_sleep(ZTIMER64_USEC, ((uint64_t)milliseconds) * 1000LLU);
131 }
132}
133
134static inline void xtimer_usleep(uint32_t microseconds)
135{
136 ztimer_sleep(ZTIMER_USEC, microseconds);
137}
138
139static inline void xtimer_nanosleep(uint32_t nanoseconds)
140{
141 ztimer_sleep(ZTIMER_USEC, nanoseconds / NS_PER_US);
142}
143
144static inline void xtimer_set(xtimer_t *timer, uint32_t offset)
145{
146 ztimer64_set(ZTIMER64_USEC, timer, offset);
147}
148
149static inline void xtimer_remove(xtimer_t *timer)
150{
152}
153
154static inline bool xtimer_is_set(const xtimer_t *timer)
155{
156 return ztimer64_is_set(timer);
157}
158
159static inline void xtimer_set_msg(xtimer_t *timer, uint32_t offset, msg_t *msg,
160 kernel_pid_t target_pid)
161{
162 ztimer64_set_msg(ZTIMER64_USEC, timer, offset, msg, target_pid);
163}
164
165static inline void xtimer_periodic_wakeup(xtimer_ticks32_t *last_wakeup,
166 uint32_t period)
167{
168 ztimer_periodic_wakeup(ZTIMER_USEC, last_wakeup, period);
169}
170
171static inline uint32_t xtimer_usec_from_ticks(xtimer_ticks32_t ticks)
172{
173 return ticks;
174}
175
176static inline xtimer_ticks32_t xtimer_ticks_from_usec(uint32_t usec)
177{
178 return usec;
179}
180
181static inline void xtimer_now_timex(timex_t *out)
182{
183 uint64_t now = xtimer_now_usec64();
184
185 out->seconds = div_u64_by_1000000(now);
186 out->microseconds = now - (out->seconds * US_PER_SEC);
187}
188
189static inline int xtimer_msg_receive_timeout(msg_t *msg, uint32_t timeout)
190{
191 return ztimer_msg_receive_timeout(ZTIMER_USEC, msg, timeout);
192}
193
194static inline void xtimer_set_wakeup(xtimer_t *timer, uint32_t offset,
195 kernel_pid_t pid)
196{
197 ztimer64_set_wakeup(ZTIMER64_USEC, timer, offset, pid);
198}
199
200static inline int xtimer_mutex_lock_timeout(mutex_t *mutex, uint64_t us)
201{
203 /* Impedance matching required: Convert -ECANCELED error code to -1: */
204 return -1;
205 }
206 return 0;
207}
208
209static inline int xtimer_rmutex_lock_timeout(rmutex_t *rmutex, uint64_t timeout)
210{
211 if (ztimer64_rmutex_lock_timeout(ZTIMER64_USEC, rmutex, timeout)) {
212 /* Impedance matching required: Convert -ECANCELED error code to -1: */
213 return -1;
214 }
215 return 0;
216}
217
218static inline void xtimer_set_timeout_flag64(xtimer_t *t, uint64_t timeout)
219{
221}
222
223static inline void xtimer_set_timeout_flag(xtimer_t *t, uint32_t timeout)
224{
225 xtimer_set_timeout_flag64(t, timeout);
226}
227
228static inline void xtimer_spin(xtimer_ticks32_t ticks)
229{
230 assert(ticks < US_PER_MS);
232
233 while (ztimer_now(ZTIMER_USEC) - start < ticks) {
234 /* busy waiting */
235 }
236}
237
240{
241 return a - b;
242}
243
246{
247 return a - b;
248}
249
252{
253 return (xtimer_ticks32_t)(a - b);
254}
255
256static inline xtimer_ticks64_t xtimer_ticks64(uint64_t ticks)
257{
258 return ticks;
259}
260
261static inline bool xtimer_less(xtimer_ticks32_t a, xtimer_ticks32_t b)
262{
263 return a < b;
264}
265
266static inline bool xtimer_less64(xtimer_ticks64_t a, xtimer_ticks64_t b)
267{
268 return a < b;
269}
270
271static inline void xtimer_set64(xtimer_t *timer, uint64_t offset_us)
272{
273 ztimer64_set(ZTIMER64_USEC, timer, offset_us);
274}
275
276static inline void xtimer_tsleep32(xtimer_ticks32_t ticks)
277{
279}
280
281static inline void xtimer_tsleep64(xtimer_ticks64_t ticks)
282{
284}
285
286static inline void xtimer_set_wakeup64(xtimer_t *timer, uint64_t offset,
287 kernel_pid_t pid)
288{
289 ztimer64_set_wakeup(ZTIMER64_USEC, timer, offset, pid);
290}
291
292#if defined(MODULE_CORE_MSG) || defined(DOXYGEN)
293static inline void xtimer_set_msg64(xtimer_t *timer, uint64_t offset,
294 msg_t *msg, kernel_pid_t target_pid)
295{
296 ztimer64_set_msg(ZTIMER64_USEC, timer, offset, msg, target_pid);
297}
298
299static inline int xtimer_msg_receive_timeout64(msg_t *msg, uint64_t timeout)
300{
301 return ztimer64_msg_receive_timeout(ZTIMER64_USEC, msg, timeout);
302}
303
304#endif
305
306#endif /* DOXYGEN */
307
308#ifdef __cplusplus
309}
310#endif
311
313#endif /* ZTIMER64_XTIMER_COMPAT_H */
POSIX.1-2008 compliant version of the assert macro.
#define assert(cond)
abort the program if assertion is false
Definition assert.h:136
static uint64_t div_u64_by_1000000(uint64_t val)
Integer divide val by 1000000.
Definition div.h:111
int16_t kernel_pid_t
Unique process identifier.
Definition sched.h:139
#define US_PER_MS
The number of microseconds per millisecond.
Definition time_units.h:90
#define US_PER_SEC
The number of microseconds per second.
Definition time_units.h:85
#define NS_PER_US
The number of nanoseconds per microsecond.
Definition time_units.h:100
void xtimer_set_timeout_flag64(xtimer_t *t, uint64_t timeout)
Set timeout thread flag after timeout.
static void xtimer_usleep(uint32_t microseconds)
Pause the execution of a thread for some microseconds.
static xtimer_ticks32_t xtimer_diff(xtimer_ticks32_t a, xtimer_ticks32_t b)
Compute difference between two xtimer time stamps.
static uint32_t xtimer_now_usec(void)
get the current system time in microseconds since start
static void xtimer_set_msg64(xtimer_t *timer, uint64_t offset, msg_t *msg, kernel_pid_t target_pid)
Set a timer that sends a message, 64bit version.
void xtimer_remove(xtimer_t *timer)
remove a timer
static void xtimer_set_wakeup64(xtimer_t *timer, uint64_t offset, kernel_pid_t pid)
Set a timer that wakes up a thread, 64bit version.
static bool xtimer_less64(xtimer_ticks64_t a, xtimer_ticks64_t b)
Compare two xtimer time stamps, 64 bit version.
static xtimer_ticks64_t xtimer_now64(void)
get the current system time as 64bit time stamp
static void xtimer_set64(xtimer_t *timer, uint64_t offset_us)
Set a timer to execute a callback at some time in the future, 64bit version.
void(* xtimer_callback_t)(void *)
xtimer callback type
Definition xtimer.h:92
static void xtimer_tsleep32(xtimer_ticks32_t ticks)
Stop execution of a thread for some time, 32bit version.
static void xtimer_msleep(uint32_t milliseconds)
Pause the execution of a thread for some milliseconds.
static void xtimer_sleep(uint32_t seconds)
Pause the execution of a thread for some seconds.
static xtimer_ticks32_t xtimer_diff32_64(xtimer_ticks64_t a, xtimer_ticks64_t b)
Compute 32 bit difference between two 64 bit xtimer time stamps.
int xtimer_rmutex_lock_timeout(rmutex_t *rmutex, uint64_t us)
lock a rmutex but with timeout
static xtimer_ticks64_t xtimer_ticks64(uint64_t ticks)
Create an xtimer time stamp, 64 bit version.
static void xtimer_periodic_wakeup(xtimer_ticks32_t *last_wakeup, uint32_t period)
will cause the calling thread to be suspended until the absolute time (last_wakeup + period).
static void xtimer_set_msg(xtimer_t *timer, uint32_t offset, msg_t *msg, kernel_pid_t target_pid)
Set a timer that sends a message.
static bool xtimer_less(xtimer_ticks32_t a, xtimer_ticks32_t b)
Compare two xtimer time stamps.
static uint32_t xtimer_usec_from_ticks(xtimer_ticks32_t ticks)
Convert xtimer ticks to microseconds.
static xtimer_ticks64_t xtimer_diff64(xtimer_ticks64_t a, xtimer_ticks64_t b)
Compute difference between two xtimer time stamps, 64 bit version.
int xtimer_mutex_lock_timeout(mutex_t *mutex, uint64_t us)
lock a mutex but with timeout
static void xtimer_usleep64(uint64_t microseconds)
Pause the execution of a thread for some microseconds.
static int xtimer_msg_receive_timeout(msg_t *msg, uint32_t timeout)
receive a message blocking but with timeout
void xtimer_now_timex(timex_t *out)
get the current system time into a timex_t
void xtimer_set_timeout_flag(xtimer_t *t, uint32_t timeout)
Set timeout thread flag after timeout.
static void xtimer_tsleep64(xtimer_ticks64_t ticks)
Stop execution of a thread for some time, 64bit version.
static void xtimer_nanosleep(uint32_t nanoseconds)
Stop execution of a thread for some time.
static void xtimer_set_wakeup(xtimer_t *timer, uint32_t offset, kernel_pid_t pid)
Set a timer that wakes up a thread.
void xtimer_init(void)
xtimer initialization function
static void xtimer_spin(xtimer_ticks32_t ticks)
Stop execution of a thread for some time, blocking.
static uint64_t xtimer_now_usec64(void)
get the current system time in microseconds since start
static void xtimer_set(xtimer_t *timer, uint32_t offset)
Set a timer to execute a callback at some time in the future.
static xtimer_ticks32_t xtimer_ticks_from_usec(uint32_t usec)
Convert microseconds to xtimer ticks.
struct xtimer xtimer_t
xtimer timer structure
static xtimer_ticks32_t xtimer_ticks(uint32_t ticks)
Create an xtimer time stamp.
static xtimer_ticks32_t xtimer_now(void)
get the current system time as 32bit time stamp value
static bool xtimer_is_set(const xtimer_t *timer)
state if an xtimer is currently set (waiting to be expired)
static int xtimer_msg_receive_timeout64(msg_t *msg, uint64_t timeout)
receive a message blocking but with timeout, 64bit version
void ztimer64_init(void)
Initialize the board-specific default ztimer configuration.
unsigned ztimer64_is_set(const ztimer64_t *timer)
Check if a timer is currently active.
static void ztimer64_set_wakeup(ztimer64_clock_t *clock, ztimer64_t *timer, uint64_t offset, kernel_pid_t pid)
Set a timer that wakes up a thread (relative version)
Definition ztimer64.h:379
void ztimer64_remove(ztimer64_clock_t *clock, ztimer64_t *timer)
Remove a timer from a clock.
static void ztimer64_set_timeout_flag(ztimer64_clock_t *clock, ztimer64_t *timer, uint64_t timeout)
Set timeout thread flag after timeout.
Definition ztimer64.h:410
uint64_t ztimer64_now(ztimer64_clock_t *clock)
Get the current time from a clock.
static void ztimer64_set_msg(ztimer64_clock_t *clock, ztimer64_t *timer, uint64_t offset, msg_t *msg, kernel_pid_t target_pid)
Post a message after a delay (relative version)
Definition ztimer64.h:242
static int ztimer64_msg_receive_timeout(ztimer64_clock_t *clock, msg_t *msg, uint64_t timeout)
receive a message (blocking, with relative timeout)
Definition ztimer64.h:288
static int ztimer64_mutex_lock_timeout(ztimer64_clock_t *clock, mutex_t *mutex, uint64_t timeout)
Try to lock the given mutex, but give up after timeout.
Definition ztimer64.h:441
static void ztimer64_set(ztimer64_clock_t *clock, ztimer64_t *timer, uint64_t offset)
Set a timer on a clock (relative version)
Definition ztimer64.h:179
static void ztimer64_sleep(ztimer64_clock_t *clock, uint64_t duration)
Put the calling thread to sleep for the specified number of ticks.
Definition ztimer64.h:335
static int ztimer64_rmutex_lock_timeout(ztimer64_clock_t *clock, rmutex_t *rmutex, uint64_t timeout)
Try to lock the given rmutex, but give up after timeout.
Definition ztimer64.h:472
ztimer64_clock_t *const ZTIMER64_USEC
Default ztimer microsecond clock.
ztimer64_clock_t *const ZTIMER64_MSEC
Default ztimer millisecond clock.
void ztimer_periodic_wakeup(ztimer_clock_t *clock, uint32_t *last_wakeup, uint32_t period)
Suspend the calling thread until the time (last_wakeup + period)
ztimer_clock_t *const ZTIMER_USEC
Default ztimer microsecond clock.
uint32_t ztimer_now_t
type for ztimer_now() result
Definition ztimer.h:310
static ztimer_now_t ztimer_now(ztimer_clock_t *clock)
Get the current time from a clock.
Definition ztimer.h:666
int ztimer_msg_receive_timeout(ztimer_clock_t *clock, msg_t *msg, uint32_t timeout)
receive a message (blocking, with timeout)
void ztimer_sleep(ztimer_clock_t *clock, uint32_t duration)
Put the calling thread to sleep for the specified number of ticks.
ztimer_clock_t *const ZTIMER_MSEC
Default ztimer millisecond clock.
uint32_t _xtimer_now(void)
xtimer internal stuff
#define IS_ACTIVE(macro)
Allows to verify a macro definition outside the preprocessor.
Definition modules.h:60
Mutex for thread synchronization.
time_point now()
Returns the current time saved in a time point.
Definition chrono.hpp:104
Scheduler API definition.
Describes a message object which can be sent between threads.
Definition msg.h:196
Mutex structure.
Definition mutex.h:146
Mutex structure.
Definition rmutex.h:38
A timex timestamp.
Definition timex.h:49
uint32_t seconds
number of seconds
Definition timex.h:50
uint32_t microseconds
number of microseconds
Definition timex.h:51
xtimer timestamp (32 bit)
Definition xtimer.h:85
xtimer timestamp (64 bit)
Definition xtimer.h:76
xtimer timer structure
Definition xtimer.h:97
ztimer64 structure
Definition ztimer64.h:101
Utility library for comparing and computing timestamps.
ztimer 64bit API
ztimer API