#include "tp.h"

/////////////////////////////
#if HW_I2C
IOCTRL_I2CInitTypeDef IOI2CInit;
I2C_HandleTypeDef I2C1_Handle;
static int InitI2cPort(void)
{
	IOI2CInit.id=IOCTRL_I2C1;
	HAL_IOCTRL_I2CInit(&IOI2CInit);

	I2C1_Handle.Instance = I2C1;
	I2C1_Handle.Init.Mode = I2C_MODE_MASTER;
	I2C1_Handle.Init.AddBits = 0x00;
	I2C1_Handle.Init.Add = 0x00;
	I2C1_Handle.Init.HighSpeed = DISABLE;
	//I2C1_Handle.Init.HighSpeed = ENABLE;
	I2C1_Handle.Init.ClockMode = I2C_CLOCK_MODE_NORMAL;
	I2C1_Handle.Init.Prescaler = 0;

	HAL_I2C_Init(&I2C1_Handle);
	return 0;
}
static void ReadBytes(u16 addr,u8 addr_len,u8* buf,u16 buf_len)
{
	HAL_I2C_MemRead(&I2C1_Handle, CTP_SLAVE_ADDR, addr&0xffff, addr_len, buf, buf_len, 100);
}
static void ReadBytes2(u16 addr,u8* buf,u16 buf_len)
{
	HAL_I2C_MemRead(&I2C1_Handle, CTP_SLAVE_ADDR, addr&0xffff, 2, buf, buf_len, 100);
}
static void WriteBytes(u16 addr,u8* buf,u16 buf_len)
{
	HAL_I2C_MemWrite(&I2C1_Handle, CTP_SLAVE_ADDR, addr&0xffff, 2, buf, buf_len, 100);
}
#else

#define sda_in	HAL_I2C_GPIOReadPin(I2C1, I2C_PIN_SDA)
#define sda_1		HAL_I2C_GPIOWritePin(I2C1, I2C_PIN_SDA, SET)
#define sda_0		HAL_I2C_GPIOWritePin(I2C1, I2C_PIN_SDA, RESET)

#define scl_1  HAL_I2C_GPIOWritePin(I2C1, I2C_PIN_SCL, SET)
#define scl_0  HAL_I2C_GPIOWritePin(I2C1, I2C_PIN_SCL, RESET)

static void delay(void)
{
	u32 time=300;
	while(time--);
}
int start(void)
{
	scl_1;
	delay();	
	scl_0;	
	delay();		
	scl_1;		
	delay();
	scl_0;	
	delay();	
	scl_1;
	
	set_sda_in();	
	delay();

	if(!sda_in)
	{
		//printf("err:bus busy!");
		return -1;	
	}
	set_sda_out();
	sda_0;
	delay();

	sda_0;
	delay();

	return 0;
}
void stop(void)
{	
	 delay(); 
	 scl_0;	
	 sda_0;
	 scl_1;		
	 delay();
	 sda_1;
	 delay(); 
	 scl_0;	
	 delay();	
}
void ack(void)
{
	volatile int i=0;
	set_sda_in();??
	sda_1;
	delay();
	scl_1;	
	delay();
	do{
		if(sda_in==0)break;
		i++;
	}while(i<50000);
	//if(i>=50000) printf("ack timeout!\r\n"); 
	set_sda_out();
	scl_0;
}
void noack(void)
{
	sda_0;
	scl_1;
	delay();
	scl_0;
}
void iic_write_byte(u8 dat)
{
	u8 i;
	scl_0;
	for(i=0;i<8;i++)
	{
		//--------------- 	 
		if(dat&0x80)
			sda_1;
		else 
			sda_0;
		delay(); 
		dat=dat<<1;
		//---------------  
		scl_1; 		 
		delay();
		scl_0;
		delay(); 	 
	}
}
u8 iic_read_byte(void)
{
	u8 i;
	u8 dat;
	scl_0;
	set_sda_in();

	for(i=0;i<8;i++)
	{ 
		scl_1;	 
		delay(); 
		//--------------- 	 
		dat=dat<<1;
		if(sda_in)dat++;
		//--------------- 
		scl_0;
		delay();
	}
	set_sda_out();
	sda_1;
	return dat;  
}
u8 IO_slave_addr=0;
void IO_set_slave_addr(u8 addr)
{
	IO_slave_addr=addr;
}
void IO_write_byte(u16 addr,u8 addr_len,u8* buf,u16 buf_len)
{
	int i=0;

	start();
	iic_write_byte(IO_slave_addr<<1);
	ack();
	if(addr_len==1)
	{
	 iic_write_byte(addr&0xff);
	 ack();	  
	}
	else if(addr_len==2)
	{
	 iic_write_byte(addr>>8);
	 ack();
	 iic_write_byte(addr&0xff);
	 ack();	 
	} 

	for(i=0;i<buf_len;i++)
	{
	 iic_write_byte(buf[i]);
	 ack();
	}
	stop(); 

 	delay();
}
void IO_read_byte(u16 addr,u8 addr_len,u8* buf,u16 buf_len)
{
	int i=0;
	start();

	iic_write_byte(IO_slave_addr<<1);
	ack();

	if(addr_len==1)
	{
		iic_write_byte(addr&0xff);
		ack();	  
	}
	else if(addr_len==2)
	{
		iic_write_byte(addr>>8);
		ack();
		iic_write_byte(addr&0xff);
		ack();
	} 

	start();
	iic_write_byte((IO_slave_addr<<1)|0x01);
	ack();
 
	for(i=0;i<buf_len;i++)
	{
	 buf[i]=iic_read_byte();
	 noack();
	}

	stop();	
}
void ConfigSclOutput(void)
{
	  GPIO_InitTypeDef I2CGPIOInitStruct;
    GPIO_PinStateTypeDef PinState;

    I2CGPIOInitStruct.Pin = I2C_PIN_SCL;
    I2CGPIOInitStruct.Dir = GPIO_DIR_OUT;
    I2CGPIOInitStruct.OutputMode = GPIO_OUTPUT_MODE_CMOS;
    I2CGPIOInitStruct.PullMode = GPIO_PULL_MODE_UP;
    HAL_I2C_GPIOInit(I2C1, &I2CGPIOInitStruct);
}
void ConfigSdaOutput(void)
{
	  GPIO_InitTypeDef I2CGPIOInitStruct;
    GPIO_PinStateTypeDef PinState;

    I2CGPIOInitStruct.Pin = I2C_PIN_SDA;
    I2CGPIOInitStruct.Dir = GPIO_DIR_OUT;
    I2CGPIOInitStruct.OutputMode = GPIO_OUTPUT_MODE_CMOS;
    I2CGPIOInitStruct.PullMode = GPIO_PULL_MODE_UP;
    HAL_I2C_GPIOInit(I2C1, &I2CGPIOInitStruct);
}
void ConfigSdaInput(void)
{
	  GPIO_InitTypeDef I2CGPIOInitStruct;
    GPIO_PinStateTypeDef PinState;
		
    I2CGPIOInitStruct.Pin = I2C_PIN_SDA;
    I2CGPIOInitStruct.Dir = GPIO_DIR_IN;
    I2CGPIOInitStruct.OutputMode = GPIO_OUTPUT_MODE_CMOS;
    I2CGPIOInitStruct.PullMode = GPIO_PULL_MODE_UP;
    HAL_I2C_GPIOInit(I2C1, &I2CGPIOInitStruct);
}
void set_sda_in(void)
{
	ConfigSdaInput();
}
void set_sda_out()
{
	ConfigSdaOutput();
}

