#include "common_include.h"

volatile uint32_t Timer1msCnts = 0;
volatile uint32_t CoverPageTimerCnts = 2000;
volatile uint32_t ClusterValidTimerCnts = 0;
uint32_t Timer10msCnts;
volatile uint32_t Timer100usCnts;
 FREQUENCE Frequeice;
FREQUENCE BackLight;
uint8_t ADTime;
uint8_t Time_1s_Flag,Time_2s_Flag,Time_Charge_1s,Time_Charge_500ms;
uint8_t CANSendEnable;
volatile uint8_t Time_500ms_Flag;
uint16_t IntervalTimerDelta = 0;
uint32_t IntervalTimerCnts = 0;
extern uint16_t Charge_Value;
extern uint8_t CheckClusterValid;

extern uint8_t  main_board_flag;
volatile uint16_t  BackLight_Value;
uint32_t MODEtimer = 0;
uint16_t TftTimer;
void TimeProcessing(void)
{
    if (Timer1msCnts > IntervalTimerCnts) 
    {
        IntervalTimerDelta = (Timer1msCnts - IntervalTimerCnts);
        IntervalTimerCnts = Timer1msCnts;
        //StaticCurrent Time Out State
        if(LampState_L.Lamps.RELAY_IGN3 == 1){
            if(StaticCurrent.Time < 100000){
                StaticCurrent.Time += IntervalTimerDelta;
            }
        }else{
            StaticCurrent.Time = 0;
        }
        //�ȶ�����5��xxhh close
//        if(StaticCurrent.enable == 1){
//            StaticCurrent.count += IntervalTimerDelta;
//        }else{
//            StaticCurrent.count = 0;
//        }
         
        
        
        if(Iol_TempCurrent.enable == 1){
            Iol_TempCurrent.Time += IntervalTimerDelta;
        }else{
            Iol_TempCurrent.Time = 0;
        }
        
        if(LedCurrent.enable == 1){
            LedCurrent.Time += IntervalTimerDelta;
        }else{
            LedCurrent.Time = 0;
        }
        
        if(ButtomCurrent.enable == 1){
            ButtomCurrent.Time += IntervalTimerDelta;
        }else{
            ButtomCurrent.Time = 0;
        }
        
        if(TripKeyCurrent.enable == 1){
            TripKeyCurrent.Time += IntervalTimerDelta;
        }else{
            TripKeyCurrent.Time = 0;
        }
        
        
         if(TFTCurrent.enable == 1){
            TFTCurrent.Time += IntervalTimerDelta;
        }else{
            TFTCurrent.Time = 0;
        }
        if(TFTCurrent1.enable == 1){
            TFTCurrent1.Time += IntervalTimerDelta;
        }else{
            TFTCurrent1.Time = 0;
        }
        if(TFTCurrent2.enable == 1){
            TFTCurrent2.Time += IntervalTimerDelta;
        }else{
            TFTCurrent2.Time = 0;
        }
        if(TFTCurrent3.enable == 1){
            TFTCurrent3.Time += IntervalTimerDelta;
        }else{
            TFTCurrent3.Time = 0;
        }
        if(TFTCurrent4.enable == 1){
            TFTCurrent4.Time += IntervalTimerDelta;
        }else{
            TFTCurrent4.Time = 0;
        }
        if(TFTCurrent5.enable == 1){
            TFTCurrent5.Time += IntervalTimerDelta;
        }else{
            TFTCurrent5.Time = 0;
        }
        if(TFTCurrent6.enable == 1){
            TFTCurrent6.Time += IntervalTimerDelta;
        }else{
            TFTCurrent6.Time = 0;
        }
        if(TFTCurrent7.enable == 1){
            TFTCurrent7.Time += IntervalTimerDelta;
        }else{
            TFTCurrent7.Time = 0;
        }
        if(ElseCurrent.enable != 0){
            ElseCurrent.Time += IntervalTimerDelta;
        }else{
            ElseCurrent.Time = 0;
        }
        
        if(EndCurrent.enable == 1){
            EndCurrent.Time += IntervalTimerDelta;
        }else{
            EndCurrent.Time = 0;
        }
        
//        if(ElseCurrent.count == 10){
//            MODEtimer += IntervalTimerDelta;
//        }else{
//            MODEtimer = 0;
//        } 
        
        
        
        if(BuzzerAlarm.enable == 1){
            BuzzerAlarm.Time += IntervalTimerDelta;
        }else{
            BuzzerAlarm.Time = 0;
        }
        
        if(CheckCurrent.enable == 1){
            CheckCurrent.Time += IntervalTimerDelta;
            if(CheckCurrent.Time > 10000)
              CheckCurrent.Time = 10000;
        }else{
            CheckCurrent.Time = 0;
        }
        
        
        //StaticCurrent Time Out Stop
        if (main_board_flag)
            Button_Statu_Manage();
        else
            Button_Statu_Manage_Slave();
        
    }
}
uint32_t timeCount = 0;
uint32_t AutoStepAddTime=0;
uint32_t ADCTime1=0;
extern uint32_t GetInfoDelayTime;
static void TIMER_1MS_ISR(void)
{
    if(GetInfoDelayTime){
        GetInfoDelayTime--;
    }
#if debug_mode 
    JLINKDCC_Process();
#endif
     if(ADCTime1>0)
    {
      ADCTime1--;  
    }

    if(AutoStepAddTime<5000)
    {
      AutoStepAddTime++;  
    }
    Timer1msCnts += 1;
    if(Timer1msCnts % 1000 == 0)
    {
        //LampState_L.Lamps.LED3 = !LampState_L.Lamps.LED3;
    }
    /*  xxhh Unused
    ADTime++;
    if(ADTime >= 50){
        ADTime = 0;
        ADFlag = 1;
    }
    */
    CanRecvLibTimerCall(Timer1msCnts); 
    //button State
    if (CoverPageTimerCnts) {
        CoverPageTimerCnts--;
    }
    if (ClusterValidTimerCnts) {
        ClusterValidTimerCnts--;
        if (LampState_L.Lamps.RELAY_IGN1 != 1) {
            ClusterValidTimerCnts = 1000;
        }
    } else {
        if (LampState_L.Lamps.RELAY_IGN1 == 1) {
            CheckClusterValid = 0;
            g_Stage = 0;
//            WHL_SPD11.msg.bits.WHL_SPD_FL = 0x500;//����40
//            WHL_SPD11.msg.bits.WHL_SPD_RL = 0x500;//����40
//            CGW_PC4.msg.bits.N=0x1F40;//2000  ת��
//            //EMS_01_10ms.ENG_EngSpdVal = 0;
        } else {
            ClusterValidTimerCnts = 1000;
        }
    }
    if (TftTimer > 0) {
        TftTimer --;
    }
    
    if(Timer1msCnts % 1000 == 0){
      Time_1s_Flag = 1;
      Time_Charge_1s ++;
      Time_Charge_500ms++;
      if(Time_Charge_1s == 3)
        Time_Charge_1s = 0;
            if(Time_Charge_500ms == 4)
        Time_Charge_500ms = 0;
        timeCount++;
    }else{
      Time_1s_Flag = 0;
    }
    if(Timer1msCnts % 500 == 0)
    {
      Time_500ms_Flag = 1;
      Charge_Value = StaticCurrent.Value - 6;
    }
    else
    {
      Time_500ms_Flag =0 ;
    }
    /*if(Timer1msCnts % 3000 == 0 && LedCurrent.Flag == 1){
      //Time_2s_Flag = 1;
      g_Stage = STAGE_TelltaleAndSpeed;
      LedCurrent.count ++;
      
    }else{
        //Time_2s_Flag = 0;
    }*/
        
   // if (CheckClusterValid == 1) 
    {
        Get_Button_Value();
    }
    //button Stop

    //UDS Time Out State
    if(RxFinish == FINISH){
        g_UDS.Flag = 0;
    }else if(UDSSend == FINISH){
        g_UDS.Flag = 1;
    }
    if(g_UDS.Flag == 1){
        if(g_UDS.Time < 1000){
            g_UDS.Time ++;
        }else{
            g_UDS.Statu = 1;
        }
    }else{
        g_UDS.Time = 0;
        g_UDS.Statu = 0;
    }
    //UDS Time Out Stop
    if(Timer1msCnts % 500 == 0)
    {
        //  Time_500ms_Flag = 1;
        //    switch(SpeedCurrent.count)//Manual compensation
        //     {
        //      case 13: BackLight_Value = BackLight.Value - 15; break;
        //      case 14: BackLight_Value = BackLight.Value - 9; break;
        //      case 15: BackLight_Value = BackLight.Value - 6; break;
        //      case 16: BackLight_Value = BackLight.Value - 8; break;
        //      case 17: BackLight_Value = BackLight.Value - 12; break;
        //      case 18: BackLight_Value = BackLight.Value - 16; break;
        //      case 19: BackLight_Value = BackLight.Value - 13; break;
        //      case 20: BackLight_Value = BackLight.Value - 12; break;
        //      case 21: BackLight_Value = BackLight.Value - 12; break;
        //      case 22: BackLight_Value = BackLight.Value - 14; break;
        //      case 23: BackLight_Value = BackLight.Value - 15; break;
        //      case 24: BackLight_Value = BackLight.Value - 12; break;
        //      case 25: BackLight_Value = BackLight.Value - 1; break;
        //      case 26: BackLight_Value = BackLight.Value - 4; break;
        //      case 27: BackLight_Value = BackLight.Value - 7; break;
        //      case 28: BackLight_Value = BackLight.Value + 6; break;
        //      case 29: BackLight_Value = BackLight.Value + 3; break;
        //      case 30: BackLight_Value = BackLight.Value + 1; break;
        //      case 31: BackLight_Value = BackLight.Value - 3; break;
        //      case 32: BackLight_Value = BackLight.Value - 5; break;
        //      
        //      default : BackLight_Value = BackLight.Value;break;
        //      }
           Time_500ms_Flag = 1;
      switch(SpeedCurrent.count)//Manual compensation
      {
      case 13: BackLight_Value = BackLight.Value ; break;
      case 14: BackLight_Value = BackLight.Value ; break;
      case 15: BackLight_Value = BackLight.Value ; break;
      case 16: BackLight_Value = BackLight.Value ; break;
      case 17: BackLight_Value = BackLight.Value ; break;
      case 18: BackLight_Value = BackLight.Value ; break;
      case 19: BackLight_Value = BackLight.Value ; break;
      case 20: BackLight_Value = BackLight.Value ; break;
      case 21: BackLight_Value = BackLight.Value ; break;
      case 22: BackLight_Value = BackLight.Value ; break;
      case 23: BackLight_Value = BackLight.Value ; break;
      case 24: BackLight_Value = BackLight.Value ; break;
      case 25: BackLight_Value = BackLight.Value ; break;
      case 26: BackLight_Value = BackLight.Value ; break;
      case 27: BackLight_Value = BackLight.Value ; break;
      case 28: BackLight_Value = BackLight.Value ; break;
      case 29: BackLight_Value = BackLight.Value ; break;
      case 30: BackLight_Value = BackLight.Value ; break;
      case 31: BackLight_Value = BackLight.Value ; break;
      case 32: BackLight_Value = BackLight.Value ; break;
      
      default : BackLight_Value = BackLight.Value;break;
      }
      
    }
    else
    {
      Time_500ms_Flag =0 ;
    }
    
}


