2025-02-22
在进行嵌入式Linux开发的过程中,经常需要和i2c
设备打交道。所以需要获取i2c
设备的状态,进行读写操作。在开源的世界里,当然有大神已经为我们写好了对应的工具:i2c-tools
。这个工具箱里包含了四个工具:i2cget
,i2cdump
,i2cset
和i2ctransfer
工具,帮助我们快速进行调试。
sudo apt-get install i2c-tools
或者到这个地址下载
https://mirrors.edge.kernel.org/pub/software/utils/i2c-tools/
或者使用git下载
git clone https://kernel.googlesource.com/pub/scm/utils/i2c-tools/i2c-tools
Makefile
中的下面编译器选项CC ?= gcc
AR ?= ar
将tools
目录下的i2cdetect、i2ctransfer、i2cdump、i2cset和i2cget复制到开发板
将lib/libi2c.so.0.1.1
复制到开发版的/usr/lib
目录下
i2c
设备:i2cdetect -l
i2c-0 unknown SMBus PIIX4 adapter at 4100 N/A
i2c-0
上的设备:i2cdetect -r -y 0
--
表示没有挂载设备UU
表示该设备已经被占用 0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- 16 -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- UU -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
i2c
上的功能:i2cdetect -F 0
Functionalities implemented by /dev/i2c-0:
I2C no
SMBus Quick Command yes
SMBus Read Byte yes
...
10-bit addressing no
Target mode no
读取i2c-0
上的0x16
设备位置0x10
上的数据:i2cget -y -f 0 0x16 0x10
读取i2c-0
上的0x16
设备上的数据:i2cdump -y -f 0 0x16
向i2c-0
上的0x16
设备位置0x10
上写入数据0xff
:i2cset -y -f 0 0x16 0x10 0xff
一次写入4个数据0x1 0x2 0x3 0x4
到i2c-0
上的0x16
设备的0x10
位置
i2ctransfer -y -f 0 w5@0x16 0x10 0x1 0x2 0x3 0x4
i2c-0
上的0x16
设备的0x10
位置一次读取4个数据i2ctransfer -y -f 0 w1@0x16 0x10 r4
上面只是列举了一些常用的例子,更多的使用方法可使用man
命令进行查看。