Parcourir la source

4-1集成,1.增加LED显示,2.精简UDS代码,3.新增保护板刷写,4.增加下线检测策略

CHENJIE-PC\QiXiang_CHENJIE il y a 4 ans
Parent
commit
fc5c7af27a
10 fichiers modifiés avec 841 ajouts et 580 suppressions
  1. 1 1
      inc/TcpTask.h
  2. 28 0
      inc/UartTask.h
  3. 1 0
      inc/app.h
  4. 3 1
      inc/hal_module_adapter.h
  5. 23 2
      src/MainTask.c
  6. 297 502
      src/UDSService.c
  7. 412 61
      src/UartTask.c
  8. 4 0
      src/app.c
  9. 2 1
      src/bsp_custom.c
  10. 70 12
      src/hal_module_adapter.c

+ 1 - 1
inc/TcpTask.h

@@ -142,7 +142,7 @@ typedef struct VersionMsgtoTcpType
 /*---------------------------------------------------------------------------*/
 typedef enum
 {
-    APP_SOCKET_CONNECTION_CLOSED,
+    APP_SOCKET_CONNECTION_CLOSED = 0,
     APP_SOCKET_CONNECTION_CONNECTING,
     APP_SOCKET_CONNECTION_CONNECTED,    
 }AppSocketConnectionStatus;

+ 28 - 0
inc/UartTask.h

@@ -64,6 +64,32 @@ typedef struct Uart_Write_Answer_Msg_Type
     uint8_t Bms_Address; 
     uint8_t Bms_Funcode; 
 }Uart_Write_Answer_Msg_Type;
+typedef struct BMS_Update_Recv_Msg_Type
+{
+	UINT8 startFlag;
+	UINT8 addrFlag;
+	UINT8 cmdRW;
+	UINT8 dataLen;
+	UINT8 cmd;
+	UINT8 data;
+	UINT8 checkSum;
+	UINT8 endFlag;
+}BMS_Update_Recv_Msg_Type;
+typedef enum
+{
+	UPDATE_STEP_CHECK_VERSION=0,
+	UPDATE_STEP_REQUEST_UPDATE,
+	UPDATE_STEP_START_UPDATE,	
+	UPDATE_STEP_SET_BAUD_RATE,
+	UPDATE_STEP_PREPARE_SEND_DATA_LEN,
+	UPDATE_STEP_SEND_DATA_LEN,
+	UPDATE_STEP_PREPARE_SEND_UPDATE_DATA,
+	UPDATE_STEP_SEND_UPDATE_DATA,
+	UPDATE_STEP_SEND_DATA_END,
+	UPDATE_STEP_START_INSTALL,
+	UPDATE_STEP_END,
+	UPDATE_STEP_ERROR
+}UpdateStep;
 typedef enum
 {
     PROCESS_UART_STATE_IDLE = 0,
@@ -73,3 +99,5 @@ typedef enum
 }process_Uart;
 void UartTaskInit(void* arg);
 void UartTaskDeInit(void* arg);
+UINT8 SP_BMS_Update_Query(UINT8* pSend,UINT32 sendLen, UINT8* pRead, UINT32 readLen, UINT32 timeout);
+UINT8 SP_BMS_Update_CheckSUM(UINT8* pSendData,UINT8 len);

+ 1 - 0
inc/app.h

@@ -50,6 +50,7 @@ typedef struct AppNVMDataType
     UINT32 BLSwVersion;
     UINT32 DrvSwVersion;
     UINT32 AppSwVersion;
+	UINT8  EOLState;
 }AppNVMDataType;
 
 typedef struct AppConfigHeader

+ 3 - 1
inc/hal_module_adapter.h

@@ -8,7 +8,7 @@ extern "C" {
 #include "sensor.h"
 #include "can.h"
 
-#define USING_PRINTF 
+#define USING_PRINTF
 #define GPS_DATA_RECV_BUFFER_SIZE     						(1024)
 
 #define I2C_INT1_REQ_BITMAP    								 (0x1)
@@ -141,6 +141,8 @@ typedef struct CAN_Msg_Type
 }CAN_Msg_Type;
 typedef void (*posGGACallBack )(UINT8 res, UINT8 * params);
 void PowerPinConfig(IOType iotype);
+void relayConfigInit(void);
+void relayControl(BOOL onOrOff);
 
 void posGGAServiceStop( void);
 void posGGAReset(void);

+ 23 - 2
src/MainTask.c

@@ -32,7 +32,7 @@
 #include "Fota.h"
 #include "UartTask.h"
 extern volatile bool Fota_update_flag;
-CHAR BattSN[BATT_SN_LEN] = "GYTEST00000000003";//SN仍在测试
+CHAR defaultBattSN[BATT_SN_LEN] = "GYTEST00000000003";//SN仍在测试
 //全局变量
 UINT32 Timer_count;//每100ms加1
 volatile bool Sleep_flag = false;//睡眠标志位
