|
| 1 | +#include <stdbool.h> |
| 2 | + |
1 | 3 | #include "bootloader.h"
|
2 | 4 |
|
3 | 5 | #include "stm32_def.h"
|
|
11 | 13 | /* Private definitions to manage system memory address */
|
12 | 14 | #define SYSMEM_ADDR_COMMON 0xFFF
|
13 | 15 |
|
| 16 | +static bool BootIntoBootloaderAfterReset __attribute__((__section__(".noinit"))); |
| 17 | + |
14 | 18 | typedef struct {
|
15 | 19 | uint32_t devID;
|
16 | 20 | uint32_t sysMemAddr;
|
@@ -79,18 +83,25 @@ uint32_t getSysMemAddr(void)
|
79 | 83 | /* Request to jump to system memory boot */
|
80 | 84 | WEAK void jumpToBootloaderRequested(void)
|
81 | 85 | {
|
82 |
| - enableBackupDomain(); |
83 |
| - setBackupRegister(SYSBL_MAGIC_NUMBER_BKP_INDEX, SYSBL_MAGIC_NUMBER_BKP_VALUE); |
| 86 | + BootIntoBootloaderAfterReset = true; |
84 | 87 | NVIC_SystemReset();
|
85 | 88 | }
|
86 | 89 |
|
87 | 90 | /* Jump to system memory boot from user application */
|
88 | 91 | WEAK void jumpToBootloader(void)
|
89 | 92 | {
|
90 |
| - enableBackupDomain(); |
91 |
| - if (getBackupRegister(SYSBL_MAGIC_NUMBER_BKP_INDEX) == SYSBL_MAGIC_NUMBER_BKP_VALUE) { |
92 |
| - setBackupRegister(SYSBL_MAGIC_NUMBER_BKP_INDEX, 0); |
| 93 | + // Boot into bootloader if BootIntoBootloaderAfterReset is set. |
| 94 | + // Note that BootIntoBootloaderAfterReset is a noinit variable, so it |
| 95 | + // s not automatically initialized on startup (so it can keep its |
| 96 | + // value across resets). At initial poweron, its value can be |
| 97 | + // *anything*, so only consider its value after a software reset. In |
| 98 | + // all cases, clear its value (this both takes care of giving it an |
| 99 | + // initial value after power-up, and prevents booting into the |
| 100 | + // bootloader more than once for a single request). |
| 101 | + bool doBootloader = __HAL_RCC_GET_FLAG(RCC_FLAG_SFTRST) && BootIntoBootloaderAfterReset; |
| 102 | + BootIntoBootloaderAfterReset = false; |
93 | 103 |
|
| 104 | + if (doBootloader) { |
94 | 105 | #ifdef USBCON
|
95 | 106 | USBD_reenumerate();
|
96 | 107 | #endif
|
|
0 commit comments