Hardfault when jumping to app from bootloader

anonymous wrote on Tuesday, September 03, 2013:

Hi All,

I’m using an STM32F407 and IAR EWARM version 6.50.6
I have an old non FreeRTOS boot loader at address 0x08000000 and a FreeRTOS main application at address 0x08010000.  I can download via my boot loader and jump to my application fine.  Obviously the jump to the main application occurs when the CPU is using the MSP.

My new boot loader now contains FreeRTOS and replaces the old boot loader.
When I attempt to jump from a task in the boot loader to the main application, I get a hard fault. 
This happens since the task in the boot loader was using the PSP and this area of SRAM gets initialized when the main application starts causing a function to return to address 0.

I have read other posts and realize I need to be using MSP when I jump to the main application, not the PSP.

I therefore generated an interrupt from within the boot loader task so the CPU was using the MSP and performed the jump.
Before the jump (but from within the IRQ Handler), I stop the FreeRTOS timer, disable all IRQ’s and clear all pending IRQ’s in the NVIC.
I then perform the jump to the main application from within the IRQ Handler.

The jump works fine and the main FreeRTOS based application goes through all its initialization clearing SRAM, creating tasks and queues etc.
I now get a hard fault when “svc 0” is executed within vPortStartFirstTask()
I assume this is caused by the fact that the IRQ_Handler I was executing within the boot loader is still “ACTIVE” as per the NVIC->IABR.

Can anyone suggest the solution please, or correct my assumptions?


anonymous wrote on Wednesday, September 04, 2013:

Hi All,

I have seen the error of my ways!

The fix to my issue involved:
1. Creating the boot loader task that jumps to the main application with portPRIVILEGE_BIT set
2.  __set_CONTROL(0);                                                                             // Change from PSP to MSP
3. __set_MSP(*(__IO uint32_t*) APP_START_ADDRESS);
4. jumpToApplication()

I hope this post helps others.