25 IIC(三)I2C_Tools

i2c-tools

1. 编译i2c-tools

  • 从官网下载相关源码包

  • 设置交叉编译工具链

    export ARCH=arm
    export CROSS_COMPILE=arm-linux-gnueabihf-
    export PATH=$PATH:/home/book/100ask_imx6ull-sdk/ToolChain/gcc-linaro-6.2.1-2016.11-x86_64_arm-linux-gnueabihf/bin
    
  • 修改makefile,在makefile中指定修改后的工具链

    CC      ?= gcc
    AR      ?= ar
    STRIP   ?= strip
    改为(指定交叉编译工具链前缀, 去掉问号):
    CC      = $(CROSS_COMPILE)gcc
    AR      = $(CROSS_COMPILE)ar
    STRIP   = $(CROSS_COMPILE)strip
    
  • make编译

    编译后将libi2c.so放到开发板的lib目录下,将编译的执行文件上传到开发板子中

2. i2c-tools命令的使用

2.1 i2cdetect(i2c检测)

2.1.1 用法

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.

	-q 使用SMBus Qucik Write。不推荐
	-r 使用SMBus receive byte。不推荐

2.1.2 示例

i2cdetect -l					// 列出当前I2C总线
i2cdetect -F I2Cbus_number		// 列出i2c总线支持的功能
i2cdetect -y -a I2Cbus_number	// 列出总线上的设备。--表示没有该地址对应的设备, UU表示有该设备并且它已经有驱动程序,
								// 数值表示有该设备但是没有对应的设备驱动


2.2 i2cget(读数据)

2.2.1 用法

Usage: i2cget [-f] [-y] [-a] I2CBUS CHIP-ADDRESS [DATA-ADDRESS [MODE]]
								IIC		芯片地址		数据地址		
  I2CBUS is an integer or an I2C bus name
  ADDRESS is an integer (0x03 - 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)		写字/读字 ???
    Append p for SMBus PEC

	-f 强制访问。i2cget默认将拒绝访问已经在存和驱动程序控制下的程序。所以一般最好不用
	-y 禁用交互模式。默认情况i2cget将会在使用或者称为干扰i2c总线前等待对方确认。-y则忽略此步骤,直接生效。
	-a 允许使用0x00-0x07和0x78-0x7f之间的地址
	一般而言上述操作都不推荐,仅仅用于自己调试。

2.2.2 示例

2.3 i2cset(写数据)

2.3.1 用法

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 (0x03 - 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

	-r 写入值之后立即回读该值,并将结果与写入值进行比较

2.3.2 示例

写一个字节
i2cset -f -y I2CBUS CHIP-ADDRESS DATA-ADDRESS VALUE
写两个字节
i2cset -f -y I2CBUS CHIP-ADDRESS DATA-ADDRESS VALUE w
写N个字节
i2cset -f -y I2CBUS CHIP-ADDRESS DATA-ADDRESS VALUE1 ... VALUEN s
i2cset -f -y I2CBUS CHIP-ADDRESS DATA-ADDRESS VALUE1 ... VALUEN i

2.4 i2cdump(查看所有寄存器的值)

2.4.1 用法

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 (0x03 - 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)
    i (I2C block)
    c (consecutive byte)
    Append p for SMBus PEC

	ADDRESS 设备地址

2.4.2 示例

2.5 i2ctransfer(i2c的读写)

此方法为i2c的读写

2.5.1 用法

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) 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-

	address 设备地址

2.5.2 示例

写2个数据0 0x4到0xle
i2ctransfer -f -y 0 w2@0xle 0 0x4

写数据0xc到地址0xle,然后读出两个数据
i2ctransfer -f -y 0 w1@0xle 0xc r2
posted @ 2023-05-14 14:23  人民广场的二道贩子  阅读(63)  评论(0编辑  收藏  举报