extern uint32_t Time_100us;


static void TIMER4_50US_ISR(void)
{    

     if(Time_100us < 0xffffffff)
        Time_100us++;

    //TFT �����õ�
    if(UP_VSYNC)
    {
        UpScreenTime ++;
        if(LCDPanelUpdtCtrl.Request == 0)
        {
            if(UpScreenStep == 1)
            {
                if(UpScreenTime >= 66)
                {
                    UpScreenStep = 2;
                }
            }
            else if(UpScreenStep == 2)
            {
                LCD_Panel_Update_Req(64,96);
                UpScreenStep = 3;
            }
            else if(UpScreenStep == 3)
            {
                if(UpScreenTime >= 132)
                {
                    UpScreenStep = 4;
                }
            }
            else if(UpScreenStep == 4)
            {
                LCD_Panel_Update_Req(32,64);
                UpScreenStep = 5;
            }
            else if(UpScreenStep == 5)
            {
                if(UpScreenTime >= 198)
                {
                    UpScreenStep = 6;
                }
            }
            else if(UpScreenStep == 6)
            {
                LCD_Panel_Update_Req(0,32);
                UpScreenStep = 7;
            }
            else if(UpScreenStep == 7)
            {
                if(UpScreenTime >= 264)
                {
                    UpScreenStep = 8;
                }
            }
            else if(UpScreenStep == 8)
            {
                LCD_Panel_Update_Req(128,160);
                UpScreenStep = 9;
            }
        }
    }
        if (BackLight.Flag == 1)
    {
        if (BackLight.Time <= 10000) 
        {
            BackLight.Time++;
        }else
        {
            BackLight.Time = 0;
            BackLight.Value = 0;
            BackLight.Flag = 0;
        }
    } 
    
    if (BackLight.Flag == 0)
    {
        if (BackLight.TimeL <= 10000) 
        {
            BackLight.TimeL++;
        }else
        {
            BackLight.TimeL = 0;
            BackLight.Value = 0;
            BackLight.Flag = 0;
        }
    }
    
}
static void TIMER5_100US_ISR(void)
{
    /*
    if(Timer100usCnts <0xFFFFFFFF)
    {
        Timer100usCnts++;
    }
    */
 
    //ͨ��һ����
    if (Frequeice.Flag == 1){
        if (Frequeice.Time <= 10000) {
            Frequeice.Time++;
        }else{
            Frequeice.Time = 0;
            Frequeice.Value = 0;
            Frequeice.Flag = 0;
        }
    }      
}

