⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tv_i2c.c

📁 泰景的模拟电视全部驱动及MTK平台界面代码
💻 C
字号:
/************************************************************************************************
*
*
*		  (C) Copyright	Telegent Systems, Inc. 2006	All	right reserved.
*				Confidential Information
*
*		  TLG1100  DEVICE ID: 0x2c
*
*		  Description: TLG1100 I2C timing sample code,code need	to OPTIMIZE
*
*		  Author:		 Zeng Hai
*
*		  Last modification	: 2006.	05.	22
*************************************************************************************************/

#include "Tv_Include.h"

/******************************************************************************************
*			   Macros
*******************************************************************************************/
#define	 I2C_DELAY_UNIT		12


/* define I2C interface	*/
#define	SDA					ATV_I2C_SDA			/* I2C serial interface	data I/O */
#define	SCL					ATV_I2C_SCL			/* I2C serial interface	clock input	*/
                    		
                    		
#define	IIC_TRUE			1
#define	IIC_FALSE			0
#define	IIC_ERR				0
#define	IIC_DONE			1


#define	ENTRY(m)								/* These two defines cause warnings when */
#define	EXIT(m)									/* FUNCTION_CALL_TRACING is not defined */

/******************************************************************************************
*			   TYPES
*******************************************************************************************/
typedef	unsigned char	BOOL;


/******************************************************************************************
*			   Prototypes
*******************************************************************************************/
static void	i2c_delay(unsigned int time);
static void	set_i2c_pin(unsigned int pin);
static void	clr_i2c_pin(unsigned int pin);
static unsigned	char get_i2c_pin(unsigned int pin);

static void	i2c_begin(void);
static void	i2c_end(void);
static void	i2c_write_ack(unsigned char	flag);
static unsigned	char i2c_read_ack(void);
static unsigned	char i2c_read_byte(void);
static unsigned	char i2c_write_byte(unsigned char data);


/******************************************************************************************
*
*				FUNCTION
*
*******************************************************************************************/
static unsigned	char i2c_read_byte(void)
{
	unsigned char i;
	unsigned char ret;

	ENTRY(i2c_read_byte);

	ret	= 0;

	/* set SDA as intput status	here */
	GPIO_InitIO(0, SDA);

	for	(i=0; i<8; i++)
	{
		i2c_delay(I2C_DELAY_UNIT <<	0);
		set_i2c_pin(SCL);
		i2c_delay(I2C_DELAY_UNIT <<	0);
		ret	= ret<<1;
		if (get_i2c_pin(SDA))
			ret	|= 1;
		i2c_delay(I2C_DELAY_UNIT <<	0);
		clr_i2c_pin(SCL);
			i2c_delay(I2C_DELAY_UNIT <<	0);

			if (i==7){
				   /* set SDA as output	status here	*/
			   GPIO_InitIO(1, SDA);
				}

			i2c_delay(I2C_DELAY_UNIT <<	0);
	}


	EXIT(i2c_read_byte);

	return ret;
}

static unsigned	char i2c_write_byte(unsigned char data)
{
	unsigned char i;


	ENTRY(i2c_write_byte);

	for	(i=0; i<8; i++)
	{
		i2c_delay(I2C_DELAY_UNIT <<	0);
		if (data & 0x80)
			set_i2c_pin(SDA);
		else
			clr_i2c_pin(SDA);
		data <<= 1;

		i2c_delay(I2C_DELAY_UNIT <<	0);
		set_i2c_pin(SCL);
		i2c_delay(I2C_DELAY_UNIT <<	0);
		clr_i2c_pin(SCL);
	}


	EXIT(i2c_write_byte);

	return i2c_read_ack();
}


