i2cdetect

扫描我们使用的i2c总线上的设备,包括查询总线组数,侦测总线上使用的设备。主要用于判断我们的i2c总线是否挂载以及是否被正常使用,用法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Usage: i2cdetect [-y] [-a] [-q|-r] I2CBUS [FIRST LAST]
i2cdetect -F I2CBUS(i2c总线编号)
i2cdetect -l
I2CBUS is an integer or an I2C bus name
If provided, FIRST and LAST limit the probing range.

-y:关闭交互式,不会显示警告信息(指令执行自动yes,否则会提示确认执行Continue? [Y/n] Y,不加参数y会有很多执行提示,可以帮助判断)
-a:扫描总线上所有设备(输出总线上所有地址(00-7f),没有 -a,只显示 03-77)
-q:使用SMBus的"quick write"命令进行检测,不建议使用(快速写指令)
-r:使用SMBus的"receive byte"命令进行检测,不建议使用(读指令)
-q和-r不能同时使用

i2cbus:指定查询某个总线编号
first、last:扫描的地址范围
  • i2cdetect -l
    列出我们系统目前使用了几组i2c总线,格式为
1
2
i2cdetect -l
i2c-0 i2c rk3x-i2c
  • i2cdetect -V

打印使用的i2c-tools版本

  • i2cdetect -F I2CBUS

查询总线支持的功能

image-20221115222345678

i2cdetect [-y/-a/-r] I2CBUS

查询总线上使用的设备地址

image-20221115222537554

--表示这个地址被检测到,但是没有芯片应答;

UU表示这个地址被内核驱动使用

数字表示这个地址被芯片使用并应答

i2cdump

i2cdump读取设备上所有寄存器的值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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

-f:强制使用此设备地址,即使此设备地址已经被使用;若不添加此参数,地址可能写失败
-y:同i2cdetect
-r:读取从first-last之间的寄存器的值
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//0x38设备地址
root@Linux:/proc# i2cdump -f -y -a 1 0x38
No size specified (using byte-data access)
0 1 2 3 4 5 6 7 8 9 a b c d e f 0123456789abcdef
00: 00 00 00 40 a8 00 b0 00 00 ff ff ff ff ff ff ff ...@?.?.........
10: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................
20: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................
30: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................
40: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................
50: ff ff ff 18 18 00 00 00 00 00 00 00 00 ff ff ff ...??...........
60: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................
70: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................
80: 1e 00 19 00 00 c8 01 1e 0a 28 00 00 00 00 00 00 ?.?..????(......
90: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 26 ...............&
a0: 01 30 0d 64 01 00 01 00 88 19 00 00 00 00 00 01 ?0?d?.?.??.....?
b0: 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 ............?...
c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
d0: 00 ff ff 00 00 ff ff ff ff ff 00 00 ff ff ff ff ................
e0: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................
f0: ff ff ff ff ff ff ff ff ff ff ff ff 01 ff ff ff ............?...

//0x38设备地址,只读取0x50-0x7f寄存器范围的值
root@Linux:/proc# i2cdump -f -y -r 0x50-0x7f 1 0x38
No size specified (using byte-data access)
0 1 2 3 4 5 6 7 8 9 a b c d e f 0123456789abcdef
50: ff ff ff 18 18 00 00 00 00 00 00 00 00 ff ff ff ...??...........
60: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................
70: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................

i2cget

i2cget读取设备上寄存器的值

1
2
3
4
5
6
7
8
9
10
11
Usage: i2cget [-f] [-y] [-a] I2CBUS CHIP-ADDRESS [DATA-ADDRESS [MODE]]
I2CBUS is an integer or an I2C bus name
ADDRESS is an integer (0x03 - 0x77, or 0x00 - 0x7f if -a is given)
MODE is one of:
b (read byte data, default)
w (read word data)
c (write byte/read byte)
Append p for SMBus PEC
-f:同i2cdetect
-y:同i2cdetect
-a:允许使用0x00-0x02和0x78-0x7f之间的地址
1
2
root@Linux:/# i2cget -f -y 1 0x38 0x04
0x45

i2cset

i2cset设置设备上寄存器的值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Usage: i2cset [-f] [-y] [-m MASK] [-r] [-a] I2CBUS CHIP-ADDRESS DATA-ADDRESS [VALUE] ... [MODE]
I2CBUS is an integer or an I2C bus name
ADDRESS is an integer (0x03 - 0x77, or 0x00 - 0x7f if -a is given)
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

-f:同i2cdetect
-y:同i2cdetect
-m:添加掩码
-r:回显,显示是否写入成功,要写的值和读取的值
-a:同i2cget

CHIP-ADDRESS:设备地址
DATA-ADDRESS:要写入的寄存器地址
VALUE:要写入的值
MODE:数据长度类型
1
2
3
4
5
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

参考链接:
手把手教你使用 i2c-tools
i2c-tools安装与使用总结