stm32_dfu.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 "bootloader.h"
  17. #include <ch.h>
  18. #include <hal.h>
  19. #include "wait.h"
  20. extern uint32_t __ram0_end__;
  21. #ifndef STM32_BOOTLOADER_DUAL_BANK
  22. # define STM32_BOOTLOADER_DUAL_BANK FALSE
  23. #endif
  24. #if STM32_BOOTLOADER_DUAL_BANK
  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 100
  34. # endif
  35. __attribute__((weak)) 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. // Wait for a while for the capacitor to charge
  48. wait_ms(STM32_BOOTLOADER_DUAL_BANK_DELAY);
  49. // Issue a system reset to get the ROM bootloader to execute, with BOOT0 high
  50. NVIC_SystemReset();
  51. }
  52. __attribute__((weak)) void mcu_reset(void) {
  53. NVIC_SystemReset();
  54. }
  55. // not needed at all, but if anybody attempts to invoke it....
  56. void enter_bootloader_mode_if_requested(void) {}
  57. #else
  58. /* This code should be checked whether it runs correctly on platforms */
  59. # define SYMVAL(sym) (uint32_t)(((uint8_t *)&(sym)) - ((uint8_t *)0))
  60. # define BOOTLOADER_MAGIC 0xDEADBEEF
  61. # define MAGIC_ADDR (unsigned long *)(SYMVAL(__ram0_end__) - 4)
  62. __attribute__((weak)) void bootloader_jump(void) {
  63. *MAGIC_ADDR = BOOTLOADER_MAGIC; // set magic flag => reset handler will jump into boot loader
  64. NVIC_SystemReset();
  65. }
  66. __attribute__((weak)) void mcu_reset(void) {
  67. NVIC_SystemReset();
  68. }
  69. void enter_bootloader_mode_if_requested(void) {
  70. unsigned long *check = MAGIC_ADDR;
  71. if (*check == BOOTLOADER_MAGIC) {
  72. *check = 0;
  73. __set_CONTROL(0);
  74. __set_MSP(*(__IO uint32_t *)STM32_BOOTLOADER_ADDRESS);
  75. __enable_irq();
  76. typedef void (*BootJump_t)(void);
  77. BootJump_t boot_jump = *(BootJump_t *)(STM32_BOOTLOADER_ADDRESS + 4);
  78. boot_jump();
  79. while (1)
  80. ;
  81. }
  82. }
  83. #endif