Skip to content

Commit

Permalink
Jump to system memory boot from user application
Browse files Browse the repository at this point in the history
Fixes stm32duino#706

Signed-off-by: Frederic Pillon <[email protected]>
  • Loading branch information
fpistm committed Oct 17, 2019
1 parent e1f9868 commit 1f9772e
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 3 deletions.
2 changes: 0 additions & 2 deletions cores/arduino/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#define ARDUINO_MAIN
#include "Arduino.h"


// Weak empty variant initialization function.
// May be redefined by variant files.
void initVariant() __attribute__((weak));
Expand All @@ -30,7 +29,6 @@ void initVariant() { }
// Otherwise, statically allocated objects that need HAL may fail.
__attribute__((constructor(101))) void premain()
{

// Required by FreeRTOS, see http:https://www.freertos.org/RTOS-Cortex-M3-M4.html
#ifdef NVIC_PRIORITYGROUP_4
HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
Expand Down
7 changes: 7 additions & 0 deletions cores/arduino/stm32/backup.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ extern "C" {
#endif /* HID_MAGIC_NUMBER_BKP_VALUE */
#endif /* BL_HID */

#if !defined(SYSBL_MAGIC_NUMBER_BKP_INDEX)
#define SYSBL_MAGIC_NUMBER_BKP_INDEX LL_RTC_BKP_DR2
#endif /* SYSBL_MAGIC_NUMBER_BKP_INDEX */
#ifndef SYSBL_MAGIC_NUMBER_BKP_VALUE
#define SYSBL_MAGIC_NUMBER_BKP_VALUE 0x515B
#endif /* SYSBL_MAGIC_NUMBER_BKP_VALUE */

/* Exported functions ------------------------------------------------------- */
static inline void resetBackupDomain(void)
{
Expand Down
58 changes: 58 additions & 0 deletions cores/arduino/stm32/bootloader.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "stm32_def.h"
#include "backup.h"
#include "usbd_if.h"

#ifdef BL_LEGACY_LEAF
void dtr_togglingHook(uint8_t *buf, uint32_t *len)
Expand Down Expand Up @@ -37,3 +38,60 @@ void dtr_togglingHook(uint8_t *buf, uint32_t *len)
}
}
#endif /* BL_HID */

/* Request to jump to system memory boot */
void jumpToBootloaderRequested(void)
{
enableBackupDomain();
setBackupRegister(SYSBL_MAGIC_NUMBER_BKP_INDEX, SYSBL_MAGIC_NUMBER_BKP_VALUE);
NVIC_SystemReset();
}

/* Jump to system memory boot from user application */
void jumpToBootloader(void)
{
enableBackupDomain();
if (getBackupRegister(SYSBL_MAGIC_NUMBER_BKP_INDEX) == SYSBL_MAGIC_NUMBER_BKP_VALUE) {
setBackupRegister(SYSBL_MAGIC_NUMBER_BKP_INDEX, 0);

#ifdef USBCON
USBD_reenumerate();
#endif
void (*sysMemBootJump)(void);

/**
* Get system memory address
*
* Available in AN2606 document:
* Table 116. Bootloader device-dependent parameters
*/
volatile uint32_t sysMem_addr = 0x1FFF0000;

/* Remap system Flash memory at address 0x00000000 */
__HAL_SYSCFG_REMAPMEMORY_SYSTEMFLASH();

/**
* Set jump memory location for system memory
* Use address with 4 bytes offset which specifies jump location
* where program starts
*/
sysMemBootJump = (void (*)(void))(*((uint32_t *)(sysMem_addr + 4)));

/**
* Set main stack pointer.
* This step must be done last otherwise local variables in this function
* don't have proper value since stack pointer is located on different position
*
* Set direct address location which specifies stack pointer in SRAM location
*/
__set_MSP(*(uint32_t *)sysMem_addr);

/**
* Jump to set location
* This will start system memory execution
*/
sysMemBootJump();

while (1);
}
}
6 changes: 6 additions & 0 deletions cores/arduino/stm32/bootloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ extern "C" {
void dtr_togglingHook(uint8_t *buf, uint32_t *len);
#endif

/* Request to jump to system memory boot */
void jumpToBootloaderRequested(void);

/* Jump to system memory boot from user application */
void jumpToBootloader(void);

#ifdef __cplusplus
}
#endif /* __cplusplus */
Expand Down
6 changes: 5 additions & 1 deletion cores/arduino/stm32/hw_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@
*/
#include "stm32_def.h"
#include "hw_config.h"
#include "usbd_if.h"
#include "dwt.h"
#include "bootloader.h"
#include "usbd_if.h"

#ifdef __cplusplus
extern "C" {
Expand All @@ -59,6 +60,9 @@ void hw_config_init(void)
/* Initialize the HAL */
HAL_Init();

/* Check if a jump to system memory boot requested */
jumpToBootloader();

/* Configure the system clock */
SystemClock_Config();

Expand Down
3 changes: 3 additions & 0 deletions cores/arduino/stm32/usb/cdc/usbd_cdc_if.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ static int8_t USBD_CDC_Control(uint8_t cmd, uint8_t *pbuf, uint16_t length)
linecoding.format = pbuf[4];
linecoding.paritytype = pbuf[5];
linecoding.datatype = pbuf[6];
if (linecoding.bitrate == 1200) {
jumpToBootloaderRequested();
}
break;

case CDC_GET_LINE_CODING:
Expand Down

0 comments on commit 1f9772e

Please sign in to comment.