Linux kernel i2c-tools command
Today I will share some simple and common commands.
The related commands of i2c-tools are often used for online debugging of reading and writing i2c device registers in Linux system.
Installation command:
apt-get update
apt-get install i2c-tools
Linux version code download path:
https://mirrors.edge.kernel.org/pub/software/utils/i2c-tools/
1. i2cdetect
1. Command
root@linaro-alip:/# i2cdetect
Error: No i2c-bus specified!
Usage: i2cdetect [-y] [-a] [-q|-r] I2CBUS [FIRST LAST]
i2cdetect -F I2CBUS
i2cdetect -l
I2CBUS is an integer or an I2C bus name
If provided, FIRST and LAST limit the probing range.
2. Usage
## 列出i2c总线
root@linaro-alip:/# i2cdetect -l
i2c-0 i2c rk3x-i2c I2C adapter
i2c-1 i2c rk3x-i2c I2C adapter
i2c-9 i2c DesignWare HDMI I2C adapter
## 列出某个i2c总线上的i2c设备
root@linaro-alip:/# i2cdetect -y 0
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- UU -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: UU UU -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
root@linaro-alip:/#
In the RockPi 4A schematic, the I2C0 bus mounts the I2C device address as follows:
2. i2cdump
1. Command
root@linaro-alip:~# i2cdump
Error: No i2c-bus specified!
Usage: i2cdump [-f] [-y] [-r first-last] I2CBUS ADDRESS [MODE [BANK [BANKREG]]]
I2CBUS is an integer or an I2C bus name
ADDRESS is an integer (0x03 - 0x77)
MODE is one of:
b (byte, default)
w (word)
W (word on even register addresses)
s (SMBus block)
i (I2C block)
c (consecutive byte)
Append p for SMBus PEC
2. Usage
## 显示I2C 0号总线上0x1b设备的寄存器值。MODE为byte,可省略。
root@linaro-alip:~# i2cdump -f -y 0 0x1b b
0 1 2 3 4 5 6 7 8 9 a b c d e f 0123456789abcdef
00: 12 50 08 21 01 13 01 00 00 00 00 01 01 00 00 00 ?P?!???....??...
10: 80 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ??..............
20: 01 17 00 6f ff 00 00 00 10 00 ff 0f ff 02 19 0f ??.o....?..?.???
30: 00 00 19 07 00 00 02 03 00 00 09 00 00 00 00 0a ..??..??..?....?
40: 00 0c 00 0c 00 07 00 01 00 0c 00 00 00 5f 00 03 .?.?.?.?.?..._.?
50: 06 01 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 cf 03 00 28 00 0c 1c 80 19 00 34 12 00 71 00 .??.(.????.4?.q.
80: 10 50 1f ac 00 40 10 01 40 00 08 00 09 09 00 00 ?P??.@??@.?.??..
90: 55 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 U...............
a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
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 ................
3. i2cget
1. Command
root@linaro-alip:~# i2cget
Usage: i2cget [-f] [-y] I2CBUS CHIP-ADDRESS [DATA-ADDRESS [MODE]]
I2CBUS is an integer or an I2C bus name
ADDRESS is an integer (0x03 - 0x77)
MODE is one of:
b (read byte data, default)
w (read word data)
c (write byte/read byte)
Append p for SMBus PEC
2. Usage
## 显示I2C 0号总线上0x1b设备0x0和0x1寄存器地址,按字节读取
root@linaro-alip:~# i2cget -f -y 0 0x1b 0x0
0x12
root@linaro-alip:~# i2cget -f -y 0 0x1b 0x1
0x50
## 按字读取
root@linaro-alip:~# i2cget -f -y 0 0x1b 0x0 w
0x5012
4. i2cset
1. Command
root@linaro-alip:~# i2cset
Usage: i2cset [-f] [-y] [-m MASK] [-r] I2CBUS CHIP-ADDRESS DATA-ADDRESS [VALUE] ... [MODE]
I2CBUS is an integer or an I2C bus name
ADDRESS is an integer (0x03 - 0x77)
MODE is one of:
c (byte, no value)
b (byte data, default)
w (word data)
i (I2C block data)
s (SMBus block data)
Append p for SMBus PEC
2. Usage
root@linaro-alip:/sys/rk8xx# i2cget -f -y 0 0x1b 0x10
0x80
root@linaro-alip:/sys/rk8xx# i2cset -f -y 0 0x1b 0x10 0x0
root@linaro-alip:/sys/rk8xx# i2cget -f -y 0 0x1b 0x10
0x00
5. i2ctransfer
i2c-tools-4.0 and later versions added the i2ctransfer command.
The register address of the i2c device that can be read and written by i2cget and i2cset is less than 0xff, that is, the register is an 8-bit address.
If the i2c device register is a 16-bit address, you can use the i2ctransfer command, which can also be used for devices with 8-bit register addresses.
1. Command
"Usage: i2ctransfer [-f] [-y] [-v] [-V] [-a] I2CBUS DESC [DATA] [DESC [DATA]]...\n"
" I2CBUS is an integer or an I2C bus name\n"
" DESC describes the transfer in the form: {r|w}LENGTH[@address]\n"
" 1) read/write-flag 2) LENGTH (range 0-65535, or '?')\n"
" 3) I2C address (use last one if omitted)\n"
" DATA are LENGTH bytes for a write message. They can be shortened by a suffix:\n"
" = (keep value constant until LENGTH)\n"
" + (increase value by 1 until LENGTH)\n"
" - (decrease value by 1 until LENGTH)\n"
" p (use pseudo random generator until LENGTH with value as seed)\n\n"
"Example (bus 0, read 8 byte at offset 0x64 from EEPROM at 0x50):\n"
" # i2ctransfer 0 w1@0x50 0x64 r8\n"
"Example (same EEPROM, at offset 0x42 write 0xff 0xfe ... 0xf0):\n"
" # i2ctransfer 0 w17@0x50 0x42 0xff-\n");
2. Usage
## 从i2c 4号总线0x38设备的0x3a01寄存器开始读16个字节的数据,w2:表示寄存器0x3a01的长度为2个字节
i2ctransfer -y -f 4 w2@0x38 0x3a 0x01 r16
## 向i2c 4号总线0x38设备的0x3a01寄存器写0x10,w3:表示寄存器0x3a01和写入值0x10的长度为3字节
i2ctransfer -y -f 4 w3@0x38 0x3a 0x01 0x10