extern uint8_t EOL_Flag;

extern uint8_t start_uart_timer;
extern uint8_t uart_timer_300;

extern uint8_t g_butto_msg[8];
extern uint8_t uart_send_end_flag;
extern uint8_t uart_send_flag;
extern uint8_t uart_send_count;
extern uint8_t  stop_uart_timer;

uint8_t uart_timer_count;
extern uint8_t Button_Press;
uint16_t TestCheck5V = 0;
extern volatile uint8_t  Auto_HW_flag;
static void TIMER13_10MS_ISR(void)
{
    uint8_t i=0;
    Timer10msCnts += 10; 
    
    if(CANSendEnable == OFF){
        //SendCan0Message_7DF();
    } 
    TestCheck5V = Calculate_Thermistor(AdcValList[13]);
    if((StaticCurrent.count < 50) && (LampState_L.Lamps.RELAY_IGN3 == 1)){
        StaticCurrent.buf[StaticCurrent.count] = Calculate_Thermistor(AdcValList[14]);//18
        StaticCurrent.count ++;
        if(StaticCurrent.count == 50) {
            StaticCurrent.count = 0;
            for(i=0;i<50;i++){
               StaticCurrent.sum += StaticCurrent.buf[i];
            }
            StaticCurrent.Value = StaticCurrent.sum/50;
            StaticCurrent.sum = 0;
        }
    } else {
       StaticCurrent.count = 0; 
       StaticCurrent.sum = 0;
       StaticCurrent.Value = 0;
    }
	
    if(FiveVoltage.count < 50) {
        FiveVoltage.buf[FiveVoltage.count] =(Calculate_Thermistor(AdcValList[13]));//18
        FiveVoltage.count ++;
        if(FiveVoltage.count == 50) {
            FiveVoltage.count = 0;
            for(i=0;i<50;i++){
               FiveVoltage.sum += FiveVoltage.buf[i];
            }
            FiveVoltage.Value = FiveVoltage.sum/50;
            FiveVoltage.sum = 0;
        }
    } else {
       FiveVoltage.count = 0; 
       FiveVoltage.sum = 0;
       FiveVoltage.Value = 0;
    }
    
	if(MODEtimer < 0xffff)
	{
		MODEtimer++;
	}
	
    if (main_board_flag)
    {
        if (1==start_uart_timer)
        {
            uart_timer_count++;
            if (uart_timer_count>30)
            {
                uart_send_flag =1;
                uart_timer_count=0;
                
                uart_send_count++;
                
                if (uart_send_count >10)
                {
                    uart_send_count=0;
                    start_uart_timer=0;
                    uart_send_flag=0;
                    Button_Press=0;
                }
            }

        }
     }
}



