uart_process.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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 0x4500:
  164. {
  165. //重启
  166. __set_FAULTMASK(1);//关闭所有中断
  167. HAL_NVIC_SystemReset();
  168. break;
  169. }
  170. case 0x2505:
  171. {
  172. if (strncmp("RESET", (char *)Data, 5) == 0)
  173. {
  174. // UART_TransferData(&UART_TxBuff_Struct1, UARTID_CDL_TO_MC, Mode, 0x751, Command, Data);
  175. // HAL_Delay(200);
  176. //
  177. // __set_FAULTMASK(1);//关闭所有中断
  178. // HAL_NVIC_SystemReset();
  179. T_UART1_BaudrateSwitch = HAL_GetTick();
  180. UART1_BaudrateSwitch = 2;
  181. }
  182. break;
  183. }
  184. //随机码和密钥进行校验
  185. case 0x5514:
  186. {
  187. static uint8_t InputCode[12];
  188. static uint8_t InputKey[8];
  189. static uint8_t OutputCode[12];
  190. static uint8_t SendData[27];
  191. memcpy(InputCode, Data, 12);
  192. memcpy(InputKey, Data + 12, 8);
  193. CheckCodeCal(InputCode, InputKey, OutputCode);
  194. memcpy(SendData, OutputCode, 12);
  195. memcpy(SendData + 12, (uint8_t*)"V4.1.2_20240906", 15);
  196. SendCmdData(&UART_TxBuff_Struct3, MODE_REPORT, 0x07FF, 0x551B, SendData);
  197. break;
  198. }
  199. //授权
  200. case 0x6600:
  201. {
  202. uint8_t SendData[24];
  203. Ctf_CalAndSave();
  204. memcpy(SendData, MAC_ID, 12);
  205. memcpy(SendData + 12, Check_Code, 12);
  206. SendCmdData(&UART_TxBuff_Struct3, MODE_REPORT, 0x07FF, 0x6618, SendData);
  207. HAL_Delay(200);
  208. __set_FAULTMASK(1);//关闭所有中断
  209. HAL_NVIC_SystemReset();
  210. break;
  211. }
  212. case 0x7702:
  213. {
  214. UART_SendSwitch_Lidian2(&UART_TxBuff_Struct1, 0x01, Command, Data);
  215. HAL_Delay(50);
  216. UART_SendSwitch_zhihuigainian(&UART_TxBuff_Struct1, Command, Data);
  217. HAL_Delay(50);
  218. break;
  219. }
  220. case 0x8000:
  221. {
  222. uint8_t SendData[4];
  223. Transmit_UARTOrCAN = (uint8_t)FLASH_Read(CDLPROPERTIES_AREA_ADDRESS, 1);
  224. SendData[0] = 0;
  225. SendData[1] = 0;
  226. SendData[2] = 0;
  227. SendData[3] = Transmit_UARTOrCAN;
  228. SendCmdData(&UART_TxBuff_Struct3, MODE_REPORT, 0x07FF, 0x8004, SendData);
  229. HAL_Delay(50);
  230. break;
  231. }
  232. case 0x8104:
  233. {
  234. Transmit_UARTOrCAN = Data[3];
  235. FLASH_WriteCDLProperties(CDLPROPERTIES_AREA_ADDRESS);
  236. SendCmdData(&UART_TxBuff_Struct3, MODE_REPORT, 0x07FF, 0x8002, (uint8_t*)"OK");
  237. break;
  238. }
  239. case 0x8200:
  240. {
  241. uint8_t SendData[4];
  242. Uart_BootBaudrate = FLASH_Read(CDLPROPERTIES_AREA_ADDRESS+4, 1);
  243. SendData[0] = (uint8_t)((Uart_BootBaudrate >> 24) & 0xFF);
  244. SendData[1] = (uint8_t)((Uart_BootBaudrate >> 16) & 0xFF);
  245. SendData[2] = (uint8_t)((Uart_BootBaudrate >> 8) & 0xFF);
  246. SendData[3] = (uint8_t)((Uart_BootBaudrate >> 0) & 0xFF);
  247. SendCmdData(&UART_TxBuff_Struct3, MODE_REPORT, 0x07FF, 0x8204, SendData);
  248. HAL_Delay(50);
  249. break;
  250. }
  251. case 0x8304:
  252. {
  253. Uart_BootBaudrate = (uint32_t)((Data[0]<<24) + (Data[1]<<16) + (Data[2]<<8) +(Data[3]));
  254. FLASH_WriteCDLProperties(CDLPROPERTIES_AREA_ADDRESS);
  255. SendCmdData(&UART_TxBuff_Struct3, MODE_REPORT, 0x07FF, 0x8002, (uint8_t*)"OK");
  256. break;
  257. }
  258. case 0x8400:
  259. {
  260. uint8_t SendData[4];
  261. Uart_AppBaudrate = FLASH_Read(CDLPROPERTIES_AREA_ADDRESS+8, 1);
  262. SendData[0] = (uint8_t)((Uart_AppBaudrate >> 24) & 0xFF);
  263. SendData[1] = (uint8_t)((Uart_AppBaudrate >> 16) & 0xFF);
  264. SendData[2] = (uint8_t)((Uart_AppBaudrate >> 8) & 0xFF);
  265. SendData[3] = (uint8_t)((Uart_AppBaudrate >> 0) & 0xFF);
  266. SendCmdData(&UART_TxBuff_Struct3, MODE_REPORT, 0x07FF, 0x8404, SendData);
  267. HAL_Delay(50);
  268. break;
  269. }
  270. case 0x8504:
  271. {
  272. Uart_AppBaudrate = (uint32_t)((Data[0]<<24) + (Data[1]<<16) + (Data[2]<<8) +(Data[3]));
  273. FLASH_WriteCDLProperties(CDLPROPERTIES_AREA_ADDRESS);
  274. SendCmdData(&UART_TxBuff_Struct3, MODE_REPORT, 0x07FF, 0x8002, (uint8_t*)"OK");
  275. break;
  276. }
  277. case 0x8600:
  278. {
  279. uint8_t SendData[4];
  280. CAN_Baudrate = FLASH_Read(CDLPROPERTIES_AREA_ADDRESS+12, 1);
  281. SendData[0] = (uint8_t)((CAN_Baudrate >> 24) & 0xFF);
  282. SendData[1] = (uint8_t)((CAN_Baudrate >> 16) & 0xFF);
  283. SendData[2] = (uint8_t)((CAN_Baudrate >> 8) & 0xFF);
  284. SendData[3] = (uint8_t)((CAN_Baudrate >> 0) & 0xFF);
  285. SendCmdData(&UART_TxBuff_Struct3, MODE_REPORT, 0x07FF, 0x8604, SendData);
  286. HAL_Delay(50);
  287. break;
  288. }
  289. case 0x8704:
  290. {
  291. CAN_Baudrate = (uint32_t)((Data[0]<<24) + (Data[1]<<16) + (Data[2]<<8) +(Data[3]));
  292. FLASH_WriteCDLProperties(CDLPROPERTIES_AREA_ADDRESS);
  293. SendCmdData(&UART_TxBuff_Struct3, MODE_REPORT, 0x07FF, 0x8002, (uint8_t*)"OK");
  294. break;
  295. }
  296. case 0x8800:
  297. {
  298. uint8_t SendData[4];
  299. HandingFlag_Enable = FLASH_Read(CDLPROPERTIES_AREA_ADDRESS+16, 1);
  300. SendData[0] = (uint8_t)((HandingFlag_Enable >> 24) & 0xFF);
  301. SendData[1] = (uint8_t)((HandingFlag_Enable >> 16) & 0xFF);
  302. SendData[2] = (uint8_t)((HandingFlag_Enable >> 8) & 0xFF);
  303. SendData[3] = (uint8_t)((HandingFlag_Enable >> 0) & 0xFF);
  304. SendCmdData(&UART_TxBuff_Struct3, MODE_REPORT, 0x07FF, 0x8804, SendData);
  305. HAL_Delay(50);
  306. break;
  307. }
  308. case 0x8904:
  309. {
  310. HandingFlag_Enable = (uint32_t)((Data[0]<<24) + (Data[1]<<16) + (Data[2]<<8) +(Data[3]));
  311. FLASH_WriteCDLProperties(CDLPROPERTIES_AREA_ADDRESS);
  312. SendCmdData(&UART_TxBuff_Struct3, MODE_REPORT, 0x07FF, 0x8002, (uint8_t*)"OK");
  313. break;
  314. }
  315. default:break;
  316. }
  317. }
  318. static void UART1_DataProcess(uint8_t Mode, uint16_t Command, uint8_t* Data)
  319. {
  320. }
  321. //发送数据
  322. void UART_SendSwitch_Lidian2(USART_Buf_TypeDef * ptTx, uint8_t Addr, uint16_t Cmd, uint8_t* Data)
  323. {
  324. uint8_t databuf[256] = {0};
  325. uint8_t i = 0;
  326. uint8_t len = 0;
  327. uint16_t checkxor = 0;
  328. len = 20;
  329. databuf[0] = Addr;
  330. databuf[1] = len;
  331. databuf[2] = (uint8_t)((Cmd >> 8) & 0xFF);
  332. for(i=0; i<(Cmd & 0xFF); i++)
  333. {
  334. databuf[3+i] = Data[i];
  335. }
  336. for(i=0; i<len; i++)
  337. {
  338. checkxor ^= databuf[i];
  339. }
  340. databuf[len-1] = checkxor;
  341. UART_SendData(ptTx, databuf, len);
  342. }
  343. void UART_SendSwitch_zhihuigainian(USART_Buf_TypeDef * ptTx, uint16_t Cmd, uint8_t* Data)
  344. {
  345. uint8_t databuf[256] = {0};
  346. uint8_t i = 0;
  347. uint8_t len = 0;
  348. uint16_t checkxor = 0;
  349. len = 13;
  350. databuf[0] = 0x55;
  351. for(i=0; i<(Cmd & 0xFF); i++)
  352. {
  353. databuf[1+i] = Data[i];
  354. }
  355. for(i=0; i<10; i=i+2)
  356. {
  357. checkxor ^= (uint16_t)((databuf[i]<<8) + databuf[i+1]);
  358. }
  359. databuf[len-3] = (uint8_t)(checkxor&0xff);
  360. databuf[len-2] = (uint8_t)(checkxor>>8);
  361. databuf[len-1] = 0xF0;
  362. UART_SendData(ptTx, databuf, len);
  363. }
  364. void UART_TransferData(USART_Buf_TypeDef * ptTx, uint8_t Addr, uint8_t Mode, uint16_t ID, uint16_t Cmd, uint8_t* Data)
  365. {
  366. uint8_t databuf[256] = {0};
  367. uint8_t i = 0;
  368. uint8_t len = 0;
  369. uint16_t checksum = 0;
  370. len = (uint8_t)((Cmd & 0xff) + 4);
  371. if(len > 247)
  372. {
  373. len = 247;
  374. }
  375. databuf[0] = UARTFRAME_BEGIN;
  376. databuf[1] = Addr;
  377. databuf[2] = Mode;
  378. databuf[3] = len;
  379. databuf[4] = (uint8_t)((ID >> 8) & 0xFF);
  380. databuf[5] = (uint8_t)(ID & 0xFF);
  381. databuf[6] = (uint8_t)((Cmd >> 8) & 0xFF);
  382. databuf[7] = (uint8_t)(Cmd & 0xFF);
  383. for(i=0; i<(len-4); i++)
  384. {
  385. databuf[8+i] = Data[i];
  386. }
  387. for(i=1; i<(len+4); i++)
  388. {
  389. checksum = (uint16_t)(databuf[i] + checksum);
  390. }
  391. databuf[4+len] = (uint8_t)checksum;
  392. databuf[5+len] = (uint8_t)(checksum>>8);
  393. databuf[6+len] = UARTFRAME_END1;
  394. databuf[7+len] = UARTFRAME_END2;
  395. UART_SendData(ptTx, databuf, len + 8);
  396. }
  397. void SendCmdData(USART_Buf_TypeDef * ptTx, uint8_t Mode, uint16_t ID, uint16_t Command, uint8_t* Data)
  398. {
  399. uint8_t SendBuf[255] = {0};
  400. uint8_t CRC_Buf[255] = {0};
  401. uint32_t CRC_Result = 0x00000000;
  402. uint8_t DataLength;
  403. DataLength = (uint8_t)(Command & 0xFF);
  404. SendBuf[0] = FRAME_BEGIN1;
  405. SendBuf[1] = FRAME_BEGIN2;
  406. SendBuf[2] = (uint8_t)((ID >> 8) & 0xFF);
  407. SendBuf[3] = (uint8_t)(ID & 0xFF);
  408. SendBuf[4] = Mode;
  409. SendBuf[5] = DataLength + 2;
  410. SendBuf[6] = (uint8_t)((Command >> 8) & 0xFF);
  411. SendBuf[7] = DataLength;
  412. memcpy(SendBuf + 8, Data, DataLength);
  413. memcpy(CRC_Buf, SendBuf, 8 + DataLength);
  414. CRC_Result = CRC32_Calculate(CRC_Buf, DataLength + 8);
  415. SendBuf[8 + DataLength] = (uint8_t)((CRC_Result >> 24) & 0xFF);
  416. SendBuf[9 + DataLength] = (uint8_t)((CRC_Result >> 16) & 0xFF);
  417. SendBuf[10 + DataLength] = (uint8_t)((CRC_Result >> 8) & 0xFF);
  418. SendBuf[11 + DataLength] = (uint8_t)(CRC_Result & 0xFF);
  419. SendBuf[12 + DataLength] = FRAME_END;
  420. UART_SendData(ptTx, SendBuf, DataLength + 13);
  421. }
  422. //串口1数据解析
  423. void Uart1_RxData_Process(USART_Buf_TypeDef* ptUartRx, uint16_t TimeOut)
  424. {
  425. uint8_t Mode, DataLength, Addr;
  426. static uint8_t Data[255];
  427. uint16_t Cmd, i, ID;
  428. uint16_t CheckSumResult=0, CheckSumData=0;
  429. uint8_t FrameBegin, FrameEnd1, FrameEnd2;
  430. static uint32_t DelayTimeCnt = 0;
  431. static TrueOrFalse_Flag_Struct_t IsWaitRX_Flag = FALSE;
  432. if(ptUartRx->ucBufCnt >= 8)
  433. {
  434. //读取帧头
  435. FrameBegin = UART_ReadChar(ptUartRx, 0);
  436. if(FrameBegin == UARTFRAME_BEGIN)
  437. {
  438. Addr = UART_ReadChar(ptUartRx, 1);//读取地址ID
  439. if(Addr == UARTADDR_CDL)
  440. {
  441. //读取帧模式
  442. Mode = UART_ReadChar(ptUartRx, 2);
  443. if((Mode == UARTMODE_READ) || (Mode == UARTMODE_WRITE) || (Mode == UARTMODE_REPORT))
  444. {
  445. //读取数据段长度
  446. DataLength = UART_ReadChar(ptUartRx, 3);
  447. if(ptUartRx->ucBufCnt < (DataLength + 8))//帧头1byte + 地址1byte + 模式1byte + 长度1byte + 校验位2bytes + 帧尾2bytes
  448. {
  449. if(IsWaitRX_Flag == FALSE)
  450. {
  451. DelayTimeCnt = HAL_GetTick();
  452. IsWaitRX_Flag = TRUE;
  453. }
  454. if((HAL_GetTick() - DelayTimeCnt) > TimeOut)//超时,单位ms
  455. {
  456. UART_DelChar(ptUartRx, ptUartRx->ucBufCnt);
  457. IsWaitRX_Flag = FALSE;
  458. }
  459. return;
  460. }
  461. else
  462. {
  463. T_RecvUartMsg = HAL_GetTick();
  464. IsWaitRX_Flag = FALSE;
  465. //接收到完整正确数据包
  466. for(i=0; i<(DataLength+4); i++)//读取数据段
  467. {
  468. Data[i] = UART_ReadChar(ptUartRx, i);
  469. }
  470. CheckSumData = (UART_ReadChar(ptUartRx, 4 + DataLength)) + \
  471. (UART_ReadChar(ptUartRx, 5 + DataLength) << 8);
  472. for(i = 1; i<(DataLength+4); i++)
  473. {
  474. CheckSumResult = (uint16_t)(Data[i] + CheckSumResult);
  475. }
  476. FrameEnd1 = UART_ReadChar(ptUartRx, 6 + DataLength);
  477. FrameEnd2 = UART_ReadChar(ptUartRx, 7 + DataLength);
  478. if((CheckSumData == CheckSumResult) && (FrameEnd1 == UARTFRAME_END1) && (FrameEnd2 == UARTFRAME_END2))// 比较校验和帧尾
  479. {
  480. //数据处理
  481. UART1_DataProcess(Mode, Cmd, &Data[4]);//Mode为帧模式,Cmd为命令字,Data为数据段
  482. if(DataLength >= 4)//数据段包含ID+Cmd,至少4bytes
  483. {
  484. ID = (uint16_t)((Data[4]<<8) + Data[5]);
  485. Cmd = (uint16_t)((Data[6]<<8) + Data[7]);
  486. SendCmdData(&UART_TxBuff_Struct3, Mode, ID, Cmd, &Data[8]);
  487. }
  488. if((Cmd==0xC109) || (Cmd==0xC202) || (Cmd==0xC302) || (Cmd==0xC402) || (Cmd==0xC502))
  489. {
  490. T_UART1_BaudrateSwitch = HAL_GetTick();
  491. }
  492. //清除缓存
  493. UART_DelChar(ptUartRx, DataLength + 8);
  494. //通信指示
  495. HAL_GPIO_TogglePin(LED_Display_GPIO_Port, LED_Display_Pin);
  496. }
  497. else
  498. {
  499. UART_DelChar(ptUartRx, 1);
  500. }
  501. }
  502. }
  503. else
  504. {
  505. UART_DelChar(ptUartRx, 1);
  506. }
  507. }
  508. else
  509. {
  510. UART_DelChar(ptUartRx, 1);
  511. }
  512. }
  513. else
  514. {
  515. UART_DelChar(ptUartRx, 1);
  516. }
  517. }
  518. }
  519. //串口3数据解析
  520. void Uart3_RxData_Process(USART_Buf_TypeDef* ptUartRx, uint16_t TimeOut)
  521. {
  522. uint8_t Mode, CmdLength, DataLength;
  523. static uint8_t Data[255], CRC_Buf[255];
  524. uint16_t Cmd, i, ID;
  525. uint32_t CrcResult, CrcData;
  526. uint8_t FrameBegin1, FrameBegin2;
  527. static uint32_t DelayTimeCnt = 0;
  528. static TrueOrFalse_Flag_Struct_t IsWaitRX_Flag = FALSE;
  529. if(ptUartRx->ucBufCnt >= 13)
  530. {
  531. //读取帧头
  532. FrameBegin1 = UART_ReadChar(ptUartRx, 0);
  533. CRC_Buf[0] = FrameBegin1;
  534. FrameBegin2 = UART_ReadChar(ptUartRx, 1);
  535. CRC_Buf[1] = FrameBegin2;
  536. if((FrameBegin1 == FRAME_BEGIN1) && (FrameBegin2 == FRAME_BEGIN2))
  537. {
  538. CRC_Buf[2] = UART_ReadChar(ptUartRx, 2);
  539. CRC_Buf[3] = UART_ReadChar(ptUartRx, 3);
  540. ID = (uint16_t)(CRC_Buf[2] << 8) + CRC_Buf[3];
  541. //读取帧模式
  542. Mode = UART_ReadChar(ptUartRx, 4);
  543. CRC_Buf[4] = Mode;
  544. if((Mode == MODE_READ) || (Mode == MODE_WRITE) || (Mode == MODE_REPORT))
  545. {
  546. //读取命令段长度和命令字
  547. CmdLength = UART_ReadChar(ptUartRx, 5);
  548. CRC_Buf[5] = CmdLength;
  549. Cmd = (UART_ReadChar(ptUartRx, 6) << 8) + UART_ReadChar(ptUartRx, 7);
  550. CRC_Buf[6] = (uint8_t)((Cmd >> 8) & 0xFF);
  551. CRC_Buf[7] = (uint8_t)(Cmd & 0xFF);
  552. DataLength = UART_ReadChar(ptUartRx, 7);
  553. if((CmdLength - DataLength) == 2)
  554. {
  555. if(ptUartRx->ucBufCnt < (CmdLength + 11))//帧头2bytes + ID2bytes 模式1byte + 命令段长度1byte + 校验位4bytes + 帧尾1byte
  556. {
  557. if(IsWaitRX_Flag == FALSE)
  558. {
  559. DelayTimeCnt = HAL_GetTick();
  560. IsWaitRX_Flag = TRUE;
  561. }
  562. if((HAL_GetTick() - DelayTimeCnt) > TimeOut)//超时,单位ms
  563. {
  564. UART_DelChar(ptUartRx, ptUartRx->ucBufCnt);
  565. IsWaitRX_Flag = FALSE;
  566. }
  567. return;
  568. }
  569. else
  570. {
  571. IsWaitRX_Flag = FALSE;
  572. //接收到完整正确数据包
  573. for(i=0; i<DataLength; i++)//读取数据段
  574. {
  575. Data[i] = UART_ReadChar(ptUartRx, 8 + i);
  576. CRC_Buf[8 + i] = Data[i];
  577. }
  578. CrcData = (UART_ReadChar(ptUartRx, 8 + DataLength) << 24) + \
  579. (UART_ReadChar(ptUartRx, 9 + DataLength) << 16) + \
  580. (UART_ReadChar(ptUartRx, 10 + DataLength) << 8) + \
  581. UART_ReadChar(ptUartRx, 11 + DataLength);
  582. CrcResult = CRC32_Calculate(CRC_Buf, 8 + DataLength);
  583. if((CrcData - CrcResult) == 0) // 比较校验
  584. {
  585. //数据处理
  586. UART3_DataProcess(Mode, Cmd, Data);//Mode为帧模式,Cmd为命令字,Data为数据段
  587. //数据转发CAN
  588. memcpy((uint8_t*)(CRC_Buf + 2), (uint8_t*)(CRC_Buf + 4), DataLength + 4);
  589. CRC_Buf[6 + DataLength] = UART_ReadChar(ptUartRx, 8 + DataLength);
  590. CRC_Buf[7 + DataLength] = UART_ReadChar(ptUartRx, 9 + DataLength);
  591. CRC_Buf[8 + DataLength] = UART_ReadChar(ptUartRx, 10 + DataLength);
  592. CRC_Buf[9 + DataLength] = UART_ReadChar(ptUartRx, 11 + DataLength);
  593. CRC_Buf[10 + DataLength] = FRAME_END;
  594. CAN_SendData(ID, CRC_Buf, DataLength + 11);
  595. UART_TransferData(&UART_TxBuff_Struct1, UARTADDR_CDL, Mode, ID, Cmd, Data);
  596. //清除缓存
  597. UART_DelChar(ptUartRx, CmdLength + 11);
  598. //通信指示
  599. HAL_GPIO_TogglePin(LED_Display_GPIO_Port, LED_Display_Pin);
  600. return;
  601. }
  602. UART_DelChar(ptUartRx, 1);
  603. }
  604. }
  605. else
  606. {
  607. UART_DelChar(ptUartRx, 1);
  608. }
  609. }
  610. else
  611. {
  612. UART_DelChar(ptUartRx, 1);
  613. }
  614. }
  615. else
  616. {
  617. UART_DelChar(ptUartRx, 1);
  618. }
  619. }
  620. }