Browse Source

完成框架搭建和MainTask部分任务,配置文件和运行文件加载操作,编译通过。

CHENJIE-PC\QiXiang_CHENJIE 3 years ago
parent
commit
42deee1c7c
17 changed files with 611 additions and 4888 deletions
  1. 6 6
      ARMCC/Makefile
  2. 43 5
      inc/AppConfig.h
  3. 9 17
      inc/AppFunc.h
  4. 7 1
      inc/AppSignal.h
  5. 4 4
      inc/AppTaskGps.h
  6. 4 4
      inc/AppTaskMain.h
  7. 5 155
      inc/AppTaskTcp.h
  8. 63 120
      inc/AppTaskUart.h
  9. 3 1
      inc/app.h
  10. 310 1249
      src/AppFunc.c
  11. 2 2
      src/AppSignal.c
  12. 3 3
      src/AppTaskControl.c
  13. 9 6
      src/AppTaskGps.c
  14. 49 537
      src/AppTaskMain.c
  15. 0 3
      src/AppTaskTcp.c
  16. 93 2770
      src/AppTaskUart.c
  17. 1 5
      src/bsp_custom.c

+ 6 - 6
ARMCC/Makefile

@@ -20,10 +20,10 @@ obj-y             += PLAT/project/$(TARGET)/apps/qx_app/src/app.o \
 					PLAT/project/$(TARGET)/apps/qx_app/src/sensor.o \
 					PLAT/project/$(TARGET)/apps/qx_app/src/AppSignal.o \
 					PLAT/project/$(TARGET)/apps/qx_app/src/AppFunc.o \
-					PLAT/project/$(TARGET)/apps/qx_app/src/AppMainTask.o \
-					PLAT/project/$(TARGET)/apps/qx_app/src/AppTcpTask.o \
-					PLAT/project/$(TARGET)/apps/qx_app/src/AppLedTask.o \
-					PLAT/project/$(TARGET)/apps/qx_app/src/AppUartTask.o \
-					PLAT/project/$(TARGET)/apps/qx_app/src/AppCanTask.o \
-					PLAT/project/$(TARGET)/apps/qx_app/src/AppGpsTask.o 
+					PLAT/project/$(TARGET)/apps/qx_app/src/AppTaskMain.o \
+					PLAT/project/$(TARGET)/apps/qx_app/src/AppTaskTcp.o \
+					PLAT/project/$(TARGET)/apps/qx_app/src/AppTaskControl.o \
+					PLAT/project/$(TARGET)/apps/qx_app/src/AppTaskUart.o \
+					PLAT/project/$(TARGET)/apps/qx_app/src/AppTaskCan.o \
+					PLAT/project/$(TARGET)/apps/qx_app/src/AppTaskGps.o 
 include $(TOP)/PLAT/tools/scripts/Makefile.rules

+ 43 - 5
inc/AppConfig.h

@@ -6,7 +6,8 @@
  * History:      Rev3.0   2021-07-06
  *
  ****************************************************************************/
-
+#ifndef  APPCONFIG_H
+#define  APPCONFIG_H
 #define BMS_MANUFACTURE             (2)                 //BMS制造商表示1-超力源,2-美顺
 #define BMS_INFO                    (1)                 //1-1表示超力源4830,1-2表示超力源6060,2-1表示美顺6455
 #define DATA_MODULE_TYPE            (1)                 //1表示NB模块,2表示4G cat1
@@ -24,10 +25,34 @@
 
 #define APP_CONFIG_FILE_NAME                "qxappconfig.nvm"
 #define APP_DATAINFO_FILE_NAME              "qxappDataInfo.nvm"
-#define BATT_SN_LEN                          17
+#define BATT_SN_LEN                17
+
+#define BMS_OTHER_TEMP          2
+#define TCP_START_SYM1			0x23
+#define TCP_START_SYM2			0x23
+#define TCP_CMD_SYM				0x02	//实时信息上报是0x02
+#define TCP_ANS_SYM 			0xFE
+
+/*-------以下为下行指令定义区----------*/
+#define TCP_QUERY_SYM				0x80
+#define TCP_SETCMD_SYM				0x81
+#define TCP_CONCMD_SYM				0x82
+/*----------------end----------------*/
+
+//encrypt methord
+#define TCP_ENCPT_ENABLE		0x00
+#define TCP_ENCPT_DISABLE		0x01
+
+//message type mark 0x02上报数据单元定义区
+#define BATTMSG				0x80
+#define GPSMSG				0x82
+#define STATESMSG			0x85
+#define VERSIONMSG			0x86
+#define DEBUGMSG			0x8C
 
 //declear struct vars   结构体变量定义
-typedef struct AppConfigDataType
+
+typedef struct _AppConfigDataType
 {	
     BOOL   appDataModify;		//数据更改标志位
 	UINT8  battSN[BATT_SN_LEN]; //电池SN号码
@@ -37,13 +62,13 @@ typedef struct AppConfigDataType
     UINT8  BattTempCount;       //电池温度个数
 }AppConfigDataType;
 
-typedef struct AppConfigDataHeader
+typedef struct _AppConfigDataHeader
 {
     UINT16 fileBodySize; //file body size, not include size of header;
     UINT8  checkSum;
 }AppConfigDataHeader;
 
-typedef struct AppDataType
+typedef struct _AppDataType
 {	
     BOOL   appDataModify;		        //数据更改标志位
     UINT8  BattInfoSendFreqHigh;        //电池信息发送间隔高频,单位s
@@ -55,6 +80,8 @@ typedef struct AppDataType
     UINT32 AccMileage;                  //累计里程,单位m
     UINT16 SysReStartCount;             //系统重启次数
     UINT8  RelayControl;                //继电器控制
+    UINT8  BattLock;                    //电池锁定状态
+    UINT8  BattStolenFlag;              //电池被盗状态
 }AppDataBody;
 typedef struct _AppDataHeader
 {
@@ -67,3 +94,14 @@ typedef struct AppSocketConnectionContext_Tag
     INT32 id;
     INT32 status;
 }AppSocketConnectionContext;
+
+typedef enum
+{
+    IDLE = 0,
+    WORK,
+    LISTEN
+}process_app;
+
+
+
+#endif

+ 9 - 17
inc/AppFunc.h

@@ -4,7 +4,7 @@
  * File name:    AppFunc.h
  * Description:  应用层函数调用文件
  * History:      2021-07-15
- *
+ * Version:      V3.0
  ****************************************************************************/
 #include "flash_ec616_rt.h"
 #include "commontypedef.h"
@@ -12,7 +12,7 @@
 #include "mem_map.h"
 #include "AppConfig.h"
 #include "ec_tcpip_api.h"
-
+#include "AppSignal.h"
 #define FLASH_BMS_FOTA_START_ADDR         0x2E6000          //BMS升级文件存储起始地址
 #define FLASH_BMS_FOTA_LEN                0x32000           //BMS升级文件存储长度 = =200k
 #define FLASH_BMS_FOTA_END_ADDR           0x318000          //BMS升级文件存储结束地址
@@ -28,20 +28,12 @@ typedef struct _Fota_Type
 
 }Fota_Type;
 
-UINT8 Fota_Func(UINT8 *DataPtr,INT32 connectId);
-void CANEncodeFunction(UINT32 ID,UINT8* rawData);
-void UDS_Service();
-UINT16 GetErrorNum(UINT16 *ErrorArray,UINT8 Errorlen);
-UINT8 PutErrorNum(UINT16 *ErrorArray,UINT8 Errorlen,UINT16 ErrorNum);
-UINT8 LookUpRTtable(UINT32 R_value);
-
 
-void strdel(char * str,char c);
-UINT32 location_handle(char *in1);
+void SaveAppData(void);
+void LoadAppData(void);
+void SaveAppConfig(void);
+void LoadAppConfig(void);
+static void setDefaultAppConfigData(void);
+static void setDefaultAppDataInfo(void);
 
-static BOOL UDSClearFotaDownloadRegion(void);
-static BOOL UDSAskforDownLoadData(void);
-static UINT8 UDSPositiveAnswer(UINT8 answerLen,UINT8 messageIndex,UINT32 posCode);
-static UINT8 UDSNegtiveAnswer(UINT8 answerLen,UINT8 messageIndex, UINT32 negCode);
-static UINT8 bcc_chk_fota(UINT8* data, UINT8 length);
-static UINT8 Fota_crc_chk(UINT8* data,UINT8 length);
+UINT16  encryptionAlgorithm (UINT16 plainText);

+ 7 - 1
inc/AppSignal.h

@@ -9,11 +9,12 @@
 #include "commontypedef.h"
 #include "cmsis_os2.h"
 #include "AppConfig.h"
+#include "osasys.h"
+#include "queue.h"
 //declear the bool vars
 extern volatile BOOL NB_Fota_update_flag;
 extern volatile BOOL BMS_Fota_update_flag;
 
-
 //extern declear the UINT8 vars
 extern UINT8	battCellTemp[8];
 extern UINT8	MOSTemp;
@@ -70,6 +71,11 @@ extern UINT32  TimeCounter;
 
 //declear other vars
 extern osMutexId_t Error_Mutex;
+extern osMutexId_t GpsMutex;
+extern QueueHandle_t UartWriteCmdHandle;
 extern AppConfigDataType AppNVMData;
 extern AppDataBody AppDataInfo;
 extern AppSocketConnectionContext socContext;  //网络连接状态
+extern process_app 		    gProcess_app;
+
+

+ 4 - 4
inc/AppTaskGps.h

@@ -1,8 +1,8 @@
 /****************************************************************************
  *
  * Copy right:   Qx.Chen jie
- * File name:    GpsTask.h
- * Description:  Gps处理任务
+ * File name:    AppTaskGps.h
+ * Description:  Gps处理任务和三轴数据处理及获取
  * History:      2021-03-07 2021-07-15
  * Version:      V3.0
  ****************************************************************************/
@@ -13,7 +13,6 @@
 #include "ostask.h"
 #include "queue.h"
 #include "ps_event_callback.h"
-#include "app.h"
 #include "cmisim.h"
 #include "cmimm.h"
 #include "cmips.h"
@@ -31,4 +30,5 @@
 #include "hal_module_adapter.h"
 
 #define GPS_TASK_STACK_SIZE   								 (512)
-INT32 AppTaskGpsInit(void);
+void AppTaskGpsInit(void *arg);
+void AppTaskGpsDeInit(void *arg);

+ 4 - 4
inc/AppTaskMain.h

@@ -27,16 +27,16 @@
 #include "hal_module_adapter.h"
 #include "timers.h"
 #include "AppSignal.h"
-
+#include "AppFunc.h"
 #define PROC_MAIN_TASK_STACK_SIZE           (1024+512)
 
 typedef enum
 {
     PROCESS_STATE_IDLE = 0,
     PROCESS_STATE_WORK,
-    PROCESS_STATE_SLEEP
+    PROCESS_STATE_LISTEN
 }process_Main;
 
-void MainTaskInit(void* arg);
-void MainTaskDeInit(void* arg);
+void AppTaskMainInit(void* arg);
+void AppTaskMainDeInit(void* arg);
 

+ 5 - 155
inc/AppTaskTcp.h

@@ -26,161 +26,11 @@
 #include "ec_tcpip_api.h"
 #include "hal_module_adapter.h"
 #include "timers.h"
-#include "Signal.h"
+#include "AppSignal.h"
+#include "AppConfig.h"
+#include "AppFunc.h"
 
 #define PROC_TCP_TASK_STACK_SIZE           (2048)
-#define QMSG_ID_BASE               (0x160) 
-#define QMSG_ID_NW_IP_READY        (QMSG_ID_BASE)
-#define QMSG_ID_NW_IP_SUSPEND      (QMSG_ID_BASE + 1)
-#define QMSG_ID_NW_IP_NOREACHABLE  (QMSG_ID_BASE + 2)
-#define QMSG_ID_SOCK_SENDPKG       (QMSG_ID_BASE + 4)
-#define QMSG_ID_SOCK_RECVPKG       (QMSG_ID_BASE + 5)
-#define QMSG_ID_SOCK_EXIT       	(QMSG_ID_BASE + 6)
-#define APP_EVENT_QUEUE_SIZE    (10)
-
-/*---------------测试IP地址-----------------------------------*/
-// #define QX_TCP_ADD 				"rpi.it5000.com"
-// #define QX_TCP_PORT					8812
-/*---------------测试IP地址END-----------------------------------*/
-
-#define TCP_START_SYM1			0x23
-#define TCP_START_SYM2			0x23
-#define TCP_CMD_SYM				0x02	//实时信息上报是0x02
-#define TCP_ANS_SYM 			0xFE
-
-/*-------以下为下行指令定义区----------*/
-#define TCP_QUERY_SYM				0x80
-#define TCP_SETCMD_SYM				0x81
-#define TCP_CONCMD_SYM				0x82
-/*----------------end----------------*/
-
-//encrypt methord
-#define TCP_ENCPT_ENABLE		0x00
-#define TCP_ENCPT_DISABLE		0x01
-
-//message type mark 0x02上报数据单元定义区
-#define BATTMSG				0x80
-#define GPSMSG				0x82
-#define STATESMSG			0x85
-#define VERSIONMSG			0x86
-#define DEBUGMSG			0x8C
-
-typedef struct BattInfoType
-{	
-	UINT8	sendTimeUTC[6];
-	UINT8	msgMark;
-	UINT8	msgCollectionTimeUTC[6];
-	UINT8	signalStrength;
-	UINT8	errClass;
-	UINT8	errCode[2];
-	UINT8	battI[2];
-	UINT8	battLinkVol[2];
-	UINT8	battPackVol[2];
-	UINT8   switchState;
-	UINT8	battSOC;
-	UINT8	battSOH;
-	UINT8	batCellBalenceState[4];	//uint32 should change to uint8[]: each bit stand for 1 cell, up to 1024
-	UINT8	battCellNum;			//uint8 should change to uint16 (0~65535)  //zhengchao
-	UINT8	battCellU[BATT_CELL_VOL_NUM_2];
-	UINT8	battTempNum;			//uint8 should change to uint16 (0~65535)
-	UINT8	battCellTemp[BATT_TEMP_NUM];
-	UINT8	battWorkState;
-	UINT8	battHeatEnableState;
-	UINT8 	battotherTempNum;
-	UINT8 	battotherTemp[BATT_OTHER_TEMP_NUM+NB_OTHER_TEMP_NUM];
-}BattInfoType;
-typedef struct BattMsgtoTcpType
-{	
-	UINT8 	startSymbol[2];
-	UINT8	cmdSymbol;
-	UINT8	ansSymbol;
-	UINT8	SN[BATT_SN_LEN];
-	UINT8	encryptMethod;
-	UINT8	dataLength[2];	
-	BattInfoType battInfo;	
-	UINT8	CRC;
-}BattMsgtoTcpType;
-/*---------------------------------------------------------------------------*/
-typedef struct _GPSInfoType
-{
-	UINT8	sendTimeUTC[6];
-	UINT8	msgMark;
-	UINT8	msgCollectionTimeUTC[6];
-	UINT8	locateMark;
-	UINT8	satelliteNum;
-	UINT8	direction[2];
-	UINT8	speed[2];
-	UINT8	altitude[2];
-	UINT8	latitude[4];
-	UINT8	longitude[4];
-	UINT8   AccMileage[4];
-	
-}GPSInfoType;
-typedef struct GPSMsgtoTcpType
-{
-	UINT8 	startSymbol[2];
-	UINT8	cmdSymbol;
-	UINT8	ansSymbol;
-	UINT8	SN[BATT_SN_LEN];
-	UINT8	encryptMethod;
-	UINT8	dataLength[2];	
-	GPSInfoType gpsInfo;	
-	UINT8	CRC;
-}GPSMsgtoTcpType;
-/*---------------------------------------------------------------------------*/
-typedef struct _VerInfoType
-{
-	UINT8	sendTimeUTC[6];
-	UINT8	msgMark;
-	UINT8	msgCollectionTimeUTC[6];
-	UINT8	ICCID[20];
-	UINT8	IMEI[15];
-	UINT8	BMSHwVersion[2];
-	UINT8	BMSSwVersion[4];
-	UINT8	NBHwVersion[2];
-	UINT8	BLVersion[4];
-	UINT8	DRVVersion[4];
-	UINT8	APPVersion[4];
-	UINT8	BmsType;
-	UINT8	BmsInfo;
-	UINT8   DataModuleType;
-}VerInfoType;
-typedef struct VersionMsgtoTcpType
-{
-	UINT8 	startSymbol[2];
-	UINT8	cmdSymbol;
-	UINT8	ansSymbol;
-	UINT8	SN[BATT_SN_LEN];
-	UINT8	encryptMethod;
-	UINT8	dataLength[2];	
-	VerInfoType VerInfo;	
-	UINT8	CRC;
-}VersionMsgtoTcpType;
-typedef struct _DebugInfoType
-{
-	UINT8	sendTimeUTC[6];
-	UINT8	msgMark;
-	UINT8  	DebugLen[2];
-}DebugInfoType;
-typedef struct DebugMsgtoTcp_Type
-{
-	UINT8 	startSymbol[2];
-	UINT8	cmdSymbol;
-	UINT8	ansSymbol;
-	UINT8	SN[BATT_SN_LEN];
-	UINT8	encryptMethod;
-	UINT8	dataLength[2];		
-	DebugInfoType DebugInfo;
-	UINT8	CRC;
-}DebugMsgtoTcpType;
-/*---------------------------------------------------------------------------*/
-typedef enum
-{
-    APP_SOCKET_CONNECTION_CLOSED = 0,
-    APP_SOCKET_CONNECTION_CONNECTING,
-    APP_SOCKET_CONNECTION_CONNECTED,    
-}AppSocketConnectionStatus;
-
 typedef enum
 {
     PROCESS_TCP_STATE_IDLE = 0,
@@ -188,5 +38,5 @@ typedef enum
     PROCESS_TCP_STATE_RECV,
     PROCESS_TCP_STATE_SLEEP
 }process_TCP;
-void TcpTaskInit(void *arg);
-void TcpTaskDeInit(void *arg);
+void AppTaskTcpInit(void *arg);
+void AppTaskTcpDeInit(void *arg);

+ 63 - 120
inc/AppTaskUart.h

@@ -7,62 +7,36 @@
  *
  ****************************************************************************/
 #include "commontypedef.h"
-extern UINT8 BattChrgEndFlag;
-typedef struct _UartRedMsg
-{
-	volatile BOOL UartFlag;
-    UINT8 Header[3];
-    UINT8 data[120];
-    UINT16 len;
-}UartReadMsgType;
-typedef struct Uart_Write_Data_Type
-{
-    volatile uint8_t WriteCmd; 
-    uint8_t Data[2];
-}Uart_Write_Data_Type;
-//全局变量输出区
-extern QueueHandle_t UartWriteCmdHandle;
-//
-#define UART_WRITE_FLAG 0x01
-#define UART_READ_FLAG 0x00
+#include "bsp.h"
+#include "bsp_custom.h"
+#include "osasys.h"
+#include "ostask.h"
+#include "queue.h"
+#include "ps_event_callback.h"
+#include "cmisim.h"
+#include "cmimm.h"
+#include "cmips.h"
+#include "sockets.h"
+#include "psifevent.h"
+#include "ps_lib_api.h"
+#include "lwip/netdb.h"
+#include "debug_log.h"
+#include "slpman_ec616.h"
+#include "plat_config.h"
+#include "ec_tcpip_api.h"
+#include "hal_module_adapter.h"
+#include <stdlib.h>
+#include "numeric.h"
+#include "AppSignal.h"
 
-#define PROC_UART_TASK_STACK_SIZE           (1024)
-#define BMS_ADDRESS_CODE 0x01
-#define UART_READ_CODE 0x03
-#define UART_WRITE_CODE 0x10
-#define UART_ENCRYPT_CODE 0x05
+#define  PROC_UART_TASK_STACK_SIZE (1024)
 
-typedef struct Uart_Read_Msg_Type
-{
-    uint8_t Bms_Address; 
-    uint8_t Bms_Funcode; 
-    uint8_t Reg_Begin_H; 
-    uint8_t Reg_Begin_L; 
-    uint8_t Reg_Num_H; 
-    uint8_t Reg_Num_L; 
-    uint8_t CRC_L;
-    uint8_t CRC_H;
-}Uart_Read_Msg_Type;
-
-typedef struct Uart_Write_Msg_Type
-{
-    uint8_t Bms_Address; 
-    uint8_t Bms_Funcode; 
-    uint8_t Reg_Begin_H; 
-    uint8_t Reg_Begin_L; 
-    uint8_t Reg_Num_H; 
-    uint8_t Reg_Num_L; 
-    uint8_t Data_Count;
-    uint8_t Data[2];
-    uint8_t CRC_L;
-    uint8_t CRC_H;
-}Uart_Write_Msg_Type;
-
-typedef struct Uart_Write_Answer_Msg_Type
-{
-    uint8_t Bms_Address; 
-    uint8_t Bms_Funcode; 
-}Uart_Write_Answer_Msg_Type;
+#define BMS_ADDRESS_CODE    0x01
+#define UART_READ_CODE      0x03
+#define UART_WRITE_CODE     0x10
+#define UART_ENCRYPT_CODE   0x05
+#define UART_WRITE_FLAG     0x01
+#define UART_READ_FLAG      0x00
 typedef enum
 {
     PROCESS_UART_STATE_ENCRYPT = 0,
@@ -72,72 +46,41 @@ typedef enum
     PROCESS_UART_STATE_UPDATE,
     PROCESS_UART_STATE_SLEEP
 }process_Uart;
