logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

stm32_dfu.c (4878B)


  1. /* Copyright 2021-2023 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 "util.h"
  18. #include <ch.h>
  19. #include <hal.h>
  20. #include "wait.h"
  21. #ifndef STM32_BOOTLOADER_RAM_SYMBOL
  22. # define STM32_BOOTLOADER_RAM_SYMBOL __ram0_end__
  23. #endif
  24. extern uint32_t STM32_BOOTLOADER_RAM_SYMBOL;
  25. #ifndef STM32_BOOTLOADER_DUAL_BANK
  26. # define STM32_BOOTLOADER_DUAL_BANK FALSE
  27. #endif
  28. #if STM32_BOOTLOADER_DUAL_BANK
  29. # include "gpio.h"
  30. # ifndef STM32_BOOTLOADER_DUAL_BANK_GPIO
  31. # error "No STM32_BOOTLOADER_DUAL_BANK_GPIO defined, don't know which pin to toggle"
  32. # endif
  33. # ifndef STM32_BOOTLOADER_DUAL_BANK_POLARITY
  34. # define STM32_BOOTLOADER_DUAL_BANK_POLARITY 0
  35. # endif
  36. # ifndef STM32_BOOTLOADER_DUAL_BANK_DELAY
  37. # define STM32_BOOTLOADER_DUAL_BANK_DELAY 100
  38. # endif
  39. __attribute__((weak)) void bootloader_jump(void) {
  40. // For STM32 MCUs with dual-bank flash, and we're incapable of jumping to the bootloader. The first valid flash
  41. // bank is executed unconditionally after a reset, so it doesn't enter DFU unless BOOT0 is high. Instead, we do
  42. // it with hardware...in this case, we pull a GPIO high/low depending on the configuration, connects 3.3V to
  43. // BOOT0's RC charging circuit, lets it charge the capacitor, and issue a system reset. See the QMK discord
  44. // #hardware channel pins for an example circuit.
  45. palSetPadMode(PAL_PORT(STM32_BOOTLOADER_DUAL_BANK_GPIO), PAL_PAD(STM32_BOOTLOADER_DUAL_BANK_GPIO), PAL_MODE_OUTPUT_PUSHPULL);
  46. # if STM32_BOOTLOADER_DUAL_BANK_POLARITY
  47. palSetPad(PAL_PORT(STM32_BOOTLOADER_DUAL_BANK_GPIO), PAL_PAD(STM32_BOOTLOADER_DUAL_BANK_GPIO));
  48. # else
  49. palClearPad(PAL_PORT(STM32_BOOTLOADER_DUAL_BANK_GPIO), PAL_PAD(STM32_BOOTLOADER_DUAL_BANK_GPIO));
  50. # endif
  51. // Wait for a while for the capacitor to charge
  52. wait_ms(STM32_BOOTLOADER_DUAL_BANK_DELAY);
  53. // Issue a system reset to get the ROM bootloader to execute, with BOOT0 high
  54. NVIC_SystemReset();
  55. }
  56. __attribute__((weak)) void mcu_reset(void) {
  57. NVIC_SystemReset();
  58. }
  59. // not needed at all, but if anybody attempts to invoke it....
  60. void enter_bootloader_mode_if_requested(void) {}
  61. #else
  62. /* This code should be checked whether it runs correctly on platforms */
  63. # define SYMVAL(sym) (uint32_t)(((uint8_t *)&(sym)) - ((uint8_t *)0))
  64. # define BOOTLOADER_MAGIC 0xDEADBEEF
  65. # define MAGIC_ADDR (unsigned long *)(SYMVAL(STM32_BOOTLOADER_RAM_SYMBOL) - 4)
  66. __attribute__((weak)) void bootloader_marker_enable(void) {
  67. uint32_t *marker = (uint32_t *)MAGIC_ADDR;
  68. *marker = BOOTLOADER_MAGIC; // set magic flag => reset handler will jump into boot loader
  69. }
  70. __attribute__((weak)) bool bootloader_marker_active(void) {
  71. const uint32_t *marker = (const uint32_t *)MAGIC_ADDR;
  72. return (*marker == BOOTLOADER_MAGIC) ? true : false;
  73. }
  74. __attribute__((weak)) void bootloader_marker_disable(void) {
  75. uint32_t *marker = (uint32_t *)MAGIC_ADDR;
  76. *marker = 0;
  77. }
  78. __attribute__((weak)) void bootloader_jump(void) {
  79. bootloader_marker_enable();
  80. NVIC_SystemReset();
  81. }
  82. __attribute__((weak)) void mcu_reset(void) {
  83. NVIC_SystemReset();
  84. }
  85. void enter_bootloader_mode_if_requested(void) {
  86. if (bootloader_marker_active()) {
  87. bootloader_marker_disable();
  88. struct system_memory_vector_t {
  89. uint32_t stack_top;
  90. void (*entrypoint)(void);
  91. };
  92. const struct system_memory_vector_t *bootloader = (const struct system_memory_vector_t *)(STM32_BOOTLOADER_ADDRESS);
  93. __disable_irq();
  94. # if defined(QMK_MCU_ARCH_CORTEX_M7)
  95. SCB_DisableDCache();
  96. SCB_DisableICache();
  97. # endif
  98. # if defined(__MPU_PRESENT) && (__MPU_PRESENT == 1U)
  99. ARM_MPU_Disable();
  100. # endif
  101. SysTick->CTRL = 0;
  102. SysTick->VAL = 0;
  103. SysTick->LOAD = 0;
  104. // Clear interrupt enable and interrupt pending registers
  105. for (int i = 0; i < ARRAY_SIZE(NVIC->ICER); i++) {
  106. NVIC->ICER[i] = 0xFFFFFFFF;
  107. NVIC->ICPR[i] = 0xFFFFFFFF;
  108. }
  109. __set_CONTROL(0);
  110. __set_MSP(bootloader->stack_top);
  111. __enable_irq();
  112. // Jump to bootloader
  113. bootloader->entrypoint();
  114. while (true) {
  115. }
  116. }
  117. }
  118. #endif