@@ -4,6 +4,10 @@ the /dev interface. You need to load module i2c-dev for this.
44
55Each registered i2c adapter gets a number, counting from 0. You can
66examine /sys/class/i2c-dev/ to see what number corresponds to which adapter.
7+ Alternatively, you can run "i2cdetect -l" to obtain a formated list of all
8+ i2c adapters present on your system at a given time. i2cdetect is part of
9+ the i2c-tools package.
10+
711I2C device files are character device files with major device number 89
812and a minor device number corresponding to the number assigned as
913explained above. They should be called "i2c-%d" (i2c-0, i2c-1, ...,
@@ -17,94 +21,106 @@ So let's say you want to access an i2c adapter from a C program. The
1721first thing to do is "#include <linux/i2c-dev.h>". Please note that
1822there are two files named "i2c-dev.h" out there, one is distributed
1923with the Linux kernel and is meant to be included from kernel
20- driver code, the other one is distributed with lm_sensors and is
24+ driver code, the other one is distributed with i2c-tools and is
2125meant to be included from user-space programs. You obviously want
2226the second one here.
2327
2428Now, you have to decide which adapter you want to access. You should
25- inspect /sys/class/i2c-dev/ to decide this. Adapter numbers are assigned
26- somewhat dynamically, so you can not even assume /dev/i2c-0 is the
27- first adapter .
29+ inspect /sys/class/i2c-dev/ or run "i2cdetect -l" to decide this.
30+ Adapter numbers are assigned somewhat dynamically, so you can not
31+ assume much about them. They can even change from one boot to the next .
2832
2933Next thing, open the device file, as follows:
34+
3035 int file;
3136 int adapter_nr = 2; /* probably dynamically determined */
3237 char filename[20];
3338
34- sprintf(filename,"/dev/i2c-%d",adapter_nr);
35- if ((file = open(filename,O_RDWR)) < 0) {
39+ snprintf(filename, 19, "/dev/i2c-%d", adapter_nr);
40+ file = open(filename, O_RDWR);
41+ if (file < 0) {
3642 /* ERROR HANDLING; you can check errno to see what went wrong */
3743 exit(1);
3844 }
3945
4046When you have opened the device, you must specify with what device
4147address you want to communicate:
48+
4249 int addr = 0x40; /* The I2C address */
43- if (ioctl(file,I2C_SLAVE,addr) < 0) {
50+
51+ if (ioctl(file, I2C_SLAVE, addr) < 0) {
4452 /* ERROR HANDLING; you can check errno to see what went wrong */
4553 exit(1);
4654 }
4755
4856Well, you are all set up now. You can now use SMBus commands or plain
4957I2C to communicate with your device. SMBus commands are preferred if
5058the device supports them. Both are illustrated below.
59+
5160 __u8 register = 0x10; /* Device register to access */
5261 __s32 res;
5362 char buf[10];
63+
5464 /* Using SMBus commands */
55- res = i2c_smbus_read_word_data(file,register);
65+ res = i2c_smbus_read_word_data(file, register);
5666 if (res < 0) {
5767 /* ERROR HANDLING: i2c transaction failed */
5868 } else {
5969 /* res contains the read word */
6070 }
71+
6172 /* Using I2C Write, equivalent of
62- i2c_smbus_write_word_data(file,register,0x6543) */
73+ i2c_smbus_write_word_data(file, register, 0x6543) */
6374 buf[0] = register;
6475 buf[1] = 0x43;
6576 buf[2] = 0x65;
66- if ( write(file,buf,3) != 3) {
77+ if (write(file, buf, 3) ! = 3) {
6778 /* ERROR HANDLING: i2c transaction failed */
6879 }
80+
6981 /* Using I2C Read, equivalent of i2c_smbus_read_byte(file) */
70- if (read(file,buf,1) != 1) {
82+ if (read(file, buf, 1) != 1) {
7183 /* ERROR HANDLING: i2c transaction failed */
7284 } else {
7385 /* buf[0] contains the read byte */
7486 }
7587
88+ Note that only a subset of the I2C and SMBus protocols can be achieved by
89+ the means of read() and write() calls. In particular, so-called combined
90+ transactions (mixing read and write messages in the same transaction)
91+ aren't supported. For this reason, this interface is almost never used by
92+ user-space programs.
93+
7694IMPORTANT: because of the use of inline functions, you *have* to use
7795'-O' or some variation when you compile your program!
7896
7997
8098Full interface description
8199==========================
82100
83- The following IOCTLs are defined and fully supported
84- (see also i2c-dev.h):
101+ The following IOCTLs are defined:
85102
86- ioctl(file,I2C_SLAVE,long addr)
103+ ioctl(file, I2C_SLAVE, long addr)
87104 Change slave address. The address is passed in the 7 lower bits of the
88105 argument (except for 10 bit addresses, passed in the 10 lower bits in this
89106 case).
90107
91- ioctl(file,I2C_TENBIT,long select)
108+ ioctl(file, I2C_TENBIT, long select)
92109 Selects ten bit addresses if select not equals 0, selects normal 7 bit
93110 addresses if select equals 0. Default 0. This request is only valid
94111 if the adapter has I2C_FUNC_10BIT_ADDR.
95112
96- ioctl(file,I2C_PEC,long select)
113+ ioctl(file, I2C_PEC, long select)
97114 Selects SMBus PEC (packet error checking) generation and verification
98115 if select not equals 0, disables if select equals 0. Default 0.
99116 Used only for SMBus transactions. This request only has an effect if the
100117 the adapter has I2C_FUNC_SMBUS_PEC; it is still safe if not, it just
101118 doesn't have any effect.
102119
103- ioctl(file,I2C_FUNCS,unsigned long *funcs)
120+ ioctl(file, I2C_FUNCS, unsigned long *funcs)
104121 Gets the adapter functionality and puts it in *funcs.
105122
106- ioctl(file,I2C_RDWR,struct i2c_rdwr_ioctl_data *msgset)
107-
123+ ioctl(file, I2C_RDWR, struct i2c_rdwr_ioctl_data *msgset)
108124 Do combined read/write transaction without stop in between.
109125 Only valid if the adapter has I2C_FUNC_I2C. The argument is
110126 a pointer to a
@@ -120,10 +136,9 @@ ioctl(file,I2C_RDWR,struct i2c_rdwr_ioctl_data *msgset)
120136 The slave address and whether to use ten bit address mode has to be
121137 set in each message, overriding the values set with the above ioctl's.
122138
123-
124- Other values are NOT supported at this moment, except for I2C_SMBUS,
125- which you should never directly call; instead, use the access functions
126- below.
139+ ioctl(file, I2C_SMBUS, struct i2c_smbus_ioctl_data *args)
140+ Not meant to be called directly; instead, use the access functions
141+ below.
127142
128143You can do plain i2c transactions by using read(2) and write(2) calls.
129144You do not need to pass the address byte; instead, set it through
@@ -148,7 +163,7 @@ what happened. The 'write' transactions return 0 on success; the
148163returns the number of values read. The block buffers need not be longer
149164than 32 bytes.
150165
151- The above functions are all macros , that resolve to calls to the
152- i2c_smbus_access function, that on its turn calls a specific ioctl
166+ The above functions are all inline functions , that resolve to calls to
167+ the i2c_smbus_access function, that on its turn calls a specific ioctl
153168with the data in a specific format. Read the source code if you
154169want to know what happens behind the screens.
0 commit comments