linux IIC子系统分析(七)——实例分析通过i2c-dev操作I2C设备

在前面的platform device和platform driver初始化中,我们已经实现了I2C总线驱动(adapter),但是我们的设备驱动还没有实现。如果我们现在要访问I2C设备(比如eeprom),我知道的有三总方法:

(一)i2c-dev操作I2C设备:不用添加设备驱动,用户直接在应用层完成对具体I2C 设备的驱动工作。

(二)sysfs操作I2C设备:需添加设备驱动,通过sys展示出来的文件操作设备(比如/sys/devices/platform/s3c2440-i2c/i2c-0/0-0050/eeprom

(三)设备节点操作i2C设备:添加设备驱动,为设备驱动创建设备节点,从/dev访问I2C设备(比如/dev/eeprom


AT24C02芯片在《linux IIC子系统分析(一)——AT24C02芯片简介已介绍

下面以AT24C02为I2C设备实例,分别实现这三种方法。

用户要在应用程序的dev 目录下看到i2c设备节点,需要内核配置IIC选项:

I2C support-->

I2C device interface 

选上该选项之后,内核将会把i2c-dev.c文件编译进内核,同时产生设备节点/dev/i2c/n,它代表一个可操作的适配器。如果不配置I2C device interface,将看不到节点/dev/i2c/n。 view plain

查看include/linux/i2c-dev.h文件,可以看到i2c-dev支持的IOCTL命令。如i2c-dev IOCTL命令

#define I2C_RETRIES                    0x0701     /*设置收不到ACK时的重试次数*/

#define I2C_TIMEOUT                 0x0702 /* 设置超时时限的jiffies */

#define I2C_SLAVE                       0x0703 /*设置从机地址 */

#define I2C_SLAVE_FORCE        0x0706  /* 强制设置从机地址 */

#define I2C_TENBIT                      0x0704 /*选择地址位长:=0 for 7bit , != 0 for 10 bit */

#define I2C_FUNCS                      0x0705 /*获取适配器支持的功能 */

#define I2C_RDWR                        0x0707                                 /*Combined R/W transfer (one STOP only)  */

#define I2C_PEC                            0x0708                                  /* != 0 to use PEC with SMBus */

#define I2C_SMBUS                      0x0720                                   /*SMBus transfer  */

  co

  1. /*i2c_test.c 
  2. */  
  3. #include <stdio.h>  
  4. #include <linux/types.h>  
  5. #include <stdlib.h>  
  6. #include <fcntl.h>  
  7. #include <unistd.h>  
  8. #include <sys/types.h>  
  9. #include <sys/ioctl.h>  
  10. #include <errno.h>  
  11. //#include <linux/i2c-dev.h>  
  12. #define I2C_RETRIES 0x070  
  13. #define I2C_TIMEOUT 0x0702  
  14. #define I2C_RDWR 0x0707   
  15. /*********定义struct i2c_rdwr_ioctl_data和struct i2c_msg,要和内核一致*******/  
  16. struct i2c_msg  
  17. {  
  18.     unsigned short addr;  
  19.     unsigned short flags;     
  20.     #define I2C_M_TEN 0x0010  
  21.     #define I2C_M_RD 0x0001  
  22.     unsigned short len;  
  23.     unsigned charchar *buf;  
  24. };  
  25. struct i2c_rdwr_ioctl_data  
  26. {  
  27.     struct i2c_msg *msgs;  
  28.     int nmsgs;   
  29.     /* nmsgs这个数量决定了有多少开始信号,对于“单开始时序”,取1*/  
  30. };  
  31.   
  32. int main()  
  33. {  
  34.         int fd,ret;  
  35.         struct i2c_rdwr_ioctl_data e2prom_data;  
  36.         fd=open("/dev/i2c/0",O_RDWR);  
  37.         if(fd<0)  
  38.         {  
  39.                 perror("open error");  
  40.         }  
  41.         e2prom_data.nmsgs=2;   
  42.         e2prom_data.msgs=(struct i2c_msg*)malloc(e2prom_data.nmsgs*sizeof(struct i2c_msg));  
  43.         if(!e2prom_data.msgs)  
  44.         {  
  45.                 perror("malloc error");  
  46.                 exit(1);  
  47.         }  
  48.         ioctl(fd,I2C_TIMEOUT,1);  
  49.         ioctl(fd,I2C_RETRIES,2);  
  50.         /***write data to e2prom**/  
  51.   
  52.         e2prom_data.nmsgs=1;  
  53.         (e2prom_data.msgs[0]).len=2;   
  54.         (e2prom_data.msgs[0]).addr=0x50;  
  55.         (e2prom_data.msgs[0]).flags=0;   
  56.         (e2prom_data.msgs[0]).buf=(unsigned char*)malloc(2);  
  57.         (e2prom_data.msgs[0]).buf[0]=0x10;  
  58.         (e2prom_data.msgs[0]).buf[1]=0x58;  
  59.         ret=ioctl(fd,I2C_RDWR,(unsigned long)&e2prom_data);  
  60.         if(ret<0)  
  61.         {  
  62.                 perror("ioctl error1");  
  63.         }  
  64.         sleep(1);  
  65.         /******read data from e2prom*******/  
  66.         e2prom_data.nmsgs=2;  
  67.         (e2prom_data.msgs[0]).len=1;   
  68.         (e2prom_data.msgs[0]).addr=0x50;   
  69.         (e2prom_data.msgs[0]).flags=0;//write  
  70.         (e2prom_data.msgs[0]).buf[0]=0x10;  
  71.         (e2prom_data.msgs[1]).len=1;  
  72.         (e2prom_data.msgs[1]).addr=0x50;  
  73.         (e2prom_data.msgs[1]).flags=I2C_M_RD;//read  
  74.         (e2prom_data.msgs[1]).buf=(unsigned char*)malloc(1);  
  75.         (e2prom_data.msgs[1]).buf[0]=0;  
  76.         ret=ioctl(fd,I2C_RDWR,(unsigned long)&e2prom_data);  
  77.         if(ret<0)  
  78.         {  
  79.                 perror("ioctl error2");  
  80.         }  
  81.         printf("buff[0]=%x\n",(e2prom_data.msgs[1]).buf[0]);  
  82.         close(fd);  
  83.         return 0;  
  84. }  

经测试,可以正常输出:buff[0]=58


从上面的代码中可以看出来,用户直接在应用层通过适配器dev/i2c/0接口操作连接在I2C0总线上的设备。这种操作方式需要对I2C通讯总线以及I2C设备都非常熟悉才能够很好的操作,并且它的可移植性非常的差。对那些非常简单的I2C设备进行简单的操作可以使用这种方法。


上面代码是我自己写的一个测试程序。

如果要在应用层实现对EEPROM设备的所有操作接口函数,友善支臂有提供更好的应用程序。

以下代码为友善支臂提供,以供参考:

24cXX.h

  1. /*************************************************************************** 
  2.     copyright            : (C) by 2003-2004 Stefano Barbato 
  3.     email                : stefano@codesink.org 
  4.  
  5.     $Id: 24cXX.h,v 1.6 2004/02/29 11:05:28 tat Exp $ 
  6.  ***************************************************************************/  
  7.   
  8. /*************************************************************************** 
  9.  *                                                                         * 
  10.  *   This program is free software; you can redistribute it and/or modify  * 
  11.  *   it under the terms of the GNU General Public License as published by  * 
  12.  *   the Free Software Foundation; either version 2 of the License, or     * 
  13.  *   (at your option) any later version.                                   * 
  14.  *                                                                         * 
  15.  ***************************************************************************/  
  16. #ifndef _24CXX_H_  
  17. #define _24CXX_H_  
  18. #include <linux/i2c-dev.h>  
  19. #include <linux/i2c.h>  
  20.   
  21. #define EEPROM_TYPE_UNKNOWN 0  
  22. #define EEPROM_TYPE_8BIT_ADDR   1  
  23. #define EEPROM_TYPE_16BIT_ADDR  2  
  24.   
  25. struct eeprom  
  26. {  
  27.     charchar *dev;  // device file i.e. /dev/i2c-N  
  28.     int addr;   // i2c address  
  29.     int fd;     // file descriptor  
  30.     int type;   // eeprom type  
  31. };  
  32.   
  33. /* 
  34.  * opens the eeprom device at [dev_fqn] (i.e. /dev/i2c-N) whose address is 
  35.  * [addr] and set the eeprom_24c32 [e] 
  36.  */  
  37. int eeprom_open(charchar *dev_fqn, int addr, int type, struct eeprom*);  
  38. /* 
  39.  * closees the eeprom device [e]  
  40.  */  
  41. int eeprom_close(struct eeprom *e);  
  42. /* 
  43.  * read and returns the eeprom byte at memory address [mem_addr]  
  44.  * Note: eeprom must have been selected by ioctl(fd,I2C_SLAVE,address)  
  45.  */  
  46. int eeprom_read_byte(struct eeprom* e, __u16 mem_addr);  
  47. /* 
  48.  * read the current byte 
  49.  * Note: eeprom must have been selected by ioctl(fd,I2C_SLAVE,address)  
  50.  */  
  51. int eeprom_read_current_byte(struct eeprom *e);  
  52. /* 
  53.  * writes [data] at memory address [mem_addr]  
  54.  * Note: eeprom must have been selected by ioctl(fd,I2C_SLAVE,address)  
  55.  */  
  56. int eeprom_write_byte(struct eeprom *e, __u16 mem_addr, __u8 data);  
  57.   
  58. #endif  


24cXX.c 

  1. /*************************************************************************** 
  2.     copyright            : (C) by 2003-2004 Stefano Barbato 
  3.     email                : stefano@codesink.org 
  4.  
  5.     $Id: 24cXX.h,v 1.6 2004/02/29 11:05:28 tat Exp $ 
  6.  ***************************************************************************/  
  7.   
  8. /*************************************************************************** 
  9.  *                                                                         * 
  10.  *   This program is free software; you can redistribute it and/or modify  * 
  11.  *   it under the terms of the GNU General Public License as published by  * 
  12.  *   the Free Software Foundation; either version 2 of the License, or     * 
  13.  *   (at your option) any later version.                                   * 
  14.  *                                                                         * 
  15.  ***************************************************************************/  
  16. #include <stdio.h>  
  17. #include <fcntl.h>  
  18. #include <unistd.h>  
  19. #include <stdlib.h>  
  20. #include <linux/fs.h>  
  21. #include <sys/types.h>  
  22. #include <sys/ioctl.h>  
  23. #include <errno.h>  
  24. #include <assert.h>  
  25. #include <string.h>  
  26. #include "24cXX.h"  
  27.   
  28.   
  29. static inline __s32 i2c_smbus_access(int file, char read_write, __u8 command,   
  30.                                      int size, union i2c_smbus_data *data)  
  31. {  
  32.     struct i2c_smbus_ioctl_data args;  
  33.   
  34.     args.read_write = read_write;  
  35.     args.command = command;  
  36.     args.size = size;  
  37.     args.data = data;  
  38.     return ioctl(file,I2C_SMBUS,&args);  
  39. }  
  40.   
  41.   
  42. static inline __s32 i2c_smbus_write_quick(int file, __u8 value)  
  43. {  
  44.     return i2c_smbus_access(file,value,0,I2C_SMBUS_QUICK,NULL);  
  45. }  
  46.       
  47. static inline __s32 i2c_smbus_read_byte(int file)  
  48. {  
  49.     union i2c_smbus_data data;  
  50.     if (i2c_smbus_access(file,I2C_SMBUS_READ,0,I2C_SMBUS_BYTE,&data))  
  51.         return -1;  
  52.     else  
  53.         return 0x0FF & data.byte;  
  54. }  
  55.   
  56. static inline __s32 i2c_smbus_write_byte(int file, __u8 value)  
  57. {  
  58.     return i2c_smbus_access(file,I2C_SMBUS_WRITE,value,  
  59.                             I2C_SMBUS_BYTE,NULL);  
  60. }  
  61.   
  62. static inline __s32 i2c_smbus_read_byte_data(int file, __u8 command)  
  63. {  
  64.     union i2c_smbus_data data;  
  65.     if (i2c_smbus_access(file,I2C_SMBUS_READ,command,  
  66.                          I2C_SMBUS_BYTE_DATA,&data))  
  67.         return -1;  
  68.     else  
  69.         return 0x0FF & data.byte;  
  70. }  
  71.   
  72. static inline __s32 i2c_smbus_write_byte_data(int file, __u8 command,   
  73.                                               __u8 value)  
  74. {  
  75.     union i2c_smbus_data data;  
  76.     data.byte = value;  
  77.     return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,  
  78.                             I2C_SMBUS_BYTE_DATA, &data);  
  79. }  
  80.   
  81. static inline __s32 i2c_smbus_read_word_data(int file, __u8 command)  
  82. {  
  83.     union i2c_smbus_data data;  
  84.     if (i2c_smbus_access(file,I2C_SMBUS_READ,command,  
  85.                          I2C_SMBUS_WORD_DATA,&data))  
  86.         return -1;  
  87.     else  
  88.         return 0x0FFFF & data.word;  
  89. }  
  90.   
  91. static inline __s32 i2c_smbus_write_word_data(int file, __u8 command,   
  92.                                               __u16 value)  
  93. {  
  94.     union i2c_smbus_data data;  
  95.     data.word = value;  
  96.     return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,  
  97.                             I2C_SMBUS_WORD_DATA, &data);  
  98. }  
  99.   
  100. static inline __s32 i2c_smbus_process_call(int file, __u8 command, __u16 value)  
  101. {  
  102.     union i2c_smbus_data data;  
  103.     data.word = value;  
  104.     if (i2c_smbus_access(file,I2C_SMBUS_WRITE,command,  
  105.                          I2C_SMBUS_PROC_CALL,&data))  
  106.         return -1;  
  107.     else  
  108.         return 0x0FFFF & data.word;  
  109. }  
  110.   
  111.   
  112. /* Returns the number of read bytes */  
  113. static inline __s32 i2c_smbus_read_block_data(int file, __u8 command,   
  114.                                               __u8 *values)  
  115. {  
  116.     union i2c_smbus_data data;  
  117.     int i;  
  118.     if (i2c_smbus_access(file,I2C_SMBUS_READ,command,  
  119.                          I2C_SMBUS_BLOCK_DATA,&data))  
  120.         return -1;  
  121.     else {  
  122.         for (i = 1; i <= data.block[0]; i++)  
  123.             values[i-1] = data.block[i];  
  124.         return data.block[0];  
  125.     }  
  126. }  
  127.   
  128. static inline __s32 i2c_smbus_write_block_data(int file, __u8 command,   
  129.                                                __u8 length, __u8 *values)  
  130. {  
  131.     union i2c_smbus_data data;  
  132.     int i;  
  133.     if (length > 32)  
  134.         length = 32;  
  135.     for (i = 1; i <= length; i++)  
  136.         data.block[i] = values[i-1];  
  137.     data.block[0] = length;  
  138.     return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,  
  139.                             I2C_SMBUS_BLOCK_DATA, &data);  
  140. }  
  141.   
  142. /* Returns the number of read bytes */  
  143. static inline __s32 i2c_smbus_read_i2c_block_data(int file, __u8 command,  
  144.                                                   __u8 *values)  
  145. {  
  146.     union i2c_smbus_data data;  
  147.     int i;  
  148.     if (i2c_smbus_access(file,I2C_SMBUS_READ,command,  
  149.                           I2C_SMBUS_I2C_BLOCK_DATA,&data))  
  150.         return -1;  
  151.     else {  
  152.         for (i = 1; i <= data.block[0]; i++)  
  153.             values[i-1] = data.block[i];  
  154.         return data.block[0];  
  155.     }  
  156. }  
  157.   
  158. static inline __s32 i2c_smbus_write_i2c_block_data(int file, __u8 command,  
  159.                                                __u8 length, __u8 *values)  
  160. {  
  161.     union i2c_smbus_data data;  
  162.     int i;  
  163.     if (length > 32)  
  164.         length = 32;  
  165.     for (i = 1; i <= length; i++)  
  166.         data.block[i] = values[i-1];  
  167.     data.block[0] = length;  
  168.     return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,  
  169.                             I2C_SMBUS_I2C_BLOCK_DATA, &data);  
  170. }  
  171.   
  172. /* Returns the number of read bytes */  
  173. static inline __s32 i2c_smbus_block_process_call(int file, __u8 command,  
  174.                                                  __u8 length, __u8 *values)  
  175. {  
  176.     union i2c_smbus_data data;  
  177.     int i;  
  178.     if (length > 32)  
  179.         length = 32;  
  180.     for (i = 1; i <= length; i++)  
  181.         data.block[i] = values[i-1];  
  182.     data.block[0] = length;  
  183.     if (i2c_smbus_access(file,I2C_SMBUS_WRITE,command,  
  184.                          I2C_SMBUS_BLOCK_PROC_CALL,&data))  
  185.         return -1;  
  186.     else {  
  187.         for (i = 1; i <= data.block[0]; i++)  
  188.             values[i-1] = data.block[i];  
  189.         return data.block[0];  
  190.     }  
  191. }  
  192.   
  193. static int i2c_write_1b(struct eeprom *e, __u8 buf)  
  194. {  
  195.     int r;  
  196.     // we must simulate a plain I2C byte write with SMBus functions  
  197.     r = i2c_smbus_write_byte(e->fd, buf);  
  198.     if(r < 0)  
  199.         fprintf(stderr, "Error i2c_write_1b: %s\n", strerror(errno));  
  200.     usleep(10);  
  201.     return r;  
  202. }  
  203.   
  204. static int i2c_write_2b(struct eeprom *e, __u8 buf[2])  
  205. {  
  206.     int r;  
  207.     // we must simulate a plain I2C byte write with SMBus functions  
  208.     r = i2c_smbus_write_byte_data(e->fd, buf[0], buf[1]);  
  209.     if(r < 0)  
  210.         fprintf(stderr, "Error i2c_write_2b: %s\n", strerror(errno));  
  211.     usleep(10);  
  212.     return r;  
  213. }  
  214.   
  215. static int i2c_write_3b(struct eeprom *e, __u8 buf[3])  
  216. {  
  217.     int r;  
  218.     // we must simulate a plain I2C byte write with SMBus functions  
  219.     // the __u16 data field will be byte swapped by the SMBus protocol  
  220.     r = i2c_smbus_write_word_data(e->fd, buf[0], buf[2] << 8 | buf[1]);  
  221.     if(r < 0)  
  222.         fprintf(stderr, "Error i2c_write_3b: %s\n", strerror(errno));  
  223.     usleep(10);  
  224.     return r;  
  225. }  
  226.   
  227.   
  228. #define CHECK_I2C_FUNC( var, label ) \  
  229.     do {    if(0 == (var & label)) { \  
  230.         fprintf(stderr, "\nError: " \  
  231.             #label " function is required. Program halted.\n\n"); \  
  232.         exit(1); } \  
  233.     } while(0);  
  234.   
  235. int eeprom_open(charchar *dev_fqn, int addr, int type, struct eeprom* e)  
  236. {  
  237.     int funcs, fd, r;  
  238.     e->fd = e->addr = 0;  
  239.     e->dev = 0;  
  240.       
  241.     fd = open(dev_fqn, O_RDWR);  
  242.     if(fd <= 0)  
  243.     {  
  244.         fprintf(stderr, "Error eeprom_open: %s\n", strerror(errno));  
  245.         return -1;  
  246.     }  
  247.   
  248.     // get funcs list  
  249.     if((r = ioctl(fd, I2C_FUNCS, &funcs) < 0))  
  250.     {  
  251.         fprintf(stderr, "Error eeprom_open: %s\n", strerror(errno));  
  252.         return -1;  
  253.     }  
  254.   
  255.       
  256.     // check for req funcs  
  257.     CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_READ_BYTE );  
  258.     CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_WRITE_BYTE );  
  259.     CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_READ_BYTE_DATA );  
  260.     CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_WRITE_BYTE_DATA );  
  261.     CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_READ_WORD_DATA );  
  262.     CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_WRITE_WORD_DATA );  
  263.   
  264.     // set working device  
  265.     if( ( r = ioctl(fd, I2C_SLAVE, addr)) < 0)  
  266.     {  
  267.         fprintf(stderr, "Error eeprom_open: %s\n", strerror(errno));  
  268.         return -1;  
  269.     }  
  270.     e->fd = fd;  
  271.     e->addr = addr;  
  272.     e->dev = dev_fqn;  
  273.     e->type = type;  
  274.     return 0;  
  275. }  
  276.   
  277. int eeprom_close(struct eeprom *e)  
  278. {  
  279.     close(e->fd);  
  280.     e->fd = -1;  
  281.     e->dev = 0;  
  282.     e->type = EEPROM_TYPE_UNKNOWN;  
  283.     return 0;  
  284. }  
  285.   
  286. #if 0  
  287. int eeprom_24c32_write_byte(struct eeprom *e, __u16 mem_addr, __u8 data)  
  288. {  
  289.     __u8 buf[3] = { (mem_addr >> 8) & 0x00ff, mem_addr & 0x00ff, data };  
  290.     return i2c_write_3b(e, buf);  
  291. }  
  292.   
  293.   
  294. int eeprom_24c32_read_current_byte(struct eeprom* e)  
  295. {  
  296.     ioctl(e->fd, BLKFLSBUF); // clear kernel read buffer  
  297.     return i2c_smbus_read_byte(e->fd);  
  298. }  
  299.   
  300. int eeprom_24c32_read_byte(struct eeprom* e, __u16 mem_addr)  
  301. {  
  302.     int r;  
  303.     ioctl(e->fd, BLKFLSBUF); // clear kernel read buffer  
  304.     __u8 buf[2] = { (mem_addr >> 8) & 0x0ff, mem_addr & 0x0ff };  
  305.     r = i2c_write_2b(e, buf);  
  306.     if (r < 0)  
  307.         return r;  
  308.     r = i2c_smbus_read_byte(e->fd);  
  309.     return r;  
  310. }  
  311. #endif  
  312.   
  313.   
  314. int eeprom_read_current_byte(struct eeprom* e)  
  315. {  
  316.     ioctl(e->fd, BLKFLSBUF); // clear kernel read buffer  
  317.     return i2c_smbus_read_byte(e->fd);  
  318. }  
  319.   
  320. int eeprom_read_byte(struct eeprom* e, __u16 mem_addr)  
  321. {  
  322.     int r;  
  323.     ioctl(e->fd, BLKFLSBUF); // clear kernel read buffer  
  324.     if(e->type == EEPROM_TYPE_8BIT_ADDR)  
  325.     {  
  326.         __u8 buf =  mem_addr & 0x0ff;  
  327.         r = i2c_write_1b(e, buf);  
  328.     } else if(e->type == EEPROM_TYPE_16BIT_ADDR) {  
  329.         __u8 buf[2] = { (mem_addr >> 8) & 0x0ff, mem_addr & 0x0ff };  
  330.         r = i2c_write_2b(e, buf);  
  331.     } else {  
  332.         fprintf(stderr, "ERR: unknown eeprom type\n");  
  333.         return -1;  
  334.     }  
  335.     if (r < 0)  
  336.         return r;  
  337.     r = i2c_smbus_read_byte(e->fd);  
  338.     return r;  
  339. }  
  340.   
  341. int eeprom_write_byte(struct eeprom *e, __u16 mem_addr, __u8 data)  
  342. {  
  343.     if(e->type == EEPROM_TYPE_8BIT_ADDR) {  
  344.         __u8 buf[2] = { mem_addr & 0x00ff, data };  
  345.         return i2c_write_2b(e, buf);  
  346.     } else if(e->type == EEPROM_TYPE_16BIT_ADDR) {  
  347.         __u8 buf[3] =   
  348.             { (mem_addr >> 8) & 0x00ff, mem_addr & 0x00ff, data };  
  349.         return i2c_write_3b(e, buf);  
  350.     }   
  351.     fprintf(stderr, "ERR: unknown eeprom type\n");  
  352.     return -1;  
  353. }  

