diff options
Diffstat (limited to 'src/generic')
-rw-r--r-- | src/generic/misc.h | 3 | ||||
-rw-r--r-- | src/generic/timer.c | 11 |
2 files changed, 12 insertions, 2 deletions
diff --git a/src/generic/misc.h b/src/generic/misc.h index 92d4d145..b6d49ffd 100644 --- a/src/generic/misc.h +++ b/src/generic/misc.h @@ -10,8 +10,9 @@ char *console_get_output(uint8_t len); void console_push_output(uint8_t len); uint32_t timer_from_us(uint32_t us); -void timer_periodic(void); +uint8_t timer_is_before(uint32_t time1, uint32_t time2); uint32_t timer_read_time(void); +void timer_periodic(void); uint8_t timer_try_set_next(uint32_t next); size_t alloc_maxsize(size_t reqsize); diff --git a/src/generic/timer.c b/src/generic/timer.c index 13a8bfe5..436ecb68 100644 --- a/src/generic/timer.c +++ b/src/generic/timer.c @@ -24,6 +24,15 @@ timer_from_us(uint32_t us) return us * (CONFIG_CLOCK_FREQ / 1000000); } +// Return true if time1 is before time2. Always use this function to +// compare times as regular C comparisons can fail if the counter +// rolls over. +uint8_t +timer_is_before(uint32_t time1, uint32_t time2) +{ + return (int32_t)(time1 - time2) < 0; +} + // Called by main code once every millisecond. (IRQs disabled.) void timer_periodic(void) @@ -50,7 +59,7 @@ timer_try_set_next(uint32_t next) goto done; // Next timer is in the past or near future - can't reschedule to it - if (likely(sched_is_before(now, timer_repeat_until))) { + if (likely(timer_is_before(now, timer_repeat_until))) { // Can run more timers from this irq; briefly allow irqs irq_enable(); while (diff >= 0) { |