#define  GLOBALS_BSP_SYS
#include "../app_Headers/g_includes.h"

/*-------------------------------------------------------------------------
* Function Name  : bsp_Sys_Init
* Description    : 系统初始化
* Input          : None
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
void bsp_Sys_Init(void)
{
   DisableInterrupts;
   bsp_Clock_Init(); 
   bsp_GPIO_Init();
   bsp_CAN_Init();   
   bsp_Timer0_Init(TPM_DIV1);     
   bsp_SCI_Init();
   bsp_LCD_Init();
   Api_API_Init(1);   
   EnableInterrupts;
}
/*-------------------------------------------------------------------------
* Function Name  : bsp_Clock_Init
* Description    : 系统时钟初始化
* Input          : None
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
void bsp_Clock_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       = InitCPMUCLKS;         //在停止模式下振荡运行 选择PLL时钟
    CPMUCLKS_PLLSEL = Enable;
                
    CPMUOSC_OSCE   = Enable;               //使能外部振荡器
    while(!CPMUFLG_UPOSC)                  //等待振荡稳定
    { 
       dwTime++;
       _FEED_COP();
       if(dwTime > 10000)
       {
           dwTime = null;
           break;
       }
    }
    CPMUCLKS_PSTP  = Enable;               //伪停止模式
    CPMUCLKS_RTIOSCSEL = Enable;           //RTI STOP MODE 继续运行 
    CPMUCLKS_PRE   = Enable;               
    CPMUCLKS_COPOSCSEL = !Enable;           //COP STOP MODE 运行 
    CPMUCLKS_PCE   = !Enable; 
    
    CPMUPROT       = Enable;               //使能时钟保护
    CPMUHTCTL      = Disable;              //温度保护禁止
    //中断
    CPMUINT_OSCIE  = Disable;              //Osc
    CPMUINT_LOCKIE = Disable;              //Lock
    //RTI
    CPMURTI_RTDEC  = True;                 //十进制
    CPMURTI_RTR    = 0x57;                   //400*10^3 0X57 4M  0x5F 8M  800 *10^4
    
    CPMUINT_RTIE   = Enable;               //RTI  
    CPMULVCTL_LVIE = Disable;              //LV
    IRQCR          = Disable;              //IRQ  
    
    


}



 //===========================================
//
//===========================================
void GJ_CS10_JS_ECUOUT_N(void)
{
    INT16U m16;  

    m16=973/g_runStep;  //8hz   	
    //m16=469/g_runStep;  //16.6hz 
    //m16=78/g_runStep;  //100hz 
    //m16=156/g_runStep;  //50hz
    //m16=7812/g_runStep;  //1hz 
    
    CS_FR_BASIC= ((INT32U)695*8*10)/3600;
    
    //glz--20170428  车速PWM
     if(g_FRStep==0) 
      {
      CS_FR=7812; //1HZ

      ZS_FR=7812; //1HZ
      }

      if(g_FRStep==1) 
      {
        CS_FR=326; //20KM       //===== CS_FR=651;10KM  12HZ =====//
        ZS_FR=234;//8.33hz 1000r
      }
      if(g_FRStep==2) 
      {
        CS_FR=163; //40KM
        ZS_FR=117;//16.66hz 2000r
      }  
      if(g_FRStep==3) 
      {
        CS_FR=108; //60KM
        ZS_FR=78;      //3000
      }  
      if(g_FRStep==4) 
      {
        CS_FR=81; //80KM
        ZS_FR=58;     //4000
      }  
      if(g_FRStep==5) 
      {
        CS_FR=65; //100KM
        ZS_FR=47;      //5000
      }  
      if(g_FRStep==6) 
      {
        CS_FR=54; //120KM
        ZS_FR=39;     //6000
      }  
      if(g_FRStep==7) 
      {
        CS_FR=47; //140KM
        ZS_FR=33;     //7000
      }  
      if(g_FRStep==8) 
      {
        CS_FR=30; //160KM
        ZS_FR=31;     //7600
      }     

     /* //====1/2表盘=======//  
        
       if(g_FRStep==9) 
      {
        CS_FR=7812; //0KM
        ZS_FR=7812; //0
      }
      
      if(g_FRStep==11) 
      {
        CS_FR=81; //80KM
        ZS_FR=58;     //4000
      }
      
      if(g_FRStep==13) 
      {
        CS_FR=7812; //0KM
        ZS_FR=7812; //0
      } 
      
        if(g_FRStep==15) 
      {
        CS_FR=65; //100KM
        ZS_FR=47;      //5000
      } 
      
        if(g_FRStep==17) 
      {
        CS_FR=7812; //0KM
        ZS_FR=7812; //0
      } 
      
        if(g_FRStep==19) 
      {
        CS_FR=54; //120KM
        ZS_FR=39;     //6000
      } 
      
          if(g_FRStep==21) 
      {
        CS_FR=7812; //0KM
        ZS_FR=7812; //0
      } 
      
        if(g_FRStep==23) 
      {
        CS_FR=47; //140KM
        ZS_FR=33;     //7000
      }                    
                   
                   
      if(g_FRStep==26) 

      {
        CS_FR=7812; //0KM
        ZS_FR=7812; //0     //
      } 
      
      
          if(g_FRStep==29) 
      {
        CS_FR=40; //160KM
        ZS_FR=31;     //7600
      }                    
         

      if(g_FRStep==35) 

      {
        CS_FR=7812; //0KM
        ZS_FR=7812; //0

        
      } */

      //     
          
    cli();     
    CS_OUT_N=CS_FR;
    ZS_OUT_N=ZS_FR; 
    sei();    
}         



