|
@@ -9,8 +9,8 @@ i2c adapters present on your system at a given time. i2cdetect is part of
|
|
|
the i2c-tools package.
|
|
|
|
|
|
I2C device files are character device files with major device number 89
|
|
|
-and a minor device number corresponding to the number assigned as
|
|
|
-explained above. They should be called "i2c-%d" (i2c-0, i2c-1, ...,
|
|
|
+and a minor device number corresponding to the number assigned as
|
|
|
+explained above. They should be called "i2c-%d" (i2c-0, i2c-1, ...,
|
|
|
i2c-10, ...). All 256 minor device numbers are reserved for i2c.
|
|
|
|
|
|
|
|
@@ -23,11 +23,6 @@ First, you need to include these two headers:
|
|
|
#include <linux/i2c-dev.h>
|
|
|
#include <i2c/smbus.h>
|
|
|
|
|
|
-(Please note that there are two files named "i2c-dev.h" out there. One is
|
|
|
-distributed with the Linux kernel and the other one is included in the
|
|
|
-source tree of i2c-tools. They used to be different in content but since 2012
|
|
|
-they're identical. You should use "linux/i2c-dev.h").
|
|
|
-
|
|
|
Now, you have to decide which adapter you want to access. You should
|
|
|
inspect /sys/class/i2c-dev/ or run "i2cdetect -l" to decide this.
|
|
|
Adapter numbers are assigned somewhat dynamically, so you can not
|
|
@@ -38,7 +33,7 @@ Next thing, open the device file, as follows:
|
|
|
int file;
|
|
|
int adapter_nr = 2; /* probably dynamically determined */
|
|
|
char filename[20];
|
|
|
-
|
|
|
+
|
|
|
snprintf(filename, 19, "/dev/i2c-%d", adapter_nr);
|
|
|
file = open(filename, O_RDWR);
|
|
|
if (file < 0) {
|
|
@@ -72,8 +67,10 @@ the device supports them. Both are illustrated below.
|
|
|
/* res contains the read word */
|
|
|
}
|
|
|
|
|
|
- /* Using I2C Write, equivalent of
|
|
|
- i2c_smbus_write_word_data(file, reg, 0x6543) */
|
|
|
+ /*
|
|
|
+ * Using I2C Write, equivalent of
|
|
|
+ * i2c_smbus_write_word_data(file, reg, 0x6543)
|
|
|
+ */
|
|
|
buf[0] = reg;
|
|
|
buf[1] = 0x43;
|
|
|
buf[2] = 0x65;
|
|
@@ -140,14 +137,14 @@ ioctl(file, I2C_RDWR, struct i2c_rdwr_ioctl_data *msgset)
|
|
|
set in each message, overriding the values set with the above ioctl's.
|
|
|
|
|
|
ioctl(file, I2C_SMBUS, struct i2c_smbus_ioctl_data *args)
|
|
|
- Not meant to be called directly; instead, use the access functions
|
|
|
- below.
|
|
|
+ If possible, use the provided i2c_smbus_* methods described below instead
|
|
|
+ of issuing direct ioctls.
|
|
|
|
|
|
You can do plain i2c transactions by using read(2) and write(2) calls.
|
|
|
You do not need to pass the address byte; instead, set it through
|
|
|
ioctl I2C_SLAVE before you try to access the device.
|
|
|
|
|
|
-You can do SMBus level transactions (see documentation file smbus-protocol
|
|
|
+You can do SMBus level transactions (see documentation file smbus-protocol
|
|
|
for details) through the following functions:
|
|
|
__s32 i2c_smbus_write_quick(int file, __u8 value);
|
|
|
__s32 i2c_smbus_read_byte(int file);
|
|
@@ -158,7 +155,7 @@ for details) through the following functions:
|
|
|
__s32 i2c_smbus_write_word_data(int file, __u8 command, __u16 value);
|
|
|
__s32 i2c_smbus_process_call(int file, __u8 command, __u16 value);
|
|
|
__s32 i2c_smbus_read_block_data(int file, __u8 command, __u8 *values);
|
|
|
- __s32 i2c_smbus_write_block_data(int file, __u8 command, __u8 length,
|
|
|
+ __s32 i2c_smbus_write_block_data(int file, __u8 command, __u8 length,
|
|
|
__u8 *values);
|
|
|
All these transactions return -1 on failure; you can read errno to see
|
|
|
what happened. The 'write' transactions return 0 on success; the
|
|
@@ -166,10 +163,9 @@ what happened. The 'write' transactions return 0 on success; the
|
|
|
returns the number of values read. The block buffers need not be longer
|
|
|
than 32 bytes.
|
|
|
|
|
|
-The above functions are all inline functions, that resolve to calls to
|
|
|
-the i2c_smbus_access function, that on its turn calls a specific ioctl
|
|
|
-with the data in a specific format. Read the source code if you
|
|
|
-want to know what happens behind the screens.
|
|
|
+The above functions are made available by linking against the libi2c library,
|
|
|
+which is provided by the i2c-tools project. See:
|
|
|
+https://git.kernel.org/pub/scm/utils/i2c-tools/i2c-tools.git/.
|
|
|
|
|
|
|
|
|
Implementation details
|