Linux I2C Tools

本文基于RockPi 4A Debian系统介绍i2c设备的调试工具i2c-tools。

i2c-tools的相关命令常用于linux系统读写i2c设备寄存器的在线调试。

安装命令:


apt-get update
apt-get install i2c-tools

linux版本代码下载路径:


https://mirrors.edge.kernel.org/pub/software/utils/i2c-tools/

一、i2cdetect

1、命令


root@vxworks:/# 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.

2、用法



## 列出i2c总线
root@vxworks:/# i2cdetect -l
i2c-0   i2c             rk3x-i2c                                I2C adapter
i2c-1   i2c             rk3x-i2c                                I2C adapter
i2c-9   i2c             DesignWare HDMI                         I2C adapter
## 列出某个i2c总线上的i2c设备
root@vxworks:/# i2cdetect -y 0
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- UU -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: UU UU -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
root@vxworks:/# 

RockPi 4A原理图中,I2C0总线挂载I2C设备地址如下:

Linux I2C Tools

二、i2cdump

1、命令


root@vxworks:~# i2cdump
Error: No i2c-bus specified!
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

2、用法


## 显示I2C 0号总线上0x1b设备的寄存器值。MODE为byte,可省略。
root@vxworks:~# i2cdump -f -y 0 0x1b b
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f    0123456789abcdef
00: 12 50 08 21 01 13 01 00 00 00 00 01 01 00 00 00    ?P?!???....??...
10: 80 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ??..............
20: 01 17 00 6f ff 00 00 00 10 00 ff 0f ff 02 19 0f    ??.o....?..?.???
30: 00 00 19 07 00 00 02 03 00 00 09 00 00 00 00 0a    ..??..??..?....?
40: 00 0c 00 0c 00 07 00 01 00 0c 00 00 00 5f 00 03    .?.?.?.?.?..._.?
50: 06 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ??..............
60: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
70: 00 cf 03 00 28 00 0c 1c 80 19 00 34 12 00 71 00    .??.(.????.4?.q.
80: 10 50 1f ac 00 40 10 01 40 00 08 00 09 09 00 00    ?P??.@??@.?.??..
90: 55 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    U...............
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

1、命令


root@vxworks:~# 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

2、用法



## 显示I2C 0号总线上0x1b设备0x0和0x1寄存器地址,按字节读取
root@vxworks:~# i2cget -f -y 0 0x1b 0x0
0x12
root@vxworks:~# i2cget -f -y 0 0x1b 0x1
0x50
## 按字读取
root@vxworks:~# i2cget -f -y 0 0x1b 0x0 w
0x5012

四、i2cset

1、命令


root@vxworks:~# i2cset
Usage: i2cset [-f] [-y] [-m MASK] [-r] I2CBUS CHIP-ADDRESS DATA-ADDRESS [VALUE] ... [MODE]
  I2CBUS is an integer or an I2C bus name
  ADDRESS is an integer (0x03 - 0x77)
  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

2、用法


root@vxworks:/sys/rk8xx# i2cget -f -y 0 0x1b 0x10
0x80
root@vxworks:/sys/rk8xx# i2cset -f -y 0 0x1b 0x10 0x0
root@vxworks:/sys/rk8xx# i2cget -f -y 0 0x1b 0x10
0x00

注:RK808-D 是PMIC芯片,如果没有芯片手册,建议慎重使用i2cset命令设置RK808-D寄存器的值。

五、i2ctransfer

i2c-tools-4.0及以后版本添加了i2ctransfer命令。

i2cget和i2cset可以读写的i2c设备的寄存器地址小于0xff,即寄存器是8位地址。

如果i2c设备寄存器是16位地址,可使用i2ctransfer命令,该命令同样可用在寄存器地址是8位的设备。

1、命令


"Usage: i2ctransfer [-f] [-y] [-v] [-V] [-a] I2CBUS DESC [DATA] [DESC [DATA]]...\n"
"  I2CBUS is an integer or an I2C bus name\n"
"  DESC describes the transfer in the form: {r|w}LENGTH[@address]\n"
"    1) read/write-flag 2) LENGTH (range 0-65535, or '?')\n"
"    3) I2C address (use last one if omitted)\n"
"  DATA are LENGTH bytes for a write message. They can be shortened by a suffix:\n"
"    = (keep value constant until LENGTH)\n"
"    + (increase value by 1 until LENGTH)\n"
"    - (decrease value by 1 until LENGTH)\n"
"    p (use pseudo random generator until LENGTH with value as seed)\n\n"
"Example (bus 0, read 8 byte at offset 0x64 from EEPROM at 0x50):\n"
"  # i2ctransfer 0 w1@0x50 0x64 r8\n"
"Example (same EEPROM, at offset 0x42 write 0xff 0xfe ... 0xf0):\n"
"  # i2ctransfer 0 w17@0x50 0x42 0xff-\n");

2、用法


## 从i2c 4号总线0x38设备的0x3a01寄存器开始读16个字节的数据,w2:表示寄存器0x3a01的长度为2个字节
i2ctransfer -y -f 4 w2@0x38 0x3a 0x01 r16
## 向i2c 4号总线0x38设备的0x3a01寄存器写0x10,w3:表示寄存器0x3a01和写入值0x10的长度为3字节
i2ctransfer -y -f 4 w3@0x38 0x3a 0x01 0x10