Motion_EC_Stm32_archived/App/Src/th_power.c

104 lines
3.7 KiB
C

/*
* @Description:
* @Date: 2021-03-10 10:44:19
* @LastEditors: CK.Zh
* @LastEditTime: 2021-03-10 10:45:36
* @FilePath: \NaviKit_EC_stm32\App\Src\th_power.c
*/
#define LOG_TAG "TH-Power"
#include <th_power.h>
#include "main.h"
#include "adc.h"
#include "navikit.h"
const osThreadAttr_t PowerMonitTask_attributes = {
.name = "PowerMonitTask",
.priority = (osPriority_t) osPriorityBelowNormal,
.stack_size = 128 * 8
};
/* USER CODE BEGIN Header_StartPowerMonitTask */
/**
* @brief Function: Monit som power status and PMB status
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartPowerMonitTask */
void StartPowerMonitTask(void *argument)
{
/* USER CODE BEGIN StartPowerMonitTask */
log_d("Start Power Monitor Task");
HAL_ADCEx_Calibration_Start(&hadc1);
HAL_ADC_Start_DMA(&hadc1, (uint32_t*)&(NaviKit.pmb.rails.adc), ADC_CH_COUNT);
osDelay(100);
/* Infinite loop */
for(;;)
{
if(NaviKit.pmb.rails.main_pwr > 30){
TaskBeep(200,1);
log_e("Main power is exception:%dmV",(uint32_t)(NaviKit.pmb.rails.main_pwr*1000));
}
if(NaviKit.pmb.rails.bkp_bat > 15){
log_e("Backup battery is exception:%dmV",(uint32_t)(NaviKit.pmb.rails.bkp_bat*1000));
TaskBeep(200,1);
}
//stat1 stat2 lead-acid
//1 1 Not Charging
//1 0 Float Charge
//0 1 Absorption Charge
//0 0 Bulk Charge
NaviKit.pmb.sta.chrg_stat1 = (bool)HAL_GPIO_ReadPin(PMB_CHRG_STAT1_GPIO_Port, PMB_CHRG_STAT1_Pin);
NaviKit.pmb.sta.chrg_stat2 = (bool)HAL_GPIO_ReadPin(PMB_CHRG_STAT2_GPIO_Port, PMB_CHRG_STAT2_Pin);
if(!NaviKit.pmb.sta.chrg_stat1 && !NaviKit.pmb.sta.chrg_stat2)
log_v("Backup battery status:%dmV Bulk Charge(Vth=12.6V)",(uint32_t)(NaviKit.pmb.rails.bkp_bat*1000));
if(!NaviKit.pmb.sta.chrg_stat1 && NaviKit.pmb.sta.chrg_stat2)
log_v("Backup battery status:%dmV Absorption Charge(Vth=14.4V)",(uint32_t)(NaviKit.pmb.rails.bkp_bat*1000));
if(NaviKit.pmb.sta.chrg_stat1 && !NaviKit.pmb.sta.chrg_stat2)
log_v("Backup battery status:%dmV Float Charge(Vth=13.3V)",(uint32_t)(NaviKit.pmb.rails.bkp_bat*1000));
if(NaviKit.pmb.sta.chrg_stat1 && NaviKit.pmb.sta.chrg_stat2)
log_v("Backup battery status:%dmV Not Charge",(uint32_t)(NaviKit.pmb.rails.bkp_bat*1000));
if(NaviKit.sys.sta == run || NaviKit.sys.sta == dfu ){
if(NaviKit.pmb.rails.main_pwr < 19){
TaskBeep(200,1);
log_w("Main power has been lost, please shutdown computer as soon as possible:%dmV",(uint32_t)(NaviKit.pmb.rails.main_pwr*1000));
}else
log_v("Main power rail regular:%dmV",(uint32_t)(NaviKit.pmb.rails.main_pwr*1000));
if(NaviKit.pmb.rails.out_24v > 26.4 || NaviKit.pmb.rails.out_24v < 21.6)
log_e("24V(±10%%) power rail exception:%dmV",(uint32_t)(NaviKit.pmb.rails.out_24v*1000));
else
log_v("24V(±10%%) power rail regular:%dmV",(uint32_t)(NaviKit.pmb.rails.out_24v*1000));
if(NaviKit.pmb.rails.out_5v > 5.5 || NaviKit.pmb.rails.out_5v < 4.5)
log_e("5V(±10%%) power rail exception:%dmV",(uint32_t)(NaviKit.pmb.rails.out_5v*1000));
else
log_v("5V(±10%%) power rail regular:%dmV",(uint32_t)(NaviKit.pmb.rails.out_5v*1000));
if(NaviKit.pmb.rails.out_12v > 13.2 || NaviKit.pmb.rails.out_12v < 10.8)
log_e("12V(±10%%) power rail exception:%dmV",(uint32_t)(NaviKit.pmb.rails.out_12v*1000));
else
log_v("12V(±10%%) power rail regular:%dmV",(uint32_t)(NaviKit.pmb.rails.out_12v*1000));
if(NaviKit.pmb.rails.bkp_bat < 9.5)
log_w("Backup battery exception:%dmV",(uint32_t)(NaviKit.pmb.rails.bkp_bat*1000));
else
log_v("Backup battery is regular:%dmV",(uint32_t)(NaviKit.pmb.rails.bkp_bat*1000));
osDelay(2000);
}else {//not run state
osDelay(5000);
}
// log_d("state :%d",NaviKit.sys.sta);
// log_d("next_state :%d",NaviKit.sys.next_sta);
}
/* USER CODE END StartPowerMonitTask */
}