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

/*-------------------------------------------------------------------------
* Function Name  : App_MsgDropProc
* Description    : 
* Input          : None
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
void App_MsgDropProc(void)
{
    if(DropTime > T_3s)
    {
        BcmModel.status[0] = null;
        BcmModel.status[1] = null; 
        Beep[3].Prio = DisAlr;
        Beep[3].CurBeepCn = null;
        Beep[5].Prio = DisAlr;
        Beep[5].CurBeepCn = null;  
        
    }
}
/*-------------------------------------------------------------------------
函数名称：App_SCI_Proc
功    能：串口数据处理
参    数：None
返回数值：None
使用说明：
--------------------------------------------------------------------------*/
void App_SCI_Proc(void)
{
  if((VehMeter.CurBoardVal >= VehMeterParamer.Resistor[VehMeterParamer.bVirtualVal-1]-3)&&\
     (RevMeter.CurBoardVal >= RevMeterParamer.Resistor[RevMeterParamer.bVirtualVal-1]-200)&&\
     (SCIACR1_BKDIE))
  {
    SCICR1_LOOPS   = Disable;   //串口正常模式
    SCICR1_SCISWAI = Enable;    //禁止等待模式
    SCICR1_M       = Disable;   //8 byte 模式
    SCICR1_WAKE    = Enable;    //Adrress Wake
    SCICR1_PE      = Disable;   //禁止校验
    SCISR1_RDRF    = Disable;

    SCIACR1_RXEDGIE = Disable;  //
    SCIACR1_BERRIE  = Disable;
    SCIACR1_BKDIE   = Disable;
    SCIACR2 = Disable;

    SCIBDH_TNP     = div4;
    /*
    When IREN = 0 then,
    SCI baud rate = SCI bus clock / (16 x SBR[12:0])
    When IREN = 1 then,
    SCI baud rate = SCI bus clock / (32 x SBR[12:1])
    */
    SCIBDH_IREN    = Disable;   //禁止红外线
    SCIBD_SBR      = baud_9600;

    SCICR2_TIE  = Disable;
    SCICR2_TCIE = Disable;
    SCICR2_ILIE = Disable;
    SCICR2_RE  = Enable;    //使能接收
    SCICR2_TE  = Enable;    //使能发送
    SCICR2_RWU = Disable;
    SCICR2_SBK = Disable;   //使能发送
      
    SCICR2_RIE = Enable;    //使能接收中断
                
    SCISR2_AMAP = Enable;
    SCIACR2_BKDFE   = Disable;   //禁止断点检测电路
    SCIACR2_BERRM   = 0;    
    SCIACR1_BERRIE  = Disable;  //位错误
    SCIACR1_RXEDGIE = Disable;  //
    SCIACR1_BKDIE   = Disable;   //断点检测禁止
    SCIASR1_BKDIF = ClearFlag;         
  }
      
  if(CarFlag.FlagBits.RS232RV_Full == Enable)
  {
    CarFlag.FlagBits.RS232RV_Full = Disable;
    App_CopyRam((INT8U *)&SciFrame,(INT8U *)SCIRV,SCI_LEN);
    if(SCIRV[2] == 1)   //车速
    {
      SCIRV[0] = 0xeb;
      SCIRV[1] = 0x90;
      SCIRV[2] = 0x55;
      SCIRV[3] = 0x01;
      SCIRV[4] = 0x02;//上传
      SCIRV[5] = 0x00; 
      SCIRV[6] = 0x21;

      App_CopyRam((INT8U *)&SCIRV[7],(INT8U *)&VehMeterParamer.bVirtualVal,33);
           
      SCIRV[40] = App_CreateCheckSun((INT8U*)&SCIRV[7],SCIRV[2],33);
      SCIRV[41] = 0x55;
      SCIRV[42] = 0xaa;
      Api_SCI_Trans_Bytes((INT8U *)SCIRV,43);
    }
    if(SCIRV[2] == 2)   //转速
    {
      SCIRV[0] = 0xeb;
      SCIRV[1] = 0x90;
      SCIRV[2] = 0x55;
      SCIRV[3] = 0x02;
      SCIRV[4] = 0x02;//上传
      SCIRV[5] = 0x00; 
      SCIRV[6] = 0x21;

      App_CopyRam((INT8U *)&SCIRV[7],(INT8U *)&RevMeterParamer.bVirtualVal,33);
           
      SCIRV[40] = App_CreateCheckSun((INT8U*)&SCIRV[7],SCIRV[2],33);
      SCIRV[41] = 0x55;
      SCIRV[42] = 0xaa;
      Api_SCI_Trans_Bytes((INT8U *)SCIRV,43);
    } 
    if(SCIRV[2] == 3)   //水温
    {
      SCIRV[0] = 0xeb;
      SCIRV[1] = 0x90;
      SCIRV[2] = 0x55;
      SCIRV[3] = 0x03;
      SCIRV[4] = 0x02;//上传
      SCIRV[5] = 0x00; 
      SCIRV[6] = 0x21;

      App_CopyRam((INT8U *)&SCIRV[7],(INT8U *)&TempMeterParamer.bVirtualVal,33);
           
      SCIRV[40] = App_CreateCheckSun((INT8U*)&SCIRV[7],SCIRV[2],33);
      SCIRV[41] = 0x55;
      SCIRV[42] = 0xaa;
      Api_SCI_Trans_Bytes((INT8U *)SCIRV,43);
    }  
    if(SCIRV[2] == 4)   //燃油
    {
      SCIRV[0] = 0xeb;
      SCIRV[1] = 0x90;
      SCIRV[2] = 0x55;
      SCIRV[3] = 0x04;
      SCIRV[4] = 0x02;//上传
      SCIRV[5] = 0x00; 
      SCIRV[6] = 0x21;

      App_CopyRam((INT8U *)&SCIRV[7],(INT8U *)&FuelMeterParamer.bVirtualVal,33);
           
      SCIRV[40] = App_CreateCheckSun((INT8U*)&SCIRV[7],SCIRV[2],33);
      SCIRV[41] = 0x55;
      SCIRV[42] = 0xaa;
      Api_SCI_Trans_Bytes((INT8U *)SCIRV,43);
    } 
    if(SCIRV[2] == 11)   //速比
    {
      SCIRV[0] = 0xeb;
      SCIRV[1] = 0x90;
      SCIRV[2] = 0x55;
      SCIRV[3] = 0x0b;
      SCIRV[4] = 0x02;//上传
      SCIRV[5] = 0x00; 
      SCIRV[6] = 0x21;

      App_CopyRam((INT8U *)&SCIRV[7],(INT8U *)&Radio.VehRadio,6);
           
      SCIRV[40] = App_CreateCheckSun((INT8U*)&SCIRV[7],SCIRV[2],33);
      SCIRV[41] = 0x55;
      SCIRV[42] = 0xaa;
      Api_SCI_Trans_Bytes((INT8U *)SCIRV,43);
    } 
    if(SCIRV[2] == 12)   //电压
    {
      SCIRV[0] = 0xeb;
      SCIRV[1] = 0x90;
      SCIRV[2] = 0x55;
      SCIRV[3] = 0x0c;
      SCIRV[4] = 0x02;//上传
      SCIRV[5] = 0x00; 
      SCIRV[6] = 0x21;
      //Veh
      SCIRV[7] = 0;//ADEveArry.TmpEven
      SCIRV[8] = 0;
      //Rev
      SCIRV[9] = 0;
      SCIRV[10] = 0;
      //tmp
      SCIRV[11] = (INT8U)(TempMeter.CurBoardVal>>8);
      SCIRV[12] = (INT8U)TempMeter.CurBoardVal;
      //fuel
      SCIRV[13] = (INT8U)(FuelMeter.CurBoardVal>>8);
      SCIRV[14] = (INT8U)FuelMeter.CurBoardVal;
                                                            
      SCIRV[40] = App_CreateCheckSun((INT8U*)&SCIRV[7],SCIRV[2],33);
      SCIRV[41] = 0x55;
      SCIRV[42] = 0xaa;
      Api_SCI_Trans_Bytes((INT8U *)SCIRV,43);
    }                    
    if(SciFrame.Dir == DownLoad)
    {
      switch(SciFrame.Frame_Type)
      {
        case VehStepType:
          VehMeter.DestPosition = clear;
          VehMeter.DestPosition = SciFrame.Frame_Data[0];
          VehMeter.DestPosition += SciFrame.Frame_Data[1]*256;
          break;
                    
        case RevStepType:
          RevMeter.DestPosition = clear;
          RevMeter.DestPosition = SciFrame.Frame_Data[0];
          RevMeter.DestPosition += SciFrame.Frame_Data[1]*256;
          break;
                    
        case TmpStepType:
          TempMeter.DestPosition = clear;
          TempMeter.DestPosition = SciFrame.Frame_Data[0];
          TempMeter.DestPosition += SciFrame.Frame_Data[1]*256;
          break;
                    
        case FuelStepType:
          FuelMeter.DestPosition = clear;
          FuelMeter.DestPosition = SciFrame.Frame_Data[0];
          FuelMeter.DestPosition += SciFrame.Frame_Data[1]*256;
          break;
                    
        case VehType:                
          check = App_CreateCheckSun((INT8U*)SciFrame.Frame_Data,SciFrame.Frame_Check,33);
          if(check == SciFrame.Frame_CheckSun)
          {
            VehMeterParamer.bFlag = SciFrame.Frame_Check;
            VehMeterParamer.bVirtualVal = SciFrame.Frame_Data[0];
            App_CopyRam((INT8U *)VehMeterParamer.Resistor,(INT8U *)&SciFrame.Frame_Data[1],32);
            VehMeterParamer.bCheckSun = App_CreateCheckSun((INT8U*)&VehMeterParamer.bVirtualVal,VehMeterParamer.bFlag,33);
            SCIRV[0] = 0xeb;
            SCIRV[1] = 0x90;
            SCIRV[2] = 0x01;
            SCIRV[3] = 0x55;
            SCIRV[4] = 0xaa;
            Api_SCI_Trans_Bytes((INT8U *)SCIRV,5);
          }                  	                    
          break; 
        case RevType:                
          check = App_CreateCheckSun((INT8U*)SciFrame.Frame_Data,SciFrame.Frame_Check,33);
          if(check == SciFrame.Frame_CheckSun)
          {
            RevMeterParamer.bFlag = SciFrame.Frame_Check;
            RevMeterParamer.bVirtualVal = SciFrame.Frame_Data[0];
            App_CopyRam((INT8U *)RevMeterParamer.Resistor,(INT8U *)&SciFrame.Frame_Data[1],32);
            RevMeterParamer.bCheckSun = App_CreateCheckSun((INT8U*)&RevMeterParamer.bVirtualVal,RevMeterParamer.bFlag,33);
            SCIRV[0] = 0xeb;
            SCIRV[1] = 0x90;
            SCIRV[2] = 0x02;
            SCIRV[3] = 0x55;
            SCIRV[4] = 0xaa;
            Api_SCI_Trans_Bytes((INT8U *)SCIRV,5);
          }                  	                    
          break;  
        case TmpType:                
          check = App_CreateCheckSun((INT8U*)SciFrame.Frame_Data,SciFrame.Frame_Check,33);
          if(check == SciFrame.Frame_CheckSun)
          {
            TempMeterParamer.bFlag = SciFrame.Frame_Check;
            TempMeterParamer.bVirtualVal = SciFrame.Frame_Data[0];
            App_CopyRam((INT8U *)TempMeterParamer.Resistor,(INT8U *)&SciFrame.Frame_Data[1],32);
            TempMeterParamer.bCheckSun = App_CreateCheckSun((INT8U*)&TempMeterParamer.bVirtualVal,TempMeterParamer.bFlag,33);
            SCIRV[0] = 0xeb;
            SCIRV[1] = 0x90;
            SCIRV[2] = 0x03;
            SCIRV[3] = 0x55;
            SCIRV[4] = 0xaa;
            Api_SCI_Trans_Bytes((INT8U *)SCIRV,5);
          }                  	                    
          break; 
        case FueType:                
          check = App_CreateCheckSun((INT8U*)SciFrame.Frame_Data,SciFrame.Frame_Check,33);
          if(check == SciFrame.Frame_CheckSun)
          {
            FuelMeterParamer.bFlag = SciFrame.Frame_Check;
            FuelMeterParamer.bVirtualVal = SciFrame.Frame_Data[0];
            App_CopyRam((INT8U *)FuelMeterParamer.Resistor,(INT8U *)&SciFrame.Frame_Data[1],32);
            FuelMeterParamer.bCheckSun = App_CreateCheckSun((INT8U*)&FuelMeterParamer.bVirtualVal,FuelMeterParamer.bFlag,33);
            SCIRV[0] = 0xeb;
            SCIRV[1] = 0x90;
            SCIRV[2] = 0x04;
            SCIRV[3] = 0x55;
            SCIRV[4] = 0xaa;
            Api_SCI_Trans_Bytes((INT8U *)SCIRV,5);
          }                  	                    
          break;  
        case RadioType:                
          check = App_CreateCheckSun((INT8U*)SciFrame.Frame_Data,SciFrame.Frame_Check,33);
          if(check == SciFrame.Frame_CheckSun)
          {
            Radio.bFlag = SciFrame.Frame_Check;
            App_CopyRam((INT8U *)&Radio.VehRadio,(INT8U *)&SciFrame.Frame_Data[0],2);
            App_CopyRam((INT8U *)&Radio.VehNum,(INT8U *)&SciFrame.Frame_Data[2],2);
            App_CopyRam((INT8U *)&Radio.RevRadio,(INT8U *)&SciFrame.Frame_Data[4],2);
                  	    
            Radio.bCheckSun = App_CreateCheckSun((INT8U*)&Radio.VehRadio,Radio.bFlag,6);
                  	    
            SCIRV[0] = 0xeb;
            SCIRV[1] = 0x90;
            SCIRV[2] = 0x0b;
            SCIRV[3] = 0x55;
            SCIRV[4] = 0xaa;
            Api_SCI_Trans_Bytes((INT8U *)SCIRV,5);
            _FEED_COP();
            bReturn = Api_DFlashEraseSector(DFLASH_VEH_ADDR);            
            bReturn = Api_DFalshProgram(DFLASH_VEH_ADDR,PAGE_LEN,(INT16U*)&VehMeterParamer);
                        
            bReturn = Api_DFlashEraseSector(DFLASH_REV_ADDR);  
            bReturn = Api_DFalshProgram(DFLASH_REV_ADDR,PAGE_LEN,(INT16U*)&RevMeterParamer);   
                        
            bReturn = Api_DFlashEraseSector(DFLASH_TMP_ADDR);                 	    
            bReturn = Api_DFalshProgram(DFLASH_TMP_ADDR,PAGE_LEN,(INT16U*)&TempMeterParamer);
                        
            bReturn = Api_DFlashEraseSector(DFLASH_FUEL_ADDR);  
            bReturn = Api_DFalshProgram(DFLASH_FUEL_ADDR,PAGE_LEN,(INT16U*)&FuelMeterParamer);
                        
            bReturn = Api_DFlashEraseSector(DFLASH_RADIO_ADDR);   
            bReturn = Api_DFalshProgram(DFLASH_RADIO_ADDR,PAGE_LEN,(INT16U*)&Radio);
          }                  	                    
          break;  
        case AccType:                
          check = App_CreateCheckSun((INT8U*)SciFrame.Frame_Data,SciFrame.Frame_Check,33);
          if(check == SciFrame.Frame_CheckSun)
          {
            AccKm.ClrCnt = null;
            App_ClearAccStock();
          }                  	                    
          break;                                                                              
        default:
          ;
      }
    }
    SciFrame.Frame_Head[0] = null;
    SCIRV[0] = null;
    SCIRV[1] = null; 
    SCIRV[41] = null;
    SCIRV[42] = null;               
  }
}
/*-------------------------------------------------------------------------
* Function Name  : App_SCI_Proc
* Description    : 串口数据处理
* Input          : None
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
INT8U App_CreateCheckSun(INT8U *pData,INT8U checkbyte,INT8U len)
{
  	INT8U Check=0,i;
  	Check = 0xff ^ checkbyte;
  	for(i=0;i<len;i++)
  	{
  		Check = Check ^ pData[i];
  	}
  	return Check;
}