@@ -75,6 +75,7 @@ AppConfigHeader    AppConfigHr;   //4 bytes
 static void MainTask(void* arg)
 {
     int32_t inParam = 0xAABBCCDD;
+    UINT32  param;
     slpManSetPmuSleepMode(true,SLP_HIB_STATE,false);
     slpManApplyPlatVoteHandle("MainSlp",&MainSlpHandler);
     slpManPlatVoteDisableSleep(MainSlpHandler, SLP_SLP1_STATE);
@@ -121,6 +122,16 @@ static void MainTask(void* arg)
     montior_timer = xTimerCreate("montior_timer", 100 / portTICK_RATE_MS, pdTRUE, NULL, montior_timer_callback);
     work_timer = xTimerCreate("work_timer", AppNVMData.wakeupWorkTime*60*1000 / portTICK_RATE_MS, pdTRUE, NULL, work_timer_callback);
 
+	/***calibrate the ADC Collect***/
+	AdcSendReq(ADC_REQ_BITMAP_VBAT_CALI,&param,1,ADC_GET_RESULT_TIMOUT);						
+	AdcSendReq(ADC_REQ_BITMAP_INRES_CALI,&param,1,ADC_GET_RESULT_TIMOUT);
+
+	
+	NetSocDisplay(LED_SOC_0,LED_TURN_OFF);
+	NetSocDisplay(LED_SOC_1,LED_TURN_OFF);
+	NetSocDisplay(LED_SOC_2,LED_TURN_OFF);
+	NetSocDisplay(LED_SOC_3,LED_TURN_OFF);
+	FaultDisplay(LED_TURN_OFF);
     PROC_MAIN_STATE_SWITCH(PROCESS_STATE_IDLE);
     while(true)
     {
@@ -207,6 +218,9 @@ static void MainTask(void* arg)
             default:
             {
                 PROC_MAIN_STATE_SWITCH(PROCESS_STATE_IDLE);
+                #ifdef USING_PRINTF
+                    printf("default!\n");
+                #endif
                 break;
             }
         }
@@ -240,6 +254,12 @@ void MainTaskDeInit(void *arg)
 //定时器回调函数区
 void montior_timer_callback(TimerHandle_t xTimer)
 {
+    #ifdef USING_PRINTF
+        if (Timer_count%50==0)
+        {
+            printf("Main Task,Batt_Cell_Num:%d,%d!\n",BATT_CELL_VOL_NUM,Timer_count);
+        }
+    #endif
     Timer_count++;
     if(Timer_count>100000*100)
     {
@@ -557,7 +577,7 @@ static void setDefaultAppDataValue(void)
     memset(&AppNVMData, 0x00, sizeof(AppNVMDataType));
 	for(i=0;i<BATT_SN_LEN;i++)
 	{
-		AppNVMData.battSN[i] = BattSN[i];  //the defult sn is "0000..0000"(17 bytes ASCII)
+		AppNVMData.battSN[i] = defaultBattSN[i];  //the defult sn is "0000..0000"(17 bytes ASCII)
 	}
 	AppNVMData.isBattLocked = false;
 	AppNVMData.chargEndWorkTime = 30;  //30 min
@@ -567,6 +587,7 @@ static void setDefaultAppDataValue(void)
     AppNVMData.BLSwVersion = BLSWVERSION;
     AppNVMData.DrvSwVersion = DRVSWVERSION;
     AppNVMData.AppSwVersion = APPSWVERSION;
+    AppNVMData.EOLState = 0; //未进行下线配置
     return;
 }
 

+ 297 - 502
src/UDSService.c

@@ -21,17 +21,23 @@
 #include "MainTask.h"
 #include "app.h"
 #include "CANTask.h"
+#include "flash_ec616_rt.h"
+
 #include "UDSService.h"
 
 
 //
 extern UartReadMsgType UartReadMsg;
 extern osMutexId_t UartMutex;
+extern AppSocketConnectionContext socContext;
+extern UINT8 GpsData[16];
 
 void UDS_Service(void);
 void UDS_CAN_Update(void);
 BOOL UDSClearFotaDownloadRegion(void);
 BOOL UDSAskforDownLoadData(void);
+UINT8 UDSPositiveAnswer(UINT8 answerLen,UINT8 messageIndex,UINT32 posCode);
+UINT8 UDSNegtiveAnswer(UINT8 answerLen,UINT8 messageIndex, UINT32 negCode);
 extern void appSaveConfigValue(void);
 
 
@@ -39,34 +45,37 @@ UINT8	UDSService[2];
 UINT8	UDSSubService[2];
 UINT8	UDSSubServiceActionCode[2];
 UINT8 	UDSSwitch = 0;
-UINT8   UDSDialogMode = 0;
+UINT8   UDSDialogMode = 01;
 UINT8   UDSBattSN[BATT_SN_LEN];
 
-UINT16  updateDifferDataByteLen = 0;
+UINT32  updateDifferDataByteLen = 0;
 
 //each package means 100 bytes, max PackageCounter = updateDifferDataByteLen%100 + 1
 UINT16  updateDifferDataPackageCounter = 0;  
 
 //each slice means 4 bytes, max SliceCounter = 25;
-UINT16  updateDifferDataSliceCounter = 0;
-
+UINT32  updateDifferDataSliceCounter = 0;
+static BOOL downloadReady = FALSE;
 
 UINT8   updateDataBuffer[100];
 
-extern UINT8 BattSN[BATT_SN_LEN];
+
+
 
 
 void UDS_Service()
 {
-	UINT8 i,j,k = 0;
-	INT8 ret = 0;
+	UINT8 i,j= 0;
+	UINT32 k=0;
+	INT8 ret,errorCount = 0;
 	BOOL boolRet = false;
 	static UINT8 snFlag = 0;
 	static UINT32 sliceCounterFlag = 0;
-	UINT16 tempSliceCounter=0;
+	UINT32 tempSliceCounter=0;	
 	static UINT8 counter=0;  
 	CAN_Msg_Type UDSAnswer ={0};
 	static UINT8 battInfo[8*14] = {0};
+	UINT32 posCode,negCode;
 	
 	//UartReadMsg Uart_Recv_Msg;
 	//UINT8 temp00[94] ={0x00,0x0E,0x00,0x05,0x0D,0xDE,0x0D,0xDE,0x0D,0xDE,0x0D,0xDE,0x0D,0x7A,0x0D,0xDE,0x0D,0xDE,0x0D,0xDE,0x0D,0xDE,0x0D,0xDE,0x0E,0x42,0x0D,0xDE,0x0D,0xDE,0x0D,0xDE,0x13,0x38,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x21,0x00,0x24,0x00,0x26,0x00,0x22,0x00,0x24,0x00,0x00,0x00,0x00,0x6e,0x78,0x00,0x00,0x00,0x00,0x00,0x5F,0x00,0x62,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x13,0x74,0x0E,0x42,0x0D,0x7A,0x00,0x00};
@@ -85,102 +94,42 @@ void UDS_Service()
 							case 0x10:
 								if(UDSSubService[i] == 0x01)
 								{
-									if(UDSDialogMode == 0x01 || UDSDialogMode==0x02)
-									{
-										UDSAnswer.Id = 0x7A8;
-										UDSAnswer.DLC = 8;
-										UDSAnswer.Data[0] = 0x04;
-										UDSAnswer.Data[1] = 0x78;
-										UDSAnswer.Data[2] = UDSService[i];
-										UDSAnswer.Data[3] = UDSSubService[i];
-										UDSAnswer.Data[4] = 0x00;
-										UDSAnswer.Data[5] = 0x00;
-										UDSAnswer.Data[6] = 0x00;
-										UDSAnswer.Data[7] = 0x00;
-										ret = HAL_Can_Transmit(UDSAnswer);
+									if((UDSDialogMode == 0x01 || UDSDialogMode==0x02)||UDSSwitch == 0)
+									{									
+										UDSPositiveAnswer(0x04,i,0x00);
 										UDSSwitch = 0;
 										UDSDialogMode = 1;
 									}
 									else
 									{
-										UDSAnswer.Id = 0x7A8;
-										UDSAnswer.DLC = 8;
-										UDSAnswer.Data[0] = 0x05;
-										UDSAnswer.Data[1] = 0x7F;
-										UDSAnswer.Data[2] = UDSService[i];
-										UDSAnswer.Data[3] = UDSSubService[i];
-										UDSAnswer.Data[4] = 0xFF;
-										UDSAnswer.Data[5] = 0x00;
-										UDSAnswer.Data[6] = 0x00;
-										UDSAnswer.Data[7] = 0x00;
-										ret = HAL_Can_Transmit(UDSAnswer);
+										UDSNegtiveAnswer(0x05,i,0xFF);
 									}
 								}
 								else if(UDSSubService[i] == 0x02)
 								{
-									UDSAnswer.Id = 0x7A8;
-									UDSAnswer.DLC = 8;
-									UDSAnswer.Data[0] = 0x04;
-									UDSAnswer.Data[1] = 0x78;
-									UDSAnswer.Data[2] = UDSService[i];
-									UDSAnswer.Data[3] = UDSSubService[i];
-									UDSAnswer.Data[4] = 0x00;
-									UDSAnswer.Data[5] = 0x00;
-									UDSAnswer.Data[6] = 0x00;
-									UDSAnswer.Data[7] = 0x00;
+									UDSPositiveAnswer(0x04,i,0x00);
 									UDSSwitch = 1;
-									UDSDialogMode = 2;
-									ret = HAL_Can_Transmit(UDSAnswer);
+									UDSDialogMode = 2;									
 								}
 								else if(UDSSubService[i] == 0x03)
 								{
 									if(UDSSwitch == 1)
 									{
-										UDSAnswer.Id = 0x7A8;
-										UDSAnswer.DLC = 8;
-										UDSAnswer.Data[0] = 0x04;
-										UDSAnswer.Data[1] = 0x78;
-										UDSAnswer.Data[2] = UDSService[i];
-										UDSAnswer.Data[3] = UDSSubService[i];
-										UDSAnswer.Data[4] = 0x00;
-										UDSAnswer.Data[5] = 0x00;
-										UDSAnswer.Data[6] = 0x00;
-										UDSAnswer.Data[7] = 0x00;
-										ret = HAL_Can_Transmit(UDSAnswer);
+										UDSPositiveAnswer(0x04,i,0x00);
 										UDSSwitch = 1;
 										UDSDialogMode = 3;
 			                		}
 			                		else
 			                		{
-										UDSAnswer.Id = 0x7A8;
-										UDSAnswer.DLC = 8;
-										UDSAnswer.Data[0] = 0x05;
-										UDSAnswer.Data[1] = 0x7F;
-										UDSAnswer.Data[2] = UDSService[i];
-										UDSAnswer.Data[3] = UDSSubService[i];
-										UDSAnswer.Data[4] = 0xFF;				//the dialog mode error
-										UDSAnswer.Data[5] = 0x00;
-										UDSAnswer.Data[6] = 0x00;
-										UDSAnswer.Data[7] = 0x00;
-										ret = HAL_Can_Transmit(UDSAnswer);
+										UDSNegtiveAnswer(0x05,i,0xFF);
 			                		}
 
 								}
 								else if(UDSSubService[i] == 0x11)   //make NB software reset
 								{
-									if(UDSDialogMode = 2)
+									if(UDSDialogMode == 2)
 									{
-										UDSAnswer.Id = 0x7A8;
-										UDSAnswer.DLC = 8;
-										UDSAnswer.Data[0] = 0x04;
-										UDSAnswer.Data[1] = 0x78;
-										UDSAnswer.Data[2] = UDSService[i];
-										UDSAnswer.Data[3] = UDSSubService[i];
-										UDSAnswer.Data[4] = 0x00;
-										UDSAnswer.Data[5] = 0x00;
-										UDSAnswer.Data[6] = 0x00;
-										UDSAnswer.Data[7] = 0x00;
-										ret = HAL_Can_Transmit(UDSAnswer);
+										UDSPositiveAnswer(0x04,i,0x00);										
 										osDelay(100);
 										appSaveNVMData();
 										appSetCFUN(0);
@@ -189,32 +138,12 @@ void UDS_Service()
 			                		}
 			                		else
 			                		{
-										UDSAnswer.Id = 0x7A8;
-										UDSAnswer.DLC = 8;
-										UDSAnswer.Data[0] = 0x05;
-										UDSAnswer.Data[1] = 0x7F;
-										UDSAnswer.Data[2] = UDSService[i];
-										UDSAnswer.Data[3] = UDSSubService[i];
-										UDSAnswer.Data[4] = 0xFF;				//the dialog mode error
-										UDSAnswer.Data[5] = 0x00;
-										UDSAnswer.Data[6] = 0x00;
-										UDSAnswer.Data[7] = 0x00;
-										ret = HAL_Can_Transmit(UDSAnswer);
+										UDSNegtiveAnswer(0x05,i,0xFF);  
 			                		}
 								}								
 								else
 								{
-									UDSAnswer.Id = 0x7A8;
-									UDSAnswer.DLC = 8;
-									UDSAnswer.Data[0] = 0x05;
-									UDSAnswer.Data[1] = 0x7F;
-									UDSAnswer.Data[2] = UDSService[i];
-									UDSAnswer.Data[3] = UDSSubService[i];
-									UDSAnswer.Data[4] = 0xEE;  //the sub service is not surported
-									UDSAnswer.Data[5] = 0x00;
-									UDSAnswer.Data[6] = 0x00;
-									UDSAnswer.Data[7] = 0x00;
-									ret = HAL_Can_Transmit(UDSAnswer);
+									UDSNegtiveAnswer(0x05,i,0xEE);
 								}
 								break;
 							case 0x22:
@@ -222,31 +151,11 @@ void UDS_Service()
 								{
 									if(UDSSubService[i] == 0x01)		//check the sw of NB
 									{
-										UDSAnswer.Id = 0x7A8;
-										UDSAnswer.DLC = 8;
-										UDSAnswer.Data[0] = 0x08;
-										UDSAnswer.Data[1] = 0x78;
-										UDSAnswer.Data[2] = UDSService[i];
-										UDSAnswer.Data[3] = UDSSubService[i];
-										UDSAnswer.Data[4] = APPSWVERSION>>24;
-										UDSAnswer.Data[5] = APPSWVERSION>>16;
-										UDSAnswer.Data[6] = APPSWVERSION>>8;
-										UDSAnswer.Data[7] = APPSWVERSION;
-										ret = HAL_Can_Transmit(UDSAnswer);
+										UDSPositiveAnswer(0x08,i,APPSWVERSION);
 									}
 									else if(UDSSubService[i] == 0x02)  //check the hw of NB
 									{
-										UDSAnswer.Id = 0x7A8;
-										UDSAnswer.DLC = 8;
-										UDSAnswer.Data[0] = 0x06;
-										UDSAnswer.Data[1] = 0x78;
-										UDSAnswer.Data[2] = UDSService[i];
-										UDSAnswer.Data[3] = UDSSubService[i];
-										UDSAnswer.Data[4] = HWVERSION>>8;
-										UDSAnswer.Data[5] = HWVERSION;
-										UDSAnswer.Data[6] = 0x00;
-										UDSAnswer.Data[7] = 0x00;
-										ret = HAL_Can_Transmit(UDSAnswer);
+										UDSPositiveAnswer(0x06,i,HWVERSION);										
 									}
 									else if(UDSSubService[i] == 0x03)  //check the SN number
 									{	
@@ -281,42 +190,23 @@ void UDS_Service()
 												ret = HAL_Can_Transmit(UDSAnswer);
 												break;
 											default:
-											break;
-											
-										}
-									#ifdef USING_PRINTF
-										printf("ID = %x\nData = ",UDSAnswer.Id);
-										for (int ii = 0; ii < 8; ii++)
-										{
-											printf("%x  ", UDSAnswer.Data[ii]);
-										}
-										printf("\n");
-									#endif
+											break;											
+										}									
 									}
 									else if(UDSSubService[i] == 0x04)  //check the batt message
-									{
-									/*
-										if(UDSSubService[i] == 0x00)
-										{
-											ret = xQueuePeek(UartReadQueueHandle,&Uart_Recv_Msg,1000);
-										}
-										if(ret)
-										{
-											//the uart_recv_msg.len = 5+(0x21+CellNum+TempNum)*2 = 5+(33+17+7)*2 = 	5+ 114(max);114>112(14*8)	
-											UINT8* p = (UINT8*)Uart_Recv_Msg.dataPtr+3;
-											MEMCPY(battInfo, p, 14*8);																			
-										}
-										*/
+									{									
 										osStatus_t result = osMutexAcquire(UartMutex, osWaitForever);
 										if(CanRxMsg[i].Data[2] == 0x00)
 										{
-											memcpy(battInfo,UartReadMsg.data,14*8);
+											MEMCPY(battInfo,UartReadMsg.data,14*8);
+										/*
 											#ifdef USING_PRINTF
 											printf("battInfo=\n");
 											for(int jj = 0;jj<14*8;jj++)
 												printf("%x ",battInfo[jj]);
 											printf("\n");
 											#endif
+										*/
 										}
 										osMutexRelease(UartMutex);
 
@@ -324,95 +214,35 @@ void UDS_Service()
 										UDSAnswer.DLC = 8;
 
 										MEMCPY(UDSAnswer.Data, &(battInfo[UDSSubServiceActionCode[i]*8]),8);
-										ret = HAL_Can_Transmit(UDSAnswer);
-										#ifdef USING_PRINTF
-										printf("ID = %x\nData = ",UDSAnswer.Id);
-										for (int ij = 0; ij < 8; ij++)
-										{
-											printf("%x  ", UDSAnswer.Data[ij]);
-										}
-										printf("\n");
-										#endif
+										ret = HAL_Can_Transmit(UDSAnswer);										
 									}
 									else if(UDSSubService[i] == 0x05)  //check the enviroment temp of NB
 									{
-										UDSAnswer.Id = 0x7A8;
-										UDSAnswer.DLC = 8;
-										UDSAnswer.Data[0] = 0x08;
-										UDSAnswer.Data[1] = 0x78;
-										UDSAnswer.Data[2] = UDSService[i];
-										UDSAnswer.Data[3] = UDSSubService[i];
-										UDSAnswer.Data[4] = 0x00;//gNtcDev.NTCvalue[2+NTC_Channel2];
-										UDSAnswer.Data[5] = 0x00;//gNtcDev.NTCvalue[2+NTC_Channel3];
-										UDSAnswer.Data[6] = 0x00;//gNtcDev.NTCvalue[2+NTC_Channel4];
-										UDSAnswer.Data[7] = 0x00;//gNtcDev.NTCvalue[2+NTC_Channel5];
-										ret = HAL_Can_Transmit(UDSAnswer);
-										
+										UDSPositiveAnswer(0x08,i,0x00);  //ntcvalue										
 									}									
 									else if(UDSSubService[i] == 0x06)	//check the tcp link of NB
 									{
-										
-
-										UDSAnswer.Id = 0x7A8;
-										UDSAnswer.DLC = 8;
-										UDSAnswer.Data[0] = 0x06;
-										UDSAnswer.Data[1] = 0x78;
-										UDSAnswer.Data[2] = UDSService[i];
-										UDSAnswer.Data[3] = UDSSubService[i];
-										UDSAnswer.Data[4] = socContext.status;
-										UDSAnswer.Data[5] = 0x00;
-										UDSAnswer.Data[6] = 0x00;
-										UDSAnswer.Data[7] = 0x00;
-										ret = HAL_Can_Transmit(UDSAnswer);
+										UDSPositiveAnswer(0x06,i,socContext.status);  //TcpconnectStatus
 									}
 									else if(UDSSubService[i] == 0x07)  //check the GPS link of NB
 									{
-										UDSAnswer.Id = 0x7A8;
-										UDSAnswer.DLC = 8;
-										UDSAnswer.Data[0] = 0x05;
-										UDSAnswer.Data[1] = 0x78;
-										UDSAnswer.Data[2] = UDSService[i];
-										UDSAnswer.Data[3] = UDSSubService[i];
-										UDSAnswer.Data[4] = GpsData[1];  //gps satellite num(uint8), should be modified
-										UDSAnswer.Data[5] = 0x00;
-										UDSAnswer.Data[6] = 0x00;
-										UDSAnswer.Data[7] = 0x00;
-										ret = HAL_Can_Transmit(UDSAnswer);
+										posCode =(UINT32) GpsData[1];
+										UDSPositiveAnswer(0x05,i,posCode);  //gps satellite num(uint8), should be modified
 									}	
 									else
 									{
-										UDSAnswer.Id = 0x7A8;
-										UDSAnswer.DLC = 8;
-										UDSAnswer.Data[0] = 0x05;
-										UDSAnswer.Data[1] = 0x7F;
-										UDSAnswer.Data[2] = UDSService[i];
-										UDSAnswer.Data[3] = UDSSubService[i];
-										UDSAnswer.Data[4] = 0xEE;  //the sub service is not surported
-										UDSAnswer.Data[5] = 0x00;
-										UDSAnswer.Data[6] = 0x00;
-										UDSAnswer.Data[7] = 0x00;
-										ret = HAL_Can_Transmit(UDSAnswer);
+										UDSNegtiveAnswer(0x05,i,0xEE);
 									}
 								}
-								else	//当前会话模式错误
+								else	//the service is not surpported in current dialog mode
 								{
-									UDSAnswer.Id = 0x7A8;
-									UDSAnswer.DLC = 8;
-									UDSAnswer.Data[0] = 0x05;
-									UDSAnswer.Data[1] = 0x7F;
-									UDSAnswer.Data[2] = UDSService[i];
-									UDSAnswer.Data[3] = UDSSubService[i];
-									UDSAnswer.Data[4] = 0xFF;
-									UDSAnswer.Data[5] = 0x00;
-									UDSAnswer.Data[6] = 0x00;
-									UDSAnswer.Data[7] = 0x00;
-									ret = HAL_Can_Transmit(UDSAnswer);
+									UDSNegtiveAnswer(0x05,i,0xFF);
 								}
 								break;
-							case 0x2E:
+							case 0x2E:   //write service
 								if(UDSDialogMode == 2)
 								{
-									if(UDSSubService[i] == 0x03)
+									if(UDSSubService[i] == 0x03)  // write the battSN
 									{
 										if(UDSSubServiceActionCode[i] == 0x00 && snFlag == 0x00)
 										{
@@ -449,19 +279,10 @@ void UDS_Service()
 
 										if(snFlag == 0x0F)
 										{
-											UDSAnswer.Id = 0x7A8;
-											UDSAnswer.DLC = 8;
-											UDSAnswer.Data[0] = 0x04;
-											UDSAnswer.Data[1] = 0x78;
-											UDSAnswer.Data[2] = UDSService[i];
-											UDSAnswer.Data[3] = UDSSubService[i];
-											UDSAnswer.Data[4] = 0x00;
-											UDSAnswer.Data[5] = 0x00;
-											UDSAnswer.Data[6] = 0x00;
-											UDSAnswer.Data[7] = 0x00;
 											snFlag = 0;
-
-											MEMCPY(AppNVMData.battSN, UDSBattSN, BATT_SN_LEN);											
+											UDSPositiveAnswer(0x04,i,00);
+											MEMCPY(AppNVMData.battSN, UDSBattSN, BATT_SN_LEN);
+											AppNVMData.EOLState = 1; //SN号写入完成,表明已经进行过下线配置
 										}
 										else
 										{
@@ -475,330 +296,201 @@ void UDS_Service()
 											UDSAnswer.Data[5] = 0x00;
 											UDSAnswer.Data[6] = 0x00;
 											UDSAnswer.Data[7] = 0x00;
-										}
-										ret = HAL_Can_Transmit(UDSAnswer);
-									}
-									else
-									{
-										UDSAnswer.Id = 0x7A8;
-										UDSAnswer.DLC = 8;
-										UDSAnswer.Data[0] = 0x05;
-										UDSAnswer.Data[1] = 0x7F;
-										UDSAnswer.Data[2] = UDSService[i];
-										UDSAnswer.Data[3] = UDSSubService[i];
-										UDSAnswer.Data[4] = 0xEE;			//the sub service is not surported
-										UDSAnswer.Data[5] = 0x00;
-										UDSAnswer.Data[6] = 0x00;
-										UDSAnswer.Data[7] = 0x00;
-										ret = HAL_Can_Transmit(UDSAnswer);
+											ret = HAL_Can_Transmit(UDSAnswer);
+										}										
 									}
-									break;
-								}
-								else if(UDSDialogMode==3)
-								{
-									if(UDSSubService[i] == 0x0F) //程序更新配置信息写入
+									else if(UDSSubService[i] == 0x0F) //write the update config:updateDifferDataByteLen
 									{										
-										updateDifferDataByteLen = (CanRxMsg[i].Data[2]<<8)|(CanRxMsg[i].Data[3]);
-										UDSAnswer.Id = 0x7A8;
-										UDSAnswer.DLC = 8;
-										UDSAnswer.Data[0] = 0x04;
-										UDSAnswer.Data[1] = 0x78;
-										UDSAnswer.Data[2] = UDSService[i];
-										UDSAnswer.Data[3] = UDSSubService[i];
-										UDSAnswer.Data[4] = 0x00;			
-										UDSAnswer.Data[5] = 0x00;
-										UDSAnswer.Data[6] = 0x00;
-										UDSAnswer.Data[7] = 0x00;
-										ret = HAL_Can_Transmit(UDSAnswer);
+										updateDifferDataByteLen = (CanRxMsg[i].Data[2]<<16)|(CanRxMsg[i].Data[3]<<8)|CanRxMsg[i].Data[4];										
+										UDSPositiveAnswer(0x04,i,00);
 									}
 									else
 									{
-										UDSAnswer.Id = 0x7A8;
-										UDSAnswer.DLC = 8;
-										UDSAnswer.Data[0] = 0x05;
-										UDSAnswer.Data[1] = 0x7F;
-										UDSAnswer.Data[2] = UDSService[i];
-										UDSAnswer.Data[3] = UDSSubService[i];
-										UDSAnswer.Data[4] = 0xEE;			//the sub service is not surported
-										UDSAnswer.Data[5] = 0x00;
-										UDSAnswer.Data[6] = 0x00;
-										UDSAnswer.Data[7] = 0x00;
-										ret = HAL_Can_Transmit(UDSAnswer);
+										UDSNegtiveAnswer(0x05,i,0xEE);
+										
 									}
-								}						
-								
+									break;
+								}								
 								else
 								{
-									UDSAnswer.Id = 0x7A8;
-									UDSAnswer.DLC = 8;
-									UDSAnswer.Data[0] = 0x05;
-									UDSAnswer.Data[1] = 0x7F;
-									UDSAnswer.Data[2] = UDSService[i];
-									UDSAnswer.Data[3] = UDSSubService[i];
-									UDSAnswer.Data[4] = 0xFF;				//the dialog mode error
-									UDSAnswer.Data[5] = 0x00;
-									UDSAnswer.Data[6] = 0x00;
-									UDSAnswer.Data[7] = 0x00;
-									ret = HAL_Can_Transmit(UDSAnswer);
+									UDSNegtiveAnswer(0x05,i,0xFF);
 								}
-									break;
-							case 0x31:
-								if(UDSDialogMode == 3)
+								break;
+							case 0x31:  //clear the flash service
+								if(UDSDialogMode == 3)  
 								{
-									if(UDSSubService[i] == 0x0F)
+									if(UDSSubService[i] == 0x0F)  //clear the fota flash
 									{
 										if(UDSSubServiceActionCode[i] == 0x01)
 										{
 											//Clear the Flash
 											boolRet = UDSClearFotaDownloadRegion();
-											//if ok
-											
-											UDSAnswer.Id = 0x7A8;
-											UDSAnswer.DLC = 8;
-											UDSAnswer.Data[0] = 0x05;
-											UDSAnswer.Data[1] = 0x78;
-											UDSAnswer.Data[2] = UDSService[i];
-											UDSAnswer.Data[3] = UDSSubService[i];
-											UDSAnswer.Data[4] = (UINT8)boolRet;  // true:01, false: 00			
-											UDSAnswer.Data[5] = 0x00;
-											UDSAnswer.Data[6] = 0x00;
-											UDSAnswer.Data[7] = 0x00;
-											ret = HAL_Can_Transmit(UDSAnswer);						
+											//if ok											
+											UDSPositiveAnswer(0x05,i,(UINT32)boolRet);
 										}
 										else
 										{
-											UDSAnswer.Id = 0x7A8;
-											UDSAnswer.DLC = 8;
-											UDSAnswer.Data[0] = 0x06;
-											UDSAnswer.Data[1] = 0x7F;
-											UDSAnswer.Data[2] = UDSService[i];
-											UDSAnswer.Data[3] = UDSSubService[i];
-											UDSAnswer.Data[4] = UDSSubServiceActionCode[i];		
-											UDSAnswer.Data[5] = 0xFF;					//the uds subservice action code is not surpported
-											UDSAnswer.Data[6] = 0x00;
-											UDSAnswer.Data[7] = 0x00;
-											ret = HAL_Can_Transmit(UDSAnswer);			
-										}
-										
+											UDSNegtiveAnswer(0x06,i,UDSSubServiceActionCode[i]<<8|0xFF);  //重点测试
+										}										
 									}
 									else
 									{
-										UDSAnswer.Id = 0x7A8;
-										UDSAnswer.DLC = 8;
-										UDSAnswer.Data[0] = 0x05;
-										UDSAnswer.Data[1] = 0x7F;
-										UDSAnswer.Data[2] = UDSService[i];
-										UDSAnswer.Data[3] = UDSSubService[i];
-										UDSAnswer.Data[4] = 0xEE;			//the sub service is not surpported
-										UDSAnswer.Data[5] = 0x00;
-										UDSAnswer.Data[6] = 0x00;
-										UDSAnswer.Data[7] = 0x00;
-										ret = HAL_Can_Transmit(UDSAnswer);
+										UDSNegtiveAnswer(0x05,i,0xEE);  //the subservice is not surpported
 									}
 								}
 								else
 								{
-									UDSAnswer.Id = 0x7A8;
-									UDSAnswer.DLC = 8;
-									UDSAnswer.Data[0] = 0x05;
-									UDSAnswer.Data[1] = 0x7F;
-									UDSAnswer.Data[2] = UDSService[i];
-									UDSAnswer.Data[3] = UDSSubService[i];
-									UDSAnswer.Data[4] = 0xFF;				//the dialog mode error
-									UDSAnswer.Data[5] = 0x00;
-									UDSAnswer.Data[6] = 0x00;
-									UDSAnswer.Data[7] = 0x00;
-									ret = HAL_Can_Transmit(UDSAnswer);
-
+									UDSNegtiveAnswer(0x05,i,0xFF);  //the survie is not surpported in current dialog mode
 								}
 								break;
-							case 0x34:  //ask for download update data
+							case 0x34:  //prepare for some one process
 								if(UDSDialogMode == 3)
 								{
-									if(UDSSubService[i] == 0x0F)
-									{
-										//ask for download the update data
-											boolRet = UDSAskforDownLoadData();
-											//if ok
-											
-											UDSAnswer.Id = 0x7A8;
-											UDSAnswer.DLC = 8;
-											UDSAnswer.Data[0] = 0x05;
-											UDSAnswer.Data[1] = 0x78;
-											UDSAnswer.Data[2] = UDSService[i];
-											UDSAnswer.Data[3] = UDSSubService[i];
-											UDSAnswer.Data[4] = (UINT8)boolRet;  // true:01, false: 00			
-											UDSAnswer.Data[5] = 0x00;
-											UDSAnswer.Data[6] = 0x00;
-											UDSAnswer.Data[7] = 0x00;
-											ret = HAL_Can_Transmit(UDSAnswer);
+									if(UDSSubService[i] == 0x0F)  //ask for download update data
+									{									
+										boolRet = UDSAskforDownLoadData();
+										//if ok											
+										UDSPositiveAnswer(0x05,i,(UINT32)boolRet);
 									}
 									else
 									{
-										UDSAnswer.Id = 0x7A8;
-										UDSAnswer.DLC = 8;
-										UDSAnswer.Data[0] = 0x05;
-										UDSAnswer.Data[1] = 0x7F;
-										UDSAnswer.Data[2] = UDSService[i];
-										UDSAnswer.Data[3] = UDSSubService[i];
-										UDSAnswer.Data[4] = 0xEE;			//the sub service is not surported
-										UDSAnswer.Data[5] = 0x00;
-										UDSAnswer.Data[6] = 0x00;
-										UDSAnswer.Data[7] = 0x00;
-										ret = HAL_Can_Transmit(UDSAnswer);
+										UDSNegtiveAnswer(0x05,i,0xEE);
 									}
 								}
 								else
 								{
-									UDSAnswer.Id = 0x7A8;
-									UDSAnswer.DLC = 8;
-									UDSAnswer.Data[0] = 0x05;
-									UDSAnswer.Data[1] = 0x7F;
-									UDSAnswer.Data[2] = UDSService[i];
-									UDSAnswer.Data[3] = UDSSubService[i];
-									UDSAnswer.Data[4] = 0xFF;				//the dialog mode error
-									UDSAnswer.Data[5] = 0x00;
-									UDSAnswer.Data[6] = 0x00;
-									UDSAnswer.Data[7] = 0x00;
-									ret = HAL_Can_Transmit(UDSAnswer);
-
+									UDSNegtiveAnswer(0x05,i,0xFF);
 								}
 								break;
 							case 0x36:  //download the update data
-								if(UDSSubService[i] == 0x0F)
+								if(UDSDialogMode == 3)
 								{
-									counter++;  //记录报文数量
-									
-									tempSliceCounter =  (CanRxMsg[i].Data[2]<<8)|(CanRxMsg[i].Data[3]);
-									printf("tempSliceCounter = %x\n",tempSliceCounter);
-									// updateDifferDataByteLen
-									if(tempSliceCounter < (updateDifferDataPackageCounter+1)*25 && tempSliceCounter>=updateDifferDataPackageCounter*25)
+									if(downloadReady == TRUE)
 									{
-										updateDifferDataSliceCounter = tempSliceCounter % 25;
-										for(j=0;j<4;j++)
-										{
-											updateDataBuffer[updateDifferDataSliceCounter*4+j] = CanRxMsg[i].Data[4+j];
-										}								
-										
-										if(updateDifferDataSliceCounter>0)
-										{
-											sliceCounterFlag = sliceCounterFlag |(0x01<<updateDifferDataSliceCounter);
-										}
-										else
-										{
-											sliceCounterFlag = sliceCounterFlag |0x01;
-										}
-										
-										printf("sliceCounterFlag = %x\n",sliceCounterFlag);
-										
-										
-									}
-
-									if(counter == 25)
-									{
-										counter = 0;
-										
-										if(sliceCounterFlag == 0x1FFFFFF)
+										counter++;  //记录报文数量									
+										tempSliceCounter =  (CanRxMsg[i].Data[1]<<16)|(CanRxMsg[i].Data[2]<<8)|(CanRxMsg[i].Data[3]);
+									/*
+									#ifdef USING_PRINTF
+										printf("tempSliceCounter = %x\n",tempSliceCounter);
+									#endif
+									*/										
+										if(tempSliceCounter < (updateDifferDataPackageCounter+1)*25 && tempSliceCounter>=updateDifferDataPackageCounter*25)
 										{
+											updateDifferDataSliceCounter = tempSliceCounter % 25;
+											for(j=0;j<4;j++)
+											{
+												updateDataBuffer[updateDifferDataSliceCounter*4+j] = CanRxMsg[i].Data[4+j];
+											}								
 											
-											UDSAnswer.Id = 0x7A8;
-											UDSAnswer.DLC = 8;
-											UDSAnswer.Data[0] = 0x06;
-											UDSAnswer.Data[1] = 0x78;
-											UDSAnswer.Data[2] = UDSService[i];
-											UDSAnswer.Data[3] = UDSSubService[i];
-											UDSAnswer.Data[4] = tempSliceCounter>>8;				
-											UDSAnswer.Data[5] = tempSliceCounter;
-											UDSAnswer.Data[6] = 0x00;
-											UDSAnswer.Data[7] = 0x00;
-											ret = HAL_Can_Transmit(UDSAnswer);											
-											
-											sliceCounterFlag = 0x0;
-											updateDifferDataPackageCounter++;
-											printf("updateDifferDataPackageCounter = %d\n",updateDifferDataPackageCounter);
-
-											printf("updateDataBuffer\n");
-											for(k=0;k<100;k++)
+											if(updateDifferDataSliceCounter>0)
 											{
-												printf("%x  ",updateDataBuffer[k]);
+												sliceCounterFlag = sliceCounterFlag |(0x01<<updateDifferDataSliceCounter);
 											}
-											//BSP_QSPI_Write_Safe(updateDataBuffer,FLASH_FOTA_REGION_START+(updateDifferDataPackageCounter-1)*100,100);
-											printf("startAddr = 0x%x,endAddr = 0x%x\n",
-												FLASH_FOTA_REGION_START+(updateDifferDataPackageCounter-1)*100,FLASH_FOTA_REGION_START+updateDifferDataPackageCounter*100-1);
-											memset(updateDataBuffer,0, 100);
+											else
+											{
+												sliceCounterFlag = sliceCounterFlag |0x01;
+											}
+											/*
+										#ifdef USING_PRINTF
+											printf("sliceCounterFlag = %x  counter=%d\n",sliceCounterFlag,counter);
+										#endif
+										*/											
 										}
-										else
+										if(counter == 25)
 										{
-											UDSAnswer.Id = 0x7A8;
-											UDSAnswer.DLC = 8;
-											UDSAnswer.Data[0] = 0x08;
-											UDSAnswer.Data[1] = 0x7F;
-											UDSAnswer.Data[2] = UDSService[i];
-											UDSAnswer.Data[3] = UDSSubService[i];
-											UDSAnswer.Data[4] = sliceCounterFlag>>24;				
-											UDSAnswer.Data[5] = sliceCounterFlag>>16;
-											UDSAnswer.Data[6] = sliceCounterFlag>>8;
-											UDSAnswer.Data[7] = sliceCounterFlag;
-											ret = HAL_Can_Transmit(UDSAnswer);
+											counter = 0;
 											
-											printf("updateDataBuffer\n");
-											for(k=0;k<100;k++)
+											if(sliceCounterFlag == 0x1FFFFFF)  //received all the 25 message of current package
+											{
+												sliceCounterFlag = 0x0;
+												updateDifferDataPackageCounter++;
+											/*
+											#ifdef USING_PRINTF
+												printf("updateDifferDataPackageCounter = %d\n",updateDifferDataPackageCounter);
+
+												printf("updateDataBuffer\n");
+												for(k=0;k<100;k++)
+												{
+													printf("%x  ",updateDataBuffer[k]);
+												}
+												printf("\n");
+												
+												printf("startAddr = 0x%x,endAddr = 0x%x\n",
+													FLASH_FOTA_REGION_START+(updateDifferDataPackageCounter-1)*100,FLASH_FOTA_REGION_START+updateDifferDataPackageCounter*100-1);
+											#endif
+											*/			
+												//write the buffer(100 byte) to flash
+												ret = (uint8_t)BSP_QSPI_Write_Safe(updateDataBuffer,FLASH_FOTA_REGION_START+(updateDifferDataPackageCounter-1)*100,100);
+												errorCount = 0;
+												while(ret != QSPI_OK && errorCount<3)  //try to write most 3 times
+												{
+													errorCount++;
+													BSP_QSPI_Erase_Safe(FLASH_FOTA_REGION_START+(updateDifferDataPackageCounter-1)*100,100);
+													ret = (uint8_t)BSP_QSPI_Write_Safe(updateDataBuffer,FLASH_FOTA_REGION_START+(updateDifferDataPackageCounter-1)*100,100);
+												}
+												if(ret == QSPI_OK)
+												{
+													UDSPositiveAnswer(0x08,i,tempSliceCounter);												
+												}
+												else
+												{
+													UDSNegtiveAnswer(0x08,i,0xFFFFFFFF);  //failed to write this package to flash
+												}
+												memset(updateDataBuffer,0, 100);
+												sliceCounterFlag = 0x0;
+												
+											}
+											else
 											{
-												printf("%x  ",updateDataBuffer[k]);
+												//received 25 messages, but lose one or more messages
+												//in this case, updater will try to send the package again(most 4 times)
+												UDSNegtiveAnswer(0x08,i,sliceCounterFlag);  
+												//sliceCounterFlag = 0x0;
+												/*
+											#ifdef USING_PRINTF
+												printf("updateDifferDataPackageCounter = %d\n",updateDifferDataPackageCounter+1);
+												printf("updateDataBuffer:\n");
+												for(k=0;k<100;k++)
+												{
+													printf("%x  ",updateDataBuffer[k]);
+												}
+												printf("\n");
+											#endif
+											*/
+												//memset(updateDataBuffer,0, 100);
 											}
 										}
 									}
-									
-									
+									else
+									{
+										UDSNegtiveAnswer(0x05,i,0xEF); //the download process is not ready
+									}
 								}
 								else
 								{
-									UDSAnswer.Id = 0x7A8;
-									UDSAnswer.DLC = 8;
-									UDSAnswer.Data[0] = 0x05;
-									UDSAnswer.Data[1] = 0x7F;
-									UDSAnswer.Data[2] = UDSService[i];
-									UDSAnswer.Data[3] = UDSSubService[i];
-									UDSAnswer.Data[4] = 0xEE;			//the sub service is not surported
-									UDSAnswer.Data[5] = 0x00;
-									UDSAnswer.Data[6] = 0x00;
-									UDSAnswer.Data[7] = 0x00;
-									ret = HAL_Can_Transmit(UDSAnswer);
+									UDSNegtiveAnswer(0x05,i,0xFF);  //the service is not surpported in current dialog mode
 								}
 								break;
-							case 0x37:  // exit the download
-								 if(UDSSubService[i] == 0x0F)
-								 {
-									//
-								 }
-								 else
-								 {
-								 	UDSAnswer.Id = 0x7A8;
-									UDSAnswer.DLC = 8;
-									UDSAnswer.Data[0] = 0x05;
-									UDSAnswer.Data[1] = 0x7F;
-									UDSAnswer.Data[2] = UDSService[i];
-									UDSAnswer.Data[3] = UDSSubService[i];
-									UDSAnswer.Data[4] = 0xEE;			//the sub service is not surported
-									UDSAnswer.Data[5] = 0x00;
-									UDSAnswer.Data[6] = 0x00;
-									UDSAnswer.Data[7] = 0x00;
-									ret = HAL_Can_Transmit(UDSAnswer);
+							case 0x37:  //exit some one process
+								if(UDSDialogMode == 3)
+								{
+									if(UDSSubService[i] == 0x0F)  // exit the download 
+									 {
+										UDSPositiveAnswer(0x04,i,0x00);
+										downloadReady = FALSE;
+									 }
+									 else
+									 {
+										UDSNegtiveAnswer(0x05,i,0xEE);
+									}
+								}
+								else
+								{
+									UDSNegtiveAnswer(0x05,i,0xFF);
 								}
-								 
 								break;
-							default:
-								UDSAnswer.Id = 0x7A8;
-								UDSAnswer.DLC = 8;
-								UDSAnswer.Data[0] = 0x04;
-								UDSAnswer.Data[1] = 0x7F;
-								UDSAnswer.Data[2] = UDSService[i];	//the serviece is not surpoted
-								UDSAnswer.Data[3] = 0xFF;
-								UDSAnswer.Data[4] = 0x00;				
-								UDSAnswer.Data[5] = 0x00;
-								UDSAnswer.Data[6] = 0x00;
-								UDSAnswer.Data[7] = 0x00;
-								ret = HAL_Can_Transmit(UDSAnswer);
+							default:								
+								UDSNegtiveAnswer(0x04,i,0xFF);  //the service is not surpported
 								break;
 								
 						}
@@ -814,22 +506,125 @@ void UDS_Service()
 
 }
 
-void UDS_CAN_Update()
+UINT8 UDSPositiveAnswer(UINT8 answerLen,UINT8 messageIndex,UINT32 posCode)
 {
-	
-	
-
+	CAN_Msg_Type UDSAnswer;
+	UINT8 ret;
+	UDSAnswer.Id = 0x7A8;
+	UDSAnswer.DLC = 8;
+	UDSAnswer.Data[0] = answerLen;
+	UDSAnswer.Data[1] = 0x78;
+	UDSAnswer.Data[2] = UDSService[messageIndex];
+	UDSAnswer.Data[3] = UDSSubService[messageIndex];
+	if(answerLen == 4)
+	{
+		UDSAnswer.Data[4] = 0x00;
+		UDSAnswer.Data[5] = 0x00;
+		UDSAnswer.Data[6] = 0x00;
+		UDSAnswer.Data[7] = 0x00;
+	}
+	else if(answerLen==5)
+	{	UDSAnswer.Data[4] = posCode;
+		UDSAnswer.Data[5] = 0x00;
+		UDSAnswer.Data[6] = 0x00;
+		UDSAnswer.Data[7] = 0x00;
+	}
+	else if(answerLen==6)
+	{
+		UDSAnswer.Data[4] = posCode>>8;
+		UDSAnswer.Data[5] = posCode;
+		UDSAnswer.Data[6] = 0x00;
+		UDSAnswer.Data[7] = 0x00;
+	}
+	else if(answerLen==7)
+	{
+		UDSAnswer.Data[4] = posCode>>16;
+		UDSAnswer.Data[5] = posCode>>8;
+		UDSAnswer.Data[6] = posCode;
+		UDSAnswer.Data[7] = 0x00;
+	}
+	else if(answerLen==8)
+	{
+		UDSAnswer.Data[4] = posCode>>24;
+		UDSAnswer.Data[5] = posCode>>16;
+		UDSAnswer.Data[6] = posCode>>8;
+		UDSAnswer.Data[7] = posCode;
+	}
+	ret = HAL_Can_Transmit(UDSAnswer);
+	return ret;
+}
 
+UINT8 UDSNegtiveAnswer(UINT8 answerLen,UINT8 messageIndex, UINT32 negCode)
+{
+	CAN_Msg_Type UDSAnswer;
+	UINT8 ret;
+	UDSAnswer.Id = 0x7A8;
+	UDSAnswer.DLC = 8;
+	UDSAnswer.Data[0] = answerLen;
+	UDSAnswer.Data[1] = 0x7F;
+	UDSAnswer.Data[2] = UDSService[messageIndex];
+	UDSAnswer.Data[3] = UDSSubService[messageIndex];
+	if(answerLen==4)
+	{
+		UDSAnswer.Data[3] = negCode;
+		UDSAnswer.Data[4] = 0x00;
+		UDSAnswer.Data[5] = 0x00;
+		UDSAnswer.Data[6] = 0x00;
+		UDSAnswer.Data[7] = 0x00;
+	}
+	if(answerLen==5)
+	{	UDSAnswer.Data[4] = negCode;
+		UDSAnswer.Data[5] = 0x00;
+		UDSAnswer.Data[6] = 0x00;
+		UDSAnswer.Data[7] = 0x00;
+	}
+	else if(answerLen==6)
+	{
+		UDSAnswer.Data[4] = negCode>>8;
+		UDSAnswer.Data[5] = negCode;
+		UDSAnswer.Data[6] = 0x00;
+		UDSAnswer.Data[7] = 0x00;
+	}
+	else if(answerLen==7)
+	{
+		UDSAnswer.Data[4] = negCode>>16;
+		UDSAnswer.Data[5] = negCode>>8;
+		UDSAnswer.Data[6] = negCode;
+		UDSAnswer.Data[7] = 0x00;
+	}
+	else if(answerLen==8)
+	{
+		UDSAnswer.Data[4] = negCode>>24;
+		UDSAnswer.Data[5] = negCode>>16;
+		UDSAnswer.Data[6] = negCode>>8;
+		UDSAnswer.Data[7] = negCode;
+	}
+	ret = HAL_Can_Transmit(UDSAnswer);
+	return ret;
 }
 
-BOOL UDSClearFotaDownloadRegion()
+
+
+BOOL UDSAskforDownLoadData()
 {
+	updateDifferDataPackageCounter = 0;
+	downloadReady = true;
 	
 	return TRUE;
 }
 
-BOOL UDSAskforDownLoadData()
+BOOL UDSClearFotaDownloadRegion()
 {
+	UINT8 ret = FALSE;
+	ret = BSP_QSPI_Erase_Safe(FLASH_FOTA_REGION_START, 0x78000); //512k-32k = 480k -> 0x75300
+	
+	if(ret == QSPI_OK)
+	{
+		return TRUE;
+	}
+	else
+	{
+		return FALSE;
+	}
+}
 
-	return TRUE;
-}

