Jumping from one firmware to another in MCU internal FLASH

后端 未结 3 929
余生分开走
余生分开走 2021-01-03 09:32

I am currently working on a bootloader firmware application targeted to STM32F030C8. I specified in my scatter file that the bootloader app will occupy main memory location

3条回答
  •  囚心锁ツ
    2021-01-03 10:15

    The Hard Fault ISR indicated to me where the fault was. R0 had __initial_sp which is the top of the stack. It made me realise that I had to initialise my application's Stack pointer in the bootloader before jumping to the app. After doing that I can now successfully jump to the app from the bootloader. The jump address also has to be (for some reason I do not quite understand) the application address + 4.

    Also, note that you need to modify the main application RAM allocation to start from 0x200000C0 with size 0x00001FD0. This is done so you can relocate the vector table to the internal SRAM at 0x20000000 in the main application "main" function as follows:

    /* Private macro -------------------------------------------------------------*/
    __IO uint32_t VectorTable[48] __attribute__((at(0x20000000)));
    
    /************************************************************//**
    * \brief Main application application entry point
    *
    * At this stage the microcontroller clock setting is already configured, 
    * this is done through SystemInit() function which is called from startup
    * file (startup_stm32f030x8.s) before to branch to application main.
    * To reconfigure the default setting of SystemInit() function, refer to
    * startup_stm32f030x8.c file
    *****************************************************************/
    int main( void )
    {
        uint32_t i = 0;
        uint8_t status;
    
    /* Relocate by software the vector table to the internal SRAM at 0x20000000 ***/  
      /* Copy the vector table from the Flash (mapped at the base of the application
         load address 0x08003000) to the base address of the SRAM at 0x20000000. */
      for(i = 0; i < 48; i++)
      {
        VectorTable[i] = *(__IO uint32_t*)(IAP_BTLUPG_ADDRESS + (i<<2));
      }
    
        RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN;   /* Enable the SYSCFG peripheral clock*/
    
        SYSCFG->CFGR1 = SYSCFG_CFGR1_MEM_MODE;  /* Remap SRAM at 0x00000000 */
    
    /***************** Application code starts below ***************************/       
    }
    

    See my new bootloader INTFLASH_execute_main_app function below:

    void INTFLASH_execute_main_app(const char mode)
    {
      MyFunc_ptr Jump_To_Application;
      uint32_t JumpAddress;
    
    //  __disable_irq();
    
        JumpAddress = *(__IO uint32_t*) (IAP_APP_ADDRESS + 4);
      Jump_To_Application = (MyFunc_ptr) JumpAddress;
    
      if( mode || intflash_check_main_app() )
      {
        App_ptr = (uint8_t*)Jump_To_Application;
        if( (*App_ptr != 0xFF) &&  (App_ptr) )
        {                  
        __set_MSP( *(__IO uint32_t*)IAP_APP_ADDRESS );  // Initialise app's Stack Pointer
        Jump_To_Application();
        }
      }          
    //  __enable_irq();
    }
    

    Many Thanks

提交回复
热议问题