#include "hal_sc7a20.h"
#include "hc32_ll_gpio.h"

//pb10 --sda pb15 --sck
 

#define READ_SDA3   GPIO_ReadInputPins(GPIO_PORT_B,GPIO_PIN_10)

#define IIC_SDA3(n) (n?GPIO_SetPins(GPIO_PORT_B, GPIO_PIN_10):GPIO_ResetPins(GPIO_PORT_B, GPIO_PIN_10))
#define IIC_SCL3(n) (n?GPIO_SetPins(GPIO_PORT_B, GPIO_PIN_15):GPIO_ResetPins(GPIO_PORT_B, GPIO_PIN_15))

 
 
//需要示波器调1us的时间
static void delay_us(uint16_t time)
{
	uint16_t i;

	for(i=time;i>0;i--)
	{
		__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
		__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();		
		__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
		__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
		__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
		__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
		__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
		__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
		__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
		__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
		__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
		__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
		__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
		__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
		__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
	  __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
		__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
		__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
	}
}

void iic3_init(void)
{
	stc_gpio_init_t  pstcGpioInit1;
	GPIO_StructInit(&pstcGpioInit1);
	
	pstcGpioInit1.u16PinDir = PIN_DIR_OUT;
	pstcGpioInit1.u16PinState = PIN_STAT_RST;
	GPIO_Init(GPIO_PORT_B, GPIO_PIN_10,&pstcGpioInit1); 
	GPIO_Init(GPIO_PORT_B, GPIO_PIN_15,&pstcGpioInit1); 
	
	GPIO_SetPins(GPIO_PORT_B, GPIO_PIN_10);
	GPIO_SetPins(GPIO_PORT_B, GPIO_PIN_15);
}


void sc7a20_init(void)
{
	iic3_init();
}

static void SDA_IN3(void)
{
	 stc_gpio_init_t pstcGpioInit3;//iic的引脚
	 pstcGpioInit3.u16PinDir = PIN_DIR_IN;
	 GPIO_Init(GPIO_PORT_B, GPIO_PIN_10,&pstcGpioInit3);
}

static void SDA_OUT3(void)  
{
	stc_gpio_init_t pstcGpioInit3;
	pstcGpioInit3.u16PinDir = PIN_DIR_OUT;
	pstcGpioInit3.u16PinOutputType=PIN_OUT_TYPE_NMOS;
	GPIO_Init(GPIO_PORT_B, GPIO_PIN_10,&pstcGpioInit3);
}	



 
static void iic_start(void)
{
	SDA_OUT3();
	
	IIC_SDA3(1);
	IIC_SCL3(1);
	delay_us(5);
	
	IIC_SDA3(0);
	delay_us(5);
	IIC_SCL3(0);
}


static void iic_stop(void)
{
	SDA_OUT3();
	
	IIC_SCL3(0);
	IIC_SDA3(0);
	delay_us(5);
	
	IIC_SCL3(1);
	IIC_SDA3(1);
	delay_us(5);
}

static void iic_ack(void)
{
	SDA_OUT3();
	
	IIC_SCL3(0);
	IIC_SDA3(0);
	delay_us(5);
	
	IIC_SCL3(1);
	delay_us(5);
	IIC_SCL3(0);
}

static void iic_nack(void)
{
	SDA_OUT3();
	
	IIC_SCL3(0);
	IIC_SDA3(1);
	delay_us(5);
	
	IIC_SCL3(1);
	delay_us(5);
	IIC_SCL3(0);
}
 

static uint8_t iic_wait_ack(void)
{
	uint16_t t =200;
	SDA_OUT3();
	
	IIC_SDA3(1);delay_us(1);
	IIC_SCL3(1);delay_us(1);

	SDA_IN3();

	while(READ_SDA3) 
	{
		t--;
		
		if(t==0)
		{
			iic_stop();
			return 1;
		}
		delay_us(1);
	}
	IIC_SCL3(0);
		
	return 0;
}


