From ee02a86e808a637839275bda9177cf1f93c408dd Mon Sep 17 00:00:00 2001 From: Keir Fraser Date: Fri, 14 Jul 2023 14:24:03 +0100 Subject: [PATCH] time: Proactive pull from FlashFloppy for 32-bit timebase update period Make the period proportional to systick rate by updating the timebase every 2^23 systicks. This will always be safe, no matter how high the systick rate. --- inc/time.h | 1 + src/time.c | 8 ++++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/inc/time.h b/inc/time.h index b1503f0..9bafda1 100644 --- a/inc/time.h +++ b/inc/time.h @@ -14,6 +14,7 @@ typedef uint32_t time_t; #define TIME_MHZ STK_MHZ #define time_us(x) stk_us(x) #define time_ms(x) stk_ms(x) +#define time_stk(x) (x) #define time_sysclk(x) stk_sysclk(x) #define sysclk_time(x) sysclk_stk(x) diff --git a/src/time.c b/src/time.c index 1efa2e1..a00c147 100644 --- a/src/time.c +++ b/src/time.c @@ -12,11 +12,15 @@ static volatile time_t time_stamp; static struct timer time_stamp_timer; +/* Hardware systick timer overflows every 2^24 ticks. We aim to update + * the timestamp at twice that rate (2^23 systicks). */ +#define TIME_UPDATE_PERIOD time_stk(1u<<23) + static void time_stamp_update(void *unused) { time_t now = time_now(); time_stamp = ~now; - timer_set(&time_stamp_timer, now + time_ms(500)); + timer_set(&time_stamp_timer, now + TIME_UPDATE_PERIOD); } time_t time_now(void) @@ -34,7 +38,7 @@ void time_init(void) timers_init(); time_stamp = stk_now(); timer_init(&time_stamp_timer, time_stamp_update, NULL); - timer_set(&time_stamp_timer, time_now() + time_ms(500)); + timer_set(&time_stamp_timer, time_now() + TIME_UPDATE_PERIOD); }