copeeprog.c

  1. /*************************************************************************** 
  2.     copyright            : (C) by 2009 Guangzhou FriendlyaRM, in China 
  3.     email                : capbily@163.com 
  4.     website      : http://www.arm9.net 
  5.  
  6.  ***************************************************************************/  
  7.   
  8. #include <stdio.h>  
  9. #include <fcntl.h>  
  10. #include <getopt.h>  
  11. #include <unistd.h>  
  12. #include <stdlib.h>  
  13. #include <errno.h>  
  14. #include <string.h>  
  15. #include <sys/types.h>  
  16. #include <sys/stat.h>  
  17. #include "24cXX.h"  
  18.   
  19. #define usage_if(a) do { do_usage_if( a , __LINE__); } while(0);  
  20. void do_usage_if(int b, int line)  
  21. {  
  22.     const static charchar *eeprog_usage =   
  23.         "I2C-24C08(256 bytes) Read/Write Program, ONLY FOR TEST!\n"  
  24.         "FriendlyARM Computer Tech. 2009\n";  
  25.     if(!b)  
  26.         return;  
  27.     fprintf(stderr, "%s\n[line %d]\n", eeprog_usage, line);  
  28.     exit(1);  
  29. }  
  30.   
  31.   
  32. #define die_if(a, msg) do { do_die_if( a , msg, __LINE__); } while(0);  
  33. void do_die_if(int b, char* msg, int line)  
  34. {  
  35.     if(!b)  
  36.         return;  
  37.     fprintf(stderr, "Error at line %d: %s\n", line, msg);  
  38.     fprintf(stderr, "   sysmsg: %s\n", strerror(errno));  
  39.     exit(1);  
  40. }  
  41.   
  42.   
  43. static int read_from_eeprom(struct eeprom *e, int addr, int size)  
  44. {  
  45.     int ch, i;  
  46.     for(i = 0; i < size; ++i, ++addr)  
  47.     {  
  48.         die_if((ch = eeprom_read_byte(e, addr)) < 0"read error");  
  49.         if( (i % 16) == 0 )   
  50.             printf("\n %.4x|  ", addr);  
  51.         else if( (i % 8) == 0 )   
  52.             printf("  ");  
  53.         printf("%.2x ", ch);  
  54.         fflush(stdout);  
  55.     }  
  56.     fprintf(stderr, "\n\n");  
  57.     return 0;  
  58. }  
  59.   
  60. static int write_to_eeprom(struct eeprom *e, int addr)  
  61. {  
  62.     int i;  
  63.     for(i=0, addr=0; i<256; i++, addr++)  
  64.     {  
  65.         if( (i % 16) == 0 )   
  66.             printf("\n %.4x|  ", addr);  
  67.         else if( (i % 8) == 0 )   
  68.             printf("  ");  
  69.         printf("%.2x ", i);  
  70.         fflush(stdout);  
  71.         die_if(eeprom_write_byte(e, addr, i), "write error");  
  72.     }  
  73.     fprintf(stderr, "\n\n");  
  74.     return 0;  
  75. }  
  76.   
  77. int main(int argc, char** argv)  
  78. {  
  79.     struct eeprom e;  
  80.     int op;  
  81.   
  82.     op = 0;  
  83.   
  84.     usage_if(argc != 2 || argv[1][0] != '-' || argv[1][2] != '\0');  
  85.     op = argv[1][1];  
  86.   
  87.     fprintf(stderr, "Open /dev/i2c/0 with 8bit mode\n");  
  88.     die_if(eeprom_open("/dev/i2c/0"0x50, EEPROM_TYPE_8BIT_ADDR, &e) < 0,   
  89.             "unable to open eeprom device file "  
  90.             "(check that the file exists and that it's readable)");  
  91.     switch(op)  
  92.     {  
  93.     case 'r':  
  94.         fprintf(stderr, "  Reading 256 bytes from 0x0\n");  
  95.         read_from_eeprom(&e, 0256);  
  96.         break;  
  97.     case 'w':  
  98.         fprintf(stderr, "  Writing 0x00-0xff into 24C08 \n");  
  99.         write_to_eeprom(&e, 0);  
  100.         break;  
  101.     default:  
  102.         usage_if(1);  
  103.         exit(1);  
  104.     }  
  105.     eeprom_close(&e);  
  106.   
  107.     return 0;  
  108. }  