static void iic_send_byte(uint8_t byte)
{
  uint8_t bit_cnt;
	SDA_OUT3();
	IIC_SCL3(0);
	 
	for(bit_cnt=0;bit_cnt<8;bit_cnt++)
	{	 
		if(byte&0x80)	
			IIC_SDA3(1); 
		else	
			IIC_SDA3(0);
		
		byte<<=1;
		delay_us(5);
		IIC_SCL3(1);
		delay_us(5);
		IIC_SCL3(0);
	}
}
 

static uint8_t iic_recive_byte(unsigned char ack)
{
  uint8_t retc = 0;
  uint8_t bit_cnt;
	
	SDA_IN3(); 
	delay_us(5);
	
	for(bit_cnt=0;bit_cnt<8;bit_cnt++)
	{
		IIC_SCL3(0);
		delay_us(5);
		IIC_SCL3(1);
		
		retc=retc<<1;
		
		if(READ_SDA3) 
			retc++;
		
		delay_us(5);
	}
 
	return(retc);
}



static void sc7a20_write_byte(uint8_t RAddr, uint8_t WData)
{
  iic_start();								 		 
  iic_send_byte(Sensor_Wr_Addr1); 
  iic_wait_ack();							 		 	
  iic_send_byte(RAddr);   		 		 
  iic_wait_ack(); 	 					 		 				  		   
  iic_send_byte(WData);      		 				   
  iic_wait_ack();  		    	   		 		 
  iic_stop();									 		 
}

static void sc7a20_read_byte(uint8_t RAddr, uint8_t *RData)
{
  iic_start();								 		 
  iic_send_byte(Sensor_Wr_Addr1); 
  iic_wait_ack();  						 		 
  iic_send_byte(RAddr);   		 		 
  iic_wait_ack();	    				 		 
  iic_start();  	 	   				 		 
  iic_send_byte(Sensor_Rd_Addr1);    
  iic_wait_ack();	 						 		 	
  *RData = iic_recive_byte(0);	 		    
  iic_stop();									 		 	
}

/*SC7A20获取加速度值 
accd_lsb：加速度值低寄存器地址 
accd_msb：加速度值高寄存器地址 
返回值accd_f：加速度值，单位mg */
int SC7A20_Get_ACCD(uint8_t accd_lsb,uint8_t accd_msb) 
{
	uint8_t accd_l,accd_m = 0; 
	unsigned int temp; 
	int accd; 
	sc7a20_read_byte(accd_lsb,&accd_l); 
	sc7a20_read_byte(accd_msb,&accd_m); 
	temp &= 0x0000; 
	temp |= accd_m; 
	temp <<= 8; 
	temp &= 0xff00; 
	temp |= accd_l; 
	if(temp&0x8000) 
	{
		temp >>= 4; 
		temp |= 0xf000; 
	}
	else 
	{
		temp >>= 4; 
		temp &= 0x0fff; 
	}
	accd = temp*1; 
	return accd; 
}



 
uint8_t read_chip_id(void)
{
	uint8_t chip_id;
	sc7a20_read_byte(0x0f,&chip_id);
//	printf("chip_id=0x%x\r\n",chip_id);

	return chip_id;
	
}
void SC7A20_BOOT(void)
{
	uint8_t ret;
	sc7a20_read_byte(0x24,&ret);
	sc7a20_write_byte(0x24,0x80/*|ret*/); //boot
}




