STM32L412: When I turn the reset (physical) switch, the µC jumps into the DFU bootloader, although it isn't supposed to
Hi there!
I have a weird problem, where I'm a bit lost, probably also due to my limited knowledge of assembly. So I am trying to write a function (which is called using a very secret pattern) which sets a magic number and soft resets the microcontroller. On startup, a comparison to the magic number is made, and it either jumps into the DFU bootloader, or continues regular startup.
But somehow even when I press the physical reset switch, it goes into the bootloader, although it shouldn't. And I'd like to ask for your advice why.
Here's the reset handler. Line 3 to line 8 is what I have added. It loads the bootloader_flag address, reads out bootloader_flag, loads magic number 0x00C0FFEE, and clears bootloader_flag. Then it compares the dereferenced bootloader_flag in r2 to the magic number in r1 and decides whether to jump or to continue.
Reset_Handler:
/*Check for bootloader*/
ldr r0, =_bootloader_flag // Load bootloader flag address
ldr r1, =0x00C0FFEE // Load magic number
ldr r2, [r0, #0] // Dereference flag
str r0, [r0, #0] // Clear
cmp r2, r1 // Check flag
beq Reboot_Loader // Flag detected? Go to loader
ldr sp, =_estack /* Set stack pointer */
/* Copy the data segment initializers from flash to SRAM */
movs r1, #0
b LoopCopyDataInitHere's the code of Reboot_Loader:
Reboot_Loader:
ldr r0, =0x1FFF0000 // Load bootloader address
ldr r1, [r0, #0] // Load default stack pointer
mov sp, r1 // Set stack pointer to default
ldr r0, [r0, #4] // Load bootloader +4
bx r0 // Go to bootloader +4And finally the code piece that triggers the dirty reset:
volatile uint32_t _bootloader_flag = 0x00000001;
/**
* @brief Write dirty bit and go into DFU mode bootloader
* @param bootloader_status
*/
void activate_bootloader(uint32_t bootloader_status) {
if(bootloader_status == 1) {
// Write DFU message
NVIC_DisableIRQ(TIM1_UP_TIM16_IRQn);
WriteSomeMessageToNotifyUser("");
NVIC_EnableIRQ(TIM1_UP_TIM16_IRQn);
// Disable interrupts
__disable_irq();
// http://blog.fahhem.com/2017/12/jump-to-emb-bootloader/
_bootloader_flag = 0x00C0FFEE; // Magic number flag set
// See you latte
NVIC_SystemReset();
while(1);
}
}Thank you for your help.
I'm using STM32CubeMX, everything HAL, SystemWorkbench, macOS Catalina and the microcontroller is an STM32L412KB.
Stay safe and cheers.
