|
@@ -42,6 +42,9 @@ extern AppDataBody AppDataInfo;
|
|
|
BOOL UartBattInfoRecvFlag = false;
|
|
|
QueueHandle_t UartWriteCmdHandle = NULL;
|
|
|
UINT8 BattChrgEndFlag;
|
|
|
+
|
|
|
+UINT8 Lockstatus;
|
|
|
+
|
|
|
//
|
|
|
|
|
|
extern ARM_DRIVER_USART Driver_USART1;
|
|
@@ -78,6 +81,9 @@ UINT8 SP_BMS_Update_CheckSUM(UINT8* pSendData,UINT8 len);
|
|
|
void SP_BMS_Update_Service();
|
|
|
UINT8 SP_BMS_Update_Query(UINT8* pSend,UINT32 sendLen, UINT8* pRead, UINT32 readLen, UINT32 timeout);
|
|
|
|
|
|
+updateBMSStatus MS_BMS_Update_Service();
|
|
|
+UINT16 MS_BMS_Update_CRC16(UINT8* pSendData,UINT16 len);
|
|
|
+UINT8 MS_BMS_Update_Query(UINT8* pSend,UINT32 sendLen, UINT8* pRead, UINT32 readLen, UINT32 timeout);
|
|
|
//Uart线程任务区
|
|
|
static void UartTask(void* arg)
|
|
|
{
|
|
@@ -128,7 +134,7 @@ static void UartTask(void* arg)
|
|
|
}
|
|
|
else if(Timer_count%10==0)
|
|
|
{
|
|
|
- #ifdef USING_PRINTF
|
|
|
+ #ifdef USING_PRINTF1
|
|
|
printf("[%d]Uart Timer 5s:%d,Header:%x\n",__LINE__,Timer_count,UartReadMsg.Header[2]);
|
|
|
#endif
|
|
|
if(osMessageQueueGet(UartWriteCmdHandle,&UartWriteData,0,0)==osOK)
|
|
@@ -169,6 +175,16 @@ static void UartTask(void* arg)
|
|
|
// }
|
|
|
}
|
|
|
currentTimerCount = Timer_count;
|
|
|
+
|
|
|
+ //------------
|
|
|
+ if(Lockstatus)
|
|
|
+ {
|
|
|
+ BMS_Fota_update_flag = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ //-----------
|
|
|
+
|
|
|
+
|
|
|
if(BMS_Fota_update_flag)
|
|
|
{
|
|
|
PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_UPDATE);
|
|
@@ -241,7 +257,14 @@ static void UartTask(void* arg)
|
|
|
break;
|
|
|
}
|
|
|
case PROCESS_UART_STATE_UPDATE:
|
|
|
- SP_BMS_Update_Service();
|
|
|
+ #if BMS_MANUFACTURE==1
|
|
|
+ {
|
|
|
+ SP_BMS_Update_Service();
|
|
|
+ }
|
|
|
+ #elif BMS_MANUFACTURE==2
|
|
|
+ MS_BMS_Update_Service();
|
|
|
+ #endif
|
|
|
+
|
|
|
PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_IDLE);
|
|
|
BMS_Fota_update_flag = FALSE;
|
|
|
break;
|
|
@@ -285,9 +308,6 @@ static BOOL uartBattInfoDecode(UINT8* dataPtr)
|
|
|
packTemp = dataPtr[(0x06+BATT_CELL_VOL_NUM+BATT_TEMP_NUM+1)*2+1];
|
|
|
|
|
|
Batt_current = (dataPtr[(0x02+BATT_CELL_VOL_NUM)*2]<<8)|(dataPtr[(0x02+BATT_CELL_VOL_NUM)*2+1]);
|
|
|
- #ifdef USING_PRINTF
|
|
|
- printf("Batt_current = %x\n",Batt_current);
|
|
|
- #endif
|
|
|
//原始数据:充电为负,放电为正
|
|
|
if(battWorkState==0x02&&Batt_current>0x8000)
|
|
|
{
|
|
@@ -311,9 +331,6 @@ static BOOL uartBattInfoDecode(UINT8* dataPtr)
|
|
|
//Batt_current = Batt_current;
|
|
|
}
|
|
|
battI = Batt_current;
|
|
|
- #ifdef USING_PRINTF
|
|
|
- printf("battI = %x\n",battI);
|
|
|
- #endif
|
|
|
//bit0 ~ bit31 represent cell0 ~ cell31
|
|
|
battBalanceoInfo = dataPtr[(0x06+BATT_CELL_VOL_NUM+TEMP_NUM)*2+1] | (dataPtr[(0x06+BATT_CELL_VOL_NUM+TEMP_NUM)*2] <<8) + (dataPtr[(0x07+BATT_CELL_VOL_NUM+TEMP_NUM)*2+1]<<16) | (dataPtr[(0x07+BATT_CELL_VOL_NUM+TEMP_NUM)*2] <<24);
|
|
|
|
|
@@ -447,7 +464,7 @@ UINT8 Uart_WriteCmd_func(Uart_Write_Data_Type UartWriteData)
|
|
|
}
|
|
|
}
|
|
|
USARTdrv->Send((UINT8 *)&Uart_Write_Msg,sizeof(Uart_Write_Msg));
|
|
|
- #ifdef USING_PRINTF
|
|
|
+ #ifdef USING_PRINTF1
|
|
|
printf("Uart_Send_buffer: ");
|
|
|
for(int i=0;i<sizeof(Uart_Write_Msg);i++)
|
|
|
{
|
|
@@ -505,7 +522,7 @@ UINT8 Uart_DataRecv_func(UINT8* Uart_Read_Msg,UINT8* Uart_Recv_Buffer)
|
|
|
*(Uart_Read_Msg+6) = CRC_chk_buffer;
|
|
|
*(Uart_Read_Msg+7) = CRC_chk_buffer>>8;
|
|
|
USARTdrv->Send(Uart_Read_Msg,8);
|
|
|
- #ifdef USING_PRINTF
|
|
|
+ #ifdef USING_PRINTF1
|
|
|
printf("Uart_Send_buffer: ");
|
|
|
for(int i=0;i<8;i++)
|
|
|
{
|
|
@@ -1948,8 +1965,6 @@ UINT8 SP_BMS_Update_Query(UINT8* pSend,UINT32 sendLen, UINT8* pRead, UINT32 read
|
|
|
return 1;
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
UINT8 SP_BMS_Update_CheckSUM(UINT8* pSendData,UINT8 len)
|
|
|
{
|
|
|
UINT8 ret = 0;
|
|
@@ -1959,4 +1974,744 @@ UINT8 SP_BMS_Update_CheckSUM(UINT8* pSendData,UINT8 len)
|
|
|
ret +=*(pSendData+i);
|
|
|
}
|
|
|
return ret&0xFF;
|
|
|
+}
|
|
|
+//________________________________________________________________________________
|
|
|
+updateBMSStatus MS_BMS_Update_Service() //美顺BMS升级服务
|
|
|
+{
|
|
|
+ #ifdef USING_PRINTF
|
|
|
+ UINT8 ii = 0;
|
|
|
+ #endif
|
|
|
+
|
|
|
+ UINT8 errorCount = 0;
|
|
|
+ UINT16 currentPackage = 0;
|
|
|
+ UINT32 updateDataTotalByteLen = 0;
|
|
|
+ UINT16 updateDataPackageCount = 0;
|
|
|
+ UINT8 ReadNVMTemp[64];
|
|
|
+ UpdateStep_MS_BMS updateStep = MS_UPDATE_STEP_SEND_FIRMWARE_UPDATE_REQUEST_AND_JUMP_TO_BOOTLOADER;
|
|
|
+
|
|
|
+
|
|
|
+ UINT16 i,j=0;
|
|
|
+ UINT8 dataLen = 0;
|
|
|
+ UINT8 ret0 = 0;
|
|
|
+ updateBMSStatus ret = updateFailed;
|
|
|
+
|
|
|
+ UINT8 pUpdateMsgSend[80];
|
|
|
+ UINT32 updateMsgSendLen = 0;
|
|
|
+ UINT32 updateMsgReadLen = 0;
|
|
|
+
|
|
|
+ BOOL bmsUpdateFlag = TRUE;
|
|
|
+ UINT8 bmsAnswerMsg[8];
|
|
|
+
|
|
|
+ //static UpdateStep step = UPDATE_STEP_CHECK_VERSION;
|
|
|
+ UINT8 Cycle_conut = 0;
|
|
|
+ UINT16 CRCtemp = 0;
|
|
|
+ UINT8 headerLen = 5;
|
|
|
+
|
|
|
+ UINT8 checkSum = 0x00;
|
|
|
+ UINT8 checkSumCal = 0x00;
|
|
|
+ UINT8 tempLen = 0x00;
|
|
|
+
|
|
|
+ BSP_QSPI_Read_Safe(&checkSum,FLASH_BMS_FOTA_START_ADDR,1);
|
|
|
+
|
|
|
+ memset(ReadNVMTemp, 0, 64);
|
|
|
+ BSP_QSPI_Read_Safe(ReadNVMTemp, FLASH_BMS_FOTA_START_ADDR+1, 4); //data
|
|
|
+
|
|
|
+ updateDataTotalByteLen = ((ReadNVMTemp[0]<<24)&0xFF000000) | ((ReadNVMTemp[1]<<16)&0xFF0000) | ((ReadNVMTemp[2]<<8)&0xFF00) | (ReadNVMTemp[3]&0xFF) ;
|
|
|
+ updateDataPackageCount = (updateDataTotalByteLen+(64-1))/64; //进一法 e = (a+(b-1))/b
|
|
|
+
|
|
|
+ for(i=0; i<((updateDataTotalByteLen+4)+(64-1))/64;i++)
|
|
|
+ {
|
|
|
+ memset(ReadNVMTemp, 0, 64);
|
|
|
+ if((i+1)*64 < (updateDataTotalByteLen+4))
|
|
|
+ {
|
|
|
+ tempLen = 64;
|
|
|
+ BSP_QSPI_Read_Safe(ReadNVMTemp,FLASH_BMS_FOTA_START_ADDR+1+i*64,64);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ tempLen = (updateDataTotalByteLen+4) - i*64;
|
|
|
+ BSP_QSPI_Read_Safe(ReadNVMTemp,FLASH_BMS_FOTA_START_ADDR+1+i*64,tempLen);
|
|
|
+ }
|
|
|
+
|
|
|
+ for(j = 0; j< tempLen; j++)
|
|
|
+ {
|
|
|
+ checkSumCal = (checkSumCal + ReadNVMTemp[j]) & 0xFF;
|
|
|
+ }
|
|
|
+ //osDelay(10);
|
|
|
+ }
|
|
|
+ if(checkSum != checkSumCal)
|
|
|
+ {
|
|
|
+ #ifdef USING_PRINTF
|
|
|
+ printf("checksum error: checksum = %x, checksumCal = %x\n",checkSum,checkSumCal);
|
|
|
+ #endif
|
|
|
+ ret = updateErrorCheckSumError;
|
|
|
+ return ret;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ #ifdef USING_PRINTF
|
|
|
+ printf("checksum OK: checksum = %x, checksumCal = %x\n",checkSum,checkSumCal);
|
|
|
+ #endif
|
|
|
+ }
|
|
|
+
|
|
|
+ #ifdef USING_PRINTF
|
|
|
+ printf(" bmsUpdateFlag = %x, Cycle_conut = %x\n",bmsUpdateFlag,Cycle_conut);
|
|
|
+ #endif
|
|
|
+ while(bmsUpdateFlag && Cycle_conut<2)
|
|
|
+ {
|
|
|
+ #ifdef USING_PRINTF
|
|
|
+ printf("update ms bms step %d\n:",updateStep);
|
|
|
+ #endif
|
|
|
+ switch (updateStep)
|
|
|
+ {
|
|
|
+
|
|
|
+ case MS_UPDATE_STEP_SEND_FIRMWARE_UPDATE_REQUEST_AND_JUMP_TO_BOOTLOADER: //0x01
|
|
|
+ dataLen = 0x00;
|
|
|
+ updateMsgSendLen = 6+dataLen;
|
|
|
+ updateMsgReadLen = 8;
|
|
|
+
|
|
|
+ pUpdateMsgSend[0] = 0x01; //node byte
|
|
|
+ pUpdateMsgSend[1] = 0x40; //func byte
|
|
|
+ pUpdateMsgSend[2] = updateStep; //cmd byte
|
|
|
+ pUpdateMsgSend[3] = dataLen; //data len
|
|
|
+ //no data type
|
|
|
+ CRCtemp = MS_BMS_Update_CRC16(pUpdateMsgSend, 4);
|
|
|
+ pUpdateMsgSend[4] = (CRCtemp>>8)&0xFF; // CRC High
|
|
|
+ pUpdateMsgSend[5] = CRCtemp&0xFF; //CRC Low
|
|
|
+
|
|
|
+ memset((UINT8*)(bmsAnswerMsg) , 0, 8);
|
|
|
+ ret0 = MS_BMS_Update_Query(pUpdateMsgSend, updateMsgSendLen, (UINT8*)(bmsAnswerMsg), updateMsgReadLen, 500);
|
|
|
+ #ifdef USING_PRINTF
|
|
|
+ printf("update step 1 answer,updateMsgReadLen = %x:\n",updateMsgReadLen);
|
|
|
+
|
|
|
+ for(ii=0;ii<updateMsgReadLen;ii++)
|
|
|
+ printf("%x ",bmsAnswerMsg[ii]);
|
|
|
+
|
|
|
+ printf("\nret0 = %d",ret0);
|
|
|
+ printf("\n");
|
|
|
+ #endif
|
|
|
+ if(ret0!=0)
|
|
|
+ {
|
|
|
+ if(bmsAnswerMsg[0] == 0x01 && bmsAnswerMsg[1] == 0x40) // node and func byte
|
|
|
+ {
|
|
|
+ if(bmsAnswerMsg[2] == MS_UPDATE_STEP_FIRMWARE_UPDATE_REQUEST_ANSWER && bmsAnswerMsg[3] == 0x02) //answer cmd byte:0x02, answer data len:0x02
|
|
|
+ {
|
|
|
+ if(bmsAnswerMsg[4] == 0x00) //answer data byte1
|
|
|
+ {
|
|
|
+ if(bmsAnswerMsg[5] == 0x00) //answer data byte2
|
|
|
+ {
|
|
|
+ updateStep = MS_UPDATE_STEP_SEND_FIRMWARE_INFO;
|
|
|
+ errorCount = 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else if(bmsAnswerMsg[4] == 0x01) //不允许升级
|
|
|
+ {
|
|
|
+ if(bmsAnswerMsg[5] == 0x01)//电量过低
|
|
|
+ {
|
|
|
+ updateStep = MS_UPDATE_STEP_ERROR;
|
|
|
+ ret = updateErrorBMSPowerLow;
|
|
|
+ }
|
|
|
+ else if(bmsAnswerMsg[5] == 0x02)//电池存在保护状态不允许升级
|
|
|
+ {
|
|
|
+ updateStep = MS_UPDATE_STEP_ERROR;
|
|
|
+ ret = updateErrorBMSWarningProtect;
|
|
|
+ }
|
|
|
+ else if(bmsAnswerMsg[5] == 0x03) //不支持升级
|
|
|
+ {
|
|
|
+ updateStep = MS_UPDATE_STEP_ERROR;
|
|
|
+ ret = updateErrorBMSNotSurport;
|
|
|
+ }
|
|
|
+ else if(bmsAnswerMsg[5] == 0x04) //当前电池处于充放电状态
|
|
|
+ {
|
|
|
+ updateStep = MS_UPDATE_STEP_ERROR;
|
|
|
+ ret = updateErrorBMSWorkState;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ errorCount++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ errorCount++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ errorCount++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ errorCount++;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(errorCount>10)
|
|
|
+ {
|
|
|
+ updateStep = MS_UPDATE_STEP_ERROR;
|
|
|
+ errorCount = 0;
|
|
|
+ }
|
|
|
+ osDelay(50);
|
|
|
+ printf(" step 1 ret = %d\n",ret);
|
|
|
+ break;
|
|
|
+ case MS_UPDATE_STEP_SEND_FIRMWARE_INFO: //0x03
|
|
|
+ dataLen = 52;
|
|
|
+ updateMsgSendLen = 6+dataLen;
|
|
|
+ updateMsgReadLen = 7;
|
|
|
+ pUpdateMsgSend[0] = 0x01; //node byte
|
|
|
+ pUpdateMsgSend[1] = 0x40; //func byte
|
|
|
+ pUpdateMsgSend[2] = updateStep; //cmd byte
|
|
|
+ pUpdateMsgSend[3] = dataLen; //data len
|
|
|
+
|
|
|
+ memset(ReadNVMTemp, 0, 64);
|
|
|
+ BSP_QSPI_Read_Safe(ReadNVMTemp, FLASH_BMS_FOTA_START_ADDR+headerLen, 16); //data
|
|
|
+ MEMCPY(&pUpdateMsgSend[4], ReadNVMTemp, 16); //厂家信息,未开启校验
|
|
|
+ MEMCPY(&pUpdateMsgSend[4+16], ReadNVMTemp, 16); //保护板硬件序列号,未开启校验
|
|
|
+ pUpdateMsgSend[4+16*2 + 0] = (updateDataTotalByteLen>>24)&0xFF; //固件包大小
|
|
|
+ pUpdateMsgSend[4+16*2 + 1] = (updateDataTotalByteLen>>16)&0xFF;
|
|
|
+ pUpdateMsgSend[4+16*2 + 2] = (updateDataTotalByteLen>>8)&0xFF;
|
|
|
+ pUpdateMsgSend[4+16*2 + 3] = (updateDataTotalByteLen)&0xFF;
|
|
|
+ MEMCPY(&pUpdateMsgSend[4+16*2+4], ReadNVMTemp, 16); // 固件包头信息,未开启校验
|
|
|
+
|
|
|
+ CRCtemp = MS_BMS_Update_CRC16(pUpdateMsgSend, 4+dataLen);
|
|
|
+
|
|
|
+ pUpdateMsgSend[4+dataLen] = (CRCtemp>>8)&0xFF; // CRC High
|
|
|
+ pUpdateMsgSend[5+dataLen] = CRCtemp&0xFF; //CRC Low
|
|
|
+
|
|
|
+ memset((UINT8*)(bmsAnswerMsg) , 0, 8);
|
|
|
+ ret0 = MS_BMS_Update_Query(pUpdateMsgSend, updateMsgSendLen, (UINT8*)(bmsAnswerMsg), updateMsgReadLen, 500);
|
|
|
+ #ifdef USING_PRINTF
|
|
|
+ printf("update step 3 answer:\n");
|
|
|
+ for(ii=0;ii<updateMsgReadLen;ii++)
|
|
|
+ printf("%x ",bmsAnswerMsg[ii]);
|
|
|
+
|
|
|
+ printf("\nret0 = %d",ret0);
|
|
|
+ printf("\n");
|
|
|
+ #endif
|
|
|
+ if(ret0!=0)
|
|
|
+ {
|
|
|
+ if(bmsAnswerMsg[0] == 0x01 && bmsAnswerMsg[1] == 0x40) // node and func byte
|
|
|
+ {
|
|
|
+ if(bmsAnswerMsg[2] == MS_UPDATE_STEP_FIRMWARE_INFO_CHECK_AND_UPDATE_REQEST_ANSWER && bmsAnswerMsg[3] == 0x01) //answer cmd byte:0x04, answer data len:0x01
|
|
|
+ {
|
|
|
+ if(bmsAnswerMsg[4] == 0x00) //answer data byte1
|
|
|
+ {
|
|
|
+ updateStep = MS_UPDATE_STEP_EREASE_APP_FLASH_REQUEST;
|
|
|
+ errorCount = 0;
|
|
|
+ }
|
|
|
+ else if(bmsAnswerMsg[4] == 0x01) //厂家信息错误
|
|
|
+ {
|
|
|
+ errorCount++;
|
|
|
+ ret = updateErrorFirmwareInfoError;
|
|
|
+ }
|
|
|
+ else if(bmsAnswerMsg[4] == 0x02) //硬件序列号不匹配
|
|
|
+ {
|
|
|
+ errorCount++;
|
|
|
+ ret = updateErrorFirmwareInfoError;
|
|
|
+ }
|
|
|
+ else if(bmsAnswerMsg[4] == 0x03) //固件大小超出范围
|
|
|
+ {
|
|
|
+ errorCount++;
|
|
|
+ ret = updateErrorFirmwareSizeError;
|
|
|
+ }
|
|
|
+ else if(bmsAnswerMsg[4] == 0x04) //固件包头信息错误
|
|
|
+ {
|
|
|
+ errorCount++;
|
|
|
+ ret = updateErrorFirmwareInfoError;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ errorCount++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ errorCount++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ errorCount++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ errorCount++;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(errorCount>10)
|
|
|
+ {
|
|
|
+ updateStep = MS_UPDATE_STEP_ERROR;
|
|
|
+ errorCount = 0;
|
|
|
+ }
|
|
|
+ printf(" step 3 ret = %d\n",ret);
|
|
|
+ osDelay(50);
|
|
|
+ break;
|
|
|
+ case MS_UPDATE_STEP_EREASE_APP_FLASH_REQUEST: //0x05
|
|
|
+ dataLen = 0;
|
|
|
+ updateMsgSendLen = 6+dataLen;
|
|
|
+ updateMsgReadLen = 8;
|
|
|
+ pUpdateMsgSend[0] = 0x01; //node byte
|
|
|
+ pUpdateMsgSend[1] = 0x40; //func byte
|
|
|
+ pUpdateMsgSend[2] = updateStep; //cmd byte
|
|
|
+ pUpdateMsgSend[3] = dataLen; //data len
|
|
|
+
|
|
|
+ CRCtemp = MS_BMS_Update_CRC16(pUpdateMsgSend, 4+dataLen);
|
|
|
+
|
|
|
+ pUpdateMsgSend[4+dataLen] = (CRCtemp>>8)&0xFF; // CRC High
|
|
|
+ pUpdateMsgSend[5+dataLen] = CRCtemp&0xFF; //CRC Low
|
|
|
+
|
|
|
+ memset((UINT8*)(bmsAnswerMsg) , 0, 8);
|
|
|
+ ret0 = MS_BMS_Update_Query(pUpdateMsgSend, updateMsgSendLen, (UINT8*)(bmsAnswerMsg), updateMsgReadLen, 500);
|
|
|
+ #ifdef USING_PRINTF
|
|
|
+ printf("update step 5 answer:\n");
|
|
|
+ for(ii=0;ii<updateMsgReadLen;ii++)
|
|
|
+ printf("%x ",bmsAnswerMsg[ii]);
|
|
|
+
|
|
|
+ printf("\nret0 = %d",ret0);
|
|
|
+ printf("\n");
|
|
|
+ #endif
|
|
|
+ if(ret0!=0)
|
|
|
+ {
|
|
|
+ if(bmsAnswerMsg[0] == 0x01 && bmsAnswerMsg[1] == 0x40) // node and func byte
|
|
|
+ {
|
|
|
+ if(bmsAnswerMsg[2] == MS_UPDATE_STEP_EREASE_FLASH_ANSWER && bmsAnswerMsg[3] == 0x02) //answer cmd byte:0x06, answer data len:0x02
|
|
|
+ {
|
|
|
+ if(bmsAnswerMsg[4] == 0x00) //answer data byte1, erease successed
|
|
|
+ {
|
|
|
+ updateStep = MS_UPDATE_STEP_SEND_UPDATE_DATA; //0x07
|
|
|
+ errorCount = 0;
|
|
|
+ }
|
|
|
+ else if(bmsAnswerMsg[4] == 0x01) //擦除失败
|
|
|
+ {
|
|
|
+ errorCount++;
|
|
|
+ ret = updateErrorAppErease;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ errorCount++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ errorCount++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ errorCount++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ errorCount++;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(errorCount>10)
|
|
|
+ {
|
|
|
+ updateStep = MS_UPDATE_STEP_ERROR;
|
|
|
+ errorCount = 0;
|
|
|
+ }
|
|
|
+ osDelay(50);
|
|
|
+ break;
|
|
|
+ case MS_UPDATE_STEP_SEND_UPDATE_DATA: //0x07
|
|
|
+
|
|
|
+ updateMsgReadLen = 7;
|
|
|
+ pUpdateMsgSend[0] = 0x01; //node byte
|
|
|
+ pUpdateMsgSend[1] = 0x40; //func byte
|
|
|
+ pUpdateMsgSend[2] = updateStep; //cmd byte
|
|
|
+
|
|
|
+ for(i = 0; i < updateDataPackageCount ; i++ )
|
|
|
+ {
|
|
|
+
|
|
|
+
|
|
|
+ memset(ReadNVMTemp, 0, 64);
|
|
|
+
|
|
|
+ if((i+1)*64 < (updateDataTotalByteLen))
|
|
|
+ {
|
|
|
+ tempLen = 64;
|
|
|
+ BSP_QSPI_Read_Safe(ReadNVMTemp,FLASH_BMS_FOTA_START_ADDR+headerLen+i*64,64);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ tempLen = (updateDataTotalByteLen+4) - i*64;
|
|
|
+ BSP_QSPI_Read_Safe(ReadNVMTemp,FLASH_BMS_FOTA_START_ADDR+headerLen+i*64,tempLen);
|
|
|
+ }
|
|
|
+
|
|
|
+ CRCtemp = MS_BMS_Update_CRC16(ReadNVMTemp, tempLen);
|
|
|
+
|
|
|
+ dataLen = tempLen+6; //data len =count(2+2 byte) + crc(2byte) + update data len
|
|
|
+ updateMsgSendLen = 6+dataLen; // updateMsgSendLen = data len + header len(6byte)
|
|
|
+
|
|
|
+ pUpdateMsgSend[3] = dataLen; //data len
|
|
|
+
|
|
|
+ pUpdateMsgSend[4] = ((i+1)>>8)&0xFF; //当前包序号,大端模式
|
|
|
+ pUpdateMsgSend[5] = (i+1)&0xFF;
|
|
|
+
|
|
|
+ pUpdateMsgSend[6] = (updateDataPackageCount>>8)&0xFF;
|
|
|
+ pUpdateMsgSend[7] = updateDataPackageCount&0xFF;
|
|
|
+ pUpdateMsgSend[8] = (CRCtemp>>8)&0xFF; // data CRC High
|
|
|
+ pUpdateMsgSend[9] = CRCtemp&0xFF; //data CRC Low
|
|
|
+
|
|
|
+ MEMCPY(&pUpdateMsgSend[4+6], ReadNVMTemp, 64); //升级数据,64字节
|
|
|
+
|
|
|
+
|
|
|
+ CRCtemp = MS_BMS_Update_CRC16(pUpdateMsgSend, 4+dataLen);
|
|
|
+
|
|
|
+ pUpdateMsgSend[4+dataLen] = (CRCtemp>>8)&0xFF; // CRC High
|
|
|
+ pUpdateMsgSend[5+dataLen] = CRCtemp&0xFF; //CRC Low
|
|
|
+
|
|
|
+ memset((UINT8*)(bmsAnswerMsg) , 0, 8);
|
|
|
+ ret0 = MS_BMS_Update_Query(pUpdateMsgSend, updateMsgSendLen, (UINT8*)(bmsAnswerMsg), updateMsgReadLen, 500);
|
|
|
+ #ifdef USING_PRINTF
|
|
|
+ printf("update step 7 answer:\n");
|
|
|
+ for(ii=0;ii<updateMsgReadLen;ii++)
|
|
|
+ printf("%x ",bmsAnswerMsg[ii]);
|
|
|
+
|
|
|
+ printf("\nret0 = %d",ret0);
|
|
|
+ printf("\n");
|
|
|
+ #endif
|
|
|
+ if(ret0!=0)
|
|
|
+ {
|
|
|
+ if(bmsAnswerMsg[0] == 0x01 && bmsAnswerMsg[1] == 0x40) // node and func byte
|
|
|
+ {
|
|
|
+ if(bmsAnswerMsg[2] == MS_UPDATE_STEP_UPDATE_DATA_WRITE_ANSWER && bmsAnswerMsg[3] == 0x01) //answer cmd byte:0x04, answer data len:0x01
|
|
|
+ {
|
|
|
+ if(bmsAnswerMsg[4] == 0x00) //answer data byte1,接收并操作成功
|
|
|
+ {
|
|
|
+ updateStep = MS_UPDATE_STEP_EREASE_APP_FLASH_REQUEST;
|
|
|
+ errorCount = 0;
|
|
|
+ }
|
|
|
+ else if(bmsAnswerMsg[4] == 0x01) //固件块校验失败
|
|
|
+ {
|
|
|
+ errorCount=10;
|
|
|
+ ret = updateErrorPackageCRC;
|
|
|
+ }
|
|
|
+ else if(bmsAnswerMsg[4] == 0x02) //烧写失败
|
|
|
+ {
|
|
|
+ errorCount=10;
|
|
|
+ ret = updateErrorPackageWrite;
|
|
|
+ }
|
|
|
+ else if(bmsAnswerMsg[4] == 0x03) //固件块编号异常
|
|
|
+ {
|
|
|
+ errorCount=10;
|
|
|
+ ret = updateErrorPackageNo;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ errorCount=10;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ errorCount=10;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ errorCount=10;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ errorCount=10;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(errorCount>=10)
|
|
|
+ {
|
|
|
+ updateStep = MS_UPDATE_STEP_ERROR;
|
|
|
+ errorCount = 0;
|
|
|
+ i--;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ osDelay(50);
|
|
|
+ }
|
|
|
+ if(i == updateDataPackageCount)
|
|
|
+ {
|
|
|
+ updateStep = MS_UPDATE_STEP_SEND_UPDATE_DATA_END_AND_JUMP_TO_APP;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case MS_UPDATE_STEP_SEND_UPDATE_DATA_END_AND_JUMP_TO_APP: //0x09
|
|
|
+ dataLen = 0x00;
|
|
|
+ updateMsgSendLen = 6+dataLen;
|
|
|
+ updateMsgReadLen = 7;
|
|
|
+
|
|
|
+ pUpdateMsgSend[0] = 0x01; //node byte
|
|
|
+ pUpdateMsgSend[1] = 0x40; //func byte
|
|
|
+ pUpdateMsgSend[2] = updateStep; //cmd byte
|
|
|
+ pUpdateMsgSend[3] = dataLen; //data len
|
|
|
+ //no data type
|
|
|
+ CRCtemp = MS_BMS_Update_CRC16(pUpdateMsgSend, 4);
|
|
|
+ pUpdateMsgSend[4] = (CRCtemp>>8)&0xFF; // CRC High
|
|
|
+ pUpdateMsgSend[5] = CRCtemp&0xFF; //CRC Low
|
|
|
+
|
|
|
+ memset((UINT8*)(bmsAnswerMsg) , 0, 8);
|
|
|
+ ret0 = MS_BMS_Update_Query(pUpdateMsgSend, updateMsgSendLen, (UINT8*)(bmsAnswerMsg), updateMsgReadLen, 500);
|
|
|
+ #ifdef USING_PRINTF
|
|
|
+ printf("update step 9 answer:\n");
|
|
|
+ for(ii=0;ii<updateMsgReadLen;ii++)
|
|
|
+ printf("%x ",bmsAnswerMsg[ii]);
|
|
|
+
|
|
|
+ printf("\nret0 = %d",ret0);
|
|
|
+ printf("\n");
|
|
|
+ #endif
|
|
|
+ if(ret0!=0)
|
|
|
+ {
|
|
|
+ if(bmsAnswerMsg[0] == 0x01 && bmsAnswerMsg[1] == 0x40) // node and func byte
|
|
|
+ {
|
|
|
+ if(bmsAnswerMsg[2] == MS_UPDATE_STEP_JUMP_TO_APP_ANSWER && bmsAnswerMsg[3] == 0x01) //answer cmd byte:0x0A, answer data len:0x01
|
|
|
+ {
|
|
|
+ if(bmsAnswerMsg[4] == 0x00) //answer data byte1, update succeed
|
|
|
+ {
|
|
|
+ errorCount = 0;
|
|
|
+ updateStep = MS_UPDATE_STEP_READ_CURRENT_RUNNING_MODE; //0x0B
|
|
|
+ }
|
|
|
+ else if(bmsAnswerMsg[4] == 0x01) //升级失败
|
|
|
+ {
|
|
|
+ errorCount = 10;
|
|
|
+ ret = updateFailed;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ errorCount++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ errorCount++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ errorCount++;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(errorCount>=10)
|
|
|
+ {
|
|
|
+ updateStep = MS_UPDATE_STEP_ERROR;
|
|
|
+ errorCount = 0;
|
|
|
+ }
|
|
|
+ osDelay(50);
|
|
|
+ break;
|
|
|
+ case MS_UPDATE_STEP_READ_CURRENT_RUNNING_MODE: //0x0B
|
|
|
+ dataLen = 0x00;
|
|
|
+ updateMsgSendLen = 6+dataLen;
|
|
|
+ updateMsgReadLen = 8;
|
|
|
+
|
|
|
+ pUpdateMsgSend[0] = 0x01; //node byte
|
|
|
+ pUpdateMsgSend[1] = 0x40; //func byte
|
|
|
+ pUpdateMsgSend[2] = updateStep; //cmd byte
|
|
|
+ pUpdateMsgSend[3] = dataLen; //data len
|
|
|
+ //no data type
|
|
|
+ CRCtemp = MS_BMS_Update_CRC16(pUpdateMsgSend, 4);
|
|
|
+ pUpdateMsgSend[4] = (CRCtemp>>8)&0xFF; // CRC High
|
|
|
+ pUpdateMsgSend[5] = CRCtemp&0xFF; //CRC Low
|
|
|
+
|
|
|
+ memset((UINT8*)(bmsAnswerMsg) , 0, 8);
|
|
|
+ ret0 = MS_BMS_Update_Query(pUpdateMsgSend, updateMsgSendLen, (UINT8*)(bmsAnswerMsg), updateMsgReadLen, 500);
|
|
|
+ #ifdef USING_PRINTF
|
|
|
+ printf("update step A answer:\n");
|
|
|
+ for(ii=0;ii<updateMsgReadLen;ii++)
|
|
|
+ printf("%x ",bmsAnswerMsg[ii]);
|
|
|
+
|
|
|
+ printf("\nret0 = %d",ret0);
|
|
|
+ printf("\n");
|
|
|
+ #endif
|
|
|
+ if(ret0!=0)
|
|
|
+ {
|
|
|
+ if(bmsAnswerMsg[0] == 0x01 && bmsAnswerMsg[1] == 0x40) // node and func byte
|
|
|
+ {
|
|
|
+ if(bmsAnswerMsg[2] == MS_UPDATE_STEP_CURRENT_RUNNING_MODE_ANSWER && bmsAnswerMsg[3] == 0x02) //answer cmd byte:0x0C, answer data len:0x02
|
|
|
+ {
|
|
|
+ if(bmsAnswerMsg[4] == 0x01) //answer data byte1, update succeed, app is running
|
|
|
+ {
|
|
|
+ errorCount = 0;
|
|
|
+ updateStep = MS_UPDATE_STEP_END;
|
|
|
+ }
|
|
|
+ else if(bmsAnswerMsg[4] == 0x00) //update failed , boot is running,error
|
|
|
+ {
|
|
|
+ errorCount = 10;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ errorCount++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ errorCount++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ errorCount++;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(errorCount>=3)
|
|
|
+ {
|
|
|
+ updateStep = MS_UPDATE_STEP_ERROR;
|
|
|
+ errorCount = 0;
|
|
|
+ }
|
|
|
+ osDelay(50);
|
|
|
+ break;
|
|
|
+ case MS_UPDATE_STEP_END: //0x0D
|
|
|
+ errorCount = 0;
|
|
|
+ bmsUpdateFlag = FALSE;
|
|
|
+ ret = updateOK;
|
|
|
+ break;
|
|
|
+ case MS_UPDATE_STEP_ERROR: //0x0E
|
|
|
+ errorCount = 0;
|
|
|
+ bmsUpdateFlag = true;
|
|
|
+ Cycle_conut++;
|
|
|
+ if(Cycle_conut>2)
|
|
|
+ {
|
|
|
+ ret = updateErrorTimeout;
|
|
|
+ bmsUpdateFlag = FALSE;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ bmsUpdateFlag = FALSE;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ #ifdef USING_PRINTF
|
|
|
+ printf("last ret = %x\n",ret);
|
|
|
+ #endif
|
|
|
+ return ret;
|
|
|
+
|
|
|
+}
|
|
|
+UINT8 MS_BMS_Update_Query(UINT8* pSend,UINT32 sendLen, UINT8* pRead, UINT32 readLen, UINT32 timeout)
|
|
|
+{
|
|
|
+
|
|
|
+ UINT8 timeCount = 0;
|
|
|
+ UINT8 j=0;
|
|
|
+ USARTdrv->Send(pSend,sendLen);
|
|
|
+ #ifdef USING_PRINTF
|
|
|
+ printf("query in:");
|
|
|
+ for(j=0;j<sendLen;j++)
|
|
|
+ {
|
|
|
+ printf("%x ",*(pSend+j));
|
|
|
+ }
|
|
|
+ printf("\n");
|
|
|
+ #endif
|
|
|
+ if(readLen>0)
|
|
|
+ {
|
|
|
+ USARTdrv->Receive(pRead,readLen);
|
|
|
+ while((isRecvTimeout == false) && (isRecvComplete == false))
|
|
|
+ {
|
|
|
+ timeCount++;
|
|
|
+ osDelay(100);
|
|
|
+ if (timeCount>=timeout/100)
|
|
|
+ {
|
|
|
+ timeCount =0;
|
|
|
+ isRecvTimeout = true;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ #ifdef USING_PRINTF
|
|
|
+ printf("\nanswer in:");
|
|
|
+ for(j=0;j<readLen;j++)
|
|
|
+ {
|
|
|
+ printf("%x ",*(pRead+j));
|
|
|
+ }
|
|
|
+ printf("\n");
|
|
|
+ #endif
|
|
|
+ if (isRecvComplete == true)
|
|
|
+ {
|
|
|
+ isRecvComplete = false;
|
|
|
+ if(*(pRead+0)!=0x01)
|
|
|
+ {
|
|
|
+ USARTdrv->Uninitialize();
|
|
|
+ osDelay(100);
|
|
|
+ USARTdrv->Initialize(USART_callback);
|
|
|
+ USARTdrv->PowerControl(ARM_POWER_FULL);
|
|
|
+ USARTdrv->Control(ARM_USART_MODE_ASYNCHRONOUS |
|
|
|
+ ARM_USART_DATA_BITS_8 |
|
|
|
+ ARM_USART_PARITY_NONE |
|
|
|
+ ARM_USART_STOP_BITS_1 |
|
|
|
+ ARM_USART_FLOW_CONTROL_NONE, 9600);
|
|
|
+ #ifdef USING_PRINTF
|
|
|
+ printf("\nuart reset in \n");
|
|
|
+ #endif
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ return readLen;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ memset(pRead,0x00,readLen);
|
|
|
+ isRecvTimeout = false;
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+static void __invert_uint8(UINT8* dBuf, UINT8* srcBuf)
|
|
|
+{
|
|
|
+ int i;
|
|
|
+ UINT8 tmp[4];
|
|
|
+ tmp[0] = 0;
|
|
|
+ for (i = 0;i < 8;i++)
|
|
|
+ {
|
|
|
+ if(srcBuf[0] & (1 << i))
|
|
|
+ {
|
|
|
+ tmp[0] |= 1<<(7-i);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ dBuf[0] = tmp[0];
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+static void __invert_uint16(UINT16* dBuf, UINT16* srcBuf)
|
|
|
+{
|
|
|
+ int i;
|
|
|
+ UINT16 tmp[4];
|
|
|
+ tmp[0] = 0;
|
|
|
+ for (i = 0;i < 16;i++)
|
|
|
+ {
|
|
|
+ if(srcBuf[0] & (1 << i))
|
|
|
+ {
|
|
|
+ tmp[0] |= 1 << (15 - i);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ dBuf[0] = tmp[0];
|
|
|
+}
|
|
|
+
|
|
|
+UINT16 MS_BMS_Update_CRC16(UINT8* pSendData,UINT16 len)
|
|
|
+{
|
|
|
+
|
|
|
+ UINT16 wCRCin = 0xFFFF;
|
|
|
+ UINT16 wCPoly = 0x8005;
|
|
|
+ UINT8 wChar = 0;
|
|
|
+ UINT16 crc_rslt = 0;
|
|
|
+ int i;
|
|
|
+ while (len--)
|
|
|
+ {
|
|
|
+ wChar = *(pSendData++);
|
|
|
+ __invert_uint8(&wChar, &wChar);
|
|
|
+ wCRCin ^= (wChar << 8);
|
|
|
+ for (i = 0;i < 8;i++)
|
|
|
+ {
|
|
|
+ if(wCRCin & 0x8000)
|
|
|
+ {
|
|
|
+ wCRCin = (wCRCin << 1) ^ wCPoly;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ wCRCin = wCRCin << 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ __invert_uint16(&wCRCin, &wCRCin);
|
|
|
+ crc_rslt = ((wCRCin << 8) & 0xFF00) | ((wCRCin >> 8) & 0x00FF);
|
|
|
+ return (crc_rslt);
|
|
|
}
|