5 static uint32_t reset_point
= 0;
6 #if CH_CFG_ST_RESOLUTION < 32
7 static uint32_t last_systime
= 0;
8 static uint32_t overflow
= 0;
11 void timer_init(void) { timer_clear(); }
13 void timer_clear(void) {
14 reset_point
= (uint32_t)chVTGetSystemTime();
15 #if CH_CFG_ST_RESOLUTION < 32
16 last_systime
= reset_point
;
21 uint16_t timer_read(void) { return (uint16_t)timer_read32(); }
23 uint32_t timer_read32(void) {
24 uint32_t systime
= (uint32_t)chVTGetSystemTime();
26 #if CH_CFG_ST_RESOLUTION < 32
27 // If/when we need to support 64-bit chips, this may need to be modified to match the native bit-ness of the MCU.
28 // At this point, the only SysTick resolution allowed other than 32 is 16 bit.
29 // In the 16-bit case, at:
30 // - CH_CFG_ST_FREQUENCY = 100000, overflow will occur every ~0.65 seconds
31 // - CH_CFG_ST_FREQUENCY = 10000, overflow will occur every ~6.5 seconds
32 // - CH_CFG_ST_FREQUENCY = 1000, overflow will occur every ~65 seconds
33 // With this implementation, as long as we ensure a timer read happens at least once during the overflow period, timing should be accurate.
34 if (systime
< last_systime
) {
35 overflow
+= ((uint32_t)1) << CH_CFG_ST_RESOLUTION
;
38 last_systime
= systime
;
39 return (uint32_t)TIME_I2MS(systime
- reset_point
+ overflow
);
41 return (uint32_t)TIME_I2MS(systime
- reset_point
);
45 uint16_t timer_elapsed(uint16_t last
) { return TIMER_DIFF_16(timer_read(), last
); }
47 uint32_t timer_elapsed32(uint32_t last
) { return TIMER_DIFF_32(timer_read32(), last
); }