xtimer_compat.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2019 Kaspar Schleiser <kaspar@schleiser.de>
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
19 #ifndef ZTIMER_XTIMER_COMPAT_H
20 #define ZTIMER_XTIMER_COMPAT_H
21 
22 #include <stdbool.h>
23 #include <stdint.h>
24 
25 #include "div.h"
26 #include "timex.h"
27 #ifdef MODULE_CORE_MSG
28 #include "msg.h"
29 #endif /* MODULE_CORE_MSG */
30 #include "mutex.h"
31 #include "sched.h"
32 
33 #include "ztimer.h"
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
39 /* the xtimer API is documented elsewhere. This is just an (incomplete) wrapper,
40  * so skip doxygen.
41  */
42 #ifndef DOXYGEN
43 
44 typedef ztimer_t xtimer_t;
45 typedef uint32_t xtimer_ticks32_t;
46 typedef uint64_t xtimer_ticks64_t;
47 
48 static inline xtimer_ticks32_t xtimer_now(void)
49 {
50  return ztimer_now(ZTIMER_USEC);
51 }
52 
53 static inline xtimer_ticks64_t xtimer_now64(void)
54 {
55  return ztimer_now(ZTIMER_USEC);
56 }
57 
58 /*static void xtimer_now_timex(timex_t *out) {
59  }*/
60 
61 static inline uint32_t xtimer_now_usec(void)
62 {
63  return ztimer_now(ZTIMER_USEC);
64 }
65 
66 static inline uint64_t xtimer_now_usec64(void)
67 {
68  return ztimer_now(ZTIMER_USEC);
69 }
70 
71 static inline void _ztimer_sleep_scale(ztimer_clock_t *clock, uint32_t time, uint32_t scale)
72 {
73  const uint32_t max_sleep = UINT32_MAX / scale;
74 
75  while (time > max_sleep) {
76  ztimer_sleep(clock, max_sleep * scale);
77  time -= max_sleep;
78  }
79 
80  ztimer_sleep(clock, time * scale);
81 }
82 
83 static inline void xtimer_sleep(uint32_t seconds)
84 {
85  /* TODO: use ZTIMER_SEC */
86  if (IS_ACTIVE(MODULE_ZTIMER_MSEC)) {
87  _ztimer_sleep_scale(ZTIMER_MSEC, seconds, 1000);
88  } else {
89  _ztimer_sleep_scale(ZTIMER_USEC, seconds, 1000000);
90  }
91 }
92 
93 static inline void xtimer_msleep(uint32_t milliseconds)
94 {
95  if (IS_ACTIVE(MODULE_ZTIMER_MSEC)) {
96  ztimer_sleep(ZTIMER_MSEC, milliseconds);
97  } else {
98  _ztimer_sleep_scale(ZTIMER_USEC, milliseconds, 1000);
99  }
100 }
101 
102 static inline void xtimer_usleep(uint32_t microseconds)
103 {
104  ztimer_sleep(ZTIMER_USEC, microseconds);
105 }
106 
107 static inline void xtimer_nanosleep(uint32_t nanoseconds)
108 {
109  ztimer_sleep(ZTIMER_USEC, nanoseconds / NS_PER_US);
110 }
111 
112 static inline void xtimer_set(xtimer_t *timer, uint32_t offset)
113 {
114  ztimer_set(ZTIMER_USEC, timer, offset);
115 }
116 
117 static inline void xtimer_remove(xtimer_t *timer)
118 {
119  ztimer_remove(ZTIMER_USEC, timer);
120 }
121 
122 static inline void xtimer_set_msg(xtimer_t *timer, uint32_t offset, msg_t *msg,
123  kernel_pid_t target_pid)
124 {
125  ztimer_set_msg(ZTIMER_USEC, timer, offset, msg, target_pid);
126 }
127 
128 static inline void xtimer_periodic_wakeup(xtimer_ticks32_t *last_wakeup,
129  uint32_t period)
130 {
131  ztimer_periodic_wakeup(ZTIMER_USEC, last_wakeup, period);
132 }
133 
134 static inline uint32_t xtimer_usec_from_ticks(xtimer_ticks32_t ticks)
135 {
136  return ticks;
137 }
138 
139 static inline xtimer_ticks32_t xtimer_ticks_from_usec(uint32_t usec)
140 {
141  return usec;
142 }
143 
144 static inline void xtimer_now_timex(timex_t *out)
145 {
146  uint64_t now = xtimer_now_usec64();
147 
148  out->seconds = div_u64_by_1000000(now);
149  out->microseconds = now - (out->seconds * US_PER_SEC);
150 }
151 
152 static inline int xtimer_msg_receive_timeout(msg_t *msg, uint32_t timeout)
153 {
154  return ztimer_msg_receive_timeout(ZTIMER_USEC, msg, timeout);
155 }
156 
157 static inline void xtimer_set_wakeup(xtimer_t *timer, uint32_t offset,
158  kernel_pid_t pid)
159 {
160  ztimer_set_wakeup(ZTIMER_USEC, timer, offset, pid);
161 }
162 
163 /*
164  static inline void xtimer_set64(xtimer_t *timer, uint64_t offset_us);
165  static inline void xtimer_tsleep32(xtimer_ticks32_t ticks);
166  static inline void xtimer_tsleep64(xtimer_ticks64_t ticks);
167  static inline void xtimer_spin(xtimer_ticks32_t ticks);
168  static inline void xtimer_periodic_wakeup(xtimer_ticks32_t *last_wakeup,
169  uint32_t period);
170  static inline void xtimer_set_wakeup64(xtimer_t *timer, uint64_t offset,
171  kernel_pid_t pid);
172  static inline xtimer_ticks32_t xtimer_ticks_from_usec(uint32_t usec);
173  static inline xtimer_ticks64_t xtimer_ticks_from_usec64(uint64_t usec);
174  static inline uint32_t xtimer_usec_from_ticks(xtimer_ticks32_t ticks);
175  static inline uint64_t xtimer_usec_from_ticks64(xtimer_ticks64_t ticks);
176  static inline xtimer_ticks32_t xtimer_ticks(uint32_t ticks);
177  static inline xtimer_ticks64_t xtimer_ticks64(uint64_t ticks);
178  static inline xtimer_ticks32_t xtimer_diff(xtimer_ticks32_t a,
179  xtimer_ticks32_t b);
180  static inline xtimer_ticks64_t xtimer_diff64(xtimer_ticks64_t a,
181  xtimer_ticks64_t b);
182  static inline xtimer_ticks32_t xtimer_diff32_64(xtimer_ticks64_t a,
183  xtimer_ticks64_t b);
184  static inline bool xtimer_less(xtimer_ticks32_t a, xtimer_ticks32_t b);
185  static inline bool xtimer_less64(xtimer_ticks64_t a, xtimer_ticks64_t b);
186  int xtimer_mutex_lock_timeout(mutex_t *mutex, uint64_t us);
187  void xtimer_set_timeout_flag(xtimer_t *t, uint32_t timeout);
188 
189  #if defined(MODULE_CORE_MSG) || defined(DOXYGEN)
190  static inline void xtimer_set_msg64(xtimer_t *timer, uint64_t offset,
191  msg_t *msg, kernel_pid_t target_pid);
192  static inline int xtimer_msg_receive_timeout64(msg_t *msg, uint64_t timeout);
193  #endif
194  */
195 
196 #endif /* DOXYGEN */
197 
198 #ifdef __cplusplus
199 }
200 #endif
201 
203 #endif /* ZTIMER_XTIMER_COMPAT_H */
ztimer_remove
void ztimer_remove(ztimer_clock_t *clock, ztimer_t *timer)
Remove a timer from a clock.
div.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_t
struct xtimer xtimer_t
xtimer timer structure
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_now_timex
void xtimer_now_timex(timex_t *out)
get the current system time into a timex_t
NS_PER_US
#define NS_PER_US
The number of nanoseconds per microsecond.
Definition: timex.h:64
ztimer.h
ztimer API
xtimer_now64
static xtimer_ticks64_t xtimer_now64(void)
get the current system time as 64bit time stamp
timex_t
A timex timestamp.
Definition: timex.h:89
xtimer_now
static xtimer_ticks32_t xtimer_now(void)
get the current system time as 32bit time stamp value
ztimer_t
ztimer structure
Definition: ztimer.h:279
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.
msg.h
Messaging API for inter process communication.
sched.h
Scheduler API definition.
xtimer_ticks_from_usec
static xtimer_ticks32_t xtimer_ticks_from_usec(uint32_t usec)
Convert microseconds to xtimer ticks.
ZTIMER_MSEC
ztimer_clock_t *const ZTIMER_MSEC
Default ztimer millisecond clock.
ztimer_sleep
void ztimer_sleep(ztimer_clock_t *clock, uint32_t duration)
Put the calling thread to sleep for the specified number of ticks.
timex.h
Utility library for comparing and computing timestamps.
ztimer_set
void ztimer_set(ztimer_clock_t *clock, ztimer_t *timer, uint32_t val)
Set a timer on a clock.
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.
ztimer_clock
ztimer device structure
Definition: ztimer.h:312
riot::now
time_point now()
Returns the current time saved in a time point.
Definition: chrono.hpp:107
ZTIMER_USEC
ztimer_clock_t *const ZTIMER_USEC
Default ztimer microsecond clock.
xtimer_usleep
static void xtimer_usleep(uint32_t microseconds)
Pause the execution of a thread for some microseconds.
IS_ACTIVE
#define IS_ACTIVE(macro)
Allows to verify a macro definition outside the preprocessor.
Definition: kernel_defines.h:177
div_u64_by_1000000
static uint64_t div_u64_by_1000000(uint64_t val)
Integer divide val by 1000000.
Definition: div.h:111
ztimer_msg_receive_timeout
int ztimer_msg_receive_timeout(ztimer_clock_t *clock, msg_t *msg, uint32_t timeout)
receive a message (blocking, with timeout)
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.
xtimer_msg_receive_timeout
static int xtimer_msg_receive_timeout(msg_t *msg, uint32_t timeout)
receive a message blocking but with timeout
timex_t::microseconds
uint32_t microseconds
number of microseconds
Definition: timex.h:91
xtimer_ticks32_t
xtimer timestamp (32 bit)
Definition: xtimer.h:69
xtimer_nanosleep
static void xtimer_nanosleep(uint32_t nanoseconds)
Stop execution of a thread for some time.
timex_t::seconds
uint32_t seconds
number of seconds
Definition: timex.h:90
mutex.h
Mutex for thread synchronization.
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.
ztimer_periodic_wakeup
void ztimer_periodic_wakeup(ztimer_clock_t *clock, uint32_t *last_wakeup, uint32_t period)
Suspend the calling thread until the time (last_wakeup + period)
xtimer_sleep
static void xtimer_sleep(uint32_t seconds)
Pause the execution of a thread for some seconds.
msg_t
Describes a message object which can be sent between threads.
Definition: msg.h:185
ztimer_set_msg
void ztimer_set_msg(ztimer_clock_t *clock, ztimer_t *timer, uint32_t offset, msg_t *msg, kernel_pid_t target_pid)
Post a message after a delay.
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_remove
void xtimer_remove(xtimer_t *timer)
remove a timer
ztimer_set_wakeup
void ztimer_set_wakeup(ztimer_clock_t *clock, ztimer_t *timer, uint32_t offset, kernel_pid_t pid)
Set a timer that wakes up a thread.
US_PER_SEC
#define US_PER_SEC
The number of microseconds per second.
Definition: timex.h:34