I2C自编设备驱动设计
一、自编设备驱动模型
at24.c:
- static int __init at24_init(void)
- {
- io_limit = rounddown_pow_of_two(io_limit);
- return i2c_add_driver(&at24_driver); //注册i2c驱动设备
- }
at24_driver:
- static struct i2c_driver at24_driver = {
- .driver = {
- .name = "at24",
- .owner = THIS_MODULE,
- },
- .probe = at24_probe, //找到驱动对应的设备调用的函数
- .remove = __devexit_p(at24_remove),
- .id_table = at24_ids, //这个表里的设备都支持
- };
at24_probe:
- static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id)
- {
- struct at24_platform_data chip;
- bool writable;
- bool use_smbus = false;
- struct at24_data *at24;
- int err;
- unsigned i, num_addresses;
- kernel_ulong_t magic;
- if (client->dev.platform_data) {
- chip = *(struct at24_platform_data *)client->dev.platform_data;
- } else {
- if (!id->driver_data) {
- err = -ENODEV;
- goto err_out;
- }
- magic = id->driver_data;
- chip.byte_len = BIT(magic & AT24_BITMASK(AT24_SIZE_BYTELEN));
- magic >>= AT24_SIZE_BYTELEN;
- chip.flags = magic & AT24_BITMASK(AT24_SIZE_FLAGS);
- /*
- * This is slow, but we can't know all eeproms, so we better
- * play safe. Specifying custom eeprom-types via platform_data
- * is recommended anyhow.
- */
- chip.page_size = 1;
- chip.setup = NULL;
- chip.context = NULL;
- }
- if (!is_power_of_2(chip.byte_len))
- dev_warn(&client->dev,
- "byte_len looks suspicious (no power of 2)!\n");
- if (!is_power_of_2(chip.page_size))
- dev_warn(&client->dev,
- "page_size looks suspicious (no power of 2)!\n");
- /* Use I2C operations unless we're stuck with SMBus extensions. */
- if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
- if (chip.flags & AT24_FLAG_ADDR16) {
- err = -EPFNOSUPPORT;
- goto err_out;
- }
- if (!i2c_check_functionality(client->adapter,
- I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
- err = -EPFNOSUPPORT;
- goto err_out;
- }
- use_smbus = true;
- }
- if (chip.flags & AT24_FLAG_TAKE8ADDR)
- num_addresses = 8;
- else
- num_addresses = DIV_ROUND_UP(chip.byte_len,
- (chip.flags & AT24_FLAG_ADDR16) ? 65536 : 256);
- at24 = kzalloc(sizeof(struct at24_data) +
- num_addresses * sizeof(struct i2c_client *), GFP_KERNEL);
- if (!at24) {
- err = -ENOMEM;
- goto err_out;
- }
- mutex_init(&at24->lock);
- at24->use_smbus = use_smbus;
- at24->chip = chip;
- at24->num_addresses = num_addresses;
- /*
- * Export the EEPROM bytes through sysfs, since that's convenient.
- * By default, only root should see the data (maybe passwords etc)
- */
- at24->bin.attr.name = "eeprom";
- at24->bin.attr.mode = chip.flags & AT24_FLAG_IRUGO ? S_IRUGO : S_IRUSR;
- at24->bin.read = at24_bin_read;
- at24->bin.size = chip.byte_len;
- at24->macc.read = at24_macc_read;
- writable = !(chip.flags & AT24_FLAG_READONLY);
- if (writable) {
- if (!use_smbus || i2c_check_functionality(client->adapter,
- I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)) {
- unsigned write_max = chip.page_size;
- at24->macc.write = at24_macc_write;
- at24->bin.write = at24_bin_write; //这里注册了at24_bin_write,用户的write函数的调用接口
- at24->bin.attr.mode |= S_IWUSR;
- if (write_max > io_limit)
- write_max = io_limit;
- if (use_smbus && write_max > I2C_SMBUS_BLOCK_MAX)
- write_max = I2C_SMBUS_BLOCK_MAX;
- at24->write_max = write_max;
- /* buffer (data + address at the beginning) */
- at24->writebuf = kmalloc(write_max + 2, GFP_KERNEL);
- if (!at24->writebuf) {
- err = -ENOMEM;
- goto err_struct;
- }
- } else {
- dev_warn(&client->dev,
- "cannot write due to controller restrictions.");
- }
- }
- at24->client[0] = client;
- /* use dummy devices for multiple-address chips */
- for (i = 1; i < num_addresses; i++) {
- at24->client[i] = i2c_new_dummy(client->adapter,
- client->addr + i);
- if (!at24->client[i]) {
- dev_err(&client->dev, "address 0x%02x unavailable\n",
- client->addr + i);
- err = -EADDRINUSE;
- goto err_clients;
- }
- }
- err = sysfs_create_bin_file(&client->dev.kobj, &at24->bin); //创建一个文件应用程序实际上是在/sys目录下的文件,而这个文件就是它函数创建的。
- if (err)
- goto err_clients;
- i2c_set_clientdata(client, at24);
- dev_info(&client->dev, "%zu byte %s EEPROM %s\n",
- at24->bin.size, client->name,
- writable ? "(writable)" : "(read-only)");
- dev_dbg(&client->dev,
- "page_size %d, num_addresses %d, write_max %d%s\n",
- chip.page_size, num_addresses,
- at24->write_max,
- use_smbus ? ", use_smbus" : "");
- /* export data to kernel code */
- if (chip.setup)
- chip.setup(&at24->macc, chip.context);
- return 0;
- err_clients:
- for (i = 1; i < num_addresses; i++)
- if (at24->client[i])
- i2c_unregister_device(at24->client[i]);
- kfree(at24->writebuf);
- err_struct:
- kfree(at24);
- err_out:
- dev_dbg(&client->dev, "probe error %d\n", err);
- return err;
- }
at24_bin_write:
- static ssize_t at24_bin_write(struct kobject *kobj, struct bin_attribute *attr,
- char *buf, loff_t off, size_t count)
- {
- struct at24_data *at24;
- at24 = dev_get_drvdata(container_of(kobj, struct device, kobj));
- return at24_write(at24, buf, off, count); //调用at24_write
- }
at24_write:
- static ssize_t at24_write(struct at24_data *at24, const char *buf, loff_t off,
- size_t count)
- {
- ssize_t retval = 0;
- if (unlikely(!count))
- return count;
- mutex_lock(&at24->lock);
- while (count) {
- ssize_t status;
- status = at24_eeprom_write(at24, buf, off, count); //调用at24_eeprom_write
- if (status <= 0) {
- if (retval == 0)
- retval = status;
- break;
- }
- buf += status;
- off += status;
- count -= status;
- retval += status;
- }
- mutex_unlock(&at24->lock);
- return retval;
- }
at24_eeprom_write:
- static ssize_t at24_eeprom_write(struct at24_data *at24, const char *buf,
- unsigned offset, size_t count)
- {
- struct i2c_client *client;
- struct i2c_msg msg;
- ssize_t status;
- unsigned long timeout, write_time;
- unsigned next_page;
- /* Get corresponding I2C address and adjust offset */
- client = at24_translate_offset(at24, &offset);
- /* write_max is at most a page */
- if (count > at24->write_max)
- count = at24->write_max;
- /* Never roll over backwards, to the start of this page */
- next_page = roundup(offset + 1, at24->chip.page_size);
- if (offset + count > next_page)
- count = next_page - offset;
- /* If we'll use I2C calls for I/O, set up the message */ //I2C的消息
- if (!at24->use_smbus) {
- int i = 0;
- msg.addr = client->addr;
- msg.flags = 0;
- /* msg.buf is u8 and casts will mask the values */
- msg.buf = at24->writebuf;
- if (at24->chip.flags & AT24_FLAG_ADDR16)
- msg.buf[i++] = offset >> 8;
- msg.buf[i++] = offset; //提供偏移地址
- memcpy(&msg.buf[i], buf, count); //拷贝用户发送数据
- msg.len = i + count; //设置长度
- }
- /*
- * Writes fail if the previous one didn't complete yet. We may
- * loop a few times until this one succeeds, waiting at least
- * long enough for one entire page write to work.
- */
- timeout = jiffies + msecs_to_jiffies(write_timeout);
- do {
- write_time = jiffies;
- if (at24->use_smbus) {
- status = i2c_smbus_write_i2c_block_data(client,
- offset, count, buf);
- if (status == 0)
- status = count;
- } else {
- status = i2c_transfer(client->adapter, &msg, 1); //交给I2C控制器完成
- if (status == 1)
- status = count;
- }
- dev_dbg(&client->dev, "write %zu@%d --> %zd (%ld)\n",
- count, offset, status, jiffies);
- if (status == count)
- return count;
- /* REVISIT: at HZ=100, this is sloooow */
- msleep(1);
- } while (time_before(write_time, timeout));
- return -ETIMEDOUT;
- }
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); //调用控制器中的算法
- mutex_unlock(&adap->bus_lock);
- return ret;
- } else {
- dev_dbg(&adap->dev, "I2C level transfers not supported\n");
- return -EOPNOTSUPP;
- }
- }
然后看一下i2c_bin_read:
- static ssize_t at24_bin_read(struct kobject *kobj, struct bin_attribute *attr,
- char *buf, loff_t off, size_t count)
- {
- struct at24_data *at24;
- at24 = dev_get_drvdata(container_of(kobj, struct device, kobj));
- return at24_read(at24, buf, off, count); //调用at24_read
- }
at24_read:
- static ssize_t at24_read(struct at24_data *at24,
- char *buf, loff_t off, size_t count)
- {
- ssize_t retval = 0;
- if (unlikely(!count))
- return count;
- mutex_lock(&at24->lock);
- while (count) {
- ssize_t status;
- status = at24_eeprom_read(at24, buf, off, count); //继续调用at24_eeprom_read
- if (status <= 0) {
- if (retval == 0)
- retval = status;
- break;
- }
- buf += status;
- off += status;
- count -= status;
- retval += status;
- }
- mutex_unlock(&at24->lock);
- return retval;
- }
at24_eeprom_read:
- static ssize_t at24_eeprom_read(struct at24_data *at24, char *buf,
- unsigned offset, size_t count)
- {
- struct i2c_msg msg[2];
- u8 msgbuf[2];
- struct i2c_client *client;
- int status, i;
- memset(msg, 0, sizeof(msg));
- client = at24_translate_offset(at24, &offset);
- if (count > io_limit)
- count = io_limit;
- /* Smaller eeproms can work given some SMBus extension calls */
- if (at24->use_smbus) {
- if (count > I2C_SMBUS_BLOCK_MAX)
- count = I2C_SMBUS_BLOCK_MAX;
- status = i2c_smbus_read_i2c_block_data(client, offset,
- count, buf);
- dev_dbg(&client->dev, "smbus read %zu@%d --> %d\n",
- count, offset, status);
- return (status < 0) ? -EIO : status;
- }
- i = 0;
- if (at24->chip.flags & AT24_FLAG_ADDR16)
- msgbuf[i++] = offset >> 8;
- msgbuf[i++] = offset; //设置偏移
- msg[0].addr = client->addr; //第一条消息,提供从设备地址
- msg[0].buf = msgbuf;
- msg[0].len = i;
- msg[1].addr = client->addr; //第二条消息
- msg[1].flags = I2C_M_RD; //flags是读
- msg[1].buf = buf; //提供数据量
- msg[1].len = count;
- status = i2c_transfer(client->adapter, msg, 2);
- dev_dbg(&client->dev, "i2c read %zu@%d --> %d\n",
- count, offset, status);
- if (status == 2)
- return count;
- else if (status >= 0)
- return -EIO;
- else
- return status;
- }
二、对驱动程序的修改和移植
I2C设备的注册:
- static void __init tq2440_machine_init(void)
- {
- s3c24xx_fb_set_platdata(&tq2440_fb_info);
- s3c_i2c0_set_platdata(NULL);
- platform_add_devices(tq2440_devices, ARRAY_SIZE(tq2440_devices));
- EmbedSky_machine_init();
- s3c2410_gpio_setpin(S3C2410_GPG12, 0);
- s3c2410_gpio_cfgpin(S3C2410_GPG12, S3C2410_GPIO_OUTPUT);
- s3c24xx_udc_set_platdata(&EmbedSky_udc_cfg);
- }
增加设备:
- static struct at24_platform_data at24c02 = {
- .byte_len = 2048 / 8,
- .page_size = 8,
- .flags = 0,
- };
- static struct i2c_board_info __initdata tq2440_i2c_devices[] = {
- {
- I2C_BOARD_INFO("24c02", 0x50),
- .platform_data = &at24c02,
- },
- };
然后在tq2440_machine_init中添加:
- i2c_register_board_info(0, tq2440_i2c_devices, ARRAY_SIZE(tq2440_i2c_devices));
然后添加头文件:
- #include <linux/i2c.h>
- #include <linux/i2c/at24.h>
然后会在开发板/sys/bus/i2c/devices/有一个0-0050的目录,有一个eeprom文件。
编写i2c-app.c文件:
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- int main()
- {
- char write_data[256],read_data[256];
- int fd;
- int i = 0;
- //打开at24c02对应的sys文件
- fd = open("/sys/bus/i2c/devices/0-0050/eeprom", O_RDWR);
- //写入数据
- for(i=0;i<256;i++)
- write_data[i] = i;
- lseek(fd, 0, SEEK_SET);
- write(fd, write_data, 256);
- //读出数据
- lseek(fd, 0, SEEK_SET);
- read(fd, read_data, 256);
- //打印对比
- for(i=0;i<256;i++)
- {
- if(i%16 == 0) printf("\r\n");
- printf("%3d ",read_data[i]);
- }
- printf("\n");
- close(fd);
- }
I2C自编设备驱动设计的更多相关文章
- [国嵌攻略][156][I2C自编设备驱动设计]
AT24C08的驱动在Linux内核中已经提供,在/drivers/misc/eeprom/at24.c文件中.在对应的probe函数中有一个创建/sys/.../eeprom文件的函数,应用程序通过 ...
- [连载]《C#通讯(串口和网络)框架的设计与实现》-3.设备驱动的设计
目 录 第三章 设备驱动的设计... 2 3.1 初始化设备... 4 3.2 运行设备接口设计... 4 3.3 ...
- Linux I2C设备驱动编写(二)
在(一)中简述了Linux I2C子系统的三个主要成员i2c_adapter.i2c_driver.i2c_client.三者的关系也在上一节进行了描述.应该已经算是对Linux I2C子系统有了初步 ...
- 【转】Linux I2C设备驱动编写(二)
原文网址:http://www.cnblogs.com/biglucky/p/4059582.html 在(一)中简述了Linux I2C子系统的三个主要成员i2c_adapter.i2c_drive ...
- i2c设备驱动注册
Linux I2C设备驱动编写(二) 原创 2014年03月16日 23:26:50 在(一)中简述了Linux I2C子系统的三个主要成员i2c_adapter.i2c_driver.i2c ...
- 《linux设备驱动开发详解》笔记——15 linux i2c驱动
结合实际代码和书中描述,可能跟书上有一定出入.本文后续芯片相关代码参考ZYNQ. 15.1 总体结构 如下图,i2c驱动分为如下几个重要模块 核心层core,完成i2c总线.设备.驱动模型,对用户提供 ...
- 《Linux设备驱动开发详解(第2版)》配套视频登录51cto教育频道
http://edu.51cto.com/course/course_id-379-page-1.html http://edu.51cto.com/course/course_id-379-page ...
- 《linux设备驱动开发详解》笔记——6字符设备驱动
6.1 字符设备驱动结构 先看看字符设备驱动的架构: 6.1.1 cdev cdev结构体是字符设备的核心数据结构,用于描述一个字符设备,cdev定义如下: #include <linux/cd ...
- 例说linux内核与应用数据通信(三):读写内核设备驱动文件
[版权声明:尊重原创,转载请保留出处:blog.csdn.net/shallnet.文章仅供学习交流.请勿用于商业用途] 读写设备文件也就是调用系统调用read()和write(),系 ...
随机推荐
- mybatis源码分析之01环境搭建
直接使用maven搭建一个mybatis的运行环境 1. pom.xml <?xml version="1.0" encoding="UTF-8"?> ...
- php strcasecmp()函数 语法
php strcasecmp()函数 语法 作用:比较两个字符串(不区分大小写)直线电机驱动器 语法:strcasecmp(string1,string2) 参数: 参数 描述 string1 必须, ...
- 【BZOJ3052&UOJ58】糖果公园(树上带修莫队)
题意:给定一个n个点的树,每个结点上有一种颜色c[i] 定义一条简单路径的偷税指数为simga (sigma w[i](i=0..a[j]))*v[j](j=0..m),其中a[i]为第i种颜色在路径 ...
- cannot access Input/output error
ls: cannot access Input/output errorls: cannot open directory .: Input/output error 硬盘故障,只读或只写,你可以d ...
- LOJ 2555 「CTSC2018」混合果汁——主席树
题目:https://loj.ac/problem/2555 二分答案,在可以选的果汁中,从价格最小的开始选. 按价格排序,每次可以选的就是一个前缀.对序列建主席树,以价格为角标,维护体积和.体积*价 ...
- 【丛林】HTML Table 表格浅谈(边框、隔行变色
此例子已经包含本文大部分内容,请对照参考.查看代码 > 定义和用法 table标签定义 HTML 表格. 创建表格的四要素:table.tr.th.td <table> 整个表格以& ...
- UPLOADIFY用法
把下面代码 this.settings.upload_url = SWFUpload.completeURL(this.settings.upload_url); this.settings.but ...
- 六. jenkins部署springboot项目(3)--windows环境--远程windows server服务器
前提:jenkins服务器和windows server服务器不在一台机器上 对于jenkins服务器上编译好的jar或war包如何推送到windows server服务器上. 参照网上的,在wind ...
- MATLAB 中几个颜色空间的坐标范围
在一些图象处理的程序中,我们经常要在不同的颜色空间操作,而且经常要把各颜色空间的坐标转化到 [0,1] 之间,这就需要知道一些常用颜色空间的坐标范围. 虽然可以通过颜色空间转化的公式推导这些范围,但是 ...
- webservice 学习笔记 1
Webservice----------->跨语言服务调用 (视频学习总结) 1-1.有OA系统 需要添加一个功能,登录之后显示天气情况 此时可以使用Webservice eg1: 气象局自己有 ...