Browse Source

bootloader升级程序,协议更新,蓝牙和WiFi处理部分待验证

huang_chao 1 year ago
parent
commit
fcba943dda

+ 4 - 1
.cproject

@@ -24,6 +24,7 @@
 							<option id="com.nxp.s32ds.cle.arm.mbs.arm32.bare.option.target.endianness.795150415" name="Endianness" superClass="com.nxp.s32ds.cle.arm.mbs.arm32.bare.option.target.endianness" useByScannerDiscovery="false" value="com.nxp.s32ds.cle.arm.mbs.arm32.bare.option.target.endianness.little" valueType="enumerated"/>
 							<option id="com.nxp.s32ds.cle.arm.mbs.arm32.bare.option.target.fpu.abi.261875578" name="Float ABI" superClass="com.nxp.s32ds.cle.arm.mbs.arm32.bare.option.target.fpu.abi" useByScannerDiscovery="false" value="com.nxp.s32ds.cle.arm.mbs.arm32.bare.option.target.fpu.abi.hard" valueType="enumerated"/>
 							<option id="com.nxp.s32ds.cle.arm.mbs.arm32.bare.option.target.fpu.unit.412690300" name="FPU Type" superClass="com.nxp.s32ds.cle.arm.mbs.arm32.bare.option.target.fpu.unit" useByScannerDiscovery="false" value="com.nxp.s32ds.cle.arm.mbs.arm32.bare.option.target.fpu.unit.fpv4spd16" valueType="enumerated"/>
+							<option id="com.nxp.s32ds.cle.arm.mbs.arm32.bare.option.addtools.createflash.1529875059" superClass="com.nxp.s32ds.cle.arm.mbs.arm32.bare.option.addtools.createflash" value="true" valueType="boolean"/>
 							<targetPlatform archList="all" binaryParser="org.eclipse.cdt.core.ELF" id="cdt.managedbuild.targetPlatform.gnu.cross.603998441" isAbstract="false" osList="all" superClass="cdt.managedbuild.targetPlatform.gnu.cross"/>
 							<builder buildPath="${workspace_loc:/S32K146_wifi_master}/Debug_FLASH" id="com.freescale.s32ds.cross.gnu.builder.1951038843" keepEnvironmentInBuildfile="false" managedBuildOn="true" name="FSL Make Builder" superClass="com.freescale.s32ds.cross.gnu.builder"/>
 							<tool id="com.nxp.s32ds.cle.arm.mbs.arm32.bare.gnu.9.2.tool.c.compiler.1102374714" name="Standard S32DS C Compiler" superClass="com.nxp.s32ds.cle.arm.mbs.arm32.bare.gnu.9.2.tool.c.compiler">
@@ -159,7 +160,9 @@
 								<inputType id="cdt.managedbuild.tool.gnu.assembler.input.1507567568" superClass="cdt.managedbuild.tool.gnu.assembler.input"/>
 								<inputType id="com.freescale.s32ds.cross.gnu.tool.assembler.inputType.asmfile.146607273" superClass="com.freescale.s32ds.cross.gnu.tool.assembler.inputType.asmfile"/>
 							</tool>
-							<tool id="com.nxp.s32ds.cle.arm.mbs.arm32.bare.tool.createflash.2110259576" name="Standard S32DS Create Flash Image" superClass="com.nxp.s32ds.cle.arm.mbs.arm32.bare.tool.createflash"/>
+							<tool id="com.nxp.s32ds.cle.arm.mbs.arm32.bare.tool.createflash.2110259576" name="Standard S32DS Create Flash Image" superClass="com.nxp.s32ds.cle.arm.mbs.arm32.bare.tool.createflash">
+								<option id="com.freescale.s32ds.cross.gnu.option.createflash.choice.2079879030" superClass="com.freescale.s32ds.cross.gnu.option.createflash.choice" useByScannerDiscovery="false" value="com.freescale.s32ds.cross.gnu.option.createflash.choice.ihex" valueType="enumerated"/>
+							</tool>
 							<tool id="com.freescale.s32ds.cross.gnu.tool.createlisting.775876516" name="Standard S32DS Create Listing" superClass="com.freescale.s32ds.cross.gnu.tool.createlisting">
 								<option id="com.freescale.s32ds.cross.gnu.option.createlisting.source.1078160491" name="Display source (--source|-S)" superClass="com.freescale.s32ds.cross.gnu.option.createlisting.source" value="true" valueType="boolean"/>
 								<option id="com.freescale.s32ds.cross.gnu.option.createlisting.allheaders.1845635644" name="Display all headers (--all-headers|-x)" superClass="com.freescale.s32ds.cross.gnu.option.createlisting.allheaders" value="true" valueType="boolean"/>

+ 4 - 4
.settings/language.settings.xml

@@ -5,7 +5,7 @@
 			<provider copy-of="extension" id="org.eclipse.cdt.ui.UserLanguageSettingsProvider"/>
 			<provider-reference id="org.eclipse.cdt.core.ReferencedProjectsLanguageSettingsProvider" ref="shared-provider"/>
 			<provider-reference id="org.eclipse.cdt.managedbuilder.core.MBSLanguageSettingsProvider" ref="shared-provider"/>
-			<provider class="com.freescale.s32ds.cross.gnu.CrossGCCBuiltinSpecsDetector" console="false" env-hash="-1281025181662638893" id="com.freescale.s32ds.cross.gnu.CrossGCCBuiltinSpecsDetector" keep-relative-paths="false" name="CDT S32DS Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
+			<provider class="com.freescale.s32ds.cross.gnu.CrossGCCBuiltinSpecsDetector" console="false" env-hash="-320733184466660709" id="com.freescale.s32ds.cross.gnu.CrossGCCBuiltinSpecsDetector" keep-relative-paths="false" name="CDT S32DS Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
 				<language-scope id="org.eclipse.cdt.core.gcc"/>
 				<language-scope id="org.eclipse.cdt.core.g++"/>
 			</provider>
@@ -16,7 +16,7 @@
 			<provider copy-of="extension" id="org.eclipse.cdt.ui.UserLanguageSettingsProvider"/>
 			<provider-reference id="org.eclipse.cdt.core.ReferencedProjectsLanguageSettingsProvider" ref="shared-provider"/>
 			<provider-reference id="org.eclipse.cdt.managedbuilder.core.MBSLanguageSettingsProvider" ref="shared-provider"/>
-			<provider class="com.freescale.s32ds.cross.gnu.CrossGCCBuiltinSpecsDetector" console="false" env-hash="-1281025181662638893" id="com.freescale.s32ds.cross.gnu.CrossGCCBuiltinSpecsDetector" keep-relative-paths="false" name="CDT S32DS Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
+			<provider class="com.freescale.s32ds.cross.gnu.CrossGCCBuiltinSpecsDetector" console="false" env-hash="-320733184466660709" id="com.freescale.s32ds.cross.gnu.CrossGCCBuiltinSpecsDetector" keep-relative-paths="false" name="CDT S32DS Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
 				<language-scope id="org.eclipse.cdt.core.gcc"/>
 				<language-scope id="org.eclipse.cdt.core.g++"/>
 			</provider>
@@ -27,7 +27,7 @@
 			<provider copy-of="extension" id="org.eclipse.cdt.ui.UserLanguageSettingsProvider"/>
 			<provider-reference id="org.eclipse.cdt.core.ReferencedProjectsLanguageSettingsProvider" ref="shared-provider"/>
 			<provider-reference id="org.eclipse.cdt.managedbuilder.core.MBSLanguageSettingsProvider" ref="shared-provider"/>
-			<provider class="com.freescale.s32ds.cross.gnu.CrossGCCBuiltinSpecsDetector" console="false" env-hash="-1281025181662638893" id="com.freescale.s32ds.cross.gnu.CrossGCCBuiltinSpecsDetector" keep-relative-paths="false" name="CDT S32DS Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
+			<provider class="com.freescale.s32ds.cross.gnu.CrossGCCBuiltinSpecsDetector" console="false" env-hash="-320733184466660709" id="com.freescale.s32ds.cross.gnu.CrossGCCBuiltinSpecsDetector" keep-relative-paths="false" name="CDT S32DS Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
 				<language-scope id="org.eclipse.cdt.core.gcc"/>
 				<language-scope id="org.eclipse.cdt.core.g++"/>
 			</provider>
@@ -38,7 +38,7 @@
 			<provider copy-of="extension" id="org.eclipse.cdt.ui.UserLanguageSettingsProvider"/>
 			<provider-reference id="org.eclipse.cdt.core.ReferencedProjectsLanguageSettingsProvider" ref="shared-provider"/>
 			<provider-reference id="org.eclipse.cdt.managedbuilder.core.MBSLanguageSettingsProvider" ref="shared-provider"/>
-			<provider class="com.freescale.s32ds.cross.gnu.CrossGCCBuiltinSpecsDetector" console="false" env-hash="-1281025181662638893" id="com.freescale.s32ds.cross.gnu.CrossGCCBuiltinSpecsDetector" keep-relative-paths="false" name="CDT S32DS Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
+			<provider class="com.freescale.s32ds.cross.gnu.CrossGCCBuiltinSpecsDetector" console="false" env-hash="-320733184466660709" id="com.freescale.s32ds.cross.gnu.CrossGCCBuiltinSpecsDetector" keep-relative-paths="false" name="CDT S32DS Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
 				<language-scope id="org.eclipse.cdt.core.gcc"/>
 				<language-scope id="org.eclipse.cdt.core.g++"/>
 			</provider>

+ 21 - 12
Project_Settings/Linker_Files/linker_flash_s32k146.ld

@@ -31,13 +31,18 @@
 
 MEMORY
 {         
-    int_flash_interrupts    : ORIGIN = 0x00000000, LENGTH = 0x00000400    /* 1K */    /* Do not change this section */
-    int_flash_config        : ORIGIN = 0x00000400, LENGTH = 0x00000010    /* 16bytes */ /* Do not change this section */
-    int_flash               : ORIGIN = 0x00000410, LENGTH = 0x000FFBF0    /* ~1.0MB */ 
+    int_flash_interrupts    : ORIGIN = 0x00014200, LENGTH = 0x00000400    /* 1K */    /* Do not change this section */
+   /* int_flash_config        : ORIGIN = 0x00000400, LENGTH = 0x00000010    /* 16bytes */ /* Do not change this section */
+     int_flash               : ORIGIN = 0x00014600, LENGTH = 0x000EBA00    /* ~1.0MB */ 
+/*	int_flash_interrupts    : ORIGIN = 0x00080200, LENGTH = 0x00000400	/**/
+/*	int_flash               : ORIGIN = 0x00080600, LENGTH = 0x0007FA00	/**/
+	
+	m_flexram				: ORIGIN = 0x14000000, LENGTH = 0x00001000	
     int_sram_results        : ORIGIN = 0x1FFF0000, LENGTH = 0x00000100    /* 256bytes */
     int_sram                : ORIGIN = 0x1FFF0100, LENGTH = 0x0001DF00    /* ~120K */
-    int_sram_stack_c0       : ORIGIN = 0x2000E000, LENGTH = 0x00001000    /* 4K  */
-    ram_rsvd2               : ORIGIN = 0x2000F000, LENGTH = 0             /* End of SRAM */
+    int_sram_stack_c0       : ORIGIN = 0x2000E000, LENGTH = 0x00001000 - 0x10    /* 4K  */
+    ram_rsvd2               : ORIGIN = 0x2000EFF0, LENGTH = 0             /* End of SRAM */
+    ExchangeInfo            : ORIGIN = 0x2000EFF0, LENGTH = 0x10          
 }
 
 
