1. Linux内核支持I2C通用设备驱动(用户态驱动:由应用层实现对硬件的控制可以称之为用户态驱动),实现文件位于drivers/i2c/i2c-dev.c,设备文件为/dev/i2c-0

2. I2C通用设备驱动以字符设备注册进内核的

  1. static const struct file_operations i2cdev_fops = {
  2. .owner = THIS_MODULE,
  3. .llseek = no_llseek,
  4. .read = i2cdev_read,
  5. .write = i2cdev_write,
  6. .unlocked_ioctl = i2cdev_ioctl,
  7. .open = i2cdev_open,
  8. .release = i2cdev_release,
  9. };
  10.  
  11. res = register_chrdev(I2C_MAJOR, "i2c", &i2cdev_fops);

3. 对设备文件进行读写时,可以调用read、write或者ioctl等方法,他们都是通过调用函数i2c_transfer来实现对I2C设备的操作的

  1. int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
  2. {
  3. int ret;
  4.  
  5. /* REVISIT the fault reporting model here is weak:
  6. *
  7. * - When we get an error after receiving N bytes from a slave,
  8. * there is no way to report "N".
  9. *
  10. * - When we get a NAK after transmitting N bytes to a slave,
  11. * there is no way to report "N" ... or to let the master
  12. * continue executing the rest of this combined message, if
  13. * that's the appropriate response.
  14. *
  15. * - When for example "num" is two and we successfully complete
  16. * the first message but get an error part way through the
  17. * second, it's unclear whether that should be reported as
  18. * one (discarding status on the second message) or errno
  19. * (discarding status on the first one).
  20. */
  21.  
  22. if (adap->algo->master_xfer) {
  23. #ifdef DEBUG
  24. for (ret = ; ret < num; ret++) {
  25. dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
  26. "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
  27. ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
  28. (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
  29. }
  30. #endif
  31.  
  32. if (in_atomic() || irqs_disabled()) {
  33. ret = mutex_trylock(&adap->bus_lock);
  34. if (!ret)
  35. /* I2C activity is ongoing. */
  36. return -EAGAIN;
  37. } else {
  38. mutex_lock_nested(&adap->bus_lock, adap->level);
  39. }
  40.  
  41. ret = adap->algo->master_xfer(adap,msgs,num);
  42. mutex_unlock(&adap->bus_lock);
  43.  
  44. return ret;
  45. } else {
  46. dev_dbg(&adap->dev, "I2C level transfers not supported\n");
  47. return -EOPNOTSUPP;
  48. }
  49. }

4. i2c_transfer通过代码可以看出,i2c_transfer 通过调用相应的 adapter 的 master_xfer 方法实现的,而 master_xfer 主要是根据 struct i2c_msg 类型的msgs来进行处理的。

  1. struct i2c_msg {
  2. __u16 addr; /* slave address */
  3. __u16 flags;
  4. #define I2C_M_TEN 0x0010 /* this is a ten bit chip address */
  5. #define I2C_M_RD 0x0001 /* read data, from slave to master */
  6. #define I2C_M_NOSTART 0x4000 /* if I2C_FUNC_PROTOCOL_MANGLING */
  7. #define I2C_M_REV_DIR_ADDR 0x2000 /* if I2C_FUNC_PROTOCOL_MANGLING */
  8. #define I2C_M_IGNORE_NAK 0x1000 /* if I2C_FUNC_PROTOCOL_MANGLING */
  9. #define I2C_M_NO_RD_ACK 0x0800 /* if I2C_FUNC_PROTOCOL_MANGLING */
  10. #define I2C_M_RECV_LEN 0x0400 /* length will be first received byte */
  11. __u16 len; /* msg length */
  12. __u8 *buf; /* pointer to msg data */
  13. };

5. I2C用户态驱动简单示例

(1)通过read、write实现对I2C设备的操作

  1. #include <stdio.h>
  2. #include <fcntl.h>
  3. #include <unistd.h>
  4. #include <malloc.h>
  5. #include <sys/ioctl.h>
  6. #include <string.h>
  7. #include <linux/i2c.h>
  8. #include <linux/i2c-dev.h>
  9.  
  10. #define SLAVE_ADDRESS 0x50
  11. #define I2C_WRITE_FLAG 0x00
  12. #define I2C_READ_FLAG 0x01
  13.  
  14. #define USAGE "xxx -r address length\nxxx -w address data1 data2...\n"
  15.  
  16. void i2c_write_bytes(int fd, unsigned char address, unsigned char* data, unsigned short len)
  17. {
  18. unsigned char* write_data = malloc(len + );
  19.  
  20. write_data[] = address;
  21. memcpy(&write_data[], data, len);
  22.  
  23. ioctl(fd, I2C_SLAVE, SLAVE_ADDRESS);
  24. ioctl(fd, I2C_TIMEOUT, );
  25. ioctl(fd, I2C_RETRIES, );
  26.  
  27. write(fd, write_data, len + );
  28.  
  29. printf("Write data success\n");
  30.  
  31. if(write_data != NULL)
  32. {
  33. free(write_data);
  34. write_data = NULL;
  35. }
  36. }
  37.  
  38. void i2c_read_bytes(int fd, unsigned char address, unsigned char* buf, unsigned short len)
  39. {
  40. ioctl(fd, I2C_SLAVE, SLAVE_ADDRESS);
  41. ioctl(fd, I2C_TIMEOUT, );
  42. ioctl(fd, I2C_RETRIES, );
  43.  
  44. write(fd, &address, );
  45.  
  46. read(fd, buf, len);
  47.  
  48. printf("buf[0] = 0x%x\n", buf[]);
  49.  
  50. printf("Read data success\n");
  51. }
  52.  
  53. int main(int argc, char* argv[])
  54. {
  55. int opt;
  56. int fd = -;
  57.  
  58. unsigned char address;
  59. unsigned short len = , i = ;
  60. unsigned char buf[] = {};
  61.  
  62. // Open device file
  63. fd = open("/dev/i2c-0", O_RDWR);
  64. if(fd < )
  65. {
  66. printf("Open file error\n");
  67. goto Exit;
  68. }
  69.  
  70. while((opt = getopt(argc, argv, "w:r:")) != -)
  71. {
  72. switch(opt)
  73. {
  74. case 'w':
  75. printf("optarg = %s\n", optarg);
  76. printf("optind = %d\n", optind);
  77. printf("argc = %d\n", argc);
  78. printf("argv[optind] = %s\n", argv[optind]);
  79.  
  80. address = (unsigned char)strtol(optarg, NULL, );
  81. printf("address = %x\n", address);
  82.  
  83. for(len = ; optind < argc; optind++, len++)
  84. {
  85. buf[len] = (unsigned char)strtol(argv[optind], NULL, );
  86. }
  87.  
  88. printf("len = %x\n", len);
  89.  
  90. i2c_write_bytes(fd, address, buf, len);
  91.  
  92. break;
  93.  
  94. case 'r':
  95. printf("optarg = %s\n", optarg);
  96. printf("optind = %d\n", optind);
  97. printf("argv[optind] = %s\n", argv[optind]);
  98.  
  99. address = (unsigned char)strtol(optarg, NULL, );
  100. printf("address = 0x%x\n", address);
  101.  
  102. len = (unsigned short)strtol(argv[optind], NULL, );
  103. printf("len = 0x%x\n", len);
  104.  
  105. i2c_read_bytes(fd, address, buf, len);
  106.  
  107. printf("Read content:\n");
  108. for(i = ; i < len; i++)
  109. {
  110. printf("0x%x ", buf[i]);
  111. }
  112. printf("\n");
  113. break;
  114.  
  115. default:
  116. printf("Invalid parameter\n");
  117. printf(USAGE);
  118. break;
  119. }
  120. }
  121.  
  122. Exit:
  123. close(fd);
  124.  
  125. return ;
  126. }

(2)通过ioctl实现对I2C设备的操作

  1. #include <stdio.h>
  2. #include <fcntl.h>
  3. #include <unistd.h>
  4. #include <malloc.h>
  5. #include <sys/ioctl.h>
  6. #include <string.h>
  7. #include <linux/i2c.h>
  8. #include <linux/i2c-dev.h>
  9.  
  10. #define SLAVE_ADDRESS 0x50
  11. #define I2C_WRITE_FLAG 0x00
  12. #define I2C_READ_FLAG 0x01
  13.  
  14. #define USAGE "xxx -r address length\nxxx -w address data1 data2...\n"
  15.  
  16. void i2c_write_bytes(int fd, unsigned char address, unsigned char* data, unsigned short len)
  17. {
  18. struct i2c_rdwr_ioctl_data e2prom_write_data;
  19.  
  20. e2prom_write_data.nmsgs = ;
  21. e2prom_write_data.msgs = malloc(sizeof(struct i2c_msg) * e2prom_write_data.nmsgs);
  22.  
  23. e2prom_write_data.msgs[].addr = SLAVE_ADDRESS;
  24. e2prom_write_data.msgs[].flags = I2C_WRITE_FLAG;
  25. e2prom_write_data.msgs[].len = len + ;//address data
  26. e2prom_write_data.msgs[].buf = malloc(e2prom_write_data.msgs[].len);
  27.  
  28. e2prom_write_data.msgs[].buf[] = address;
  29.  
  30. memcpy(&(e2prom_write_data.msgs[].buf[]), data, (size_t)len);
  31.  
  32. // Using ioctl to write data
  33. ioctl(fd, I2C_RDWR, (unsigned long)&e2prom_write_data);
  34.  
  35. printf("Write data success\n");
  36.  
  37. if(e2prom_write_data.msgs != NULL)
  38. {
  39. free(e2prom_write_data.msgs);
  40. e2prom_write_data.msgs = NULL;
  41. }
  42. }
  43.  
  44. void i2c_read_bytes(int fd, unsigned char address, unsigned char* buf, unsigned short len)
  45. {
  46. struct i2c_rdwr_ioctl_data e2prom_read_data;
  47.  
  48. e2prom_read_data.nmsgs = ;//Need writing address first, then reading
  49. e2prom_read_data.msgs = malloc(sizeof(struct i2c_msg) * e2prom_read_data.nmsgs);
  50.  
  51. e2prom_read_data.msgs[].addr = SLAVE_ADDRESS;
  52. e2prom_read_data.msgs[].flags = I2C_WRITE_FLAG;
  53. e2prom_read_data.msgs[].len = ;
  54. e2prom_read_data.msgs[].buf = malloc(e2prom_read_data.msgs[].len);
  55.  
  56. e2prom_read_data.msgs[].buf[] = address;
  57.  
  58. e2prom_read_data.msgs[].addr = SLAVE_ADDRESS;
  59. e2prom_read_data.msgs[].flags = I2C_READ_FLAG;
  60. e2prom_read_data.msgs[].len = len;
  61. e2prom_read_data.msgs[].buf = malloc(e2prom_read_data.msgs[].len);
  62. e2prom_read_data.msgs[].buf[] = 0x00;
  63.  
  64. // Using ioctl to read data
  65. ioctl(fd, I2C_RDWR, (unsigned long)&e2prom_read_data);
  66.  
  67. printf("e2prom_read_data.msgs[1].buf[0] = 0x%x\n", e2prom_read_data.msgs[].buf[]);
  68.  
  69. memcpy((void*)buf, (void*)(e2prom_read_data.msgs[].buf), (unsigned int)len);
  70.  
  71. if(e2prom_read_data.msgs != NULL)
  72. {
  73. free(e2prom_read_data.msgs);
  74. e2prom_read_data.msgs = NULL;
  75. }
  76. }
  77.  
  78. int main(int argc, char* argv[])
  79. {
  80. int opt;
  81. int fd = -;
  82.  
  83. unsigned char address;
  84. unsigned short len = , i = ;
  85. unsigned char buf[] = {};
  86.  
  87. // Open device file
  88. fd = open("/dev/i2c-0", O_RDWR);
  89. if(fd < )
  90. {
  91. printf("Open file error\n");
  92. goto Exit;
  93. }
  94.  
  95. while((opt = getopt(argc, argv, "w:r:")) != -)
  96. {
  97. switch(opt)
  98. {
  99. case 'w':
  100. printf("optarg = %s\n", optarg);
  101. printf("optind = %d\n", optind);
  102. printf("argc = %d\n", argc);
  103. printf("argv[optind] = %s\n", argv[optind]);
  104.  
  105. address = (unsigned char)strtol(optarg, NULL, );
  106. printf("address = %x\n", address);
  107.  
  108. for(len = ; optind < argc; optind++,len++)
  109. {
  110. buf[len] = (unsigned char)strtol(argv[optind], NULL, );
  111. }
  112.  
  113. printf("len = %x\n", len);
  114.  
  115. i2c_write_bytes(fd, address, buf, len);
  116.  
  117. break;
  118.  
  119. case 'r':
  120. printf("optarg = %s\n", optarg);
  121. printf("optind = %d\n", optind);
  122. printf("argv[optind] = %s\n", argv[optind]);
  123.  
  124. address = (unsigned char)strtol(optarg, NULL, );
  125. printf("address = 0x%x\n", address);
  126.  
  127. len = (unsigned short)strtol(argv[optind], NULL, );
  128. printf("len = 0x%x\n", len);
  129.  
  130. i2c_read_bytes(fd, address, buf, len);
  131.  
  132. printf("Read content:\n");
  133. for(i = ; i < len; i++)
  134. {
  135. printf("0x%x ", buf[i]);
  136. }
  137. printf("\n");
  138. break;
  139.  
  140. default:
  141. printf("Invalid parameter\n");
  142. printf(USAGE);
  143. break;
  144. }
  145. }
  146.  
  147. Exit:
  148. close(fd);
  149.  
  150. return ;
  151. }

Linux I2C驱动--用户态驱动简单示例的更多相关文章

  1. linuxok6410的I2C驱动分析---用户态驱动

    3  i2c-dev 3.1 概述 之前在介绍I2C子系统时,提到过使用i2c-dev.c文件在应用程序中实现我们的I2C从设备驱动.不过,它实现的是一个虚拟,临时的i2c_client,随着设备文件 ...

  2. 聊聊Linux用户态驱动设计

    序言 设备驱动可以运行在内核态,也可以运行在用户态,用户态驱动的利弊网上有很多的讨论,而且有些还上升到政治性上,这里不再多做讨论.不管用户态驱动还是内核态驱动,他们都有各自的缺点.内核态驱动的问题是: ...

  3. Linux用户态驱动设计

    聊聊Linux用户态驱动设计   序言 设备驱动可以运行在内核态,也可以运行在用户态,用户态驱动的利弊网上有很多的讨论,而且有些还上升到政治性上,这里不再多做讨论.不管用户态驱动还是内核态驱动,他们都 ...

  4. I2C用户态驱动设计

    一.用户态驱动模型 1.1 I2C通用驱动代码 i2c_dev_init: static int __init i2c_dev_init(void) { int res; printk(KERN_IN ...

  5. [国嵌攻略][155][I2C用户态驱动设计]

    用户态驱动模型 用户态驱动模型首先是一个应用程序,其次是在这个用户程序中通过内核调用来驱动设备. IIC通用驱动代码 IIC通用驱动程序的代码在/drivers/i2c/i2c-dev.c中.一次读操 ...

  6. [windows驱动]内核态驱动架构

    1.windows驱动简介: 1.1 windows组件简介: 1.2 windows驱动类型: windows驱动分为两种基本类型: 用户态驱动在用户态下执行.它们一般提供一套win32应用程序和内 ...

  7. useradd linux系统创建用户和设置密码简单脚本-1

    useradd linux系统创建用户和设置密码简单脚本-1 linux_wangqiang 2019-12-04 20:51:18 65 收藏展开#!/bin/bash#快速创建用户 使用$1第一个 ...

  8. 用户态驱动--UIO机制的实现【转】

    转自:https://blog.csdn.net/u013982161/article/details/51584900 1 uio理论部分   1.1为什么出现了UIO? 硬件设备可以根据功能分为网 ...

  9. Linux探秘之用户态与内核态

    一. Unix/Linux的体系架构 如上图所示,从宏观上来看,Linux操作系统的体系架构分为用户态和内核态(或者用户空间和内核).内核从本质上看是一种软件——控制计算机的硬件资源,并提供上层应用程 ...

随机推荐

  1. c语言条件编译#ifdef与#if defined

    c语言条件编译#ifdef与#if defined   c语言条件编译#ifdef与#if defined 摘自:https://www.cnblogs.com/zhangshenghui/p/566 ...

  2. HaXe以及OpenFL部署

    HaXe以及OpenFL部署 Haxe是一种跨平台的编程语言,本文并未HAXE的教程,只是针对OPENFL以及HAXE的部署教程.HAXE的语法非常类似AS3,由于国内部署HAXE艰难,经常下载到一半 ...

  3. linux系统学习(二)

    文件,目录 pwd:查看当前目录 Print Working Directory cd,ls(ll),mkdir -p Change Directory List Make Directory du ...

  4. 在Chrome 39中无法使用插件

    在chrome 42+版本中在开启npapi选项.   1.打开插件面板,在地址栏中输入 chrome://plugins   2.找到npScreenCapture插件,点击始终允许选框 3允许控件

  5. “undefined reference to JNI_GetCreatedJavaVM”和“File format not recognized”错误原因分析

    "undefined reference to JNI_GetCreatedJavaVM"和"File format not recognized"错误原因分析 ...

  6. xfce4快捷键设置

    xfce4的"Keyboard"可以方便的设置启动应用程序的快捷键. 例如,添加xfce4-terminal和emacs的启动快捷键 Alt+F3打开"Applicati ...

  7. JavaScript语言精粹 笔记02 函数

    函数函数对象函数字面量调用参数返回异常给类型增加方法递归作用域闭包回调模块级联套用记忆   函数 1 函数对象 在JS中函数就是对象.对象是“名/值”对的集合并拥有一个连接到原型对象的隐藏连接.对象字 ...

  8. Chrome与Firefox对于时间处理的不同

    new Date() 函数传参数,在火狐浏览器和谷歌浏览器控制台运行,会得到不同的结果,刚开始觉得不可能,后来实际操作才发现此陷阱. 在Firefox中: var dString = "20 ...

  9. centos7 安装dnf包管理器和常用命令

    Installing DNF Currently the DNF package comes from the EPEL repository, so if your Linux system is ...

  10. on where having总结

    1. ON 和WHERE 所有的查询都回产生一个中间临时报表,查询结果就是从返回临时报表中得到.ON和WHERE后面所跟限制条件的区别,主要与限制条件起作用的时机有关, ON根据限制条件对数据库记录进 ...