xtimer_compat.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2019 Kaspar Schleiser <kaspar@schleiser.de>
3  * SPDX-License-Identifier: LGPL-2.1-only
4  */
5 
6 #pragma once
7 
20 #include <assert.h>
21 #include <stdbool.h>
22 #include <stdint.h>
23 
24 /* make sure to overwrite potentially conflicting XTIMER_WIDTH definition from
25  * board.h by eagerly including it */
26 #include "board.h"
27 #include "div.h"
28 #include "timex.h"
29 #ifdef MODULE_CORE_MSG
30 #include "msg.h"
31 #endif /* MODULE_CORE_MSG */
32 #include "mutex.h"
33 #include "sched.h"
34 
35 #include "ztimer.h"
36 #include "ztimer64.h"
37 
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
42 /* the xtimer API is documented elsewhere. This is just an (incomplete) wrapper,
43  * so skip doxygen.
44  */
45 #ifndef DOXYGEN
46 
47 /* ztimer clocks with width lower than 32 bit get extended to 32 bit in software
48  * via ztimer_extend. So no matter what was defined elsewhere, we overwrite it
49  */
50 #ifdef XTIMER_WIDTH
51 #undef XTIMER_WIDTH
52 #endif
53 
54 #define XTIMER_WIDTH (32)
55 #define XTIMER_MASK (0)
56 
61 #ifndef XTIMER_BACKOFF
62 #define XTIMER_BACKOFF 1
63 #endif
64 
65 typedef ztimer64_t xtimer_t;
66 typedef uint32_t xtimer_ticks32_t;
67 typedef uint64_t xtimer_ticks64_t;
68 typedef void (*xtimer_callback_t)(void *);
69 
70 static inline void xtimer_init(void)
71 {
72  ztimer64_init();
73 }
74 
75 static inline xtimer_ticks32_t xtimer_ticks(uint32_t ticks)
76 {
77  return ticks;
78 }
79 
80 static inline xtimer_ticks32_t xtimer_now(void)
81 {
83 }
84 
85 static inline uint32_t _xtimer_now(void)
86 {
88 }
89 
90 static inline xtimer_ticks64_t xtimer_now64(void)
91 {
93 }
94 
95 static inline void xtimer_usleep64(uint64_t microseconds)
96 {
97  ztimer64_sleep(ZTIMER64_USEC, microseconds);
98 }
99 
100 static inline uint32_t xtimer_now_usec(void)
101 {
102  return ztimer64_now(ZTIMER64_USEC);
103 }
104 
105 static inline uint64_t xtimer_now_usec64(void)
106 {
107  return ztimer64_now(ZTIMER64_USEC);
108 }
109 
110 static inline void xtimer_sleep(uint32_t seconds)
111 {
112  /* TODO: use ZTIMER64_SEC */
113  if (IS_ACTIVE(MODULE_ZTIMER64_MSEC)) {
114  ztimer64_sleep(ZTIMER64_MSEC, ((uint64_t)seconds) * 1000LLU);
115  }
116  else {
117  ztimer64_sleep(ZTIMER64_USEC, ((uint64_t)seconds) * 1000000LLU);
118  }
119 }
120 
121 static inline void xtimer_msleep(uint32_t milliseconds)
122 {
123  if (IS_ACTIVE(MODULE_ZTIMER_MSEC)) {
124  ztimer_sleep(ZTIMER_MSEC, milliseconds);
125  }
126  else {
127  ztimer64_sleep(ZTIMER64_USEC, ((uint64_t)milliseconds) * 1000LLU);
128  }
129 }
130 
131 static inline void xtimer_usleep(uint32_t microseconds)
132 {
133  ztimer_sleep(ZTIMER_USEC, microseconds);
134 }
135 
136 static inline void xtimer_nanosleep(uint32_t nanoseconds)
137 {
138  ztimer_sleep(ZTIMER_USEC, nanoseconds / NS_PER_US);
139 }
140 
141 static inline void xtimer_set(xtimer_t *timer, uint32_t offset)
142 {
143  ztimer64_set(ZTIMER64_USEC, timer, offset);
144 }
145 
146 static inline void xtimer_remove(xtimer_t *timer)
147 {
149 }
150 
151 static inline bool xtimer_is_set(const xtimer_t *timer)
152 {
153  return ztimer64_is_set(timer);
154 }
155 
156 static inline void xtimer_set_msg(xtimer_t *timer, uint32_t offset, msg_t *msg,
157  kernel_pid_t target_pid)
158 {
159  ztimer64_set_msg(ZTIMER64_USEC, timer, offset, msg, target_pid);
160 }
161 
162 static inline void xtimer_periodic_wakeup(xtimer_ticks32_t *last_wakeup,
163  uint32_t period)
164 {
165  ztimer_periodic_wakeup(ZTIMER_USEC, last_wakeup, period);
166 }
167 
168 static inline uint32_t xtimer_usec_from_ticks(xtimer_ticks32_t ticks)
169 {
170  return ticks;
171 }
172 
173 static inline xtimer_ticks32_t xtimer_ticks_from_usec(uint32_t usec)
174 {
175  return usec;
176 }
177 
178 static inline void xtimer_now_timex(timex_t *out)
179 {
180  uint64_t now = xtimer_now_usec64();
181 
183  out->microseconds = now - (out->seconds * US_PER_SEC);
184 }
185 
186 static inline int xtimer_msg_receive_timeout(msg_t *msg, uint32_t timeout)
187 {
188  return ztimer_msg_receive_timeout(ZTIMER_USEC, msg, timeout);
189 }
190 
191 static inline void xtimer_set_wakeup(xtimer_t *timer, uint32_t offset,
192  kernel_pid_t pid)
193 {
194  ztimer64_set_wakeup(ZTIMER64_USEC, timer, offset, pid);
195 }
196 
197 static inline int xtimer_mutex_lock_timeout(mutex_t *mutex, uint64_t us)
198 {
199  if (ztimer64_mutex_lock_timeout(ZTIMER64_USEC, mutex, us)) {
200  /* Impedance matching required: Convert -ECANCELED error code to -1: */
201  return -1;
202  }
203  return 0;
204 }
205 
206 static inline int xtimer_rmutex_lock_timeout(rmutex_t *rmutex, uint64_t timeout)
207 {
208  if (ztimer64_rmutex_lock_timeout(ZTIMER64_USEC, rmutex, timeout)) {
209  /* Impedance matching required: Convert -ECANCELED error code to -1: */
210  return -1;
211  }
212  return 0;
213 }
214 
215 static inline void xtimer_set_timeout_flag64(xtimer_t *t, uint64_t timeout)
216 {
218 }
219 
220 static inline void xtimer_set_timeout_flag(xtimer_t *t, uint32_t timeout)
221 {
222  xtimer_set_timeout_flag64(t, timeout);
223 }
224 
225 static inline void xtimer_spin(xtimer_ticks32_t ticks)
226 {
227  assert(ticks < US_PER_MS);
229 
230  while (ztimer_now(ZTIMER_USEC) - start < ticks) {
231  /* busy waiting */
232  }
233 }
234 
237 {
238  return a - b;
239 }
240 
243 {
244  return a - b;
245 }
246 
249 {
250  return (xtimer_ticks32_t)(a - b);
251 }
252 
253 static inline xtimer_ticks64_t xtimer_ticks64(uint64_t ticks)
254 {
255  return ticks;
256 }
257 
258 static inline bool xtimer_less(xtimer_ticks32_t a, xtimer_ticks32_t b)
259 {
260  return a < b;
261 }
262 
263 static inline bool xtimer_less64(xtimer_ticks64_t a, xtimer_ticks64_t b)
264 {
265  return a < b;
266 }
267 
268 static inline void xtimer_set64(xtimer_t *timer, uint64_t offset_us)
269 {
270  ztimer64_set(ZTIMER64_USEC, timer, offset_us);
271 }
272 
273 static inline void xtimer_tsleep32(xtimer_ticks32_t ticks)
274 {
275  ztimer_sleep(ZTIMER_USEC, ticks);
276 }
277 
278 static inline void xtimer_tsleep64(xtimer_ticks64_t ticks)
279 {
281 }
282 
283 static inline void xtimer_set_wakeup64(xtimer_t *timer, uint64_t offset,
284  kernel_pid_t pid)
285 {
286  ztimer64_set_wakeup(ZTIMER64_USEC, timer, offset, pid);
287 }
288 
289 #if defined(MODULE_CORE_MSG) || defined(DOXYGEN)
290 static inline void xtimer_set_msg64(xtimer_t *timer, uint64_t offset,
291  msg_t *msg, kernel_pid_t target_pid)
292 {
293  ztimer64_set_msg(ZTIMER64_USEC, timer, offset, msg, target_pid);
294 }
295 
296 static inline int xtimer_msg_receive_timeout64(msg_t *msg, uint64_t timeout)
297 {
298  return ztimer64_msg_receive_timeout(ZTIMER64_USEC, msg, timeout);
299 }
300 
301 #endif
302 
303 #endif /* DOXYGEN */
304 
305 #ifdef __cplusplus
306 }
307 #endif
308 
POSIX.1-2008 compliant version of the assert macro.
#define assert(cond)
abort the program if assertion is false
Definition: assert.h:143
static uint64_t div_u64_by_1000000(uint64_t val)
Integer divide val by 1000000.
Definition: div.h:107
int16_t kernel_pid_t
Unique process identifier.
Definition: sched.h:135
#define US_PER_MS
The number of microseconds per millisecond.
Definition: time_units.h:86
#define US_PER_SEC
The number of microseconds per second.
Definition: time_units.h:81
#define NS_PER_US
The number of nanoseconds per microsecond.
Definition: time_units.h:96
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:95
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:375
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:406
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:238
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:284
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:437
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:175
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:331
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:468
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:308
static ztimer_now_t ztimer_now(ztimer_clock_t *clock)
Get the current time from a clock.
Definition: ztimer.h:680
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:56
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:192
Mutex structure.
Definition: mutex.h:36
Mutex structure.
Definition: rmutex.h:34
A timex timestamp.
Definition: timex.h:45
uint32_t seconds
number of seconds
Definition: timex.h:46
uint32_t microseconds
number of microseconds
Definition: timex.h:47
xtimer timestamp (32 bit)
Definition: xtimer.h:88
xtimer timestamp (64 bit)
Definition: xtimer.h:79
xtimer timer structure
Definition: xtimer.h:100
ztimer64 structure
Definition: ztimer64.h:97
Utility library for comparing and computing timestamps.
ztimer 64bit API
ztimer API