Linux下i2c-tools工具使用
关键字:i2cdetect、i2cget、i2cset
概述
i2cdetect:检查总线挂载i2c设备情况
i2cget:获取某个总线上某个设备的寄存器值
i2cset:设置某个总线上某个设备的寄存器值
i2cdump:查看某个总线上某个从设备(8位地址)所有寄存器的值
i2ctranfer:向寄存器地址为16位的从设备读取或写入数据
1.i2cdetect
指令提示及说明
$ 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.
参数 内容
-y 取消交互模式,默认情况下,i2cdetect将等待用户的确认,使用该标志时
它将直接执行操作
-a 强制扫描非规则地址(不推荐)
-q 使用SMBus"快速写入"命令进行探测(不推荐)
-r 使用SMBus"接收字节"命令进行探测(不推荐)
-F 显示适配器实现的功能列表并退出
-V 显示工具版本号
-l 显示已经在系统中使用的I2C总线
i2cbus 表示要扫描的i2c总线的编号和名称
first last 表示要扫描的从设备地址范围
1.1 i2cdetect -l查看当前系统的I2C总线
$ sudo i2cdetect -l
i2c-3 i2c i915 gmbus dpd I2C adapter
i2c-1 i2c i915 gmbus dpc I2C adapter
i2c-4 i2c DPDDC-B I2C adapter
i2c-2 i2c i915 gmbus misc I2C adapter
i2c-0 i2c i915 gmbus dpb I2C adapter
1.2 i2cdetect -y -r 1 查看总线1上所有从设备
"--":表示地址被检测到,但是没有芯片
"UU":表示该地址从设备正在被其他驱动使用
"37":表示05从地址的设备空闲可以使用
$ sudo i2cdetect -y 4
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- UU -- -- -- -- -- --
30: 30 -- -- -- -- -- -- 37 -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- 4a 4b -- -- -- --
50: 50 -- -- -- -- -- -- -- -- -- -- -- 5c -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
1.3 i2cdetect -F 1 查看总线(I2C-1)的功能
$ sudo i2cdetect -F 1
Functionalities implemented by /dev/i2c-1:
I2C yes
SMBus Quick Command yes
SMBus Send Byte yes
SMBus Receive Byte yes
SMBus Write Byte yes
SMBus Read Byte yes
SMBus Write Word yes
SMBus Read Word yes
SMBus Process Call yes
SMBus Block Write yes
SMBus Block Read yes
SMBus Block Process Call yes
SMBus PEC yes
I2C Block Write yes
I2C Block Read yes
2.i2cget
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
参数 内容
-f 强制访问设备
-y 取消交互模式,使用此标志,直接执行操作。
读取i2c-4总线上0x30的数据
$ sudo i2cget -f -y 4 0x30
0xff
3.i2cset
i2cset:设置某个总线上某个从设备的寄存器的值
i2cset -f -y 1 0x27 0x00 0x01
设置i2c1总线0x27从地址0x00寄存器的值为0x01
4.i2cdump
查看一个总线上某个设备所有寄存器(寄存器地址为8位)的值
i2cdump -f -y 1 0x29
5.i2ctranfer
向寄存器地址为16位的从设备读取或写入数据
i2ctransfer -f -y 1 q2@0x38 0x00 0x00 r32 读取
1:哪条总线
q2:写两个字节地址
0x00 0x00:寄存器地址
r32:往后32个寄存器所对应的寄存器值
i2ctransfer -f -y 1 q2@0x62 0x00 0x00 0x10
1:哪条总线
q2:写两个字节地址
0x00 0x00:寄存器地址
0x10:0x00 0x00寄存器地址往后的寄存器写入0x10