
#include "eflash_drv.h"
#include "tp_drv.h"


#include "sys.h" 
#include "delay.h"
#include "iomacros.h"

UINT8 gt911flag=0;
UINT8 cjt540flag=0;
UINT8 ft5216flag=0;
UINT8 CTP_ADDR=0;

void RTP_io_Init(void)
{
	
}

//模拟I2C
/*****************************CTI2C delay***************************/
void I2C_delay(UINT32 val)
{
	for(int i=0;i<val*60;i++)
	{
		//__NOP;
		__ASM("nop");
	}
}

/*****************************I2C_Start***************************/
u8 I2C_Start(void)
{
	SCL_High;
	SDA_High;
	I2C_delay(1);
//					SDA_INPUT;
//					I2C_delay(1);
//					if(!GetSDABit) 
//					{
//						SDA_OUTPUT;
//						printf("I2C_Start  BUSY \r\n");	           //总线忙
//						return  1;
//					}
//					SDA_OUTPUT;
	SDA_Low;
	I2C_delay(1);
//				SDA_INPUT;	
//				if(GetSDABit) 
//				{	
//					SDA_OUTPUT;
//					printf("I2C_Start  error \r\n");			//总线错误
//					return  1;
//				}
//				SDA_OUTPUT;
	SCL_Low;
	I2C_delay(1);
	return  0;
}

/*****************************I2C_Stop**************************/
void I2C_Stop(void)
{
	//SCL_Low;
	SDA_Low;
	I2C_delay(1);
	SCL_High;
	I2C_delay(1);
	SDA_High;
	I2C_delay(2);
}

///*****************************I2C_SendAck**************************/
void I2C_SendAck(u8 ack)  //参数 0=ack  1=noack
{
	
	SCL_Low;
	I2C_delay(1);
	if(ack) SDA_High;
	else		SDA_Low;
	I2C_delay(1);
	SCL_High;
	I2C_delay(1);
	SCL_Low;
	I2C_delay(1);
}

/*****************************I2C_WaitAck**************************/
u8 I2C_WaitAck(void)     //返回值 0=ack  1=noack
{
	u8 i=0;
	
	SDA_INPUT;
	SDA_High;
	I2C_delay(1);
	SCL_High;
	I2C_delay(1);
	for(i=0;i<200;i++)
	{
		if(!GetSDABit)
		{
			SCL_Low;
			SDA_OUTPUT;
			I2C_delay(1);
			//printf("ack 0k \r\n");
			return 0;
		}
	}
	SCL_Low;
	SDA_OUTPUT;
	I2C_Stop();                 //ERROR
	I2C_delay(1);
	//printf("ack error \r\n");
	return 1;
}

/********************I2C写1byte函数,S为需要写的内容*****************/
void I2C_write_byte(const u8 s)
{
	u8 ch_data,i=0;
	
	//SCL_Low;
	ch_data=s;
	for(i=0;i<8;i++)
	{
		if(ch_data&0x80)	SDA_High;
		else    					SDA_Low;
		I2C_delay(1);
		SCL_High;
		I2C_delay(1);
		SCL_Low;                       
		I2C_delay(1);
		ch_data<<=1;
	}
}

/****************I2C读1byte函数，返回值为所读数据*****************/
u8 I2C_read_byte(void)
{
	u8 ch_data=0x00,i=0;
	
	SDA_INPUT;
	SDA_High;
	I2C_delay(1);
	for(i=0;i<8;i++)
	{
		SCL_Low;
		I2C_delay(1);
		SCL_High;	
		I2C_delay(1);
		ch_data<<=1;
		if(GetSDABit)  ch_data|=0x01;
	}
	SDA_OUTPUT;
	
	return ch_data;
}

/****************I2C连续写数据*****************/
u8 I2C_BufferWrite(u8* pBuffer, u8 WriteAddr, u16 NumByte)
{

}

/****************I2C连续读数据*****************/
u8 I2C_BufferRead(u8* pBuffer, u8 ReadAddr, u16 NumByte)
{

}

void CTP_io_Init(void)
{
	unsigned char ack,i;
	
	cjt540flag=0;
	gt911flag=0;
	ft5216flag=0;
	
	//IO_Init
	RST_OUTPUT; INT_OUTPUT;
	SCL_OUTPUT; SCL_Pullup;
	SDA_OUTPUT; SDA_Pullup;
	RST_High; INT_High; SCL_High; SDA_High;
	
	INT_Low;
	RST_Low;
	DelayMS(2);
	INT_High;
	DelayMS(2);
	RST_High;
	DelayMS(10);
	INT_Low;
	DelayMS(60);
	
	INT_INPUT;
	INT_Interrupt;         //设为中断
	DelayMS(20);
	
	//slave addr scan
	for(i=0;i<128;i++)
	{
		I2C_Start();
		I2C_write_byte(i<<1);
		ack = I2C_WaitAck(); 
		I2C_Stop();
		if(ack) 
		{
			//printf("addr==0x%x, ack==ok  \r\n",i<<1);
			CTP_ADDR=(i<<1);
			if(CTP_ADDR==0x28)			gt911flag		=	1;
			else if(CTP_ADDR==0x34)	cjt540flag	=	1;
			else if(CTP_ADDR==0x70)	ft5216flag	=	1;			
			break;
		}
		DelayUS(3);
	}

	//printf("CTP_ADDR = 0x%x \r\n",CTP_ADDR);
}