-typedef enum
+typedef struct _UartAnswerData
 {
-        UPDATE_STEP_CHECK_VERSION=0,
-        UPDATE_STEP_REQUEST_UPDATE,
-        UPDATE_STEP_START_UPDATE, 
-        UPDATE_STEP_CHECK_VERSION_AGAIN,       
-        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_RESET, //exit download, and go to before UPDATE_STEP_PREPARE_SEND_UPDATE_DATA
-        UPDATE_STEP_DOWNLOAD_BREAK_OFF,  //exite download, and return boot mode(not app mode)
-        UPDATE_STEP_ERROR
-}UpdateStep;
-
-typedef struct BMS_Update_Recv_Msg_Type
+	volatile BOOL UartFlag;
+    UINT8 Header[3];
+    UINT8 data[120];
+    UINT16 len;
+}UartAnswerData_S;
+typedef struct _UartWriteDataType
 {
-        UINT8 startFlag;
-        UINT8 addrFlag;
-        UINT8 cmdRW;
-        UINT8 dataLen;
-        UINT8 cmd;
-        UINT8 data;
-        UINT8 checkSum;
-        UINT8 endFlag;
-}BMS_Update_Recv_Msg_Type;
-typedef enum
-{        
-        MS_UPDATE_STEP_SEND_FIRMWARE_UPDATE_REQUEST_AND_JUMP_TO_BOOTLOADER = 1,
-        MS_UPDATE_STEP_FIRMWARE_UPDATE_REQUEST_ANSWER, 
-        MS_UPDATE_STEP_SEND_FIRMWARE_INFO,       
-        MS_UPDATE_STEP_FIRMWARE_INFO_CHECK_AND_UPDATE_REQEST_ANSWER,
-        MS_UPDATE_STEP_EREASE_APP_FLASH_REQUEST,
-        MS_UPDATE_STEP_EREASE_FLASH_ANSWER,        
-        MS_UPDATE_STEP_SEND_UPDATE_DATA,
-        MS_UPDATE_STEP_UPDATE_DATA_WRITE_ANSWER,
-        MS_UPDATE_STEP_SEND_UPDATE_DATA_END_AND_JUMP_TO_APP,
-        MS_UPDATE_STEP_JUMP_TO_APP_ANSWER,
-        MS_UPDATE_STEP_READ_CURRENT_RUNNING_MODE,
-        MS_UPDATE_STEP_CURRENT_RUNNING_MODE_ANSWER,
-        MS_UPDATE_STEP_END,
-        MS_UPDATE_STEP_ERROR
-}UpdateStep_MS_BMS;
-
-typedef enum
+    volatile UINT8 WriteCmd; 
+    UINT8 Data[2];
+}UartWriteData_S;
+typedef struct _UartQueryType
+{
+    UINT8 Bms_Address; 
+    UINT8 Bms_Funcode; 
+    UINT8 Reg_Begin_H; 
+    UINT8 Reg_Begin_L; 
+    UINT8 Reg_Num_H; 
+    UINT8 Reg_Num_L; 
+    UINT8 CRC_L;
+    UINT8 CRC_H;
+}UartQueryType;
+typedef struct _UartWriteMsgType
 {
-	updateOK                 	= 0,         ///< update successfully.
-	updateErrorTimeout			= 1,         ///< update failed after try 3 times, so time out
-	updateErrorBMSPowerLow        	= 2,         ///update failed, because of bmp power low
-	updateErrorBMSWarningProtect           	= 3,         ///update failed, because there are some warning in bms which not allowed to update
-	updateErrorBMSNotSurport          	= 4,         ///update failed, because of bms not surpport
-	updateErrorBMSWorkState           	= 5,         ///update failed, because bms is charging or discharging
-	updateErrorFirmwareInfoError                	= 6, //update failed, the firmware info error
-	updateErrorFirmwareSizeError          	= 7,  ///update failed, the firmware size in firmware info is to large
-	updateErrorAppErease = 8,			///update failed, erease app flash failed
-	updateErrorPackageCRC = 9,			///the crc of some package is wrong
-	updateErrorPackageWrite = 10,		///package write in failed
-	updateErrorPackageNo = 11,			///package number is not right or not continues
-	updateErrorCheckSumError,			// checksum != checksumcal
-	updateFailed = 0xFF					///					
-}updateBMSStatus;
-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);
+    UINT8 Bms_Address; 
+    UINT8 Bms_Funcode; 
+    UINT8 Reg_Begin_H; 
+    UINT8 Reg_Begin_L; 
+    UINT8 Reg_Num_H; 
+    UINT8 Reg_Num_L; 
+    UINT8 Data_Count;
+    UINT8 Data[2];
+    UINT8 CRC_L;
+    UINT8 CRC_H;
+}UartWriteMsgType;
+void AppTaskUartInit(void *arg);
+void AppTaskUartDeInit(void *arg);

+ 3 - 1
inc/app.h

@@ -26,7 +26,8 @@
 #include "ec_tcpip_api.h"
 #include "hal_module_adapter.h"
 #include "timers.h"
-
+#ifndef  APP_H
+#define  APP_H
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -39,3 +40,4 @@ extern void AppTaskCanInit(void *arg);
 #ifdef __cplusplus
 }
 #endif
+#endif /* APP_H */

+ 310 - 1249
src/AppFunc.c

@@ -7,1293 +7,354 @@
  *
  ****************************************************************************/
 #include "AppFunc.h"
-#include "AppSignal.h"
+
 /*******
- * Fota升级处理函数
- * 输入:网络接收的数据,网络链接ID
- * 输出:0x00表示数据未准备完成,0x01表示数据模块OTA数据准备完成,0x88表示BMS的OTA数据准备完成
- * 处理:将接收的数据进行校验,搬运至升级区域,并进行应答
+ * 运行数据保存函数
+ * 输入:
+ * 输出:
+ * 处理:将运行数据进行保存
 *******/
-UINT8 Fota_Func(UINT8 *DataPtr,INT32 connectId)
+void SaveAppData(void)
 {
-    UINT8 		Fota_Answer[43]={0};
-	Fota_Type 	Fota_S;
-    UINT8 		Fota_Cmd = 0;
-    INT8 		ret = 0;
-    UINT8* 		Data_Read_Buffer = NULL;
-    UINT8 		Data_Read_Crc = 0;
-    if(*(DataPtr+30)==0x01)
+    OSAFILE fp = PNULL;
+    UINT32  writeCount = 0;
+	AppDataHeader AppDataHr;
+    fp = OsaFopen(APP_DATAINFO_FILE_NAME, "wb");   //read & write
+    if(OsaFseek(fp, 0, SEEK_SET) != 0)
+	{
+        #ifdef USING_PRINTF
+				printf("Seek file failed [%d] \r\n",__LINE__);
+	    #endif
+	    OsaFclose(fp);
+        return;
+	}
+    if (fp == PNULL)
     {
-        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;
-                    BSP_QSPI_Erase_Safe(Fota_S.Fota_Flash_Addres,Fota_S.Fota_All_Data_Len + 4 - (Fota_S.Fota_All_Data_Len%4)); //对齐擦除
-                }
-                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 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);
-                    }
-                    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);
-                    Data_Read_Buffer = malloc(Fota_S.Fota_Recv_Data_Len);
-                    BSP_QSPI_Read_Safe(Data_Read_Buffer,Fota_S.Fota_Flash_Addres+Fota_S.Fota_Current_Addres,Fota_S.Fota_Recv_Data_Len);
-                    Data_Read_Crc = Fota_crc_chk(Data_Read_Buffer,Fota_S.Fota_Recv_Data_Len);
-                    #ifdef USING_PRINTF
-                        printf("\n\n\n");
-                        for(int i=0;i<Fota_S.Fota_Recv_Data_Len;i++)
-                        {
-                            printf("%x  ",*(Data_Read_Buffer+i));
-                        }    
-                        printf("\n\n\n");
-                    #endif
-					if(Data_Read_Buffer!=NULL)
-                    	free(Data_Read_Buffer);
-					Data_Read_Buffer = NULL;
-                    if(Data_Read_Crc==Fota_S.Fota_CRC )
-                    {
-                        Fota_Answer[3] = 0x01;
-                    }
-                    else
-                    {
-                        Fota_Answer[3] = 0x02;
-                        BSP_QSPI_Erase_Safe(Fota_S.Fota_Flash_Addres+Fota_S.Fota_Current_Addres,Fota_S.Fota_Recv_Data_Len);
-                    }
-                }
-                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)
-                {
-                    return 0x01;
-                }
-                else
-                {
-                    return 0x00;
-                }
-                break;
-            }
-            default:
-            {
-                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;
-            }
-                
-        }
-		return 0x00;
+	    #ifdef USING_PRINTF
+			printf(" NVM, can't open/create NVM: 'qxappconfig.nvm', save NVM failed\n");
+		#endif
+        return;
     }
-    else if(*(DataPtr+30)==0x88)//BMS升级文件存放
+    AppDataHr.fileBodySize   = sizeof(AppDataInfo); 
+    AppDataHr.checkSum       = OsaCalcCrcValue((UINT8 *)&AppDataInfo, sizeof(AppDataInfo));
+    #ifdef USING_PRINTF
+		printf(" AppDataHr.fileBodySize:%x\n",AppDataHr.fileBodySize);
+	#endif
+    writeCount = OsaFwrite(&AppDataHr, sizeof(AppDataHr), 1, fp);
+    if (writeCount != 1)
     {
-        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);
-                    }
-                    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);
-                    Data_Read_Buffer = malloc(Fota_S.Fota_Recv_Data_Len);
-                    BSP_QSPI_Read_Safe(Data_Read_Buffer,Fota_S.Fota_Flash_Addres+Fota_S.Fota_Current_Addres,Fota_S.Fota_Recv_Data_Len);
-                    Data_Read_Crc = Fota_crc_chk(Data_Read_Buffer,Fota_S.Fota_Recv_Data_Len);
-                    #ifdef USING_PRINTF1
-                        printf("\n\n\n");
-                        UINT8 temp[1];
-                        for(int i=0;i<Fota_S.Fota_Recv_Data_Len;i++)
-                        {
-                            printf("%x  ",*(Data_Read_Buffer+i));
-                        }    
-                        printf("\n\n\n");
-                    #endif
-                    if(Data_Read_Buffer!=NULL)
-                    	free(Data_Read_Buffer);
-					Data_Read_Buffer = NULL;
-                    if(Data_Read_Crc==Fota_S.Fota_CRC )
-                    {
-                        Fota_Answer[3] = 0x01;
-                    }
-                    else
-                    {
-                        Fota_Answer[3] = 0x02;
-                        BSP_QSPI_Erase_Safe(Fota_S.Fota_Flash_Addres+Fota_S.Fota_Current_Addres,Fota_S.Fota_Recv_Data_Len);
-                    }
-                }
-                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)
-                {
-                    return 0x88;
-                }
-                else
-                {
-                    return 0x00;
-                }
-                break;
-            }
-            default:
-            {
-                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;
-            }
-        }
-		return 0x00;
+        #ifdef USING_PRINTF
+                    printf(" NVM: 'qxappconfig.nvm', write the file header failed\n");
+        #endif
+        OsaFclose(fp);
+        return;
     }
-}
-static UINT8 bcc_chk_fota(UINT8* data, UINT8 length)
-{
-    UINT8 bcc_chk_return = 0x00;
-    UINT8 count = 0;
-    while (count<length)
+    AppDataInfo.appDataModify = FALSE;
+    writeCount = OsaFwrite(&AppDataInfo, sizeof(AppDataInfo), 1, fp);
+    if (writeCount != 1)
     {
-        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++;
+        #ifdef USING_PRINTF
+			printf(" NVM: 'qxappconfig.nvm', write the file body failed\n");
+		#endif
     }
-    return reg_crc;
+    OsaFclose(fp);
+    return;
 }
 /*******
- * Can解码组包函数
- * 输入:全局变量和缓存地址
+ * 运行数据加载函数
+ * 输入:空
  * 输出:空
- * 处理:将全局变量进行组包后存放
+ * 处理:将运行数据进行加载,若新增了数据项,则保持前面数据项不动
 *******/
-void CANEncodeFunction(UINT32 ID,UINT8* rawData)
+void LoadAppData(void)
 {
-	memset(rawData, 0xFF, 8);	
-	switch (ID)
-	{
-		case	0x6A0:
-			*(UINT16*)(rawData +0) = ((battCellU[0]>>8) & 0xFF) | (((battCellU[0]&0xFF)<<8)&0xFF00);
-			*(UINT16*)(rawData +2) = ((battCellU[1]>>8) & 0xFF) | (((battCellU[1]&0xFF)<<8)&0xFF00);
-			*(UINT16*)(rawData +4) = ((battCellU[2]>>8) & 0xFF) | (((battCellU[2]&0xFF)<<8)&0xFF00);
-			*(UINT16*)(rawData +6) = ((battCellU[3]>>8) & 0xFF) | (((battCellU[3]&0xFF)<<8)&0xFF00);
-			break;
-		case	0x6A1:
-			*(UINT16*)(rawData +0) = ((battCellU[4]>>8) & 0xFF) | (((battCellU[4]&0xFF)<<8)&0xFF00);
-			*(UINT16*)(rawData +2) = ((battCellU[5]>>8) & 0xFF) | (((battCellU[5]&0xFF)<<8)&0xFF00);
-			*(UINT16*)(rawData +4) = ((battCellU[6]>>8) & 0xFF) | (((battCellU[6]&0xFF)<<8)&0xFF00);
-			*(UINT16*)(rawData +6) = ((battCellU[7]>>8) & 0xFF) | (((battCellU[7]&0xFF)<<8)&0xFF00);
-			break;
-		case	0x6A2:
-			*(UINT16*)(rawData +0) = ((battCellU[8]>>8) & 0xFF) | (((battCellU[8]&0xFF)<<8)&0xFF00);
-			*(UINT16*)(rawData +2) = ((battCellU[9]>>8) & 0xFF) | (((battCellU[9]&0xFF)<<8)&0xFF00);
-			*(UINT16*)(rawData +4) = ((battCellU[10]>>8) & 0xFF) | (((battCellU[10]&0xFF)<<8)&0xFF00);
-			*(UINT16*)(rawData +6) = ((battCellU[11]>>8) & 0xFF) | (((battCellU[11]&0xFF)<<8)&0xFF00);
-			break;
-		case	0x6A3:
-			*(UINT16*)(rawData +0) = ((battCellU[12]>>8) & 0xFF) | (((battCellU[12]&0xFF)<<8)&0xFF00);
-			*(UINT16*)(rawData +2) = ((battCellU[13]>>8) & 0xFF) | (((battCellU[13]&0xFF)<<8)&0xFF00);
-			*(UINT16*)(rawData +4) = ((battCellU[14]>>8) & 0xFF) | (((battCellU[14]&0xFF)<<8)&0xFF00);
-			*(UINT16*)(rawData +6) = ((battCellU[15]>>8) & 0xFF) | (((battCellU[15]&0xFF)<<8)&0xFF00);
-			break;
-		case	0x6A4:
-			*(UINT16*)(rawData +0) = ((battCellU[16]>>8) & 0xFF) | (((battCellU[16]&0xFF)<<8)&0xFF00);
-			*(UINT16*)(rawData +2) = ((battCellU[17]>>8) & 0xFF) | (((battCellU[17]&0xFF)<<8)&0xFF00);
-			*(UINT16*)(rawData +4) = ((battCellU[18]>>8) & 0xFF) | (((battCellU[18]&0xFF)<<8)&0xFF00);
-			*(UINT16*)(rawData +6) = ((battCellU[19]>>8) & 0xFF) | (((battCellU[19]&0xFF)<<8)&0xFF00);
-			break;
-		case	0x6A5:
-			*(UINT16*)(rawData +0) = ((battCellU[20]>>8) & 0xFF) | (((battCellU[20]&0xFF)<<8)&0xFF00);
-			*(UINT16*)(rawData +2) = ((battCellU[21]>>8) & 0xFF) | (((battCellU[21]&0xFF)<<8)&0xFF00);
-			*(UINT16*)(rawData +4) = ((battCellU[22]>>8) & 0xFF) | (((battCellU[22]&0xFF)<<8)&0xFF00);
-			*(UINT16*)(rawData +6) = ((battCellU[23]>>8) & 0xFF) | (((battCellU[23]&0xFF)<<8)&0xFF00);
-			break;
-		case	0x6A6:
-			*(UINT16*)(rawData +0) = ((battCellU[24]>>8) & 0xFF) | (((battCellU[24]&0xFF)<<8)&0xFF00);
-			*(UINT16*)(rawData +2) = ((battCellU[25]>>8) & 0xFF) | (((battCellU[25]&0xFF)<<8)&0xFF00);
-			*(UINT16*)(rawData +4) = ((battCellU[26]>>8) & 0xFF) | (((battCellU[26]&0xFF)<<8)&0xFF00);
-			*(UINT16*)(rawData +6) = ((battCellU[27]>>8) & 0xFF) | (((battCellU[27]&0xFF)<<8)&0xFF00);
-			break;
-		case	0x6B0:
-			*(UINT8*)(rawData +0) =battCellTemp[0]&0xFF;
-			*(UINT8*)(rawData +1) =battCellTemp[1]&0xFF;
-			*(UINT8*)(rawData +2) =battCellTemp[2]&0xFF;
-			*(UINT8*)(rawData +3) =battCellTemp[3]&0xFF;
-			*(UINT8*)(rawData +4) =battCellTemp[4]&0xFF;
-			*(UINT8*)(rawData +5) =battCellTemp[5]&0xFF;
-			*(UINT8*)(rawData +6) =battCellTemp[6]&0xFF;
-			*(UINT8*)(rawData +7) =battCellTemp[7]&0xFF;
-			break;
-		case	0x6BA:
-			*(UINT8*)(rawData +0) =MOSTemp&0xFF;
-			*(UINT8*)(rawData +1) =packTemp&0xFF;
-			*(UINT8*)(rawData +2) =fastChargeTemp&0xFF;
-			*(UINT8*)(rawData +3) =normalChargeTemp&0xFF;
-			*(UINT8*)(rawData +4) =heatTemp1&0xFF;
-			*(UINT8*)(rawData +5) =heatTemp2&0xFF;
-			*(UINT8*)(rawData +6) =nbReservedTemp1&0xFF;
-			*(UINT8*)(rawData +7) =nbReservedTemp2&0xFF;
-			break;
-		case	0x6C0:
-			*(UINT16*)(rawData +0) = ((minCellVol>>8) & 0xFF) | (((minCellVol&0xFF)<<8)&0xFF00);
-			*(UINT16*)(rawData +2) = ((maxCellVol>>8) & 0xFF) | (((maxCellVol&0xFF)<<8)&0xFF00);
-			*(UINT16*)(rawData +4) = ((battPackVol>>8) & 0xFF) | (((battPackVol&0xFF)<<8)&0xFF00);
-			*(UINT8*)(rawData +6) =maxCellTemp&0xFF;
-			*(UINT8*)(rawData +7) =minCellTemp&0xFF;
-			break;
-		case	0x6C1:
-			*(UINT8*)(rawData +0) =battSOC&0xFF;
-			*(UINT8*)(rawData +1) =battSOH&0xFF;
-			*(UINT16*)(rawData +2) = ((battI>>8) & 0xFF) | (((battI&0xFF)<<8)&0xFF00);
-			*(UINT8*)(rawData +4) =battWorkState&0xFF;
-			*(UINT32*)(rawData +5) = ((battWarningState>>16) & 0xFF)|((battWarningState)&0xFF00)|(((battWarningState&0xFF)<<16)&0xFF0000);
-			break;
-		case	0x6C2:
-			*(UINT32*)(rawData +0) = ((nbSwVersion>>24) & 0xFF)|((nbSwVersion>>8)&0xFF00)|((nbSwVersion<<8)&0xFF0000)|(((nbSwVersion&0xFF)<<24)&0xFF000000);
-			*(UINT16*)(rawData +4) = ((nbHwVersion>>8) & 0xFF) | (((nbHwVersion&0xFF)<<8)&0xFF00);
-			*(UINT8*)(rawData +6) =bmsSwVersion&0xFF;
-			*(UINT8*)(rawData +7) =bmsHwVersion&0xFF;
-			break;
-		case	0x6C3:
-			*(UINT32*)(rawData +0) = ((battBalanceoInfo>>24) & 0xFF)|((battBalanceoInfo>>8)&0xFF00)|((battBalanceoInfo<<8)&0xFF0000)|(((battBalanceoInfo&0xFF)<<24)&0xFF000000);
-			*(UINT8*)(rawData +4) =battMOSSwitchState&0xFF;
-			*(UINT8*)(rawData +5) =battHeatEnableState&0xFF;
-			break;
-		case	0x6C4:
-			*(UINT8*)(rawData +0) =reservedSignal1&0xFF;
-			*(UINT8*)(rawData +1) =reservedSignal2&0xFF;
-			*(UINT8*)(rawData +2) =reservedSignal3&0xFF;
-			*(UINT8*)(rawData +3) =reservedSignal4&0xFF;
-			*(UINT8*)(rawData +4) =reservedSignal5&0xFF;
-			*(UINT8*)(rawData +5) =reservedSignal6&0xFF;
-			*(UINT8*)(rawData +6) =reservedSignal7&0xFF;
-			*(UINT8*)(rawData +7) =reservedSignal8&0xFF;
-			break;
-		case	0x7C0:
-			*(UINT16*)(rawData +0) = ((battCellU[0]>>8) & 0xFF) | (((battCellU[0]&0xFF)<<8)&0xFF00);
-			*(UINT16*)(rawData +2) = ((battCellU[1]>>8) & 0xFF) | (((battCellU[1]&0xFF)<<8)&0xFF00);
-			*(UINT16*)(rawData +4) = ((battCellU[2]>>8) & 0xFF) | (((battCellU[2]&0xFF)<<8)&0xFF00);
-			*(UINT16*)(rawData +6) = ((battCellU[3]>>8) & 0xFF) | (((battCellU[3]&0xFF)<<8)&0xFF00);
-			break;
-		case	0x7C1:
-			*(UINT16*)(rawData +0) = ((battCellU[4]>>8) & 0xFF) | (((battCellU[4]&0xFF)<<8)&0xFF00);
-			*(UINT16*)(rawData +2) = ((battCellU[5]>>8) & 0xFF) | (((battCellU[5]&0xFF)<<8)&0xFF00);
-			*(UINT16*)(rawData +4) = ((battCellU[6]>>8) & 0xFF) | (((battCellU[6]&0xFF)<<8)&0xFF00);
-			*(UINT16*)(rawData +6) = ((battCellU[7]>>8) & 0xFF) | (((battCellU[7]&0xFF)<<8)&0xFF00);
-			break;
-		case	0x7C2:
-			*(UINT16*)(rawData +0) = ((battCellU[8]>>8) & 0xFF) | (((battCellU[8]&0xFF)<<8)&0xFF00);
-			*(UINT16*)(rawData +2) = ((battCellU[9]>>8) & 0xFF) | (((battCellU[9]&0xFF)<<8)&0xFF00);
-			*(UINT16*)(rawData +4) = ((battCellU[10]>>8) & 0xFF) | (((battCellU[10]&0xFF)<<8)&0xFF00);
-			*(UINT16*)(rawData +6) = ((battCellU[11]>>8) & 0xFF) | (((battCellU[11]&0xFF)<<8)&0xFF00);
-			break;
-		case	0x7C3:
-			*(UINT16*)(rawData +0) = ((battCellU[12]>>8) & 0xFF) | (((battCellU[12]&0xFF)<<8)&0xFF00);
-			*(UINT16*)(rawData +2) = ((battCellU[13]>>8) & 0xFF) | (((battCellU[13]&0xFF)<<8)&0xFF00);
-			*(UINT16*)(rawData +4) = ((battCellU[14]>>8) & 0xFF) | (((battCellU[14]&0xFF)<<8)&0xFF00);
-			*(UINT16*)(rawData +6) = ((battCellU[15]>>8) & 0xFF) | (((battCellU[15]&0xFF)<<8)&0xFF00);
-			break;
-		case	0x7C4:
-			*(UINT16*)(rawData +0) = ((battCellU[16]>>8) & 0xFF) | (((battCellU[16]&0xFF)<<8)&0xFF00);
-			*(UINT16*)(rawData +2) = ((battCellU[17]>>8) & 0xFF) | (((battCellU[17]&0xFF)<<8)&0xFF00);
-			*(UINT16*)(rawData +4) = ((battCellU[18]>>8) & 0xFF) | (((battCellU[18]&0xFF)<<8)&0xFF00);
-			*(UINT16*)(rawData +6) = ((battCellU[19]>>8) & 0xFF) | (((battCellU[19]&0xFF)<<8)&0xFF00);
-			break;
-		case	0x7C5:
-			*(UINT16*)(rawData +0) = ((battCellU[20]>>8) & 0xFF) | (((battCellU[20]&0xFF)<<8)&0xFF00);
-			*(UINT16*)(rawData +2) = ((battCellU[21]>>8) & 0xFF) | (((battCellU[21]&0xFF)<<8)&0xFF00);
-			*(UINT16*)(rawData +4) = ((battCellU[22]>>8) & 0xFF) | (((battCellU[22]&0xFF)<<8)&0xFF00);
-			*(UINT16*)(rawData +6) = ((battCellU[23]>>8) & 0xFF) | (((battCellU[23]&0xFF)<<8)&0xFF00);
-			break;
-		case	0x7C6:
-			*(UINT16*)(rawData +0) = ((battCellU[24]>>8) & 0xFF) | (((battCellU[24]&0xFF)<<8)&0xFF00);
-			*(UINT16*)(rawData +2) = ((battCellU[25]>>8) & 0xFF) | (((battCellU[25]&0xFF)<<8)&0xFF00);
-			*(UINT16*)(rawData +4) = ((battCellU[26]>>8) & 0xFF) | (((battCellU[26]&0xFF)<<8)&0xFF00);
-			*(UINT16*)(rawData +6) = ((battCellU[27]>>8) & 0xFF) | (((battCellU[27]&0xFF)<<8)&0xFF00);
-			break;
-		case	0x7C7:
-			*(UINT8*)(rawData +0) =battCellTemp[0]&0xFF;
-			*(UINT8*)(rawData +1) =battCellTemp[1]&0xFF;
-			*(UINT8*)(rawData +2) =battCellTemp[2]&0xFF;
-			*(UINT8*)(rawData +3) =battCellTemp[3]&0xFF;
-			*(UINT8*)(rawData +4) =battCellTemp[4]&0xFF;
-			*(UINT8*)(rawData +5) =battCellTemp[5]&0xFF;
-			*(UINT8*)(rawData +6) =battCellTemp[6]&0xFF;
-			*(UINT8*)(rawData +7) =battCellTemp[7]&0xFF;
-			break;
-		case	0x7C8:
-			*(UINT8*)(rawData +0) =MOSTemp&0xFF;
-			*(UINT8*)(rawData +1) =packTemp&0xFF;
-			*(UINT8*)(rawData +2) =fastChargeTemp&0xFF;
-			*(UINT8*)(rawData +3) =normalChargeTemp&0xFF;
-			*(UINT8*)(rawData +4) =heatTemp1&0xFF;
-			*(UINT8*)(rawData +5) =heatTemp2&0xFF;
-			*(UINT8*)(rawData +6) =nbReservedTemp1&0xFF;
-			*(UINT8*)(rawData +7) =nbReservedTemp2&0xFF;
-			break;
-		case	0x7C9:
-			*(UINT16*)(rawData +0) = ((minCellVol>>8) & 0xFF) | (((minCellVol&0xFF)<<8)&0xFF00);
-			*(UINT16*)(rawData +2) = ((maxCellVol>>8) & 0xFF) | (((maxCellVol&0xFF)<<8)&0xFF00);
-			*(UINT16*)(rawData +4) = ((battPackVol>>8) & 0xFF) | (((battPackVol&0xFF)<<8)&0xFF00);
-			*(UINT8*)(rawData +6) =maxCellTemp&0xFF;
-			*(UINT8*)(rawData +7) =minCellTemp&0xFF;
-			break;
-		case	0x7CA:
-			*(UINT8*)(rawData +0) =battSOC&0xFF;
-			*(UINT8*)(rawData +1) =battSOH&0xFF;
-			*(UINT16*)(rawData +2) = ((battI>>8) & 0xFF) | (((battI&0xFF)<<8)&0xFF00);
-			*(UINT8*)(rawData +4) =battWorkState&0xFF;
-			*(UINT32*)(rawData +5) = ((battWarningState>>16) & 0xFF)|((battWarningState)&0xFF00)|(((battWarningState&0xFF)<<16)&0xFF0000);
-			break;
-		case	0x7CB:
-			*(UINT32*)(rawData +0) = ((nbSwVersion>>24) & 0xFF)|((nbSwVersion>>8)&0xFF00)|((nbSwVersion<<8)&0xFF0000)|(((nbSwVersion&0xFF)<<24)&0xFF000000);
-			*(UINT16*)(rawData +4) = ((nbHwVersion>>8) & 0xFF) | (((nbHwVersion&0xFF)<<8)&0xFF00);
-			*(UINT8*)(rawData +6) =bmsSwVersion&0xFF;
-			*(UINT8*)(rawData +7) =bmsHwVersion&0xFF;
-			break;
-		case	0x7CC:
-			*(UINT32*)(rawData +0) = ((battBalanceoInfo>>24) & 0xFF)|((battBalanceoInfo>>8)&0xFF00)|((battBalanceoInfo<<8)&0xFF0000)|(((battBalanceoInfo&0xFF)<<24)&0xFF000000);
-			*(UINT8*)(rawData +4) =battMOSSwitchState&0xFF;
-			*(UINT8*)(rawData +5) =battHeatEnableState&0xFF;
-			break;
-		case	0x7CD:
-			*(UINT8*)(rawData +0) =reservedSignal1&0xFF;
-			*(UINT8*)(rawData +1) =reservedSignal2&0xFF;
-			*(UINT8*)(rawData +2) =reservedSignal3&0xFF;
-			*(UINT8*)(rawData +3) =reservedSignal4&0xFF;
-			*(UINT8*)(rawData +4) =reservedSignal5&0xFF;
-			*(UINT8*)(rawData +5) =reservedSignal6&0xFF;
-			*(UINT8*)(rawData +6) =reservedSignal7&0xFF;
-			*(UINT8*)(rawData +7) =reservedSignal8&0xFF;
-			break;
-		case	0x7B0:
-			*(UINT8*)(rawData +0) = (AppNVMData.battSN[0])&0xFF;
-			*(UINT8*)(rawData +1) = (AppNVMData.battSN[1])&0xFF;
-			*(UINT8*)(rawData +2) = (AppNVMData.battSN[2])&0xFF;
-			*(UINT8*)(rawData +3) = (AppNVMData.battSN[3])&0xFF;
-			*(UINT8*)(rawData +4) = (AppNVMData.battSN[4])&0xFF;
-			*(UINT8*)(rawData +5) = (AppNVMData.battSN[5])&0xFF;
-			*(UINT8*)(rawData +6) = (AppNVMData.battSN[6])&0xFF;
-			*(UINT8*)(rawData +7) = (AppNVMData.battSN[7])&0xFF;
-			break;
-		case	0x7B1:
-			*(UINT8*)(rawData +0) = (AppNVMData.battSN[8])&0xFF;
-			*(UINT8*)(rawData +1) = (AppNVMData.battSN[9])&0xFF;
-			*(UINT8*)(rawData +2) = (AppNVMData.battSN[10])&0xFF;
-			*(UINT8*)(rawData +3) = (AppNVMData.battSN[11])&0xFF;
-			*(UINT8*)(rawData +4) = (AppNVMData.battSN[12])&0xFF;
-			*(UINT8*)(rawData +5) = (AppNVMData.battSN[13])&0xFF;
-			*(UINT8*)(rawData +6) = (AppNVMData.battSN[14])&0xFF;
-			*(UINT8*)(rawData +7) = (AppNVMData.battSN[15])&0xFF;
-			break;
-		case	0x7B2:
-			*(UINT8*)(rawData +0) = (AppNVMData.battSN[16])&0xFF;
-			
-			break;
-		
-		default:
-			break;
-	}
+    OSAFILE fp = PNULL;
+    UINT32  readCount = 0;
+    UINT8   crcCheck = 0;
+    void    *pReadAppConfig = (void *)&AppDataInfo;
+	AppDataHeader AppDataHr;
+    fp = OsaFopen(APP_DATAINFO_FILE_NAME, "rb");   //read only
+    if (fp == PNULL)
+    {
+        #ifdef USING_PRINTF
+            printf(" NVM, can't open NVM: 'qxappData.nvm', use the defult value\n");
+        #endif
+        setDefaultAppDataInfo();
+        SaveAppData();
+        return;
+    }
+
+    readCount = OsaFread(&AppDataHr, sizeof(AppDataHr), 1, fp);
+    UINT8 readtimes=0;
+    while (readCount != 1 && readtimes<=5 )
+    {
+        readtimes++;
+        readCount = OsaFread(&AppDataHr, sizeof(AppDataHr), 1, fp);
+        osDelay(10);
+    }
+    if(readtimes>5)
+    {
+        OsaFclose(fp);
+        setDefaultAppDataInfo();
+        SaveAppData();
+        return;
+    }
+    if(AppDataHr.fileBodySize==sizeof(AppDataInfo))//结构体数据没变动,可直接读出
+    {
+        readCount = OsaFread(pReadAppConfig, AppDataHr.fileBodySize, 1, fp);
+        //crcCheck = OsaCalcCrcValue((UINT8 *)pReadAppConfig, sizeof(AppNVMData));
+        readtimes = 0;
+        while (readtimes<=5 && readCount != 1)
+        {
+            readtimes++;
+            readCount = OsaFread(pReadAppConfig, AppDataHr.fileBodySize, 1, fp);
+            //crcCheck = OsaCalcCrcValue((UINT8 *)pReadAppConfig, sizeof(AppNVMData));
+        }
+    }
+    else
+    {
+        setDefaultAppDataInfo();
+        readCount = OsaFread(pReadAppConfig, AppDataHr.fileBodySize, 1, fp);
+        //crcCheck = OsaCalcCrcValue((UINT8 *)pReadAppConfig, sizeof(AppNVMData));
+        readtimes = 0;
+        while (readtimes<=5 && readCount != 1)
+        {
+            readtimes++;
+            readCount = OsaFread(pReadAppConfig, AppDataHr.fileBodySize, 1, fp);
+            //crcCheck = OsaCalcCrcValue((UINT8 *)pReadAppConfig, sizeof(AppNVMData));
+        }
+        SaveAppData();
+        #ifdef USING_PRINTF
+            printf(" Struct update\n");
+        #endif
+    }
+    OsaFclose(fp);
+    return;
 }
 /*******
- * 下线检测诊断函数
+ * 配置文件保存函数
  * 输入:空
  * 输出:空
- * 处理:下线检测诊断
+ * 处理:将全局配置文件进行保存
 *******/
