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 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 extern volatile uint64_t _xtimer_current_time;
44 
48 #define MSG_XTIMER 12345
49 
53 static inline uint32_t _xtimer_lltimer_now(void)
54 {
55 #ifndef MODULE_XTIMER_ON_ZTIMER
56  return timer_read(XTIMER_DEV);
57 #else
58  return ztimer_now(ZTIMER_USEC);
59 #endif
60 
61 }
62 
66 static inline uint32_t _xtimer_lltimer_mask(uint32_t val)
67 {
68  /* cppcheck-suppress shiftTooManyBits
69  * (reason: cppcheck bug. `XTIMER_MASK` is zero when `XTIMER_WIDTH` is 32) */
70  return val & ~XTIMER_MASK;
71 }
72 
79 uint32_t _xtimer_now(void);
80 
81 void _xtimer_set64(xtimer_t *timer, uint32_t offset, uint32_t long_offset);
82 void _xtimer_periodic_wakeup(uint32_t *last_wakeup, uint32_t period);
83 void _xtimer_set_wakeup(xtimer_t *timer, uint32_t offset, kernel_pid_t pid);
84 void _xtimer_set_wakeup64(xtimer_t *timer, uint64_t offset, kernel_pid_t pid);
85 #ifdef MODULE_CORE_MSG
86 int _xtimer_msg_receive_timeout(msg_t *msg, uint32_t ticks);
87 int _xtimer_msg_receive_timeout64(msg_t *msg, uint64_t ticks);
88 void _xtimer_set_msg(xtimer_t *timer, uint32_t offset, msg_t *msg, kernel_pid_t target_pid);
89 void _xtimer_set_msg64(xtimer_t *timer, uint64_t offset, msg_t *msg, kernel_pid_t target_pid);
90 #endif /* MODULE_CORE_MSG */
91 
95 void _xtimer_tsleep(uint32_t offset, uint32_t long_offset);
98 #ifndef XTIMER_MIN_SPIN
99 
102 #define XTIMER_MIN_SPIN _xtimer_usec_from_ticks(1)
103 #endif
104 
105 #ifndef DOXYGEN
106 /* Doxygen warns that these are undocumented, but the documentation can be found in xtimer.h */
107 
108 static inline uint64_t _xtimer_now64(void)
109 {
110  uint32_t now, elapsed;
111 
112  /* time sensitive since _xtimer_current_time is updated here */
113  unsigned state = irq_disable();
114  now = _xtimer_lltimer_now();
115 #if XTIMER_MASK
116  elapsed = _xtimer_lltimer_mask(now - _xtimer_lltimer_mask((uint32_t)_xtimer_current_time));
117  _xtimer_current_time += (uint64_t)elapsed;
118 #else
119  elapsed = now - ((uint32_t)_xtimer_current_time & 0xFFFFFFFF);
120  _xtimer_current_time += (uint64_t)elapsed;
121 #endif
122  irq_restore(state);
123 
124  return _xtimer_current_time;
125 }
126 
127 static inline xtimer_ticks32_t xtimer_now(void)
128 {
129  xtimer_ticks32_t ret;
130  ret.ticks32 = _xtimer_now();
131  return ret;
132 }
133 
134 static inline xtimer_ticks64_t xtimer_now64(void)
135 {
136  xtimer_ticks64_t ret;
137  ret.ticks64 = _xtimer_now64();
138  return ret;
139 }
140 
141 static inline uint32_t xtimer_now_usec(void)
142 {
144 }
145 
146 static inline uint64_t xtimer_now_usec64(void)
147 {
149 }
150 
151 static inline void _xtimer_spin(uint32_t offset) {
152  uint32_t start = _xtimer_lltimer_now();
153 #if XTIMER_MASK
155  while (_xtimer_lltimer_mask(_xtimer_lltimer_now() - start) < offset);
156 #else
157  while ((_xtimer_lltimer_now() - start) < offset);
158 #endif
159 }
160 
161 static inline void _xtimer_tsleep32(uint32_t ticks)
162 {
163  _xtimer_tsleep(ticks, 0);
164 }
165 
166 static inline void _xtimer_tsleep64(uint64_t ticks)
167 {
168  _xtimer_tsleep((uint32_t)ticks, (uint32_t)(ticks >> 32));
169 }
170 
171 static inline void xtimer_spin(xtimer_ticks32_t ticks) {
172  _xtimer_spin(ticks.ticks32);
173 }
174 
175 static inline void xtimer_msleep(uint32_t milliseconds)
176 {
177  _xtimer_tsleep64(_xtimer_ticks_from_usec64(milliseconds * US_PER_MS));
178 }
179 
180 static inline void xtimer_usleep(uint32_t microseconds)
181 {
182  _xtimer_tsleep32(_xtimer_ticks_from_usec(microseconds));
183 }
184 
185 static inline void xtimer_usleep64(uint64_t microseconds)
186 {
187  _xtimer_tsleep64(_xtimer_ticks_from_usec64(microseconds));
188 }
189 
190 static inline void xtimer_sleep(uint32_t seconds)
191 {
192  _xtimer_tsleep64(_xtimer_ticks_from_usec64((uint64_t)seconds * US_PER_SEC));
193 }
194 
195 static inline void xtimer_nanosleep(uint32_t nanoseconds)
196 {
197  _xtimer_tsleep32(_xtimer_ticks_from_usec(nanoseconds / NS_PER_US));
198 }
199 
200 static inline void xtimer_tsleep32(xtimer_ticks32_t ticks)
201 {
202  _xtimer_tsleep32(ticks.ticks32);
203 }
204 
205 static inline void xtimer_tsleep64(xtimer_ticks64_t ticks)
206 {
207  _xtimer_tsleep64(ticks.ticks64);
208 }
209 
210 static inline void xtimer_periodic_wakeup(xtimer_ticks32_t *last_wakeup, uint32_t period)
211 {
212  _xtimer_periodic_wakeup(&last_wakeup->ticks32, _xtimer_ticks_from_usec(period));
213 }
214 
215 static inline void xtimer_set_wakeup(xtimer_t *timer, uint32_t offset, kernel_pid_t pid)
216 {
217  _xtimer_set_wakeup(timer, _xtimer_ticks_from_usec(offset), pid);
218 }
219 
220 static inline void xtimer_set_wakeup64(xtimer_t *timer, uint64_t offset, kernel_pid_t pid)
221 {
222  _xtimer_set_wakeup64(timer, _xtimer_ticks_from_usec64(offset), pid);
223 }
224 
225 static inline void xtimer_set(xtimer_t *timer, uint32_t offset)
226 {
227  _xtimer_set64(timer, _xtimer_ticks_from_usec(offset), 0);
228 }
229 
230 static inline void xtimer_set64(xtimer_t *timer, uint64_t period_us)
231 {
232  uint64_t ticks = _xtimer_ticks_from_usec64(period_us);
233  _xtimer_set64(timer, ticks, ticks >> 32);
234 }
235 
236 #ifdef MODULE_CORE_MSG
237 static inline int xtimer_msg_receive_timeout(msg_t *msg, uint32_t timeout)
238 {
239  return _xtimer_msg_receive_timeout(msg, _xtimer_ticks_from_usec(timeout));
240 }
241 
242 static inline int xtimer_msg_receive_timeout64(msg_t *msg, uint64_t timeout)
243 {
244  return _xtimer_msg_receive_timeout64(msg, _xtimer_ticks_from_usec64(timeout));
245 }
246 
247 static inline void xtimer_set_msg(xtimer_t *timer, uint32_t offset, msg_t *msg, kernel_pid_t target_pid)
248 {
249  _xtimer_set_msg(timer, _xtimer_ticks_from_usec(offset), msg, target_pid);
250 }
251 
252 static inline void xtimer_set_msg64(xtimer_t *timer, uint64_t offset, msg_t *msg, kernel_pid_t target_pid)
253 {
254  _xtimer_set_msg64(timer, _xtimer_ticks_from_usec64(offset), msg, target_pid);
255 }
256 #endif /* MODULE_CORE_MSG */
257 
258 static inline xtimer_ticks32_t xtimer_ticks_from_usec(uint32_t usec)
259 {
260  xtimer_ticks32_t ticks;
261  ticks.ticks32 = _xtimer_ticks_from_usec(usec);
262  return ticks;
263 }
264 
265 static inline xtimer_ticks64_t xtimer_ticks_from_usec64(uint64_t usec)
266 {
267  xtimer_ticks64_t ticks;
268  ticks.ticks64 = _xtimer_ticks_from_usec64(usec);
269  return ticks;
270 }
271 
272 static inline uint32_t xtimer_usec_from_ticks(xtimer_ticks32_t ticks)
273 {
274  return _xtimer_usec_from_ticks(ticks.ticks32);
275 }
276 
277 static inline uint64_t xtimer_usec_from_ticks64(xtimer_ticks64_t ticks)
278 {
279  return _xtimer_usec_from_ticks64(ticks.ticks64);
280 }
281 
282 static inline xtimer_ticks32_t xtimer_ticks(uint32_t ticks)
283 {
284  xtimer_ticks32_t ret;
285  ret.ticks32 = ticks;
286  return ret;
287 }
288 
289 static inline xtimer_ticks64_t xtimer_ticks64(uint64_t ticks)
290 {
291  xtimer_ticks64_t ret;
292  ret.ticks64 = ticks;
293  return ret;
294 }
295 
297 {
298  xtimer_ticks32_t ret;
299  ret.ticks32 = a.ticks32 - b.ticks32;
300  return ret;
301 }
302 
304 {
305  xtimer_ticks64_t ret;
306  ret.ticks64 = a.ticks64 - b.ticks64;
307  return ret;
308 }
309 
311 {
312  uint64_t diff = a.ticks64 - b.ticks64;
313  xtimer_ticks32_t ret;
314  ret.ticks32 = diff;
315  return ret;
316 }
317 
318 static inline bool xtimer_less(xtimer_ticks32_t a, xtimer_ticks32_t b)
319 {
320  return (a.ticks32 < b.ticks32);
321 }
322 
323 static inline bool xtimer_less64(xtimer_ticks64_t a, xtimer_ticks64_t b)
324 {
325  return (a.ticks64 < b.ticks64);
326 }
327 
328 #endif /* !defined(DOXYGEN) */
329 
330 #ifdef __cplusplus
331 }
332 #endif
333 
334 #endif /* XTIMER_IMPLEMENTATION_H */
kernel_pid_t
int16_t kernel_pid_t
Unique process identifier.
Definition: sched.h:125
xtimer
xtimer timer structure
Definition: xtimer.h:81
xtimer_periodic_wakeup
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).
xtimer_less
static bool xtimer_less(xtimer_ticks32_t a, xtimer_ticks32_t b)
Compare two xtimer time stamps.
xtimer_usleep64
static void xtimer_usleep64(uint64_t microseconds)
Pause the execution of a thread for some microseconds.
NS_PER_US
#define NS_PER_US
The number of nanoseconds per microsecond.
Definition: timex.h:64
xtimer_msg_receive_timeout64
static int xtimer_msg_receive_timeout64(msg_t *msg, uint64_t timeout)
receive a message blocking but with timeout, 64bit version
timer.h
Low-level timer peripheral driver interface definitions.
irq_disable
MAYBE_INLINE unsigned irq_disable(void)
This function sets the IRQ disable bit in the status register.
xtimer_ticks64
static xtimer_ticks64_t xtimer_ticks64(uint64_t ticks)
Create an xtimer time stamp, 64 bit version.
xtimer_ticks32_t::ticks32
uint32_t ticks32
Tick count.
Definition: xtimer.h:70
ztimer.h
ztimer API
xtimer_now64
static xtimer_ticks64_t xtimer_now64(void)
get the current system time as 64bit time stamp
xtimer_set_wakeup64
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.
xtimer_now
static xtimer_ticks32_t xtimer_now(void)
get the current system time as 32bit time stamp value
xtimer_now_usec
static uint32_t xtimer_now_usec(void)
get the current system time in microseconds since start
xtimer_msleep
static void xtimer_msleep(uint32_t milliseconds)
Pause the execution of a thread for some milliseconds.
_xtimer_lltimer_now
static uint32_t _xtimer_lltimer_now(void)
returns the (masked) low-level timer counter value.
Definition: implementation.h:53
xtimer_set_msg64
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.
xtimer_diff32_64
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.
xtimer_tsleep32
static void xtimer_tsleep32(xtimer_ticks32_t ticks)
Stop execution of a thread for some time, 32bit version.
xtimer_ticks_from_usec
static xtimer_ticks32_t xtimer_ticks_from_usec(uint32_t usec)
Convert microseconds to xtimer ticks.
xtimer_ticks_from_usec64
static xtimer_ticks64_t xtimer_ticks_from_usec64(uint64_t usec)
Convert microseconds to xtimer ticks, 64 bit version.
xtimer_diff64
static xtimer_ticks64_t xtimer_diff64(xtimer_ticks64_t a, xtimer_ticks64_t b)
Compute difference between two xtimer time stamps, 64 bit version.
_xtimer_tsleep
void _xtimer_tsleep(uint32_t offset, uint32_t long_offset)
Sleep for the given number of ticks.
_xtimer_now
uint32_t _xtimer_now(void)
xtimer internal stuff
xtimer_set
static void xtimer_set(xtimer_t *timer, uint32_t offset)
Set a timer to execute a callback at some time in the future.
irq_restore
MAYBE_INLINE void irq_restore(unsigned state)
This function restores the IRQ disable bit in the status register to the value contained within passe...
ZTIMER_USEC
ztimer_clock_t *const ZTIMER_USEC
Default ztimer microsecond clock.
XTIMER_MASK
#define XTIMER_MASK
xtimer timer mask
Definition: xtimer.h:590
xtimer_diff
static xtimer_ticks32_t xtimer_diff(xtimer_ticks32_t a, xtimer_ticks32_t b)
Compute difference between two xtimer time stamps.
xtimer_usleep
static void xtimer_usleep(uint32_t microseconds)
Pause the execution of a thread for some microseconds.
xtimer_ticks64_t::ticks64
uint64_t ticks64
Tick count.
Definition: xtimer.h:61
xtimer_ticks
static xtimer_ticks32_t xtimer_ticks(uint32_t ticks)
Create an xtimer time stamp.
_xtimer_lltimer_mask
static uint32_t _xtimer_lltimer_mask(uint32_t val)
drop bits of a value that don't fit into the low-level timer.
Definition: implementation.h:66
xtimer_less64
static bool xtimer_less64(xtimer_ticks64_t a, xtimer_ticks64_t b)
Compare two xtimer time stamps, 64 bit version.
xtimer_set_msg
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.
irq.h
IRQ driver interface.
xtimer_msg_receive_timeout
static int xtimer_msg_receive_timeout(msg_t *msg, uint32_t timeout)
receive a message blocking but with timeout
xtimer_tsleep64
static void xtimer_tsleep64(xtimer_ticks64_t ticks)
Stop execution of a thread for some time, 64bit version.
xtimer_ticks32_t
xtimer timestamp (32 bit)
Definition: xtimer.h:69
timer_read
unsigned int timer_read(tim_t dev)
Read the current value of the given timer device.
xtimer_nanosleep
static void xtimer_nanosleep(uint32_t nanoseconds)
Stop execution of a thread for some time.
xtimer_ticks64_t
xtimer timestamp (64 bit)
Definition: xtimer.h:60
xtimer_usec_from_ticks
static uint32_t xtimer_usec_from_ticks(xtimer_ticks32_t ticks)
Convert xtimer ticks to microseconds.
xtimer_set_wakeup
static void xtimer_set_wakeup(xtimer_t *timer, uint32_t offset, kernel_pid_t pid)
Set a timer that wakes up a thread.
xtimer_set64
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.
xtimer_sleep
static void xtimer_sleep(uint32_t seconds)
Pause the execution of a thread for some seconds.
xtimer::offset
uint32_t offset
lower 32bit offset time
Definition: xtimer.h:83
msg_t
Describes a message object which can be sent between threads.
Definition: msg.h:185
XTIMER_DEV
#define XTIMER_DEV
Underlying hardware timer device to assign to xtimer.
Definition: xtimer.h:556
US_PER_MS
#define US_PER_MS
The number of microseconds per millisecond.
Definition: timex.h:54
xtimer_usec_from_ticks64
static uint64_t xtimer_usec_from_ticks64(xtimer_ticks64_t ticks)
Convert xtimer ticks to microseconds, 64 bit version.
xtimer_spin
static void xtimer_spin(xtimer_ticks32_t ticks)
Stop execution of a thread for some time, blocking.
ztimer_now
static ztimer_now_t ztimer_now(ztimer_clock_t *clock)
Get the current time from a clock.
Definition: ztimer.h:422
xtimer_now_usec64
static uint64_t xtimer_now_usec64(void)
get the current system time in microseconds since start
xtimer::long_offset
uint32_t long_offset
upper 32bit offset time
Definition: xtimer.h:84
US_PER_SEC
#define US_PER_SEC
The number of microseconds per second.
Definition: timex.h:34