Browse Source

Merge branch 'V2版本CAN修改分支' into 发布版本

CHENJIE-PC\QiXiang_CHENJIE 3 years ago
parent
commit
4ce97385cf
21 changed files with 1443 additions and 622 deletions
  1. 5 2
      ARMCC/Makefile
  2. 2 0
      inc/CANEncode.h
  3. 1 0
      inc/CANTask.h
  4. 1 1
      inc/MainTask.h
  5. 56 0
      inc/Signal.h
  6. 3 1
      inc/TcpTask.h
  7. 1 2
      inc/UartTask.h
  8. 4 3
      inc/app.h
  9. 3 1
      inc/hal_module_adapter.h
  10. 25 25
      inc/sensor.h
  11. 234 0
      src/CANEncode.c
  12. 131 16
      src/CANTask.c
  13. 1 1
      src/GpsTask.c
  14. 26 12
      src/MainTask.c
  15. 84 0
      src/Signal.c
  16. 94 85
      src/TcpTask.c
  17. 107 81
      src/UDSService.c
  18. 391 240
      src/UartTask.c
  19. 9 7
      src/bsp_custom.c
  20. 4 1
      src/hal_module_adapter.c
  21. 261 144
      src/sensor.c

+ 5 - 2
ARMCC/Makefile

@@ -23,7 +23,10 @@ obj-y             += PLAT/project/$(TARGET)/apps/qx_app/src/app.o \
 					PLAT/project/$(TARGET)/apps/qx_app/src/TcpTask.o  \
 					PLAT/project/$(TARGET)/apps/qx_app/src/GpsTask.o \
 					PLAT/project/$(TARGET)/apps/qx_app/src/CANTask.o \
-					PLAT/project/$(TARGET)/apps/qx_app/src/UDSService.o \
-					PLAT/project/$(TARGET)/apps/qx_app/src/Fota.o
+					PLAT/project/$(TARGET)/apps/qx_app/src/UDSService.o\
+					PLAT/project/$(TARGET)/apps/qx_app/src/Fota.o\
+					PLAT/project/$(TARGET)/apps/qx_app/src/Signal.o\
+					PLAT/project/$(TARGET)/apps/qx_app/src/CANEncode.o\
+					PLAT/project/$(TARGET)/apps/qx_app/src/sensor.o
 
 include $(TOP)/PLAT/tools/scripts/Makefile.rules

+ 2 - 0
inc/CANEncode.h

@@ -0,0 +1,2 @@
+#include 	 "commontypedef.h"
+extern void CANEncodeFunction(UINT32 ID,UINT8* rawData);

+ 1 - 0
inc/CANTask.h

@@ -19,6 +19,7 @@ typedef enum
 
 void CANTaskInit(void* arg);
 static void Can_Receive(void);
+void CanMsgTx1000ms(void);
 
 
 

+ 1 - 1
inc/MainTask.h

@@ -9,7 +9,7 @@
 //全局变量
 #include "app.h"
 extern UINT32 Timer_count;//每100ms加1
-extern volatile bool Sleep_flag;//睡眠标志位
+extern volatile BOOL Sleep_flag;//睡眠标志位
 extern AppNVMDataType AppNVMData;
 extern AppDataBody AppDataInfo;
 /*---------------------------------------------------------------------------*/

+ 56 - 0
inc/Signal.h

@@ -0,0 +1,56 @@
+#include "commontypedef.h"
+
+extern	UINT16	COMOutTable[28];
+//declear the UINT8 vars
+extern	UINT8	battCellTemp[8];
+extern	UINT8	MOSTemp;
+extern	UINT8	packTemp;
+extern	UINT8	fastChargeTemp;
+extern	UINT8	normalChargeTemp;
+extern	UINT8	heatTemp1;
+extern	UINT8	heatTemp2;
+extern	UINT8	nbReservedTemp1;
+extern	UINT8	nbReservedTemp2;
+extern	UINT8	maxCellTemp;
+extern	UINT8	minCellTemp;
+extern	UINT8	battSOC;
+extern	UINT8	battSOH;
+extern	UINT8	battWorkState;
+extern	UINT8	bmsSwVersion;
+extern	UINT8	bmsHwVersion;
+extern	UINT8	battMOSSwitchState;
+extern	UINT8	battHeatEnableState;
+extern	UINT8	reservedSignal1;
+extern	UINT8	reservedSignal2;
+extern	UINT8	reservedSignal3;
+extern	UINT8	reservedSignal4;
+extern	UINT8	reservedSignal5;
+extern	UINT8	reservedSignal6;
+extern	UINT8	reservedSignal7;
+extern	UINT8	reservedSignal8;
+
+
+
+
+//declear the UINT16 vars
+extern	UINT16	battCellU[28];
+extern	UINT16	minCellVol;
+extern	UINT16	maxCellVol;
+extern	UINT16	battPackVol;
+extern	UINT16	battI;
+extern	UINT16	nbHwVersion;
+
+
+
+
+//declear the UINT32 vars
+extern	UINT32	battWarningState;
+extern	UINT32	nbSwVersion;
+extern	UINT32	battBalanceoInfo;
+
+
+
+extern UINT8 	TcpSendLen;
+
+
+

+ 3 - 1
inc/TcpTask.h

@@ -18,6 +18,8 @@ extern UINT32 TcpService;
 #define QMSG_ID_SOCK_EXIT       	(QMSG_ID_BASE + 6)
 #define APP_EVENT_QUEUE_SIZE    (10)
 
+#define CHRG_END_DELAY    (1800)
+#define RUN_END_DELAY    (60)
 
 #define QX_TCP_IPADRRES				"47.97.127.222"
 #define QX_TCP_PORT					8712
@@ -69,7 +71,7 @@ typedef struct BattInfoType
 	UINT8	battTempNum;			//uint8 should change to uint16 (0~65535)
 	UINT8	battCellTemp[BATT_TEMP_NUM];
 	UINT8	battWorkState;
-	UINT8	battHeatState;
+	UINT8	battHeatEnableState;
 	UINT8 	battotherTempNum;
 	UINT8 	battotherTemp[BATT_OTHER_TEMP_NUM];
 }BattInfoType;

+ 1 - 2
inc/UartTask.h

@@ -10,7 +10,7 @@
 extern UINT8 BattChrgEndFlag;
 typedef struct _UartRedMsg
 {
-	volatile bool UartFlag;
+	volatile BOOL UartFlag;
     UINT8 Header[3];
     UINT8 data[120];
     UINT16 len;
@@ -21,7 +21,6 @@ typedef struct Uart_Write_Data_Type
     uint8_t Data[2];
 }Uart_Write_Data_Type;
 //全局变量输出区
-extern UartReadMsgType UartReadMsg;
 extern osMutexId_t UartMutex;
 extern QueueHandle_t UartWriteCmdHandle;
 //

+ 4 - 3
inc/app.h

@@ -43,8 +43,8 @@ extern "C" {
 #define HWVERSION		    0x0102    //硬件主版本,现为V1.2板
 #define	BLSWVERSION		0x01020000    //BootLoader版本号V1.2.0.0
 #define	DRVSWVERSION		0x01040000     //驱动层版本号V1.4.0.0
-//#define	APPSWVERSION		0x01020206     
-#define	APPSWVERSION		0x02000005
+//#define	APPSWVERSION		0x01020208     
+#define	APPSWVERSION		0x02000008
 //--------------------------------------------------------------------------------
 
 #define APP_CONFIG_FILE_LATEST_VERSION 0
@@ -74,7 +74,7 @@ typedef struct AppConfigHeader
 }AppConfigHeader;
 typedef struct _UartAnswerMsg
 {
-	bool UartFlag;
+	BOOL UartFlag;
     UINT8 dataPtr[8];
     UINT16 len;
 }UartAnswerMsgType;
@@ -97,6 +97,7 @@ typedef struct AppDataType
     UINT8  BmsDisChrgInfoSendFreq;
     UINT8  GpsChrgInfoSendFreq;
     UINT8  GpsDisChrgInfoSendFreq;
+    INT8  BattCurrentNegFlag;
     UINT32 CumulativeCapacity;
 }AppDataBody;
 typedef struct _AppDataHeader

+ 3 - 1
inc/hal_module_adapter.h

@@ -172,7 +172,9 @@ void bluejoy_printf(BlLogLevel level, const UINT8 *format, ...);
 void bluejoy_read_logfile(void);
 void bluejoy_del_logfile(void);
 #endif
-
+extern void GsensorInit(void);
+extern void GsensorI2CHandler(ARM_I2C_SignalEvent_t cb_event);
+extern void GsensorI2CCallback(UINT32 event);
 #ifdef __cplusplus
 }
 #endif

+ 25 - 25
inc/sensor.h

