#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define I2C_PCF8574T_ADDRESS 0x40
#define I2C_PCF8574T_SLAVE_ADDRESS 0x00
#define GPIO_IIC_SCL 0 // S3C6410_GPP(9)
#define GPIO_IIC_SDA 1 // S3C6410_GPP(12)
//#define GPPCON 0x7F008160 /*gpiop con寄存器地址*/
//#define GPPDAT 0x7F008164 /*gpiop dat寄存器地址*/
#define SCL_H { gpio_set_value (S3C64XX_GPP(9), 1) ; } /*gpp9(scl)输出高电平*/
#define SCL_L { gpio_set_value (S3C64XX_GPP(9), 0) ; } /*gpp9(scl)输出低电平*/
#define SDA_H { gpio_set_value (S3C64XX_GPP(12), 1); } /*gpp12(sda)输出高电平*/
#define SDA_L { gpio_set_value (S3C64XX_GPP(12), 0); } /*gpp12(sda)输出低电平*/
#define SDA_IN { gpio_direction_input (S3C64XX_GPP(12)); } /*sda输入*/
#define SDA_OUT { gpio_direction_output (S3C64XX_GPP(12),1); } /*sda输出*/
#define SCL_OUT { gpio_direction_output (S3C64XX_GPP(9),1); } /*scl输出*/
#define WHILE_SDA_HIGH (gpio_get_value (S3C64XX_GPP(12)))
unsigned int ByteDelayTimeout = 0x700 ;
unsigned int BitDelayTimeout = 0x1000;
static void ByteDelay ( void )
{
volatile unsigned int dwTimeout ;
dwTimeout = ByteDelayTimeout ;
while ( --dwTimeout )
{
asm ( "nop" ) ;
}
}
static void BitDelay ( void )
{
volatile unsigned int dwTimeout ;
dwTimeout = BitDelayTimeout ;
while ( --dwTimeout )
{
asm ( "nop" ) ;
}
}
static void InteRFaceInit ( void )
{
SCL_OUT;
SDA_OUT;
SCL_H;
SDA_H;
ByteDelay () ;
ByteDelay () ;
ByteDelay () ;
}
/*
* i2c总线开始信号
*/
static void I2C_Start()
{
SDA_OUT ;
SDA_H ;
BitDelay () ;
SCL_H ;
BitDelay () ;
SDA_L ;
BitDelay () ;
}
/*
*i2c总线停止信号
*/
static void I2C_Stop ()
{
SDA_OUT ;
SDA_L ;
BitDelay () ;
SCL_H ;
BitDelay () ;
SDA_H ;
BitDelay () ;
}
/*
*i2c应答确认信号
*/
static void I2C_Ack()
{
SDA_OUT ;
SDA_L ;
BitDelay () ;
SCL_H ;
BitDelay () ;
SCL_L ;
BitDelay () ;
SDA_IN ;
BitDelay () ;
printk(KERN_ALERT "------------iic_ACK------------\n" ) ;
}
static int I2C_Ack1()
{
int i;
SCL_H ;
BitDelay () ;
SDA_IN ;
while((WHILE_SDA_HIGH)&&(i<255))i++; //无应答延时一段时间后默认已经收到
SCL_L ;
BitDelay () ;
SDA_OUT;
BitDelay () ;
/* printk(KERN_ALERT "------------iic_ACK1------------\n" ) ;
SCL_H;
BitDelay();
IF(WHILE_SDA_HIGH==1)
{
SCL_L;
BitDelay();
SCL_H;
if(WHILE_SDA_HIGH==1)
return 1;
}
else
return 0; */
}
/*
*i2c结束应答信号
*/
static void I2C_Nack ()
{
SDA_OUT ;
SDA_H ;
BitDelay () ;
SCL_H ;
BitDelay () ;
SCL_L ;
BitDelay () ;
SCL_H ;
}
/*
*向i2c总线写入一字节数据
*/
static char Write_I2C_Byte ( char byte )
{
char i ;
SCL_L ;
BitDelay () ;
for ( i = 0 ; i < 8 ; i++ )
{
if ( (byte & 0x80) == 0x80 )
{
SDA_H ;
}
else
{
SDA_L ;
}
BitDelay () ;
SCL_H ;
BitDelay () ;
SCL_L ;
BitDelay () ;
byte <<= 1 ;
}
return 1 ;
}
static char Read_I2C_Byte ( void )
{
char i, buff = 0 ;
SCL_L ;
BitDelay () ;
for ( i = 0 ; i < 8 ; i++ )
{
SDA_OUT ;
SDA_H ;
BitDelay () ;
SCL_H ;
SDA_IN ;
BitDelay () ;
if ( WHILE_SDA_HIGH )
{
buff |= 0x01 ;
}
else
{
buff &=~0x01;
}
if ( i < 7 )
{
buff <<= 1 ;
}
SCL_L ;
BitDelay () ;
}
return buff ;
}