+ 412 - 61
src/UartTask.c

@@ -29,6 +29,7 @@
 #include "MainTask.h"
 #include "app.h"
 #include "numeric.h"
+
 //全局变量输入区
 extern UINT32 Timer_count;
 extern volatile bool Sleep_flag; 
@@ -57,7 +58,12 @@ void USART_callback(uint32_t event);
 UINT8 Uart_Transmit_func(UINT8* Uart_Read_Msg,UINT8* Uart_Recv_Buffer);
 UINT8 Uart_WriteCmd_func(Uart_Write_Data_Type UartWriteData);
 UINT16 crc_chk(UINT8* data, UINT8 length);
+void battSOCDisplay(void);
+void battErrorStateDisplay(void);
+void battLockStateDisplay(UINT8 lockState);
+void SP_BMS_Update_Service(void);
 BOOL BattHeaterSwitch(UINT8* heaterSwitch);
+
 //Uart线程任务区
 static void UartTask(void* arg)
 {
@@ -72,6 +78,8 @@ static void UartTask(void* arg)
     UINT16  Reg_Num = 0;
     UINT16  Uart_Uds_LEN;
     UINT16  Uart_Recv_LEN;
+	UINT32  currentTimerCount=0;
+	BOOL uartReadSuccessFlag = false;
     Uart_Read_Msg_Type Uart_Read_Msg;
     Uart_Write_Data_Type UartWriteData; //Uart控制命令
     if(UartWriteCmdHandle == NULL)//Uart控制命令传输指针
@@ -121,6 +129,24 @@ static void UartTask(void* arg)
                     else
                         PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_READ);
                 }
