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(),系 ...
随机推荐
- Hadoop编程调用HDFS(PYTHON)
1.运行环境 开发工具:PyCharm Python 版本:3.5 Hadoop环境: Cloudera QuickStart 2.GITHUB地址 https://github.com/nbfujx ...
- 4412 使用小度wifi
本文转载至:https://blog.csdn.net/robertsong2004/article/details/42985223 作者:刘老师,华清远见嵌入式学院讲师. FS_4412可以同链接 ...
- 4412 RS485
一.485硬件原理 差分对传输数据的原理 IO数据的传输→差分对 rs232传输的距离在15米以下,RS485传输距离是几十米到1000米以上 为什么485可以传输这么远 差分对的机制可以降低电磁场的 ...
- C#中的6种常见的集合
1.动态数组(ArrayList) 动态数组(ArrayList)代表了可被单独索引的对象的有序集合.它基本上可以替代一个数组.但是,与数组不同的是,您可以使用索引在指定的位置添加和移除项目,动态数组 ...
- Win7隐藏登录界面中的用户(不建议HOME版使用)
一天一點 能登多高,靠的不是双脚!能看多远,靠的不是双眼!人生路,贵在坚持! Win7隐藏登录界面中的用户(不建议HOME版使用) Win7中如何隐藏不想出现在登录界面中的用户 在Windows系统管 ...
- Linux 删除特殊文件名的文件
1.文件名含有特殊字符: 1) 执行 ls -i 命令 ,文件前面会出现一个数字,这个数字是文件的节点号 2) 使用find命令删除 find ./ -inum 节点号 -delete 2.文件名是以 ...
- handler消息机制入门
handler消息机制入门 为什么要用handle? 我们在网络上读取图片信息时,是不能把耗时操作放在主线程里面的,当我们在子线程中获取到了图片的消息的时候,我们就需要把这个数据传给主线程. 而直接使 ...
- phpredis报错信息:protocol error, got 'o' as reply type byte解决方案
今天在前端调用PHP的接口时,有报错信息为:protocol error, got 'o' as reply type byte另外此错误有几率会重现,并不是必现的.十分疑惑,遂百度一下,发现是red ...
- Gson字符串转换对象数组
public class Input { private String title; private int formId; private String content; public String ...
- ASP.NET MVC学习系列(二)-WebAPI请求 转载https://www.cnblogs.com/babycool/p/3922738.html
继续接着上文 ASP.NET MVC学习系列(一)-WebAPI初探 来看看对于一般前台页面发起的get和post请求,我们在Web API中要如何来处理. 这里我使用Jquery 来发起异步请求实现 ...