Browse Source

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.
Keir Fraser 1 year ago
parent
commit
ee02a86e80
2 changed files with 7 additions and 2 deletions
  1. 1 0
      inc/time.h
  2. 6 2
      src/time.c

+ 1 - 0
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)
 

+ 6 - 2
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);
 }