
#ifdef __cplusplus
extern "C"
{
#endif

	
#include "UWater_power_app.h"
#include "UWater_adc_driver.h"
#include "UWater_rtcc_driver.h"
#include "soe.h"
/*------------------------------ Variable Define -------------------------------------*/

static u8 s_g_voltage_stat = NORMAL_POWER; // 初始化，电源电压正常
static u8 s_g_power_machine_state_num = POWER_SYS_S0; // 电源电压检测处理机状态号
static u8 s_power_adc_done = 0; // AD装换结果完成标志
static u32 s_power_adc_result = 0; // AD转换后结果存放
static u8 s_power_reset=0;
	

u8 s_power_sleep_sum = 0;
u8 s_power_sleep_flag = FALSE; // 电源管理之休眠
u32 temp_value;

/*------------------------------ Function Define -------------------------------------*/


/***********************电池电压检测使能***************************
Function:    void PowerADCEnable(void)  void PowerADCDisable(void)
Description:
Input:
Return:
Others:
 ****************************************************************/
 void PowerADCEnable(void)
 {
		stc_gpio_cfg_t stcGpioCfg;
		DDL_ZERO_STRUCT(stcGpioCfg);
		stcGpioCfg.enDir = GpioDirOut;    ///< 端口方向配置
		stcGpioCfg.enDrv = GpioDrvL;      ///< 端口驱动能力配置->低驱动能力
		stcGpioCfg.enPu = GpioPuDisable;  ///< 端口上下拉配置->无
		stcGpioCfg.enPd = GpioPdDisable;
		stcGpioCfg.enOD = GpioOdDisable;  ///< 端口开漏输出配置:关闭开漏,使用推挽输出
		stcGpioCfg.enCtrlMode = GpioAHB;  ///< 端口输入/输出值寄存器总线控制模式配置->AHB
		Gpio_Init(BAT_CHECK_ENPORT, BAT_CHECK_ENPIN, &stcGpioCfg);
		Gpio_WriteOutputIO(BAT_CHECK_ENPORT, BAT_CHECK_ENPIN, TRUE);
 }
 
void PowerADCDisable(void)
{	
		stc_gpio_cfg_t stcGpioCfg;
		DDL_ZERO_STRUCT(stcGpioCfg);
		stcGpioCfg.enDir = GpioDirOut;    ///< 端口方向配置
		stcGpioCfg.enDrv = GpioDrvL;      ///< 端口驱动能力配置->低驱动能力
		stcGpioCfg.enPu = GpioPuDisable;  ///< 端口上下拉配置->无
		stcGpioCfg.enPd = GpioPdDisable;
		stcGpioCfg.enOD = GpioOdDisable;  ///< 端口开漏输出配置:关闭开漏,使用推挽输出
		stcGpioCfg.enCtrlMode = GpioAHB;  ///< 端口输入/输出值寄存器总线控制模式配置->AHB
		Gpio_Init(BAT_CHECK_ENPORT, BAT_CHECK_ENPIN, &stcGpioCfg);
		Gpio_WriteOutputIO(BAT_CHECK_ENPORT, BAT_CHECK_ENPIN, FALSE);
}

/***********************上电初始化***************************
Function:    void PowerManageInitSys(void)
Description:
Input:
Return:
Others:
 ****************************************************************/
void PowerManageInitSys(void)
{
    s_g_voltage_stat = NORMAL_POWER; // 初始化，电源电压正常
    s_g_power_machine_state_num = POWER_SYS_S0; // 电源电压检测处理机状态号
    RtccSetTimer(RTCC_TIMER_BAT_NUM, 30);        //上电30s后检测电池电压
    PowerADCDisable();
}

/***********************查询欠压信息***************************
Function:    u8 PowerCheckStatusSys(void)
Description:  查询当前电池是否欠压
Input:
Return:     LOW_POWER ：电池电压低   NORMAL_POWER：电压正常
Others:
 ****************************************************************/
u8 PowerCheckStatusSys (void)
{
    return (s_g_voltage_stat);
}
/***********************查询电压值信息***************************
Function:    u16 PowerCheckValueSys(void)
Description:  查询当前电池值,采样处的值
Input:
Return:    放大100倍的 u16,16进制
Others:
 ****************************************************************/
u16 PowerCheckValueSys(void)
{
	return ((u16)temp_value);
}
/***********************任务建立接口***************************
Function:    void PowerMachineSys (void)
Description:  应用层建立任务接口
Input:
Return:
Others:
 *****************************************************************/
void PowerMachineSys(void)
{
	static u8 low_power_sum = 0;
		
    switch (s_g_power_machine_state_num)
    {
    case POWER_SYS_S0:
		if (0 == RtccCheckTimer(RTCC_TIMER_BAT_NUM))
		{
			if(TASK_IDLE == AdcIfIdle())
			{
				PowerADCEnable();
				AdcStartConvert(POWER_CHECK_CHANNEL, POWER_CHECK_NUM, POWER_CHECK_INTERVAL, &s_power_adc_result, &s_power_adc_done);
				s_g_power_machine_state_num = POWER_SYS_S1;
			}
		}
        break;
    case POWER_SYS_S1:
        if (TRUE == s_power_adc_done)
        {
            PowerADCDisable();	
            if (ADC_OK == AdcGetStatus())
            {
                if(s_power_adc_result <= LOW_POWER_ADC_VALUE)    //欠压
                {
                    low_power_sum ++;
					RtccSetTimer(RTCC_TIMER_BAT_NUM, VOLTAGE_TEST_INTERVAL);
                    if(low_power_sum >=6)
                    {
						SaveNewSoe(SOE_BAT_LOW_LEVEL,SOE_REMARK_NOR);	//存SOE
                        s_g_voltage_stat = LOW_POWER;
                        low_power_sum = 0;
                        RtccSetTimer(RTCC_TIMER_BAT_NUM, VOLTAGE_TEST_INTERVAL); //定时12h
                    }
                }
                else
                {
                    s_g_voltage_stat = NORMAL_POWER;
                    low_power_sum = 0;
                    RtccSetTimer(RTCC_TIMER_BAT_NUM, VOLTAGE_TEST_INTERVAL); //定时12h
                }
                temp_value = s_power_adc_result * 500;  //((v/2)/2.5)*4096 = s_power_adc_result
                temp_value = temp_value/4096;
				if(s_power_reset==0)//法拉电容充电
				{
					s_power_reset =1;
					if(temp_value<360)
					{
						RtccSetTimer(RTCC_TIMER_BAT_NUM, 10);//10秒后重新测试
					}
				}
				s_g_power_machine_state_num = POWER_SYS_S0;
           }
       }
        break;
    default:
        break;
    }
}

/***********************休眠处理函数***************************
Function:    u8 PowerIfSleepSys(void)
Description:
Input:
Return:
Others:
 *****************************************************************/
u8 PowerIfSleepSys(void)
{
	if (s_g_power_machine_state_num == POWER_SYS_S0)
	{
		return TRUE;
	}
	return FALSE;
}

void PowerPreSleepSys(void)
{
//    PowerADCDisable(); //管脚控制
}




//长时间不休眠处理函数2017-7-21/////////////////////////////////////

/***********************数据清零***************************
Function:    void PowerUnSleepClearSumSys (void)
Description:
Input:
Return:
Others:
 *****************************************************************/
void PowerUnSleepClearSumSys(void)
{
    s_power_sleep_sum = 0;
}


/**************************************************
Function:    u8 PowerUnSleepSaveDataMsgSys (void)
Description:
Input:
Return:
Others:
 *****************************************************************/
u8 PowerUnSleepSaveDataMsgSys(void)
{
    return s_power_sleep_flag;
}

/**************************************************
Function:    void PowerUnSleepSaveDataClearMsgSys (void)
Description:  
Input:
Return:
Others:
 *****************************************************************/
void PowerUnSleepSaveDataClearMsgSys(void)
{
    s_power_sleep_flag = FALSE;
}


void PowerUnSleepSumAddSys(void)
{
	s_power_sleep_sum++;
	if (s_power_sleep_sum >= 10)//10min
	{
		s_power_sleep_sum = 0;
		s_power_sleep_flag = TRUE;
	}
}




#ifdef	__cplusplus
}
#endif


