#include "SL_Sc7a20_Driver.h"
#include "string.h"

Std_ReturnType Gsensor_SPI_Read(uint8 Addr,uint8 *DataBufferPtr,uint16 Length)
{
	Std_ReturnType spiSendStatus;
	Spi_DataBufferType txBuffer[Length+1];
	Spi_DataBufferType rxBuffer[Length+1];

	memset(txBuffer,0,sizeof(txBuffer));
	memset(rxBuffer,0,sizeof(rxBuffer));
	if(Length == 1)
	{
		txBuffer[0] = Addr | 0x80;
	}
	else if(Length > 1)
	{
		txBuffer[0] = Addr | 0x80 | 0x40;
	}
	else
	{
		return E_NOT_OK;
	}

	Spi_SetupEB(0,txBuffer,rxBuffer,Length+1);
	spiSendStatus = Spi_SyncTransmit(0);

	if(spiSendStatus == E_OK)
	{
		memcpy(DataBufferPtr,&rxBuffer[1],Length);
		return E_OK;
	}
	else
	{
		return E_NOT_OK;
	}
}

Std_ReturnType Gsensor_SPI_Write(uint8 Addr,uint8 *DataBufferPtr,uint16 Length)
{
	Std_ReturnType spiSendStatus;
	Spi_DataBufferType txBuffer[Length+1];
	memset(txBuffer,0,sizeof(txBuffer));
	if(Length == 1)
	{
		txBuffer[0] = Addr;
	}
	else if(Length > 1)
	{
		txBuffer[0] = Addr | 0x40;
	}
	else
	{
		return E_NOT_OK;
	}
	memcpy(&txBuffer[1],DataBufferPtr,Length);

	Spi_SetupEB(0,txBuffer,NULL,Length+1);
	spiSendStatus = Spi_SyncTransmit(0);

	if(spiSendStatus == E_OK)
	{
		return E_OK;
	}
	else
	{
		return E_NOT_OK;
	}
}


/***************在线测试**********************/
Std_ReturnType  SL_SC7A20_Online_Test(void)
{
	uint8 SL_Read_Reg[1]= {0xff};
    Gsensor_SPI_Read(SC7A20_CHIP_ID_ADDRESS,SL_Read_Reg,1);
    if(SL_Read_Reg[0]==SC7A20_CHIP_ID_VALUE)
    	return  E_OK;
    else
    	return E_NOT_OK;
}

/***************BOOT 重载内部寄存器值*********************/
Std_ReturnType  SL_SC7A20_BOOT(void)
{
	uint8 SL_Read_Reg[1]={0xff};
	uint8 SL_Write_Reg[1];
    Gsensor_SPI_Read(SL_SC7A20_CTRL_REG5,SL_Read_Reg,1);
    SL_Write_Reg[0] = SL_SC7A20_BOOT_ENABLE | SL_Read_Reg[0];
    Gsensor_SPI_Write(SL_SC7A20_CTRL_REG5, SL_Write_Reg,1);
    return  E_OK;
}

