uart_process.c 7.3 KB

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