/**************************************************************************** * * Copy right: Qx. * File name: Fota.c * Description: Fota升级函数 * History: 2021-03-15 * ****************************************************************************/ #include "Fota.h" #include "ec_tcpip_api.h" #include "ps_lib_api.h" #include "MainTask.h" #include "TcpTask.h" #include "os_exception.h" #include "flash_ec616_rt.h" extern AppNVMDataType AppNVMData; static Fota_Type Fota_S; static UINT8 bcc_chk_fota(UINT8* data, UINT8 length); static UINT8 Fota_crc_chk(UINT8* data,UINT8 length); volatile bool NB_Fota_update_flag = FALSE; //NB可以升级标志 volatile bool BMS_Fota_update_flag = FALSE; //NB可以升级标志 void Fota_Func(UINT8 *DataPtr,INT32 connectId) { UINT8 Fota_Answer[43]; UINT8 Fota_Cmd; INT8 ret; if(*(DataPtr+30)==0x01) { Fota_S.Fota_Flash_Addres = FLASH_FOTA_REGION_START; Fota_Cmd = *(DataPtr+31); Fota_Answer[0] = TCP_START_SYM1; Fota_Answer[1] = TCP_START_SYM2; Fota_Answer[2] = TCP_CONCMD_SYM; switch (Fota_Cmd) { case 0x01: { Fota_S.Fota_All_Data_Len = *(DataPtr+33)<<24|*(DataPtr+34)<<16|*(DataPtr+35)<<8|*(DataPtr+36); Fota_S.Fota_Current_Addres = *(DataPtr+37)<<24|*(DataPtr+38)<<16|*(DataPtr+39)<<8|*(DataPtr+40); if(Fota_S.Fota_All_Data_Len>=(FLASH_BMS_FOTA_START_ADDR - FLASH_FOTA_REGION_START)) { Fota_Answer[3] = 0x02; } else { Fota_Answer[3] = 0x01; } memcpy(&Fota_Answer[4],(DataPtr+4),BATT_SN_LEN); Fota_Answer[21] = TCP_ENCPT_DISABLE; Fota_Answer[22] = 0x00; Fota_Answer[23] = 0x12; memcpy(&Fota_Answer[24],(DataPtr+24),18); Fota_Answer[42] = bcc_chk_fota(Fota_Answer,42); tcpipConnectionSend(connectId,Fota_Answer,43,0,0,0); if(Fota_Answer[3] == 0x01) { BSP_QSPI_Erase_Safe(Fota_S.Fota_Flash_Addres,Fota_S.Fota_All_Data_Len + 4 - (Fota_S.Fota_All_Data_Len%4)); //512k-32k = 480k -> 0x75300 0x78000 } break; } case 0x02: { Fota_S.Fota_All_Data_Len = *(DataPtr+33)<<24|*(DataPtr+34)<<16|*(DataPtr+35)<<8|*(DataPtr+36); Fota_S.Fota_Current_Addres = *(DataPtr+37)<<24|*(DataPtr+38)<<16|*(DataPtr+39)<<8|*(DataPtr+40); Fota_S.Fota_Recv_Data_Len = *(DataPtr+41); memset(Fota_S.Fota_Recv_Data,0x00,100); memcpy(Fota_S.Fota_Recv_Data,(DataPtr+42),*(DataPtr+41)); Fota_S.Fota_CRC = Fota_crc_chk(Fota_S.Fota_Recv_Data,Fota_S.Fota_Recv_Data_Len); if(Fota_S.Fota_CRC == *(DataPtr+Fota_S.Fota_Recv_Data_Len+42)) { if(Fota_S.Fota_Recv_Data_Len%4!=0) { Fota_S.Fota_Recv_Data_Len = Fota_S.Fota_Recv_Data_Len + 4-(Fota_S.Fota_Recv_Data_Len%4); } ret = BSP_QSPI_Write_Safe(Fota_S.Fota_Recv_Data,Fota_S.Fota_Flash_Addres+Fota_S.Fota_Current_Addres,Fota_S.Fota_Recv_Data_Len); if(ret==QSPI_OK) { Fota_Answer[3] = 0x01; } else { Fota_Answer[3] = 0x02; } } else//数据校验失败 { Fota_Answer[3] = 0x02; } memcpy(&Fota_Answer[4],(DataPtr+4),BATT_SN_LEN); Fota_Answer[21] = TCP_ENCPT_DISABLE; Fota_Answer[22] = 0x00; Fota_Answer[23] = 0x12; memcpy(&Fota_Answer[24],(DataPtr+24),18); Fota_Answer[42] = bcc_chk_fota(Fota_Answer,42); tcpipConnectionSend(connectId,Fota_Answer,43,0,0,0); break; } case 0x03: { Fota_S.Fota_All_Data_Len = *(DataPtr+33)<<24|*(DataPtr+34)<<16|*(DataPtr+35)<<8|*(DataPtr+36); Fota_S.Fota_Current_Addres = *(DataPtr+37)<<24|*(DataPtr+38)<<16|*(DataPtr+39)<<8|*(DataPtr+40); Fota_Answer[3] = 0x01; memcpy(&Fota_Answer[4],(DataPtr+4),BATT_SN_LEN); Fota_Answer[21] = TCP_ENCPT_DISABLE; Fota_Answer[22] = 0x00; Fota_Answer[23] = 0x12; memcpy(&Fota_Answer[24],(DataPtr+24),18); Fota_Answer[42] = bcc_chk_fota(Fota_Answer,42); tcpipConnectionSend(connectId,Fota_Answer,43,0,0,0); if(Fota_S.Fota_All_Data_Len==Fota_S.Fota_Current_Addres) { NB_Fota_update_flag = TRUE; #ifdef USING_PRINTF printf("\n\n\n"); UINT8 temp[1]; for(int i=0;i=(FLASH_BMS_FOTA_END_ADDR - FLASH_BMS_FOTA_START_ADDR)) { Fota_Answer[3] = 0x02; } else { Fota_Answer[3] = 0x01; } memcpy(&Fota_Answer[4],(DataPtr+4),BATT_SN_LEN); Fota_Answer[21] = TCP_ENCPT_DISABLE; Fota_Answer[22] = 0x00; Fota_Answer[23] = 0x12; memcpy(&Fota_Answer[24],(DataPtr+24),18); Fota_Answer[42] = bcc_chk_fota(Fota_Answer,42); tcpipConnectionSend(connectId,Fota_Answer,43,0,0,0); if(Fota_Answer[3] == 0x01) { BSP_QSPI_Erase_Safe(Fota_S.Fota_Flash_Addres,Fota_S.Fota_All_Data_Len + 4 - (Fota_S.Fota_All_Data_Len%4)); //512k-32k = 480k -> 0x75300 0x78000 } break; } case 0x02: { Fota_S.Fota_All_Data_Len = *(DataPtr+33)<<24|*(DataPtr+34)<<16|*(DataPtr+35)<<8|*(DataPtr+36); Fota_S.Fota_Current_Addres = *(DataPtr+37)<<24|*(DataPtr+38)<<16|*(DataPtr+39)<<8|*(DataPtr+40); Fota_S.Fota_Recv_Data_Len = *(DataPtr+41); memset(Fota_S.Fota_Recv_Data,0x00,100); memcpy(Fota_S.Fota_Recv_Data,(DataPtr+42),*(DataPtr+41)); Fota_S.Fota_CRC = Fota_crc_chk(Fota_S.Fota_Recv_Data,Fota_S.Fota_Recv_Data_Len); if(Fota_S.Fota_CRC == *(DataPtr+Fota_S.Fota_Recv_Data_Len+42)) { if(Fota_S.Fota_Recv_Data_Len%4!=0) { Fota_S.Fota_Recv_Data_Len = Fota_S.Fota_Recv_Data_Len + 4-(Fota_S.Fota_Recv_Data_Len%4); } ret = BSP_QSPI_Write_Safe(Fota_S.Fota_Recv_Data,Fota_S.Fota_Flash_Addres+Fota_S.Fota_Current_Addres,Fota_S.Fota_Recv_Data_Len); if(ret==QSPI_OK) { Fota_Answer[3] = 0x01; } else { Fota_Answer[3] = 0x02; } } else//数据校验失败 { Fota_Answer[3] = 0x02; } memcpy(&Fota_Answer[4],(DataPtr+4),BATT_SN_LEN); Fota_Answer[21] = TCP_ENCPT_DISABLE; Fota_Answer[22] = 0x00; Fota_Answer[23] = 0x12; memcpy(&Fota_Answer[24],(DataPtr+24),18); Fota_Answer[42] = bcc_chk_fota(Fota_Answer,42); tcpipConnectionSend(connectId,Fota_Answer,43,0,0,0); break; } case 0x03: { Fota_S.Fota_All_Data_Len = *(DataPtr+33)<<24|*(DataPtr+34)<<16|*(DataPtr+35)<<8|*(DataPtr+36); Fota_S.Fota_Current_Addres = *(DataPtr+37)<<24|*(DataPtr+38)<<16|*(DataPtr+39)<<8|*(DataPtr+40); Fota_Answer[3] = 0x01; memcpy(&Fota_Answer[4],(DataPtr+4),BATT_SN_LEN); Fota_Answer[21] = TCP_ENCPT_DISABLE; Fota_Answer[22] = 0x00; Fota_Answer[23] = 0x12; memcpy(&Fota_Answer[24],(DataPtr+24),18); Fota_Answer[42] = bcc_chk_fota(Fota_Answer,42); tcpipConnectionSend(connectId,Fota_Answer,43,0,0,0); if(Fota_S.Fota_All_Data_Len==Fota_S.Fota_Current_Addres) { BMS_Fota_update_flag = TRUE; } else { BMS_Fota_update_flag = FALSE; } break; } default: break; } } } static UINT8 bcc_chk_fota(UINT8* data, UINT8 length) { UINT8 bcc_chk_return = 0x00; UINT8 count = 0; while (count