linux iopen i2c dev,i2c-dev - Linux驱动子系统之I2C_Linux编程_Linux公社-Linux系统门户网站...

[概述]

之前在介绍I2C子系统时,提到过使用i2c-dev.c文件在应用程序中实现我们的I2C从设备驱动。不过,它实现的是一个虚拟,临时的i2c_client,随着设备文件的打开而产生,并随着设备文件的关闭而撤销。I2c-dev.c针对每个I2C适配器生成一个主设备号为89的设备文件,实现了i2c_driver的成员函数以及文件操作接口,所以i2c-dev.c的主题是”i2c_driver成员函数+字符设备驱动”。

[i2c-dev.c源码分析]

I2c-dev初始化函数主要做了注册名为”i2c”的字符设备文件和”i2c-dev”的类。

I2c-dev.c中实现的i2cdev_read和i2cdev_write函数不具有太强的通用性,只适合下面这种单开始信号情况:

efa96bf6184886ca1598837929752edd.gif

而不适合多开始信号的情况:

47eeaf681a2923a34955d9682c9a2dd2.gif

所以我们经常会使用i2cdev_ioctl函数的I2C_RDWR,在分析i2cdev_ioctl函数之前,我们需要了解一个结构体:

/* This is the structure as used in theI2C_RDWR ioctl call */

structi2c_rdwr_ioctl_data {

structi2c_msg __user *msgs;/* pointersto i2c_msgs */

__u32nmsgs;/* number ofi2c_msgs */

};

Msgs     表示单个开始信号传递的数据;

Nmsgs     表示有多少个msgs,比如上图,单开始信号时,nmsgs等于1;多开始信号时,nmsgs等于2

structi2c_msg {

__u16addr;/* slave address                         */

__u16flags;/* 默认为写入 */

#define I2C_M_TEN                  0x0010     /*this is a ten bit chip address */

#define I2C_M_RD           0x0001     /* readdata, from slave to master */

#define I2C_M_NOSTART                  0x4000     /* if I2C_FUNC_PROTOCOL_MANGLING */

#define I2C_M_REV_DIR_ADDR     0x2000     /*if I2C_FUNC_PROTOCOL_MANGLING */

#define I2C_M_IGNORE_NAK          0x1000     /*if I2C_FUNC_PROTOCOL_MANGLING */

#define I2C_M_NO_RD_ACK           0x0800     /* if I2C_FUNC_PROTOCOL_MANGLING */

#define I2C_M_RECV_LEN               0x0400     /* length will be first received byte */

__u16len;/* msg length                              */

__u8*buf;/* pointer to msgdata                       */

};

使用i2cdev_ioctl函数的I2C_RDWR指令会调用到i2cdev_ioctl_rdrw函数:

staticnoinlineinti2cdev_ioctl_rdrw(structi2c_client *client,

unsignedlong arg)

