This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TM4C123 bootloader - jump to desired flash location and run app

Other Parts Discussed in Thread: TM4C123GH6PM

Hi,

I am using below code to jump to application, but it throws some errors. Please help in resolving these errors.

void
CallApplication(uint_fast32_t ui32StartAddr)
{
//
// Set the vector table to the beginning of the app in flash.
//
HWREG(NVIC_VTABLE) = ui32StartAddr;

//
// Load the stack pointer from the application's vector table.
//
__asm("ldr r1, [r0]\n"
"mov sp, r1\n");

//
// Load the initial PC from the application's vector table and branch to
// the application's entry point.
//
__asm(" ldr r0, [r0, #4]\n"
" bx r0\n");
}

ERRORs:

error: #549: variable "R0" is used before its value is set
error: #20: identifier "sp" is undefined
error: #1084: This instruction not permitted in inline assembler

  • Would it not ease your quest to employ vendor's bootloader examples (coded in C) and to observe the ASM code which results? And as a successful Bootloader example EXISTS - why would you invest time/effort to re-invent a (proven) wheel?

    Forcing those here AWAY from vendor's API - or "normal C" - places extra strain upon vendor's (rare) and most valuable resource.

    Beyond the above - you've provided no indication of your IDE, nor compiler - such (often) proves useful.
  • Hello Dileep,

    Add a space between the starting quote and the ASM instruction as shown below

    __asm(" ldr r1, [r0]\n"
    " mov sp, r1\n");

    Regards
    Amit
  • Hi Amit,

    Below is a piece of code which i took it from one of bootloader example and calling it once after writing bin file to flash. But still the application is not called. Please let me know where i am going wrong.

    #define _APP_START_ADDRESS 0x2800
    #define _VTABLE_START_ADDRESS 0x2800

    __asm uint32_t
    callApp(void)
    {
    //EnterApplication

    mov lr, r9

    /* ;
    ; Copy the application's vector table to the target address if necessary.
    ; Note that incorrect boot loader configuration could cause this to
    ; corrupt the code! Setting VTABLE_START_ADDRESS to 0x20000000 (the start
    ; of SRAM) is safe since this will use the same memory that the boot loader
    ; already uses for its vector table. Great care will have to be taken if
    ; other addresses are to be used.
    ;*/
    if (_APP_START_ADDRESS != _VTABLE_START_ADDRESS)
    movw r0, #(_VTABLE_START_ADDRESS & 0xffff)
    if (_VTABLE_START_ADDRESS > 0xffff)
    movt r0, #(_VTABLE_START_ADDRESS >> 16)
    endif
    movw r1, #(_APP_START_ADDRESS & 0xffff)
    if (_APP_START_ADDRESS > 0xffff)
    movt r1, #(_APP_START_ADDRESS >> 16)
    endif

    /* ;
    ; Calculate the end address of the vector table assuming that it has the
    ; maximum possible number of vectors. We don't know how many the app has
    ; populated so this is the safest approach though it may copy some non
    ; vector data if the app table is smaller than the maximum.
    ;*/
    movw r2, #(70 * 4)
    adds r2, r2, r0
    VectorCopyLoop2
    ldr r3, [r1], #4
    str r3, [r0], #4
    cmp r0, r2
    blt VectorCopyLoop2
    endif

    /* ;
    ; Set the application's vector table start address. Typically this is the
    ; application start address but in some cases an application may relocate
    ; this so we can't assume that these two addresses are equal.
    ;*/
    movw r0, #(_VTABLE_START_ADDRESS & 0xffff)
    if (_VTABLE_START_ADDRESS > 0xffff)
    movt r0, #(_VTABLE_START_ADDRESS >> 16)
    endif
    movw r1, #(NVIC_VTABLE & 0xffff)
    movt r1, #(NVIC_VTABLE >> 16)
    str r0, [r1]

    /* ;
    ; Remove the NMI stack frame from the boot loader's stack.
    ;*/
    ldmia sp, {r4-r11}

    /* ;
    ; Get the application's stack pointer.
    ;*/
    if (_APP_START_ADDRESS != _VTABLE_START_ADDRESS)
    movw r0, #(_APP_START_ADDRESS & 0xffff)
    if (_APP_START_ADDRESS > 0xffff)
    movt r0, #(_APP_START_ADDRESS >> 16)
    endif
    endif
    ldr sp, [r0, #0x00]

    /* ;
    ; Fix up the NMI stack frame's return address to be the reset handler of
    ; the application.
    ;*/
    ldr r10, [r0, #0x04]
    bic r10, #0x00000001

    /* ;
    ; Store the NMI stack frame onto the application's stack.
    ;*/
    stmdb sp!, {r4-r11}

    /* ;
    ; Branch to the application's NMI handler.
    ;*/
    ldr r0, [r0, #0x08]
    bx r0

    }

  • Hello Dileep

    Which example are you referring to?

    Regards
    Amit
  • Hi Amit,

    My previous issue got solved, I had to disable the systick before jumping to application.

    New issue:
    Controller : TM4C123GH6PM
    Application StartAddress : 0x2800

    If Application size is 52004 bytes, bootloader writes to Flash but fails to start the application.
    I have reduced application size to 51864 bytes, bootloader writes to flash and start the application also.

    Don't know why the behavior is like this.

    Regards,
    Dileep
  • Hello Dileep,

    That should not be the case. If the application+bootloader size is within 256K and the application is placed in the flash such that Total Flash Size - Boot Loader size > Application Size, then it should be able to flash and execute.

    Regards
    Amit