i2c-tool
工具下载地址:
https://mirrors.edge.kernel.org/pub/software/utils/i2c-tools/
因为我这里需要将i2c-tool移植到imx6ull嵌入式平台,所以编译时肯定需要重新指定GCC。查看i2c-tool根目录下的Makefile
文件,默认为系统GCC工具编译:
CC ?= gcc
AR ?= ar
STRIP ?= strip
在ubuntu编译i2c-tool源码之前,首先设置imx6ull嵌入式平台的交叉工具链,可以直接在命令行输入如下命令:
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
在make
时指定GCC即可:
make CC=arm-none-linux-gnueabihf-gcc AR=arm-none-linux-gnueabihf-ar STRIP=arm-none-linux-gnueabihf-strip USE_STATIC_LIB=1
添加USE_STATIC_LIB=1
是为了静态编译,因为默认为动态编译,运行程序时还需要拷贝libi2c.so。
i2cdetect
命令的使用方法:
# ./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
:强制模式,即使驱动程序已经注册该设备,也允许用户从寄存器读取/写入寄存器。
-y
:默认输入yes,不再询问用户确认。
列出I2C所有总线
命令如下:
./i2cdetect -l
列出I2Cx总线上所有的设备
命令如下:
./i2cdetect -y -a 4
--
表示没有该地址对应的设备,
UU
表示有该设备并且它已经有驱动程序,
数值
表示有该设备但是没有对应的设备驱动
i2cdump
命令的使用方法:
# ./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
:强制模式,即使驱动程序已经注册该设备,也允许用户从寄存器读取/写入寄存器。
-y
:默认输入yes,不再询问用户确认。
i2cget
命令的使用方法:
# ./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
:强制模式,即使驱动程序已经注册该设备,也允许用户从寄存器读取/写入寄存器。
-y
:默认输入yes,不再询问用户确认。
4
上的0x30
设备读取寄存器0x85的值,大小为1byte。默认模式的为b
,这里可以省略。命令如下:i2cget -f -y 4 0x30 0x85
4
上的0x30
设备读取寄存器0x85的值,大小为1word(1word = 2byte)。命令如下:i2cget -f -y 4 0x30 0x85 w
i2cset
命令的使用方法:
# ./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
:强制模式,即使驱动程序已经注册该设备,也允许用户从寄存器读取/写入寄存器。
-y
:默认输入yes,不再询问用户确认。
4
上的0x30
设备寄存器0x85写入0x23,大小为1byte。默认模式的为b
,这里可以省略。命令如下:i2cget -f -y 4 0x30 0x85 0x23
4
上的0x30
设备读取寄存器0x85写入0x2345,大小为1word(1word = 2byte)。命令如下:./i2cset -f -y 4 0x30 0x85 0x2345 w
i2cget
和i2cset
命令使用的是SMBus协议,i2ctransfer
命令使用的是I2C协议。SMBus是基于I2C协议的,SMBus要求更严格,SMBus是I2C协议的子集。
i2ctransfer
使用方法如下:
# ./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 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-
-f
:强制模式,即使驱动程序已经注册该设备,也允许用户从寄存器读取/写入寄存器。
-y
:默认输入yes,不再询问用户确认。