I am currently working on a project using the STM32F072RBT6 microcontroller, where I have implemented a custom bootloader to load an application program into flash memory. However, after the bootloader successfully writes the application binary, the stack pointer does not correctly jump to the application’s address, and the application fails to execute.
MCU Model: STM32F072RBT6 Bootloader
Functionality: The bootloader receives the application binary and writes it to flash memory at the designated application address.
Application Flash Address: 0x08004000
Problem: After the bootloader completes, I attempt to jump to the application by setting the stack pointer and program counter accordingly, but the application does not start. It appears that the stack pointer is not correctly set to the application's address.
Jump Code in Bootloader:
static void goto_application(void) {
void (* app_reset_handler)(void) = (void *)(*(volatile uint32_t *) (0x08004000 + 4));
__disable_irq();
__set_MSP((*(volatile uint32_t *)0x08004000));
__enable_irq();
app_reset_handler();
}
... = (void (*)(void)) (0x08004000 + 4);
. Converting function pointers tovoid*
or vice versa is undefined behavior. Converting integers to function pointers is OK though, if the compiler allows it and if the address is corresponding to the function alignment requirement \$\endgroup\$