aboutsummaryrefslogtreecommitdiff
path: root/tmk_core/common/wait.h
diff options
context:
space:
mode:
Diffstat (limited to 'tmk_core/common/wait.h')
-rw-r--r--tmk_core/common/wait.h125
1 files changed, 17 insertions, 108 deletions
diff --git a/tmk_core/common/wait.h b/tmk_core/common/wait.h
index 28224fe3a..cf7180fb0 100644
--- a/tmk_core/common/wait.h
+++ b/tmk_core/common/wait.h
@@ -1,3 +1,18 @@
1/* Copyright 2021 QMK
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
1#pragma once 16#pragma once
2 17
3#include <inttypes.h> 18#include <inttypes.h>
@@ -6,114 +21,8 @@
6extern "C" { 21extern "C" {
7#endif 22#endif
8 23
9#if defined(__ARMEL__) || defined(__ARMEB__) 24#if __has_include_next("_wait.h")
10# ifndef __OPTIMIZE__ 25# include_next "_wait.h" /* Include the platforms _wait.h */
11# pragma message "Compiler optimizations disabled; wait_cpuclock() won't work as designed"
12# endif
13
14# define wait_cpuclock(x) wait_cpuclock_allnop(x)
15
16# define CLOCK_DELAY_NOP8 "nop\n\t nop\n\t nop\n\t nop\n\t nop\n\t nop\n\t nop\n\t nop\n\t"
17
18__attribute__((always_inline)) static inline void wait_cpuclock_allnop(unsigned int n) { /* n: 1..135 */
19 /* The argument n must be a constant expression.
20 * That way, compiler optimization will remove unnecessary code. */
21 if (n < 1) {
22 return;
23 }
24 if (n > 8) {
25 unsigned int n8 = n / 8;
26 n = n - n8 * 8;
27 switch (n8) {
28 case 16:
29 asm volatile(CLOCK_DELAY_NOP8::: "memory");
30 case 15:
31 asm volatile(CLOCK_DELAY_NOP8::: "memory");
32 case 14:
33 asm volatile(CLOCK_DELAY_NOP8::: "memory");
34 case 13:
35 asm volatile(CLOCK_DELAY_NOP8::: "memory");
36 case 12:
37 asm volatile(CLOCK_DELAY_NOP8::: "memory");
38 case 11:
39 asm volatile(CLOCK_DELAY_NOP8::: "memory");
40 case 10:
41 asm volatile(CLOCK_DELAY_NOP8::: "memory");
42 case 9:
43 asm volatile(CLOCK_DELAY_NOP8::: "memory");
44 case 8:
45 asm volatile(CLOCK_DELAY_NOP8::: "memory");
46 case 7:
47 asm volatile(CLOCK_DELAY_NOP8::: "memory");
48 case 6:
49 asm volatile(CLOCK_DELAY_NOP8::: "memory");
50 case 5:
51 asm volatile(CLOCK_DELAY_NOP8::: "memory");
52 case 4:
53 asm volatile(CLOCK_DELAY_NOP8::: "memory");
54 case 3:
55 asm volatile(CLOCK_DELAY_NOP8::: "memory");
56 case 2:
57 asm volatile(CLOCK_DELAY_NOP8::: "memory");
58 case 1:
59 asm volatile(CLOCK_DELAY_NOP8::: "memory");
60 case 0:
61 break;
62 }
63 }
64 switch (n) {
65 case 8:
66 asm volatile("nop" ::: "memory");
67 case 7:
68 asm volatile("nop" ::: "memory");
69 case 6:
70 asm volatile("nop" ::: "memory");
71 case 5:
72 asm volatile("nop" ::: "memory");
73 case 4:
74 asm volatile("nop" ::: "memory");
75 case 3:
76 asm volatile("nop" ::: "memory");
77 case 2:
78 asm volatile("nop" ::: "memory");
79 case 1:
80 asm volatile("nop" ::: "memory");
81 case 0:
82 break;
83 }
84}
85#endif
86
87#if defined(__AVR__)
88# include <util/delay.h>
89# define wait_ms(ms) _delay_ms(ms)
90# define wait_us(us) _delay_us(us)
91# define wait_cpuclock(x) __builtin_avr_delay_cycles(x)
92#elif defined PROTOCOL_CHIBIOS
93# include <ch.h>
94# define wait_ms(ms) \
95 do { \
96 if (ms != 0) { \
97 chThdSleepMilliseconds(ms); \
98 } else { \
99 chThdSleepMicroseconds(1); \
100 } \
101 } while (0)
102# define wait_us(us) \
103 do { \
104 if (us != 0) { \
105 chThdSleepMicroseconds(us); \
106 } else { \
107 chThdSleepMicroseconds(1); \
108 } \
109 } while (0)
110#elif defined PROTOCOL_ARM_ATSAM
111# include "clks.h"
112# define wait_ms(ms) CLK_delay_ms(ms)
113# define wait_us(us) CLK_delay_us(us)
114#else // Unit tests
115void wait_ms(uint32_t ms);
116# define wait_us(us) wait_ms(us / 1000)
117#endif 26#endif
118 27
119#ifdef __cplusplus 28#ifdef __cplusplus