原文:http://hi.baidu.com/jjhopeforever/item/4e5e75c6156aa42ea1b50a67
#include <stdio.h>
#include <linux/types.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <errno.h>
#define I2C_RETRIES 0x0701
#define I2C_TIMEOUT 0x0702
#define I2C_RDWR 0x0707
struct i2c_msg
{
unsigned short addr;
unsigned short flags;
#define I2C_M_TEN 0x0010
#define I2C_M_RD 0x0001
unsigned short len;
unsigned char *buf;
};
struct i2c_rdwr_ioctl_data
{
struct i2c_msg *msgs;
int nmsgs;
};
int main()
{
int fd,ret;
struct i2c_rdwr_ioctl_data e2prom_data;
fd=open("/dev/i2c-0",O_RDWR);
if(fd<0)
{
perror("open error");
}
e2prom_data.nmsgs=2;
e2prom_data.msgs=(struct i2c_msg*)malloc(e2prom_data.nmsgs*sizeof(struct i2c_msg));
if(!e2prom_data.msgs)
{
perror("malloc error");
exit(1);
}
ioctl(fd,I2C_TIMEOUT,100);
ioctl(fd,I2C_M_TEN,0);
ioctl(fd,I2C_RETRIES,2);
e2prom_data.nmsgs=1;
(e2prom_data.msgs[0]).len=2; //1個 e2prom 寫入目标的位址和1個資料
(e2prom_data.msgs[0]).addr=0x5c;//e2prom 裝置位址
(e2prom_data.msgs[0]).flags=0; //write
(e2prom_data.msgs[0]).buf=(unsigned char*)malloc(2);
(e2prom_data.msgs[0]).buf[0]=0x01;// e2prom 寫入目标的位址
(e2prom_data.msgs[0]).buf[1]=0x74;//the data to write
ret=ioctl(fd,I2C_RDWR,(unsigned long)&e2prom_data);
if(ret<0)
{
perror("ioctl error1");
}
sleep(1);
e2prom_data.nmsgs=2;
(e2prom_data.msgs[0]).len=1; //e2prom 目标資料的位址
(e2prom_data.msgs[0]).addr=0x5c; // e2prom 裝置位址
(e2prom_data.msgs[0]).flags=0;//write
(e2prom_data.msgs[0]).buf[0]=0x01;//e2prom資料位址
(e2prom_data.msgs[1]).len=1;//讀出的資料
(e2prom_data.msgs[1]).addr=0x5c;// e2prom 裝置位址
(e2prom_data.msgs[1]).flags=I2C_M_RD;//read
(e2prom_data.msgs[1]).buf=(unsigned char*)malloc(1);//存放傳回值的位址。
(e2prom_data.msgs[1]).buf[0]=0;//初始化讀緩沖
ret=ioctl(fd,I2C_RDWR,(unsigned long)&e2prom_data);
if(ret<0)
{
perror("ioctl error2");
}
printf("buff[0]=%x\n",(e2prom_data.msgs[1]).buf[0]);
close(fd);
return 0;
}