tasks.c 15 KB

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