@@ -6,23 +6,23 @@
 #ifdef __cplusplus
 extern "C" {
 #endif
-/***使用驱动前请根据实际接线情况配置******/
-/**SC7A20的SDO 脚接地:  0****************/
-/**SC7A20的SDO 脚接电源:1****************/
+/***使锟斤拷锟斤拷锟斤拷前锟斤拷锟斤拷锟绞碉拷式锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷******/
+/**SC7A20锟斤拷SDO 锟脚接地o拷  0****************/
+/**SC7A20锟斤拷SDO 锟脚接碉拷源锟斤拷1****************/
 #define SC7A20_SDO_VDD_GND           						0
 /*****************************************/
 
-/***使用驱动前请根据实际IIC情况进行配置***/
-/**SC7A20的IIC 接口地址类型 7bits:  0****/
-/**SC7A20的IIC 接口地址类型 8bits:  1****/
+/***使锟斤拷锟斤拷锟斤拷前锟斤拷锟斤拷锟绞碉拷锟絀IC锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟�***/
+/**SC7A20锟斤拷IIC 锟接口碉拷址锟斤拷锟斤拷 7bits锟斤拷  0****/
+/**SC7A20锟斤拷IIC 锟接口碉拷址锟斤拷锟斤拷 8bits锟斤拷  1****/
 #define SC7A20_IIC_7BITS_8BITS       						0
 /*****************************************/
 
-#define  SL_SC7A20_16BIT_8BIT         						0
-/**SC7A20的数据位数选择  16bits:    1****/
-/**SC7A20的数据位数选择   8bits:    0****/
+#define  SL_SC7A20_16BIT_8BIT         						1
+/**SC7A20锟斤拷锟斤拷锟斤拷位锟斤拷选锟斤拷  16bits锟斤拷    1****/
+/**SC7A20锟斤拷锟斤拷锟斤拷位锟斤拷选锟斤拷   8bits锟斤拷    0****/
 /*****************************************/
-#define SL_6D4D2D1D_SEL    							      1
+#define SL_6D4D2D1D_SEL    							      6
 
 #if SC7A20_SDO_VDD_GND==0
 #define SC7A20_IIC_7BITS_ADDR        						0x18
@@ -80,11 +80,11 @@ extern "C" {
 #define  SL_SC7A20_ACT_THS        						(UINT8)(0x3E)
 #define  SL_SC7A20_ACT_DURATION 						(UINT8)(0x3F)
 	
-/*连续读取数据时的数据寄存器地址*/
-#define  SL_SC7A20_DATA_OUT       						(UINT8)(SL_SC7A20_OUT_X_L|0x80)
+/*锟斤拷锟斤拷锟斤拷取锟斤拷锟斤拷时锟斤拷锟斤拷锟捷寄达拷锟斤拷锟斤拷址*/
+#define  SL_SC7A20_DATA_OUT       						(UINT8)(SL_SC7A20_OUT_X_L)
 
-/**********特殊功能寄存器**********/
-/*非原厂技术人员请勿修改*/
+/**********锟斤拷锟解功锟杰寄达拷锟斤拷**********/
+/*锟斤拷原锟斤拷锟斤拷锟斤拷锟斤拷员锟斤拷锟斤拷锟睫革拷*/
 #define  SL_SC7A20_MTP_ENABLE    	             			(0x00)
 #define  SL_SC7A20_MTP_CFG    	  						(UINT8)(0x1E)
 #define  SL_SC7A20_MTP_VALUE   	  						(UINT8)(0x05)
@@ -93,10 +93,10 @@ extern "C" {
 #define  SL_SC7A20_I2C_PU_MSK     						(UINT8)(0x04)
 #define  SL_SC7A20_HR_ENABLE      						(UINT8)(0X08)
 #define  SL_SC7A20_BOOT_ENABLE   						(UINT8)(0X80)   
-/*非原厂技术人员请勿修改*/
+/*锟斤拷原锟斤拷锟斤拷锟斤拷锟斤拷员锟斤拷锟斤拷锟睫革拷*/
 
 
-/***************数据更新速率**加速度计使能**********/
+/***************锟斤拷锟捷革拷锟斤拷锟斤拷锟斤拷**锟斤拷锟劫度硷拷使锟斤拷**********/
 #define  SL_SC7A20_ODR_POWER_DOWN 					(UINT8)(0x00)
 #define  SL_SC7A20_ODR_1HZ        						(UINT8)(0x17)
 #define  SL_SC7A20_ODR_10HZ       						(UINT8)(0x27)
@@ -116,18 +116,18 @@ extern "C" {
 #define  SL_SC7A20_LOWER_POWER_ODR_100HZ      		(UINT8)(0x5F)
 #define  SL_SC7A20_LOWER_POWER_ODR_200HZ     			(UINT8)(0x6F)
 #define  SL_SC7A20_LOWER_POWER_ODR_400HZ      		(UINT8)(0x7F)
-/***************数据更新速率**加速度计使能**********/
+/***************锟斤拷锟捷革拷锟斤拷锟斤拷锟斤拷**锟斤拷锟劫度硷拷使锟斤拷**********/
 
 
-/***************传感器量程设置**********************/
+/***************锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷**********************/
 #define  SL_SC7A20_FS_2G        							(UINT8)(0x00)	
 #define  SL_SC7A20_FS_4G        							(UINT8)(0x10)
 #define  SL_SC7A20_FS_8G       							(UINT8)(0x20)
 #define  SL_SC7A20_FS_16G         							(UINT8)(0x30)
-/***************传感器量程设置**********************/
+/***************锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷**********************/
 
 
-/***取值在0-127之间,此处仅举例****/
+/***取值锟斤拷0-127之锟戒,锟剿达拷锟斤拷锟斤拷锟斤拷****/
 #define SL_SC7A20_INT_THS_5PERCENT  					 (UINT8)(0x06)
 #define SL_SC7A20_INT_THS_10PERCENT					 (UINT8)(0x0C)
 #define SL_SC7A20_INT_THS_20PERCENT 					 (UINT8)(0x18)
@@ -135,17 +135,17 @@ extern "C" {
 #define SL_SC7A20_INT_THS_80PERCENT 					 (UINT8)(0x60)
 
 
-/***取值在0-127之间,此处仅举例 乘以ODR单位时间****/
+/***取值锟斤拷0-127之锟戒,锟剿达拷锟斤拷锟斤拷锟斤拷 锟斤拷锟斤拷ODR锟斤拷位时锟斤拷****/
 #define SL_SC7A20_INT_DURATION_2CLK 					 (UINT8)(0x02)
 #define SL_SC7A20_INT_DURATION_5CLK 					 (UINT8)(0x05)
 #define SL_SC7A20_INT_DURATION_10CLK					 (UINT8)(0x0A)
 #define SL_SC7A20_INT_DURATION_30CLK					 (UINT8)(0x2E)
 
-/***中断有效时的电平设置,高电平相当于上升沿,低电平相当于下降沿****/
-#define SL_SC7A20_INT_ACTIVE_LOWER_LEVEL				 (UINT8)(0x02) //0x02:中断时INT1脚输出 低电
-#define SL_SC7A20_INT_ACTIVE_HIGH_LEVEL				 (UINT8)(0x00) //0x00:中断时INT1脚输出 高电
+/***锟叫讹拷锟斤拷效时锟侥碉拷平锟斤拷锟矫o拷锟竭碉拷平锟洁当锟斤拷锟斤拷锟斤拷锟截o拷锟酵碉拷平锟洁当锟斤拷锟铰斤拷锟斤拷****/
+#define SL_SC7A20_INT_ACTIVE_LOWER_LEVEL				 (UINT8)(0x02) //0x02:锟叫讹拷时INT1锟斤拷锟斤拷锟� 锟酵碉拷
+#define SL_SC7A20_INT_ACTIVE_HIGH_LEVEL				 (UINT8)(0x00) //0x00:锟叫讹拷时INT1锟斤拷锟斤拷锟� 锟竭碉拷
 
-/***中断有效时的电平设置,高电平相当于上升沿,低电平相当于下降沿****/
+/***锟叫讹拷锟斤拷效时锟侥碉拷平锟斤拷锟矫o拷锟竭碉拷平锟洁当锟斤拷锟斤拷锟斤拷锟截o拷锟酵碉拷平锟洁当锟斤拷锟铰斤拷锟斤拷****/
 #define SL_SC7A20_INT_AOI1_INT1         					 (UINT8)(0x40) //AOI1 TO INT1
 #define SL_SC7A20_INT_AOI2_INT1          					 (UINT8)(0x20) //AOI2 TO INT1
 

+ 234 - 0
src/CANEncode.c

@@ -0,0 +1,234 @@
+#include  "CANEncode.h"
+#include  "Signal.h"
+#include  "mainTask.h"
+void CANEncodeFunction(UINT32 ID,UINT8* rawData);
+
+
+void CANEncodeFunction(UINT32 ID,UINT8* rawData)
+{
+	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;
+	}
+}

+ 131 - 16
src/CANTask.c

@@ -22,11 +22,11 @@
 #include "app.h"
 #include "CANTask.h"
 #include "UDSService.h"
+#include "signal.h"
 
-extern osMutexId_t UartMutex;
 extern UINT32 Timer_count;
-extern volatile bool Sleep_flag;
-extern UartReadMsgType UartReadMsg;
+extern volatile BOOL Sleep_flag;
+
 CAN_Msg_Type CanRxMsg[2];
 /*线程声明*/
 #define 			PROC_CAN_RX_TASK_STACK_SIZE			(1024)
@@ -67,9 +67,8 @@ void CANTaskInit(void* arg)
 static void Can_Receive()
 {    
 	slpManSlpState_t State;
-    uint8_t cnt,temp,udsFlag=0;
-	UINT8 i = 0;
-	CAN_Msg_Type CANSendBuffer ={0};
+    uint8_t cnt,temp,udsFlag,msgFlag=0;
+	UINT8 i,j = 0;
     while(true)
     {
 		memset(CanRxMsg, 0, sizeof(CanRxMsg));    		
@@ -93,14 +92,6 @@ static void Can_Receive()
 					{								
 						if(CanRxMsg[i].Id == 0x7A0)
 						{	
-							#ifdef USING_PRINTF1
-								printf("ID = 0x%x\n",CanRxMsg[i].Id);
-								for(UINT8 j = 0; j<8;j++)
-								{
-									printf("%x ",CanRxMsg[i].Data[j]);
-								}
-								printf("\n");
-							#endif
 							udsFlag = 1;
 							UDSService[i] =  CanRxMsg[i].Data[0];
 							UDSSubService[i] = CanRxMsg[i].Data[1];
@@ -113,12 +104,28 @@ static void Can_Receive()
 								}								
 							}
 						}
+						else if(CanRxMsg[i].Id == 0x7A1)
+						{
+							if(CanRxMsg[i].Data[0] != 0x00)
+							{
+								msgFlag = 1;	
+							}
+							else
+							{
+								msgFlag = 0;
+							}
+						}
 					}	
 					if(udsFlag==1)
 					{	
 						UDS_Service();
-						udsFlag = 0;							
-					}		
+						udsFlag = 0;
+					}
+					else if(msgFlag == 1)
+					{
+						CanMsgTx1000ms();
+						
+					}
 					break;
 				}
 	        case PROCESS_CAN_STATE_SLEEP:
@@ -139,3 +146,111 @@ static void Can_Receive()
 }
 
 
+void CanMsgTx1000ms()
+{
+	
+	static UINT32 timerRecord = 0;
+	UINT8 count = 0;
+	CAN_Msg_Type canMsg;
+	
+	if(Timer_count - timerRecord > 0)
+	{
+		timerRecord = Timer_count;
+		count = Timer_count%10;
+		switch (count)
+		{
+			case 0:  //send cell batt info 0x6A0、0x6A1	
+				canMsg.DLC = 8;
+				canMsg.Id = COMOutTable[count*2];
+				CANEncodeFunction( COMOutTable[count*2],  canMsg.Data);
+				HAL_Can_Transmit(canMsg);
+
+				canMsg.DLC = 8;
+				canMsg.Id = COMOutTable[count*2+1];
+				CANEncodeFunction( COMOutTable[count*2+1], canMsg.Data);
+				HAL_Can_Transmit(canMsg);
+				
+				break;
+
+			case 1:  //send cell batt info 0x6A2、0x6A3
+				canMsg.DLC = 8;
+				canMsg.Id = COMOutTable[count*2];
+				CANEncodeFunction( COMOutTable[count*2], canMsg.Data);
+				HAL_Can_Transmit(canMsg);
+
+				canMsg.DLC = 8;
+				canMsg.Id = COMOutTable[count*2+1];
+				CANEncodeFunction( COMOutTable[count*2+1], canMsg.Data);
+				HAL_Can_Transmit(canMsg);
+				break;
+
+			case 2: //send cell batt info 0x6A4、 0x6A5
+				canMsg.DLC = 8;
+				canMsg.Id = COMOutTable[count*2];
+				CANEncodeFunction( COMOutTable[count*2], canMsg.Data);
+				HAL_Can_Transmit(canMsg);
+
+				canMsg.DLC = 8;
+				canMsg.Id = COMOutTable[count*2+1];
+				CANEncodeFunction( COMOutTable[count*2+1], canMsg.Data);
+				HAL_Can_Transmit(canMsg);
+				break;
+			
+			case 3:	//send cell batt info 0x6A6、0x6B0
+				canMsg.DLC = 8;
+				canMsg.Id = COMOutTable[count*2];
+				CANEncodeFunction( COMOutTable[count*2], canMsg.Data);
+				HAL_Can_Transmit(canMsg);
+
+				canMsg.DLC = 8;
+				canMsg.Id = COMOutTable[count*2+1];
+				CANEncodeFunction( COMOutTable[count*2+1], canMsg.Data);
+				HAL_Can_Transmit(canMsg);
+				
+				break;
+			
+			case 4: //send batt temp info 0x6BA、0x6C0
+				canMsg.DLC = 8;
+				canMsg.Id = COMOutTable[count*2];
+				CANEncodeFunction( COMOutTable[count*2], canMsg.Data);
+				HAL_Can_Transmit(canMsg);
+
+				canMsg.DLC = 8;
+				canMsg.Id = COMOutTable[count*2+1];
+				CANEncodeFunction( COMOutTable[count*2+1], canMsg.Data);
+				HAL_Can_Transmit(canMsg);
+			break;
+			
+			case 5: //send batt other info 0x6C1、0x6C2
+				canMsg.DLC = 8;
+				canMsg.Id = COMOutTable[count*2];
+				CANEncodeFunction( COMOutTable[count*2],  canMsg.Data);
+				HAL_Can_Transmit(canMsg);
+
+				canMsg.DLC = 8;
+				canMsg.Id = COMOutTable[count*2+1];
+				CANEncodeFunction( COMOutTable[count*2+1], canMsg.Data);
+				HAL_Can_Transmit(canMsg);
+			break;
+
+			case 6:	//send batt other info 0x6C3、0x6C4
+				canMsg.DLC = 8;
+				canMsg.Id = COMOutTable[count*2];
+				CANEncodeFunction( COMOutTable[count*2],  canMsg.Data);
+				HAL_Can_Transmit(canMsg);
+
+				canMsg.DLC = 8;
+				canMsg.Id = COMOutTable[count*2+1];
+				CANEncodeFunction( COMOutTable[count*2+1], canMsg.Data);
+				HAL_Can_Transmit(canMsg);
+			break;
+			
+			default:
+			break;
+			
+		}
+	}
+}
+
+
+

+ 1 - 1
src/GpsTask.c

@@ -30,7 +30,7 @@
 #include "GpsTask.h"
 #include "TcpTask.h"
 //全局变量区输入
-extern volatile bool Sleep_flag; 
+extern volatile BOOL Sleep_flag; 
 //全局变量区输出
 UINT8 GpsData[16];
 osMutexId_t GpsMutex = NULL;

+ 26 - 12
src/MainTask.c

@@ -31,13 +31,17 @@
 #include "TcpTask.h"
 #include "Fota.h"
 #include "UartTask.h"
+#include "sensor.h"
+
+#define EOLSTATE (0) //1标识下线检测跳过,使用默认值
+
 extern UINT8 	UDSSwitch;
 extern volatile bool NB_Fota_update_flag;
 extern volatile bool BMS_Fota_update_flag;
 CHAR defaultBattSN[BATT_SN_LEN] = "GYTEST00000000003";//未进行下线检测的默认SN
 //全局变量
 UINT32 Timer_count;//每100ms加1
-volatile bool Sleep_flag = false;//睡眠标志位
+volatile BOOL Sleep_flag = false;//睡眠标志位
 extern UINT32 TcpService;
 extern UINT8 BattChrgEndFlag;
 AppConfigHeader   AppConfigHr;   //4 bytes
@@ -55,6 +59,9 @@ volatile bool Work_timer_end = false;
 static process_Main 		    gProcess_Main_Task;
 #define PROC_MAIN_STATE_SWITCH(a)  (gProcess_Main_Task = a)
 
+extern void GsensorInit(void);
+extern void GsensorI2CHandler(ARM_I2C_SignalEvent_t cb_event);
+extern void GsensorI2CCallback(UINT32 event);
 //函数声明区
 static void appBeforeHib(void *pdata, slpManLpState state);
 static void appAfterHib(void *pdata, slpManLpState state);
@@ -76,6 +83,12 @@ static void appSaveDataInfo(void);
 //主线程任务区
 static void MainTask(void* arg)
 {
+
+    GsensorI2CHandler(GsensorI2CCallback);
+    GsensorInit();
+
+
+
     int32_t inParam = 0xAABBCCDD;
     UINT32  param;
     slpManSetPmuSleepMode(true,SLP_HIB_STATE,false);
@@ -146,11 +159,11 @@ static void MainTask(void* arg)
 	AdcSendReq(ADC_REQ_BITMAP_INRES_CALI,&param,1,ADC_GET_RESULT_TIMOUT);
 
 	
-	// NetSocDisplay(LED_SOC_0,LED_TURN_OFF);
-	// NetSocDisplay(LED_SOC_1,LED_TURN_OFF);
-	// NetSocDisplay(LED_SOC_2,LED_TURN_OFF);
-	// NetSocDisplay(LED_SOC_3,LED_TURN_OFF);
-	// FaultDisplay(LED_TURN_OFF);
+	NetSocDisplay(LED_SOC_0,LED_TURN_OFF);
+	NetSocDisplay(LED_SOC_1,LED_TURN_OFF);
+	NetSocDisplay(LED_SOC_2,LED_TURN_OFF);
+	NetSocDisplay(LED_SOC_3,LED_TURN_OFF);
+	FaultDisplay(LED_TURN_OFF);
     PROC_MAIN_STATE_SWITCH(PROCESS_STATE_IDLE);
     while(true)
     {
@@ -199,13 +212,13 @@ static void MainTask(void* arg)
                 {
                     appSaveDataInfo();
                 }
-                osDelay(5000);
                 if(NB_Fota_update_flag)
                 {
                     appSetCFUN(0);
                     osDelay(1000);
                     EC_SystemReset();
                 }
+                osDelay(1*1000);
                 xTimerStop(work_timer, 0);
                 slpManSlpState_t State;
                 uint8_t cnt;
@@ -231,7 +244,7 @@ static void MainTask(void* arg)
                     printf("which slpstate can go now :%d \n",slpstate);
                 #endif
                 slpManDeepSlpTimerStart(deepslpTimerID, AppNVMData.sleepTime*1000*60);
-                UINT8 sleeptime_count;
+                UINT8 sleeptime_count=0;
                 while(1)
                 {
                     if(!slpManDeepSlpTimerIsRunning(deepslpTimerID))
@@ -239,11 +252,11 @@ static void MainTask(void* arg)
 				    slpManDeepSlpTimerStart(deepslpTimerID, AppNVMData.sleepTime*1000*60);
                     osDelay(1*60*1000/portTICK_PERIOD_MS);
                     sleeptime_count++;
+                    #ifdef USING_PRINTF
+                        printf("sleeptime_count :%d \n",sleeptime_count);
+                    #endif
                     if(sleeptime_count>10)
                     {
-                        #ifdef USING_PRINTF
-                            printf("sleeptime_count :%d \n",sleeptime_count);
-                        #endif
                         EC_SystemReset();
                     }
                 }
@@ -614,7 +627,7 @@ static void setDefaultAppDataValue(void)
     AppNVMData.BLSwVersion = BLSWVERSION;
     AppNVMData.DrvSwVersion = DRVSWVERSION;
     AppNVMData.AppSwVersion = APPSWVERSION;
-    AppNVMData.EOLState = 0; //未进行下线配置,北京电池测试默认进行下线测试
+    AppNVMData.EOLState = EOLSTATE; //未进行下线配置,北京电池测试默认进行下线测试
     return;
 }
 
@@ -738,6 +751,7 @@ static void setDefaultAppDataInfo(void)
     AppDataInfo.BmsDisChrgInfoSendFreq = 30;
     AppDataInfo.GpsChrgInfoSendFreq = 30;
     AppDataInfo.GpsDisChrgInfoSendFreq = 5;
+    AppDataInfo.BattCurrentNegFlag = 1;
     AppDataInfo.CumulativeCapacity = 0;
     return;
 }

+ 84 - 0
src/Signal.c

@@ -0,0 +1,84 @@
+#include "Signal.h"
+
+UINT16	COMOutTable[28]	=	{
+0x6A0,
+0x6A1,
+0x6A2,
+0x6A3,
+0x6A4,
+0x6A5,
+0x6A6,
+0x6B0,
+0x6BA,
+0x6C0,
+0x6C1,
+0x6C2,
+0x6C3,
+0x6C4,
+
+0x7C0,
+0x7C1,
+0x7C2,
+0x7C3,
+0x7C4,
+0x7C5,
+0x7C6,
+0x7C7,
+0x7C8,
+0x7C9,
+0x7CA,
+0x7CB,
+0x7CC,
+0x7CD
+
+		};
+
+//declear the UINT8 vars
+UINT8	battCellTemp[8] = {0xFF};
+UINT8	MOSTemp = 0xFF;
+UINT8	packTemp = 0xFF;
+UINT8	fastChargeTemp = 0xFF;
+UINT8	normalChargeTemp = 0xFF;
+UINT8	heatTemp1 = 0xFF;
+UINT8	heatTemp2 = 0xFF;
+UINT8	nbReservedTemp1 = 0xFF;
+UINT8	nbReservedTemp2 = 0xFF;
+UINT8	maxCellTemp = 0xFF;
+UINT8	minCellTemp = 0xFF;
+UINT8	battSOC = 0xFF;
+UINT8	battSOH = 0xFF;
+UINT8	battWorkState = 0xFF;
+UINT8	bmsSwVersion = 0xFF;
+UINT8	bmsHwVersion = 0xFF;
+UINT8	battMOSSwitchState = 0xFF;
+UINT8	battHeatEnableState = 0xFF;
+UINT8	reservedSignal1 = 0xFF;
+UINT8	reservedSignal2 = 0xFF;
+UINT8	reservedSignal3 = 0xFF;
+UINT8	reservedSignal4 = 0xFF;
+UINT8	reservedSignal5 = 0xFF;
+UINT8	reservedSignal6 = 0xFF;
+UINT8	reservedSignal7 = 0xFF;
+UINT8	reservedSignal8 = 0xFF;
+
+//declear the UINT16 vars
+UINT16	battCellU[28] = {0xFFFF};
+UINT16	minCellVol = 0xFFFF;
+UINT16	maxCellVol = 0xFFFF;
+UINT16	battPackVol = 0xFFFF;
+UINT16	battI = 0xFFFF;
+UINT16	nbHwVersion = 0xFFFF;
+
+
+
+
+//declear the UINT32 vars
+UINT32	battWarningState = 0xFFFFFFFF;
+UINT32	nbSwVersion = 0xFFFFFFFF;
+UINT32	battBalanceoInfo = 0xFFFFFFFF;
+
+//Tcp
+UINT8 	TcpSendLen = 0;
+
+
+

+ 94 - 85
src/TcpTask.c

@@ -30,18 +30,21 @@
 #include "UartTask.h"
 #include "GpsTask.h"
 #include "Fota.h"
+#include "Signal.h"
 //全局变量输出区
 UINT32 TcpService = 0;
 //全局变量输入区
 extern UINT32 Timer_count;
-extern volatile bool Sleep_flag;
-extern UartReadMsgType UartReadMsg;
+extern volatile BOOL Sleep_flag;
+//extern UartReadMsgType UartReadMsg;
 extern osMutexId_t UartMutex;
 extern osMutexId_t GpsMutex;
 extern UINT8 GpsData[16];
 extern AppNVMDataType AppNVMData;
 extern AppDataBody AppDataInfo;
 extern QueueHandle_t UartWriteCmdHandle;
+extern BOOL UartBattInfoRecvFlag;
+
 
 //局部变量申请
 AppSocketConnectionContext socContext = {-1, APP_SOCKET_CONNECTION_CLOSED};
@@ -192,7 +195,7 @@ static void TcpTask(void* arg)
                     TcpTaskDeInit(arg);
                     break;
             }
