#include"common.h"
#include "drv_io.h"
#include "drv_usart.h"
#include "drv_usart_2.h"
#include "app.h"
#include "app_rs485_1.h"
#include "app_rs485_2.h"
#include "drv_can.h"
#include "app_adas.h"
#include "measure_vol.h"
#include "app_can.h"
#include "low_power.h"
#include "measure_temprature.h"

DELAY_COMMON enter_sleep_delay;

void LP_Measure_Vol_Initial(void)
{
	/* enable GPIOA clock */
    rcu_periph_clock_enable(RCU_GPIOA);

	rcu_periph_clock_enable(RCU_AF);
    /* enable ADC0 clock */
    rcu_periph_clock_enable(RCU_ADC0);
    /* config ADC clock */
    rcu_adc_clock_config(RCU_CKADC_CKAPB2_DIV8);

	gpio_init(GPIOA, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_5);
	
}

void Set_Enter_Sleep_Delay(void)
{
	enter_sleep_delay.set = 1;
	enter_sleep_delay.count = 0;
}

void Reset_Enter_Sleep_Delay(void)
{
	enter_sleep_delay.set = 0;
	enter_sleep_delay.count = 0;
}

void Enter_Sleep_Delay_Timeout(void)
{
	if(enter_sleep_delay.set)
	{
		if(++enter_sleep_delay.count >= 15000)
		{
			memset(&enter_sleep_delay,0x00,sizeof(enter_sleep_delay));
			g_event |= ENTER_SLEEP_EVENT;
		}
	}
}


void RTC_Open(void)
{

	nvic_irq_enable(RTC_IRQn,1,0);
	
    /* enable PMU and BKPI clocks */
    rcu_periph_clock_enable(RCU_BKPI);
    rcu_periph_clock_enable(RCU_PMU);
    /* allow access to BKP domain */
    pmu_backup_write_enable();

    /* reset backup domain */
    bkp_deinit();

	exti_init(EXTI_17, EXTI_INTERRUPT, EXTI_TRIG_RISING);
    exti_interrupt_flag_clear(EXTI_17);
	exti_interrupt_enable(EXTI_17);
	
    /* enable LXTAL */
    rcu_osci_on(RCU_IRC40K);
    /* wait till LXTAL is ready */
    rcu_osci_stab_wait(RCU_IRC40K);
    
    /* select RCU_LXTAL as RTC clock source */
    rcu_rtc_clock_config(RCU_RTCSRC_IRC40K);

    /* enable RTC Clock */
    rcu_periph_clock_enable(RCU_RTC);

    /* wait for RTC registers synchronization */
    rtc_register_sync_wait();

    /* wait until last write operation on RTC registers has finished */
    rtc_lwoff_wait();

    /* enable the RTC second interrupt*/
    rtc_interrupt_enable(RTC_INT_ALARM);

    /* wait until last write operation on RTC registers has finished */
    rtc_lwoff_wait();

    /* set RTC prescaler: set RTC period to 1s */
    rtc_prescaler_set(40);

    /* wait until last write operation on RTC registers has finished */
    rtc_lwoff_wait();

	rtc_lwoff_wait();
	rtc_alarm_config(1000);
	rtc_lwoff_wait();
	
}
void Enter_Sleep(void)
{
	uint32_t temp;
	
#if DEBUG_MODE == 0
	Feed_Watch_Dog();
#endif

	systick_close();
	
	usart_deinit(USART0);

	usart_deinit(USART1);

	can_deinit(CAN0);



	RTC_Open();

	
	

	//while(1);
	
SLEEP:
	rcu_periph_clock_enable(RCU_PMU);
	pmu_to_deepsleepmode(PMU_LDO_LOWPOWER, WFI_CMD);

#if 1
	SystemInit();

	systick_config();

	LP_Measure_Vol_Initial();

	temp = Measure_Vol();

	systick_close();
	
	if(temp > 20000 && temp < 120000)
	{
		
		__set_FAULTMASK(1);
		NVIC_SystemReset();
	}
	else
	{
#if DEBUG_MODE == 0
		Feed_Watch_Dog();
#endif
		goto SLEEP;
	}
#else
	SystemInit();

	__set_FAULTMASK(1);
	NVIC_SystemReset();
	
#endif


}

void Wake_Up(void)
{
	
}

void Low_Power_Initial(void)
{
	Set_Enter_Sleep_Delay();
}

void RTC_IRQHandler(void)
{
    if (rtc_flag_get(RTC_FLAG_SECOND) != RESET)
	{
        /* clear the RTC second interrupt flag*/
        rtc_flag_clear(RTC_FLAG_SECOND);
        
    }

	if (rtc_flag_get(RTC_FLAG_ALARM) != RESET)
	{
        /* clear the RTC second interrupt flag*/
        rtc_flag_clear(RTC_FLAG_ALARM);
		exti_interrupt_flag_clear(EXTI_17);
        //rtc_alarm_config(5000);
        rtc_lwoff_wait();
		rtc_counter_set(0x00);
		rtc_lwoff_wait();
    }
}

