KL82: Can't jump to application from custom bootloader

cancel
Showing results for 
Search instead for 
Did you mean: 

KL82: Can't jump to application from custom bootloader

Jump to solution
55 Views
marcwillem
Contributor II

Hello,

I needed to implement a custom bootloader, as I am using a MCP2515 CAN Transceiver for flashing.

I can program the device without issue (verified by readback and MCUxpresso Memory Watcher).
When I jump to the application, I get a hardfault (unknown hard fault at 0x2900B289).

I can see the PC jumping to the application (0x0001 0000), but then it get's stuck somewhere.
The program is fairly simple but jumps to the hard fault handler immediately after the jump;

If I reset the program via SWD I get another hard fault, this time the PC is at 0F64, which is in my bootloader;

I configured the app in MCUxpresso for the flash to start at 0x00010000 (at C/C++ Build / MCU settings) and enabled position independent code.

This is the code of my application;

uint8_t i=0;
int main(void)
{
while (1) i++;
return 0 ;
}

 

This is how I jump to the application

#define START_OF_APPLICATION 0x00010000ul
static void (*go_to_app)(void) = 0;
go_to_app = (void (*)(void))(START_OF_APPLICATION);
SCB->VTOR = START_OF_APPLICATION;
__set_MSP(START_OF_APPLICATION);
__set_PSP(START_OF_APPLICATION);
go_to_app();

Did I forget anything?
Do you know why I get the hard fault or how I could debug it?

 

 

 

 

 

 

0 Kudos
1 Solution
33 Views
marcwillem
Contributor II

I found the solution here:
https://stackoverflow.com/questions/58296733/cortex-m0-jump-to-user-application-failing

#define PROGRAM_VECTOR_TABLE ((uint32_t *) PROGRAM_ADDRESS)

void jump_to_app(void)
{
    static void (*go_to_app)(void) = 0;
    go_to_app = (void (*)(void))(PROGRAM_VECTOR_TABLE[1]);
    SCB->VTOR = (uint32_t)PROGRAM_VECTOR_TABLE;
    __set_MSP(PROGRAM_VECTOR_TABLE[0]);
    __set_PSP(PROGRAM_VECTOR_TABLE[0]);
    go_to_app();
}
  


 

View solution in original post

0 Kudos
2 Replies
36 Views
marcwillem
Contributor II

I figured I was missing the offset of 4 for calling the app.

static void (*go_to_app)(void) = 0;
go_to_app = (void (*)(void))(START_OF_APPLICATION+4);
SCB->VTOR = START_OF_APPLICATION;
__set_MSP(START_OF_APPLICATION);
__set_PSP(START_OF_APPLICATION);
__DSB();
go_to_app();

 

One issue is remaining, the application directly hard faults on execution. If I issue an reset from my debugging tool (SEGGER Ozone) it will start the application.

0 Kudos
34 Views
marcwillem
Contributor II

I found the solution here:
https://stackoverflow.com/questions/58296733/cortex-m0-jump-to-user-application-failing

#define PROGRAM_VECTOR_TABLE ((uint32_t *) PROGRAM_ADDRESS)

void jump_to_app(void)
{
    static void (*go_to_app)(void) = 0;
    go_to_app = (void (*)(void))(PROGRAM_VECTOR_TABLE[1]);
    SCB->VTOR = (uint32_t)PROGRAM_VECTOR_TABLE;
    __set_MSP(PROGRAM_VECTOR_TABLE[0]);
    __set_PSP(PROGRAM_VECTOR_TABLE[0]);
    go_to_app();
}
  


 

View solution in original post

0 Kudos