AnsweredAssumed Answered

After bootloading a program, how to automatically start it on reboot?

Question asked by Robert Poor on Sep 16, 2017
Latest reply on Sep 16, 2017 by Robert Poor

Summary:

Once I've downloaded a program using the flash bootloader, how do I arrange things so on the next reboot, it launches the program (rather than the bootloader)?

Details:

I've adapted a flash bootloader for the KL27Z.  As far as I can tell, it has successfully downloaded a small test program:

[~]$ blhost -p /dev/cu.usbmodem219,115200 flash-image blinky.hex 1
Ping responded in 1 attempt(s)
Inject command 'flash-image'
Successful generic response to command 'flash-erase-region'
Successful generic response to command 'flash-erase-region'
Wrote 192 bytes to address 0x8000
Successful generic response to command 'write-memory'
(1/2)100% Completed!
Successful generic response to command 'write-memory'
Wrote 7756 bytes to address 0x8400
Successful generic response to command 'write-memory'
(2/2)100% Completed!
Successful generic response to command 'write-memory'
Response status = 0 (0x0) Success.

Note that blinky.hex has slightly non-standard memory map: I've modified the MKL27Z64xxx4_flash.ld so the code starts at 0x8000 rather than the normal 0x0000 so it doesn't overwrite the resident flash loader:

/* Specify the memory areas */
MEMORY
{
  m_interrupts          (RX)  : ORIGIN = 0x00008000, LENGTH = 0x00000200
  m_flash_config        (RX)  : ORIGIN = 0x00008400, LENGTH = 0x00000010
  m_text                (RX)  : ORIGIN = 0x00008410, LENGTH = 0x0000FBF0
  m_usb_sram            (RW)  : ORIGIN = 0x040FE000, LENGTH = 0x00000200
  m_data                (RW)  : ORIGIN = 0x1FFFF000, LENGTH = 0x00004000
}

After loading the blinky.hex file, the flash boloader is still resident, and it gets started upon reboot.  It examines the valued stored for sp and pc to decide if there's a valid program to run:

    uint32_t *vectorTable = (uint32_t*)APPLICATION_BASE;
    uint32_t sp = vectorTable[0];
    uint32_t pc = vectorTable[1];

So I believe my real questions are:

  • How do I discover the proper values for sp and pc in order to launch blinky.hex?
  • How do I insert sp and pc into memory addresses APPLICATION_BASE[0] and APPLICATION_BASE[1] in the .hex file so they are properly set up after the .hex file is loaded?  Ideally this would be automatic, rather than a manual edit.

Or is this the wrong way to go about it?

Outcomes