基于S3c2440的I2C驱动与测试程序追踪交叉分析

基于S3c2440的I2C驱动与测试程序追踪交叉分析,第1张

VMware虚拟机+Fedora10, 硬件平台TQ2440, 内核2.6.30.4

对应的驱动程序豁然开朗, 然后自己添加了一些dev_dbg后, 对于不理解的地方也有了一定的参考提示, 记录下来与大家分享。

测试程序如下:

/*i2c_test.c

* hongtao_liu

*/

#include

#include

#include

#include

#include

#include

#include

#include

#define I2C_RETRIES 0x0701

#define I2C_TIMEOUT 0x0702

#define I2C_RDWR 0x0707

/*********定义struct i2c_rdwr_ioctl_data和struct i2c_msg,要和内核一致*******/

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;

/* nmsgs这个数量决定了有多少开始信号,对于“单开始时序”,取1*/

};

/***********主程序***********/

int main()

{

int fd,ret;

struct i2c_rdwr_ioctl_data e2prom_data;

fd=open(“/dev/i2c-0”,O_RDWR);

/*

*dev/i2c-0是在注册i2c-dev.c后产生的,代表一个可 *** 作的适配器。如果不使用i2c-dev.c

*的方式,就没有,也不需要这个节点。

*/

if(fd《0)

{

perror(“open error”);

}

e2prom_data.nmsgs=2;

/*

*因为 *** 作时序中,最多是用到2个开始信号(字节读 *** 作中),所以此将

*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,1);/*超时时间*/

ioctl(fd,I2C_RETRIES,2);/*重复次数*/

/***write data to e2prom**/

e2prom_data.nmsgs=1;

(e2prom_data.msgs[0]).len=2; //1个 e2prom 写入目标的地址和1个数据

(e2prom_data.msgs[0]).addr=0x50;//e2prom 设备地址

(e2prom_data.msgs[0]).flags=0; //write

(e2prom_data.msgs[0]).buf=(unsigned char*)malloc(2);

(e2prom_data.msgs[0]).buf[0]=0x10;// e2prom 写入目标的地址

(e2prom_data.msgs[0]).buf[1]=0x58;//the data to write

ret=ioctl(fd,I2C_RDWR,(unsigned long)&e2prom_data);

if(ret《0)

{

perror(“ioctl error1”);

}

sleep(1);

/******read data from e2prom*******/

e2prom_data.nmsgs=2;

(e2prom_data.msgs[0]).len=1; //e2prom 目标数据的地址

(e2prom_data.msgs[0]).addr=0x50; // e2prom 设备地址

(e2prom_data.msgs[0]).flags=0;//write

(e2prom_data.msgs[0]).buf[0]=0x10;//e2prom数据地址

(e2prom_data.msgs[1]).len=1;//读出的数据

(e2prom_data.msgs[1]).addr=0x50;// 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]);

/***打印读出的值,没错的话,就应该是前面写的0x58了***/

close(fd);

return 0;

}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/dianzi/2605678.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-08-09
下一篇 2022-08-09

发表评论

登录后才能评论

评论列表(0条)

保存