uart_process.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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 UART3_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, 0x07FF, 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*)"V4.0.1_20240229", 15);
  91. SendCmdData(&UART_TxBuff_Struct3, MODE_REPORT, 0x07FF, 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, 0x07FF, 0x6618, SendData);
  102. HAL_Delay(200);
  103. __set_FAULTMASK(1);//关闭所有中断
  104. HAL_NVIC_SystemReset();
  105. break;
  106. }
  107. default:break;
  108. }
  109. }
  110. static void UART1_DataProcess(uint8_t Mode, uint16_t Command, uint8_t* Data)
  111. {
  112. }
  113. //发送数据
  114. void UART_TransferData(USART_Buf_TypeDef * ptTx, uint8_t Addr, uint8_t Mode, uint16_t ID, uint16_t Cmd, uint8_t* Data)
  115. {
  116. uint8_t databuf[256] = {0};
  117. uint8_t i = 0;
  118. uint8_t len = 0;
  119. uint16_t checksum = 0;
  120. len = (uint8_t)((Cmd & 0xff) + 4);
  121. if(len > 247)
  122. {
  123. len = 247;
  124. }
  125. databuf[0] = UARTFRAME_BEGIN;
  126. databuf[1] = Addr;
  127. databuf[2] = Mode;
  128. databuf[3] = len;
  129. databuf[4] = (uint8_t)((ID >> 8) & 0xFF);
  130. databuf[5] = (uint8_t)(ID & 0xFF);
  131. databuf[6] = (uint8_t)((Cmd >> 8) & 0xFF);
  132. databuf[7] = (uint8_t)(Cmd & 0xFF);
  133. for(i=0; i<(len-4); i++)
  134. {
  135. databuf[8+i] = Data[i];
  136. }
  137. for(i=1; i<(len+4); i++)
  138. {
  139. checksum = (uint16_t)(databuf[i] + checksum);
  140. }
  141. databuf[4+len] = (uint8_t)checksum;
  142. databuf[5+len] = (uint8_t)(checksum>>8);
  143. databuf[6+len] = UARTFRAME_END1;
  144. databuf[7+len] = UARTFRAME_END2;
  145. UART_SendData(ptTx, databuf, len + 8);
  146. }
  147. void SendCmdData(USART_Buf_TypeDef * ptTx, uint8_t Mode, uint16_t ID, uint16_t Command, uint8_t* Data)
  148. {
  149. uint8_t SendBuf[255] = {0};
  150. uint8_t CRC_Buf[255] = {0};
  151. uint32_t CRC_Result = 0x00000000;
  152. uint8_t DataLength;
  153. DataLength = (uint8_t)(Command & 0xFF);
  154. SendBuf[0] = FRAME_BEGIN1;
  155. SendBuf[1] = FRAME_BEGIN2;
  156. SendBuf[2] = (uint8_t)((ID >> 8) & 0xFF);
  157. SendBuf[3] = (uint8_t)(ID & 0xFF);
  158. SendBuf[4] = Mode;
  159. SendBuf[5] = DataLength + 2;
  160. SendBuf[6] = (uint8_t)((Command >> 8) & 0xFF);
  161. SendBuf[7] = DataLength;
  162. memcpy(SendBuf + 8, Data, DataLength);
  163. memcpy(CRC_Buf, SendBuf, 8 + DataLength);
  164. CRC_Result = CRC32_Calculate(CRC_Buf, DataLength + 8);
  165. SendBuf[8 + DataLength] = (uint8_t)((CRC_Result >> 24) & 0xFF);
  166. SendBuf[9 + DataLength] = (uint8_t)((CRC_Result >> 16) & 0xFF);
  167. SendBuf[10 + DataLength] = (uint8_t)((CRC_Result >> 8) & 0xFF);
  168. SendBuf[11 + DataLength] = (uint8_t)(CRC_Result & 0xFF);
  169. SendBuf[12 + DataLength] = FRAME_END;
  170. UART_SendData(ptTx, SendBuf, DataLength + 13);
  171. }
  172. //串口1数据解析
  173. void Uart1_RxData_Process(USART_Buf_TypeDef* ptUartRx, uint16_t TimeOut)
  174. {
  175. uint8_t Mode, DataLength, Addr;
  176. static uint8_t Data[255];
  177. uint16_t Cmd, i, ID;
  178. uint16_t CheckSumResult=0, CheckSumData=0;
  179. uint8_t FrameBegin, FrameEnd1, FrameEnd2;
  180. static uint32_t DelayTimeCnt = 0;
  181. static TrueOrFalse_Flag_Struct_t IsWaitRX_Flag = FALSE;
  182. if(ptUartRx->ucBufCnt >= 8)
  183. {
  184. //读取帧头
  185. FrameBegin = UART_ReadChar(ptUartRx, 0);
  186. if(FrameBegin == UARTFRAME_BEGIN)
  187. {
  188. Addr = UART_ReadChar(ptUartRx, 1);//读取地址ID
  189. if(Addr == UARTID_MC_TO_CDL)
  190. {
  191. //读取帧模式
  192. Mode = UART_ReadChar(ptUartRx, 2);
  193. if((Mode == UARTMODE_READ) || (Mode == UARTMODE_WRITE) || (Mode == UARTMODE_REPORT))
  194. {
  195. //读取数据段长度
  196. DataLength = UART_ReadChar(ptUartRx, 3);
  197. if(ptUartRx->ucBufCnt < (DataLength + 8))//帧头1byte + 地址1byte + 模式1byte + 长度1byte + 校验位2bytes + 帧尾2bytes
  198. {
  199. if(IsWaitRX_Flag == FALSE)
  200. {
  201. DelayTimeCnt = HAL_GetTick();
  202. IsWaitRX_Flag = TRUE;
  203. }
  204. if((HAL_GetTick() - DelayTimeCnt) > TimeOut)//超时,单位ms
  205. {
  206. UART_DelChar(ptUartRx, ptUartRx->ucBufCnt);
  207. IsWaitRX_Flag = FALSE;
  208. }
  209. return;
  210. }
  211. else
  212. {
  213. IsWaitRX_Flag = FALSE;
  214. //接收到完整正确数据包
  215. for(i=0; i<(DataLength+4); i++)//读取数据段
  216. {
  217. Data[i] = UART_ReadChar(ptUartRx, i);
  218. }
  219. CheckSumData = (UART_ReadChar(ptUartRx, 4 + DataLength)) + \
  220. (UART_ReadChar(ptUartRx, 5 + DataLength) << 8);
  221. for(i = 1; i<(DataLength+4); i++)
  222. {
  223. CheckSumResult = (uint16_t)(Data[i] + CheckSumResult);
  224. }
  225. FrameEnd1 = UART_ReadChar(ptUartRx, 6 + DataLength);
  226. FrameEnd2 = UART_ReadChar(ptUartRx, 7 + DataLength);
  227. if((CheckSumData == CheckSumResult) && (FrameEnd1 == UARTFRAME_END1) && (FrameEnd2 == UARTFRAME_END2))// 比较校验和帧尾
  228. {
  229. //数据处理
  230. UART1_DataProcess(Mode, Cmd, &Data[4]);//Mode为帧模式,Cmd为命令字,Data为数据段
  231. if(DataLength >= 4)//数据段包含ID+Cmd,至少4bytes
  232. {
  233. ID = (uint16_t)((Data[4]<<8) + Data[5]);
  234. Cmd = (uint16_t)((Data[6]<<8) + Data[7]);
  235. SendCmdData(&UART_TxBuff_Struct3, Mode, ID, Cmd, &Data[8]);
  236. }
  237. //清除缓存
  238. UART_DelChar(ptUartRx, DataLength + 8);
  239. //通信指示
  240. HAL_GPIO_TogglePin(LED_Display_GPIO_Port, LED_Display_Pin);
  241. }
  242. else
  243. {
  244. UART_DelChar(ptUartRx, 1);
  245. }
  246. }
  247. }
  248. else
  249. {
  250. UART_DelChar(ptUartRx, 1);
  251. }
  252. }
  253. else
  254. {
  255. UART_DelChar(ptUartRx, 1);
  256. }
  257. }
  258. else
  259. {
  260. UART_DelChar(ptUartRx, 1);
  261. }
  262. }
  263. }
  264. //串口3数据解析
  265. void Uart3_RxData_Process(USART_Buf_TypeDef* ptUartRx, uint16_t TimeOut)
  266. {
  267. uint8_t Mode, CmdLength, DataLength;
  268. static uint8_t Data[255], CRC_Buf[255];
  269. uint16_t Cmd, i, ID;
  270. uint32_t CrcResult, CrcData;
  271. uint8_t FrameBegin1, FrameBegin2;
  272. static uint32_t DelayTimeCnt = 0;
  273. static TrueOrFalse_Flag_Struct_t IsWaitRX_Flag = FALSE;
  274. if(ptUartRx->ucBufCnt >= 13)
  275. {
  276. //读取帧头
  277. FrameBegin1 = UART_ReadChar(ptUartRx, 0);
  278. CRC_Buf[0] = FrameBegin1;
  279. FrameBegin2 = UART_ReadChar(ptUartRx, 1);
  280. CRC_Buf[1] = FrameBegin2;
  281. if((FrameBegin1 == FRAME_BEGIN1) && (FrameBegin2 == FRAME_BEGIN2))
  282. {
  283. CRC_Buf[2] = UART_ReadChar(ptUartRx, 2);
  284. CRC_Buf[3] = UART_ReadChar(ptUartRx, 3);
  285. ID = (uint16_t)(CRC_Buf[2] << 8) + CRC_Buf[3];
  286. //读取帧模式
  287. Mode = UART_ReadChar(ptUartRx, 4);
  288. CRC_Buf[4] = Mode;
  289. if((Mode == MODE_READ) || (Mode == MODE_WRITE) || (Mode == MODE_REPORT))
  290. {
  291. //读取命令段长度和命令字
  292. CmdLength = UART_ReadChar(ptUartRx, 5);
  293. CRC_Buf[5] = CmdLength;
  294. Cmd = (UART_ReadChar(ptUartRx, 6) << 8) + UART_ReadChar(ptUartRx, 7);
  295. CRC_Buf[6] = (uint8_t)((Cmd >> 8) & 0xFF);
  296. CRC_Buf[7] = (uint8_t)(Cmd & 0xFF);
  297. DataLength = UART_ReadChar(ptUartRx, 7);
  298. if((CmdLength - DataLength) == 2)
  299. {
  300. if(ptUartRx->ucBufCnt < (CmdLength + 11))//帧头2bytes + ID2bytes 模式1byte + 命令段长度1byte + 校验位4bytes + 帧尾1byte
  301. {
  302. if(IsWaitRX_Flag == FALSE)
  303. {
  304. DelayTimeCnt = HAL_GetTick();
  305. IsWaitRX_Flag = TRUE;
  306. }
  307. if((HAL_GetTick() - DelayTimeCnt) > TimeOut)//超时,单位ms
  308. {
  309. UART_DelChar(ptUartRx, ptUartRx->ucBufCnt);
  310. IsWaitRX_Flag = FALSE;
  311. }
  312. return;
  313. }
  314. else
  315. {
  316. IsWaitRX_Flag = FALSE;
  317. //接收到完整正确数据包
  318. for(i=0; i<DataLength; i++)//读取数据段
  319. {
  320. Data[i] = UART_ReadChar(ptUartRx, 8 + i);
  321. CRC_Buf[8 + i] = Data[i];
  322. }
  323. CrcData = (UART_ReadChar(ptUartRx, 8 + DataLength) << 24) + \
  324. (UART_ReadChar(ptUartRx, 9 + DataLength) << 16) + \
  325. (UART_ReadChar(ptUartRx, 10 + DataLength) << 8) + \
  326. UART_ReadChar(ptUartRx, 11 + DataLength);
  327. CrcResult = CRC32_Calculate(CRC_Buf, 8 + DataLength);
  328. if((CrcData - CrcResult) == 0) // 比较校验
  329. {
  330. //数据处理
  331. UART3_DataProcess(Mode, Cmd, Data);//Mode为帧模式,Cmd为命令字,Data为数据段
  332. //数据转发CAN
  333. memcpy((uint8_t*)(CRC_Buf + 2), (uint8_t*)(CRC_Buf + 4), DataLength + 4);
  334. CRC_Buf[6 + DataLength] = UART_ReadChar(ptUartRx, 8 + DataLength);
  335. CRC_Buf[7 + DataLength] = UART_ReadChar(ptUartRx, 9 + DataLength);
  336. CRC_Buf[8 + DataLength] = UART_ReadChar(ptUartRx, 10 + DataLength);
  337. CRC_Buf[9 + DataLength] = UART_ReadChar(ptUartRx, 11 + DataLength);
  338. CRC_Buf[10 + DataLength] = FRAME_END;
  339. //CAN_SendData(ID, CRC_Buf, DataLength + 11);
  340. UART_TransferData(&UART_TxBuff_Struct1, UARTID_CDL_TO_MC, Mode, ID, Cmd, Data);
  341. //清除缓存
  342. UART_DelChar(ptUartRx, CmdLength + 11);
  343. //通信指示
  344. HAL_GPIO_TogglePin(LED_Display_GPIO_Port, LED_Display_Pin);
  345. return;
  346. }
  347. UART_DelChar(ptUartRx, 1);
  348. }
  349. }
  350. else
  351. {
  352. UART_DelChar(ptUartRx, 1);
  353. }
  354. }
  355. else
  356. {
  357. UART_DelChar(ptUartRx, 1);
  358. }
  359. }
  360. else
  361. {
  362. UART_DelChar(ptUartRx, 1);
  363. }
  364. }
  365. }