Skip to content

Commit 6e834ce

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

File tree

6 files changed

+79
-3
lines changed

6 files changed

+79
-3
lines changed

cores/arduino/main.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#define ARDUINO_MAIN
2121
#include "Arduino.h"
2222

23-
2423
// Weak empty variant initialization function.
2524
// May be redefined by variant files.
2625
void initVariant() __attribute__((weak));
@@ -30,7 +29,6 @@ void initVariant() { }
3029
// Otherwise, statically allocated objects that need HAL may fail.
3130
__attribute__((constructor(101))) void premain()
3231
{
33-
3432
// Required by FreeRTOS, see http://www.freertos.org/RTOS-Cortex-M3-M4.html
3533
#ifdef NVIC_PRIORITYGROUP_4
3634
HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);

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
{

cores/arduino/stm32/bootloader.c

+58
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "stm32_def.h"
44
#include "backup.h"
5+
#include "usbd_if.h"
56

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

cores/arduino/stm32/bootloader.h

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

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

cores/arduino/stm32/hw_config.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@
3737
*/
3838
#include "stm32_def.h"
3939
#include "hw_config.h"
40-
#include "usbd_if.h"
4140
#include "dwt.h"
41+
#include "bootloader.h"
42+
#include "usbd_if.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

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)