aboutsummaryrefslogtreecommitdiff
path: root/tmk_core/common
diff options
context:
space:
mode:
Diffstat (limited to 'tmk_core/common')
-rw-r--r--tmk_core/common/action_util.c58
-rw-r--r--tmk_core/common/action_util.h5
-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/avr/suspend.c7
-rw-r--r--tmk_core/common/bootmagic.c163
-rw-r--r--tmk_core/common/bootmagic.h102
-rw-r--r--tmk_core/common/bootmagic_lite.c49
-rw-r--r--tmk_core/common/chibios/_wait.h55
-rw-r--r--tmk_core/common/chibios/bootloader.c2
-rw-r--r--tmk_core/common/chibios/chibios_config.h7
-rw-r--r--tmk_core/common/chibios/pin_defs.h81
-rw-r--r--tmk_core/common/chibios/sleep_led.c88
-rw-r--r--tmk_core/common/chibios/suspend.c14
-rw-r--r--tmk_core/common/chibios/wait.c89
-rw-r--r--tmk_core/common/eeconfig.c2
-rw-r--r--tmk_core/common/eeconfig.h14
-rw-r--r--tmk_core/common/keyboard.c19
-rw-r--r--tmk_core/common/magic.c39
-rw-r--r--tmk_core/common/magic.h3
-rw-r--r--tmk_core/common/test/_wait.h22
-rw-r--r--tmk_core/common/usb_util.c29
-rw-r--r--tmk_core/common/usb_util.h22
-rw-r--r--tmk_core/common/wait.h125
24 files changed, 502 insertions, 544 deletions
diff --git a/tmk_core/common/action_util.c b/tmk_core/common/action_util.c
index 000503b08..a57c8bf66 100644
--- a/tmk_core/common/action_util.c
+++ b/tmk_core/common/action_util.c
@@ -147,12 +147,16 @@ void clear_oneshot_swaphands(void) {
147 * FIXME: needs doc 147 * FIXME: needs doc
148 */ 148 */
149void set_oneshot_layer(uint8_t layer, uint8_t state) { 149void set_oneshot_layer(uint8_t layer, uint8_t state) {
150 oneshot_layer_data = layer << 3 | state; 150 if (!keymap_config.oneshot_disable) {
151 layer_on(layer); 151 oneshot_layer_data = layer << 3 | state;
152 layer_on(layer);
152# if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0)) 153# if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
153 oneshot_layer_time = timer_read(); 154 oneshot_layer_time = timer_read();
154# endif 155# endif
155 oneshot_layer_changed_kb(get_oneshot_layer()); 156 oneshot_layer_changed_kb(get_oneshot_layer());
157 } else {
158 layer_on(layer);
159 }
156} 160}
157/** \brief Reset oneshot layer 161/** \brief Reset oneshot layer
158 * 162 *
@@ -172,7 +176,7 @@ void reset_oneshot_layer(void) {
172void clear_oneshot_layer_state(oneshot_fullfillment_t state) { 176void clear_oneshot_layer_state(oneshot_fullfillment_t state) {
173 uint8_t start_state = oneshot_layer_data; 177 uint8_t start_state = oneshot_layer_data;
174 oneshot_layer_data &= ~state; 178 oneshot_layer_data &= ~state;
175 if (!get_oneshot_layer_state() && start_state != oneshot_layer_data) { 179 if ((!get_oneshot_layer_state() && start_state != oneshot_layer_data) || keymap_config.oneshot_disable) {
176 layer_off(get_oneshot_layer()); 180 layer_off(get_oneshot_layer());
177 reset_oneshot_layer(); 181 reset_oneshot_layer();
178 } 182 }
@@ -182,6 +186,39 @@ void clear_oneshot_layer_state(oneshot_fullfillment_t state) {
182 * FIXME: needs doc 186 * FIXME: needs doc
183 */ 187 */
184bool is_oneshot_layer_active(void) { return get_oneshot_layer_state(); } 188bool is_oneshot_layer_active(void) { return get_oneshot_layer_state(); }
189
190/** \brief set oneshot
191 *
192 * FIXME: needs doc
193 */
194void oneshot_set(bool active) {
195 if (keymap_config.oneshot_disable != active) {
196 keymap_config.oneshot_disable = active;
197 eeconfig_update_keymap(keymap_config.raw);
198 dprintf("Oneshot: active: %d\n", active);
199 }
200}
201
202/** \brief toggle oneshot
203 *
204 * FIXME: needs doc
205 */
206void oneshot_toggle(void) { oneshot_set(!keymap_config.oneshot_disable); }
207
208/** \brief enable oneshot
209 *
210 * FIXME: needs doc
211 */
212void oneshot_enable(void) { oneshot_set(true); }
213
214/** \brief disable oneshot
215 *
216 * FIXME: needs doc
217 */
218void oneshot_disable(void) { oneshot_set(false); }
219
220bool is_oneshot_enabled(void) { return keymap_config.oneshot_disable; }
221
185#endif 222#endif
186 223
187/** \brief Send keyboard report 224/** \brief Send keyboard report
@@ -321,14 +358,17 @@ void del_oneshot_mods(uint8_t mods) {
321 * FIXME: needs doc 358 * FIXME: needs doc
322 */ 359 */
323void set_oneshot_mods(uint8_t mods) { 360void set_oneshot_mods(uint8_t mods) {
324 if (oneshot_mods != mods) { 361 if (!keymap_config.oneshot_disable) {
362 if (oneshot_mods != mods) {
325# if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0)) 363# if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
326 oneshot_time = timer_read(); 364 oneshot_time = timer_read();
327# endif 365# endif
328 oneshot_mods = mods; 366 oneshot_mods = mods;
329 oneshot_mods_changed_kb(mods); 367 oneshot_mods_changed_kb(mods);
368 }
330 } 369 }
331} 370}
371
332/** \brief clear oneshot mods 372/** \brief clear oneshot mods
333 * 373 *
334 * FIXME: needs doc 374 * FIXME: needs doc
diff --git a/tmk_core/common/action_util.h b/tmk_core/common/action_util.h
index ff29f79b0..f2b3897ae 100644
--- a/tmk_core/common/action_util.h
+++ b/tmk_core/common/action_util.h
@@ -85,6 +85,11 @@ void oneshot_mods_changed_kb(uint8_t mods);
85void oneshot_layer_changed_user(uint8_t layer); 85void oneshot_layer_changed_user(uint8_t layer);
86void oneshot_layer_changed_kb(uint8_t layer); 86void oneshot_layer_changed_kb(uint8_t layer);
87 87
88void oneshot_toggle(void);
89void oneshot_enable(void);
90void oneshot_disable(void);
91bool is_oneshot_enabled(void);
92
88/* inspect */ 93/* inspect */
89uint8_t has_anymod(void); 94uint8_t has_anymod(void);
90 95
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/avr/suspend.c b/tmk_core/common/avr/suspend.c
index 47a82a2ee..96b19a77f 100644
--- a/tmk_core/common/avr/suspend.c
+++ b/tmk_core/common/avr/suspend.c
@@ -28,6 +28,13 @@
28# include "rgblight.h" 28# include "rgblight.h"
29#endif 29#endif
30 30
31#ifdef LED_MATRIX_ENABLE
32# include "led_matrix.h"
33#endif
34#ifdef RGB_MATRIX_ENABLE
35# include "rgb_matrix.h"
36#endif
37
31/** \brief Suspend idle 38/** \brief Suspend idle
32 * 39 *
33 * FIXME: needs doc 40 * FIXME: needs doc
diff --git a/tmk_core/common/bootmagic.c b/tmk_core/common/bootmagic.c
deleted file mode 100644
index c1b3adf94..000000000
--- a/tmk_core/common/bootmagic.c
+++ /dev/null
@@ -1,163 +0,0 @@
1#include <stdint.h>
2#include <stdbool.h>
3#include "wait.h"
4#include "matrix.h"
5#include "bootloader.h"
6#include "debug.h"
7#include "keymap.h"
8#include "host.h"
9#include "action_layer.h"
10#include "eeconfig.h"
11#include "bootmagic.h"
12
13keymap_config_t keymap_config;
14
15/** \brief Bootmagic
16 *
17 * FIXME: needs doc
18 */
19void bootmagic(void) {
20 /* check signature */
21 if (!eeconfig_is_enabled()) {
22 eeconfig_init();
23 }
24
25 /* do scans in case of bounce */
26 print("bootmagic scan: ... ");
27 uint8_t scan = 100;
28 while (scan--) {
29 matrix_scan();
30 wait_ms(10);
31 }
32 print("done.\n");
33
34 /* bootmagic skip */
35 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SKIP)) {
36 return;
37 }
38
39 /* eeconfig clear */
40 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_EEPROM_CLEAR)) {
41 eeconfig_init();
42 }
43
44 /* bootloader */
45 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_BOOTLOADER)) {
46 bootloader_jump();
47 }
48
49 /* debug enable */
50 debug_config.raw = eeconfig_read_debug();
51 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEBUG_ENABLE)) {
52 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEBUG_MATRIX)) {
53 debug_config.matrix = !debug_config.matrix;
54 } else if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEBUG_KEYBOARD)) {
55 debug_config.keyboard = !debug_config.keyboard;
56 } else if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEBUG_MOUSE)) {
57 debug_config.mouse = !debug_config.mouse;
58 } else {
59 debug_config.enable = !debug_config.enable;
60 }
61 }
62 eeconfig_update_debug(debug_config.raw);
63
64 /* keymap config */
65 keymap_config.raw = eeconfig_read_keymap();
66 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_CONTROL_CAPSLOCK)) {
67 keymap_config.swap_control_capslock = !keymap_config.swap_control_capslock;
68 }
69 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_CAPSLOCK_TO_CONTROL)) {
70 keymap_config.capslock_to_control = !keymap_config.capslock_to_control;
71 }
72 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_LALT_LGUI)) {
73 keymap_config.swap_lalt_lgui = !keymap_config.swap_lalt_lgui;
74 }
75 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_RALT_RGUI)) {
76 keymap_config.swap_ralt_rgui = !keymap_config.swap_ralt_rgui;
77 }
78 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_NO_GUI)) {
79 keymap_config.no_gui = !keymap_config.no_gui;
80 }
81 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_GRAVE_ESC)) {
82 keymap_config.swap_grave_esc = !keymap_config.swap_grave_esc;
83 }
84 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_BACKSLASH_BACKSPACE)) {
85 keymap_config.swap_backslash_backspace = !keymap_config.swap_backslash_backspace;
86 }
87 if (bootmagic_scan_keycode(BOOTMAGIC_HOST_NKRO)) {
88 keymap_config.nkro = !keymap_config.nkro;
89 }
90 eeconfig_update_keymap(keymap_config.raw);
91
92 /* default layer */
93 uint8_t default_layer = 0;
94 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_0)) {
95 default_layer |= (1 << 0);
96 }
97 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_1)) {
98 default_layer |= (1 << 1);
99 }
100 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_2)) {
101 default_layer |= (1 << 2);
102 }
103 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_3)) {
104 default_layer |= (1 << 3);
105 }
106 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_4)) {
107 default_layer |= (1 << 4);
108 }
109 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_5)) {
110 default_layer |= (1 << 5);
111 }
112 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_6)) {
113 default_layer |= (1 << 6);
114 }
115 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_7)) {
116 default_layer |= (1 << 7);
117 }
118 if (default_layer) {
119 eeconfig_update_default_layer(default_layer);
120 default_layer_set((layer_state_t)default_layer);
121 } else {
122 default_layer = eeconfig_read_default_layer();
123 default_layer_set((layer_state_t)default_layer);
124 }
125 /* Also initialize layer state to trigger callback functions for layer_state */
126 layer_state_set_kb((layer_state_t)layer_state);
127
128 /* EE_HANDS handedness */
129 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_EE_HANDS_LEFT)) {
130 eeconfig_update_handedness(true);
131 }
132 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_EE_HANDS_RIGHT)) {
133 eeconfig_update_handedness(false);
134 }
135}
136
137/** \brief Scan Keycode
138 *
139 * FIXME: needs doc
140 */
141static bool scan_keycode(uint8_t keycode) {
142 for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
143 matrix_row_t matrix_row = matrix_get_row(r);
144 for (uint8_t c = 0; c < MATRIX_COLS; c++) {
145 if (matrix_row & ((matrix_row_t)1 << c)) {
146 if (keycode == keymap_key_to_keycode(0, (keypos_t){.row = r, .col = c})) {
147 return true;
148 }
149 }
150 }
151 }
152 return false;
153}
154
155/** \brief Bootmagic Scan Keycode
156 *
157 * FIXME: needs doc
158 */
159bool bootmagic_scan_keycode(uint8_t keycode) {
160 if (!scan_keycode(BOOTMAGIC_KEY_SALT)) return false;
161
162 return scan_keycode(keycode);
163}
diff --git a/tmk_core/common/bootmagic.h b/tmk_core/common/bootmagic.h
deleted file mode 100644
index 8209d0194..000000000
--- a/tmk_core/common/bootmagic.h
+++ /dev/null
@@ -1,102 +0,0 @@
1#pragma once
2
3/* FIXME: Add special doxygen comments for defines here. */
4
5/* bootmagic salt key */
6#ifndef BOOTMAGIC_KEY_SALT
7# define BOOTMAGIC_KEY_SALT KC_SPACE
8#endif
9
10/* skip bootmagic and eeconfig */
11#ifndef BOOTMAGIC_KEY_SKIP
12# define BOOTMAGIC_KEY_SKIP KC_ESC
13#endif
14
15/* eeprom clear */
16#ifndef BOOTMAGIC_KEY_EEPROM_CLEAR
17# define BOOTMAGIC_KEY_EEPROM_CLEAR KC_BSPACE
18#endif
19
20/* kick up bootloader */
21#ifndef BOOTMAGIC_KEY_BOOTLOADER
22# define BOOTMAGIC_KEY_BOOTLOADER KC_B
23#endif
24
25/* debug enable */
26#ifndef BOOTMAGIC_KEY_DEBUG_ENABLE
27# define BOOTMAGIC_KEY_DEBUG_ENABLE KC_D
28#endif
29#ifndef BOOTMAGIC_KEY_DEBUG_MATRIX
30# define BOOTMAGIC_KEY_DEBUG_MATRIX KC_X
31#endif
32#ifndef BOOTMAGIC_KEY_DEBUG_KEYBOARD
33# define BOOTMAGIC_KEY_DEBUG_KEYBOARD KC_K
34#endif
35#ifndef BOOTMAGIC_KEY_DEBUG_MOUSE
36# define BOOTMAGIC_KEY_DEBUG_MOUSE KC_M
37#endif
38#ifndef BOOTMAGIC_KEY_EE_HANDS_LEFT
39# define BOOTMAGIC_KEY_EE_HANDS_LEFT KC_L
40#endif
41#ifndef BOOTMAGIC_KEY_EE_HANDS_RIGHT
42# define BOOTMAGIC_KEY_EE_HANDS_RIGHT KC_R
43#endif
44
45/*
46 * keymap config
47 */
48#ifndef BOOTMAGIC_KEY_SWAP_CONTROL_CAPSLOCK
49# define BOOTMAGIC_KEY_SWAP_CONTROL_CAPSLOCK KC_LCTRL
50#endif
51#ifndef BOOTMAGIC_KEY_CAPSLOCK_TO_CONTROL
52# define BOOTMAGIC_KEY_CAPSLOCK_TO_CONTROL KC_CAPSLOCK
53#endif
54#ifndef BOOTMAGIC_KEY_SWAP_LALT_LGUI
55# define BOOTMAGIC_KEY_SWAP_LALT_LGUI KC_LALT
56#endif
57#ifndef BOOTMAGIC_KEY_SWAP_RALT_RGUI
58# define BOOTMAGIC_KEY_SWAP_RALT_RGUI KC_RALT
59#endif
60#ifndef BOOTMAGIC_KEY_NO_GUI
61# define BOOTMAGIC_KEY_NO_GUI KC_LGUI
62#endif
63#ifndef BOOTMAGIC_KEY_SWAP_GRAVE_ESC
64# define BOOTMAGIC_KEY_SWAP_GRAVE_ESC KC_GRAVE
65#endif
66#ifndef BOOTMAGIC_KEY_SWAP_BACKSLASH_BACKSPACE
67# define BOOTMAGIC_KEY_SWAP_BACKSLASH_BACKSPACE KC_BSLASH
68#endif
69#ifndef BOOTMAGIC_HOST_NKRO
70# define BOOTMAGIC_HOST_NKRO KC_N
71#endif
72
73/*
74 * change default layer
75 */
76#ifndef BOOTMAGIC_KEY_DEFAULT_LAYER_0
77# define BOOTMAGIC_KEY_DEFAULT_LAYER_0 KC_0
78#endif
79#ifndef BOOTMAGIC_KEY_DEFAULT_LAYER_1
80# define BOOTMAGIC_KEY_DEFAULT_LAYER_1 KC_1
81#endif
82#ifndef BOOTMAGIC_KEY_DEFAULT_LAYER_2
83# define BOOTMAGIC_KEY_DEFAULT_LAYER_2 KC_2
84#endif
85#ifndef BOOTMAGIC_KEY_DEFAULT_LAYER_3
86# define BOOTMAGIC_KEY_DEFAULT_LAYER_3 KC_3
87#endif
88#ifndef BOOTMAGIC_KEY_DEFAULT_LAYER_4
89# define BOOTMAGIC_KEY_DEFAULT_LAYER_4 KC_4
90#endif
91#ifndef BOOTMAGIC_KEY_DEFAULT_LAYER_5
92# define BOOTMAGIC_KEY_DEFAULT_LAYER_5 KC_5
93#endif
94#ifndef BOOTMAGIC_KEY_DEFAULT_LAYER_6
95# define BOOTMAGIC_KEY_DEFAULT_LAYER_6 KC_6
96#endif
97#ifndef BOOTMAGIC_KEY_DEFAULT_LAYER_7
98# define BOOTMAGIC_KEY_DEFAULT_LAYER_7 KC_7
99#endif
100
101void bootmagic(void);
102bool bootmagic_scan_keycode(uint8_t keycode);
diff --git a/tmk_core/common/bootmagic_lite.c b/tmk_core/common/bootmagic_lite.c
deleted file mode 100644
index cbf756a17..000000000
--- a/tmk_core/common/bootmagic_lite.c
+++ /dev/null
@@ -1,49 +0,0 @@
1#include "quantum.h"
2
3/** \brief Reset eeprom
4 *
5 * ...just incase someone wants to only change the eeprom behaviour
6 */
7__attribute__((weak)) void bootmagic_lite_reset_eeprom(void) {
8#if defined(VIA_ENABLE)
9 via_eeprom_reset();
10#else
11 eeconfig_disable();
12#endif
13}
14
15/** \brief The lite version of TMK's bootmagic based on Wilba.
16 *
17 * 100% less potential for accidentally making the keyboard do stupid things.
18 */
19__attribute__((weak)) void bootmagic_lite(void) {
20 // We need multiple scans because debouncing can't be turned off.
21 matrix_scan();
22#if defined(DEBOUNCE) && DEBOUNCE > 0
23 wait_ms(DEBOUNCE * 2);
24#else
25 wait_ms(30);
26#endif
27 matrix_scan();
28
29 // If the configured key (commonly Esc) is held down on power up,
30 // reset the EEPROM valid state and jump to bootloader.
31 // This isn't very generalized, but we need something that doesn't
32 // rely on user's keymaps in firmware or EEPROM.
33 uint8_t row = BOOTMAGIC_LITE_ROW;
34 uint8_t col = BOOTMAGIC_LITE_COLUMN;
35
36#if defined(SPLIT_KEYBOARD) && defined(BOOTMAGIC_LITE_ROW_RIGHT) && defined(BOOTMAGIC_LITE_COLUMN_RIGHT)
37 if (!is_keyboard_left()) {
38 row = BOOTMAGIC_LITE_ROW_RIGHT;
39 col = BOOTMAGIC_LITE_COLUMN_RIGHT;
40 }
41#endif
42
43 if (matrix_get_row(row) & (1 << col)) {
44 bootmagic_lite_reset_eeprom();
45
46 // Jump to bootloader.
47 bootloader_jump();
48 }
49}
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/bootloader.c b/tmk_core/common/chibios/bootloader.c
index 6cabcc4b8..4a175a628 100644
--- a/tmk_core/common/chibios/bootloader.c
+++ b/tmk_core/common/chibios/bootloader.c
@@ -79,7 +79,7 @@ void enter_bootloader_mode_if_requested(void) {
79 } 79 }
80} 80}
81 81
82#elif defined(KL2x) || defined(K20x) // STM32_BOOTLOADER_DUAL_BANK // STM32_BOOTLOADER_ADDRESS 82#elif defined(KL2x) || defined(K20x) || defined(MK66F18) // STM32_BOOTLOADER_DUAL_BANK // STM32_BOOTLOADER_ADDRESS
83/* Kinetis */ 83/* Kinetis */
84 84
85# if defined(BOOTLOADER_KIIBOHD) 85# if defined(BOOTLOADER_KIIBOHD)
diff --git a/tmk_core/common/chibios/chibios_config.h b/tmk_core/common/chibios/chibios_config.h
index b4d96465d..1d8ace495 100644
--- a/tmk_core/common/chibios/chibios_config.h
+++ b/tmk_core/common/chibios/chibios_config.h
@@ -15,6 +15,8 @@
15 */ 15 */
16#pragma once 16#pragma once
17 17
18#define SPLIT_USB_DETECT // Force this on for now
19
18#if defined(STM32F1XX) 20#if defined(STM32F1XX)
19# define USE_GPIOV1 21# define USE_GPIOV1
20#endif 22#endif
@@ -28,4 +30,9 @@
28# define USE_I2CV1 30# define USE_I2CV1
29# 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
30# define USE_GPIOV1 32# define USE_GPIOV1
33# define STM32_SYSCLK KINETIS_SYSCLK_FREQUENCY
34#endif
35
36#if defined(MK66F18)
37# define STM32_SYSCLK KINETIS_SYSCLK_FREQUENCY
31#endif 38#endif
diff --git a/tmk_core/common/chibios/pin_defs.h b/tmk_core/common/chibios/pin_defs.h
index 86bc1076e..c03f8de0c 100644
--- a/tmk_core/common/chibios/pin_defs.h
+++ b/tmk_core/common/chibios/pin_defs.h
@@ -70,6 +70,23 @@
70# define A13 PAL_LINE(GPIOA, 13) 70# define A13 PAL_LINE(GPIOA, 13)
71# define A14 PAL_LINE(GPIOA, 14) 71# define A14 PAL_LINE(GPIOA, 14)
72# define A15 PAL_LINE(GPIOA, 15) 72# define A15 PAL_LINE(GPIOA, 15)
73# define A16 PAL_LINE(GPIOA, 16)
74# define A17 PAL_LINE(GPIOA, 17)
75# define A18 PAL_LINE(GPIOA, 18)
76# define A19 PAL_LINE(GPIOA, 19)
77# define A20 PAL_LINE(GPIOA, 20)
78# define A21 PAL_LINE(GPIOA, 21)
79# define A22 PAL_LINE(GPIOA, 22)
80# define A23 PAL_LINE(GPIOA, 23)
81# define A24 PAL_LINE(GPIOA, 24)
82# define A25 PAL_LINE(GPIOA, 25)
83# define A26 PAL_LINE(GPIOA, 26)
84# define A27 PAL_LINE(GPIOA, 27)
85# define A28 PAL_LINE(GPIOA, 28)
86# define A29 PAL_LINE(GPIOA, 29)
87# define A30 PAL_LINE(GPIOA, 30)
88# define A31 PAL_LINE(GPIOA, 31)
89# define A32 PAL_LINE(GPIOA, 32)
73# define B0 PAL_LINE(GPIOB, 0) 90# define B0 PAL_LINE(GPIOB, 0)
74# define B1 PAL_LINE(GPIOB, 1) 91# define B1 PAL_LINE(GPIOB, 1)
75# define B2 PAL_LINE(GPIOB, 2) 92# define B2 PAL_LINE(GPIOB, 2)
@@ -90,6 +107,19 @@
90# define B17 PAL_LINE(GPIOB, 17) 107# define B17 PAL_LINE(GPIOB, 17)
91# define B18 PAL_LINE(GPIOB, 18) 108# define B18 PAL_LINE(GPIOB, 18)
92# define B19 PAL_LINE(GPIOB, 19) 109# define B19 PAL_LINE(GPIOB, 19)
110# define B20 PAL_LINE(GPIOB, 20)
111# define B21 PAL_LINE(GPIOB, 21)
112# define B22 PAL_LINE(GPIOB, 22)
113# define B23 PAL_LINE(GPIOB, 23)
114# define B24 PAL_LINE(GPIOB, 24)
115# define B25 PAL_LINE(GPIOB, 25)
116# define B26 PAL_LINE(GPIOB, 26)
117# define B27 PAL_LINE(GPIOB, 27)
118# define B28 PAL_LINE(GPIOB, 28)
119# define B29 PAL_LINE(GPIOB, 29)
120# define B30 PAL_LINE(GPIOB, 30)
121# define B31 PAL_LINE(GPIOB, 31)
122# define B32 PAL_LINE(GPIOB, 32)
93# define C0 PAL_LINE(GPIOC, 0) 123# define C0 PAL_LINE(GPIOC, 0)
94# define C1 PAL_LINE(GPIOC, 1) 124# define C1 PAL_LINE(GPIOC, 1)
95# define C2 PAL_LINE(GPIOC, 2) 125# define C2 PAL_LINE(GPIOC, 2)
@@ -106,6 +136,23 @@
106# define C13 PAL_LINE(GPIOC, 13) 136# define C13 PAL_LINE(GPIOC, 13)
107# define C14 PAL_LINE(GPIOC, 14) 137# define C14 PAL_LINE(GPIOC, 14)
108# define C15 PAL_LINE(GPIOC, 15) 138# define C15 PAL_LINE(GPIOC, 15)
139# define C16 PAL_LINE(GPIOC, 16)
140# define C17 PAL_LINE(GPIOC, 17)
141# define C18 PAL_LINE(GPIOC, 18)
142# define C19 PAL_LINE(GPIOC, 19)
143# define C20 PAL_LINE(GPIOC, 20)
144# define C21 PAL_LINE(GPIOC, 21)
145# define C22 PAL_LINE(GPIOC, 22)
146# define C23 PAL_LINE(GPIOC, 23)
147# define C24 PAL_LINE(GPIOC, 24)
148# define C25 PAL_LINE(GPIOC, 25)
149# define C26 PAL_LINE(GPIOC, 26)
150# define C27 PAL_LINE(GPIOC, 27)
151# define C28 PAL_LINE(GPIOC, 28)
152# define C29 PAL_LINE(GPIOC, 29)
153# define C30 PAL_LINE(GPIOC, 30)
154# define C31 PAL_LINE(GPIOC, 31)
155# define C32 PAL_LINE(GPIOC, 32)
109# define D0 PAL_LINE(GPIOD, 0) 156# define D0 PAL_LINE(GPIOD, 0)
110# define D1 PAL_LINE(GPIOD, 1) 157# define D1 PAL_LINE(GPIOD, 1)
111# define D2 PAL_LINE(GPIOD, 2) 158# define D2 PAL_LINE(GPIOD, 2)
@@ -122,6 +169,23 @@
122# define D13 PAL_LINE(GPIOD, 13) 169# define D13 PAL_LINE(GPIOD, 13)
123# define D14 PAL_LINE(GPIOD, 14) 170# define D14 PAL_LINE(GPIOD, 14)
124# define D15 PAL_LINE(GPIOD, 15) 171# define D15 PAL_LINE(GPIOD, 15)
172# define D16 PAL_LINE(GPIOD, 16)
173# define D17 PAL_LINE(GPIOD, 17)
174# define D18 PAL_LINE(GPIOD, 18)
175# define D19 PAL_LINE(GPIOD, 19)
176# define D20 PAL_LINE(GPIOD, 20)
177# define D21 PAL_LINE(GPIOD, 21)
178# define D22 PAL_LINE(GPIOD, 22)
179# define D23 PAL_LINE(GPIOD, 23)
180# define D24 PAL_LINE(GPIOD, 24)
181# define D25 PAL_LINE(GPIOD, 25)
182# define D26 PAL_LINE(GPIOD, 26)
183# define D27 PAL_LINE(GPIOD, 27)
184# define D28 PAL_LINE(GPIOD, 28)
185# define D29 PAL_LINE(GPIOD, 29)
186# define D30 PAL_LINE(GPIOD, 30)
187# define D31 PAL_LINE(GPIOD, 31)
188# define D32 PAL_LINE(GPIOD, 32)
125# define E0 PAL_LINE(GPIOE, 0) 189# define E0 PAL_LINE(GPIOE, 0)
126# define E1 PAL_LINE(GPIOE, 1) 190# define E1 PAL_LINE(GPIOE, 1)
127# define E2 PAL_LINE(GPIOE, 2) 191# define E2 PAL_LINE(GPIOE, 2)
@@ -138,6 +202,23 @@
138# define E13 PAL_LINE(GPIOE, 13) 202# define E13 PAL_LINE(GPIOE, 13)
139# define E14 PAL_LINE(GPIOE, 14) 203# define E14 PAL_LINE(GPIOE, 14)
140# define E15 PAL_LINE(GPIOE, 15) 204# define E15 PAL_LINE(GPIOE, 15)
205# define E16 PAL_LINE(GPIOE, 16)
206# define E17 PAL_LINE(GPIOE, 17)
207# define E18 PAL_LINE(GPIOE, 18)
208# define E19 PAL_LINE(GPIOE, 19)
209# define E20 PAL_LINE(GPIOE, 20)
210# define E21 PAL_LINE(GPIOE, 21)
211# define E22 PAL_LINE(GPIOE, 22)
212# define E23 PAL_LINE(GPIOE, 23)
213# define E24 PAL_LINE(GPIOE, 24)
214# define E25 PAL_LINE(GPIOE, 25)
215# define E26 PAL_LINE(GPIOE, 26)
216# define E27 PAL_LINE(GPIOE, 27)
217# define E28 PAL_LINE(GPIOE, 28)
218# define E29 PAL_LINE(GPIOE, 29)
219# define E30 PAL_LINE(GPIOE, 30)
220# define E31 PAL_LINE(GPIOE, 31)
221# define E32 PAL_LINE(GPIOE, 32)
141# define F0 PAL_LINE(GPIOF, 0) 222# define F0 PAL_LINE(GPIOF, 0)
142# define F1 PAL_LINE(GPIOF, 1) 223# define F1 PAL_LINE(GPIOF, 1)
143# define F2 PAL_LINE(GPIOF, 2) 224# define F2 PAL_LINE(GPIOF, 2)
diff --git a/tmk_core/common/chibios/sleep_led.c b/tmk_core/common/chibios/sleep_led.c
index 5595eec0e..477056a45 100644
--- a/tmk_core/common/chibios/sleep_led.c
+++ b/tmk_core/common/chibios/sleep_led.c
@@ -9,21 +9,13 @@
9 * Use LP timer on Kinetises, TIM14 on STM32F0. 9 * Use LP timer on Kinetises, TIM14 on STM32F0.
10 */ 10 */
11 11
12#if defined(KL2x) || defined(K20x) 12#ifndef SLEEP_LED_GPT_DRIVER
13 13# if defined(STM32F0XX)
14/* Use Low Power Timer (LPTMR) */ 14# define SLEEP_LED_GPT_DRIVER GPTD14
15# define TIMER_INTERRUPT_VECTOR KINETIS_LPTMR0_IRQ_VECTOR 15# endif
16# define RESET_COUNTER LPTMR0->CSR |= LPTMRx_CSR_TCF
17
18#elif defined(STM32F0XX)
19
20/* Use TIM14 manually */
21# define TIMER_INTERRUPT_VECTOR STM32_TIM14_HANDLER
22# define RESET_COUNTER STM32_TIM14->SR &= ~STM32_TIM_SR_UIF
23
24#endif 16#endif
25 17
26#if defined(KL2x) || defined(K20x) || defined(STM32F0XX) /* common parts for timers/interrupts */ 18#if defined(KL2x) || defined(K20x) || defined(SLEEP_LED_GPT_DRIVER) /* common parts for timers/interrupts */
27 19
28/* Breathing Sleep LED brighness(PWM On period) table 20/* Breathing Sleep LED brighness(PWM On period) table
29 * (64[steps] * 4[duration]) / 64[PWM periods/s] = 4 second breath cycle 21 * (64[steps] * 4[duration]) / 64[PWM periods/s] = 4 second breath cycle
@@ -33,10 +25,7 @@
33 */ 25 */
34static const uint8_t breathing_table[64] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4, 6, 10, 15, 23, 32, 44, 58, 74, 93, 113, 135, 157, 179, 199, 218, 233, 245, 252, 255, 252, 245, 233, 218, 199, 179, 157, 135, 113, 93, 74, 58, 44, 32, 23, 15, 10, 6, 4, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 26static const uint8_t breathing_table[64] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4, 6, 10, 15, 23, 32, 44, 58, 74, 93, 113, 135, 157, 179, 199, 218, 233, 245, 252, 255, 252, 245, 233, 218, 199, 179, 157, 135, 113, 93, 74, 58, 44, 32, 23, 15, 10, 6, 4, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
35 27
36/* interrupt handler */ 28void sleep_led_timer_callback(void) {
37OSAL_IRQ_HANDLER(TIMER_INTERRUPT_VECTOR) {
38 OSAL_IRQ_PROLOGUE();
39
40 /* Software PWM 29 /* Software PWM
41 * timer:1111 1111 1111 1111 30 * timer:1111 1111 1111 1111
42 * \_____/\/ \_______/____ count(0-255) 31 * \_____/\/ \_______/____ count(0-255)
@@ -64,17 +53,16 @@ OSAL_IRQ_HANDLER(TIMER_INTERRUPT_VECTOR) {
64 if (timer.pwm.count == breathing_table[timer.pwm.index]) { 53 if (timer.pwm.count == breathing_table[timer.pwm.index]) {
65 led_set(0); 54 led_set(0);
66 } 55 }
67
68 /* Reset the counter */
69 RESET_COUNTER;
70
71 OSAL_IRQ_EPILOGUE();
72} 56}
73 57
74#endif /* common parts for known platforms */ 58#endif /* common parts for known platforms */
75 59
76#if defined(KL2x) || defined(K20x) /* platform selection: familiar Kinetis chips */ 60#if defined(KL2x) || defined(K20x) /* platform selection: familiar Kinetis chips */
77 61
62/* Use Low Power Timer (LPTMR) */
63# define TIMER_INTERRUPT_VECTOR KINETIS_LPTMR0_IRQ_VECTOR
64# define RESET_COUNTER LPTMR0->CSR |= LPTMRx_CSR_TCF
65
78/* LPTMR clock options */ 66/* LPTMR clock options */
79# define LPTMR_CLOCK_MCGIRCLK 0 /* 4MHz clock */ 67# define LPTMR_CLOCK_MCGIRCLK 0 /* 4MHz clock */
80# define LPTMR_CLOCK_LPO 1 /* 1kHz clock */ 68# define LPTMR_CLOCK_LPO 1 /* 1kHz clock */
@@ -86,6 +74,18 @@ OSAL_IRQ_HANDLER(TIMER_INTERRUPT_VECTOR) {
86# define SIM_SCGC5_LPTMR SIM_SCGC5_LPTIMER 74# define SIM_SCGC5_LPTMR SIM_SCGC5_LPTIMER
87# endif 75# endif
88 76
77/* interrupt handler */
78OSAL_IRQ_HANDLER(TIMER_INTERRUPT_VECTOR) {
79 OSAL_IRQ_PROLOGUE();
80
81 sleep_led_timer_callback();
82
83 /* Reset the counter */
84 RESET_COUNTER;
85
86 OSAL_IRQ_EPILOGUE();
87}
88
89/* Initialise the timer */ 89/* Initialise the timer */
90void sleep_led_init(void) { 90void sleep_led_init(void) {
91 /* Make sure the clock to the LPTMR is enabled */ 91 /* Make sure the clock to the LPTMR is enabled */
@@ -159,45 +159,23 @@ void sleep_led_toggle(void) {
159 LPTMR0->CSR ^= LPTMRx_CSR_TEN; 159 LPTMR0->CSR ^= LPTMRx_CSR_TEN;
160} 160}
161 161
162#elif defined(STM32F0XX) /* platform selection: STM32F0XX */ 162#elif defined(SLEEP_LED_GPT_DRIVER)
163
164/* Initialise the timer */
165void sleep_led_init(void) {
166 /* enable clock */
167 rccEnableTIM14(FALSE); /* low power enable = FALSE */
168 rccResetTIM14();
169
170 /* prescale */
171 /* Assuming 48MHz internal clock */
172 /* getting cca 65484 irqs/sec */
173 STM32_TIM14->PSC = 733;
174 163
175 /* auto-reload */ 164static void gptTimerCallback(GPTDriver *gptp) {
176 /* 0 => interrupt every time */ 165 (void)gptp;
177 STM32_TIM14->ARR = 3; 166 sleep_led_timer_callback();
167}
178 168
179 /* enable counter update event interrupt */ 169static const GPTConfig gptcfg = {1000000, gptTimerCallback, 0, 0};
180 STM32_TIM14->DIER |= STM32_TIM_DIER_UIE;
181 170
182 /* register interrupt vector */ 171/* Initialise the timer */
183 nvicEnableVector(STM32_TIM14_NUMBER, 2); /* vector, priority */ 172void sleep_led_init(void) { gptStart(&SLEEP_LED_GPT_DRIVER, &gptcfg); }
184}
185 173
186void sleep_led_enable(void) { 174void sleep_led_enable(void) { gptStartContinuous(&SLEEP_LED_GPT_DRIVER, gptcfg.frequency / 0xFFFF); }
187 /* Enable the timer */
188 STM32_TIM14->CR1 = STM32_TIM_CR1_CEN | STM32_TIM_CR1_URS;
189 /* URS => update event only on overflow; setting UG bit disabled */
190}
191 175
192void sleep_led_disable(void) { 176void sleep_led_disable(void) { gptStopTimer(&SLEEP_LED_GPT_DRIVER); }
193 /* Disable the timer */
194 STM32_TIM14->CR1 = 0;
195}
196 177
197void sleep_led_toggle(void) { 178void sleep_led_toggle(void) { (SLEEP_LED_GPT_DRIVER.state == GPT_READY) ? sleep_led_enable() : sleep_led_disable(); }
198 /* Toggle the timer */
199 STM32_TIM14->CR1 ^= STM32_TIM_CR1_CEN;
200}
201 179
202#else /* platform selection: not on familiar chips */ 180#else /* platform selection: not on familiar chips */
203 181
diff --git a/tmk_core/common/chibios/suspend.c b/tmk_core/common/chibios/suspend.c
index 49e20641f..b3949185e 100644
--- a/tmk_core/common/chibios/suspend.c
+++ b/tmk_core/common/chibios/suspend.c
@@ -24,6 +24,13 @@
24# include "rgblight.h" 24# include "rgblight.h"
25#endif 25#endif
26 26
27#ifdef LED_MATRIX_ENABLE
28# include "led_matrix.h"
29#endif
30#ifdef RGB_MATRIX_ENABLE
31# include "rgb_matrix.h"
32#endif
33
27/** \brief suspend idle 34/** \brief suspend idle
28 * 35 *
29 * FIXME: needs doc 36 * FIXME: needs doc
@@ -53,6 +60,13 @@ void suspend_power_down(void) {
53 backlight_set(0); 60 backlight_set(0);
54#endif 61#endif
55 62
63#ifdef LED_MATRIX_ENABLE
64 led_matrix_task();
65#endif
66#ifdef RGB_MATRIX_ENABLE
67 rgb_matrix_task();
68#endif
69
56 // Turn off LED indicators 70 // Turn off LED indicators
57 uint8_t leds_off = 0; 71 uint8_t leds_off = 0;
58#if defined(BACKLIGHT_CAPS_LOCK) && defined(BACKLIGHT_ENABLE) 72#if defined(BACKLIGHT_CAPS_LOCK) && defined(BACKLIGHT_ENABLE)
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/eeconfig.c b/tmk_core/common/eeconfig.c
index 5e3ebe6ee..92a509217 100644
--- a/tmk_core/common/eeconfig.c
+++ b/tmk_core/common/eeconfig.c
@@ -57,7 +57,7 @@ void eeconfig_init_quantum(void) {
57 eeprom_update_dword(EECONFIG_HAPTIC, 0); 57 eeprom_update_dword(EECONFIG_HAPTIC, 0);
58 eeprom_update_byte(EECONFIG_VELOCIKEY, 0); 58 eeprom_update_byte(EECONFIG_VELOCIKEY, 0);
59 eeprom_update_dword(EECONFIG_RGB_MATRIX, 0); 59 eeprom_update_dword(EECONFIG_RGB_MATRIX, 0);
60 eeprom_update_byte(EECONFIG_RGB_MATRIX_SPEED, 0); 60 eeprom_update_word(EECONFIG_RGB_MATRIX_EXTENDED, 0);
61 61
62 // TODO: Remove once ARM has a way to configure EECONFIG_HANDEDNESS 62 // TODO: Remove once ARM has a way to configure EECONFIG_HANDEDNESS
63 // within the emulated eeprom via dfu-util or another tool 63 // within the emulated eeprom via dfu-util or another tool
diff --git a/tmk_core/common/eeconfig.h b/tmk_core/common/eeconfig.h
index 86b9e6f99..9e18fd4e1 100644
--- a/tmk_core/common/eeconfig.h
+++ b/tmk_core/common/eeconfig.h
@@ -21,7 +21,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
21#include <stdbool.h> 21#include <stdbool.h>
22 22
23#ifndef EECONFIG_MAGIC_NUMBER 23#ifndef EECONFIG_MAGIC_NUMBER
24# define EECONFIG_MAGIC_NUMBER (uint16_t)0xFEEB // When changing, decrement this value to avoid future re-init issues 24# define EECONFIG_MAGIC_NUMBER (uint16_t)0xFEEA // When changing, decrement this value to avoid future re-init issues
25#endif 25#endif
26#define EECONFIG_MAGIC_NUMBER_OFF (uint16_t)0xFFFF 26#define EECONFIG_MAGIC_NUMBER_OFF (uint16_t)0xFFFF
27 27
@@ -43,12 +43,18 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
43#define EECONFIG_VELOCIKEY (uint8_t *)23 43#define EECONFIG_VELOCIKEY (uint8_t *)23
44 44
45#define EECONFIG_HAPTIC (uint32_t *)24 45#define EECONFIG_HAPTIC (uint32_t *)24
46
47// Mutually exclusive
48#define EECONFIG_LED_MATRIX (uint32_t *)28
46#define EECONFIG_RGB_MATRIX (uint32_t *)28 49#define EECONFIG_RGB_MATRIX (uint32_t *)28
47#define EECONFIG_RGB_MATRIX_SPEED (uint8_t *)32 50// Speed & Flags
51#define EECONFIG_LED_MATRIX_EXTENDED (uint16_t *)32
52#define EECONFIG_RGB_MATRIX_EXTENDED (uint16_t *)32
53
48// TODO: Combine these into a single word and single block of EEPROM 54// TODO: Combine these into a single word and single block of EEPROM
49#define EECONFIG_KEYMAP_UPPER_BYTE (uint8_t *)33 55#define EECONFIG_KEYMAP_UPPER_BYTE (uint8_t *)34
50// Size of EEPROM being used, other code can refer to this for available EEPROM 56// Size of EEPROM being used, other code can refer to this for available EEPROM
51#define EECONFIG_SIZE 34 57#define EECONFIG_SIZE 35
52/* debug bit */ 58/* debug bit */
53#define EECONFIG_DEBUG_ENABLE (1 << 0) 59#define EECONFIG_DEBUG_ENABLE (1 << 0)
54#define EECONFIG_DEBUG_MATRIX (1 << 1) 60#define EECONFIG_DEBUG_MATRIX (1 << 1)
diff --git a/tmk_core/common/keyboard.c b/tmk_core/common/keyboard.c
index ce3255c06..132affe7a 100644
--- a/tmk_core/common/keyboard.c
+++ b/tmk_core/common/keyboard.c
@@ -34,11 +34,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
34#ifdef BACKLIGHT_ENABLE 34#ifdef BACKLIGHT_ENABLE
35# include "backlight.h" 35# include "backlight.h"
36#endif 36#endif
37#ifdef BOOTMAGIC_ENABLE
38# include "bootmagic.h"
39#else
40# include "magic.h"
41#endif
42#ifdef MOUSEKEY_ENABLE 37#ifdef MOUSEKEY_ENABLE
43# include "mousekey.h" 38# include "mousekey.h"
44#endif 39#endif
@@ -54,6 +49,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
54#ifdef RGBLIGHT_ENABLE 49#ifdef RGBLIGHT_ENABLE
55# include "rgblight.h" 50# include "rgblight.h"
56#endif 51#endif
52#ifdef LED_MATRIX_ENABLE
53# include "led_matrix.h"
54#endif
57#ifdef RGB_MATRIX_ENABLE 55#ifdef RGB_MATRIX_ENABLE
58# include "rgb_matrix.h" 56# include "rgb_matrix.h"
59#endif 57#endif
@@ -296,11 +294,6 @@ void keyboard_init(void) {
296#ifdef ADB_MOUSE_ENABLE 294#ifdef ADB_MOUSE_ENABLE
297 adb_mouse_init(); 295 adb_mouse_init();
298#endif 296#endif
299#ifdef BOOTMAGIC_ENABLE
300 bootmagic();
301#else
302 magic();
303#endif
304#ifdef BACKLIGHT_ENABLE 297#ifdef BACKLIGHT_ENABLE
305 backlight_init(); 298 backlight_init();
306#endif 299#endif
@@ -337,6 +330,9 @@ void keyboard_init(void) {
337 * This is differnet than keycode events as no layer processing, or filtering occurs. 330 * This is differnet than keycode events as no layer processing, or filtering occurs.
338 */ 331 */
339void switch_events(uint8_t row, uint8_t col, bool pressed) { 332void switch_events(uint8_t row, uint8_t col, bool pressed) {
333#if defined(LED_MATRIX_ENABLE)
334 process_led_matrix(row, col, pressed);
335#endif
340#if defined(RGB_MATRIX_ENABLE) 336#if defined(RGB_MATRIX_ENABLE)
341 process_rgb_matrix(row, col, pressed); 337 process_rgb_matrix(row, col, pressed);
342#endif 338#endif
@@ -422,6 +418,9 @@ MATRIX_LOOP_END:
422 rgblight_task(); 418 rgblight_task();
423#endif 419#endif
424 420
421#ifdef LED_MATRIX_ENABLE
422 led_matrix_task();
423#endif
425#ifdef RGB_MATRIX_ENABLE 424#ifdef RGB_MATRIX_ENABLE
426 rgb_matrix_task(); 425 rgb_matrix_task();
427#endif 426#endif
diff --git a/tmk_core/common/magic.c b/tmk_core/common/magic.c
deleted file mode 100644
index e14994164..000000000
--- a/tmk_core/common/magic.c
+++ /dev/null
@@ -1,39 +0,0 @@
1#include <stdint.h>
2#include <stdbool.h>
3#if defined(__AVR__)
4# include <util/delay.h>
5#endif
6#include "matrix.h"
7#include "bootloader.h"
8#include "debug.h"
9#include "keymap.h"
10#include "host.h"
11#include "action_layer.h"
12#include "eeconfig.h"
13#include "magic.h"
14
15keymap_config_t keymap_config;
16
17/** \brief Magic
18 *
19 * FIXME: Needs doc
20 */
21void magic(void) {
22 /* check signature */
23 if (!eeconfig_is_enabled()) {
24 eeconfig_init();
25 }
26
27 /* debug enable */
28 debug_config.raw = eeconfig_read_debug();
29
30 /* keymap config */
31 keymap_config.raw = eeconfig_read_keymap();
32
33 uint8_t default_layer = 0;
34 default_layer = eeconfig_read_default_layer();
35 default_layer_set((layer_state_t)default_layer);
36
37 /* Also initialize layer state to trigger callback functions for layer_state */
38 layer_state_set_kb((layer_state_t)layer_state);
39}
diff --git a/tmk_core/common/magic.h b/tmk_core/common/magic.h
deleted file mode 100644
index a6552c04d..000000000
--- a/tmk_core/common/magic.h
+++ /dev/null
@@ -1,3 +0,0 @@
1#pragma once
2
3void magic(void);
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/usb_util.c b/tmk_core/common/usb_util.c
new file mode 100644
index 000000000..e4c50fcb1
--- /dev/null
+++ b/tmk_core/common/usb_util.c
@@ -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#include "usb_util.h"
17#include "wait.h"
18
19__attribute__((weak)) void usb_disable(void) {}
20__attribute__((weak)) bool usb_connected_state(void) { return true; }
21__attribute__((weak)) bool usb_vbus_state(void) {
22#ifdef USB_VBUS_PIN
23 setPinInput(USB_VBUS_PIN);
24 wait_us(5);
25 return readPin(USB_VBUS_PIN);
26#else
27 return true;
28#endif
29}
diff --git a/tmk_core/common/usb_util.h b/tmk_core/common/usb_util.h
new file mode 100644
index 000000000..4ebedb1e7
--- /dev/null
+++ b/tmk_core/common/usb_util.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 <stdbool.h>
19
20void usb_disable(void);
21bool usb_connected_state(void);
22bool usb_vbus_state(void);
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