Makefile

  1. CFLAGS= -Wall -O2  
  2. CC=arm-linux-gcc  
  3.   
  4. i2c: eeprog.o 24cXX.o  
  5.     $(CC) $(CFLAGS) -o i2c eeprog.o 24cXX.o  
  6.   
  7. clean:   
  8.     rm -f i224cXX.o eeprog.o  




说明:

1.分析的内核版本是linux2.6.32.2

2.开发板为友善之臂的mini2440, 用的是ARM9(S3C2440A)处理器

3.链接的IIC设备是EEPROM(AT24C02)

4.按照内核I2C子系统的注册顺序分析。

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要读取通过 I2C 转 USB 接口发送来的数据,你需要遵循以下步骤: 1. 确认设备连接 首先,你需要确认设备已经正确连接到计算机,并且在 Debian 系统中被正确地检测到。你可以使用 `lsusb` 命令来查看连接的 USB 设备列表。如果设备未在列表中显示,可能需要安装额外的驱动程序或配置。 2. 安装 i2c-tools 要与 I2C 设备进行通信,你需要安装 i2c-tools 包。可以使用以下命令来安装: ``` sudo apt-get install i2c-tools ``` 3. 确认 I2C 地址 确认 I2C 设备的地址,这个地址在设备的数据手册中可以找到。你可以使用以下命令来扫描 I2C 总线并列出已连接的设备: ``` sudo i2cdetect -y 8 ``` 在这个命令中,`-y` 选项指定了 I2C 总线的编号,`8` 是我们的 I2C 总线号。如果设备正确连接并且能够被检测到,你应该能够在扫描结果中看到该设备的地址。 4. 读取数据 要读取从设备发送的数据,你可以使用 `i2cget` 命令。该命令的语法如下: ``` sudo i2cget -y 8 [设备地址] [寄存器地址] [数据格式] ``` 在这个命令中,`-y` 选项指定了 I2C 总线的编号,`8` 是我们的 I2C 总线号。`[设备地址]` 应该是你在前一步中找到的 I2C 设备地址。`[寄存器地址]` 是要读取的数据所在的寄存器地址。`[数据格式]` 指定了读取数据的格式,可以是 `b`(byte)、`w`(word)或 `c`(block)。 例如,要读取设备地址为 0x50、寄存器地址为 0x00 的字节数据,你可以使用以下命令: ``` sudo i2cget -y 8 0x50 0x00 b ``` 如果一切设置正确,该命令应该返回一个字节的数据。 ### 回答2: 在Debian中,要读取通过I2C转USB设备发送的数据(如/dev/i2c-8),可以按照以下步骤进行: 1. 安装必要的软件包:使用apt命令安装i2c-tools软件包,该软件包为I2C工具提供了必要的命令和实用程序。 ``` sudo apt install i2c-tools ``` 2. 确认设备存在:使用i2cdetect命令来扫描系统上连接的I2C设备,并确认转USB设备I2C总线上的地址。 ``` sudo i2cdetect -y 8 ``` 3. 设置权限:默认情况下,I2C设备的读写权限是root用户。为了让当前用户能够访问设备,可以将其添加到i2c用户组中。 ``` sudo adduser <username> i2c ``` 4. 测试读取数据:使用i2cget命令来读取I2C设备上指定地址的数据。 ``` sudo i2cget -y 8 <device-address> <register-address> ``` 其中,`<device-address>`是通过i2cdetect命令获得的设备地址,`<register-address>`是要读取的数据寄存器地址。运行后,会返回I2C设备上指定地址的数据值。 5. 使用编程语言读取数据:如果需要在编程语言中读取数据,可以使用相应的I2C库来访问I2C总线。在Python中,可以使用smbus库来进行I2C通信。 ```python import smbus bus = smbus.SMBus(8) # 选择对应的I2C总线 data = bus.read_byte_data(<device-address>, <register-address>) ``` 其中,`<device-address>`和`<register-address>`是具体的设备地址和寄存器地址。读取后,数据将赋值给`data`变量。 通过以上步骤,你可以在Debian中成功读取通过I2C转USB设备发送来的数据。 ### 回答3: Debian操作系统是一款开源的Linux发行版,它可以在各种不同的硬件平台上运行,并且支持多种接口和协议,包括I2C。 要在Debian中读取通过I2C转USB(/dev/i2c-8)发送的数据,你需要按照以下步骤进行操作: 1. 安装I2C工具包:在Debian系统中,你需要安装适用于I2C的工具包。输入以下命令来安装: sudo apt-get install i2c-tools 2. 配置I2C内核模块:在Debian系统上,I2C内核模块默认是加载的。你可以通过以下命令来验证: lsmod | grep i2c 如果没有任何输出,则表示I2C内核模块未加载。你可以通过编辑/boot/config.txt文件来启用I2C内核模块,并重启系统。 3. 确定I2C设备的地址:使用以下命令来扫描I2C总线并查找连接的设备: sudo i2cdetect -y 8 这里的"8"表示I2C总线号,根据你的实际情况进行修改。该命令输出的结果将显示在I2C总线上找到的设备的地址。 4. 读取数据:一旦你知道了I2C设备的地址,就可以使用i2cget命令来读取数据。以下命令演示了如何读取地址为0x50的设备上的一个字节数据: sudo i2cget -y 8 0x50 这里的"8"表示I2C总线号,"0x50"表示设备地址。该命令将输出一个字节数据。 通过以上步骤,你可以在Debian操作系统中读取通过I2C转USB发送来的数据。请注意,具体的命令和参数可能会根据你的实际情况稍有不同,可以根据需要进行适当的调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值