Using i2ctools for I2C Bus and Device Diagnostics on Embedded Linux
Source Code Location
The utility suite is often found within the toybox repository in Android-based environments:
android/external/toybox/toys/other/i2ctools.c
Available Utilities
The compiled binary provides four distinct commands for enteracting with the I2C subsystem:
- i2cdetect: Scans a specific bus to identify connected slave addresses.
- i2cdump: Displays a hex dump of the registers on a target device.
- i2cget: Reads a single byte or word from a specific register.
- i2cset: Writes data to a specific register address on a device.
Verifying Installation
Check if the binaries are present on your system using shell auto-completion:
console:/ # i2c<TAB>
i2cdetect i2cdump i2cget i2cset
Identifying Active Buses
List the available I2C adapters recognized by the kernel. These typically appear as i2c- followed by a number:
console:/ # ls -l /dev/i2c-*
crw------- 1 root root 89, 3 /dev/i2c-3
crw------- 1 root root 89, 5 /dev/i2c-5
Scanning Addresses with i2cdetect
Use i2cdetect to probe a bus (e.g., bus 3) and find active device addresses. The following example scans the full range (0x00-0x7f) and finds a device at address 0x1e.
console:/ # i2cdetect --help
usage: i2cdetect [-ary] BUS [FIRST LAST]
...
console:/ # i2cdetect -a 3
Probe chips 0x00-0x7f on bus 3? (Y/n): y
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- 1e --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
...
70: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Writing Data with i2cset
The i2cset command allows you to modify register values. The syntax generally follows: i2cset -y [BUS] [DEVICE_ADDR] [REG_ADDR] [VALUE] [MODE].
Supported modes include:
b: Byte (8-bit)w: Word (16-bit)i: I2C block data
console:/ # i2cset --help
usage: i2cset [-fy] BUS CHIP ADDR VALUE... MODE
...
console:/ # i2cset -y 3 0x1E 0x00 0x3 b
Reading Data with i2cget
To verify the write operation, use i2cget to read back the value from register 0x00. The output confirms the value 0x03 was successfully stored.
console:/ # i2cget --help
usage: i2cget [-fy] BUS CHIP ADDR
...
console:/ # i2cget -y 3 0x1E 0x00
0x03
Dumping Registers with i2cdump
For a comprehensive view of the device state, i2cdump reads consecutive registers. Below is a dump of the devicce at 0x1e on bus 3:
console:/ # i2cdump -y 3 0x1E
0 1 2 3 4 5 6 7 8 9 a b c d e f 0123456789abcdef
00: 03 00 00 02 02 01 ee ee 00 80 00 00 00 00 00 00 ?..?????.?......
10: 00 01 00 03 ee ee ee ee ee 40 00 00 ff ff ee ee .?.??????@..????
20: 05 13 01 00 00 ee ee ee 00 00 00 80 00 80 ee ee ???..???...?.???
30: 00 00 ee 05 ee 00 ee 1b 00 00 00 00 00 00 00 00 ..???.??........
40: 82 84 80 93 80 8f 86 80 ee ee ee ee ee ee ee ee ????????????????
50: ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ????????????????
...
f0: ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ????????????????