#include "RTI.h"

/*
uint16_t  RTI25msCounter;
uint16_t  RTI15s;
extern  uint8_t CLEAR_ODO;
extern  uint8_t time_start;

uint8_t  RTI100msCounter;
uint8_t  RTI1sFlag;
uint8_t  flag;
extern uint8_t flag_RADO;
extern uint8_t INIT_cnt;
extern uint8_t InsFuel_clear;
extern  volatile    FuelEco   g_fuelEco;
extern uint8_t  Time_Street_EN;
extern uint8_t  Time_Street_CLOSE;
extern uint8_t  Time_Street_2MIN;
extern uint8_t  Time_Thruway_EN;
extern uint8_t  Time_Thruway_CLOSE;
extern uint8_t  Time_Thruway_2MIN;
uint32_t Relax_cnt;
uint32_t Relax_cnt1;
uint16_t Relax_cnt_street;
uint16_t Relax_cnt_Thruway;
extern  volatile    FuelEco   g_fuelEco1;

uint16_t cntInsFuel;
uint16_t RTIIGNONTimer;
uint16_t RTIIGNOFFTimer;

uint32_t RTITravelTimer;
uint8_t  RTITravelTimerEnable;

uint8_t  RTCSecond;
uint8_t  RTCMinute;
uint8_t  RTCHour;
uint8_t  RTCDate;
uint8_t  RTCMonth;
uint16_t RTCYear;
uint8_t  RTCLeapYear;
uint8_t clear_avaflag2;

volatile uint16_t CallSecond;
volatile uint16_t CallMinute;

uint8_t TickTimer2msFlag1;
uint8_t	TickCnt2ms1;

uint8_t  RTICNT;

extern uint16_t             STOP_2H_FLAG;            //ͣ��2H��־λ

uint16_t RTI_Get_IGN_ON_Timer(void)
{
  return RTIIGNONTimer;
}

uint16_t RTI_Get_IGN_OFF_Timer(void)
{
  return RTIIGNOFFTimer;
}

void RTI_Reset_IGN_ON_Timer(void)
{
  RTIIGNONTimer = 0;
}

void RTI_Reset_IGN_OFF_Timer(void)
{
  RTIIGNOFFTimer = 0;
}

void RTI_Reset_Travel_Timer(void)
{
  RTITravelTimer = 0;
}

uint32_t RTI_Get_Travel_Timer(void)
{
  return RTITravelTimer;
}

void RTI_Travel_Timer_Control(uint8_t Status)
{
  RTITravelTimerEnable = Status;
}


uint8_t Get_Tick_Timer_2ms_Flag1(void)
{
  if (TickTimer2msFlag1)
  {
    TickTimer2msFlag1 = 0;
    return 1;
  }
  else
    return 0;
}


void RTI_IGN_ON_Timing_Service(void)
{
  if (RTI1sFlag)
  {
    RTI1sFlag = 0;
    //IGN����ʱ��
    if (RTIIGNONTimer < 65535) {
      RTIIGNONTimer++;
    }



    //��ʻʱ��
    if (RTITravelTimerEnable)
    {
      RTITravelTimer++;


      if (RTITravelTimer > 359999)       //99ʱ59��59��
        RTITravelTimer = 0;
      }
  }
}

void RTI_IGN_OFF_Timing_Service(void)
{
  if (RTI1sFlag)
  {
    RTI1sFlag = 0;

    if(STOP_2H_FLAG<7200)
      {
        STOP_2H_FLAG++;
      }
     else
      {
      STOP_2H_FLAG=8000;
      clear_avaflag2=1;
      }

    if (RTIIGNOFFTimer < 65535)
      RTIIGNOFFTimer++;
  }
}

void Set_RTC_Second(uint8_t Second)
{
  RTCSecond = Second;
}

void Set_RTC_Minute(uint8_t Minute)
{
  RTCMinute = Minute;
}

void Set_RTC_Hour(uint8_t Hour)
{
  RTCHour   = Hour;
}

void Set_RTC_Date(uint8_t Date)
{
  RTCDate  = Date;
}

void Set_RTC_Month(uint8_t Month)
{
  RTCMonth = Month;
}

void Set_RTC_Year(uint16_t Year)
{
  RTCYear  = Year;
  RTCLeapYear = Determine_Leap_Year(Year);
}

uint8_t Read_RTC_Second(void)
{
  return RTCSecond;
}

uint8_t Read_RTC_Minute(void)
{
  return RTCMinute;
}

uint8_t Read_RTC_Hour(void)
{
  return RTCHour;
}

uint8_t Read_RTC_Date(void)
{
  return RTCDate;
}

uint8_t Read_RTC_Month(void)
{
  return RTCMonth;
}

uint16_t Read_RTC_Year(void)
{
  return RTCYear;
}

void Power_ON_RTI_Init(void)
{
  PLLCTL_PRE = 1;             //1 RTI continues running during Pseudo Stop Mode.
  PLLCTL_PCE = 1;             //1 COP continues running during Pseudo Stop Mode

  //Real Time ʱ��Ƶ������
  RTICTL = 0x97;      //E1              //Real Time=8M/200000=40HZ  (25MS)           ���ڸ�Ϊ8M/16000=500HZ   2MS   ����12.5��
  CRGINT_RTIE = 1;	          //��Real Time �ж�

  RTI25msCounter = 0;
  RTI1sFlag      = 0;

  Set_RTC_Second(0);
  Set_RTC_Minute(0);
  Set_RTC_Hour(0);
  Set_RTC_Date(30);
  Set_RTC_Month(9);
  Set_RTC_Year(2016);
}

uint8_t Determine_Leap_Year(uint16_t Year)
{
  uint8_t LeapYear;

  LeapYear = 0;
  if (Year % 4 == 0)
  {
    if (Year % 100 != 0)
      LeapYear = 1;
    else if (Year % 400 == 0)
      LeapYear = 1;
  }

  return LeapYear;
}
*/
#pragma CODE_SEG __NEAR_SEG NON_BANKED   	//�������жϺ�������FLASH�ķǷ�ҳ��

