]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/common/chibios/timer.c
[Keyboard] fixed pins for numpad_5x4 layout (#6311)
[qmk_firmware.git] / tmk_core / common / chibios / timer.c
1 #include "ch.h"
2
3 #include "timer.h"
4
5 static systime_t last_systime = 0;
6 static systime_t overflow = 0;
7 static uint32_t current_time_ms = 0;
8
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 }
18
19 uint16_t timer_read(void) {
20   return (uint16_t)timer_read32();
21 }
22
23 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
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;
33 }
34
35 uint16_t timer_elapsed(uint16_t last) {
36   return timer_read() - last;
37 }
38
39 uint32_t timer_elapsed32(uint32_t last) {
40   return timer_read32() - last;
41 }