-
-void UDS_Service()
-{
-	static UINT8	UDSService[2];
-	static UINT8	UDSSubService[2];
-	static UINT8	UDSSubServiceActionCode[2];
-	static UINT8 	UDSSwitch = 0;
-	static UINT8   UDSDialogMode = 01;
-	static UINT32  updateDifferDataByteLen = 0;
-	//each package means 100 bytes, max PackageCounter = updateDifferDataByteLen%100 + 1
-	static UINT16  updateDifferDataPackageCounter = 0;  
-	//each slice means 4 bytes, max SliceCounter = 25;
-	static UINT32  updateDifferDataSliceCounter = 0;
-	static UINT8   UDSBattSN[BATT_SN_LEN];
-	static BOOL downloadReady = FALSE;
-	UINT8 i,j= 0;
-	UINT32 k=0;
-	INT8 ret,errorCount = 0;
-	BOOL boolRet = false;
-	static UINT8 snFlag = 0;
-	static UINT32 sliceCounterFlag = 0;
-	UINT32 tempSliceCounter=0;	
-	static UINT8 counter=0;  
-	CAN_Msg_Type UDSAnswer ={0};
-	UINT8   updateDataBuffer[100];
-	UINT32 	posCode,negCode;
-	
-	
-	//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};
-	//MEMCPY(battInfo, temp00, 94);
-	
-	//while(true)
-	//{
-		//if(UDSSwitch == 1)
-		{
-			
-			
-			for(i=0; i<2; i++)
-			{
-				UDSAnswer.DLC = 8;
-				memset(UDSAnswer.Data, 0, UDSAnswer.DLC);
-				
-				if(CanRxMsg[i].Id == 0x7A0)
-					{
-					switch (UDSService[i])
-						{
-							case 0x10:
-								if(UDSSubService[i] == 0x01)
-								{
-									if((UDSDialogMode == 0x01 || UDSDialogMode==0x02)||UDSSwitch == 0)
-									{									
-										UDSPositiveAnswer(0x04,i,0x00);
-										UDSSwitch = 0;
-										UDSDialogMode = 1;
-									}
-									else
-									{
-										UDSNegtiveAnswer(0x05,i,0xFF);
-									}
-								}
-								else if(UDSSubService[i] == 0x02)
-								{
-									UDSPositiveAnswer(0x04,i,0x00);
-									UDSSwitch = 1;
-									UDSDialogMode = 2;									
-								}
-								else if(UDSSubService[i] == 0x03)
-								{
-									if(UDSSwitch == 1)
-									{
-										UDSPositiveAnswer(0x04,i,0x00);
-										UDSSwitch = 1;
-										UDSDialogMode = 3;
-			                		}
-			                		else
-			                		{
-										UDSNegtiveAnswer(0x05,i,0xFF);
-			                		}
-
-								}
-								else if(UDSSubService[i] == 0x11)   //make NB software reset
-								{
-									if(UDSDialogMode == 2)
-									{
-										UDSPositiveAnswer(0x04,i,0x00);										
-										osDelay(100);
-										appSaveNVMData();
-										appSetCFUN(0);
-										osDelay(1000);
-			                			EC_SystemReset();
-			                		}
-			                		else
-			                		{
-										UDSNegtiveAnswer(0x05,i,0xFF);  
-			                		}
-								}								
-								else
-								{
-									UDSNegtiveAnswer(0x05,i,0xEE);
-								}
-								break;
-							case 0x22:
-								if(UDSDialogMode == 2)
-								{
-									if(UDSSubService[i] == 0x01)		//check the sw of NB
-									{
-										UDSPositiveAnswer(0x08,i,APPSWVERSION);
-									}
-									else if(UDSSubService[i] == 0x02)  //check the hw of NB
-									{
-										UDSPositiveAnswer(0x06,i,HWVERSION);										
-									}
-									else if(UDSSubService[i] == 0x03)  //check the SN number
-									{											
-										switch(UDSSubServiceActionCode[i])
-										{
-											case 00:
-												UDSAnswer.Id = 0x7B0;
-												
-												CANEncodeFunction(UDSAnswer.Id, UDSAnswer.Data);
-												ret = HAL_Can_Transmit(UDSAnswer);
-												
-												break;
-												
-											case 01:
-												UDSAnswer.Id = 0x7B1;
-												CANEncodeFunction(UDSAnswer.Id, UDSAnswer.Data);
-												ret = HAL_Can_Transmit(UDSAnswer);
-
-												break;
-
-											case 02:
-												UDSAnswer.Id = 0x7B2;
-												CANEncodeFunction(UDSAnswer.Id, UDSAnswer.Data);
-												ret = HAL_Can_Transmit(UDSAnswer);
-												
-												break;
-											default:
-											break;											
-										}									
-									}
-									else if(UDSSubService[i] == 0x04)  //check the batt message
-									{
-										UDSAnswer.Id = 0x7C0+UDSSubServiceActionCode[i];
-										CANEncodeFunction(UDSAnswer.Id, UDSAnswer.Data);										
-										ret = HAL_Can_Transmit(UDSAnswer);										
-									}
-									else if(UDSSubService[i] == 0x05)  //check the enviroment temp of NB
-									{				
-										UINT32 temp = ((fastChargeTemp<<24)&0xFF000000) | ((normalChargeTemp<<16)&0xFF0000) | ((heatTemp1<<8)&0xFF00) | (heatTemp2&0xFF);				
-										UDSPositiveAnswer(0x08,i,temp);  //ntcvalue										
-									}									
-									else if(UDSSubService[i] == 0x06)	//check the tcp link of NB
-									{
-										UDSPositiveAnswer(0x06,i,TcpSendLen);  //TcpconnectStatus
-									}
-									else if(UDSSubService[i] == 0x07)  //check the GPS link of NB
-									{
-										posCode =(UINT32) PosFlag;
-										UDSPositiveAnswer(0x05,i,posCode);  //gps satellite num(uint8), should be modified
-									}	
-									else
-									{
-										UDSNegtiveAnswer(0x05,i,0xEE);
-									}
-								}
-								else	//the service is not surpported in current dialog mode
-								{
-									UDSNegtiveAnswer(0x05,i,0xFF);
-								}
-								break;
-							case 0x2E:   //write service
-								if(UDSDialogMode == 2)
-								{
-									if(UDSSubService[i] == 0x03)  // write the battSN
-									{
-										if(UDSSubServiceActionCode[i] == 0x00 && snFlag == 0x00)
-										{
-											for(j=0; j<5; j++)
-											{
-												UDSBattSN[j+5*0] = CanRxMsg[i].Data[j+3];
-											}	
-											snFlag = snFlag|0x01;
-										}
-										else if(UDSSubServiceActionCode[i] == 0x01 && snFlag == 0x01)
-										{
-											for(j=0; j<5; j++)
-											{
-												UDSBattSN[j+5*1] = CanRxMsg[i].Data[j+3];
-											}
-											snFlag = snFlag|0x02;
-										}
-										else if(UDSSubServiceActionCode[i] == 0x02 && snFlag == 0x03)
-										{
-											for(j=0; j<5; j++)
-											{
-												UDSBattSN[j+5*2] = CanRxMsg[i].Data[j+3];
-											}
-											snFlag = snFlag|0x04;
-										}
-										else if(UDSSubServiceActionCode[i] == 0x03 && snFlag == 0x07)
-										{
-											for(j=0; j<2; j++)
-											{
-												UDSBattSN[j+5*3] = CanRxMsg[i].Data[j+3];
-											}
-											snFlag = snFlag|0x08;
-										}
-
-										if(snFlag == 0x0F)
-										{
-											snFlag = 0;
-											UDSPositiveAnswer(0x04,i,00);
-											MEMCPY(AppConfigData.battSN, UDSBattSN, BATT_SN_LEN);
-											AppConfigData.EOLState = 1; //SN号写入完成,表明已经进行过下线配置
-											appSaveNVMData();
-										}
-										else
-										{
-											UDSAnswer.Id = 0x7A8;											
-											UDSAnswer.Data[0] = 0x05;
-											UDSAnswer.Data[1] = 0x3E;
-											UDSAnswer.Data[2] = UDSService[i];
-											UDSAnswer.Data[3] = UDSSubService[i];
-											UDSAnswer.Data[4] = snFlag;	
-											
-											ret = HAL_Can_Transmit(UDSAnswer);
-										}										
-									}
-									else if(UDSSubService[i] == 0x0F) //write the update config:updateDifferDataByteLen
-									{										
-										updateDifferDataByteLen = (CanRxMsg[i].Data[2]<<16)|(CanRxMsg[i].Data[3]<<8)|CanRxMsg[i].Data[4];										
-										UDSPositiveAnswer(0x04,i,00);
-									}
-									else
-									{
-										UDSNegtiveAnswer(0x05,i,0xEE);
-										
-									}
-									break;
-								}								
-								else
-								{
-									UDSNegtiveAnswer(0x05,i,0xFF);
-								}
-								break;
-							case 0x31:  //clear the flash service
-								if(UDSDialogMode == 3)  
-								{
-									if(UDSSubService[i] == 0x0F)  //clear the fota flash
-									{
-										if(UDSSubServiceActionCode[i] == 0x01)
-										{
-											//Clear the Flash
-											boolRet = UDSClearFotaDownloadRegion();
-											//if ok											
-											UDSPositiveAnswer(0x05,i,(UINT32)boolRet);
-										}
-										else
-										{
-											UDSNegtiveAnswer(0x06,i,UDSSubServiceActionCode[i]<<8|0xFF);  //重点测试
-										}										
-									}
-									else
-									{
-										UDSNegtiveAnswer(0x05,i,0xEE);  //the subservice is not surpported
-									}
-								}
-								else
-								{
-									UDSNegtiveAnswer(0x05,i,0xFF);  //the survie is not surpported in current dialog mode
-								}
-								break;
-							case 0x34:  //prepare for some one process
-								if(UDSDialogMode == 3)
-								{
-									if(UDSSubService[i] == 0x0F)  //ask for download update data
-									{									
-										boolRet = UDSAskforDownLoadData();
-										//if ok											
-										UDSPositiveAnswer(0x05,i,(UINT32)boolRet);
-									}
-									else
-									{
-										UDSNegtiveAnswer(0x05,i,0xEE);
-									}
-								}
-								else
-								{
-									UDSNegtiveAnswer(0x05,i,0xFF);
-								}
-								break;
-							case 0x36:  //download the update data
-								if(UDSDialogMode == 3)
-								{
-									if(downloadReady == TRUE)
-									{
-										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];
-											}								
-											
-											if(updateDifferDataSliceCounter>0)
-											{
-												sliceCounterFlag = sliceCounterFlag |(0x01<<updateDifferDataSliceCounter);
-											}
-											else
-											{
-												sliceCounterFlag = sliceCounterFlag |0x01;
-											}
-											/*
-										#ifdef USING_PRINTF
-											printf("sliceCounterFlag = %x  counter=%d\n",sliceCounterFlag,counter);
-										#endif
-										*/											
-										}
-										else
-										{
-											counter--;
-										}
-										
-										if(counter == 25)
-										{
-											counter = 0;											
-											if(sliceCounterFlag == 0x1FFFFFF)  //received all the 25 message of current package
-											{															
-												//write the buffer(100 byte) to flash
-												ret = (uint8_t)BSP_QSPI_Write_Safe(updateDataBuffer,FLASH_FOTA_REGION_START+(updateDifferDataPackageCounter)*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)*100,100);
-													ret = (uint8_t)BSP_QSPI_Write_Safe(updateDataBuffer,FLASH_FOTA_REGION_START+(updateDifferDataPackageCounter)*100,100);
-												}
-												if(ret == QSPI_OK)  // write successed
-												{
-													UDSPositiveAnswer(0x08,i,tempSliceCounter);	
-				/*									
-													#if 0
-													#ifdef USING_PRINTF
-														UINT8 checksum0,checksum1=0;
-														printf("updateDifferDataPackageCounter = %d\n",updateDifferDataPackageCounter);
-														printf("updateDataBuffer write:\n");
-														for(k=0;k<100;k++)
-														{
-															printf("%x  ",updateDataBuffer[k]);
-															checksum0+=updateDataBuffer[k];
-														}
-														printf("\n");	
-														
-														UINT8 readFlash=0;
-														printf("updateDataBuffer read:\n");
-														
-														for(k=0;k<100;k++)
-														{
-															BSP_QSPI_Read_Safe(&readFlash,FLASH_FOTA_REGION_START+updateDifferDataPackageCounter*100+k, 1);
-															printf("%2x ",readFlash);
-															checksum1+=readFlash;
-														}
-														printf("\n");
-														if(checksum0 == checksum1)
-														{
-															printf("write succeed!\nchecksum0 = %2x,checksum1 = %2x\n",checksum0,checksum1);
-														}
-														else
-														{
-															printf("write fail!\nchecksum0 = %2x,checksum1 = %2x\n",checksum0,checksum1);
-														}
-														//printf("write succeed, next updateDifferDataPackageCounter = %x\n",updateDifferDataPackageCounter+1);
-													#endif
-													#endif
-*/
-													memset(updateDataBuffer,0, 100);
-													sliceCounterFlag = 0x0;
-													updateDifferDataPackageCounter++;
-												}
-												else  //write fail
-												{
-													UDSNegtiveAnswer(0x08,i,0xFFFFFFFF);  //failed to write this package to flash
-													/*
-													#if 0
-													#ifdef USING_PRINTF
-														printf("write flash fail!! updateDifferDataPackageCounter = %d\n",updateDifferDataPackageCounter);
-														printf("ERROR: sliceCounterFlag=%x\n",sliceCounterFlag);
-													#endif
-													#endif
-													*/
-												}
-											}
-											else
-											{
-												//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);
-												#ifdef USING_PRINTF
-														printf("ERROR: package lose one or mo message!! sliceCounterFlag = %x\n",sliceCounterFlag);
-												#endif
-												//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
-										counter = 0;
-									}
-								}
-								else
-								{
-									UDSNegtiveAnswer(0x05,i,0xFF);  //the service is not surpported in current dialog mode
-								}
-								break;
-							case 0x37:  //exit some one process
-								if(UDSDialogMode == 3)
-								{
-									if(UDSSubService[i] == 0x0F)  // exit the download 
-									 {
-										UDSPositiveAnswer(0x04,i,0x00);
-									/*
-									#if 0
-									#ifdef USING_PRINTF
-										printf("CAN update data:\n");
-										UINT8 ts=0;
-										for(UINT32 jjj =0;jjj<updateDifferDataByteLen;jjj++)
-										{											
-											BSP_QSPI_Read_Safe(&ts,FLASH_FOTA_REGION_START+jjj,1);
-											printf("%x ",ts);
-										}
-										printf("\n");
-									#endif
-									#endif
-									*/
-										downloadReady = FALSE;
-									 }
-									 else
-									 {
-										UDSNegtiveAnswer(0x05,i,0xEE);
-									}
-								}
-								else
-								{
-									UDSNegtiveAnswer(0x05,i,0xFF);
-								}
-								break;
-							default:								
-								UDSNegtiveAnswer(0x04,i,0xFF);  //the service is not surpported
-								break;
-								
-						}
-					}
-				}
-			}
-	//}
-	for(i=0; i<2; i++)
-	{
-		UDSService[i] = 0;
-		UDSSubService[i] = 0;
-	}
-}
-
-static 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;
-}
-
-static 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;
-}
-
-static BOOL UDSAskforDownLoadData()
-{
-	updateDifferDataPackageCounter = 0;
-	downloadReady = true;
-	
-	return TRUE;
-}
-
-static BOOL UDSClearFotaDownloadRegion()
+void SaveAppConfig(void)
 {
-	UINT8 ret = FALSE;
-	ret = BSP_QSPI_Erase_Safe(FLASH_FOTA_REGION_START, 0x46000); //512k-32k -200k = 280k -> 0x46000
-	
-	if(ret == QSPI_OK)
-	{
-		return TRUE;
-	}
-	else
+    OSAFILE fp = PNULL;
+    UINT32  writeCount = 0;
+    AppConfigDataHeader    AppConfigHr;   //4 bytes
+    /*
+     * open the NVM file
+    */
+    fp = OsaFopen(APP_CONFIG_FILE_NAME, "wb");   //read & write
+    if(OsaFseek(fp, 0, SEEK_SET) != 0)
 	{
-		return FALSE;
+        #ifdef USING_PRINTF
+				printf("Seek file failed [%d] \r\n",__LINE__);
+	    #endif
+	    OsaFclose(fp);
+        return;
 	}
+    if (fp == PNULL)
+    {
+	   #ifdef USING_PRINTF
+			printf(" NVM, can't open/create NVM: 'qxappconfig.nvm', save NVM failed\n");
+		#endif
+        return;
+    }
+    /*
+     * write the header
+    */
+    AppConfigHr.fileBodySize   = sizeof(AppNVMData);   
+    AppConfigHr.checkSum       = OsaCalcCrcValue((UINT8 *)&AppNVMData, sizeof(AppNVMData));
+    writeCount = OsaFwrite(&AppConfigHr, sizeof(AppConfigHr), 1, fp);
+    if (writeCount != 1)
+    {
+		#ifdef USING_PRINTF
+					printf(" NVM: 'qxappconfig.nvm', write the file header failed\n");
+		#endif
+        OsaFclose(fp);
+        return;
+    }
+    /*
+     * write the file body
+    */
+    AppNVMData.appDataModify = FALSE;
+    writeCount = OsaFwrite(&AppNVMData, sizeof(AppNVMData), 1, fp);
+    if (writeCount != 1)
+    {
+        #ifdef USING_PRINTF
+					printf(" NVM: 'qxappconfig.nvm', write the file body failed\n");
+		#endif
+    }
+    OsaFclose(fp);
+    return;
 }
 /*******
- * RT查询函数
- * 输入:R
- * 输出:T
- * 处理:利用R反查T
+ * 配置文件加载函数
+ * 输入:空
+ * 输出:空
+ * 处理:将全局配置文件进行加载
 *******/