Std_ReturnType  SL_SC7A20_INT_Config(void)
{
	uint8 SL_Read_Reg[1]={0xff};
	uint8 SL_Write_Reg[1];

    /*******************AOI1  IN  INT1********************/
	SL_Write_Reg[0]=0x08;//AOI1 LATCH
    Gsensor_SPI_Write(SL_SC7A20_CTRL_REG5, SL_Write_Reg,1);

    SL_Write_Reg[0]=SL_SC7A20_INT_ACTIVE_HIGH_LEVEL;
    //interrupt happen,int pin output lower level
    Gsensor_SPI_Write(SL_SC7A20_CTRL_REG6, SL_Write_Reg,1);

    //AOI1 CONFIG  只开高中断
    SL_Write_Reg[0]=0x00;            //0x7F
#if  SL_6D4D2D1D_SEL== 1
    SL_Write_Reg[0]=SL_Write_Reg[0]|0x02;//X
#elif SL_6D4D2D1D_SEL== 2
    SL_Write_Reg[0]=SL_Write_Reg[0]|0x02;//X
    SL_Write_Reg[0]=SL_Write_Reg[0]|0x04;//Yֵ
#elif SL_6D4D2D1D_SEL== 4
    SL_Write_Reg[0]=SL_Write_Reg[0]|0x03;//X
    SL_Write_Reg[0]=SL_Write_Reg[0]|0x0C;//Y
#elif  SL_6D4D2D1D_SEL== 6
    SL_Write_Reg[0]=SL_Write_Reg[0]|0x02;//Xֵ
    SL_Write_Reg[0]=SL_Write_Reg[0]|0x08;//Yֵ
    SL_Write_Reg[0]=SL_Write_Reg[0]|0x20;//Z
#endif

    Gsensor_SPI_Write(SL_SC7A20_INT1_CFG, SL_Write_Reg,1);

    //HPF SET
  //  Gsensor_SPI_Read(SL_SC7A20_CTRL_REG2,1, &SL_Read_Reg);
    SL_Write_Reg[0]=0x81;//SL_Read_Reg|0xCF;//Normal HP , HPF TO AOI1
    Gsensor_SPI_Write(SL_SC7A20_CTRL_REG2, SL_Write_Reg,1);

#if SL_6D4D2D1D_SEL== 6
    SL_Write_Reg[0] = SL_SC7A20_INT_THS_20PERCENT;
	Gsensor_SPI_Write(SL_SC7A20_INT1_THS, SL_Write_Reg,1);

	SL_Write_Reg[0] = SL_SC7A20_INT_DURATION_10CLK;
	Gsensor_SPI_Write(SL_SC7A20_INT1_DURATION, SL_Write_Reg, 1);
#else
    SL_Write_Reg[0] = SL_SC7A20_INT_THS_10PERCENT;
	Gsensor_SPI_Write(SL_SC7A20_INT1_THS, SL_Write_Reg,1);

	SL_Write_Reg[0] = SL_SC7A20_INT_DURATION_2CLK;
	Gsensor_SPI_Write(SL_SC7A20_INT1_DURATION, SL_Write_Reg, 1);
#endif


    //AOI1 TO INT1
   // Gsensor_SPI_Read(SL_SC7A20_CTRL_REG3,1, &SL_Read_Reg);
    SL_Write_Reg[0]=0x40;//SL_Read_Reg|0x40; //AOI1 TO INT1
    Gsensor_SPI_Write(SL_SC7A20_CTRL_REG3, SL_Write_Reg,1);
    /*******************AOI1  IN  INT1********************/


    /*******************AOI2  IN  INT2********************/
    Gsensor_SPI_Read(SL_SC7A20_CTRL_REG5,SL_Read_Reg, 1);
    SL_Write_Reg[0]=SL_Read_Reg[0]|0x02;//AOI2 LATCH
    Gsensor_SPI_Write(SL_SC7A20_CTRL_REG5, SL_Write_Reg,1);


    //AOI2 CONFIG
    SL_Write_Reg[0]=0x00|0x0C;            //0xFF

#if  SL_6D4D2D1D_SEL== 1
    SL_Write_Reg[0]=SL_Write_Reg[0]|0x02;//X
#elif SL_6D4D2D1D_SEL== 2
    SL_Write_Reg[0]=SL_Write_Reg[0]|0x02;//X
    SL_Write_Reg[0]=SL_Write_Reg[0]|0x04;//Y
#elif SL_6D4D2D1D_SEL== 4
    SL_Write_Reg[0]=SL_Write_Reg[0]|0x03;//Xֵ
    SL_Write_Reg[0]=SL_Write_Reg[0]|0x0C;//Y
#elif  SL_6D4D2D1D_SEL== 6
    SL_Write_Reg[0]=SL_Write_Reg[0]|0x03;//Xֵ
    SL_Write_Reg[0]=SL_Write_Reg[0]|0x0C;//Y
    SL_Write_Reg[0]=SL_Write_Reg[0]|0x30;//Z
#endif
    Gsensor_SPI_Write(SL_SC7A20_INT2_CFG, SL_Write_Reg,1);

    //HPF SET
#if SL_6D4D2D1D_SEL== 6
    SL_Read_Reg[0]= 0x00;
    Gsensor_SPI_Read(SL_SC7A20_CTRL_REG2,SL_Read_Reg,1);
#endif
   //SL_Read_Reg=SL_Read_Reg&0xFD;//NO HPF TO AOI2
   // Gsensor_SPI_Write(SL_SC7A20_CTRL_REG2, SL_Read_Reg);



#if SL_6D4D2D1D_SEL== 6
    SL_Write_Reg[0] = SL_SC7A20_INT_THS_20PERCENT;
#else
    SL_Write_Reg[0] = SL_SC7A20_INT_THS_10PERCENT;
#endif
    Gsensor_SPI_Write(SL_SC7A20_INT2_THS,SL_Write_Reg,1 );

    SL_Write_Reg[0] = SL_SC7A20_INT_DURATION_10CLK;
    Gsensor_SPI_Write(SL_SC7A20_INT2_DURATION, SL_Write_Reg,1);

    //AOI2 TO INT2
    memset(SL_Read_Reg,0xFF,1);
    Gsensor_SPI_Read(SL_SC7A20_CTRL_REG6,SL_Read_Reg,1);
    SL_Write_Reg[0]=SL_Read_Reg[0]|0x20; //AOI2 TO INT2
    Gsensor_SPI_Write(SL_SC7A20_CTRL_REG6, SL_Write_Reg,1);
    /*******************AOI2  IN  INT2********************/

    return  E_OK;
}



