hc32f4a0 bootloader跳转app后中断异常

发布时间 2023-10-24 10:24:20作者: USTHzhanglu

起因

从HC32F460上移植bootloader到HC32F4A0上后,应用程序无法正常使用。主要代码如下:

void execute_user_code(uint32_t appxaddr)
{
  uint32_t JumpAddress;
  sram_size = (*(uint32_t *)appxaddr) -0x1FFF8000;//different sram adress with other MCU
  if (sram_size <= 0x2F000)//栈地址合法
  {
    LogPrint("[run app]\n");
    __disable_irq();
    JumpAddress = *(__IO uint32_t *)(appxaddr + 4);
    jump2app = (iapfun)JumpAddress;
    __set_MSP(*(__IO uint32_t *)appxaddr);
    
    for(int Cnt = 0; Cnt < Int143_IRQn; Cnt++)
   {
     enIrqResign(Cnt);
    }//取消注册所有中断
    SCB->VTOR = ((uint32_t) APP_CODE_ADDR & SCB_VTOR_TBLOFF_Msk);//重设向量表
    __enable_irq();//使能中断
    jump2app();
  }
  else
  {
    LogPrint("[run ota]\n");
    is_upgrade_firmware = true;//需要升级
  }
}

定位到systick_handler中断始终无法执行。必须将重设向量表放到APP中才能正常启动。
SCB->VTOR = ((uint32_t) APP_CODE_ADDR & SCB_VTOR_TBLOFF_Msk);//重设向量表

原因

对比HC32460和HC32F4A0的启动代码后,发现以下区别:

HC32F460

void SystemInit(void)
{
#if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
    SCB->CPACR |= ((3UL << 20) | (3UL << 22)); /* set CP10 and CP11 Full Access */
#endif

    SystemCoreClockUpdate();
}

HC32F4A0

void SystemInit(void)
{
    /* FPU settings */
#if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
    SCB->CPACR |= ((3UL << 20) | (3UL << 22)); /* set CP10 and CP11 Full Access */
#endif
    SystemCoreClockUpdate();
#if defined (ROM_EXT_QSPI)
    SystemInit_QspiMem();
#endif /* ROM_EXT_QSPI */
    /* Configure the Vector Table relocation */
    SCB->VTOR = VECT_TAB_OFFSET;    /* Vector Table Relocation */
}

区别在于F4A0初始化时,手动设置了SCB-VTOR的值。