-UINT8 LookUpRTtable(UINT32 R_value)
+void LoadAppConfig(void)
 {
-	UINT8 Temp_Table1[23] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22};
-	UINT8 Temp_Table2[217] = {23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,
-	49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,
-	84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,
-	115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,
-	142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,
-	169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,
-	196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,
-	223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239};
-	UINT32 R_Table1[23] = {202269,191063,180554,170694,161438,152746,144580,136905,129687,122898,116508,
-						110493,104827,99488,94455,89710,85233,81008,77019,73252,69693,66329,63148};
-	UINT16 R_Table2[217] = {
-	60140,57293,54599,52049,49633,47343,45174,43117,41166,39315,37558,35891,34307,32802,31373,
-	30014,28722,27493,26324,25211,24152,23144,22183,21268,20395,19564,18771,18015,17293,16604,
-	15947,15319,14720,14147,13600,13077,12577,12098,11641,11203,10784,10383,10000,9632,9280,8942,
-	8619,8309,8012,7727,7454,7191,6940,6698,6466,6244,6030,5825,5627,5438,5255,5080,4911,4749,4593,
-	4443,4299,4160,4026,3898,3773,3654,3539,3428,3321,3218,3119,3023,2931,2841,2755,2672,2592,2515,
-	2441,2369,2299,2232,2167,2104,2044,1985,1928,1874,1821,1769,1720,1672,1626,1581,1537,1495,1455,
-	1415,1377,1340,1305,1270,1236,1204,1172,1142,1112,1083,1056,1029,1002,977,952,928,905,883,861,
-	839,819,799,779,760,742,724,707,690,674,658,642,627,613,599,585,571,558,546,533,521,509,498,487,
-	476,466,455,445,436,426,417,408,399,391,382,374,366,359,351,344,337,330,323,316,310,304,298,292,
-	286,280,274,269,264,259,254,249,244,239,234,230,226,221,217,213,209,205,201,198,194,190,187,183,
-	180,177,174,171,168,165,162,159,156,153,151,148,145,143,141,138,136,133,131,129,127,125};
-	if(R_value>R_Table2[0])
-	{
-		if(R_value<=R_Table1[22])//判断是否超出表尾
-		{
-			return Temp_Table1[22];
-		}
-		else if(R_value>=R_Table1[0])//判断是否超出表头
-		{
-			return Temp_Table1[0];
-		}
-		else 
-		{
-			for(int i=0;i<23-1;i++)
-			{
-				if ((R_value<=R_Table1[i])&&(R_value>R_Table1[i+1]))//中间判断
-				{
-					return Temp_Table1[i];
-				}
-				else
-				{
-					continue;
-				} 
-				
-			}
-		}
-	}//R-1
-	else
-	{
-		if(R_value<=R_Table2[216])//判断是否超出表尾
-		{
-			return Temp_Table2[216];
-		}
-		else if(R_value>=R_Table2[0])//判断是否超出表头
-		{
-			return Temp_Table2[0];
-		}
-		else 
-		{
-			for(int i=0;i<217-1;i++)
-			{
-				if ((R_value<R_Table2[i])&&(R_value>=R_Table2[i+1]))//中间判断
-				{
-					return Temp_Table2[i+1];
-				}
-				else
-				{
-					continue;
-				} 
-				
-			}
-		}
-	}//R-2
+    OSAFILE fp = PNULL;
+    UINT32  readCount = 0;
+    AppConfigDataHeader    AppConfigHr;
+    UINT8   crcCheck = 0;
+    void    *pReadAppConfig = (void *)&AppNVMData;
+    /*
+     * open the NVM file
+    */
+    fp = OsaFopen(APP_CONFIG_FILE_NAME, "rb");   //read only
+    
+    if (fp == PNULL)
+    {
+	#ifdef USING_PRINTF
+		printf(" NVM, can't open NVM: 'qxappConfig.nvm', use the defult value\n");
+	#endif
+        setDefaultAppConfigData();
+        SaveAppConfig();
+        return;
+    }
+    /*
+     * read the file header
+    */
+    readCount = OsaFread(&AppConfigHr, sizeof(AppConfigHr), 1, fp);
+    UINT8 readtimes=0;
+    while (readCount != 1 && readtimes<=5 )
+    {
+        #ifdef USING_PRINTF
+            printf("NVM: 'qxappconfig.nvm', can't read header, return: %d, use the defult value \n");
+        #endif
+        readtimes++;
+        readCount = OsaFread(&AppConfigHr, sizeof(AppConfigHr), 1, fp);
+        osDelay(10);
+    }
+    if(readtimes>5)
+    {
+        OsaFclose(fp);
+        setDefaultAppConfigData();
+        SaveAppConfig();
+        return;
+    }
+    if (AppConfigHr.fileBodySize != sizeof(AppNVMData))  //file version is the same, but NVM file size not right
+    {
+		#ifdef USING_PRINTF
+				printf("MW NVM: 'midwareconfig.nvm', file body size not right: (%u/%u), use the defult value",
+        		            AppConfigHr.fileBodySize, sizeof(AppNVMData));
+		#endif
+        OsaFclose(fp);
+        setDefaultAppConfigData();
+        SaveAppConfig();
+        return;
+    }
+    /*
+     * read the file body
+    */
+    readCount = OsaFread(pReadAppConfig, AppConfigHr.fileBodySize, 1, fp);
+    crcCheck = OsaCalcCrcValue((UINT8 *)pReadAppConfig, sizeof(AppNVMData));
+	readtimes = 0;
+    //while (readtimes<=5 && crcCheck != AppConfigHr.checkSum)
+    while (readtimes<=5 && readCount != 1)
+    {
+        readtimes++;
+        readCount = OsaFread(pReadAppConfig, AppConfigHr.fileBodySize, 1, fp);
+        crcCheck = OsaCalcCrcValue((UINT8 *)pReadAppConfig, sizeof(AppNVMData));
+    }
+    if (readtimes>5)
+    {
+		#ifdef USING_PRINTF
+			printf(" NVM: 'qxappconfig.nvm', can't read body, or body not right, (%u/%u), use the defult value\n ",
+						crcCheck, AppConfigHr.checkSum);
+		#endif
+        OsaFclose(fp);
+        setDefaultAppConfigData();
+        SaveAppConfig();
+        return;
+    }
+    OsaFclose(fp);
+    return;
 }
 /*******
- * 获取故障代码函数
- * 输入:故障代码列表,长度
- * 输出:第一个故障代码
- * 处理:获取第一个,将后面依次前移
+ * 配置文件初始化函数
+ * 输入:空
+ * 输出:空
+ * 处理:将全局配置文件进行初始化
+ * 注意:配置数据不增加数据项,若增加数据项,需要修改加载函数
 *******/
-UINT16 GetErrorNum(UINT16 *ErrorArray,UINT8 Errorlen)
+static void setDefaultAppConfigData(void)
 {
-	UINT16 OutNum;
-	OutNum = *(ErrorArray);
-	for(int i=0;i<Errorlen-1;i++)
-	{
-		*(ErrorArray+i) = *(ErrorArray+i+1);
-		if(*(ErrorArray+i+1)==0)
-			break;
-	}
-	return OutNum;
+    UINT8  i = 0;
+    memset(&AppNVMData, 0x00, sizeof(AppConfigDataType));
+	AppNVMData.appDataModify = FALSE;
+	memcpy(AppNVMData.battSN,DEFAULT_SN,BATT_SN_LEN);
+	AppNVMData.EOLState = EOLSTATE; 
+    AppNVMData.AppSwVersion = APPSWVERSION;
+	AppNVMData.BattCellCount = 17;//默认17串
+	AppNVMData.BattTempCount = 5;//默认5个温度
+    return;
 }
 /*******
- * 放入故障代码函数
- * 输入:故障代码列表,长度,故障码
- * 输出:状态
- * 处理:将故障代码放入故障列表,若存在则不放入,若不存在,则放入
+ * 运行数据初始化函数
+ * 输入:空
+ * 输出:空
+ * 处理:将运行数据进行初始化
+ * 注:每增加一项数据项,需要增加默认值
 *******/
-UINT8 PutErrorNum(UINT16 *ErrorArray,UINT8 Errorlen,UINT16 ErrorNum)
+static void setDefaultAppDataInfo(void)
 {
-	for(int i=0;i<Errorlen;i++)
-	{
-		if(*(ErrorArray+i)==0)
-		{
-			*(ErrorArray+i) = ErrorNum;
-			return 0;
-		}
-		else
-		{
-			if(*(ErrorArray+i)==ErrorNum)
-			{
-				return 1;
-			}
-			else
-			{
-				continue;
-			}
-		}
-	}
-	return 2;
+    memset(&AppDataInfo, 0x00, sizeof(AppDataInfo));
+    AppDataInfo.appDataModify = false;
+    AppDataInfo.BattInfoSendFreqHigh = 	10;
+    AppDataInfo.BattInfoSendFreqNomal = 30;
+    AppDataInfo.BattInfoSendFreqLow = 	60;
+    AppDataInfo.PosInfoSendFreqHigh = 	10;
+    AppDataInfo.PosInfoSendFreqNormal = 30;
+    AppDataInfo.PosInfoSendFreqLow = 	60;
+    AppDataInfo.AccMileage = 0;
+    AppDataInfo.SysReStartCount = 0;
+	AppDataInfo.RelayControl = 0;
+	AppDataInfo.BattLock = 0;
+	AppDataInfo.BattStolenFlag = 0;
+    return;
 }
 /*******
- * 字符串处理函数
- * 输入:需要删除的字符,以及目标字符串
- * 输出:
- * 处理:将指定字符串删掉
+ * Fota升级处理函数
+ * 输入:网络接收的数据,网络链接ID
+ * 输出:0x00表示数据未准备完成,0x01表示数据模块OTA数据准备完成,0x88表示BMS的OTA数据准备完成
+ * 处理:将接收的数据进行校验,搬运至升级区域,并进行应答
 *******/
-void strdel(char * str,char c)
+UINT16  encryptionAlgorithm (UINT16 plainText)
 {
-	char *p = str;
-	while(*str)
+	UINT16 cipherText = 1; 
+	UINT16 privateKeyE = 37507;
+	UINT16 privateKeyN = 10961;
+	plainText = plainText % privateKeyN;
+	while(privateKeyE >0)
 	{
-		if(*str!=c)
-			*p++ = *str;
-		str++;
+	if(privateKeyE % 2 ==1)
+	{   
+	cipherText =  ( cipherText * plainText) % privateKeyN;
+	}
+	privateKeyE = privateKeyE/2;  
+	plainText = (plainText * plainText) % privateKeyN;
 	}
-	*p = '\0';
+	return cipherText;
 }
-/*******
- * 位置处理函数
- * 输入:经纬度
- * 输出:经纬度
- * 处理:将经纬度进行处理
-*******/
-UINT32 location_handle(char *in1)
-{
-	UINT32 location_temp;
-	UINT32 location_degree;
-	UINT32 location_min;
-	location_temp = atol(in1);
-	location_degree = location_temp/(1e7);
-	location_degree = location_degree*(1e6);
-	location_min = location_temp-location_degree*10;
-	location_min = location_min/6;
-	location_temp = location_degree+location_min;
-	return location_temp;
-}
+// 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;
+// }

+ 2 - 2
src/AppSignal.c

@@ -69,7 +69,7 @@ UINT32  TimeCounter = 0x00;             //主任务定时器计数
 /**declear the Handle vars**/
 osMutexId_t Error_Mutex = NULL;         //故障互锁句柄
 osMutexId_t GpsMutex = NULL;           //GPS互锁句柄
-
+QueueHandle_t UartWriteCmdHandle = NULL;//Uart写命令队列句柄
 
 /**declear other vars**/   
 
@@ -79,4 +79,4 @@ AppDataBody             AppDataInfo;
 
 AppSocketConnectionContext socContext;  //网络连接状态
 
-
+process_app 		    gProcess_app;//全局工作状态

+ 3 - 3
src/AppTaskControl.c

@@ -10,7 +10,7 @@
 
 static StaticTask_t                 gProcess_Control_Task_t;
 static UINT8                        gProcess_Control_TaskStack[PROC_CONTROL_TASK_STACK_SIZE];
-static osThreadId_t                 LedTaskId = NULL;
+static osThreadId_t                 ControlTaskId = NULL;
 static process_control 		        gProcess_Control_Task = PROCESS_CONTROL_STATE_IDLE;
 #define PROC_CONTROL_STATE_SWITCH(a)    (gProcess_Control_Task = a)
 
@@ -26,8 +26,8 @@ void AppTaskControlInit(void *arg)
 {
     osThreadAttr_t task_attr;
     memset(&task_attr,0,sizeof(task_attr));
-    memset(gProcess_Led_TaskStack, 0xA5, PROC_CONTROL_TASK_STACK_SIZE);
-    task_attr.name = "Led_Task";
+    memset(gProcess_Control_TaskStack, 0xA5, PROC_CONTROL_TASK_STACK_SIZE);
+    task_attr.name = "Control_Task";
     task_attr.stack_mem = gProcess_Control_TaskStack;
     task_attr.stack_size = PROC_CONTROL_TASK_STACK_SIZE;
     task_attr.priority = osPriorityBelowNormal;

+ 9 - 6
src/AppTaskGps.c

@@ -1,7 +1,7 @@
 /****************************************************************************
  *
  * Copy right:   Qx.Chen jie
- * File name:    GpsTask.c
+ * File name:    AppTaskGps.c
  * Description:  Gps处理任务和三轴数据处理及获取
  * History:      2021-03-07 2021-07-15
  * Version:      V3.0
@@ -23,14 +23,14 @@ static void GpsTask(void* arg)
         osDelay(1000);
     }
 }
-INT32 AppTaskGpsInit(void)
+void AppTaskGpsInit(void *arg)
 {
 
 	if(norGpsHandle == NULL)
 	{
 		norGpsHandle = osMessageQueueNew(1,sizeof(gpsReqMsg), NULL);
 		if(norGpsHandle == NULL)
-			return 1;
+			return;
 	}
 	if(gpsTaskHandle == NULL)
 	{
@@ -45,8 +45,11 @@ INT32 AppTaskGpsInit(void)
 		memset(& gpsTaskStack, 0xa5, GPS_TASK_STACK_SIZE);
 		gpsTaskHandle = osThreadNew(GpsTask , NULL,&task_attr);
 		if(gpsTaskHandle == NULL)
-			return 1;
+			return;
 	}
-	
-	return 0;
 }
+void AppTaskGpsDeInit(void *arg)
+{
+    osThreadTerminate(gpsTaskHandle);
+    gpsTaskHandle = NULL;
+}

+ 49 - 537
src/AppTaskMain.c

@@ -6,119 +6,71 @@
  * History:      2021-07-05
  * Version:      V3.0
  ****************************************************************************/
-#include "MainTask.h"
+#include "AppTaskMain.h"
 
 static StaticTask_t           gProcess_Main_Task_t;
 static UINT8                  gProcess_Main_TaskStack[PROC_MAIN_TASK_STACK_SIZE];
 static osThreadId_t           MainTaskId = NULL;
 static process_Main 		    gProcess_Main_Task;
 #define PROC_MAIN_STATE_SWITCH(a)  (gProcess_Main_Task = a)
-
-//函数声明区
-static void appBeforeHib(void *pdata, slpManLpState state);
-static void appAfterHib(void *pdata, slpManLpState state);
-static void appBeforeSlp1(void *pdata, slpManLpState state);
-static void appAfterSlp1(void *pdata, slpManLpState state);
-static void appBeforeSlp2(void *pdata, slpManLpState state);
-static void appAfterSlp2(void *pdata, slpManLpState state);
-static void montior_timer_callback(TimerHandle_t xTimer);
-static void work_timer_callback(TimerHandle_t xTimer);
-
-static void setDefaultAppDataValue(void);
-static void appLoadConfig(void);
-static void appSaveConfig(void);
-static void appGetNVMSavedData(void);
-static void appSaveNVMData(void);
-static void setDefaultAppDataInfo(void);
-static void LoadAppDataInfo(void);
-static void appSaveDataInfo(void);
-//主线程任务区
+#define PROC_APP_STATE_SWITCH(a)  (gProcess_app = a)
+static volatile BOOL WorkTimerEnd = FALSE;
+static volatile BOOL SleepTimerEnd = FALSE;
+void monitor_timer_callback(TimerHandle_t xTimer);
+void work_timer_callback(TimerHandle_t xTimer);
+void sleep_timer_callback(TimerHandle_t xTimer);
 static void MainTask(void* arg)
 {
-    /***GsensorInit***/
-    GsensorI2CHandler(GsensorI2CCallback);
-    GsensorInit();
-    /***calibrate the ADC Collect***/
-    UINT32  param = 0;
-    AdcSendReq(ADC_REQ_BITMAP_VBAT_CALI,&param,1,ADC_GET_RESULT_TIMOUT);						
-	AdcSendReq(ADC_REQ_BITMAP_INRES_CALI,&param,1,ADC_GET_RESULT_TIMOUT);
-    /***Sleep***/
-    UINT32 inParam = 0xAABBCCDD;
-    slpManSetPmuSleepMode(true,SLP_HIB_STATE,false);
-    UINT8 MainSlpHandler=0xff;
-    slpManApplyPlatVoteHandle("MainSlp",&MainSlpHandler);
-    slpManPlatVoteDisableSleep(MainSlpHandler, SLP_SLP2_STATE);
-    slpManRegisterUsrdefinedBackupCb(appBeforeHib,&inParam,SLPMAN_HIBERNATE_STATE);
-    slpManRegisterUsrdefinedRestoreCb(appAfterHib,NULL,SLPMAN_HIBERNATE_STATE);
-    slpManRegisterUsrdefinedBackupCb(appBeforeSlp1,NULL,SLPMAN_SLEEP1_STATE);
-    slpManRegisterUsrdefinedRestoreCb(appAfterSlp1,NULL,SLPMAN_SLEEP1_STATE);
-    slpManRegisterUsrdefinedBackupCb(appBeforeSlp2,NULL,SLPMAN_SLEEP2_STATE);
-    slpManRegisterUsrdefinedRestoreCb(appAfterSlp2,NULL,SLPMAN_SLEEP2_STATE);
-    slpManSlpState_t slpstate = slpManGetLastSlpState(); 
-
-    TimerHandle_t montior_timer = NULL;
+    
+    TimerHandle_t monitor_timer = NULL;
     TimerHandle_t work_timer = NULL;
-    slpManWakeSrc_e Wakeup_source;
-    Wakeup_source =  slpManGetWakeupSrc();//获取唤醒源
-    #ifdef USING_PRINTF1
-        printf("Wakeup_source:%d \n",Wakeup_source);
-    #endif
-    #ifdef DEBUGLOG
-    Debug_printf("Wkp-%d \n",Wakeup_source);
-    #endif
-
-    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);
-	NetSocDisplay(LED_SOC_0,LED_TURN_OFF);
+    TimerHandle_t sleep_timer = NULL;
+    monitor_timer = xTimerCreate("monitor_timer", 100 / portTICK_RATE_MS, pdTRUE, NULL, monitor_timer_callback);
+    work_timer    = xTimerCreate("work_timer", 5*60*1000 / portTICK_RATE_MS, pdTRUE, NULL, work_timer_callback);
+    sleep_timer   = xTimerCreate("sleep_timer", 60*60*1000 / portTICK_RATE_MS, pdTRUE, NULL, sleep_timer_callback);
+    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);
+    xTimerStart(monitor_timer, 0);
+
+    //配置文件及运行数据加载
+    LoadAppConfig();
+    LoadAppData();
     while(true)
     {
+        osDelay(100);
         switch(gProcess_Main_Task)
         {
             case PROCESS_STATE_IDLE:
             {
-                appGetNVMSavedData();
-                LoadAppDataInfo();
-                TimeCounter = 0;
-                xTimerStart(montior_timer, 0);
+                PROC_APP_STATE_SWITCH(IDLE);
                 xTimerStart(work_timer, 0);
                 PROC_MAIN_STATE_SWITCH(PROCESS_STATE_WORK);
                 break;
             }
             case PROCESS_STATE_WORK:
             {
-                /***if Tcp connect error 5 times then save data and restart sys***/
-                if(TcpErrorcount>=5)
+                PROC_APP_STATE_SWITCH(WORK);
+                if(WorkTimerEnd)
                 {
-                    AppDataInfo.appDataModify = true;
-                    AppDataInfo.SysReStart = AppDataInfo.SysReStart +1;
-                    if(AppDataInfo.SysReStart>=0xfffe)
-                    {
-                        AppDataInfo.SysReStart = 0;
-                    }
-                    appSaveNVMData();
-                    appSaveDataInfo();
-                    appSetCFUN(0);
-                    osDelay(1000);
-                    EC_SystemReset();
+                    PROC_MAIN_STATE_SWITCH(PROCESS_STATE_LISTEN);
+                    xTimerStop(work_timer,0);
+                    WorkTimerEnd = FALSE;
                 }
-                
-                osDelay(1000);
                 break;
             }
-            case PROCESS_STATE_SLEEP:
+            case PROCESS_STATE_LISTEN:
             {
-                if(NB_Fota_update_flag)
+                PROC_APP_STATE_SWITCH(LISTEN);
+                xTimerStart(sleep_timer, 0);
+                if(SleepTimerEnd)
                 {
-                    appSaveNVMData();
-                    appSaveDataInfo();
-                    appSetCFUN(0);
-                    osDelay(1000);
-                    EC_SystemReset();
+                    PROC_MAIN_STATE_SWITCH(PROCESS_STATE_IDLE);
+                    xTimerStop(sleep_timer,0);
+                    SleepTimerEnd = FALSE;
                 }
                 break;
             }
@@ -130,6 +82,22 @@ static void MainTask(void* arg)
         }
     }
 }
