中级会员
- 积分
- 279
- 金钱
- 279
- 注册时间
- 2021-2-5
- 在线时间
- 133 小时
|
楼主 |
发表于 2023-9-11 16:56:05
|
显示全部楼层
我用的是BCC校验,升级前上位机会给一个校验结果存储器来,然后底层再校验得出一个校验结果,对比校验结果,不对就会调用备份程序,我把代码贴出来
HAL_StatusTypeDef UpgradeMain(uint32_t UpgradeSize,uint8_t Code)
{
uint8_t CheckCode = 0x00;
char DataBuff[32];
uint8_t ContinueBuff[20] = "continue";
uint32_t FlashFrameNum,FlashStarAddr;
uint16_t WriteLength;
uint8_t *FlashBuff = pvPortMalloc(FLASH_BUFF_LENGTH);
uint8_t Result = HAL_OK;
UpgradeTypdef Upgrade;
SemaphoreHandle_t UpgradexSemaphore;
UpgradexSemaphore = xSemaphoreCreateBinary();
xSemaphoreGive(UpgradexSemaphore);
memset(FlashBuff,0,FLASH_BUFF_LENGTH);
/* 计算更新帧数量 */
FlashFrameNum = UpgradeSize % FLASH_WRITE_LENGTH ? (UpgradeSize / FLASH_WRITE_LENGTH) + 1 : UpgradeSize / FLASH_WRITE_LENGTH ;
FlashStarAddr = APP_FLASHADDR_S;
Upgrade.FrameLen = FlashFrameNum;
Upgrade.Finish = 0;
/* 非常重要 */
memset(DataBuff,0,32);
sprintf(DataBuff,"0;1;0"); /* 置位升级完成标识,避免升级中断无法恢复 */
FlashErase(CORE_DATAADDR_S4,0x08061FFF);
FlashWrite(CORE_DATAADDR_S4,(uint8_t *)DataBuff,strlen((char *)DataBuff));
if(FlashErase(APP_FLASHADDR_S, APP_FLASHADDR_E) != HAL_OK)
Result = 1;
while(FlashFrameNum && !Result)
{
sprintf((char *)ContinueBuff,"continue:%0.0f%%*",((Upgrade.Finish)/(Upgrade.FrameLen))*100);
/* 升级末尾进度作100%处理 */
if(FlashFrameNum == 1)
{
sprintf((char *)ContinueBuff,"continue:%0.0f%%*",100.0f);
}
if(USART3_Transmit(ContinueBuff, sizeof(ContinueBuff), 500) != HAL_OK)
Result = 2;
xSemaphoreTake(UpgradexSemaphore,portMAX_DELAY);
/* 文件未传输提示错误码 */
memset(DataBuff,0,32);
sprintf(DataBuff,"0;1;30");
FlashErase(CORE_DATAADDR_S4,0x08061FFF);
FlashWrite(CORE_DATAADDR_S4,(uint8_t *)DataBuff,strlen((char *)DataBuff));
USART3_Receive(FlashBuff, portMAX_DELAY);
memset(DataBuff,0,32);
sprintf(DataBuff,"0;1;0");
FlashErase(CORE_DATAADDR_S4,0x08061FFF);
FlashWrite(CORE_DATAADDR_S4,(uint8_t *)DataBuff,strlen((char *)DataBuff));
if(FlashFrameNum > 1)
{
WriteLength = FLASH_WRITE_LENGTH;
if(FlashWrite(FlashStarAddr, FlashBuff, WriteLength) == HAL_OK)
FlashStarAddr += FLASH_WRITE_LENGTH;
else
Result = 4;
}
else
{
WriteLength = UpgradeSize % FLASH_WRITE_LENGTH ? UpgradeSize % FLASH_WRITE_LENGTH : FLASH_WRITE_LENGTH;
FlashWrite(FlashStarAddr, FlashBuff, WriteLength);
}
CheckCode = BccCheck(CheckCode, FlashBuff, WriteLength);
FlashFrameNum--;
Upgrade.Finish++;
xSemaphoreGive(UpgradexSemaphore);
memset(FlashBuff,0,FLASH_BUFF_LENGTH);
/* 规避传输过程中手机掉电 */
IWDG_FeedDog();
}
if(Code != CheckCode)
{
log_i("CheckError:%02X-%02X",Code,CheckCode);
Result = 6;
}
vPortFree(FlashBuff);
if(Result != 0)
{
log_i("Error Code(%d)",Result);
return HAL_ERROR;
}
return HAL_OK;
} |
|