#include "PowerManagement.h"
#include "PowerManag_user.h"
#include "Components.h"
#include "Application.h"
#include "RTE.h"

RTC_Information_st_t g_stRTCInformation;

static void Power_KL30_Init(void);
static void Power_Wakeup_Init(void);
static void Power_LVP_Init(void);
static void Power_OVP_Init(void);
static void Power_IG_OFF_Init(void);
static void Power_IG_ON_Init(void);
static void Power_Sleep_Init(void);

static Power_Status_em Power_Stay_ON(void);
static Power_Status_em Power_Stay_OFF(void);
static Power_Status_em Power_Stay_Protect(void);
static Power_Status_em Power_Stay_Sleep(void);

extern uint8_t u8LEDDriverCheckCount;

static const st_PowerGroup g_stPwrTasks = {
    Power_KL30_Init,
    Power_IG_OFF_Init,
    Power_Stay_OFF,
    Power_Sleep_Init,
    Power_Stay_Sleep,
    Power_Wakeup_Init,
    Power_IG_ON_Init,
    Power_Stay_ON,
    Power_LVP_Init,
    Power_OVP_Init,
    Power_Stay_Protect,
};

void PwrMemInit(void)
{
    PowerMemInit(&g_stPwrTasks);
}

static void Power_KL30_Init(void)
{
    Simulated_IIC_2_Init();
    Gpio_Init(Gpio_KL30_Init);
    eeprom_StoreInfo_Init();
    Can_Init();
    DFlash_init();
    CAN_TX_Init();
    Analog_Signal_Conv_Init();
    BU98R10_Init();
    Sys_KL30_Init();
    Checkself_Init();
    LINE_IN_Init();
    BackLight_User_Init();
    LED_Driver_Init_Example();//注意顺序2
    LED_Driver_Service_Immediate();
    Telltales_Init();
    TimerM_PWM_counter_Output_Init(TIMERM_COUNTER0, 1300, 64000000);
    TimerM_PWM_counter_Output_Init(TIMERM_COUNTER1, 1300, 64000000);
    TimerM_PWM_CH_Output_init(TIMERM_COUNTER0, TIMERM_CHB, ActiveLevel_High);
    TimerM_PWM_CH_Output_init(TIMERM_COUNTER0, TIMERM_CHC, ActiveLevel_High);
    TimerM_PWM_CH_Output_init(TIMERM_COUNTER0, TIMERM_CHD, ActiveLevel_High);
    TimerM_PWM_CH_Output_init(TIMERM_COUNTER1, TIMERM_CHB, ActiveLevel_High);
    
    g_stRTCInformation.u8RTCSecond = 0;
	g_stRTCInformation.u8RTCMinute = 0;
	g_stRTCInformation.u8RTCHour = 0;          
	g_stRTCInformation.u8RTCDayOfMonth = 1;
	g_stRTCInformation.u8RTCMonth = 1;   
	g_stRTCInformation.u8RTCYear = 20; 
    RTE_RTC_Init(g_stRTCInformation);
    // RTE_DEEPSLEEP_GPIO_Interrupt_Enable(KL15_AD_IN,Trigger_Rising);
    Data_Soc_KL30_Init();
    Data_Time_KL30_Init();
    Data_Gear_KL30_Init();
    Data_Fuel_KL30_Init();
    Fault_Code_KL30_Init();
    Buzzer_KL30_Init();

}
extern uint32_t PowerIgnOffTimeLine;
static void Power_Wakeup_Init(void)
{
    PowerIgnOffTimeLine = 0;
    g_u8CanrxSleepstate = 0U;
    Simulated_IIC_2_Init();
    Gpio_Init(Gpio_WakeUp_Init);
    Can_Init();
    CAN_TX_Init();
    eeprom_StoreInfo_Init();
    Analog_Signal_Conv_Init();
    BU98R10_Init();
    Sys_WakeUp_Init();
    LINE_IN_Init();
    Line_In_KL15_ON_Init();
    LED_Driver_Init_Example();//注意顺序2
    TimerM_PWM_counter_Output_Init(TIMERM_COUNTER0, 1300, 64000000);
    TimerM_PWM_counter_Output_Init(TIMERM_COUNTER1, 1300, 64000000);
    TimerM_PWM_CH_Output_init(TIMERM_COUNTER0, TIMERM_CHD, ActiveLevel_High);
    TimerM_PWM_CH_Output_init(TIMERM_COUNTER1, TIMERM_CHB, ActiveLevel_High);
    Telltales_Init();
    // RTE_DEEPSLEEP_GPIO_Interrupt_Enable(KL15_AD_IN,Trigger_Rising);
}