@@ -47,20 +52,23 @@ ENTRY(Reset_Handler)
 
 SECTIONS
 {
-    
+   .eeeprom (NOLOAD):		
+   {						
+    	*(.eeprom)			
+    } >m_flexram			
 	.flash_interrupts :
 	{
-		. = ALIGN(4096);
+		. = ALIGN(512);
         __interrupts_rom_start = .;
         KEEP(*(.intc_vector))    
-        . = ALIGN(4);
+        . = ALIGN(512);
         __interrupts_rom_end = .;
 	} > int_flash_interrupts
 	
-	.flash_config :
-	{
-		KEEP(*(.flash_config))
-	} > int_flash_config
+/*	.flash_config :				*/
+/*	{							*/
+/*		KEEP(*(.flash_config))	*/
+/*	} > int_flash_config		*/
 	
 	.flash :
 	{
@@ -107,6 +115,7 @@ SECTIONS
 		__DATA_ROM = .;
 	} > int_flash
     
+    
     . = ALIGN(4);
     PROVIDE(__exidx_start = .);
     .ARM.exidx :

+ 96 - 4
src/AppGlobalVar.c

@@ -16,14 +16,106 @@
 
 #include "AppGlobalVar.h"
 //*全局变量*//
-uint32 BleVersion = 0x01010401;         // ble的软件版本
-uint8 BleMacAddr[6];
-uint8 BleTargetMacAddr[6] = {0xBE, 0x84, 0xCC, 0x29, 0x3B, 0xCB};
+uint32 BleVersion = 0x01010401;       /* BLE 软件版本*/
+uint8 BleMacAddr[6] = {0};
+uint8 BleTargetMacAddr[6] = {0xBE, 0x84, 0xCC, 0x29, 0x3B, 0xCB};	/* 目标蓝牙地址 */
+/* 管理蓝牙设备的全局变量 */
+uint8 BleConnectControl = 0;    /* 蓝牙连接管理 0-无操作 1-连接蓝牙 2-断开蓝牙 (车端蓝牙暂时不提供主动连接功能)*/
+uint8 BlePwrControl = 0; 	   	/* 蓝牙开关机  0-无操作  1-关闭蓝牙 2-开启蓝牙*/
+
+/* 管理WiFi设备的全局变量 */
+uint8 WiFiConnectControl;     	/* WiFi连接管理 0-无操作 1-连接WiFi 2-断开WiFi */
+uint8 WiFiPwrControl;         	/* 0-无操作 1-关闭WiFI 2-开启WiFI */
+
+/* 心跳丢失次数 */
+uint8 heartTickLost_W = 0;   	/* wifi 心跳丢失次数计数 */
+uint8 heartTickLost_B = 0;  	/* BLE 心跳丢失次数计数 */
+
+/* EBC状态 */
+uint8 EBC_State = 0;		  	/* EBC当前状态 0-行车 1-通信 2-换电 */
+uint8 EBC_ExchangeMode = 0;   	/* EBC换电模式状态 0-无线蓝牙 1-有线连接 默认无线连接 */
+uint8 EBC_LockMode = 0;       	/* EBC解锁与锁紧模式  0-单步模式 1-自动模式 默认为单步模式*/
+uint8 EBC_ExchangeKind = 0;   	/* 换电连接器种类 0-4连接器 1-单连接器 */
+uint8 EBC_AExchangeCheck = 0; 	/* 允许换电判断 0-不允许 1-允许 */
+
+/* 传感器状态 */
+uint8 UnlockCheck = 0;        	/* 解锁完成判断 0-完全未解锁 1-完全解锁 2-未完全解 */
+uint8 LockCheck = 0;          	/* 锁紧完成判断 0-完全未锁紧 1-完全锁紧 2-未完全锁紧*/
+uint8 BatPlace = 0;			  	/* 电池底座到位判断  0-完全未到位 1-完全到位 2-未完全到位 */
+uint8 UnlockSensorSignal = 0; 	/* 解锁传感器信号  传感器0-3的状态 每bit:1-触发,0-不触发 */
+uint8 LockSensorSignal = 0;   	/* 锁紧传感器信号 传感器0-3的状态 每bit:1-触发,0-不触发 */
+uint8 BaseSensorSignal = 0;   	/* 底座落座传感器 bit0-bit1:传感器0-1的状态 每bit:1-触发,0-无触发 */
+uint8 InterlockSignal = 0;    	/* 互锁信号  bit0:连接器互锁,bit1:整车互锁 */
+uint8 SolValActuSt    = 0;      /* 电磁阀驱动状态 0:驱动关闭    1:驱动打开 */
+
+uint8 AirPrValue = 0;         	/* 压力传感器气压值 单位Mpa,精度0.01Mpa*/
+uint8 TheManFluid = 0;        	/* 热管理液位状态 1-液位正常 0-液位过低 */
+
+/* 车辆信息 */
+uint8 WholeVehComState = 0;   	/* 整车通信状态 0-通信中断 1-通信正常 */
+uint8 KeySignal = 0;          	/* 钥匙信号 1-触发(钥匙上电)0-无触发(钥匙下电)*/
+uint8 VehicleState = 0;       	/* 车辆状态 0001-车辆启动状态  0010-熄火 0011-  其他状态1111-无效 */
+uint8 BrakeSignal = 0; 		  	/* 刹车信号 bit0:手刹,bit1:脚刹,0-无效,1-有效 */
+uint8 GearSignal = 0;         	/* 挡位信号 FF-倒挡(R)0-空挡(N)X-前进档(DX)X取1-12 */
+uint8 TurnSignal = 0;         	/* 方向盘转动 bit0-左侧转向灯 bit1-右侧转向灯 每bit 0-OFF,1-ON*/
+uint8 FrontAxleValue = 0;	  	/* 前桥气压值 */
+uint8 RearAxleValue = 0;	  	/* 后桥气压值 */
+uint8 VehicleVin[17] = {0}; 	/* 车辆VIN码 17长度 */
+/* 电池信息 */
+uint8 BatComState = 0; 		  	/* 电池通信状态 0-通信中断 1-通信正常 */
+uint8 BatBMS_State = 0;		  	/* 电池BMS状态 0-高压开启 1-预充 2-高压关断 3-高压上电故障 */
+uint8 MContactorsState= 0;	  	/* 主接触器状态 0-保留 1-断开 2-关闭 3-异常 */
+uint8 SOC = 0; 				  	/* 电池SOC 0.10% */
+uint8 BatValtage = 0; 		  	/* 电池电压 0.1V */
+uint8 SOH = 0;				  	/* 电池SOH 0.40% */
+uint8 BatSN[26] = {0};		  	/* 电池SN码 */
+uint16 BatteryCurrent = 0;      /* 电池电流 */
+uint16 BatRateRnergy  = 0;      /* 电池额定能量 */
+/* 故障数据 */
+uint8 ErrorLevel = 0; 		  	/* 故障等级 */
+uint8 ErrorCode = 0;		 	/* 故障码 */
+/* 累计信息 */
+uint32 AccruedChargeAh = 0;   	/* 累计充入Ah数  Ah 0.1 */
+uint32 AccruedChargeKwh = 0;  	/* 累计充入kwh数 kwh 0.1 */
+uint32 AccruedDischargeAh = 0;	/* 累计放出Ah数 Ah 0.1 */
+uint32 AccruedDischargeKwh= 0;	/* 累计放出kwh数 kwh 0.1*/
+uint16 SingleChargeAh = 0;    	/* 单次充电Ah数 Ah 0.1 */
+uint16 SingleChargeKwh = 0;   	/* 单次充电kwh数 kwh 0.1 */
+uint32 AccruedMileage = 0; 	  	/* 累计行驶里程 km 0.1 */
+uint32 SS_AccruedChargeAh = 0;	/* 累计换电站充入Ah数 Ah 0.1 */
+uint32 SS_AccruedChargeKwh = 0;	/* 累计换电站充入kwh数 kwh 0.1 */
+uint32 IG_AccruedChargeAh = 0; 	/* 累计插枪充入Ah数 Ah 0.1 */
+uint32 IG_AccruedChargeKwh = 0; /* 累计插枪充入kwh数 kwh 0.1 */
+uint32 AFE_AccruedChargeAh = 0; /* 累计动能回馈Ah数 */
+uint32 AFE_AccruedChargeKwh = 0;/* 累计动能回馈kwh数 */
+uint16 ExchangeNum = 0; 		/* 换电次数 */
+uint16 ExchangeNumSuc = 0; 		/* 换电成功次数 */
+
+/* BLE To EBC */
+
+/* 通讯模块反馈状态 */
+uint8 BLEConFed = 0; 		   	 /* 0-无操作 1-蓝牙已连接 2-蓝牙已断开 */
+uint8 BLEPWRFed = 0;			 /* 0-无操作 1-蓝牙已关机 2-蓝牙已开机 */
+uint8 WiFiConFed = 0;			 /* 0-无操作 1-WIFI已连接 2-WIFI已断开 */
+uint8 WiFiPWRFed = 0;			 /* 0-无操作 1-WIFI已关机 2-WIFI已开机 */
+uint8 EffectiveDataFed = 0;       /* 0-无数据应答 1-有数据应答 */
+
+uint8 EmeStopSta = 0; 			 /* 0-无操作,1-进行紧急停止 */
+uint8 LockSta = 0; 				 /* 0-无操作,1-控制锁机构锁紧(和解锁互斥,以锁紧为准) */
+uint8 UnlockSta = 0; 			 /* 0-无操作,1-控制锁机构解锁 */
+uint8 AutoLockSta = 0; 			 /* 0-无操作,1-锁机构自动锁紧 */
+uint8 AutoUnlockSta = 0; 		 /* 0-无操作,1-锁机构自动解锁 */
+uint8 StateSwitch = 0; 			 /* 0-无操作,1-切换为换电状态,2-切换为行车状态 */
+
+
+uint8 BleStateFlg = 0;    	/* 蓝牙当前状态,0-未连接,1-已经连接 2-电源关机   -- 默认0与1状态下电源开机*/
+uint8 tcp_server_process_status = 2; /* WiFi当前状态 0-未连接,1-已经连接 2-电源关机   -- 默认0与1状态下电源开机 */
+
 uint8 BleCurrentRole = 0;
 uint8 BleSetRole = 0;          // 0-从机(默认),1-主机
 uint8 BleSetRoleEnable = 0;    //默认角色不设置
 uint8 BleConnectEnable = 0;    //主机蓝牙连接使能,0-未使能,1-使能
-uint8 BleConnectFlg = 0;       //主机蓝牙连接状态,0-未连接,1-连接
+//uint8 BleConnectFlg = 0;       //主机蓝牙连接状态,0-未连接,1-连接
 uint8 BleDisconnectEnable = 0; //主机蓝牙断开使能,0-未使能,1-使能
 uint8 BleDisconnectFlg = 0;    //主机蓝牙断开连接状态,0-未连接,1-连接
 uint8 BlePwrControlEnable = 0; //蓝牙开关机使能,默认不使能,自动开机

+ 89 - 2
src/AppGlobalVar.h

@@ -17,14 +17,101 @@
 #define APPGLOBALVAR_H_
 
 #include "hal_adapter.h"
+#define MAX_LOSTHEART 30
 
 extern uint8 BleMacAddr[6];
-extern uint8 BleTargetMacAddr[6];
+extern uint8 BleTargetMacAddr[6];	/* 目标蓝牙地址 */
+/* 管理蓝牙设备的全局变量 */
+extern uint8 BleConnectControl;    /* 蓝牙连接管理 0-无操作 1-连接蓝牙 2-断开蓝牙 (车端蓝牙暂时不提供主动连接功能)*/
+extern uint8 BlePwrControl; 	   	/* 蓝牙开关机  0-无操作  1-关闭蓝牙 2-开启蓝牙*/
+
+/* 管理WiFi设备的全局变量 */
+extern uint8 WiFiConnectControl;     	/* WiFi连接管理 0-无操作 1-连接WiFi 2-断开WiFi */
+extern uint8 WiFiPwrControl;         	/* 0-无操作 1-关闭WiFI 2-开启WiFI */
+
+/* 心跳丢失次数 */
+extern uint8 heartTickLost_W;
+extern uint8 heartTickLost_B;
+
+/* EBC状态 */
+extern uint8 EBC_State;		  	/* EBC当前状态 0-行车 1-通信 2-换电 */
+extern uint8 EBC_ExchangeMode;   	/* EBC换电模式状态 0-无线蓝牙 1-有线连接 默认无线连接 */
+extern uint8 EBC_LockMode;       	/* EBC解锁与锁紧模式  0-单步模式 1-自动模式 默认为单步模式*/
+extern uint8 EBC_ExchangeKind;   	/* 换电连接器种类 0-4连接器 1-单连接器 */
+extern uint8 EBC_AExchangeCheck; 	/* 允许换电判断 0-不允许 1-允许*/
+
+/* 传感器状态 */
+extern uint8 UnlockCheck;        	/* 解锁完成判断 0-完全未解锁 1-完全解锁 2-未完全解 */
+extern uint8 LockCheck;          	/* 锁紧完成判断 0-完全未锁紧 1-完全锁紧 2-未完全锁紧*/
+extern uint8 BatPlace;			  	/* 电池底座到位判断  0-完全未到位 1-完全到位 2-未完全到位 */
+extern uint8 UnlockSensorSignal; 	/* 解锁传感器信号  传感器0-3的状态 每bit:1-触发,0-不触发 */
+extern uint8 LockSensorSignal;   	/* 锁紧传感器信号 传感器0-3的状态 每bit:1-触发,0-不触发 */
+extern uint8 BaseSensorSignal;   	/* 底座落座传感器 bit0-bit1:传感器0-1的状态 每bit:1-触发,0-无触发 */
+extern uint8 InterlockSignal;    	/* 互锁信号  bit0:连接器互锁,bit1:整车互锁 */
+extern uint8 SolValActuSt;          /* 电磁阀驱动状态 0:驱动关闭    1:驱动打开 */
+extern uint8 AirPrValue;        	/* 压力传感器气压值 单位Mpa,精度0.01Mpa*/
+extern uint8 TheManFluid;        	/* 热管理液位状态 1-液位正常 0-液位过低 */
+
+/* 车辆信息 */
+extern uint8 WholeVehComState;   	/* 整车通信状态 0-通信中断 1-通信正常 */
+extern uint8 KeySignal;          	/* 钥匙信号 1-触发(钥匙上电)0-无触发(钥匙下电)*/
+extern uint8 VehicleState;       	/* 车辆状态 0001-车辆启动状态  0010-熄火 0011-  其他状态1111-无效 */
+extern uint8 BrakeSignal; 		  	/* 刹车信号 bit0:手刹,bit1:脚刹,0-无效,1-有效 */
+extern uint8 GearSignal;         	/* 挡位信号 FF-倒挡(R)0-空挡(N)X-前进档(DX)X取1-12 */
+extern uint8 TurnSignal;         	/* 方向盘转动 bit0-左侧转向灯 bit1-右侧转向灯 每bit 0-OFF,1-ON*/
+extern uint8 FrontAxleValue;	  	/* 前桥气压值 */
+extern uint8 RearAxleValue;	  	  /* 后桥气压值 */
+extern uint8 VehicleVin[17]; 		/* 车辆VIN码 17长度 */
+/* 电池信息 */
+extern uint8 BatComState; 		  	/* 电池通信状态 0-通信中断 1-通信正常 */
+extern uint8 BatBMS_State;		  	/* 电池BMS状态 0-高压开启 1-预充 2-高压关断 3-高压上电故障 */
+extern uint8 MContactorsState;	  	/* 主接触器状态 0-保留 1-断开 2-关闭 3-异常 */
+extern uint8 SOC; 				  	/* 电池SOC 0.10% */
+extern uint8 BatValtage; 		  	/* 电池电压 0.1V */
+extern uint8 SOH;				  	/* 电池SOH 0.40% */
+extern uint8 BatSN[26];		  	/* 电池SN码 */
+extern uint16 BatteryCurrent;      /* 电池电流 */
+extern uint16 BatRateRnergy;      /* 电池额定能量 */
+/* 故障数据 */
+extern uint8 ErrorLevel; 		  	/* 故障等级 */
+extern uint8 ErrorCode;		 	/* 故障码 */
+/* 累计信息 */
+extern uint32 AccruedChargeAh;   	/* 累计充入Ah数  Ah 0.1 */
+extern uint32 AccruedChargeKwh;  	/* 累计充入kwh数 kwh 0.1 */
+extern uint32 AccruedDischargeAh;	/* 累计放出Ah数 Ah 0.1 */
+extern uint32 AccruedDischargeKwh;	/* 累计放出kwh数 kwh 0.1*/
+extern uint16 SingleChargeAh;    	/* 单次充电Ah数 Ah 0.1 */
+extern uint16 SingleChargeKwh;   	/* 单次充电kwh数 kwh 0.1 */
+extern uint32 AccruedMileage; 	  	/* 累计行驶力创 km 0.1 */
+extern uint32 SS_AccruedChargeAh;	/* 累计换电站充入Ah数 Ah 0.1 */
+extern uint32 SS_AccruedChargeKwh;	/* 累计换电站充入kwh数 kwh 0.1 */
+extern uint32 IG_AccruedChargeAh; 	/* 累计插枪充入Ah数 Ah 0.1 */
+extern uint32 IG_AccruedChargeKwh; /* 累计插枪充入kwh数 kwh 0.1 */
+extern uint32 AFE_AccruedChargeAh; /* 累计动能回馈Ah数 */
+extern uint32 AFE_AccruedChargeKwh;/* 累计动能回馈kwh数 */
+extern uint16 ExchangeNum; 		/* 换电次数 */
+extern uint16 ExchangeNumSuc; 		/* 换电成功次数 */
+extern uint8 BleStateFlg;    	/* 蓝牙当前状态,0-未连接,1-已经连接 2-电源关机   -- 默认0与1状态下电源开机*/
+extern uint8 tcp_server_process_status; /* WiFi当前状态 0-未连接,1-已经连接 2-电源关机 */
+
+/* 通讯模块反馈状态 */
+extern uint8 BLEConFed; 		   	 /* 0-无操作 1-蓝牙已连接 2-蓝牙已断开 */
+extern uint8 BLEPWRFed;			 /* 0-无操作 1-蓝牙已关机 2-蓝牙已开机 */
+extern uint8 WiFiConFed;			 /* 0-无操作 1-WIFI已连接 2-WIFI已断开 */
+extern uint8 WiFiPWRFed;			 /* 0-无操作 1-WIFI已关机 2-WIFI已开机 */
+extern uint8 EffectiveDataFed;		 /* 0-无数据应答 1-有数据应答 */
+extern uint8 EmeStopSta; 			 /* 0-无操作,1-进行紧急停止 */
+extern uint8 LockSta; 				 /* 0-无操作,1-控制锁机构锁紧(和解锁互斥,以锁紧为准) */
+extern uint8 UnlockSta; 			 /* 0-无操作,1-控制锁机构解锁 */
+extern uint8 AutoLockSta; 			 /* 0-无操作,1-锁机构自动锁紧 */
+extern uint8 AutoUnlockSta; 		 /* 0-无操作,1-锁机构自动解锁 */
+extern uint8 StateSwitch; 			 /* 0-无操作,1-切换为换电状态,2-切换为行车状态 */
+
 extern uint8 BleCurrentRole;
 extern uint8 BleSetRole;
 extern uint8 BleSetRoleEnable;
 extern uint8 BleConnectEnable;
-extern uint8 BleConnectFlg;
+
 extern uint8 BleDisconnectEnable;
 extern uint8 BleDisconnectFlg;
 extern uint8 BlePwrControlEnable;

+ 371 - 362
src/AppTaskBle.c

@@ -5,6 +5,7 @@
  *      Author: 陈杰
  */
 #include "AppTaskBle.h"
+
 const BleAtCmdFunc_t BleAtCmdFunc[] = {
 	{AT_Cmd_Test, "AT", at_callbackFunc},
 	{AT_GetMac, "AT+MAC?", at_callbackFunc},
@@ -16,206 +17,31 @@ const BleAtCmdFunc_t BleAtCmdFunc[] = {
 	{AT_RoleSet, "AT+ROLE=", at_callbackFunc},
 	{AT_Conn, "AT+CONN=", at_callbackFunc},
 	{AT_DisCon, "AT+DISCON", at_callbackFunc},
-	{AT_Reset, "AT+RESET", at_callbackFunc}};
+	{AT_Reset, "AT+RESET", at_callbackFunc},
+	{AT_QuitCSend, "+++", at_callbackFunc}};
 static void AtcmdTransmit(sint8 CmdIdx, uint8 *SetValuePtr, uint16 SetValueLen, uint8 *CmdRead, sint8 *retFunc);
 static void bleInit(void);
-static sint8 bleRoleSetFunc(void);
+static sint8 bleRoleSetFunc(uint8 SET_ROLE);
+static uint8 bleDisconnect(void);
+static void BLEStaMan(void);
+static void BLEConnectMan(uint8* AtCmdRead, uint16* ReadLen, BleState BleStateFlg);
+
+static uint8 bleSendFunc(uint8 *SendDataPtr, uint16 SendDataLen);
+static void bleDataDecode(uint8 *dataIn,uint16 dataLen);
 void BleTask(void *pvParameters)
 {
 	(void)pvParameters;
-	uint8 McuDataSend[10] = {0};
-	sint8 QueueRet = -1;
 	uint8 AtCmdRead[150] = {0};
+	BleStateFlg = NoConnect;
     Dio_WriteChannel(DioConf_DioChannel_PTE9_GPIO_OUT_MCU_BT_POW_EN,STD_ON);
-
 	Dio_WriteChannel(DioConf_DioChannel_PTE7_GPIO_OUT_MCU_BT_MOD, STD_OFF);
 	bleInit();
 	Dio_WriteChannel(DioConf_DioChannel_PTE7_GPIO_OUT_MCU_BT_MOD, STD_ON);
-	uint8 bleWorkState = 0;
-	sint8 ret = -1;
-	QueueDataTrans_Type BleAnsData={0,NULL};
-	QueueDataTrans_Type BleAskData={0,NULL};
 	uint16 ReadLen = 0;
-	uint8 *retptr = NULL;
-	uint8 *pSend = NULL;
 	for (;;)
 	{
-		switch (bleWorkState)
-		{
-		case 0: //等待连接或者主动连接
-		{
-			if (BleSetRoleEnable) //是否更改角色
-			{
-				if (bleRoleSetFunc() == 0)
-				{
-					BleSetRoleEnable = 0;
-				}
-			}
-			if (BleCurrentRole == 1)
-			{
-				if (BleConnectEnable == 1 && BleConnectFlg == 0) //主机蓝牙主动请求连接
-				{
-					Dio_WriteChannel(DioConf_DioChannel_PTE7_GPIO_OUT_MCU_BT_MOD, STD_OFF);
-					AtcmdTransmit(AT_Conn, BleTargetMacAddr, sizeof(BleTargetMacAddr), AtCmdRead, &ret);
-					if (ret == 0) //连接成功
-					{
-						bleWorkState = 1;
-						BleConnectEnable = 0;
-						BleConnectFlg = 1;
-						BleDisconnectFlg = 0;
-						xSemaphoreGive(ConnSemaphore);
-					}
-					else
-					{
-						BleConnectFlg = 0;
-					}
-					Dio_WriteChannel(DioConf_DioChannel_PTE7_GPIO_OUT_MCU_BT_MOD, STD_ON);
-				}
-			}
-			UART_Receive_Data(UART_LPUART2, AtCmdRead,&ReadLen, 100);
-			if (ReadLen>0)//未连接状态下,蓝牙串口处会发送连接状态以及扫描的内容
-			{
-				retptr = (uint8 *)strstr((char *)AtCmdRead, (char *)("STA:connect"));
-				if (retptr)
-				{
-					bleWorkState = 1;
-					BleConnectEnable = 0;
-					BleConnectFlg = 1;
-					BleDisconnectFlg = 0;
-					xSemaphoreGive(ConnSemaphore);
-				}
-				if(BleCurrentRole == 0)//如果从机在串口0上收到数据,则证明已连接
-				{
-					uint8 DataTemp[8] = {0x00};
-					DataTemp[0] = 0x01;
-					DataTemp[1] = 0x01;
-					bleWorkState = 1;
-					BleConnectFlg = 1;
-					BleDisconnectFlg = 0;
-					BleAskData.RealLen = 1;
-					BleAskData.DataPtr = DataTemp;
-					xQueueSend(Uart2ToCanDataHandle,&BleAskData,0);
-				}
-			}
-			break;
-		}
-		case 1: //已连接
-		{
-			if (BleDisconnectEnable == 1 && BleDisconnectFlg == 0)//主从机均可断开连接
-			{
-				//蓝牙断开指令,Mod拉低,才能发送命令
-				/*S32K*/
-				Dio_WriteChannel(DioConf_DioChannel_PTE7_GPIO_OUT_MCU_BT_MOD, STD_OFF);
-
-				memset(AtCmdRead, 0x00, sizeof(AtCmdRead));
-				AtcmdTransmit(AT_DisCon, NULL, 0, AtCmdRead, &ret);
-				if (ret == 0)
-				{
-					memset(AtCmdRead, 0x00, sizeof(AtCmdRead));
-					AtcmdTransmit(AT_Reset, NULL, 0, AtCmdRead, &ret);
-					if (ret == 0)
-					{
-						BleDisconnectFlg = 1;
-						BleDisconnectEnable = 0;
-						BleConnectFlg = 0;
-						/*S32K*/
-						bleWorkState = 0;
-						Dio_WriteChannel(DioConf_DioChannel_PTE7_GPIO_OUT_MCU_BT_MOD, STD_ON);
-					}
-				}
-				else
-				{
-					BleDisconnectFlg = 0;
-				}
-			}
-			/*已连接状态下的数据交换在此进行
-			UartTramsit;
-			*/
-			/*S32K144*/
-			if (BleCurrentRole == 1)//主机蓝牙在连接状态下的蓝牙串口发送功能,从MCU串口0队列得到,发给串口2
-			{
-				if(AT_MsgFlag)
-				{
-					QueueRet = xQueueReceive(McuDataSendHandle,McuDataSend,0);
-					UART_Send_Data(UART_LPUART2,McuDataSend,sizeof(McuDataSend),100);
-					AT_MsgFlag = false;
-				}
-			}
-			else if(BleCurrentRole == 0)//从机蓝牙在连接状态下的蓝牙串口发送功能,从CAN队列得到,发给串口2
-			{
-				if((xQueueReceive(CanToUart2DataHandle,&BleAnsData,0)==pdPASS))
-				{
-					UART_Send_Data(UART_LPUART2,BleAnsData.DataPtr,BleAnsData.RealLen,1000);
-					if(BleAnsData.DataPtr)
-					{
-						free(BleAnsData.DataPtr);
-					}
-					BleAnsData.DataPtr = NULL;
-				}
-			}
-			UART_Receive_Data(UART_LPUART2, AtCmdRead,&ReadLen, 100);
-			if (ReadLen>0)//连接的状态下,蓝牙串口会发送断开状态以及发送的内容
-			{
-				uint8 *retptr = NULL;
-				retptr = (uint8 *)strstr((char *)AtCmdRead, (char *)("STA:disconnec"));
-				if(retptr)
-				{
-					bleWorkState = 0;
-					BleDisconnectFlg = 1;
-					BleConnectFlg = 0;
-					if(BleCurrentRole == 0)//从机断开连接,主动给ebc发送一帧信息
-					{
-						uint8 DataTemp[8] = {0x00};
-						DataTemp[0] = 0x01;
-						DataTemp[1] = 0x02;
-						BleAskData.RealLen = 1;
-						BleAskData.DataPtr = DataTemp;
-						xQueueSend(Uart2ToCanDataHandle,&BleAskData,0);
-					}
-				}
-				else
-				{
-					if(BleCurrentRole == 1)
-					{
-						QueueDataTrans_Type DataSend={0,NULL};
-						DataSend.RealLen = ReadLen;
-						DataSend.DataPtr = malloc(ReadLen+1);
-						memset(DataSend.DataPtr,0x00,ReadLen+1);
-						memcpy(DataSend.DataPtr,AtCmdRead,DataSend.RealLen);
-						sint8 McuDataRet = -1;
-						McuDataRet= xQueueSend(McuDataRecvHandle,&DataSend,1000*2);//增加数据的有效性判定,主机蓝牙从Uart2->Uart0数据的有效性,len>7?
-						if(McuDataRet!=pdPASS)
-						{
-							if(DataSend.DataPtr != NULL)
-							{
-								free(DataSend.DataPtr);
-							}
-							DataSend.DataPtr = NULL;
-						}
-					}
-					else if(BleCurrentRole == 0 && ReadLen==10)
-					{
-						BleAskData.RealLen = ReadLen;
-						BleAskData.DataPtr = malloc(ReadLen);
-						memset(BleAskData.DataPtr,0x00,BleAskData.RealLen);
-						memcpy(BleAskData.DataPtr,AtCmdRead,BleAskData.RealLen);
-						if(xQueueSend(Uart2ToCanDataHandle,&BleAskData,1000*5)!=pdPASS)
-						{
-							if(BleAskData.DataPtr != NULL)
-							{
-								free(BleAskData.DataPtr);
-							}
-							BleAskData.DataPtr = NULL;
-						}
-					}
-				}
-			}
-			break;
-		}
-		default:
-			bleWorkState = 0;
-			break;
-		}
+		BLEStaMan();
+		BLEConnectMan(AtCmdRead, &ReadLen, BleStateFlg);
 	}
 }
 static void AtcmdTransmit(sint8 CmdIdx, uint8 *SetValuePtr, uint16 SetValueLen, uint8 *CmdRead, sint8 *retFunc)
@@ -232,6 +58,7 @@ static void AtcmdTransmit(sint8 CmdIdx, uint8 *SetValuePtr, uint16 SetValueLen,
 	}
 	ATCmd_SendLen = ATCmd_SendLen + SetValueLen;
 	UART_Query_Data(UART_LPUART2, UART_LPUART2, ATCmd_Send, ATCmd_SendLen, CmdRead,&ReadLen, 1000);
+
 	*retFunc = BleAtCmdFunc[CmdIdx].cb(ATCmd_Send, CmdRead, CmdIdx);
 	return;
 }