+void monitor_timer_callback(TimerHandle_t xTimer)
+{
+    TimeCounter++;
+    if(TimeCounter>100000*100)
+    {
+        TimeCounter=0;
+    }
+}
+void work_timer_callback(TimerHandle_t xTimer)
+{
+    WorkTimerEnd = true;
+}
+void sleep_timer_callback(TimerHandle_t xTimer)
+{
+    SleepTimerEnd = true;
+}
 void AppTaskMainInit(void *arg)
 {
     #ifndef USING_PRINTF
@@ -155,460 +123,4 @@ void AppTaskMainDeInit(void *arg)
     osThreadTerminate(MainTaskId);
     MainTaskId = NULL;
 }
-//定时器回调函数区
-void montior_timer_callback(TimerHandle_t xTimer)
-{
-    TimeCounter++;
-    if(TimeCounter>0xfffffffd)
-    {
-        TimeCounter=0;
-    }
-}
-void work_timer_callback(TimerHandle_t xTimer)
-{
-    #ifdef USING_PRINTF
-        printf("Hello work Timer!\n");
-    #endif
-}
-
-//睡眠进出函数区
-static void appBeforeHib(void *pdata, slpManLpState state)
-{
-    //uint32_t *p_param = (uint32_t *)pdata;
-
-    #ifdef USING_PRINTF
-        printf("Before Hibernate:%d \n",state);
-    #endif
-	slpManAONIOLatchEn(AonIOLatch_Enable);
-
-}
-static void appAfterHib(void *pdata, slpManLpState state)
-{
-    #ifdef USING_PRINTF
-        printf("Try Hibernate Failed:%d \n",state);
-    #endif
-}
-static void appBeforeSlp1(void *pdata, slpManLpState state)
-{
-    #ifdef USING_PRINTF
-        printf("Before Sleep1:%d \n",state);
-    #endif
-	slpManAONIOLatchEn(AonIOLatch_Enable);
-}
-static void appAfterSlp1(void *pdata, slpManLpState state)
-{
-    #ifdef USING_PRINTF
-        printf("After Sleep1:%d \n",state);
-    #endif
-}
-static void appBeforeSlp2(void *pdata, slpManLpState state)
-{
-    #ifdef USING_PRINTF
-        printf("before sleep2:%d \n",state);
-    #endif
-	slpManAONIOLatchEn(AonIOLatch_Enable);
-}
-static void appAfterSlp2(void *pdata, slpManLpState state)
-{
-    #ifdef USING_PRINTF
-        printf("sleep2 failed:%d \n",state);
-    #endif
-}
-
-
-static void appGetNVMSavedData(void)
-{
-    appLoadConfig();
-}
-
-void appSaveNVMData(void)
-{
-	appSaveConfig();
-}
-
-static void appSaveConfig(void)
-{
-    OSAFILE fp = PNULL;
-    UINT32  writeCount = 0;
-    AppConfigHeader    AppConfigHr;   //4 bytes
-
-    /*
-     * open the NVM file
-    */
-    fp = OsaFopen(APP_CONFIG_FILE_NAME, "wb");   //read & write
-    if(OsaFseek(fp, 0, SEEK_SET) != 0)
-	{
-        #ifdef USING_PRINTF
-				printf("Seek file failed [%d] \r\n",__LINE__);
-	    #endif
-	    OsaFclose(fp);
-        return;
-	}
-    if (fp == PNULL)
-    {
-	   #ifdef USING_PRINTF
-			printf(" NVM, can't open/create NVM: 'qxappconfig.nvm', save NVM failed\n");
-		#endif
-        return;
-    }
-
-    /*
-     * write the header
-    */
-    AppConfigHr.fileBodySize   = sizeof(AppNVMData);
-    AppConfigHr.version	= 	APP_CONFIG_FILE_LATEST_VERSION;    
-    AppConfigHr.checkSum       = OsaCalcCrcValue((UINT8 *)&AppNVMData, sizeof(AppNVMData));
-
-    writeCount = OsaFwrite(&AppConfigHr, sizeof(AppConfigHeader), 1, fp);
-    if (writeCount != 1)
-    {
-        //ECOMM_TRACE(UNILOG_PLA_MIDWARE, AppConfigSave_2, P_ERROR, 0,
-        //           " NVM: 'qxappconfig.nvm', write the file header failed");
-	#ifdef USING_PRINTF
-				printf(" NVM: 'qxappconfig.nvm', write the file header failed\n");
-	#endif
-
-        OsaFclose(fp);
-        return;
-    }
-
-    /*
-     * write the file body
-    */
-    AppNVMData.appDataModify = FALSE;
-    writeCount = OsaFwrite(&AppNVMData, sizeof(AppNVMData), 1, fp);
-    if (writeCount != 1)
-    {
-        //ECOMM_TRACE(UNILOG_PLA_MIDWARE, AppConfigSave_3, P_ERROR, 0,
-        //           " NVM: 'qxappconfig.nvm', write the file body failed");
-        #ifdef USING_PRINTF
-					printf(" NVM: 'qxappconfig.nvm', write the file body failed\n");
-		#endif
-    }
-    OsaFclose(fp);
-    return;
-}
-
-
-void appLoadConfig(void)
-{
-    OSAFILE fp = PNULL;
-    UINT32  readCount = 0;
-    AppConfigHeader    AppConfigHr;   //4 bytes
-    UINT8   crcCheck = 0;
-    void    *pReadAppConfig = (void *)&AppNVMData;
-    /*
-     * open the NVM file
-    */
-    fp = OsaFopen(APP_CONFIG_FILE_NAME, "rb");   //read only
-    
-    if (fp == PNULL)
-    {
-	#ifdef USING_PRINTF
-		printf(" NVM, can't open NVM: 'qxappConfig.nvm', use the defult value\n");
-	#endif
-        setDefaultAppDataValue();
-        appSaveConfig();
-        return;
-    }
-    /*
-     * read the file header
-    */
-    readCount = OsaFread(&AppConfigHr, sizeof(AppConfigHeader), 1, fp);
-    UINT8 readtimes=0;
-    while (readCount != 1 && readtimes<=5 )
-    {
-        //ECOMM_TRACE(UNILOG_PLA_MIDWARE, AppLoadNvmConfig_header_e_1, P_ERROR, 1,
-        //            "NVM: 'qxappconfig.nvm', can't read header, return: %d, use the defult value", readCount);
-        #ifdef USING_PRINTF
-                    printf("NVM: 'qxappconfig.nvm', can't read header, return: %d, use the defult value \n",readtimes);
-        #endif
-        readtimes++;
-        readCount = OsaFread(&AppConfigHr, sizeof(AppConfigHeader), 1, fp);
-        osDelay(10);
-    }
-    if(readtimes>5)
-    {
-        OsaFclose(fp);
-        setDefaultAppDataValue();
-        appSaveConfig();
-        return;
-    }
-    if (AppConfigHr.version != APP_CONFIG_FILE_LATEST_VERSION)
-    {
-        //ECOMM_TRACE(UNILOG_PLA_MIDWARE, AppLoadNvmConfig_ver_w_1, P_ERROR, 2,
-        //            "NVM: 'qxappconfig.nvm', version: %d not latest: %d, try to adapt",
-        //            AppConfigHr.version, MID_WARE_NVM_FILE_LATEST_VERSION);
-
-		#ifdef USING_PRINTF
-				printf("NVM: 'qxappconfig.nvm', version: %d not latest: %d, try to adapt\n",
-						AppConfigHr.version, MID_WARE_NVM_FILE_LATEST_VERSION);
-		#endif
-        if (AppConfigHr.fileBodySize > 1024)   //in fact this NVM size should limited in 1KB
-        {
-            //ECOMM_TRACE(UNILOG_PLA_MIDWARE, AppLoadNvmConfig_ver_e_1, P_ERROR, 2,
-            //            "NVM: 'qxappconfig.nvm', version: %d, NVM body size: %d > 1024, not right, use the default value",
-             //           AppConfigHr.version, AppConfigHr.fileBodySize);
-			#ifdef USING_PRINTF
-				printf("NVM: 'qxappconfig.nvm', version: %d, NVM body size: %d > 1024, not right, use the default value",
-                       AppConfigHr.version, AppConfigHr.fileBodySize);
-			#endif
-            OsaFclose(fp);
-            setDefaultAppDataValue();
-            appSaveConfig();
-            return;
-        }
-
-        /*
-         * As need to do adaption, can't read the old NVM into "mwNvmConfig", here we allocate a new buffer to store it
-        */
-        pReadAppConfig  = OsaAllocZeroMemory(AppConfigHr.fileBodySize);
-
-        if (pReadAppConfig == PNULL)
-        {
-            //ECOMM_TRACE(UNILOG_PLA_MIDWARE, AppLoadNvmConfig_buff_e_1, P_ERROR, 2,
-            //            "NVM: 'qxappconfig.nvm', version: %d, can not allo memsize: %d, use the default value",
-            //            AppConfigHr.version, AppConfigHr.fileBodySize);
-			#ifdef USING_PRINTF
-				printf("NVM: 'qxappconfig.nvm', version: %d, can not allo memsize: %d, use the default value",
-                        AppConfigHr.version, AppConfigHr.fileBodySize);
-			#endif
-            OsaFclose(fp);
-            setDefaultAppDataValue();
-            appSaveConfig();
-
-            return;
-        }		
-        //needAdjust = TRUE;
-    }
-    else if (AppConfigHr.fileBodySize != sizeof(AppNVMData))  //file version is the same, but NVM file size not right
-    {
-        //ECOMM_TRACE(UNILOG_PLA_MIDWARE, AppLoadNvmConfig_2, P_ERROR, 2,
-        //            "MW NVM: 'midwareconfig.nvm', file body size not right: (%u/%u), use the defult value",
-        //            AppConfigHr.fileBodySize, sizeof(AppNVMData));
-		#ifdef USING_PRINTF
-				printf("MW NVM: 'midwareconfig.nvm', file body size not right: (%u/%u), use the defult value",
-        		            AppConfigHr.fileBodySize, sizeof(AppNVMData));
-		#endif
-		
-        OsaFclose(fp);
-
-        setDefaultAppDataValue();
-        appSaveConfig();
-
-        return;
-    }
-
-    /*
-     * read the file body
-    */
-    
-    readCount = OsaFread(pReadAppConfig, AppConfigHr.fileBodySize, 1, fp);
-    crcCheck = OsaCalcCrcValue((UINT8 *)pReadAppConfig, sizeof(AppNVMData));
-	readtimes = 0;
-    //while (readtimes<=5 && crcCheck != AppConfigHr.checkSum)
-    while (readtimes<=5 && readCount != 1)
-    {
-        readtimes++;
-        readCount = OsaFread(pReadAppConfig, AppConfigHr.fileBodySize, 1, fp);
-        crcCheck = OsaCalcCrcValue((UINT8 *)pReadAppConfig, sizeof(AppNVMData));
-    }
-    if (readtimes>5)
-    {
-        //ECOMM_TRACE(UNILOG_PLA_MIDWARE, AppLoadNvmConfig_3, P_ERROR, 2,
-        //            "NVM: 'qxappconfig.nvm', can't read body, or body not right, (%u/%u), use the defult value",
-        //            crcCheck, AppConfigHr.checkSum);
-		#ifdef USING_PRINTF
-			printf(" NVM: 'qxappconfig.nvm', can't read body, or body not right, (%u/%u), use the defult value\n ",
-						crcCheck, AppConfigHr.checkSum);
-		#endif
-        OsaFclose(fp);
-        setDefaultAppDataValue();
-        appSaveConfig();
-        /*
-        if (needAdjust)
-        {
-            OsaFreeMemory(&pReadAppConfig);
-        }
-        */
-        return;
-    }
-
-    #if 0
-        if (needAdjust)
-        {
-            OsaFclose(fp);
-            setDefaultAppDataValue();
-            appSaveConfig();
-
-            /* free memory */
-            OsaFreeMemory(&pReadAppConfig);
-        }
-    #endif
-    #if 0 
-        int i=0;
-
-        printf("AppConfigHeader:\n");
-        printf("%d,%d,%d\n",AppConfigHr.fileBodySize,AppConfigHr.version,AppConfigHr.checkSum);
-        printf("AppConfigFileBody:\n");
-        printf("%d,%d,%d\n",AppNVMData.chargEndWorkTime,AppNVMData.wakeupWorkTime,AppNVMData.sleepTime);
-        printf("\n");
-    #endif
-    OsaFclose(fp);
-    return;
-}
-
-
-//if the AppNVMData.version(read form nvm) != APP_CONFIG_FILE_LATEST_VERSION, it will setDefaultAppDataValue()
-//so if you want to modify the default value, please modify the APP_CONFIG_FILE_LATEST_VERSION (in MainTask.h) meanwhile, so that will be store to the nvm file
-static void setDefaultAppDataValue(void)
-{
-    UINT8  i = 0;
-    memset(&AppNVMData, 0x00, sizeof(AppNVMDataType));
-    CHAR defaultBattSN[BATT_SN_LEN] = DEFAULT_SN;
-	for(i=0;i<BATT_SN_LEN;i++)
-	{
-		AppNVMData.battSN[i] = defaultBattSN[i];  //the defult sn is "0000..0000"(17 bytes ASCII)
-	}
-	AppNVMData.isBattLocked = false;
-	AppNVMData.chargEndWorkTime = 30;  //30 min
-	AppNVMData.wakeupWorkTime = 2;	//2min
-	AppNVMData.sleepTime = 5;			//5min
-    AppNVMData.HwVersion = HWVERSION;
-    AppNVMData.BLSwVersion = BLSWVERSION;
-    AppNVMData.DrvSwVersion = DRVSWVERSION;
-    AppNVMData.AppSwVersion = APPSWVERSION;
-    AppNVMData.EOLState = EOLSTATE; //未进行下线配置,北京电池测试默认进行下线测试
-    return;
-}
-
-static void appSaveDataInfo(void)
-{
-    OSAFILE fp = PNULL;
-    AppDataHeader  AppDataHr;
-    UINT32  writeCount = 0;
-    fp = OsaFopen(APP_DATAINFO_FILE_NAME, "wb");   //read & write
-    if(OsaFseek(fp, 0, SEEK_SET) != 0)
-	{
-        #ifdef USING_PRINTF
-				printf("Seek file failed [%d] \r\n",__LINE__);
-	    #endif
-	    OsaFclose(fp);
-        return;
-	}
-    if (fp == PNULL)
-    {
-	    #ifdef USING_PRINTF
-			printf(" NVM, can't open/create NVM: 'qxappconfig.nvm', save NVM failed\n");
-		#endif
-        return;
-    }
-    AppDataHr.fileBodySize   = sizeof(AppDataInfo); 
-    AppDataHr.checkSum       = OsaCalcCrcValue((UINT8 *)&AppDataInfo, sizeof(AppDataInfo));
-    #ifdef USING_PRINTF
-		printf(" AppDataHr.fileBodySize:%x\n",AppDataHr.fileBodySize);
-	#endif
-    writeCount = OsaFwrite(&AppDataHr, sizeof(AppDataHr), 1, fp);
-    if (writeCount != 1)
-    {
-        #ifdef USING_PRINTF
-                    printf(" NVM: 'qxappconfig.nvm', write the file header failed\n");
-        #endif
-        OsaFclose(fp);
-        return;
-    }
-    AppDataInfo.appDataModify = FALSE;
-    writeCount = OsaFwrite(&AppDataInfo, sizeof(AppDataInfo), 1, fp);
-    if (writeCount != 1)
-    {
-        #ifdef USING_PRINTF
-			printf(" NVM: 'qxappconfig.nvm', write the file body failed\n");
-		#endif
-    }
-    OsaFclose(fp);
-    return;
-}
-
-static void LoadAppDataInfo(void)
-{
-    OSAFILE fp = PNULL;
-    UINT32  readCount = 0;
-    //UINT8   crcCheck = 0;
-    AppDataHeader  AppDataHr;
-    void    *pReadAppConfig = (void *)&AppDataInfo;
-    fp = OsaFopen(APP_DATAINFO_FILE_NAME, "rb");   //read only
-    if (fp == PNULL)
-    {
-        #ifdef USING_PRINTF
-            printf(" NVM, can't open NVM: 'qxappData.nvm', use the defult value\n");
-        #endif
-        setDefaultAppDataInfo();
-        appSaveDataInfo();
-        return;
-    }
-
-    readCount = OsaFread(&AppDataHr, sizeof(AppDataHr), 1, fp);
-    UINT8 readtimes=0;
-    while (readCount != 1 && readtimes<=5 )
-    {
-        readtimes++;
-        readCount = OsaFread(&AppDataHr, sizeof(AppDataHr), 1, fp);
-        osDelay(10);
-    }
-    if(readtimes>5)
-    {
-        OsaFclose(fp);
-        setDefaultAppDataInfo();
-        appSaveDataInfo();
-        return;
-    }
-    if(AppDataHr.fileBodySize==sizeof(AppDataInfo))//结构体数据没变动,可直接读出
-    {
-        readCount = OsaFread(pReadAppConfig, AppDataHr.fileBodySize, 1, fp);
-        //crcCheck = OsaCalcCrcValue((UINT8 *)pReadAppConfig, sizeof(AppNVMData));
-        readtimes = 0;
-        while (readtimes<=5 && readCount != 1)
-        {
-            readtimes++;
-            readCount = OsaFread(pReadAppConfig, AppDataHr.fileBodySize, 1, fp);
-            //crcCheck = OsaCalcCrcValue((UINT8 *)pReadAppConfig, sizeof(AppNVMData));
-        }
-    }
-    else
-    {
-        setDefaultAppDataInfo();
-        readCount = OsaFread(pReadAppConfig, AppDataHr.fileBodySize, 1, fp);
-        //crcCheck = OsaCalcCrcValue((UINT8 *)pReadAppConfig, sizeof(AppNVMData));
-        readtimes = 0;
-        while (readtimes<=5 && readCount != 1)
-        {
-            readtimes++;
-            readCount = OsaFread(pReadAppConfig, AppDataHr.fileBodySize, 1, fp);
-            //crcCheck = OsaCalcCrcValue((UINT8 *)pReadAppConfig, sizeof(AppNVMData));
-        }
-        appSaveDataInfo();
-        #ifdef USING_PRINTF
-            printf(" Struct update\n");
-        #endif
-    }
-    OsaFclose(fp);
-    return;
-}
-
-//无法找到文件时的第一次启动,后续不会启动此函数
-static void setDefaultAppDataInfo(void)
-{
-    memset(&AppDataInfo, 0x00, sizeof(AppDataInfo));
-    AppDataInfo.appDataModify = false;
-    AppDataInfo.BmsChrgInfoSendFreq = 5;
-    AppDataInfo.BmsDisChrgInfoSendFreq = 30;
-    AppDataInfo.GpsChrgInfoSendFreq = 30;
-    AppDataInfo.GpsDisChrgInfoSendFreq = 5;
-    AppDataInfo.BattCurrentNegFlag = 1;
-    AppDataInfo.CumulativeCapacity = 0;
-    AppDataInfo.AccMileage = 0;
-    AppDataInfo.SysReStart = 0;
-    return;
-}
-
 

+ 0 - 3
src/AppTaskTcp.c

@@ -21,9 +21,6 @@ static UINT32                        gCellID = 0;
 static QueueHandle_t                 TcpRecvHandle = NULL;
 static UINT8                         TcpRecvEnd = 0;
 
-//函数声明区
-
-//线程任务区
 static void TcpTask(void* arg)
 {
     osDelay(5000);//等待驻网延时5s

+ 93 - 2770
src/AppTaskUart.c

@@ -7,70 +7,23 @@
  *
  * 2021-04-11:可以通过Ota线路升级Bms保护板
  ****************************************************************************/
-#include "bsp.h"
-#include "bsp_custom.h"
-#include "osasys.h"
-#include "ostask.h"
-#include "queue.h"
-#include "ps_event_callback.h"
-#include "cmisim.h"
-#include "cmimm.h"
-#include "cmips.h"
-#include "sockets.h"
-#include "psifevent.h"
-#include "ps_lib_api.h"
-#include "lwip/netdb.h"
-//#include <cis_def.h>
-#include "debug_log.h"
-#include "slpman_ec616.h"
-#include "plat_config.h"
-#include "ec_tcpip_api.h"
-#include "hal_module_adapter.h"
-#include "UartTask.h"
-#include "MainTask.h"
-#include <stdlib.h>
-#include "app.h"
-#include "numeric.h"
-#include "Fota.h"
-#include "signal.h"
 
-extern ARM_DRIVER_USART Driver_USART1;
-static ARM_DRIVER_USART *USARTdrv = &Driver_USART1;
+#include "AppTaskUart.h"
 
-
-//线程声明区
 static StaticTask_t             gProcess_Uart_Task_t;
 static UINT8                  gProcess_Uart_TaskStack[PROC_UART_TASK_STACK_SIZE];
 static osThreadId_t           UartTaskId = NULL;
 static process_Uart             gProcess_Uart_Task = PROCESS_UART_STATE_IDLE;
 #define PROC_UART_STATE_SWITCH(a)  (gProcess_Uart_Task = a)
+extern ARM_DRIVER_USART Driver_USART1;
+static ARM_DRIVER_USART *USARTdrv = &Driver_USART1;
+volatile BOOL isRecvTimeout = false;
+volatile BOOL isRecvComplete = false;
 
-//函数声明区
 void USART_callback(uint32_t event);
-UINT8 Uart_DataRecv_func(Uart_Read_Msg_Type Uart_Read_Msg_Fun,UINT8* Uart_Recv_Buffer_Fun);
-static BOOL uartBattInfoDecode(UINT8* dataPtr);
-UINT8 Uart_WriteCmd_func(Uart_Write_Data_Type UartWriteData);
-UINT16 crc_chk(UINT8* data, UINT8 length);
-void battSOCDisplay(void);
-void battErrorStateDisplay(void);
-void battWarningStateDisplay(void);
-void battLockStateDisplay(UINT8 lockState);
-void relayPWMControl(void);
-void SP_BMS_Update_Service(void);
-BOOL BattHeaterSwitch(UINT8* heaterSwitch);
-UINT16  encryptionAlgorithm (UINT16 plainText);
-UINT8  decryptionAlgorithm (UINT16 cipherText);
+static UINT16 crc_chk(UINT8* data, UINT8 length);
 UINT8 Uart_Encrypt_Send(void);
-UINT8 BmsErrorDecode(UINT32 battWarningState);
-//BMS升级函数声明
-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)
 {
     USARTdrv->Initialize(USART_callback);
@@ -80,95 +33,53 @@ static void UartTask(void* arg)
                       ARM_USART_PARITY_NONE |
                       ARM_USART_STOP_BITS_1 |
                       ARM_USART_FLOW_CONTROL_NONE, 9600);