/***************************************************************************
*
*
*	 IIC Write data	for	TLG1100
*	 Parameter:
*			   dadd:device address (must be	0x58)
*			   radd:register address
*
****************************************************************************/
unsigned char Tlg_i2c_write_data(unsigned char dadd, unsigned short	radd, unsigned short *data)
{
	unsigned char tmpData =	0;


	ENTRY(Tlg_i2c_write_data);



	i2c_begin();
	if (!i2c_write_byte((unsigned char)(dadd<<1)))
	{
		i2c_end();
		return IIC_ERR;
	}
	/* write 16bits	register */
	  tmpData =	((radd & 0x7f00) >>	8);

	if (!i2c_write_byte(tmpData))
	{
		i2c_end();
		return IIC_ERR;
	}

	tmpData	= (radd	& 0x00ff);
	if (!i2c_write_byte(tmpData))
	{
		i2c_end();
		return IIC_ERR;
	}

	 /*	write 16bits data */
	tmpData	= (((*data)	& 0xff00) >> 8);
	if (!i2c_write_byte(tmpData))
	{
		i2c_end();
		return IIC_ERR;
	}

	tmpData	= ((*data) & 0x00ff);
	if (!i2c_write_byte(tmpData))
	{
		i2c_end();
		return IIC_ERR;
	}


	i2c_end();


	EXIT(Tlg_i2c_write_data);

	return IIC_DONE;
}

/***************************************************************************
*
*
*	 IIC Read data for TLG1100
*	 Parameter:
*			   dadd:device add (must be	0x58)
*			   radd:register add
*
****************************************************************************/
unsigned char Tlg_i2c_read_data(unsigned char dadd,	unsigned short radd, unsigned short	*data)
{
	unsigned char tmpData =	0;


	ENTRY(Tlg_i2c_read_data);


	i2c_begin();
	if (!i2c_write_byte((unsigned char)(dadd<<1)))
	{
		i2c_end();
		return IIC_ERR;
	}
	/* set MSB 1 */
	tmpData	= (((radd |	0x8000)	& 0xff00) >> 8);
	if (!i2c_write_byte(tmpData))
	{
		i2c_end();
		return IIC_ERR;
	}
	tmpData	= ((radd & 0x00ff));
	if (!i2c_write_byte(tmpData))
	{
		i2c_end();
		return IIC_ERR;
	}
///////////////////////////////////////////////////////////////////////////////////////////
//  ???????????????????????
//////////////////////////////////////////////////////////////////////////////////////////
	   /* write	any	data into this register	*/
	if (!i2c_write_byte(0xff))
	{
		i2c_end();
		return IIC_ERR;
	}
	if (!i2c_write_byte(0xff))
	{
		i2c_end();
		return IIC_ERR;
	}
	i2c_end();	/* stop	bit	*/
/////////////////////////////////////////////////////////////////////////////////////////

	/* start again */
	i2c_begin();

	if (!i2c_write_byte((unsigned char)((dadd<<1) |	1)))
	{
		i2c_end();
		return IIC_ERR;
	}

//////////////////////////////////////////////////////////
//			???????????????????
/////////////////////////////////////////////////////////
	*data =	i2c_read_byte();
	i2c_write_ack(0);

	*data =	i2c_read_byte();
	i2c_write_ack(0);
//////////////////////////////////////////////////////////

	  tmpData =	i2c_read_byte();
	  i2c_write_ack(0);

	  *data	= (tmpData << 8) & 0xff00;

	  tmpData =	i2c_read_byte();
	  i2c_write_ack(1);					 /*	can	not	send ACK,must send NAck	*/

	  *data	|= tmpData;

	i2c_end();


	EXIT(Tlg_i2c_read_data);


	return IIC_DONE;
}


/************************************************************************
*
*			   PULL	HIGH SDA/SCK PIN
*
*************************************************************************/
static void	set_i2c_pin(unsigned int pin)
{
	 ENTRY(set_i2c_pin);

		/* pull	high sda/sck here */
	 GPIO_WriteIO(1, pin);		/* only	apply on MTK platform */

	 EXIT(set_i2c_pin);
}

/************************************************************************
*
*			   TLGI2C_Initcheck()
*
*************************************************************************/
int TLGI2C_Initcheck(unsigned i2c_addr, unsigned short on)
{
     ENTRY(TLGI2C_Initcheck);
	 return ((SDA<<3)+SCL);
     EXIT(TLGI2C_Initcheck);
}

