/**************************************************************************** * * 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 Fota_update_flag = FALSE; //可以升级标志 void Fota_Func(UINT8 *DataPtr,INT32 connectId,UINT8 FotaType) { UINT8 Fota_Answer[43]; UINT8 Fota_Cmd; INT8 ret; if(FotaType==0x01) { Fota_S.Fota_Flash_Addres = FLASH_FOTA_REGION_START; } else if(FotaType == 0x88) { Fota_S.Fota_Flash_Addres = FLASH_BMS_FOTA_START_ADDR; } if(*(DataPtr+30)==0x01) { 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(FotaType==0x01) { 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; } } if(FotaType==0x88) { if(Fota_S.Fota_All_Data_Len>=(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); #ifdef USING_PRINTF1 printf("\Data_Add:%x,Crc:%x,%x\n",Fota_S.Fota_Current_Addres,Fota_S.Fota_CRC,*(DataPtr+Fota_S.Fota_Recv_Data_Len+42)); UINT8 temp[5] = {0x01,0x02,0x03,0x04,0x05}; ret = BSP_QSPI_Write_Safe(temp,Fota_S.Fota_Flash_Addres+0xffe,5); UINT8 tPtr[1]; printf("test:\n"); for(UINT32 i = 0;i<5;i++) { BSP_QSPI_Read_Safe(tPtr,Fota_S.Fota_Flash_Addres+0xffe+i,1); printf("%x ",tPtr[0]); } printf("\ndata end\n"); #endif 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); } #ifdef USING_PRINTF1 printf("\nlen:%x,%xFota_Data_Recv :",Fota_S.Fota_Recv_Data_Len,Fota_S.Fota_Current_Addres); for(UINT32 i = 0;i