Using embOS in a bootloader and in an application. When jump to Application get a hard fault

This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

  • Using embOS in a bootloader and in an application. When jump to Application get a hard fault

    Hello Folks,

    Our engineering group is using embOS for their application code.
    I am trying to re-use a lot of that code for a bootloader for our project. This means that I have to use embOS in the bootloader.

    The processor is a ST32F103VG (Cortex M3) running at 24 MHz. Compiler tools is IAR ver 6.6

    The problem I have is when I try to jump to the application at the completion of the boot load I get a hard fault.
    I have checked that the App binary is good. If I load the app and then jump to it with a non-OS version of the bootloader, everything works.

    I do the jump to the Application inside a task with:

    NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0xF800);
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);

    uint32_t* address = (uint32_t *)(0x0800F804);
    __set_MSP(*(__IO uint32_t*)0x0800F800); //set the stack pointer to that of the application
    ((void (*)())(*address))(); // pointer recast as function pointer and the dereferenced/called

    I think part of the problem is that the bootloader calls OS_InitHw() and OS_Start(), but then when the Application starts it calls those routines again.
    Is there a way to stop or completely unload the changes the embOS has made once it starts so that a second invocation of the OS would work like a
    fresh start of the OS?

    Thank you in advance,

    Dave


    Are there any other issues that could cause my problem like using the wrong stack, or interrupts still firing from OS when change the int vector table?
  • Your suggestion was correct.
    I used the assembly instructions to switch over to the MSP and the
    Application starts up fine now. Thank you very much for your help !

    Here is what the tail end of my bootloader looks like:

    //check for valid app
    if(gAlldone) //jump to application via function pointer
    {
    OS_SuspendAllTasks(); // no more task switching at this point

    __disable_interrupt(); // stop all interrupts

    OS_EnterRegion(); // make sure OS does not bother us

    HAL_Interrupt_DisableSysTick(); // Stop OS tick to effectively stop the OS

    uint32_t* address = (uint32_t *)(0x0800F804); // this is the addr of the Application
    __set_MSP(*(__IO uint32_t*)0x0800F800); //set the main stack pointer to that of the application

    asm(" mov r4, #0"); // make sure you are in priveleged mode
    asm(" msr CONTROL, r4"); // change over to the MSP (vs. PSP) stack for the application

    ((void (*)())(*address))(); // a Never Return call to the application.
    // pointer recast as function pointer and the dereferenced/called

    while (1); // this while is for debug. You should Never hit it.
    }

    -Dave





    From: "SEGGER - Support embOS (TS)" <support_embos@segger.com>
    Subject: SEGGER embOS Support: Tech support question on using embOS in a bootloader


    Can I actually do what I want to do?
    Yes, of course.

    How would I essentially shut down
    and restart the OS as I go from the bootloader into the application code?
    There are some things which could cause your problem.
    Basically you should disable interrupts before jumping to your application.
    The bootloader has now to do everything which is normally done by the hardware.
    This includes setting the VTOR register, initializing the MSP stack pointer and jumping to the application.

    I guess you call the application from a task in the bootloader application?
    embOS tasks run on the PSP and not on the MSP, which means you have to switch to MSP before jumping to the application.
    If you don't switch to MSP the application runs before OS_Start() on the PSP which points to the task stack of the bootloader and could overwrite application RAM.
    You can switch to MSP and privileged mode with:
    asm(" mov r4, #0");
    asm(" msr CONTROL, r4");

    The problem I have is when I try to jump to the application at the completion of the boot load I get a hard fault.
    Where do you exactly get the hard fault?

    Best regards,

    SEGGER - Support embOS

    Solutions for real time microcontroller applications
    segger.com
    **********************************************************


    On 14-01-21 19:56, Dave wrote:
    Hello,

    I am trying to do something a bit weird and use embOS in a bootloader and
    then again in my application. I hard fault when I do the jump to the application.
    Can I actually do what I want to do? How would I essentially shut down
    and restart the OS as I go from the bootloader into the application code?

    Please see my post in your forum for more details:


    Using embOS in a bootloader and in an application. When jump to Application get a hard fault

    Thank you for your help,

    Dave