I2CTransplant i2c-tool tool based on Linux

Article directory

  • 1. Download the i2c-tool tool
  • 2. Compile i2c-tool source code
  • 3. Usage of i2cdetect
  • 4. Usage of i2cdump
  • 5. Usage of i2cget
  • 6. Usage of i2cset
  • 7. Usage of i2ctransfer

1. Download i2c-tool

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

2. Compile i2c-tool source code

Because I need to port the i2c-tool to the imx6ull embedded platform, I must re-specify GCC when compiling. Check the Makefile file in the root directory of i2c-tool, which is compiled by the system GCC tool by default:

CC ?= gcc
AR ?= ar
STRIP ?= strip

Before compiling the i2c-tool source code on ubuntu, first set the cross-tool chain of the imx6ull embedded platform, you can directly enter the following command on the command line:

export ARCH=arm
export CROSS_COMPILE=arm-none-linux-gnueabihf-
export PATH=$PATH:/opt/ToolChain/gcc-arm-10.2-2020.11-x86_64-arm-none-linux-gnueabihf/bin

Just specify GCC when make:

make CC=arm-none-linux-gnueabihf-gcc AR=arm-none-linux-gnueabihf-ar STRIP=arm-none-linux-gnueabihf-strip USE_STATIC_LIB=1

Adding USE_STATIC_LIB=1 is for static compilation, because the default is dynamic compilation, and libi2c.so needs to be copied when running the program.

3. Usage of i2cdetect

How to use the i2cdetect command:

# ./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.

-f: Force mode, allowing user to read/write registers from/to registers even if the device is already registered by the driver.
-y: Enter yes by default, and no longer ask the user for confirmation.

  • List all I2C buses
    The command is as follows:

    ./i2cdetect -l
    

    The output is as follows:

  • List all devices on the I2Cx bus
    The command is as follows:

    ./i2cdetect -y -a 4
    

    The output is as follows:

    -- indicates that there is no device corresponding to this address,
    UU indicates that the device exists and it already has a driver,
    value indicates that the device exists but there is no corresponding device driver

4. Usage of i2cdump

How to use the i2cdump command:

# ./i2cdump
Error: No i2c-bus specified!
Usage: i2cdump [-f] [-y] [-r first-last] [-a] I2CBUS ADDRESS [MODE [BANK [BANKREG]]]
  I2CBUS is an integer or an I2C bus name
  ADDRESS is an integer (0x08 - 0x77, or 0x00 - 0x7f if -a is given)
  MODE is one of:
    b (byte, default)
    w (word)
    W (word on even register addresses)
    s (SMBus block, deprecated)
    i (I2C block)
    c (consecutive byte)
    Append p for SMBus PEC

-f: Force mode, allowing user to read/write registers from/to registers even if the device is already registered by the driver.
-y: Enter yes by default, and no longer ask the user for confirmation.

  • Display the register value of social security on the I2C bus
    Display the register value of the 0x30 device on the I2C 4 bus, the MODE is byte, the command is as follows:
    ./i2cdump -f -y 4 0x30 b
    

    The output is as follows:

5. Usage of i2cget

How to use the i2cget command:

# ./i2cget
Usage: i2cget [-f] [-y] [-a] I2CBUS CHIP-ADDRESS [DATA-ADDRESS [MODE [LENGTH]]]
  I2CBUS is an integer or an I2C bus name
  ADDRESS is an integer (0x08 - 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)
    s (read SMBus block data)
    i (read I2C block data)
    Append p for SMBus PEC
  LENGTH is the I2C block data length (between 1 and 32, default 32)

-f: Force mode, allowing user to read/write registers from/to registers even if the device is already registered by the driver.
-y: Enter yes by default, and no longer ask the user for confirmation.

  • Read a byte data from a register
    Read the value of the register 0x85 from the 0x30 device on the i2c bus 4, the size is 1 byte. The default mode is b, which can be omitted here. The command is as follows:
    i2cget -f -y 4 0x30 0x85
    
  • Read a word data from a register
    Read the value of the register 0x85 from the 0x30 device on the i2c bus 4, the size is 1word (1word = 2byte). The command is as follows:
    i2cget -f -y 4 0x30 0x85 w
    

6. Usage of i2cset

How to use the i2cset command:

# ./i2cset
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 (0x08 - 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: Force mode, allowing user to read/write registers from/to registers even if the device is already registered by the driver.
-y: Enter yes by default, and no longer ask the user for confirmation.

  • Write a byte data into a register
    Write 0x23 to the 0x30 device register 0x85 on the i2c bus 4, the size is 1 byte. The default mode is b, which can be omitted here. The command is as follows:
    i2cget -f -y 4 0x30 0x85 0x23
    
  • Write a word data into a register
    Write 0x2345 to 0x30 device read register 0x85 on i2c bus 4, the size is 1word (1word = 2byte). The command is as follows:
    ./i2cset -f -y 4 0x30 0x85 0x2345 w
    

7. Usage of i2ctransfer

The i2cget and i2cset commands use the SMBus protocol, i2ctransfer The code>command uses the I2C protocol. SMBus is based on the I2C protocol, SMBus has stricter requirements, and SMBus is a subset of the I2C protocol.

i2ctransfer is used as follows:

# ./i2ctransfer
Usage: 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, or '?')
    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 bytes 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-

-f: Force mode, allowing user to read/write registers from/to registers even if the device is already registered by the driver.
-y: Enter yes by default, and no longer ask the user for confirmation.

  • Write multiple data into a register
    Write 3 byte data to the 0x30 device on the i2c bus 4, the command is as follows:

    i2ctransfer -y -f 4 w3@0x30 0x85 0x01 0x10
    

    The specific instructions are as follows:

  • Read multiple data from a register
    Read 3 byte data from the 0x30 device 0x8501 register on the i2c bus 4, the command is as follows:

    i2ctransfer -y -f 4 w2@0x30 0x85 0x01 r3
    

    The specific instructions are as follows: