Skip to content

Commit 618bfaa

Browse files
matthijskooijmanfpistm
authored andcommitted
Do not use the RTC to track a bootloader request
Instead, use a .noinit variable, which survives across reboots. Note: This file uses the bool type, so needs to include stdbool.h. It seems that for the F4, this was indirectly included elsewhere, but for e.g. F0, this did not seem to be the case, causing the build to fail.
1 parent 174a6e2 commit 618bfaa

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

cores/arduino/stm32/backup.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,6 @@ extern "C" {
5959
#endif /* HID_MAGIC_NUMBER_BKP_VALUE */
6060
#endif /* BL_HID */
6161

62-
#if !defined(SYSBL_MAGIC_NUMBER_BKP_INDEX) && defined(ENABLE_BACKUP_SUPPORT)
63-
#define SYSBL_MAGIC_NUMBER_BKP_INDEX LL_RTC_BKP_DR2
64-
#else
65-
#define SYSBL_MAGIC_NUMBER_BKP_INDEX 0
66-
#endif /* SYSBL_MAGIC_NUMBER_BKP_INDEX */
67-
#ifndef SYSBL_MAGIC_NUMBER_BKP_VALUE
68-
#define SYSBL_MAGIC_NUMBER_BKP_VALUE 0x515B
69-
#endif /* SYSBL_MAGIC_NUMBER_BKP_VALUE */
70-
7162
/* Exported functions ------------------------------------------------------- */
7263
static inline void resetBackupDomain(void)
7364
{

libraries/SrcWrapper/src/stm32/bootloader.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#include <stdbool.h>
2+
13
#include "bootloader.h"
24

35
#include "stm32_def.h"
@@ -11,6 +13,8 @@
1113
/* Private definitions to manage system memory address */
1214
#define SYSMEM_ADDR_COMMON 0xFFF
1315

16+
static bool BootIntoBootloaderAfterReset __attribute__((__section__(".noinit")));
17+
1418
typedef struct {
1519
uint32_t devID;
1620
uint32_t sysMemAddr;
@@ -79,18 +83,25 @@ uint32_t getSysMemAddr(void)
7983
/* Request to jump to system memory boot */
8084
WEAK void jumpToBootloaderRequested(void)
8185
{
82-
enableBackupDomain();
83-
setBackupRegister(SYSBL_MAGIC_NUMBER_BKP_INDEX, SYSBL_MAGIC_NUMBER_BKP_VALUE);
86+
BootIntoBootloaderAfterReset = true;
8487
NVIC_SystemReset();
8588
}
8689

8790
/* Jump to system memory boot from user application */
8891
WEAK void jumpToBootloader(void)
8992
{
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;
93103

104+
if (doBootloader) {
94105
#ifdef USBCON
95106
USBD_reenumerate();
96107
#endif

0 commit comments

Comments
 (0)