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

Avoid setting an alarm in the past #1954

Merged
merged 2 commits into from
Sep 26, 2024
Merged
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
6 changes: 2 additions & 4 deletions src/common/pico_time/time.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ static void alarm_pool_irq_handler(void) {
uint timer_num = ta_timer_num(timer);
alarm_pool_t *pool = pools[timer_num][timer_alarm_num];
assert(pool->timer_alarm_num == timer_alarm_num);
int64_t now = (int64_t) ta_time_us_64(timer);
int64_t earliest_target;
// 1. clear force bits if we were forced (do this outside the loop, as forcing is hopefully rare)
ta_clear_force_irq(timer, timer_alarm_num);
Expand All @@ -159,7 +158,7 @@ static void alarm_pool_irq_handler(void) {
if (earliest_index >= 0) {
alarm_pool_entry_t *earliest_entry = &pool->entries[earliest_index];
earliest_target = earliest_entry->target;
if ((now - earliest_target) >= 0) {
if (((int64_t)ta_time_us_64(timer) - earliest_target) >= 0) {
// time to call the callback now (or in the past)
// note that an entry->target of < 0 means the entry has been canceled (not this is set
// by this function, in response to the entry having been queued by the cancel_alarm API
Expand Down Expand Up @@ -259,15 +258,14 @@ static void alarm_pool_irq_handler(void) {
index = next;
}
}
now = (int64_t) ta_time_us_64(timer);
earliest_index = pool->ordered_head;
if (earliest_index < 0) break;
// need to wait
alarm_pool_entry_t *earliest_entry = &pool->entries[earliest_index];
earliest_target = earliest_entry->target;
ta_set_timeout(timer, timer_alarm_num, earliest_target);
// check we haven't now past the target time; if not we don't want to loop again
} while ((earliest_target - now) <= 0);
} while ((earliest_target - (int64_t)ta_time_us_64(timer)) <= 0);
}

void alarm_pool_post_alloc_init(alarm_pool_t *pool, alarm_pool_timer_t *timer, uint hardware_alarm_num, uint max_timers) {
Expand Down
Loading