static void Power_LVP_Init(void)
{
    
}
static void Power_OVP_Init(void)
{
    
}

static void Power_IG_OFF_Init(void)
{
    Line_In_KL15_OFF_Init();
    Checkself_Init();
}

static void Power_IG_ON_Init(void)
{
    Checkself_Init();
    Line_In_KL15_ON_Init();
    LED_Driver_Service_Immediate();
    Fault_Code_KL15_Init();
    Data_Time_KL15_Init();
    Data_Soc_KL15_Init();
    Data_Fuel_KL15_Init();
}

static void Power_Sleep_Init(void)
{
    TimerM_PWM_counter_Output_Init(TIMERM_COUNTER0, 0, 0);
    TimerM_PWM_counter_Output_Init(TIMERM_COUNTER1, 0, 0);
	//RTC_Stop();
	rte_can_deinit(CAN_CH_0);
	TMM0_Stop();
	TMM1_Stop();
    TMM_All_Stop();
    RTE_ADC_DeInit();
	//CGC->CSC = 1 ;   //HIOSTOP = 1
	BU98R10_Shutdown();
    Gpio_Init(Gpio_Sleep_Init);
    u8LEDDriverCheckCount = 0;
    LED_Driver_Service_Immediate();
    LED_Driver_Init_Sleep();
    LED_Driver_Service();
    Analog_Signal_Conv_Stop();
    // RTE_DEEPSLEEP_Enable();
    RTE_DEEPSLEEP_GPIO_Interrupt_Enable(CAN_MCU_RXD,Trigger_Falling);
    g_u8CanrxSleepstate = 0U;
}

static Power_Status_em Power_Stay_ON(void)
{
    Power_Status_em u8PowerSts;
    u8PowerSts = EM_IGN_ON;

    if (SYS_OPR_STAT_IGN_OFF)
    {
        u8PowerSts = EM_IGN_OFF_Init;
    }

    if (SYS_OPR_STAT_HALT)
    {
        u8PowerSts = EM_IGN_LIMIT;
    }

    return u8PowerSts;
}
static Power_Status_em Power_Stay_OFF(void)
{
    Power_Status_em u8PowerSts;
    u8PowerSts = EM_IGN_OFF;
    #if (PART_NUMBER == RMR42E_60)
    if (Common_Get_IG_Sts( ) == COMMON_POWER_ON)
    {
        u8PowerSts = EM_IGN_ON_Init;
    }
    else
    {
        if ((Common_GetIgnOffTime() < 5000))
        {
            u8PowerSts = EM_IGN_OFF;
        }
        // else
        // {
        //     u8PowerSts = EM_IGN_Sleep_Init;
        // }
    }
    #else
    if (Get_CAN_Power_State() == KEY_ACC)
    {
        u8PowerSts = EM_IGN_ON_Init;
    }
    else
    {
        if (Get_CAN_Power_State() == PKEY_ON)
        {
            if (EnterDeepSleepCount >= 5000)
            {
                u8PowerSts = EM_IGN_Sleep_Init;
            }
            else 
            {
                u8PowerSts = EM_IGN_OFF;
            }
        }
        else
        {
            if ((Common_GetIgnOffTime() < 5000))
            {
                u8PowerSts = EM_IGN_OFF;
            }
            else
            {
                u8PowerSts = EM_IGN_Sleep_Init;
            }
        }
        
    }
    #endif
    return u8PowerSts;
}

