bootloader.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include "bootloader.h"
  2. #include <ch.h>
  3. #include <hal.h>
  4. #include "wait.h"
  5. /* This code should be checked whether it runs correctly on platforms */
  6. #define SYMVAL(sym) (uint32_t)(((uint8_t *)&(sym)) - ((uint8_t *)0))
  7. #define BOOTLOADER_MAGIC 0xDEADBEEF
  8. #define MAGIC_ADDR (unsigned long *)(SYMVAL(__ram0_end__) - 4)
  9. #ifndef STM32_BOOTLOADER_DUAL_BANK
  10. # define STM32_BOOTLOADER_DUAL_BANK FALSE
  11. #endif
  12. #if STM32_BOOTLOADER_DUAL_BANK
  13. // Need pin definitions
  14. # include "config_common.h"
  15. # ifndef STM32_BOOTLOADER_DUAL_BANK_GPIO
  16. # error "No STM32_BOOTLOADER_DUAL_BANK_GPIO defined, don't know which pin to toggle"
  17. # endif
  18. # ifndef STM32_BOOTLOADER_DUAL_BANK_POLARITY
  19. # define STM32_BOOTLOADER_DUAL_BANK_POLARITY 0
  20. # endif
  21. # ifndef STM32_BOOTLOADER_DUAL_BANK_DELAY
  22. # define STM32_BOOTLOADER_DUAL_BANK_DELAY 100000
  23. # endif
  24. extern uint32_t __ram0_end__;
  25. __attribute__((weak)) void bootloader_jump(void) {
  26. // For STM32 MCUs with dual-bank flash, and we're incapable of jumping to the bootloader. The first valid flash
  27. // bank is executed unconditionally after a reset, so it doesn't enter DFU unless BOOT0 is high. Instead, we do
  28. // it with hardware...in this case, we pull a GPIO high/low depending on the configuration, connects 3.3V to
  29. // BOOT0's RC charging circuit, lets it charge the capacitor, and issue a system reset. See the QMK discord
  30. // #hardware channel pins for an example circuit.
  31. palSetPadMode(PAL_PORT(STM32_BOOTLOADER_DUAL_BANK_GPIO), PAL_PAD(STM32_BOOTLOADER_DUAL_BANK_GPIO), PAL_MODE_OUTPUT_PUSHPULL);
  32. # if STM32_BOOTLOADER_DUAL_BANK_POLARITY
  33. palSetPad(PAL_PORT(STM32_BOOTLOADER_DUAL_BANK_GPIO), PAL_PAD(STM32_BOOTLOADER_DUAL_BANK_GPIO));
  34. # else
  35. palClearPad(PAL_PORT(STM32_BOOTLOADER_DUAL_BANK_GPIO), PAL_PAD(STM32_BOOTLOADER_DUAL_BANK_GPIO));
  36. # endif
  37. // Wait for a while for the capacitor to charge
  38. wait_ms(100);
  39. // Issue a system reset to get the ROM bootloader to execute, with BOOT0 high
  40. NVIC_SystemReset();
  41. }
  42. void enter_bootloader_mode_if_requested(void) {} // not needed at all, but if anybody attempts to invoke it....
  43. #elif defined(STM32_BOOTLOADER_ADDRESS) // STM32_BOOTLOADER_DUAL_BANK
  44. extern uint32_t __ram0_end__;
  45. __attribute__((weak)) void bootloader_jump(void) {
  46. *MAGIC_ADDR = BOOTLOADER_MAGIC; // set magic flag => reset handler will jump into boot loader
  47. NVIC_SystemReset();
  48. }
  49. void enter_bootloader_mode_if_requested(void) {
  50. unsigned long *check = MAGIC_ADDR;
  51. if (*check == BOOTLOADER_MAGIC) {
  52. *check = 0;
  53. __set_CONTROL(0);
  54. __set_MSP(*(__IO uint32_t *)STM32_BOOTLOADER_ADDRESS);
  55. __enable_irq();
  56. typedef void (*BootJump_t)(void);
  57. BootJump_t boot_jump = *(BootJump_t *)(STM32_BOOTLOADER_ADDRESS + 4);
  58. boot_jump();
  59. while (1)
  60. ;
  61. }
  62. }
  63. #elif defined(KL2x) || defined(K20x) // STM32_BOOTLOADER_DUAL_BANK // STM32_BOOTLOADER_ADDRESS
  64. /* Kinetis */
  65. # if defined(BOOTLOADER_KIIBOHD)
  66. /* Kiibohd Bootloader (MCHCK and Infinity KB) */
  67. # define SCB_AIRCR_VECTKEY_WRITEMAGIC 0x05FA0000
  68. const uint8_t sys_reset_to_loader_magic[] = "\xff\x00\x7fRESET TO LOADER\x7f\x00\xff";
  69. __attribute__((weak)) void bootloader_jump(void) {
  70. __builtin_memcpy((void *)VBAT, (const void *)sys_reset_to_loader_magic, sizeof(sys_reset_to_loader_magic));
  71. // request reset
  72. SCB->AIRCR = SCB_AIRCR_VECTKEY_WRITEMAGIC | SCB_AIRCR_SYSRESETREQ_Msk;
  73. }
  74. # else /* defined(BOOTLOADER_KIIBOHD) */
  75. /* Default for Kinetis - expecting an ARM Teensy */
  76. # include "wait.h"
  77. __attribute__((weak)) void bootloader_jump(void) {
  78. wait_ms(100);
  79. __BKPT(0);
  80. }
  81. # endif /* defined(BOOTLOADER_KIIBOHD) */
  82. #else /* neither STM32 nor KINETIS */
  83. __attribute__((weak)) void bootloader_jump(void) {}
  84. #endif