C文件
void JumpToApplication(void)
{
typedef void (*pFunction)(void);
pFunction Jump_To_Application;
u32 JumpAddress;
/* Test if user code is programmed starting from address "ApplicationAddress" */
if (((*(__IO uint32_t*)APP_BASEADDR) & 0x2FFE0000 ) == 0x20000000)
{
DBG_LOG("----[Boot]Jump to Application!!!\r\n");
LED_ALL_OFF();
__disable_irq();
/* Jump to user application */
JumpAddress = *(__IO uint32_t*) (APP_BASEADDR + 4);
Jump_To_Application = (pFunction) JumpAddress;
/* Initialize user application's Stack Pointer */
__set_MSP(*(__IO uint32_t*) APP_BASEADDR);
Jump_To_Application();
}
else
{
DBG_LOG("----[Boot]Application Address is ERROR!!!\r\n");
LED_ALL_ON();
}
}
union {
u32 stm32[SPIFLASHBUF_MAX_LEN/4];
u8 spi[SPIFLASHBUF_MAX_LEN];
}Flash;
void Boot_Initializes(void)
{
u16 i,j;
u32 update_file_mark;
u32 writeFlashData;
HAL_StatusTypeDef status;
FLASH_EraseInitTypeDef f;
f.TypeErase = FLASH_TYPEERASE_PAGES;
f.PageAddress = APP_BASEADDR;
f.NbPages = APP_PAGE_NUM;
//1.判断spi flash DFU区是否有文件需要升级
SFlash_Read(SECTOR_ADDR(SECTOR_DFU_START),(u8 *)&update_file_mark,4);
if(update_file_mark == 0xFFFFFFFF){
DBG_LOG("[Boot] No Firmware Need to Update.");
LED_ON(ERR);
JumpToApplication();
return;
}
//有文件需要升级
DBG_LOG("[Boot] New Firmware Exist! Update Beginning...");
//2.Flash解锁
status = HAL_FLASH_Unlock();
if(status != HAL_OK){
DBG_LOG("[Boot] FLASH Unlock Failed.");
LED_ON(ERR);
return;
}
DBG_LOG("[Boot] FLASH Unlock Ok.");
DBG_LOG("[Boot] Ready to Erase Flash ...");
//3.擦除Flash
status = HAL_FLASHEx_Erase(&f,&writeFlashData);
if(status != HAL_OK){
DBG_LOG("[Boot] Flash Erase Failed.");
LED_ON(ERR);
return;
}
DBG_LOG("[Boot] Flash Erase OK.");
//4.从SPIFlash中读出数据然后copy到内部Flash中
for(i = 0;i < (APP_PAGE_NUM / 2);i++){
SFlash_Read(SECTOR_ADDR(SECTOR_DFU_START) + SPIFLASHBUF_MAX_LEN*i, Flash.spi, SPIFLASHBUF_MAX_LEN);
DBG_LOG("[Boot] Write Flash Progress:%d%%",(i*100/(APP_PAGE_NUM / 2)) );
LED_TOGGLE(WIFI);
for(j = 0;j < (SPIFLASHBUF_MAX_LEN/4);j++){
//注意基地址是在变化的
HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, APP_BASEADDR+SPIFLASHBUF_MAX_LEN*i+4*j, Flash.stm32[j]);
}
}
DBG_LOG("[Boot] Write Flash Progress:100%%");
DBG_LOG("[Boot] Program Complete!!!");
//5.Flash上锁
HAL_FLASH_Lock();
//6.跳转到用户程序区
JumpToApplication();
}
|