标签:com target 重写 blog app keil base starting 直接
int main(void)
{
HAL_Init();//STM32初始化
SystemClock_Config();//时钟配置
System_GPIOInit();//IO口配置
#ifdef BOOTLOAD_DISPLAY_ENABLE
SystemColorInit();//显示屏配置
#endif
System_LoadUpdateFile();//升级函数
while (1)
{
}
}
void System_LoadUpdateFile(void)
{
uint8_t res;
if(bNandFlash_Error)//如果NandFlash错误,串口打印错误信息,跳转到用户程序
{
d_printf("NandFlash_Error jump\n");
BootLoad_Jump();//跳转函数
return;
}
if(bNo_FileSystem)//如果没有文件系统,串口打印错误信息,跳转到用户程序
{
d_printf("no file system jump\n");
BootLoad_Jump();//跳转函数
return;
}
if(f_open(&File, (char *)UPDATE_FILE_PATH, FA_READ)==FR_OK)//如果存在升级文件,开始执行升级
{
d_printf("update\n");
if(BootLoad_Program())//是否写入成功
{
f_close(&File);//关闭升级文件
res=f_unlink((char *)UPDATE_FILE_PATH);//删除升级文件
d_printfhex(res);d_printf("\n");
res=f_unlink((char *)UPDATE_DIR_PATH);//删除升级目录
d_printfhex(res);d_printf("\n");
BootLoad_Jump();//跳转函数
}
else
{
HAL_FLASH_Lock();//锁定Flash
d_printf("update fail\n");
f_close(&File);//关闭升级文件
BootLoad_Jump();//跳转函数
}
}
else
{
d_printf("jump\n");
f_close(&File);
BootLoad_Jump();
}
}
uint8_t BootLoad_Program(void)
{
uint32_t BaseAddress=APPLICATION_ADDRESS;//APP地址
uint32_t i,br,datacnt=0;
uint8_t data8;
GlobalPtr32=(uint32_t *)BootBuff;
HAL_FLASH_Unlock();//解锁Flash
if(BootLoad_Erase()==false)//擦除Flash
{
return false;
}
d_printf("size:");d_printfhex32(File.fsize);d_printf("\n");
while(1)
{
f_read(&File,BootBuff,8192,(void *)&br);//读取升级文件
for (i=0;i<(br>>2);i++)
{
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, BaseAddress, GlobalPtr32[i]) == HAL_OK)//写入升级文件
{
BaseAddress = BaseAddress +4;
}
else
{
d_printf("program err\n");
return false;
}
}
datacnt+=br;
if(datacnt>=File.fsize)//写入完成
{
break;
}
}
d_printf("verify\n"); //验证Flash中的内容与升级文件是否一致
f_lseek(&File,0); //若一致代表升级成功
datacnt=0; //若不一致代表升级失败
BaseAddress=APPLICATION_ADDRESS;
while(1)
{
f_read(&File,BootBuff,8192,(void *)&br);
for (i=0;i<br;i++)
{
data8 = *(__IO uint8_t*)BaseAddress;
if (data8 != BootBuff[i])
{
d_printf("error!\n");
return false;
}
BaseAddress ++;
}
datacnt+=br;
if(datacnt>=File.fsize)
{
break;
}
}
HAL_FLASH_Lock();//锁定Flash
return true;
}
void BootLoad_Jump(void)
{
/* Check Vector Table: Test if user code is programmed starting from address
"APPLICATION_ADDRESS" */
d_printfhex32((*(__IO uint32_t*)APPLICATION_ADDRESS));d_printf("\n");
if (((*(__IO uint32_t*)APPLICATION_ADDRESS) & 0x2FFE0000 ) == 0x20000000)
{
JumpAddress = *(__IO uint32_t*) (APPLICATION_ADDRESS +4);
d_printfhex32(JumpAddress);d_printf("\n");
HAL_Delay(100);
Jump_To_Application = (pFunction) JumpAddress;
/* Initialize user application‘s Stack Pointer */
__set_MSP(*(__IO uint32_t*) APPLICATION_ADDRESS);
Jump_To_Application();
}
}
标签:com target 重写 blog app keil base starting 直接
原文地址:http://www.cnblogs.com/jack-hzm/p/7999634.html