implementation.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Kaspar Schleiser <kaspar@schleiser.de>
3  * 2016 Eistec AB
4  * 2018 Josua Arndt
5  *
6  * This file is subject to the terms and conditions of the GNU Lesser
7  * General Public License v2.1. See the file LICENSE in the top level
8  * directory for more details.
9  */
10 
23 #ifndef XTIMER_IMPLEMENTATION_H
24 #define XTIMER_IMPLEMENTATION_H
25 
26 #ifndef XTIMER_H
27 #error "Do not include this file directly! Use xtimer.h instead"
28 #endif
29 
30 #ifdef MODULE_XTIMER_ON_ZTIMER
31 #include "ztimer.h"
32 #else
33 #include "periph/timer.h"
34 #endif
35 
36 #include "irq.h"
37 
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
42 extern volatile uint64_t _xtimer_current_time;
43 
47 #define MSG_XTIMER 12345
48 
52 static inline uint32_t _xtimer_lltimer_now(void)
53 {
54 #ifndef MODULE_XTIMER_ON_ZTIMER
55  return timer_read(XTIMER_DEV);
56 #else
57  return ztimer_now(ZTIMER_USEC);
58 #endif
59 
60 }
61 
65 static inline uint32_t _xtimer_lltimer_mask(uint32_t val)
66 {
67  /* cppcheck-suppress shiftTooManyBits
68  * (reason: cppcheck bug. `XTIMER_MASK` is zero when `XTIMER_WIDTH` is 32) */
69  return val & ~XTIMER_MASK;
70 }
71 
78 uint32_t _xtimer_now(void);
79 
80 void _xtimer_set64(xtimer_t *timer, uint32_t offset, uint32_t long_offset);
81 void _xtimer_periodic_wakeup(uint32_t *last_wakeup, uint32_t period);
82 void _xtimer_set_wakeup(xtimer_t *timer, uint32_t offset, kernel_pid_t pid);
83 void _xtimer_set_wakeup64(xtimer_t *timer, uint64_t offset, kernel_pid_t pid);
84 #ifdef MODULE_CORE_MSG
85 int _xtimer_msg_receive_timeout(msg_t *msg, uint32_t ticks);
86 int _xtimer_msg_receive_timeout64(msg_t *msg, uint64_t ticks);
87 void _xtimer_set_msg(xtimer_t *timer, uint32_t offset, msg_t *msg, kernel_pid_t target_pid);
88 void _xtimer_set_msg64(xtimer_t *timer, uint64_t offset, msg_t *msg, kernel_pid_t target_pid);
89 #endif /* MODULE_CORE_MSG */
90 
94 void _xtimer_tsleep(uint32_t offset, uint32_t long_offset);
97 #ifndef XTIMER_MIN_SPIN
101 #define XTIMER_MIN_SPIN _xtimer_usec_from_ticks(1)
102 #endif
103 
104 #ifndef DOXYGEN
105 /* Doxygen warns that these are undocumented, but the documentation can be found in xtimer.h */
106 
107 static inline uint64_t _xtimer_now64(void)
108 {
109  uint32_t now, elapsed;
110 
111  /* time sensitive since _xtimer_current_time is updated here */
112  unsigned state = irq_disable();
114 #if XTIMER_MASK
115  elapsed = _xtimer_lltimer_mask(now - _xtimer_lltimer_mask((uint32_t)_xtimer_current_time));
116  _xtimer_current_time += (uint64_t)elapsed;
117 #else
118  elapsed = now - ((uint32_t)_xtimer_current_time & 0xFFFFFFFF);
119  _xtimer_current_time += (uint64_t)elapsed;
120 #endif
121  irq_restore(state);
122 
123  return _xtimer_current_time;
124 }
125 
126 static inline xtimer_ticks32_t xtimer_now(void)
127 {
128  xtimer_ticks32_t ret;
129  ret.ticks32 = _xtimer_now();
130  return ret;
131 }
132 
133 static inline xtimer_ticks64_t xtimer_now64(void)
134 {
135  xtimer_ticks64_t ret;
136  ret.ticks64 = _xtimer_now64();
137  return ret;
138 }
139 
140 static inline uint32_t xtimer_now_usec(void)
141 {
143 }
144 
145 static inline uint64_t xtimer_now_usec64(void)
146 {
148 }
149 
150 static inline void _xtimer_spin(uint32_t offset) {
151  uint32_t start = _xtimer_lltimer_now();
152 #if XTIMER_MASK
153  offset = _xtimer_lltimer_mask(offset);
154  while (_xtimer_lltimer_mask(_xtimer_lltimer_now() - start) < offset);
155 #else
156  while ((_xtimer_lltimer_now() - start) < offset);
157 #endif
158 }
159 
160 static inline void _xtimer_tsleep32(uint32_t ticks)
161 {
162  _xtimer_tsleep(ticks, 0);
163 }
164 
165 static inline void _xtimer_tsleep64(uint64_t ticks)
166 {
167  _xtimer_tsleep((uint32_t)ticks, (uint32_t)(ticks >> 32));
168 }
169 
170 static inline void xtimer_spin(xtimer_ticks32_t ticks) {
171  _xtimer_spin(ticks.ticks32);
172 }
173 
174 static inline void xtimer_msleep(uint32_t milliseconds)
175 {
176  _xtimer_tsleep64(_xtimer_ticks_from_usec64(milliseconds * US_PER_MS));
177 }
178 
179 static inline void xtimer_usleep(uint32_t microseconds)
180 {
181  _xtimer_tsleep32(_xtimer_ticks_from_usec(microseconds));
182 }
183 
184 static inline void xtimer_usleep64(uint64_t microseconds)
185 {
186  _xtimer_tsleep64(_xtimer_ticks_from_usec64(microseconds));
187 }
188 
189 static inline void xtimer_sleep(uint32_t seconds)
190 {
191  _xtimer_tsleep64(_xtimer_ticks_from_usec64((uint64_t)seconds * US_PER_SEC));
192 }
193 
194 static inline void xtimer_nanosleep(uint32_t nanoseconds)
195 {
196  _xtimer_tsleep32(_xtimer_ticks_from_usec(nanoseconds / NS_PER_US));
197 }
198 
199 static inline void xtimer_tsleep32(xtimer_ticks32_t ticks)
200 {
201  _xtimer_tsleep32(ticks.ticks32);
202 }
203 
204 static inline void xtimer_tsleep64(xtimer_ticks64_t ticks)
205 {
206  _xtimer_tsleep64(ticks.ticks64);
207 }
208 
209 static inline void xtimer_periodic_wakeup(xtimer_ticks32_t *last_wakeup, uint32_t period)
210 {
211  _xtimer_periodic_wakeup(&last_wakeup->ticks32, _xtimer_ticks_from_usec(period));
212 }
213 
214 static inline void xtimer_set_wakeup(xtimer_t *timer, uint32_t offset, kernel_pid_t pid)
215 {
216  _xtimer_set_wakeup(timer, _xtimer_ticks_from_usec(offset), pid);
217 }
218 
219 static inline void xtimer_set_wakeup64(xtimer_t *timer, uint64_t offset, kernel_pid_t pid)
220 {
221  _xtimer_set_wakeup64(timer, _xtimer_ticks_from_usec64(offset), pid);
222 }
223 
224 static inline void xtimer_set(xtimer_t *timer, uint32_t offset)
225 {
226  _xtimer_set64(timer, _xtimer_ticks_from_usec(offset), 0);
227 }
228 
229 static inline void xtimer_set64(xtimer_t *timer, uint64_t period_us)
230 {
231  uint64_t ticks = _xtimer_ticks_from_usec64(period_us);
232  _xtimer_set64(timer, ticks, ticks >> 32);
233 }
234 
235 #ifdef MODULE_CORE_MSG
236 static inline int xtimer_msg_receive_timeout(msg_t *msg, uint32_t timeout)
237 {
238  return _xtimer_msg_receive_timeout(msg, _xtimer_ticks_from_usec(timeout));
239 }
240 
241 static inline int xtimer_msg_receive_timeout64(msg_t *msg, uint64_t timeout)
242 {
243  return _xtimer_msg_receive_timeout64(msg, _xtimer_ticks_from_usec64(timeout));
244 }
245 
246 static inline void xtimer_set_msg(xtimer_t *timer, uint32_t offset, msg_t *msg, kernel_pid_t target_pid)
247 {
248  _xtimer_set_msg(timer, _xtimer_ticks_from_usec(offset), msg, target_pid);
249 }
250 
251 static inline void xtimer_set_msg64(xtimer_t *timer, uint64_t offset, msg_t *msg, kernel_pid_t target_pid)
252 {
253  _xtimer_set_msg64(timer, _xtimer_ticks_from_usec64(offset), msg, target_pid);
254 }
255 #endif /* MODULE_CORE_MSG */
256 
257 static inline xtimer_ticks32_t xtimer_ticks_from_usec(uint32_t usec)
258 {
259  xtimer_ticks32_t ticks;
260  ticks.ticks32 = _xtimer_ticks_from_usec(usec);
261  return ticks;
262 }
263 
264 static inline xtimer_ticks64_t xtimer_ticks_from_usec64(uint64_t usec)
265 {
266  xtimer_ticks64_t ticks;
267  ticks.ticks64 = _xtimer_ticks_from_usec64(usec);
268  return ticks;
269 }
270 
271 static inline uint32_t xtimer_usec_from_ticks(xtimer_ticks32_t ticks)
272 {
273  return _xtimer_usec_from_ticks(ticks.ticks32);
274 }
275 
276 static inline uint64_t xtimer_usec_from_ticks64(xtimer_ticks64_t ticks)
277 {
278  return _xtimer_usec_from_ticks64(ticks.ticks64);
279 }
280 
281 static inline xtimer_ticks32_t xtimer_ticks(uint32_t ticks)
282 {
283  xtimer_ticks32_t ret;
284  ret.ticks32 = ticks;
285  return ret;
286 }
287 
288 static inline xtimer_ticks64_t xtimer_ticks64(uint64_t ticks)
289 {
290  xtimer_ticks64_t ret;
291  ret.ticks64 = ticks;
292  return ret;
293 }
294 
296 {
297  xtimer_ticks32_t ret;
298  ret.ticks32 = a.ticks32 - b.ticks32;
299  return ret;
300 }
301 
303 {
304  xtimer_ticks64_t ret;
305  ret.ticks64 = a.ticks64 - b.ticks64;
306  return ret;
307 }
308 
310 {
311  uint64_t diff = a.ticks64 - b.ticks64;
312  xtimer_ticks32_t ret;
313  ret.ticks32 = diff;
314  return ret;
315 }
316 
317 static inline bool xtimer_less(xtimer_ticks32_t a, xtimer_ticks32_t b)
318 {
319  return (a.ticks32 < b.ticks32);
320 }
321 
322 static inline bool xtimer_less64(xtimer_ticks64_t a, xtimer_ticks64_t b)
323 {
324  return (a.ticks64 < b.ticks64);
325 }
326 
327 static inline bool xtimer_is_set(const xtimer_t *timer)
328 {
329  return timer->offset || timer->long_offset;
330 }
331 
332 #endif /* !defined(DOXYGEN) */
333 
334 #ifdef __cplusplus
335 }
336 #endif
337 
338 #endif /* XTIMER_IMPLEMENTATION_H */
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:139
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: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
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:583
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:617
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:683
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:196
xtimer timestamp (32 bit)
Definition: xtimer.h:85
uint32_t ticks32
Tick count.
Definition: xtimer.h:86
xtimer timestamp (64 bit)
Definition: xtimer.h:76
uint64_t ticks64
Tick count.
Definition: xtimer.h:77
xtimer timer structure
Definition: xtimer.h:97
uint32_t long_offset
upper 32bit offset time
Definition: xtimer.h:100
uint32_t offset
lower 32bit offset time
Definition: xtimer.h:99
ztimer API