2020 February 29 Breaking Changes Update (#8064)
[jackhill/qmk/firmware.git] / tmk_core / common / chibios / timer.c
CommitLineData
4d4f7684 1#include "ch.h"
2
3#include "timer.h"
4
26eef35f
JY
5static uint32_t reset_point = 0;
6#if CH_CFG_ST_RESOLUTION < 32
7static uint32_t last_systime = 0;
8static uint32_t overflow = 0;
9#endif
4d4f7684 10
b624f32f 11void timer_init(void) { timer_clear(); }
c5db272c
FS
12
13void timer_clear(void) {
26eef35f
JY
14 reset_point = (uint32_t)chVTGetSystemTime();
15#if CH_CFG_ST_RESOLUTION < 32
16 last_systime = reset_point;
17 overflow = 0;
18#endif
c5db272c 19}
4d4f7684 20
b624f32f 21uint16_t timer_read(void) { return (uint16_t)timer_read32(); }
4d4f7684 22
c5db272c 23uint32_t timer_read32(void) {
26eef35f
JY
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
4d4f7684 43}
44
1a79f14e 45uint16_t timer_elapsed(uint16_t last) { return TIMER_DIFF_16(timer_read(), last); }
4d4f7684 46
1a79f14e 47uint32_t timer_elapsed32(uint32_t last) { return TIMER_DIFF_32(timer_read32(), last); }