Linux i2c:用户空间“免设备驱动”读写i2c设备方法2——i2c-tools工具(i2cdetect/i2cdump...)的编译及使用示例

1、准备

如上篇文章所说,用户空间“免设备驱动”读写i2c设备的前提是内核里面配置了i2c-dev,由于i2c-tools工具与上一篇《参考内核文档(Documentation/i2c/dev-interface)编写自己的测试程序》的本质都是一样的(注意:有一点与上一篇文章的应用程序不同,i2c-tools是可以搭配“设备驱动”一起调试的),所以也需要确认是否已经配置了i2c-dev,菜单路径如下:

Device Drivers  ---> 
	-*- I2C support  --->
		<*> I2C device interface

2、i2c-tools源码下载

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

3、文件内容快速了解

3.1 README

There’s no configure script, so simply run “make” to build the tools, and
“make install” to install them. You also can use “make uninstall” to remove
all the files you installed. By default, files are installed in /usr/local
but you can change this behavior by editing the Makefile file and setting
prefix to wherever you want. You may change the C compiler and the
compilation flags as well.
Optionally, you can run “make strip” prior to “make install” if you want
smaller binaries. However, be aware that this will prevent any further
attempt to debug the programs.

通过README可以知道只需要make编译和make install安装即可,但同时也允许我们改变C编译器(CC)为交叉编译和安装的路径(prefix),继续往下看Makefile内容就清楚了。

3.2 Makefile
prefix	= /usr/local
bindir	= $(prefix)/bin
sbindir	= $(prefix)/sbin
...
CC	:= gcc
CFLAGS	:= -Wall
CFLAGS	+= -O2
...
include tools/Module.mk

其中末尾的tools/Module.mk文件里可以看到:

install-tools: $(addprefix $(TOOLS_DIR)/,$(TOOLS_TARGETS))
	$(INSTALL_DIR) $(DESTDIR)$(sbindir) $(DESTDIR)$(man8dir)
	...

install: install-tools

4、编译安装

tar xzvf i2c-tools-3.0.2.tar.gz	# 解压
cd i2c-tools-3.0.2/
make CC=arm-linux-gnueabihf-gcc	# 交叉编译
make prefix=$PWD/tmp install	# 安装
ls -l ./tmp/sbin/				# 查看安装得到的工具:i2cdetect i2cdump i2cget i2cset

另外,可以使用strip命令去除符号信息等来减小文件大小,不过需要先修改tools/Module.mk(如下所示),然后再执行make strip命令:

--- a/tools/Module.mk
+++ b/tools/Module.mk
@@ -57,7 +57,7 @@ $(TOOLS_DIR)/util.o: $(TOOLS_DIR)/util.c $(TOOLS_DIR)/util.h
 all-tools: $(addprefix $(TOOLS_DIR)/,$(TOOLS_TARGETS))
 
 strip-tools: $(addprefix $(TOOLS_DIR)/,$(TOOLS_TARGETS))
-       strip $(addprefix $(TOOLS_DIR)/,$(TOOLS_TARGETS))
+       arm-linux-gnueabihf-strip $(addprefix $(TOOLS_DIR)/,$(TOOLS_TARGETS))
 
 clean-tools:
        $(RM) $(addprefix $(TOOLS_DIR)/,*.o $(TOOLS_TARGETS))

5、使用

① 检测系统中的i2c总线

/ # i2cdetect -l
i2c-0   i2c             21a0000.i2c                             I2C adapter
i2c-1   i2c             21a4000.i2c                             I2C adapter

② 检测接在i2c-0上的设备

/ # i2cdetect -y -r 0
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --

③ 查看i2c-0上地址为0x68的设备中所有寄存器的值(重要!!!)

/ # i2cdump -f -y 0 0x68
No size specified (using byte-data access)
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f    0123456789abcdef
00: 81 01 81 2e 5e e3 fa 84 f1 05 03 3e 28 6f 6e 95    ???.^??????>(on?
10: 43 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    C...............
20: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
30: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
40: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
50: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
60: 00 00 00 00 00 00 00 00 00 00 00 40 00 00 00 00    ...........@....
70: 00 00 00 00 00 68 00 00 00 00 00 00 00 00 00 00    .....h..........
80: 81 01 81 2e 5e e3 fa 84 f1 05 03 3e 28 6f 6e 95    ???.^??????>(on?
90: 43 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    C...............
a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
e0: 00 00 00 00 00 00 00 00 00 00 00 40 00 00 00 00    ...........@....
f0: 00 00 00 00 00 68 00 00 00 00 00 00 00 00 00 00    .....h..........

④ 使用i2cget/i2cset对i2c-0上地址为0x68的设备进行读写某个寄存器的值
(注意:并非所有的写数据都会生效,还取决设备是否允许对该寄存器写入数据)

/ # i2cget -f -y 0 0x68 0x6b
0x40
/ # i2cset -f -y 0 0x68 0x6b 0x00
/ # i2cget -f -y 0 0x68 0x6b
0x00

参考:i2c-tools的使用方法及举例 – 博客园

猜你喜欢

转载自blog.csdn.net/weixin_44498318/article/details/112912993