Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add alarm_pool_remaining_time() #1629

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/common/pico_time/include/pico/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,18 @@ static inline alarm_id_t alarm_pool_add_alarm_in_ms(alarm_pool_t *pool, uint32_t
*/
bool alarm_pool_cancel_alarm(alarm_pool_t *pool, alarm_id_t alarm_id);

/*!
* \brief Return the time remaining before the next trigger of an alarm
* \ingroup alarm
*
* @param pool the alarm_pool containing the alarm
* @param alarm_id the alarm
*
* @return >0 the number of microseconds before the next trigger
* @return 0 if either the given alarm is not in progress or it has passed
*/
uint32_t alarm_pool_remaining_time(alarm_pool_t *pool, alarm_id_t alarm_id);

#if !PICO_TIME_DEFAULT_ALARM_POOL_DISABLED
/*!
* \brief Add an alarm callback to be called at a specific time
Expand Down
11 changes: 11 additions & 0 deletions src/common/pico_time/time.c
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,17 @@ void alarm_pool_dump(alarm_pool_t *pool) {
spin_unlock(pool->lock, save);
}

uint32_t alarm_pool_remaining_time(alarm_pool_t *pool, alarm_id_t alarm_id) {
uint32_t save = spin_lock_blocking(pool->lock);
pheap_node_id_t next_id = ph_peek_head(pool->heap);
spin_unlock(pool->lock, save);
if (alarm_id == next_id && timer_hw->alarm[alarm_pool_hardware_alarm_num(pool)] > timer_hw->timerawl) {
return timer_hw->alarm[alarm_pool_hardware_alarm_num(pool)] - timer_hw->timerawl;
} else {
return 0;
}
}

#if !PICO_TIME_DEFAULT_ALARM_POOL_DISABLED
static int64_t sleep_until_callback(__unused alarm_id_t id, __unused void *user_data) {
uint32_t save = spin_lock_blocking(sleep_notifier.spin_lock);
Expand Down
Loading