uart_process.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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.4_20240710", 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. break;
  128. }
  129. default:break;
  130. }
  131. }
  132. static void UART1_DataProcess(uint8_t Mode, uint16_t Command, uint8_t* Data)
  133. {
  134. }
  135. //发送数据
  136. void UART_SendSwitch_Lidian2(USART_Buf_TypeDef * ptTx, uint8_t Addr, uint16_t Cmd, uint8_t* Data)
  137. {
  138. uint8_t databuf[256] = {0};
  139. uint8_t i = 0;
  140. uint8_t len = 0;
  141. uint16_t checkxor = 0;
  142. len = 20;
  143. databuf[0] = Addr;
  144. databuf[1] = len;
  145. databuf[2] = (uint8_t)((Cmd >> 8) & 0xFF);
  146. for(i=0; i<(Cmd & 0xFF); i++)
  147. {
  148. databuf[3+i] = Data[i];
  149. }
  150. for(i=0; i<len; i++)
  151. {
  152. checkxor ^= databuf[i];
  153. }
  154. databuf[len-1] = checkxor;
  155. UART_SendData(ptTx, databuf, len);
  156. }
  157. void UART_TransferData(USART_Buf_TypeDef * ptTx, uint8_t Addr, uint8_t Mode, uint16_t ID, uint16_t Cmd, uint8_t* Data)
  158. {
  159. uint8_t databuf[256] = {0};
  160. uint8_t i = 0;
  161. uint8_t len = 0;
  162. uint16_t checksum = 0;
  163. len = (uint8_t)((Cmd & 0xff) + 4);
  164. if(len > 247)
  165. {
  166. len = 247;
  167. }
  168. databuf[0] = UARTFRAME_BEGIN;
  169. databuf[1] = Addr;
  170. databuf[2] = Mode;
  171. databuf[3] = len;
  172. databuf[4] = (uint8_t)((ID >> 8) & 0xFF);
  173. databuf[5] = (uint8_t)(ID & 0xFF);
  174. databuf[6] = (uint8_t)((Cmd >> 8) & 0xFF);
  175. databuf[7] = (uint8_t)(Cmd & 0xFF);
  176. for(i=0; i<(len-4); i++)
  177. {
  178. databuf[8+i] = Data[i];
  179. }
  180. for(i=1; i<(len+4); i++)
  181. {
  182. checksum = (uint16_t)(databuf[i] + checksum);
  183. }
  184. databuf[4+len] = (uint8_t)checksum;
  185. databuf[5+len] = (uint8_t)(checksum>>8);
  186. databuf[6+len] = UARTFRAME_END1;
  187. databuf[7+len] = UARTFRAME_END2;
  188. UART_SendData(ptTx, databuf, len + 8);
  189. }
  190. void SendCmdData(USART_Buf_TypeDef * ptTx, uint8_t Mode, uint16_t ID, uint16_t Command, uint8_t* Data)
  191. {
  192. uint8_t SendBuf[255] = {0};
  193. uint8_t CRC_Buf[255] = {0};
  194. uint32_t CRC_Result = 0x00000000;
  195. uint8_t DataLength;
  196. DataLength = (uint8_t)(Command & 0xFF);
  197. SendBuf[0] = FRAME_BEGIN1;
  198. SendBuf[1] = FRAME_BEGIN2;
  199. SendBuf[2] = (uint8_t)((ID >> 8) & 0xFF);
  200. SendBuf[3] = (uint8_t)(ID & 0xFF);
  201. SendBuf[4] = Mode;
  202. SendBuf[5] = DataLength + 2;
  203. SendBuf[6] = (uint8_t)((Command >> 8) & 0xFF);
  204. SendBuf[7] = DataLength;
  205. memcpy(SendBuf + 8, Data, DataLength);
  206. memcpy(CRC_Buf, SendBuf, 8 + DataLength);
  207. CRC_Result = CRC32_Calculate(CRC_Buf, DataLength + 8);
  208. SendBuf[8 + DataLength] = (uint8_t)((CRC_Result >> 24) & 0xFF);
  209. SendBuf[9 + DataLength] = (uint8_t)((CRC_Result >> 16) & 0xFF);
  210. SendBuf[10 + DataLength] = (uint8_t)((CRC_Result >> 8) & 0xFF);
  211. SendBuf[11 + DataLength] = (uint8_t)(CRC_Result & 0xFF);
  212. SendBuf[12 + DataLength] = FRAME_END;
  213. UART_SendData(ptTx, SendBuf, DataLength + 13);
  214. }
  215. //串口1数据解析
  216. void Uart1_RxData_Process(USART_Buf_TypeDef* ptUartRx, uint16_t TimeOut)
  217. {
  218. uint8_t Mode, DataLength, Addr;
  219. static uint8_t Data[255];
  220. uint16_t Cmd, i, ID;
  221. uint16_t CheckSumResult=0, CheckSumData=0;
  222. uint8_t FrameBegin, FrameEnd1, FrameEnd2;
  223. static uint32_t DelayTimeCnt = 0;
  224. static TrueOrFalse_Flag_Struct_t IsWaitRX_Flag = FALSE;
  225. if(ptUartRx->ucBufCnt >= 8)
  226. {
  227. //读取帧头
  228. FrameBegin = UART_ReadChar(ptUartRx, 0);
  229. if(FrameBegin == UARTFRAME_BEGIN)
  230. {
  231. Addr = UART_ReadChar(ptUartRx, 1);//读取地址ID
  232. if(Addr == UARTADDR_CDL)
  233. {
  234. //读取帧模式
  235. Mode = UART_ReadChar(ptUartRx, 2);
  236. if((Mode == UARTMODE_READ) || (Mode == UARTMODE_WRITE) || (Mode == UARTMODE_REPORT))
  237. {
  238. //读取数据段长度
  239. DataLength = UART_ReadChar(ptUartRx, 3);
  240. if(ptUartRx->ucBufCnt < (DataLength + 8))//帧头1byte + 地址1byte + 模式1byte + 长度1byte + 校验位2bytes + 帧尾2bytes
  241. {
  242. if(IsWaitRX_Flag == FALSE)
  243. {
  244. DelayTimeCnt = HAL_GetTick();
  245. IsWaitRX_Flag = TRUE;
  246. }
  247. if((HAL_GetTick() - DelayTimeCnt) > TimeOut)//超时,单位ms
  248. {
  249. UART_DelChar(ptUartRx, ptUartRx->ucBufCnt);
  250. IsWaitRX_Flag = FALSE;
  251. }
  252. return;
  253. }
  254. else
  255. {
  256. IsWaitRX_Flag = FALSE;
  257. //接收到完整正确数据包
  258. for(i=0; i<(DataLength+4); i++)//读取数据段
  259. {
  260. Data[i] = UART_ReadChar(ptUartRx, i);
  261. }
  262. CheckSumData = (UART_ReadChar(ptUartRx, 4 + DataLength)) + \
  263. (UART_ReadChar(ptUartRx, 5 + DataLength) << 8);
  264. for(i = 1; i<(DataLength+4); i++)
  265. {
  266. CheckSumResult = (uint16_t)(Data[i] + CheckSumResult);
  267. }
  268. FrameEnd1 = UART_ReadChar(ptUartRx, 6 + DataLength);
  269. FrameEnd2 = UART_ReadChar(ptUartRx, 7 + DataLength);
  270. if((CheckSumData == CheckSumResult) && (FrameEnd1 == UARTFRAME_END1) && (FrameEnd2 == UARTFRAME_END2))// 比较校验和帧尾
  271. {
  272. //数据处理
  273. UART1_DataProcess(Mode, Cmd, &Data[4]);//Mode为帧模式,Cmd为命令字,Data为数据段
  274. if(DataLength >= 4)//数据段包含ID+Cmd,至少4bytes
  275. {
  276. ID = (uint16_t)((Data[4]<<8) + Data[5]);
  277. Cmd = (uint16_t)((Data[6]<<8) + Data[7]);
  278. SendCmdData(&UART_TxBuff_Struct3, Mode, ID, Cmd, &Data[8]);
  279. }
  280. if((Cmd==0xC109) || (Cmd==0xC202) || (Cmd==0xC302) || (Cmd==0xC402) || (Cmd==0xC502))
  281. {
  282. T_UART1_BaudrateSwitch = HAL_GetTick();
  283. }
  284. //清除缓存
  285. UART_DelChar(ptUartRx, DataLength + 8);
  286. //通信指示
  287. HAL_GPIO_TogglePin(LED_Display_GPIO_Port, LED_Display_Pin);
  288. }
  289. else
  290. {
  291. UART_DelChar(ptUartRx, 1);
  292. }
  293. }
  294. }
  295. else
  296. {
  297. UART_DelChar(ptUartRx, 1);
  298. }
  299. }
  300. else
  301. {
  302. UART_DelChar(ptUartRx, 1);
  303. }
  304. }
  305. else
  306. {
  307. UART_DelChar(ptUartRx, 1);
  308. }
  309. }
  310. }
  311. //串口3数据解析
  312. void Uart3_RxData_Process(USART_Buf_TypeDef* ptUartRx, uint16_t TimeOut)
  313. {
  314. uint8_t Mode, CmdLength, DataLength;
  315. static uint8_t Data[255], CRC_Buf[255];
  316. uint16_t Cmd, i, ID;
  317. uint32_t CrcResult, CrcData;
  318. uint8_t FrameBegin1, FrameBegin2;
  319. static uint32_t DelayTimeCnt = 0;
  320. static TrueOrFalse_Flag_Struct_t IsWaitRX_Flag = FALSE;
  321. if(ptUartRx->ucBufCnt >= 13)
  322. {
  323. //读取帧头
  324. FrameBegin1 = UART_ReadChar(ptUartRx, 0);
  325. CRC_Buf[0] = FrameBegin1;
  326. FrameBegin2 = UART_ReadChar(ptUartRx, 1);
  327. CRC_Buf[1] = FrameBegin2;
  328. if((FrameBegin1 == FRAME_BEGIN1) && (FrameBegin2 == FRAME_BEGIN2))
  329. {
  330. CRC_Buf[2] = UART_ReadChar(ptUartRx, 2);
  331. CRC_Buf[3] = UART_ReadChar(ptUartRx, 3);
  332. ID = (uint16_t)(CRC_Buf[2] << 8) + CRC_Buf[3];
  333. //读取帧模式
  334. Mode = UART_ReadChar(ptUartRx, 4);
  335. CRC_Buf[4] = Mode;
  336. if((Mode == MODE_READ) || (Mode == MODE_WRITE) || (Mode == MODE_REPORT))
  337. {
  338. //读取命令段长度和命令字
  339. CmdLength = UART_ReadChar(ptUartRx, 5);
  340. CRC_Buf[5] = CmdLength;
  341. Cmd = (UART_ReadChar(ptUartRx, 6) << 8) + UART_ReadChar(ptUartRx, 7);
  342. CRC_Buf[6] = (uint8_t)((Cmd >> 8) & 0xFF);
  343. CRC_Buf[7] = (uint8_t)(Cmd & 0xFF);
  344. DataLength = UART_ReadChar(ptUartRx, 7);
  345. if((CmdLength - DataLength) == 2)
  346. {
  347. if(ptUartRx->ucBufCnt < (CmdLength + 11))//帧头2bytes + ID2bytes 模式1byte + 命令段长度1byte + 校验位4bytes + 帧尾1byte
  348. {
  349. if(IsWaitRX_Flag == FALSE)
  350. {
  351. DelayTimeCnt = HAL_GetTick();
  352. IsWaitRX_Flag = TRUE;
  353. }
  354. if((HAL_GetTick() - DelayTimeCnt) > TimeOut)//超时,单位ms
  355. {
  356. UART_DelChar(ptUartRx, ptUartRx->ucBufCnt);
  357. IsWaitRX_Flag = FALSE;
  358. }
  359. return;
  360. }
  361. else
  362. {
  363. IsWaitRX_Flag = FALSE;
  364. //接收到完整正确数据包
  365. for(i=0; i<DataLength; i++)//读取数据段
  366. {
  367. Data[i] = UART_ReadChar(ptUartRx, 8 + i);
  368. CRC_Buf[8 + i] = Data[i];
  369. }
  370. CrcData = (UART_ReadChar(ptUartRx, 8 + DataLength) << 24) + \
  371. (UART_ReadChar(ptUartRx, 9 + DataLength) << 16) + \
  372. (UART_ReadChar(ptUartRx, 10 + DataLength) << 8) + \
  373. UART_ReadChar(ptUartRx, 11 + DataLength);
  374. CrcResult = CRC32_Calculate(CRC_Buf, 8 + DataLength);
  375. if((CrcData - CrcResult) == 0) // 比较校验
  376. {
  377. //数据处理
  378. UART3_DataProcess(Mode, Cmd, Data);//Mode为帧模式,Cmd为命令字,Data为数据段
  379. //数据转发CAN
  380. memcpy((uint8_t*)(CRC_Buf + 2), (uint8_t*)(CRC_Buf + 4), DataLength + 4);
  381. CRC_Buf[6 + DataLength] = UART_ReadChar(ptUartRx, 8 + DataLength);
  382. CRC_Buf[7 + DataLength] = UART_ReadChar(ptUartRx, 9 + DataLength);
  383. CRC_Buf[8 + DataLength] = UART_ReadChar(ptUartRx, 10 + DataLength);
  384. CRC_Buf[9 + DataLength] = UART_ReadChar(ptUartRx, 11 + DataLength);
  385. CRC_Buf[10 + DataLength] = FRAME_END;
  386. //CAN_SendData(ID, CRC_Buf, DataLength + 11);
  387. UART_TransferData(&UART_TxBuff_Struct1, UARTADDR_CDL, Mode, ID, Cmd, Data);
  388. //清除缓存
  389. UART_DelChar(ptUartRx, CmdLength + 11);
  390. //通信指示
  391. HAL_GPIO_TogglePin(LED_Display_GPIO_Port, LED_Display_Pin);
  392. return;
  393. }
  394. UART_DelChar(ptUartRx, 1);
  395. }
  396. }
  397. else
  398. {
  399. UART_DelChar(ptUartRx, 1);
  400. }
  401. }
  402. else
  403. {
  404. UART_DelChar(ptUartRx, 1);
  405. }
  406. }
  407. else
  408. {
  409. UART_DelChar(ptUartRx, 1);
  410. }
  411. }
  412. }