diff options
Diffstat (limited to 'tmk_core/common/chibios')
| -rw-r--r-- | tmk_core/common/chibios/bootloader.c | 74 | ||||
| -rw-r--r-- | tmk_core/common/chibios/suspend.c | 16 |
2 files changed, 72 insertions, 18 deletions
diff --git a/tmk_core/common/chibios/bootloader.c b/tmk_core/common/chibios/bootloader.c index 4cf5dae7e..7b2cf5c43 100644 --- a/tmk_core/common/chibios/bootloader.c +++ b/tmk_core/common/chibios/bootloader.c | |||
| @@ -2,29 +2,67 @@ | |||
| 2 | 2 | ||
| 3 | #include "ch.h" | 3 | #include "ch.h" |
| 4 | #include "hal.h" | 4 | #include "hal.h" |
| 5 | 5 | #include "wait.h" | |
| 6 | #ifdef STM32_BOOTLOADER_ADDRESS | ||
| 7 | /* STM32 */ | ||
| 8 | 6 | ||
| 9 | /* This code should be checked whether it runs correctly on platforms */ | 7 | /* This code should be checked whether it runs correctly on platforms */ |
| 10 | # define SYMVAL(sym) (uint32_t)(((uint8_t *)&(sym)) - ((uint8_t *)0)) | 8 | #define SYMVAL(sym) (uint32_t)(((uint8_t *)&(sym)) - ((uint8_t *)0)) |
| 9 | #define BOOTLOADER_MAGIC 0xDEADBEEF | ||
| 10 | #define MAGIC_ADDR (unsigned long *)(SYMVAL(__ram0_end__) - 4) | ||
| 11 | |||
| 12 | #ifndef STM32_BOOTLOADER_DUAL_BANK | ||
| 13 | # define STM32_BOOTLOADER_DUAL_BANK FALSE | ||
| 14 | #endif | ||
| 15 | |||
| 16 | #if STM32_BOOTLOADER_DUAL_BANK | ||
| 17 | |||
| 18 | // Need pin definitions | ||
| 19 | # include "config_common.h" | ||
| 20 | |||
| 21 | # ifndef STM32_BOOTLOADER_DUAL_BANK_GPIO | ||
| 22 | # error "No STM32_BOOTLOADER_DUAL_BANK_GPIO defined, don't know which pin to toggle" | ||
| 23 | # endif | ||
| 24 | |||
| 25 | # ifndef STM32_BOOTLOADER_DUAL_BANK_POLARITY | ||
| 26 | # define STM32_BOOTLOADER_DUAL_BANK_POLARITY 0 | ||
| 27 | # endif | ||
| 28 | |||
| 29 | # ifndef STM32_BOOTLOADER_DUAL_BANK_DELAY | ||
| 30 | # define STM32_BOOTLOADER_DUAL_BANK_DELAY 100000 | ||
| 31 | # endif | ||
| 32 | |||
| 33 | extern uint32_t __ram0_end__; | ||
| 34 | |||
| 35 | void bootloader_jump(void) { | ||
| 36 | // For STM32 MCUs with dual-bank flash, and we're incapable of jumping to the bootloader. The first valid flash | ||
| 37 | // bank is executed unconditionally after a reset, so it doesn't enter DFU unless BOOT0 is high. Instead, we do | ||
| 38 | // it with hardware...in this case, we pull a GPIO high/low depending on the configuration, connects 3.3V to | ||
| 39 | // BOOT0's RC charging circuit, lets it charge the capacitor, and issue a system reset. See the QMK discord | ||
| 40 | // #hardware channel pins for an example circuit. | ||
| 41 | palSetPadMode(PAL_PORT(STM32_BOOTLOADER_DUAL_BANK_GPIO), PAL_PAD(STM32_BOOTLOADER_DUAL_BANK_GPIO), PAL_MODE_OUTPUT_PUSHPULL); | ||
| 42 | # if STM32_BOOTLOADER_DUAL_BANK_POLARITY | ||
| 43 | palSetPad(PAL_PORT(STM32_BOOTLOADER_DUAL_BANK_GPIO), PAL_PAD(STM32_BOOTLOADER_DUAL_BANK_GPIO)); | ||
| 44 | # else | ||
| 45 | palClearPad(PAL_PORT(STM32_BOOTLOADER_DUAL_BANK_GPIO), PAL_PAD(STM32_BOOTLOADER_DUAL_BANK_GPIO)); | ||
| 46 | # endif | ||
| 47 | |||
| 48 | // Wait for a while for the capacitor to charge | ||
| 49 | wait_ms(100); | ||
| 50 | |||
| 51 | // Issue a system reset to get the ROM bootloader to execute, with BOOT0 high | ||
| 52 | NVIC_SystemReset(); | ||
| 53 | } | ||
| 54 | |||
| 55 | void enter_bootloader_mode_if_requested(void) {} // not needed at all, but if anybody attempts to invoke it.... | ||
| 56 | |||
| 57 | #elif defined(STM32_BOOTLOADER_ADDRESS) // STM32_BOOTLOADER_DUAL_BANK | ||
| 58 | |||
| 11 | extern uint32_t __ram0_end__; | 59 | extern uint32_t __ram0_end__; |
| 12 | # define BOOTLOADER_MAGIC 0xDEADBEEF | ||
| 13 | # define MAGIC_ADDR (unsigned long *)(SYMVAL(__ram0_end__) - 4) | ||
| 14 | 60 | ||
| 15 | /** \brief Jump to the bootloader | ||
| 16 | * | ||
| 17 | * FIXME: needs doc | ||
| 18 | */ | ||
| 19 | void bootloader_jump(void) { | 61 | void bootloader_jump(void) { |
| 20 | *MAGIC_ADDR = BOOTLOADER_MAGIC; // set magic flag => reset handler will jump into boot loader | 62 | *MAGIC_ADDR = BOOTLOADER_MAGIC; // set magic flag => reset handler will jump into boot loader |
| 21 | NVIC_SystemReset(); | 63 | NVIC_SystemReset(); |
| 22 | } | 64 | } |
| 23 | 65 | ||
| 24 | /** \brief Enter bootloader mode if requested | ||
| 25 | * | ||
| 26 | * FIXME: needs doc | ||
| 27 | */ | ||
| 28 | void enter_bootloader_mode_if_requested(void) { | 66 | void enter_bootloader_mode_if_requested(void) { |
| 29 | unsigned long *check = MAGIC_ADDR; | 67 | unsigned long *check = MAGIC_ADDR; |
| 30 | if (*check == BOOTLOADER_MAGIC) { | 68 | if (*check == BOOTLOADER_MAGIC) { |
| @@ -41,10 +79,10 @@ void enter_bootloader_mode_if_requested(void) { | |||
| 41 | } | 79 | } |
| 42 | } | 80 | } |
| 43 | 81 | ||
| 44 | #elif defined(KL2x) || defined(K20x) /* STM32_BOOTLOADER_ADDRESS */ | 82 | #elif defined(KL2x) || defined(K20x) // STM32_BOOTLOADER_DUAL_BANK // STM32_BOOTLOADER_ADDRESS |
| 45 | /* Kinetis */ | 83 | /* Kinetis */ |
| 46 | 84 | ||
| 47 | # if defined(KIIBOHD_BOOTLOADER) | 85 | # if defined(BOOTLOADER_KIIBOHD) |
| 48 | /* Kiibohd Bootloader (MCHCK and Infinity KB) */ | 86 | /* Kiibohd Bootloader (MCHCK and Infinity KB) */ |
| 49 | # define SCB_AIRCR_VECTKEY_WRITEMAGIC 0x05FA0000 | 87 | # define SCB_AIRCR_VECTKEY_WRITEMAGIC 0x05FA0000 |
| 50 | const uint8_t sys_reset_to_loader_magic[] = "\xff\x00\x7fRESET TO LOADER\x7f\x00\xff"; | 88 | const uint8_t sys_reset_to_loader_magic[] = "\xff\x00\x7fRESET TO LOADER\x7f\x00\xff"; |
| @@ -54,14 +92,14 @@ void bootloader_jump(void) { | |||
| 54 | SCB->AIRCR = SCB_AIRCR_VECTKEY_WRITEMAGIC | SCB_AIRCR_SYSRESETREQ_Msk; | 92 | SCB->AIRCR = SCB_AIRCR_VECTKEY_WRITEMAGIC | SCB_AIRCR_SYSRESETREQ_Msk; |
| 55 | } | 93 | } |
| 56 | 94 | ||
| 57 | # else /* defined(KIIBOHD_BOOTLOADER) */ | 95 | # else /* defined(BOOTLOADER_KIIBOHD) */ |
| 58 | /* Default for Kinetis - expecting an ARM Teensy */ | 96 | /* Default for Kinetis - expecting an ARM Teensy */ |
| 59 | # include "wait.h" | 97 | # include "wait.h" |
| 60 | void bootloader_jump(void) { | 98 | void bootloader_jump(void) { |
| 61 | wait_ms(100); | 99 | wait_ms(100); |
| 62 | __BKPT(0); | 100 | __BKPT(0); |
| 63 | } | 101 | } |
| 64 | # endif /* defined(KIIBOHD_BOOTLOADER) */ | 102 | # endif /* defined(BOOTLOADER_KIIBOHD) */ |
| 65 | 103 | ||
| 66 | #else /* neither STM32 nor KINETIS */ | 104 | #else /* neither STM32 nor KINETIS */ |
| 67 | __attribute__((weak)) void bootloader_jump(void) {} | 105 | __attribute__((weak)) void bootloader_jump(void) {} |
diff --git a/tmk_core/common/chibios/suspend.c b/tmk_core/common/chibios/suspend.c index 8c071e7a0..64dfc05ab 100644 --- a/tmk_core/common/chibios/suspend.c +++ b/tmk_core/common/chibios/suspend.c | |||
| @@ -9,6 +9,7 @@ | |||
| 9 | #include "mousekey.h" | 9 | #include "mousekey.h" |
| 10 | #include "host.h" | 10 | #include "host.h" |
| 11 | #include "suspend.h" | 11 | #include "suspend.h" |
| 12 | #include "led.h" | ||
| 12 | #include "wait.h" | 13 | #include "wait.h" |
| 13 | 14 | ||
| 14 | #ifdef BACKLIGHT_ENABLE | 15 | #ifdef BACKLIGHT_ENABLE |
| @@ -47,6 +48,20 @@ __attribute__((weak)) void suspend_power_down_kb(void) { suspend_power_down_user | |||
| 47 | * FIXME: needs doc | 48 | * FIXME: needs doc |
| 48 | */ | 49 | */ |
| 49 | void suspend_power_down(void) { | 50 | void suspend_power_down(void) { |
| 51 | #ifdef BACKLIGHT_ENABLE | ||
| 52 | backlight_set(0); | ||
| 53 | #endif | ||
| 54 | |||
| 55 | // Turn off LED indicators | ||
| 56 | uint8_t leds_off = 0; | ||
| 57 | #if defined(BACKLIGHT_CAPS_LOCK) && defined(BACKLIGHT_ENABLE) | ||
| 58 | if (is_backlight_enabled()) { | ||
| 59 | // Don't try to turn off Caps Lock indicator as it is backlight and backlight is already off | ||
| 60 | leds_off |= (1 << USB_LED_CAPS_LOCK); | ||
| 61 | } | ||
| 62 | #endif | ||
| 63 | led_set(leds_off); | ||
| 64 | |||
| 50 | // TODO: figure out what to power down and how | 65 | // TODO: figure out what to power down and how |
| 51 | // shouldn't power down TPM/FTM if we want a breathing LED | 66 | // shouldn't power down TPM/FTM if we want a breathing LED |
| 52 | // also shouldn't power down USB | 67 | // also shouldn't power down USB |
| @@ -119,6 +134,7 @@ void suspend_wakeup_init(void) { | |||
| 119 | #ifdef BACKLIGHT_ENABLE | 134 | #ifdef BACKLIGHT_ENABLE |
| 120 | backlight_init(); | 135 | backlight_init(); |
| 121 | #endif /* BACKLIGHT_ENABLE */ | 136 | #endif /* BACKLIGHT_ENABLE */ |
| 137 | led_set(host_keyboard_leds()); | ||
| 122 | #if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE) | 138 | #if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE) |
| 123 | is_suspended = false; | 139 | is_suspended = false; |
| 124 | if (rgblight_enabled) { | 140 | if (rgblight_enabled) { |