-    PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_ENCRYPT);
-    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;
-	memset(&(Uart_Read_Msg),0x00,sizeof(Uart_Read_Msg_Type));
-    Uart_Write_Data_Type UartWriteData; //Uart控制命令
-	memset(&(UartWriteData),0x00,sizeof(Uart_Write_Data_Type));
-    UartReadMsgType UartReadMsg;
-    memset(&(UartReadMsg.UartFlag),0x00,sizeof(UartReadMsgType));
     if(UartWriteCmdHandle == NULL)//Uart控制命令传输指针
 	{
-		UartWriteCmdHandle = osMessageQueueNew(3,sizeof(Uart_Write_Data_Type), NULL);
+		UartWriteCmdHandle = osMessageQueueNew(3,sizeof(UartWriteData_S), NULL);
 	}
-    //上电起始控制区域
+    UartAnswerData_S UartAnswerData;//应答数据初始化
+	memset(&(UartAnswerData),0x00,sizeof(UartAnswerData_S));
+    UartWriteData_S UartWriteData; //Uart控制命令初始化
+	memset(&(UartWriteData),0x00,sizeof(UartWriteData_S));
+
+    PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_ENCRYPT);
     while (1)
     {
+		osDelay(100);
         switch (gProcess_Uart_Task)
         {
-			case PROCESS_UART_STATE_ENCRYPT:
-			{
-				UINT8 EncryptFlag=0x00;
+            case PROCESS_UART_STATE_ENCRYPT:
+            {
+                UINT8 EncryptFlag=0x00;
 				UINT8 EncryptCount=0;
 				while(EncryptFlag!=0x01&&EncryptCount<=3)
 				{
 					EncryptFlag = Uart_Encrypt_Send();
 					EncryptCount++;
 				}
-				PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_IDLE);
-				break;
-			}
+                PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_IDLE);
+                break;
+            }
             case PROCESS_UART_STATE_IDLE:
             {
                 osDelay(100);
-                if(Sleep_flag)
+                if(BMS_Fota_update_flag)
                 {
-                    PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_SLEEP);
+					PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_UPDATE);
+                    break;
                 }
-                else if(Timer_count%10==0)
+                else if(TimeCounter%10==0)
                 {
-                    #ifdef USING_PRINTF1
-                        printf("[%d]Uart Timer 1s:%d,uartReadSuccessFlag:%d\n",__LINE__,Timer_count,uartReadSuccessFlag);
-                    #endif
                     if(osMessageQueueGet(UartWriteCmdHandle,&UartWriteData,0,0)==osOK)
                     {
-                        #ifdef USING_PRINTF1
-                            printf("[%d]UartWriteCmdHandle :%x\n",__LINE__,UartWriteData.WriteCmd);
-                        #endif
                         PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_WRITE);
                     }
                     else
+                    {
                         PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_READ);
+                    }
                 }
-				if(UartReadMsg.Header[2]>0)
-				{
-					uartReadSuccessFlag = true;
-				}
-				else
-				{
-					uartReadSuccessFlag = false;
-				}
-				if(Timer_count-currentTimerCount >= 1)
-				{
-					if(AppNVMData.isBattLocked != 0)
-					{
-						battLockStateDisplay(TRUE);
-					}
-					else if(uartReadSuccessFlag)
-					{
-						battSOCDisplay();
-						battErrorStateDisplay();
-					}
-					else
-					{
-						battWarningStateDisplay();
-					}
-				}
-				currentTimerCount = Timer_count;
-				if(BMS_Fota_update_flag)
-				{
-					if(WorkFlag==0x00)
-					{
-						PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_UPDATE);
-					}
-					
-				}
-				if(AppNVMData.isBattLocked==TRUE && ((UartReadMsg.data[(0x09+BATT_CELL_VOL_NUM+BATT_TEMP_NUM+2)*2+1])>>1)&0x03!=0x00 && Timer_count%10==0)//try to lock
+                if(AppDataInfo.BattLock==TRUE && ((UartAnswerData.data[(0x09 + AppNVMData.BattCellCount + AppNVMData.BattTempCount + BMS_OTHER_TEMP)*2+1])>>1)&0x03!=0x00 && TimeCounter%10==0)//try to lock
 				{
 					UartWriteData.WriteCmd = 0x01;
 					UartWriteData.Data[0] = 0x00;
@@ -176,7 +87,7 @@ static void UartTask(void* arg)
 					osMessageQueuePut(UartWriteCmdHandle,&UartWriteData,0,1000);
 					PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_READ);
 				}
-				else if (AppNVMData.isBattLocked==FALSE && ((UartReadMsg.data[(0x09+BATT_CELL_VOL_NUM+BATT_TEMP_NUM+2)*2+1])>>1)&0x03==0x00 && Timer_count%10==0 ) // try to unlock
+				else if (AppDataInfo.BattLock==FALSE && ((UartAnswerData.data[(0x09 + AppNVMData.BattCellCount + AppNVMData.BattTempCount + BMS_OTHER_TEMP)*2+1])>>1)&0x03==0x00 && TimeCounter%10==0 ) // try to unlock
 				{
 					UartWriteData.WriteCmd = 0x01;
 					UartWriteData.Data[0] = 0x00;
@@ -188,208 +99,82 @@ static void UartTask(void* arg)
             }
             case PROCESS_UART_STATE_READ:
             {
-                UINT16 CRC_chk_buffer;
-                Reg_Num = 0x21+BATT_CELL_VOL_NUM+BATT_TEMP_NUM + 2;//按照协议里面的0x21+X+N的结束地址
-                Uart_Read_Msg.Bms_Address = BMS_ADDRESS_CODE;
-                Uart_Read_Msg.Bms_Funcode = UART_READ_CODE;
-                Uart_Read_Msg.Reg_Begin_H = 0x00;
-                Uart_Read_Msg.Reg_Begin_L= 0x00;
-                Uart_Read_Msg.Reg_Num_H = Reg_Num>>8;
-                Uart_Read_Msg.Reg_Num_L = Reg_Num;
-                Uart_Uds_LEN = Reg_Num*2;
-                memset(UartReadMsg.Header,0x00,Uart_Uds_LEN);
-				CRC_chk_buffer = crc_chk((UINT8 *)&Uart_Read_Msg,6);
-				Uart_Read_Msg.CRC_L = CRC_chk_buffer;
-				Uart_Read_Msg.CRC_H = CRC_chk_buffer>>8;
-                //Uart_Recv_LEN = Uart_DataRecv_func((UINT8 *)&Uart_Read_Msg,(UINT8*)UartReadMsg.Header);
-				Uart_Recv_LEN = Uart_DataRecv_func(Uart_Read_Msg,(UINT8*)(UartReadMsg.Header));
-				if(Uart_Recv_LEN>0)
-				{
-					UartBattInfoRecvFlag =  TRUE;
-					uartBattInfoDecode(UartReadMsg.data);
-				}
-				else
-				{
-					UartBattInfoRecvFlag =  FALSE;
-				}
-                UartReadMsg.len = Uart_Recv_LEN;
-                PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_IDLE);
-                if( battWorkState ==0x02)
-                {
-                    BattChrgEndFlag=TRUE;
-                }
-                else
-                {
-                    BattChrgEndFlag=FALSE;
-                }
-				#ifdef USING_PRINTF1
-					printf("\nUart_Recv_buffer:  ");
-					for(int i=0;i<Uart_Recv_LEN;i++)
-					{
-						printf("%x ",*((UINT8 *)&UartReadMsg.Header+i));
-					}
-					printf("\n");
-				#endif
                 break;
             }
             case PROCESS_UART_STATE_WRITE:
             {
-                Uart_WriteCmd_func(UartWriteData);
-                PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_IDLE);
                 break;
             }
-			case PROCESS_UART_STATE_UPDATE:
-				UartBattInfoRecvFlag =  FALSE;
-				#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;
+            case PROCESS_UART_STATE_UPDATE:
+            {
+                break;
+            }
             case PROCESS_UART_STATE_SLEEP:
             {
-                USARTdrv->PowerControl(ARM_POWER_LOW);
-                while(TRUE)
-                {
-                    osDelay(60000/portTICK_PERIOD_MS);
-                }
-				osThreadExit();
+                break;
+            }
+            default:
+            {
+                PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_IDLE);
                 break;
             }
 
         }
     }
 }
-
-
-
-
-//Uart 接收的数据解码
-static BOOL uartBattInfoDecode(UINT8* dataPtr)
+UINT8 Uart_Encrypt_Send()
 {
-	//BattInfoType battInfo;
-	UINT8 i,temp=0;
-    UINT8 TEMP_NUM = BATT_TEMP_NUM + BATT_OTHER_TEMP_NUM;
-    UINT16 Batt_current;
-    for(i=0;i<BATT_CELL_VOL_NUM;i++)
-    {
-		battCellU[i] = (dataPtr[(0x02+i)*2] << 8) | dataPtr[(0x02+i)*2 + 1];
-    }
-    
-    battWorkState = (dataPtr[(0x03+BATT_CELL_VOL_NUM)*2+1])&0x03;//电池状态(原始数据),0表示静置,1表示放电,2表示充电
-    
-    for(i=0; i<BATT_TEMP_NUM; i++)
-    {
-	    battCellTemp[i] = dataPtr[(0x06+BATT_CELL_VOL_NUM+i)*2+1];
-    }
-    MOSTemp = dataPtr[(0x06+BATT_CELL_VOL_NUM+BATT_TEMP_NUM)*2+1];
-   	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]);
-    //原始数据:充电为负,放电为正
-	if(battWorkState == 0x02) //充电过程
+	UINT8 SeedNumberArrray[4]={0x38,0x56,0xfe,0xac};
+	UINT16 EncodeNumberArray[4];
+	UINT8 UartEncryptBuffer[17];
+	UINT8 UartDecryptBuffer[5];
+	UINT16 CRC_chk_buffer;
+	UINT8 timeCount = 0;
+	UartEncryptBuffer[0] = BMS_ADDRESS_CODE;
+	UartEncryptBuffer[1] = UART_ENCRYPT_CODE;
+	UartEncryptBuffer[2] = 0x0c;
+	for(int i=0;i<4;i++)
 	{
-		if(Batt_current >0x8000)// 数据为负
-		{			
-			//求补码,结果为负
-			Batt_current = (UINT16)((UINT16)(~(Batt_current))+1);
-        	Batt_current = Batt_current/10;
-			AppDataInfo.BattCurrentNegFlag = -1;
-		}
-		else
-		{
-			//源码,结果为负
-			Batt_current = Batt_current/10;
-			AppDataInfo.BattCurrentNegFlag = -1;
-		}
+		SeedNumberArrray[i]=rand();
+		EncodeNumberArray[i] = encryptionAlgorithm(SeedNumberArrray[i]);
+		UartEncryptBuffer[i+3] = SeedNumberArrray[i];
+		UartEncryptBuffer[i*2+7] = EncodeNumberArray[i]>>8;
+		UartEncryptBuffer[i*2+8] = EncodeNumberArray[i];
 	}
-	else //放电过程
+	CRC_chk_buffer = crc_chk(UartEncryptBuffer,17-2);
+	UartEncryptBuffer[15] = CRC_chk_buffer;
+	UartEncryptBuffer[16] = CRC_chk_buffer>>8;
+	USARTdrv->Send(UartEncryptBuffer,17);
+	USARTdrv->Receive(UartDecryptBuffer,5); 
+	while((isRecvTimeout == false) && (isRecvComplete == false))
 	{
-		if(Batt_current >0x8000)// 数据为负
+		timeCount++;
+		osDelay(100);
+		if (timeCount>=10)
 		{
-			//求补码,结果为正
-			Batt_current = (UINT16)((UINT16)(~(Batt_current))+1);
-        	Batt_current = Batt_current/10;
-			AppDataInfo.BattCurrentNegFlag = 1;
+			timeCount =0;
+			isRecvTimeout = true;
+			break;
 		}
-		else
+	}
+	#ifdef USING_PRINTF
+		printf("Uart_Rece_buffer: ");
+		for(int i=0;i<5;i++)
 		{
-			//源码,结果为正
-			Batt_current = Batt_current/10;
-			AppDataInfo.BattCurrentNegFlag = 1;
+		printf("%x ",UartDecryptBuffer[i]);
 		}
+    #endif	
+	if (isRecvComplete == true)
+	{
+		isRecvComplete = false;
+		return UartDecryptBuffer[2];
 	}
-
-	battI = Batt_current*AppDataInfo.BattCurrentNegFlag + 0x2710;
-    //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);
-
-    bmsHwVersion = dataPtr[(0x08+BATT_CELL_VOL_NUM+TEMP_NUM)*2+1]; 
-    bmsSwVersion = dataPtr[(0x08+BATT_CELL_VOL_NUM+TEMP_NUM)*2];    
-
-    temp = ((dataPtr[(0x09+BATT_CELL_VOL_NUM+TEMP_NUM)*2+1])>>1)&0x03;    
-    battMOSSwitchState = ((temp&0x01)<<1)|((temp&0x02)>>1);	
-    if(AppNVMData.isBattLocked==TRUE)
-    {
-        battMOSSwitchState = battMOSSwitchState |(0x01<<2);
-    }
-    else
-    {
-        battMOSSwitchState = battMOSSwitchState |(0x00<<2);
-    }
-	battWarningState = (dataPtr[(0x09+BATT_CELL_VOL_NUM+TEMP_NUM)*2+0]<<16) | (dataPtr[(0x0A+BATT_CELL_VOL_NUM+TEMP_NUM)*2+0] << 8) |(dataPtr[(0x0A+BATT_CELL_VOL_NUM+TEMP_NUM)*2+1]);
-    battSOC = dataPtr[(0x0B+BATT_CELL_VOL_NUM+TEMP_NUM)*2+1];
-    battSOH = dataPtr[(0x0C+BATT_CELL_VOL_NUM+TEMP_NUM)*2+1];
-	Battdesigncap = (dataPtr[(0x0E+BATT_CELL_VOL_NUM+TEMP_NUM)*2])<<24|(dataPtr[(0x0E+BATT_CELL_VOL_NUM+TEMP_NUM)*2+1])<<16|(dataPtr[(0x0F+BATT_CELL_VOL_NUM+TEMP_NUM)*2])<<8|(dataPtr[(0x0F+BATT_CELL_VOL_NUM+TEMP_NUM)*2+1]);
-	battPackVol =((dataPtr[(0x18+BATT_CELL_VOL_NUM+TEMP_NUM)*2])<<8|(dataPtr[(0x18+BATT_CELL_VOL_NUM+TEMP_NUM)*2+1]))/10;  //uint 100mV
-	maxCellVol = (dataPtr[(0x19+BATT_CELL_VOL_NUM+TEMP_NUM)*2] << 8) | dataPtr[(0x19+BATT_CELL_VOL_NUM+TEMP_NUM)*2 + 1];
-	minCellVol = (dataPtr[(0x1A+BATT_CELL_VOL_NUM+TEMP_NUM)*2] << 8) | dataPtr[(0x1A+BATT_CELL_VOL_NUM+TEMP_NUM)*2 + 1];
-    battHeatEnableState = dataPtr[(0x1C+BATT_CELL_VOL_NUM+TEMP_NUM)*2+1]&0x01;
-
-	maxCellTemp = 0x00;
-	minCellTemp = 0xFF;
-	for(i=0;i<BATT_TEMP_NUM;i++)
+	else
 	{
-		maxCellTemp = max(maxCellTemp,battCellTemp[i]);
-		minCellTemp = min(minCellTemp,battCellTemp[i]);		
+		isRecvTimeout = false;
+		return 0x03;
 	}
-	nbSwVersion = APPSWVERSION;
-	nbHwVersion = HWVERSION;
-	BmsErrorDecode(battWarningState);
-    return true;
-}
-
-
-
-
-
-
-//Uart线程初始化
-void UartTaskInit(void *arg)
-{
-    osThreadAttr_t task_attr;
-    memset(&task_attr,0,sizeof(task_attr));
-    memset(gProcess_Uart_TaskStack, 0xA5, PROC_UART_TASK_STACK_SIZE);
-    task_attr.name = "Uart_Task";
-    task_attr.stack_mem = gProcess_Uart_TaskStack;
-    task_attr.stack_size = PROC_UART_TASK_STACK_SIZE;
-    task_attr.priority = osPriorityBelowNormal7;
-    task_attr.cb_mem = &gProcess_Uart_Task_t;
-    task_attr.cb_size = sizeof(StaticTask_t);
-    UartTaskId = osThreadNew(UartTask, NULL, &task_attr);
-}
-void UartTaskDeInit(void *arg)
-{
-    osThreadTerminate(UartTaskId);
-    UartTaskId = NULL;
 }
-//函数区
-//Uart回调程序
 void USART_callback(uint32_t event)
 {
     if(event & ARM_USART_EVENT_RX_TIMEOUT)
@@ -401,8 +186,7 @@ void USART_callback(uint32_t event)
         isRecvComplete = true;
     }
 }
-//Uart校验程序
-UINT16 crc_chk(UINT8* data, UINT8 length)
+static UINT16 crc_chk(UINT8* data, UINT8 length)
 {  
     UINT8 j;
     UINT16 reg_crc=0xFFFF;
@@ -423,2482 +207,21 @@ UINT16 crc_chk(UINT8* data, UINT8 length)
     }
     return reg_crc;
 }