//retrun_value&0x20=0x20  ZH
//retrun_value&0x20=0x10  ZL
//retrun_value&0x20=0x08  YH
//retrun_value&0x20=0x04  YL
//retrun_value&0x20=0x02  XH
//retrun_value&0x20=0x01  XL
Std_ReturnType  SL_SC7A20_INT_RESET(void)
{

	uint8 SL_Write_Reg[1] = {0x00};
    uint8 SL_Read_Reg1[1] = {0xFF};
    uint8 SL_Read_Reg2[1] = {0xFF};



    //SL_Read_Reg1 display the int1 type
    Gsensor_SPI_Read(SL_SC7A20_INT1_SRC,SL_Read_Reg1,1);
    //SL_Read_Reg2 display the int2 type
    Gsensor_SPI_Read(SL_SC7A20_INT2_SRC,SL_Read_Reg2,1);

    //close INT1
    SL_Write_Reg[0] = 0x00;
    Gsensor_SPI_Write(SL_SC7A20_INT1_CFG, SL_Write_Reg, 1);
    //close INT2
    Gsensor_SPI_Write(SL_SC7A20_INT2_CFG, SL_Write_Reg ,1);

    if((SL_Read_Reg2[0]&0x02)==0x02)
    {
        //X+
    }
    if((SL_Read_Reg2[0]&0x01)==0x01)
    {
        //X-
    }
    if((SL_Read_Reg2[0]&0x08)==0x08)
    {
        //Y+
    }
    if((SL_Read_Reg2[0]&0x04)==0x04)
    {
        //Y-
    }
    if((SL_Read_Reg2[0]&0x20)==0x20)
    {
        //Z+
    }
    if((SL_Read_Reg2[0]&0x10)==0x10)
    {
        //Z-
    }

    if(SL_Read_Reg1[0]!=0||SL_Read_Reg2[0]!=0)
    {
        return E_OK;
    }
    else
    {
        return E_NOT_OK;
    }

}