-            free(queueItem);
+        free(queueItem);
         }
     }//while 循环
 }
@@ -226,28 +229,28 @@ static void TcpDataInfoAssembleSend()
     UINT8 csq=0;
     INT8 snr=0;
     INT8 rsnr=0;
-    INT16 Batt_current;
-    UINT16 BattU;
+    INT16 Batt_current=0;
+    UINT16 BattU=0;
     UINT8 temp=0;
 
-    UINT16 DataLen;
-    UINT8 BattSendFreq;
-    UINT8 GpsSendFreq;
-    UINT8 WorkState;
+    UINT16 DataLen=0;
+    UINT8 BattSendFreq=0;
+    UINT8 GpsSendFreq=0;
+    UINT8 WorkState=0;
     static UINT8 WorkFlag;
-    osMutexAcquire(UartMutex, osWaitForever);
-    WorkState = (UartReadMsg.data[(0x03+BATT_CELL_VOL_NUM)*2+1]&0x03);
-    osMutexRelease(UartMutex); 
+	int i = 0;
+	INT8 len = -1;
+    WorkState = battWorkState;
     if(WorkState==0x00 && WorkFlag==0x01)
     {
-        if(send_counter%20==0)
+        if(send_counter%RUN_END_DELAY==0)
         {
             WorkFlag=0x00;
         }
     }
     else if(WorkState==0x00 && WorkFlag==0x02)
     {
-        if(send_counter%60==0)
+        if(send_counter%CHRG_END_DELAY==0)
         {
             WorkFlag=0x00;
         }
@@ -275,12 +278,12 @@ static void TcpDataInfoAssembleSend()
             GpsSendFreq = AppDataInfo.GpsChrgInfoSendFreq/5;
             break;
     }    
-    #ifdef USING_PRINTF
+    #ifdef USING_PRINTF1
             printf("send_counter= %x,BattSendFreq= %x,GpsSendFreq= %x,UartReadMsg:%x\n",send_counter,BattSendFreq,GpsSendFreq,UartReadMsg.data[(0x03+BATT_CELL_VOL_NUM)*2+1]);
     #endif
     if(send_counter%BattSendFreq==0)
     {
-        osStatus_t result = osMutexAcquire(UartMutex, osWaitForever);
+        //osStatus_t result = osMutexAcquire(UartMutex, osWaitForever);
         appGetSystemTimeUtcSync(&TimeStracture);
         appGetSignalInfoSync(&csq,&snr,&rsnr);
         DataLen= (UINT16)sizeof(BattToTcpInfo.battInfo);
@@ -310,65 +313,54 @@ static void TcpDataInfoAssembleSend()
         BattToTcpInfo.battInfo.errClass = 0x00;
         BattToTcpInfo.battInfo.errCode[0] = 0x00;
 	    BattToTcpInfo.battInfo.errCode[1] = 0x00;
-        Batt_current = (UartReadMsg.data[(0x02+BATT_CELL_VOL_NUM)*2]<<8)|(UartReadMsg.data[(0x02+BATT_CELL_VOL_NUM)*2+1]);
-        if (Batt_current>0x8000)//放电为正,充电为负
-        {
-            Batt_current = Batt_current|0x7fff;
-            Batt_current = Batt_current/10;
-            Batt_current = 0x2710 + Batt_current;
-            Batt_current = Batt_current;
-        }
-        else
-        {
-            Batt_current = Batt_current/10;
-            Batt_current = 0x2710 - Batt_current;
-            Batt_current = Batt_current;
-        }
-        BattToTcpInfo.battInfo.battI[0] = Batt_current>>8;
-	    BattToTcpInfo.battInfo.battI[1] = Batt_current & 0xFF;
-        BattU =( (UartReadMsg.data[(0x18+BATT_CELL_VOL_NUM+BATT_TEMP_NUM+BATT_OTHER_TEMP_NUM)*2])<<8|(UartReadMsg.data[(0x18+BATT_CELL_VOL_NUM+BATT_TEMP_NUM+BATT_OTHER_TEMP_NUM)*2+1]))/10;
-        //电池内外电压保持一致
-        BattToTcpInfo.battInfo.battLinkVol[0] = BattU >> 8;
-	    BattToTcpInfo.battInfo.battLinkVol[1] = BattU & 0xFF;
-	    BattToTcpInfo.battInfo.battPackVol[0] = BattU >> 8;
-	    BattToTcpInfo.battInfo.battPackVol[1] = BattU & 0xFF;
-        temp = ((UartReadMsg.data[(0x09+BATT_CELL_VOL_NUM+BATT_TEMP_NUM+BATT_OTHER_TEMP_NUM)*2+1])>>1)&0x03;
-        if(AppNVMData.isBattLocked==TRUE)
-        {
-            BattToTcpInfo.battInfo.switchState = ((temp&0x01)<<1)|((temp&0x02)>>1)|0x01<<2;
-        }
-        else
-        {
-            BattToTcpInfo.battInfo.switchState = ((temp&0x01)<<1)|((temp&0x02)>>1)|0x00<<2;
-        }
-        BattToTcpInfo.battInfo.battSOC = UartReadMsg.data[(0x0B+BATT_CELL_VOL_NUM+BATT_TEMP_NUM+BATT_OTHER_TEMP_NUM)*2+1];
-        BattToTcpInfo.battInfo.battSOH = UartReadMsg.data[(0x0C+BATT_CELL_VOL_NUM+BATT_TEMP_NUM+BATT_OTHER_TEMP_NUM)*2+1];
-        memcpy(BattToTcpInfo.battInfo.batCellBalenceState,&UartReadMsg.data[(0x06+BATT_CELL_VOL_NUM+BATT_TEMP_NUM+BATT_OTHER_TEMP_NUM)*2],4);
-        BattToTcpInfo.battInfo.battCellNum= BATT_CELL_VOL_NUM ;
-        memcpy(BattToTcpInfo.battInfo.battCellU,&UartReadMsg.data[0x04],BATT_CELL_VOL_NUM_2);
-        BattToTcpInfo.battInfo.battTempNum = BATT_TEMP_NUM;
-        for(int i=0; i<BATT_TEMP_NUM; i++)
-	    {
-		    BattToTcpInfo.battInfo.battCellTemp[i] = UartReadMsg.data[(0x06+BATT_CELL_VOL_NUM+i)*2+1];
-	    }
-        BattToTcpInfo.battInfo.battWorkState =UartReadMsg.data[(0x03+BATT_CELL_VOL_NUM)*2+1]&0x03;//电池状态,0表示静置,1表示放电,2表示充电
-        BattToTcpInfo.battInfo.battHeatState = UartReadMsg.data[(0x1C+BATT_CELL_VOL_NUM+BATT_TEMP_NUM+BATT_OTHER_TEMP_NUM)*2+1];
-        BattToTcpInfo.battInfo.battotherTempNum = BATT_OTHER_TEMP_NUM;//其他温度编号(包含环境温度mos温度以及接插件温度)2021-03-24其他温度修改
-        BattToTcpInfo.battInfo.battotherTemp[0] = UartReadMsg.data[(0x06+BATT_CELL_VOL_NUM+BATT_TEMP_NUM)*2+1];
-        BattToTcpInfo.battInfo.battotherTemp[1] = UartReadMsg.data[(0x06+BATT_CELL_VOL_NUM+BATT_TEMP_NUM+1)*2+1];
+		
+		BattToTcpInfo.battInfo.battI[0] = battI>>8;
+		BattToTcpInfo.battInfo.battI[1] = battI & 0xFF;
+		
+		//电池内外电压保持一致
+		BattToTcpInfo.battInfo.battLinkVol[0] = battPackVol >> 8;
+		BattToTcpInfo.battInfo.battLinkVol[1] = battPackVol & 0xFF;
+		BattToTcpInfo.battInfo.battPackVol[0] = battPackVol >> 8;
+		BattToTcpInfo.battInfo.battPackVol[1] = battPackVol & 0xFF;
+		
+		BattToTcpInfo.battInfo.switchState = battMOSSwitchState;
+		
+		BattToTcpInfo.battInfo.battSOC = battSOC;
+		BattToTcpInfo.battInfo.battSOH = battSOH;
+		memcpy(BattToTcpInfo.battInfo.batCellBalenceState,&battBalanceoInfo,4);
+		BattToTcpInfo.battInfo.battCellNum= BATT_CELL_VOL_NUM ;
+
+		for(i=0;i<BATT_CELL_VOL_NUM;i++)
+		{
+			BattToTcpInfo.battInfo.battCellU[i*2] = (battCellU[i]>>8) & 0xFF;
+			BattToTcpInfo.battInfo.battCellU[i*2+1] = battCellU[i] & 0xFF;
+		}
+		
+		BattToTcpInfo.battInfo.battTempNum = BATT_TEMP_NUM;
+		for(i=0; i<BATT_TEMP_NUM; i++)
+		{
+			BattToTcpInfo.battInfo.battCellTemp[i] = battCellTemp[i];
+		}
+		BattToTcpInfo.battInfo.battWorkState = battWorkState;//电池状态,0表示静置,1表示放电,2表示充电
+		BattToTcpInfo.battInfo.battHeatEnableState = battHeatEnableState;
+		BattToTcpInfo.battInfo.battotherTempNum = BATT_OTHER_TEMP_NUM;//其他温度编号(包含环境温度mos温度以及接插件温度)2021-03-24其他温度修改
+		BattToTcpInfo.battInfo.battotherTemp[0] = MOSTemp;
+		BattToTcpInfo.battInfo.battotherTemp[1] = packTemp;       
+        
         BattToTcpInfo.CRC = bcc_chk((UINT8 *)&BattToTcpInfo, sizeof(BattMsgtoTcpType)-1 );
-        osMutexRelease(UartMutex);
-        UINT8 len;
-        if(UartReadMsg.UartFlag)
+        //osMutexRelease(UartMutex);
+        if(UartBattInfoRecvFlag)
         {
             len = tcpipConnectionSend(socContext.id, (UINT8 *)&BattToTcpInfo, sizeof(BattToTcpInfo), 0, 0, 0);
+            if(len>0)
+            {
+                TcpSendLen = 0x02 | TcpSendLen;
+            }
+            else
+            {
+                TcpSendLen = 0xFD & TcpSendLen;
+            }
         }
-        #ifdef USING_PRINTF1
-            printf("Len = %d\n",len);
-            for(int i=0;i<sizeof(BattToTcpInfo);i++)
-                printf("%x  ",*((UINT8 *)&BattToTcpInfo+i));
-            printf("end V4! \n",len);
-        #endif
     }
     if(send_counter%GpsSendFreq==0)
     {
@@ -401,7 +393,15 @@ static void TcpDataInfoAssembleSend()
         osMutexRelease(GpsMutex);
         if(GpsToTcpInfo.gpsInfo.locateMark==0x01)
         {
-            tcpipConnectionSend(socContext.id, (UINT8 *)&GpsToTcpInfo, sizeof(GpsToTcpInfo), 0, 0, 0);
+            len = tcpipConnectionSend(socContext.id, (UINT8 *)&GpsToTcpInfo, sizeof(GpsToTcpInfo), 0, 0, 0);
+            if(len>0)
+            {
+                TcpSendLen = 0x04 | TcpSendLen;
+            }
+            else
+            {
+                TcpSendLen = 0xFB & TcpSendLen;
+            }
         }
     }
     if(send_counter==0)
@@ -435,14 +435,15 @@ static void TcpDataInfoAssembleSend()
         VerMsgToTcpInfo.VerInfo.msgCollectionTimeUTC[5] = ((TimeStracture.UTCtimer2) >> 8 ) & 0xFF;
         memcpy(VerMsgToTcpInfo.VerInfo.ICCID,iccid,20);
         memcpy(VerMsgToTcpInfo.VerInfo.IMEI,imei,15);
-        osMutexAcquire(UartMutex, osWaitForever);//Bms版本号上传2021-03-24修改
-        VerMsgToTcpInfo.VerInfo.BMSHwVersion[0] = UartReadMsg.data[(0x08+BATT_CELL_VOL_NUM+BATT_TEMP_NUM+BATT_OTHER_TEMP_NUM)*2+1]/10;
-        VerMsgToTcpInfo.VerInfo.BMSHwVersion[1] = UartReadMsg.data[(0x08+BATT_CELL_VOL_NUM+BATT_TEMP_NUM+BATT_OTHER_TEMP_NUM)*2+1]%10;
-        VerMsgToTcpInfo.VerInfo.BMSSwVersion[0] = 0;
-        VerMsgToTcpInfo.VerInfo.BMSSwVersion[1] = 0;
-        VerMsgToTcpInfo.VerInfo.BMSSwVersion[2] = UartReadMsg.data[(0x08+BATT_CELL_VOL_NUM+BATT_TEMP_NUM+BATT_OTHER_TEMP_NUM)*2]/10;
-        VerMsgToTcpInfo.VerInfo.BMSSwVersion[3] = UartReadMsg.data[(0x08+BATT_CELL_VOL_NUM+BATT_TEMP_NUM+BATT_OTHER_TEMP_NUM)*2]%10;
-        osMutexRelease(UartMutex);
+        //osMutexAcquire(UartMutex, osWaitForever);//Bms版本号上传2021-03-24修改
+        
+		VerMsgToTcpInfo.VerInfo.BMSHwVersion[0] = bmsHwVersion /10;
+		VerMsgToTcpInfo.VerInfo.BMSHwVersion[1] = bmsHwVersion%10;
+		VerMsgToTcpInfo.VerInfo.BMSSwVersion[0] = 0;
+		VerMsgToTcpInfo.VerInfo.BMSSwVersion[1] = 0;
+		VerMsgToTcpInfo.VerInfo.BMSSwVersion[2] = bmsSwVersion/10;
+		VerMsgToTcpInfo.VerInfo.BMSSwVersion[3] = bmsSwVersion%10;
+        //osMutexRelease(UartMutex);
         VerMsgToTcpInfo.VerInfo.NBHwVersion[0] = (HWVERSION>>8)& 0xFF;
         VerMsgToTcpInfo.VerInfo.NBHwVersion[1] = (HWVERSION)& 0xFF;
         VerMsgToTcpInfo.VerInfo.BLVersion[0] = (BLSWVERSION>>24)& 0xFF;