{

structi2c_rdwr_ioctl_data rdwr_arg;

structi2c_msg *rdwr_pa;

u8__user **data_ptrs;

inti, res;

if(copy_from_user(&rdwr_arg,

(structi2c_rdwr_ioctl_data __user *)arg,

sizeof(rdwr_arg)))

return-EFAULT;

/*Put an arbitrary limit on the number of messages that can

* be sent at once */

if(rdwr_arg.nmsgs > I2C_RDRW_IOCTL_MAX_MSGS)

return-EINVAL;

rdwr_pa= kmalloc(rdwr_arg.nmsgs *sizeof(structi2c_msg), GFP_KERNEL);

if(!rdwr_pa)

return-ENOMEM;

if(copy_from_user(rdwr_pa, rdwr_arg.msgs,

rdwr_arg.nmsgs *sizeof(structi2c_msg))) {

kfree(rdwr_pa);

return-EFAULT;

}

data_ptrs= kmalloc(rdwr_arg.nmsgs *sizeof(u8 __user *), GFP_KERNEL);

if(data_ptrs == NULL) {

kfree(rdwr_pa);

return-ENOMEM;

}

res= 0;

for(i = 0; i 

/*Limit the size of the message to a sane amount;

* and don't let length change either. */

if((rdwr_pa[i].len > 8192) ||

(rdwr_pa[i].flags & I2C_M_RECV_LEN)) {

res= -EINVAL;

break;

}

data_ptrs[i]= (u8 __user *)rdwr_pa[i].buf;

rdwr_pa[i].buf= memdup_user(data_ptrs[i], rdwr_pa[i].len);

if(IS_ERR(rdwr_pa[i].buf)) {

res= PTR_ERR(rdwr_pa[i].buf);

break;

}

}

if(res 

intj;

for(j = 0; j 

kfree(rdwr_pa[j].buf);

kfree(data_ptrs);

kfree(rdwr_pa);

returnres;

}

res= i2c_transfer(client->adapter, rdwr_pa, rdwr_arg.nmsgs);

while(i-- > 0) {

if(res >= 0 && (rdwr_pa[i].flags & I2C_M_RD)) {

if(copy_to_user(data_ptrs[i], rdwr_pa[i].buf,

rdwr_pa[i].len))

res= -EFAULT;

}

kfree(rdwr_pa[i].buf);

}

kfree(data_ptrs);

kfree(rdwr_pa);

returnres;

}

咋一看,还挺复杂,其实主要做了一件事情:把用户空间传递过来的i2c_rdwr_ioctl_data数据进行错误检查,www.linuxidc.com 然后调用i2c_transfer函数与适配器进行通信,如果是接收数据,代码会将访问到的数据传回i2c_rdwr_ioctl_data的buf中。I2c_transfer最终会调用到I2C适配器具体实现的master_xfer函数来与硬件进行通信。

[eeprom实例]

预备知识

使用的mini2440开发板,eeprom的地址为0x50,实验完成一个数据的读写,先看下读写时序

AT24C08任意地址字节写的时序:

b8ff329b718dae8f0f004fc87cf833cf.gif

AT24C08任意地址字节写的时序:

d41282d594a55f4e8b2e8d94b1594d94.gif

下面的代码可以按照上面的两个图来阅读:

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

intmain()

{

intfd, ret;

unsignedchar rdwr_addr = 0x42;/* e2prom 读写地址 */

unsignedchar device_addr = 0x50;/* e2prom 设备地址 */

unsignedchar data = 0x12;/* 向e2prom写的数据 */

structi2c_rdwr_ioctl_data e2prom_data;

fd= open("/dev/i2c/0", O_RDWR);

if(fd 

perror("openerror");

exit(1);

}

e2prom_data.msgs= (structi2c_msg *)malloc(e2prom_data.nmsgs * \

sizeof(structi2c_msg));

if(e2prom_data.msgs == NULL) {

perror("mallocerror");

exit(1);

}

ioctl(fd,I2C_TIMEOUT, 1);/* 设置超时 */

ioctl(fd,I2C_RETRIES, 2);/* 设置重试次数 */

/*向e2prom的rdwr_addr地址写入数据data*/

e2prom_data.nmsgs= 1;

e2prom_data.msgs[0].len= 2;

e2prom_data.msgs[0].addr= device_addr;

e2prom_data.msgs[0].flags= 0;/* write */

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

e2prom_data.msgs[0].buf[0]= rdwr_addr;/* write address */

e2prom_data.msgs[0].buf[1]= data;/* write data */

ret= ioctl(fd, I2C_RDWR, (unsignedlong)&e2prom_data);

if(ret 

perror("writedata error");

exit(1);

}

printf("writedata: %d to address: %#x\n", data, rdwr_addr);

data= 0;/* be zero*/

/*从e2prom的rdwr_addr地址读取数据存入buf*/

e2prom_data.nmsgs= 2;

e2prom_data.msgs[0].len= 1;

e2prom_data.msgs[0].addr= device_addr;

//      e2prom_data.msgs[0].flags= 0;     /* write */

e2prom_data.msgs[0].buf= &rdwr_addr;

e2prom_data.msgs[1].len= 1;

e2prom_data.msgs[1].addr= device_addr;

e2prom_data.msgs[1].flags= 1;/* read */

e2prom_data.msgs[1].buf= &data;

ret= ioctl(fd, I2C_RDWR, (unsignedlong)&e2prom_data);

if(ret 

perror("readerror");

exit(1);

}

printf("read  data: %d from address: %#x\n", data,rdwr_addr);

free(e2prom_data.msgs);

close(fd);

return0;

}

在mini2440开发板上已经实验成功。0b1331709591d260c1c78e86d0c51c18.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值