关于读写I2C总线的时候出错的问题

我的程序是这样的:

#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");

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;

}

音频的硬件地址是没有错误的。在开发板上运行的结果是这样的

ioctl error2: Remote I/O error

只提示了读的时候错误了。请问,这个错误是怎么造成的,我一直困惑了好久!

音频,I2C

------解决方案--------------------

读的时候,codec_data.msgs[1] 没有赋值。

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