+				if(UartReadMsg.Header[2]>0)
+				{
+					uartReadSuccessFlag = true;
+				}
+				
+				if(Timer_count-currentTimerCount >= 1)
+				{
+					if(AppNVMData.isBattLocked != 0)
+					{
+						battLockStateDisplay(TRUE);
+					}
+					if(uartReadSuccessFlag)
+					{
+						battSOCDisplay();
+						battErrorStateDisplay();
+					}
+				}
+				currentTimerCount = Timer_count;
                 break;
             }
             case PROCESS_UART_STATE_READ:
@@ -379,29 +405,30 @@ UINT8 Uart_Transmit_func(UINT8* Uart_Read_Msg,UINT8* Uart_Recv_Buffer)
         return 0;
     }
 }
-//
+
+
 /**
   \fn   BOOL BattHeaterSwitch(UINT8* heaterSwitch)
   \param[in]  (UINT8*) heaterSwitch: the heater switch state
   \brief    according to the current switch state and all the cell temp, it will turn on/off the switch
   \return   (BOOL) isNeedtoSwitch: true: need to send cmd to turn on/off the switch
-                                                                   false: do not need to do anything
+  								 false: do not need to do anything
 */
 BOOL BattHeaterSwitch(UINT8* heaterSwitch)
 {
-        BOOL isNeedtoSwitch = FALSE;
-        
-        UINT8 battCellTemp[BATT_TEMP_NUM];
-        UINT8 maxCellTemp,minCellTemp;
-        
-        UINT8 i =0;        
-        UINT8 currentSwitchState = 0;
+	BOOL isNeedtoSwitch = FALSE;
+	
+	UINT8 battCellTemp[BATT_TEMP_NUM];
+	UINT8 maxCellTemp,minCellTemp;
+	
+	UINT8 i =0;	
+	UINT8 currentSwitchState = 0;
 
-        //get the current switch state and the cell temp
-        currentSwitchState = UartReadMsg.data[(0x1C+BATT_CELL_VOL_NUM+(BATT_TEMP_NUM+2))*2+1]&0x01;
-        for(int i=0; i<BATT_TEMP_NUM; i++)
+	//get the current switch state and the cell temp
+	currentSwitchState = UartReadMsg.data[(0x1C+BATT_CELL_VOL_NUM+(BATT_TEMP_NUM+2))*2+1]&0x01;
+	for(int i=0; i<BATT_TEMP_NUM; i++)
     {
-            battCellTemp[i] = UartReadMsg.data[(0x06+BATT_CELL_VOL_NUM+i)*2+1];            
+	    battCellTemp[i] = UartReadMsg.data[(0x06+BATT_CELL_VOL_NUM+i)*2+1];	    
     }
 
         //cal the maxtemp and mintemp
@@ -409,62 +436,386 @@ BOOL BattHeaterSwitch(UINT8* heaterSwitch)
         minCellTemp = battCellTemp[0];
     for(i=1;i<BATT_TEMP_NUM;i++)
     {
-                maxCellTemp = max(maxCellTemp,battCellTemp[i]);
-                minCellTemp = min(minCellTemp,battCellTemp[i]);
+		maxCellTemp = max(maxCellTemp,battCellTemp[i]);
+        minCellTemp = min(minCellTemp,battCellTemp[i]);
     }
 
     
-        if(currentSwitchState==0)         //当前状态为关闭,判断是否应该开启
-        {
-                if(minCellTemp<=5+40 && maxCellTemp<25+40)//温度偏移为40 
-                {
-                        *heaterSwitch = 1;
-                        isNeedtoSwitch = true;
-                }
-        }
-        else                                                  //当前状态为开启,判断是否应该关闭
-        {
-                if(minCellTemp>10+40||maxCellTemp>30+40)
-                {
-                        *heaterSwitch = 0;
-                        isNeedtoSwitch= true;
-                }
-        }
-        return isNeedtoSwitch;
+	if(currentSwitchState==0) 	//当前状态为关闭,判断是否应该开启
+	{
+		if(minCellTemp<=5+40 && maxCellTemp<25+40)//温度偏移为40 
+		{
+			*heaterSwitch = 1;
+			isNeedtoSwitch = true;
+		}
+	}
+	else  						//当前状态为开启,判断是否应该关闭
+	{
+		if(minCellTemp>10+40||maxCellTemp>30+40)
+		{
+			*heaterSwitch = 0;
+			isNeedtoSwitch= true;
+		}
+	}
+	return isNeedtoSwitch;
+}
+
+void battSOCDisplay()
+{
+	static UINT8 workState;
+	static UINT8 currentSoc;
+	static UINT8 lightTimer = 0;
+	UINT8 socLowLEDFlashPeriod = 10;//10*100 = 1000ms
+	UINT8 chargeLEDFlashPeriod = 6;//6*100 = 600ms
+	float dutyRatio = 0.4;
+	UINT8 temp;
+
+	if(AppNVMData.isBattLocked == TRUE)
+	{
+		return;
+	}
+	
+	if(UartReadMsg.Header[2]>0)
+	{
+		temp = UartReadMsg.data[(0x03+BATT_CELL_VOL_NUM)*2+1]&0x03;
+	    workState = ((temp&0x01)<<01)|(temp>>0x01);
+	    currentSoc = UartReadMsg.data[(0x0B+BATT_CELL_VOL_NUM+BATT_TEMP_NUM+2)*2+1];
+	}
+#ifdef USING_PRINTF
+	printf("current SOC = %d\n",currentSoc);
+	printf("work state = %d\n",workState);
+#endif
+
+	lightTimer++;
+	
+	if(workState == 0||workState == 2)  //静置或放电状态
+	{
+		if(currentSoc<=10)
+		{
+			if(lightTimer<(UINT8)(socLowLEDFlashPeriod*dutyRatio))
+			{
+				NetSocDisplay(LED_SOC_0,LED_TURN_ON);
+				NetSocDisplay(LED_SOC_1,LED_TURN_OFF);
+				NetSocDisplay(LED_SOC_2,LED_TURN_OFF);
+				NetSocDisplay(LED_SOC_3,LED_TURN_OFF);	
+			}
+			else if(lightTimer>=(UINT8)(socLowLEDFlashPeriod*dutyRatio) && lightTimer<socLowLEDFlashPeriod)
+			{
+				NetSocDisplay(LED_SOC_0,LED_TURN_OFF);
+				NetSocDisplay(LED_SOC_1,LED_TURN_OFF);
+				NetSocDisplay(LED_SOC_2,LED_TURN_OFF);
+				NetSocDisplay(LED_SOC_3,LED_TURN_OFF);	
+			}
+			else
+			{
+				NetSocDisplay(LED_SOC_0,LED_TURN_OFF);
+				NetSocDisplay(LED_SOC_1,LED_TURN_OFF);
+				NetSocDisplay(LED_SOC_2,LED_TURN_OFF);
+				NetSocDisplay(LED_SOC_3,LED_TURN_OFF);	
+				lightTimer = 0;
+			}			
+		}	
+		else if(currentSoc>10&&currentSoc<=25)
+		{
+			NetSocDisplay(LED_SOC_0,LED_TURN_ON);
+			NetSocDisplay(LED_SOC_1,LED_TURN_OFF);
+			NetSocDisplay(LED_SOC_2,LED_TURN_OFF);
+			NetSocDisplay(LED_SOC_3,LED_TURN_OFF);	
+			lightTimer = 0;
+		}
+		else if(currentSoc>25&&currentSoc<=50)
+		{
+			NetSocDisplay(LED_SOC_0,LED_TURN_ON);
+			NetSocDisplay(LED_SOC_1,LED_TURN_ON);
+			NetSocDisplay(LED_SOC_2,LED_TURN_OFF);
+			NetSocDisplay(LED_SOC_3,LED_TURN_OFF);	
+			lightTimer = 0;
+		}
+		else if(currentSoc>50&&currentSoc<=75)
+		{
+			NetSocDisplay(LED_SOC_0,LED_TURN_ON);
+			NetSocDisplay(LED_SOC_1,LED_TURN_ON);
+			NetSocDisplay(LED_SOC_2,LED_TURN_ON);
+			NetSocDisplay(LED_SOC_3,LED_TURN_OFF);	
+			lightTimer = 0;
+		}
+		else if(currentSoc>75&&currentSoc<=100)
+		{
+			NetSocDisplay(LED_SOC_0,LED_TURN_ON);
+			NetSocDisplay(LED_SOC_1,LED_TURN_ON);
+			NetSocDisplay(LED_SOC_2,LED_TURN_ON);
+			NetSocDisplay(LED_SOC_3,LED_TURN_ON);
+			lightTimer = 0;
+		}
+	}
+	else if(workState == 1)
+	{
+		if(currentSoc<=25)
+		{
+			if(lightTimer<(UINT8)(chargeLEDFlashPeriod*dutyRatio))
+			{
+				NetSocDisplay(LED_SOC_0,LED_TURN_ON);
+				NetSocDisplay(LED_SOC_1,LED_TURN_OFF);
+				NetSocDisplay(LED_SOC_2,LED_TURN_OFF);
+				NetSocDisplay(LED_SOC_3,LED_TURN_OFF);	
+			}
+			else if(lightTimer>=(UINT8)(chargeLEDFlashPeriod*dutyRatio) && lightTimer<chargeLEDFlashPeriod)
+			{
+				NetSocDisplay(LED_SOC_0,LED_TURN_OFF);
+				NetSocDisplay(LED_SOC_1,LED_TURN_OFF);
+				NetSocDisplay(LED_SOC_2,LED_TURN_OFF);
+				NetSocDisplay(LED_SOC_3,LED_TURN_OFF);	
+			}
+			else
+			{
+				NetSocDisplay(LED_SOC_0,LED_TURN_OFF);
+				NetSocDisplay(LED_SOC_1,LED_TURN_OFF);
+				NetSocDisplay(LED_SOC_2,LED_TURN_OFF);
+				NetSocDisplay(LED_SOC_3,LED_TURN_OFF);	
+				lightTimer = 0;
+			}			
+		}	
+		else if(currentSoc>25&&currentSoc<=50)
+		{
+			if(lightTimer<(UINT8)(chargeLEDFlashPeriod*dutyRatio))
+			{
+				NetSocDisplay(LED_SOC_0,LED_TURN_ON);
+				NetSocDisplay(LED_SOC_1,LED_TURN_ON);
+				NetSocDisplay(LED_SOC_2,LED_TURN_OFF);
+				NetSocDisplay(LED_SOC_3,LED_TURN_OFF);	
+			}
+			else if(lightTimer>=(UINT8)(chargeLEDFlashPeriod*dutyRatio) && lightTimer<chargeLEDFlashPeriod)
+			{
+				NetSocDisplay(LED_SOC_0,LED_TURN_ON);
+				NetSocDisplay(LED_SOC_1,LED_TURN_OFF);
+				NetSocDisplay(LED_SOC_2,LED_TURN_OFF);
+				NetSocDisplay(LED_SOC_3,LED_TURN_OFF);	
+			}
+			else
+			{
+				NetSocDisplay(LED_SOC_0,LED_TURN_ON);
+				NetSocDisplay(LED_SOC_1,LED_TURN_OFF);
+				NetSocDisplay(LED_SOC_2,LED_TURN_OFF);
+				NetSocDisplay(LED_SOC_3,LED_TURN_OFF);
+				lightTimer = 0;
+			}
+		}
+		else if(currentSoc>50&&currentSoc<=75)
+		{
+			if(lightTimer<(UINT8)(chargeLEDFlashPeriod*dutyRatio))
+			{
+				NetSocDisplay(LED_SOC_0,LED_TURN_ON);
+				NetSocDisplay(LED_SOC_1,LED_TURN_ON);
+				NetSocDisplay(LED_SOC_2,LED_TURN_ON);
+				NetSocDisplay(LED_SOC_3,LED_TURN_OFF);	
+			}
+			else if(lightTimer>=(UINT8)(chargeLEDFlashPeriod*dutyRatio) && lightTimer<chargeLEDFlashPeriod)
+			{
+				NetSocDisplay(LED_SOC_0,LED_TURN_ON);
+				NetSocDisplay(LED_SOC_1,LED_TURN_ON);
+				NetSocDisplay(LED_SOC_2,LED_TURN_OFF);
+				NetSocDisplay(LED_SOC_3,LED_TURN_OFF);	
+			}
+			else
+			{
+				NetSocDisplay(LED_SOC_0,LED_TURN_ON);
+				NetSocDisplay(LED_SOC_1,LED_TURN_ON);
+				NetSocDisplay(LED_SOC_2,LED_TURN_OFF);
+				NetSocDisplay(LED_SOC_3,LED_TURN_OFF);
+				lightTimer = 0;
+			}
+
+		}
+		else if(currentSoc>75&&currentSoc<=97)
+		{
+			if(lightTimer<(UINT8)(chargeLEDFlashPeriod*dutyRatio))
+			{
+				NetSocDisplay(LED_SOC_0,LED_TURN_ON);
+				NetSocDisplay(LED_SOC_1,LED_TURN_ON);
+				NetSocDisplay(LED_SOC_2,LED_TURN_ON);
+				NetSocDisplay(LED_SOC_3,LED_TURN_ON);	
+			}
+			else if(lightTimer>=(UINT8)(chargeLEDFlashPeriod*dutyRatio) && lightTimer<chargeLEDFlashPeriod)
+			{
+				NetSocDisplay(LED_SOC_0,LED_TURN_ON);
+				NetSocDisplay(LED_SOC_1,LED_TURN_ON);
+				NetSocDisplay(LED_SOC_2,LED_TURN_ON);
+				NetSocDisplay(LED_SOC_3,LED_TURN_OFF);	
+			}
+			else
+			{
+				NetSocDisplay(LED_SOC_0,LED_TURN_ON);
+				NetSocDisplay(LED_SOC_1,LED_TURN_ON);
+				NetSocDisplay(LED_SOC_2,LED_TURN_ON);
+				NetSocDisplay(LED_SOC_3,LED_TURN_OFF);	
+				lightTimer = 0;
+			}
+		}
+		else if(currentSoc>97&&currentSoc<=100)
+		{
+			NetSocDisplay(LED_SOC_0,LED_TURN_ON);
+			NetSocDisplay(LED_SOC_1,LED_TURN_ON);
+			NetSocDisplay(LED_SOC_2,LED_TURN_ON);
+			NetSocDisplay(LED_SOC_3,LED_TURN_ON);	
+		}		
+	}
+	
+}
+void battErrorStateDisplay()
+{
+	static UINT32 errorState;	
+	static UINT8  errorLightTimer = 0;
+	//static UINT32 currentTimerCount=0;
+	
+	UINT8  errorLEDFlashPeriod = 6;//600ms
+	float errorDutyRatio = 0.4;
+
+	if(AppNVMData.isBattLocked == TRUE)
+	{
+		return;
+	}
+	
+	if(UartReadMsg.Header[2]>0)
+	{
+		MEMCPY(&errorState,&(UartReadMsg.data[(0x09+BATT_CELL_VOL_NUM+BATT_TEMP_NUM+2)*2]),4); 
+	}
+	
+	errorLightTimer++;
+	
+	//errorState = testErrorState;
+#ifdef USING_PRINTF
+	printf("error state = %d\n",errorState);
+#endif
+	if(errorState != 0)
+	{
+		if(errorLightTimer<(UINT8)(errorLEDFlashPeriod*errorDutyRatio)) 
+		{				
+			FaultDisplay(LED_TURN_ON);
+			
+		}
+		else if(errorLightTimer>=(UINT8)(errorLEDFlashPeriod*errorDutyRatio) && errorLightTimer<errorLEDFlashPeriod)
+		{
+			FaultDisplay(LED_TURN_OFF);
+			
+		}
+		else
+		{
+			FaultDisplay(LED_TURN_OFF);
+			errorLightTimer = 0;			
+		}
+		
+	}
+	else
+	{
+		FaultDisplay(LED_TURN_OFF);
+		errorLightTimer = 0;
+	}	
 }
 
