The System Bootloader in STM32L071KB (RAK4200) does a check of the BOOT0 bit and if BOOT0 is not set it checks the application at addresses 0x8010000 and 0x800000. If the stack pointer is in the SRAM range, the bootloader switches to the program in flash. Therefore, it is impossible to start the system bootloader without BOOT0 officially. But ...
1. In the application, set the flag (MAGIC number) to go to the system bootloader on reset. The address must be greater than the stack pointer of the system loader (see 0x1FF00000).
#define MAGIC_BOOT ( (uint32_t *)( 0x20003000 ) )
*MAGIC_BOOT = 0xDEADBEEF;
NVIC_SystemReset( );
2. After restart check MAGIC value and jump to system bootloader (startup_....s Keil IDE)
MAGIC_BOOT EQU 0x20003000
Reset_Handler PROC
EXPORT Reset_Handler [WEAK]
LDR R3, =MAGIC_BOOT
LDR R1, [R3, #0]
MOVS R0, #0
STR R0, [R3, #0] ; clear for next reboot
LDR R0, =0xDEADDEAD
CMP R1, R0
BNE UseNormalBoot
; Undocumented entry into the System Bootloader
LDR R0, =0x1FF00000 ; load SP SYSTEM FLASH
LDR R1, [R0, #0] ; System Boot stack
MSR MSP, R1
LDR R0, =(0x1FF00466+1) ; Jump to internal System Bootloader
BX R0 ; Now you can use ST Flash Programmer to download main flash
UseNormal
CPSIE I
LDR R0, =__initial_sp ; set stack pointer
MSR MSP, R0
LDR R0, =0xDEADBEEF ; the same value as set in application (see above)
CMP R1, R0
BNE AppStart
; start SYSTEM FLASH bootloader
LDR R4, =0x40021034 ; RCC_APB2ENR
MOVS R5, #0x1 ; enable SYSCFG/COMP clock
STR R5, [R4]
LDR R4, =0x40010000 ; SYSCFG
MOVS R5, #0x1
STR R5, [R4] ; = 01, map SYSTEM FLASH (doesn't really switch)
LDR R0, =0xDEADDEAD ; Use for undocumented entry to System Bootloader
STR R0, [R3, #0]
LDR R0, =0x1FF00000 ; load SP and PC from SYSTEM FLASH
LDR R1, [R0, #0] ; System Boot stack
MSR MSP, R1
LDR R1, [R0, #4] ; System Bootloader Reset Entry
BX R1 ; Start but really back to Reset_Handler from System Bootloader
B Reset_Handler ; really never return
IMPORT __main
IMPORT SystemInit
AppStart
LDR R0, =SystemInit
BLX R0
LDR R0, =__main
BX R0
ENDP
0 comment(s) so far