diff options
Diffstat (limited to 'tmk_core/common/chibios/timer.c')
-rw-r--r-- | tmk_core/common/chibios/timer.c | 46 |
1 files changed, 19 insertions, 27 deletions
diff --git a/tmk_core/common/chibios/timer.c b/tmk_core/common/chibios/timer.c index 473e533ca..1ce9d1d17 100644 --- a/tmk_core/common/chibios/timer.c +++ b/tmk_core/common/chibios/timer.c | |||
@@ -2,40 +2,32 @@ | |||
2 | 2 | ||
3 | #include "timer.h" | 3 | #include "timer.h" |
4 | 4 | ||
5 | static systime_t last_systime = 0; | 5 | static systime_t last_systime = 0; |
6 | static systime_t overflow = 0; | 6 | static systime_t overflow = 0; |
7 | static uint32_t current_time_ms = 0; | 7 | static uint32_t current_time_ms = 0; |
8 | 8 | ||
9 | void timer_init(void) { | 9 | void timer_init(void) { timer_clear(); } |
10 | timer_clear(); | ||
11 | } | ||
12 | 10 | ||
13 | void timer_clear(void) { | 11 | void timer_clear(void) { |
14 | last_systime = chVTGetSystemTime(); | 12 | last_systime = chVTGetSystemTime(); |
15 | overflow = 0; | 13 | overflow = 0; |
16 | current_time_ms = 0; | 14 | current_time_ms = 0; |
17 | } | 15 | } |
18 | 16 | ||
19 | uint16_t timer_read(void) { | 17 | uint16_t timer_read(void) { return (uint16_t)timer_read32(); } |
20 | return (uint16_t)timer_read32(); | ||
21 | } | ||
22 | 18 | ||
23 | uint32_t timer_read32(void) { | 19 | uint32_t timer_read32(void) { |
24 | // Note: We assume that the timer update is called at least once betweeen every wrap around of the system time | 20 | // Note: We assume that the timer update is called at least once betweeen every wrap around of the system time |
25 | systime_t current_systime = chVTGetSystemTime(); | 21 | systime_t current_systime = chVTGetSystemTime(); |
26 | systime_t elapsed = current_systime - last_systime + overflow; | 22 | systime_t elapsed = current_systime - last_systime + overflow; |
27 | uint32_t elapsed_ms = ST2MS(elapsed); | 23 | uint32_t elapsed_ms = ST2MS(elapsed); |
28 | current_time_ms += elapsed_ms; | 24 | current_time_ms += elapsed_ms; |
29 | overflow = elapsed - MS2ST(elapsed_ms); | 25 | overflow = elapsed - MS2ST(elapsed_ms); |
30 | last_systime = current_systime; | 26 | last_systime = current_systime; |
31 | 27 | ||
32 | return current_time_ms; | 28 | return current_time_ms; |
33 | } | 29 | } |
34 | 30 | ||
35 | uint16_t timer_elapsed(uint16_t last) { | 31 | uint16_t timer_elapsed(uint16_t last) { return timer_read() - last; } |
36 | return timer_read() - last; | ||
37 | } | ||
38 | 32 | ||
39 | uint32_t timer_elapsed32(uint32_t last) { | 33 | uint32_t timer_elapsed32(uint32_t last) { return timer_read32() - last; } |
40 | return timer_read32() - last; | ||
41 | } | ||