+
+
+void battLockStateDisplay(UINT8 lockState)
+{
+	static UINT8 currentState = 0;
+	static UINT8  errorLightTimer = 0;
+	//static UINT32 currentTimerCount=0;
+	
+	UINT8  errorLEDFlashPeriod = 10;//1000ms
+	float errorDutyRatio = 0.4;
+	
+	printf("lockState = %d\ncurrent State = %d\n",lockState,currentState);
+	if(lockState==0)//no error
+	{
+		if(currentState!=lockState)
+		{
+			NetSocDisplay(LED_SOC_0,LED_TURN_OFF);
+			NetSocDisplay(LED_SOC_1,LED_TURN_OFF);
+			NetSocDisplay(LED_SOC_2,LED_TURN_OFF);
+			NetSocDisplay(LED_SOC_3,LED_TURN_OFF);
+			FaultDisplay(LED_TURN_OFF);
+			currentState = lockState;
+			errorLightTimer = 0;
+		}
+		else
+		{
+			return;
+		}
+
+	}
+	else  // error occurred, errorState = 1
+	{
+		if(errorLightTimer<(UINT8)(errorLEDFlashPeriod*errorDutyRatio)) 
+		{	
+			NetSocDisplay(LED_SOC_0,LED_TURN_ON);
+			NetSocDisplay(LED_SOC_1,LED_TURN_ON);
+			NetSocDisplay(LED_SOC_2,LED_TURN_ON);
+			NetSocDisplay(LED_SOC_3,LED_TURN_ON);
+			FaultDisplay(LED_TURN_ON);
+			
+		}
+		else if(errorLightTimer>=(UINT8)(errorLEDFlashPeriod*errorDutyRatio) && errorLightTimer<errorLEDFlashPeriod)
+		{
+			NetSocDisplay(LED_SOC_0,LED_TURN_OFF);
+			NetSocDisplay(LED_SOC_1,LED_TURN_OFF);
+			NetSocDisplay(LED_SOC_2,LED_TURN_OFF);
+			NetSocDisplay(LED_SOC_3,LED_TURN_OFF);
+			FaultDisplay(LED_TURN_OFF);			
+		}
+		else
+		{
+			NetSocDisplay(LED_SOC_0,LED_TURN_OFF);
+			NetSocDisplay(LED_SOC_1,LED_TURN_OFF);
+			NetSocDisplay(LED_SOC_2,LED_TURN_OFF);
+			NetSocDisplay(LED_SOC_3,LED_TURN_OFF);
+			FaultDisplay(LED_TURN_OFF);
+			errorLightTimer = 0;			
+		}		
+	}	
+	errorLightTimer++;
+}
+  
 UINT8  decryptionAlgorithm (UINT16 cipherText)
 {
-        UINT16 plainText = 1;        
-        UINT16 publicKeyD = 43;
-        UINT16 publicKeyN = 10961;
-        cipherText = cipherText % publicKeyN;
-        while(publicKeyD >0)
-        {
-                if(publicKeyD % 2 ==1)
-                {                        
-                        plainText = plainText * cipherText % publicKeyN;
-                }
-                publicKeyD = publicKeyD/2;                
-                cipherText = (cipherText * cipherText) % publicKeyN;
-        }
-        return (UINT8)plainText;
+	UINT16 plainText = 1;	
+	UINT16 publicKeyD = 43;
+	UINT16 publicKeyN = 10961;
+	cipherText = cipherText % publicKeyN;
+	while(publicKeyD >0)
+	{
+		if(publicKeyD % 2 ==1)
+		{			
+			plainText = plainText * cipherText % publicKeyN;
+		}
+		publicKeyD = publicKeyD/2;		
+		cipherText = (cipherText * cipherText) % publicKeyN;
+	}
+	return (UINT8)plainText;
 }
 
 UINT16  encryptionAlgorithm (UINT8 plainText)
 {
-        UINT16 cipherText = 1;        
-        UINT16 privateKeyE = 37507;
-        UINT16 privateKeyN = 10961;
-        plainText = plainText % privateKeyN;
-        while(privateKeyE >0)
-        {
-                if(privateKeyE % 2 ==1)
-                {                        
-                        cipherText = cipherText * plainText % privateKeyN;
-                }
-                privateKeyE = privateKeyE/2;                
-                cipherText = (plainText * plainText) % privateKeyN;
-        }
-        return cipherText;
-}
+	UINT16 cipherText = 1;	
+	UINT16 privateKeyE = 37507;
+	UINT16 privateKeyN = 10961;
+	plainText = plainText % privateKeyN;
+	while(privateKeyE >0)
+	{
+		if(privateKeyE % 2 ==1)
+		{			
+			cipherText = cipherText * plainText % privateKeyN;
+		}
+		privateKeyE = privateKeyE/2;		
+		cipherText = (plainText * plainText) % privateKeyN;
+	}
+	return cipherText;
+}
+UINT8 SP_BMS_Update_CheckSUM(UINT8* pSendData,UINT8 len)
+{
+	UINT8 ret = 0;
+	UINT8 i=0;
+	for(i=0;i<len;i++)
+	{
+		ret +=*(pSendData+i);
+	}
+	return ret&0xFF;
+}

