123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- /****************************************************************************
- *
- * 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<Fota_S.Fota_All_Data_Len;i++)
- {
- BSP_QSPI_Read_Safe(temp,FLASH_FOTA_REGION_START+i,1);
- printf("%x ",temp[0]);
- }
- printf("\nend data \n\n\n");
- #endif
- }
- else
- {
- NB_Fota_update_flag = FALSE;
- }
- break;
- }
- default:
- break;
- }
- }
- else if(*(DataPtr+30)==0x88)//BMS升级文件存放
- {
- Fota_S.Fota_Flash_Addres = FLASH_BMS_FOTA_START_ADDR;
- 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_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<length)
- {
- bcc_chk_return^=data[count];
- count++;
- }
- return bcc_chk_return;
- }
- static UINT8 Fota_crc_chk(UINT8* data,UINT8 length)
- {
- UINT8 reg_crc=0x00;
- while(length--)
- {
- reg_crc ^= *data++;
- }
- return reg_crc;
- }
|