int8_t  SC7A20_INT_Config(void)
{
    uint8_t SL_Read_Reg;

    /*******************AOI1  IN  INT1********************/
    SL_Read_Reg=0x08;//AOI1 LATCH
    sc7a20_write_byte(SL_SC7A20_CTRL_REG5, SL_Read_Reg); 
    
    SL_Read_Reg=SL_SC7A20_INT_ACTIVE_HIGH_LEVEL;
    //interrupt happen,int pin output lower level
    sc7a20_write_byte(SL_SC7A20_CTRL_REG6, SL_Read_Reg);  
    
    //AOI1 CONFIG
    SL_Read_Reg=0x00;            //0x7F
    SL_Read_Reg=SL_Read_Reg|0x40;//閿熸枻鎷烽敓鏂ゆ嫹閿熷壙璁规嫹璇嗛敓鏂ゆ嫹妯″紡
    SL_Read_Reg=SL_Read_Reg|0x03;//X 閿熸枻鎷峰彥閿熸枻鎷峰綍閿燂拷 X閿熸枻鎷烽敓鏂ゆ嫹閿熸嵎鐨勬拝鎷烽敓鏂ゆ嫹鍊奸敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹鍊�
    SL_Read_Reg=SL_Read_Reg|0x0C;//Y 閿熸枻鎷峰彥閿熸枻鎷峰綍閿燂拷 Y閿熸枻鎷烽敓鏂ゆ嫹閿熸嵎鐨勬拝鎷烽敓鏂ゆ嫹鍊奸敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹鍊�	
    SL_Read_Reg=SL_Read_Reg|0x30;//Z 閿熸枻鎷峰彥閿熸枻鎷峰綍閿燂拷 Z閿熸枻鎷烽敓鏂ゆ嫹閿熸嵎鐨勬拝鎷烽敓鏂ゆ嫹鍊奸敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹鍊�	
    sc7a20_write_byte(SL_SC7A20_INT1_CFG, SL_Read_Reg);    

    //HPF SET
  //  GSENSOR_ReadReg(SL_SC7A20_CTRL_REG2,1, &SL_Read_Reg); 
    SL_Read_Reg=0x00;//SL_Read_Reg|0x81;//Normal HP , HPF TO AOI1 
    sc7a20_write_byte(SL_SC7A20_CTRL_REG2, SL_Read_Reg);
    
    //閿熷彨璁规嫹閿熸枻鎷峰€奸敓鏂ゆ嫹閿熸枻鎷�
    sc7a20_write_byte(SL_SC7A20_INT1_THS, SL_SC7A20_INT_THS_20PERCENT);	
    //閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷峰€奸敓鏂ゆ嫹閿熸枻鎷锋椂閿熸垝瑙﹂敓鏂ゆ嫹閿熷彨璁规嫹
    sc7a20_write_byte(SL_SC7A20_INT1_DURATION, SL_SC7A20_INT_DURATION_10CLK);
        
    //AOI1 TO INT1
   // GSENSOR_ReadReg(SL_SC7A20_CTRL_REG3,1, &SL_Read_Reg);
    SL_Read_Reg=0x40;//SL_Read_Reg|0x40; //AOI1 TO INT1
    sc7a20_write_byte(SL_SC7A20_CTRL_REG3, SL_Read_Reg);  
    /*******************AOI1  IN  INT1********************/


    /*******************AOI2  IN  INT2********************/
    sc7a20_read_byte(SL_SC7A20_CTRL_REG5,&SL_Read_Reg);
    SL_Read_Reg=SL_Read_Reg|0x02;//AOI2 LATCH
    sc7a20_write_byte(SL_SC7A20_CTRL_REG5, SL_Read_Reg);	
    

    //AOI2 CONFIG
    SL_Read_Reg=0x00;            //0xFF
    SL_Read_Reg=SL_Read_Reg|0xc0;//閿熸枻鎷烽敓鏂ゆ嫹閿熷壙璁规嫹璇嗛敓鏂ゆ嫹妯″紡
    SL_Read_Reg=SL_Read_Reg|0x03;//X 閿熸枻鎷峰彥閿熸枻鎷峰綍閿燂拷 X閿熸枻鎷烽敓鏂ゆ嫹閿熸嵎鐨勬拝鎷烽敓鏂ゆ嫹鍊奸敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹鍊�
    SL_Read_Reg=SL_Read_Reg|0x0C;//Y 閿熸枻鎷峰彥閿熸枻鎷峰綍閿燂拷 Y閿熸枻鎷烽敓鏂ゆ嫹閿熸嵎鐨勬拝鎷烽敓鏂ゆ嫹鍊奸敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹鍊�	
    SL_Read_Reg=SL_Read_Reg|0x30;//Z 閿熸枻鎷峰彥閿熸枻鎷峰綍閿燂拷 Z閿熸枻鎷烽敓鏂ゆ嫹閿熸嵎鐨勬拝鎷烽敓鏂ゆ嫹鍊奸敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹鍊�	
    sc7a20_write_byte(SL_SC7A20_INT2_CFG, SL_Read_Reg);    

    //HPF SET
    sc7a20_read_byte(SL_SC7A20_CTRL_REG2,&SL_Read_Reg); 
 //   SL_Read_Reg=SL_Read_Reg&0xFD;//NO HPF TO AOI2 
//    GSENSOR_WriteReg(SL_SC7A20_CTRL_REG2, SL_Read_Reg);

    //閿熷彨璁规嫹閿熸枻鎷峰€奸敓鏂ゆ嫹閿熸枻鎷�
    sc7a20_write_byte(SL_SC7A20_INT2_THS, SL_SC7A20_INT_THS_20PERCENT);	
    //閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷峰€奸敓鏂ゆ嫹閿熸枻鎷锋椂閿熸垝瑙﹂敓鏂ゆ嫹閿熷彨璁规嫹
    sc7a20_write_byte(SL_SC7A20_INT2_DURATION, SL_SC7A20_INT_DURATION_10CLK);
    
    //AOI2 TO INT2
    sc7a20_read_byte(SL_SC7A20_CTRL_REG6,&SL_Read_Reg);
    SL_Read_Reg=SL_Read_Reg|0x20; //AOI2 TO INT2
    sc7a20_write_byte(SL_SC7A20_CTRL_REG6, SL_Read_Reg);  
    /*******************AOI2  IN  INT2********************/

    return  1;
}