static InitI2cPort(void)
{
	ConfigSclOutput();
	ConfigSdaOutput();
	IO_set_slave_addr(CTP_SLAVE_ADDR>>1);	
	sda_1;
  scl_1;
}
static void ReadBytes(u16 addr,u8 addr_len,u8* buf,u16 buf_len)
{
	IO_read_byte(addr,addr_len,buf,buf_len);
}
#endif

void InitTouch(void)
{
	u8 buf[6];
	InitI2cPort();
	HAL_IOCTRL_Swapint(IOCTRL_SWAP10, ENABLE);
	
	INT_INPUT;
	RESET_OUTPUT;	
	RESET_LOW;
	DelayMS(200);
	RESET_HIGH;
	DelayMS(200);

	ReadBytes(0xA3,1,buf,1);
	//printf("Ctp id=%02x\r\n",buf[0]);
//gt911-----------------------------------------------------------------------
#if 0
	unsigned int Product_ID;

	INT_OUTPUT;
	RESET_OUTPUT;

	INT_LOW;   //INT = 0,For GT911 Addr = 0xBB
	RESET_LOW; //RESET = 0;
	DelayMS(2);
	INT_HIGH; //INT  = 1;
	DelayMS(2);
	RESET_HIGH;
	DelayMS(10);
	INT_LOW;
	DelayMS(60);
	INT_INPUT;
	DelayMS(20);

	ReadBytes2(0x4081, buf, 6);

	Product_ID = buf[3];
	Product_ID = Product_ID << 8 | buf[2];
	Product_ID = Product_ID << 8 | buf[1];
	Product_ID = Product_ID << 8 | buf[0];

	printf("\r\nID:%x \r\n", Product_ID);

	while (1)
	{
		if (IsTouchPressed())
		{
			ReadTouchData2();
		}
	}
#endif
//----------------------------------------------------------------------------
}

u16 CtpX(u8 num,u8 *data)
{
	return ((data[4*num]&0x0f)<<8)|data[4*num+1];			
}
u16 CtpY(u8 num,u8 *data)
{
	return ((data[4*num+2]&0x0f)<<8)|data[4*num+3];			
}
unsigned char IsTouchPressed(void)
{
	return !INT_LEVEL;
}
void ReadTouchData(unsigned int *x,unsigned int *y)
{
	u16 x_new = 0;
	u16 y_new = 0;
	u8 data[5];

	ReadBytes(0x02,1,data,5);
	x_new = CtpX(0,data+1)%800;
	y_new = CtpY(0,data+1)%480;
	*x = x_new;
	*y = y_new;
}

void ReadTouchData2(void)
{
	u16 x_new = 0;
	u16 y_new = 0;
	u8 data[9];
	u8 writedata[1]={0};

	ReadBytes2(0x4e81,data,9);
	
	if(data[0]&0x80)   //buffer status 为1时才是数据有效
	{
		
		x_new = data[3];
		x_new = x_new<<8 |data[2];		
		
		y_new = data[5];
		y_new = y_new<<8 |data[4];
		
		WriteBytes(0x4e81,writedata,1);
		printf("x=%d y=%d\r\n",x_new,y_new);
	}
}



