bootloader.c 4.2 KB

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