I'm working on a STM32F401 MCU with custom bootloader and application. Compiler is GCC 5.2.1, not optimizations are running.
I'm getting a hardfault after the first interrupt after the following jump sequence: bootloader -> application -> bootloader -> application. After the first jump to the application from the bootloader, the system is working properly. However, after jumping to the application after jumping back to the bootloader (I'm not resetting the board on purpose), the hardfault happens after the first interrupt that may be anything from SysTick to EXTI.
What could be the reason for this? Anything that I'm not updating? Thanks.
stubs of the code:
jumping procedure (for both programs; application is at 0x08008000 and bootloader is at 0x08000000):
typedef void (*pFunction)(void);
uint32_t appStack;
pFunction appEntry;
//Jump to address
/* Get the application stack pointer */
appStack = (uint32_t) * ((__IO uint32_t*)address);
/* Get the application entry point */
appEntry = (pFunction) * (__IO uint32_t*) (address + 4);
/* Reconfigure vector table offset */
SCB->VTOR = address;
__set_MSP(appStack);
appEntry();
application cleanup before jumping:
osThreadSuspendAll();
__disable_irq();
SysTick->CTRL =0;
SysTick->LOAD=0;
SysTick->VAL=0;
__set_PRIMASK(1);
HAL_UART_DeInit(&huart2);
HAL_I2C_DeInit(&hi2c1);
HAL_RCC_DeInit();
HAL_DeInit();
来源:https://stackoverflow.com/questions/38453553/stm32-hard-fault-when-jumping-to-application-from-custom-bootloader