-//Uart写命令函数
-UINT8 Uart_WriteCmd_func(Uart_Write_Data_Type UartWriteData)
-{
-    Uart_Write_Msg_Type Uart_Write_Msg;
-    UINT16 RegAddress = 0x0000;
-    UINT16 CRC_chk_buffer;
-    UINT8 timeout = 0x00;
-    UINT8 Uart_Recv_Buffer[8];
-    switch (UartWriteData.WriteCmd)
-    {
-        case 0x01://是否锁定
-        {   
-            RegAddress = 0x1B + BATT_CELL_VOL_NUM+BATT_TEMP_NUM+BATT_OTHER_TEMP_NUM;
-            Uart_Write_Msg.Bms_Address = BMS_ADDRESS_CODE;
-            Uart_Write_Msg.Bms_Funcode = UART_WRITE_CODE;
-            Uart_Write_Msg.Reg_Begin_H = RegAddress>>8;
-            Uart_Write_Msg.Reg_Begin_L = RegAddress;
-            Uart_Write_Msg.Reg_Num_H = 0x00;
-            Uart_Write_Msg.Reg_Num_L = 0x01;
-            Uart_Write_Msg.Data_Count = 0x02;//要写入的字节数
-            memcpy(Uart_Write_Msg.Data,UartWriteData.Data,2);
-            CRC_chk_buffer = crc_chk((UINT8 *)&Uart_Write_Msg,sizeof(Uart_Write_Msg)-2);
-            Uart_Write_Msg.CRC_L = CRC_chk_buffer;
-            Uart_Write_Msg.CRC_H = CRC_chk_buffer>>8;
-            break;
-        }
-		case 0x02://是否加热
-		{
-			RegAddress = 0x1C + BATT_CELL_VOL_NUM+BATT_TEMP_NUM+BATT_OTHER_TEMP_NUM;
-            Uart_Write_Msg.Bms_Address = BMS_ADDRESS_CODE;
-            Uart_Write_Msg.Bms_Funcode = UART_WRITE_CODE;
-            Uart_Write_Msg.Reg_Begin_H = RegAddress>>8;
-            Uart_Write_Msg.Reg_Begin_L = RegAddress;
-            Uart_Write_Msg.Reg_Num_H = 0x00;
-            Uart_Write_Msg.Reg_Num_L = 0x01;
-            Uart_Write_Msg.Data_Count = 0x02;//要写入的字节数
-            memcpy(Uart_Write_Msg.Data,UartWriteData.Data,2);
-            CRC_chk_buffer = crc_chk((UINT8 *)&Uart_Write_Msg,sizeof(Uart_Write_Msg)-2);
-            Uart_Write_Msg.CRC_L = CRC_chk_buffer;
-            Uart_Write_Msg.CRC_H = CRC_chk_buffer>>8;
-            break;
-		}
-        default:
-        {
-            UartWriteData.WriteCmd = 0x00;
-            return 0;
-            break;
-        }
-    }
-    USARTdrv->Send((UINT8 *)&Uart_Write_Msg,sizeof(Uart_Write_Msg));
-    #ifdef USING_PRINTF1
-        printf("Uart_Send_buffer:  ");
-        for(int i=0;i<sizeof(Uart_Write_Msg);i++)
-        {
-            printf("%x ",*((UINT8 *)&Uart_Write_Msg+i));
-        }
-        printf("\n");
-    #endif
-    USARTdrv->Receive(Uart_Recv_Buffer,8);
-    while((isRecvTimeout == false) && (isRecvComplete == false))
-    {
-        timeout++;
-        osDelay(100);
-        if (timeout>=10)
-        {
-            timeout =0;
-            isRecvTimeout = true;
-            break;
-        }
-    }
-    if (isRecvComplete == true)
-    {
-        #ifdef USING_PRINTF1
-            printf("Uart_Rece_buffer: ");
-            for(int i=0;i<8;i++)
-            {
-            printf("%x ",Uart_Recv_Buffer[i]);
-            }
-            printf("\n");
-        #endif
-        isRecvComplete = false;
-        if(Uart_Recv_Buffer[1]==0x10)
-        {
-            return UartWriteData.WriteCmd;
-        }
-        else
-        {
-            return 0x00;
-        }
-    }
-    else
-    {
-        isRecvTimeout = false;
-        return 0x00;
-    }
-}
-//Uart发送接收函数
-UINT8 Uart_DataRecv_func(Uart_Read_Msg_Type Uart_Read_Msg_Fun,UINT8* Uart_Recv_Buffer_Fun)
-{
-    UINT16 CRC_Rece_buffer;
-    UINT16 CRC_chk_buffer;
-    UINT16 Data_Len ;
-    UINT8 timeout = 0x00;
-	UINT8 pSendCmd[8];
-	memcpy(pSendCmd,(UINT8*)(&Uart_Read_Msg_Fun),8);
-    //Data_Len = (*(Uart_Read_Msg_Fun+4)|*(Uart_Read_Msg_Fun+5))*2+5;
-	Data_Len  = ((Uart_Read_Msg_Fun.Reg_Num_H<<8)|(Uart_Read_Msg_Fun.Reg_Num_L))*2+5;
-
-    //USARTdrv->Send(Uart_Read_Msg_Fun,8);
-	USARTdrv->Send(pSendCmd,8);
-    #ifdef USING_PRINTF1
-		
-        printf("Uart_Send_buffer:  ");
-        for(int i=0;i<8;i++)
-        // {
-             printf("%x ",pSendCmd[i]);
-        // }
-        printf("end\n");
-		//printf("%x ",*(Uart_Read_Msg_Fun));
-	//UINT8 temp = *(Uart_Read_Msg_Fun);
-    #endif
-    USARTdrv->Receive(Uart_Recv_Buffer_Fun,Data_Len);
-	while(true)
-    {
-        timeout++;
-        if((isRecvTimeout == true) || (isRecvComplete == true))
-        {
-            break;
-        }
-        else
-        {
-            osDelay(100);
-            if (timeout>=10)
-            {
-				// Data_Len = 0;
-                timeout =0;
-                isRecvTimeout = true;
-            }
-        } 
-    }
-     #ifdef USING_PRINTF1
-         printf("Uart_Rece_buffer1: ");
-         for(int j=0;j<Data_Len;j++)
-         {
-         printf("%x ",*(Uart_Recv_Buffer_Fun+j));
-         }
-     #endif
-    if (isRecvComplete == true)
-    {
-        isRecvComplete = false;
-        CRC_Rece_buffer =*(Uart_Recv_Buffer_Fun+Data_Len-1)<<8|*(Uart_Recv_Buffer_Fun+Data_Len-2);
-        CRC_chk_buffer = crc_chk(Uart_Recv_Buffer_Fun,Data_Len-2);
-	    #ifdef USING_PRINTF1
-	         printf("Uart_Rece_buffer after Crc: ");
-	         for(int i=0;i<Data_Len;i++)
-	         {
-	         	printf("%x ",*(Uart_Recv_Buffer_Fun+i));
-	   	     }
-	         printf("\tcrcchk:%x,%x\n ",CRC_chk_buffer,CRC_Rece_buffer);
-	    #endif
-        if (CRC_Rece_buffer == CRC_chk_buffer)//满足校验
-        {
-            return Data_Len;//此处指针移位出现重启问题
-        }
-        else //接收数据的校验不过
-        {
-            USARTdrv->Uninitialize();
-            osDelay(1000);
-            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);
-            memset(Uart_Recv_Buffer_Fun,0xff,Data_Len);
-            return 0;
-        }
-    }
-    else
-    {
-        memset(Uart_Recv_Buffer_Fun,0x00,Data_Len);
-        isRecvTimeout = false;
-        return 0;
-    }
-	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
-*/
-BOOL BattHeaterSwitch(UINT8* heaterSwitch)
-{
-	BOOL isNeedtoSwitch = FALSE;
-	
-	UINT8 i =0;	
-	UINT8 currentSwitchState = 0;
-	//get the current switch state and the cell temp
-	currentSwitchState = battMOSSwitchState & 0x01;
-	if(currentSwitchState==0) 	//当前状态为关闭,判断是否应该开启
-	{
-		if(minCellTemp<=5+40 && maxCellTemp<25+40 && battSOC>=12)//温度偏移为40 
-		{
-			*heaterSwitch = 1;
-			isNeedtoSwitch = true;
-		}
-	}
-	else  						//当前状态为开启,判断是否应该关闭
-	{
-		if(minCellTemp>10+40 || maxCellTemp>30+40 || battSOC<10)
-		{
-			*heaterSwitch = 0;
-			isNeedtoSwitch= true;
-		}
-	}
-	return isNeedtoSwitch;
-}
-
-void battSOCDisplay()
-{
-	
-	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(UartBattInfoRecvFlag == true)
-	{ 
-		lightTimer++;
-		
-		if(battWorkState == 0||battWorkState == 1)  //静置或放电状态
-		{
-			if(battSOC<=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(battSOC>10&&battSOC<=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(battSOC>25&&battSOC<=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(battSOC>50&&battSOC<=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(battSOC>75&&battSOC<=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(battWorkState == 2)
-		{
-			if(battSOC<=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(battSOC>25&&battSOC<=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(battSOC>50&&battSOC<=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(battSOC>75&&battSOC<=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(battSOC>97&&battSOC<=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 UINT8  errorLightTimer = 0;
-	//static UINT32 currentTimerCount=0;
-	
-	UINT8  errorLEDFlashPeriod = 6;//600ms
-	float errorDutyRatio = 0.4;
-
-	if(AppNVMData.isBattLocked == TRUE)
-	{
-		return;
-	}
-	
-	if(UartBattInfoRecvFlag == true)
-	{	
-	
-		errorLightTimer++;
-
-		if(battWarningState != 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 battWarningStateDisplay()
+void AppTaskUartInit(void *arg)
 {
-	static UINT8  warningLightTimer = 0;
-	//static UINT32 currentTimerCount=0;
-	
-	UINT8  warningLEDFlashPeriod = 6;//600ms
-	float warningDutyRatio = 0.4;
-
-	if(AppNVMData.isBattLocked == TRUE)
-	{
-		return;
-	}
-	
-	if(UartBattInfoRecvFlag == false)
-	{		
-		warningLightTimer++;
-
-		//if(battWarningState != 0)
-		{
-			if(warningLightTimer<(UINT8)(warningLEDFlashPeriod*warningDutyRatio)) 
-			{			
-				NetSocDisplay(LED_SOC_0,LED_TURN_ON);
-				FaultDisplay(LED_TURN_ON);
-				
-			}
-			else if(warningLightTimer>=(UINT8)(warningLEDFlashPeriod*warningDutyRatio) && warningLightTimer<warningLEDFlashPeriod)
-			{
-				NetSocDisplay(LED_SOC_0,LED_TURN_OFF);
-				FaultDisplay(LED_TURN_OFF);
-				
-			}
-			else
-			{
-				NetSocDisplay(LED_SOC_0,LED_TURN_OFF);
-				FaultDisplay(LED_TURN_OFF);
-				warningLightTimer = 0;			
-			}
-			
-		}
-			
-	}
-
+    osThreadAttr_t task_attr;
+    memset(&task_attr,0,sizeof(task_attr));
+    memset(gProcess_Uart_TaskStack, 0xA5, PROC_UART_TASK_STACK_SIZE);
+    task_attr.name = "Uart_Task";
+    task_attr.stack_mem = gProcess_Uart_TaskStack;
+    task_attr.stack_size = PROC_UART_TASK_STACK_SIZE;
+    task_attr.priority = osPriorityBelowNormal7;
+    task_attr.cb_mem = &gProcess_Uart_Task_t;
+    task_attr.cb_size = sizeof(StaticTask_t);
+    UartTaskId = osThreadNew(UartTask, NULL, &task_attr);
 }
-
-
-void battLockStateDisplay(UINT8 lockState)
+void AppTaskUartDeInit(void *arg)
 {
-	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++;
-}
-
-void relayPWMControl()
-{
-	static UINT8 timerCount=0;
-	//printf("timerCout=%d\n",timerCount);
-	if(timerCount<2)
-	{
-		relayControl(TRUE);
-		timerCount++;
-	}
-	else if(timerCount>=2 && timerCount<5)
-	{
-		relayControl(FALSE);
-		timerCount++;
-	}
-	else
-	{
-		relayControl(FALSE);
-		timerCount = 0;
-	}
-}
-
-
-
-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  encryptionAlgorithm (UINT16 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;  
-	plainText = (plainText * plainText) % privateKeyN;
-	}
-	return cipherText;
-}
-UINT8 Uart_Encrypt_Send()
-{
-	UINT8 SeedNumberArrray[4]={0x38,0x56,0xfe,0xac};
-	UINT16 EncodeNumberArray[4];
-	UINT8 UartEncryptBuffer[17];
-	UINT8 UartDecryptBuffer[5];
-	UINT16 CRC_chk_buffer;
-	UINT8 timeCount = 0;
-	UartEncryptBuffer[0] = BMS_ADDRESS_CODE;
-	UartEncryptBuffer[1] = UART_ENCRYPT_CODE;
-	UartEncryptBuffer[2] = 0x0c;
-	for(int i=0;i<4;i++)
-	{
-		SeedNumberArrray[i]=rand();
-		EncodeNumberArray[i] = encryptionAlgorithm(SeedNumberArrray[i]);
-		UartEncryptBuffer[i+3] = SeedNumberArrray[i];
-		UartEncryptBuffer[i*2+7] = EncodeNumberArray[i]>>8;
-		UartEncryptBuffer[i*2+8] = EncodeNumberArray[i];
-	}
-	CRC_chk_buffer = crc_chk(UartEncryptBuffer,17-2);
-	UartEncryptBuffer[15] = CRC_chk_buffer;
-	UartEncryptBuffer[16] = CRC_chk_buffer>>8;
-	USARTdrv->Send(UartEncryptBuffer,17);
-	USARTdrv->Receive(UartDecryptBuffer,5); 
-	while((isRecvTimeout == false) && (isRecvComplete == false))
-	{
-		timeCount++;
-		osDelay(100);
-		if (timeCount>=10)
-		{
-			timeCount =0;
-			isRecvTimeout = true;
-			break;
-		}
-	}
-	#ifdef USING_PRINTF
-		printf("Uart_Rece_buffer: ");
-		for(int i=0;i<5;i++)
-		{
-		printf("%x ",UartDecryptBuffer[i]);
-		}
-    #endif	
-	if (isRecvComplete == true)
-	{
-		isRecvComplete = false;
-		return UartDecryptBuffer[2];
-	}
-	else
-	{
-		isRecvTimeout = false;
-		return 0x03;
-	}
-}
-
-
-
-
-/*-----------------------------------------------------------------------------*/
-void SP_BMS_Update_Service() //超力源BMS升级服务
-{
-	
-	UINT8 errorCount = 0;
-	UINT8 resetCount = 0;	
-	UINT16 currentPackage = 0;	
-	UINT32 updateDataTotalByteLen = 0;
-	UpdateStep updateStep = UPDATE_STEP_CHECK_VERSION;	
-	
-	UINT8 i,j,ret=0;
-	UINT8 dataLen = 0;
-		
-	UINT8 pUpdateMsgSend[80];
-	UINT32 updateMsgSendLen = 0;	
-	UINT32 currentPackageStartAddr = 0;
-	BMS_Update_Recv_Msg_Type pUpdateMsgRecv;
-	UINT8 bmsUpdateFlag = 1;
-	//BMS_Update_Recv_Msg_Type bmsMsg;
-	//static UpdateStep step = UPDATE_STEP_CHECK_VERSION;
-	UINT8 Cycle_conut = 0;
-	while(bmsUpdateFlag && Cycle_conut<2)
-	{
-		switch (updateStep)
-		{
-			case UPDATE_STEP_CHECK_VERSION:
-
-				dataLen = 0;
-				updateMsgSendLen = 7;
-				pUpdateMsgSend[0] = 0xEB; //start flag
-				pUpdateMsgSend[1] = 0x01;	//add flag
-				pUpdateMsgSend[2] = 0x01; //read
-				pUpdateMsgSend[3] = 0x03;	//data len
-				pUpdateMsgSend[4] = 0x90;	//cmd
-				pUpdateMsgSend[5] = 0x93;	//checksum
-				pUpdateMsgSend[6] = 0xF5;	//end flag		
-				//printf("updateMsgSendLen0 = %x\n",updateMsgSendLen);
-				memset((UINT8*)(&pUpdateMsgRecv) , 0, sizeof(BMS_Update_Recv_Msg_Type));
-				ret = SP_BMS_Update_Query(pUpdateMsgSend, updateMsgSendLen, (UINT8*)(&pUpdateMsgRecv),sizeof(BMS_Update_Recv_Msg_Type), 500);
-				//printf("updateMsgSendLen1 = %x\n",updateMsgSendLen);
-				if(ret!=0)
-				{
-					if(pUpdateMsgRecv.startFlag == 0xEB && pUpdateMsgRecv.endFlag == 0xF5)
-					{
-						if(pUpdateMsgRecv.cmd == 0x90)
-						{
-							if(pUpdateMsgRecv.data != 0xFF)
-							{
-								updateStep = UPDATE_STEP_REQUEST_UPDATE;
-								errorCount = 0;
-							}
-							else
-							{
-								updateStep = UPDATE_STEP_SET_BAUD_RATE;
-								errorCount = 0;
-							}
-						}
-						else
-						{
-							errorCount++;
-						}
-					}
-					else
-					{
-						errorCount++;
-					}
-				}			
-				else
-				{
-					errorCount++;
-				}
-				#ifdef USING_PRINTF1
-							//printf("update step:%d\n",updateStep);
-							printf("query:");
-							
-							for(j=0;j<updateMsgSendLen;j++)
-							{
-								printf("%x ",pUpdateMsgSend[j]);
-							}
-							
-							printf("\nanswer:");
-							for(j=0;j<sizeof(BMS_Update_Recv_Msg_Type);j++)
-							{
-								printf("%x ",*(((UINT8*)&pUpdateMsgRecv)+j));
-							}
-							printf("\n");
-							printf("next update step:%d\n",updateStep);
-				#endif
-				if(errorCount>10)
-				{
-					updateStep = UPDATE_STEP_RESET;
-					errorCount = 0;
-				}
-				osDelay(50);
-				break;
-			
-			case UPDATE_STEP_REQUEST_UPDATE:
-				dataLen = 1;
-				updateMsgSendLen = 8;
-				pUpdateMsgSend[0] = 0xEB; //start flag
-				pUpdateMsgSend[1] = 0x01;	//add flag
-				pUpdateMsgSend[2] = 0x00; //write
-				pUpdateMsgSend[3] = 0x04;	//data len
-				pUpdateMsgSend[4] = 0x80;	//cmd
-				pUpdateMsgSend[5] = 0x22;	//data
-				pUpdateMsgSend[6] = 0xA6;	//check
-				pUpdateMsgSend[7] = 0xF5;	//end flag
-				memset((UINT8*)(&pUpdateMsgRecv) , 0, sizeof(BMS_Update_Recv_Msg_Type));
-				ret = SP_BMS_Update_Query(pUpdateMsgSend, updateMsgSendLen, (UINT8*)(&pUpdateMsgRecv), sizeof(BMS_Update_Recv_Msg_Type), 500);
-				if(ret!=0)
-				{
-					if(pUpdateMsgRecv.startFlag == 0xEB && pUpdateMsgRecv.endFlag == 0xF5)
-					{
-						if(pUpdateMsgRecv.cmd == 0x80)
-						{
-							if(pUpdateMsgRecv.data == 0x33)
-							{
-								updateStep = UPDATE_STEP_START_UPDATE;
-								errorCount = 0;
-							}
-							else
-							{
-								errorCount++;
-							}						
-						}
-						else
-						{
-							errorCount++;
-						}
-					}
-					else
-					{
-						errorCount++;
-					}
-				}			
-				else
-				{
-					errorCount++;
-				}
-
-				if(errorCount>10)
-				{
-					updateStep = UPDATE_STEP_RESET;
-					errorCount = 0;
-				}
-				
-				#ifdef USING_PRINTF1
-							printf("update step:%d\n",updateStep);
-							printf("query:");
-							for(j=0;j<updateMsgSendLen;j++)
-							{
-								printf("%x ",pUpdateMsgSend[j]);
-							}
-							printf("\nanswer:");
-							for(j=0;j<sizeof(BMS_Update_Recv_Msg_Type);j++)
-							{
-								printf("%x ",*(((UINT8*)&pUpdateMsgRecv)+j));
-							}
-							printf("\n");
-							printf("next update step:%d\n",updateStep);
-				#endif
-				osDelay(50);
-				break;
-
-			case UPDATE_STEP_START_UPDATE:
-				dataLen = 1;
-				updateMsgSendLen = 8;
-				pUpdateMsgSend[0] = 0xEB; //start flag
-				pUpdateMsgSend[1] = 0x01;	//add flag
-				pUpdateMsgSend[2] = 0x00; //write
-				pUpdateMsgSend[3] = 0x04;	//data len
-				pUpdateMsgSend[4] = 0x80;	//cmd
-				pUpdateMsgSend[5] = 0x55;	//data
-				pUpdateMsgSend[6] = 0xD9;	//check
-				pUpdateMsgSend[7] = 0xF5;	//end flag
-				memset((UINT8*)(&pUpdateMsgRecv) , 0, sizeof(BMS_Update_Recv_Msg_Type));
-				ret = SP_BMS_Update_Query(pUpdateMsgSend, updateMsgSendLen, (UINT8*)(&pUpdateMsgRecv), 0, 500);
-				//updateStep = UPDATE_STEP_SET_BAUD_RATE;
-				updateStep = UPDATE_STEP_CHECK_VERSION_AGAIN;//2021-04-09跳过波特率设置
-				#ifdef USING_PRINTF1
-							
-							printf("query:");
-							for(j=0;j<updateMsgSendLen;j++)
-							{
-								printf("%x ",pUpdateMsgSend[j]);
-							}
-							printf("\nanswer:");
-							for(j=0;j<sizeof(BMS_Update_Recv_Msg_Type);j++)
-							{
-								printf("%x ",*(((UINT8*)&pUpdateMsgRecv)+j));
-							}
-							
-							printf("\n");		
-							printf("next update step:%d\n",updateStep);
-				#endif
-				break;
-			case UPDATE_STEP_CHECK_VERSION_AGAIN:
-				dataLen = 0;
-				updateMsgSendLen = 7;
-				pUpdateMsgSend[0] = 0xEB; //start flag
-				pUpdateMsgSend[1] = 0x01;	//add flag
-				pUpdateMsgSend[2] = 0x01; //read
-				pUpdateMsgSend[3] = 0x03;	//data len
-				pUpdateMsgSend[4] = 0x90;	//cmd
-				pUpdateMsgSend[5] = 0x93;	//checksum
-				pUpdateMsgSend[6] = 0xF5;	//end flag		
-				//printf("updateMsgSendLen0 = %x\n",updateMsgSendLen);
-				memset((UINT8*)(&pUpdateMsgRecv) , 0, sizeof(BMS_Update_Recv_Msg_Type));
-				ret = SP_BMS_Update_Query(pUpdateMsgSend, updateMsgSendLen, (UINT8*)(&pUpdateMsgRecv),sizeof(BMS_Update_Recv_Msg_Type), 100);
-				//printf("updateMsgSendLen1 = %x\n",updateMsgSendLen);
-				if(ret!=0)
-				{
-					if(pUpdateMsgRecv.startFlag == 0xEB && pUpdateMsgRecv.endFlag == 0xF5)
-					{
-						if(pUpdateMsgRecv.cmd == 0x90)
-						{
-							if(pUpdateMsgRecv.data != 0xFF)
-							{
-								updateStep = UPDATE_STEP_RESET;
-								errorCount = 0;
-							}
-							else
-							{
-								updateStep = UPDATE_STEP_SET_BAUD_RATE;
-								errorCount = 0;
-							}
-						}
-						else
-						{
-							errorCount++;
-						}
-					}
-					else
-					{
-						errorCount++;
-					}
-				}			
-				else
-				{
-					errorCount++;
-				}
-				#ifdef USING_PRINTF1
-							//printf("update step:%d\n",updateStep);
-							printf("query:");
-							
-							for(j=0;j<updateMsgSendLen;j++)
-							{
-								printf("%x ",pUpdateMsgSend[j]);
-							}
-							
-							printf("\nanswer:");
-							for(j=0;j<sizeof(BMS_Update_Recv_Msg_Type);j++)
-							{
-								printf("%x ",*(((UINT8*)&pUpdateMsgRecv)+j));
-							}
-							printf("\n");
-							printf("next update step:%d\n",updateStep);
-				#endif
-				if(errorCount>10)
-				{
-					updateStep = UPDATE_STEP_RESET;
-					errorCount = 0;
-				}
-				osDelay(50);
-				break;
-			case UPDATE_STEP_SET_BAUD_RATE:
-				printf("start step %d\n",updateStep);
-				dataLen = 4;
-				updateMsgSendLen = 12;
-				pUpdateMsgSend[0] = 0xEB; //start flag
-				pUpdateMsgSend[1] = 0x01;	//add flag
-				pUpdateMsgSend[2] = 0x00; //write
-				pUpdateMsgSend[3] = 0x08;	//data len
-				pUpdateMsgSend[4] = 0x81;	//cmd
-				pUpdateMsgSend[5] = 0x33;	//data
-				pUpdateMsgSend[6] = 0x00;	//baud rate:9600
-				pUpdateMsgSend[7] = 0x00;
-				pUpdateMsgSend[8] = 0x25;
-				pUpdateMsgSend[9] = 0x80;
-				pUpdateMsgSend[10] = 0x61;	//check
-				pUpdateMsgSend[11] = 0xF5;	//end flag
-				#ifdef USING_PRINTF1
-				printf("query:");
-				for(j=0;j<updateMsgSendLen;j++)
-				{
-					printf("%x ",pUpdateMsgSend[j]);
-				}
-				printf("\n");
-				#endif
-				memset((UINT8*)(&pUpdateMsgRecv) , 0, sizeof(BMS_Update_Recv_Msg_Type));
-				ret = SP_BMS_Update_Query(pUpdateMsgSend, updateMsgSendLen, (UINT8*)(&pUpdateMsgRecv), sizeof(BMS_Update_Recv_Msg_Type), 500);
-				printf("ret = %d\n",ret);
-				if(ret!=0)
-				{
-					if(pUpdateMsgRecv.startFlag == 0xEB && pUpdateMsgRecv.endFlag == 0xF5)
-					{
-						if(pUpdateMsgRecv.cmd == 0x81)
-						{
-							if(pUpdateMsgRecv.data == 0x11)
-							{
-								updateStep = UPDATE_STEP_PREPARE_SEND_DATA_LEN;
-								errorCount = 0;
-							}
-							else
-							{
-								errorCount++;
-							}						
-						}
-						else
-						{
-							errorCount++;
-						}
-					}
-					else
-					{
-						errorCount++;
-					}
-				}			
-				else
-				{
-					errorCount++;
-				}
-
-				if(errorCount>10)
-				{
-					updateStep = UPDATE_STEP_RESET;
-					errorCount = 0;
-				}
-				
-				#ifdef USING_PRINTF1
-					//printf("update step:%d\n",updateStep);
-					printf("query:");
-					for(j=0;j<updateMsgSendLen;j++)
-					{
-						printf("%x ",pUpdateMsgSend[j]);
-					}
-					printf("\nanswer:");
-					for(j=0;j<sizeof(BMS_Update_Recv_Msg_Type);j++)
-					{
-						printf("%x ",*(((UINT8*)&pUpdateMsgRecv)+j));
-					}
-					printf("\n");
-					printf("next update step:%d\n",updateStep);
-				#endif
-				osDelay(50);
-				break;
-			
-			
-			case UPDATE_STEP_PREPARE_SEND_DATA_LEN:
-				printf("start step %d\n",updateStep);
-				dataLen = 1;
-				updateMsgSendLen = 8;
-				pUpdateMsgSend[0] = 0xEB; //start flag
-				pUpdateMsgSend[1] = 0x01;	//add flag
-				pUpdateMsgSend[2] = 0x00; //write
-				pUpdateMsgSend[3] = 0x04;	//data len
-				pUpdateMsgSend[4] = 0x81;	//cmd
-				pUpdateMsgSend[5] = 0x44;	//data
-				pUpdateMsgSend[6] = 0xC9;	//check
-				pUpdateMsgSend[7] = 0xF5;	//end flag
-				memset((UINT8*)(&pUpdateMsgRecv) , 0, sizeof(BMS_Update_Recv_Msg_Type));
-				ret = SP_BMS_Update_Query(pUpdateMsgSend, updateMsgSendLen, (UINT8*)(&pUpdateMsgRecv), sizeof(BMS_Update_Recv_Msg_Type), 500);
-				if(ret!=0)
-				{
-					if(pUpdateMsgRecv.startFlag == 0xEB && pUpdateMsgRecv.endFlag == 0xF5)
-					{
-						if(pUpdateMsgRecv.cmd == 0x81)
-						{
-							if(pUpdateMsgRecv.data == 0x11)
-							{
-								updateStep = UPDATE_STEP_SEND_DATA_LEN;
-								errorCount = 0;
-							}
-							else
-							{
-								errorCount++;
-							}						
-						}
-						else
-						{
-							errorCount++;
-						}
-					}
-					else
-					{
-						errorCount++;
-					}
-				}			
-				else
-				{
-					errorCount++;
-				}
-
-				if(errorCount>10)
-				{
-					updateStep = UPDATE_STEP_RESET;
-					errorCount = 0;
-				}
-				
-				#ifdef USING_PRINTF1
-					//printf("update step:%d\n",updateStep);
-					printf("query:");
-					for(j=0;j<updateMsgSendLen;j++)
-					{
-						printf("%x ",pUpdateMsgSend[j]);
-					}
-					printf("\nanswer:");
-					for(j=0;j<sizeof(BMS_Update_Recv_Msg_Type);j++)
-					{
-						printf("%x ",*(((UINT8*)&pUpdateMsgRecv)+j));
-					}
-					printf("\n");	
-					printf("next update step:%d\n",updateStep);
-				#endif
-				osDelay(50);
-				break;		
-			case UPDATE_STEP_SEND_DATA_LEN:
-				
-				dataLen = 4;
-				BSP_QSPI_Read_Safe(&updateDataTotalByteLen,FLASH_BMS_FOTA_START_ADDR,4);
-				updateDataTotalByteLen = (((updateDataTotalByteLen)&0xFF)<<24)|(((updateDataTotalByteLen>>8)&0xFF)<<16)|(((updateDataTotalByteLen>>16)&0xFF)<<8)|(((updateDataTotalByteLen>>24)&0xFF));
-				updateMsgSendLen = 11;
-				pUpdateMsgSend[0] = 0xEB; //start flag
-				pUpdateMsgSend[1] = 0x01;	//add flag
-				pUpdateMsgSend[2] = 0x00; //write
-				pUpdateMsgSend[3] = 0x07;	//data len
-				pUpdateMsgSend[4] = 0x82;	//cmd
-				pUpdateMsgSend[5] = (updateDataTotalByteLen>>24)&0xFF;	//data: package byte len
-				pUpdateMsgSend[6] = (updateDataTotalByteLen>>16)&0xFF;	
-				pUpdateMsgSend[7] = (updateDataTotalByteLen>>8)&0xFF;	
-				pUpdateMsgSend[8] = (updateDataTotalByteLen)&0xFF;
-				pUpdateMsgSend[9] = SP_BMS_Update_CheckSUM(&pUpdateMsgSend[3], dataLen+2);	//check sum
-				pUpdateMsgSend[10] = 0xF5;	//end flag
-				
-				memset((UINT8*)(&pUpdateMsgRecv),0,sizeof(BMS_Update_Recv_Msg_Type));
-				ret = SP_BMS_Update_Query(pUpdateMsgSend, updateMsgSendLen, (UINT8*)(&pUpdateMsgRecv), sizeof(BMS_Update_Recv_Msg_Type), 500);
-				if(ret!=0)
-				{
-					if(pUpdateMsgRecv.startFlag == 0xEB && pUpdateMsgRecv.endFlag == 0xF5)
-					{
-						if(pUpdateMsgRecv.cmd == 0x81)
-						{
-							if(pUpdateMsgRecv.data == 0x11)
-							{
-								updateStep = UPDATE_STEP_PREPARE_SEND_UPDATE_DATA;
-								errorCount = 0;
-							}
-							else
-							{
-								errorCount++;
-							}						
-						}
-						else
-						{
-							errorCount++;
-						}
-					}
-					else
-					{
-						errorCount++;
-					}
-				}			
-				else
-				{
-					errorCount++;
-				}
-
-				if(errorCount>10)
-				{
-					updateStep = UPDATE_STEP_RESET;
-					errorCount = 0;
-				}
-				
-				#ifdef USING_PRINTF1
-							//printf("update step:%d\n",updateStep);
-							printf("query:");
-							for(j=0;j<updateMsgSendLen;j++)
-							{
-								printf("%x ",pUpdateMsgSend[j]);
-							}
-							printf("\nanswer:");
-							for(j=0;j<sizeof(BMS_Update_Recv_Msg_Type);j++)
-							{
-								printf("%x ",*(((UINT8*)&pUpdateMsgRecv)+j));
-							}
-							printf("\n");
-							printf("next update step:%d\n",updateStep);
-				#endif
-				osDelay(50);
-				break;	
-				
-			case UPDATE_STEP_PREPARE_SEND_UPDATE_DATA:
-				dataLen = 1;
-				updateMsgSendLen = 8;
-				pUpdateMsgSend[0] = 0xEB; //start flag
-				pUpdateMsgSend[1] = 0x01;	//add flag
-				pUpdateMsgSend[2] = 0x00; //write
-				pUpdateMsgSend[3] = 0x04;	//data len
-				pUpdateMsgSend[4] = 0x81;	//cmd
-				pUpdateMsgSend[5] = 0x55;	//data
-				pUpdateMsgSend[6] = 0xDA;	//check
-				pUpdateMsgSend[7] = 0xF5;	//end flag
-				memset((UINT8*)(&pUpdateMsgRecv) , 0, sizeof(BMS_Update_Recv_Msg_Type));
-				ret = SP_BMS_Update_Query(pUpdateMsgSend, updateMsgSendLen,(UINT8*)(&pUpdateMsgRecv), sizeof(BMS_Update_Recv_Msg_Type), 500);
-				if(ret!=0)
-				{
-					if(pUpdateMsgRecv.startFlag == 0xEB && pUpdateMsgRecv.endFlag == 0xF5)
-					{
-						if(pUpdateMsgRecv.cmd == 0x81)
-						{
-							if(pUpdateMsgRecv.data == 0x11)
-							{
-								updateStep = UPDATE_STEP_SEND_UPDATE_DATA;
-								errorCount = 0;
-							}
-							else
-							{
-								errorCount++;
-							}						
-						}
-						else
-						{
-							errorCount++;
-						}
-					}
-					else
-					{
-						errorCount++;
-					}
-				}			
-				else
-				{
-					errorCount++;
-				}
-
-				if(errorCount>10)
-				{
-					updateStep = UPDATE_STEP_RESET;
-					errorCount = 0;
-				}
-				
-				#ifdef USING_PRINTF1
-					//printf("update step:%d\n",updateStep);
-					printf("query:");
-					for(j=0;j<updateMsgSendLen;j++)
-					{
-						printf("%x ",pUpdateMsgSend[j]);
-					}
-					printf("\nanswer:");
-					for(j=0;j<sizeof(BMS_Update_Recv_Msg_Type);j++)
-					{
-						printf("%x ",*(((UINT8*)&pUpdateMsgRecv)+j));
-					}
-					printf("\n");	
-					printf("next update step:%d\n",updateStep);
-				#endif
-				osDelay(50);
-				break;
-			case UPDATE_STEP_SEND_UPDATE_DATA:
-				dataLen = 64;
-				updateMsgSendLen = 75;
-							
-				for(currentPackage=0;currentPackage<updateDataTotalByteLen/64;currentPackage++)
-				{
-					currentPackageStartAddr = currentPackage*64;
-				
-					pUpdateMsgSend[0] = 0xEB; //start flag
-					pUpdateMsgSend[1] = 0x01;	//add flag
-					pUpdateMsgSend[2] = 0x00; //write
-					pUpdateMsgSend[3] = 0x47;	//data len			
-					pUpdateMsgSend[4] = 0x82;	//cmd
-					pUpdateMsgSend[5] = (currentPackageStartAddr>>24)&0xFF;
-					pUpdateMsgSend[6] = (currentPackageStartAddr>>16)&0xFF;
-					pUpdateMsgSend[7] = (currentPackageStartAddr>>8)&0xFF;
-					pUpdateMsgSend[8] = currentPackageStartAddr&0xFF;
-					BSP_QSPI_Read_Safe(&pUpdateMsgSend[9], FLASH_BMS_FOTA_START_ADDR+4+currentPackage*dataLen, dataLen);  //data			
-					pUpdateMsgSend[8+dataLen+1] = SP_BMS_Update_CheckSUM(&pUpdateMsgSend[3], dataLen+6);  //check sum
-					pUpdateMsgSend[8+dataLen+2] = 0xF5;	//end flag
-					memset((UINT8*)(&pUpdateMsgRecv) , 0, sizeof(BMS_Update_Recv_Msg_Type));
-					ret = SP_BMS_Update_Query(pUpdateMsgSend, updateMsgSendLen, (UINT8*)(&pUpdateMsgRecv), sizeof(BMS_Update_Recv_Msg_Type), 500);
-					if(ret!=0)
-					{
-						if(pUpdateMsgRecv.startFlag == 0xEB && pUpdateMsgRecv.endFlag == 0xF5)
-						{
-							if(pUpdateMsgRecv.cmd == 0x81)
-							{
-								if(pUpdateMsgRecv.data == 0x11)
-								{
-									if(currentPackage+1 == updateDataTotalByteLen/64)
-									{
-										updateStep = UPDATE_STEP_SEND_DATA_END;
-									}
-									errorCount = 0;
-								}
-								else
-								{
-									errorCount++;
-								}						
-							}
-							else
-							{
-								errorCount++;
-							}
-						}
-						else
-						{
-							errorCount++;
-						}
-					}			
-					else
-					{
-						errorCount++;
-					}
-
-					if(errorCount>10)
-					{					
-						updateStep = UPDATE_STEP_RESET;
-						errorCount = 0;
-						break;
-					}
-				#ifdef USING_PRINTF1
-							//printf("update step:%d\n",updateStep);
-							printf("query:");
-							for(j=0;j<updateMsgSendLen;j++)
-							{
-								printf("%x ",pUpdateMsgSend[j]);
-							}
-							printf("\nanswer:");
-							for(j=0;j<sizeof(BMS_Update_Recv_Msg_Type);j++)
-							{
-								printf("%x ",*(((UINT8*)&pUpdateMsgRecv)+j));
-							}
-							printf("\n");
-							printf("next update step:%d\n",updateStep);
-				#endif
-				}
-				osDelay(50);
-				break;
-
-			case UPDATE_STEP_SEND_DATA_END:
-				dataLen = 1;
-				updateMsgSendLen = 8;
-				pUpdateMsgSend[0] = 0xEB; //start flag
-				pUpdateMsgSend[1] = 0x01;	//add flag
-				pUpdateMsgSend[2] = 0x00; //write
-				pUpdateMsgSend[3] = 0x04;	//data len
-				pUpdateMsgSend[4] = 0x81;	//cmd
-				pUpdateMsgSend[5] = 0x66;	//data
-				pUpdateMsgSend[6] = 0xEB;	//check
-				pUpdateMsgSend[7] = 0xF5;	//end flag
-				memset((UINT8*)(&pUpdateMsgRecv) , 0, sizeof(BMS_Update_Recv_Msg_Type));
-				ret = SP_BMS_Update_Query(pUpdateMsgSend, updateMsgSendLen, (UINT8*)(&pUpdateMsgRecv), sizeof(BMS_Update_Recv_Msg_Type), 500);
-				if(ret!=0)
-				{
-					if(pUpdateMsgRecv.startFlag == 0xEB && pUpdateMsgRecv.endFlag == 0xF5)
-					{
-						if(pUpdateMsgRecv.cmd == 0x81)
-						{
-							if(pUpdateMsgRecv.data == 0x11)
-							{
-								updateStep = UPDATE_STEP_START_INSTALL;
-								errorCount = 0;
-							}
-							else
-							{
-								errorCount++;
-							}						
-						}
-						else
-						{
-							errorCount++;
-						}
-					}
-					else
-					{
-						errorCount++;
-					}
-				}			
-				else
-				{
-					errorCount++;
-				}
-
-				if(errorCount>10)
-				{
-					updateStep = UPDATE_STEP_RESET;
-					errorCount = 0;
-				}
-				
-				#ifdef USING_PRINTF1
-							//printf("update step:%d\n",updateStep);
-							printf("query:");
-							for(j=0;j<updateMsgSendLen;j++)
-							{
-								printf("%x ",pUpdateMsgSend[j]);
-							}
-							printf("\nanswer:");
-							for(j=0;j<sizeof(BMS_Update_Recv_Msg_Type);j++)
-							{
-								printf("%x ",*(((UINT8*)&pUpdateMsgRecv)+j));
-							}
-							printf("\n");
-							printf("next update step:%d\n",updateStep);
-				#endif
-				osDelay(50);
-				break;			
-
-			case UPDATE_STEP_START_INSTALL:
-				dataLen = 1;
-				updateMsgSendLen = 8;
-				pUpdateMsgSend[0] = 0xEB; //start flag
-				pUpdateMsgSend[1] = 0x01;	//add flag
-				pUpdateMsgSend[2] = 0x00; //write
-				pUpdateMsgSend[3] = 0x04;	//data len
-				pUpdateMsgSend[4] = 0x81;	//cmd
-				pUpdateMsgSend[5] = 0x99;	//data
-				pUpdateMsgSend[6] = 0x1E;	//check
-				pUpdateMsgSend[7] = 0xF5;	//end flag
-				memset((UINT8*)(&pUpdateMsgRecv) , 0, sizeof(BMS_Update_Recv_Msg_Type));
-				SP_BMS_Update_Query(pUpdateMsgSend, updateMsgSendLen, (UINT8*)(&pUpdateMsgRecv), 0, 500);
-				
-				updateStep = UPDATE_STEP_END;
-				
-				#ifdef USING_PRINTF1
-							//printf("update step:%d\n",updateStep);
-							printf("query:");
-							for(j=0;j<updateMsgSendLen;j++)
-							{
-								printf("%x ",pUpdateMsgSend[j]);
-							}
-							printf("\nanswer:");
-							for(j=0;j<sizeof(BMS_Update_Recv_Msg_Type);j++)
-							{
-								printf("%x ",*(((UINT8*)&pUpdateMsgRecv)+j));
-							}
-							printf("\n");	
-							printf("next update step:%d\n",updateStep);
-				#endif
-				osDelay(50);
-				break;
-			case UPDATE_STEP_END:
-				updateStep = UPDATE_STEP_CHECK_VERSION;
-				printf("update end\n");
-				bmsUpdateFlag = 0;
-				break;
-			case UPDATE_STEP_RESET:
-				dataLen = 1;
-				updateMsgSendLen = 8;
-				pUpdateMsgSend[0] = 0xEB; //start flag
-				pUpdateMsgSend[1] = 0x01;	//add flag
-				pUpdateMsgSend[2] = 0x00; //write
-				pUpdateMsgSend[3] = 0x04;	//data len
-				pUpdateMsgSend[4] = 0x81;	//cmd
-				pUpdateMsgSend[5] = 0xAA;	//data
-				pUpdateMsgSend[6] = 0x2F;	//check
-				pUpdateMsgSend[7] = 0xF5;	//end flag
-				memset((UINT8*)(&pUpdateMsgRecv) , 0, sizeof(BMS_Update_Recv_Msg_Type));
-				SP_BMS_Update_Query(pUpdateMsgSend, updateMsgSendLen, (UINT8*)(&pUpdateMsgRecv), 0, 500);
-				osDelay(50);
-				
-				resetCount++;
-				if(resetCount>=2)
-				{
-					updateStep = UPDATE_STEP_DOWNLOAD_BREAK_OFF;
-					resetCount = 0;
-				}
-				else
-				{
-					updateStep = UPDATE_STEP_PREPARE_SEND_DATA_LEN;
-				}
-			#ifdef USING_PRINTF
-				printf("update error!!\n rest and start send data lenth again!!\n continue update!\n");
-			#endif
-				break;
-			case UPDATE_STEP_DOWNLOAD_BREAK_OFF:
-				dataLen = 1;
-				updateMsgSendLen = 8;
-				pUpdateMsgSend[0] = 0xEB; //start flag
-				pUpdateMsgSend[1] = 0x01;	//add flag
-				pUpdateMsgSend[2] = 0x00; //write
-				pUpdateMsgSend[3] = 0x04;	//data len
-				pUpdateMsgSend[4] = 0x81;	//cmd
-				pUpdateMsgSend[5] = 0xBB;	//data
-				pUpdateMsgSend[6] = 0x40;	//check
-				pUpdateMsgSend[7] = 0xF5;	//end flag
-				memset((UINT8*)(&pUpdateMsgRecv) , 0, sizeof(BMS_Update_Recv_Msg_Type));
-				SP_BMS_Update_Query(pUpdateMsgSend, updateMsgSendLen, (UINT8*)(&pUpdateMsgRecv), 0, 500);
-				osDelay(50);
-				updateStep = UPDATE_STEP_CHECK_VERSION;
-				Cycle_conut++;
-				break;
-			case UPDATE_STEP_ERROR:
-				updateStep = UPDATE_STEP_CHECK_VERSION;
-				printf("update error end\n");
-				bmsUpdateFlag = 0;
-			break;
-
-			default:
-				updateStep = UPDATE_STEP_CHECK_VERSION;
-				printf("update default end\n");
-				bmsUpdateFlag = 0;
-			break;
-		}
-	}
-}
-
-UINT8 SP_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)!=0xEB)
-			{
-				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;
-	}
-}
-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;
-}
-//________________________________________________________________________________
-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);
-}
-
-UINT8 BmsErrorDecode(UINT32 battWarningState)
-{
-	UINT16 ErrorNumTemp;
-	UINT8 ret;
-	if(battWarningState==0)
-	{
-		return 0;
-	}
-	else
-	{
-		if(osOK==osMutexAcquire(Error_Mutex, 100))
-		{
-			ret = ((battWarningState) & 0x01) == 1 ;
-			if (ret)
-			{
-				ErrorNumTemp = 7;
-				PutErrorNum((UINT16 *)ErrorNum,sizeof(ErrorNum),ErrorNumTemp);
-				//str += "ERROR:存在电芯过放告警故障!!\n";单体电压过低
-			}
-
-			ret = ((battWarningState >> 1) & 0x01) == 1 ;
-			if (ret)
-			{
-				ErrorNumTemp = 10;
-				PutErrorNum((UINT16 *)ErrorNum,sizeof(ErrorNum),ErrorNumTemp);
-				//str += "ERROR:存在总电压过放告警故障!!\n";总电压过低
-			}
-
-			ret = ((battWarningState >> 2) & 0x01) == 1 ;
-			if (ret)
-			{
-				ErrorNumTemp = 8;
-				PutErrorNum((UINT16 *)ErrorNum,sizeof(ErrorNum),ErrorNumTemp);
-				//str += "ERROR:存在电芯过压告警故障!!\n";
-			}
-
-			ret = ((battWarningState >> 3) & 0x01) == 1 ;
-			if (ret)
-			{
-				ErrorNumTemp = 11;
-				PutErrorNum((UINT16 *)ErrorNum,sizeof(ErrorNum),ErrorNumTemp);
-				//str += "ERROR:存在总电压过压告警故障!!\n";
-			}
-
-			ret = ((battWarningState >> 4) & 0x01) == 1 ;
-			if (ret)
-			{
-				ErrorNumTemp = 12;
-				PutErrorNum((UINT16 *)ErrorNum,sizeof(ErrorNum),ErrorNumTemp);
-				//str += "ERROR:存在放电过流告警故障!!\n";
-			}
-
-			ret = ((battWarningState >> 5) & 0x01) == 1 ;
-			if (ret)
-			{
-				ErrorNumTemp = 13;
-				PutErrorNum((UINT16 *)ErrorNum,sizeof(ErrorNum),ErrorNumTemp);
-				//str += "ERROR:存在充电过流告警故障!!\n";
-			}
-
-			ret = ((battWarningState >> 6) & 0x01) == 1 ;
-			if (ret)
-			{
-				ErrorNumTemp = 2;
-				PutErrorNum((UINT16 *)ErrorNum,sizeof(ErrorNum),ErrorNumTemp);
-				//str += "ERROR:存在放电过温告警故障!!\n";
-			}
-
-			ret = ((battWarningState >> 7) & 0x01) == 1 ;
-			if (ret)
-			{
-				ErrorNumTemp = 2;
-				PutErrorNum((UINT16 *)ErrorNum,sizeof(ErrorNum),ErrorNumTemp);
-				//str += "ERROR:存在充电过温告警故障!!\n";
-			}
-
-			ret = ((battWarningState >> 8) & 0x01) == 1 ;
-			if (ret)
-			{
-				//str += "ERROR:存在环境高温告警故障!!\n";
-			}
-
-			ret = ((battWarningState >> 9) & 0x01) == 1 ;
-			if (ret)
-			{
-				//str += "ERROR:存在环境低温告警故障!!\n";
-			}
-
-			ret = ((battWarningState >> 10) & 0x01) == 1 ;
-			if (ret)
-			{
-				ErrorNumTemp = 27;
-				PutErrorNum((UINT16 *)ErrorNum,sizeof(ErrorNum),ErrorNumTemp);
-				//str += "ERROR:存在battSOC低告警故障!!\n";
-			}
-
-			ret = ((battWarningState >> 11) & 0x01) == 1 ;
-			if (ret)
-			{
-				ErrorNumTemp = 3;
-				PutErrorNum((UINT16 *)ErrorNum,sizeof(ErrorNum),ErrorNumTemp);
-				//str += "ERROR:存在MOS高温告警故障!!\n";
-			}
-
-			ret = ((battWarningState >> 16) & 0x01) == 1;
-			if (ret)
-			{
-				ErrorNumTemp = 18;
-				PutErrorNum((UINT16 *)ErrorNum,sizeof(ErrorNum),ErrorNumTemp);
-				//str += "ERROR:存在温度采集失效/传感器故障!!\n";
-			}
-
-			ret = ((battWarningState >> 17) & 0x01) == 1;
-			if (ret)
-			{
-				ErrorNumTemp = 19;
-				PutErrorNum((UINT16 *)ErrorNum,sizeof(ErrorNum),ErrorNumTemp);
-				//str += "ERROR:存在电压采集失效/断线故障!!\n";
-			}
-
-			ret = ((battWarningState >> 18) & 0x01) == 1;
-			if (ret)
-			{
-				ErrorNumTemp = 17;
-				PutErrorNum((UINT16 *)ErrorNum,sizeof(ErrorNum),ErrorNumTemp);
-				//str += "ERROR:存在放电MOS失效故障!!\n";
-			}
-
-			ret = ((battWarningState >> 19) & 0x01) == 1;
-			if (ret)
-			{
-				ErrorNumTemp = 16;
-				PutErrorNum((UINT16 *)ErrorNum,sizeof(ErrorNum),ErrorNumTemp);
-				//str += "ERROR:存在充电MOS失效故障!!\n";
-			}
-
-			ret = ((battWarningState >> 20) & 0x01) == 1;
-			if (ret)
-			{
-				ErrorNumTemp = 22;
-				PutErrorNum((UINT16 *)ErrorNum,sizeof(ErrorNum),ErrorNumTemp);
-				//str += "ERROR:存在电芯不均衡告警!!\n";
-			}
-
-			ret = ((battWarningState >> 22) & 0x01) == 1;
-			if (ret)
-			{
-				ErrorNumTemp = 1;
-				PutErrorNum((UINT16 *)ErrorNum,sizeof(ErrorNum),ErrorNumTemp);
-				//str += "ERROR:存在放电低温告警故障!!\n";
-			}
-
-			ret = ((battWarningState >> 23) & 0x01) == 1 ;
-			if (ret)
-			{
-				ErrorNumTemp = 1;
-				PutErrorNum((UINT16 *)ErrorNum,sizeof(ErrorNum),ErrorNumTemp);
-				//str += "ERROR:存在充电低温告警故障!!\n";
-			}
-		}
-		else
-		{
-			#ifdef USING_PRINTF
-				printf("get Muxtex error\n");
-			#endif
-		}
-		osMutexRelease(Error_Mutex);
-	}
-	return 1;
+    osThreadTerminate(UartTaskId);
+    UartTaskId = NULL;
 }

+ 1 - 5
src/bsp_custom.c

@@ -13,7 +13,7 @@
 #include "slpman_ec616.h"
 #include "plat_config.h"
 #include "debug_log.h"
-#include "Signal.h"
+#include "AppSignal.h"
 #ifdef BL_FILE_LOG
 extern uint8_t lockoutState;
 #endif
@@ -206,10 +206,6 @@ void Pad0_WakeupIntHandler(void)
     {
         return;
     }
-    else
-    {
-        CanMsgEnble = true;
-    }
 #ifdef USING_PRINTF1
 	printf("[%d]PadWakeup0_IRQn\r\n",__LINE__);
 // #else