新手入门
- 积分
- 8
- 金钱
- 8
- 注册时间
- 2024-8-27
- 在线时间
- 3 小时
|
8金钱
试了一下将主程序放在第一个flash中,采用U盘将程序(bin文件添加了crc,用U盘读出来的时候,进行了校验)读取进外部SRAM(0x60000000)中,再将程序写入FLASH2中,再进行FLASH SWAP,然后看门狗重启,结果升级后,程序无法启动,同时也无法进行仿真了,flash下载不了,到擦除的时候就出错了(timeout),跪求大佬能给分析一下!
flash的过程如下:
//获取程序内存缓存地址
Uint32 GetProgramCacheAddr(void)
{
return((Uint32)SampleCur); //返回数据存储的首地址0x60000000
}
HAL_StatusTypeDef Flash_Swap(void)
{
FLASH_OBProgramInitTypeDef OBInit;
HAL_StatusTypeDef status;
// 解锁 Flash
HAL_FLASH_Unlock();
HAL_FLASH_OB_Unlock();
// 获取当前活动 Bank
uint32_t current_bank = (FLASH->OPTSR_CUR & FLASH_OPTSR_SWAP_BANK_OPT) >> FLASH_OPTSR_SWAP_BANK_OPT_Pos;
// 配置选项字节
OBInit.OptionType = OPTIONBYTE_USER;
OBInit.USERType = OB_USER_SWAP_BANK;
OBInit.USERConfig = (current_bank == 1) ? OB_SWAP_BANK_DISABLE : OB_SWAP_BANK_ENABLE;
// 编程选项字节
status = HAL_FLASHEx_OBProgram(&OBInit);
if(status == HAL_OK) {
// 启动选项字节加载
status = HAL_FLASH_OB_Launch();
}
// 锁定 Flash
HAL_FLASH_OB_Lock();
HAL_FLASH_Lock();
return status;
}
Uint8 UpdateProgram(void)
{
uint32_t inactive_bank;
HAL_StatusTypeDef status;
Uint32 size;
Uint32 *pldata;
// 确定非活动 Bank
inactive_bank = (FLASH->OPTSR_CUR & FLASH_OPTSR_SWAP_BANK_OPT) ? FLASH_BANK1_BASE : FLASH_BANK2_BASE;
// 擦除非活动 Bank
FLASH_EraseInitTypeDef EraseInit;
EraseInit.TypeErase = FLASH_TYPEERASE_SECTORS;
EraseInit.Banks = (inactive_bank == FLASH_BANK1_BASE) ? FLASH_BANK_1 : FLASH_BANK_2;
EraseInit.Sector = 0; // 从0扇区开始
EraseInit.NbSectors = 3; // 擦除 Bank
uint32_t SectorError;
status = HAL_FLASHEx_Erase(&EraseInit, &SectorError);
if(status != HAL_OK)
{
SysData.Sys_RunFlag.bit.Needupgradef = 0;
return status;
}
size = USBData.ReadFileLenth+32;
pldata = (Uint32*)GetProgramCacheAddr();
HAL_FLASH_Unlock();
// 写入新固件
for(uint32_t i = 0; i < size; i += 32) {
status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_FLASHWORD,
inactive_bank + i,(Uint32)&pldata[i]);
if(status != HAL_OK)
{
RS485_char_to_reg(20,ProgUpDate_Text3,"升级失败,正在重启"); //程序更新提示
SysData.Sys_RunFlag.bit.Needupgradef = 0;
return status;
}
IWDG_Feed();
}
HAL_FLASH_Lock();
// 执行 Bank 交换
if(Flash_Swap()==HAL_OK)
{
while(1); //等待看门狗重启
}
else
{
SysData.Sys_RunFlag.bit.Needupgradef = 0;
__enable_irq();
return HAL_ERROR;
}
}
|
|