AnsweredAssumed Answered

basic bootloader for M0

Question asked by carter.bob on Jun 20, 2016
Latest reply on Aug 15, 2016 by carter.bob
Hello Forum,

I've been reading on the forums about custom bootloaders stored in flash and I was wondering if there was another possibility (possibly because I want to avoid messing with stack pointers, reset vectors etc.)

In my main app, I have a bootloader function, that copies the new app stored in external flash to processor flash. This bootloader function contains a number of inline functions to read from external flash and copy to processor flash. Checking the .map file, this bootloader function looks to be all located in one place.

My thought process is to copy this bootloader function into RAM at say 0x20000100 and then jump to that address as shown below



void CopyLoaderToRam(void)
{
 //   uint32_t *Ram = (uint32_t *)RAM_ADDR;
    char *Ram = (char *)RAM_ADDR;///////////// Byte Alignment test
    typedef  void (*pFunction)(void);
    pFunction RunCode;

 ptr_bootloader_fn = &bootloader;    // Find the address of the bootloader function.  
   
//    memcpy(Ram, (void *)ptr_bootloader_fn, LOADER_FLASH_SIZE); // Copy bootloader into ram
   
    
      char *boot_byte;
      boot_byte = (char *)&bootloader;
      int i;
      for (i=0; i<= 4*LOADER_FLASH_SIZE; i++)
          {
                Ram[i] = *(boot_byte+i);    
                }
             
    RunCode = (pFunction)Ram[1];       // ResetHandler (PC in second vector)
     
    RunCode();
}

Well the first issue is it doesnt seem to work as I get a hard fault.My fears are that I am overwriting some necessary data in Ram or some alignment issue. I'm not sure. 

However, is it even technically feasible in the first place and if not, why? If it is a non-starter, I would rather know at this stage. Device is a STM32F051.

Kind Regards
Bob

Outcomes