@@ -242,140 +69,98 @@ static void bleInit(void)
 	uint8 bleInitStep = 0;
 	uint8 BleCurrentName[6];
 	uint16 Readlen = 0;
+	/* 设置为从机 */
+	bleRoleSetFunc(0);
 	while (1)
 	{
 		vTaskDelay(pdMS_TO_TICKS(100));
 		switch (bleInitStep)
 		{
-		case 0: // AT同步
-		{
-			AtcmdTransmit(AT_Cmd_Test, NULL, 0, AtCmdRead, &ret);
-			if (ret == 0)
-			{
-				bleInitStep++;
-			}
-			else
-			{
-				vTaskDelay(pdMS_TO_TICKS(1000));
-				bleInitStep = 0;
-			}
-			break;
-		}
-		case 1: //获取Mac地址
-		{
-			memset(AtCmdRead, 0x00, sizeof(AtCmdRead));
-			AtcmdTransmit(AT_GetMac, NULL, 0, AtCmdRead, &ret);
-			memcpy(BleMacAddr, &AtCmdRead[4], sizeof(BleMacAddr));
-			if (ret == 0)
-			{
-				bleInitStep++;
-			}
-			else
+			case 0: // AT同步
 			{
-				bleInitStep = 0;
+				AtcmdTransmit(AT_Cmd_Test, NULL, 0, AtCmdRead, &ret);
+				if (ret == 0)
+				{
+					bleInitStep++;
+				}
+				else
+				{
+					vTaskDelay(pdMS_TO_TICKS(100));
+					UART_Reset(UART_LPUART2);
+				}
+				break;
 			}
-			break;
-		}
-		case 2: //广播名称设置
-		{
-			uint8 NameChk = 0; //默认不需要设置
-			memset(AtCmdRead, 0x00, sizeof(AtCmdRead));
-			AtcmdTransmit(AT_NameChk, NULL, 0, AtCmdRead, &ret);
-			for (uint8 i = 0; i < 6; i++)
+			case 1: //获取Mac地址
 			{
-				if (mstrlen((char *)AtCmdRead) != 16)
+				memset(AtCmdRead, 0x00, sizeof(AtCmdRead));
+				AtcmdTransmit(AT_GetMac, NULL, 0, AtCmdRead, &ret);
+				memcpy(BleMacAddr, &AtCmdRead[4], sizeof(BleMacAddr));
+				if (ret == 0)
 				{
-					NameChk = 1;
-					break;
+					bleInitStep++;
 				}
 				else
 				{
-					BleCurrentName[i] = (CharToHex(AtCmdRead[(i * 2) + 4]) << 4) + CharToHex(AtCmdRead[(i * 2 + 1) + 4]);
-					if (BleMacAddr[i] != BleCurrentName[i])
+					UART_Reset(UART_LPUART2);
+				}
+				break;
+			}
+			case 2: //广播名称设置
+			{
+				uint8 NameChk = 0; //默认不需要设置
+				memset(AtCmdRead, 0x00, sizeof(AtCmdRead));
+				AtcmdTransmit(AT_NameChk, NULL, 0, AtCmdRead, &ret);
+				for (uint8 i = 0; i < 6; i++)
+				{
+					if (mstrlen((char *)AtCmdRead) != 16)
 					{
 						NameChk = 1;
 						break;
 					}
+					else
+					{
+						BleCurrentName[i] = (CharToHex(AtCmdRead[(i * 2) + 4]) << 4) + CharToHex(AtCmdRead[(i * 2 + 1) + 4]);
+						if (BleMacAddr[i] != BleCurrentName[i])
+						{
+							NameChk = 1;
+							break;
+						}
+					}
 				}
-			}
-			if (NameChk)
-			{
-				uint8 NameTemp[12] = {0};
-				for (uint8 i = 0; i < 6; i++)
+				if (NameChk)
 				{
-					NameTemp[(i * 2)] = HexToChar(BleMacAddr[i] >> 4);
-					NameTemp[(i * 2) + 1] = HexToChar(BleMacAddr[i] & 0x0F);
+					uint8 NameTemp[12] = {0};
+					for (uint8 i = 0; i < 6; i++)
+					{
+						NameTemp[(i * 2)] = HexToChar(BleMacAddr[i] >> 4);
+						NameTemp[(i * 2) + 1] = HexToChar(BleMacAddr[i] & 0x0F);
+					}
+					memset(AtCmdRead, 0x00, sizeof(AtCmdRead));
+					AtcmdTransmit(AT_NameSet, NameTemp, 12, AtCmdRead, &ret);
 				}
-				memset(AtCmdRead, 0x00, sizeof(AtCmdRead));
-				AtcmdTransmit(AT_NameSet, NameTemp, 12, AtCmdRead, &ret);
-			}
-			if (ret == 0)
-			{
-				bleInitStep++;
-			}
-			else
-			{
-				bleInitStep = 0;
-			}
-			break;
-		}
-		case 3: //关闭自动连接
-		{
-			uint8 CmdTemp = '0';
-			memset(AtCmdRead, 0x00, sizeof(AtCmdRead));
-			AtcmdTransmit(AT_AutoConnSet, &CmdTemp, 1, AtCmdRead, &ret);
-			if (ret == 0)
-			{
-				bleInitStep++;
-			}
-			else
-			{
-				bleInitStep = 0;
+				if (ret == 0)
+				{
+					bleInitStep++;
+				}
+				else
+				{
+					UART_Reset(UART_LPUART2);
+				}
+				break;
 			}
-			break;
-		}
-		case 4: //打开log输出
-		{
-			uint8 CmdTemp = '1';
-			memset(AtCmdRead, 0x00, sizeof(AtCmdRead));
-			AtcmdTransmit(AT_LogMsgSet, &CmdTemp, 1, AtCmdRead, &ret);
-			if (ret == 0)
+			case 3:
 			{
-				bleInitStep++;
+				return;
 			}
-			else
-			{
+			default:
 				bleInitStep = 0;
+				break;
 			}
-			break;
-		}
-		case 5: //角色获取
-		{
-			memset(AtCmdRead, 0x00, sizeof(AtCmdRead));
-			AtcmdTransmit(AT_RoleChk, NULL, 0, AtCmdRead, &ret);
-			BleCurrentRole = CharToHex(AtCmdRead[4]);
-			if (ret == 0 && (BleCurrentRole == 0 || BleCurrentRole == 1))
-			{
-				bleInitStep++;
-			}
-			else
-			{
-				bleInitStep = 0;
-			}
-			break;
-		}
-		case 6:
-		{
-			return;
-		}
-		default:
-			bleInitStep = 0;
-			break;
-		}
 	}
 	return;
 }
-static sint8 bleRoleSetFunc(void)
+/* 0-从机设置 1-主机设置 */
+static sint8 bleRoleSetFunc(uint8 SET_ROLE)
 {
 	uint8 AtCmdRead[50];
 	sint8 ret = -1;
@@ -388,68 +173,53 @@ static sint8 bleRoleSetFunc(void)
 		{
 		case 0: // AT同步
 		{
-			//			UartTramsit(NULL, 0, AtCmdRead, 1, 100);
-			UART_Receive_Data(UART_LPUART2, AtCmdRead,&ReadLen, 1000);
-			memset(AtCmdRead, 0x00, sizeof(AtCmdRead));
-			AtcmdTransmit(AT_Cmd_Test, NULL, 0, AtCmdRead, &ret);
-			if (ret == 0)
-			{
-				BleRoleSetStep++;
-			}
-			else
-			{
-				return -1;
+				memset(AtCmdRead, 0x00, sizeof(AtCmdRead));
+				AtcmdTransmit(AT_Cmd_Test, NULL, 0, AtCmdRead, &ret);
+				if (ret == 0)
+				{
+					BleRoleSetStep++;
+				}
+				else
+				{
+					vTaskDelay(pdMS_TO_TICKS(100));
+					UART_Reset(UART_LPUART2);
+				}
+				break;
 			}
-			break;
-		}
-		case 1: //角色检查
-		{
-			memset(AtCmdRead, 0x00, sizeof(AtCmdRead));
-			AtcmdTransmit(AT_RoleChk, NULL, 0, AtCmdRead, &ret);
-			BleCurrentRole = CharToHex(AtCmdRead[4]);
-			if (ret == 0)
+			case 1: //角色设置
 			{
-				if (BleCurrentRole == BleSetRole)
+				memset(AtCmdRead, 0x00, sizeof(AtCmdRead));
+				uint8 roleMode[1] = {0};
+				if(1 == SET_ROLE)
 				{
-					return 0;
+					roleMode[0] = '1';
 				}
 				else
+				{
+					roleMode[0] = '0';
+				}
+				memset(AtCmdRead, 0x00, sizeof(AtCmdRead));
+				AtcmdTransmit(AT_RoleSet, roleMode, 1, AtCmdRead, &ret);
+				if (ret == 0)
 				{
 					BleRoleSetStep++;
 				}
+				else
+				{
+					UART_Reset(UART_LPUART2);
+					BleRoleSetStep = 0;
+				}
+				break;
 			}
-			else
-			{
-				BleRoleSetStep = 0;
-			}
-			break;
-		}
-		case 2: //角色设置
-		{
-			memset(AtCmdRead, 0x00, sizeof(AtCmdRead));
-			uint8 RoleTemp;
-			RoleTemp = HexToChar(BleSetRole);
-			AtcmdTransmit(AT_RoleSet, &RoleTemp, 1, AtCmdRead, &ret);
-			if (ret == 0)
-			{
-				BleRoleSetStep++;
-			}
-			else
+			case 3: //蓝牙模块重启
 			{
-				BleRoleSetStep = 0;
+				memset(AtCmdRead, 0x00, sizeof(AtCmdRead));
+				AtcmdTransmit(AT_Reset, NULL, 0, AtCmdRead, &ret);
+				break;
 			}
-			break;
-		}
-		case 3: //蓝牙模块重启
-		{
-			memset(AtCmdRead, 0x00, sizeof(AtCmdRead));
-			AtcmdTransmit(AT_Reset, NULL, 0, AtCmdRead, &ret);
-			BleRoleSetStep = 0;
-			break;
-		}
-		default:
-			BleRoleSetStep = 0;
-			break;
+			default:
+				return 0;;
+				break;
 		}
 	}
 	return 0;
@@ -459,22 +229,27 @@ sint8 at_callbackFunc(uint8 *PSendStr, uint8 *ptr, BleAtCmd CmdIdx)
 	uint16 pReadLen = 0;
 	sint8 returnvalue = -1;
 	uint8 *retptr = NULL;
-	switch (CmdIdx)
+	if(NULL != strstr((char *)ptr, (char *)("STA:connect")))
 	{
-	case AT_Cmd_Test:
+		/* 防止蓝牙初始化时,被主机连接,设置标志位 */
+		BleStateFlg = Connected;
+	}
+	switch (CmdIdx)
 	{
-		pReadLen = ATstrdel((char *)ptr);
-		retptr = (uint8 *)strstr((char *)ptr, (char *)("OK"));
-		if (retptr && strlen((char *)ptr) < 5)
-		{
-			returnvalue = 0;
-		}
-		else
+		case AT_Cmd_Test:
 		{
-			returnvalue = -1;
+			pReadLen = ATstrdel((char *)ptr);
+			retptr = (uint8 *)strstr((char *)ptr, (char *)("OK"));
+			if (retptr && strlen((char *)ptr) < 5)
+			{
+				returnvalue = 0;
+			}
+			else
+			{
+				returnvalue = -1;
+			}
+			break;
 		}
-		break;
-	}
 	case AT_GetMac:
 	{
 		retptr = (uint8 *)strstr((char *)ptr, (char *)("OK"));
@@ -505,11 +280,6 @@ sint8 at_callbackFunc(uint8 *PSendStr, uint8 *ptr, BleAtCmd CmdIdx)
 	case AT_Conn:
 	{
 		pReadLen = ATstrdel((char *)ptr);
-//		retptr = (uint8 *)strstr((char *)ptr, (char *)("OK"));
-//		if (retptr)
-//		{
-//			returnvalue = 0;
-//		}
 		retptr = (uint8 *)strstr((char *)ptr, (char *)("ERR=-14"));//利用最大连接数错误判断连接成功
 		if(retptr)
 		{
@@ -534,3 +304,242 @@ sint8 at_callbackFunc(uint8 *PSendStr, uint8 *ptr, BleAtCmd CmdIdx)
 	}
 	return returnvalue;
 }
+
+static uint8 bleSendFunc(uint8 *SendDataPtr, uint16 SendDataLen)
+{
+	return UART_Send_Data(UART_LPUART2, (uint8 *)SendDataPtr, SendDataLen, 100);
+}
+/**************************解码部分***********************************/
+static void bleDataDecode(uint8 *dataIn,uint16 dataLen)
+{
+	(void)dataLen;
+	uint8 cmd = 0;
+	uint8 sendBuffer[255];
+	uint16 dataOutLen = 0;
+	static uint16 lockOrUnlockIng = 0; /* 目前等待解锁或者加锁反馈 0 无 LOCK_M 加锁 UNLOCK_M 解锁 */
+	static uint32 awaitTick = 0;
+	uint16 frameLen = 0;
+	for(uint16 i = 0; i < dataLen - 23; i++)
+	{
+		if(0X68 == dataIn[i] && 0X23 == dataIn[i + 1] && 0X24 == dataIn[i + 2])
+		{
+			frameLen = dataIn[i + 3] + (dataIn[i + 4] << 8);
+			if((i + frameLen <= dataLen) && 0X68 == dataIn[frameLen - 1])
+			{
+				cmd  = dataIn[i + 5];
+				EffectiveDataFed = 1;
+				break;
+			}
+
+			else
+			{
+				cmd = 0XFF;
+			}
+
+		}
+	}
+	switch(cmd)
+	{
+		case LOCK_M:
+		{
+			LockSta = 1;
+			break;
+		}
+		case UNLOCK_M:
+		{
+			UnlockSta = 1;
+			break;
+		}
+		case GETINFO_M:
+		{
+			dataOutLen = DecodeSlaveBuffer(sendBuffer, sizeof(sendBuffer), GETINFO_S);
+			bleSendFunc(sendBuffer,dataOutLen);
+			break;
+		}
+		case HEARTBEAT_M:
+		{
+			heartTickLost_B = 0;
+			break;
+		}
+		default:break;
+	}
+	if((0 != lockOrUnlockIng) &&
+			((xTaskGetTickCount() - awaitTick >= pdMS_TO_TICKS(20000)) ||
+			((LOCK_S == lockOrUnlockIng && 0 == SolValActuSt) ||
+			(UNLOCK_S == lockOrUnlockIng && 1 == SolValActuSt))))
+	{
+		dataOutLen = DecodeSlaveBuffer(sendBuffer, sizeof(sendBuffer), lockOrUnlockIng);
+		bleSendFunc(sendBuffer,dataOutLen);
+		if(LOCK_S == lockOrUnlockIng)
+			UnlockSta = 0;
+		else if(UNLOCK_S == lockOrUnlockIng)
+			LockSta = 0;
+		else {}
+		lockOrUnlockIng = 0;
+	}
+}
+
+static uint8 bleDisconnect(void)
+{
+	uint8 AtCmdRead[50];
+	sint8 ret = -1;
+	uint8 BleRoleSetStep = 0;
+	//蓝牙断开指令,Mod拉低,才能发送命令
+	/*S32K*/
+	Dio_WriteChannel(DioConf_DioChannel_PTE7_GPIO_OUT_MCU_BT_MOD, STD_OFF);
+	uint32 clickCount = xTaskGetTickCount();
+	EffectiveDataFed = 0;
+	while (1)
+	{
+		vTaskDelay(pdMS_TO_TICKS(100));
+		if(xTaskGetTickCount() - clickCount >= pdMS_TO_TICKS(5000))
+		{
+			return -1;
+		}
+		switch (BleRoleSetStep)
+		{
+			case 0: // AT同步
+			{
+				UART_Reset(UART_LPUART2);
+				memset(AtCmdRead, 0x00, sizeof(AtCmdRead));
+				AtcmdTransmit(AT_Cmd_Test, NULL, 0, AtCmdRead, &ret);
+				if (ret == 0)
+				{
+					BleRoleSetStep++;
+				}
+				else
+				{
+					vTaskDelay(pdMS_TO_TICKS(100));
+				}
+				break;
+			}
+			case 1: //断开连接
+			{
+				memset(AtCmdRead, 0x00, sizeof(AtCmdRead));
+				AtcmdTransmit(AT_DisCon, NULL, 0, AtCmdRead, &ret);
+				if (ret == 0)
+				{
+					BleRoleSetStep++;
+				}
+				else
+				{
+					UART_Reset(UART_LPUART2);
+					BleRoleSetStep = 0;
+				}
+				break;
+			}
+			case 2: //蓝牙模块重启
+			{
+				memset(AtCmdRead, 0x00, sizeof(AtCmdRead));
+				AtcmdTransmit(AT_Reset, NULL, 0, AtCmdRead, &ret);
+				/*S32K*/
+				Dio_WriteChannel(DioConf_DioChannel_PTE7_GPIO_OUT_MCU_BT_MOD, STD_ON);
+				return 0;
+				break;
+			}
+			default:
+				return 0;
+				break;
+		}
+	}
+	return 0;
+}
+
+static void BLEStaMan(void)
+{
+	if(1 == BlePwrControl && Shutdown != BleStateFlg) /* 关机 */
+	{
+		bleDisconnect();
+		Dio_WriteChannel(DioConf_DioChannel_PTE9_GPIO_OUT_MCU_BT_POW_EN,STD_OFF);
+		BleStateFlg = Shutdown;
+		//
+	}
+	else if(2 == BlePwrControl && Shutdown == BleStateFlg) /* 开机 */
+	{
+		Dio_WriteChannel(DioConf_DioChannel_PTE7_GPIO_OUT_MCU_BT_MOD, STD_ON);
+		bleInit();
+		BleStateFlg = NoConnect;
+	}
+	if(1 == BleConnectControl && 1 == BleStateFlg) //断开连接
+	{
+		if(0 == bleDisconnect())
+		{
+			BleStateFlg = NoConnect;
+		}
+	}
+}
+
+static void BLEConnectMan(uint8* AtCmdRead, uint16* pReadLen, BleState BleStateFlg)
+{
+	switch (BleStateFlg)
+	{
+		case NoConnect: //等待连接或者主动连接
+		{
+			UART_Receive_Data(UART_LPUART2, (uint8 *)AtCmdRead, pReadLen, 100);
+			if (*pReadLen > 0)//未连接状态下,蓝牙串口处会发送连接状态以及扫描的内容
+			{
+				if (0 != strstr((char *)AtCmdRead, (char *)("STA:connect")))
+				{
+					BleStateFlg = Connected;  /* 已连接 */
+				}
+			}
+			break;
+		}
+		case Connected: /* 已连接 */
+		{
+			if (2 == BleConnectControl)//主从机均可断开连接
+			{
+				bleDisconnect();
+				BleStateFlg = NoConnect; /* 已经断开连接 */
+			}
+			/*
+			 * 已连接状态下的数据交换在此进行
+			*/
+			UART_Receive_Data(UART_LPUART2, AtCmdRead,pReadLen, 100);
+			if (*pReadLen > 0)//连接的状态下,蓝牙串口会发送断开状态以及发送的内容
+			{
+				if((uint8 *)strstr((char *)AtCmdRead, (char *)("STA:disconnec")))
+				{
+					BleStateFlg = NoConnect;
+				}
+				else
+				{
+					bleDataDecode(AtCmdRead,*pReadLen);
+				}
+
+			}
+			/* 心跳报文发送 */
+			static uint64 tickcount = 0;
+			if(xTaskGetTickCount() - tickcount > pdMS_TO_TICKS(2000))
+			{
+				tickcount = xTaskGetTickCount();
+				uint16 dataOutLen = 0;
+				uint8 dataOutBuffer[128] = {0};
+				/* 组包 */
+				dataOutLen = DecodeSlaveBuffer(dataOutBuffer, sizeof(dataOutBuffer),  HEARTBEAT_S);
+				/* 数据发送 */
+				bleSendFunc(dataOutBuffer,dataOutLen);
+				heartTickLost_B++;
+			}
+			if(heartTickLost_B > MAX_LOSTHEART)
+			{
+				if(0 == bleDisconnect())
+				{
+					BleStateFlg = NoConnect;
+					heartTickLost_B = 0;
+				}
+			}
+
+			break;
+		}
+		case Shutdown:
+		{
+			break;
+		}
+		default:
+		{
+			BleStateFlg = 0;
+			break;
+		}
+	}
+}

+ 12 - 1
src/AppTaskBle.h

@@ -19,6 +19,9 @@
 #include "hal_adapter.h"
 #include "AppGlobalVar.h"
 #include "AppFuncLib.h"
+#include "WirelessDecodeSlave.h"
+#define BLE_DEBUG_PRINT 1
+
 typedef enum
 {
     AT_Cmd_Test = 0,
@@ -31,9 +34,17 @@ typedef enum
     AT_RoleSet,
     AT_Conn,
     AT_DisCon,
-    AT_Reset
+    AT_Reset,
+	AT_QuitCSend
 } BleAtCmd;
 
+typedef enum
+{
+	NoConnect,
+	Connected,
+	Shutdown,
+}BleState;
+
 typedef sint8 (*pFunc)(uint8 *PSendStr, uint8 *ptr, BleAtCmd CmdIdx);
 sint8 at_callbackFunc(uint8 *PSendStr, uint8 *ptr, BleAtCmd CmdIdx);
 

+ 232 - 310
src/AppTaskCan.c

@@ -5,10 +5,13 @@
  *      Author: QiXiang_CHENJIE
  */
 #include "AppTaskCan.h"
+static void CanDataDecode(uint32 DataCanID, uint8 CanData[]);
 static void CanDataAsk(QueueDataTrans_Type AskData);
+static void CanCirSendMess(void);
+static void GropPack_CANID(uint8 DataPtr[], uint32 CAN_ID);
+
 uint8 DataPackCounter[21] = {1,1,7,5,16,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
-static void UdsAns(bool PosFlg,uint8 UdsService,uint8 UdsSubService,uint8 Idx,uint8 *AnsData,uint8 AnsDataLen);
-void udsService(uint8 *DataIn);
+
 static void vCanRxCallback(TimerHandle_t pxTimer)
 {
 	uint32 ulTimerID;
@@ -48,374 +51,293 @@ void CanTask(void *data)
 			case 1:
 			{
 				memset(&CanRxMsg, 0, sizeof(CanRxMsg));
-	//			Can_MainFunction_Read();
 				BaseType_t ret = xQueueReceive(CanRecvQueueHandle, &CanRxMsg, 0);
-				if (ret == pdPASS)
+				/* 处理来自Can总线的消息 */
+				if(pdPASS == ret)
 				{
-					BleCanTransmit(CanRxMsg.id, CanRxMsg.data);
+					CanDataDecode(CanRxMsg.id, CanRxMsg.data);
 				}
+				/* 接收处理来自串口线程的消息 */
 				BaseType_t ret2 = xQueueReceive(Uart2ToCanDataHandle,&CanAskData,10);//带有校验码
 				if (ret2 == pdPASS)
 				{
 					CanDataAsk(CanAskData);
 				}
-//				vTaskDelay(pdMS_TO_TICKS(10));
+				/* 循环发送的数据 */
+				CanCirSendMess();
 				break;
 			}
 		}
 	}
 }