@@ -461,9 +462,17 @@ static void TcpDataInfoAssembleSend()
         VerMsgToTcpInfo.VerInfo.BmsInfo = BMS_INFO;
         VerMsgToTcpInfo.VerInfo.DataModuleType = DATA_MODULE_TYPE;
         VerMsgToTcpInfo.CRC = bcc_chk((UINT8 *)&VerMsgToTcpInfo, sizeof(VerMsgToTcpInfo)-1 );
-        if(UartReadMsg.UartFlag)
+        if(UartBattInfoRecvFlag)
         {
-            tcpipConnectionSend(socContext.id, (UINT8 *)&VerMsgToTcpInfo, sizeof(VerMsgToTcpInfo), 0, 0, 0);
+            len = tcpipConnectionSend(socContext.id, (UINT8 *)&VerMsgToTcpInfo, sizeof(VerMsgToTcpInfo), 0, 0, 0);
+            if(len>0)
+            {
+                TcpSendLen = 0x01 | TcpSendLen;
+            }
+            else
+            {
+                TcpSendLen = 0xFE & TcpSendLen;
+            }
         }
         else
         {
@@ -524,7 +533,7 @@ static void TcpDataInfoRecvHandle()
                 }
                 else if(*(Ptr+30)==0x80)//远程锁定命令
                 {
-                    osMutexAcquire(UartMutex, osWaitForever);
+                    //osMutexAcquire(UartMutex, osWaitForever);
                     TcpCmdAnswer[3] = 0x01;
                     memcpy(&TcpCmdAnswer[4],(Ptr+4),BATT_SN_LEN);
                     TcpCmdAnswer[21] = TCP_ENCPT_DISABLE;
@@ -539,7 +548,7 @@ static void TcpDataInfoRecvHandle()
                         UartWriteCmd.WriteCmd = 0x01;
                         UartWriteCmd.Data[0] = 0x00;
                         UartWriteCmd.Data[1] = 0x00;
-                        if((UartReadMsg.data[(0x03+BATT_CELL_VOL_NUM)*2+1]&0x03)==0x00)
+                        if(battWorkState ==0x00)
                         {
                             osMessageQueuePut(UartWriteCmdHandle,&UartWriteCmd,0,1000);
                         }
@@ -555,7 +564,7 @@ static void TcpDataInfoRecvHandle()
                         osMessageQueuePut(UartWriteCmdHandle,&UartWriteCmd,0,1000);
                         tcpipConnectionSend(socContext.id,TcpCmdAnswer,31,0,0,0);
                     }
-                    osMutexRelease(UartMutex);
+                    //osMutexRelease(UartMutex);
                     TcpService=0x00;
                     
                 }

+ 107 - 81
src/UDSService.c

@@ -22,15 +22,15 @@
 #include "app.h"
 #include "CANTask.h"
 #include "flash_ec616_rt.h"
-
+#include "Signal.h"
 #include "UDSService.h"
 
 
-//
-extern UartReadMsgType UartReadMsg;
-extern osMutexId_t UartMutex;
+
 extern AppSocketConnectionContext socContext;
 extern UINT8 GpsData[16];
+extern UINT8 battInfo[8*14];
+
 
 void UDS_Service(void);
 void UDS_CAN_Update(void);
@@ -74,10 +74,10 @@ void UDS_Service()
 	UINT32 tempSliceCounter=0;	
 	static UINT8 counter=0;  
 	CAN_Msg_Type UDSAnswer ={0};
-	static UINT8 battInfo[8*14] = {0};
+	
 	UINT32 posCode,negCode;
 	
-	//UartReadMsg Uart_Recv_Msg;
+	
 	//UINT8 temp00[94] ={0x00,0x0E,0x00,0x05,0x0D,0xDE,0x0D,0xDE,0x0D,0xDE,0x0D,0xDE,0x0D,0x7A,0x0D,0xDE,0x0D,0xDE,0x0D,0xDE,0x0D,0xDE,0x0D,0xDE,0x0E,0x42,0x0D,0xDE,0x0D,0xDE,0x0D,0xDE,0x13,0x38,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x21,0x00,0x24,0x00,0x26,0x00,0x22,0x00,0x24,0x00,0x00,0x00,0x00,0x6e,0x78,0x00,0x00,0x00,0x00,0x00,0x5F,0x00,0x62,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x13,0x74,0x0E,0x42,0x0D,0x7A,0x00,0x00};
 	//MEMCPY(battInfo, temp00, 94);
 	
@@ -85,8 +85,13 @@ void UDS_Service()
 	//{
 		//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])
@@ -158,62 +163,38 @@ void UDS_Service()
 										UDSPositiveAnswer(0x06,i,HWVERSION);										
 									}
 									else if(UDSSubService[i] == 0x03)  //check the SN number
-									{	
-										UDSAnswer.DLC = 8;
+									{											
 										switch(UDSSubServiceActionCode[i])
 										{
 											case 00:
-												UDSAnswer.Id = 0x20;
-												for(k=0; k<8; k++)
-												{
-													UDSAnswer.Data[k] = AppNVMData.battSN[k+8*0];
-												}
+												UDSAnswer.Id = 0x7B0;
+												
+												CANEncodeFunction(UDSAnswer.Id, UDSAnswer.Data);
 												ret = HAL_Can_Transmit(UDSAnswer);
+												
 												break;
 												
 											case 01:
-												UDSAnswer.Id = 0x21;
-												for(k=0; k<8; k++)
-												{
-													UDSAnswer.Data[k] = AppNVMData.battSN[k+8*1];
-												}
+												UDSAnswer.Id = 0x7B1;
+												CANEncodeFunction(UDSAnswer.Id, UDSAnswer.Data);
 												ret = HAL_Can_Transmit(UDSAnswer);
+
 												break;
 
 											case 02:
-												UDSAnswer.Id = 0x22;
-												UDSAnswer.Data[0] = AppNVMData.battSN[16];;
-												for(k=1; k<8; k++)
-												{
-													UDSAnswer.Data[k] = 0x00;
-												}
+												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
-									{									
-										osStatus_t result = osMutexAcquire(UartMutex, osWaitForever);
-										if(CanRxMsg[i].Data[2] == 0x00)
-										{
-											MEMCPY(battInfo,UartReadMsg.data,14*8);
-										/*
-											#ifdef USING_PRINTF
-											printf("battInfo=\n");
-											for(int jj = 0;jj<14*8;jj++)
-												printf("%x ",battInfo[jj]);
-											printf("\n");
-											#endif
-										*/
-										}
-										osMutexRelease(UartMutex);
-
-										UDSAnswer.Id = 0x30+UDSSubServiceActionCode[i];
-										UDSAnswer.DLC = 8;
-
-										MEMCPY(UDSAnswer.Data, &(battInfo[UDSSubServiceActionCode[i]*8]),8);
+									{
+										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
@@ -222,7 +203,7 @@ void UDS_Service()
 									}									
 									else if(UDSSubService[i] == 0x06)	//check the tcp link of NB
 									{
-										UDSPositiveAnswer(0x06,i,socContext.status);  //TcpconnectStatus
+										UDSPositiveAnswer(0x06,i,TcpSendLen);  //TcpconnectStatus
 									}
 									else if(UDSSubService[i] == 0x07)  //check the GPS link of NB
 									{
@@ -287,16 +268,13 @@ void UDS_Service()
 										}
 										else
 										{
-											UDSAnswer.Id = 0x7A8;
-											UDSAnswer.DLC = 8;
+											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;	
-											UDSAnswer.Data[5] = 0x00;
-											UDSAnswer.Data[6] = 0x00;
-											UDSAnswer.Data[7] = 0x00;
+											
 											ret = HAL_Can_Transmit(UDSAnswer);
 										}										
 									}
@@ -397,55 +375,88 @@ void UDS_Service()
 										#endif
 										*/											
 										}
+										else
+										{
+											counter--;
+										}
+										
 										if(counter == 25)
 										{
-											counter = 0;
-											
+											counter = 0;											
 											if(sliceCounterFlag == 0x1FFFFFF)  //received all the 25 message of current package
-											{
-												sliceCounterFlag = 0x0;
-												updateDifferDataPackageCounter++;
-											/*
-											#ifdef USING_PRINTF
-												printf("updateDifferDataPackageCounter = %d\n",updateDifferDataPackageCounter);
-
-												printf("updateDataBuffer\n");
-												for(k=0;k<100;k++)
-												{
-													printf("%x  ",updateDataBuffer[k]);
-												}
-												printf("\n");
-												
-												printf("startAddr = 0x%x,endAddr = 0x%x\n",
-													FLASH_FOTA_REGION_START+(updateDifferDataPackageCounter-1)*100,FLASH_FOTA_REGION_START+updateDifferDataPackageCounter*100-1);
-											#endif
-											*/			
+											{															
 												//write the buffer(100 byte) to flash
-												ret = (uint8_t)BSP_QSPI_Write_Safe(updateDataBuffer,FLASH_FOTA_REGION_START+(updateDifferDataPackageCounter-1)*100,100);
+												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-1)*100,100);
-													ret = (uint8_t)BSP_QSPI_Write_Safe(updateDataBuffer,FLASH_FOTA_REGION_START+(updateDifferDataPackageCounter-1)*100,100);
+													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)
+												if(ret == QSPI_OK)  // write successed
 												{
-													UDSPositiveAnswer(0x08,i,tempSliceCounter);												
+													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
+												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
+													*/
 												}
-												memset(updateDataBuffer,0, 100);
-												sliceCounterFlag = 0x0;
-												
 											}
 											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);  
+												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
@@ -465,6 +476,7 @@ void UDS_Service()
 									else
 									{
 										UDSNegtiveAnswer(0x05,i,0xEF); //the download process is not ready
+										counter = 0;
 									}
 								}
 								else
@@ -478,6 +490,20 @@ void UDS_Service()
 									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
@@ -616,7 +642,7 @@ BOOL UDSAskforDownLoadData()
 BOOL UDSClearFotaDownloadRegion()
 {
 	UINT8 ret = FALSE;
-	ret = BSP_QSPI_Erase_Safe(FLASH_FOTA_REGION_START, 0x78000); //512k-32k = 480k -> 0x75300
+	ret = BSP_QSPI_Erase_Safe(FLASH_FOTA_REGION_START, 0x46000); //512k-32k -200k = 280k -> 0x46000
 	
 	if(ret == QSPI_OK)
 	{

+ 391 - 240
src/UartTask.c

@@ -32,14 +32,14 @@
 #include "app.h"
 #include "numeric.h"
 #include "Fota.h"
-
+#include "signal.h"
 //全局变量输入区
 extern UINT32 Timer_count;
-extern volatile bool Sleep_flag; 
+extern volatile BOOL Sleep_flag; 
 extern AppNVMDataType AppNVMData;
+extern AppDataBody AppDataInfo;
 //全局变量输出区
-UartReadMsgType UartReadMsg;
-osMutexId_t UartMutex = NULL;//Uart数据锁
+BOOL UartBattInfoRecvFlag  = false;
 QueueHandle_t UartWriteCmdHandle = NULL;
 UINT8 BattChrgEndFlag;
 //
@@ -59,11 +59,14 @@ static process_Uart             gProcess_Uart_Task = PROCESS_UART_STATE_IDLE;
 //函数声明区
 void USART_callback(uint32_t event);
 UINT8 Uart_DataRecv_func(UINT8* Uart_Read_Msg,UINT8* Uart_Recv_Buffer);
+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);
@@ -93,14 +96,12 @@ static void UartTask(void* arg)
 	BOOL uartReadSuccessFlag = false;
     Uart_Read_Msg_Type Uart_Read_Msg;
     Uart_Write_Data_Type UartWriteData; //Uart控制命令
+    UartReadMsgType UartReadMsg;
+    memset(&(UartReadMsg.UartFlag),0x00,sizeof(UartReadMsgType));
     if(UartWriteCmdHandle == NULL)//Uart控制命令传输指针
 	{
 		UartWriteCmdHandle = osMessageQueueNew(3,sizeof(Uart_Write_Data_Type), NULL);
 	}
-    if(UartMutex == NULL)
-    {
-        UartMutex = osMutexNew(NULL);
-    }
     //上电起始控制区域
     while (1)
     {
@@ -144,6 +145,10 @@ static void UartTask(void* arg)
 				{
 					uartReadSuccessFlag = true;
 				}
+				else
+				{
+					uartReadSuccessFlag = false;
+				}
 				if(Timer_count-currentTimerCount >= 1)
 				{
 					if(AppNVMData.isBattLocked != 0)
@@ -155,6 +160,13 @@ static void UartTask(void* arg)
 						battSOCDisplay();
 						battErrorStateDisplay();
 					}
+					// else
+					// {
+					// 	#ifdef USING_PRINTF
+					// 		printf("battWarningStateDisplay\n");
+					// 	#endif
+					// 	battWarningStateDisplay();
+					// }
 				}
 				currentTimerCount = Timer_count;
 				if(BMS_Fota_update_flag)
@@ -167,6 +179,7 @@ static void UartTask(void* arg)
 					UartWriteData.Data[0] = 0x00;
 					UartWriteData.Data[1] = 0x00;
 					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)
 				{
@@ -174,12 +187,13 @@ static void UartTask(void* arg)
 					UartWriteData.Data[0] = 0x00;
 					UartWriteData.Data[1] = 0x03;
 					osMessageQueuePut(UartWriteCmdHandle,&UartWriteData,0,1000);
+					PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_READ);
 				}
                 break;
             }
             case PROCESS_UART_STATE_READ:
             {
-				osStatus_t result = osMutexAcquire(UartMutex, osWaitForever);
+                
                 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;
@@ -188,19 +202,21 @@ static void UartTask(void* arg)
                 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);
                 Uart_Recv_LEN = Uart_DataRecv_func((UINT8 *)&Uart_Read_Msg,UartReadMsg.Header);
-				osMutexRelease(UartMutex);
+				//osMutexRelease(UartMutex);
 				if(Uart_Recv_LEN>0)
 				{
-					UartReadMsg.UartFlag = TRUE;
+					UartBattInfoRecvFlag =  TRUE;
+					uartBattInfoDecode(UartReadMsg.data);
 				}
 				else
 				{
-					UartReadMsg.UartFlag = FALSE;
+					UartBattInfoRecvFlag =  FALSE;
 				}
                 UartReadMsg.len = Uart_Recv_LEN;
                 PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_IDLE);
-                if((UartReadMsg.data[(0x03+BATT_CELL_VOL_NUM)*2+1]&0x03)==0x02)
+                if( battWorkState ==0x02)
                 {
                     BattChrgEndFlag=TRUE;
                 }
@@ -239,9 +255,110 @@ static void UartTask(void* arg)
 				osThreadExit();
                 break;
             }
+
         }
     }
 }