/************************************************************************
*
*			   PULL	LOW	SDA/SCK	PIN
*
*************************************************************************/
static void	clr_i2c_pin(unsigned int pin)
{
	 ENTRY(clr_i2c_pin);

		/* pull	low	sda/sck	here */
	 GPIO_WriteIO(0, pin);		/* only	apply on MTK platform */

	 EXIT(clr_i2c_pin);
}

/************************************************************************
*
*			   READ	DATA FROM SDA PIN
*
*************************************************************************/
static unsigned	char get_i2c_pin(unsigned int pin)
{
	unsigned char ret;


	ENTRY(get_i2c_pin);

	   /* add read I/O here	*/
	ret	= GPIO_ReadIO((char)pin);	 /*	only apply on MTK platform */

	EXIT(get_i2c_pin);

	return ret;
}

/**************************************************************************
*
*		   IIC START BIT
*
***************************************************************************/
static void	i2c_begin(void)
{

		ENTRY(i2c_begin);


	i2c_delay(I2C_DELAY_UNIT <<	0);
	set_i2c_pin(SDA);
	i2c_delay(I2C_DELAY_UNIT <<	0);
	set_i2c_pin(SCL);
	i2c_delay(I2C_DELAY_UNIT <<	0);
	clr_i2c_pin(SDA);
	i2c_delay(I2C_DELAY_UNIT <<	0);
	clr_i2c_pin(SCL);
	i2c_delay(I2C_DELAY_UNIT <<	0);


		EXIT(i2c_begin);
}

/**************************************************************************
*
*		   IIC STOP	BIT
*
***************************************************************************/
static void	i2c_end(void)
{

		ENTRY(i2c_end);

	i2c_delay(I2C_DELAY_UNIT <<	2);
	clr_i2c_pin(SDA);
	i2c_delay(I2C_DELAY_UNIT <<	2);
	set_i2c_pin(SCL);
	i2c_delay(I2C_DELAY_UNIT <<	3);
	set_i2c_pin(SDA);
	i2c_delay(I2C_DELAY_UNIT <<	4);


		EXIT(i2c_end);
}

/**************************************************************************
*
*		   IIC SEND	ACK	BIT
*
*					   flag	= 1: write NACK(SDA	HIGH)
*								0: write ACK(SDA LOW)
***************************************************************************/
static void	i2c_write_ack(unsigned char	flag)
{

	  ENTRY(i2c_write_ack);


	if(flag)
		set_i2c_pin(SDA);
	else
		clr_i2c_pin(SDA);
	i2c_delay(I2C_DELAY_UNIT <<	0);
	set_i2c_pin(SCL);
	i2c_delay(I2C_DELAY_UNIT <<	0);
	clr_i2c_pin(SCL);
	i2c_delay(I2C_DELAY_UNIT <<	0);
	//set_i2c_pin(SDA);				  // need to verify	here
	i2c_delay(I2C_DELAY_UNIT <<	0);


	  EXIT(i2c_write_ack);
}

/**************************************************************************
*
*		   IIC READ	ACK	BIT
*
***************************************************************************/
static unsigned	char i2c_read_ack(void)
{
	   unsigned	char ret;


	   ENTRY(i2c_read_ack);

	/* set SDA as input	status here	*/
	GPIO_InitIO(0, SDA);				   /* only apply on	MTK	platform */

	i2c_delay(I2C_DELAY_UNIT <<	0);
	set_i2c_pin(SCL);
	i2c_delay(I2C_DELAY_UNIT <<	0);
	if (!get_i2c_pin(SDA))
	{
		ret	= IIC_TRUE;
	}
	else
	{
		ret	= IIC_FALSE;
	}

	i2c_delay(I2C_DELAY_UNIT <<	0);
	clr_i2c_pin(SCL);
	   i2c_delay(I2C_DELAY_UNIT	<< 0);

	   /* set SDA as output	status here	*/
	   GPIO_InitIO(1, SDA);					   /* only apply on	MTK	platform */

	i2c_delay(I2C_DELAY_UNIT <<	0);


	  EXIT(i2c_read_ack);

	return ret;
}

/********************************************************************************
*
*			  INSERT DELAY
*
*********************************************************************************/
static void	i2c_delay(unsigned int time)
{
	while(time--)
	{
		;
	}
}

/* end of file */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -