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

/*-------------------------------------------------------------------------
* Function Name  : Api_SCI_Init
* Description    : SCI模块初始化
* Input          : 
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
void Api_SCI_Init(void)
{  
    SCISR2_AMAP = Disable;
    SCICR1_LOOPS   = Disable;   //串口正常模式
    SCICR1_SCISWAI = Disable;    //使能等待模式
    SCICR1_M       = Disable;   //8 byte 模式
    SCICR1_WAKE    = Enable;    //Adrress Wake
    SCICR1_PE      = Disable;   //禁止校验
    SCISR1_RDRF    = Disable;
    
    SCISR2_BRK13 = !Enable; //14bit break    
    SCISR2_AMAP = Disable;
                
    SCIBDH_TNP     = div4;
    SCIBDH_IREN    = Disable;   //禁止红外线
    SCIBD_SBR      = baud_9600;
    
    SCICR2_TIE  = Disable;
    SCICR2_TCIE = Disable;
    SCICR2_ILIE = Disable;
    SCICR2_RE  = Enable;    //使能接收
    SCICR2_TE  = Enable;    //使能发送
    SCICR2_RWU = Enable;
    SCICR2_SBK = Disable;   //
  
    SCICR2_RIE = Enable;    //使能接收中断
    
    SCISR2_AMAP = Enable;
    SCIACR2_BKDFE   = Enable;   //使能断点检测电路
    SCIACR2_BERRM   = 0;    
    SCIACR1_BERRIE  = Disable;  //位错误
    SCIACR1_RXEDGIE = Disable;  //
    SCIACR1_BKDIE   = Enable;   //断点检测使能
    SCIASR1_BKDIF = ClearFlag; 
   
}
/*-------------------------------------------------------------------------
* Function Name  : Api_SCI_Trans_Bytes
* Description    : 串口发送
* Input          : INT8U *str,INT16U len
* Output         : None
* Return         : None
* onther         : str:发送字符串   len:发送数据长度
--------------------------------------------------------------------------*/
void Api_SCI_Trans_Bytes(INT8U *str,INT8U len)
{
    INT8U i;    
    for(i=0;i<len;i++)
    {
       Api_SCI_Trans_Byte((*str));
       str++;
    }
}
/*-------------------------------------------------------------------------
* Function Name  : Api_SCI_Trans_Byte
* Description    : 串口发送
* Input          : INT8U data
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
void  Api_SCI_Trans_Byte(INT8U data)
{
    while(SCISR1_TDRE == null);
    SCIDRL = data;
    while(!SCISR1_TC);    
}
/*-------------------------------------------------------------------------
* Function Name  : Api_SCI_Rev_isr
* Description    : 串口接收中断
* Input          : 
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
interrupt void Api_SCI_Rev_isr(void)
{    
    //断点检测中断
    if(SCIASR1_BKDIF == Enable)
    {
        Comm.ReceiveType = LIN;   
        API_SetTimer9(T_3s,(ClockType*)&Clock);    
        rvcnt = null;            
    }     
    switch(Comm.ReceiveType)
    {
      case COM:
          if(SCISR1_RDRF == Enable)
          {      
              SCIRV[rvcnt] = SCIDRL;
              rvcnt ++;    
              if((SCIRV[0]==0xEB)&&(SCIRV[1]==0x90))
              {                    
                  if(SCIRV[rvcnt-1]==0xAA)
                  {   
                      if(SCIRV[rvcnt-2]==0x55)
                      {
                          rvcnt   = null;
                          CarFlag.FlagBits.RS232RV_Full = Enable;
                          CarFlag.FlagBits.AdjEnable = Enable;
                      }
                  }       
              }
          }
          if(SCIRV[0] != 0xEB)
          {
              rvcnt = null;
          }
          if(rvcnt >= SCI_LEN) 
          {
              rvcnt = null;
          }
          break;
      case LIN:
          if(SCISR1_RDRF == Enable)
          {                    
              SCIRV[rvcnt] = SCIDRL;
              rvcnt ++;    
          }

          if(SCIRV[0] == 0x55)
          {                  
              switch(SCIRV[1])
              {
                case BCM_ID:
                    if(rvcnt == 8) 
                    {                        
                        DropTime = null;
                        rvcnt = null;
                    }
                    break;                    
                default:
                    SCIRV[1] = 0;
                    SCIRV[2] = 0;
                    SCIRV[3] = 0;
              }             
          }
          else
          {
              rvcnt = null;
              SCIRV[0] = 0;
              SCIRV[1] = 0;
              SCIRV[2] = 0;
              SCIRV[3] = 0;              
          }
          break;
    }
    SCIASR1_BKDIF = ClearFlag;      
    SCISR1_RDRF = ClearFlag;
    
}
