usart.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. /**
  2. ******************************************************************************
  3. * File Name : USART.c
  4. * Description : This file provides code for the configuration
  5. * of the USART instances.
  6. ******************************************************************************
  7. ** This notice applies to any and all portions of this file
  8. * that are not between comment pairs USER CODE BEGIN and
  9. * USER CODE END. Other portions of this file, whether
  10. * inserted by the user or by software development tools
  11. * are owned by their respective copyright owners.
  12. *
  13. * COPYRIGHT(c) 2019 STMicroelectronics
  14. *
  15. * Redistribution and use in source and binary forms, with or without modification,
  16. * are permitted provided that the following conditions are met:
  17. * 1. Redistributions of source code must retain the above copyright notice,
  18. * this list of conditions and the following disclaimer.
  19. * 2. Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  23. * may be used to endorse or promote products derived from this software
  24. * without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  29. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  30. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  32. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  33. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  34. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. ******************************************************************************
  38. */
  39. /* Includes ------------------------------------------------------------------*/
  40. #include "usart.h"
  41. #include "gpio.h"
  42. /* USER CODE BEGIN 0 */
  43. #include "crc_cal.h"
  44. #include "tasks.h"
  45. uint8_t USART1_RxBuffer[256];
  46. USART_Buf_TypeDef UART_RxBuff_Struct1 = {255,0,0,0,0,USART1_RxBuffer,&huart1};
  47. uint8_t USART1_TxBuffer[256];
  48. USART_Buf_TypeDef UART_TxBuff_Struct1 = {255,0,0,0,0,USART1_TxBuffer,&huart1};
  49. /* USER CODE END 0 */
  50. UART_HandleTypeDef huart1;
  51. /* USART1 init function */
  52. void MX_USART1_UART_Init(void)
  53. {
  54. huart1.Instance = USART1;
  55. huart1.Init.BaudRate = 9600;
  56. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  57. huart1.Init.StopBits = UART_STOPBITS_1;
  58. huart1.Init.Parity = UART_PARITY_NONE;
  59. huart1.Init.Mode = UART_MODE_TX_RX;
  60. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  61. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  62. if (HAL_UART_Init(&huart1) != HAL_OK)
  63. {
  64. _Error_Handler(__FILE__, __LINE__);
  65. }
  66. }
  67. void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
  68. {
  69. GPIO_InitTypeDef GPIO_InitStruct;
  70. if(uartHandle->Instance==USART1)
  71. {
  72. /* USER CODE BEGIN USART1_MspInit 0 */
  73. /* USER CODE END USART1_MspInit 0 */
  74. /* USART1 clock enable */
  75. __HAL_RCC_USART1_CLK_ENABLE();
  76. /**USART1 GPIO Configuration
  77. PA9 ------> USART1_TX
  78. PA10 ------> USART1_RX
  79. */
  80. GPIO_InitStruct.Pin = GPIO_PIN_9;
  81. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  82. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  83. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  84. GPIO_InitStruct.Pin = GPIO_PIN_10;
  85. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  86. GPIO_InitStruct.Pull = GPIO_NOPULL;
  87. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  88. /* USART1 interrupt Init */
  89. HAL_NVIC_SetPriority(USART1_IRQn, 1, 1);
  90. HAL_NVIC_EnableIRQ(USART1_IRQn);
  91. /* USER CODE BEGIN USART1_MspInit 1 */
  92. // __HAL_UART_ENABLE_IT(&huart1, UART_IT_PE);
  93. /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
  94. // __HAL_UART_ENABLE_IT(&huart1, UART_IT_ERR);
  95. /* Enable the UART Data Register not empty Interrupt */
  96. __HAL_UART_ENABLE_IT(&huart1, UART_IT_RXNE);
  97. /* USER CODE END USART1_MspInit 1 */
  98. }
  99. }
  100. void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
  101. {
  102. if(uartHandle->Instance==USART1)
  103. {
  104. /* USER CODE BEGIN USART1_MspDeInit 0 */
  105. /* USER CODE END USART1_MspDeInit 0 */
  106. /* Peripheral clock disable */
  107. __HAL_RCC_USART1_CLK_DISABLE();
  108. /**USART1 GPIO Configuration
  109. PA9 ------> USART1_TX
  110. PA10 ------> USART1_RX
  111. */
  112. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10);
  113. /* USART1 interrupt Deinit */
  114. HAL_NVIC_DisableIRQ(USART1_IRQn);
  115. /* USER CODE BEGIN USART1_MspDeInit 1 */
  116. /* USER CODE END USART1_MspDeInit 1 */
  117. }
  118. }
  119. /* USER CODE BEGIN 1 */
  120. uint8_t UART_GetChar(USART_Buf_TypeDef * ptRx)
  121. {
  122. uint8_t ucData;
  123. ucData = *((*ptRx).pcBufAddr + (*ptRx).ucBufRdInde);
  124. if ((*ptRx).ucBufCnt != 0)
  125. {
  126. if (++(*ptRx).ucBufRdInde == (*ptRx).ucBufSize)
  127. {
  128. (*ptRx).ucBufRdInde = 0;
  129. }
  130. __HAL_UART_DISABLE_IT(ptRx->ptUartHandel,UART_IT_RXNE);//关接收中断
  131. --(*ptRx).ucBufCnt;
  132. __HAL_UART_ENABLE_IT(ptRx->ptUartHandel,UART_IT_RXNE);//开接收中断
  133. }
  134. return ucData;
  135. }
  136. uint8_t UART_ReadChar(USART_Buf_TypeDef * ptRx, uint8_t ucNum)
  137. {
  138. uint8_t ucData;
  139. uint16_t i;
  140. i = ucNum;
  141. if ((*ptRx).ucBufCnt >= ucNum)
  142. {
  143. i += (*ptRx).ucBufRdInde;
  144. if (i >= (*ptRx).ucBufSize)
  145. {
  146. i -=((*ptRx).ucBufSize);
  147. }
  148. }
  149. else
  150. {
  151. i=0;
  152. }
  153. ucData = *((*ptRx).pcBufAddr + i);
  154. return ucData;
  155. }
  156. void UART_DelChar(USART_Buf_TypeDef * ptRx, uint8_t ucNum)
  157. {
  158. uint16_t i;
  159. if ((*ptRx).ucBufCnt >= ucNum)
  160. {
  161. __HAL_UART_DISABLE_IT(ptRx->ptUartHandel,UART_IT_RXNE);//关接收中断
  162. (*ptRx).ucBufCnt -= ucNum;
  163. __HAL_UART_ENABLE_IT(ptRx->ptUartHandel,UART_IT_RXNE);//开接收中断
  164. i = ucNum;
  165. i += (*ptRx).ucBufRdInde;
  166. if (i >= (*ptRx).ucBufSize)
  167. {
  168. i -= (*ptRx).ucBufSize;
  169. }
  170. (*ptRx).ucBufRdInde = i;
  171. }
  172. else
  173. {
  174. __HAL_UART_DISABLE_IT(ptRx->ptUartHandel,UART_IT_RXNE);//关接收中断
  175. i = (*ptRx).ucBufCnt;
  176. (*ptRx).ucBufCnt = 0;
  177. __HAL_UART_ENABLE_IT(ptRx->ptUartHandel,UART_IT_RXNE);//开接收中断
  178. i += (*ptRx).ucBufRdInde;
  179. if (i >= (*ptRx).ucBufSize)
  180. {
  181. i -= (*ptRx).ucBufSize;
  182. }
  183. (*ptRx).ucBufRdInde = i;
  184. }
  185. }
  186. void UART_PutChar(USART_Buf_TypeDef * ptTx, uint8_t ucData)
  187. {
  188. while ((*ptTx).ucBufCnt == (*ptTx).ucBufSize); //发送缓冲满,等待。若有此情况建议增大缓冲区
  189. __HAL_UART_DISABLE_IT(ptTx->ptUartHandel,UART_IT_TXE);//关发送中断
  190. if((*ptTx).ucBufCnt == 0)
  191. {
  192. if(__HAL_UART_GET_FLAG(ptTx->ptUartHandel, UART_FLAG_TXE)== SET) //check if transmit data register full or not
  193. {
  194. ptTx->ptUartHandel->Instance->DR = ucData;
  195. __HAL_UART_ENABLE_IT(ptTx->ptUartHandel,UART_IT_TXE);//开发送中断
  196. return;
  197. }
  198. }
  199. *((*ptTx).pcBufAddr + (*ptTx).ucBufWrInde) = ucData;
  200. if(++(*ptTx).ucBufWrInde == (*ptTx).ucBufSize)
  201. {
  202. (*ptTx).ucBufWrInde = 0;
  203. }
  204. ++(*ptTx).ucBufCnt;
  205. __HAL_UART_ENABLE_IT(ptTx->ptUartHandel,UART_IT_TXE);//开发送中断
  206. }
  207. uint16_t Usart_Send(const uint8_t *Data, uint16_t length)
  208. {
  209. uint16_t i;
  210. for(i = 0; i < length; i++)
  211. {
  212. UART_PutChar(&UART_TxBuff_Struct1, Data[i]);
  213. }
  214. return length;
  215. }
  216. void SendCmdData(const int8_t Mode, const uint8_t *Data, uint16_t Size)
  217. {
  218. uint8_t i;
  219. uint32_t check_sum;
  220. uint8_t s_buffer[256] = {0x55, 0xAA, 0x01, 0x00};
  221. s_buffer[4] = Mode;
  222. s_buffer[5] = Size;
  223. for(i = 0; i < Size; i++)
  224. {
  225. s_buffer[i + 6] = Data[i];
  226. }
  227. check_sum = CRC32_Calculate(s_buffer, Size + 6);
  228. s_buffer[Size + 6] = check_sum >> 24;
  229. s_buffer[Size + 7] = (check_sum >> 16) & 0xff;
  230. s_buffer[Size + 8] = (check_sum >> 8) & 0xff;
  231. s_buffer[Size + 9] = check_sum & 0xff;
  232. s_buffer[Size + 10] = 0xF0;
  233. Usart_Send(s_buffer, Size + 11);
  234. }
  235. void SendUartDataToTE(uint16_t IDnum,uint8_t Mode, uint16_t Command, uint8_t* Data)
  236. {
  237. uint8_t SendBuf[150] = {0};
  238. uint8_t CRC_Buf[150] = {0};
  239. uint32_t CRC_Result = 0x00000000;
  240. uint8_t DataLength;
  241. DataLength = (uint8_t)(Command & 0xFF);
  242. SendBuf[0] = FRAME_BEGIN1;
  243. SendBuf[1] = FRAME_BEGIN2;
  244. SendBuf[2] = (uint8_t)((IDnum >> 8) & 0xFF);
  245. SendBuf[3] = (uint8_t)(IDnum & 0xFF);;
  246. SendBuf[4] = Mode;
  247. SendBuf[5] = DataLength + 2;
  248. SendBuf[6] = (uint8_t)((Command >> 8) & 0xFF);
  249. SendBuf[7] = DataLength;
  250. memcpy(SendBuf + 8, Data, DataLength);
  251. memcpy(CRC_Buf, SendBuf, 8 + DataLength);
  252. CRC_Result = CRC32_Calculate(CRC_Buf, DataLength + 8);
  253. SendBuf[8 + DataLength] = (uint8_t)((CRC_Result >> 24) & 0xFF);
  254. SendBuf[9 + DataLength] = (uint8_t)((CRC_Result >> 16) & 0xFF);
  255. SendBuf[10 + DataLength] = (uint8_t)((CRC_Result >> 8) & 0xFF);
  256. SendBuf[11 + DataLength] = (uint8_t)(CRC_Result & 0xFF);
  257. SendBuf[12 + DataLength] = FRAME_END;
  258. Usart_Send(SendBuf, DataLength + 13);
  259. }
  260. void USARTx_Rx_IRQ(USART_Buf_TypeDef *USARTx_RxBuf_Struct)
  261. {
  262. uint8_t ucData;
  263. if(__HAL_UART_GET_FLAG(USARTx_RxBuf_Struct->ptUartHandel,UART_FLAG_RXNE) != RESET) //接收中断
  264. {
  265. ucData = (uint8_t)(USARTx_RxBuf_Struct->ptUartHandel->Instance->DR & 0x01FF);
  266. if(((USARTx_RxBuf_Struct->ptUartHandel->Instance->SR) & 0x0000) == 0)//无错误
  267. {
  268. *((* USARTx_RxBuf_Struct).pcBufAddr + (* USARTx_RxBuf_Struct).ucBufWrInde) = ucData;
  269. if(++(* USARTx_RxBuf_Struct).ucBufWrInde >= (* USARTx_RxBuf_Struct).ucBufSize)
  270. {
  271. (* USARTx_RxBuf_Struct).ucBufWrInde = 0;
  272. }
  273. if(++(* USARTx_RxBuf_Struct).ucBufCnt > (* USARTx_RxBuf_Struct).ucBufSize)
  274. {
  275. (* USARTx_RxBuf_Struct).ucBufCnt = (* USARTx_RxBuf_Struct).ucBufSize;
  276. (* USARTx_RxBuf_Struct).ucBufOvf = 1;
  277. }
  278. }
  279. __HAL_UART_CLEAR_OREFLAG(USARTx_RxBuf_Struct->ptUartHandel);
  280. }
  281. }
  282. void USARTx_Tx_IRQ(USART_Buf_TypeDef *USARTx_TxBuf_Struct)
  283. {
  284. if((__HAL_UART_GET_FLAG(USARTx_TxBuf_Struct->ptUartHandel, UART_FLAG_TXE) != RESET) && (__HAL_UART_GET_IT_SOURCE(USARTx_TxBuf_Struct->ptUartHandel, UART_IT_TXE) != RESET)) //发送完成中断
  285. {
  286. __HAL_UART_CLEAR_FLAG(USARTx_TxBuf_Struct->ptUartHandel,UART_FLAG_TXE);
  287. if((* USARTx_TxBuf_Struct).ucBufCnt)
  288. {
  289. --(* USARTx_TxBuf_Struct).ucBufCnt;
  290. USARTx_TxBuf_Struct->ptUartHandel->Instance->DR = *(USARTx_TxBuf_Struct->pcBufAddr + USARTx_TxBuf_Struct->ucBufRdInde);
  291. if(++(* USARTx_TxBuf_Struct).ucBufRdInde >= (* USARTx_TxBuf_Struct).ucBufSize)
  292. {
  293. (* USARTx_TxBuf_Struct).ucBufRdInde = 0;
  294. }
  295. }
  296. else
  297. {
  298. /* Disable the UART Transmit Data Register Empty Interrupt */
  299. __HAL_UART_DISABLE_IT(USARTx_TxBuf_Struct->ptUartHandel, UART_IT_TXE);
  300. }
  301. }
  302. }
  303. void Data_Process(uint16_t ID, uint8_t Mode, uint16_t Cmd, uint8_t* Data)
  304. {
  305. static uint8_t PBU_TE_Status_FirstFlag = 0;
  306. static uint8_t ErrorCnt_Key_Power = 0;
  307. static uint8_t ErrorCnt_Key_Walk = 0;
  308. static uint8_t ErrorCnt_Key_Add = 0;
  309. static uint8_t ErrorCnt_Key_Dec = 0;
  310. if(ID == 0x0101)
  311. {
  312. switch(Cmd)
  313. {
  314. case 0x2201:
  315. Key_Recv_Status.Status = Data[0];
  316. //Power键判断
  317. if(Key_Recv_Status.Status_Bit.Power == 1)
  318. {
  319. ErrorCnt_Key_Power++;
  320. if(ErrorCnt_Key_Power > 10)
  321. {
  322. PBU_TE_ErrorCode.ERROR_Bit.Key_Power = 1;
  323. }
  324. }
  325. else
  326. {
  327. ErrorCnt_Key_Power = 0;
  328. PBU_TE_ErrorCode.ERROR_Bit.Key_Power = 0;
  329. }
  330. //Walk键判断
  331. if(Key_Recv_Status.Status_Bit.Walk == 1)
  332. {
  333. ErrorCnt_Key_Walk++;
  334. if(ErrorCnt_Key_Walk > 10)
  335. {
  336. PBU_TE_ErrorCode.ERROR_Bit.Key_Walk = 1;
  337. }
  338. }
  339. else
  340. {
  341. ErrorCnt_Key_Walk = 0;
  342. PBU_TE_ErrorCode.ERROR_Bit.Key_Walk = 0;
  343. }
  344. //Add键判断
  345. if(Key_Recv_Status.Status_Bit.Add == 1)
  346. {
  347. ErrorCnt_Key_Add++;
  348. if(ErrorCnt_Key_Add > 10)
  349. {
  350. PBU_TE_ErrorCode.ERROR_Bit.Key_Add = 1;
  351. }
  352. }
  353. else
  354. {
  355. ErrorCnt_Key_Add = 0;
  356. PBU_TE_ErrorCode.ERROR_Bit.Key_Add = 0;
  357. }
  358. //Dec键判断
  359. if(Key_Recv_Status.Status_Bit.Dec == 1)
  360. {
  361. ErrorCnt_Key_Dec++;
  362. if(ErrorCnt_Key_Dec > 10)
  363. {
  364. PBU_TE_ErrorCode.ERROR_Bit.Key_Dec = 1;
  365. }
  366. }
  367. else
  368. {
  369. ErrorCnt_Key_Dec = 0;
  370. PBU_TE_ErrorCode.ERROR_Bit.Key_Dec = 0;
  371. }
  372. PBU_TE_ErrorCode.ERROR_Bit.MCU_Fault = Key_Recv_Status.Status_Bit.sync;
  373. TE_Online_TimeCnt=SysTime_5ms;
  374. TE_Online_Flag = 1;
  375. break;
  376. default:
  377. break;
  378. }
  379. }
  380. else if(ID == 0x0635)//透传0X635的数据
  381. {
  382. SendData(ID, Mode, Cmd, Data);//将TE过来的数据直接透传到CAN
  383. switch(Cmd)
  384. {
  385. case 0xc109:
  386. {
  387. if(PBU_TE_Status_FirstFlag == 0)
  388. {
  389. PBU_TE_Status_FirstFlag = 1;
  390. PBU_TE_Status = HANDSHAKE_YES;
  391. }
  392. break;
  393. }
  394. case 0xc502://最后一帧数据
  395. {
  396. PBU_TE_Status = UPDATE_FINISH;
  397. PBU_RunMode = PBU_RunMode_PowerOff;
  398. HAL_Delay(20);
  399. break;
  400. }
  401. default:
  402. {
  403. break;
  404. }
  405. }
  406. }
  407. }
  408. void Uart_RxData_Process(USART_Buf_TypeDef *ptUARTRx, uint16_t TimeOutCnt)
  409. {
  410. uint8_t Mode, CmdLength, DataLength, Data[150], CRC_Buf[150];
  411. uint16_t Cmd, id, i;
  412. uint32_t CrcResult, CrcData;
  413. uint8_t FrameBegin1, FrameBegin2;
  414. static uint32_t DelayTimeCnt1 = 0;
  415. static TrueOrFalse_Flag_Struct_t IsWaitRX_Flag = FALSE;
  416. /* Detect the data size of the buffer area to meet the minimum frame length */
  417. if(ptUARTRx->ucBufCnt >= 13)
  418. {
  419. /* Get frame header data */
  420. FrameBegin1 = UART_ReadChar(ptUARTRx, 0);
  421. CRC_Buf[0] = FrameBegin1;
  422. FrameBegin2 = UART_ReadChar(ptUARTRx, 1);
  423. CRC_Buf[1] = FrameBegin2;
  424. /* Compare frame header data */
  425. if((FrameBegin1 == FRAME_BEGIN1) && (FrameBegin2 == FRAME_BEGIN2))
  426. {
  427. /* Get ID */
  428. CRC_Buf[2] = UART_ReadChar(ptUARTRx, 2);
  429. CRC_Buf[3] = UART_ReadChar(ptUARTRx, 3);
  430. id = (CRC_Buf[2] << 8) | CRC_Buf[3];
  431. /* Get mode */
  432. Mode = UART_ReadChar(ptUARTRx, 4);
  433. CRC_Buf[4] = Mode;
  434. /* Comparison mode data */
  435. if((Mode == MODE_READ) || (Mode == MODE_WRITE) || (Mode == MODE_REPORT))
  436. {
  437. /* Get data length */
  438. CmdLength = UART_ReadChar(ptUARTRx, 5);
  439. CRC_Buf[5] = CmdLength;
  440. /* Get the command word */
  441. Cmd = (UART_ReadChar(ptUARTRx, 6) << 8) + UART_ReadChar(ptUARTRx, 7);
  442. CRC_Buf[6] = (uint8_t)((Cmd >> 8) & 0xFF);
  443. CRC_Buf[7] = (uint8_t)(Cmd & 0xFF);
  444. DataLength = UART_ReadChar(ptUARTRx, 7);
  445. /* Compare the data length with the command word and the data length without the command word by 2 bytes. */
  446. if((CmdLength - DataLength) == 2)
  447. {
  448. if(ptUARTRx->ucBufCnt < (CmdLength + 11))
  449. {
  450. if(IsWaitRX_Flag == FALSE)
  451. {
  452. DelayTimeCnt1 = SysTime_5ms;
  453. IsWaitRX_Flag = TRUE;
  454. }
  455. if(TimeCntDiff_5ms(DelayTimeCnt1) > TimeOutCnt)
  456. {
  457. DelayTimeCnt1 = SysTime_5ms;
  458. // UART_DelChar(ptUARTRx, ptUARTRx->ucBufCnt);
  459. IsWaitRX_Flag = TRUE;
  460. }
  461. }
  462. else
  463. {
  464. IsWaitRX_Flag = FALSE;
  465. for(i = 0; i < DataLength; i++)
  466. {
  467. Data[i] = UART_ReadChar(ptUARTRx, 8 + i);
  468. CRC_Buf[8 + i] = Data[i];
  469. }
  470. /* Get CRC check data */
  471. CrcData = (UART_ReadChar(ptUARTRx, 8 + DataLength) << 24) + (UART_ReadChar(ptUARTRx, 9 + DataLength) << 16)
  472. + (UART_ReadChar(ptUARTRx, 10 + DataLength) << 8) + UART_ReadChar(ptUARTRx, 11 + DataLength);
  473. /* Calculate the CRC check result */
  474. CrcResult = CRC32_Calculate(CRC_Buf, 8 + DataLength);
  475. /* Check CRC check result */
  476. if((CrcData - CrcResult) == 0)
  477. {
  478. /* Data processing */
  479. Data_Process(id, Mode, Cmd, Data);
  480. /* Remove parsed data */
  481. UART_DelChar(ptUARTRx, DataLength + 13);
  482. }
  483. else
  484. {
  485. /* Delete the first byte and reparse the data */
  486. UART_DelChar(ptUARTRx, 1);
  487. }
  488. }
  489. }
  490. else
  491. {
  492. /* Delete the first byte and reparse the data */
  493. UART_DelChar(ptUARTRx, 1);
  494. }
  495. }
  496. else
  497. {
  498. /* Delete the first byte and reparse the data */
  499. UART_DelChar(ptUARTRx, 1);
  500. }
  501. }
  502. else
  503. {
  504. /* Delete the first byte and reparse the data */
  505. UART_DelChar(ptUARTRx, 1);
  506. }
  507. }
  508. }
  509. /* USER CODE END 1 */
  510. /**
  511. * @}
  512. */
  513. /**
  514. * @}
  515. */
  516. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/