本文主要对i2c-tools工具中的i2cdetect 、i2cdump、i2cget 、 i2cset操作使用进行说明。
一、i2cdetect 命令
1.1 description
Detect i2c devices.
-a All addresses
-F Show functionality
-l List all buses
-r Probe with SMBus Read Byte
-y Answer "yes" to confirmation prompts (for script use)
1.2 usage
语法:
i2cdetect [-y] [-a] [-q|-r] i2cbus [first last]
1.3 Example
查看当前所有i2c总线:
i2cdetect -l
i2c-3 i2c qt-i2c I2C Adapter
i2c-10 i2c qt-i2c I2C Adapter
i2c-1 i2c qt-i2c I2C Adapter
i2c-8 i2c qt-i2c I2C Adapter
i2c-6 i2c qt-i2c I2C Adapter
i2c-4 i2c qt-i2c I2C Adapter
i2c-11 i2c qt-i2c I2C Adapter
i2c-2 i2c qt-i2c I2C Adapter
i2c-0 i2c qt-i2c I2C Adapter
i2c-9 i2c qt-i2c I2C Adapter
i2c-7 i2c qt-i2c I2C Adapter
i2c-5 i2c qt-i2c I2C Adapter
返回值说明: ‘–’:表示该地址被检测,但没有芯片应答; ‘UU’:表示该地址当前由内核驱动程序使用; ‘**’:**表示以十六进制表示的设备地址编号.
扫描bus设备: 扫描i2cbus 0上的设备 i2cdetect -y 0
i2cdetect -y 0
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- 18 -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
二、i2cget命令
2.1 description
usage: i2cget [-fy] BUS CHIP ADDR
Read an i2c register.
-f Force access to busy devices
-y Answer "yes" to confirmation prompts (for script use)
2.2 usage
i2cget -f -y
b (read byte data, default)
w (read word data)
i (read I2C block data)
语法:
i2cget [-f] [-y] i2cbus chip-address [data-address [mode]]
2.3 Example
读取i2c 0总线上 从设备是0x68地址 的0x00寄存器的内容
i2cget -f -y 0 0x68 0x00
0x51
or sometimes :
i2cget -f -y 2 0x1a 0x0 bs
三、i2cset命令
3.1 description
usage: i2cset [-fy] BUS CHIP ADDR VALUE... MODE
Write an i2c register. MODE is b for byte, w for 16-bit word, i for I2C block.
-f Force access to busy devices
-y Answer "yes" to confirmation prompts (for script use)
3.2 usage
语法:
i2cset [-f] [-y] [-m mask] [-r] i2cbus chip-address data-address [value] … [mode]
3.3 Example
将i2c 2总线上 0x1a从设备 0x0寄存器的值设置为0xff
i2cset -f -y 2 0x1a 0x0 0xff b
四、i2cdump命令
4.1 description
usage: i2cdump [-fy] BUS CHIP
Dump i2c registers.
-f Force access to busy devices
-y Answer "yes" to confirmation prompts (for script use)
4.2 usage
语法:
i2cdump [-f] [-r first-last] [-y] i2cbus chip-address [mode [bank [bankreg]]] 参数y:关闭人机交互模式;
4.3 Example
读取bus 0总线上0x1d 从设备上的全部寄存器的值
i2cdump -y 0 0x1d
五、i2ctransfer
有时需要传输多位数据,可以用到此命令
i2ctransfer -y -f 1 w3@0x51 $reg_addr $data_high $data_low
i2ctransfer
命令执行了一个 I2C 传输操作(总线为i2c bus 3),向地址为 0x51
的设备发送了一个写入操作。w3
表示写入 3 个数据字节。第一个字节是寄存器地址 reg_addr
,第二个字节是高位数据 data_high
,第三个字节是低位数据 data_low.