aboutsummaryrefslogtreecommitdiff
path: root/tmk_core
diff options
context:
space:
mode:
authorJoel Challis <git@zvecr.com>2021-03-10 22:47:36 +0000
committerGitHub <noreply@github.com>2021-03-10 22:47:36 +0000
commit40c7ecfdeaf50ab76e10854a84aebfcb82ddb092 (patch)
tree035c7d9a905198bdab9a659e653da6d320e1708b /tmk_core
parent2e24cfadb75b00156acf2827d5643d6c2d55a60c (diff)
downloadqmk_firmware-40c7ecfdeaf50ab76e10854a84aebfcb82ddb092.tar.gz
qmk_firmware-40c7ecfdeaf50ab76e10854a84aebfcb82ddb092.zip
Move gpio wait logic to wait.h (#12067)
Diffstat (limited to 'tmk_core')
-rw-r--r--tmk_core/common/arm_atsam/_wait.h22
-rw-r--r--tmk_core/common/avr/_wait.h29
-rw-r--r--tmk_core/common/chibios/_wait.h55
-rw-r--r--tmk_core/common/chibios/chibios_config.h1
-rw-r--r--tmk_core/common/chibios/wait.c89
-rw-r--r--tmk_core/common/test/_wait.h22
-rw-r--r--tmk_core/common/wait.h125
7 files changed, 235 insertions, 108 deletions
diff --git a/tmk_core/common/arm_atsam/_wait.h b/tmk_core/common/arm_atsam/_wait.h
new file mode 100644
index 000000000..41b686b56
--- /dev/null
+++ b/tmk_core/common/arm_atsam/_wait.h
@@ -0,0 +1,22 @@
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 */
16#pragma once
17
18#include "clks.h"
19
20#define wait_ms(ms) CLK_delay_ms(ms)
21#define wait_us(us) CLK_delay_us(us)
22#define waitInputPinDelay()
diff --git a/tmk_core/common/avr/_wait.h b/tmk_core/common/avr/_wait.h
new file mode 100644
index 000000000..56eb316fa
--- /dev/null
+++ b/tmk_core/common/avr/_wait.h
@@ -0,0 +1,29 @@
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 */
16#pragma once
17
18#include <util/delay.h>
19
20#define wait_ms(ms) _delay_ms(ms)
21#define wait_us(us) _delay_us(us)
22
23/* The AVR series GPIOs have a one clock read delay for changes in the digital input signal.
24 * But here's more margin to make it two clocks. */
25#ifndef GPIO_INPUT_PIN_DELAY
26# define GPIO_INPUT_PIN_DELAY 2
27#endif
28
29#define waitInputPinDelay() __builtin_avr_delay_cycles(GPIO_INPUT_PIN_DELAY)
diff --git a/tmk_core/common/chibios/_wait.h b/tmk_core/common/chibios/_wait.h
new file mode 100644
index 000000000..5bface53e
--- /dev/null
+++ b/tmk_core/common/chibios/_wait.h
@@ -0,0 +1,55 @@
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 */
16#pragma once
17
18#include <ch.h>
19
20/* chThdSleepX of zero maps to infinite - so we map to a tiny delay to still yield */
21#define wait_ms(ms) \
22 do { \
23 if (ms != 0) { \
24 chThdSleepMilliseconds(ms); \
25 } else { \
26 chThdSleepMicroseconds(1); \
27 } \
28 } while (0)
29#define wait_us(us) \
30 do { \
31 if (us != 0) { \
32 chThdSleepMicroseconds(us); \
33 } else { \
34 chThdSleepMicroseconds(1); \
35 } \
36 } while (0)
37
38/* For GPIOs on ARM-based MCUs, the input pins are sampled by the clock of the bus
39 * to which the GPIO is connected.
40 * The connected buses differ depending on the various series of MCUs.
41 * And since the instruction execution clock of the CPU and the bus clock of GPIO are different,
42 * there is a delay of several clocks to read the change of the input signal.
43 *
44 * Define this delay with the GPIO_INPUT_PIN_DELAY macro.
45 * If the GPIO_INPUT_PIN_DELAY macro is not defined, the following default values will be used.
46 * (A fairly large value of 0.25 microseconds is set.)
47 */
48
49#include "wait.c"
50
51#ifndef GPIO_INPUT_PIN_DELAY
52# define GPIO_INPUT_PIN_DELAY (STM32_SYSCLK / 1000000L / 4)
53#endif
54
55#define waitInputPinDelay() wait_cpuclock(GPIO_INPUT_PIN_DELAY)
diff --git a/tmk_core/common/chibios/chibios_config.h b/tmk_core/common/chibios/chibios_config.h
index bebf026de..9a66ac317 100644
--- a/tmk_core/common/chibios/chibios_config.h
+++ b/tmk_core/common/chibios/chibios_config.h
@@ -30,4 +30,5 @@
30# define USE_I2CV1 30# define USE_I2CV1
31# define USE_I2CV1_CONTRIB // for some reason a bunch of ChibiOS-Contrib boards only have clock_speed 31# define USE_I2CV1_CONTRIB // for some reason a bunch of ChibiOS-Contrib boards only have clock_speed
32# define USE_GPIOV1 32# define USE_GPIOV1
33# define STM32_SYSCLK KINETIS_SYSCLK_FREQUENCY
33#endif 34#endif
diff --git a/tmk_core/common/chibios/wait.c b/tmk_core/common/chibios/wait.c
new file mode 100644
index 000000000..c6270fd95
--- /dev/null
+++ b/tmk_core/common/chibios/wait.c
@@ -0,0 +1,89 @@
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 */
16
17#ifndef __OPTIMIZE__
18# pragma message "Compiler optimizations disabled; wait_cpuclock() won't work as designed"
19#endif
20
21#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"
22
23__attribute__((always_inline)) static inline void wait_cpuclock(unsigned int n) { /* n: 1..135 */
24 /* The argument n must be a constant expression.
25 * That way, compiler optimization will remove unnecessary code. */
26 if (n < 1) {
27 return;
28 }
29 if (n > 8) {
30 unsigned int n8 = n / 8;
31 n = n - n8 * 8;
32 switch (n8) {
33 case 16:
34 asm volatile(CLOCK_DELAY_NOP8::: "memory");
35 case 15:
36 asm volatile(CLOCK_DELAY_NOP8::: "memory");
37 case 14:
38 asm volatile(CLOCK_DELAY_NOP8::: "memory");
39 case 13:
40 asm volatile(CLOCK_DELAY_NOP8::: "memory");
41 case 12:
42 asm volatile(CLOCK_DELAY_NOP8::: "memory");
43 case 11:
44 asm volatile(CLOCK_DELAY_NOP8::: "memory");
45 case 10:
46 asm volatile(CLOCK_DELAY_NOP8::: "memory");
47 case 9:
48 asm volatile(CLOCK_DELAY_NOP8::: "memory");
49 case 8:
50 asm volatile(CLOCK_DELAY_NOP8::: "memory");
51 case 7:
52 asm volatile(CLOCK_DELAY_NOP8::: "memory");
53 case 6:
54 asm volatile(CLOCK_DELAY_NOP8::: "memory");
55 case 5:
56 asm volatile(CLOCK_DELAY_NOP8::: "memory");
57 case 4:
58 asm volatile(CLOCK_DELAY_NOP8::: "memory");
59 case 3:
60 asm volatile(CLOCK_DELAY_NOP8::: "memory");
61 case 2:
62 asm volatile(CLOCK_DELAY_NOP8::: "memory");
63 case 1:
64 asm volatile(CLOCK_DELAY_NOP8::: "memory");
65 case 0:
66 break;
67 }
68 }
69 switch (n) {
70 case 8:
71 asm volatile("nop" ::: "memory");
72 case 7:
73 asm volatile("nop" ::: "memory");
74 case 6:
75 asm volatile("nop" ::: "memory");
76 case 5:
77 asm volatile("nop" ::: "memory");
78 case 4:
79 asm volatile("nop" ::: "memory");
80 case 3:
81 asm volatile("nop" ::: "memory");
82 case 2:
83 asm volatile("nop" ::: "memory");
84 case 1:
85 asm volatile("nop" ::: "memory");
86 case 0:
87 break;
88 }
89} \ No newline at end of file
diff --git a/tmk_core/common/test/_wait.h b/tmk_core/common/test/_wait.h
new file mode 100644
index 000000000..4e22f593b
--- /dev/null
+++ b/tmk_core/common/test/_wait.h
@@ -0,0 +1,22 @@
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 */
16#pragma once
17
18#include <inttypes.h>
19
20void wait_ms(uint32_t ms);
21#define wait_us(us) wait_ms(us / 1000)
22#define waitInputPinDelay()
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