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 #include "periph/timer.h"
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 #if XTIMER_MASK
37 extern volatile uint32_t _xtimer_high_cnt;
38 #endif
39 
43 #define MSG_XTIMER 12345
44 
48 static inline uint32_t _xtimer_lltimer_now(void)
49 {
50  return timer_read(XTIMER_DEV);
51 }
52 
56 static inline uint32_t _xtimer_lltimer_mask(uint32_t val)
57 {
58  /* cppcheck-suppress shiftTooManyBits
59  * (reason: cppcheck bug. `XTIMER_MASK` is zero when `XTIMER_WIDTH` is 32) */
60  return val & ~XTIMER_MASK;
61 }
62 
68 uint64_t _xtimer_now64(void);
69 int _xtimer_set_absolute(xtimer_t *timer, uint32_t target);
70 void _xtimer_set(xtimer_t *timer, uint32_t offset);
71 void _xtimer_set64(xtimer_t *timer, uint32_t offset, uint32_t long_offset);
72 void _xtimer_periodic_wakeup(uint32_t *last_wakeup, uint32_t period);
73 void _xtimer_set_msg(xtimer_t *timer, uint32_t offset, msg_t *msg, kernel_pid_t target_pid);
74 void _xtimer_set_msg64(xtimer_t *timer, uint64_t offset, msg_t *msg, kernel_pid_t target_pid);
75 void _xtimer_set_wakeup(xtimer_t *timer, uint32_t offset, kernel_pid_t pid);
76 void _xtimer_set_wakeup64(xtimer_t *timer, uint64_t offset, kernel_pid_t pid);
77 int _xtimer_msg_receive_timeout(msg_t *msg, uint32_t ticks);
78 int _xtimer_msg_receive_timeout64(msg_t *msg, uint64_t ticks);
79 
83 void _xtimer_tsleep(uint32_t offset, uint32_t long_offset);
86 #ifndef XTIMER_MIN_SPIN
87 
90 #define XTIMER_MIN_SPIN _xtimer_usec_from_ticks(1)
91 #endif
92 
93 #ifndef DOXYGEN
94 /* Doxygen warns that these are undocumented, but the documentation can be found in xtimer.h */
95 
96 static inline uint32_t _xtimer_now(void)
97 {
98 #if XTIMER_MASK
99  uint32_t latched_high_cnt, now;
100 
101  /* _high_cnt can change at any time, so check the value before
102  * and after reading the low-level timer. If it hasn't changed,
103  * then it can be safely applied to the timer count. */
104 
105  do {
106  latched_high_cnt = _xtimer_high_cnt;
107  now = _xtimer_lltimer_now();
108  } while (_xtimer_high_cnt != latched_high_cnt);
109 
110  return latched_high_cnt | now;
111 #else
112  return _xtimer_lltimer_now();
113 #endif
114 }
115 
116 static inline xtimer_ticks32_t xtimer_now(void)
117 {
118  xtimer_ticks32_t ret;
119  ret.ticks32 = _xtimer_now();
120  return ret;
121 }
122 
123 static inline xtimer_ticks64_t xtimer_now64(void)
124 {
125  xtimer_ticks64_t ret;
126  ret.ticks64 = _xtimer_now64();
127  return ret;
128 }
129 
130 static inline uint32_t xtimer_now_usec(void)
131 {
133 }
134 
135 static inline uint64_t xtimer_now_usec64(void)
136 {
138 }
139 
140 static inline void _xtimer_spin(uint32_t offset) {
141  uint32_t start = _xtimer_lltimer_now();
142 #if XTIMER_MASK
143  offset = _xtimer_lltimer_mask(offset);
144  while (_xtimer_lltimer_mask(_xtimer_lltimer_now() - start) < offset);
145 #else
146  while ((_xtimer_lltimer_now() - start) < offset);
147 #endif
148 }
149 
150 static inline void _xtimer_tsleep32(uint32_t ticks)
151 {
152  _xtimer_tsleep(ticks, 0);
153 }
154 
155 static inline void _xtimer_tsleep64(uint64_t ticks)
156 {
157  _xtimer_tsleep((uint32_t)ticks, (uint32_t)(ticks >> 32));
158 }
159 
160 static inline void xtimer_spin(xtimer_ticks32_t ticks) {
161  _xtimer_spin(ticks.ticks32);
162 }
163 
164 static inline void xtimer_usleep(uint32_t microseconds)
165 {
166  _xtimer_tsleep32(_xtimer_ticks_from_usec(microseconds));
167 }
168 
169 static inline void xtimer_usleep64(uint64_t microseconds)
170 {
171  _xtimer_tsleep64(_xtimer_ticks_from_usec64(microseconds));
172 }
173 
174 static inline void xtimer_sleep(uint32_t seconds)
175 {
176  _xtimer_tsleep64(_xtimer_ticks_from_usec64((uint64_t)seconds * US_PER_SEC));
177 }
178 
179 static inline void xtimer_nanosleep(uint32_t nanoseconds)
180 {
181  _xtimer_tsleep32(_xtimer_ticks_from_usec(nanoseconds / NS_PER_US));
182 }
183 
184 static inline void xtimer_tsleep32(xtimer_ticks32_t ticks)
185 {
186  _xtimer_tsleep32(ticks.ticks32);
187 }
188 
189 static inline void xtimer_tsleep64(xtimer_ticks64_t ticks)
190 {
191  _xtimer_tsleep64(ticks.ticks64);
192 }
193 
194 static inline void xtimer_periodic_wakeup(xtimer_ticks32_t *last_wakeup, uint32_t period)
195 {
196  _xtimer_periodic_wakeup(&last_wakeup->ticks32, _xtimer_ticks_from_usec(period));
197 }
198 
199 static inline void xtimer_set_msg(xtimer_t *timer, uint32_t offset, msg_t *msg, kernel_pid_t target_pid)
200 {
201  _xtimer_set_msg(timer, _xtimer_ticks_from_usec(offset), msg, target_pid);
202 }
203 
204 static inline void xtimer_set_msg64(xtimer_t *timer, uint64_t offset, msg_t *msg, kernel_pid_t target_pid)
205 {
206  _xtimer_set_msg64(timer, _xtimer_ticks_from_usec64(offset), msg, target_pid);
207 }
208 
209 static inline void xtimer_set_wakeup(xtimer_t *timer, uint32_t offset, kernel_pid_t pid)
210 {
211  _xtimer_set_wakeup(timer, _xtimer_ticks_from_usec(offset), pid);
212 }
213 
214 static inline void xtimer_set_wakeup64(xtimer_t *timer, uint64_t offset, kernel_pid_t pid)
215 {
216  _xtimer_set_wakeup64(timer, _xtimer_ticks_from_usec64(offset), pid);
217 }
218 
219 static inline void xtimer_set(xtimer_t *timer, uint32_t offset)
220 {
221  _xtimer_set(timer, _xtimer_ticks_from_usec(offset));
222 }
223 
224 static inline void xtimer_set64(xtimer_t *timer, uint64_t period_us)
225 {
226  uint64_t ticks = _xtimer_ticks_from_usec64(period_us);
227  _xtimer_set64(timer, ticks, ticks >> 32);
228 }
229 
230 static inline int xtimer_msg_receive_timeout(msg_t *msg, uint32_t timeout)
231 {
232  return _xtimer_msg_receive_timeout(msg, _xtimer_ticks_from_usec(timeout));
233 }
234 
235 static inline int xtimer_msg_receive_timeout64(msg_t *msg, uint64_t timeout)
236 {
237  return _xtimer_msg_receive_timeout64(msg, _xtimer_ticks_from_usec64(timeout));
238 }
239 
240 static inline xtimer_ticks32_t xtimer_ticks_from_usec(uint32_t usec)
241 {
242  xtimer_ticks32_t ticks;
243  ticks.ticks32 = _xtimer_ticks_from_usec(usec);
244  return ticks;
245 }
246 
247 static inline xtimer_ticks64_t xtimer_ticks_from_usec64(uint64_t usec)
248 {
249  xtimer_ticks64_t ticks;
250  ticks.ticks64 = _xtimer_ticks_from_usec64(usec);
251  return ticks;
252 }
253 
254 static inline uint32_t xtimer_usec_from_ticks(xtimer_ticks32_t ticks)
255 {
256  return _xtimer_usec_from_ticks(ticks.ticks32);
257 }
258 
259 static inline uint64_t xtimer_usec_from_ticks64(xtimer_ticks64_t ticks)
260 {
261  return _xtimer_usec_from_ticks64(ticks.ticks64);
262 }
263 
264 static inline xtimer_ticks32_t xtimer_ticks(uint32_t ticks)
265 {
266  xtimer_ticks32_t ret;
267  ret.ticks32 = ticks;
268  return ret;
269 }
270 
271 static inline xtimer_ticks64_t xtimer_ticks64(uint64_t ticks)
272 {
273  xtimer_ticks64_t ret;
274  ret.ticks64 = ticks;
275  return ret;
276 }
277 
279 {
280  xtimer_ticks32_t ret;
281  ret.ticks32 = a.ticks32 - b.ticks32;
282  return ret;
283 }
284 
286 {
287  xtimer_ticks64_t ret;
288  ret.ticks64 = a.ticks64 - b.ticks64;
289  return ret;
290 }
291 
293 {
294  uint64_t diff = a.ticks64 - b.ticks64;
295  xtimer_ticks32_t ret;
296  ret.ticks32 = diff;
297  return ret;
298 }
299 
300 static inline bool xtimer_less(xtimer_ticks32_t a, xtimer_ticks32_t b)
301 {
302  return (a.ticks32 < b.ticks32);
303 }
304 
305 static inline bool xtimer_less64(xtimer_ticks64_t a, xtimer_ticks64_t b)
306 {
307  return (a.ticks64 < b.ticks64);
308 }
309 
310 #endif /* !defined(DOXYGEN) */
311 
312 #ifdef __cplusplus
313 }
314 #endif
315 
316 #endif /* XTIMER_IMPLEMENTATION_H */
static xtimer_ticks64_t xtimer_ticks64(uint64_t ticks)
Create an xtimer time stamp, 64 bit version.
static uint64_t xtimer_usec_from_ticks64(xtimer_ticks64_t ticks)
Convert xtimer ticks to microseconds, 64 bit version.
uint64_t _xtimer_now64(void)
xtimer internal stuff
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_tsleep32(xtimer_ticks32_t ticks)
Stop execution of a thread for some time, 32bit version.
int16_t kernel_pid_t
Unique process identifier.
Definition: kernel_types.h:83
static uint64_t xtimer_now_usec64(void)
get the current system time in microseconds since start
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)...
#define XTIMER_DEV
Underlying hardware timer device to assign to xtimer.
Definition: xtimer.h:551
Low-level timer peripheral driver interface definitions.
static uint32_t xtimer_now_usec(void)
get the current system time in microseconds since start
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 uint32_t _xtimer_lltimer_now(void)
returns the (masked) low-level timer counter value.
static xtimer_ticks64_t xtimer_now64(void)
get the current system time as 64bit time stamp
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.
#define XTIMER_MASK
xtimer timer mask
Definition: xtimer.h:585
unsigned int timer_read(tim_t dev)
Read the current value of the given timer device.
static void xtimer_usleep(uint32_t microseconds)
Pause the execution of a thread for some microseconds.
static void xtimer_nanosleep(uint32_t nanoseconds)
Stop execution of a thread for some time.
static xtimer_ticks32_t xtimer_ticks_from_usec(uint32_t usec)
Convert microseconds to xtimer ticks.
#define NS_PER_US
The number of nanoseconds per microsecond.
Definition: timex.h:59
uint64_t ticks64
Tick count.
Definition: xtimer.h:50
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.
uint32_t ticks32
Tick count.
Definition: xtimer.h:59
static void xtimer_spin(xtimer_ticks32_t ticks)
Stop execution of a thread for some time, blocking.
Describes a message object which can be sent between threads.
Definition: msg.h:184
static void xtimer_tsleep64(xtimer_ticks64_t ticks)
Stop execution of a thread for some time, 64bit version.
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 xtimer_ticks64_t xtimer_ticks_from_usec64(uint64_t usec)
Convert microseconds to xtimer ticks, 64 bit version.
static xtimer_ticks32_t xtimer_diff(xtimer_ticks32_t a, xtimer_ticks32_t b)
Compute difference between two xtimer time stamps.
static void xtimer_set(xtimer_t *timer, uint32_t offset)
Set a timer to execute a callback at some time in the future.
static void xtimer_sleep(uint32_t seconds)
Pause the execution of a thread for some seconds.
void _xtimer_tsleep(uint32_t offset, uint32_t long_offset)
Sleep for the given number of ticks.
static xtimer_ticks32_t xtimer_ticks(uint32_t ticks)
Create an xtimer time stamp.
#define US_PER_SEC
The number of microseconds per second.
Definition: timex.h:34
static bool xtimer_less64(xtimer_ticks64_t a, xtimer_ticks64_t b)
Compare two xtimer time stamps, 64 bit version.
xtimer timer structure
Definition: xtimer.h:70
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 int xtimer_msg_receive_timeout64(msg_t *msg, uint64_t timeout)
receive a message blocking but with timeout, 64bit version
static uint32_t _xtimer_lltimer_mask(uint32_t val)
drop bits of a value that don&#39;t fit into the low-level timer.
static bool xtimer_less(xtimer_ticks32_t a, xtimer_ticks32_t b)
Compare two xtimer time stamps.
xtimer timestamp (32 bit)
Definition: xtimer.h:58
xtimer timestamp (64 bit)
Definition: xtimer.h:49
static uint32_t xtimer_usec_from_ticks(xtimer_ticks32_t ticks)
Convert xtimer ticks to microseconds.
static void xtimer_set_wakeup(xtimer_t *timer, uint32_t offset, kernel_pid_t pid)
Set a timer that wakes up a thread.
static int xtimer_msg_receive_timeout(msg_t *msg, uint32_t timeout)
receive a message blocking but with timeout
static xtimer_ticks32_t xtimer_now(void)
get the current system time as 32bit time stamp value