I2C Device
Detailed information about the I2C subsystem can be found in the <Linux Kernel Source>/Documentation/i2c directory.
sysfs Interface
The I2C subsystem provides a user-space interface through the sysfs file system, allowing users to access and configure information related to I2C devices.
The /sys/bus/i2c/devices directory is used to manage and configure the properties and status information of I2C devices. Users can read and write sysfs files to obtain device information or control devices.
Each I2C device is defined in a specific subfolder. For example, /sys/bus/i2c/devices/i2c-1 corresponds to the i2c bus 1.
I2C Device Nodes
I2c node are character devices. I2c device node /dev/i2c-x are created in the /dev directory.
User applications can send and receive data to/from I2C devices.
The i2c-tools package provides command to interact with i2c devices.
I2C BUS detection
To find all i2c busses, i2cdetect command is used. The following command executed on a STM32MP15 board.
i2cdetect -l
i2c-0 i2c ST I2C(0xAAAAAAA) I2C adapter
i2c-1 i2c ST I2C(0xBBBBBBB) I2C adapter
i2c-2 i2c ST I2C(0xCCCCCCC) I2C adapter
i2c-3 i2c ST I2C(0xDDDDDDD) I2C adapter
i2c-4 i2c ST I2C(0xEEEEEEE) I2C adapter
i2c-5 i2c ST I2C(0xFFFFFFF) I2C adapter
I2C device detection
To detect all devices connected to a specific i2c bus, the i2cdetect command is used. The following command executed on a STM32MP15 board to detect all devices attaches on i2c bus 3. It show that two devices are detected: at address 0x4a and 0x50.
2cdetect -y 3
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- UU -- -- -- -- --
50: UU -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
Read device registers
To read all registers of a specific i2c device, the i2cdump command is used. The following command show all registers of the device at address 0x50 attached on bus 0.
i2cdump -f -y 0 0x50
0 1 2 3 4 5 6 7 8 9 a b c d e f 0123456789abcdef
00: 51 18 0a 86 32 03 32 00 00 00 00 00 ff 01 00 00 Q???2?2......?..
10: 00 00 00 00 00 00 00 00 00 00 00 00 70 03 00 00 ............p?..
20: 50 05 00 00 00 00 00 00 00 00 00 00 00 00 00 00 P?..............
30: 00 b8 01 00 00 00 00 00 00 00 00 00 00 00 00 00 .??.............
40: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
50: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
60: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
70: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
80: 30 10 12 02 04 00 20 62 00 00 00 00 10 02 05 00 0????. b....???.
90: 00 00 00 00 a0 01 f2 03 7a 0d 00 00 00 00 80 3e ....????z?....?>
a0: 80 3e 80 3e 00 7d 80 bb 30 75 27 01 a0 00 82 00 ?>?>.}??0u'??.?.
b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
c0: 00 00 00 00 00 00 88 13 08 88 13 08 20 4e 20 10 ......?????? N ?
d0: 27 10 15 34 20 10 27 10 c4 09 04 4c 1d 0c 00 00 '??4 ?'????L??..
e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
To read the value of a specific register of a i2c device, the i2cget command is used. The following command get register value of the i2c device at address 0x50 on i2c bus 0.
I2cget -f -y 0 0x50 0x1c
0x70
To read several registers from a specific register address. The following command read a block of 6 bytes of the i2c device 0x50, on bus 0, from register address 0x70
I2cget -f -y 0 0x50 0x1c i 6
0x70 0x03 0x00 0x00 0x50 0x05
Write register
To write a value to a register of a i2c device, the i2cset command is used.
The following command write a byte value 0xbc to the register 0x1c of the i2c device 0x50, on bus 0.
I2cset -f -y 0 0x50 0x1c 0xbc
The following command write a bloc of 6 bytes 0x71 0x04 0x01 0x01 0x51 0x06 to the register 0x1c of the i2c device 0x50, on bus 0.
I2cset -f -y 0 0x50 0x1c -i 6 0x71 0x04 0x01 0x01 0x51 0x06