aboutsummaryrefslogtreecommitdiff
path: root/platforms/timer.h
diff options
context:
space:
mode:
authorJoel Challis <git@zvecr.com>2021-11-19 18:41:02 +0000
committerGitHub <noreply@github.com>2021-11-19 10:41:02 -0800
commit2728603fe6d73e805a539d337fd01051c46ca806 (patch)
tree5c83ffc7efa112da870bd5d8502a9d91d4792f35 /platforms/timer.h
parent43b9e23bae12916d5161f03700c9bfe46737324b (diff)
downloadqmk_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.h67
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/*
2Copyright 2011 Jun Wako <wakojun@gmail.com>
3Copyright 2021 Simon Arlott
4
5This program is free software: you can redistribute it and/or modify
6it under the terms of the GNU General Public License as published by
7the Free Software Foundation, either version 2 of the License, or
8(at your option) any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along 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
34extern "C" {
35#endif
36
37extern volatile uint32_t timer_count;
38
39void timer_init(void);
40void timer_clear(void);
41uint16_t timer_read(void);
42uint32_t timer_read32(void);
43uint16_t timer_elapsed(uint16_t last);
44uint32_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)
54typedef uint16_t fast_timer_t;
55fast_timer_t inline timer_read_fast(void) { return timer_read(); }
56fast_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)
60typedef uint32_t fast_timer_t;
61fast_timer_t inline timer_read_fast(void) { return timer_read32(); }
62fast_timer_t inline timer_elapsed_fast(fast_timer_t last) { return timer_elapsed32(last); }
63#endif
64
65#ifdef __cplusplus
66}
67#endif