I2C工具集
linux驱动开发时经常会与i2c设备打交道,开发调试12c设备的时进行遇到各种各种问题,在介绍鸿蒙的i2c驱动时需要用到这个工具集,没找到合适的文章,所以把以前总结的这个工具说明发上来了,记不清具体的根文件系统版本了,仅供参考吧。
下面介绍l2C-tools的使用帮助和应用示例,包括以下几个工具
- i2cdetect查询12C bus上的设备
- i2cdump 用于查看对应bus上的所有寄存器
- i2cget用于读取对应bus上寄存器
- i2cset用于设置寄存器
- i2ctransfer用于读写多字节寄存器地址。
i2cdetect
i2cdetect -h
i2cdetect: invalid option -- 'h'
BusyBox v1.29.3 (2023-06-08 18:47:57 CST) multi-call binary.
Usage: i2cdetect -l | -F I2CBUS | [-ya] [-q|-r] I2CBUS [FIRST LAST]
Detect I2C chips
-l List installed buses 列出总线
-F BUS# List functionalities on this bus 列总线功能
-y Disable interactive mode 关闭交互模式
-a Force scanning of non-regular addresses 强制扫描非常规地址
-q Use smbus quick write commands for probing (default)使用smbus快速写入命令进行探测
-r Use smbus read byte commands for probing 使用smbus读取字节命令进行探测
FIRST and LAST limit probing range
- 列出总线
sudo i2cdetect -l
- 列出总线功能(假设总线号为3)
sudo i2cdetect -F 3
- 扫描总线上设备地址
sudo i2cdetect -y -a 3
i2cdump
Usage: i2cdump [-fy] [-r FIRST-LAST] BUS ADDR [MODE]
Examine I2C registers
I2CBUS I2C bus number
ADDRESS 0x03-0x77
MODE is:
b Byte (default)
w Word
W Word on even register addresses
i I2C block
s SMBus block
c Consecutive byte
Append p for SMBus PEC
-f Force access 强行进入
-y Disable interactive mode 关闭交互模式
-r Limit the number of registers being accessed 限制访问的寄存器数量
- 显示I2C 3号总线上0x5f设备的寄存器值。MODE为byte,可省略。
sudo i2cdump -f -y 3 0x5f b
sysadm@SCT230A:~$ sudo ./i2cdump -f -y 4 0x5f b
0 1 2 3 4 5 6 7 8 9 a b c d e f 0123456789abcdef
00: 96 02 50 00 00 00 00 00 00 00 00 00 00 00 00 00 ??P.............
10: 00 79 79 79 00 00 00 00 00 00 00 00 00 00 00 00 .yyy............
20: 00 79 79 79 00 00 00 00 00 00 00 00 00 00 00 00 .yyy............
30: 00 79 79 79 00 00 00 00 00 00 00 00 00 00 00 00 .yyy............
40: 00 79 79 79 00 00 00 00 00 00 00 00 00 00 00 00 .yyy............
50: 00 79 79 79 00 00 00 00 00 00 00 00 00 00 00 00 .yyy............
60: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
70: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
90: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
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 ................
i2cget
Usage: i2cget [-fy] BUS CHIP-ADDRESS [DATA-ADDRESS [MODE]]
Read from I2C/SMBus chip registers
I2CBUS I2C bus number
ADDRESS 0x03-0x77
MODE is:
b Read byte data (default)
w Read word data
c Write byte/read byte
Append p for SMBus PEC
-f Force access强行进入
-y Disable interactive mode 关闭交互模式
- 读3号总线上设备(0x5f)寄存器地址为1的值
sudo i2cget -fy 3 0x5f 1 b
i2cset
BusyBox v1.29.3 (2023-06-08 18:47:57 CST) multi-call binary.
Usage: i2cset [-fy] [-m MASK] BUS CHIP-ADDRESS DATA-ADDRESS [VALUE] ... [MODE]
Set I2C registers
I2CBUS I2C bus number
ADDRESS 0x03-0x77
MODE is:
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 Force access
-y Disable interactive mode
-r Read back and compare the result
-m MASK Mask specifying which bits to write
设置3号总线上设备(0x5f)寄存器地址0x10的值为1
i2cset -f -y 3 0x5f 0x10 0x1
i2ctransfer
sage: i2ctransfer [-f] [-y] [-v] [-V] [-a] I2CBUS DESC [DATA] [DESC [DATA]]...
I2CBUS is an integer or an I2C bus name
DESC describes the transfer in the form: {r|w}LENGTH[@address]
1) read/write-flag 2) LENGTH (range 0-65535) 3) I2C address (use last one if omitted)
DATA are LENGTH bytes for a write message. They can be shortened by a suffix:
= (keep value constant until LENGTH)
+ (increase value by 1 until LENGTH)
- (decrease value by 1 until LENGTH)
p (use pseudo random generator until LENGTH with value as seed)
Example (bus 0, read 8 byte at offset 0x64 from EEPROM at 0x50):
# i2ctransfer 0 w1@0x50 0x64 r8
Example (same EEPROM, at offset 0x42 write 0xff 0xfe ... 0xf0):
# i2ctransfer 0 w17@0x50 0x42 0xff-
sudo i2ctransfer -f -y 3 w1@0x5f 0x64 r8
读寄存器(0x2102)的两个字节的值
sudo i2ctransfer -f -y 4 w2@0x5f 0x21 0x02 r2
在寄存器(0x5013)中写入0x03的值
sudo i2ctransfer -f -y 4 w3@0x5f 0x50 0x13 0x03