void  SL_SC7A20_AOI1_event(void)
{

    uint8 SL_SRC_Reg1[1] = {0xFF};
    uint8 SL_THS_Reg1[1] = {0xFF};
    uint8 SL_DURATION_Reg1 = {0xFF};


    //SL_Read_Reg1 display the int1 type
    Gsensor_SPI_Read(SL_SC7A20_INT1_SRC,SL_SRC_Reg1,1);
    Gsensor_SPI_Read(SL_SC7A20_INT1_THS,SL_THS_Reg1,1);
    Gsensor_SPI_Read(SL_SC7A20_INT1_DURATION,SL_DURATION_Reg1,1);

//    printf("SL_SC7A20_INT1_SRC=%#x\r\n",SL_SRC_Reg1);
 //   if((SL_SRC_Reg1&0x40)==0x40){

	    if((SL_SRC_Reg1[0]&0x01)==0x01)
	    {
	        //X-
//		    printf("X1-\r\n");
	    }
	    if((SL_SRC_Reg1[0]&0x02)==0x02)
	    {
	        //X+
//		    printf("X1+\r\n");
	    }
	    if((SL_SRC_Reg1[0]&0x04)==0x04)
	    {
	        //Y-
//		    printf("Y1-\r\n");
	    }
	    if((SL_SRC_Reg1[0]&0x08)==0x08)
	    {
	        //Y+
//		    printf("Y1+\r\n");
	    }
	    if((SL_SRC_Reg1[0]&0x10)==0x10)
	    {
	        //Z-
//		    printf("Z1-\r\n");
	    }
	    if((SL_SRC_Reg1[0]&0x20)==0x20)
	    {
	        //Z+
//		    printf("Z1+\r\n");
	    }
//    }

}


void  SL_SC7A20_AOI2_event(void)
{

    uint8 SL_SRC_Reg2[1] = {0xFF};
    uint8 SL_THS_Reg2 = {0xFF};
    uint8 SL_DURATION_Reg2 = {0xFF};


    //SL_Read_Reg1 display the int1 type
    Gsensor_SPI_Read(SL_SC7A20_INT2_SRC,SL_SRC_Reg2,1);
    Gsensor_SPI_Read(SL_SC7A20_INT2_THS,SL_THS_Reg2,1);
    Gsensor_SPI_Read(SL_SC7A20_INT2_DURATION,SL_DURATION_Reg2,1);

//    printf("SL_SC7A20_INT2_SRC=%#x\r\n",SL_SRC_Reg2);
  //  if((SL_SRC_Reg2&0x40)==0x40){

	    if((SL_SRC_Reg2[0]&0x01)==0x01)
	    {
	        //X-
//		    printf("X2-\r\n");
	    }
	    if((SL_SRC_Reg2[0]&0x02)==0x02)
	    {
	        //X+
//		    printf("X2+\r\n");
	    }
	    if((SL_SRC_Reg2[0]&0x04)==0x04)
	    {
	        //Y-
//		    printf("Y2-\r\n");
	    }
	    if((SL_SRC_Reg2[0]&0x08)==0x08)
	    {
	        //Y+
//		    printf("Y2+\r\n");
	    }
	    if((SL_SRC_Reg2[0]&0x10)==0x10)
	    {
	        //Z-
//		    printf("Z2-\r\n");
	    }
	    if((SL_SRC_Reg2[0]&0x20)==0x20)
	    {
	        //Z+
//		    printf("Z2+\r\n");
	    }
 //   }

}


void  SL_SC7A20_Reg_read_all(void)
{
    uint8 SL_Read_Reg[32];
    memset(SL_Read_Reg,0xFF,32);
	uint8 adr = 0x20;
	for(adr=0x20;adr<=0x3F;adr++)
	{
		Gsensor_SPI_Read(adr,&SL_Read_Reg[adr-0x20],1);
	}
	return;
}


/***************传感器量程设置**********************/
Std_ReturnType  SL_SC7A20_FS_Config(uint8 Sc7a20_FS_Reg)
{
	uint8 SL_Read_Reg[1]={0xff};
	uint8 SL_Write_Reg[1];

    Gsensor_SPI_Read(SL_SC7A20_CTRL_REG4,SL_Read_Reg,1);
    SL_Write_Reg[0]=0x80|Sc7a20_FS_Reg|SL_SC7A20_HR_ENABLE;

    Gsensor_SPI_Write(SL_SC7A20_CTRL_REG4, SL_Write_Reg,1);
    Gsensor_SPI_Read(SL_SC7A20_CTRL_REG4,SL_Read_Reg,1);
    if(SL_Read_Reg[0]==SL_Write_Reg[0])
    {
    	return  E_OK;
    }
    else
    {
    	return E_NOT_OK;
    }
}