-static void CanDataAsk(QueueDataTrans_Type AskData)
+static void CanDataDecode(uint32 DataCanID, uint8 CanData[])
 {
-	Can_Msg_Type CanTxMsg;
-	uint16 CrcGet = 0x00;
-	uint16 CrcCal = 0xff;
-	if(AskData.RealLen==10)
-	{
-		CrcGet = (*(AskData.DataPtr + AskData.RealLen - 2)<<0 )| (*(AskData.DataPtr + AskData.RealLen - 1)<<8 );
-		CrcCal = CRC16_Modbus(AskData.DataPtr,AskData.RealLen-2);
-		if(CrcGet==CrcCal)
-		{
-			CanTxMsg.id = 0X1CEF0000;
-			CanTxMsg.idFrame = CAN_EXTENDED_ID_TYPE;
-			CanTxMsg.sdu = AskData.DataPtr;
-			CanTxMsg.length = 8;
-			CanIf_SendMessage(0, CanTxMsg);
-		}
-	}
-	else if(AskData.RealLen==1)//从机连接与断开,主动给EBC发送一帧状态
-	{
-		CanTxMsg.id = 0X1CEF0002;
-		CanTxMsg.idFrame = CAN_EXTENDED_ID_TYPE;
-		CanTxMsg.sdu = AskData.DataPtr;
-		CanTxMsg.length = 8;
-		CanIf_SendMessage(0, CanTxMsg);
-	}
 
-	if(AskData.DataPtr)
+	if(IsJumptoBootloader(DataCanID, CanData))
 	{
-		free(AskData.DataPtr);
+		/* 升级 */
+		DoRoutinePositiveAnswer();
+		RequestEnterBootloader();
+		DoResetECU();
+		return;
 	}
-	AskData.DataPtr = NULL;
-}
-void BleCanTransmit(uint32 AskCanID, uint8 AskData[])
-{
-	Can_Msg_Type CanTxMsg;
-	uint32 AnsCanID = 0;
-	uint8 AnsData[8];
-	memset(AnsData, 0x00, 8);
-	static QueueDataTrans_Type CanAnsData={0,NULL};
-	static uint8 CanDataCounter = 0;//数据打包需要的包数
-	static uint8 DataPackDone = 0;//已打包数目
-	static uint8 CanByte0Idx = 0;
-	static volatile uint8 PackState = 0;//0-未打包,1-正在打包,2-打包完成
-	uint8 CanAnsPerLen = 7;
-	uint8 CanByte0Seq = 0;
-	switch (AskCanID)
-	{
-	case 0X1CEE0000: //数据通道,换电控制的数据发过来在此打包后转uart2
+
+	switch (DataCanID)
 	{
-		if(PackState==0)
+		case 0x1C00EBEC:
 		{
-			DataPackDone = 0;
-			CanDataCounter = DataPackCounter[(AskData[0]>>4)];
-			CanAnsData.RealLen = CanDataCounter*CanAnsPerLen + 2;
-			CanAnsData.DataPtr = malloc(CanAnsData.RealLen);
-			memset(CanAnsData.DataPtr,0x00,CanAnsData.RealLen);
-			CanByte0Idx = (AskData[0]>>4);
-			PackState = 1;
+			/* 连接蓝牙,断开蓝牙 从机状态暂时只考虑主动断开*/
+			BleConnectControl = CanData[2];
+			BlePwrControl = CanData[3];
+			WiFiConnectControl = CanData[4];
+			WiFiPwrControl = CanData[5];
+			break;
 		}
-		if(PackState==1)
+		case 0x1C01EBEC:
 		{
-			if(CanByte0Idx==(AskData[0]>>4))
-			{
-				CanByte0Seq = (AskData[0])&0x0F;
-				if(CanByte0Idx==4)
-				{
-					CanByte0Seq = CanByte0Seq + 1;
-				}
-				if(DataPackDone<CanDataCounter)
-				{
-					memcpy(CanAnsData.DataPtr+(CanByte0Seq - 1)*CanAnsPerLen,AskData,CanAnsPerLen);//从CAN信息中只截取前7个数据
-					DataPackDone++;
-				}
-				if(DataPackDone==CanDataCounter)
-				{
-					CanAnsData.RealLen = DataPackDone*CanAnsPerLen + 2;
-					PackState = 2;
-				}
-			}
-			else//序号不对,打包立刻完成,更改数据长度
+			EBC_State          = CanData[0];
+			EBC_ExchangeMode   = CanData[1];
+			EBC_LockMode       = CanData[2];
+			EBC_ExchangeKind   = CanData[3];
+			EBC_AExchangeCheck = CanData[4];
+			break;
+		}
+		case 0x1C02EBEC:
+		{
+			UnlockCheck        = CanData[0];
+			LockCheck          = CanData[1];
+			BatPlace   	       = CanData[2];
+			UnlockSensorSignal = CanData[3];
+			LockSensorSignal   = CanData[4];
+			BaseSensorSignal   = CanData[5];
+			InterlockSignal    = CanData[6];
+			SolValActuSt       = CanData[7];
+			break;
+		}
+		case 0x1C12EBEC:
+		{
+			AirPrValue         = CanData[0];
+			TheManFluid        = CanData[1];
+			break;
+		}
+		case 0x1C03EBEC:
+		{
+			WholeVehComState   = CanData[0];
+			KeySignal          = CanData[1];
+			VehicleState   	   = CanData[2];
+			BrakeSignal        = CanData[3];
+			GearSignal         = CanData[4];
+			TurnSignal         = CanData[5];
+			FrontAxleValue     = CanData[6];
+			RearAxleValue	   = CanData[7];
+			break;
+		}
+		case 0x1C04EBEC:
+		{
+			BatComState        = CanData[0];
+			BatBMS_State       = CanData[1];
+			MContactorsState   = CanData[2];
+			SOC                = CanData[3];
+			BatValtage         = CanData[4];
+			SOH                = CanData[5];
+			break;
+		}
+		case 0x1C05EBEC:
+		{
+			ErrorLevel         = CanData[0];
+			ErrorCode          = CanData[1];
+			break;
+		}
+		case 0x1C06EBEC:
+		{
+			/* CAN ID顺序从0开始1 */
+			uint8 CanIndex = 1;
+			for(uint8 i = CanData[0] * 7; CanIndex < 8  && i < 26; i++)
 			{
-				CanAnsData.RealLen = DataPackDone*CanAnsPerLen + 2;
-				PackState = 2;
+				BatSN[i] = CanData[CanIndex++];
 			}
+			break;
 		}
-		if(PackState==2)
+		case 0x1C07EBEC:
 		{
-			uint16 CrcCal = 0;
-			CrcCal = CRC16_Modbus(CanAnsData.DataPtr,CanAnsData.RealLen-2);
-			*(CanAnsData.DataPtr + CanAnsData.RealLen-2) = CrcCal &0xFF;
-			*(CanAnsData.DataPtr + CanAnsData.RealLen-1) = (CrcCal>>8) &0xFF;
-			if(xQueueSend(CanToUart2DataHandle,&CanAnsData,1000*10)!=pdPASS)
+			/* CAN ID顺序从0开始1 */
+			uint8 CANDATA_Viwer = 1;
+			for(uint8 i = CanData[0] * 7; CANDATA_Viwer < 8  && i < 17; i++)
 			{
-				if(CanAnsData.DataPtr)
-				{
-					free(CanAnsData.DataPtr);
-				}
-				CanAnsData.DataPtr = NULL;
-				CanAnsData.RealLen = 0;
+				VehicleVin[i] = CanData[CANDATA_Viwer];
+				CANDATA_Viwer++;
 			}
-			PackState = 0;
+			break;
 		}
-		CanTxMsg.id = AnsCanID;
-		CanTxMsg.idFrame = CAN_EXTENDED_ID_TYPE;
-		CanTxMsg.sdu = AnsData;
-		CanTxMsg.length = 8;
-		CanIf_SendMessage(0, CanTxMsg);
-		return;
-	}
-	case 0X1CEE0001: //查询
-	{
-		if (AskData[0] == 1) //软件版本查询
+		case 0x1C08EBEC:
 		{
-			AnsData[0] = 0x01;
-			AnsData[1] = (BleVersion >> 24) & 0xFF;
-			AnsData[2] = (BleVersion >> 16) & 0xFF;
-			AnsData[3] = (BleVersion >> 8) & 0xFF;
-			AnsData[4] = (BleVersion >> 0) & 0xFF;
+			AccruedChargeAh = *(uint32 *)CanData;
+			AccruedChargeKwh = *((uint32*)CanData + 1);
+			break;
 		}
-		else if (AskData[0] == 2) // Mac地址查询
+		case 0x1C09EBEC:
 		{
-			AnsData[0] = 0x02;
-			AnsData[1] = (BleMacAddr[0]) & 0xFF;
-			AnsData[2] = (BleMacAddr[1]) & 0xFF;
-			AnsData[3] = (BleMacAddr[2]) & 0xFF;
-			AnsData[4] = (BleMacAddr[3]) & 0xFF;
-			AnsData[5] = (BleMacAddr[4]) & 0xFF;
-			AnsData[6] = (BleMacAddr[5]) & 0xFF;
+			AccruedDischargeAh = *(uint32 *)CanData;
+			AccruedDischargeKwh = *((uint32*)CanData + 1);
+			break;
 		}
-		AnsCanID = 0X1CEF0001;
-		CanTxMsg.id = AnsCanID;
-		CanTxMsg.idFrame = CAN_EXTENDED_ID_TYPE;
-		CanTxMsg.sdu = AnsData;
-		CanTxMsg.length = 8;
-		CanIf_SendMessage(0, CanTxMsg);
-		break;
-	}
-	case 0X1CEE0002: //连接控制
-	{
-		if (AskData[0] == 1) //连接状态查询
+		case 0x1C0AEBEC:
 		{
-			AnsData[0] = 0x01;
-			AnsData[1] = (BleDisconnectFlg<<1)|(BleConnectFlg);
+			SingleChargeAh  = *(uint16 *)CanData;    	/* 单次充电Ah数 Ah 0.1 */
+			SingleChargeKwh = *((uint16*)CanData + 1);   	/* 单次充电kwh数 kwh 0.1 */
+			AccruedMileage  = *((uint32*)CanData + 1); 	  	/* 累计行驶里程 km 0.1 */
+			break;
 		}
-		else if (AskData[0] == 2) //连接指定Mac地址
+		case 0x1C0BEBEC:
 		{
-			BleTargetMacAddr[0] = AskData[1];
-			BleTargetMacAddr[1] = AskData[2];
-			BleTargetMacAddr[2] = AskData[3];
-			BleTargetMacAddr[3] = AskData[4];
-			BleTargetMacAddr[4] = AskData[5];
-			BleTargetMacAddr[5] = AskData[6];
-			BleConnectEnable = 1;
-			AnsData[0] = 2;
-			AnsData[1] = 2;
+			SS_AccruedChargeAh  = *((uint32*)CanData);	/* 累计换电站充入Ah数 Ah 0.1 */
+			SS_AccruedChargeKwh = *((uint32*)CanData + 1);	/* 累计换电站充入kwh数 kwh 0.1 */
+			break;
 		}
-		else if (AskData[0] == 3) //断开连接
+		case 0x1C0CEBEC:
 		{
-			BleDisconnectEnable = 1;
-			AnsData[0] = 3;
-			AnsData[1] = 3;
+			IG_AccruedChargeAh  = *((uint32*)CanData); 	/* 累计插枪充入Ah数 Ah 0.1 */
+			IG_AccruedChargeKwh = *((uint32*)CanData + 1); /* 累计插枪充入kwh数 kwh 0.1 */
+			break;
 		}
-		AnsCanID = 0X1CEF0002;
-		CanTxMsg.id = AnsCanID;
-		CanTxMsg.idFrame = CAN_EXTENDED_ID_TYPE;
-		CanTxMsg.sdu = AnsData;
-		CanTxMsg.length = 8;
-		CanIf_SendMessage(0, CanTxMsg);
-		break;
-	}
-	case 0X1CEE0003: //角色控制
-	{
-		if (AskData[0] == 1) //当前角色查询
+		case 0x1C0DEBEC:
 		{
-			AnsData[0] = 0x01;
-			AnsData[1] = BleCurrentRole;
+			AFE_AccruedChargeAh  = *((uint32*)CanData); /* 累计动能回馈Ah数 */
+			AFE_AccruedChargeKwh = *((uint32*)CanData + 1);/* 累计动能回馈kwh数 */
+			break;
 		}
-		else if (AskData[0] == 2) //角色设置
+		case 0x1C0EEBEC:
 		{
-			BleSetRoleEnable = 1;
-			BleSetRole = AskData[1] & 0x03;
-			AnsData[0] = 0x02;
-			AnsData[1] = BleSetRole;
+			ExchangeNum    = *((uint16*)CanData); 		/* 换电次数 */
+			ExchangeNumSuc = *((uint16*)CanData + 1); 		/* 换电成功次数 */
+			break;
 		}
-		AnsCanID = 0X1CEF0003;
-		CanTxMsg.id = AnsCanID;
-		CanTxMsg.idFrame = CAN_EXTENDED_ID_TYPE;
-		CanTxMsg.sdu = AnsData;
-		CanTxMsg.length = 8;
-		CanIf_SendMessage(0, CanTxMsg);
-		break;
+		case 0x1C0FEBEC:
+		{
+			BatteryCurrent = *((uint16*)CanData);;      /* 电池电流 */
+			BatRateRnergy  = *((uint16*)CanData + 1);      /* 电池额定能量 */
+			break;
+		}
+		default:
+			break;
+
 	}
-	case 0X1CEE0004: //蓝牙开关机控制
+}
+static void CanDataAsk(QueueDataTrans_Type AskData)
+{
+	Can_Msg_Type CanTxMsg;
+	uint16 CrcGet = 0x00;
+	uint16 CrcCal = 0xff;
+	if(AskData.RealLen==10)
 	{
-		if (AskData[0] == 1)
+		CrcGet = (*(AskData.DataPtr + AskData.RealLen - 2)<<0 )| (*(AskData.DataPtr + AskData.RealLen - 1)<<8 );
+		CrcCal = CRC16_Modbus(AskData.DataPtr,AskData.RealLen-2);
+		if(CrcGet==CrcCal)
 		{
-			if (AskData[1] == 0) //开机
-			{
-				BlePwrControlEnable = 1;
-				BlePwrControlFlg = 0;
-			}
-			else if (AskData[1] == 1) //关机
-			{
-				BlePwrControlEnable = 1;
-				BlePwrControlFlg = 1;
-			}
-			else if (AskData[1] == 2) //重启
-			{
-				BlePwrControlEnable = 1;
-				BlePwrControlFlg = 2;
-			}
-			AnsData[0] = 1;
-			AnsData[1] = AskData[1];
+			CanTxMsg.id = 0X1CEF0000;
+			CanTxMsg.idFrame = CAN_EXTENDED_ID_TYPE;
+			CanTxMsg.sdu = AskData.DataPtr;
+			CanTxMsg.length = 8;
+			CanIf_SendMessage(0, CanTxMsg);
 		}
-		AnsCanID = 0X1CEF0004;
-		CanTxMsg.id = AnsCanID;
+	}
+	else if(1 == AskData.RealLen)//从机连接与断开,主动给EBC发送一帧状态
+	{
+		CanTxMsg.id = 0X1CEF0002;
 		CanTxMsg.idFrame = CAN_EXTENDED_ID_TYPE;
-		CanTxMsg.sdu = AnsData;
+		CanTxMsg.sdu = AskData.DataPtr;
 		CanTxMsg.length = 8;
 		CanIf_SendMessage(0, CanTxMsg);
-		break;
 	}
-	case 0x1C00EBEA:
+
+	if(NULL != AskData.DataPtr)
 	{
-		udsService(AskData);
-		break;
-	}
-	default:
-		return;
-		break;
+		free(AskData.DataPtr);
 	}
-	return;
+	AskData.DataPtr = NULL;
 }