+ 4 - 0
src/app.c

@@ -30,6 +30,10 @@
 #include "TcpTask.h"
 void appInit(void *arg)
 {
+    #ifdef USING_PRINTF	
+    	printf("%s[%d]\r\n",__FUNCTION__, __LINE__);
+    #endif
+    AdcTaskInit();
     MainTaskInit(arg);
     osDelay(1000);
     UartTaskInit(arg);

+ 2 - 1
src/bsp_custom.c

@@ -138,8 +138,9 @@ void BSP_CustomInit(void)
 {
     extern void mpu_init(void);
     mpu_init();
-
+	
     GPR_SetUartClk();
+    relayConfigInit();
 
 #if LOW_POWER_AT_TEST
     slpManRegisterUsrSlpDepthCb(CheckUsrdefSlpStatus);

+ 70 - 12
src/hal_module_adapter.c

@@ -820,7 +820,7 @@ void HAL_Can_Init(Can_InitType param)
 	
 	}
 
-	CAN_WriteReg(BFPCTRL,0x0F);//zhengchao20210304  add
+	//CAN_WriteReg(BFPCTRL,0x0F);//zhengchao20210304  add
 	CAN_WriteReg(CANINTE,0x43); //zhengchao20210304  0x43 -> 0x03
 	CAN_WriteReg(CANINTF,0x00);
 	
@@ -984,13 +984,10 @@ INT8  HAL_Can_Transmit(CAN_Msg_Type Can_TxMsg)
 	if((TXB0CTRLvalue&0x20)||(TXB1CTRLvalue&0x20)||(TXB2CTRLvalue&0x20)||(CANINTFValue&0x80))
 	{
 		ret = -1;
-		printf("TXB0CTRLvalue = 0x%x\n",TXB0CTRLvalue);
-		printf("TXB1CTRLvalue = 0x%x\n",TXB1CTRLvalue);
-		printf("TXB2CTRLvalue = 0x%x\n",TXB2CTRLvalue);
-		printf("CANINTFValue = 0x%x\n",CANINTFValue);
+		
 	}
 	
-	printf("ret = %d\n",ret);
+	
 	return ret;
 }
 
