I2C command

I2C 寄存器读写命令

探测设备

i2cdetect

HELLO@WORLD:~# 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.
# I2CBUS: 0 1 2 ...
# Notice: -r 参数很重要,不带 -r 参数有些设备探测不到
i2cdetect -y -r <I2CBUS>

对于8bit的reg, value 可以使用 i2cget / i2cset

i2cget

HELLO@WORLD:~# 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
# SLAVE_ADDRESS: 使用i2cdetect 出来的结果
i2cget -y -f <I2CBUS> <SLAVE_ADDRESS> <REG>

i2cset

HELLO@WORLD:~# 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
i2cset -y -f <I2CBUS> <SLAVE_ADDRESS> <REG> <VALUE>

对于16bit+的reg, value 需要使用 i2ctransfer

i2ctransfer

root@J3Pilot-B:~# i2ctransfer
Usage: i2ctransfer [-f] [-y] [-v] [-V] 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-
# W3 写三个参数 16bit 寄存器 (2 Bytes)+ value(1 Byte)
# i2c bus 0
# slave address 0x36
i2ctransfer -y -f 0 w3@0x33 0x3B 0x9D 0x01
# w2 (16bit寄存器)写两个参数 0x4F 0x0C 寄存器(0x4F0C)到 0x36 slave address
# i2c bus 总线 0
# 读取 i2c 总线 0 的 slave address 为 0x36 的设备的寄存器 0x4F0C, 读一个 char
# 0x36 是 ovx3c 的 slave address (7bit i2c address)
i2ctransfer -y -f 0 w2@0x36 0x4F 0x0C r1

dump 寄存器 00~FF

i2cdump

HELLO@WORLD:~# 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
i2cdump -f -y <I2CBUS> <SLAVE_ADDRESS>