implementation.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2015 Kaspar Schleiser <kaspar@schleiser.de>
3  * SPDX-FileCopyrightText: 2016 Eistec AB
4  * SPDX-FileCopyrightText: 2018 Josua Arndt
5  * SPDX-License-Identifier: LGPL-2.1-only
6  */
7 
8 #pragma once
9 
23 #ifndef XTIMER_H
24 #error "Do not include this file directly! Use xtimer.h instead"
25 #endif
26 
27 #ifdef MODULE_XTIMER_ON_ZTIMER
28 #include "ztimer.h"
29 #else
30 #include "periph/timer.h"
31 #endif
32 
33 #include "irq.h"
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
39 extern volatile uint64_t _xtimer_current_time;
40 
44 #define MSG_XTIMER 12345
45 
49 static inline uint32_t _xtimer_lltimer_now(void)
50 {
51 #ifndef MODULE_XTIMER_ON_ZTIMER
52  return timer_read(XTIMER_DEV);
53 #else
54  return ztimer_now(ZTIMER_USEC);
55 #endif
56 
57 }
58 
62 static inline uint32_t _xtimer_lltimer_mask(uint32_t val)
63 {
64  /* cppcheck-suppress shiftTooManyBits
65  * (reason: cppcheck bug. `XTIMER_MASK` is zero when `XTIMER_WIDTH` is 32) */
66  return val & ~XTIMER_MASK;
67 }
68 
75 uint32_t _xtimer_now(void);
76 
77 void _xtimer_set64(xtimer_t *timer, uint32_t offset, uint32_t long_offset);
78 void _xtimer_periodic_wakeup(uint32_t *last_wakeup, uint32_t period);
79 void _xtimer_set_wakeup(xtimer_t *timer, uint32_t offset, kernel_pid_t pid);
80 void _xtimer_set_wakeup64(xtimer_t *timer, uint64_t offset, kernel_pid_t pid);
81 #ifdef MODULE_CORE_MSG
82 int _xtimer_msg_receive_timeout(msg_t *msg, uint32_t ticks);
83 int _xtimer_msg_receive_timeout64(msg_t *msg, uint64_t ticks);
84 void _xtimer_set_msg(xtimer_t *timer, uint32_t offset, msg_t *msg, kernel_pid_t target_pid);
85 void _xtimer_set_msg64(xtimer_t *timer, uint64_t offset, msg_t *msg, kernel_pid_t target_pid);
86 #endif /* MODULE_CORE_MSG */
87 
91 void _xtimer_tsleep(uint32_t offset, uint32_t long_offset);
94 #ifndef XTIMER_MIN_SPIN
98 #define XTIMER_MIN_SPIN _xtimer_usec_from_ticks(1)
99 #endif
100 
101 #ifndef DOXYGEN
102 /* Doxygen warns that these are undocumented, but the documentation can be found in xtimer.h */
103 
104 static inline uint64_t _xtimer_now64(void)
105 {
106  uint32_t now, elapsed;
107 
108  /* time sensitive since _xtimer_current_time is updated here */
109  unsigned state = irq_disable();
111 #if XTIMER_MASK
112  elapsed = _xtimer_lltimer_mask(now - _xtimer_lltimer_mask((uint32_t)_xtimer_current_time));
113  _xtimer_current_time += (uint64_t)elapsed;
114 #else
115  elapsed = now - ((uint32_t)_xtimer_current_time & 0xFFFFFFFF);
116  _xtimer_current_time += (uint64_t)elapsed;
117 #endif
118  irq_restore(state);
119 
120  return _xtimer_current_time;
121 }
122 
123 static inline xtimer_ticks32_t xtimer_now(void)
124 {
125  xtimer_ticks32_t ret;
126  ret.ticks32 = _xtimer_now();
127  return ret;
128 }
129 
130 static inline xtimer_ticks64_t xtimer_now64(void)
131 {
132  xtimer_ticks64_t ret;
133  ret.ticks64 = _xtimer_now64();
134  return ret;
135 }
136 
137 static inline uint32_t xtimer_now_usec(void)
138 {
140 }
141 
142 static inline uint64_t xtimer_now_usec64(void)
143 {
145 }
146 
147 static inline void _xtimer_spin(uint32_t offset) {
148  uint32_t start = _xtimer_lltimer_now();
149 #if XTIMER_MASK
150  offset = _xtimer_lltimer_mask(offset);
151  while (_xtimer_lltimer_mask(_xtimer_lltimer_now() - start) < offset);
152 #else
153  while ((_xtimer_lltimer_now() - start) < offset);
154 #endif
155 }
156 
157 static inline void _xtimer_tsleep32(uint32_t ticks)
158 {
159  _xtimer_tsleep(ticks, 0);
160 }
161 
162 static inline void _xtimer_tsleep64(uint64_t ticks)
163 {
164  _xtimer_tsleep((uint32_t)ticks, (uint32_t)(ticks >> 32));
165 }
166 
167 static inline void xtimer_spin(xtimer_ticks32_t ticks) {
168  _xtimer_spin(ticks.ticks32);
169 }
170 
171 static inline void xtimer_msleep(uint32_t milliseconds)
172 {
173  _xtimer_tsleep64(_xtimer_ticks_from_usec64(milliseconds * US_PER_MS));
174 }
175 
176 static inline void xtimer_usleep(uint32_t microseconds)
177 {
178  _xtimer_tsleep32(_xtimer_ticks_from_usec(microseconds));
179 }
180 
181 static inline void xtimer_usleep64(uint64_t microseconds)
182 {
183  _xtimer_tsleep64(_xtimer_ticks_from_usec64(microseconds));
184 }
185 
186 static inline void xtimer_sleep(uint32_t seconds)
187 {
188  _xtimer_tsleep64(_xtimer_ticks_from_usec64((uint64_t)seconds * US_PER_SEC));
189 }
190 
191 static inline void xtimer_nanosleep(uint32_t nanoseconds)
192 {
193  _xtimer_tsleep32(_xtimer_ticks_from_usec(nanoseconds / NS_PER_US));
194 }
195 
196 static inline void xtimer_tsleep32(xtimer_ticks32_t ticks)
197 {
198  _xtimer_tsleep32(ticks.ticks32);
199 }
200 
201 static inline void xtimer_tsleep64(xtimer_ticks64_t ticks)
202 {
203  _xtimer_tsleep64(ticks.ticks64);
204 }
205 
206 static inline void xtimer_periodic_wakeup(xtimer_ticks32_t *last_wakeup, uint32_t period)
207 {
208  _xtimer_periodic_wakeup(&last_wakeup->ticks32, _xtimer_ticks_from_usec(period));
209 }
210 
211 static inline void xtimer_set_wakeup(xtimer_t *timer, uint32_t offset, kernel_pid_t pid)
212 {
213  _xtimer_set_wakeup(timer, _xtimer_ticks_from_usec(offset), pid);
214 }
215 
216 static inline void xtimer_set_wakeup64(xtimer_t *timer, uint64_t offset, kernel_pid_t pid)
217 {
218  _xtimer_set_wakeup64(timer, _xtimer_ticks_from_usec64(offset), pid);
219 }
220 
221 static inline void xtimer_set(xtimer_t *timer, uint32_t offset)
222 {
223  _xtimer_set64(timer, _xtimer_ticks_from_usec(offset), 0);
224 }
225 
226 static inline void xtimer_set64(xtimer_t *timer, uint64_t period_us)
227 {
228  uint64_t ticks = _xtimer_ticks_from_usec64(period_us);
229  _xtimer_set64(timer, ticks, ticks >> 32);
230 }
231 
232 #ifdef MODULE_CORE_MSG
233 static inline int xtimer_msg_receive_timeout(msg_t *msg, uint32_t timeout)
234 {
235  return _xtimer_msg_receive_timeout(msg, _xtimer_ticks_from_usec(timeout));
236 }
237 
238 static inline int xtimer_msg_receive_timeout64(msg_t *msg, uint64_t timeout)
239 {
240  return _xtimer_msg_receive_timeout64(msg, _xtimer_ticks_from_usec64(timeout));
241 }
242 
243 static inline void xtimer_set_msg(xtimer_t *timer, uint32_t offset, msg_t *msg, kernel_pid_t target_pid)
244 {
245  _xtimer_set_msg(timer, _xtimer_ticks_from_usec(offset), msg, target_pid);
246 }
247 
248 static inline void xtimer_set_msg64(xtimer_t *timer, uint64_t offset, msg_t *msg, kernel_pid_t target_pid)
249 {
250  _xtimer_set_msg64(timer, _xtimer_ticks_from_usec64(offset), msg, target_pid);
251 }
252 #endif /* MODULE_CORE_MSG */
253 
254 static inline xtimer_ticks32_t xtimer_ticks_from_usec(uint32_t usec)
255 {
256  xtimer_ticks32_t ticks;
257  ticks.ticks32 = _xtimer_ticks_from_usec(usec);
258  return ticks;
259 }
260 
261 static inline xtimer_ticks64_t xtimer_ticks_from_usec64(uint64_t usec)
262 {
263  xtimer_ticks64_t ticks;
264  ticks.ticks64 = _xtimer_ticks_from_usec64(usec);
265  return ticks;
266 }
267 
268 static inline uint32_t xtimer_usec_from_ticks(xtimer_ticks32_t ticks)
269 {
270  return _xtimer_usec_from_ticks(ticks.ticks32);
271 }
272 
273 static inline uint64_t xtimer_usec_from_ticks64(xtimer_ticks64_t ticks)
274 {
275  return _xtimer_usec_from_ticks64(ticks.ticks64);
276 }
277 
278 static inline xtimer_ticks32_t xtimer_ticks(uint32_t ticks)
279 {
280  xtimer_ticks32_t ret;
281  ret.ticks32 = ticks;
282  return ret;
283 }
284 
285 static inline xtimer_ticks64_t xtimer_ticks64(uint64_t ticks)
286 {
287  xtimer_ticks64_t ret;
288  ret.ticks64 = ticks;
289  return ret;
290 }
291 
293 {
294  xtimer_ticks32_t ret;
295  ret.ticks32 = a.ticks32 - b.ticks32;
296  return ret;
297 }
298 
300 {
301  xtimer_ticks64_t ret;
302  ret.ticks64 = a.ticks64 - b.ticks64;
303  return ret;
304 }
305 
307 {
308  uint64_t diff = a.ticks64 - b.ticks64;
309  xtimer_ticks32_t ret;
310  ret.ticks32 = diff;
311  return ret;
312 }
313 
314 static inline bool xtimer_less(xtimer_ticks32_t a, xtimer_ticks32_t b)
315 {
316  return (a.ticks32 < b.ticks32);
317 }
318 
319 static inline bool xtimer_less64(xtimer_ticks64_t a, xtimer_ticks64_t b)
320 {
321  return (a.ticks64 < b.ticks64);
322 }
323 
324 static inline bool xtimer_is_set(const xtimer_t *timer)
325 {
326  return timer->offset || timer->long_offset;
327 }
328 
329 #endif /* !defined(DOXYGEN) */
330 
331 #ifdef __cplusplus
332 }
333 #endif
334 
Low-level timer peripheral driver interface definitions.
MAYBE_INLINE void irq_restore(unsigned state)
This function restores the IRQ disable bit in the status register to the value contained within passe...
MAYBE_INLINE unsigned irq_disable(void)
This function sets the IRQ disable bit in the status register.
int16_t kernel_pid_t
Unique process identifier.
Definition: sched.h:135
unsigned int timer_read(tim_t dev)
Read the current value of the given timer device.
#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
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 xtimer_ticks64_t xtimer_ticks_from_usec64(uint64_t usec)
Convert microseconds to xtimer ticks, 64 bit version.
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.
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.
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.
#define XTIMER_DEV
Underlying hardware timer device to assign to xtimer.
Definition: xtimer.h:586
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.
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.
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
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.
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.
#define XTIMER_MASK
xtimer timer mask
Definition: xtimer.h:620
static xtimer_ticks32_t xtimer_ticks_from_usec(uint32_t usec)
Convert microseconds to xtimer ticks.
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 uint64_t xtimer_usec_from_ticks64(xtimer_ticks64_t ticks)
Convert xtimer ticks to microseconds, 64 bit version.
static int xtimer_msg_receive_timeout64(msg_t *msg, uint64_t timeout)
receive a message blocking but with timeout, 64bit version
ztimer_clock_t *const ZTIMER_USEC
Default ztimer microsecond clock.
static ztimer_now_t ztimer_now(ztimer_clock_t *clock)
Get the current time from a clock.
Definition: ztimer.h:680
uint32_t _xtimer_now(void)
xtimer internal stuff
static uint32_t _xtimer_lltimer_mask(uint32_t val)
drop bits of a value that don't fit into the low-level timer.
void _xtimer_tsleep(uint32_t offset, uint32_t long_offset)
Sleep for the given number of ticks.
static uint32_t _xtimer_lltimer_now(void)
returns the (masked) low-level timer counter value.
IRQ driver interface.
time_point now()
Returns the current time saved in a time point.
Definition: chrono.hpp:104
Describes a message object which can be sent between threads.
Definition: msg.h:192
xtimer timestamp (32 bit)
Definition: xtimer.h:88
uint32_t ticks32
Tick count.
Definition: xtimer.h:89
xtimer timestamp (64 bit)
Definition: xtimer.h:79
uint64_t ticks64
Tick count.
Definition: xtimer.h:80
xtimer timer structure
Definition: xtimer.h:100
uint32_t long_offset
upper 32bit offset time
Definition: xtimer.h:103
uint32_t offset
lower 32bit offset time
Definition: xtimer.h:102
ztimer API