33#ifndef PARAM_ASSERTIONS_ENABLED_PICO_TIME
34#ifdef PARAM_ASSERTIONS_ENABLED_PICO_TIME
35#define PARAM_ASSERTIONS_ENABLED_PICO_TIME PARAM_ASSERTIONS_ENABLED_TIME
37#define PARAM_ASSERTIONS_ENABLED_PICO_TIME 0
42#ifndef PICO_TIME_SLEEP_OVERHEAD_ADJUST_US
43#define PICO_TIME_SLEEP_OVERHEAD_ADJUST_US 6
72static inline uint32_t us_to_ms(uint64_t us) {
74 return (uint32_t)(us / 1000u);
76 return ((uint32_t)us) / 1000u;
114 uint64_t delayed = base + us;
115 if ((int64_t)delayed < 0) {
133 uint64_t delayed = base + ms * 1000ull;
134 if ((int64_t)delayed < 0) {
326#ifndef PICO_TIME_DEFAULT_ALARM_POOL_DISABLED
339#define PICO_TIME_DEFAULT_ALARM_POOL_DISABLED 0
343#ifndef PICO_TIME_DEFAULT_ALARM_POOL_HARDWARE_ALARM_NUM
349#define PICO_TIME_DEFAULT_ALARM_POOL_HARDWARE_ALARM_NUM 3
353#ifndef PICO_TIME_DEFAULT_ALARM_POOL_MAX_TIMERS
362#define PICO_TIME_DEFAULT_ALARM_POOL_MAX_TIMERS 16
393typedef void alarm_pool_timer_t;
400void runtime_init_default_alarm_pool(
void);
402#if !PICO_TIME_DEFAULT_ALARM_POOL_DISABLED
413alarm_pool_t *alarm_pool_create_on_timer(alarm_pool_timer_t *timer, uint timer_alarm_num, uint max_timers);
415alarm_pool_timer_t *alarm_pool_timer_for_timer_num(uint timer_num);
417alarm_pool_timer_t *alarm_pool_get_default_timer(
void);
438 return alarm_pool_create_on_timer(alarm_pool_get_default_timer(), timer_alarm_num, max_timers);
441alarm_pool_t *alarm_pool_create_on_timer_with_unused_hardware_alarm(alarm_pool_timer_t *timer, uint max_timers);
461 return alarm_pool_create_on_timer_with_unused_hardware_alarm(alarm_pool_get_default_timer(), max_timers);
472static inline uint alarm_pool_hardware_alarm_num(alarm_pool_t *pool) {
619#if !PICO_TIME_DEFAULT_ALARM_POOL_DISABLED
801#if !PICO_TIME_DEFAULT_ALARM_POOL_DISABLED
bool alarm_pool_cancel_alarm(alarm_pool_t *pool, alarm_id_t alarm_id)
Cancel an alarm.
Definition time.c:419
static 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)
Add an alarm callback to be called after a delay specified in microseconds.
Definition time.h:556
static alarm_pool_t * alarm_pool_create(uint timer_alarm_num, uint max_timers)
Create an alarm pool.
Definition time.h:437
int64_t alarm_pool_remaining_alarm_time_us(alarm_pool_t *pool, alarm_id_t alarm_id)
Return the time remaining before the next trigger of an alarm.
Definition time.c:612
int32_t alarm_pool_remaining_alarm_time_ms(alarm_pool_t *pool, alarm_id_t alarm_id)
Return the time remaining before the next trigger of an alarm.
Definition time.c:640
uint alarm_pool_core_num(alarm_pool_t *pool)
Return the core number the alarm pool was initialized on (and hence callbacks are called on).
Definition time.c:445
alarm_pool_t * alarm_pool_get_default(void)
The default alarm pool used when alarms are added without specifying an alarm pool,...
Definition time.c:113
int64_t remaining_alarm_time_us(alarm_id_t alarm_id)
Return the time remaining before the next trigger of an alarm.
Definition time.c:647
int32_t remaining_alarm_time_ms(alarm_id_t alarm_id)
Return the time remaining before the next trigger of an alarm.
Definition time.c:651
static alarm_id_t add_alarm_in_us(uint64_t us, alarm_callback_t callback, void *user_data, bool fire_if_past)
Add an alarm callback to be called after a delay specified in microseconds.
Definition time.h:664
static 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)
Add an alarm callback to be called after a delay specified in milliseconds.
Definition time.h:581
static alarm_pool_t * alarm_pool_create_with_unused_hardware_alarm(uint max_timers)
Create an alarm pool, claiming an used timer_alarm to back it.
Definition time.h:460
alarm_id_t alarm_pool_add_alarm_at_force_in_context(alarm_pool_t *pool, absolute_time_t time, alarm_callback_t callback, void *user_data)
Add an alarm callback to be called at or after a specific time.
Definition time.c:386
static alarm_id_t add_alarm_in_ms(uint32_t ms, alarm_callback_t callback, void *user_data, bool fire_if_past)
Add an alarm callback to be called after a delay specified in milliseconds.
Definition time.h:688
int32_t alarm_id_t
The identifier for an alarm.
Definition time.h:379
void alarm_pool_init_default(void)
Create the default alarm pool (if not already created or disabled).
Definition time.c:108
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)
Add an alarm callback to be called at a specific time.
Definition time.c:377
int64_t(* alarm_callback_t)(alarm_id_t id, void *user_data)
User alarm callback.
Definition time.h:390
void alarm_pool_destroy(alarm_pool_t *pool)
Destroy the alarm pool, cancelling all alarms and freeing up the underlying timer_alarm.
Definition time.c:361
static alarm_id_t add_alarm_at(absolute_time_t time, alarm_callback_t callback, void *user_data, bool fire_if_past)
Add an alarm callback to be called at a specific time.
Definition time.h:640
static bool cancel_alarm(alarm_id_t alarm_id)
Cancel an alarm from the default alarm pool.
Definition time.h:698
uint alarm_pool_timer_alarm_num(alarm_pool_t *pool)
Return the timer alarm used by an alarm pool.
Definition time.c:441
uint64_t time_us_64(void)
Return the current 64 bit timestamp value in microseconds for the default timer instance.
Definition timer.c:125
static bool add_repeating_timer_ms(int32_t delay_ms, repeating_timer_callback_t callback, void *user_data, repeating_timer_t *out)
Add a repeating timer that is called repeatedly at the specified interval in milliseconds.
Definition time.h:840
bool(* repeating_timer_callback_t)(repeating_timer_t *rt)
Callback for a repeating timer.
Definition time.h:744
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)
Add a repeating timer that is called repeatedly at the specified interval in microseconds.
Definition time.c:584
static bool add_repeating_timer_us(int64_t delay_us, repeating_timer_callback_t callback, void *user_data, repeating_timer_t *out)
Add a repeating timer that is called repeatedly at the specified interval in microseconds.
Definition time.h:819
bool cancel_repeating_timer(repeating_timer_t *timer)
Cancel a repeating timer.
Definition time.c:595
static 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)
Add a repeating timer that is called repeatedly at the specified interval in milliseconds.
Definition time.h:797
void sleep_until(absolute_time_t target)
Wait until after the given timestamp to return.
Definition time.c:462
void sleep_ms(uint32_t ms)
Wait for the given number of milliseconds before returning.
Definition time.c:516
bool best_effort_wfe_or_timeout(absolute_time_t timeout_timestamp)
Helper method for blocking on a timeout.
Definition time.c:520
void sleep_us(uint64_t us)
Wait for the given number of microseconds before returning.
Definition time.c:499
static bool is_nil_time(absolute_time_t t)
Determine if the given timestamp is nil.
Definition time.h:216
static uint64_t to_ms_64_since_boot(absolute_time_t t)
Convert a timestamp into a number of 64-bit milliseconds since boot.
Definition time.h:99
static uint64_t to_us_since_boot(absolute_time_t t)
convert an absolute_time_t into a number of microseconds since boot.
Definition types.h:52
static int64_t absolute_time_diff_us(absolute_time_t from, absolute_time_t to)
Return the difference in microseconds between two timestamps.
Definition time.h:173
static bool is_at_the_end_of_time(absolute_time_t t)
Determine if the given timestamp is "at_the_end_of_time".
Definition time.h:201
static absolute_time_t absolute_time_min(absolute_time_t a, absolute_time_t b)
Return the earlier of two timestamps.
Definition time.h:184
static absolute_time_t get_absolute_time(void)
Return a representation of the current time.
Definition time.h:66
static absolute_time_t make_timeout_time_us(uint64_t us)
Convenience method to get the timestamp a number of microseconds from the current time.
Definition time.h:148
static absolute_time_t delayed_by_us(const absolute_time_t t, uint64_t us)
Return a timestamp value obtained by adding a number of microseconds to another timestamp.
Definition time.h:111
static absolute_time_t delayed_by_ms(const absolute_time_t t, uint32_t ms)
Return a timestamp value obtained by adding a number of milliseconds to another timestamp.
Definition time.h:130
const absolute_time_t nil_time
The timestamp representing a null timestamp.
static absolute_time_t make_timeout_time_ms(uint32_t ms)
Convenience method to get the timestamp a number of milliseconds from the current time.
Definition time.h:158
static uint32_t to_ms_since_boot(absolute_time_t t)
Convert a timestamp into a number of milliseconds since boot.
Definition time.h:87
const absolute_time_t at_the_end_of_time
The timestamp representing the end of time; this is actually not the maximum possible timestamp,...
static void update_us_since_boot(absolute_time_t *t, uint64_t us_since_boot)
update an absolute_time_t value to represent a given number of microseconds since boot
Definition types.h:67
uint64_t absolute_time_t
An opaque 64 bit timestamp in microseconds.
Definition types.h:43
Information about a repeating timer.
Definition time.h:751
alarm_pool_t * pool
The alarm pool used to back this repeating timer.
Definition time.h:753
alarm_id_t alarm_id
The alarm id of the underlying alarm backing this repeating timer.
Definition time.h:754
void * user_data
User data passed to the callback.
Definition time.h:756
int64_t delay_us
The delay in microseconds between callbacks (negative if measured between starts).
Definition time.h:752
repeating_timer_callback_t callback
The callback to call each time the repeating timer fires.
Definition time.h:755