//==================================================
//RTC�ж�   (����Ϊ25MS)           2015-12-30       500hz  2ms
//==================================================
//void interrupt VectorNumber_Vrti RTC_ISR(void)
void interrupt  RTI_ISR ( void )
{
  /*uint8_t MonthUpdate;
  uint8_t NM_count;
  uint8_t fuelcnt;
  uint8_t contt;

  CRGFLG_RTIF = 1;      //ͨ��д1��0�жϱ�


  TickCnt2ms1++;
  if (TickCnt2ms1 >= 1)
  {
    TickCnt2ms1 = 0;
    TickTimer2msFlag1 = 1;
   }

   if(TaskLock==1)
      	{
       OsekNM_Manage_Main();
      	}

  if(NM_Time.TmaxEnable)
    {
        if(NM_Time.TmaxTime < NM_Time.OverTmaxTime)
        {
            NM_Time.TmaxTime += 2;
        }
     }

    if(NM_Time.TtypEnable)
    {
        if(NM_Time.TtypTime < NM_Time.OverTtypTime)
        {
            NM_Time.TtypTime += 2;
        }
    }
    if(NM_Time.TwbsEnable)
    {
        if(NM_Time.TwbsTime < NM_Time.OverTwbsTime)
        {
            NM_Time.TwbsTime += 2;
        }
    }
    if(NM_Time.SleepEnable)
    {
        if(NM_Time.SleepTime < NM_Time.OverSleepTime)
        {
            NM_Time.SleepTime += 2;
        }
    }

     NM_tSleepRequest_Fun();




    INIT_cnt++;   //25ms��һ
    if(INIT_cnt>=1000)   //ȷ��û���״����ݺ�2s�ر���ʾ
      {
        flag_RADO=0;   //�ر���ʾ
         INIT_cnt=1200;
      }

   cntInsFuel++;
   if(cntInsFuel>=2500)
    {
      InsFuel_clear =1;
      cntInsFuel=0;
    }*/
  /*
  NM_count++;
  if(NM_count>=40)
  {
    NM_count=0;

  } */
  /*
  if(time_start==1)
  	{
  		  RTI15s++;
  		  if(RTI15s>=7500)
  		  	{
  		  	RTI15s=0;
  		  	CLEAR_ODO=1;
  		  	}

  	}
  	else
  		{
  		   CLEAR_ODO=0;

  		}

  fuelcnt++;
  if(fuelcnt>=50)
  {
    g_fuelEco.FuelEcoTime++;
    g_fuelEco.InstCalTime++;
    g_fuelEco.InstUpTime++;
    if(g_fuelEco.AveTime < 200)
          {
                g_fuelEco.AveTime++;
          }
    g_fuelEco1.FuelEcoTime++;
    g_fuelEco1.InstCalTime++;
    g_fuelEco1.InstUpTime++;
    if(g_fuelEco1.AveTime < 200)
          {
                g_fuelEco1.AveTime++;
          }


    fuelcnt=0;
  }


  RTI25msCounter++;





  if (RTI25msCounter >= 500)
  {
    RTI25msCounter -= 500;
    RTI1sFlag = 1;

   if(Time_Street_EN)
      {
       Relax_cnt++;  //ԭ��һ�μ�25ms  ����һ�μ�2ms
      }
     else
      {
       Relax_cnt=0;
      }

    if(Time_Thruway_EN)
      {
       Relax_cnt1++;
      }
   else
      {
      Relax_cnt1=0;
      }


    if(Time_Thruway_CLOSE)
      {
       Relax_cnt_Thruway++;
       if(Relax_cnt_Thruway>=120)
        {
          Relax_cnt_Thruway=0;
          Time_Thruway_2MIN=1;
        }
      }
     else
      {
        Relax_cnt_Thruway=0;
        Time_Thruway_2MIN=0;
      }

    if(Time_Street_CLOSE)
      {
        Relax_cnt_street++;
        if(Relax_cnt_street>=120)
          {
            Relax_cnt_street=0;
          Time_Street_2MIN=1;
          }

      }
     else
      {
         Relax_cnt_street=0;
         Time_Street_2MIN=0;
      }



    if(TEL_STATUS_HUM==2)
    {

        CallSecond++;
        if(CallSecond>=60)
          {
          CallSecond=0;
          CallMinute++;
          }

    } else if((TEL_STATUS_HUM==1)||(TEL_STATUS_HUM==0))
    {
    CallSecond=0;
    CallMinute=0;
    }



    RTCSecond++;
    if (RTCSecond > 59)
    {
      RTCSecond = 0;

      RTCMinute++;
      if(RTCMinute > 59)
      {
        RTCMinute = 0;

        RTCHour++;
        if (RTCHour > 23)
        {
          RTCHour = 0;

          RTCDate++;
          MonthUpdate = 0;
          switch (RTCMonth)
          {
            case 1  :
            case 3  :
            case 5  :
            case 7  :
            case 8  :
            case 10 :
            case 12 : if (RTCDate > 31)
                      {
                        RTCDate = 1;

                        RTCMonth++;
                        MonthUpdate = 1;
                      }
                      break;

            case 4  :
            case 6  :
            case 9  :
            case 11 : if (RTCDate > 30)
                      {
                        RTCDate = 1;

                        RTCMonth++;
                        MonthUpdate = 1;
                      }
                      break;

            case 2  : if (RTCLeapYear)
                      {
                        if (RTCDate > 29)
                        {
                          RTCDate = 1;

                          RTCMonth++;
                          MonthUpdate = 1;
                        }
                      }
                      else
                      {
                        if (RTCDate > 28)
                        {
                          RTCDate = 1;

                          RTCMonth++;
                          MonthUpdate = 1;
                        }
                      }
                      break;
          }

          if (MonthUpdate)
          {
            if (RTCMonth > 12)
            {
              RTCYear++;
              RTCLeapYear = Determine_Leap_Year(RTCYear);
            }
          }
        }
      }
    }
  }
  */
}

#pragma CODE_SEG DEFAULT
#pragma MESSAGE DISABLE C1825