void TIMER1_1ms_Init(void)
{
    // RLT - configuration
    stc_rlt_config_t stcRltConfig1 = { 
        .enPrescaler            = RltDiv16,                   // Clock further divided by 16
        .enOperationMode        = RltSoftwareTriggerOnly,     // Trigger with Software
        .b32bitTimerModeEnable  = FALSE,                      // 16bit mode
        .bOutputPolarityInverse = FALSE,                      // normal polarity
        .bOneshotEnable         = FALSE,                      // run cyclic
        .bGateInputEnable       = FALSE,                      // trigger input
        .pfnTriggerCallback     = NULL,         // use callback for interrupt
        .pfnUnderflowCallback   = TIMER_1MS_ISR};
    
    // Init Timer with above configuration
    Bt_Rlt_Init ((stc_btn_rt_t*)&BT01_RT, &stcRltConfig1);
    
    // Start RLT by software trigger, and loads RLT_RELOAD_VALUE
    Bt_Rlt_Start ((stc_btn_rt_t*)&BT01_RT, NULL, (uint32_t) (RLT_RELOAD_1MS_VALUE - 1), TRUE);
}


void TIMER_50us_Init(void)  
{
    stc_rlt_config_t stcRltConfig4 = {
        .enPrescaler            = RltDiv16,                   // Clock further divided by 16
        .enOperationMode        = RltSoftwareTriggerOnly,     // Trigger with Software
        .b32bitTimerModeEnable  = FALSE,                      // 16bit mode
        .bOutputPolarityInverse = FALSE,                      // normal polarity
        .bOneshotEnable         = FALSE,                      // run cyclic
        .bGateInputEnable       = FALSE,                      // trigger input
        .pfnTriggerCallback     = NULL,    // use callback for interrupt
    .pfnUnderflowCallback   = TIMER4_50US_ISR};
    
    // Init Timer with above configuration
    Bt_Rlt_Init ((stc_btn_rt_t*)&BT04_RT, &stcRltConfig4);
    
    // Start RLT by software trigger, and loads RLT_RELOAD_VALUE
    Bt_Rlt_Start ((stc_btn_rt_t*)&BT04_RT, NULL, (uint32_t) RLT_RELOAD_50US_VALUE, TRUE);
}

