uart_process.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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. uint8_t UART1_BaudrateSwitch = 0;
  13. uint32_t T_UART1_BaudrateSwitch = 0;
  14. void PowerOnOff(uint8_t *mode)
  15. {
  16. if(*mode == 1)
  17. {
  18. if((HAL_GetTick() - T_startPowerOnOff) >= 1000)//Off延时1s
  19. {
  20. HAL_GPIO_WritePin(ACC_WELLING_EN_GPIO_Port, ACC_WELLING_EN_Pin, GPIO_PIN_RESET);//Off
  21. *mode = 0;
  22. }
  23. }
  24. else if(*mode == 2)
  25. {
  26. if((HAL_GetTick() - T_startPowerOnOff) >= 3000)//On延时3s
  27. {
  28. HAL_GPIO_WritePin(ACC_WELLING_EN_GPIO_Port, ACC_WELLING_EN_Pin, GPIO_PIN_RESET);//Off
  29. *mode = 0;
  30. }
  31. }
  32. }
  33. //命令处理
  34. static void UART3_DataProcess(uint8_t Mode, uint16_t Command, uint8_t* Data)
  35. {
  36. switch(Command)
  37. {
  38. //CDL在线检测
  39. case 0x1100:
  40. {
  41. SendCmdData(&UART_TxBuff_Struct3, MODE_REPORT, 0x07FF, 0x1100, (uint8_t*)NULL);
  42. break;
  43. }
  44. //设备开关机
  45. case 0x2201:
  46. {
  47. if(Power_mode == 0)
  48. {
  49. T_startPowerOnOff = HAL_GetTick();
  50. if(Data[0] == 0xF0)//关机
  51. {
  52. Power_mode = 1;
  53. HAL_GPIO_WritePin(ACC_WELLING_EN_GPIO_Port, ACC_WELLING_EN_Pin, GPIO_PIN_SET);//On
  54. }
  55. else if(Data[0] == 0xF1)//开机
  56. {
  57. Power_mode = 2;
  58. HAL_GPIO_WritePin(ACC_WELLING_EN_GPIO_Port, ACC_WELLING_EN_Pin, GPIO_PIN_SET);//On
  59. }
  60. }
  61. if(Data[0] == 0xF0)//关机
  62. {
  63. HAL_GPIO_WritePin(SwitchOnOrOff_GPIO_Port, SwitchOnOrOff_Pin, GPIO_PIN_RESET);
  64. HAL_GPIO_WritePin(ACC_MOTINOVA_EN_GPIO_Port, ACC_MOTINOVA_EN_Pin, GPIO_PIN_RESET);
  65. }
  66. else if(Data[0] == 0xF1)//开机
  67. {
  68. HAL_GPIO_WritePin(SwitchOnOrOff_GPIO_Port, SwitchOnOrOff_Pin, GPIO_PIN_SET);
  69. HAL_GPIO_WritePin(ACC_MOTINOVA_EN_GPIO_Port, ACC_MOTINOVA_EN_Pin, GPIO_PIN_SET);
  70. }
  71. break;
  72. }
  73. //复位
  74. case 0x4400:
  75. {
  76. Refresh_Flag();
  77. __set_FAULTMASK(1);//关闭所有中断
  78. HAL_NVIC_SystemReset();
  79. break;
  80. }
  81. case 0x2505:
  82. {
  83. if (strncmp("RESET", (char *)Data, 5) == 0)
  84. {
  85. // UART_TransferData(&UART_TxBuff_Struct1, UARTID_CDL_TO_MC, Mode, 0x751, Command, Data);
  86. // HAL_Delay(200);
  87. //
  88. // __set_FAULTMASK(1);//关闭所有中断
  89. // HAL_NVIC_SystemReset();
  90. T_UART1_BaudrateSwitch = HAL_GetTick();
  91. UART1_BaudrateSwitch = 2;
  92. }
  93. break;
  94. }
  95. //随机码和密钥进行校验
  96. case 0x5514:
  97. {
  98. static uint8_t InputCode[12];
  99. static uint8_t InputKey[8];
  100. static uint8_t OutputCode[12];
  101. static uint8_t SendData[27];
  102. memcpy(InputCode, Data, 12);
  103. memcpy(InputKey, Data + 12, 8);
  104. CheckCodeCal(InputCode, InputKey, OutputCode);
  105. memcpy(SendData, OutputCode, 12);
  106. memcpy(SendData + 12, (uint8_t*)"V4.0.5_20240711", 15);
  107. SendCmdData(&UART_TxBuff_Struct3, MODE_REPORT, 0x07FF, 0x551B, SendData);
  108. break;
  109. }
  110. //授权
  111. case 0x6600:
  112. {
  113. uint8_t SendData[24];
  114. Ctf_CalAndSave();
  115. memcpy(SendData, MAC_ID, 12);
  116. memcpy(SendData + 12, Check_Code, 12);
  117. SendCmdData(&UART_TxBuff_Struct3, MODE_REPORT, 0x07FF, 0x6618, SendData);
  118. HAL_Delay(200);
  119. __set_FAULTMASK(1);//关闭所有中断
  120. HAL_NVIC_SystemReset();
  121. break;
  122. }
  123. case 0x7702:
  124. {
  125. UART_SendSwitch_Lidian2(&UART_TxBuff_Struct1, 0x01, Command, Data);
  126. HAL_Delay(50);
  127. UART_SendSwitch_zhihuigainian(&UART_TxBuff_Struct1, Command, Data);
  128. HAL_Delay(50);
  129. break;
  130. }
  131. default:break;
  132. }
  133. }
  134. static void UART1_DataProcess(uint8_t Mode, uint16_t Command, uint8_t* Data)
  135. {
  136. }
  137. //发送数据
  138. void UART_SendSwitch_Lidian2(USART_Buf_TypeDef * ptTx, uint8_t Addr, uint16_t Cmd, uint8_t* Data)
  139. {
  140. uint8_t databuf[256] = {0};
  141. uint8_t i = 0;
  142. uint8_t len = 0;
  143. uint16_t checkxor = 0;
  144. len = 20;
  145. databuf[0] = Addr;
  146. databuf[1] = len;
  147. databuf[2] = (uint8_t)((Cmd >> 8) & 0xFF);
  148. for(i=0; i<(Cmd & 0xFF); i++)
  149. {
  150. databuf[3+i] = Data[i];
  151. }
  152. for(i=0; i<len; i++)
  153. {
  154. checkxor ^= databuf[i];
  155. }
  156. databuf[len-1] = checkxor;
  157. UART_SendData(ptTx, databuf, len);
  158. }
  159. void UART_SendSwitch_zhihuigainian(USART_Buf_TypeDef * ptTx, uint16_t Cmd, uint8_t* Data)
  160. {
  161. uint8_t databuf[256] = {0};
  162. uint8_t i = 0;
  163. uint8_t len = 0;
  164. uint16_t checkxor = 0;
  165. len = 13;
  166. databuf[0] = 0x55;
  167. for(i=0; i<(Cmd & 0xFF); i++)
  168. {
  169. databuf[1+i] = Data[i];
  170. }
  171. for(i=0; i<10; i=i+2)
  172. {
  173. checkxor ^= (uint16_t)((databuf[i]<<8) + databuf[i+1]);
  174. }
  175. databuf[len-3] = (uint8_t)(checkxor&0xff);
  176. databuf[len-2] = (uint8_t)(checkxor>>8);
  177. databuf[len-1] = 0xF0;
  178. UART_SendData(ptTx, databuf, len);
  179. }
  180. void UART_TransferData(USART_Buf_TypeDef * ptTx, uint8_t Addr, uint8_t Mode, uint16_t ID, uint16_t Cmd, uint8_t* Data)
  181. {
  182. uint8_t databuf[256] = {0};
  183. uint8_t i = 0;
  184. uint8_t len = 0;
  185. uint16_t checksum = 0;
  186. len = (uint8_t)((Cmd & 0xff) + 4);
  187. if(len > 247)
  188. {
  189. len = 247;
  190. }
  191. databuf[0] = UARTFRAME_BEGIN;
  192. databuf[1] = Addr;
  193. databuf[2] = Mode;
  194. databuf[3] = len;
  195. databuf[4] = (uint8_t)((ID >> 8) & 0xFF);
  196. databuf[5] = (uint8_t)(ID & 0xFF);
  197. databuf[6] = (uint8_t)((Cmd >> 8) & 0xFF);
  198. databuf[7] = (uint8_t)(Cmd & 0xFF);
  199. for(i=0; i<(len-4); i++)
  200. {
  201. databuf[8+i] = Data[i];
  202. }
  203. for(i=1; i<(len+4); i++)
  204. {
  205. checksum = (uint16_t)(databuf[i] + checksum);
  206. }
  207. databuf[4+len] = (uint8_t)checksum;
  208. databuf[5+len] = (uint8_t)(checksum>>8);
  209. databuf[6+len] = UARTFRAME_END1;
  210. databuf[7+len] = UARTFRAME_END2;
  211. UART_SendData(ptTx, databuf, len + 8);
  212. }
  213. void SendCmdData(USART_Buf_TypeDef * ptTx, uint8_t Mode, uint16_t ID, uint16_t Command, uint8_t* Data)
  214. {
  215. uint8_t SendBuf[255] = {0};
  216. uint8_t CRC_Buf[255] = {0};
  217. uint32_t CRC_Result = 0x00000000;
  218. uint8_t DataLength;
  219. DataLength = (uint8_t)(Command & 0xFF);
  220. SendBuf[0] = FRAME_BEGIN1;
  221. SendBuf[1] = FRAME_BEGIN2;
  222. SendBuf[2] = (uint8_t)((ID >> 8) & 0xFF);
  223. SendBuf[3] = (uint8_t)(ID & 0xFF);
  224. SendBuf[4] = Mode;
  225. SendBuf[5] = DataLength + 2;
  226. SendBuf[6] = (uint8_t)((Command >> 8) & 0xFF);
  227. SendBuf[7] = DataLength;
  228. memcpy(SendBuf + 8, Data, DataLength);
  229. memcpy(CRC_Buf, SendBuf, 8 + DataLength);
  230. CRC_Result = CRC32_Calculate(CRC_Buf, DataLength + 8);
  231. SendBuf[8 + DataLength] = (uint8_t)((CRC_Result >> 24) & 0xFF);
  232. SendBuf[9 + DataLength] = (uint8_t)((CRC_Result >> 16) & 0xFF);
  233. SendBuf[10 + DataLength] = (uint8_t)((CRC_Result >> 8) & 0xFF);
  234. SendBuf[11 + DataLength] = (uint8_t)(CRC_Result & 0xFF);
  235. SendBuf[12 + DataLength] = FRAME_END;
  236. UART_SendData(ptTx, SendBuf, DataLength + 13);
  237. }
  238. //串口1数据解析
  239. void Uart1_RxData_Process(USART_Buf_TypeDef* ptUartRx, uint16_t TimeOut)
  240. {
  241. uint8_t Mode, DataLength, Addr;
  242. static uint8_t Data[255];
  243. uint16_t Cmd, i, ID;
  244. uint16_t CheckSumResult=0, CheckSumData=0;
  245. uint8_t FrameBegin, FrameEnd1, FrameEnd2;
  246. static uint32_t DelayTimeCnt = 0;
  247. static TrueOrFalse_Flag_Struct_t IsWaitRX_Flag = FALSE;
  248. if(ptUartRx->ucBufCnt >= 8)
  249. {
  250. //读取帧头
  251. FrameBegin = UART_ReadChar(ptUartRx, 0);
  252. if(FrameBegin == UARTFRAME_BEGIN)
  253. {
  254. Addr = UART_ReadChar(ptUartRx, 1);//读取地址ID
  255. if(Addr == UARTADDR_CDL)
  256. {
  257. //读取帧模式
  258. Mode = UART_ReadChar(ptUartRx, 2);
  259. if((Mode == UARTMODE_READ) || (Mode == UARTMODE_WRITE) || (Mode == UARTMODE_REPORT))
  260. {
  261. //读取数据段长度
  262. DataLength = UART_ReadChar(ptUartRx, 3);
  263. if(ptUartRx->ucBufCnt < (DataLength + 8))//帧头1byte + 地址1byte + 模式1byte + 长度1byte + 校验位2bytes + 帧尾2bytes
  264. {
  265. if(IsWaitRX_Flag == FALSE)
  266. {
  267. DelayTimeCnt = HAL_GetTick();
  268. IsWaitRX_Flag = TRUE;
  269. }
  270. if((HAL_GetTick() - DelayTimeCnt) > TimeOut)//超时,单位ms
  271. {
  272. UART_DelChar(ptUartRx, ptUartRx->ucBufCnt);
  273. IsWaitRX_Flag = FALSE;
  274. }
  275. return;
  276. }
  277. else
  278. {
  279. IsWaitRX_Flag = FALSE;
  280. //接收到完整正确数据包
  281. for(i=0; i<(DataLength+4); i++)//读取数据段
  282. {
  283. Data[i] = UART_ReadChar(ptUartRx, i);
  284. }
  285. CheckSumData = (UART_ReadChar(ptUartRx, 4 + DataLength)) + \
  286. (UART_ReadChar(ptUartRx, 5 + DataLength) << 8);
  287. for(i = 1; i<(DataLength+4); i++)
  288. {
  289. CheckSumResult = (uint16_t)(Data[i] + CheckSumResult);
  290. }
  291. FrameEnd1 = UART_ReadChar(ptUartRx, 6 + DataLength);
  292. FrameEnd2 = UART_ReadChar(ptUartRx, 7 + DataLength);
  293. if((CheckSumData == CheckSumResult) && (FrameEnd1 == UARTFRAME_END1) && (FrameEnd2 == UARTFRAME_END2))// 比较校验和帧尾
  294. {
  295. //数据处理
  296. UART1_DataProcess(Mode, Cmd, &Data[4]);//Mode为帧模式,Cmd为命令字,Data为数据段
  297. if(DataLength >= 4)//数据段包含ID+Cmd,至少4bytes
  298. {
  299. ID = (uint16_t)((Data[4]<<8) + Data[5]);
  300. Cmd = (uint16_t)((Data[6]<<8) + Data[7]);
  301. SendCmdData(&UART_TxBuff_Struct3, Mode, ID, Cmd, &Data[8]);
  302. }
  303. if((Cmd==0xC109) || (Cmd==0xC202) || (Cmd==0xC302) || (Cmd==0xC402) || (Cmd==0xC502))
  304. {
  305. T_UART1_BaudrateSwitch = HAL_GetTick();
  306. }
  307. //清除缓存
  308. UART_DelChar(ptUartRx, DataLength + 8);
  309. //通信指示
  310. HAL_GPIO_TogglePin(LED_Display_GPIO_Port, LED_Display_Pin);
  311. }
  312. else
  313. {
  314. UART_DelChar(ptUartRx, 1);
  315. }
  316. }
  317. }
  318. else
  319. {
  320. UART_DelChar(ptUartRx, 1);
  321. }
  322. }
  323. else
  324. {
  325. UART_DelChar(ptUartRx, 1);
  326. }
  327. }
  328. else
  329. {
  330. UART_DelChar(ptUartRx, 1);
  331. }
  332. }
  333. }
  334. //串口3数据解析
  335. void Uart3_RxData_Process(USART_Buf_TypeDef* ptUartRx, uint16_t TimeOut)
  336. {
  337. uint8_t Mode, CmdLength, DataLength;
  338. static uint8_t Data[255], CRC_Buf[255];
  339. uint16_t Cmd, i, ID;
  340. uint32_t CrcResult, CrcData;
  341. uint8_t FrameBegin1, FrameBegin2;
  342. static uint32_t DelayTimeCnt = 0;
  343. static TrueOrFalse_Flag_Struct_t IsWaitRX_Flag = FALSE;
  344. if(ptUartRx->ucBufCnt >= 13)
  345. {
  346. //读取帧头
  347. FrameBegin1 = UART_ReadChar(ptUartRx, 0);
  348. CRC_Buf[0] = FrameBegin1;
  349. FrameBegin2 = UART_ReadChar(ptUartRx, 1);
  350. CRC_Buf[1] = FrameBegin2;
  351. if((FrameBegin1 == FRAME_BEGIN1) && (FrameBegin2 == FRAME_BEGIN2))
  352. {
  353. CRC_Buf[2] = UART_ReadChar(ptUartRx, 2);
  354. CRC_Buf[3] = UART_ReadChar(ptUartRx, 3);
  355. ID = (uint16_t)(CRC_Buf[2] << 8) + CRC_Buf[3];
  356. //读取帧模式
  357. Mode = UART_ReadChar(ptUartRx, 4);
  358. CRC_Buf[4] = Mode;
  359. if((Mode == MODE_READ) || (Mode == MODE_WRITE) || (Mode == MODE_REPORT))
  360. {
  361. //读取命令段长度和命令字
  362. CmdLength = UART_ReadChar(ptUartRx, 5);
  363. CRC_Buf[5] = CmdLength;
  364. Cmd = (UART_ReadChar(ptUartRx, 6) << 8) + UART_ReadChar(ptUartRx, 7);
  365. CRC_Buf[6] = (uint8_t)((Cmd >> 8) & 0xFF);
  366. CRC_Buf[7] = (uint8_t)(Cmd & 0xFF);
  367. DataLength = UART_ReadChar(ptUartRx, 7);
  368. if((CmdLength - DataLength) == 2)
  369. {
  370. if(ptUartRx->ucBufCnt < (CmdLength + 11))//帧头2bytes + ID2bytes 模式1byte + 命令段长度1byte + 校验位4bytes + 帧尾1byte
  371. {
  372. if(IsWaitRX_Flag == FALSE)
  373. {
  374. DelayTimeCnt = HAL_GetTick();
  375. IsWaitRX_Flag = TRUE;
  376. }
  377. if((HAL_GetTick() - DelayTimeCnt) > TimeOut)//超时,单位ms
  378. {
  379. UART_DelChar(ptUartRx, ptUartRx->ucBufCnt);
  380. IsWaitRX_Flag = FALSE;
  381. }
  382. return;
  383. }
  384. else
  385. {
  386. IsWaitRX_Flag = FALSE;
  387. //接收到完整正确数据包
  388. for(i=0; i<DataLength; i++)//读取数据段
  389. {
  390. Data[i] = UART_ReadChar(ptUartRx, 8 + i);
  391. CRC_Buf[8 + i] = Data[i];
  392. }
  393. CrcData = (UART_ReadChar(ptUartRx, 8 + DataLength) << 24) + \
  394. (UART_ReadChar(ptUartRx, 9 + DataLength) << 16) + \
  395. (UART_ReadChar(ptUartRx, 10 + DataLength) << 8) + \
  396. UART_ReadChar(ptUartRx, 11 + DataLength);
  397. CrcResult = CRC32_Calculate(CRC_Buf, 8 + DataLength);
  398. if((CrcData - CrcResult) == 0) // 比较校验
  399. {
  400. //数据处理
  401. UART3_DataProcess(Mode, Cmd, Data);//Mode为帧模式,Cmd为命令字,Data为数据段
  402. //数据转发CAN
  403. memcpy((uint8_t*)(CRC_Buf + 2), (uint8_t*)(CRC_Buf + 4), DataLength + 4);
  404. CRC_Buf[6 + DataLength] = UART_ReadChar(ptUartRx, 8 + DataLength);
  405. CRC_Buf[7 + DataLength] = UART_ReadChar(ptUartRx, 9 + DataLength);
  406. CRC_Buf[8 + DataLength] = UART_ReadChar(ptUartRx, 10 + DataLength);
  407. CRC_Buf[9 + DataLength] = UART_ReadChar(ptUartRx, 11 + DataLength);
  408. CRC_Buf[10 + DataLength] = FRAME_END;
  409. //CAN_SendData(ID, CRC_Buf, DataLength + 11);
  410. UART_TransferData(&UART_TxBuff_Struct1, UARTADDR_CDL, Mode, ID, Cmd, Data);
  411. //清除缓存
  412. UART_DelChar(ptUartRx, CmdLength + 11);
  413. //通信指示
  414. HAL_GPIO_TogglePin(LED_Display_GPIO_Port, LED_Display_Pin);
  415. return;
  416. }
  417. UART_DelChar(ptUartRx, 1);
  418. }
  419. }
  420. else
  421. {
  422. UART_DelChar(ptUartRx, 1);
  423. }
  424. }
  425. else
  426. {
  427. UART_DelChar(ptUartRx, 1);
  428. }
  429. }
  430. else
  431. {
  432. UART_DelChar(ptUartRx, 1);
  433. }
  434. }
  435. }