/***************数据更新速率**加速度计使能**********/
Std_ReturnType  SL_SC7A20_Power_Config(uint8 Power_Config_Reg)
{
	uint8 SL_Read_Reg[1]={0xff};
	uint8 SL_Write_Reg[1];

#if  SL_SC7A20_MTP_ENABLE == 0X01
    SL_Read_Reg  = 0x00;
    Gsensor_SPI_Write(SL_SC7A20_SPI_IIC_MODE,SL_SC7A20_MTP_CFG, SL_SC7A20_MTP_VALUE);
    Gsensor_SPI_Read(SL_SC7A20_SPI_IIC_MODE,SL_SC7A20_SDOI2C_PU_CFG,1, &SL_Read_Reg);
    SL_Read_Reg=SL_Read_Reg|SL_SC7A20_SDO_PU_MSK|SL_SC7A20_I2C_PU_MSK;
    Gsensor_SPI_Write(SL_SC7A20_SPI_IIC_MODE,SL_SC7A20_SDOI2C_PU_CFG, SL_Read_Reg);
#endif
    SL_Write_Reg[0] = Power_Config_Reg;
    Gsensor_SPI_Write(SL_SC7A20_CTRL_REG1, SL_Write_Reg,1);
    Gsensor_SPI_Read(SL_SC7A20_CTRL_REG1,SL_Read_Reg,1);

    if(SL_Read_Reg[0]==Power_Config_Reg)
    	return  E_OK;
    else
    	return E_NOT_OK;
}


/***************加速度计数据读取*16bits*********/
Std_ReturnType  SL_SC7A20_Read_XYZ_Data(sint16 *SL_SC7A20_Data_XYZ_Buf)
{
    uint8 SL_Read_Buf[7];
    
    Gsensor_SPI_Read(SL_SC7A20_STATUS_REG,&SL_Read_Buf[0],1);
    
    if((SL_Read_Buf[0]&0x0f)==0x0f)
    {

        Gsensor_SPI_Read(SL_SC7A20_DATA_OUT,&SL_Read_Buf[1],6);
        SL_SC7A20_Data_XYZ_Buf[0]=(sint16)((SL_Read_Buf[2]<<8) + SL_Read_Buf[1]);
        SL_SC7A20_Data_XYZ_Buf[1]=(sint16)((SL_Read_Buf[4]<<8) + SL_Read_Buf[3]);
        SL_SC7A20_Data_XYZ_Buf[2]=(sint16)((SL_Read_Buf[6]<<8) + SL_Read_Buf[5]);
        SL_SC7A20_Data_XYZ_Buf[0] = SL_SC7A20_Data_XYZ_Buf[0] >>4;
        SL_SC7A20_Data_XYZ_Buf[1] = SL_SC7A20_Data_XYZ_Buf[1] >>4;
        SL_SC7A20_Data_XYZ_Buf[2] = SL_SC7A20_Data_XYZ_Buf[2] >>4;
        SL_SC7A20_Data_XYZ_Buf[0] = SL_SC7A20_Data_XYZ_Buf[0] * 2;
		SL_SC7A20_Data_XYZ_Buf[1] = SL_SC7A20_Data_XYZ_Buf[1] * 2;
		SL_SC7A20_Data_XYZ_Buf[2] = SL_SC7A20_Data_XYZ_Buf[2] * 2;
        return  E_OK;

    }
    else
    {
    	return E_NOT_OK;
    }
}

Std_ReturnType GsensorInit(void)
{
	Std_ReturnType Status = E_NOT_OK;
	Status = SL_SC7A20_Online_Test();
	if(Status==E_OK)
	{

		SL_SC7A20_BOOT();
		SL_SC7A20_FS_Config(SL_SC7A20_FS_4G);
		SL_SC7A20_INT_Config();
		//SL_SC7A20_INT_RESET();
		SL_SC7A20_Power_Config(SL_SC7A20_ODR_400HZ);
	}
	return Status;
}
