Skip to content

Commit 84f6647

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

File tree

5 files changed

+80
-3
lines changed

5 files changed

+80
-3
lines changed

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

+7
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ extern "C" {
5858
#endif /* HID_MAGIC_NUMBER_BKP_VALUE */
5959
#endif /* BL_HID */
6060

61+
#if !defined(SYSBL_MAGIC_NUMBER_BKP_INDEX)
62+
#define SYSBL_MAGIC_NUMBER_BKP_INDEX LL_RTC_BKP_DR2
63+
#endif /* SYSBL_MAGIC_NUMBER_BKP_INDEX */
64+
#ifndef SYSBL_MAGIC_NUMBER_BKP_VALUE
65+
#define SYSBL_MAGIC_NUMBER_BKP_VALUE 0x515B
66+
#endif /* SYSBL_MAGIC_NUMBER_BKP_VALUE */
67+
6168
/* Exported functions ------------------------------------------------------- */
6269
static inline void resetBackupDomain(void)
6370
{

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

+58-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "bootloader.h"
2-
32
#include "stm32_def.h"
43
#include "backup.h"
4+
#include "usbd_if.h"
55

66
#ifdef BL_LEGACY_LEAF
77
void dtr_togglingHook(uint8_t *buf, uint32_t *len)
@@ -37,3 +37,60 @@ void dtr_togglingHook(uint8_t *buf, uint32_t *len)
3737
}
3838
}
3939
#endif /* BL_HID */
40+
41+
/* Request to jump to system memory boot */
42+
void jumpToBootloaderRequested(void)
43+
{
44+
enableBackupDomain();
45+
setBackupRegister(SYSBL_MAGIC_NUMBER_BKP_INDEX, SYSBL_MAGIC_NUMBER_BKP_VALUE);
46+
NVIC_SystemReset();
47+
}
48+
49+
/* Jump to system memory boot from user application */
50+
void jumpToBootloader(void)
51+
{
52+
enableBackupDomain();
53+
if (getBackupRegister(SYSBL_MAGIC_NUMBER_BKP_INDEX) == SYSBL_MAGIC_NUMBER_BKP_VALUE) {
54+
setBackupRegister(SYSBL_MAGIC_NUMBER_BKP_INDEX, 0);
55+
56+
#ifdef USBCON
57+
USBD_reenumerate();
58+
#endif
59+
void (*sysMemBootJump)(void);
60+
61+
/**
62+
* Get system memory address
63+
*
64+
* Available in AN2606 document:
65+
* Table 116. Bootloader device-dependent parameters
66+
*/
67+
volatile uint32_t sysMem_addr = 0x1FFF0000;
68+
69+
/* Remap system Flash memory at address 0x00000000 */
70+
__HAL_SYSCFG_REMAPMEMORY_SYSTEMFLASH();
71+
72+
/**
73+
* Set jump memory location for system memory
74+
* Use address with 4 bytes offset which specifies jump location
75+
* where program starts
76+
*/
77+
sysMemBootJump = (void (*)(void))(*((uint32_t *)(sysMem_addr + 4)));
78+
79+
/**
80+
* Set main stack pointer.
81+
* This step must be done last otherwise local variables in this function
82+
* don't have proper value since stack pointer is located on different position
83+
*
84+
* Set direct address location which specifies stack pointer in SRAM location
85+
*/
86+
__set_MSP(*(uint32_t *)sysMem_addr);
87+
88+
/**
89+
* Jump to set location
90+
* This will start system memory execution
91+
*/
92+
sysMemBootJump();
93+
94+
while (1);
95+
}
96+
}

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

+6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ extern "C" {
1919
void dtr_togglingHook(uint8_t *buf, uint32_t *len);
2020
#endif
2121

22+
/* Request to jump to system memory boot */
23+
void jumpToBootloaderRequested(void);
24+
25+
/* Jump to system memory boot from user application */
26+
void jumpToBootloader(void);
27+
2228
#ifdef __cplusplus
2329
}
2430
#endif /* __cplusplus */

Diff for: cores/arduino/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

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:

0 commit comments

Comments
 (0)