分类: 嵌入式
2016-12-07 13:12:38
fd=open("/dev/i2c-1",O_RDWR);
if(fd < 0)
{ //error info }
2. 驱动I2C 从机地址
if (ioctl(file, I2C_SLAVE_FORCE, addr) < 0)
{
// error info
}
3. read/write 读写I2c总线,或者通过IOCTL读写
#include#include #include #include #include #include #include #include #include #include int main() { int fd,ret; struct i2c_rdwr_ioctl_data codec_data; fd=open("/dev/i2c-3",O_RDWR); if(fd<0) perror("open error");
#if 0 // write/read 方式
if (ioctl(fd, I2C_SLAVE_FORCE, 0x40>>1) < 0)
{
printf("set slave dev addrs failed\n");
return -1;
}
memset(buf, 0, sizeof(buf));
// read fan speed
buf[0] = 0xAA;
buf[1] = 0xAA;
buf[2] = 0x04;
iRtn = write(fd, buf, 3);
if (iRtn != 3)
{
printf("Get fan speed: write data error %d\n", iRtn);
printf("Error: write data failure %s, errno %x\n", strerror(errno), errno);
return -1;
}
memset(Resp, 0, sizeof(Resp));
iRtn = read(fd, Resp, 5);
if (iRtn != 5)
{
printf("Get fan speed: read data error %d\n", iRtn);
printf("Error: write data failure %s\n", strerror(errno));
return -1;
}
#endif
//ioclt方式
codec_data.nmsgs=2;
codec_data.msgs=(struct i2c_msg*)malloc(codec_data.nmsgs*sizeof(struct i2c_msg));
if(!codec_data.msgs)
{
perror("malloc error");
exit(1);
}
ioctl(fd,I2C_TIMEOUT,1);/*超时时间*/
ioctl(fd,I2C_RETRIES,2);/*重复次数*/
sleep(1);
codec_data.nmsgs=1;
(codec_data.msgs[0]).len=2;
(codec_data.msgs[0]).addr=(0x36 >> 1);//我的音频硬件地址;
(codec_data.msgs[0]).flags=0; //write
(codec_data.msgs[0]).buf=(unsigned char*)malloc(2);
(codec_data.msgs[0]).buf[0]=0x04;
(codec_data.msgs[0]).buf[1]=0x55;//the data to write
ret=ioctl(fd,I2C_RDWR,(unsigned long)&codec_data);
if(ret<0)
perror("ioctl error1");
sleep(1);
/******read data from e2prom*******/
printf("read start\n");
codec_data.nmsgs=2;
(codec_data.msgs[0]).len=1; //e2prom 目标数据的地址
(codec_data.msgs[0]).addr=(0x36 >> 1);//yinpin;
(codec_data.msgs[0]).flags=0;//write
(codec_data.msgs[0]).buf[0]=0x04;
(codec_data.msgs[1]).len=1;//读出的数据
(codec_data.msgs[0]).addr=(0x36 >> 1);
(codec_data.msgs[1]).flags=I2C_M_RD;//read
(codec_data.msgs[1]).buf=(unsigned char*)malloc(1);//存放返回值的地址。
(codec_data.msgs[1]).buf[0]=0;//初始化读缓冲
ret=ioctl(fd,I2C_RDWR,(unsigned long)&codec_data);
if(ret<0)
perror("ioctl error2");
close(fd);
return 0;
}