/*SC7A20初始化 */
void SC7A20_Init(void) 
{ 
	if(read_chip_id() == CHIP_ID)
	{
		SC7A20_BOOT();
		sc7a20_write_byte(0x23,0x80|0x10/*|0x08关闭滤波*/); //读取完成再更新、4g+正常模式  
		SC7A20_INT_Config();
		sc7a20_write_byte(0x20,0x77); //400Hz,正常模式xyz使能 	
	}
}
 
/*SC7A20获取xyz轴加速度值 */
uint8_t SC7A20_GetInfo(int16_t *SL_SC7A20_Data_XYZ_Buf) 
{
    uint8_t SL_Read_Buf[7];
    sc7a20_read_byte(SL_SC7A20_STATUS_REG,&SL_Read_Buf[0]);
    if((SL_Read_Buf[0]&0x0f)==0x0f)
    {
        for (uint8_t i = 1; i < 7; i++)
        {
            sc7a20_read_byte(SL_SC7A20_STATUS_REG+i,&SL_Read_Buf[i]);
        }
        SL_SC7A20_Data_XYZ_Buf[0]=(int16_t)((SL_Read_Buf[2]<<8) + SL_Read_Buf[1]);
        SL_SC7A20_Data_XYZ_Buf[1]=(int16_t)((SL_Read_Buf[4]<<8) + SL_Read_Buf[3]);
        SL_SC7A20_Data_XYZ_Buf[2]=(int16_t)((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;
				printf("x = %d; y = %d; z = %d\r\n",SL_SC7A20_Data_XYZ_Buf[0],SL_SC7A20_Data_XYZ_Buf[1],SL_SC7A20_Data_XYZ_Buf[2]);
        return  1;
    }
    else
    {
        return 0;
    }
}

#include "FreeRTOS.h"
#include "task.h"

void sc7a20_task(void *argv)
{		
	int16_t SL_SC7A20_Data_XYZ_Buf[3] = {0};
	SC7A20_Init();
	while(1)
	{
		SC7A20_GetInfo(SL_SC7A20_Data_XYZ_Buf);
 
		vTaskDelay(1000);
	}
}

 