diff options
author | Joel Challis <git@zvecr.com> | 2021-11-19 18:41:02 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-19 10:41:02 -0800 |
commit | 2728603fe6d73e805a539d337fd01051c46ca806 (patch) | |
tree | 5c83ffc7efa112da870bd5d8502a9d91d4792f35 /platforms/timer.h | |
parent | 43b9e23bae12916d5161f03700c9bfe46737324b (diff) | |
download | qmk_firmware-2728603fe6d73e805a539d337fd01051c46ca806.tar.gz qmk_firmware-2728603fe6d73e805a539d337fd01051c46ca806.zip |
Move tmk_core/common/<plat> (#13918)
Diffstat (limited to 'platforms/timer.h')
-rw-r--r-- | platforms/timer.h | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/platforms/timer.h b/platforms/timer.h new file mode 100644 index 000000000..02e39e79e --- /dev/null +++ b/platforms/timer.h | |||
@@ -0,0 +1,67 @@ | |||
1 | /* | ||
2 | Copyright 2011 Jun Wako <wakojun@gmail.com> | ||
3 | Copyright 2021 Simon Arlott | ||
4 | |||
5 | This program is free software: you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published by | ||
7 | the Free Software Foundation, either version 2 of the License, or | ||
8 | (at your option) any later version. | ||
9 | |||
10 | This program is distributed in the hope that it will be useful, | ||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | GNU General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | */ | ||
18 | |||
19 | #pragma once | ||
20 | |||
21 | #if __has_include_next("_timer.h") | ||
22 | # include_next "_timer.h" /* Include the platform's _timer.h */ | ||
23 | #endif | ||
24 | |||
25 | #include <stdint.h> | ||
26 | |||
27 | #define TIMER_DIFF(a, b, max) ((max == UINT8_MAX) ? ((uint8_t)((a) - (b))) : ((max == UINT16_MAX) ? ((uint16_t)((a) - (b))) : ((max == UINT32_MAX) ? ((uint32_t)((a) - (b))) : ((a) >= (b) ? (a) - (b) : (max) + 1 - (b) + (a))))) | ||
28 | #define TIMER_DIFF_8(a, b) TIMER_DIFF(a, b, UINT8_MAX) | ||
29 | #define TIMER_DIFF_16(a, b) TIMER_DIFF(a, b, UINT16_MAX) | ||
30 | #define TIMER_DIFF_32(a, b) TIMER_DIFF(a, b, UINT32_MAX) | ||
31 | #define TIMER_DIFF_RAW(a, b) TIMER_DIFF_8(a, b) | ||
32 | |||
33 | #ifdef __cplusplus | ||
34 | extern "C" { | ||
35 | #endif | ||
36 | |||
37 | extern volatile uint32_t timer_count; | ||
38 | |||
39 | void timer_init(void); | ||
40 | void timer_clear(void); | ||
41 | uint16_t timer_read(void); | ||
42 | uint32_t timer_read32(void); | ||
43 | uint16_t timer_elapsed(uint16_t last); | ||
44 | uint32_t timer_elapsed32(uint32_t last); | ||
45 | |||
46 | // Utility functions to check if a future time has expired & autmatically handle time wrapping if checked / reset frequently (half of max value) | ||
47 | #define timer_expired(current, future) ((uint16_t)(current - future) < UINT16_MAX / 2) | ||
48 | #define timer_expired32(current, future) ((uint32_t)(current - future) < UINT32_MAX / 2) | ||
49 | |||
50 | // Use an appropriate timer integer size based on architecture (16-bit will overflow sooner) | ||
51 | #if FAST_TIMER_T_SIZE < 32 | ||
52 | # define TIMER_DIFF_FAST(a, b) TIMER_DIFF_16(a, b) | ||
53 | # define timer_expired_fast(current, future) timer_expired(current, future) | ||
54 | typedef uint16_t fast_timer_t; | ||
55 | fast_timer_t inline timer_read_fast(void) { return timer_read(); } | ||
56 | fast_timer_t inline timer_elapsed_fast(fast_timer_t last) { return timer_elapsed(last); } | ||
57 | #else | ||
58 | # define TIMER_DIFF_FAST(a, b) TIMER_DIFF_32(a, b) | ||
59 | # define timer_expired_fast(current, future) timer_expired32(current, future) | ||
60 | typedef uint32_t fast_timer_t; | ||
61 | fast_timer_t inline timer_read_fast(void) { return timer_read32(); } | ||
62 | fast_timer_t inline timer_elapsed_fast(fast_timer_t last) { return timer_elapsed32(last); } | ||
63 | #endif | ||
64 | |||
65 | #ifdef __cplusplus | ||
66 | } | ||
67 | #endif | ||