diff options
| author | stein3 <stein3@gmail.com> | 2020-10-06 07:15:41 -0700 |
|---|---|---|
| committer | stein3 <stein3@gmail.com> | 2020-10-06 07:15:41 -0700 |
| commit | 2e402741a89c5eec8cf30c966ce6f36d6ec9249b (patch) | |
| tree | 3592e8c5e6bd19943ae55db7fc02a5f755afbb51 /tmk_core/common/chibios/bootloader.c | |
| parent | 3e5e4f74272c610bb9fa737f674f8e65ed6100ca (diff) | |
| parent | 2013f6313430b977e557e482d30daa279a46e75d (diff) | |
| download | qmk_firmware-2e402741a89c5eec8cf30c966ce6f36d6ec9249b.tar.gz qmk_firmware-2e402741a89c5eec8cf30c966ce6f36d6ec9249b.zip | |
Merge branch 'master' into meteor
Diffstat (limited to 'tmk_core/common/chibios/bootloader.c')
| -rw-r--r-- | tmk_core/common/chibios/bootloader.c | 74 |
1 files changed, 56 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) {} |
