Skip to content

Commit 238898b

Browse files
committed
Jump to system memory boot from user application
Fixes stm32duino#706 Signed-off-by: Frederic Pillon <[email protected]>
1 parent b8dfe20 commit 238898b

File tree

5 files changed

+167
-2
lines changed

5 files changed

+167
-2
lines changed

Diff for: cores/arduino/stm32/backup.h

+9
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ 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+
6271
/* Exported functions ------------------------------------------------------- */
6372
static inline void resetBackupDomain(void)
6473
{

Diff for: cores/arduino/stm32/bootloader.h

+6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414
extern "C" {
1515
#endif /* __cplusplus */
1616

17+
/* Request to jump to system memory boot */
18+
void jumpToBootloaderRequested(void);
19+
20+
/* Jump to system memory boot from user application */
21+
void jumpToBootloader(void);
22+
1723
#ifdef DTR_TOGGLING_SEQ
1824
/* DTR toggling sequence management */
1925
void dtr_togglingHook(uint8_t *buf, uint32_t *len);

Diff for: cores/arduino/stm32/usb/cdc/usbd_cdc_if.c

+3
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@ static int8_t USBD_CDC_Control(uint8_t cmd, uint8_t *pbuf, uint16_t length)
168168
linecoding.format = pbuf[4];
169169
linecoding.paritytype = pbuf[5];
170170
linecoding.datatype = pbuf[6];
171+
if (linecoding.bitrate == 1200) {
172+
jumpToBootloaderRequested();
173+
}
171174
break;
172175

173176
case CDC_GET_LINE_CODING:

Diff for: libraries/SrcWrapper/src/stm32/bootloader.c

+143
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,147 @@
22

33
#include "stm32_def.h"
44
#include "backup.h"
5+
#include "stm32yyxx_ll_system.h"
6+
#include "usbd_if.h"
57

8+
/*
9+
* STM32 built-in bootloader in system memory support
10+
*/
11+
/* Private definitions to manage system memory address */
12+
#define SYSMEM_ADDR_COMMON 0xFFF
13+
14+
typedef struct {
15+
uint32_t devID;
16+
uint32_t sysMemAddr;
17+
} devSysMemAddr_str;
18+
19+
devSysMemAddr_str devSysMemAddr[] = {
20+
#ifdef STM32F0xx
21+
{0x440, 0x1FFFEC00},
22+
{0x444, 0x1FFFEC00},
23+
{0x442, 0x1FFFD800},
24+
{0x445, 0x1FFFC400},
25+
{0x448, 0x1FFFC800},
26+
#elif STM32F1xx
27+
{0x412, 0x1FFFF000},
28+
{0x410, 0x1FFFF000},
29+
{0x414, 0x1FFFF000},
30+
{0x420, 0x1FFFF000},
31+
{0x428, 0x1FFFF000},
32+
{0x418, 0x1FFFB000},
33+
{0x430, 0x1FFFE000},
34+
#elif STM32F2xx
35+
{0x411, 0x1FFF0000},
36+
#elif STM32F3xx
37+
{SYSMEM_ADDR_COMMON, 0x1FFFD800},
38+
#elif STM32F4xx
39+
{SYSMEM_ADDR_COMMON, 0x1FFF0000},
40+
#elif STM32F7xx
41+
{SYSMEM_ADDR_COMMON, 0x1FF00000},
42+
#elif STM32G0xx
43+
{SYSMEM_ADDR_COMMON, 0x1FFF0000},
44+
#elif STM32G4xx
45+
{SYSMEM_ADDR_COMMON, 0x1FFF0000},
46+
#elif STM32H7xx
47+
{0x450, 0x1FF00000},
48+
#elif STM32L0xx
49+
{SYSMEM_ADDR_COMMON, 0x1FF00000},
50+
#elif STM32L1xx
51+
{SYSMEM_ADDR_COMMON, 0x1FF00000},
52+
#elif STM32L4xx
53+
{SYSMEM_ADDR_COMMON, 0x1FFF0000},
54+
#elif STM32WBxx
55+
{SYSMEM_ADDR_COMMON, 0x1FFF0000},
56+
#else
57+
#warning "No system memory address for this serie!"
58+
#endif
59+
{0x0000, 0x00000000}
60+
};
61+
62+
uint32_t getSysMemAddr(void)
63+
{
64+
uint32_t sysMemAddr = 0;
65+
if (devSysMemAddr[0].devID == SYSMEM_ADDR_COMMON) {
66+
sysMemAddr = devSysMemAddr[0].sysMemAddr;
67+
} else {
68+
uint32_t devId = LL_DBGMCU_GetDeviceID();
69+
for (uint32_t id = 0; devSysMemAddr[id].devID != 0; id++) {
70+
if (devSysMemAddr[id].devID == devId) {
71+
sysMemAddr = devSysMemAddr[id].sysMemAddr;
72+
break;
73+
}
74+
}
75+
}
76+
return sysMemAddr;
77+
}
78+
79+
/* Request to jump to system memory boot */
80+
WEAK void jumpToBootloaderRequested(void)
81+
{
82+
enableBackupDomain();
83+
setBackupRegister(SYSBL_MAGIC_NUMBER_BKP_INDEX, SYSBL_MAGIC_NUMBER_BKP_VALUE);
84+
NVIC_SystemReset();
85+
}
86+
87+
/* Jump to system memory boot from user application */
88+
WEAK void jumpToBootloader(void)
89+
{
90+
enableBackupDomain();
91+
if (getBackupRegister(SYSBL_MAGIC_NUMBER_BKP_INDEX) == SYSBL_MAGIC_NUMBER_BKP_VALUE) {
92+
setBackupRegister(SYSBL_MAGIC_NUMBER_BKP_INDEX, 0);
93+
94+
#ifdef USBCON
95+
USBD_reenumerate();
96+
#endif
97+
void (*sysMemBootJump)(void);
98+
99+
/**
100+
* Get system memory address
101+
*
102+
* Available in AN2606 document:
103+
* Table xxx Bootloader device-dependent parameters
104+
*/
105+
volatile uint32_t sysMem_addr = getSysMemAddr();
106+
/*
107+
* If the system address is not not referenced in 'devSysMemAddr' array,
108+
* does not do anything
109+
*/
110+
if (sysMem_addr != 0) {
111+
#ifdef __HAL_SYSCFG_REMAPMEMORY_SYSTEMFLASH
112+
/* Remap system Flash memory at address 0x00000000 */
113+
__HAL_SYSCFG_REMAPMEMORY_SYSTEMFLASH();
114+
#endif
115+
116+
/**
117+
* Set jump memory location for system memory
118+
* Use address with 4 bytes offset which specifies jump location
119+
* where program starts
120+
*/
121+
sysMemBootJump = (void (*)(void))(*((uint32_t *)(sysMem_addr + 4)));
122+
123+
/**
124+
* Set main stack pointer.
125+
* This step must be done last otherwise local variables in this function
126+
* don't have proper value since stack pointer is located on different position
127+
*
128+
* Set direct address location which specifies stack pointer in SRAM location
129+
*/
130+
__set_MSP(*(uint32_t *)sysMem_addr);
131+
132+
/**
133+
* Jump to set location
134+
* This will start system memory execution
135+
*/
136+
sysMemBootJump();
137+
138+
while (1);
139+
}
140+
}
141+
}
142+
143+
/*
144+
* Legacy maple bootloader support
145+
*/
6146
#ifdef BL_LEGACY_LEAF
7147
void dtr_togglingHook(uint8_t *buf, uint32_t *len)
8148
{
@@ -17,6 +157,9 @@ void dtr_togglingHook(uint8_t *buf, uint32_t *len)
17157
}
18158
#endif /* BL_LEGACY_LEAF */
19159

160+
/*
161+
* HID bootloader support
162+
*/
20163
#ifdef BL_HID
21164
void dtr_togglingHook(uint8_t *buf, uint32_t *len)
22165
{

Diff for: libraries/SrcWrapper/src/stm32/hw_config.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@
3535
*
3636
******************************************************************************
3737
*/
38-
#include "stm32_def.h"
38+
#include "bootloader.h"
39+
#include "dwt.h"
3940
#include "hw_config.h"
4041
#include "usbd_if.h"
41-
#include "dwt.h"
42+
#include "stm32_def.h"
4243

4344
#ifdef __cplusplus
4445
extern "C" {
@@ -59,6 +60,9 @@ void hw_config_init(void)
5960
/* Initialize the HAL */
6061
HAL_Init();
6162

63+
/* Check if a jump to system memory boot requested */
64+
jumpToBootloader();
65+
6266
/* Configure the system clock */
6367
SystemClock_Config();
6468

0 commit comments

Comments
 (0)