From 9f56a47dca1221a09b7e366ec009bd83110fe9c9 Mon Sep 17 00:00:00 2001 From: Peter Harper <77111776+peterharperuk@users.noreply.github.com> Date: Thu, 26 Sep 2024 16:56:05 +0100 Subject: [PATCH] Avoid setting an alarm in the past (#1954) * Avoid setting an alarm in the past Fixes #1953 * Call ta_time_us_64 when needed Remove local "now" variable. --- src/common/pico_time/time.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/common/pico_time/time.c b/src/common/pico_time/time.c index 118547d8a..f0638fffb 100644 --- a/src/common/pico_time/time.c +++ b/src/common/pico_time/time.c @@ -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); @@ -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 @@ -259,7 +258,6 @@ 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 @@ -267,7 +265,7 @@ static void alarm_pool_irq_handler(void) { 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) {