-void udsService(uint8 *DataIn)
+/* CAN循环发送数据 */
+static void CanCirSendMess(void)
 {
-	static uint8 udsMode = 0;
-	uint8 UdsService = *(DataIn + 0);
-	uint8 UdsSubService = *(DataIn + 1);
-	uint8 AnsData[8]={0};
-	switch(UdsService)
-	{
-	case 0x10:
+	static uint32 can_last_tickcount00_100ms = 0;
+
+	if(xTaskGetTickCount() - can_last_tickcount00_100ms >= pdMS_TO_TICKS(100))
 	{
-		if(UdsSubService==0x01)
-		{
-			udsMode = 1;
-			UdsAns(true,UdsService,UdsSubService,0x00,NULL,0);
-		}
-		else if(*(DataIn+1)==0x02)
-		{
-			if(udsMode==1||udsMode==2)
-			{
-				udsMode = 2;
-				UdsAns(true,UdsService,UdsSubService,0x00,NULL,0);
-			}
-			else
-			{
-				udsMode = 0;
-				UdsAns(false,UdsService,UdsSubService,0x00,NULL,0);
-			}
-		}
-		else
-		{
-			udsMode = 0;
-			UdsAns(false,UdsService,UdsSubService,0x00,NULL,0);
-		}
-		break;
+
+		can_last_tickcount00_100ms = xTaskGetTickCount();
+		Can_Msg_Type CanTxMsg;
+		uint8 DataPtr[8] = {0};
+
+		CanTxMsg.idFrame = CAN_EXTENDED_ID_TYPE;
+		CanTxMsg.sdu = DataPtr;
+		CanTxMsg.length = 8;
+
+		GropPack_CANID(DataPtr, 0x1C00ECEB);
+		CanTxMsg.id = 0x1C00ECEB;
+		CanIf_SendMessage(0, CanTxMsg);
+
+		GropPack_CANID(DataPtr, 0x1C01ECEB);
+		CanTxMsg.id = 0x1C01ECEB;
+		CanIf_SendMessage(0, CanTxMsg);
+
 	}
-	case 0x22:
+
+}
+static void GropPack_CANID(uint8 DataPtr[], uint32 CAN_ID)
+{
+	static uint8 LifeSignal00 = 0;
+	static uint8 LifeSignal01 = 0;
+	switch(CAN_ID)
 	{
-		if(udsMode==2)
-		{
-			switch(UdsSubService)
-			{
-			case 0x01://软件版本查询
-			{
-				AnsData[0] = (BleVersion >> 24) & 0xFF;
-				AnsData[1] = (BleVersion >> 16) & 0xFF;
-				AnsData[2] = (BleVersion >> 8) & 0xFF;
-				AnsData[3] = (BleVersion >> 0) & 0xFF;
-				UdsAns(true,UdsService,UdsSubService,0x00,AnsData,4);
-				break;
-			}
-			case 0x02://MAC地址查询
-			{
-				memcpy(AnsData,BleMacAddr,6);
-				for(uint8 i=0;i<2;i++)
-				{
-					UdsAns(true,UdsService,UdsSubService,i,&AnsData[i*4],4);
-				}
-				break;
-			}
-			case 0x03://角色查询
-			{
-				AnsData[0] = (BleCurrentRole) & 0xFF;
-				UdsAns(true,UdsService,UdsSubService,0x00,AnsData,1);
-				break;
-			}
-			case 0x04://连接查询
+		case 0x1C00ECEB:
+			DataPtr[0] = 0;
+			DataPtr[1] = LifeSignal00++;
+			if(LifeSignal00 > 14)
+				LifeSignal00 = 0;
+			if(1 == BleStateFlg)
 			{
-				AnsData[0] = (BleConnectFlg) & 0xFF;
-				UdsAns(true,UdsService,UdsSubService,0x00,AnsData,1);
-				break;
+				BLEConFed = 1;
 			}
-			default:
+			else
 			{
-				UdsAns(false,UdsService,UdsSubService,0x00,NULL,0);
-				break;
-			}
+				BLEConFed = 2;
 			}
-		}
-		else
-		{
-			UdsAns(false,UdsService,UdsSubService,0x00,NULL,0);
-		}
-		break;
-	}
-	default:
-	{
-		UdsAns(false,0x00,0x00,0x00,NULL,0);
-		break;
-	}
-	}
-}
-static void UdsAns(bool PosFlg,uint8 UdsService,uint8 UdsSubService,uint8 Idx,uint8 *AnsData,uint8 AnsDataLen)
-{
-	Can_Msg_Type CanTxMsg;
-	uint32 CanAnsId = 0x1C00EAEB;
-	uint8 SendData[8] = {0};
-	if(PosFlg)
-	{
-		SendData[0] = 0x78;
-		SendData[1] = UdsService;
-		SendData[2] = UdsSubService;
-		SendData[3] = Idx;
-		if(AnsData!=NULL&&AnsDataLen!=0x00)
-		{
-			memcpy(&SendData[4],AnsData,AnsDataLen);
-		}
-	}
-	else
-	{
-		SendData[0] = 0x7F;
-		SendData[1] = UdsService;
-		SendData[2] = UdsSubService;
-		SendData[3] = 0x00;
+			DataPtr[2] = BLEConFed;
+
+			if(2 == BleStateFlg)
+				BLEPWRFed = 1;
+			else
+				BLEPWRFed = 2;
+			DataPtr[3] = BLEPWRFed;
+
+			if(1 == tcp_server_process_status)
+				WiFiConFed = 1;
+			else
+				WiFiConFed = 2;
+			DataPtr[4] = WiFiConFed;
+
+			if(2 == tcp_server_process_status)
+				WiFiPWRFed = 1;
+			else
+				WiFiPWRFed = 2;
+			DataPtr[5] = WiFiPWRFed;
+
+			DataPtr[6] = EffectiveDataFed;
+
+			break;
+		case 0x1C01ECEB:
+			DataPtr[0] = 0;
+			DataPtr[1] = LifeSignal01++;
+			if(LifeSignal01 > 14)
+				LifeSignal01 = 0;
+			DataPtr[2] = EmeStopSta;
+			DataPtr[3] = LockSta;
+			DataPtr[4] = UnlockSta;
+			DataPtr[5] = AutoLockSta;
+			DataPtr[6] = AutoUnlockSta;
+			DataPtr[7] = StateSwitch;
+			break;
+		default:
+			break;
 	}
-	CanTxMsg.id = CanAnsId;
-	CanTxMsg.idFrame = CAN_EXTENDED_ID_TYPE;
-	CanTxMsg.sdu = SendData;
-	CanTxMsg.length = 8;
-	CanIf_SendMessage(0, CanTxMsg);
 }

+ 2 - 0
src/AppTaskCan.h

@@ -10,6 +10,8 @@
 #include "hal_adapter.h"
 #include "AppGlobalVar.h"
 #include "AppFuncLib.h"
+#include "UDSTask.h"
+#include "Hal_Wdg.h"
 void CanTask(void *data);
 void BleCanTransmit(uint32 AskCanID, uint8 AskData[]);
 #endif /* APPTASKCAN_H_ */

+ 1 - 543
src/AppTaskUart0.c