static Power_Status_em Power_Stay_Protect(void)
{
    Power_Status_em u8PowerSts;
    static uint8_t  i;
    static uint8_t  Delaycnt;
    static uint8_t LED;
    static uint8_t LEDDriverCheckCount = 0U;
    /* 关闭所有外设，坪保留AD采集，电溝状思切�? */
    {
        BU98R10_Shutdown( );
        TimerM_PWM_set_duty(TIMERM_COUNTER1, TIMERM_CHB, 0);
        TimerM_PWM_set_duty(TIMERM_COUNTER0, TIMERM_CHD, 1000);
        #if (PART_NUMBER == RMR42E_60)
        RTE_GPIO_Set_Level(RTE_GPIO_PORT04_PIN01, RTE_GPIO_LEVEL_LOW); // OUT_EN_1
        #else
        RTE_GPIO_Set_Level(BUZZER_PWM_MCU_OUT, RTE_GPIO_LEVEL_LOW);
        #endif
        CAN_TX_SetEnable(&CAN_CH0_CanMsgTxOp, CAN_N_TX_Disable);
    }
    u8PowerSts = EM_IGN_OFF_Init;
    /* 电溝模弝正常 */
    for ( ;; )
    {
        RTE_WDT_Clear();
        Gen_TimeDelay(2000u, 50u);
        BU98R10_Shutdown( );
        TimerM_PWM_set_duty(TIMERM_COUNTER1, TIMERM_CHB, 0);
        TimerM_PWM_set_duty(TIMERM_COUNTER0, TIMERM_CHD, 1000);
        #if (PART_NUMBER == RMR42E_60)
        RTE_GPIO_Set_Level(RTE_GPIO_PORT04_PIN01, RTE_GPIO_LEVEL_LOW); // OUT_EN_1
        #else
        RTE_GPIO_Set_Level(BUZZER_PWM_MCU_OUT, RTE_GPIO_LEVEL_LOW);
        #endif
        CAN_TX_SetEnable(&CAN_CH0_CanMsgTxOp, CAN_N_TX_Disable);
        for(LED = 0u; LED < LampCh0_MAX; LED++)
        {
            LED_Driver_Channel_Set(0u, LED, 0u);
        }

        for ( i = 0; i < LED_Max; i++ )
        {
            Telltales_DiagCtrl(i, 0x00u);
        }
        LED_Driver_Service( );
        Analog_Signal_Conv_Service();
        Sys_Status_Update_Service();

        Delaycnt++;
        if ( Delaycnt >= 5u )
        {
            Delaycnt = 0u;
            LED_Driver_Scan_Refresh( );
            if ( LEDDriverCheckCount >= 10U )
            {
                LEDDriverCheckCount = 0U;
                LED_Driver_Work_Check( );
            }
            else
            {
                LEDDriverCheckCount++;
            }
        }

        if ( SYS_OPR_STAT_RUN )
        {
            for ( i = 0; i < LED_Max; i++ )
            {
                Telltales_DiagRelease(i);
            }
            BU98R10_Init( );
            CAN_TX_SetEnable(&CAN_CH0_CanMsgTxOp, CAN_N_TX_Enable);
            Power_Stay_Flag = 1;
            return u8PowerSts;
        }
    }
 }

static Power_Status_em Power_Stay_Sleep(void)
{
    Power_Status_em u8PowerSts = EM_IGN_Sleep;
    #if (PART_NUMBER == RMR42E_60)
    while (1)
    {
        RTE_WDT_Clear();
        if (RTE_GPIO_Get_Level(KL15_AD_IN))/* KL15 */
        {
            u8PowerSts = EM_IGN_Wakeup;
            break;
        }
    }
    #else
    while (1)
    {
        RTE_WDT_Clear();
        if ((Get_CAN_Power_State() == PKEY_ON) && (g_u8CanrxSleepstate == 1U))/*当电源状态出于PKEY_ON，且接到CAN信号时，才能触发唤醒*/
        {
            u8PowerSts = EM_IGN_Wakeup;
            g_u8CanrxSleepstate = 0U;
            break;
        }
        else
        {
            RTE_DEEPSLEEP_Enable();
        }
    }
    #endif
    return u8PowerSts;
}


void Sys_Startup_Init(void)
{
    uint8_t u8StartuoMode = 0;
    PwrMemInit( );
    Power_Management_Init(u8StartuoMode);
}

