I2C用户态驱动设计
一、用户态驱动模型
1.1 I2C通用驱动代码
i2c_dev_init:
- static int __init i2c_dev_init(void)
- {
- int res;
- printk(KERN_INFO "i2c /dev entries driver\n");
- res = register_chrdev(I2C_MAJOR, "i2c", &i2cdev_fops); //早期注册字符设备
- if (res)
- goto out;
- i2c_dev_class = class_create(THIS_MODULE, "i2c-dev"); //生成自动创建字符设备文件
- if (IS_ERR(i2c_dev_class)) {
- res = PTR_ERR(i2c_dev_class);
- goto out_unreg_chrdev;
- }
- res = i2c_add_driver(&i2cdev_driver); //注册一个I2C的设备驱动
- if (res)
- goto out_unreg_class;
- return 0;
- out_unreg_class:
- class_destroy(i2c_dev_class);
- out_unreg_chrdev:
- unregister_chrdev(I2C_MAJOR, "i2c");
- out:
- printk(KERN_ERR "%s: Driver Initialisation failed\n", __FILE__);
- return res;
- }
i2cdev_fops:
- static const struct file_operations i2cdev_fops = {
- .owner = THIS_MODULE,
- .llseek = no_llseek,
- .read = i2cdev_read,
- .write = i2cdev_write,
- .unlocked_ioctl = i2cdev_ioctl,
- .open = i2cdev_open,
- .release = i2cdev_release,
- };
i2cdev_ioctl:
- static long i2cdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
- {
- struct i2c_client *client = (struct i2c_client *)file->private_data;
- unsigned long funcs;
- dev_dbg(&client->adapter->dev, "ioctl, cmd=0x%02x, arg=0x%02lx\n",
- cmd, arg);
- switch ( cmd ) {
- case I2C_SLAVE:
- case I2C_SLAVE_FORCE:
- /* NOTE: devices set up to work with "new style" drivers
- * can't use I2C_SLAVE, even when the device node is not
- * bound to a driver. Only I2C_SLAVE_FORCE will work.
- *
- * Setting the PEC flag here won't affect kernel drivers,
- * which will be using the i2c_client node registered with
- * the driver model core. Likewise, when that client has
- * the PEC flag already set, the i2c-dev driver won't see
- * (or use) this setting.
- */
- if ((arg > 0x3ff) ||
- (((client->flags & I2C_M_TEN) == 0) && arg > 0x7f))
- return -EINVAL;
- if (cmd == I2C_SLAVE && i2cdev_check_addr(client->adapter, arg))
- return -EBUSY;
- /* REVISIT: address could become busy later */
- client->addr = arg;
- return 0;
- case I2C_TENBIT:
- if (arg)
- client->flags |= I2C_M_TEN;
- else
- client->flags &= ~I2C_M_TEN;
- return 0;
- case I2C_PEC:
- if (arg)
- client->flags |= I2C_CLIENT_PEC;
- else
- client->flags &= ~I2C_CLIENT_PEC;
- return 0;
- case I2C_FUNCS:
- funcs = i2c_get_functionality(client->adapter);
- return put_user(funcs, (unsigned long __user *)arg);
- case I2C_RDWR: //这里是读写命令
- return i2cdev_ioctl_rdrw(client, arg);
- case I2C_SMBUS:
- return i2cdev_ioctl_smbus(client, arg);
- case I2C_RETRIES:
- client->adapter->retries = arg;
- break;
- case I2C_TIMEOUT:
- /* For historical reasons, user-space sets the timeout
- * value in units of 10 ms.
- */
- client->adapter->timeout = msecs_to_jiffies(arg * 10);
- break;
- default:
- /* NOTE: returning a fault code here could cause trouble
- * in buggy userspace code. Some old kernel bugs returned
- * zero in this case, and userspace code might accidentally
- * have depended on that bug.
- */
- return -ENOTTY;
- }
- return 0;
- }
i2cdev_ioctl_rdrw:
- static noinline int i2cdev_ioctl_rdrw(struct i2c_client *client,
- unsigned long arg)
- {
- struct i2c_rdwr_ioctl_data rdwr_arg;
- struct i2c_msg *rdwr_pa;
- u8 __user **data_ptrs;
- int i, res;
- if (copy_from_user(&rdwr_arg,
- (struct i2c_rdwr_ioctl_data __user *)arg, //传给ioctl的arg值,强制转换成i2c_rdwr_ioctl_data
- 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 = (struct i2c_msg *)
- kmalloc(rdwr_arg.nmsgs * sizeof(struct i2c_msg),
- GFP_KERNEL);
- if (!rdwr_pa)
- return -ENOMEM;
- if (copy_from_user(rdwr_pa, rdwr_arg.msgs,
- rdwr_arg.nmsgs * sizeof(struct i2c_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 < rdwr_arg.nmsgs; 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 = kmalloc(rdwr_pa[i].len, GFP_KERNEL);
- if (rdwr_pa[i].buf == NULL) {
- res = -ENOMEM;
- break;
- }
- if (copy_from_user(rdwr_pa[i].buf, data_ptrs[i],
- rdwr_pa[i].len)) {
- ++i; /* Needs to be kfreed too */
- res = -EFAULT;
- break;
- }
- }
- if (res < 0) {
- int j;
- for (j = 0; j < i; ++j)
- kfree(rdwr_pa[j].buf);
- kfree(data_ptrs);
- kfree(rdwr_pa);
- return res;
- }
- res = i2c_transfer(client->adapter, rdwr_pa, rdwr_arg.nmsgs); //发送数据来源于i2c核心
- 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);
- return res;
- }
i2c_rdwr_ioctl_data:
- struct i2c_rdwr_ioctl_data {
- struct i2c_msg __user *msgs; /* pointers to i2c_msgs */ 消息指针
- __u32 nmsgs; /* number of i2c_msgs */ 有多少条消息
- };
一条消息(一个读操作和写操作):
- struct i2c_msg {
- __u16 addr; /* slave address */ //从设备地址
- __u16 flags; //读还是写
- #define I2C_M_TEN 0x0010 /* this is a ten bit chip address */
- #define I2C_M_RD 0x0001 /* read data, 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 */
- __u16 len; /* msg length */ //消息里面有多少字节
- __u8 *buf; /* pointer to msg data */ //消息里面的数据
- };
i2c_transfer:
- int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
- {
- int ret;
- if (adap->algo->master_xfer) {
- #ifdef DEBUG
- for (ret = 0; ret < num; ret++) {
- dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
- "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
- ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
- (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
- }
- #endif
- if (in_atomic() || irqs_disabled()) {
- ret = mutex_trylock(&adap->bus_lock);
- if (!ret)
- /* I2C activity is ongoing. */
- return -EAGAIN;
- } else {
- mutex_lock_nested(&adap->bus_lock, adap->level);
- }
- ret = adap->algo->master_xfer(adap,msgs,num); //找到adapter(控制器)->algo(算法)->master_xfer(发送)
- mutex_unlock(&adap->bus_lock);
- return ret;
- } else {
- dev_dbg(&adap->dev, "I2C level transfers not supported\n");
- return -EOPNOTSUPP;
- }
- }
二、EEPROM用户态驱动编写
i2c-app-drv.c:
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <linux/types.h>
- #define I2C_RDWR 0x0707
- struct i2c_msg {
- unsigned short addr; /* slave address */
- unsigned short flags;
- unsigned short len; /* msg length */
- unsigned char *buf; /* pointer to msg data */
- };
- struct i2c_rdwr_ioctl_data {
- struct i2c_msg *msgs; /* pointers to i2c_msgs */
- unsigned int nmsgs; /* number of i2c_msgs */
- };
- int main()
- {
- int fd;
- struct i2c_rdwr_ioctl_data e2prom_data;
- //1.打开通用设备文件
- fd = open("/dev/i2c-0", O_RDWR);
- e2prom_data.msgs = (struct i2c_msg *)malloc(2*sizeof(struct i2c_msg));
- //2.构造写数据到eeprom的消息
- e2prom_data.nmsgs = 1;
- (e2prom_data.msgs[0]).len = 2;
- (e2prom_data.msgs[0]).addr = 0x50;
- (e2prom_data.msgs[0]).flags = 0;
- (e2prom_data.msgs[0]).buf = (unsigned char *)malloc(2);
- (e2prom_data.msgs[0]).buf[0] = 0x10;
- (e2prom_data.msgs[0]).buf[1] = 0xF0;
- //3.利用ioctl写入数据
- ioctl(fd, I2C_RDWR, (unsigned long)&e2prom_data);
- //4.构造从eeprom读书据的消息
- e2prom_data.nmsgs = 2;
- (e2prom_data.msgs[0]).len = 1;
- (e2prom_data.msgs[0]).addr = 0x50;
- (e2prom_data.msgs[0]).flags = 0;
- (e2prom_data.msgs[0]).buf[0] = 0x10;
- (e2prom_data.msgs[1]).len = 1;
- (e2prom_data.msgs[1]).addr = 0x50;
- (e2prom_data.msgs[1]).flags = 1;
- (e2prom_data.msgs[1]).buf = (unsigned char *)malloc(2);
- (e2prom_data.msgs[1]).buf[0] = 0;
- //5.使用ioctl读出数据
- ioctl(fd, I2C_RDWR, (unsigned long)&e2prom_data);
- printf("buf[0]=%x\n",(e2prom_data.msgs[1]).buf[0]);
- //6.关闭设备
- close(fd);
- }
遇到的问题:
- # ./i2c-app-drv
- s3c2440-i2c s3c2440-i2c: cannot get bus (error -110)
- s3c2440-i2c s3c2440-i2c: cannot get bus (error -110)
- s3c2440-i2c s3c2440-i2c: cannot get bus (error -110)
- s3c2440-i2c s3c2440-i2c: cannot get bus (error -110)
- buf[0]=0
内核中的i2c和视频模块冲突了。所以要在内核中去掉视频模块:
Device Drivers>>Multimedia devices>>Video For Linux
结果:
- # ./i2c-app-drv
- buf[0]=f0
I2C用户态驱动设计的更多相关文章
- [国嵌攻略][155][I2C用户态驱动设计]
用户态驱动模型 用户态驱动模型首先是一个应用程序,其次是在这个用户程序中通过内核调用来驱动设备. IIC通用驱动代码 IIC通用驱动程序的代码在/drivers/i2c/i2c-dev.c中.一次读操 ...
- Linux用户态驱动设计
聊聊Linux用户态驱动设计 序言 设备驱动可以运行在内核态,也可以运行在用户态,用户态驱动的利弊网上有很多的讨论,而且有些还上升到政治性上,这里不再多做讨论.不管用户态驱动还是内核态驱动,他们都 ...
- 聊聊Linux用户态驱动设计
序言 设备驱动可以运行在内核态,也可以运行在用户态,用户态驱动的利弊网上有很多的讨论,而且有些还上升到政治性上,这里不再多做讨论.不管用户态驱动还是内核态驱动,他们都有各自的缺点.内核态驱动的问题是: ...
- Linux I2C驱动--用户态驱动简单示例
1. Linux内核支持I2C通用设备驱动(用户态驱动:由应用层实现对硬件的控制可以称之为用户态驱动),实现文件位于drivers/i2c/i2c-dev.c,设备文件为/dev/i2c-0 2. I ...
- linuxok6410的I2C驱动分析---用户态驱动
3 i2c-dev 3.1 概述 之前在介绍I2C子系统时,提到过使用i2c-dev.c文件在应用程序中实现我们的I2C从设备驱动.不过,它实现的是一个虚拟,临时的i2c_client,随着设备文件 ...
- 用户态驱动--UIO机制的实现【转】
转自:https://blog.csdn.net/u013982161/article/details/51584900 1 uio理论部分 1.1为什么出现了UIO? 硬件设备可以根据功能分为网 ...
- tiny6410在I2c用户态中的程序设计eeprom
在读写的过程中,发现写数据成功但是读取数据却失败,猜测是因为iic的读写操作过快,故在写操作后给一定的延迟,进而读写成功. 代码如下: #include <stdio.h>#include ...
- [中英对照]User-Space Device Drivers in Linux: A First Look | 初识Linux用户态设备驱动程序
如对Linux用户态驱动程序开发有兴趣,请阅读本文,否则请飘过. User-Space Device Drivers in Linux: A First Look | 初识Linux用户态设备驱动程序 ...
- [windows驱动]内核态驱动架构
1.windows驱动简介: 1.1 windows组件简介: 1.2 windows驱动类型: windows驱动分为两种基本类型: 用户态驱动在用户态下执行.它们一般提供一套win32应用程序和内 ...
随机推荐
- spring+springMVC+mybatis框架整合——配置文件说明
如下图 web.xml配置说明: spring配置文件说明-1: spring配置文件说明-2: spring配置助记: 扫注(base) 读配(loc) 数据源(和comb(使用c3p0数据源)) ...
- appium 链接真机后,运行代码,但是APP并没有启动
要淡定,链接真机后,问题一下多出来这么多,还没有启动程序,就碰到接二连三的问题. 爽到家了.慢慢解决吧. 具体问题是这样的: # coding=utf-8from appium import webd ...
- 三种数据库截取字段内容&&获取字符长度的函数如下
if(databaseutil.getValue("database").equalsIgnoreCase("sqlserver")){ list =categ ...
- Gradle Wrapper 介绍
回顾 上一篇介绍了如何使用 Gradle 快速创建 Java 项目,开始讲 Gradle Wrapper 之前先来重温一下通过 gradle init 命令创建的 Java 项目目录结构 ├── bu ...
- Maven初了解
这周开始,我正式上手了接口测试.我们接口测试使用的是Maven做项目管理,用Junit做测试框架.所以我稍微了解了一下Maven. 那么什么是Maven呢? Maven是基于项目对象模型(POM pr ...
- QTP read or write XML file
'strNodePath = "/soapenv:Envelope/soapenv:Body/getProductsResponse/transaction/queryProducts/qu ...
- 数组中重复的数字(js实现)
题目 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果输入长度为7的 ...
- css浮动以及清除
首先要知道,div是块级元素,在页面中独占一行,自上而下排列,也就是传说中的流.如下图: 可以看出,即使div1的宽度很小,页面中一行可以容下div1和div2,div2也不会排在div1后边,因为d ...
- python post 发送字符串
python post 发送一段字符串 把字符串写在表单里,表单用字典格式,字符串作value import requests data={key:str} r=requests.post(url,d ...
- 添加阿里巴巴图标,让你页面小图标都是CSS3写成
第一步把你想要的小图标添加到购物车里,然后如图下载 第二步下载完了,如下图有这些文件 第三步,把你需要的必要文件放到文件夹中,然后把需要的代码放到一个CSS中,然后引用 备注,这几个文件就是字体,必须 ...