aboutsummaryrefslogtreecommitdiff
path: root/tmk_core/common
diff options
context:
space:
mode:
authorJames Young <18669334+noroadsleft@users.noreply.github.com>2021-05-29 14:38:50 -0700
committerGitHub <noreply@github.com>2021-05-29 14:38:50 -0700
commit1646c0f26cfa21a7023d404008e4d0aa4917193d (patch)
tree337ab0498a929285a234518fee34a4d9dcf51656 /tmk_core/common
parentf55e39e8a2246f6f96fd5d4a84a866e2615cde7b (diff)
downloadqmk_firmware-1646c0f26cfa21a7023d404008e4d0aa4917193d.tar.gz
qmk_firmware-1646c0f26cfa21a7023d404008e4d0aa4917193d.zip
2021 May 29 Breaking Changes Update (#13034)
* Add Per Key functionality for AutoShift (#11536) * LED Matrix: Reactive effect buffers & advanced indicators (#12588) * [Keyboard] kint36: switch to sym_eager_pk debouncing (#12626) * [Keyboard] kint2pp: reduce input latency by ≈10ms (#12625) * LED Matrix: Split (#12633) * [CI] Format code according to conventions (#12650) * feat: infinite timeout for leader key (#6580) * feat: implement leader_no_timeout logic * docs(leader_key): infinite leader timeout docs * Format code according to conventions (#12680) * Update ADC driver for STM32F1xx, STM32F3xx, STM32F4xx (#12403) * Fix default ADC_RESOLUTION for ADCv3 (and ADCv4) Recent ChibiOS update removed ADC_CFGR1_RES_10BIT from the ADCv3 headers (that macro should not have been there, because ADCv3 has CFGR instead of CFGR1). Fix the default value for ADC_RESOLUTION to use ADC_CFGR_RES_10BITS if it is defined (that name is used for ADCv3 and ADCv4). * Update ADC docs to match the actually used resolution ADC driver for ChibiOS actually uses the 10-bit resolution by default (probably to match AVR); fix the documentation accordingly. Also add both ADC_CFGR_RES_10BITS and ADC_CFGR1_RES_10BIT constants (these names differ according to the ADC implementation in the particular MCU). * Fix pinToMux() for B12 and B13 on STM32F3xx Testing on STM32F303CCT6 revealed that the ADC mux values for B12 and B13 pins were wrong. * Add support for all possible analog pins on STM32F1xx Added ADC mux values for pins A0...A7, B0, B1, C0...C5 on STM32F1xx (they are the same at least for STM32F103x8 and larger F103 devices, and also F102, F105, F107 families). Actually tested on STM32F103C8T6 (therefore pins C0...C5 were not tested). Pins F6...F10, which are present on STM32F103x[C-G] in 144-pin packages, cannot be supported at the moment, because those pins are connected only to ADC3, but the ChibiOS ADC driver for STM32F1xx supports only ADC1. * Add support for all possible analog pins on STM32F4xx Added ADC mux values for pins A0...A7, B0, B1, C0...C5 and optionally F3...F10 (if STM32_ADC_USE_ADC3 is enabled). These mux values are apparently the same for all F4xx devices, except some smaller devices may not have ADC3. Actually tested on STM32F401CCU6, STM32F401CEU6, STM32F411CEU6 (using various WeAct “Blackpill” boards); only pins A0...A7, B0, B1 were tested. Pins F3...F10 are inside `#if STM32_ADC_USE_ADC3` because some devices which don't have ADC3 also don't have the GPIOF port, therefore the code which refers to Fx pins does not compile. * Fix STM32F3xx ADC mux table in documentation The ADC driver documentation had some errors in the mux table for STM32F3xx. Fix this table to match the datasheet and the actual code (mux settings for B12 and B13 were also tested on a real STM32F303CCT6 chip). * Add STM32F1xx ADC pins to the documentation * Add STM32F4xx ADC pins to the documentation * Add initial support for tinyuf2 bootloader (when hosted on F411 blackpill) (#12600) * Add support for jumping to tinyuf2 bootloader. Adds blackpill UF2 example. * Update flashing.md * Update chconf.h * Update config.h * Update halconf.h * Update mcuconf.h * eeprom driver: Refactor where eeprom driver initialisation (and EEPROM emulation initialisation) occurs to make it non-target-specific. (#12671) * Add support for MCU = STM32F446 (#12619) * Add support for MCU = STM32F446 * Update platforms/chibios/GENERIC_STM32_F446XE/configs/config.h * Restore mcuconf.h to the one used by RT-STM32F446RE-NUCLEO64 * stm32f446: update mcuconf.h and board.h for 16MHz operation, with USB enabled, and other peripherals disabled. * Format code according to conventions (#12682) * Format code according to conventions (#12687) * Add STM32L433 and L443 support (#12063) * initial L433 commit * change to XC * fix L433 * disable all peripherals * update system and peripheral clocks * 433 change * use its own board files * revert its own board files * l433 specific change * fix stm32l432xx define * remove duplicate #define * fix bootloader jump * move to L443xx and add i2c2, spi2, usart3 to mcuconf.h * move to L443 * move to L443 * fix sdmmc in mcuconf.h * include STM32L443 * add L443 * Include L443 in compatible microcontrollers * Include L443 in compatible microcontrollers * Update config bootloader jump description * Update ChibiOS define reasoning * Update quantum/mcu_selection.mk * fix git conflict * Updated Function96 with V2 files and removed chconf.h and halconf.h (#12613) * Fix bad PR merge for #6580. (#12721) * Change RGB/LED Matrix to use a simple define for USB suspend (#12697) * [CI] Format code according to conventions (#12731) * Fixing transport's led/rgb matrix suspend state logic (#12770) * [CI] Format code according to conventions (#12772) * Fix comment parsing (#12750) * Added OLED fade out support (#12086) * fix some references to bin/qmk that slipped in (#12832) * Resolve a number of warnings in `qmk generate-api` (#12833) * New command: qmk console (#12828) * stash poc * stash * tidy up implementation * Tidy up slightly for review * Tidy up slightly for review * Bodge environment to make tests pass * Refactor away from asyncio due to windows issues * Filter devices * align vid/pid printing * Add hidapi to the installers * start preparing for multiple hid_listeners * udev rules for hid_listen * refactor to move closer to end state * very basic implementation of the threaded model * refactor how vid/pid/index are supplied and parsed * windows improvements * read the report directly when usage page isn't available * add per-device colors, the choice to show names or numbers, and refactor * add timestamps * Add support for showing bootloaders * tweak the color for bootloaders * Align bootloader disconnect with connect color * add support for showing all bootloaders * fix the pyusb check * tweaks * fix exception * hide a stack trace behind -v * add --no-bootloaders option * add documentation for qmk console * Apply suggestions from code review * pyformat * clean up and flesh out KNOWN_BOOTLOADERS * Remove pointless SERIAL_LINK_ENABLE rules (#12846) * Make Swap Hands use PROGMEM (#12284) This converts the array that the Swap Hands feature uses to use PROGMEM, and to read from that array, as such. Since this array never changes at runtime, there is no reason to keep it in memory. Especially for AVR boards, as memory is a precious resource. * Fix another bin/qmk reference (#12856) * [Keymap] Turn OLED off on suspend in soundmonster keymap (#10419) * Fixup build errors on `develop` branch. (#12723) * LED Matrix: Effects! (#12651) * Fix syntax error when compiling for ARM (#12866) * Remove KEYMAP and LAYOUT_kc (#12160) * alias KEYMAP to LAYOUT * remove KEYMAP and LAYOUT_kc * Add setup, clone, and env to the list of commands we allow even with broken modules (#12868) * Rename `point_t` -> `led_point_t` (#12864) * [Keyboard] updated a vendor name / fixed minor keymap issues (#12881) * Add missing LED Matrix suspend code to suspend.c (#12878) * LED Matrix: Documentation (#12685) * Deprecate `send_unicode_hex_string()` (#12602) * Fix spelling mistake regarding LED Matrix in split_common. (#12888) * [Keymap] Fix QWERTY/DVORAK status output for kzar keymap (#12895) * Use milc.subcommand.config instead of qmk.cli.config (#12915) * Use milc.subcommand.config instead * pyformat * remove the config test * Add function to allow repeated blinking of one layer (#12237) * Implement function rgblight_blink_layer_repeat to allow repeated blinking of one layer at a time * Update doc * Rework rgblight blinking according to requested change * optimize storage * Fixup housekeeping from being invoked twice per loop. (#12933) * matrix: wait for row signal to go HIGH for every row (#12945) I noticed this discrepancy (last row of the matrix treated differently than the others) when optimizing the input latency of my keyboard controller, see also https://michael.stapelberg.ch/posts/2021-05-08-keyboard-input-latency-qmk-kinesis/ Before this commit, when tuning the delays I noticed ghost key presses when pressing the F2 key, which is on the last row of the keyboard matrix: the dead_grave key, which is on the first row of the keyboard matrix, would be incorrectly detected as pressed. After this commit, all keyboard matrix rows are interpreted correctly. I suspect that my setup is more susceptible to this nuance than others because I use GPIO_INPUT_PIN_DELAY=0 and hence don’t have another delay that might mask the problem. * ensure we do not conflict with existing keymap aliases (#12976) * Add support for up to 4 IS31FL3733 drivers (#12342) * Convert Encoder callbacks to be boolean functions (#12805) * [Keyboard] Fix Terrazzo build failure (#12977) * Do not hard set config in CPTC files (#11864) * [Keyboard] Corne - Remove legacy revision support (#12226) * [Keymap] Update to Drashna keymap and user code (based on develop) (#12936) * Add Full-duplex serial driver for ARM boards (#9842) * Document LED_MATRIX_FRAMEBUFFER_EFFECTS (#12987) * Backlight: add defines for default level and breathing state (#12560) * Add dire message about LUFA mass storage bootloader (#13014) * [Keyboard] Remove redundant legacy and common headers for crkbd (#13023) Was causing compiler errors on some systems. * Fix keyboards/keymaps for boolean encoder callback changes (#12985) * `backlight.c`: include `eeprom.h` (#13024) * Add changelog for 2021-05-29 Breaking Changes merge (#12939) * Add ChangeLog for 2021-05-29 Breaking Changes Merge: initial version * Add recent develop changes * Sort recent develop changes * Remove sections for ChibiOS changes per tzarc No ChibiOS changes this round. * Add and sort recent develop changes * add notes about keyboard moves/deletions * import changelog for PR 12172 Documents the change to BOOTMAGIC_ENABLE. * update section headings * re-sort changelog * add additional note regarding Bootmagic changes * remove changelog timestamp * update dates in main Breaking Changes docs * fix broken section anchors in previous changelogs * add link to backlight/eeprom patch to changelog * highlight some more changes * link PRs from section headers * Restore standard readme * run: qmk cformat --core-only
Diffstat (limited to 'tmk_core/common')
-rw-r--r--tmk_core/common/action.c86
-rw-r--r--tmk_core/common/action.h4
-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.c21
-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.c20
-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.c92
-rw-r--r--tmk_core/common/chibios/suspend.c28
-rw-r--r--tmk_core/common/chibios/wait.c89
-rw-r--r--tmk_core/common/eeconfig.c13
-rw-r--r--tmk_core/common/eeconfig.h19
-rw-r--r--tmk_core/common/keyboard.c43
-rw-r--r--tmk_core/common/keyboard.h5
-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
27 files changed, 615 insertions, 616 deletions
diff --git a/tmk_core/common/action.c b/tmk_core/common/action.c
index aae5cbfa5..bd41d28b6 100644
--- a/tmk_core/common/action.c
+++ b/tmk_core/common/action.c
@@ -133,7 +133,8 @@ void process_hand_swap(keyevent_t *event) {
133 bool do_swap = event->pressed ? swap_hands : swap_state[pos.row] & (col_bit); 133 bool do_swap = event->pressed ? swap_hands : swap_state[pos.row] & (col_bit);
134 134
135 if (do_swap) { 135 if (do_swap) {
136 event->key = hand_swap_config[pos.row][pos.col]; 136 event->key.row = pgm_read_byte(&hand_swap_config[pos.row][pos.col].row);
137 event->key.col = pgm_read_byte(&hand_swap_config[pos.row][pos.col].col);
137 swap_state[pos.row] |= col_bit; 138 swap_state[pos.row] |= col_bit;
138 } else { 139 } else {
139 swap_state[pos.row] &= ~(col_bit); 140 swap_state[pos.row] &= ~(col_bit);
@@ -772,10 +773,9 @@ void register_code(uint8_t code) {
772 } 773 }
773#endif 774#endif
774 775
775 else if 776 else if IS_KEY (code) {
776 IS_KEY(code) { 777 // TODO: should push command_proc out of this block?
777 // TODO: should push command_proc out of this block? 778 if (command_proc(code)) return;
778 if (command_proc(code)) return;
779 779
780#ifndef NO_ACTION_ONESHOT 780#ifndef NO_ACTION_ONESHOT
781/* TODO: remove 781/* TODO: remove
@@ -792,35 +792,33 @@ void register_code(uint8_t code) {
792 } else 792 } else
793*/ 793*/
794#endif 794#endif
795 { 795 {
796 // Force a new key press if the key is already pressed 796 // Force a new key press if the key is already pressed
797 // without this, keys with the same keycode, but different 797 // without this, keys with the same keycode, but different
798 // modifiers will be reported incorrectly, see issue #1708 798 // modifiers will be reported incorrectly, see issue #1708
799 if (is_key_pressed(keyboard_report, code)) { 799 if (is_key_pressed(keyboard_report, code)) {
800 del_key(code); 800 del_key(code);
801 send_keyboard_report();
802 }
803 add_key(code);
804 send_keyboard_report(); 801 send_keyboard_report();
805 } 802 }
806 } 803 add_key(code);
807 else if
808 IS_MOD(code) {
809 add_mods(MOD_BIT(code));
810 send_keyboard_report(); 804 send_keyboard_report();
811 } 805 }
806 } else if IS_MOD (code) {
807 add_mods(MOD_BIT(code));
808 send_keyboard_report();
809 }
812#ifdef EXTRAKEY_ENABLE 810#ifdef EXTRAKEY_ENABLE
813 else if 811 else if IS_SYSTEM (code) {
814 IS_SYSTEM(code) { host_system_send(KEYCODE2SYSTEM(code)); } 812 host_system_send(KEYCODE2SYSTEM(code));
815 else if 813 } else if IS_CONSUMER (code) {
816 IS_CONSUMER(code) { host_consumer_send(KEYCODE2CONSUMER(code)); } 814 host_consumer_send(KEYCODE2CONSUMER(code));
815 }
817#endif 816#endif
818#ifdef MOUSEKEY_ENABLE 817#ifdef MOUSEKEY_ENABLE
819 else if 818 else if IS_MOUSEKEY (code) {
820 IS_MOUSEKEY(code) { 819 mousekey_on(code);
821 mousekey_on(code); 820 mousekey_send();
822 mousekey_send(); 821 }
823 }
824#endif 822#endif
825} 823}
826 824
@@ -865,26 +863,22 @@ void unregister_code(uint8_t code) {
865 } 863 }
866#endif 864#endif
867 865
868 else if 866 else if IS_KEY (code) {
869 IS_KEY(code) { 867 del_key(code);
870 del_key(code); 868 send_keyboard_report();
871 send_keyboard_report(); 869 } else if IS_MOD (code) {
872 } 870 del_mods(MOD_BIT(code));
873 else if 871 send_keyboard_report();
874 IS_MOD(code) { 872 } else if IS_SYSTEM (code) {
875 del_mods(MOD_BIT(code)); 873 host_system_send(0);
876 send_keyboard_report(); 874 } else if IS_CONSUMER (code) {
877 } 875 host_consumer_send(0);
878 else if 876 }
879 IS_SYSTEM(code) { host_system_send(0); }
880 else if
881 IS_CONSUMER(code) { host_consumer_send(0); }
882#ifdef MOUSEKEY_ENABLE 877#ifdef MOUSEKEY_ENABLE
883 else if 878 else if IS_MOUSEKEY (code) {
884 IS_MOUSEKEY(code) { 879 mousekey_off(code);
885 mousekey_off(code); 880 mousekey_send();
886 mousekey_send(); 881 }
887 }
888#endif 882#endif
889} 883}
890 884
diff --git a/tmk_core/common/action.h b/tmk_core/common/action.h
index 9a991de1c..8cb4722c6 100644
--- a/tmk_core/common/action.h
+++ b/tmk_core/common/action.h
@@ -77,8 +77,8 @@ extern bool disable_action_cache;
77 77
78/* Code for handling one-handed key modifiers. */ 78/* Code for handling one-handed key modifiers. */
79#ifdef SWAP_HANDS_ENABLE 79#ifdef SWAP_HANDS_ENABLE
80extern bool swap_hands; 80extern bool swap_hands;
81extern const keypos_t hand_swap_config[MATRIX_ROWS][MATRIX_COLS]; 81extern const keypos_t PROGMEM hand_swap_config[MATRIX_ROWS][MATRIX_COLS];
82# if (MATRIX_COLS <= 8) 82# if (MATRIX_COLS <= 8)
83typedef uint8_t swap_state_row_t; 83typedef uint8_t swap_state_row_t;
84# elif (MATRIX_COLS <= 16) 84# elif (MATRIX_COLS <= 16)
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..690d7f38c 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
@@ -156,6 +163,13 @@ void suspend_power_down(void) {
156 rgblight_suspend(); 163 rgblight_suspend();
157# endif 164# endif
158 165
166# if defined(LED_MATRIX_ENABLE)
167 led_matrix_set_suspend_state(true);
168# endif
169# if defined(RGB_MATRIX_ENABLE)
170 rgb_matrix_set_suspend_state(true);
171# endif
172
159 // Enter sleep state if possible (ie, the MCU has a watchdog timeout interrupt) 173 // Enter sleep state if possible (ie, the MCU has a watchdog timeout interrupt)
160# if defined(WDT_vect) 174# if defined(WDT_vect)
161 power_down(WDTO_15MS); 175 power_down(WDTO_15MS);
@@ -208,6 +222,13 @@ void suspend_wakeup_init(void) {
208 rgblight_wakeup(); 222 rgblight_wakeup();
209#endif 223#endif
210 224
225#if defined(LED_MATRIX_ENABLE)
226 led_matrix_set_suspend_state(false);
227#endif
228#if defined(RGB_MATRIX_ENABLE)
229 rgb_matrix_set_suspend_state(false);
230#endif
231
211 suspend_wakeup_init_kb(); 232 suspend_wakeup_init_kb();
212} 233}
213 234
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..11f7abf43 100644
--- a/tmk_core/common/chibios/bootloader.c
+++ b/tmk_core/common/chibios/bootloader.c
@@ -13,7 +13,23 @@
13# define STM32_BOOTLOADER_DUAL_BANK FALSE 13# define STM32_BOOTLOADER_DUAL_BANK FALSE
14#endif 14#endif
15 15
16#if STM32_BOOTLOADER_DUAL_BANK 16#ifdef BOOTLOADER_TINYUF2
17
18# define DBL_TAP_MAGIC 0xf01669ef // From tinyuf2's board_api.h
19
20// defined by linker script
21extern uint32_t _board_dfu_dbl_tap[];
22# define DBL_TAP_REG _board_dfu_dbl_tap[0]
23
24void bootloader_jump(void) {
25 DBL_TAP_REG = DBL_TAP_MAGIC;
26 NVIC_SystemReset();
27}
28
29void enter_bootloader_mode_if_requested(void) { /* not needed, no two-stage reset */
30}
31
32#elif STM32_BOOTLOADER_DUAL_BANK
17 33
18// Need pin definitions 34// Need pin definitions
19# include "config_common.h" 35# include "config_common.h"
@@ -79,7 +95,7 @@ void enter_bootloader_mode_if_requested(void) {
79 } 95 }
80} 96}
81 97
82#elif defined(KL2x) || defined(K20x) // STM32_BOOTLOADER_DUAL_BANK // STM32_BOOTLOADER_ADDRESS 98#elif defined(KL2x) || defined(K20x) || defined(MK66F18) // STM32_BOOTLOADER_DUAL_BANK // STM32_BOOTLOADER_ADDRESS
83/* Kinetis */ 99/* Kinetis */
84 100
85# if defined(BOOTLOADER_KIIBOHD) 101# 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..1c65016a4 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,20 +53,19 @@ 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 */
81# define LPTMR_CLOCK_ERCLK32K 2 /* external 32kHz crystal */ 69# define LPTMR_CLOCK_ERCLK32K 2 /* external 32kHz crystal */
82# define LPTMR_CLOCK_OSCERCLK 3 /* output from OSC */ 70# define LPTMR_CLOCK_OSCERCLK 3 /* output from OSC */
83 71
@@ -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 */
@@ -121,7 +121,7 @@ void sleep_led_init(void) {
121 MCG->C2 |= MCG_C2_IRCS; // fast (4MHz) internal ref clock 121 MCG->C2 |= MCG_C2_IRCS; // fast (4MHz) internal ref clock
122# if defined(KL27) // divide the 8MHz IRC by 2, to have the same MCGIRCLK speed as others 122# if defined(KL27) // divide the 8MHz IRC by 2, to have the same MCGIRCLK speed as others
123 MCG->MC |= MCG_MC_LIRC_DIV2_DIV2; 123 MCG->MC |= MCG_MC_LIRC_DIV2_DIV2;
124# endif /* KL27 */ 124# endif /* KL27 */
125 MCG->C1 |= MCG_C1_IRCLKEN; // enable internal ref clock 125 MCG->C1 |= MCG_C1_IRCLKEN; // enable internal ref clock
126 // to work in stop mode, also MCG_C1_IREFSTEN 126 // to work in stop mode, also MCG_C1_IREFSTEN
127 // Divide 4MHz by 2^N (N=6) => 62500 irqs/sec => 127 // Divide 4MHz by 2^N (N=6) => 62500 irqs/sec =>
@@ -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..38517e06f 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)
@@ -69,6 +83,13 @@ void suspend_power_down(void) {
69#if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE) 83#if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE)
70 rgblight_suspend(); 84 rgblight_suspend();
71#endif 85#endif
86
87#if defined(LED_MATRIX_ENABLE)
88 led_matrix_set_suspend_state(true);
89#endif
90#if defined(RGB_MATRIX_ENABLE)
91 rgb_matrix_set_suspend_state(true);
92#endif
72#ifdef AUDIO_ENABLE 93#ifdef AUDIO_ENABLE
73 stop_all_notes(); 94 stop_all_notes();
74#endif /* AUDIO_ENABLE */ 95#endif /* AUDIO_ENABLE */
@@ -137,5 +158,12 @@ void suspend_wakeup_init(void) {
137#if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE) 158#if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE)
138 rgblight_wakeup(); 159 rgblight_wakeup();
139#endif 160#endif
161
162#if defined(LED_MATRIX_ENABLE)
163 led_matrix_set_suspend_state(false);
164#endif
165#if defined(RGB_MATRIX_ENABLE)
166 rgb_matrix_set_suspend_state(false);
167#endif
140 suspend_wakeup_init_kb(); 168 suspend_wakeup_init_kb();
141} 169}
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..ffa56ab56 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
@@ -155,17 +155,6 @@ void eeconfig_update_keymap(uint16_t val) {
155 eeprom_update_byte(EECONFIG_KEYMAP_UPPER_BYTE, (val >> 8) & 0xFF); 155 eeprom_update_byte(EECONFIG_KEYMAP_UPPER_BYTE, (val >> 8) & 0xFF);
156} 156}
157 157
158/** \brief eeconfig read backlight
159 *
160 * FIXME: needs doc
161 */
162uint8_t eeconfig_read_backlight(void) { return eeprom_read_byte(EECONFIG_BACKLIGHT); }
163/** \brief eeconfig update backlight
164 *
165 * FIXME: needs doc
166 */
167void eeconfig_update_backlight(uint8_t val) { eeprom_update_byte(EECONFIG_BACKLIGHT, val); }
168
169/** \brief eeconfig read audio 158/** \brief eeconfig read audio
170 * 159 *
171 * FIXME: needs doc 160 * FIXME: needs doc
diff --git a/tmk_core/common/eeconfig.h b/tmk_core/common/eeconfig.h
index 86b9e6f99..a88071729 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)
@@ -88,11 +94,6 @@ void eeconfig_update_default_layer(uint8_t val);
88uint16_t eeconfig_read_keymap(void); 94uint16_t eeconfig_read_keymap(void);
89void eeconfig_update_keymap(uint16_t val); 95void eeconfig_update_keymap(uint16_t val);
90 96
91#ifdef BACKLIGHT_ENABLE
92uint8_t eeconfig_read_backlight(void);
93void eeconfig_update_backlight(uint8_t val);
94#endif
95
96#ifdef AUDIO_ENABLE 97#ifdef AUDIO_ENABLE
97uint8_t eeconfig_read_audio(void); 98uint8_t eeconfig_read_audio(void);
98void eeconfig_update_audio(uint8_t val); 99void eeconfig_update_audio(uint8_t val);
diff --git a/tmk_core/common/keyboard.c b/tmk_core/common/keyboard.c
index ce3255c06..3d6092e71 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
@@ -96,6 +94,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
96#ifdef DIP_SWITCH_ENABLE 94#ifdef DIP_SWITCH_ENABLE
97# include "dip_switch.h" 95# include "dip_switch.h"
98#endif 96#endif
97#ifdef STM32_EEPROM_ENABLE
98# include "eeprom_stm32.h"
99#endif
100#ifdef EEPROM_DRIVER
101# include "eeprom_driver.h"
102#endif
99 103
100static uint32_t last_input_modification_time = 0; 104static uint32_t last_input_modification_time = 0;
101uint32_t last_input_activity_time(void) { return last_input_modification_time; } 105uint32_t last_input_activity_time(void) { return last_input_modification_time; }
@@ -233,6 +237,12 @@ void keyboard_setup(void) {
233 disable_jtag(); 237 disable_jtag();
234#endif 238#endif
235 print_set_sendchar(sendchar); 239 print_set_sendchar(sendchar);
240#ifdef STM32_EEPROM_ENABLE
241 EEPROM_Init();
242#endif
243#ifdef EEPROM_DRIVER
244 eeprom_driver_init();
245#endif
236 matrix_setup(); 246 matrix_setup();
237 keyboard_pre_init_kb(); 247 keyboard_pre_init_kb();
238} 248}
@@ -270,6 +280,15 @@ __attribute__((weak)) void housekeeping_task_kb(void) {}
270 */ 280 */
271__attribute__((weak)) void housekeeping_task_user(void) {} 281__attribute__((weak)) void housekeeping_task_user(void) {}
272 282
283/** \brief housekeeping_task
284 *
285 * Invokes hooks for executing code after QMK is done after each loop iteration.
286 */
287void housekeeping_task(void) {
288 housekeeping_task_kb();
289 housekeeping_task_user();
290}
291
273/** \brief keyboard_init 292/** \brief keyboard_init
274 * 293 *
275 * FIXME: needs doc 294 * FIXME: needs doc
@@ -296,11 +315,6 @@ void keyboard_init(void) {
296#ifdef ADB_MOUSE_ENABLE 315#ifdef ADB_MOUSE_ENABLE
297 adb_mouse_init(); 316 adb_mouse_init();
298#endif 317#endif
299#ifdef BOOTMAGIC_ENABLE
300 bootmagic();
301#else
302 magic();
303#endif
304#ifdef BACKLIGHT_ENABLE 318#ifdef BACKLIGHT_ENABLE
305 backlight_init(); 319 backlight_init();
306#endif 320#endif
@@ -337,6 +351,9 @@ void keyboard_init(void) {
337 * This is differnet than keycode events as no layer processing, or filtering occurs. 351 * This is differnet than keycode events as no layer processing, or filtering occurs.
338 */ 352 */
339void switch_events(uint8_t row, uint8_t col, bool pressed) { 353void switch_events(uint8_t row, uint8_t col, bool pressed) {
354#if defined(LED_MATRIX_ENABLE)
355 process_led_matrix(row, col, pressed);
356#endif
340#if defined(RGB_MATRIX_ENABLE) 357#if defined(RGB_MATRIX_ENABLE)
341 process_rgb_matrix(row, col, pressed); 358 process_rgb_matrix(row, col, pressed);
342#endif 359#endif
@@ -366,9 +383,6 @@ void keyboard_task(void) {
366 bool encoders_changed = false; 383 bool encoders_changed = false;
367#endif 384#endif
368 385
369 housekeeping_task_kb();
370 housekeeping_task_user();
371
372 uint8_t matrix_changed = matrix_scan(); 386 uint8_t matrix_changed = matrix_scan();
373 if (matrix_changed) last_matrix_activity_trigger(); 387 if (matrix_changed) last_matrix_activity_trigger();
374 388
@@ -422,6 +436,9 @@ MATRIX_LOOP_END:
422 rgblight_task(); 436 rgblight_task();
423#endif 437#endif
424 438
439#ifdef LED_MATRIX_ENABLE
440 led_matrix_task();
441#endif
425#ifdef RGB_MATRIX_ENABLE 442#ifdef RGB_MATRIX_ENABLE
426 rgb_matrix_task(); 443 rgb_matrix_task();
427#endif 444#endif
diff --git a/tmk_core/common/keyboard.h b/tmk_core/common/keyboard.h
index eaf74bac5..08f4e84f9 100644
--- a/tmk_core/common/keyboard.h
+++ b/tmk_core/common/keyboard.h
@@ -70,8 +70,9 @@ void keyboard_pre_init_user(void);
70void keyboard_post_init_kb(void); 70void keyboard_post_init_kb(void);
71void keyboard_post_init_user(void); 71void keyboard_post_init_user(void);
72 72
73void housekeeping_task_kb(void); 73void housekeeping_task(void); // To be executed by the main loop in each backend TMK protocol
74void housekeeping_task_user(void); 74void housekeeping_task_kb(void); // To be overridden by keyboard-level code
75void housekeeping_task_user(void); // To be overridden by user/keymap-level code
75 76
76uint32_t last_input_activity_time(void); // Timestamp of the last matrix or encoder activity 77uint32_t last_input_activity_time(void); // Timestamp of the last matrix or encoder activity
77uint32_t last_input_activity_elapsed(void); // Number of milliseconds since the last matrix or encoder activity 78uint32_t last_input_activity_elapsed(void); // Number of milliseconds since the last matrix or encoder activity
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