aboutsummaryrefslogtreecommitdiff
path: root/platforms/chibios/timer.c
diff options
context:
space:
mode:
Diffstat (limited to 'platforms/chibios/timer.c')
-rw-r--r--platforms/chibios/timer.c47
1 files changed, 47 insertions, 0 deletions
diff --git a/platforms/chibios/timer.c b/platforms/chibios/timer.c
new file mode 100644
index 000000000..9f664e1f7
--- /dev/null
+++ b/platforms/chibios/timer.c
@@ -0,0 +1,47 @@
1#include <ch.h>
2
3#include "timer.h"
4
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
10
11void timer_init(void) { timer_clear(); }
12
13void 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
21uint16_t timer_read(void) { return (uint16_t)timer_read32(); }
22
23uint32_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
45uint16_t timer_elapsed(uint16_t last) { return TIMER_DIFF_16(timer_read(), last); }
46
47uint32_t timer_elapsed32(uint32_t last) { return TIMER_DIFF_32(timer_read32(), last); }