tasks.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. #include "tasks.h"
  2. #include "iwdg.h"
  3. #include "gpio.h"
  4. #include "tim.h"
  5. #include "can_process.h"
  6. #include "uart_process.h"
  7. #include "eeprom_24c02.h"
  8. #include "eeprom_flash.h"
  9. #include "adc.h"
  10. #include "hall_sensor.h"
  11. #include "torque_sensor.h"
  12. #include "gas_sensor.h"
  13. #include "math_tools.h"
  14. #include "remain_distance.h"
  15. #include "protect_check.h"
  16. #include "fault_check.h"
  17. #include "key_driver.h"
  18. #include "encrypt.h"
  19. /************************全局变量************************/
  20. TrueOrFalse_Flag_Struct_t IsInitFinish_Flag = FALSE;
  21. /**************************局部函数定义*********************/
  22. /**************************全局函数定义*********************/
  23. //1ms任务处理函数
  24. void HAL_SYSTICK_Callback(void)
  25. {
  26. static uint16_t TimeCnt_10ms = 0;
  27. static uint16_t TimeCnt_50ms = 0;
  28. static uint16_t TimeCnt_100ms = 0;
  29. static uint16_t TimeCnt_1000ms = 0;
  30. static uint32_t FaultDelayTime = 0;
  31. if(IsInitFinish_Flag == FALSE)
  32. {
  33. return;
  34. }
  35. //踏频传感器采集及计算
  36. CadenceSensor_Process(&MC_CadenceResult, MC_ConfigParam.StopTime);
  37. //根据电机工作模式(MC_WorkMode)、力矩传感器和指拨AD值(ADC_SensorData)、控制档位(MC_ControlCode.GearSt)计算控制FOC输入值(MC_CalParam)
  38. if(((uint8_t)(MC_WorkMode ^ MC_WorkMode_Back) != (uint8_t)~0) ||
  39. ((uint8_t)(MC_ControlCode.GearSt ^ MC_ControlCode_Back.GearSt) != (uint8_t)~0) ||
  40. ((uint8_t)(MC_ControlCode.LightSwitch ^ MC_ControlCode_Back.LightSwitch) != (uint8_t)~0))
  41. {
  42. if((HAL_GetTick() - FaultDelayTime) > 200)
  43. {
  44. MC_ErrorCode.ERROR_Bit.Fault_MCU = 1;
  45. }
  46. }
  47. else
  48. {
  49. FaultDelayTime = HAL_GetTick();
  50. }
  51. MC_CalParam_Cal(MC_WorkMode, ADC_SensorData, MC_ControlCode.GearSt, IsBreakTrig_Flag, IsGearSensorTrig_Flag, &MC_CalParam);
  52. //更新控制参数备份值
  53. Update_MC_CalParam_Back();
  54. //更新力矩传感器零点值
  55. TorqueOffSetData_Present_Update(&TorqueOffSetData.PresentData, ADC1_Result[ADC1_RANK_TORQUE_SENSOR], MC_TorqueCorrectParam.K, &MC_ErrorCode);
  56. //更新指拨零点值
  57. GasSensorOffSetData_Update(&GasSensor_OffSet, ADC1_Result[ADC1_RANK_GAS], &MC_ErrorCode);
  58. //10ms任务
  59. TimeCnt_10ms++;
  60. if(TimeCnt_10ms >= 10)
  61. {
  62. TimeCnt_10ms = 0;
  63. }
  64. //50ms任务
  65. TimeCnt_50ms++;
  66. if(TimeCnt_50ms >= 50)
  67. {
  68. TimeCnt_50ms = 0;
  69. //计算TE同步时钟频率
  70. Cal_SyncClockFreq(&MC_TE_SyncClockFreqScan);
  71. }
  72. //100ms任务
  73. TimeCnt_100ms++;
  74. if(TimeCnt_100ms >= 100)
  75. {
  76. TimeCnt_100ms = 0;
  77. //踏频计算滑动均值滤波
  78. MC_RunInfo.Cadence = MovingAverageFilter(MC_CadenceResult.Cadence_Data, MC_Cadence_Array, sizeof(MC_Cadence_Array) / 2);
  79. }
  80. //1000ms任务
  81. TimeCnt_1000ms++;
  82. if(TimeCnt_1000ms >= 1000)
  83. {
  84. TimeCnt_1000ms = 0;
  85. }
  86. }
  87. //CAN数据解析函数
  88. void CanRx_Process(void)
  89. {
  90. CAN_RxData_Process(&CAN_RxBuf_Struct_PBU, 500);
  91. CAN_RxData_Process(&CAN_RxBuf_Struct_BMS, 500);
  92. CAN_RxData_Process(&CAN_RxBuf_Struct_HMI, 500);
  93. CAN_RxData_Process(&CAN_RxBuf_Struct_CDL, 500);
  94. }
  95. //UART数据解析函数
  96. void UartRx_Process(void)
  97. {
  98. Uart_RxData_Process(&UART_RxBuff_Struct3, 500);
  99. }
  100. //MC运行信息更新
  101. void MC_RunInfo_Update(void)
  102. {
  103. static uint32_t PeriodTimeCnt = 0;
  104. if((HAL_GetTick() - PeriodTimeCnt) >= 200)
  105. {
  106. PeriodTimeCnt = HAL_GetTick();
  107. //计算电功率
  108. MC_RunInfo.Power = (uint16_t)((uint32_t)(MC_RunInfo.BusCurrent / 100 * MC_RunInfo.BusVoltage / 100) / 100) / 2;
  109. MC_RunInfo.Power = (MC_RunInfo.Power < 20) ? 0 : MC_RunInfo.Power;
  110. //更新踏频方向
  111. MC_RunInfo.CadenceDir = MC_CadenceResult.Cadence_Dir;
  112. //计算力矩值
  113. MC_RunInfo.Torque = ADC_SensorData.TorqueSensor / 28;
  114. //当前助力档位
  115. MC_RunInfo.GearSt = MC_ControlCode.GearSt;
  116. //当前灯开关
  117. MC_RunInfo.LightSwitch = MC_ControlCode.LightSwitch;
  118. //剩余电量
  119. MC_RunInfo.SOC = (DeviceOnLine_Status.Status_Bit.BMS_OffLine == 1) ? Battery_SocCal(MC_RunInfo.BusVoltage, MC_RunInfo.BusCurrent)
  120. : BMS_RunInfo.SOC;
  121. //续航里程
  122. MC_RunInfo.RemainDistance = RemainDis.remainDistance;
  123. //骑行总里程计算
  124. static uint32_t WheelTurnCount = 0;
  125. if((MC_SpeedSensorData.WheelTurnCount - WheelTurnCount) >= (10000 / MC_ConfigParam.WheelSize)) // 统计单位0.1km
  126. {
  127. MC_RunLog.ODO_Km++;
  128. MC_RunInfo.ODO_Km = MC_RunLog.ODO_Km / 10;
  129. SaveParamToEEprom_24C02(&I2C_Handle_EEPROM, EEPROM_24C02_ADDR_RUN_LOG, sizeof(MC_RunLog_Struct_t), (uint8_t*)&MC_RunLog.PowerOnCnt);
  130. WheelTurnCount = MC_SpeedSensorData.WheelTurnCount;
  131. }
  132. //骑行总时间计算
  133. static uint8_t Period_1sCnt = 0;
  134. static uint16_t SavePeriod_Cnt = 0;
  135. if(MC_RunInfo.BikeSpeed >= 30)
  136. {
  137. Period_1sCnt++;
  138. if(Period_1sCnt >= 5) //运行周期为200ms,计时周期200 * 5 = 1s
  139. {
  140. Period_1sCnt = 0;
  141. MC_RunInfo.ODO_Time++;
  142. MC_RunLog.ODO_Time++;
  143. SavePeriod_Cnt++;
  144. }
  145. }
  146. else
  147. {
  148. Period_1sCnt = 0;
  149. }
  150. //存储骑行总时间
  151. if(SavePeriod_Cnt >= 60) //累计骑行60s进行存储
  152. {
  153. SavePeriod_Cnt = 0;
  154. SaveParamToEEprom_24C02(&I2C_Handle_EEPROM, EEPROM_24C02_ADDR_RUN_LOG, sizeof(MC_RunLog_Struct_t), (uint8_t*)&MC_RunLog.PowerOnCnt);
  155. }
  156. //平均功耗
  157. MC_RunInfo.PowerPerKm = RemainDis.Power_per_km_result / 10;
  158. }
  159. }
  160. //MC故障码发送
  161. void MC_SendErrorCode_Process(MC_ErrorCode_Struct_t ErrorCode)
  162. {
  163. static uint32_t PeriodTimeCnt = 0;
  164. if(ErrorCode.Code != 0x00000000)
  165. {
  166. if((HAL_GetTick() - PeriodTimeCnt) > 500)
  167. {
  168. PeriodTimeCnt = HAL_GetTick();
  169. SendData(ID_MC_BC, MODE_REPORT, 0x1104, (uint8_t*)&ErrorCode.Code);
  170. }
  171. }
  172. else
  173. {
  174. PeriodTimeCnt = HAL_GetTick();
  175. }
  176. }
  177. //MC主动发送运行信息
  178. void MC_SendRunInfo_Process(MC_WorkMode_Struct_t WorkMode)
  179. {
  180. static uint32_t PeriodTimeCnt = 0;
  181. if(WorkMode == MC_WorkMode_Config)
  182. {
  183. if((HAL_GetTick() - PeriodTimeCnt) >= 200)
  184. {
  185. SendData(ID_MC_BC, MODE_REPORT, 0x1020, (uint8_t*)&MC_RunInfo.BikeSpeed);
  186. PeriodTimeCnt = HAL_GetTick();
  187. }
  188. }
  189. else
  190. {
  191. PeriodTimeCnt = HAL_GetTick();
  192. }
  193. }
  194. //发给TE的传感器数据处理
  195. void MC_TE_SensorData_Process(uint16_t Speed, MC_TE_SensorData_Struct_t* p_MC_TE_SensorData)
  196. {
  197. static uint32_t PeriodTimeCnt = 0;
  198. static GPIO_PinState Cadence_Hall_1;
  199. static GPIO_PinState Cadence_Hall_2;
  200. static GPIO_PinState Motor_Hall_A;
  201. static GPIO_PinState Motor_Hall_B;
  202. static GPIO_PinState Break;
  203. static GPIO_PinState SpeedSensor;
  204. static TrueOrFalse_Flag_Struct_t IsFirstEnterFalg = TRUE;
  205. GPIO_PinState GPIO_PinState_Temp;
  206. //初始化变量
  207. if(IsFirstEnterFalg == TRUE)
  208. {
  209. Cadence_Hall_1 = HAL_GPIO_ReadPin(CADENCE_1_GPIO_Port, CADENCE_1_Pin);
  210. Cadence_Hall_2 = HAL_GPIO_ReadPin(CADENCE_2_GPIO_Port, CADENCE_2_Pin);
  211. Motor_Hall_A = HAL_GPIO_ReadPin(HALL_A_GPIO_Port, HALL_A_Pin);
  212. Motor_Hall_B = HAL_GPIO_ReadPin(HALL_B_GPIO_Port, HALL_B_Pin);
  213. Break = HAL_GPIO_ReadPin(BREAK_IN_GPIO_Port, BREAK_IN_Pin);
  214. SpeedSensor = HAL_GPIO_ReadPin(SPEED_SENSOR_GPIO_Port, SPEED_SENSOR_Pin);
  215. p_MC_TE_SensorData->CadenceHall_1_Cnt = 0;
  216. p_MC_TE_SensorData->CadenceHall_2_Cnt = 0;
  217. p_MC_TE_SensorData->MotorHall_A_Cnt = 0;
  218. p_MC_TE_SensorData->MotorHall_B_Cnt = 0;
  219. p_MC_TE_SensorData->BreakTrgiCnt = 0;
  220. p_MC_TE_SensorData->SpeedSensorTrigCnt = 0;
  221. IsFirstEnterFalg = FALSE;
  222. }
  223. //ADC数据更新采集
  224. p_MC_TE_SensorData->AD_BusCurrent = ADC1_Result_Filt[ADC1_RANK_CURRENT];
  225. p_MC_TE_SensorData->AD_RoilTemp = ADC1_Result_Filt[ADC1_RANK_NTC_ROIL];
  226. p_MC_TE_SensorData->AD_TE_Voltage = ADC1_Result_Filt[ADC1_RANK_3V3_TE];
  227. p_MC_TE_SensorData->AD_Torque = ADC1_Result_Filt[ADC1_RANK_TORQUE_SENSOR];
  228. //踏频霍尔1
  229. GPIO_PinState_Temp = HAL_GPIO_ReadPin(CADENCE_1_GPIO_Port, CADENCE_1_Pin);
  230. if(Cadence_Hall_1 != GPIO_PinState_Temp)
  231. {
  232. p_MC_TE_SensorData->CadenceHall_1_Cnt++;
  233. }
  234. Cadence_Hall_1 = GPIO_PinState_Temp;
  235. //踏频霍尔2
  236. GPIO_PinState_Temp = HAL_GPIO_ReadPin(CADENCE_2_GPIO_Port, CADENCE_2_Pin);
  237. if(Cadence_Hall_2 != GPIO_PinState_Temp)
  238. {
  239. p_MC_TE_SensorData->CadenceHall_2_Cnt++;
  240. }
  241. Cadence_Hall_2 = GPIO_PinState_Temp;
  242. //马达霍尔A
  243. GPIO_PinState_Temp = HAL_GPIO_ReadPin(HALL_A_GPIO_Port, HALL_A_Pin);
  244. if(Motor_Hall_A != GPIO_PinState_Temp)
  245. {
  246. p_MC_TE_SensorData->MotorHall_A_Cnt++;
  247. }
  248. Motor_Hall_A = GPIO_PinState_Temp;
  249. //马达霍尔B
  250. GPIO_PinState_Temp = HAL_GPIO_ReadPin(HALL_B_GPIO_Port, HALL_B_Pin);
  251. if(Motor_Hall_B != GPIO_PinState_Temp)
  252. {
  253. p_MC_TE_SensorData->MotorHall_B_Cnt++;
  254. }
  255. Motor_Hall_B = GPIO_PinState_Temp;
  256. //刹车
  257. GPIO_PinState_Temp = HAL_GPIO_ReadPin(BREAK_IN_GPIO_Port, BREAK_IN_Pin);
  258. if(Break != GPIO_PinState_Temp)
  259. {
  260. p_MC_TE_SensorData->BreakTrgiCnt++;
  261. }
  262. Break = GPIO_PinState_Temp;
  263. //速度传感器
  264. GPIO_PinState_Temp = HAL_GPIO_ReadPin(SPEED_SENSOR_GPIO_Port, SPEED_SENSOR_Pin);
  265. if(SpeedSensor != GPIO_PinState_Temp)
  266. {
  267. p_MC_TE_SensorData->SpeedSensorTrigCnt++;
  268. }
  269. SpeedSensor = GPIO_PinState_Temp;
  270. //同步时钟频率
  271. p_MC_TE_SensorData->SynC_Clock_Freq = 1000; //1000KHz
  272. //数据发送
  273. if((HAL_GetTick() - PeriodTimeCnt) >= 500)
  274. {
  275. SendCmdData(&UART_TxBuff_Struct3, MODE_REPORT, 0x1014, (uint8_t*)&p_MC_TE_SensorData->AD_Torque);
  276. IsComOK_TE = FALSE;
  277. PeriodTimeCnt = HAL_GetTick();
  278. }
  279. }
  280. //根据踏频和母线电流计算限流系数
  281. uint8_t MC_CadenceLimit_Cal(uint8_t Cadence, uint16_t Current, uint8_t T_Roil)
  282. {
  283. static uint32_t PeriodTimeCnt = 0;
  284. static uint32_t IdcFiltSum = 0;
  285. static uint8_t IdcFiltCnt = 0; //滤波输入值计算
  286. static uint16_t IdcFilt = 0; //滤波结果
  287. static uint16_t Limit_Cnt = 0; //限流计时值
  288. static uint16_t OK_Cnt = 0; //限流恢复计时值
  289. static FlagStatus LimitFlag = RESET;
  290. static uint8_t Result = 100;
  291. if((HAL_GetTick() - PeriodTimeCnt) >= 100)
  292. {
  293. PeriodTimeCnt = HAL_GetTick();
  294. //母线电流滤波
  295. IdcFiltSum += Current;
  296. IdcFiltCnt++;
  297. if(IdcFiltCnt >= 8)
  298. {
  299. IdcFilt = IdcFiltSum >> 3;
  300. IdcFiltCnt = 0;
  301. IdcFiltSum = 0;
  302. }
  303. //限流保护计时
  304. if((Cadence < 70) && (IdcFilt > 6000))
  305. {
  306. Limit_Cnt++;
  307. }
  308. else
  309. {
  310. Limit_Cnt = 0;
  311. }
  312. //限流恢复计时
  313. if(((Cadence > 70) || (IdcFilt < 5000)) && (T_Roil < 150))
  314. {
  315. OK_Cnt++;
  316. }
  317. else
  318. {
  319. OK_Cnt = 0;
  320. }
  321. //限流判断
  322. if(Limit_Cnt > 300)
  323. {
  324. Limit_Cnt = 0;
  325. LimitFlag = SET;
  326. }
  327. //限流恢复判断
  328. if(OK_Cnt > 100)
  329. {
  330. OK_Cnt = 0;
  331. LimitFlag = RESET;
  332. }
  333. //限流系数计算
  334. if(LimitFlag == SET)
  335. {
  336. if(Cadence < 70)
  337. {
  338. Result = 30 + Cadence;
  339. Result = (Result > 100) ? 100 : Result;
  340. }
  341. else
  342. {
  343. Result = 100;
  344. }
  345. }
  346. else
  347. {
  348. Result = 100;
  349. }
  350. }
  351. return Result;
  352. }
  353. //接收到关机指令处理
  354. void PowerOff_Process(void)
  355. {
  356. MC_ControlCode.GearSt = MC_GearSt_OFF;
  357. Update_MC_ControlCode_Back();
  358. SendData(ID_MC_BC, MODE_REPORT, 0x1305, (uint8_t*)"READY");
  359. }
  360. //CAN设备PBU通信状态检测处理
  361. void MC_CanRxCheck_Process(MC_SupportFlag_Struct_t NoPBU_Flag, MC_SupportFlag_Struct_t NoHMI_Flag, MC_WorkMode_Struct_t WorkMode, MC_GearSt_Struct_t* GearSt)
  362. {
  363. if((WorkMode == MC_WorkMode_Run) && (NoPBU_Flag == MC_SUPPORT_DISABLE)) //不支持无PBU,且正常运行模式
  364. {
  365. //PBU通信状态检测
  366. if(IsComOK_PBU.IsOK_Flag == TRUE)
  367. {
  368. if((HAL_GetTick() - IsComOK_PBU.OK_TrigTime) > 1000)
  369. {
  370. IsComOK_PBU.IsOK_Flag = FALSE;
  371. *GearSt = MC_GearSt_OFF;
  372. Update_MC_ControlCode_Back();
  373. DeviceOnLine_Status.Status_Bit.PBU_OffLine = 1;
  374. }
  375. else
  376. {
  377. DeviceOnLine_Status.Status_Bit.PBU_OffLine = 0;
  378. }
  379. }
  380. else
  381. {
  382. *GearSt = MC_GearSt_OFF;
  383. Update_MC_ControlCode_Back();
  384. DeviceOnLine_Status.Status_Bit.PBU_OffLine = 1;
  385. }
  386. //HMI通信状态检测
  387. if(NoHMI_Flag == MC_SUPPORT_ENABLE)
  388. {
  389. DeviceOnLine_Status.Status_Bit.HMI_OffLine = 0;
  390. }
  391. else
  392. {
  393. if(IsComOK_HMI.IsOK_Flag == TRUE)
  394. {
  395. if((HAL_GetTick() - IsComOK_HMI.OK_TrigTime) > 1000)
  396. {
  397. IsComOK_HMI.IsOK_Flag = FALSE;
  398. DeviceOnLine_Status.Status_Bit.PBU_OffLine = 1;
  399. }
  400. else
  401. {
  402. DeviceOnLine_Status.Status_Bit.PBU_OffLine = 0;
  403. }
  404. }
  405. else
  406. {
  407. DeviceOnLine_Status.Status_Bit.HMI_OffLine = 1;
  408. }
  409. }
  410. //BMS通信状态检测
  411. if(IsComOK_BMS.IsOK_Flag == TRUE)
  412. {
  413. if((HAL_GetTick() - IsComOK_BMS.OK_TrigTime) > 1000)
  414. {
  415. IsComOK_BMS.IsOK_Flag = FALSE;
  416. DeviceOnLine_Status.Status_Bit.BMS_OffLine = 1;
  417. }
  418. else
  419. {
  420. DeviceOnLine_Status.Status_Bit.BMS_OffLine = 0;
  421. }
  422. }
  423. else
  424. {
  425. DeviceOnLine_Status.Status_Bit.BMS_OffLine = 1;
  426. }
  427. }
  428. }
  429. //运行总时间计算
  430. void MC_RunTime_Cal(uint32_t* p_Runtime)
  431. {
  432. static uint32_t PeriodTimeCnt = 0;
  433. if((HAL_GetTick()- PeriodTimeCnt) >= 60000)
  434. {
  435. PeriodTimeCnt = HAL_GetTick();
  436. (*p_Runtime)++;
  437. //存储运行总时间
  438. SaveParamToEEprom_24C02(&I2C_Handle_EEPROM, EEPROM_24C02_ADDR_RUN_LOG, sizeof(MC_RunLog_Struct_t), (uint8_t*)&MC_RunLog.PowerOnCnt);
  439. }
  440. }
  441. //设备在线检测
  442. void MC_OnLineCheck(OnLine_Status_Struct_t* p_OnLineStatus, MC_ErrorCode_Struct_t* p_ErrorCode)
  443. {
  444. uint8_t SendTimeCnt;
  445. uint32_t PeriodTimeCnt;
  446. //发送指令查询PBU是否在线
  447. PeriodTimeCnt = HAL_GetTick();
  448. SendTimeCnt = 0;
  449. do
  450. {
  451. CAN_RxData_Process(&CAN_RxBuf_Struct_PBU, 500);
  452. if((HAL_GetTick() - PeriodTimeCnt) >= 50)
  453. {
  454. PeriodTimeCnt = HAL_GetTick();
  455. SendTimeCnt++;
  456. SendData(ID_MC_TO_PBU, MODE_READ, 0x5009, (uint8_t*)"HANDSHAKE");
  457. }
  458. //看门狗清零
  459. #if DEBUG
  460. HAL_IWDG_Refresh(&hiwdg);
  461. #endif
  462. }while((p_OnLineStatus->Status_Bit.PBU_OffLine == 1) && (SendTimeCnt <= 4));
  463. p_ErrorCode->ERROR_Bit.Fault_PBU_Check = p_OnLineStatus->Status_Bit.PBU_OffLine;
  464. //发送指令查询HMI是否在线
  465. if(PBU_ConfigParam.NoHMI_Flag == MC_SUPPORT_DISABLE)
  466. {
  467. PeriodTimeCnt = HAL_GetTick();
  468. SendTimeCnt = 0;
  469. do
  470. {
  471. CAN_RxData_Process(&CAN_RxBuf_Struct_HMI, 500);
  472. if((HAL_GetTick() - PeriodTimeCnt) >= 50)
  473. {
  474. PeriodTimeCnt = HAL_GetTick();
  475. SendTimeCnt++;
  476. SendData(ID_MC_TO_HMI, MODE_READ, 0x7009, (uint8_t*)"HANDSHAKE");
  477. }
  478. //看门狗清零
  479. #if DEBUG
  480. HAL_IWDG_Refresh(&hiwdg);
  481. #endif
  482. }while((p_OnLineStatus->Status_Bit.HMI_OffLine == 1) && (SendTimeCnt <= 4));
  483. }
  484. else
  485. {
  486. p_OnLineStatus->Status_Bit.HMI_OffLine = 0;
  487. }
  488. p_ErrorCode->ERROR_Bit.Fault_HMI_Check = p_OnLineStatus->Status_Bit.HMI_OffLine;
  489. //发送指令查询BMS是否在线
  490. PeriodTimeCnt = HAL_GetTick();
  491. SendTimeCnt = 0;
  492. do
  493. {
  494. CAN_RxData_Process(&CAN_RxBuf_Struct_BMS, 500);
  495. if((HAL_GetTick() - PeriodTimeCnt) >= 50)
  496. {
  497. PeriodTimeCnt = HAL_GetTick();
  498. SendTimeCnt++;
  499. SendData(ID_MC_TO_BMS, MODE_READ, 0x3009, (uint8_t*)"HANDSHAKE");
  500. }
  501. //看门狗清零
  502. #if DEBUG
  503. HAL_IWDG_Refresh(&hiwdg);
  504. #endif
  505. }while((p_OnLineStatus->Status_Bit.BMS_OffLine == 1) && (SendTimeCnt <= 4));
  506. p_ErrorCode->ERROR_Bit.Fault_BMS_Check = p_OnLineStatus->Status_Bit.BMS_OffLine;
  507. }
  508. //设备授权校验
  509. void MC_DeviceCheck(MC_ErrorCode_Struct_t* p_ErrorCode)
  510. {
  511. uint8_t T[16];
  512. CheckCodeCal(NULL, Secret_Key, T);
  513. }
  514. //计算TIM2 ETR采集频率
  515. void Cal_SyncClockFreq(uint16_t* Result)
  516. {
  517. uint16_t Count = 0;
  518. Count = __HAL_TIM_GET_COUNTER(&htim2);
  519. *Result = Count * 20 / 1000;//50ms内计数值,单位转换为KHz
  520. __HAL_TIM_SET_COUNTER(&htim2, 0);
  521. }
  522. /**************************全局函数定义结束*****************/