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_set(xtimer_t *timer, uint32_t offset);
67 void _xtimer_set64(xtimer_t *timer, uint32_t offset, uint32_t long_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 int _xtimer_msg_receive_timeout(msg_t *msg, uint32_t ticks);
74 int _xtimer_msg_receive_timeout64(msg_t *msg, uint64_t ticks);
75 
79 void _xtimer_tsleep(uint32_t offset, uint32_t long_offset);
82 #ifndef XTIMER_MIN_SPIN
83 
86 #define XTIMER_MIN_SPIN _xtimer_usec_from_ticks(1)
87 #endif
88 
89 #ifndef DOXYGEN
90 /* Doxygen warns that these are undocumented, but the documentation can be found in xtimer.h */
91 
92 static inline uint32_t _xtimer_now(void)
93 {
94 #if XTIMER_MASK
95  uint32_t latched_high_cnt, now;
96 
97  /* _high_cnt can change at any time, so check the value before
98  * and after reading the low-level timer. If it hasn't changed,
99  * then it can be safely applied to the timer count. */
100 
101  do {
102  latched_high_cnt = _xtimer_high_cnt;
103  now = _xtimer_lltimer_now();
104  } while (_xtimer_high_cnt != latched_high_cnt);
105 
106  return latched_high_cnt | now;
107 #else
108  return _xtimer_lltimer_now();
109 #endif
110 }
111 
112 static inline xtimer_ticks32_t xtimer_now(void)
113 {
114  xtimer_ticks32_t ret;
115  ret.ticks32 = _xtimer_now();
116  return ret;
117 }
118 
119 static inline xtimer_ticks64_t xtimer_now64(void)
120 {
121  xtimer_ticks64_t ret;
122  ret.ticks64 = _xtimer_now64();
123  return ret;
124 }
125 
126 static inline uint32_t xtimer_now_usec(void)
127 {
129 }
130 
131 static inline uint64_t xtimer_now_usec64(void)
132 {
134 }
135 
136 static inline void _xtimer_spin(uint32_t offset) {
137  uint32_t start = _xtimer_lltimer_now();
138 #if XTIMER_MASK
139  offset = _xtimer_lltimer_mask(offset);
140  while (_xtimer_lltimer_mask(_xtimer_lltimer_now() - start) < offset);
141 #else
142  while ((_xtimer_lltimer_now() - start) < offset);
143 #endif
144 }
145 
146 static inline void _xtimer_tsleep32(uint32_t ticks)
147 {
148  _xtimer_tsleep(ticks, 0);
149 }
150 
151 static inline void _xtimer_tsleep64(uint64_t ticks)
152 {
153  _xtimer_tsleep((uint32_t)ticks, (uint32_t)(ticks >> 32));
154 }
155 
156 static inline void xtimer_spin(xtimer_ticks32_t ticks) {
157  _xtimer_spin(ticks.ticks32);
158 }
159 
160 static inline void xtimer_usleep(uint32_t microseconds)
161 {
162  _xtimer_tsleep32(_xtimer_ticks_from_usec(microseconds));
163 }
164 
165 static inline void xtimer_usleep64(uint64_t microseconds)
166 {
167  _xtimer_tsleep64(_xtimer_ticks_from_usec64(microseconds));
168 }
169 
170 static inline void xtimer_sleep(uint32_t seconds)
171 {
172  _xtimer_tsleep64(_xtimer_ticks_from_usec64((uint64_t)seconds * US_PER_SEC));
173 }
174 
175 static inline void xtimer_nanosleep(uint32_t nanoseconds)
176 {
177  _xtimer_tsleep32(_xtimer_ticks_from_usec(nanoseconds / NS_PER_US));
178 }
179 
180 static inline void xtimer_tsleep32(xtimer_ticks32_t ticks)
181 {
182  _xtimer_tsleep32(ticks.ticks32);
183 }
184 
185 static inline void xtimer_tsleep64(xtimer_ticks64_t ticks)
186 {
187  _xtimer_tsleep64(ticks.ticks64);
188 }
189 
190 static inline void xtimer_periodic_wakeup(xtimer_ticks32_t *last_wakeup, uint32_t period)
191 {
192  _xtimer_periodic_wakeup(&last_wakeup->ticks32, _xtimer_ticks_from_usec(period));
193 }
194 
195 static inline void xtimer_set_msg(xtimer_t *timer, uint32_t offset, msg_t *msg, kernel_pid_t target_pid)
196 {
197  _xtimer_set_msg(timer, _xtimer_ticks_from_usec(offset), msg, target_pid);
198 }
199 
200 static inline void xtimer_set_msg64(xtimer_t *timer, uint64_t offset, msg_t *msg, kernel_pid_t target_pid)
201 {
202  _xtimer_set_msg64(timer, _xtimer_ticks_from_usec64(offset), msg, target_pid);
203 }
204 
205 static inline void xtimer_set_wakeup(xtimer_t *timer, uint32_t offset, kernel_pid_t pid)
206 {
207  _xtimer_set_wakeup(timer, _xtimer_ticks_from_usec(offset), pid);
208 }
209 
210 static inline void xtimer_set_wakeup64(xtimer_t *timer, uint64_t offset, kernel_pid_t pid)
211 {
212  _xtimer_set_wakeup64(timer, _xtimer_ticks_from_usec64(offset), pid);
213 }
214 
215 static inline void xtimer_set(xtimer_t *timer, uint32_t offset)
216 {
217  _xtimer_set(timer, _xtimer_ticks_from_usec(offset));
218 }
219 
220 static inline int xtimer_msg_receive_timeout(msg_t *msg, uint32_t timeout)
221 {
222  return _xtimer_msg_receive_timeout(msg, _xtimer_ticks_from_usec(timeout));
223 }
224 
225 static inline int xtimer_msg_receive_timeout64(msg_t *msg, uint64_t timeout)
226 {
227  return _xtimer_msg_receive_timeout64(msg, _xtimer_ticks_from_usec64(timeout));
228 }
229 
230 static inline xtimer_ticks32_t xtimer_ticks_from_usec(uint32_t usec)
231 {
232  xtimer_ticks32_t ticks;
233  ticks.ticks32 = _xtimer_ticks_from_usec(usec);
234  return ticks;
235 }
236 
237 static inline xtimer_ticks64_t xtimer_ticks_from_usec64(uint64_t usec)
238 {
239  xtimer_ticks64_t ticks;
240  ticks.ticks64 = _xtimer_ticks_from_usec64(usec);
241  return ticks;
242 }
243 
244 static inline uint32_t xtimer_usec_from_ticks(xtimer_ticks32_t ticks)
245 {
246  return _xtimer_usec_from_ticks(ticks.ticks32);
247 }
248 
249 static inline uint64_t xtimer_usec_from_ticks64(xtimer_ticks64_t ticks)
250 {
251  return _xtimer_usec_from_ticks64(ticks.ticks64);
252 }
253 
254 static inline xtimer_ticks32_t xtimer_ticks(uint32_t ticks)
255 {
256  xtimer_ticks32_t ret;
257  ret.ticks32 = ticks;
258  return ret;
259 }
260 
261 static inline xtimer_ticks64_t xtimer_ticks64(uint64_t ticks)
262 {
263  xtimer_ticks64_t ret;
264  ret.ticks64 = ticks;
265  return ret;
266 }
267 
269 {
270  xtimer_ticks32_t ret;
271  ret.ticks32 = a.ticks32 - b.ticks32;
272  return ret;
273 }
274 
276 {
277  xtimer_ticks64_t ret;
278  ret.ticks64 = a.ticks64 - b.ticks64;
279  return ret;
280 }
281 
283 {
284  uint64_t diff = a.ticks64 - b.ticks64;
285  xtimer_ticks32_t ret;
286  ret.ticks32 = diff;
287  return ret;
288 }
289 
290 static inline bool xtimer_less(xtimer_ticks32_t a, xtimer_ticks32_t b)
291 {
292  return (a.ticks32 < b.ticks32);
293 }
294 
295 static inline bool xtimer_less64(xtimer_ticks64_t a, xtimer_ticks64_t b)
296 {
297  return (a.ticks64 < b.ticks64);
298 }
299 
300 #endif /* !defined(DOXYGEN) */
301 
302 #ifdef __cplusplus
303 }
304 #endif
305 
306 #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:530
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.
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