stm32_dfu.c 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 100000
  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(100);
  49. // Issue a system reset to get the ROM bootloader to execute, with BOOT0 high
  50. NVIC_SystemReset();
  51. }
  52. // not needed at all, but if anybody attempts to invoke it....
  53. void enter_bootloader_mode_if_requested(void) {}
  54. #else
  55. /* This code should be checked whether it runs correctly on platforms */
  56. # define SYMVAL(sym) (uint32_t)(((uint8_t *)&(sym)) - ((uint8_t *)0))
  57. # define BOOTLOADER_MAGIC 0xDEADBEEF
  58. # define MAGIC_ADDR (unsigned long *)(SYMVAL(__ram0_end__) - 4)
  59. __attribute__((weak)) void bootloader_jump(void) {
  60. *MAGIC_ADDR = BOOTLOADER_MAGIC; // set magic flag => reset handler will jump into boot loader
  61. NVIC_SystemReset();
  62. }
  63. void enter_bootloader_mode_if_requested(void) {
  64. unsigned long *check = MAGIC_ADDR;
  65. if (*check == BOOTLOADER_MAGIC) {
  66. *check = 0;
  67. __set_CONTROL(0);
  68. __set_MSP(*(__IO uint32_t *)STM32_BOOTLOADER_ADDRESS);
  69. __enable_irq();
  70. typedef void (*BootJump_t)(void);
  71. BootJump_t boot_jump = *(BootJump_t *)(STM32_BOOTLOADER_ADDRESS + 4);
  72. boot_jump();
  73. while (1)
  74. ;
  75. }
  76. }
  77. #endif