aboutsummaryrefslogtreecommitdiff
path: root/lib/chibios/os/rt/include/chvt.h
diff options
context:
space:
mode:
Diffstat (limited to 'lib/chibios/os/rt/include/chvt.h')
-rw-r--r--lib/chibios/os/rt/include/chvt.h364
1 files changed, 364 insertions, 0 deletions
diff --git a/lib/chibios/os/rt/include/chvt.h b/lib/chibios/os/rt/include/chvt.h
new file mode 100644
index 000000000..3650fb543
--- /dev/null
+++ b/lib/chibios/os/rt/include/chvt.h
@@ -0,0 +1,364 @@
1/*
2 ChibiOS - Copyright (C) 2006,2007,2008,2009,2010,2011,2012,2013,2014,
3 2015,2016,2017,2018,2019,2020,2021 Giovanni Di Sirio.
4
5 This file is part of ChibiOS.
6
7 ChibiOS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation version 3 of the License.
10
11 ChibiOS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20/**
21 * @file rt/include/chvt.h
22 * @brief Time and Virtual Timers module macros and structures.
23 *
24 * @addtogroup time
25 * @{
26 */
27
28#ifndef CHVT_H
29#define CHVT_H
30
31/*===========================================================================*/
32/* Module constants. */
33/*===========================================================================*/
34
35/*===========================================================================*/
36/* Module pre-compile time settings. */
37/*===========================================================================*/
38
39/*===========================================================================*/
40/* Derived constants and error checks. */
41/*===========================================================================*/
42
43#if (CH_CFG_ST_TIMEDELTA < 0) || (CH_CFG_ST_TIMEDELTA == 1)
44#error "invalid CH_CFG_ST_TIMEDELTA specified, must " \
45 "be zero or greater than one"
46#endif
47
48#if (CH_CFG_ST_TIMEDELTA > 0) && (CH_CFG_TIME_QUANTUM > 0)
49#error "CH_CFG_TIME_QUANTUM not supported in tickless mode"
50#endif
51
52#if (CH_CFG_ST_TIMEDELTA > 0) && (CH_DBG_THREADS_PROFILING == TRUE)
53#error "CH_DBG_THREADS_PROFILING not supported in tickless mode"
54#endif
55
56/*===========================================================================*/
57/* Module data structures and types. */
58/*===========================================================================*/
59
60/*===========================================================================*/
61/* Module macros. */
62/*===========================================================================*/
63
64/*===========================================================================*/
65/* External declarations. */
66/*===========================================================================*/
67
68/*
69 * Virtual Timers APIs.
70 */
71#ifdef __cplusplus
72extern "C" {
73#endif
74 void _vt_init(void);
75 void chVTDoSetI(virtual_timer_t *vtp, sysinterval_t delay,
76 vtfunc_t vtfunc, void *par);
77 void chVTDoResetI(virtual_timer_t *vtp);
78 void chVTDoTickI(void);
79#ifdef __cplusplus
80}
81#endif
82
83/*===========================================================================*/
84/* Module inline functions. */
85/*===========================================================================*/
86
87/**
88 * @brief Initializes a @p virtual_timer_t object.
89 * @note Initializing a timer object is not strictly required because
90 * the function @p chVTSetI() initializes the object too. This
91 * function is only useful if you need to perform a @p chVTIsArmed()
92 * check before calling @p chVTSetI().
93 *
94 * @param[out] vtp the @p virtual_timer_t structure pointer
95 *
96 * @init
97 */
98static inline void chVTObjectInit(virtual_timer_t *vtp) {
99
100 vtp->func = NULL;
101}
102
103/**
104 * @brief Current system time.
105 * @details Returns the number of system ticks since the @p chSysInit()
106 * invocation.
107 * @note The counter can reach its maximum and then restart from zero.
108 * @note This function can be called from any context but its atomicity
109 * is not guaranteed on architectures whose word size is less than
110 * @p systime_t size.
111 *
112 * @return The system time in ticks.
113 *
114 * @xclass
115 */
116static inline systime_t chVTGetSystemTimeX(void) {
117
118#if CH_CFG_ST_TIMEDELTA == 0
119 return ch.vtlist.systime;
120#else /* CH_CFG_ST_TIMEDELTA > 0 */
121 return port_timer_get_time();
122#endif /* CH_CFG_ST_TIMEDELTA > 0 */
123}
124
125/**
126 * @brief Current system time.
127 * @details Returns the number of system ticks since the @p chSysInit()
128 * invocation.
129 * @note The counter can reach its maximum and then restart from zero.
130 *
131 * @return The system time in ticks.
132 *
133 * @api
134 */
135static inline systime_t chVTGetSystemTime(void) {
136 systime_t systime;
137
138 chSysLock();
139 systime = chVTGetSystemTimeX();
140 chSysUnlock();
141
142 return systime;
143}
144
145/**
146 * @brief Returns the elapsed time since the specified start time.
147 *
148 * @param[in] start start time
149 * @return The elapsed time.
150 *
151 * @xclass
152 */
153static inline sysinterval_t chVTTimeElapsedSinceX(systime_t start) {
154
155 return chTimeDiffX(start, chVTGetSystemTimeX());
156}
157
158/**
159 * @brief Checks if the current system time is within the specified time
160 * window.
161 * @note When start==end then the function returns always false because the
162 * time window has zero size.
163 *
164 * @param[in] start the start of the time window (inclusive)
165 * @param[in] end the end of the time window (non inclusive)
166 * @retval true current time within the specified time window.
167 * @retval false current time not within the specified time window.
168 *
169 * @xclass
170 */
171static inline bool chVTIsSystemTimeWithinX(systime_t start, systime_t end) {
172
173 return chTimeIsInRangeX(chVTGetSystemTimeX(), start, end);
174}
175
176/**
177 * @brief Checks if the current system time is within the specified time
178 * window.
179 * @note When start==end then the function returns always false because the
180 * time window has zero size.
181 *
182 * @param[in] start the start of the time window (inclusive)
183 * @param[in] end the end of the time window (non inclusive)
184 * @retval true current time within the specified time window.
185 * @retval false current time not within the specified time window.
186 *
187 * @api
188 */
189static inline bool chVTIsSystemTimeWithin(systime_t start, systime_t end) {
190
191 return chTimeIsInRangeX(chVTGetSystemTime(), start, end);
192}
193
194/**
195 * @brief Returns the time interval until the next timer event.
196 * @note The return value is not perfectly accurate and can report values
197 * in excess of @p CH_CFG_ST_TIMEDELTA ticks.
198 * @note The interval returned by this function is only meaningful if
199 * more timers are not added to the list until the returned time.
200 *
201 * @param[out] timep pointer to a variable that will contain the time
202 * interval until the next timer elapses. This pointer
203 * can be @p NULL if the information is not required.
204 * @return The time, in ticks, until next time event.
205 * @retval false if the timers list is empty.
206 * @retval true if the timers list contains at least one timer.
207 *
208 * @iclass
209 */
210static inline bool chVTGetTimersStateI(sysinterval_t *timep) {
211 virtual_timers_list_t *vtlp = &ch.vtlist;
212 delta_list_t *dlp = &vtlp->dlist;
213
214 chDbgCheckClassI();
215
216 if (dlp == dlp->next) {
217 return false;
218 }
219
220 if (timep != NULL) {
221#if CH_CFG_ST_TIMEDELTA == 0
222 *timep = dlp->next->delta;
223#else
224 *timep = (dlp->next->delta + (sysinterval_t)CH_CFG_ST_TIMEDELTA) -
225 chTimeDiffX(vtlp->lasttime, chVTGetSystemTimeX());
226#endif
227 }
228
229 return true;
230}
231
232/**
233 * @brief Returns @p true if the specified timer is armed.
234 * @pre The timer must have been initialized using @p chVTObjectInit()
235 * or @p chVTDoSetI().
236 *
237 * @param[in] vtp the @p virtual_timer_t structure pointer
238 * @return true if the timer is armed.
239 *
240 * @iclass
241 */
242static inline bool chVTIsArmedI(const virtual_timer_t *vtp) {
243
244 chDbgCheckClassI();
245
246 return (bool)(vtp->func != NULL);
247}
248
249/**
250 * @brief Returns @p true if the specified timer is armed.
251 * @pre The timer must have been initialized using @p chVTObjectInit()
252 * or @p chVTDoSetI().
253 *
254 * @param[in] vtp the @p virtual_timer_t structure pointer
255 * @return true if the timer is armed.
256 *
257 * @api
258 */
259static inline bool chVTIsArmed(const virtual_timer_t *vtp) {
260 bool b;
261
262 chSysLock();
263 b = chVTIsArmedI(vtp);
264 chSysUnlock();
265
266 return b;
267}
268
269/**
270 * @brief Disables a Virtual Timer.
271 * @note The timer is first checked and disabled only if armed.
272 * @pre The timer must have been initialized using @p chVTObjectInit()
273 * or @p chVTDoSetI().
274 *
275 * @param[in] vtp the @p virtual_timer_t structure pointer
276 *
277 * @iclass
278 */
279static inline void chVTResetI(virtual_timer_t *vtp) {
280
281 if (chVTIsArmedI(vtp)) {
282 chVTDoResetI(vtp);
283 }
284}
285
286/**
287 * @brief Disables a Virtual Timer.
288 * @note The timer is first checked and disabled only if armed.
289 * @pre The timer must have been initialized using @p chVTObjectInit()
290 * or @p chVTDoSetI().
291 *
292 * @param[in] vtp the @p virtual_timer_t structure pointer
293 *
294 * @api
295 */
296static inline void chVTReset(virtual_timer_t *vtp) {
297
298 chSysLock();
299 chVTResetI(vtp);
300 chSysUnlock();
301}
302
303/**
304 * @brief Enables a virtual timer.
305 * @details If the virtual timer was already enabled then it is re-enabled
306 * using the new parameters.
307 * @pre The timer must have been initialized using @p chVTObjectInit()
308 * or @p chVTDoSetI().
309 *
310 * @param[in] vtp the @p virtual_timer_t structure pointer
311 * @param[in] delay the number of ticks before the operation timeouts, the
312 * special values are handled as follow:
313 * - @a TIME_INFINITE is allowed but interpreted as a
314 * normal time specification.
315 * - @a TIME_IMMEDIATE this value is not allowed.
316 * .
317 * @param[in] vtfunc the timer callback function. After invoking the
318 * callback the timer is disabled and the structure can
319 * be disposed or reused.
320 * @param[in] par a parameter that will be passed to the callback
321 * function
322 *
323 * @iclass
324 */
325static inline void chVTSetI(virtual_timer_t *vtp, sysinterval_t delay,
326 vtfunc_t vtfunc, void *par) {
327
328 chVTResetI(vtp);
329 chVTDoSetI(vtp, delay, vtfunc, par);
330}
331
332/**
333 * @brief Enables a virtual timer.
334 * @details If the virtual timer was already enabled then it is re-enabled
335 * using the new parameters.
336 * @pre The timer must have been initialized using @p chVTObjectInit()
337 * or @p chVTDoSetI().
338 *
339 * @param[in] vtp the @p virtual_timer_t structure pointer
340 * @param[in] delay the number of ticks before the operation timeouts, the
341 * special values are handled as follow:
342 * - @a TIME_INFINITE is allowed but interpreted as a
343 * normal time specification.
344 * - @a TIME_IMMEDIATE this value is not allowed.
345 * .
346 * @param[in] vtfunc the timer callback function. After invoking the
347 * callback the timer is disabled and the structure can
348 * be disposed or reused.
349 * @param[in] par a parameter that will be passed to the callback
350 * function
351 *
352 * @api
353 */
354static inline void chVTSet(virtual_timer_t *vtp, sysinterval_t delay,
355 vtfunc_t vtfunc, void *par) {
356
357 chSysLock();
358 chVTSetI(vtp, delay, vtfunc, par);
359 chSysUnlock();
360}
361
362#endif /* CHVT_H */
363
364/** @} */