uart_process.c 6.8 KB

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