void TIMER_100us_Init(void)  
{
    stc_rlt_config_t stcRltConfig5 = {
        .enPrescaler            = RltDiv16,                   // Clock further divided by 16
        .enOperationMode        = RltSoftwareTriggerOnly,     // Trigger with Software
        .b32bitTimerModeEnable  = FALSE,                      // 16bit mode
        .bOutputPolarityInverse = FALSE,                      // normal polarity
        .bOneshotEnable         = FALSE,                      // run cyclic
        .bGateInputEnable       = FALSE,                      // trigger input
        .pfnTriggerCallback     = NULL,    // use callback for interrupt
    .pfnUnderflowCallback   = TIMER5_100US_ISR};
    
    // Init Timer with above configuration
    Bt_Rlt_Init ((stc_btn_rt_t*)&BT05_RT, &stcRltConfig5);
    
    // Start RLT by software trigger, and loads RLT_RELOAD_VALUE
    Bt_Rlt_Start ((stc_btn_rt_t*)&BT05_RT, NULL, (uint32_t) RLT_RELOAD_100US_VALUE, TRUE);
}

void TIMER_10ms_Init(void)
{
    stc_rlt_config_t stcRltConfig13 = { .enPrescaler            = RltDiv16,                   // Clock further divided by 16
        .enOperationMode        = RltSoftwareTriggerOnly,     // Trigger with Software
        .b32bitTimerModeEnable  = FALSE,                      // 16bit mode
        .bOutputPolarityInverse = FALSE,                      // normal polarity
        .bOneshotEnable         = FALSE,                      // run cyclic
        .bGateInputEnable       = FALSE,                      // trigger input
        .pfnTriggerCallback     = NULL,         // use callback for interrupt
    .pfnUnderflowCallback   = TIMER13_10MS_ISR};
    
    // Init Timer with above configuration
    Bt_Rlt_Init ((stc_btn_rt_t*)&BT13_RT, &stcRltConfig13);
    
    // Start RLT by software trigger, and loads RLT_RELOAD_VALUE
    Bt_Rlt_Start ((stc_btn_rt_t*)&BT13_RT, NULL, (uint32_t) RLT_RELOAD_10MS_VALUE, TRUE);
}