@@ -1010,8 +1007,10 @@ void  HAL_Can_Receive(CAN_Msg_Type* CanRxMsgBuffer)
 	//static UINT16 counterBuff0,counterBuff1 = 0;
 	
 	CAN_ReadReg(CANINTF,1,&temp);
-//	printf("CANINTF = 0x%x\n",temp);
-//	USARTdrv->Send(&temp,1);
+#ifdef USING_PRINTF
+	//printf("CANINTF = 0x%x\n",temp);
+	//USARTdrv->Send(&temp,1);
+#endif
 	if(temp & 0x01)  //Rx Buffer 0
 	{	
 		/*get the id information*/
@@ -1039,7 +1038,14 @@ void  HAL_Can_Receive(CAN_Msg_Type* CanRxMsgBuffer)
 			CAN_ReadReg(RXB0D0+i,1,&((CanRxMsgBuffer[0]).Data[i]));
 			i++;
 		}	
-
+	#ifdef USING_PRINTF		
+		printf("buffer0 ID = %x\n",CanRxMsgBuffer[0].Id);
+		for(i=0;i<8;i++)
+		{
+			printf("%x ",CanRxMsgBuffer[0].Data[i]);
+		}
+		printf("\n");
+	#endif
 		
 	}
 	if(temp & 0x02)  //RX Buffer 1
