#define  GLOBALS_API_DEV
#include "../App_Headers/includes.h"

/*-------------------------------------------------------------------------
* Function Name  : Api_Sys_Init
* Description    : ϵͳ��ʼ��
* Input          : None
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
void Api_Sys_Init(void)
{
   DisableInterrupts;
   Api_Clk_Init();
   Api_GPIO_Init();
   Api_ATD_Init();      
   Api_Flash_Init();
   Api_Timer0_Init(TPM_DIV1);  
   Api_Timer1_Init(TPM_DIV8);  
   Api_PWM_Init();
   Api_CAN_Init();
 //  Api_SCI_Init();
   Api_Motor_Init();
   Api_LCD_Init();  
   EnableInterrupts;
}
/*-------------------------------------------------------------------------
* Function Name  : Api_Sys_Init
* Description    : ϵͳ��ʼ��
* Input          : None
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
void Api_Sys_ON(void)
{
 // Api_SCI_Init();                                     
  Api_GPIO_Init();  
  Api_Motor_Init();
  TIM0_TSCR1_TEN = Enable;   //ʹ��timer 0 ��������
  TIM1_TSCR1_TEN = Enable; 
 // PWME_PWME0 = Enable; 
  TIM0_TIE_C2I = Disable; 	
  //CPMUAPICTL_APIE=1;          //�����������ж�
//	CPMUAPICTL_APIFE=1;			//ʹ�����������жϹ���,����ʼ��ʱ  
	TIM1_TIE_C7I   = Enable; 
	bsp_CAN_SleepRelease();
	ClearOdoCanOut   = 0;
	ClearOdoCanOutCn = 0;
}
/*-------------------------------------------------------------------------
* Function Name  : Api_Sys_Off                               glz--20170505
* Description    : 
* Input          : None
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
void Api_Sys_Off(void)
{    
    GUI_LCD_Clear();
    //
    MCCC0_MCOM          = clear;          //˫ȫH��ģʽ
    MCCC0_MCAM          = clear;          //�����
    MCCC0_CD            = clear;          //��ֹ��ʱ
    MCCC1_MCOM          = clear;          //ȫH��ģʽ
    MCCC1_MCAM          = clear;          //�����
    MCCC1_CD            = clear;          //��ֹ��ʱ
    //
    MCCC2_MCOM          = clear;          //˫ȫH��ģʽ
    MCCC2_MCAM          = clear;          //�����
    MCCC2_CD            = clear;          //��ֹ��ʱ
    MCCC3_MCOM          = clear;          //ȫH��ģʽ
    MCCC3_MCAM          = clear;          //�����
    MCCC3_CD            = clear;          //��ֹ��ʱ
           
    MCCC4_MCOM          = clear;         
    MCCC4_MCAM          = clear;         
    MCCC4_CD            = clear;         
    MCCC5_MCOM          = clear;         
    MCCC5_MCAM          = clear;       
    MCCC5_CD            = clear;       
    
    
    MCCC6_MCOM          = clear;          //˫ȫH��ģʽ
    MCCC6_MCAM          = clear;          //�����
    MCCC6_CD            = clear;          //��ֹ��ʱ
    MCCC7_MCOM          = clear;          //ȫH��ģʽ
    MCCC7_MCAM          = clear;          //�����
    MCCC7_CD            = clear;          //��ֹ��ʱ
    
    MCCTL1_MCTOIE       = Disable;          //��ֹ�ж� 
    ZD_QIYA_OUT=0;
    CS_OUT =0;
    OILWATER_LED_P= OUT;
    ABS_LED_P=0;//
    YURE_LED_P=0;//
    ABS_LED_P=0;
    FDJ1_LED_P=0;//
    TLY_LED_P=0;//
    OBD_LED_P=1;//
    FDJ2_LED_P  =  OUT;//
    TIM0_TSCR1_TEN = Disable ;
    TIM1_TSCR1_TEN = Disable ;
    TIM0_TIE_C6I     = Disable;                 
    TIM0_TIE_C7I     = Disable;
    TIM1_TIE_C7I     = Disable;
    TIM0_TIE_C2I     = Enable;  
    bCatalystLevel=0;  
    DriverLamp_OUT =0;
    Cruise_OUT =0;
    DPF_OUT =0;
    HOutputTempL_OUT =0;   
}
#pragma CODE_SEG OTHER_ROM
/*-------------------------------------------------------------------------
* Function Name  : Api_InitParamer
* Description    : ��������
* Input          : None
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
void Api_InitParamer(void)
{
   LED.CheckTime = null;
   FuleSpeed=10;
   Fuelnum=0;
   FuleShowFlag=1;
   TempShowFlag=1;
   TempSpeed=10;
   Tempnum =0;
   KeyShortClr=0;
   KeyRadioSet=0;
   bCatalystLevel=0x00;
   FuelTime=0;

    bF004Flag=0;
    bFEEEFlag=0;
    bFECA00Flag=0;  
    bFEFFFlag=0;   
    bFEE4Flag=0;  
    bFF08Flag=0;  
    bFE5600Flag=0;
    bFE40Flag=0;
  //  bFF0AFlag=0;    lzs--shan
  //  bFF04Flag=0;    lzs--shan

   CarFlag.FlagBits.CheckState   = Disable;    
   CarFlag.FlagBits.TempMeterInit = Disable;
   CarFlag.FlagBits.FuelMeterInit = Disable;
   CarFlag.FlagBits.AdjEnable    = Disable;
   CarFlag.FlagBits.TempStar      = Disable;
   CarFlag.FlagBits.FuelStar     = Disable;
   CarFlag.FlagBits.BeepProcEn   = Enable;
   
   //motor
   VehMeter.Dir = keep_local;
   VehMeter.DestPosition = null;
   VehMeter.CurPosition = null;
   VehMeter.CurBoardVal = null; 
   VehMeter.OldBoardVal = null;
   VehMeter.CalTime     = null;
      
   RevMeter.Dir = keep_local;
   RevMeter.DestPosition = null;
   RevMeter.CurPosition = null;
   RevMeter.CurBoardVal = null; 
   RevMeter.CalTime = null;
    
   FuelMeter.Dir = keep_local;
   FuelMeter.CurPosition = null;
   FuelMeter.CalTime = null;
   
   TempMeter.Dir = keep_local;
   TempMeter.CurBoardVal = null;
   TempMeter.CurPosition = null;
   TempMeter.DestPosition = null;
   TempMeter.CalTime = null;
       
   CurRevCnt.dw32 = null;
   OldRevCnt.dw32 = null;
   CurVehCnt.dw32 = null;
   OldVehCnt.dw32 = null; 
   
   VehFre.Frequency = null; 
   VehFre.pCycle = null;
   VehFre.Cycle[0] = null;
   VehFre.Cycle[1] = null;
   VehFre.Cycle[2] = null;
   VehFre.Cycle[3] = null;
   VehFre.Cycle[4] = null;
   VehFre.Cycl  = null;
   VehCycl = null;                            
   VehFre.CyclH = null;
   VehFre.CyclL = null;
      
   RevFre.Frequency = null;
   RevFre.pCycle = null;
   RevFre.Cycle[0] = null;
   RevFre.Cycle[1] = null;
   RevFre.Cycle[2] = null;
   RevFre.Cycle[3] = null;
   RevFre.Cycle[4] = null; 
   RevFre.Cycl  = null; 
   RevCycl = null; 
   RevFre.CyclH = null;
   RevFre.CyclL = null; 
           
   Comm.ReceiveType = COM;
   TempSignal.Frequency = null;
   
   NopBeep.Prio = DisAlr;
   NopBeep.OnTime = null;
   NopBeep.OffTime = null;
   NopBeep.BeepTime = null;
   NopBeep.BeepCn = null;
   NopBeep.CurBeepCn = null;
   
   BeepBuf[0] = &NopBeep; 
   BeepBuf[1] = &NopBeep; 
   BeepBuf[2] = &NopBeep; 
   BeepBuf[4] = &NopBeep; 
   BeepBuf[5] = &NopBeep; 
   BeepBuf[6] = &NopBeep; 
   
   Beep[0].Prio  = DisAlr;
   Beep[1].Prio  = DisAlr;
   Beep[2].Prio  = DisAlr;   
   Beep[4].Prio  = DisAlr;
   Beep[5].Prio  = DisAlr; 
   Beep[6].Prio  = DisAlr; 
   
   if(pBeep->Prio != DisAlr)
   {
       pBeep = &NopBeep;
   }

   
   ADEveArry.FuelCn = null;
   ADEveArry.pFuelCal = null;
   ADEveArry.pPos = null; 
   
   CheckStep = Disp0;   
   RunStep = SysInit_Proc; 
   rvcnt = null; 
   ClrEn = Disable;
   
    //glz--20170518
    InitAdblueTime = 0;
    AdblueInitTime = 0;
    Adbule_Section = 0;
    InitAdblueFlag = 1;
    AdblueInitDealyTime = 5;
    
    //glz--20170519
    CansendVeh = 0;
    LED.StopBreakTime = null;
    ParkIn_Flag = 0;
    LED.DangweiTime = null;
    Dangwei_Flag = 0;
    CluchScaleFlag = 0;
    
    
    //
    InitQy1Time = 0;
    QY1InitTime = 0;
    QY1_Section = 0;
    InitQYFlag1 = 1;
    QY1InitDealyTime = 1;//5; lzs--gai-- 
    QY1Meter.CurBoardVal = 0;
    QY1Meter.CurPosition = 0;
    QY1Meter.DestPosition = 0;
    QY1_Section = 0; 
    
    InitQy2Time = 0;
    QY2InitTime = 0;
    QY2_Section = 0;
    InitQYFlag2 = 1;
    QY2InitDealyTime = 1; //5; lzs--gai-- 
    QY2Meter.CurBoardVal = 0;
    QY2Meter.CurPosition = 0;
    QY2Meter.DestPosition = 0;
    QY2_Section = 0; 
    bDSTable[ID_EWLAMP]|=DS0_MASK;			
    bDSTable[ID_EWLAMP]&=(~DS1_MASK);        
    bDSTable[ID_EWLAMP1]|=DS0_MASK;			
    bDSTable[ID_EWLAMP1]&=(~DS1_MASK);
    CanClk.Signal.ExistFECA00 = Enable;
    CanClk.Signal.ExistFECA3D = Enable;
    CanClk.IDFECA00Cn = 30;
    CanClk.IDFECA3DCn = 30; 
    bFECA00Flag=1;    
    OBD_CheckOneFlag = 1;
    OBD_CheckTime = 0;
    PackBroadFlag = 0;
    Charge_CheckTimer = 0;
    //
    G_Button.SET_TIME = 0;
    G_Button.JUMP = 0;
    OilPressure = 255;
   
    g_accTime = null; 
    CanMsgEECA00.Data[1].bbyte = 0x3c;
    CanMsgEECA00.Data[0].bbyte = 0x34;
    
    CanMsgFEFF.Data[1].bbyte = 0x07;
    CanMsgFD0700.Data[0].bbyte = 0x0c;

   ///////////////
   App_Load_Data();
}
#pragma CODE_SEG DEFAULT
/*-------------------------------------------------------------------------
* Function Name  : Api_Clk_Init
* Description    : ϵͳʱ�ӳ�ʼ��
* Input          : None
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
void Api_Clk_Init(void)
{
  INT32U    dwTime = null;
  MODE      = NormalChip;
  DIRECT    = InitDIRECT;           //��ʼ��ֱ��ҳ�Ĵ���
  IVBR      = InitIVBR;             //��ʼ���ж���������ַ
  CPMUOSC_OSCE   = Enable;                //ʹ���ⲿ����
  while(!CPMUFLG_UPOSC)                   //�ȴ����ȶ�
  {
    dwTime++;
    if(dwTime > 10000)
    {
      dwTime = null;
      break;
    }
  }
  ECLKCTL_NECLK  = Enable;               //��ֹECLK���
  CPMUPROT       = InitCPMUPROT;         //��ֹʱ�ӱ���   
  CPMUREFDIV     = InitCPMUREFDIV;       //���÷�Ƶϵ��
  CPMUSYNR       = InitCPMUSYNR;         //���ó�ϵ��
  CPMUPOSTDIV    = InitCPMUPOSTDIV;      //���÷�Ƶ����
  CPMUPLL        = InitCPMUPLL;          //PLLƵ�ʵ���
  CPMUCOP        = InitCPMUCOP;               //�������Ź�
  CPMUCLKS_PLLSEL = Enable;
                
  CPMUOSC_OSCE   = Enable;               //ʹ���ⲿ����
  while(!CPMUFLG_UPOSC)                  //�ȴ����ȶ�
  { 
    dwTime++;
    _FEED_COP();
    if(dwTime > 10000)
    {
           dwTime = null;
           break;
       }
    }
    CPMUCLKS_PSTP  = Enable;               //αֹͣģʽ
    CPMUCLKS_RTIOSCSEL = Enable;           //1 RTI clock source is OSCCLK.
    CPMUCLKS_PRE   = Enable;               
    CPMUCLKS_COPOSCSEL = !Enable;           //0 COP clock source is IRCCLK.
    CPMUCLKS_PCE   = !Enable;               //0 COP stops running during Pseudo Stop Mode
    
    CPMUPROT       = Enable;               //ʹ��ʱ�ӱ���
    CPMUHTCTL      = Disable;              //�¶ȱ�����ֹ
    //�ж�
    CPMUINT_OSCIE  = Disable;              //Osc
    CPMUINT_LOCKIE = Disable;              //Lock
    //RTI
    CPMURTI_RTDEC  = True;                 //ʮ����
    CPMURTI_RTR    = 0x5f;                   //400*10^3 0X57 4M  0x5F 8M  800 *10^4
    
    CPMUINT_RTIE   = Enable;               //RTI  
    CPMULVCTL_LVIE = Disable;              //LV
    IRQCR          = Disable;              //IRQ 
    
    //////////////////////////////////////////////////////
   // CPMUAPICTL_APICLK=1; 		//Bus Clock ��Ϊ ���������жϵ�ʱ��Դ
   // CPMUAPIR=1023;   			//���������ж�����=2*(1023+1)*(1/32M)=64US      +++++++++    
    //CPMUAPIR=959;   			//���������ж�����=2*(959+1)*(1/32M)=60US      +++++++++    

//	CPMUAPICTL_APIE=1;          //�����������ж�
//	CPMUAPICTL_APIFE=1;			//ʹ�����������жϹ���,����ʼ��ʱ
}
/*-------------------------------------------------------------------------
�������ƣ�Api_GPIO_Init
* Description    : 
* Input          : None
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
void Api_GPIO_Init(void)
{   

  PPSH_PPSH4 = 0;
  PERH_PERH4 = 0;
  PUCR = 0x40;  
  DDRS_DDRS4 = 1;
  DIRC_YB_ZERO_IN = OUT;
  DIRC_TLY_LED_P = OUT;
  DIRC_FDJ1_LED_P = OUT;
  DIRC_ABS_LED_P = OUT;
  DIRC_OBD_LED_P = OUT;
 // OBD_LED_P = OUT; //--lzs--jia--
  
  DIRC_AQD_LED_IN = IN;
       
  DIRC_TJA1040_STB = OUT;      
  DIRC_CS_OUT = OUT; 
       
  DIRC_FMQ = OUT;
         
  DIRC_ZZX_LED_P = OUT;
  //ZZX_LED_P  =  OUT; //--
  DIRC_YURE_LED_P = OUT;
  DIRC_BACKFOG_LED_P = OUT;    //--������--��
  BACKFOG_LED_P = OUT;  //lzs--jia--
  DIRC_YZX_LED_P   =  OUT;     
  //YZX_LED_P =  OUT; //lzs--jia
  DIRC_OILWATER_LED_P   =  OUT;   //--��ˮ�����--
  OILWATER_LED_P   =  OUT;   //--lzs--jia--
  DIRC_KEY2_IN   =IN;//  IN;
  DIRC_KEY1_IN   =IN;//  IN;
  DIRC_KEY0_IN   = IN;
  DIRC_ZD_QIYA_OUT   =  OUT;
  DIRC_VehZero_IN  = IN;
  DIRC_RevZero_IN  =  IN;
  DIRC_YB_ZERO_IN  =  IN;
  DIRC_Tpm_ZERO_IN =  IN;
  PERT_PERT1      =1 ;
  PERT_PERT0      =1 ;
  PERT_PERT2      =1;
  PPST_PPST0      =0 ;
  PPST_PPST1      =0 ;
  PPST_PPST2      =0 ;
  DDRR_DDRR3  =  IN;
  PER1AD_PER1AD2  =1 ;
  PER1AD_PER1AD3  =1 ; 
  PERR_PERR3=0; 
  PPSR_PPSR3=0;
  ///////////////////
  DIRC_LCD_SDA   =  OUT;
  DIRC_LCD_SCL   =  OUT;   
  PTTRR_PTTRR1 = 1; 
  DIRC_ZZX_LED_IN =  IN;
  DIRC_YZX_LED_IN =  IN;
  DIRC_DriverLamp_OUT     =  OUT;
  DIRC_Cruise_OUT   =  OUT;
  DIRC_RFUEL_LEVEL_LOW_OUT   =  OUT;
  DIRC_HOutputTempL_OUT   =  OUT;
  DIRC_HOutputTempR_OUT   =  OUT;
  DIRC_BACKFOG_LED_IN =  IN;
  
  
  DIRC_FDJ2_LED_P   =  OUT;
  DIRC_Park_In =  IN;
  PERP_PERP4 = 1;
  DIRC_DPF_OUT =OUT;
  DPF_OUT = 0;//OUT; 
  PERT_PERT7 = 1;
  PPST_PPST7 = 0;
  DIRC_Oil_LED_P = OUT;
  DIRC_DPFForbit_LED_P = OUT;
  DDRR_DDRR6 = OUT;
  DIRC_DPFR_OUT = OUT; //���������ϵơ�
 // DPFR_OUT = OUT; //--lzs--jia--
  //
  DDRP_DDRP0 = OUT;
  PERP_PERP0 = 0;
  PPSP_PPSP0 = 1;       
  PERS_PERS6=1;
  PERP_PERP6=1;
  PPSP_PPSP6=0;
  PERP_PERP7=1;
  PPSP_PPSP7=0 ;
  PERP_PERP4=0;
  PPSP_PPSP4=1 ;
  PTPRRL_PTPRRL1 = 1;
  //------------------------LZS---JIA----
  DDRA_DDRA7 = IN;
       
  //OBD_LED_P = 0;//�� lzs--shan--
 // FDJ1_LED_P = 1;//��
  TJA1040_STB=LOW;    
  DriverRLamp_OUT = OFF;
  RFUEL_LEVEL_LOW = OFF;
       
  FDJ2_LED_P =1; //lzs--jia--
  DIRC_Charge_LED_IN = IN;   // 充电输入80
  DIRC_Charge_LED_OUT = OUT; // 充电输出79
  Charge_OUT = 0;
  Charge_In = 1;
}