#pragma    CODE_SEG    NON_BANKED    
interrupt void bsp_API_isr(void)
{    
    //    INT16U mTIME;
        CPMUAPICTL_APIF=1;	
        
                

        if(CS_OUT_N)                 
        {
            if(++CS_OUT_CC>=CS_OUT_N)                // 64US 
            {
                
                CS_OUT_CC=0;
                CS_PWM_OUT=!CS_PWM_OUT;	
                
            }
        }  
        
        //glz--20170428
        if(ZS_OUT_N)                 
        {
                if(++ZS_OUT_CC>=ZS_OUT_N)                // 64US 
                {
                ZS_OUT_CC=0;
                
                ZS_PWM_OUT=!ZS_PWM_OUT;	
                }
        } 
        
        
                //		车速表
        CZBTIME0_6MSCCFF=!CZBTIME0_6MSCCFF;
        if(CZBTIME0_6MSCCFF)                                                       //【应该是128us】
        {

                if(++iTIME1MS==8)		//1.024MS
                {
                        T1MS_F1_FF=1;			//1MS标志(用于水油表走动)   
                        iTIME1MS=0;
                        iTIME2MS=!iTIME2MS;
                        if(iTIME2MS)
                        {
                                T2MS_F2_FF=1;       //2MS标志(用于车速表输入脉冲超长监测)
                                T2MS_F9_FF=1;       //2MS标志(任务时间标志) 
                        }  
         
                }	

  
        }
}


  #pragma CODE_SEG DEFAULT
//-------------------------------------------------------------------------------
void Api_API_Init(INT8U tog)
{   
    //自主周期中断时间
    CPMUAPICTL_APICLK=1;         //Bus Clock 做为 自主周期中断的时钟源   
   
    CPMUAPIR=1023;   			//自主周期中断周期=2*(1023+1)*(1/32M)=64US      +++++++++  
   
    CPMUAPICTL_APIFE=1;			//使能自主周期中断功能,并开始计时
    
    //-----------------------------	//306页
    if(tog)  	CPMUAPICTL_APIE=1;           //开自主周期中断
   
    else   	CPMUAPICTL_APIE=0;           //开自主周期中断
}