@@ -1068,7 +1074,17 @@ void  HAL_Can_Receive(CAN_Msg_Type* CanRxMsgBuffer)
 		{	
 			CAN_ReadReg(RXB1D0+i,1,&((CanRxMsgBuffer[1]).Data[i]));
 			i++;			
-		}		
+		}
+
+	#ifdef USING_PRINTF		
+		printf("buffer1 ID = %x\n",CanRxMsgBuffer[1].Id);
+		for(i=0;i<8;i++)
+		{
+			printf("%x ",CanRxMsgBuffer[1].Data[i]);
+		}
+		printf("\n");
+	#endif
+		
 	}
 	CAN_WriteReg(CANINTF,0);
 //	CAN_ReadReg(CANINTF,1,&temp);
@@ -1869,7 +1885,7 @@ static void AdcProcess(void* arg)
 	        ADC_GetDefaultConfig(&adcConfig);
 	        adcConfig.channelConfig.thermalInput = ADC_ThermalInputAio4;
 	        ADC_ChannelInit(ADC_ChannelThermal, ADC_UserAPP, &adcConfig, ADC_NTC4ChannelCallback);
-              //osDelay(2000/portTICK_PERIOD_MS);
+            osDelay(100/portTICK_PERIOD_MS);  //zhengchao 20210312
 		ADC_StartConversion(ADC_ChannelThermal, ADC_UserAPP);
 	}
 
@@ -2095,7 +2111,7 @@ INT32 AdcTaskInit(void)
 		osThreadAttr_t task_attr;
 		memset(&task_attr , 0 , sizeof(task_attr));
 		task_attr.name = "batAdc";
-		task_attr.priority = osPriorityNormal1;
+		task_attr.priority = osPriorityNormal;
 		task_attr.cb_mem = &adcTask;
 		task_attr.cb_size = sizeof(StaticTask_t);
 		task_attr.stack_mem = adcTaskStack;
@@ -2183,6 +2199,48 @@ void PowerPinConfig(IOType iotype)
 		GPIO_PinWrite(0, 1 << GPIO_POWER_LED, 1 << GPIO_POWER_LED);
       	}
 }
+/**
+  \fn   void relayConfigInit(void)
+  \param[in]  
+  \brief    init the relay, while switch on default
+  \return      
+*/
+void relayConfigInit()
+{
+	gpio_pin_config_t config;
+	config.pinDirection = GPIO_DirectionOutput;
+	config.misc.initOutput = 1;
+
+	pad_config_t padConfig;
+	PAD_GetDefaultConfig(&padConfig);
+
+	PAD_SetPinConfig(32, &padConfig);
+	GPIO_PinConfig(1, AON_RELAY_DRV, &config);
+	GPIO_PinWrite(1, 1 << AON_RELAY_DRV, 0 << AON_RELAY_DRV);  //the relay default is off(disconnected)
+	printf("switch off\n");
+}
+
+/**
+  \fn   void relayControl(BOOL onOrOff)
+  \param[in] onOrOff 
+  \brief    switch the relay on or off
+  \return      
+*/
+
+void relayControl(BOOL onOrOff)
+{
+	if(onOrOff == TRUE)
+	{
+		GPIO_PinWrite(1, 1 << AON_RELAY_DRV, 1 << AON_RELAY_DRV);  //switch on
+		printf("switcht on\n");
+	}
+	else
+	{
+		GPIO_PinWrite(1, 1 << AON_RELAY_DRV, 0 << AON_RELAY_DRV); //switch off
+		printf("switch off\n");
+	}
+}
+
 /**
   \fn   void posGGAReset(void)
   \param[in]