Custom Bootloader Startup

Hi, I am busy building a custom bootloader for a project of mine, and before I even code to check the images and erase and reflash I wanted to at the least get the bootloader application to jump to the main application that I have.

Now the Linker files are as follows :

Bootloader linker file

MEMORY
{
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 0x0080000 /* 512KB */
RAM (rwx) : ORIGIN = 0x1FFE0000, LENGTH = 0x00A0000 /* 640K */
DATA_FLASH (rx) : ORIGIN = 0x40100000, LENGTH = 0x0010000 /* 64K */
QSPI_FLASH (rx) : ORIGIN = 0x60000000, LENGTH = 0x10000000 /* 256M, Change in QSPI section below also */
SDRAM (rwx) : ORIGIN = 0x90000000, LENGTH = 0x2000000 /* 32M */
}

Application linker file

MEMORY
{
FLASH (rx) : ORIGIN = 0x00080000, LENGTH = 0x0180000 /* 1.5M */
RAM (rwx) : ORIGIN = 0x1FFE0000, LENGTH = 0x00A0000 /* 640K */
DATA_FLASH (rx) : ORIGIN = 0x40100000, LENGTH = 0x0010000 /* 64K */
QSPI_FLASH (rx) : ORIGIN = 0x60000000, LENGTH = 0x10000000 /* 256M, Change in QSPI section below also */
SDRAM (rwx) : ORIGIN = 0x90000000, LENGTH = 0x2000000 /* 32M */
ID_CODES (rx) : ORIGIN = 0x0100A150, LENGTH = 0x10 /* 16 bytes */
}

Then the bootloader code is as bare and simple as it comes, this is the code:

void hal_entry(void)
{
main_fnptr *p_jump_to_app; // Function pointer main that will be used to jump to application

while(1){
p_jump_to_app = (main_fnptr*)0x80000 + 1U;

__disable_irq();

/* Set the Vector Table Offset Register */
uint32_t * p_VTOR = (uint32_t *)0xE000ED08U;
*p_VTOR = (uint32_t)0x80000;

__DSB();

/* Ensure that when we jump we're using the Main Stack Pointer */
__set_CONTROL(0);

__set_MSP(*(uint32_t *)0x80000);

(*p_jump_to_app)();
}
}

Now if I run the main application in debug mode with the following debug settings

Then the system works fine, but if I load the bootloader project with the main application elf in startup, then it fails with pc ending up where it should not. see image

Now if I change the linker scripts to allow ram for the bootloader and application separately like this :

RAM (rwx) : ORIGIN = 0x1FFE0000, LENGTH = 0x0002000 /* 8K */

RAM (rwx) : ORIGIN = 0x1FFE2000, LENGTH = 0x009E000 /* 632K */

Now the system starts to seem better accept that it seems its stuck in the main application at interrupt vectors see image:

So I hope someone can guide me in what I am doing wrong, also the Main application is a ThreadX application.

Regards, Ronald Hare

  • You should disable the CPU Stack Pointer Monitor before jumping from the bootloader to the main application :-

    R_SPMON->MSPMPUPT = 0xA500;
    R_SPMON->PSPMPUPT = 0xA500;

    R_SPMON->MSPMPUCTL = 0x00;
    R_SPMON->PSPMPUCTL = 0x00;
  • In reply to Jeremy:

    Hi Jeremy,

    thanks for the fast reply, I have tried this with both linker configuration of the ram settings and it does not work, I added the lines of code you gave as follows :

    p_jump_to_app = (main_fnptr*)0x00080000+1U;

    __disable_irq();

    /* Set the Vector Table Offset Register */
    uint32_t * p_VTOR = (uint32_t *)0xE000ED08;
    *p_VTOR = (uint32_t)0x00080000;

    __DSB();

    /* Ensure that when we jump we're using the Main Stack Pointer */
    __set_CONTROL(0);

    __set_MSP(*((uint32_t*)0x00080000));

    R_SPMON->MSPMPUPT = 0xA500;
    R_SPMON->PSPMPUPT = 0xA500;

    R_SPMON->MSPMPUCTL = 0x00;
    R_SPMON->PSPMPUCTL = 0x00;

    (*p_jump_to_app)();

    still with no luck, am I putting it in the wrong place?

    Regards, Ronald Hare
  • In reply to Jeremy:

    Hi Jeremy,

    I changed it to this location and now it works:
    p_jump_to_app = (main_fnptr*)0x00080000+1U;

    __disable_irq();

    R_SPMON->MSPMPUPT = 0xA500;
    R_SPMON->PSPMPUPT = 0xA500;

    R_SPMON->MSPMPUCTL = 0x00;
    R_SPMON->PSPMPUCTL = 0x00;

    /* Set the Vector Table Offset Register */
    uint32_t * p_VTOR = (uint32_t *)0xE000ED08;
    *p_VTOR = (uint32_t)0x00080000;

    __DSB();

    /* Ensure that when we jump we're using the Main Stack Pointer */
    __set_CONTROL(0);

    __set_MSP(*((uint32_t*)0x00080000));

    (*p_jump_to_app)();

    thanks for the help.

    Regards, Ronald Hare