uart_process.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. #include "can_process.h"
  2. #include "uart_process.h"
  3. #include "crc_cal.h"
  4. #include "stdlib.h"
  5. #include "stdio.h"
  6. #include "string.h"
  7. #include "encrypt.h"
  8. #include "Update.h"
  9. #include "ctf_process.h"
  10. #include "flash.h"
  11. uint8_t Power_mode = 0;//0-default,1-off,2-on
  12. uint32_t T_startPowerOnOff = 0;
  13. uint8_t UART1_BaudrateSwitch = 0;
  14. uint32_t T_UART1_BaudrateSwitch = 0;//0-9600bps,1-57600bps,2-ready to switch baudrate to 57600bps
  15. uint32_t T_RecvCANMsg = 0;
  16. uint32_t T_RecvUartMsg = 0;
  17. uint8_t Transmit_UARTOrCAN = 1;//0-both,1-CAN,2-UART
  18. uint32_t Uart_BootBaudrate = 57600;
  19. uint32_t Uart_AppBaudrate = 9600;
  20. uint32_t CAN_Baudrate = 250;
  21. uint32_t HandingFlag_Enable = 0;
  22. uint32_t PowerOnHandingFlag = 0;
  23. uint32_t T_PowerOnHanding = 0;
  24. void UART1_BaudrateCheck(void)
  25. {
  26. if(UART1_BaudrateSwitch == 2)
  27. {
  28. if((HAL_GetTick() - T_UART1_BaudrateSwitch)>50)
  29. {
  30. UART1_BaudrateSwitch = 1;
  31. MX_USART1_Reopen(Uart_BootBaudrate);
  32. }
  33. }
  34. else if(UART1_BaudrateSwitch == 1)
  35. {
  36. if((HAL_GetTick() - T_UART1_BaudrateSwitch)>5000)
  37. {
  38. UART1_BaudrateSwitch = 0;
  39. MX_USART1_Reopen(Uart_AppBaudrate);
  40. }
  41. }
  42. }
  43. uint8_t Transmit_UARTOrCAN_pre = 0;//0-both,1-CAN,2-UART
  44. void MsgModeCheck(void)
  45. {
  46. Transmit_UARTOrCAN_pre = Transmit_UARTOrCAN;
  47. if(((HAL_GetTick() - T_RecvCANMsg) > 30000) && ((HAL_GetTick() - T_RecvUartMsg) > 30000))
  48. {
  49. Transmit_UARTOrCAN = 0;//超过x秒未收到UART和CAN数据进入模式0,下次通信同时发送CAN和UART数据
  50. }
  51. else if(((HAL_GetTick() - T_RecvCANMsg) < 5000)&&(T_RecvCANMsg>0))
  52. {
  53. Transmit_UARTOrCAN = 1;
  54. }
  55. else if(((HAL_GetTick() - T_RecvUartMsg) < 5000)&&(T_RecvUartMsg>0))
  56. {
  57. Transmit_UARTOrCAN = 2;
  58. }
  59. else if((T_RecvCANMsg == 0)&&(T_RecvUartMsg == 0))
  60. {
  61. Transmit_UARTOrCAN = 0;
  62. }
  63. if(Transmit_UARTOrCAN_pre != Transmit_UARTOrCAN)
  64. {
  65. if(Transmit_UARTOrCAN == 2)
  66. {
  67. HAL_Delay(100);
  68. HAL_GPIO_WritePin(CommSwitch_GPIO_Port, CommSwitch_Pin1, GPIO_PIN_SET);
  69. HAL_GPIO_WritePin(CommSwitch_GPIO_Port, CommSwitch_Pin2, GPIO_PIN_SET);
  70. }
  71. else
  72. {
  73. HAL_Delay(100);
  74. HAL_GPIO_WritePin(CommSwitch_GPIO_Port, CommSwitch_Pin1, GPIO_PIN_RESET);
  75. HAL_GPIO_WritePin(CommSwitch_GPIO_Port, CommSwitch_Pin2, GPIO_PIN_RESET);
  76. }
  77. }
  78. }
  79. void PowerOnOff(uint8_t *mode)
  80. {
  81. if(*mode == 1)
  82. {
  83. if((HAL_GetTick() - T_startPowerOnOff) >= 1000)//Off延时1s
  84. {
  85. HAL_GPIO_WritePin(ACC_WELLING_EN_GPIO_Port, ACC_WELLING_EN_Pin, GPIO_PIN_RESET);//Off
  86. *mode = 0;
  87. }
  88. }
  89. else if(*mode == 2)
  90. {
  91. if((HAL_GetTick() - T_startPowerOnOff) >= 3000)//On延时3s
  92. {
  93. HAL_GPIO_WritePin(ACC_WELLING_EN_GPIO_Port, ACC_WELLING_EN_Pin, GPIO_PIN_RESET);//Off
  94. *mode = 0;
  95. }
  96. }
  97. if(HandingFlag_Enable == 1)//使能
  98. {
  99. if(PowerOnHandingFlag == 1)//开机
  100. {
  101. if((HAL_GetTick() - T_PowerOnHanding) >= 1000)//开机延时1s
  102. {
  103. PowerOnHandingFlag = 0;
  104. uint8_t Data[2] = {0,0};
  105. UART_SendSwitch_Lidian2(&UART_TxBuff_Struct1, 0x01, 0x7702, Data);
  106. HAL_Delay(50);
  107. UART_SendSwitch_zhihuigainian(&UART_TxBuff_Struct1, 0x7702, Data);
  108. HAL_Delay(50);
  109. }
  110. }
  111. }
  112. }
  113. //命令处理
  114. static void UART3_DataProcess(uint8_t Mode, uint16_t Command, uint8_t* Data)
  115. {
  116. switch(Command)
  117. {
  118. //CDL在线检测
  119. case 0x1100:
  120. {
  121. SendCmdData(&UART_TxBuff_Struct3, MODE_REPORT, 0x07FF, 0x1100, (uint8_t*)NULL);
  122. break;
  123. }
  124. //设备开关机
  125. case 0x2201:
  126. {
  127. if(Power_mode == 0)
  128. {
  129. T_startPowerOnOff = HAL_GetTick();
  130. if(Data[0] == 0xF0)//关机
  131. {
  132. Power_mode = 1;
  133. HAL_GPIO_WritePin(ACC_WELLING_EN_GPIO_Port, ACC_WELLING_EN_Pin, GPIO_PIN_SET);//On
  134. }
  135. else if(Data[0] == 0xF1)//开机
  136. {
  137. Power_mode = 2;
  138. HAL_GPIO_WritePin(ACC_WELLING_EN_GPIO_Port, ACC_WELLING_EN_Pin, GPIO_PIN_SET);//On
  139. }
  140. }
  141. if(Data[0] == 0xF0)//关机
  142. {
  143. // HAL_GPIO_WritePin(SwitchOnOrOff_GPIO_Port, SwitchOnOrOff_Pin, GPIO_PIN_RESET);
  144. HAL_GPIO_WritePin(ACC_MOTINOVA_EN_GPIO_Port, ACC_MOTINOVA_EN_Pin, GPIO_PIN_RESET);
  145. }
  146. else if(Data[0] == 0xF1)//开机
  147. {
  148. PowerOnHandingFlag = 1;
  149. T_PowerOnHanding = HAL_GetTick();
  150. // HAL_GPIO_WritePin(SwitchOnOrOff_GPIO_Port, SwitchOnOrOff_Pin, GPIO_PIN_SET);
  151. HAL_GPIO_WritePin(ACC_MOTINOVA_EN_GPIO_Port, ACC_MOTINOVA_EN_Pin, GPIO_PIN_SET);
  152. }
  153. break;
  154. }
  155. //复位
  156. case 0x4400:
  157. {
  158. Refresh_Flag();
  159. __set_FAULTMASK(1);//关闭所有中断
  160. HAL_NVIC_SystemReset();
  161. break;
  162. }
  163. case 0x4508:
  164. {
  165. if (strncmp("CDLRESET", (char *)Data, 8) == 0)
  166. {
  167. //重启
  168. __set_FAULTMASK(1);//关闭所有中断
  169. HAL_NVIC_SystemReset();
  170. }
  171. break;
  172. }
  173. case 0x2505:
  174. {
  175. if (strncmp("RESET", (char *)Data, 5) == 0)
  176. {
  177. // UART_TransferData(&UART_TxBuff_Struct1, UARTID_CDL_TO_MC, Mode, 0x751, Command, Data);
  178. // HAL_Delay(200);
  179. //
  180. // __set_FAULTMASK(1);//关闭所有中断
  181. // HAL_NVIC_SystemReset();
  182. T_UART1_BaudrateSwitch = HAL_GetTick();
  183. UART1_BaudrateSwitch = 2;
  184. }
  185. break;
  186. }
  187. //随机码和密钥进行校验
  188. case 0x5514:
  189. {
  190. static uint8_t InputCode[12];
  191. static uint8_t InputKey[8];
  192. static uint8_t OutputCode[12];
  193. static uint8_t SendData[27];
  194. memcpy(InputCode, Data, 12);
  195. memcpy(InputKey, Data + 12, 8);
  196. CheckCodeCal(InputCode, InputKey, OutputCode);
  197. memcpy(SendData, OutputCode, 12);
  198. memcpy(SendData + 12, (uint8_t*)"V4.1.3_20240907", 15);
  199. SendCmdData(&UART_TxBuff_Struct3, MODE_REPORT, 0x07FF, 0x551B, SendData);
  200. break;
  201. }
  202. //授权
  203. case 0x6600:
  204. {
  205. uint8_t SendData[24];
  206. Ctf_CalAndSave();
  207. memcpy(SendData, MAC_ID, 12);
  208. memcpy(SendData + 12, Check_Code, 12);
  209. SendCmdData(&UART_TxBuff_Struct3, MODE_REPORT, 0x07FF, 0x6618, SendData);
  210. HAL_Delay(200);
  211. __set_FAULTMASK(1);//关闭所有中断
  212. HAL_NVIC_SystemReset();
  213. break;
  214. }
  215. case 0x7702:
  216. {
  217. UART_SendSwitch_Lidian2(&UART_TxBuff_Struct1, 0x01, Command, Data);
  218. HAL_Delay(50);
  219. UART_SendSwitch_zhihuigainian(&UART_TxBuff_Struct1, Command, Data);
  220. HAL_Delay(50);
  221. break;
  222. }
  223. case 0x8000:
  224. {
  225. uint8_t SendData[4];
  226. Transmit_UARTOrCAN = (uint8_t)FLASH_Read(CDLPROPERTIES_AREA_ADDRESS, 1);
  227. SendData[0] = 0;
  228. SendData[1] = 0;
  229. SendData[2] = 0;
  230. SendData[3] = Transmit_UARTOrCAN;
  231. SendCmdData(&UART_TxBuff_Struct3, MODE_REPORT, 0x07FF, 0x8004, SendData);
  232. HAL_Delay(50);
  233. break;
  234. }
  235. case 0x8104:
  236. {
  237. Transmit_UARTOrCAN = Data[3];
  238. FLASH_WriteCDLProperties(CDLPROPERTIES_AREA_ADDRESS);
  239. SendCmdData(&UART_TxBuff_Struct3, MODE_REPORT, 0x07FF, 0x8002, (uint8_t*)"OK");
  240. break;
  241. }
  242. case 0x8200:
  243. {
  244. uint8_t SendData[4];
  245. Uart_BootBaudrate = FLASH_Read(CDLPROPERTIES_AREA_ADDRESS+4, 1);
  246. SendData[0] = (uint8_t)((Uart_BootBaudrate >> 24) & 0xFF);
  247. SendData[1] = (uint8_t)((Uart_BootBaudrate >> 16) & 0xFF);
  248. SendData[2] = (uint8_t)((Uart_BootBaudrate >> 8) & 0xFF);
  249. SendData[3] = (uint8_t)((Uart_BootBaudrate >> 0) & 0xFF);
  250. SendCmdData(&UART_TxBuff_Struct3, MODE_REPORT, 0x07FF, 0x8204, SendData);
  251. HAL_Delay(50);
  252. break;
  253. }
  254. case 0x8304:
  255. {
  256. Uart_BootBaudrate = (uint32_t)((Data[0]<<24) + (Data[1]<<16) + (Data[2]<<8) +(Data[3]));
  257. FLASH_WriteCDLProperties(CDLPROPERTIES_AREA_ADDRESS);
  258. SendCmdData(&UART_TxBuff_Struct3, MODE_REPORT, 0x07FF, 0x8002, (uint8_t*)"OK");
  259. break;
  260. }
  261. case 0x8400:
  262. {
  263. uint8_t SendData[4];
  264. Uart_AppBaudrate = FLASH_Read(CDLPROPERTIES_AREA_ADDRESS+8, 1);
  265. SendData[0] = (uint8_t)((Uart_AppBaudrate >> 24) & 0xFF);
  266. SendData[1] = (uint8_t)((Uart_AppBaudrate >> 16) & 0xFF);
  267. SendData[2] = (uint8_t)((Uart_AppBaudrate >> 8) & 0xFF);
  268. SendData[3] = (uint8_t)((Uart_AppBaudrate >> 0) & 0xFF);
  269. SendCmdData(&UART_TxBuff_Struct3, MODE_REPORT, 0x07FF, 0x8404, SendData);
  270. HAL_Delay(50);
  271. break;
  272. }
  273. case 0x8504:
  274. {
  275. Uart_AppBaudrate = (uint32_t)((Data[0]<<24) + (Data[1]<<16) + (Data[2]<<8) +(Data[3]));
  276. FLASH_WriteCDLProperties(CDLPROPERTIES_AREA_ADDRESS);
  277. SendCmdData(&UART_TxBuff_Struct3, MODE_REPORT, 0x07FF, 0x8002, (uint8_t*)"OK");
  278. break;
  279. }
  280. case 0x8600:
  281. {
  282. uint8_t SendData[4];
  283. CAN_Baudrate = FLASH_Read(CDLPROPERTIES_AREA_ADDRESS+12, 1);
  284. SendData[0] = (uint8_t)((CAN_Baudrate >> 24) & 0xFF);
  285. SendData[1] = (uint8_t)((CAN_Baudrate >> 16) & 0xFF);
  286. SendData[2] = (uint8_t)((CAN_Baudrate >> 8) & 0xFF);
  287. SendData[3] = (uint8_t)((CAN_Baudrate >> 0) & 0xFF);
  288. SendCmdData(&UART_TxBuff_Struct3, MODE_REPORT, 0x07FF, 0x8604, SendData);
  289. HAL_Delay(50);
  290. break;
  291. }
  292. case 0x8704:
  293. {
  294. CAN_Baudrate = (uint32_t)((Data[0]<<24) + (Data[1]<<16) + (Data[2]<<8) +(Data[3]));
  295. FLASH_WriteCDLProperties(CDLPROPERTIES_AREA_ADDRESS);
  296. SendCmdData(&UART_TxBuff_Struct3, MODE_REPORT, 0x07FF, 0x8002, (uint8_t*)"OK");
  297. break;
  298. }
  299. case 0x8800:
  300. {
  301. uint8_t SendData[4];
  302. HandingFlag_Enable = FLASH_Read(CDLPROPERTIES_AREA_ADDRESS+16, 1);
  303. SendData[0] = (uint8_t)((HandingFlag_Enable >> 24) & 0xFF);
  304. SendData[1] = (uint8_t)((HandingFlag_Enable >> 16) & 0xFF);
  305. SendData[2] = (uint8_t)((HandingFlag_Enable >> 8) & 0xFF);
  306. SendData[3] = (uint8_t)((HandingFlag_Enable >> 0) & 0xFF);
  307. SendCmdData(&UART_TxBuff_Struct3, MODE_REPORT, 0x07FF, 0x8804, SendData);
  308. HAL_Delay(50);
  309. break;
  310. }
  311. case 0x8904:
  312. {
  313. HandingFlag_Enable = (uint32_t)((Data[0]<<24) + (Data[1]<<16) + (Data[2]<<8) +(Data[3]));
  314. FLASH_WriteCDLProperties(CDLPROPERTIES_AREA_ADDRESS);
  315. SendCmdData(&UART_TxBuff_Struct3, MODE_REPORT, 0x07FF, 0x8002, (uint8_t*)"OK");
  316. break;
  317. }
  318. default:break;
  319. }
  320. }
  321. static void UART1_DataProcess(uint8_t Mode, uint16_t Command, uint8_t* Data)
  322. {
  323. }
  324. //发送数据
  325. void UART_SendSwitch_Lidian2(USART_Buf_TypeDef * ptTx, uint8_t Addr, uint16_t Cmd, uint8_t* Data)
  326. {
  327. uint8_t databuf[256] = {0};
  328. uint8_t i = 0;
  329. uint8_t len = 0;
  330. uint16_t checkxor = 0;
  331. len = 20;
  332. databuf[0] = Addr;
  333. databuf[1] = len;
  334. databuf[2] = (uint8_t)((Cmd >> 8) & 0xFF);
  335. for(i=0; i<(Cmd & 0xFF); i++)
  336. {
  337. databuf[3+i] = Data[i];
  338. }
  339. for(i=0; i<len; i++)
  340. {
  341. checkxor ^= databuf[i];
  342. }
  343. databuf[len-1] = checkxor;
  344. UART_SendData(ptTx, databuf, len);
  345. }
  346. void UART_SendSwitch_zhihuigainian(USART_Buf_TypeDef * ptTx, uint16_t Cmd, uint8_t* Data)
  347. {
  348. uint8_t databuf[256] = {0};
  349. uint8_t i = 0;
  350. uint8_t len = 0;
  351. uint16_t checkxor = 0;
  352. len = 13;
  353. databuf[0] = 0x55;
  354. for(i=0; i<(Cmd & 0xFF); i++)
  355. {
  356. databuf[1+i] = Data[i];
  357. }
  358. for(i=0; i<10; i=i+2)
  359. {
  360. checkxor ^= (uint16_t)((databuf[i]<<8) + databuf[i+1]);
  361. }
  362. databuf[len-3] = (uint8_t)(checkxor&0xff);
  363. databuf[len-2] = (uint8_t)(checkxor>>8);
  364. databuf[len-1] = 0xF0;
  365. UART_SendData(ptTx, databuf, len);
  366. }
  367. void UART_TransferData(USART_Buf_TypeDef * ptTx, uint8_t Addr, uint8_t Mode, uint16_t ID, uint16_t Cmd, uint8_t* Data)
  368. {
  369. uint8_t databuf[256] = {0};
  370. uint8_t i = 0;
  371. uint8_t len = 0;
  372. uint16_t checksum = 0;
  373. len = (uint8_t)((Cmd & 0xff) + 4);
  374. if(len > 247)
  375. {
  376. len = 247;
  377. }
  378. databuf[0] = UARTFRAME_BEGIN;
  379. databuf[1] = Addr;
  380. databuf[2] = Mode;
  381. databuf[3] = len;
  382. databuf[4] = (uint8_t)((ID >> 8) & 0xFF);
  383. databuf[5] = (uint8_t)(ID & 0xFF);
  384. databuf[6] = (uint8_t)((Cmd >> 8) & 0xFF);
  385. databuf[7] = (uint8_t)(Cmd & 0xFF);
  386. for(i=0; i<(len-4); i++)
  387. {
  388. databuf[8+i] = Data[i];
  389. }
  390. for(i=1; i<(len+4); i++)
  391. {
  392. checksum = (uint16_t)(databuf[i] + checksum);
  393. }
  394. databuf[4+len] = (uint8_t)checksum;
  395. databuf[5+len] = (uint8_t)(checksum>>8);
  396. databuf[6+len] = UARTFRAME_END1;
  397. databuf[7+len] = UARTFRAME_END2;
  398. UART_SendData(ptTx, databuf, len + 8);
  399. }
  400. void SendCmdData(USART_Buf_TypeDef * ptTx, uint8_t Mode, uint16_t ID, uint16_t Command, uint8_t* Data)
  401. {
  402. uint8_t SendBuf[255] = {0};
  403. uint8_t CRC_Buf[255] = {0};
  404. uint32_t CRC_Result = 0x00000000;
  405. uint8_t DataLength;
  406. DataLength = (uint8_t)(Command & 0xFF);
  407. SendBuf[0] = FRAME_BEGIN1;
  408. SendBuf[1] = FRAME_BEGIN2;
  409. SendBuf[2] = (uint8_t)((ID >> 8) & 0xFF);
  410. SendBuf[3] = (uint8_t)(ID & 0xFF);
  411. SendBuf[4] = Mode;
  412. SendBuf[5] = DataLength + 2;
  413. SendBuf[6] = (uint8_t)((Command >> 8) & 0xFF);
  414. SendBuf[7] = DataLength;
  415. memcpy(SendBuf + 8, Data, DataLength);
  416. memcpy(CRC_Buf, SendBuf, 8 + DataLength);
  417. CRC_Result = CRC32_Calculate(CRC_Buf, DataLength + 8);
  418. SendBuf[8 + DataLength] = (uint8_t)((CRC_Result >> 24) & 0xFF);
  419. SendBuf[9 + DataLength] = (uint8_t)((CRC_Result >> 16) & 0xFF);
  420. SendBuf[10 + DataLength] = (uint8_t)((CRC_Result >> 8) & 0xFF);
  421. SendBuf[11 + DataLength] = (uint8_t)(CRC_Result & 0xFF);
  422. SendBuf[12 + DataLength] = FRAME_END;
  423. UART_SendData(ptTx, SendBuf, DataLength + 13);
  424. }
  425. //串口1数据解析
  426. void Uart1_RxData_Process(USART_Buf_TypeDef* ptUartRx, uint16_t TimeOut)
  427. {
  428. uint8_t Mode, DataLength, Addr;
  429. static uint8_t Data[255];
  430. uint16_t Cmd, i, ID;
  431. uint16_t CheckSumResult=0, CheckSumData=0;
  432. uint8_t FrameBegin, FrameEnd1, FrameEnd2;
  433. static uint32_t DelayTimeCnt = 0;
  434. static TrueOrFalse_Flag_Struct_t IsWaitRX_Flag = FALSE;
  435. if(ptUartRx->ucBufCnt >= 8)
  436. {
  437. //读取帧头
  438. FrameBegin = UART_ReadChar(ptUartRx, 0);
  439. if(FrameBegin == UARTFRAME_BEGIN)
  440. {
  441. Addr = UART_ReadChar(ptUartRx, 1);//读取地址ID
  442. if(Addr == UARTADDR_CDL)
  443. {
  444. //读取帧模式
  445. Mode = UART_ReadChar(ptUartRx, 2);
  446. if((Mode == UARTMODE_READ) || (Mode == UARTMODE_WRITE) || (Mode == UARTMODE_REPORT))
  447. {
  448. //读取数据段长度
  449. DataLength = UART_ReadChar(ptUartRx, 3);
  450. if(ptUartRx->ucBufCnt < (DataLength + 8))//帧头1byte + 地址1byte + 模式1byte + 长度1byte + 校验位2bytes + 帧尾2bytes
  451. {
  452. if(IsWaitRX_Flag == FALSE)
  453. {
  454. DelayTimeCnt = HAL_GetTick();
  455. IsWaitRX_Flag = TRUE;
  456. }
  457. if((HAL_GetTick() - DelayTimeCnt) > TimeOut)//超时,单位ms
  458. {
  459. UART_DelChar(ptUartRx, ptUartRx->ucBufCnt);
  460. IsWaitRX_Flag = FALSE;
  461. }
  462. return;
  463. }
  464. else
  465. {
  466. T_RecvUartMsg = HAL_GetTick();
  467. IsWaitRX_Flag = FALSE;
  468. //接收到完整正确数据包
  469. for(i=0; i<(DataLength+4); i++)//读取数据段
  470. {
  471. Data[i] = UART_ReadChar(ptUartRx, i);
  472. }
  473. CheckSumData = (UART_ReadChar(ptUartRx, 4 + DataLength)) + \
  474. (UART_ReadChar(ptUartRx, 5 + DataLength) << 8);
  475. for(i = 1; i<(DataLength+4); i++)
  476. {
  477. CheckSumResult = (uint16_t)(Data[i] + CheckSumResult);
  478. }
  479. FrameEnd1 = UART_ReadChar(ptUartRx, 6 + DataLength);
  480. FrameEnd2 = UART_ReadChar(ptUartRx, 7 + DataLength);
  481. if((CheckSumData == CheckSumResult) && (FrameEnd1 == UARTFRAME_END1) && (FrameEnd2 == UARTFRAME_END2))// 比较校验和帧尾
  482. {
  483. //数据处理
  484. UART1_DataProcess(Mode, Cmd, &Data[4]);//Mode为帧模式,Cmd为命令字,Data为数据段
  485. if(DataLength >= 4)//数据段包含ID+Cmd,至少4bytes
  486. {
  487. ID = (uint16_t)((Data[4]<<8) + Data[5]);
  488. Cmd = (uint16_t)((Data[6]<<8) + Data[7]);
  489. SendCmdData(&UART_TxBuff_Struct3, Mode, ID, Cmd, &Data[8]);
  490. }
  491. if((Cmd==0xC109) || (Cmd==0xC202) || (Cmd==0xC302) || (Cmd==0xC402) || (Cmd==0xC502))
  492. {
  493. T_UART1_BaudrateSwitch = HAL_GetTick();
  494. }
  495. //清除缓存
  496. UART_DelChar(ptUartRx, DataLength + 8);
  497. //通信指示
  498. HAL_GPIO_TogglePin(LED_Display_GPIO_Port, LED_Display_Pin);
  499. }
  500. else
  501. {
  502. UART_DelChar(ptUartRx, 1);
  503. }
  504. }
  505. }
  506. else
  507. {
  508. UART_DelChar(ptUartRx, 1);
  509. }
  510. }
  511. else
  512. {
  513. UART_DelChar(ptUartRx, 1);
  514. }
  515. }
  516. else
  517. {
  518. UART_DelChar(ptUartRx, 1);
  519. }
  520. }
  521. }
  522. //串口3数据解析
  523. void Uart3_RxData_Process(USART_Buf_TypeDef* ptUartRx, uint16_t TimeOut)
  524. {
  525. uint8_t Mode, CmdLength, DataLength;
  526. static uint8_t Data[255], CRC_Buf[255];
  527. uint16_t Cmd, i, ID;
  528. uint32_t CrcResult, CrcData;
  529. uint8_t FrameBegin1, FrameBegin2;
  530. static uint32_t DelayTimeCnt = 0;
  531. static TrueOrFalse_Flag_Struct_t IsWaitRX_Flag = FALSE;
  532. if(ptUartRx->ucBufCnt >= 13)
  533. {
  534. //读取帧头
  535. FrameBegin1 = UART_ReadChar(ptUartRx, 0);
  536. CRC_Buf[0] = FrameBegin1;
  537. FrameBegin2 = UART_ReadChar(ptUartRx, 1);
  538. CRC_Buf[1] = FrameBegin2;
  539. if((FrameBegin1 == FRAME_BEGIN1) && (FrameBegin2 == FRAME_BEGIN2))
  540. {
  541. CRC_Buf[2] = UART_ReadChar(ptUartRx, 2);
  542. CRC_Buf[3] = UART_ReadChar(ptUartRx, 3);
  543. ID = (uint16_t)(CRC_Buf[2] << 8) + CRC_Buf[3];
  544. //读取帧模式
  545. Mode = UART_ReadChar(ptUartRx, 4);
  546. CRC_Buf[4] = Mode;
  547. if((Mode == MODE_READ) || (Mode == MODE_WRITE) || (Mode == MODE_REPORT))
  548. {
  549. //读取命令段长度和命令字
  550. CmdLength = UART_ReadChar(ptUartRx, 5);
  551. CRC_Buf[5] = CmdLength;
  552. Cmd = (UART_ReadChar(ptUartRx, 6) << 8) + UART_ReadChar(ptUartRx, 7);
  553. CRC_Buf[6] = (uint8_t)((Cmd >> 8) & 0xFF);
  554. CRC_Buf[7] = (uint8_t)(Cmd & 0xFF);
  555. DataLength = UART_ReadChar(ptUartRx, 7);
  556. if((CmdLength - DataLength) == 2)
  557. {
  558. if(ptUartRx->ucBufCnt < (CmdLength + 11))//帧头2bytes + ID2bytes 模式1byte + 命令段长度1byte + 校验位4bytes + 帧尾1byte
  559. {
  560. if(IsWaitRX_Flag == FALSE)
  561. {
  562. DelayTimeCnt = HAL_GetTick();
  563. IsWaitRX_Flag = TRUE;
  564. }
  565. if((HAL_GetTick() - DelayTimeCnt) > TimeOut)//超时,单位ms
  566. {
  567. UART_DelChar(ptUartRx, ptUartRx->ucBufCnt);
  568. IsWaitRX_Flag = FALSE;
  569. }
  570. return;
  571. }
  572. else
  573. {
  574. IsWaitRX_Flag = FALSE;
  575. //接收到完整正确数据包
  576. for(i=0; i<DataLength; i++)//读取数据段
  577. {
  578. Data[i] = UART_ReadChar(ptUartRx, 8 + i);
  579. CRC_Buf[8 + i] = Data[i];
  580. }
  581. CrcData = (UART_ReadChar(ptUartRx, 8 + DataLength) << 24) + \
  582. (UART_ReadChar(ptUartRx, 9 + DataLength) << 16) + \
  583. (UART_ReadChar(ptUartRx, 10 + DataLength) << 8) + \
  584. UART_ReadChar(ptUartRx, 11 + DataLength);
  585. CrcResult = CRC32_Calculate(CRC_Buf, 8 + DataLength);
  586. if((CrcData - CrcResult) == 0) // 比较校验
  587. {
  588. //数据处理
  589. UART3_DataProcess(Mode, Cmd, Data);//Mode为帧模式,Cmd为命令字,Data为数据段
  590. //数据转发CAN
  591. memcpy((uint8_t*)(CRC_Buf + 2), (uint8_t*)(CRC_Buf + 4), DataLength + 4);
  592. CRC_Buf[6 + DataLength] = UART_ReadChar(ptUartRx, 8 + DataLength);
  593. CRC_Buf[7 + DataLength] = UART_ReadChar(ptUartRx, 9 + DataLength);
  594. CRC_Buf[8 + DataLength] = UART_ReadChar(ptUartRx, 10 + DataLength);
  595. CRC_Buf[9 + DataLength] = UART_ReadChar(ptUartRx, 11 + DataLength);
  596. CRC_Buf[10 + DataLength] = FRAME_END;
  597. CAN_SendData(ID, CRC_Buf, DataLength + 11);
  598. UART_TransferData(&UART_TxBuff_Struct1, UARTADDR_CDL, Mode, ID, Cmd, Data);
  599. //清除缓存
  600. UART_DelChar(ptUartRx, CmdLength + 11);
  601. //通信指示
  602. HAL_GPIO_TogglePin(LED_Display_GPIO_Port, LED_Display_Pin);
  603. return;
  604. }
  605. UART_DelChar(ptUartRx, 1);
  606. }
  607. }
  608. else
  609. {
  610. UART_DelChar(ptUartRx, 1);
  611. }
  612. }
  613. else
  614. {
  615. UART_DelChar(ptUartRx, 1);
  616. }
  617. }
  618. else
  619. {
  620. UART_DelChar(ptUartRx, 1);
  621. }
  622. }
  623. }