2020 February 29 Breaking Changes Update (#8064)
[jackhill/qmk/firmware.git] / tmk_core / common / chibios / timer.c
1 #include "ch.h"
2
3 #include "timer.h"
4
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;
9 #endif
10
11 void timer_init(void) { timer_clear(); }
12
13 void timer_clear(void) {
14 reset_point = (uint32_t)chVTGetSystemTime();
15 #if CH_CFG_ST_RESOLUTION < 32
16 last_systime = reset_point;
17 overflow = 0;
18 #endif
19 }
20
21 uint16_t timer_read(void) { return (uint16_t)timer_read32(); }
22
23 uint32_t timer_read32(void) {
24 uint32_t systime = (uint32_t)chVTGetSystemTime();
25
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;
36 }
37
38 last_systime = systime;
39 return (uint32_t)TIME_I2MS(systime - reset_point + overflow);
40 #else
41 return (uint32_t)TIME_I2MS(systime - reset_point);
42 #endif
43 }
44
45 uint16_t timer_elapsed(uint16_t last) { return TIMER_DIFF_16(timer_read(), last); }
46
47 uint32_t timer_elapsed32(uint32_t last) { return TIMER_DIFF_32(timer_read32(), last); }