Firmware
drv_hrt.h
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2012-2016 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
40 #pragma once
41 
42 #include <sys/ioctl.h>
43 #include <sys/types.h>
44 #include <stdbool.h>
45 #include <inttypes.h>
46 
47 #include <px4_time.h>
48 #include <queue.h>
49 
50 __BEGIN_DECLS
51 
58 typedef uint64_t hrt_abstime;
59 
67 typedef void (* hrt_callout)(void *arg);
68 
72 typedef struct hrt_call {
73  struct sq_entry_s link;
74 
75  hrt_abstime deadline;
76  hrt_abstime period;
77  hrt_callout callout;
78  void *arg;
79 } *hrt_call_t;
80 
85 
89 __EXPORT extern hrt_abstime ts_to_abstime(const struct timespec *ts);
90 
94 __EXPORT extern void abstime_to_ts(struct timespec *ts, hrt_abstime abstime);
95 
102 static inline hrt_abstime hrt_elapsed_time(const hrt_abstime *then)
103 {
104  return hrt_absolute_time() - *then;
105 }
106 
114 __EXPORT extern hrt_abstime hrt_elapsed_time_atomic(const volatile hrt_abstime *then);
115 
122 
123 #ifdef __PX4_QURT
124 
129 __EXPORT extern int hrt_set_absolute_time_offset(int32_t time_diff_us);
130 #endif
131 
138 __EXPORT extern void hrt_call_after(struct hrt_call *entry, hrt_abstime delay, hrt_callout callout, void *arg);
139 
143 __EXPORT extern void hrt_call_at(struct hrt_call *entry, hrt_abstime calltime, hrt_callout callout, void *arg);
144 
151 __EXPORT extern void hrt_call_every(struct hrt_call *entry, hrt_abstime delay, hrt_abstime interval,
152  hrt_callout callout, void *arg);
153 
160 __EXPORT extern bool hrt_called(struct hrt_call *entry);
161 
165 __EXPORT extern void hrt_cancel(struct hrt_call *entry);
166 
170 __EXPORT extern void hrt_call_init(struct hrt_call *entry);
171 
172 /*
173  * delay a hrt_call_every() periodic call by the given number of
174  * microseconds. This should be called from within the callout to
175  * cause the callout to be re-scheduled for a later time. The periodic
176  * callouts will then continue from that new base time at the
177  * previously specified period.
178  */
179 __EXPORT extern void hrt_call_delay(struct hrt_call *entry, hrt_abstime delay);
180 
181 /*
182  * Initialise the HRT.
183  */
184 __EXPORT extern void hrt_init(void);
185 
186 #ifdef __PX4_POSIX
187 
188 __EXPORT extern hrt_abstime hrt_reset(void);
189 
190 __EXPORT extern hrt_abstime hrt_absolute_time_offset(void);
191 
192 #endif
193 
194 __END_DECLS
195 
196 
197 
198 #ifdef __cplusplus
199 
200 namespace time_literals
201 {
202 
203 // User-defined integer literals for different time units.
204 // The base unit is hrt_abstime in microseconds
205 
206 constexpr hrt_abstime operator "" _s(unsigned long long seconds)
207 {
208  return hrt_abstime(seconds * 1000000ULL);
209 }
210 
211 constexpr hrt_abstime operator "" _ms(unsigned long long seconds)
212 {
213  return hrt_abstime(seconds * 1000ULL);
214 }
215 
216 constexpr hrt_abstime operator "" _us(unsigned long long seconds)
217 {
218  return hrt_abstime(seconds);
219 }
220 
221 } /* namespace time_literals */
222 
223 
224 #endif /* __cplusplus */
Definition: queue.h:75
struct hrt_call * hrt_call_t
Callout record.
__EXPORT hrt_abstime hrt_store_absolute_time(volatile hrt_abstime *now)
Store the absolute time in an interrupt-safe fashion.
Definition: drv_hrt.cpp:220
void(* hrt_callout)(void *arg)
Callout function type.
Definition: drv_hrt.h:67
__EXPORT void hrt_call_at(struct hrt_call *entry, hrt_abstime calltime, hrt_callout callout, void *arg)
Call callout(arg) at absolute time calltime.
Definition: drv_hrt.cpp:461
__EXPORT void hrt_call_after(struct hrt_call *entry, hrt_abstime delay, hrt_callout callout, void *arg)
Call callout(arg) after delay has elapsed.
Definition: drv_hrt.cpp:433
Definition: I2C.hpp:51
__EXPORT void hrt_call_init(struct hrt_call *entry)
Initialise a hrt_call structure.
Definition: drv_hrt.cpp:258
__EXPORT bool hrt_called(struct hrt_call *entry)
If this returns true, the entry has been invoked and removed from the callout list, or it has never been entered.
Definition: drv_hrt.cpp:233
__EXPORT void hrt_call_every(struct hrt_call *entry, hrt_abstime delay, hrt_abstime interval, hrt_callout callout, void *arg)
Call callout(arg) after delay, and then after every interval.
Definition: drv_hrt.cpp:449
__BEGIN_DECLS typedef uint64_t hrt_abstime
Absolute time, in microsecond units.
Definition: drv_hrt.h:58
Callout record.
Definition: drv_hrt.h:72
__EXPORT void abstime_to_ts(struct timespec *ts, hrt_abstime abstime)
Convert absolute time to a timespec.
Definition: drv_hrt.cpp:530
__EXPORT hrt_abstime ts_to_abstime(const struct timespec *ts)
Convert a timespec to absolute time.
Definition: drv_hrt.cpp:191
__EXPORT hrt_abstime hrt_absolute_time(void)
Get absolute time in [us] (does not wrap).
Definition: drv_hrt.cpp:155
__EXPORT void hrt_cancel(struct hrt_call *entry)
Remove the entry from the callout list.
Definition: drv_hrt.cpp:241
__EXPORT hrt_abstime hrt_elapsed_time_atomic(const volatile hrt_abstime *then)
Compute the delta between a timestamp taken in the past and now.
Definition: drv_hrt.cpp:208