+
+
+
+
+//Uart 接收的数据解码
+static BOOL uartBattInfoDecode(UINT8* dataPtr)
+{
+	//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]);
+    #ifdef USING_PRINTF
+		printf("Batt_current = %x\n",Batt_current);
+	#endif
+    //原始数据:充电为负,放电为正
+	if(battWorkState==0x02&&Batt_current>0x8000)
+	{
+		AppDataInfo.BattCurrentNegFlag = 1;
+	}
+	if(battWorkState==0x02&&Batt_current<0x8000)
+	{
+		AppDataInfo.BattCurrentNegFlag = -1;
+	}
+    if (Batt_current>0x8000) //为负,充电数据
+    {
+        Batt_current = (UINT16)((UINT16)(~(Batt_current))+1);
+        Batt_current = Batt_current/10;
+        Batt_current = -Batt_current*AppDataInfo.BattCurrentNegFlag + 0x2710;//平台数据:充电为负
+        //Batt_current = Batt_current;
+    }
+    else  //为正,数据
+    {
+        Batt_current = Batt_current/10;
+        Batt_current = Batt_current*AppDataInfo.BattCurrentNegFlag + 0x2710;//平台数据:充电为负
+        //Batt_current = Batt_current;
+    }
+    battI = Batt_current;
+	#ifdef USING_PRINTF
+		printf("battI = %x\n",battI);
+	#endif
+    //bit0 ~ bit31 represent cell0 ~ cell31
+    battBalanceoInfo  = dataPtr[(0x06+BATT_CELL_VOL_NUM+TEMP_NUM)*2+1] | (dataPtr[(0x06+BATT_CELL_VOL_NUM+TEMP_NUM)*2] <<8) + (dataPtr[(0x07+BATT_CELL_VOL_NUM+TEMP_NUM)*2+1]<<16) | (dataPtr[(0x07+BATT_CELL_VOL_NUM+TEMP_NUM)*2] <<24);
+
+    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];
+	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++)
+	{
+		maxCellTemp = max(maxCellTemp,battCellTemp[i]);
+		minCellTemp = min(minCellTemp,battCellTemp[i]);		
+	}
+	nbSwVersion = APPSWVERSION;
+	nbHwVersion = HWVERSION;
+	
+    return true;
+}
+
+
+
+
+
+
 //Uart线程初始化
 void UartTaskInit(void *arg)
 {
@@ -358,7 +475,7 @@ UINT8 Uart_WriteCmd_func(Uart_Write_Data_Type UartWriteData)
             {
             printf("%x ",Uart_Recv_Buffer[i]);
             }
-            printf("n");
+            printf("\n");
         #endif
         isRecvComplete = false;
         if(Uart_Recv_Buffer[1]==0x10)
@@ -399,9 +516,7 @@ UINT8 Uart_DataRecv_func(UINT8* Uart_Read_Msg,UINT8* Uart_Recv_Buffer)
     USARTdrv->Receive(Uart_Recv_Buffer,Data_Len);
 	while(true)
     {
-        
         timeout++;
-
         if((isRecvTimeout == true) || (isRecvComplete == true))
         {
             break;
@@ -416,30 +531,26 @@ UINT8 Uart_DataRecv_func(UINT8* Uart_Read_Msg,UINT8* Uart_Recv_Buffer)
             }
         } 
     }
-	#ifdef USING_PRINTF  
-             printf("%s[%d]\r\n",__FUNCTION__, __LINE__);
-             printf("timeout = %d\n",timeout);
-            #endif
-    #ifdef USING_PRINTF1
-        printf("Uart_Rece_buffer: ");
-        for(int i=0;i<Data_Len;i++)
-        {
-        printf("%x ",*(Uart_Recv_Buffer+i));
-        }
-    #endif
+     #ifdef USING_PRINTF
+         printf("Uart_Rece_buffer1: ");
+         for(int i=0;i<Data_Len;i++)
+         {
+         printf("%x ",*(Uart_Recv_Buffer+i));
+         }
+     #endif
     if (isRecvComplete == true)
     {
         isRecvComplete = false;
         CRC_Rece_buffer =*(Uart_Recv_Buffer+Data_Len-1)<<8|*(Uart_Recv_Buffer+Data_Len-2);
         CRC_chk_buffer = crc_chk(Uart_Recv_Buffer,Data_Len-2);
-        // #ifdef USING_PRINTF
-        //     printf("Uart_Rece_buffer after Crc: ");
-        //     for(int i=0;i<Data_Len;i++)
-        //     {
-        //     printf("%x ",*(Uart_Recv_Buffer+i));
-        //     }
-        //     printf("\tcrcchk:%x,%x\n ",CRC_chk_buffer,CRC_Rece_buffer);
-        // #endif
+	    #ifdef USING_PRINTF1
+	         printf("Uart_Rece_buffer after Crc: ");
+	         for(int i=0;i<Data_Len;i++)
+	         {
+	         	printf("%x ",*(Uart_Recv_Buffer+i));
+	   	     }
+	         printf("\tcrcchk:%x,%x\n ",CRC_chk_buffer,CRC_Rece_buffer);
+	    #endif
         if (CRC_Rece_buffer == CRC_chk_buffer)//满足校验
         {
             return Data_Len;//此处指针移位出现重启问题
@@ -479,32 +590,13 @@ BOOL BattHeaterSwitch(UINT8* heaterSwitch)
 {
 	BOOL isNeedtoSwitch = FALSE;
 	
-	UINT8 battCellTemp[BATT_TEMP_NUM];
-	UINT8 maxCellTemp,minCellTemp;
-	
 	UINT8 i =0;	
 	UINT8 currentSwitchState = 0;
-
 	//get the current switch state and the cell temp
-	currentSwitchState = UartReadMsg.data[(0x1C+BATT_CELL_VOL_NUM+(BATT_TEMP_NUM+2))*2+1]&0x01;
-	for(int i=0; i<BATT_TEMP_NUM; i++)
-    {
-	    battCellTemp[i] = UartReadMsg.data[(0x06+BATT_CELL_VOL_NUM+i)*2+1];	    
-    }
-
-        //cal the maxtemp and mintemp
-        maxCellTemp = battCellTemp[0];
-        minCellTemp = battCellTemp[0];
-    for(i=1;i<BATT_TEMP_NUM;i++)
-    {
-		maxCellTemp = max(maxCellTemp,battCellTemp[i]);
-        minCellTemp = min(minCellTemp,battCellTemp[i]);
-    }
-
-    
+	currentSwitchState = battMOSSwitchState & 0x01;
 	if(currentSwitchState==0) 	//当前状态为关闭,判断是否应该开启
 	{
-		if(minCellTemp<=5+40 && maxCellTemp<25+40)//温度偏移为40 
+		if(minCellTemp<=5+40 && maxCellTemp<25+40 && battSOC>=12)//温度偏移为40 
 		{
 			*heaterSwitch = 1;
 			isNeedtoSwitch = true;
@@ -512,7 +604,7 @@ BOOL BattHeaterSwitch(UINT8* heaterSwitch)
 	}
 	else  						//当前状态为开启,判断是否应该关闭
 	{
-		if(minCellTemp>10+40||maxCellTemp>30+40)
+		if(minCellTemp>10+40 || maxCellTemp>30+40 || battSOC<10)
 		{
 			*heaterSwitch = 0;
 			isNeedtoSwitch= true;
@@ -523,8 +615,7 @@ BOOL BattHeaterSwitch(UINT8* heaterSwitch)
 
 void battSOCDisplay()
 {
-	static UINT8 workState;
-	static UINT8 currentSoc;
+	
 	static UINT8 lightTimer = 0;
 	UINT8 socLowLEDFlashPeriod = 10;//10*100 = 1000ms
 	UINT8 chargeLEDFlashPeriod = 6;//6*100 = 600ms
@@ -535,196 +626,187 @@ void battSOCDisplay()
 	{
 		return;
 	}
-	
-	if(UartReadMsg.Header[2]>0)
-	{
-		temp = UartReadMsg.data[(0x03+BATT_CELL_VOL_NUM)*2+1]&0x03;
-	    workState = ((temp&0x01)<<01)|(temp>>0x01);
-	    currentSoc = UartReadMsg.data[(0x0B+BATT_CELL_VOL_NUM+BATT_TEMP_NUM+2)*2+1];
-	}
-	#ifdef USING_PRINTF1
-		printf("current SOC = %d\n",currentSoc);
-		printf("work state = %d\n",workState);
-	#endif
-
-	lightTimer++;
-	
-	if(workState == 0||workState == 2)  //静置或放电状态
-	{
-		if(currentSoc<=10)
+	if(UartBattInfoRecvFlag == true)
+	{ 
+		lightTimer++;
+		
+		if(battWorkState == 0||battWorkState == 1)  //静置或放电状态
 		{
-			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)
+			if(battSOC<=10)
 			{
-				NetSocDisplay(LED_SOC_0,LED_TURN_OFF);
-				NetSocDisplay(LED_SOC_1,LED_TURN_OFF);
-				NetSocDisplay(LED_SOC_2,LED_TURN_OFF);
-				NetSocDisplay(LED_SOC_3,LED_TURN_OFF);	
-			}
-			else
-			{
-				NetSocDisplay(LED_SOC_0,LED_TURN_OFF);
-				NetSocDisplay(LED_SOC_1,LED_TURN_OFF);
-				NetSocDisplay(LED_SOC_2,LED_TURN_OFF);
-				NetSocDisplay(LED_SOC_3,LED_TURN_OFF);	
-				lightTimer = 0;
-			}			
-		}	
-		else if(currentSoc>10&&currentSoc<=25)
-		{
-			NetSocDisplay(LED_SOC_0,LED_TURN_ON);
-			NetSocDisplay(LED_SOC_1,LED_TURN_OFF);
-			NetSocDisplay(LED_SOC_2,LED_TURN_OFF);
-			NetSocDisplay(LED_SOC_3,LED_TURN_OFF);	
-			lightTimer = 0;
-		}
-		else if(currentSoc>25&&currentSoc<=50)
-		{
-			NetSocDisplay(LED_SOC_0,LED_TURN_ON);
-			NetSocDisplay(LED_SOC_1,LED_TURN_ON);
-			NetSocDisplay(LED_SOC_2,LED_TURN_OFF);
-			NetSocDisplay(LED_SOC_3,LED_TURN_OFF);	
-			lightTimer = 0;
-		}
-		else if(currentSoc>50&&currentSoc<=75)
-		{
-			NetSocDisplay(LED_SOC_0,LED_TURN_ON);
-			NetSocDisplay(LED_SOC_1,LED_TURN_ON);
-			NetSocDisplay(LED_SOC_2,LED_TURN_ON);
-			NetSocDisplay(LED_SOC_3,LED_TURN_OFF);	
-			lightTimer = 0;
-		}
-		else if(currentSoc>75&&currentSoc<=100)
-		{
-			NetSocDisplay(LED_SOC_0,LED_TURN_ON);
-			NetSocDisplay(LED_SOC_1,LED_TURN_ON);
-			NetSocDisplay(LED_SOC_2,LED_TURN_ON);
-			NetSocDisplay(LED_SOC_3,LED_TURN_ON);
-			lightTimer = 0;
-		}
-	}
-	else if(workState == 1)
-	{
-		if(currentSoc<=25)
-		{
-			if(lightTimer<(UINT8)(chargeLEDFlashPeriod*dutyRatio))
+				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);	
-			}
-			else if(lightTimer>=(UINT8)(chargeLEDFlashPeriod*dutyRatio) && lightTimer<chargeLEDFlashPeriod)
-			{
-				NetSocDisplay(LED_SOC_0,LED_TURN_OFF);
-				NetSocDisplay(LED_SOC_1,LED_TURN_OFF);
-				NetSocDisplay(LED_SOC_2,LED_TURN_OFF);
-				NetSocDisplay(LED_SOC_3,LED_TURN_OFF);	
-			}
-			else
-			{
-				NetSocDisplay(LED_SOC_0,LED_TURN_OFF);
-				NetSocDisplay(LED_SOC_1,LED_TURN_OFF);
-				NetSocDisplay(LED_SOC_2,LED_TURN_OFF);
-				NetSocDisplay(LED_SOC_3,LED_TURN_OFF);	
 				lightTimer = 0;
-			}			
-		}	
-		else if(currentSoc>25&&currentSoc<=50)
-		{
-			if(lightTimer<(UINT8)(chargeLEDFlashPeriod*dutyRatio))
-			{
-				NetSocDisplay(LED_SOC_0,LED_TURN_ON);
-				NetSocDisplay(LED_SOC_1,LED_TURN_ON);
-				NetSocDisplay(LED_SOC_2,LED_TURN_OFF);
-				NetSocDisplay(LED_SOC_3,LED_TURN_OFF);	
 			}
-			else if(lightTimer>=(UINT8)(chargeLEDFlashPeriod*dutyRatio) && lightTimer<chargeLEDFlashPeriod)
+			else if(battSOC>25&&battSOC<=50)
 			{
 				NetSocDisplay(LED_SOC_0,LED_TURN_ON);
-				NetSocDisplay(LED_SOC_1,LED_TURN_OFF);
+				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_OFF);
-				NetSocDisplay(LED_SOC_2,LED_TURN_OFF);
-				NetSocDisplay(LED_SOC_3,LED_TURN_OFF);
 				lightTimer = 0;
 			}
-		}
-		else if(currentSoc>50&&currentSoc<=75)
-		{
-			if(lightTimer<(UINT8)(chargeLEDFlashPeriod*dutyRatio))
+			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(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
+			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_OFF);
-				NetSocDisplay(LED_SOC_3,LED_TURN_OFF);
+				NetSocDisplay(LED_SOC_2,LED_TURN_ON);
+				NetSocDisplay(LED_SOC_3,LED_TURN_ON);
 				lightTimer = 0;
 			}
-
 		}
-		else if(currentSoc>75&&currentSoc<=97)
+		else if(battWorkState == 2)
 		{
-			if(lightTimer<(UINT8)(chargeLEDFlashPeriod*dutyRatio))
+			if(battSOC<=25)
 			{
-				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);	
+				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(lightTimer>=(UINT8)(chargeLEDFlashPeriod*dutyRatio) && lightTimer<chargeLEDFlashPeriod)
+			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);	
+				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
+			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_OFF);	
-				lightTimer = 0;
-			}
+				NetSocDisplay(LED_SOC_3,LED_TURN_ON);	
+			}		
 		}
-		else if(currentSoc>97&&currentSoc<=100)
-		{
-			NetSocDisplay(LED_SOC_0,LED_TURN_ON);
-			NetSocDisplay(LED_SOC_1,LED_TURN_ON);
-			NetSocDisplay(LED_SOC_2,LED_TURN_ON);
-			NetSocDisplay(LED_SOC_3,LED_TURN_ON);	
-		}		
 	}
 	
 }
+
 void battErrorStateDisplay()
-{
-	static UINT32 errorState;	
+{	
 	static UINT8  errorLightTimer = 0;
 	//static UINT32 currentTimerCount=0;
 	
@@ -736,46 +818,84 @@ void battErrorStateDisplay()
 		return;
 	}
 	
-	if(UartReadMsg.Header[2]>0)
-	{
-		errorState = ((UartReadMsg.data[(0x09+BATT_CELL_VOL_NUM+BATT_TEMP_NUM+2)*2])<<8)|0|((UartReadMsg.data[(0x0A+BATT_CELL_VOL_NUM+BATT_TEMP_NUM+2)*2])<<24)|((UartReadMsg.data[(0x0A+BATT_CELL_VOL_NUM+BATT_TEMP_NUM+2)*2+1])<<16);
-		//MEMCPY(&errorState,&(UartReadMsg.data[(0x09+BATT_CELL_VOL_NUM+BATT_TEMP_NUM+2)*2]),4); 
-	}
-	
-	errorLightTimer++;
+	if(UartBattInfoRecvFlag == true)
+	{	
 	
-	//errorState = testErrorState;
-	#ifdef USING_PRINTF1
-		for(int k=0;k<UartReadMsg.Header[2];k++)
-			printf("%x  ",UartReadMsg.data[k]);
-		printf("error state = %x\n",errorState);
-	#endif
-	if(errorState != 0)
-	{
-		if(errorLightTimer<(UINT8)(errorLEDFlashPeriod*errorDutyRatio)) 
-		{				
-			FaultDisplay(LED_TURN_ON);
-			
-		}
-		else if(errorLightTimer>=(UINT8)(errorLEDFlashPeriod*errorDutyRatio) && errorLightTimer<errorLEDFlashPeriod)
+		errorLightTimer++;
+
+		if(battWarningState != 0)
 		{
-			FaultDisplay(LED_TURN_OFF);
+			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;			
-		}
-		
+			errorLightTimer = 0;
+		}	
 	}
-	else
-	{
-		FaultDisplay(LED_TURN_OFF);
-		errorLightTimer = 0;
-	}	
 }
 
+void battWarningStateDisplay()
+{
+	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;			
+			}
+			
+		}
+			
+	}
+
+}
 
 
 void battLockStateDisplay(UINT8 lockState)
@@ -837,7 +957,30 @@ void battLockStateDisplay(UINT8 lockState)
 	}	
 	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;	
@@ -896,7 +1039,7 @@ UINT8 Uart_Encrypt_Send()
 	UartEncryptBuffer[15] = CRC_chk_buffer;
 	UartEncryptBuffer[16] = CRC_chk_buffer>>8;
 	USARTdrv->Send(UartEncryptBuffer,17);