@@ -51,7 +51,7 @@ void UartTask(void *pvParameters)
 	for (;;)
 	{
 		memset(AtCmdRead,0x00,sizeof(AtCmdRead));
-		RecvRet = UART_Receive_Data(UART_LPUART0, (uint8*)AtCmdRead,(uint8*)&ReadLen, 100);
+		RecvRet = UART_Receive_Data(UART_LPUART0, (uint8*)AtCmdRead,(uint16*)&ReadLen, 100);
 		if(!RecvRet)
 		{
 			AtCmdAnsLen = 0;
@@ -87,549 +87,7 @@ sint8 McuAtCallbackFunc(char *Readptr, uint16 ReadLen, McuAtCmd McuAtIdx, char *
 	char *OnAns = "ON\r\n";
 	char *SlaveAns = "S";
 	char *MasterAns = "M";
-	switch (McuAtIdx)
-	{
-		case MAT:
-		{
-			*AnsPtrLen = strlen(SucAns);
-			memcpy(AnsPtr, SucAns, *AnsPtrLen);
-			break;
-		}
-		case MAT_Version:
-		{
-			uint8 chartemp;
-			memcpy(AnsPtr, SucAns, strlen(SucAns));
-			*AnsPtrLen = strlen(SucAns);
-			for (uint8 i = 0; i < sizeof(BleVersion) * 2; i++)
-			{
-				chartemp = HexToChar(BleVersion >> (28 - 4 * i));
-				memcpy(AnsPtr + *AnsPtrLen + i, &chartemp, 1);
-			}
-
-			*AnsPtrLen = sizeof(BleVersion) * 2 + *AnsPtrLen;
-			break;
-		}
-		case MAT_Connect:
-		{
-			if (ReadLen == (strlen((McuAtCmdFunc[McuAtIdx].str)) + 12))
-			{
-				if (BleConnectFlg)
-				{
-					memcpy(AnsPtr, SucAns, strlen(SucAns));
-					*AnsPtrLen = strlen(SucAns);
-					memcpy(AnsPtr + *AnsPtrLen, OnAns, strlen(OnAns));
-					*AnsPtrLen = *AnsPtrLen + strlen(OnAns);
-				}
-				else
-				{
-					if (BleCurrentRole == 1)
-					{
-						for(uint8 i=0;i<6;i++)
-						{
-							uint8 temp1 = 0x00;
-							uint8 temp2 = 0x00;
-							temp1 = (CharToHex(*(Readptr + i*2 + strlen(McuAtCmdFunc[McuAtIdx].str)))<<4);
-							temp2 = CharToHex(*(Readptr + i*2 + 1 + strlen(McuAtCmdFunc[McuAtIdx].str)));
-							BleTargetMacAddr[i] = temp1|temp2;
-						}
-						BleConnectEnable = 1;
-						if( xSemaphoreTake( ConnSemaphore, 5000 ) == pdTRUE )
-						{
-							memcpy(AnsPtr, SucAns, strlen(SucAns));
-							*AnsPtrLen = strlen(SucAns);
-							memcpy(AnsPtr + *AnsPtrLen, (char *)("SUC\n"), strlen((char *)("SUC\n")));
-							*AnsPtrLen = *AnsPtrLen + strlen((char *)("SUC\n"));
-						}
-						else
-						{
-							memcpy(AnsPtr, ErrAns, strlen(ErrAns));
-							*AnsPtrLen = strlen(ErrAns);
-							memcpy(AnsPtr + *AnsPtrLen, (char *)("-1\n"), strlen((char *)("-1\n")));
-							*AnsPtrLen = *AnsPtrLen + strlen((char *)("-1\n"));
-						}
-					}
-					else
-					{
-						*AnsPtrLen = strlen((char *)ErrAns);
-						memcpy(AnsPtr, ErrAns, *AnsPtrLen);
-					}
-				}
-			}
-			else if (*(Readptr + strlen(McuAtCmdFunc[McuAtIdx].str)) == '?')
-			{
-				if (BleConnectFlg)
-				{
-					if (BleCurrentRole == 1)
-					{
-
-						memcpy(AnsPtr, SucAns, strlen(SucAns));
-						*AnsPtrLen = strlen(SucAns);
-						memcpy(AnsPtr + *AnsPtrLen, OnAns, strlen(OnAns));
-						*AnsPtrLen = *AnsPtrLen + strlen(OnAns);
-						for(uint8 i=0;i<6;i++)
-						{
-							*(AnsPtr + *AnsPtrLen + i*2) = HexToChar((BleTargetMacAddr[i]>>4)&0x0F);
-							*(AnsPtr + *AnsPtrLen + i*2 + 1) = HexToChar(BleTargetMacAddr[i]&0x0F);
-						}
-						*AnsPtrLen = *AnsPtrLen + sizeof(BleTargetMacAddr)*2;
-						break;
-					}
-					else
-					{
-						memcpy(AnsPtr, SucAns, strlen(SucAns));
-						*AnsPtrLen = strlen(SucAns);
-						memcpy(AnsPtr + *AnsPtrLen, OnAns, strlen(OnAns));
-						*AnsPtrLen = *AnsPtrLen + strlen(OnAns);
-					}
-				}
-				else
-				{
-					memcpy(AnsPtr, SucAns, strlen(SucAns));
-					*AnsPtrLen = strlen(SucAns);
-					memcpy(AnsPtr + *AnsPtrLen, OffAns, strlen(OffAns));
-					*AnsPtrLen = *AnsPtrLen + strlen(OffAns);
-				}
-			}
-			else
-			{
-				*AnsPtrLen = strlen((char *)ErrAns);
-				memcpy(AnsPtr, ErrAns, *AnsPtrLen);
-			}
-			break;
-		}
-		case MAT_DisConnect:
-		{
-			BleDisconnectEnable = 1;
-			memcpy(AnsPtr, SucAns, strlen(SucAns));
-			*AnsPtrLen = strlen(SucAns);
-			break;
-		}
-		case MAT_Msg:
-		{
-			uint8 Databuffer[10] = {0};
-			uint16 CrcChkGet;
-			uint16 CrcChkCal;
 
-			if(BleConnectFlg)
-			{
-				if(*(Readptr+7)=='0' && *(Readptr+9)=='1'&& *(Readptr+10)=='0'&& ReadLen==22)//Hex
-				{
-					memcpy(Databuffer,Readptr+12,8);
-					CrcChkGet = (*(Readptr+12+8))|(*(Readptr+12+9)<<8);
-				}
-				else if(*(Readptr+7)=='1'&&*(Readptr+9)=='2'&& *(Readptr+10)=='0' && ReadLen==32)//string
-				{
-					for(uint8 i=0;i<8;i++)
-					{
-						Databuffer[i] = (CharToHex(*(Readptr+12+i*2))<<4) | (CharToHex(*(Readptr+12+i*2+1))<<0);
-					}
-					CrcChkGet = (CharToHex(*(Readptr+12+18))<<12) | (CharToHex(*(Readptr+12+19))<<8)|(CharToHex(*(Readptr+12+16))<<4) | (CharToHex(*(Readptr+12+17))<<0);
-				}
-				else
-				{
-					memcpy(AnsPtr, ErrAns, strlen(ErrAns));
-					*AnsPtrLen = strlen(ErrAns);
-					memcpy(AnsPtr + *AnsPtrLen, (char *)("-1\n"), strlen((char *)("-1\n")));
-					*AnsPtrLen = *AnsPtrLen + strlen((char *)("-1\n"));
-					break;
-				}
-				CrcChkCal = CRC16_Modbus (Databuffer, 8);
-				if(CrcChkCal==CrcChkGet)
-				{
-					AT_MsgFlag = true;
-					Databuffer[8] = CrcChkGet&0xff;
-					Databuffer[9] = (CrcChkGet>>8)&0xff;
-					xQueueSend(McuDataSendHandle,Databuffer,1000);
-					QueueDataTrans_Type RecvDatabuffer={0,NULL};
-					sint8 QueneRetRev = -1;
-					xQueueReset(McuDataRecvHandle);
-					QueneRetRev = xQueueReceive(McuDataRecvHandle,&RecvDatabuffer,1000*3);//����wifi���ն���
-					if(QueneRetRev==pdPASS)
-					{
-						uint8 DataLen = RecvDatabuffer.RealLen;
-						CrcChkGet = 0x00;
-						CrcChkCal = 0xff;
-						CrcChkGet = (*(RecvDatabuffer.DataPtr+DataLen-1)<<8)| (*(RecvDatabuffer.DataPtr+DataLen-2));
-						CrcChkCal = CRC16_Modbus(RecvDatabuffer.DataPtr,DataLen-2);
-						if(CrcChkCal==CrcChkGet)
-						{
-							memcpy(AnsPtr + *AnsPtrLen, (char *)("Recv:OK,"), strlen((char *)("Recv:OK,")));
-							*AnsPtrLen = *AnsPtrLen + strlen((char *)("Recv:OK,"));
-							//��Ҫת��ΪASCII�뷢��
-							for(uint8 i=0;i<DataLen;i++)
-							{
-								*(AnsPtr + *AnsPtrLen + i*2 + 0) = HexToChar((*(RecvDatabuffer.DataPtr+i)>>4)&0x0F);
-								*(AnsPtr + *AnsPtrLen + i*2 + 1) = HexToChar((*(RecvDatabuffer.DataPtr+i)&0x0F));
-							}
-							*AnsPtrLen = *AnsPtrLen + DataLen*2;
-							//ֱ�ӷ���memcpy(AnsPtr + *AnsPtrLen,RecvDatabuffer.DataPtr,DataLen);
-							//*AnsPtrLen = *AnsPtrLen + DataLen;
-						}
-						else
-						{
-							memcpy(AnsPtr + *AnsPtrLen, (char *)("Recv:ERROR,-2"), strlen((char *)("Recv:ERROR,-2")));
-							*AnsPtrLen = *AnsPtrLen + strlen((char *)("Recv:ERROR,-2"));
-						}
-					}
-					else
-					{
-						memcpy(AnsPtr + *AnsPtrLen, (char *)("Recv:ERROR,-1"), strlen((char *)("Recv:ERROR,-1")));
-						*AnsPtrLen = *AnsPtrLen + strlen((char *)("Recv:ERROR,-1"));
-					}
-					if(RecvDatabuffer.DataPtr!=NULL)
-					{
-						free(RecvDatabuffer.DataPtr);
-					}
-					RecvDatabuffer.DataPtr = NULL;
-					break;
-				}
-				else
-				{
-					memcpy(AnsPtr, ErrAns, strlen(ErrAns));
-					*AnsPtrLen = strlen(ErrAns);
-					memcpy(AnsPtr + *AnsPtrLen, (char *)("-2\n"), strlen((char *)("-2\n")));
-					*AnsPtrLen = *AnsPtrLen + strlen((char *)("-2\n"));
-					break;
-				}
-
-			}
-			else
-			{
-				memcpy(AnsPtr, ErrAns, strlen(ErrAns));
-				*AnsPtrLen = strlen(ErrAns);
-				memcpy(AnsPtr + *AnsPtrLen, OffAns, strlen(OffAns));
-				*AnsPtrLen = *AnsPtrLen + strlen(OffAns);
-			}
-			break;
-		}
-		case MAT_NameSet:
-		{
-			*AnsPtrLen = strlen((char *)ErrAns);
-			memcpy(AnsPtr, ErrAns, *AnsPtrLen);
-			break;
-		}
-		case MAT_Power:
-		{
-			if (*(Readptr + strlen(McuAtCmdFunc[McuAtIdx].str)) == '?')
-			{
-				if (BlePwrState == 0) //Ϊ0����
-				{
-					memcpy(AnsPtr, SucAns, strlen(SucAns));
-					*AnsPtrLen = strlen(SucAns);
-					memcpy(AnsPtr + *AnsPtrLen, OnAns, strlen(OnAns));
-					*AnsPtrLen = *AnsPtrLen + strlen(OnAns);
-				}
-				else
-				{
-					memcpy(AnsPtr, SucAns, strlen(SucAns));
-					*AnsPtrLen = strlen(SucAns);
-					memcpy(AnsPtr + *AnsPtrLen, OffAns, strlen(OffAns));
-					*AnsPtrLen = *AnsPtrLen + strlen(OffAns);
-				}
-			}
-			else if (*(Readptr + strlen(McuAtCmdFunc[McuAtIdx].str)) == 'O' && *(Readptr + strlen(McuAtCmdFunc[McuAtIdx].str) + 1) == 'N')
-			{
-				BlePwrControlEnable = 1;
-				BlePwrControlFlg = 0;
-				memcpy(AnsPtr, SucAns, strlen(SucAns));
-				*AnsPtrLen = strlen(SucAns);
-			}
-			else if (*(Readptr + strlen(McuAtCmdFunc[McuAtIdx].str)) == 'O' && *(Readptr + strlen(McuAtCmdFunc[McuAtIdx].str) + 1) == 'F' && *(Readptr + strlen(McuAtCmdFunc[McuAtIdx].str) + 2) == 'F')
-			{
-				BlePwrControlEnable = 1;
-				BlePwrControlFlg = 1;
-				memcpy(AnsPtr, SucAns, strlen(SucAns));
-				*AnsPtrLen = strlen(SucAns);
-			}
-			else
-			{
-				*AnsPtrLen = strlen((char *)ErrAns);
-				memcpy(AnsPtr, ErrAns, *AnsPtrLen);
-			}
-			break;
-		}
-		case MAT_Role: //��ɫ��ѯ������
-		{
-			if (*(Readptr + strlen(McuAtCmdFunc[McuAtIdx].str)) == '?')
-			{
-				if (BleCurrentRole == 0) //Ϊ0�ӻ�
-				{
-					memcpy(AnsPtr, SucAns, strlen(SucAns));
-					*AnsPtrLen = strlen(SucAns);
-					memcpy(AnsPtr + *AnsPtrLen, SlaveAns, strlen(SlaveAns));
-					*AnsPtrLen = *AnsPtrLen + strlen(SlaveAns);
-				}
-				else
-				{
-					memcpy(AnsPtr, SucAns, strlen(SucAns));
-					*AnsPtrLen = strlen(SucAns);
-					memcpy(AnsPtr + *AnsPtrLen, MasterAns, strlen(MasterAns));
-					*AnsPtrLen = *AnsPtrLen + strlen(MasterAns);
-				}
-			}
-			else if (*(Readptr + strlen(McuAtCmdFunc[McuAtIdx].str)) == *MasterAns)
-			{
-				BleSetRoleEnable = 1;
-				BleSetRole = 1;
-				memcpy(AnsPtr, SucAns, strlen(SucAns));
-				*AnsPtrLen = strlen(SucAns);
-			}
-			else if (*(Readptr + strlen(McuAtCmdFunc[McuAtIdx].str)) == *SlaveAns)
-			{
-				BleSetRoleEnable = 1;
-				BleSetRole = 0;
-				memcpy(AnsPtr, SucAns, strlen(SucAns));
-				*AnsPtrLen = strlen(SucAns);
-			}
-			else
-			{
-				*AnsPtrLen = strlen((char *)ErrAns);
-				memcpy(AnsPtr, ErrAns, *AnsPtrLen);
-			}
-			break;
-		}
-		case MAT_GetMac:
-		{
-			memcpy(AnsPtr, SucAns, strlen(SucAns));
-			*AnsPtrLen = strlen(SucAns);
-			for(uint8 i=0;i<6;i++)
-			{
-				*(AnsPtr + *AnsPtrLen + i*2) = HexToChar((BleMacAddr[i]>>4)&0x0F);
-				*(AnsPtr + *AnsPtrLen + i*2 + 1) = HexToChar(BleMacAddr[i]&0x0F);
-			}
-			*AnsPtrLen = *AnsPtrLen + sizeof(BleMacAddr)*2;
-			break;
-		}
-#if WIFI_MASTER
-		case MAT_W:
-#elif WIFI_SLAVE
-		case MAT_S:
-#endif
-		{
-			AT_WAT_Flag = 1;
-			*AnsPtrLen = 0;
-			break;
-		}
-#if WIFI_MASTER
-		case MAT_W_Set:
-#elif WIFI_SLAVE
-		case MAT_S_CONTWIFI:
-#endif
-		{
-			memset(WiFi_Info, 0x00, sizeof(WiFi_Info));
-			uint8 WiFiInfoLen = 0;
-			uint8 cmd_len = 0;
-#if WIFI_MASTER
-			cmd_len = strlen(McuAtCmdFunc[MAT_W_Set].str);
-#elif WIFI_SLAVE
-			cmd_len = strlen(McuAtCmdFunc[MAT_S_CONTWIFI].str);
-#endif
-			WiFiInfoLen = ReadLen - cmd_len;
-			/* 长度过长 */
-			if(WiFiInfoLen >= sizeof(WiFi_Info) - 3)
-			{
-				memcpy(AnsPtr, ErrAns, strlen(ErrAns));
-				*AnsPtrLen = strlen(ErrAns);
-				break;
-			}
-			WiFi_Info[WiFiInfoLen + 1] = '\r';
-			WiFi_Info[WiFiInfoLen + 2] = '\n';
-			memcpy(WiFi_Info+1, Readptr + cmd_len, WiFiInfoLen);
-			WiFiInfoLen += 2;
-			WiFi_Info[0] = WiFiInfoLen;
-			AT_WIFI_INFO_FLAG = 1;
-			break;
-		}
-#if WIFI_MASTER
-		case MAT_W_IPSet:
-		{
-			uint8 cmdLen = strlen(McuAtCmdFunc[MAT_W_IPSet].str);
-#elif WIFI_SLAVE
-		case MAT_S_TCPCONT:
-		{
-			uint8 cmdLen = strlen(McuAtCmdFunc[MAT_S_TCPCONT].str);
-#endif
-			memset(IP_SetInfo, 0x00, sizeof(IP_SetInfo));
-			uint8 ipInfoLen = ReadLen - cmdLen;
-			if(ipInfoLen >= 53)
-			{
-				memcpy(AnsPtr, ErrAns, strlen(ErrAns));
-				*AnsPtrLen = strlen(ErrAns);
-				break;
-			}
-			IP_SetInfo[ipInfoLen + 1] = '\r';
-			IP_SetInfo[ipInfoLen + 2] = '\n';
-
-			memcpy(IP_SetInfo+1, Readptr + cmdLen, ipInfoLen);
-			ipInfoLen += 2;
-			IP_SetInfo[0] = ipInfoLen;
-			AT_IPINFO_SET_Flag = 1;
-			break;
-		}
-#if WIFI_MASTER
-		case MAT_W_PortClose:
-		{
-			AT_WPROTCLOSE_FLAG = 1;
-			break;
-		}
-		case MAT_W_IP:
-		{
-			W_IP_FLAG = 1;
-			break;
-		}
-		case MAT_W_PortSet:
-		{
-			memset(Port_info, 0x00, sizeof(Port_info));
-			uint8 portInfoLen = ReadLen - strlen("AT+WPORT=");
-			if(portInfoLen > 4)
-			{
-				memcpy(AnsPtr, ErrAns, strlen(ErrAns));
-				*AnsPtrLen = strlen(ErrAns);
-				break;
-			}
-			Port_info[1] = '1';
-			Port_info[2] = ',';
-			Port_info[3 + portInfoLen] = '\r';
-			Port_info[4 + portInfoLen] = '\n';
-			memcpy(Port_info+3, Readptr + strlen("AT+WPORT=") , portInfoLen);
-			Port_info[0] = portInfoLen + 4;
-			AT_WPROTSET_FLAG = 1;
-			break;
-		}
-		case MAT_W_TcpDisconnect:
-		{
-			W_TcpDisconnect = 1;
-			break;
-		}
-		case MAT_W_conchk:
-		{
-			if (wifiConnFlg)
-			{
-				memcpy(AnsPtr, SucAns, strlen(SucAns));
-				*AnsPtrLen = strlen(SucAns);
-				memcpy(AnsPtr + *AnsPtrLen, OnAns, strlen(OnAns));
-				*AnsPtrLen = *AnsPtrLen + strlen(OnAns);
-			}
-			else
-			{
-				memcpy(AnsPtr, SucAns, strlen(SucAns));
-				*AnsPtrLen = strlen(SucAns);
-				memcpy(AnsPtr + *AnsPtrLen, OffAns, strlen(OffAns));
-				*AnsPtrLen = *AnsPtrLen + strlen(OffAns);
-			}
-			break;
-		}
-		case MAT_W_msg:
-		{
-			uint8 Databuffer[10] = {0};
-			uint16 CrcChkGet = 0;
-			uint16 CrcChkCal = 0;
-			uint8 DataLen = 0;
-			sint8 QueneRetRev = -1;
-			if(wifiConnFlg)
-			{
-				if(*(Readptr+8)=='0' && *(Readptr+10)=='1'&& *(Readptr+11)=='0'&& ReadLen==23)//Hex
-				{
-					memcpy(Databuffer,Readptr+12,8);
-					CrcChkGet = (*(Readptr+12+8))|(*(Readptr+12+9)<<8);
-				}
-				else if(*(Readptr+8)=='1'&&*(Readptr+10)=='2'&& *(Readptr+11)=='0' && ReadLen==33)//string
-				{
-					for(uint8 i=0;i<8;i++)
-					{
-						Databuffer[i] = (CharToHex(*(Readptr+13+i*2))<<4) | (CharToHex(*(Readptr+13+i*2+1))<<0);
-					}
-					CrcChkGet = (CharToHex(*(Readptr+13+18))<<12) | (CharToHex(*(Readptr+13+19))<<8)|(CharToHex(*(Readptr+13+16))<<4) | (CharToHex(*(Readptr+13+17))<<0);
-				}
-				else
-				{
-					memcpy(AnsPtr, ErrAns, strlen(ErrAns));
-					*AnsPtrLen = strlen(ErrAns);
-					memcpy(AnsPtr + *AnsPtrLen, (char *)("-1\n"), strlen((char *)("-1\n")));
-					*AnsPtrLen = *AnsPtrLen + strlen((char *)("-1\n"));
-					break;
-				}
-
-				CrcChkCal = CRC16_Modbus (Databuffer, 8);
-				if(CrcChkCal==CrcChkGet)
-				{
-					wifiAskFlg = true;
-					Databuffer[8] = CrcChkGet&0xff;
-					Databuffer[9] = (CrcChkGet>>8)&0xff;
-
-					xQueueSend(McuDataSend_wifi,Databuffer,1000);
-					/* 数据发送 */
-#if SEGGER_PRINTF==1
-SEGGER_RTT_printf("[%d]uart1 send msg to wifi\r\n",__LINE__);
-#endif
-					QueueDataTrans_Type RecvDatabuffer={0,NULL};
-					xQueueReset(McuDataRecv_wifi);
-					QueneRetRev = xQueueReceive(McuDataRecv_wifi,&RecvDatabuffer,1000*2);
-					if(QueneRetRev==pdPASS)
-					{
-						DataLen = RecvDatabuffer.RealLen;
-						CrcChkGet = 0x00;
-						CrcChkCal = 0xff;
-						CrcChkGet = (*(RecvDatabuffer.DataPtr+DataLen-1)<<8)| (*(RecvDatabuffer.DataPtr+DataLen-2));
-						CrcChkCal = CRC16_Modbus(RecvDatabuffer.DataPtr,DataLen-2);
-						if(CrcChkCal==CrcChkGet)
-						{
-							memcpy(AnsPtr + *AnsPtrLen, (char *)("Recv:OK,"), strlen((char *)("Recv:OK,")));
-							*AnsPtrLen = *AnsPtrLen + strlen((char *)("Recv:OK,"));
-							for(uint8 i=0;i<DataLen;i++)
-							{
-								*(AnsPtr + *AnsPtrLen + i*2 + 0) = HexToChar((*(RecvDatabuffer.DataPtr+i)>>4)&0x0F);
-								*(AnsPtr + *AnsPtrLen + i*2 + 1) = HexToChar((*(RecvDatabuffer.DataPtr+i)&0x0F));
-							}
-							*AnsPtrLen = *AnsPtrLen + DataLen*2;
-						}
-						else
-						{
-							memcpy(AnsPtr + *AnsPtrLen, (char *)("Recv:ERROR,-2"), strlen((char *)("Recv:ERROR,-2")));
-							*AnsPtrLen = *AnsPtrLen + strlen((char *)("Recv:ERROR,-2"));
-						}
-					}
-					else
-					{
-						memcpy(AnsPtr + *AnsPtrLen, (char *)("Recv:ERROR,-1"), strlen((char *)("Recv:ERROR,-1")));
-						*AnsPtrLen = *AnsPtrLen + strlen((char *)("Recv:ERROR,-1"));
-					}
-					if(RecvDatabuffer.DataPtr!=NULL)
-					{
-						free(RecvDatabuffer.DataPtr);
-					}
-					RecvDatabuffer.DataPtr = NULL;
-					break;
-				}
-				else
-				{
-					memcpy(AnsPtr, ErrAns, strlen(ErrAns));
-					*AnsPtrLen = strlen(ErrAns);
-					memcpy(AnsPtr + *AnsPtrLen, (char *)("-2\n"), strlen((char *)("-2\n")));
-					*AnsPtrLen = *AnsPtrLen + strlen((char *)("-2\n"));
-					break;
-				}
-
-			}
-			else
-			{
-				memcpy(AnsPtr, ErrAns, strlen(ErrAns));
-				*AnsPtrLen = strlen(ErrAns);
-				memcpy(AnsPtr + *AnsPtrLen, OffAns, strlen(OffAns));
-				*AnsPtrLen = *AnsPtrLen + strlen(OffAns);
-			}
-			break;
-		}
-#endif
-		case MAT_CMD_MAXNUM:
-		{
-			*AnsPtrLen = strlen((char *)ErrAns);
-			memcpy(AnsPtr, ErrAns, *AnsPtrLen);
-			break;
-		}
-		default:
-			break;
-	}
 	return 0;
 }
 

+ 232 - 279
src/AppTaskUart1.c

@@ -13,27 +13,27 @@
  *  Created on: 2022年5月11日
  *      Author: cw
  */
-#include "AppTaskUart1.h"
 #include "stdio.h"
+#include "AppTaskUart1.h"
 /***************************************************
-esp8266使用的串口1
-esp8266默认TCP默认断开时间为180s
-
-主机配置
-ESP8266作服务器时,IP固定为192.168.4.1
-AT+CWMODE=2 								//设置模块 WIFI 模式为 AP 模式
-AT+CWSAP_DEF=”my_test1”,”12345678”,1,4 		//设置模块的 AP 参数:SSID 为my_test1,密码为12345678,通道号为 1,加密方式为:WPA_WPA2_PSK
-AT+CIPMUX=1 								//开启多连接
-AT+CIPSERVER=1,8086 						//开启 SERVER 模式,设置端口为 8086
-AT+CIPSEND=0,25 							//向 ID0 发送 25 字节数据包
-
-从机配置
-AT+CWMODE=1 								//设置模块 WIFI 模式为 STA 模式
-AT+CWJAP_DEF="my_test1","12345678" 			//加入 WIFI 热点
-AT+CIPMUX=0 								//开启单连接
-AT+CIPSTART="TCP","192.168.4.1",8086		//建立 TCP 连接到” 192.168.4.1”,8086
-AT+CIPMODE=1 								//开启透传模式
-AT+CIPSEND 									//开始传输
+* esp8266使用的串口1
+* esp8266默认TCP默认断开时间为180s
+
+* 主机配置
+* ESP8266作服务器时,IP固定为192.168.4.1
+* AT+CWMODE=2 								//设置模块 WIFI 模式为 AP 模式
+* AT+CWSAP_DEF=”my_test1”,”12345678”,1,4 		//设置模块的 AP 参数:SSID 为my_test1,密码为12345678,通道号为 1,加密方式为:WPA_WPA2_PSK
+* AT+CIPMUX=1 								//开启多连接
+* AT+CIPSERVER=1,8086 						//开启 SERVER 模式,设置端口为 8086
+* AT+CIPSEND=0,25 							//向 ID0 发送 25 字节数据包
+
+* 从机配置
+* AT+CWMODE=1 								//设置模块 WIFI 模式为 STA 模式
+* AT+CWJAP_DEF="my_test1","12345678" 			//加入 WIFI 热点
+* AT+CIPMUX=0 								//开启单连接
+* AT+CIPSTART="TCP","192.168.4.1",8086		//建立 TCP 连接到” 192.168.4.1”,8086
+* AT+CIPMODE=1 								//开启透传模式
+* AT+CIPSEND 									//开始传输
 ****************************************************/
 typedef sint8 (*pFunc)(char *PSendStr, char *pReadStr, uint8 CmdIdx, uint16 pReadLen);
 
@@ -46,56 +46,31 @@ typedef struct
 static void wifi_init(uint8 Step);
 static void AtcmdTransmit(sint8 CmdIdx, uint8 *SetValuePtr, uint16 SetValueLen, sint8 *retFunc);
 static sint8 at_callbackFunc(char *PSendStr, char *pReadStr, uint8 CmdIdx, uint16 pReadLen);
-static void wifiDataDecode(uint8 *dataIn,uint16 dataLen);
-uint16 DecodeSlaveBuffer(uint8* dataOutBuffer, uint8 buffSize, uint8 cmd);
+static uint8 wifiDataDecode(uint8 *dataIn,uint16 dataLen);
+
 sint8 tcpSendFunc(uint8 TcpConnectId, uint8 *SendDataPtr, uint16 SendDataLen);
 volatile uint32 TimerCounter = 0; //ms单位
-sint8 tcpSendFunc(uint8 TcpConnectId, uint8 *SendDataPtr, uint16 SendDataLen);
-void GlobeVariableHandle_W1(void);
-
+static sint8 TCP_ConnectEstablish(uint32 timeoutMs);
+static sint8 WiFiDisconnec(void);
+static void WiFiStaMan(void);
 /* 获取x中第i个字节 */
 #define  GET_LOW_BYTE(x,i)    ((x >>  i*8) & 0x000000FF)
 
-/* 时间全局变量 */
-uint8 timeArray[8] = {0};
-/* 车量编码  */
-uint8 dataVinInfo[17] = {0};
-
-typedef enum
-{
-	TCP_IDLE = 0,
-	TCP_OPEN_WIFI,
-	TCP_SERVICES_OK,
-	TCP_SERVICES_CLOSE,
-}TCP_SERVER;
-
-TCP_SERVER tcp_server_process_status;
-
-
 
 const ATCmdFunc_t Atcmdfunc[] =
 {
 	{AT_CMD_TEST, "AT\r\n", at_callbackFunc},
 	{ATE_0, "ATE0\r\n", at_callbackFunc},
-#if WIFI_MASTER
-	{AT_SET_MODE, "AT+CWMODE=2\r\n", at_callbackFunc},  /* 建立SoftAP 模式 */
-	{AT_OPEN_MASTER_WIFI, "AT+CWSAP_DEF=", at_callbackFunc},//开启WIFI
-	{AT_SET_MUX, "AT+CIPMUX=1\r\n", at_callbackFunc},  /* 设置多链接模式 */
-	{AT_CLOSE_SERVER, "AT+CIPSERVER=0\r\n", at_callbackFunc},
-	{AT_OPEN_SERVER, "AT+CIPSERVER=", at_callbackFunc}, /* 打开服务器端口 */
-	{AT_CIPAP_DEF, "AT+CIPAP_DEF=", at_callbackFunc}, /* 打开服务器端口 */
-	{AT_TCP_CLOSE, "AT+CIPCLOSE=5\r\n", at_callbackFunc},
-	{AT_CHECK_LINK_STATUS, "AT+CIPSTATUS\r\n", at_callbackFunc},
-#elif WIFI_SLAVE
 	{AT_SET_MODE, "AT+CWMODE=1\r\n", at_callbackFunc},  /* 建立SoftAP 模式 */
 	{AT_CWAUTOCONN, "AT+CWAUTOCONN=0\r\n", at_callbackFunc},  /* 建立Soft Station 模式 */
 	{AT_CWJAP, "AT+CWJAP=", at_callbackFunc},  /* 连接WiFi */
 	{AT_CIPSTART, "AT+CIPSTART=", at_callbackFunc},  /* 连接IP */
 	{AT_CIPMODE, "AT+CIPMODE=1\r\n", at_callbackFunc},  /* 设置透传模式 */
 	{AT_CIPSEND, "AT+CIPSEND\r\n", at_callbackFunc},  /* 开始发送数据 */
-#endif
 	{AT_SEND_DATA, "AT+CIPSEND=0,", at_callbackFunc},
 	{AT_CIFSR, "AT+CIFSR\r\n", at_callbackFunc},        /* 查看IP信息 */
+	{AT_CWQAP, "AT+CWQAP\r\n", at_callbackFunc},        /* 断开AP连接 */
+	{AT_ISNOCON, "AT+CWJAP?\r\n", at_callbackFunc},  /* 连接WiFi */
 };
 
 
@@ -104,83 +79,44 @@ const ATCmdFunc_t Atcmdfunc[] =
 void wifi_task(void *pvParameters)
 {
 	(void)pvParameters;
-	sint8 ATRet = -1;
 	uint16 ReadLen = 0;
-	uint8 AtCmdRead[150] = {0};
 	uint8 UartDataBuffer[512] = {0};
 	//TimerHandle_t softTimer1 = xTimerCreate("softTimer1", 10, pdTRUE, (void *)0, vTimerCallback);
+
+	/* 等待钥匙下电 */
+	if(1 == KeySignal)
+	{
+		vTaskDelay(pdMS_TO_TICKS(10));
+	}
+
     Dio_WriteChannel(DioConf_DioChannel_PTD16_GPIO_OUT_MCU_WIFI_POW_EN, STD_ON);//wifi开机
 	vTaskDelay(pdMS_TO_TICKS(3000));//开机后等一段时间模块可能会返回开机后的指令 不做处理
+	tcp_server_process_status = TCP_UNCONNECT;
 	wifi_init(0);
-	#if WIFI_MASTER
-	tcp_server_process_status = TCP_IDLE;
-	#elif WIFI_SLAVE
-	tcp_server_process_status = TCP_SERVICES_OK;
-	#endif
-	uint16 pReadLen = 0;
+	//TCP_ConnectEstablish(0);
 	while(1)
 	{
 
-		GlobeVariableHandle_W1();
+		WiFiStaMan();
 		switch(tcp_server_process_status)
 		{
-			case TCP_IDLE:
-			{
-#if WIFI_MASTER    /* 等待从机加入wifi,等待从机连接服务器 */
-				memset(UartDataBuffer,0x00,sizeof(UartDataBuffer));
-				UART_Receive_Data(UART_LPUART1,(uint8*)UartDataBuffer,(uint8*)&pReadLen,100);
-				if(pReadLen>0)
-				{
-					if(strstr((char *)UartDataBuffer,(char *)("+STA_CONNECTED:")))//从机连上wifi
-					{
-						tcp_server_process_status = TCP_IDLE;
-					}
-					else if(strstr((char *)UartDataBuffer,(char *)("+STA_DISCONNECTED:"))\
-							||strstr((char *)UartDataBuffer,(char *)("CLOSED")))//从机断开wifi
-					{
-						tcp_server_process_status = TCP_IDLE;
-					}
-					else if(strstr((char *)UartDataBuffer,(char *)("CONNECT"))) //从机tcp连上服务器
-					{
-						tcp_server_process_status = TCP_SERVICES_OK;
-						wifiConnFlg = true;
-					}
-				}
-#elif WIFI_SLAVE /* 断开WiFi和TCP链接后此处重启 */
-				wifi_init(3);
-				tcp_server_process_status = TCP_SERVICES_OK;
-#endif
-				break;
-			}
-			case TCP_SERVICES_OK://可以正常进行数据交互
+			case TCP_CONNECT://可以正常进行数据交互
 			{
+
 				/*********主机收到上位机的指令,向WIFI从机发送数据*********/
-				if(wifiAskFlg)
-				{
-					/* 此处处理来自串口0的数据 */
-					wifiAskFlg = false;
-				}
 				/*********************主机收到WIFI从机的数据****************/
 				memset(UartDataBuffer, 0x00, sizeof(UartDataBuffer));
-				UART_Receive_Data(UART_LPUART1,(uint8*)UartDataBuffer,(uint8*)&ReadLen,100);
+				UART_Receive_Data(UART_LPUART1,(uint8*)UartDataBuffer,(uint16*)&ReadLen,100);
 				if(ReadLen>0)
 				{
-#if WIFI_MASTER
-					if(strstr((char *)UartDataBuffer,(char *)("+STA_DISCONNECTED")))//从机tcp断开
-					{
-						tcp_server_process_status = TCP_IDLE;
-						wifiAskFlg = false;
-					}
-#elif WIFI_SLAVE
 					if(strstr((char *)UartDataBuffer,(char *)("WIFI DISCONNECT")))/* WiFi断开 */
 					{
-						tcp_server_process_status = TCP_SERVICES_CLOSE;
+						tcp_server_process_status = TCP_UNCONNECT;
 						wifiAskFlg = false;
 					}
-#endif /* #if WIFI_MASTER */
 					else if(strstr((char *)UartDataBuffer,(char *)("CLOSED")))  /* 网络链接断开 */
 					{
-						tcp_server_process_status = TCP_IDLE;
+						tcp_server_process_status = TCP_UNCONNECT;
 						wifiAskFlg = false;
 					}
 					else if(strstr((char *)UartDataBuffer, (char *)("+IPD"))) //收到从机的数据
@@ -201,28 +137,13 @@ void wifi_task(void *pvParameters)
 						}
 						/* 数据解码 */
 						wifiDataDecode(retptr+1,TcpDataLen);
-
-						QueueDataTrans_Type DataSend={0, NULL};
-						DataSend.RealLen = TcpDataLen;
-						DataSend.DataPtr = malloc(TcpDataLen+1);
-						memset(DataSend.DataPtr,0x00,ReadLen+1);
-						memcpy(DataSend.DataPtr,retptr+1,DataSend.RealLen);
-						sint8 McuDataRet = -1;
-						McuDataRet= xQueueSend(McuDataRecv_wifi,&DataSend,1000);
-						if(McuDataRet != pdPASS)
-						{
-							if(DataSend.DataPtr != NULL)
-							{
-								free(DataSend.DataPtr);
-							}
-							DataSend.DataPtr = NULL;
-						}
 					}
 					else
 					{
 
 					}
 				}
+
 				/* 解码部分 */
 				if(ReadLen > 0)
 				{
@@ -231,7 +152,7 @@ void wifi_task(void *pvParameters)
 				}
 				/* 心跳报文发送 */
 				static uint64 tickcount = 0;
-				if(xTaskGetTickCount() - tickcount > pdMS_TO_TICKS(2000))
+				if(xTaskGetTickCount() - tickcount > pdMS_TO_TICKS(3000) && MAX_LOSTHEART >= heartTickLost_W)
 				{
 					tickcount = xTaskGetTickCount();
 					uint16 dataOutLen = 0;
@@ -240,141 +161,81 @@ void wifi_task(void *pvParameters)
 					dataOutLen = DecodeSlaveBuffer(dataOutBuffer, sizeof(dataOutBuffer),  HEARTBEAT_S);
 					/* 数据发送 */
 					tcpSendFunc(0,dataOutBuffer,dataOutLen);
+					heartTickLost_W++; /* 心跳计数 */
+				}
+				/* 断开WiFi连接 */
+				if(heartTickLost_W > MAX_LOSTHEART || 2 == WiFiConnectControl)
+				{
+					if(0 == WiFiDisconnec())
+					{
+						tickcount = 0;
+						tcp_server_process_status = TCP_UNCONNECT;
+						heartTickLost_W = 0;
+					}
 				}
 				break;
 			}
-			case TCP_SERVICES_CLOSE:/* 等待WiFi链接建立 */
+			case TCP_UNCONNECT:
 			{
-				//tcp_server_process_status = TCP_IDLE;
-				wifi_init(3);
-				tcp_server_process_status = TCP_SERVICES_OK;
-				if(true == wifiAskFlg)
-				{
-
-				}
 				break;
 			}
 			default:break;
 		}
 	}
 }
-/*
- *  解码后
- *	获得发送数据包
- * */
-uint16 DecodeSlaveBuffer(uint8* dataOutBuffer, uint8 buffSize, uint8 cmd)
+
+/**************************解码部分***********************************/
+static uint8 wifiDataDecode(uint8 *dataIn,uint16 dataLen)
 {
-	uint8  start_field[3] = {0X68,0X23,0X24};
-	uint8  car_vin[17] = "12345678901234567";
-	uint16 tx_len = 0;
-	uint8  time_now[8] = {0};
-
-	memcpy(dataOutBuffer + tx_len, start_field, 3);
-	tx_len += 3;
-	tx_len += 2; /* 长度 +2 */
-	tx_len ++;   /* 指令 +1*/
-	memcpy(dataOutBuffer + tx_len, car_vin, 17);
-	tx_len += 17;
+	(void)dataLen;
+	uint8 cmd = 0;
+	cmd = dataIn[5];
+	uint8 sendBuffer[255];
+	uint16 dataOutLen = 0;
+	uint16 frameLen = 0;
+	static uint16 lockOrUnlockIng = 0; /* 目前等待解锁或者加锁反馈 0 无 LOCK_M 加锁 UNLOCK_M 解锁 */
+	static uint32 awaitTick = 0;
 
-	switch(cmd)
+	for(uint16 i = 0; i < dataLen - 23; i++)
 	{
-		case HEARTBEAT_S:
-		{
-			//uint8 lock_sta = 0; /* 锁止状态 */
-			uint8 key_sta = 0;  /* 钥匙状态 */
-			uint8 ele_sta = 0;  /* 电磁阀状态 */
-
-			dataOutBuffer[5] = HEARTBEAT_S;
-			memcpy(dataOutBuffer + tx_len, time_now, 8);
-			tx_len += 8;
-			dataOutBuffer[tx_len] = lock_sta;
-			tx_len += 1;
-			dataOutBuffer[tx_len] = key_sta;
-			tx_len += 1;
-			dataOutBuffer[tx_len] = ele_sta;
-			tx_len += 1;
-			break;
-		}
-		case LOCK_S:
-		{
-			uint8 answer = 0;
-			dataOutBuffer[5] = LOCK_S;
-			memcpy(dataOutBuffer + tx_len, time_now, 8);
-			tx_len += 8;
-			dataOutBuffer[tx_len] = answer;
-			tx_len += 1;
-			lock_sta = 2;
-			break;
-		}
-		case UNLOCK_S:
-		{
-			uint8 answer = 0X0;
-			dataOutBuffer[5] = UNLOCK_S;
-			memcpy(dataOutBuffer + tx_len, time_now, 8);
-			tx_len += 8;
-			dataOutBuffer[tx_len] = answer;
-			tx_len += 1;
-			lock_sta = 1;
-			break;
-		}
-		case GETINFO_S:
+		if(0X68 == dataIn[i] && 0X23 == dataIn[i + 1] && 0X24 == dataIn[i + 2])
 		{
-			memcpy(dataOutBuffer + tx_len, time_now, 8);
-			tx_len += 8;
-
-			SLAVE_INFO slav_info_temp = {0};
-			memset(slav_info_temp.sn, 0, 26);
-			slav_info_temp.com_sta = 1;
-			slav_info_temp.BMS_sta = 2;
-			slav_info_temp.touch_sta = 3;
-			slav_info_temp.bms_v = 4;
-			slav_info_temp.bms_a = 5;
-			slav_info_temp.soc = 6;
-			slav_info_temp.soh = 7;
-			slav_info_temp.str_sta = 8;
-			slav_info_temp.stop_sta = 9;
-			slav_info_temp.odo = 10;
-			slav_info_temp.inputA = 11;
-			slav_info_temp.outputA = 12;
-			slav_info_temp.inputKwh = 13;
-			slav_info_temp.outputKwh = 14;
-			slav_info_temp.enger = 15;
-			dataOutBuffer[5] = GETINFO_S;
-			memcpy(dataOutBuffer + tx_len, &slav_info_temp, sizeof(SLAVE_INFO));
-			tx_len += sizeof(SLAVE_INFO);
-			break;
+			frameLen = dataIn[i + 3] + (dataIn[i + 4] << 8);
+			if((i + frameLen <= dataLen) && 0X68 == dataIn[frameLen - 1])
+			{
+				cmd  = dataIn[i + 5];
+				EffectiveDataFed = 1;
+				break;
+			}
+
+			else
+			{
+				cmd = 0XFF;
+			}
+
 		}
-		default:
-			buffSize = 0;
-			break;
 	}
-	dataOutBuffer[tx_len] = 0X68;
-	tx_len += 1;
-	dataOutBuffer[3] = tx_len & 0XFF;
-	dataOutBuffer[4] = (tx_len >> 8) & 0XFF;
 
-	return tx_len;
-}
-
-/**************************解码部分***********************************/
-static void wifiDataDecode(uint8 *dataIn,uint16 dataLen)
-{
-	uint8 cmd = 0;
-	cmd = dataIn[5];
-	uint8 sendBuffer[256];
-	uint16 dataOutLen = 0;
 	switch(cmd)
 	{
 		case LOCK_M:
 		{
-			dataOutLen = DecodeSlaveBuffer(sendBuffer, sizeof(sendBuffer), LOCK_S);
-			tcpSendFunc(0,sendBuffer,dataOutLen);
+			LockSta = 1;
+			if(LOCK_M != lockOrUnlockIng)
+			{
+				lockOrUnlockIng = LOCK_S;
+				awaitTick = xTaskGetTickCount();
+			}
 			break;
 		}
 		case UNLOCK_M:
 		{
-			dataOutLen = DecodeSlaveBuffer(sendBuffer, sizeof(sendBuffer), UNLOCK_S);
-			tcpSendFunc(0,sendBuffer,dataOutLen);
+			UnlockSta = 1;
+			if(UNLOCK_M != lockOrUnlockIng)
+			{
+				lockOrUnlockIng = UNLOCK_S;
+				awaitTick = xTaskGetTickCount();
+			}
 			break;
 		}
 		case GETINFO_M:
@@ -383,14 +244,38 @@ static void wifiDataDecode(uint8 *dataIn,uint16 dataLen)
 			tcpSendFunc(0,sendBuffer,dataOutLen);
 			break;
 		}
-		default:break;
+		case HEARTBEAT_M:
+		{
+			/* 心跳响应 处理心跳返回包内的内容*/
+			heartTickLost_W = 0;
+		}
+		default:
+			break;
+	}
+	if((0 != lockOrUnlockIng) &&
+			((xTaskGetTickCount() - awaitTick >= pdMS_TO_TICKS(20000)) ||
+			((LOCK_S == lockOrUnlockIng && 0 == SolValActuSt) ||
+			(UNLOCK_S == lockOrUnlockIng && 1 == SolValActuSt))))
+	{
+		dataOutLen = DecodeSlaveBuffer(sendBuffer, sizeof(sendBuffer), lockOrUnlockIng);
+		tcpSendFunc(0,sendBuffer,dataOutLen);
+		if(LOCK_S == lockOrUnlockIng)
+			UnlockSta = 0;
+		else if(UNLOCK_S == lockOrUnlockIng)
+			LockSta = 0;
+		else {}
+		lockOrUnlockIng = 0; // 复位
 	}
+	else{}
+
+	return 0;
 }
 
 
 sint8 tcpSendFunc(uint8 TcpConnectId, uint8 *SendDataPtr, uint16 SendDataLen)
 {
 	sint8 outValue = -1;
+	(void)TcpConnectId;
 #ifdef AT_CMD_SEND  /* 启用AT指令发送数据 */
 
 	uint8  sendErrConuter= 0;
@@ -418,7 +303,7 @@ sint8 tcpSendFunc(uint8 TcpConnectId, uint8 *SendDataPtr, uint16 SendDataLen)
 		}
 #else
 	/* 透传 */
