logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

bootmagic.c (2407B)


  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 "bootmagic.h"
  17. #include "matrix.h"
  18. #include "keyboard.h"
  19. #include "wait.h"
  20. #include "eeconfig.h"
  21. #include "bootloader.h"
  22. #ifndef BOOTMAGIC_DEBOUNCE
  23. # if defined(DEBOUNCE) && DEBOUNCE > 0
  24. # define BOOTMAGIC_DEBOUNCE (DEBOUNCE * 2)
  25. # else
  26. # define BOOTMAGIC_DEBOUNCE 30
  27. # endif
  28. #endif
  29. /** \brief Reset eeprom
  30. *
  31. * ...just incase someone wants to only change the eeprom behaviour
  32. */
  33. __attribute__((weak)) void bootmagic_reset_eeprom(void) {
  34. eeconfig_disable();
  35. }
  36. /** \brief Decide reboot based on current matrix state
  37. */
  38. __attribute__((weak)) bool bootmagic_should_reset(void) {
  39. // If the configured key (commonly Esc) is held down on power up,
  40. // reset the EEPROM valid state and jump to bootloader.
  41. // This isn't very generalized, but we need something that doesn't
  42. // rely on user's keymaps in firmware or EEPROM.
  43. uint8_t row = BOOTMAGIC_ROW;
  44. uint8_t col = BOOTMAGIC_COLUMN;
  45. #if defined(SPLIT_KEYBOARD) && defined(BOOTMAGIC_ROW_RIGHT) && defined(BOOTMAGIC_COLUMN_RIGHT)
  46. if (!is_keyboard_left()) {
  47. row = BOOTMAGIC_ROW_RIGHT;
  48. col = BOOTMAGIC_COLUMN_RIGHT;
  49. }
  50. #endif
  51. return matrix_get_row(row) & (1 << col);
  52. }
  53. /** \brief The abridged version of TMK's bootmagic based on Wilba.
  54. *
  55. * 100% less potential for accidentally making the keyboard do stupid things.
  56. */
  57. __attribute__((weak)) void bootmagic_scan(void) {
  58. // We need multiple scans because debouncing can't be turned off.
  59. matrix_scan();
  60. wait_ms(BOOTMAGIC_DEBOUNCE);
  61. matrix_scan();
  62. if (bootmagic_should_reset()) {
  63. bootmagic_reset_eeprom();
  64. // Jump to bootloader.
  65. bootloader_jump();
  66. }
  67. }
  68. void bootmagic(void) {
  69. bootmagic_scan();
  70. }