1 /*
2 * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7 #ifndef _PICO_TIME_H
8 #define _PICO_TIME_H
9
10 #include "pico.h"
11 #include "hardware/timer.h"
12
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16
17 /** \file time.h
18 * \defgroup pico_time pico_time
19 *
20 * \brief API for accurate timestamps, sleeping, and time based callbacks
21 *
22 * \note The functions defined here provide a much more powerful and user friendly wrapping around the
23 * low level hardware timer functionality. For these functions (and any other SDK functionality
24 * e.g. timeouts, that relies on them) to work correctly, the hardware timer should not be modified. i.e. it is expected
25 * to be monotonically increasing once per microsecond. Fortunately there is no need to modify the hardware
26 * timer as any functionality you can think of that isn't already covered here can easily be modelled
27 * by adding or subtracting a constant value from the unmodified hardware timer.
28 *
29 * \sa \ref hardware_timer
30 */
31
32 // PICO_CONFIG: PARAM_ASSERTIONS_ENABLED_PICO_TIME, Enable/disable assertions in the pico_time module, type=bool, default=0, group=pico_time
33 #ifndef PARAM_ASSERTIONS_ENABLED_PICO_TIME
34 #ifdef PARAM_ASSERTIONS_ENABLED_PICO_TIME // backwards compatibility with SDK < 2.0.0
35 #define PARAM_ASSERTIONS_ENABLED_PICO_TIME PARAM_ASSERTIONS_ENABLED_TIME
36 #else
37 #define PARAM_ASSERTIONS_ENABLED_PICO_TIME 0
38 #endif
39 #endif
40
41 // PICO_CONFIG: PICO_TIME_SLEEP_OVERHEAD_ADJUST_US, How many microseconds to wake up early (and then busy_wait) to account for timer overhead when sleeping in low power mode, type=int, default=6, group=pico_time
42 #ifndef PICO_TIME_SLEEP_OVERHEAD_ADJUST_US
43 #define PICO_TIME_SLEEP_OVERHEAD_ADJUST_US 6
44 #endif
45
46 /*!
47 * \defgroup timestamp timestamp
48 * \ingroup pico_time
49 * \brief Timestamp functions relating to points in time (including the current time).
50 *
51 * These are functions for dealing with timestamps (i.e. instants in time) represented by the type absolute_time_t. This opaque
52 * type is provided to help prevent accidental mixing of timestamps and relative time values.
53 */
54
55 /*! \brief Return a representation of the current time.
56 * \ingroup timestamp
57 *
58 * Returns an opaque high fidelity representation of the current time sampled during the call.
59 *
60 * \return the absolute time (now) of the hardware timer
61 *
62 * \sa absolute_time_t
63 * \sa sleep_until()
64 * \sa time_us_64()
65 */
get_absolute_time(void)66 static inline absolute_time_t get_absolute_time(void) {
67 absolute_time_t t;
68 update_us_since_boot(&t, time_us_64());
69 return t;
70 }
71
us_to_ms(uint64_t us)72 static inline uint32_t us_to_ms(uint64_t us) {
73 if (us >> 32u) {
74 return (uint32_t)(us / 1000u);
75 } else {
76 return ((uint32_t)us) / 1000u;
77 }
78 }
79
80 /*! fn to_ms_since_boot
81 * \ingroup timestamp
82 * \brief Convert a timestamp into a number of milliseconds since boot.
83 * \param t an absolute_time_t value to convert
84 * \return the number of milliseconds since boot represented by t
85 * \sa to_us_since_boot()
86 */
to_ms_since_boot(absolute_time_t t)87 static inline uint32_t to_ms_since_boot(absolute_time_t t) {
88 uint64_t us = to_us_since_boot(t);
89 return us_to_ms(us);
90 }
91
92 /*! \brief Return a timestamp value obtained by adding a number of microseconds to another timestamp
93 * \ingroup timestamp
94 *
95 * \param t the base timestamp
96 * \param us the number of microseconds to add
97 * \return the timestamp representing the resulting time
98 */
delayed_by_us(const absolute_time_t t,uint64_t us)99 static inline absolute_time_t delayed_by_us(const absolute_time_t t, uint64_t us) {
100 absolute_time_t t2;
101 uint64_t base = to_us_since_boot(t);
102 uint64_t delayed = base + us;
103 if ((int64_t)delayed < 0) {
104 // absolute_time_t (to allow for signed time deltas) is never greater than INT64_MAX which == at_the_end_of_time
105 delayed = INT64_MAX;
106 }
107 update_us_since_boot(&t2, delayed);
108 return t2;
109 }
110
111 /*! \brief Return a timestamp value obtained by adding a number of milliseconds to another timestamp
112 * \ingroup timestamp
113 *
114 * \param t the base timestamp
115 * \param ms the number of milliseconds to add
116 * \return the timestamp representing the resulting time
117 */
delayed_by_ms(const absolute_time_t t,uint32_t ms)118 static inline absolute_time_t delayed_by_ms(const absolute_time_t t, uint32_t ms) {
119 absolute_time_t t2;
120 uint64_t base = to_us_since_boot(t);
121 uint64_t delayed = base + ms * 1000ull;
122 if ((int64_t)delayed < 0) {
123 // absolute_time_t (to allow for signed time deltas) is never greater than INT64_MAX which == at_the_end_of_time
124 delayed = INT64_MAX;
125 }
126 update_us_since_boot(&t2, delayed);
127 return t2;
128 }
129
130 /*! \brief Convenience method to get the timestamp a number of microseconds from the current time
131 * \ingroup timestamp
132 *
133 * \param us the number of microseconds to add to the current timestamp
134 * \return the future timestamp
135 */
make_timeout_time_us(uint64_t us)136 static inline absolute_time_t make_timeout_time_us(uint64_t us) {
137 return delayed_by_us(get_absolute_time(), us);
138 }
139
140 /*! \brief Convenience method to get the timestamp a number of milliseconds from the current time
141 * \ingroup timestamp
142 *
143 * \param ms the number of milliseconds to add to the current timestamp
144 * \return the future timestamp
145 */
make_timeout_time_ms(uint32_t ms)146 static inline absolute_time_t make_timeout_time_ms(uint32_t ms) {
147 return delayed_by_ms(get_absolute_time(), ms);
148 }
149
150 /*! \brief Return the difference in microseconds between two timestamps
151 * \ingroup timestamp
152 *
153 * \note be careful when diffing against large timestamps (e.g. \ref at_the_end_of_time)
154 * as the signed integer may overflow.
155 *
156 * \param from the first timestamp
157 * \param to the second timestamp
158 * \return the number of microseconds between the two timestamps (positive if `to` is after `from` except
159 * in case of overflow)
160 */
absolute_time_diff_us(absolute_time_t from,absolute_time_t to)161 static inline int64_t absolute_time_diff_us(absolute_time_t from, absolute_time_t to) {
162 return (int64_t)(to_us_since_boot(to) - to_us_since_boot(from));
163 }
164
165 /*! \brief Return the earlier of two timestamps
166 * \ingroup timestamp
167 *
168 * \param a the first timestamp
169 * \param b the second timestamp
170 * \return the earlier of the two timestamps
171 */
absolute_time_min(absolute_time_t a,absolute_time_t b)172 static inline absolute_time_t absolute_time_min(absolute_time_t a, absolute_time_t b) {
173 return to_us_since_boot(a) < to_us_since_boot(b) ? a : b;
174 }
175
176 /*! \brief The timestamp representing the end of time; this is actually not the maximum possible
177 * timestamp, but is set to 0x7fffffff_ffffffff microseconds to avoid sign overflows with time
178 * arithmetic. This is almost 300,000 years, so should be sufficient.
179 * \ingroup timestamp
180 */
181 extern const absolute_time_t at_the_end_of_time;
182
183 /*! \brief Determine if the given timestamp is "at_the_end_of_time"
184 * \ingroup timestamp
185 * \param t the timestamp
186 * \return true if the timestamp is at_the_end_of_time
187 * \sa at_the_end_of_time
188 */
is_at_the_end_of_time(absolute_time_t t)189 static inline bool is_at_the_end_of_time(absolute_time_t t) {
190 return to_us_since_boot(t) == to_us_since_boot(at_the_end_of_time);
191 }
192
193 /*! \brief The timestamp representing a null timestamp
194 * \ingroup timestamp
195 */
196 extern const absolute_time_t nil_time;
197
198 /*! \brief Determine if the given timestamp is nil
199 * \ingroup timestamp
200 * \param t the timestamp
201 * \return true if the timestamp is nil
202 * \sa nil_time
203 */
is_nil_time(absolute_time_t t)204 static inline bool is_nil_time(absolute_time_t t) {
205 return !to_us_since_boot(t);
206 }
207
208 /*!
209 * \defgroup sleep sleep
210 * \ingroup pico_time
211 * \brief Sleep functions for delaying execution in a lower power state.
212 *
213 * These functions allow the calling core to sleep. This is a lower powered sleep; waking and re-checking time on every processor
214 * event (WFE)
215 *
216 * \note These functions should not be called from an IRQ handler.
217 *
218 * \note Lower powered sleep requires use of the \link alarm_pool_get_default default alarm pool\endlink which may
219 * be disabled by the PICO_TIME_DEFAULT_ALARM_POOL_DISABLED #define or currently full in which case these functions
220 * become busy waits instead.
221 *
222 * \note Whilst \a sleep_ functions are preferable to \a busy_wait functions from a power perspective, the \a busy_wait equivalent function
223 * may return slightly sooner after the target is reached.
224 *
225 * \sa busy_wait_until() \sa busy_wait_us() \sa busy_wait_us_32()
226 */
227
228 /*! \brief Wait until after the given timestamp to return
229 * \ingroup sleep
230 *
231 * \note This method attempts to perform a lower power (WFE) sleep
232 *
233 * \param target the time after which to return
234 * \sa sleep_us()
235 * \sa busy_wait_until()
236 * */
237 void sleep_until(absolute_time_t target);
238
239 /*! \brief Wait for the given number of microseconds before returning
240 * \ingroup sleep
241 *
242 * \note This method attempts to perform a lower power (WFE) sleep
243 *
244 * \param us the number of microseconds to sleep
245 * \sa busy_wait_us()
246 */
247 void sleep_us(uint64_t us);
248
249 /*! \brief Wait for the given number of milliseconds before returning
250 * \ingroup sleep
251 *
252 * \note This method attempts to perform a lower power sleep (using WFE) as much as possible.
253 *
254 * \param ms the number of milliseconds to sleep
255 */
256 void sleep_ms(uint32_t ms);
257
258 /*! \brief Helper method for blocking on a timeout
259 * \ingroup sleep
260 *
261 * This method will return in response to an event (as per __wfe) or
262 * when the target time is reached, or at any point before.
263 *
264 * This method can be used to implement a lower power polling loop waiting on
265 * some condition signalled by an event (__sev()).
266 *
267 * This is called \a best_effort because under certain circumstances (notably the default timer pool
268 * being disabled or full) the best effort is simply to return immediately without a __wfe, thus turning the calling
269 * code into a busy wait.
270 *
271 * Example usage:
272 * ```c
273 * bool my_function_with_timeout_us(uint64_t timeout_us) {
274 * absolute_time_t timeout_time = make_timeout_time_us(timeout_us);
275 * do {
276 * // each time round the loop, we check to see if the condition
277 * // we are waiting on has happened
278 * if (my_check_done()) {
279 * // do something
280 * return true;
281 * }
282 * // will try to sleep until timeout or the next processor event
283 * } while (!best_effort_wfe_or_timeout(timeout_time));
284 * return false; // timed out
285 * }
286 * ```
287 * NOTE: This method should always be used in a loop associated with checking another "event" variable, since
288 * processor events are a shared resource and can happen for a large number of reasons.
289 *
290 * @param timeout_timestamp the timeout time
291 * @return true if the target time is reached, false otherwise
292 */
293 bool best_effort_wfe_or_timeout(absolute_time_t timeout_timestamp);
294
295 /*!
296 * \defgroup alarm alarm
297 * \ingroup pico_time
298 * \brief Alarm functions for scheduling future execution
299 *
300 * Alarms are added to alarm pools, which may hold a certain fixed number of active alarms. Each alarm pool
301 * utilizes one of four underlying timer_alarms, thus you may have up to four alarm pools. An alarm pool
302 * calls (except when the callback would happen before or during being set) the callback on the core from which
303 * the alarm pool was created. Callbacks are called from the timer_alarm IRQ handler, so care must
304 * be taken in their implementation.
305 *
306 * A default pool is created the core specified by PICO_TIME_DEFAULT_ALARM_POOL_HARDWARE_ALARM_NUM
307 * on core 0, and may be used by the method variants that take no alarm pool parameter.
308 *
309 * \sa struct alarm_pool
310 * \sa hardware_timer
311 */
312
313 // PICO_CONFIG: PICO_TIME_DEFAULT_ALARM_POOL_DISABLED, Disable the default alarm pool, type=bool, default=0, advanced=true, group=pico_time
314 #ifndef PICO_TIME_DEFAULT_ALARM_POOL_DISABLED
315 /*!
316 * \brief If 1 then the default alarm pool is disabled (so no timer_alarm is claimed for the pool)
317 *
318 * \note Setting to 1 may cause some code not to compile as default timer pool related methods are removed
319 *
320 * \note When the default alarm pool is disabled, \a sleep_ methods and timeouts are no longer lower powered
321 * (they become \a busy_wait_)
322 *
323 * \ingroup alarm
324 * \sa #PICO_TIME_DEFAULT_ALARM_POOL_HARDWARE_ALARM_NUM
325 * \sa alarm_pool_get_default()
326 */
327 #define PICO_TIME_DEFAULT_ALARM_POOL_DISABLED 0
328 #endif
329
330 // PICO_CONFIG: PICO_TIME_DEFAULT_ALARM_POOL_HARDWARE_ALARM_NUM, Select which HW alarm is used for the default alarm pool, min=0, max=3, default=3, advanced=true, group=pico_time
331 #ifndef PICO_TIME_DEFAULT_ALARM_POOL_HARDWARE_ALARM_NUM
332 /*!
333 * \brief Selects which timer_alarm is used for the default alarm pool
334 * \ingroup alarm
335 * \sa alarm_pool_get_default()
336 */
337 #define PICO_TIME_DEFAULT_ALARM_POOL_HARDWARE_ALARM_NUM 3
338 #endif
339
340 // PICO_CONFIG: PICO_TIME_DEFAULT_ALARM_POOL_MAX_TIMERS, Selects the maximum number of concurrent timers in the default alarm pool, min=0, max=255, default=16, advanced=true, group=pico_time
341 #ifndef PICO_TIME_DEFAULT_ALARM_POOL_MAX_TIMERS
342 /*!
343 * \brief Selects the maximum number of concurrent timers in the default alarm pool
344 * \ingroup alarm
345 *
346 * \note For implementation reasons this is limited to PICO_PHEAP_MAX_ENTRIES which defaults to 255
347 * \sa #PICO_TIME_DEFAULT_ALARM_POOL_HARDWARE_ALARM_NUM
348 * \sa alarm_pool_get_default()
349 */
350 #define PICO_TIME_DEFAULT_ALARM_POOL_MAX_TIMERS 16
351 #endif
352
353 /**
354 * \brief The identifier for an alarm
355 *
356 * \note this identifier is signed because <0 is used as an error condition when creating alarms
357 *
358 * \note alarm ids may be reused, however for convenience the implementation makes an attempt to defer
359 * reusing as long as possible. You should certainly expect it to be hundreds of ids before one is
360 * reused, although in most cases it is more. Nonetheless care must still be taken when cancelling
361 * alarms or other functionality based on alarms when the alarm may have expired, as eventually
362 * the alarm id may be reused for another alarm.
363 *
364 * \see pico_error_codes
365 * \ingroup alarm
366 */
367 typedef int32_t alarm_id_t; // note this is signed because we use <0 as a meaningful error value
368
369 /**
370 * \brief User alarm callback
371 * \ingroup alarm
372 * \param id the alarm_id as returned when the alarm was added
373 * \param user_data the user data passed when the alarm was added
374 * \return <0 to reschedule the same alarm this many us from the time the alarm was previously scheduled to fire
375 * \return >0 to reschedule the same alarm this many us from the time this method returns
376 * \return 0 to not reschedule the alarm
377 */
378 typedef int64_t (*alarm_callback_t)(alarm_id_t id, void *user_data);
379
380 typedef struct alarm_pool alarm_pool_t;
381 typedef void alarm_pool_timer_t;
382
383 /**
384 * \brief Create the default alarm pool (if not already created or disabled)
385 * \ingroup alarm
386 */
387 void alarm_pool_init_default(void);
388 void runtime_init_default_alarm_pool(void);
389
390 #if !PICO_TIME_DEFAULT_ALARM_POOL_DISABLED
391 /*!
392 * \brief The default alarm pool used when alarms are added without specifying an alarm pool,
393 * and also used by the SDK to support lower power sleeps and timeouts.
394 *
395 * \ingroup alarm
396 * \sa #PICO_TIME_DEFAULT_ALARM_POOL_HARDWARE_ALARM_NUM
397 */
398 alarm_pool_t *alarm_pool_get_default(void);
399 #endif
400
401 alarm_pool_t *alarm_pool_create_on_timer(alarm_pool_timer_t *timer, uint timer_alarm_num, uint max_timers);
402
403 alarm_pool_timer_t *alarm_pool_timer_for_timer_num(uint timer_num);
404
405 alarm_pool_timer_t *alarm_pool_get_default_timer(void);
406
407 /**
408 * \brief Create an alarm pool
409 *
410 * The alarm pool will call callbacks from an alarm IRQ Handler on the core of this function is called from.
411 *
412 * In many situations there is never any need for anything other than the default alarm pool, however you
413 * might want to create another if you want alarm callbacks on core 1 or require alarm pools of
414 * different priority (IRQ priority based preemption of callbacks)
415 *
416 * \note This method will hard assert if the timer_alarm is already claimed.
417 *
418 * \ingroup alarm
419 * \param timer_alarm_num the timer_alarm to use to back this pool
420 * \param max_timers the maximum number of timers
421 * \note For implementation reasons this is limited to PICO_PHEAP_MAX_ENTRIES which defaults to 255
422 * \sa alarm_pool_get_default()
423 * \sa hardware_claiming
424 */
alarm_pool_create(uint timer_alarm_num,uint max_timers)425 static inline alarm_pool_t *alarm_pool_create(uint timer_alarm_num, uint max_timers) {
426 return alarm_pool_create_on_timer(alarm_pool_get_default_timer(), timer_alarm_num, max_timers);
427 }
428
429 alarm_pool_t *alarm_pool_create_on_timer_with_unused_hardware_alarm(alarm_pool_timer_t *timer, uint max_timers);
430
431 /**
432 * \brief Create an alarm pool, claiming an used timer_alarm to back it.
433 *
434 * The alarm pool will call callbacks from an alarm IRQ Handler on the core of this function is called from.
435 *
436 * In many situations there is never any need for anything other than the default alarm pool, however you
437 * might want to create another if you want alarm callbacks on core 1 or require alarm pools of
438 * different priority (IRQ priority based preemption of callbacks)
439 *
440 * \note This method will hard assert if the there is no free hardware to claim.
441 *
442 * \ingroup alarm
443 * \param max_timers the maximum number of timers
444 * \note For implementation reasons this is limited to PICO_PHEAP_MAX_ENTRIES which defaults to 255
445 * \sa alarm_pool_get_default()
446 * \sa hardware_claiming
447 */
alarm_pool_create_with_unused_hardware_alarm(uint max_timers)448 static inline alarm_pool_t *alarm_pool_create_with_unused_hardware_alarm(uint max_timers) {
449 return alarm_pool_create_on_timer_with_unused_hardware_alarm(alarm_pool_get_default_timer(), max_timers);
450 }
451
452 /**
453 * \brief Return the timer alarm used by an alarm pool
454 * \ingroup alarm
455 * \param pool the pool
456 * \return the timer_alarm used by the pool
457 */
458 uint alarm_pool_timer_alarm_num(alarm_pool_t *pool);
459 // backwards compatibility
alarm_pool_hardware_alarm_num(alarm_pool_t * pool)460 static inline uint alarm_pool_hardware_alarm_num(alarm_pool_t *pool) {
461 return alarm_pool_timer_alarm_num(pool);
462 }
463 /**
464 * \brief Return the core number the alarm pool was initialized on (and hence callbacks are called on)
465 * \ingroup alarm
466 * \param pool the pool
467 * \return the core used by the pool
468 */
469 uint alarm_pool_core_num(alarm_pool_t *pool);
470
471 /**
472 * \brief Destroy the alarm pool, cancelling all alarms and freeing up the underlying timer_alarm
473 * \ingroup alarm
474 * \param pool the pool
475 */
476 void alarm_pool_destroy(alarm_pool_t *pool);
477
478 /*!
479 * \brief Add an alarm callback to be called at a specific time
480 * \ingroup alarm
481 *
482 * Generally the callback is called as soon as possible after the time specified from an IRQ handler
483 * on the core the alarm pool was created on. If the callback is in the past or happens before
484 * the alarm setup could be completed, then this method will optionally call the callback itself
485 * and then return a return code to indicate that the target time has passed.
486 *
487 * \note It is safe to call this method from an IRQ handler (including alarm callbacks), and from either core.
488 *
489 * @param pool the alarm pool to use for scheduling the callback (this determines which timer_alarm is used, and which core calls the callback)
490 * @param time the timestamp when (after which) the callback should fire
491 * @param callback the callback function
492 * @param user_data user data to pass to the callback function
493 * @param fire_if_past if true, and the alarm time falls before or during this call before the alarm can be set,
494 * then the callback should be called during (by) this function instead
495 * @return >0 the alarm id for an active (at the time of return) alarm
496 * @return 0 if the alarm time passed before or during the call and fire_if_past was false
497 * @return <0 if there were no alarm slots available, or other error occurred
498 */
499 alarm_id_t alarm_pool_add_alarm_at(alarm_pool_t *pool, absolute_time_t time, alarm_callback_t callback, void *user_data, bool fire_if_past);
500
501 /*!
502 * \brief Add an alarm callback to be called at or after a specific time
503 * \ingroup alarm
504 *
505 * The callback is called as soon as possible after the time specified from an IRQ handler
506 * on the core the alarm pool was created on. Unlike \ref alarm_pool_add_alarm_at, this method
507 * guarantees to call the callback from that core even if the time is during this method call or in the past.
508 *
509 * \note It is safe to call this method from an IRQ handler (including alarm callbacks), and from either core.
510 *
511 * @param pool the alarm pool to use for scheduling the callback (this determines which timer_alarm is used, and which core calls the callback)
512 * @param time the timestamp when (after which) the callback should fire
513 * @param callback the callback function
514 * @param user_data user data to pass to the callback function
515 * @return >0 the alarm id for an active (at the time of return) alarm
516 * @return <0 if there were no alarm slots available, or other error occurred
517 */
518 alarm_id_t alarm_pool_add_alarm_at_force_in_context(alarm_pool_t *pool, absolute_time_t time, alarm_callback_t callback,
519 void *user_data);
520 /*!
521 * \brief Add an alarm callback to be called after a delay specified in microseconds
522 * \ingroup alarm
523 *
524 * Generally the callback is called as soon as possible after the time specified from an IRQ handler
525 * on the core the alarm pool was created on. If the callback is in the past or happens before
526 * the alarm setup could be completed, then this method will optionally call the callback itself
527 * and then return a return code to indicate that the target time has passed.
528 *
529 * \note It is safe to call this method from an IRQ handler (including alarm callbacks), and from either core.
530 *
531 * @param pool the alarm pool to use for scheduling the callback (this determines which timer_alarm is used, and which core calls the callback)
532 * @param us the delay (from now) in microseconds when (after which) the callback should fire
533 * @param callback the callback function
534 * @param user_data user data to pass to the callback function
535 * @param fire_if_past if true, and the alarm time falls during this call before the alarm can be set,
536 * then the callback should be called during (by) this function instead
537 * @return >0 the alarm id
538 * @return 0 if the alarm time passed before or during the call and fire_if_past was false
539 * @return <0 if there were no alarm slots available, or other error occurred
540 */
alarm_pool_add_alarm_in_us(alarm_pool_t * pool,uint64_t us,alarm_callback_t callback,void * user_data,bool fire_if_past)541 static inline alarm_id_t alarm_pool_add_alarm_in_us(alarm_pool_t *pool, uint64_t us, alarm_callback_t callback, void *user_data, bool fire_if_past) {
542 return alarm_pool_add_alarm_at(pool, delayed_by_us(get_absolute_time(), us), callback, user_data, fire_if_past);
543 }
544
545 /*!
546 * \brief Add an alarm callback to be called after a delay specified in milliseconds
547 * \ingroup alarm
548 *
549 * Generally the callback is called as soon as possible after the time specified from an IRQ handler
550 * on the core the alarm pool was created on. If the callback is in the past or happens before
551 * the alarm setup could be completed, then this method will optionally call the callback itself
552 * and then return a return code to indicate that the target time has passed.
553 *
554 * \note It is safe to call this method from an IRQ handler (including alarm callbacks), and from either core.
555 *
556 * @param pool the alarm pool to use for scheduling the callback (this determines which timer_alarm is used, and which core calls the callback)
557 * @param ms the delay (from now) in milliseconds when (after which) the callback should fire
558 * @param callback the callback function
559 * @param user_data user data to pass to the callback function
560 * @param fire_if_past if true, and the alarm time falls before or during this call before the alarm can be set,
561 * then the callback should be called during (by) this function instead
562 * @return >0 the alarm id
563 * @return 0 if the alarm time passed before or during the call and fire_if_past was false
564 * @return <0 if there were no alarm slots available, or other error occurred
565 */
alarm_pool_add_alarm_in_ms(alarm_pool_t * pool,uint32_t ms,alarm_callback_t callback,void * user_data,bool fire_if_past)566 static inline alarm_id_t alarm_pool_add_alarm_in_ms(alarm_pool_t *pool, uint32_t ms, alarm_callback_t callback, void *user_data, bool fire_if_past) {
567 return alarm_pool_add_alarm_at(pool, delayed_by_ms(get_absolute_time(), ms), callback, user_data, fire_if_past);
568 }
569
570 /*!
571 * \brief Return the time remaining before the next trigger of an alarm
572 * \ingroup alarm
573 *
574 * @param pool the alarm_pool containing the alarm
575 * @param alarm_id the alarm
576 *
577 * @return >=0 the number of microseconds before the next trigger
578 * @return <0 if either the given alarm is not in progress or it has passed
579 */
580 int64_t alarm_pool_remaining_alarm_time_us(alarm_pool_t *pool, alarm_id_t alarm_id);
581
582 /*!
583 * \brief Return the time remaining before the next trigger of an alarm
584 * \ingroup alarm
585 *
586 * @param pool the alarm_pool containing the alarm
587 * @param alarm_id the alarm
588 *
589 * @return >=0 the number of milliseconds before the next trigger (INT32_MAX if the number of ms is higher than can be represented0
590 * @return <0 if either the given alarm is not in progress or it has passed
591 */
592 int32_t alarm_pool_remaining_alarm_time_ms(alarm_pool_t *pool, alarm_id_t alarm_id);
593
594 /*!
595 * \brief Cancel an alarm
596 * \ingroup alarm
597 * \param pool the alarm_pool containing the alarm
598 * \param alarm_id the alarm
599 * \return true if the alarm was cancelled, false if it didn't exist
600 * \sa alarm_id_t for a note on reuse of IDs
601 */
602 bool alarm_pool_cancel_alarm(alarm_pool_t *pool, alarm_id_t alarm_id);
603
604 #if !PICO_TIME_DEFAULT_ALARM_POOL_DISABLED
605 /*!
606 * \brief Add an alarm callback to be called at a specific time
607 * \ingroup alarm
608 *
609 * Generally the callback is called as soon as possible after the time specified from an IRQ handler
610 * on the core of the default alarm pool (generally core 0). If the callback is in the past or happens before
611 * the alarm setup could be completed, then this method will optionally call the callback itself
612 * and then return a return code to indicate that the target time has passed.
613 *
614 * \note It is safe to call this method from an IRQ handler (including alarm callbacks), and from either core.
615 *
616 * @param time the timestamp when (after which) the callback should fire
617 * @param callback the callback function
618 * @param user_data user data to pass to the callback function
619 * @param fire_if_past if true, and the alarm time falls before or during this call before the alarm can be set,
620 * then the callback should be called during (by) this function instead
621 * @return >0 the alarm id
622 * @return 0 if the alarm time passed before or during the call and fire_if_past was false
623 * @return <0 if there were no alarm slots available, or other error occurred
624 */
add_alarm_at(absolute_time_t time,alarm_callback_t callback,void * user_data,bool fire_if_past)625 static inline alarm_id_t add_alarm_at(absolute_time_t time, alarm_callback_t callback, void *user_data, bool fire_if_past) {
626 return alarm_pool_add_alarm_at(alarm_pool_get_default(), time, callback, user_data, fire_if_past);
627 }
628
629 /*!
630 * \brief Add an alarm callback to be called after a delay specified in microseconds
631 * \ingroup alarm
632 *
633 * Generally the callback is called as soon as possible after the time specified from an IRQ handler
634 * on the core of the default alarm pool (generally core 0). If the callback is in the past or happens before
635 * the alarm setup could be completed, then this method will optionally call the callback itself
636 * and then return a return code to indicate that the target time has passed.
637 *
638 * \note It is safe to call this method from an IRQ handler (including alarm callbacks), and from either core.
639 *
640 * @param us the delay (from now) in microseconds when (after which) the callback should fire
641 * @param callback the callback function
642 * @param user_data user data to pass to the callback function
643 * @param fire_if_past if true, and the alarm time falls during this call before the alarm can be set,
644 * then the callback should be called during (by) this function instead
645 * @return >0 the alarm id
646 * @return 0 if the alarm time passed before or during the call and fire_if_past was false
647 * @return <0 if there were no alarm slots available, or other error occurred
648 */
add_alarm_in_us(uint64_t us,alarm_callback_t callback,void * user_data,bool fire_if_past)649 static inline alarm_id_t add_alarm_in_us(uint64_t us, alarm_callback_t callback, void *user_data, bool fire_if_past) {
650 return alarm_pool_add_alarm_in_us(alarm_pool_get_default(), us, callback, user_data, fire_if_past);
651 }
652
653 /*!
654 * \brief Add an alarm callback to be called after a delay specified in milliseconds
655 * \ingroup alarm
656 *
657 * Generally the callback is called as soon as possible after the time specified from an IRQ handler
658 * on the core of the default alarm pool (generally core 0). If the callback is in the past or happens before
659 * the alarm setup could be completed, then this method will optionally call the callback itself
660 * and then return a return code to indicate that the target time has passed.
661 *
662 * \note It is safe to call this method from an IRQ handler (including alarm callbacks), and from either core.
663 *
664 * @param ms the delay (from now) in milliseconds when (after which) the callback should fire
665 * @param callback the callback function
666 * @param user_data user data to pass to the callback function
667 * @param fire_if_past if true, and the alarm time falls during this call before the alarm can be set,
668 * then the callback should be called during (by) this function instead
669 * @return >0 the alarm id
670 * @return 0 if the alarm time passed before or during the call and fire_if_past was false
671 * @return <0 if there were no alarm slots available, or other error occurred
672 */
add_alarm_in_ms(uint32_t ms,alarm_callback_t callback,void * user_data,bool fire_if_past)673 static inline alarm_id_t add_alarm_in_ms(uint32_t ms, alarm_callback_t callback, void *user_data, bool fire_if_past) {
674 return alarm_pool_add_alarm_in_ms(alarm_pool_get_default(), ms, callback, user_data, fire_if_past);
675 }
676 /*!
677 * \brief Cancel an alarm from the default alarm pool
678 * \ingroup alarm
679 * \param alarm_id the alarm
680 * \return true if the alarm was cancelled, false if it didn't exist
681 * \sa alarm_id_t for a note on reuse of IDs
682 */
cancel_alarm(alarm_id_t alarm_id)683 static inline bool cancel_alarm(alarm_id_t alarm_id) {
684 return alarm_pool_cancel_alarm(alarm_pool_get_default(), alarm_id);
685 }
686
687 /*!
688 * \brief Return the time remaining before the next trigger of an alarm
689 * \ingroup alarm
690 *
691 * @param pool the alarm_pool containing the alarm
692 * @param alarm_id the alarm
693 *
694 * @return >=0 the number of microseconds before the next trigger
695 * @return <0 if either the given alarm is not in progress or it has passed
696 */
697 int64_t remaining_alarm_time_us(alarm_id_t alarm_id);
698
699 /*!
700 * \brief Return the time remaining before the next trigger of an alarm
701 * \ingroup alarm
702 *
703 * @param alarm_id the alarm
704 *
705 * @return >=0 the number of milliseconds before the next trigger (INT32_MAX if the number of ms is higher than can be represented0
706 * @return <0 if either the given alarm is not in progress or it has passed
707 */
708 int32_t remaining_alarm_time_ms(alarm_id_t alarm_id);
709
710 #endif
711
712 /*!
713 * \defgroup repeating_timer repeating_timer
714 * \ingroup pico_time
715 * \brief Repeating Timer functions for simple scheduling of repeated execution
716 *
717 * \note The regular \a alarm_ functionality can be used to make repeating alarms (by return non zero from the callback),
718 * however these methods abstract that further (at the cost of a user structure to store the repeat delay in (which
719 * the alarm framework does not have space for).
720 */
721
722 typedef struct repeating_timer repeating_timer_t;
723
724 /**
725 * \brief Callback for a repeating timer
726 * \ingroup repeating_timer
727 * \param rt repeating time structure containing information about the repeating time. user_data is of primary important to the user
728 * \return true to continue repeating, false to stop.
729 */
730 typedef bool (*repeating_timer_callback_t)(repeating_timer_t *rt);
731
732 /**
733 * \brief Information about a repeating timer
734 * \ingroup repeating_timer
735 * \return
736 */
737 struct repeating_timer {
738 int64_t delay_us;
739 alarm_pool_t *pool;
740 alarm_id_t alarm_id;
741 repeating_timer_callback_t callback;
742 void *user_data;
743 };
744
745 /*!
746 * \brief Add a repeating timer that is called repeatedly at the specified interval in microseconds
747 * \ingroup repeating_timer
748 *
749 * Generally the callback is called as soon as possible after the time specified from an IRQ handler
750 * on the core the alarm pool was created on. If the callback is in the past or happens before
751 * the alarm setup could be completed, then this method will optionally call the callback itself
752 * and then return a return code to indicate that the target time has passed.
753 *
754 * \note It is safe to call this method from an IRQ handler (including alarm callbacks), and from either core.
755 *
756 * @param pool the alarm pool to use for scheduling the repeating timer (this determines which timer_alarm is used, and which core calls the callback)
757 * @param delay_us the repeat delay in microseconds; if >0 then this is the delay between one callback ending and the next starting; if <0 then this is the negative of the time between the starts of the callbacks. The value of 0 is treated as 1
758 * @param callback the repeating timer callback function
759 * @param user_data user data to pass to store in the repeating_timer structure for use by the callback.
760 * @param out the pointer to the user owned structure to store the repeating timer info in. BEWARE this storage location must outlive the repeating timer, so be careful of using stack space
761 * @return false if there were no alarm slots available to create the timer, true otherwise.
762 */
763 bool alarm_pool_add_repeating_timer_us(alarm_pool_t *pool, int64_t delay_us, repeating_timer_callback_t callback, void *user_data, repeating_timer_t *out);
764
765 /*!
766 * \brief Add a repeating timer that is called repeatedly at the specified interval in milliseconds
767 * \ingroup repeating_timer
768 *
769 * Generally the callback is called as soon as possible after the time specified from an IRQ handler
770 * on the core the alarm pool was created on. If the callback is in the past or happens before
771 * the alarm setup could be completed, then this method will optionally call the callback itself
772 * and then return a return code to indicate that the target time has passed.
773 *
774 * \note It is safe to call this method from an IRQ handler (including alarm callbacks), and from either core.
775 *
776 * @param pool the alarm pool to use for scheduling the repeating timer (this determines which timer_alarm is used, and which core calls the callback)
777 * @param delay_ms the repeat delay in milliseconds; if >0 then this is the delay between one callback ending and the next starting; if <0 then this is the negative of the time between the starts of the callbacks. The value of 0 is treated as 1 microsecond
778 * @param callback the repeating timer callback function
779 * @param user_data user data to pass to store in the repeating_timer structure for use by the callback.
780 * @param out the pointer to the user owned structure to store the repeating timer info in. BEWARE this storage location must outlive the repeating timer, so be careful of using stack space
781 * @return false if there were no alarm slots available to create the timer, true otherwise.
782 */
alarm_pool_add_repeating_timer_ms(alarm_pool_t * pool,int32_t delay_ms,repeating_timer_callback_t callback,void * user_data,repeating_timer_t * out)783 static inline bool alarm_pool_add_repeating_timer_ms(alarm_pool_t *pool, int32_t delay_ms, repeating_timer_callback_t callback, void *user_data, repeating_timer_t *out) {
784 return alarm_pool_add_repeating_timer_us(pool, delay_ms * (int64_t)1000, callback, user_data, out);
785 }
786
787 #if !PICO_TIME_DEFAULT_ALARM_POOL_DISABLED
788 /*!
789 * \brief Add a repeating timer that is called repeatedly at the specified interval in microseconds
790 * \ingroup repeating_timer
791 *
792 * Generally the callback is called as soon as possible after the time specified from an IRQ handler
793 * on the core of the default alarm pool (generally core 0). If the callback is in the past or happens before
794 * the alarm setup could be completed, then this method will optionally call the callback itself
795 * and then return a return code to indicate that the target time has passed.
796 *
797 * \note It is safe to call this method from an IRQ handler (including alarm callbacks), and from either core.
798 *
799 * @param delay_us the repeat delay in microseconds; if >0 then this is the delay between one callback ending and the next starting; if <0 then this is the negative of the time between the starts of the callbacks. The value of 0 is treated as 1
800 * @param callback the repeating timer callback function
801 * @param user_data user data to pass to store in the repeating_timer structure for use by the callback.
802 * @param out the pointer to the user owned structure to store the repeating timer info in. BEWARE this storage location must outlive the repeating timer, so be careful of using stack space
803 * @return false if there were no alarm slots available to create the timer, true otherwise.
804 */
add_repeating_timer_us(int64_t delay_us,repeating_timer_callback_t callback,void * user_data,repeating_timer_t * out)805 static inline bool add_repeating_timer_us(int64_t delay_us, repeating_timer_callback_t callback, void *user_data, repeating_timer_t *out) {
806 return alarm_pool_add_repeating_timer_us(alarm_pool_get_default(), delay_us, callback, user_data, out);
807 }
808
809 /*!
810 * \brief Add a repeating timer that is called repeatedly at the specified interval in milliseconds
811 * \ingroup repeating_timer
812 *
813 * Generally the callback is called as soon as possible after the time specified from an IRQ handler
814 * on the core of the default alarm pool (generally core 0). If the callback is in the past or happens before
815 * the alarm setup could be completed, then this method will optionally call the callback itself
816 * and then return a return code to indicate that the target time has passed.
817 *
818 * \note It is safe to call this method from an IRQ handler (including alarm callbacks), and from either core.
819 *
820 * @param delay_ms the repeat delay in milliseconds; if >0 then this is the delay between one callback ending and the next starting; if <0 then this is the negative of the time between the starts of the callbacks. The value of 0 is treated as 1 microsecond
821 * @param callback the repeating timer callback function
822 * @param user_data user data to pass to store in the repeating_timer structure for use by the callback.
823 * @param out the pointer to the user owned structure to store the repeating timer info in. BEWARE this storage location must outlive the repeating timer, so be careful of using stack space
824 * @return false if there were no alarm slots available to create the timer, true otherwise.
825 */
add_repeating_timer_ms(int32_t delay_ms,repeating_timer_callback_t callback,void * user_data,repeating_timer_t * out)826 static inline bool add_repeating_timer_ms(int32_t delay_ms, repeating_timer_callback_t callback, void *user_data, repeating_timer_t *out) {
827 return alarm_pool_add_repeating_timer_us(alarm_pool_get_default(), delay_ms * (int64_t)1000, callback, user_data, out);
828 }
829 #endif
830
831 /**
832 * \brief Cancel a repeating timer
833 * \ingroup repeating_timer
834 * \param timer the repeating timer to cancel
835 * \return true if the repeating timer was cancelled, false if it didn't exist
836 * \sa alarm_id_t for a note on reuse of IDs
837 */
838 bool cancel_repeating_timer(repeating_timer_t *timer);
839
840 #ifdef __cplusplus
841 }
842 #endif
843
844 #endif
845