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