1 篇头
- 在编写某I2C设备之前,可以使用开源的I2C测试工具,先对总线和设备的连通性做一个简单测试,或更进一步手动发送一些命令,进行设备的初始化、检验相关功能。
- 本文即介绍i2ctools工具,它的源文件只有1个.c文件,但编译后有一组子工具。在全志的芯片平台上,基本都有携带,可以直接使用,例如H713以及本文的H618平台,掌握此工具后,可大幅提升I2C设备的开发和调试效率。
2 工具源码
- 位置: android\external\toybox\toys\other\i2ctools.c
3 工具介绍
i2cdetect -- 检测指定总线上的I2C设备
i2cdump -- dump指定I2C设备的寄存器数值
i2cget -- 读取I2C数据
i2cset -- 设置I2C数据
4 查看可用工具
- 可用tab补齐功能,先看下自己的平台是否有这些工具
console:/ # i2c(tab)
i2cdetect i2cdump i2cget i2cset
5 查询可用总线
- 进一步,可以看下系统当前已配置了哪几个I2C总线,以标准的Linux I2C总线的命名习惯,都是以i2c-开头,以总线的序号结尾,即i2c-num。
console:/ # ls -l /dev/i2c*
i2c-3 i2c-5
6 i2cdetect
- 检测总线上可用设备的i2c addr
- 如下所示,检测都1个设备,地址为0x1E
console:/ # i2cdetect --help
Toybox 0.8.4-android multicall binary: https://landley.net/toybox (see toybox --help)
usage: i2cdetect [-ary] BUS [FIRST LAST]
usage: i2cdetect -F BUS
usage: i2cdetect -l
Detect i2c devices.
-a All addresses (0x00-0x7f rather than 0x03-0x77)
-F Show functionality
-l List all buses
-r Probe with SMBus Read Byte
-y Answer "yes" to confirmation prompts (for script use)
console:/ # i2cdetect -a
i2cdetect: bad args
1|console:/ # i2cdetect -a 3
Probe chips 0x00-0x7f on bus 3? (Y/n):y
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- 1e --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
console:/ #
7 i2cset
- 向指定I2C设备的寄存器地址写入数据
- i2cset -y 3 0x1E 0x00 0x3 b
- i2cset -y 总线序号 设备地址 寄存器地址 数值 模式
- MODE:支持b字节、w双字节、i为数据块
console:/ # i2cset --help
Toybox 0.8.4-android multicall binary: https://landley.net/toybox (see toybox --help)
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)
console:/ #
console:/ # i2cset -y 3 0x1E 0x00 0x3 b
8 i2cget
- 读取I2C指定设备指定地址上的数据,一次只能读取1个字节
- 如下我们将楼上写入的数据又读了出来,为0x03,数据一致
console:/ # i2cget --help
Toybox 0.8.4-android multicall binary: https://landley.net/toybox (see toybox --help)
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)
console:/ #
console:/ # i2cget -y 3 0x1E 0x00
0x03
console:/ #
9 i2cdump
console:/ # i2cdump --help
Toybox 0.8.4-android multicall binary: https://landley.net/toybox (see toybox --help)
usage: i2cdump [-fy] BUS CHIP
Dump i2c registers.
-f Force access to busy devices
-y Answer "yes" to confirmation prompts (for script use)
console:/ #
console:/ #
console:/ # i2cd
i2cdetect i2cdump
console:/ # i2cdump -y 3 0x1E
0 1 2 3 4 5 6 7 8 9 a b c d e f 0123456789abcdef
00: 03 00 00 02 02 01 ee ee 00 80 00 00 00 00 00 00 ?..?????.?......
10: 00 01 00 03 ee ee ee ee ee 40 00 00 ff ff ee ee .?.??????@..????
20: 05 13 01 00 00 ee ee ee 00 00 00 80 00 80 ee ee ???..???...?.???
30: 00 00 ee 05 ee 00 ee 1b 00 00 00 00 00 00 00 00 ..???.??........
40: 82 84 80 93 80 8f 86 80 ee ee ee ee ee ee ee ee ????????????????
50: ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ????????????????
60: ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ????????????????
70: ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ????????????????
80: ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ????????????????
90: ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ????????????????
a0: ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ????????????????
b0: ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ????????????????
c0: ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ????????????????
d0: ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ????????????????
e0: ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ????????????????
f0: ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ????????????????
console:/ #