-	outValue = UART_Send_Data(UART_LPUART1, (uint8 *)SendDataPtr, SendDataLen, 100);
+	outValue = UART_Send_Data(UART_LPUART1, (uint8 *)SendDataPtr, SendDataLen, 500);
 #endif/* #ifdef AT_CMD_SEND  */
 	return outValue;
 }
@@ -466,30 +351,6 @@ static void wifi_init(uint8 Step)
 				WIFI_CMD_INIT(AT_CWAUTOCONN,NULL,0,InitStep,ATRet);
 				break;
 			}
-			case 4:
-			{
-				const char* wifi_init = "\"Hoisting\",\"jx999999\"\r\n";
-				//const char* wifi_init = "\"123456\",\"12345678\"\r\n";
-				WIFI_CMD_INIT(AT_CWJAP,(uint8*)wifi_init,strlen(wifi_init),InitStep,ATRet);
-				break;
-			}
-			case 5:
-			{
-				const char* wifi_ip_info_init = "\"TCP\",\"192.168.3.177\",5588\r\n";
-				//const char* wifi_ip_info_init = "\"TCP\",\"192.168.0.13\",5588\r\n";
-				WIFI_CMD_INIT(AT_CIPSTART,wifi_ip_info_init,strlen(wifi_ip_info_init),InitStep,ATRet);
-				break;
-			}
-			case 6:/* 设置透传模式 */
-			{
-				WIFI_CMD_INIT(AT_CIPMODE,NULL,0,InitStep,ATRet);
-				if(0 == ATRet)
-				{
-					vTaskDelay(pdMS_TO_TICKS(2000));
-					AtcmdTransmit(AT_CIPSEND, NULL, 0, &ATRet);
-				}
-				break;
-			}
 			default:
 			{
 				return;
@@ -547,14 +408,17 @@ static void AtcmdTransmit(sint8 CmdIdx, uint8 *SetValuePtr, uint16 SetValueLen,
 static sint8 at_callbackFunc(char *PSendStr, char *pReadStr, uint8 CmdIdx, uint16 pReadLen)
 {
 	sint8 OutValue = -1;
+	(void)pReadLen;
+	(void)PSendStr;
 	uint8 *retptr = NULL;
 	char *OkAns = "OK";
 	char *Error = "ERROR";
-	char *FAIL = "FAIL";
+	//char *FAIL = "FAIL";
+	//char* isBuilded = "link is builded";
 	char* noChange = "no change";
 	char* ALCON = "ALREADY CONNECTED";
 	char* busy = "busy p...";
-	char* isBuilded = "link is builded";
+
 	retptr = (uint8 *)strstr((char *)pReadStr, noChange);
 	if(retptr)
 	{
@@ -570,6 +434,7 @@ static sint8 at_callbackFunc(char *PSendStr, char *pReadStr, uint8 CmdIdx, uint1
 		case AT_CWAUTOCONN:
 		case AT_CIFSR:
 		case AT_CIPMODE:
+		case AT_CWQAP:
 		{
 			if (retptr)
 			{
@@ -588,7 +453,7 @@ static sint8 at_callbackFunc(char *PSendStr, char *pReadStr, uint8 CmdIdx, uint1
 				{
 					break;
 				}
-				UART_Receive_Data(UART_LPUART1, (uint8*)pReadStr,(uint8*)&ReadLen, 500);
+				UART_Receive_Data(UART_LPUART1, (uint8*)pReadStr,(uint16*)&ReadLen, 500);
 
 				cyclNum++;
 			}
@@ -598,8 +463,16 @@ static sint8 at_callbackFunc(char *PSendStr, char *pReadStr, uint8 CmdIdx, uint1
 			}
 			break;
 		}
+		case AT_ISNOCON:
+		{
+			char* iscon = "+CWJAP:0";
 
-		//WIFI GOT IP
+			retptr = (uint8 *)strstr((char *)pReadStr, iscon);
+			if(0 != retptr)
+			{
+				return 0;
+			}
+		}
 		default:
 			break;
 	}
@@ -610,34 +483,114 @@ static sint8 at_callbackFunc(char *PSendStr, char *pReadStr, uint8 CmdIdx, uint1
 	}
 	return OutValue;
 }
-sint8 getBackStatus(sint8 ret)
+static sint8 WiFiDisconnec(void)
 {
-	switch(ret)
+	/* 退出WiFi 断开连接 */
+	UART_Send_Data(UART_LPUART1, (uint8 *)"+++", 3, 100);
+	vTaskDelay(pdMS_TO_TICKS(400));
+	UART_Send_Data(UART_LPUART1, (uint8 *)"\r\n", 2, 100);
+	sint8 ret = 0;
+	vTaskDelay(pdMS_TO_TICKS(400));
+	UART_Reset(UART_LPUART1);
+	AtcmdTransmit(AT_CWQAP, NULL, 0, &ret);
+	if(0 != ret)
 	{
-	case 0:
-		return 2;
-	case 3:
-		return 4;
-	default:return 3;
+		vTaskDelay(pdMS_TO_TICKS(400));
+		UART_Reset(UART_LPUART1);
+		AtcmdTransmit(AT_ISNOCON, NULL, 0, &ret);
 	}
+	EffectiveDataFed = 0;
+	return ret;
 }
-void GlobeVariableHandle_W1(void)
+/*
+ * 建立TCP连接函数
+ * timeoutMs 超时时间
+ * 数值为0时意为一直等待连接
+ * */
+static sint8 TCP_ConnectEstablish(uint32 timeoutMs)
 {
+	//WIFI模块初始化
+	uint8 InitStep = 0;
 	sint8 ATRet = -1;
+	uint32 initTime = xTaskGetTickCount();
+	while(1)
+	{
+
+		switch(InitStep)
+		{
+			case 0:
+			{
+				//const char* wifi_init = "\"Hoisting\",\"jx999999\"\r\n";
+				const char* wifi_init = "\"123456\",\"12345678\"\r\n";
+				WIFI_CMD_INIT(AT_CWJAP,(uint8*)wifi_init,strlen(wifi_init),InitStep,ATRet);
+				break;
+			}
+			case 1:
+			{
+				//const char* wifi_ip_info_init = "\"TCP\",\"192.168.3.177\",5588\r\n";
+				const char* wifi_ip_info_init = "\"TCP\",\"192.168.0.181\",5588\r\n";
+				WIFI_CMD_INIT(AT_CIPSTART,wifi_ip_info_init,strlen(wifi_ip_info_init),InitStep,ATRet);
+				break;
+			}
+			case 2:/* 设置透传模式 */
+			{
+				WIFI_CMD_INIT(AT_CIPMODE,NULL,0,InitStep,ATRet);
+				if(0 == ATRet)
+				{
+					vTaskDelay(pdMS_TO_TICKS(1000));
+					AtcmdTransmit(AT_CIPSEND, NULL, 0, &ATRet);
+				}
+				break;
+			}
+			default:
+				return 0;
+				break;
+		}
+		if(xTaskGetTickCount() - initTime > pdMS_TO_TICKS(timeoutMs) && 0 != timeoutMs)
+		{
+			/* 超时退出 */
+			return 1;
+		}
+
+	}
+	return 0;
+}
 
-	if(1 == AT_WAT_Flag)
+static void WiFiStaMan(void)
+{
+
+	if((WIFI_UNENABLE != tcp_server_process_status && 1 == WiFiPwrControl)
+			|| (1 == KeySignal && TCP_CONNECT != tcp_server_process_status)) /* WiFi关机 */
+	{
+
+		WiFiPwrControl = 0;
+		if(TCP_CONNECT == tcp_server_process_status)
+		{
+			WiFiDisconnec();
+		}
+		else
+		{
+			;
+		}
+		Dio_WriteChannel(DioConf_DioChannel_PTD16_GPIO_OUT_MCU_WIFI_POW_EN, STD_OFF);
+		tcp_server_process_status = WIFI_UNENABLE;
+
+	}
+	if(WIFI_UNENABLE == tcp_server_process_status && 2 == WiFiPwrControl) /* WiFi开机 */
 	{
-		AtcmdTransmit(AT_CMD_TEST, NULL, 0, &ATRet);
-		AT_WAT_Flag = getBackStatus(ATRet);
+		WiFiPwrControl = 0;
+		Dio_WriteChannel(DioConf_DioChannel_PTD16_GPIO_OUT_MCU_WIFI_POW_EN, STD_ON);
 	}
-	else if(1 == AT_WIFI_INFO_FLAG)
+	if(TCP_UNCONNECT == tcp_server_process_status && 1 == WiFiConnectControl) /* 连接WiFi */
 	{
-		AtcmdTransmit(AT_CWJAP, WiFi_Info + 1, WiFi_Info[0], &ATRet);
-		AT_WIFI_INFO_FLAG = getBackStatus(ATRet);
+		WiFiConnectControl = 0;
+		TCP_ConnectEstablish(0);
+		tcp_server_process_status = TCP_CONNECT;
 	}
-	else if(1 == AT_IPINFO_SET_Flag)
+	if(TCP_CONNECT == tcp_server_process_status && 2 == WiFiConnectControl) /* 断开WiFi */
 	{
-		AtcmdTransmit(AT_CIPSTART, IP_SetInfo + 1, IP_SetInfo[0], &ATRet);
-		AT_IPINFO_SET_Flag = getBackStatus(ATRet);
+		WiFiConnectControl = 0;
+		WiFiDisconnec();
+		tcp_server_process_status = TCP_UNCONNECT;
 	}
 }

+ 9 - 32
src/AppTaskUart1.h

@@ -28,6 +28,7 @@
 #include "AppFunclib.h"
 #include "AppGlobalVar.h"
 
+#include "WirelessDecodeSlave.h"
 
 typedef enum
 {
@@ -39,43 +40,19 @@ typedef enum
 	AT_CIPSTART,
 	AT_CIPMODE,
 	AT_CIPSEND,
-	AT_SEND_DATA,
+	AT_SEND_DATA,   /* 获取IP信息 */
 	AT_CIFSR,     /* 获取IP信息 */
+	AT_CWQAP,     /* 断开WiFi连接 */
+	AT_ISNOCON,
 }ATCmd;
 
+
 typedef enum
 {
-	HEARTBEAT_S 	= 0X03,
-	HEARTBEAT_M,
-	LOCK_M,
-	LOCK_S,
-	UNLOCK_M,
-	UNLOCK_S,
-	GETINFO_M,
-	GETINFO_S,
-	MAX,
-}PROTPCPL_EXC;
-#pragma pack(1)
-typedef struct
-{
-	char sn[26];
-	uint8 com_sta;
-	uint8 BMS_sta;
-	uint8 touch_sta;
-	uint16 bms_v;
-	uint16 bms_a;
-	uint8 soc;
-	uint8 soh;
-	uint8 str_sta;
-	uint8 stop_sta;
-	uint32 odo;
-	uint32 inputA;
-	uint32 outputA;
-	uint32 inputKwh;
-	uint32 outputKwh;
-	uint16 enger;
-}SLAVE_INFO;
-#pragma pack(0)
+	TCP_UNCONNECT = 0,		/* 模块上电,TCP未连接 */
+	TCP_CONNECT,			/* TCP建立连接 */
+	WIFI_UNENABLE,			/* 模块未上电 */
+}TCP_SERVER;
 
 void wifi_task(void *pvParameters);
 #endif /* APPTASKUART1_H_ */

+ 2 - 2
src/USER_CONFIG.h

@@ -1,7 +1,7 @@
 /*
  * USER_CONFIG.h
  *
- *  Created on: 2022年4月27日
+ *  Created on: 2022锟斤拷4锟斤拷27锟斤拷
  *      Author: Zhengchao
  */
 
@@ -21,7 +21,7 @@
 
 //the Default Configuration of CAN0 is Extended 250k, while CAN0 standard 250k only for WuLing project
 #define CAN0_BAUDRATE CAN_BAUDRATE_250K
-#define CAN0_MSG_TYPE CAN_MSG_STANDARD
+#define CAN0_MSG_TYPE CAN_MSG_EXTENDED
 
 
 #define CAN1_BAUDRATE CAN_BAUDRATE_500K

+ 147 - 0
src/WirelessDecodeSlave.c

@@ -0,0 +1,147 @@
+/*
+ *  Created on: 2023年1月6日
+ *  Author: hc
+ *  车端处理无线通讯中的协议处理
+ */
+#include "stdio.h"
+#include "AppTaskUart1.h"
+
+/*
+ *  Author: hc
+ *  ******  车端协议解码函数 *******
+ *
+ *  uint8* dataOutBuffer 待解码数据
+ *  *** 注意:解码后的数据仍存放到该数组中 ***
+ *   uint8 buffSize 该数组长度
+ *   uint8 cmd 解码指令
+ *
+ *   return 解码后长度
+ */
+uint16 DecodeSlaveBuffer(uint8* dataOutBuffer, uint8 buffSize, uint8 cmd)
+{
+	uint8  start_field[3] = {0X68,0X23,0X24};
+	uint8  car_vin[17] = "12345678901234567";
+	uint16 tx_len = 0;
+	uint8  time_now[8] = {0};
+
+	memcpy(dataOutBuffer + tx_len, start_field, 3);
+	tx_len += 3;
+	tx_len += 2; /* 长度 +2 */
+	tx_len ++;   /* 指令 +1*/
+	memcpy(dataOutBuffer + tx_len, car_vin, 17);
+	tx_len += 17;
+
+	switch(cmd)
+	{
+		case HEARTBEAT_S:
+		{
+			uint8 lock_sta = 0; 		/* 锁止状态 */
+			uint8 key_sta = 0;			/* 钥匙状态 */
+			uint8 ele_sta = 0; 			/* 电磁阀驱动状态 */
+			if(1 == KeySignal)
+			{
+				key_sta = 2;
+			}
+			else if(0 == KeySignal)
+			{
+				key_sta = 1;
+			}
+			else{}
+
+			if(1 == LockCheck)
+			{
+				lock_sta = 2;
+			}
+			else
+				lock_sta = 1;
+
+			ele_sta = SolValActuSt;
+
+			dataOutBuffer[5] = HEARTBEAT_S;
+			memcpy(dataOutBuffer + tx_len, time_now, 8);
+			tx_len += 8;
+			dataOutBuffer[tx_len] = lock_sta;
+			tx_len += 1;
+			dataOutBuffer[tx_len] = key_sta;
+			tx_len += 1;
+			dataOutBuffer[tx_len] = ele_sta;
+			tx_len += 1;
+			break;
+		}
+		case LOCK_S:
+		{
+			uint8 answer = 0;
+			if(0 == SolValActuSt)
+			{
+				answer = 0;
+			}
+			else if(1 == SolValActuSt)
+			{
+				answer = 1;
+			}
+
+			dataOutBuffer[5] = LOCK_S;
+			memcpy(dataOutBuffer + tx_len, time_now, 8);
+			tx_len += 8;
+			dataOutBuffer[tx_len] = answer;
+			tx_len += 1;
+			lock_sta = 2;
+			break;
+		}
+		case UNLOCK_S:
+		{
+			uint8 answer = 0X0;
+			if(0 == SolValActuSt)
+			{
+				answer = 1;
+			}
+			else if(1 == SolValActuSt)
+			{
+				answer = 0;
+			}
+			dataOutBuffer[5] = UNLOCK_S;
+			memcpy(dataOutBuffer + tx_len, time_now, 8);
+			tx_len += 8;
+			dataOutBuffer[tx_len] = answer;
+			tx_len += 1;
+			lock_sta = 1;
+			break;
+		}
+		case GETINFO_S:
+		{
+			memcpy(dataOutBuffer + tx_len, time_now, 8);
+			tx_len += 8;
+
+			SLAVE_INFO slav_info_temp = {0};
+			memcpy(slav_info_temp.sn, BatSN, 26);
+			slav_info_temp.com_sta = BatComState;
+			slav_info_temp.BMS_sta = BatBMS_State;
+			slav_info_temp.touch_sta = MContactorsState;
+			slav_info_temp.bms_v = BatValtage;
+			slav_info_temp.bms_a = BatteryCurrent;
+			slav_info_temp.soc = SOC;
+			slav_info_temp.soh = SOH;
+			slav_info_temp.str_sta = VehicleState;
+			slav_info_temp.stop_sta = BrakeSignal;
+			slav_info_temp.odo = AccruedMileage;
+			slav_info_temp.inputA = AccruedChargeAh;
+			slav_info_temp.outputA = AccruedDischargeAh;
+			slav_info_temp.inputKwh = AccruedChargeKwh;
+			slav_info_temp.outputKwh = AccruedDischargeKwh;
+			slav_info_temp.enger = BatRateRnergy;
+			dataOutBuffer[5] = GETINFO_S;
+			memcpy(dataOutBuffer + tx_len, &slav_info_temp, sizeof(SLAVE_INFO));
+			tx_len += sizeof(SLAVE_INFO);
+			break;
+		}
+		default:
+			tx_len = 0;
+			break;
+	}
+	dataOutBuffer[tx_len] = 0X68;
+	tx_len += 1;
+	dataOutBuffer[3] = tx_len & 0XFF;
+	dataOutBuffer[4] = (tx_len >> 8) & 0XFF;
+
+	return tx_len;
+}

+ 48 - 0
src/WirelessDecodeSlave.h

@@ -0,0 +1,48 @@
+/*
+ * WirelessDecodeSlave.h
+ *
+ *  Created on: 2023年1月6日
+ *      Author: king
+ */
+
+#ifndef WIRELESSDECODESLAVE_H_
+#define WIRELESSDECODESLAVE_H_
+
+uint16 DecodeSlaveBuffer(uint8* dataOutBuffer, uint8 buffSize, uint8 cmd);
+
+typedef enum
+{
+	HEARTBEAT_S 	= 0X03,
+	HEARTBEAT_M,
+	LOCK_M,
+	LOCK_S,
+	UNLOCK_M,
+	UNLOCK_S,
+	GETINFO_M,
+	GETINFO_S,
+	MAX,
+}PROTPCPL_EXC;
+
+#pragma pack(1)
+typedef struct
+{
+	uint8 sn[26];
+	uint8 com_sta;
+	uint8 BMS_sta;
+	uint8 touch_sta;
+	uint16 bms_v;
+	uint16 bms_a;
+	uint8 soc;
+	uint8 soh;
+	uint8 str_sta;
+	uint8 stop_sta;
+	uint32 odo;
+	uint32 inputA;
+	uint32 outputA;
+	uint32 inputKwh;
+	uint32 outputKwh;
+	uint16 enger;
+}SLAVE_INFO;
+#pragma pack(0)
+
+#endif /* WIRELESSDECODESLAVE_H_ */

+ 8 - 1
src/hal_adapter.c

@@ -165,7 +165,7 @@ Std_ReturnType UART_Query_Data(uint8 transChannel, uint8 recvChannel, uint8 *txB
     }
     return retVal;
 }
-Std_ReturnType UART_Receive_Data(uint8 recvChannel, uint8 *rxBuffer, uint8 *rxlen, uint32 T_timeout)
+Std_ReturnType UART_Receive_Data(uint8 recvChannel, uint8 *rxBuffer, uint16 *rxlen, uint32 T_timeout)
 {
     UartMsg_t UartRecvMsg;
     BaseType_t ret = pdFALSE;
@@ -323,6 +323,13 @@ void Uart_Hal_SendTask(void *pvParameters)
 
         if (ret == pdTRUE)
         {
+        	if(2 == UartIdx)
+        	{
+#if 1
+	SEGGER_RTT_printf("ble rec:%s\r\n", UartSendMsg.dataPtr);
+#endif
+        	}
+
 #if SEGGER_PRINTF==1
     	if(UartIdx==1 && UartSendMsg.dataPtr==0x1FFF3BD4)
     	{

+ 2 - 2
src/hal_adapter.h

@@ -51,7 +51,7 @@ typedef signed short INT16;
 typedef signed long INT32;
 
 #define UARTPRINTF 0
-#define SEGGER_PRINTF 1
+#define SEGGER_PRINTF 0
 #define CAN0 0
 #define CAN1 1
 #ifndef min
@@ -230,7 +230,7 @@ Std_ReturnType HAL_EEP_Compare(uint32 eepCompareStartAddr, uint8 *pDataNeedtoCom
 sint8 AtcmdDelayRecvFunc(uint8 recvChannel,char *ResultStrPtr,uint16 delayTime);
 Std_ReturnType UART_Query_Data(uint8 transChannel, uint8 recvChannel, uint8 *txBuffer, uint16 sendLength, uint8 *rxBuffer, uint16 *rxlen, uint32 T_timeout);
 Std_ReturnType UART_Send_Data(uint8 transChannel, const uint8 *txBuffer, uint32 sendLength, uint32 T_timeout);
-Std_ReturnType UART_Receive_Data(uint8 recvChannel, uint8 *rxBuffer, uint8 *rxlen, uint32 T_timeout);
+Std_ReturnType UART_Receive_Data(uint8 recvChannel, uint8 *rxBuffer, uint16 *rxlen, uint32 T_timeout);
 Std_ReturnType UART_Reset(uint8 recvChannel);
 void UART_Callback(uint32 hwInstance, Lpuart_Uart_Ip_EventType event);
 void UartInit(void);

+ 4 - 5
src/main.c

@@ -71,8 +71,7 @@ int main(void)
 	//Init MCU Module
 #if (MCU_PRECOMPILE_SUPPORT == STD_ON)
     Mcu_Init(NULL_PTR);
-#elif (MCU_PRECOMPILE_SUPPORT == STD_OFF)
-    Mcu_Init(&Mcu_Config_VS_0);
+#elif (MCU_PRECOMPILE_SUPPORT == STD_OFF) 
 #endif /* (MCU_PRECOMPILE_SUPPORT == STD_ON) */
 
     //Init Clock Module
@@ -89,7 +88,7 @@ int main(void)
     Mcu_SetMode(McuModeSettingConf_Run);
     OsIf_Init(NULL_PTR);
 
-    //Init Platform Module
+    //Init Platform Modules
     Platform_Init(NULL_PTR);
 
     //Init Port Module
@@ -135,7 +134,7 @@ int main(void)
 	Gpt_Init(&Gpt_Config_VS_0);
 	/* Enable the Gpt notification to periodically service the Wdg */
 	Gpt_EnableNotification(GptConf_GptChannelConfiguration_GptChannelConfiguration_0);
-//	WdgInit();
+	WdgInit();
 	IP_LPUART0->CTRL |= LPUART_CTRL_ILT(1);
 	IP_LPUART1->CTRL |= LPUART_CTRL_ILT(1);
 	IP_LPUART2->CTRL |= LPUART_CTRL_ILT(1);
@@ -145,7 +144,7 @@ int main(void)
 
 	UartInit();
     xTaskCreate( MainTask, ( const char * const ) "MainTask", 128, (void*)0, main_TASK_PRIORITY+3 , NULL );
-    //xTaskCreate( BleTask, ( const char * const ) "BleTask", 256 + 64, (void*)0, main_TASK_PRIORITY , NULL );
+    xTaskCreate( BleTask, ( const char * const ) "BleTask", 256 + 64, (void*)0, main_TASK_PRIORITY , NULL );
     xTaskCreate( CanTask,  ( const char * const ) "CanTask" , 512, (void*)0, main_TASK_PRIORITY + 2, NULL ); //EBC通讯
     xTaskCreate( UartTask, ( const char * const ) "UartTask" , 512, (void*)0, main_TASK_PRIORITY + 1, NULL );//PLC通讯
     xTaskCreate( wifi_task,( const char * const ) "WifiTask" , 512, (void*)0, main_TASK_PRIORITY, NULL );	 //WIFI通讯