uart_process.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. //命令处理
  11. static void UART_DataProcess(uint8_t Mode, uint16_t Command, uint8_t* Data)
  12. {
  13. switch(Command)
  14. {
  15. //CDL在线检测
  16. case 0x1100:
  17. {
  18. SendCmdData(&UART_TxBuff_Struct3, MODE_REPORT, 0x1100, (uint8_t*)NULL);
  19. break;
  20. }
  21. //设备开关机
  22. case 0x2201:
  23. {
  24. if(Data[0] == 0xF0)//关机
  25. {
  26. HAL_GPIO_WritePin(SwitchOnOrOff_GPIO_Port, SwitchOnOrOff_Pin, GPIO_PIN_RESET);
  27. }
  28. else if(Data[0] == 0xF1)//开机
  29. {
  30. HAL_GPIO_WritePin(SwitchOnOrOff_GPIO_Port, SwitchOnOrOff_Pin, GPIO_PIN_SET);
  31. }
  32. break;
  33. }
  34. //复位
  35. case 0x4400:
  36. {
  37. Refresh_Flag();
  38. __set_FAULTMASK(1);//关闭所有中断
  39. HAL_NVIC_SystemReset();
  40. break;
  41. }
  42. //随机码和密钥进行校验
  43. case 0x5514:
  44. {
  45. static uint8_t InputCode[12];
  46. static uint8_t InputKey[8];
  47. static uint8_t OutputCode[12];
  48. static uint8_t SendData[27];
  49. memcpy(InputCode, Data, 12);
  50. memcpy(InputKey, Data + 12, 8);
  51. CheckCodeCal(InputCode, InputKey, OutputCode);
  52. memcpy(SendData, OutputCode, 12);
  53. memcpy(SendData + 12, (uint8_t*)"V1.5.0_20200702", 15);
  54. SendCmdData(&UART_TxBuff_Struct3, MODE_REPORT, 0x551B, SendData);
  55. break;
  56. }
  57. //授权
  58. case 0x6600:
  59. {
  60. uint8_t SendData[24];
  61. Ctf_CalAndSave();
  62. memcpy(SendData, MAC_ID, 12);
  63. memcpy(SendData + 12, Check_Code, 12);
  64. SendCmdData(&UART_TxBuff_Struct3, MODE_REPORT, 0x6618, SendData);
  65. HAL_Delay(200);
  66. __set_FAULTMASK(1);//关闭所有中断
  67. HAL_NVIC_SystemReset();
  68. break;
  69. }
  70. default:break;
  71. }
  72. }
  73. //发送数据
  74. void SendCmdData(USART_Buf_TypeDef * ptTx, uint8_t Mode, uint16_t Command, uint8_t* Data)
  75. {
  76. uint8_t SendBuf[255] = {0};
  77. uint8_t CRC_Buf[255] = {0};
  78. uint32_t CRC_Result = 0x00000000;
  79. uint8_t DataLength;
  80. DataLength = (uint8_t)(Command & 0xFF);
  81. SendBuf[0] = FRAME_BEGIN1;
  82. SendBuf[1] = FRAME_BEGIN2;
  83. SendBuf[2] = 0x07;
  84. SendBuf[3] = 0xFF;
  85. SendBuf[4] = Mode;
  86. SendBuf[5] = DataLength + 2;
  87. SendBuf[6] = (uint8_t)((Command >> 8) & 0xFF);
  88. SendBuf[7] = DataLength;
  89. memcpy(SendBuf + 8, Data, DataLength);
  90. memcpy(CRC_Buf, SendBuf, 8 + DataLength);
  91. CRC_Result = CRC32_Calculate(CRC_Buf, DataLength + 8);
  92. SendBuf[8 + DataLength] = (uint8_t)((CRC_Result >> 24) & 0xFF);
  93. SendBuf[9 + DataLength] = (uint8_t)((CRC_Result >> 16) & 0xFF);
  94. SendBuf[10 + DataLength] = (uint8_t)((CRC_Result >> 8) & 0xFF);
  95. SendBuf[11 + DataLength] = (uint8_t)(CRC_Result & 0xFF);
  96. SendBuf[12 + DataLength] = FRAME_END;
  97. UART_SendData(ptTx, SendBuf, DataLength + 13);
  98. }
  99. //串口数据解析
  100. void Uart_RxData_Process(USART_Buf_TypeDef* ptUartRx, uint16_t TimeOut)
  101. {
  102. uint8_t Mode, CmdLength, DataLength;
  103. static uint8_t Data[255], CRC_Buf[255];
  104. uint16_t Cmd, i, ID;
  105. uint32_t CrcResult, CrcData;
  106. uint8_t FrameBegin1, FrameBegin2;
  107. static uint32_t DelayTimeCnt = 0;
  108. static TrueOrFalse_Flag_Struct_t IsWaitRX_Flag = FALSE;
  109. if(ptUartRx->ucBufCnt >= 13)
  110. {
  111. //读取帧头
  112. FrameBegin1 = UART_ReadChar(ptUartRx, 0);
  113. CRC_Buf[0] = FrameBegin1;
  114. FrameBegin2 = UART_ReadChar(ptUartRx, 1);
  115. CRC_Buf[1] = FrameBegin2;
  116. if((FrameBegin1 == FRAME_BEGIN1) && (FrameBegin2 == FRAME_BEGIN2))
  117. {
  118. CRC_Buf[2] = UART_ReadChar(ptUartRx, 2);
  119. CRC_Buf[3] = UART_ReadChar(ptUartRx, 3);
  120. ID = (uint16_t)(CRC_Buf[2] << 8) + CRC_Buf[3];
  121. //读取帧模式
  122. Mode = UART_ReadChar(ptUartRx, 4);
  123. CRC_Buf[4] = Mode;
  124. if((Mode == MODE_READ) || (Mode == MODE_WRITE) || (Mode == MODE_REPORT))
  125. {
  126. //读取命令段长度和命令字
  127. CmdLength = UART_ReadChar(ptUartRx, 5);
  128. CRC_Buf[5] = CmdLength;
  129. Cmd = (UART_ReadChar(ptUartRx, 6) << 8) + UART_ReadChar(ptUartRx, 7);
  130. CRC_Buf[6] = (uint8_t)((Cmd >> 8) & 0xFF);
  131. CRC_Buf[7] = (uint8_t)(Cmd & 0xFF);
  132. DataLength = UART_ReadChar(ptUartRx, 7);
  133. if((CmdLength - DataLength) == 2)
  134. {
  135. if(ptUartRx->ucBufCnt < (CmdLength + 11))//帧头2bytes + ID2bytes 模式1byte + 命令段长度1byte + 校验位4bytes + 帧尾1byte
  136. {
  137. if(IsWaitRX_Flag == FALSE)
  138. {
  139. DelayTimeCnt = HAL_GetTick();
  140. IsWaitRX_Flag = TRUE;
  141. }
  142. if((HAL_GetTick() - DelayTimeCnt) > TimeOut)//超时,单位ms
  143. {
  144. UART_DelChar(ptUartRx, ptUartRx->ucBufCnt);
  145. IsWaitRX_Flag = FALSE;
  146. }
  147. return;
  148. }
  149. else
  150. {
  151. IsWaitRX_Flag = FALSE;
  152. //接收到完整正确数据包
  153. for(i=0; i<DataLength; i++)//读取数据段
  154. {
  155. Data[i] = UART_ReadChar(ptUartRx, 8 + i);
  156. CRC_Buf[8 + i] = Data[i];
  157. }
  158. CrcData = (UART_ReadChar(ptUartRx, 8 + DataLength) << 24) + \
  159. (UART_ReadChar(ptUartRx, 9 + DataLength) << 16) + \
  160. (UART_ReadChar(ptUartRx, 10 + DataLength) << 8) + \
  161. UART_ReadChar(ptUartRx, 11 + DataLength);
  162. CrcResult = CRC32_Calculate(CRC_Buf, 8 + DataLength);
  163. if((CrcData - CrcResult) == 0) // 比较校验
  164. {
  165. //数据处理
  166. UART_DataProcess(Mode, Cmd, Data);//Mode为帧模式,Cmd为命令字,Data为数据段
  167. //数据转发CAN
  168. memcpy((uint8_t*)(CRC_Buf + 2), (uint8_t*)(CRC_Buf + 4), DataLength + 4);
  169. CRC_Buf[6 + DataLength] = UART_ReadChar(ptUartRx, 8 + DataLength);
  170. CRC_Buf[7 + DataLength] = UART_ReadChar(ptUartRx, 9 + DataLength);
  171. CRC_Buf[8 + DataLength] = UART_ReadChar(ptUartRx, 10 + DataLength);
  172. CRC_Buf[9 + DataLength] = UART_ReadChar(ptUartRx, 11 + DataLength);
  173. CRC_Buf[10 + DataLength] = FRAME_END;
  174. CAN_SendData(ID, CRC_Buf, DataLength + 11);
  175. //清除缓存
  176. UART_DelChar(ptUartRx, CmdLength + 11);
  177. //通信指示
  178. HAL_GPIO_TogglePin(LED_Display_GPIO_Port, LED_Display_Pin);
  179. return;
  180. }
  181. UART_DelChar(ptUartRx, 1);
  182. }
  183. }
  184. else
  185. {
  186. UART_DelChar(ptUartRx, 1);
  187. }
  188. }
  189. else
  190. {
  191. UART_DelChar(ptUartRx, 1);
  192. }
  193. }
  194. else
  195. {
  196. UART_DelChar(ptUartRx, 1);
  197. }
  198. }
  199. }