-	USARTdrv->Receive(UartDecryptBuffer,5); 	
+	USARTdrv->Receive(UartDecryptBuffer,5); 
 	while((isRecvTimeout == false) && (isRecvComplete == false))
 	{
 		timeCount++;
@@ -908,6 +1051,13 @@ UINT8 Uart_Encrypt_Send()
 			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;
@@ -1731,6 +1881,7 @@ void SP_BMS_Update_Service() //超力源BMS升级服务
 
 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);

+ 9 - 7
src/bsp_custom.c

@@ -120,7 +120,7 @@ static void PMU_WakeupPadInit(void)
 {
     const padWakeupSettings_t cfg =
     {
-        false, true,             // group0 posedge, negedge
+        true, false,             // group0 posedge, negedge
         true, true,             // group1 posedge, negedge
         true, true,             // group2 posedge, negedge
     };
@@ -150,6 +150,7 @@ void BSP_CustomInit(void)
 
     BSP_LoadPlatConfigFromRawFlash();
     rawFlashPlatConfig = BSP_GetRawFlashPlatConfig();
+ #if 1
 #ifdef USING_PRINTF
 	SetPrintUart(PORT_USART_0);
 #else
@@ -159,17 +160,18 @@ void BSP_CustomInit(void)
         uniLogInitStart(UART_0_FOR_UNILOG);
         ECOMM_STRING(UNILOG_PLA_STRING, build_info, P_SIG, "%s", getBuildInfo());
     }
+#endif
 #endif
 
 	slpManGetPMUSettings();				
 
 	PMU_WakeupPadInit();
-	NVIC_DisableIRQ(PadWakeup0_IRQn);
-	NVIC_EnableIRQ(PadWakeup1_IRQn);
-	NVIC_EnableIRQ(PadWakeup2_IRQn);
-	NVIC_DisableIRQ(PadWakeup3_IRQn);
-	NVIC_DisableIRQ(PadWakeup4_IRQn);
-	NVIC_EnableIRQ(PadWakeup5_IRQn);
+	NVIC_EnableIRQ(PadWakeup0_IRQn);//can
+	NVIC_EnableIRQ(PadWakeup1_IRQn);//rx
+	NVIC_EnableIRQ(PadWakeup2_IRQn);//lock
+	NVIC_EnableIRQ(PadWakeup3_IRQn);//gsensor
+	NVIC_EnableIRQ(PadWakeup4_IRQn);//gsensor
+	NVIC_DisableIRQ(PadWakeup5_IRQn);//wakeup2
 	CheckLockOutState();
 	slpManStartWaitATTimer();
 

+ 4 - 1
src/hal_module_adapter.c

