implementation.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Kaspar Schleiser <kaspar@schleiser.de>
3  * Copyright (C) 2016 Eistec AB
4  *
5  * This file is subject to the terms and conditions of the GNU Lesser
6  * General Public License v2.1. See the file LICENSE in the top level
7  * directory for more details.
8  */
9 
19 #ifndef XTIMER_IMPLEMENTATION_H
20 #define XTIMER_IMPLEMENTATION_H
21 
22 #ifndef XTIMER_H
23 #error "Do not include this file directly! Use xtimer.h instead"
24 #endif
25 
26 #include "periph/timer.h"
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 #if XTIMER_MASK
33 extern volatile uint32_t _xtimer_high_cnt;
34 #endif
35 
39 #define MSG_XTIMER 12345
40 
44 static inline uint32_t _xtimer_lltimer_now(void)
45 {
46  return timer_read(XTIMER_DEV);
47 }
48 
52 static inline uint32_t _xtimer_lltimer_mask(uint32_t val)
53 {
54  /* cppcheck-suppress shiftTooManyBits
55  * (reason: cppcheck bug. `XTIMER_MASK` is zero when `XTIMER_WIDTH` is 32) */
56  return val & ~XTIMER_MASK;
57 }
58 
64 uint64_t _xtimer_now64(void);
65 int _xtimer_set_absolute(xtimer_t *timer, uint32_t target);
66 void _xtimer_set64(xtimer_t *timer, uint32_t offset, uint32_t long_offset);
67 void _xtimer_set(xtimer_t *timer, uint32_t offset);
68 void _xtimer_periodic_wakeup(uint32_t *last_wakeup, uint32_t period);
69 void _xtimer_set_msg(xtimer_t *timer, uint32_t offset, msg_t *msg, kernel_pid_t target_pid);
70 void _xtimer_set_msg64(xtimer_t *timer, uint64_t offset, msg_t *msg, kernel_pid_t target_pid);
71 void _xtimer_set_wakeup(xtimer_t *timer, uint32_t offset, kernel_pid_t pid);
72 void _xtimer_set_wakeup64(xtimer_t *timer, uint64_t offset, kernel_pid_t pid);
73 void _xtimer_set(xtimer_t *timer, uint32_t offset);
74 int _xtimer_msg_receive_timeout(msg_t *msg, uint32_t ticks);
75 int _xtimer_msg_receive_timeout64(msg_t *msg, uint64_t ticks);
76 
80 void _xtimer_tsleep(uint32_t offset, uint32_t long_offset);
83 #ifndef XTIMER_MIN_SPIN
84 
87 #define XTIMER_MIN_SPIN _xtimer_usec_from_ticks(1)
88 #endif
89 
90 #ifndef DOXYGEN
91 /* Doxygen warns that these are undocumented, but the documentation can be found in xtimer.h */
92 
93 static inline uint32_t _xtimer_now(void)
94 {
95 #if XTIMER_MASK
96  uint32_t latched_high_cnt, now;
97 
98  /* _high_cnt can change at any time, so check the value before
99  * and after reading the low-level timer. If it hasn't changed,
100  * then it can be safely applied to the timer count. */
101 
102  do {
103  latched_high_cnt = _xtimer_high_cnt;
104  now = _xtimer_lltimer_now();
105  } while (_xtimer_high_cnt != latched_high_cnt);
106 
107  return latched_high_cnt | now;
108 #else
109  return _xtimer_lltimer_now();
110 #endif
111 }
112 
113 static inline xtimer_ticks32_t xtimer_now(void)
114 {
115  xtimer_ticks32_t ret;
116  ret.ticks32 = _xtimer_now();
117  return ret;
118 }
119 
120 static inline xtimer_ticks64_t xtimer_now64(void)
121 {
122  xtimer_ticks64_t ret;
123  ret.ticks64 = _xtimer_now64();
124  return ret;
125 }
126 
127 static inline uint32_t xtimer_now_usec(void)
128 {
130 }
131 
132 static inline uint64_t xtimer_now_usec64(void)
133 {
135 }
136 
137 static inline void _xtimer_spin(uint32_t offset) {
138  uint32_t start = _xtimer_lltimer_now();
139 #if XTIMER_MASK
140  offset = _xtimer_lltimer_mask(offset);
141  while (_xtimer_lltimer_mask(_xtimer_lltimer_now() - start) < offset);
142 #else
143  while ((_xtimer_lltimer_now() - start) < offset);
144 #endif
145 }
146 
147 static inline void _xtimer_tsleep32(uint32_t ticks)
148 {
149  _xtimer_tsleep(ticks, 0);
150 }
151 
152 static inline void _xtimer_tsleep64(uint64_t ticks)
153 {
154  _xtimer_tsleep((uint32_t)ticks, (uint32_t)(ticks >> 32));
155 }
156 
157 static inline void xtimer_spin(xtimer_ticks32_t ticks) {
158  _xtimer_spin(ticks.ticks32);
159 }
160 
161 static inline void xtimer_usleep(uint32_t microseconds)
162 {
163  _xtimer_tsleep32(_xtimer_ticks_from_usec(microseconds));
164 }
165 
166 static inline void xtimer_usleep64(uint64_t microseconds)
167 {
168  _xtimer_tsleep64(_xtimer_ticks_from_usec64(microseconds));
169 }
170 
171 static inline void xtimer_sleep(uint32_t seconds)
172 {
173  _xtimer_tsleep64(_xtimer_ticks_from_usec64((uint64_t)seconds * US_PER_SEC));
174 }
175 
176 static inline void xtimer_nanosleep(uint32_t nanoseconds)
177 {
178  _xtimer_tsleep32(_xtimer_ticks_from_usec(nanoseconds / NS_PER_US));
179 }
180 
181 static inline void xtimer_tsleep32(xtimer_ticks32_t ticks)
182 {
183  _xtimer_tsleep32(ticks.ticks32);
184 }
185 
186 static inline void xtimer_tsleep64(xtimer_ticks64_t ticks)
187 {
188  _xtimer_tsleep64(ticks.ticks64);
189 }
190 
191 static inline void xtimer_periodic_wakeup(xtimer_ticks32_t *last_wakeup, uint32_t period)
192 {
193  _xtimer_periodic_wakeup(&last_wakeup->ticks32, _xtimer_ticks_from_usec(period));
194 }
195 
196 static inline void xtimer_set_msg(xtimer_t *timer, uint32_t offset, msg_t *msg, kernel_pid_t target_pid)
197 {
198  _xtimer_set_msg(timer, _xtimer_ticks_from_usec(offset), msg, target_pid);
199 }
200 
201 static inline void xtimer_set_msg64(xtimer_t *timer, uint64_t offset, msg_t *msg, kernel_pid_t target_pid)
202 {
203  _xtimer_set_msg64(timer, _xtimer_ticks_from_usec64(offset), msg, target_pid);
204 }
205 
206 static inline void xtimer_set_wakeup(xtimer_t *timer, uint32_t offset, kernel_pid_t pid)
207 {
208  _xtimer_set_wakeup(timer, _xtimer_ticks_from_usec(offset), pid);
209 }
210 
211 static inline void xtimer_set_wakeup64(xtimer_t *timer, uint64_t offset, kernel_pid_t pid)
212 {
213  _xtimer_set_wakeup64(timer, _xtimer_ticks_from_usec64(offset), pid);
214 }
215 
216 static inline void xtimer_set(xtimer_t *timer, uint32_t offset)
217 {
218  _xtimer_set(timer, _xtimer_ticks_from_usec(offset));
219 }
220 
221 static inline int xtimer_msg_receive_timeout(msg_t *msg, uint32_t timeout)
222 {
223  return _xtimer_msg_receive_timeout(msg, _xtimer_ticks_from_usec(timeout));
224 }
225 
226 static inline int xtimer_msg_receive_timeout64(msg_t *msg, uint64_t timeout)
227 {
228  return _xtimer_msg_receive_timeout64(msg, _xtimer_ticks_from_usec64(timeout));
229 }
230 
231 static inline xtimer_ticks32_t xtimer_ticks_from_usec(uint32_t usec)
232 {
233  xtimer_ticks32_t ticks;
234  ticks.ticks32 = _xtimer_ticks_from_usec(usec);
235  return ticks;
236 }
237 
238 static inline xtimer_ticks64_t xtimer_ticks_from_usec64(uint64_t usec)
239 {
240  xtimer_ticks64_t ticks;
241  ticks.ticks64 = _xtimer_ticks_from_usec64(usec);
242  return ticks;
243 }
244 
245 static inline uint32_t xtimer_usec_from_ticks(xtimer_ticks32_t ticks)
246 {
247  return _xtimer_usec_from_ticks(ticks.ticks32);
248 }
249 
250 static inline uint64_t xtimer_usec_from_ticks64(xtimer_ticks64_t ticks)
251 {
252  return _xtimer_usec_from_ticks64(ticks.ticks64);
253 }
254 
255 static inline xtimer_ticks32_t xtimer_ticks(uint32_t ticks)
256 {
257  xtimer_ticks32_t ret;
258  ret.ticks32 = ticks;
259  return ret;
260 }
261 
262 static inline xtimer_ticks64_t xtimer_ticks64(uint64_t ticks)
263 {
264  xtimer_ticks64_t ret;
265  ret.ticks64 = ticks;
266  return ret;
267 }
268 
270 {
271  xtimer_ticks32_t ret;
272  ret.ticks32 = a.ticks32 - b.ticks32;
273  return ret;
274 }
275 
277 {
278  xtimer_ticks64_t ret;
279  ret.ticks64 = a.ticks64 - b.ticks64;
280  return ret;
281 }
282 
284 {
285  uint64_t diff = a.ticks64 - b.ticks64;
286  xtimer_ticks32_t ret;
287  ret.ticks32 = diff;
288  return ret;
289 }
290 
291 static inline bool xtimer_less(xtimer_ticks32_t a, xtimer_ticks32_t b)
292 {
293  return (a.ticks32 < b.ticks32);
294 }
295 
296 static inline bool xtimer_less64(xtimer_ticks64_t a, xtimer_ticks64_t b)
297 {
298  return (a.ticks64 < b.ticks64);
299 }
300 
301 #endif /* !defined(DOXYGEN) */
302 
303 #ifdef __cplusplus
304 }
305 #endif
306 
307 #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)...
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.
Low-level timer peripheral driver interface definitions.
#define XTIMER_MASK
xtimer timer mask
Definition: xtimer.h:564
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 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.
#define XTIMER_DEV
xtimer configuration
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