@@ -2143,6 +2143,9 @@ void PowerPinConfig(IOType iotype)
 	
       if(iotype == AON_IO)
 	{
+		slpManAONIOVoltSet(IOVOLT_3_30V);//zhengchao 20200412 add
+
+		
 		padConfig.mux = PAD_MuxAlt0;
 
 		PAD_SetPinConfig(35, &padConfig);
@@ -2155,7 +2158,7 @@ void PowerPinConfig(IOType iotype)
 
 		PAD_SetPinConfig(32, &padConfig);
 		GPIO_PinConfig(1, AON_RELAY_DRV, &config);
-		GPIO_PinWrite(1, 1 << AON_RELAY_DRV, 1 << AON_RELAY_DRV);
+		GPIO_PinWrite(1, 1 << AON_RELAY_DRV, 0 << AON_RELAY_DRV);  //zhengchao 20200412 modify 1-->0
 
 		PAD_SetPinConfig(35, &padConfig);
 		GPIO_PinConfig(1, AON_WAKEUP, &config);

+ 261 - 144
src/sensor.c

@@ -1,12 +1,12 @@
 #include "sensor.h"
 
-
-/***************在线测试**********************/
+#if 1//def _SC7A20_
+/***************锟斤拷锟竭诧拷锟斤拷**********************/
 INT8 SL_SC7A20_Online_Test(void)
 {
     UINT8 SL_Read_Reg=0xff;
     GSENSOR_ReadReg(SC7A20_CHIP_ID_ADDRESS,1, &SL_Read_Reg); 
-    //printf("SL_Read_Reg = %#x\r\n",SL_Read_Reg);	
+    printf("SL_Read_Reg = %#x\r\n",SL_Read_Reg);	
     if(SL_Read_Reg==SC7A20_CHIP_ID_VALUE){
 	return  1;
    } else { 
@@ -14,18 +14,18 @@ INT8 SL_SC7A20_Online_Test(void)
    }
 }
 
-/***************BOOT 重载内部寄存器值*********************/
+/***************BOOT 锟斤拷锟斤拷锟节诧拷锟侥达拷锟斤拷值*********************/
 INT8  SL_SC7A20_BOOT(void)
 {
     UINT8 SL_Read_Reg=0xff;
     GSENSOR_ReadReg(SL_SC7A20_CTRL_REG5,1, &SL_Read_Reg);
-    SL_Read_Reg=SL_SC7A20_BOOT_ENABLE|SL_Read_Reg;
+    SL_Read_Reg=SL_SC7A20_BOOT_ENABLE;//|SL_Read_Reg;
     GSENSOR_WriteReg(SL_SC7A20_CTRL_REG5, SL_Read_Reg);
     return  1;
 }
 
 
-/***************传感器量程设置**********************/
+/***************锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷**********************/
 INT8  SL_SC7A20_FS_Config(UINT8 Sc7a20_FS_Reg)
 {
     UINT8 SL_Read_Reg=0xff,SL_Write_Reg;
@@ -42,45 +42,40 @@ INT8  SL_SC7A20_FS_Config(UINT8 Sc7a20_FS_Reg)
 }
 
 #if SL_6D4D2D1D_SEL==1  //1D
-//AOI1_INT1  运动检测,运动触发中断        6D运动检测
-//AOI2_INT2  位置检测,检测是否离开X+位置  1D位置识别
-/***************中断设置*************/
+//AOI1_INT1  锟剿讹拷锟斤拷猓�拷硕锟斤拷锟斤拷锟斤拷卸锟�        6D锟剿讹拷锟斤拷锟�
+//AOI2_INT2  位锟矫硷拷猓�拷锟斤拷锟角凤拷锟诫开X+位锟斤拷  1D位锟斤拷识锟斤拷
+/***************锟叫讹拷锟斤拷锟斤拷*************/
 INT8  SL_SC7A20_INT_Config(void)
 {
     UINT8 SL_Read_Reg;
 
     /*******************AOI1  IN  INT1********************/
-    GSENSOR_ReadReg(SL_SC7A20_CTRL_REG5,1, &SL_Read_Reg);
-    SL_Read_Reg=SL_Read_Reg|0x08;//AOI1 LATCH
+    SL_Read_Reg=0x08;//AOI1 LATCH
     GSENSOR_WriteReg(SL_SC7A20_CTRL_REG5, SL_Read_Reg); 
     
-    //H_LACTIVE SET
-    GSENSOR_ReadReg(SL_SC7A20_CTRL_REG6,1, &SL_Read_Reg); 
-    SL_Read_Reg=SL_SC7A20_INT_ACTIVE_LOWER_LEVEL|SL_Read_Reg;
+    SL_Read_Reg=SL_SC7A20_INT_ACTIVE_HIGH_LEVEL;
     //interrupt happen,int pin output lower level
     GSENSOR_WriteReg(SL_SC7A20_CTRL_REG6, SL_Read_Reg);  
     
     //AOI1 CONFIG
     SL_Read_Reg=0x00;            //0x7F
-    SL_Read_Reg=SL_Read_Reg|0x40;//方向运动识别模式
-    SL_Read_Reg=SL_Read_Reg|0x03;//X 轴高低事件 X轴数据的绝对值大于阈值
-    SL_Read_Reg=SL_Read_Reg|0x0C;//Y 轴高低事件 Y轴数据的绝对值大于阈值	
-    SL_Read_Reg=SL_Read_Reg|0x30;//Z 轴高低事件 Z轴数据的绝对值大于阈值	
+    SL_Read_Reg=SL_Read_Reg|0x40;//锟斤拷锟斤拷锟剿讹拷识锟斤拷模式
+    SL_Read_Reg=SL_Read_Reg|0x02;//X 锟斤拷锟斤拷录锟� X锟斤拷锟斤拷锟捷的撅拷锟斤拷值锟斤拷锟斤拷锟斤拷值
     GSENSOR_WriteReg(SL_SC7A20_INT1_CFG, SL_Read_Reg);    
 
     //HPF SET
-    GSENSOR_ReadReg(SL_SC7A20_CTRL_REG2,1, &SL_Read_Reg); 
-    SL_Read_Reg=SL_Read_Reg|0x81;//Normal HP , HPF TO AOI1 
+  //  GSENSOR_ReadReg(SL_SC7A20_CTRL_REG2,1, &SL_Read_Reg); 
+    SL_Read_Reg=0xcf;//SL_Read_Reg|0x81;//Normal HP , HPF TO AOI1 
     GSENSOR_WriteReg(SL_SC7A20_CTRL_REG2, SL_Read_Reg);
     
-    //中断阈值设置
+    //锟叫讹拷锟斤拷值锟斤拷锟斤拷
     GSENSOR_WriteReg(SL_SC7A20_INT1_THS, SL_SC7A20_INT_THS_10PERCENT);	
-    //大于阈值多少时间触发中断
+    //锟斤拷锟斤拷锟斤拷值锟斤拷锟斤拷时锟戒触锟斤拷锟叫讹拷
     GSENSOR_WriteReg(SL_SC7A20_INT1_DURATION, SL_SC7A20_INT_DURATION_2CLK);
         
     //AOI1 TO INT1
-    GSENSOR_ReadReg(SL_SC7A20_CTRL_REG3,1, &SL_Read_Reg);
-    SL_Read_Reg=SL_Read_Reg|0x40; //AOI1 TO INT1
+   // GSENSOR_ReadReg(SL_SC7A20_CTRL_REG3,1, &SL_Read_Reg);
+    SL_Read_Reg=0x40;//SL_Read_Reg|0x40; //AOI1 TO INT1
     GSENSOR_WriteReg(SL_SC7A20_CTRL_REG3, SL_Read_Reg);  
     /*******************AOI1  IN  INT1********************/
 
@@ -90,26 +85,21 @@ INT8  SL_SC7A20_INT_Config(void)
     SL_Read_Reg=SL_Read_Reg|0x02;//AOI2 LATCH
     GSENSOR_WriteReg(SL_SC7A20_CTRL_REG5, SL_Read_Reg);	
     
-    //H_LACTIVE SET
-    GSENSOR_ReadReg(SL_SC7A20_CTRL_REG6,1, &SL_Read_Reg); 
-    SL_Read_Reg=SL_SC7A20_INT_ACTIVE_LOWER_LEVEL|SL_Read_Reg;
-    //interrupt happen,int pin output lower level
-    GSENSOR_WriteReg(SL_SC7A20_CTRL_REG6, SL_Read_Reg);    
 
     //AOI2 CONFIG
-    SL_Read_Reg=0x00;            //0xC2
-    SL_Read_Reg=SL_Read_Reg|0xC0;//方向位置识别模式
-    SL_Read_Reg=SL_Read_Reg|0x02;//X 轴高事件 X轴数据大于阈值 输出低电平,默认输出高电平
+    SL_Read_Reg=0x00;            //0xFF
+    SL_Read_Reg=SL_Read_Reg|0xC0;//锟斤拷锟斤拷锟剿讹拷识锟斤拷模式
+    SL_Read_Reg=SL_Read_Reg|0x02;//X 锟斤拷锟斤拷录锟� X锟斤拷锟斤拷锟捷的撅拷锟斤拷值锟斤拷锟斤拷锟斤拷值
     GSENSOR_WriteReg(SL_SC7A20_INT2_CFG, SL_Read_Reg);    
 
     //HPF SET
-    GSENSOR_ReadReg(SL_SC7A20_CTRL_REG2,1, &SL_Read_Reg); 
-    SL_Read_Reg=SL_Read_Reg&0xFD;//NO HPF TO AOI2 
-    GSENSOR_WriteReg(SL_SC7A20_CTRL_REG2, SL_Read_Reg);
+   // GSENSOR_ReadReg(SL_SC7A20_CTRL_REG2,1, &SL_Read_Reg); 
+    //SL_Read_Reg=SL_Read_Reg&0xFD;//NO HPF TO AOI2 
+   // GSENSOR_WriteReg(SL_SC7A20_CTRL_REG2, SL_Read_Reg);
 
-    //中断阈值设置
-    GSENSOR_WriteReg(SL_SC7A20_INT2_THS, SL_SC7A20_INT_THS_40PERCENT);	
-    //大于阈值多少时间触发中断
+    //锟叫讹拷锟斤拷值锟斤拷锟斤拷
+    GSENSOR_WriteReg(SL_SC7A20_INT2_THS, SL_SC7A20_INT_THS_10PERCENT);	
+    //锟斤拷锟斤拷锟斤拷值锟斤拷锟斤拷时锟戒触锟斤拷锟叫讹拷
     GSENSOR_WriteReg(SL_SC7A20_INT2_DURATION, SL_SC7A20_INT_DURATION_2CLK);
     
     //AOI2 TO INT2
@@ -121,38 +111,41 @@ INT8  SL_SC7A20_INT_Config(void)
     return  1;
 }
 #elif  SL_6D4D2D1D_SEL==2  //2D
-//AOI1_INT1  运动检测,运动触发中断 或事件   3D运动检测 
-//AOI2_INT2  位置检测,检测X+ 和 X-          2D位置识别
-/***************中断设置*************/
+//AOI1_INT1  锟剿讹拷锟斤拷猓�拷硕锟斤拷锟斤拷锟斤拷卸锟� 锟斤拷锟铰硷拷   3D锟剿讹拷锟斤拷锟� 
+//AOI2_INT2  位锟矫硷拷猓�拷锟斤拷X+ 锟斤拷 X-          2D位锟斤拷识锟斤拷
+/***************锟叫讹拷锟斤拷锟斤拷*************/
 INT8  SL_SC7A20_INT_Config(void)
 {
     UINT8 SL_Read_Reg;
 
     /*******************AOI1  IN  INT1********************/
-    GSENSOR_ReadReg(SL_SC7A20_CTRL_REG5,1, &SL_Read_Reg);
-    SL_Read_Reg=SL_Read_Reg|0x08;//AOI1 LATCH
+    SL_Read_Reg=0x08;//AOI1 LATCH
     GSENSOR_WriteReg(SL_SC7A20_CTRL_REG5, SL_Read_Reg); 
     
+    SL_Read_Reg=SL_SC7A20_INT_ACTIVE_HIGH_LEVEL;
+    //interrupt happen,int pin output lower level
+    GSENSOR_WriteReg(SL_SC7A20_CTRL_REG6, SL_Read_Reg);  
+    
     //AOI1 CONFIG
-    SL_Read_Reg=0x00;            //0x2A 或事件
-    SL_Read_Reg=SL_Read_Reg|0x02;//X 轴高事件 X轴数据的绝对值大于阈值
-    SL_Read_Reg=SL_Read_Reg|0x08;//Y 轴高事件 Y轴数据的绝对值大于阈值	
-    SL_Read_Reg=SL_Read_Reg|0x20;//Z 轴高事件 Z轴数据的绝对值大于阈
+    SL_Read_Reg=0x00;            //0x7F
+    SL_Read_Reg=SL_Read_Reg|0x40;//锟斤拷锟斤拷锟剿讹拷识锟斤拷模式
+    SL_Read_Reg=SL_Read_Reg|0x02;//X 锟斤拷锟斤拷录锟� X锟斤拷锟斤拷锟捷的撅拷锟斤拷值锟斤拷锟斤拷锟斤拷值
+    SL_Read_Reg=SL_Read_Reg|0x04;//Y 锟斤拷锟斤拷录锟� Y锟斤拷锟斤拷锟捷的撅拷锟斤拷值锟斤拷锟斤拷锟斤拷
     GSENSOR_WriteReg(SL_SC7A20_INT1_CFG, SL_Read_Reg);    
-   
+
     //HPF SET
-    GSENSOR_ReadReg(SL_SC7A20_CTRL_REG2,1, &SL_Read_Reg); 
-    SL_Read_Reg=SL_Read_Reg|0x81;//Normal HP , HPF TO AOI1 
+  //  GSENSOR_ReadReg(SL_SC7A20_CTRL_REG2,1, &SL_Read_Reg); 
+    SL_Read_Reg=0xcf;//SL_Read_Reg|0x81;//Normal HP , HPF TO AOI1 
     GSENSOR_WriteReg(SL_SC7A20_CTRL_REG2, SL_Read_Reg);
     
-    //中断阈值设置
+    //锟叫讹拷锟斤拷值锟斤拷锟斤拷
     GSENSOR_WriteReg(SL_SC7A20_INT1_THS, SL_SC7A20_INT_THS_10PERCENT);	
-    //大于阈值多少时间触发中断
+    //锟斤拷锟斤拷锟斤拷值锟斤拷锟斤拷时锟戒触锟斤拷锟叫讹拷
     GSENSOR_WriteReg(SL_SC7A20_INT1_DURATION, SL_SC7A20_INT_DURATION_2CLK);
         
     //AOI1 TO INT1
-    GSENSOR_ReadReg(SL_SC7A20_CTRL_REG3,1, &SL_Read_Reg);
-    SL_Read_Reg=SL_Read_Reg|0x40; //AOI1 TO INT1
+   // GSENSOR_ReadReg(SL_SC7A20_CTRL_REG3,1, &SL_Read_Reg);
+    SL_Read_Reg=0x40;//SL_Read_Reg|0x40; //AOI1 TO INT1
     GSENSOR_WriteReg(SL_SC7A20_CTRL_REG3, SL_Read_Reg);  
     /*******************AOI1  IN  INT1********************/
 
@@ -162,26 +155,22 @@ INT8  SL_SC7A20_INT_Config(void)
     SL_Read_Reg=SL_Read_Reg|0x02;//AOI2 LATCH
     GSENSOR_WriteReg(SL_SC7A20_CTRL_REG5, SL_Read_Reg);	
     
-    //H_LACTIVE SET
-    GSENSOR_ReadReg(SL_SC7A20_CTRL_REG6,1, &SL_Read_Reg); 
-    SL_Read_Reg=SL_SC7A20_INT_ACTIVE_LOWER_LEVEL|SL_Read_Reg;
-    //interrupt happen,int pin output lower level
-    GSENSOR_WriteReg(SL_SC7A20_CTRL_REG6, SL_Read_Reg);    
 
     //AOI2 CONFIG
-    SL_Read_Reg=0x00;            //0xC3
-    SL_Read_Reg=SL_Read_Reg|0xC0;//方向位置识别模式
-    SL_Read_Reg=SL_Read_Reg|0x03;//X 轴高低事件 X轴数据的绝对值大于阈值 输出低电平,默认输出高电平
+    SL_Read_Reg=0x00;            //0xFF
+    SL_Read_Reg=SL_Read_Reg|0xC0;//锟斤拷锟斤拷锟剿讹拷识锟斤拷模式
+    SL_Read_Reg=SL_Read_Reg|0x02;//X 锟斤拷锟斤拷录锟� X锟斤拷锟斤拷锟捷的撅拷锟斤拷值锟斤拷锟斤拷锟斤拷值
+    SL_Read_Reg=SL_Read_Reg|0x04;//Y 锟斤拷锟斤拷录锟� Y锟斤拷锟斤拷锟捷的撅拷锟斤拷值锟斤拷锟斤拷锟斤拷值	
     GSENSOR_WriteReg(SL_SC7A20_INT2_CFG, SL_Read_Reg);    
 
     //HPF SET
-    GSENSOR_ReadReg(SL_SC7A20_CTRL_REG2,1, &SL_Read_Reg); 
-    SL_Read_Reg=SL_Read_Reg&0xFD;//NO HPF TO AOI2 
-    GSENSOR_WriteReg(SL_SC7A20_CTRL_REG2, SL_Read_Reg);
+    //GSENSOR_ReadReg(SL_SC7A20_CTRL_REG2,1, &SL_Read_Reg); 
+    //SL_Read_Reg=SL_Read_Reg&0xFD;//NO HPF TO AOI2 
+    //GSENSOR_WriteReg(SL_SC7A20_CTRL_REG2, SL_Read_Reg);
 
-    //中断阈值设置
-    GSENSOR_WriteReg(SL_SC7A20_INT2_THS, SL_SC7A20_INT_THS_40PERCENT);	
-    //大于阈值多少时间触发中断
+    //锟叫讹拷锟斤拷值锟斤拷锟斤拷
+    GSENSOR_WriteReg(SL_SC7A20_INT2_THS, SL_SC7A20_INT_THS_10PERCENT);	
+    //锟斤拷锟斤拷锟斤拷值锟斤拷锟斤拷时锟戒触锟斤拷锟叫讹拷
     GSENSOR_WriteReg(SL_SC7A20_INT2_DURATION, SL_SC7A20_INT_DURATION_2CLK);
     
     //AOI2 TO INT2
@@ -193,38 +182,41 @@ INT8  SL_SC7A20_INT_Config(void)
     return  1;
 }
 #elif  SL_6D4D2D1D_SEL==4  //4D
-//AOI1_INT1  运动检测,运动触发中断 或事件     3D运动检测 
-//AOI2_INT2  位置检测,检测X+ 和 X-  Y+ 和 Y-  4D位置识别
-/***************中断设置*************/
+//AOI1_INT1  锟剿讹拷锟斤拷猓�拷硕锟斤拷锟斤拷锟斤拷卸锟� 锟斤拷锟铰硷拷     3D锟剿讹拷锟斤拷锟� 
+//AOI2_INT2  位锟矫硷拷猓�拷锟斤拷X+ 锟斤拷 X-  Y+ 锟斤拷 Y-  4D位锟斤拷识锟斤拷
+/***************锟叫讹拷锟斤拷锟斤拷*************/
 INT8  SL_SC7A20_INT_Config(void)
 {
     UINT8 SL_Read_Reg;
 
     /*******************AOI1  IN  INT1********************/
-    GSENSOR_ReadReg(SL_SC7A20_CTRL_REG5,1, &SL_Read_Reg);
-    SL_Read_Reg=SL_Read_Reg|0x08;//AOI1 LATCH
+    SL_Read_Reg=0x08;//AOI1 LATCH
     GSENSOR_WriteReg(SL_SC7A20_CTRL_REG5, SL_Read_Reg); 
     
+    SL_Read_Reg=SL_SC7A20_INT_ACTIVE_HIGH_LEVEL;
+    //interrupt happen,int pin output lower level
+    GSENSOR_WriteReg(SL_SC7A20_CTRL_REG6, SL_Read_Reg);  
+    
     //AOI1 CONFIG
-    SL_Read_Reg=0x00;            //0x2A 或事件
-    SL_Read_Reg=SL_Read_Reg|0x02;//X 轴高事件 X轴数据的绝对值大于阈值
-	SL_Read_Reg=SL_Read_Reg|0x08;//Y 轴高事件 Y轴数据的绝对值大于阈值	
-	SL_Read_Reg=SL_Read_Reg|0x20;//Z 轴高事件 Z轴数据的绝对值大于阈
+    SL_Read_Reg=0x00;            //0x7F
+    SL_Read_Reg=SL_Read_Reg|0x40;//锟斤拷锟斤拷锟剿讹拷识锟斤拷模式
+    SL_Read_Reg=SL_Read_Reg|0x03;//X 锟斤拷叩锟斤拷录锟� X锟斤拷锟斤拷锟捷的撅拷锟斤拷值锟斤拷锟斤拷锟斤拷值
+    SL_Read_Reg=SL_Read_Reg|0x0C;//Y 锟斤拷叩锟斤拷录锟� Y锟斤拷锟斤拷锟捷的撅拷锟斤拷值锟斤拷锟斤拷锟斤拷
     GSENSOR_WriteReg(SL_SC7A20_INT1_CFG, SL_Read_Reg);    
-   
+
     //HPF SET
-    GSENSOR_ReadReg(SL_SC7A20_CTRL_REG2,1, &SL_Read_Reg); 
-    SL_Read_Reg=SL_Read_Reg|0x81;//Normal HP , HPF TO AOI1 
+  //  GSENSOR_ReadReg(SL_SC7A20_CTRL_REG2,1, &SL_Read_Reg); 
+    SL_Read_Reg=0xcf;//SL_Read_Reg|0x81;//Normal HP , HPF TO AOI1 
     GSENSOR_WriteReg(SL_SC7A20_CTRL_REG2, SL_Read_Reg);
     
-    //中断阈值设置
+    //锟叫讹拷锟斤拷值锟斤拷锟斤拷
     GSENSOR_WriteReg(SL_SC7A20_INT1_THS, SL_SC7A20_INT_THS_10PERCENT);	
-    //大于阈值多少时间触发中断
+    //锟斤拷锟斤拷锟斤拷值锟斤拷锟斤拷时锟戒触锟斤拷锟叫讹拷
     GSENSOR_WriteReg(SL_SC7A20_INT1_DURATION, SL_SC7A20_INT_DURATION_2CLK);
         
     //AOI1 TO INT1
-    GSENSOR_ReadReg(SL_SC7A20_CTRL_REG3,1, &SL_Read_Reg);
-    SL_Read_Reg=SL_Read_Reg|0x40; //AOI1 TO INT1
+   // GSENSOR_ReadReg(SL_SC7A20_CTRL_REG3,1, &SL_Read_Reg);
+    SL_Read_Reg=0x40;//SL_Read_Reg|0x40; //AOI1 TO INT1
     GSENSOR_WriteReg(SL_SC7A20_CTRL_REG3, SL_Read_Reg);  
     /*******************AOI1  IN  INT1********************/
 
@@ -234,26 +226,22 @@ INT8  SL_SC7A20_INT_Config(void)
     SL_Read_Reg=SL_Read_Reg|0x02;//AOI2 LATCH
     GSENSOR_WriteReg(SL_SC7A20_CTRL_REG5, SL_Read_Reg);	
     
-    //H_LACTIVE SET
-    GSENSOR_ReadReg(SL_SC7A20_CTRL_REG6,1, &SL_Read_Reg); 
-    SL_Read_Reg=SL_SC7A20_INT_ACTIVE_LOWER_LEVEL|SL_Read_Reg;
-    //interrupt happen,int pin output lower level
-    GSENSOR_WriteReg(SL_SC7A20_CTRL_REG6, SL_Read_Reg);    
 
     //AOI2 CONFIG
-    SL_Read_Reg=0x00;            //0xCF
-    SL_Read_Reg=SL_Read_Reg|0xC0;//方向位置识别模式
-    SL_Read_Reg=SL_Read_Reg|0x0F;//XY 轴高低事件 XY轴数据的绝对值大于阈值 输出低电平,默认输出高电平
+    SL_Read_Reg=0x00;            //0xFF
+    SL_Read_Reg=SL_Read_Reg|0xC0;//锟斤拷锟斤拷锟剿讹拷识锟斤拷模式
+    SL_Read_Reg=SL_Read_Reg|0x03;//X 锟斤拷叩锟斤拷录锟� X锟斤拷锟斤拷锟捷的撅拷锟斤拷值锟斤拷锟斤拷锟斤拷值
+    SL_Read_Reg=SL_Read_Reg|0x0C;//Y 锟斤拷叩锟斤拷录锟� Y锟斤拷锟斤拷锟捷的撅拷锟斤拷值锟斤拷锟斤拷锟斤拷值	
     GSENSOR_WriteReg(SL_SC7A20_INT2_CFG, SL_Read_Reg);    
 
     //HPF SET
-    GSENSOR_ReadReg(SL_SC7A20_CTRL_REG2,1, &SL_Read_Reg); 
-    SL_Read_Reg=SL_Read_Reg&0xFD;//NO HPF TO AOI2 
-    GSENSOR_WriteReg(SL_SC7A20_CTRL_REG2, SL_Read_Reg);
+    //GSENSOR_ReadReg(SL_SC7A20_CTRL_REG2,1, &SL_Read_Reg); 
+   // SL_Read_Reg=SL_Read_Reg&0xFD;//NO HPF TO AOI2 
+   // GSENSOR_WriteReg(SL_SC7A20_CTRL_REG2, SL_Read_Reg);
 
-    //中断阈值设置
-    GSENSOR_WriteReg(SL_SC7A20_INT2_THS, SL_SC7A20_INT_THS_40PERCENT);	
-    //大于阈值多少时间触发中断
+    //锟叫讹拷锟斤拷值锟斤拷锟斤拷
+    GSENSOR_WriteReg(SL_SC7A20_INT2_THS, SL_SC7A20_INT_THS_10PERCENT);	
+    //锟斤拷锟斤拷锟斤拷值锟斤拷锟斤拷时锟戒触锟斤拷锟叫讹拷
     GSENSOR_WriteReg(SL_SC7A20_INT2_DURATION, SL_SC7A20_INT_DURATION_2CLK);
     
     //AOI2 TO INT2
@@ -265,39 +253,42 @@ INT8  SL_SC7A20_INT_Config(void)
     return  1;
 }
 #elif  SL_6D4D2D1D_SEL==6  //6D
-//AOI1_INT1  运动检测,运动触发中断                    6D运动检测
-//AOI2_INT2  位置检测,检测X+ 和 X-  Y+ 和 Y- Z+ 和 Z- 6D位置识别
-/***************中断设置*************/
+//AOI1_INT1  锟剿讹拷锟斤拷猓�拷硕锟斤拷锟斤拷锟斤拷卸锟�                    6D锟剿讹拷锟斤拷锟�
+//AOI2_INT2  位锟矫硷拷猓�拷锟斤拷X+ 锟斤拷 X-  Y+ 锟斤拷 Y- Z+ 锟斤拷 Z- 6D位锟斤拷识锟斤拷
+/***************锟叫讹拷锟斤拷锟斤拷*************/
 INT8  SL_SC7A20_INT_Config(void)
 {
     UINT8 SL_Read_Reg;
 
     /*******************AOI1  IN  INT1********************/
-    GSENSOR_ReadReg(SL_SC7A20_CTRL_REG5,1, &SL_Read_Reg);
-    SL_Read_Reg=SL_Read_Reg|0x08;//AOI1 LATCH
+    SL_Read_Reg=0x08;//AOI1 LATCH
     GSENSOR_WriteReg(SL_SC7A20_CTRL_REG5, SL_Read_Reg); 
     
+    SL_Read_Reg=SL_SC7A20_INT_ACTIVE_HIGH_LEVEL;
+    //interrupt happen,int pin output lower level
+    GSENSOR_WriteReg(SL_SC7A20_CTRL_REG6, SL_Read_Reg);  
+    
     //AOI1 CONFIG
-    SL_Read_Reg=0x00;            //0x7F  无LATCH功能
-    SL_Read_Reg=SL_Read_Reg|0x40;//方向运动识别模式
-    SL_Read_Reg=SL_Read_Reg|0x03;//X 轴高低事件 X轴数据的绝对值大于阈值
-    SL_Read_Reg=SL_Read_Reg|0x0C;//Y 轴高低事件 Y轴数据的绝对值大于阈
-    SL_Read_Reg=SL_Read_Reg|0x30;//Z 轴高低事件 Z轴数据的绝对值大于阈
+    SL_Read_Reg=0x00;            //0x7F
+    SL_Read_Reg=SL_Read_Reg|0x40;//锟斤拷锟斤拷锟剿讹拷识锟斤拷模式
+    SL_Read_Reg=SL_Read_Reg|0x03;//X 锟斤拷叩锟斤拷录锟� X锟斤拷锟斤拷锟捷的撅拷锟斤拷值锟斤拷锟斤拷锟斤拷
+    SL_Read_Reg=SL_Read_Reg|0x0C;//Y 锟斤拷叩锟斤拷录锟� Y锟斤拷锟斤拷锟捷的撅拷锟斤拷值锟斤拷锟斤拷锟斤拷
+    SL_Read_Reg=SL_Read_Reg|0x30;//Z 锟斤拷叩锟斤拷录锟� Z锟斤拷锟斤拷锟捷的撅拷锟斤拷值锟斤拷锟斤拷锟斤拷
     GSENSOR_WriteReg(SL_SC7A20_INT1_CFG, SL_Read_Reg);    
-   
+
     //HPF SET
-    GSENSOR_ReadReg(SL_SC7A20_CTRL_REG2,1, &SL_Read_Reg); 
-    SL_Read_Reg=SL_Read_Reg|0x81;//Normal HP , HPF TO AOI1 
+  //  GSENSOR_ReadReg(SL_SC7A20_CTRL_REG2,1, &SL_Read_Reg); 
+    SL_Read_Reg=0xcf;//SL_Read_Reg|0x81;//Normal HP , HPF TO AOI1 
     GSENSOR_WriteReg(SL_SC7A20_CTRL_REG2, SL_Read_Reg);
     
-    //中断阈值设置
-    GSENSOR_WriteReg(SL_SC7A20_INT1_THS, SL_SC7A20_INT_THS_10PERCENT);	
-    //大于阈值多少时间触发中断
-    GSENSOR_WriteReg(SL_SC7A20_INT1_DURATION, SL_SC7A20_INT_DURATION_2CLK);
+    //锟叫讹拷锟斤拷值锟斤拷锟斤拷
+    GSENSOR_WriteReg(SL_SC7A20_INT1_THS, SL_SC7A20_INT_THS_20PERCENT);	
+    //锟斤拷锟斤拷锟斤拷值锟斤拷锟斤拷时锟戒触锟斤拷锟叫讹拷
+    GSENSOR_WriteReg(SL_SC7A20_INT1_DURATION, SL_SC7A20_INT_DURATION_30CLK);
         
     //AOI1 TO INT1
-    GSENSOR_ReadReg(SL_SC7A20_CTRL_REG3,1, &SL_Read_Reg);
-    SL_Read_Reg=SL_Read_Reg|0x40; //AOI1 TO INT1
+   // GSENSOR_ReadReg(SL_SC7A20_CTRL_REG3,1, &SL_Read_Reg);
+    SL_Read_Reg=0x40;//SL_Read_Reg|0x40; //AOI1 TO INT1
     GSENSOR_WriteReg(SL_SC7A20_CTRL_REG3, SL_Read_Reg);  
     /*******************AOI1  IN  INT1********************/
 
@@ -307,26 +298,23 @@ INT8  SL_SC7A20_INT_Config(void)
     SL_Read_Reg=SL_Read_Reg|0x02;//AOI2 LATCH
     GSENSOR_WriteReg(SL_SC7A20_CTRL_REG5, SL_Read_Reg);	
     
-    //H_LACTIVE SET
-    GSENSOR_ReadReg(SL_SC7A20_CTRL_REG6,1, &SL_Read_Reg); 
-    SL_Read_Reg=SL_SC7A20_INT_ACTIVE_LOWER_LEVEL|SL_Read_Reg;
-    //interrupt happen,int pin output lower level
-    GSENSOR_WriteReg(SL_SC7A20_CTRL_REG6, SL_Read_Reg);    
 
     //AOI2 CONFIG
-    SL_Read_Reg=0x00;            //0xC2
-    SL_Read_Reg=SL_Read_Reg|0xC0;//方向位置识别模式
-    SL_Read_Reg=SL_Read_Reg|0x3F;//XYZ 轴高事件 XYZ轴数据的绝对值大于阈值 输出低电平,默认输出高电平
+    SL_Read_Reg=0x00;            //0xFF
+    SL_Read_Reg=SL_Read_Reg|0xc0;//锟斤拷锟斤拷锟剿讹拷识锟斤拷模式
+    SL_Read_Reg=SL_Read_Reg|0x03;//X 锟斤拷叩锟斤拷录锟� X锟斤拷锟斤拷锟捷的撅拷锟斤拷值锟斤拷锟斤拷锟斤拷值
+    SL_Read_Reg=SL_Read_Reg|0x0C;//Y 锟斤拷叩锟斤拷录锟� Y锟斤拷锟斤拷锟捷的撅拷锟斤拷值锟斤拷锟斤拷锟斤拷值	
+    SL_Read_Reg=SL_Read_Reg|0x30;//Z 锟斤拷叩锟斤拷录锟� Z锟斤拷锟斤拷锟捷的撅拷锟斤拷值锟斤拷锟斤拷锟斤拷值	
     GSENSOR_WriteReg(SL_SC7A20_INT2_CFG, SL_Read_Reg);    
 
     //HPF SET
     GSENSOR_ReadReg(SL_SC7A20_CTRL_REG2,1, &SL_Read_Reg); 
-    SL_Read_Reg=SL_Read_Reg&0xFD;//NO HPF TO AOI2 
-    GSENSOR_WriteReg(SL_SC7A20_CTRL_REG2, SL_Read_Reg);
+ //   SL_Read_Reg=SL_Read_Reg&0xFD;//NO HPF TO AOI2 
+//    GSENSOR_WriteReg(SL_SC7A20_CTRL_REG2, SL_Read_Reg);
 
-    //中断阈值设置
-    GSENSOR_WriteReg(SL_SC7A20_INT2_THS, SL_SC7A20_INT_THS_40PERCENT);	
-    //大于阈值多少时间触发中断
+    //锟叫讹拷锟斤拷值锟斤拷锟斤拷
+    GSENSOR_WriteReg(SL_SC7A20_INT2_THS, SL_SC7A20_INT_THS_5PERCENT);	
+    //锟斤拷锟斤拷锟斤拷值锟斤拷锟斤拷时锟戒触锟斤拷锟叫讹拷
     GSENSOR_WriteReg(SL_SC7A20_INT2_DURATION, SL_SC7A20_INT_DURATION_2CLK);
     
     //AOI2 TO INT2
@@ -339,20 +327,20 @@ INT8  SL_SC7A20_INT_Config(void)
 }
 #endif
 
-/******外部中断处理函数中需要调用该函数*******/
-//retrun_value&0x20=0x20  ZH  Z轴高事件
-//retrun_value&0x20=0x10  ZL  Z轴低事件
-//retrun_value&0x20=0x08  YH  Y轴高事件
-//retrun_value&0x20=0x04  YL  Y轴低事件
-//retrun_value&0x20=0x02  XH  X轴高事件
-//retrun_value&0x20=0x01  XL  X轴低事件
+/******锟解部锟叫断达拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷要锟斤拷锟矫该猴拷锟斤拷*******/
+//retrun_value&0x20=0x20  ZH  Z锟斤拷锟斤拷录锟�
+//retrun_value&0x20=0x10  ZL  Z锟斤拷锟斤拷录锟�
+//retrun_value&0x20=0x08  YH  Y锟斤拷锟斤拷录锟�
+//retrun_value&0x20=0x04  YL  Y锟斤拷锟斤拷录锟�
+//retrun_value&0x20=0x02  XH  X锟斤拷锟斤拷录锟�
+//retrun_value&0x20=0x01  XL  X锟斤拷锟斤拷录锟�
 INT8  SL_SC7A20_INT_RESET(void)
 {
 
     UINT8 SL_Read_Reg1;
     UINT8 SL_Read_Reg2;
     
-    /*****为了避免读取数据过程中又产生中断,可以暂时关闭中断*****/	
+    /*****为锟剿憋拷锟斤拷锟饺★拷锟斤拷莨锟斤拷锟斤拷锟斤拷植锟斤拷锟斤拷卸希锟斤拷锟斤拷锟斤拷锟绞憋拷乇锟斤拷卸锟�*****/	
  
     //SL_Read_Reg1 display the int1 type
     GSENSOR_ReadReg(SL_SC7A20_INT1_SRC,1, &SL_Read_Reg1);
@@ -400,7 +388,127 @@ INT8  SL_SC7A20_INT_RESET(void)
 
 }
 
-/***************数据更新速率**加速度计使能**********/
+
+void  SL_SC7A20_AOI1_event(void)
+{
+
+    UINT8 SL_SRC_Reg1;
+    UINT8 SL_THS_Reg1;
+    UINT8 SL_DURATION_Reg1;
+ 
+ 
+    //SL_Read_Reg1 display the int1 type
+    GSENSOR_ReadReg(SL_SC7A20_INT1_SRC,1, &SL_SRC_Reg1);
+    GSENSOR_ReadReg(SL_SC7A20_INT1_THS,1, &SL_THS_Reg1);
+    GSENSOR_ReadReg(SL_SC7A20_INT1_DURATION,1, &SL_DURATION_Reg1);
+	
+    printf("SL_SC7A20_INT1_SRC=%#x\r\n",SL_SRC_Reg1);	
+ //   if((SL_SRC_Reg1&0x40)==0x40){
+
+	    if((SL_SRC_Reg1&0x01)==0x01)
+	    {
+	        //X-
+		    printf("X1-\r\n");	
+	    }
+	    if((SL_SRC_Reg1&0x02)==0x02)
+	    {
+	        //X+
+		    printf("X1+\r\n");	
+	    }
+	    if((SL_SRC_Reg1&0x04)==0x04)
+	    {
+	        //Y-
+		    printf("Y1-\r\n");	
+	    }
+	    if((SL_SRC_Reg1&0x08)==0x08)
+	    {
+	        //Y+
+		    printf("Y1+\r\n");	
+	    }
+	    if((SL_SRC_Reg1&0x10)==0x10)
+	    {
+	        //Z-
+		    printf("Z1-\r\n");	
+	    }
+	    if((SL_SRC_Reg1&0x20)==0x20)
+	    {
+	        //Z+
+		    printf("Z1+\r\n");	
+	    }
+//    }
+    
+}
+
+
+INT8  SL_SC7A20_AOI2_event(void)
+{
+
+    UINT8 SL_SRC_Reg2;
+    UINT8 SL_THS_Reg2;
+    UINT8 SL_DURATION_Reg2;
+ 
+ 
+    //SL_Read_Reg1 display the int1 type
+    GSENSOR_ReadReg(SL_SC7A20_INT2_SRC,1, &SL_SRC_Reg2);
+    GSENSOR_ReadReg(SL_SC7A20_INT2_THS,1, &SL_THS_Reg2);
+    GSENSOR_ReadReg(SL_SC7A20_INT2_DURATION,1, &SL_DURATION_Reg2);
+	
+    printf("SL_SC7A20_INT2_SRC=%#x\r\n",SL_SRC_Reg2);	
+  //  if((SL_SRC_Reg2&0x40)==0x40){
+
+	    if((SL_SRC_Reg2&0x01)==0x01)
+	    {
+	        //X-
+		    printf("X2-\r\n");	
+	    }
+	    if((SL_SRC_Reg2&0x02)==0x02)
+	    {
+	        //X+
+		    printf("X2+\r\n");	
+	    }
+	    if((SL_SRC_Reg2&0x04)==0x04)
+	    {
+	        //Y-
+		    printf("Y2-\r\n");	
+	    }
+	    if((SL_SRC_Reg2&0x08)==0x08)
+	    {
+	        //Y+
+		    printf("Y2+\r\n");	
+	    }
+	    if((SL_SRC_Reg2&0x10)==0x10)
+	    {
+	        //Z-
+		    printf("Z2-\r\n");	
+	    }
+	    if((SL_SRC_Reg2&0x20)==0x20)
+	    {
+	        //Z+
+		    printf("Z2+\r\n");	
+	    }
+ //   }
+    
+}
+
+
+void  SL_SC7A20_Reg_read_all(void)
+{
+    UINT8 SL_Read_Reg;
+    printf("SL_SC7A20_Reg_readall ++++++++\r\n");	
+    /*******************AOI1  IN  INT1********************/
+	UINT8 adr;
+	for(adr=0x20;adr<=0x3F;adr++){
+		GSENSOR_ReadReg(adr,1, &SL_Read_Reg);
+		printf("%#x = %#x\r\n",adr,SL_Read_Reg);	
+	}
+	
+    printf("SL_SC7A20_Reg_readall --------\r\n");	
+}
+
+
+
+
+/***************锟斤拷锟捷革拷锟斤拷锟斤拷锟斤拷**锟斤拷锟劫度硷拷使锟斤拷**********/
 INT8  SL_SC7A20_Power_Config(UINT8 Power_Config_Reg)
 {
     UINT8 SL_Read_Reg;
@@ -411,6 +519,7 @@ INT8  SL_SC7A20_Power_Config(UINT8 Power_Config_Reg)
     GSENSOR_ReadReg(SL_SC7A20_SDOI2C_PU_CFG,1, &SL_Read_Reg); 
     SL_Read_Reg=SL_Read_Reg|SL_SC7A20_SDO_PU_MSK|SL_SC7A20_I2C_PU_MSK;
     GSENSOR_WriteReg(SL_SC7A20_SDOI2C_PU_CFG, SL_Read_Reg);
+    GSENSOR_WriteReg(SL_SC7A20_MTP_CFG, 0);
 #endif
     SL_Read_Reg  = 0xff;
     GSENSOR_WriteReg(SL_SC7A20_CTRL_REG1, Power_Config_Reg);
@@ -425,7 +534,7 @@ INT8  SL_SC7A20_Power_Config(UINT8 Power_Config_Reg)
 
 
 #if   SL_SC7A20_16BIT_8BIT==0
-/***************加速度计数据读取*8bits*********/
+/***************锟斤拷锟劫度硷拷锟斤拷锟捷讹拷取*8bits*********/
 INT8  SL_SC7A20_Read_XYZ_Data(INT8 *SL_SC7A20_Data_XYZ_Buf)
 {
     UINT8 SL_Read_Buf[7];
@@ -446,20 +555,27 @@ INT8  SL_SC7A20_Read_XYZ_Data(INT8 *SL_SC7A20_Data_XYZ_Buf)
     }
 }
 #elif SL_SC7A20_16BIT_8BIT==1
-/***************加速度计数据读取*16bits*********/
+/***************锟斤拷锟劫度硷拷锟斤拷锟捷讹拷取*16bits*********/
 INT8  SL_SC7A20_Read_XYZ_Data(INT16 *SL_SC7A20_Data_XYZ_Buf)
 {
     UINT8 SL_Read_Buf[7];
     
     GSENSOR_ReadReg(SL_SC7A20_STATUS_REG,1, &SL_Read_Buf[0]);
-    
+    #ifdef USING_PRINTF	
+		printf("SL_SC7A20_STATUS_REG = %#x\r\n",SL_Read_Buf[0]);
+	#endif
     if((SL_Read_Buf[0]&0x0f)==0x0f)
     {
         GSENSOR_ReadReg(SL_SC7A20_DATA_OUT,6, &SL_Read_Buf[1]);
- 
+        #ifdef USING_PRINTF	
+			printf("SL_SC7A20_DATA_OUT22 = %#x\r\n",SL_Read_Buf[1]);
+		#endif
         SL_SC7A20_Data_XYZ_Buf[0]=(INT16)((SL_Read_Buf[2]<<8) + SL_Read_Buf[1]);
         SL_SC7A20_Data_XYZ_Buf[1]=(INT16)((SL_Read_Buf[4]<<8) + SL_Read_Buf[3]);
         SL_SC7A20_Data_XYZ_Buf[2]=(INT16)((SL_Read_Buf[6]<<8) + SL_Read_Buf[5]);
+        #ifdef USING_PRINTF	
+			printf("SL_SC7A20_DATA_OUT BUF:X = %d,Y = %d,Z = %d\r\n",SL_SC7A20_Data_XYZ_Buf[0],SL_SC7A20_Data_XYZ_Buf[1],SL_SC7A20_Data_XYZ_Buf[2]);
+		#endif
         return  1;
     }
     else
@@ -477,7 +593,8 @@ void GsensorInit(void)
 		SL_SC7A20_BOOT();
 		SL_SC7A20_FS_Config(SL_SC7A20_FS_4G);
 		SL_SC7A20_INT_Config();
-		SL_SC7A20_INT_RESET();
+		//SL_SC7A20_INT_RESET();
 		SL_SC7A20_Power_Config(SL_SC7A20_LOWER_POWER_ODR_400HZ);
 	}
 }
+#endif