最近调试了挺多款TP驱动,对I2C设备有了一定的了解,本篇文章主要讲解i2c-tools在linux和android中的安装编译和使用。i2c-tools软件包包含用于Linux的一组不同的I2C工具:总线探测工具,chip dumper,寄存器级SMBus访问帮助器,EEPROM解码脚本,EEPROM编程工具以及用于SMBus访问的python模块。 只要内核中包含I2C支持,就支持所有版本的Linux。
可以访问i2c-tools官网获取一些信息
直接访问i2c-tools下载链接选择当前最新版本下载
或者通过git安装
git clone git://git.kernel.org/pub/scm/utils/i2c-tools/i2c-tools.git
也可以指定版本
git clone git://git.kernel.org/pub/scm/utils/i2c-tools/i2c-tools.git -b i2c-tools-3.1
下载好后解压进入i2c-tools-4.1目录,直接执行
make && make install
也可以编译成静态的
make USE_STATIC_LIB=1 && make install
需要交叉编译的话,修改Makefile中的CC、AR变量,改成自己的交叉编译工具链
CC ?= gcc
AR ?= ar
添加packages/utils/i2c-tools/Makefile
#
# Copyright (C) 2007-2015 OpenWrt.org
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#
include $(TOPDIR)/rules.mk
PKG_NAME:=i2c-tools
PKG_VERSION:=4.1
PKG_RELEASE:=3
PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.xz
PKG_SOURCE_URL:=@KERNEL/software/utils/i2c-tools
PKG_HASH:=57b219efd183795bd545dd5a60d9eabbe9dcb6f8fb92bc7ba2122b87f98527d5
PKG_MAINTAINER:=Daniel Golle <daniel@makrotopia.org>
PKG_LICENSE:=GPL-2.0-or-later LGPL-2.1-or-later
PKG_LICENSE_FILES:=COPYING COPYING.LGPL
PKG_BUILD_PARALLEL:=1
PKG_BUILD_DIR:=$(BUILD_DIR)/$(BUILD_VARIANT)-i2c-tools-$(PKG_VERSION)
include $(INCLUDE_DIR)/package.mk
include ../../lang/python/python-package.mk
include ../../lang/python/python3-package.mk
PKG_UNPACK:=$(HOST_TAR) -C $(PKG_BUILD_DIR) --strip-components=1 -xJf $(DL_DIR)/$(PKG_SOURCE)
define Package/i2c/Default
URL:=https://i2c.wiki.kernel.org/index.php/I2C_Tools
TITLE:=I2C
endef
define Package/libi2c
$(call Package/i2c/Default)
SECTION:=libs
CATEGORY:=Libraries
TITLE+=library for i2c-tools
VARIANT:=bin
endef
define Package/i2c-tools
$(call Package/i2c/Default)
SECTION:=utils
CATEGORY:=Utilities
TITLE+=tools for Linux
DEPENDS:=+libi2c
VARIANT:=bin
endef
define Package/python-smbus
$(call Package/i2c/Default)
SUBMENU:=Python
SECTION:=lang
CATEGORY:=Languages
TITLE:=Python bindings for the SMBUS
DEPENDS:=+PACKAGE_python-smbus:libi2c +PACKAGE_python-smbus:python-light
VARIANT:=python
endef
define Package/python3-smbus
$(call Package/i2c/Default)
SUBMENU:=Python
SECTION:=lang
CATEGORY:=Languages
TITLE:=Python bindings for the SMBUS
DEPENDS:=+PACKAGE_python3-smbus:libi2c +PACKAGE_python3-smbus:python3-light
VARIANT:=python3
endef
define Package/libi2c/description
This package contains i2c functionality needed by i2c-tools.
endef
define Package/i2c-tools/description
This package contains an heterogeneous set of I2C tools for Linux. These tools
were originally part of the lm-sensors package.
endef
define Package/python-smbus/description
This package contain the python bindings for Linux SMBus access through i2c-dev.
endef
define Package/python3-smbus/description
This package contain the Python3 bindings for Linux SMBus access through i2c-dev.
endef
ifeq ($(BUILD_VARIANT),bin)
define Build/Compile
$(MAKE) -C $(PKG_BUILD_DIR) \
LINUX="$(LINUX_DIR)" \
CC="$(TARGET_CC)" \
STAGING_DIR="$(STAGING_DIR)" \
LDFLAGS="$(TARGET_LDFLAGS)" \
CFLAGS="$(TARGET_CFLAGS)"
endef
define Build/InstallDev
$(INSTALL_DIR) $(1)/usr/include $(1)/usr/lib
$(CP) $(PKG_BUILD_DIR)/include/i2c $(1)/usr/include/
$(CP) $(PKG_BUILD_DIR)/lib/libi2c.{a,so*} $(1)/usr/lib/
endef
endif # ifeq
PYTHON_PKG_SETUP_ARGS:=
PYTHON3_PKG_SETUP_ARGS:=
PYTHON_PKG_SETUP_DIR:=py-smbus
PYTHON3_PKG_SETUP_DIR:=py-smbus
define Package/libi2c/install
$(INSTALL_DIR) $(1)/usr/lib
$(CP) $(PKG_BUILD_DIR)/lib/libi2c.so* $(1)/usr/lib/
endef
define Package/i2c-tools/install
$(INSTALL_DIR) $(1)/usr/sbin
$(INSTALL_BIN) $(PKG_BUILD_DIR)/tools/i2cdetect $(1)/usr/sbin/
$(INSTALL_BIN) $(PKG_BUILD_DIR)/tools/i2cdump $(1)/usr/sbin/
$(INSTALL_BIN) $(PKG_BUILD_DIR)/tools/i2cset $(1)/usr/sbin/
$(INSTALL_BIN) $(PKG_BUILD_DIR)/tools/i2cget $(1)/usr/sbin/
$(INSTALL_BIN) $(PKG_BUILD_DIR)/tools/i2ctransfer $(1)/usr/sbin/
endef
$(eval $(call BuildPackage,libi2c))
$(eval $(call BuildPackage,i2c-tools))
$(eval $(call PyPackage,python-smbus))
$(eval $(call BuildPackage,python-smbus))
$(eval $(call Py3Package,python3-smbus))
$(eval $(call BuildPackage,python3-smbus))
把解压的文件放到android/external/i2c-tools文件夹,然后在i2c-tools文件夹中新建Android.mk
LOCAL_PATH:= $(call my-dir)
################### i2c-tools #########################
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := eng
LOCAL_MODULE := i2c-tools
LOCAL_SRC_FILES := \
tools/i2cbusses.c \
tools/util.c \
lib/smbus.c
LOCAL_C_INCLUDES += \
$(LOCAL_PATH) \
$(LOCAL_PATH)/include
#LOCAL_CFLAGS := -g -Wall -Werror -Wno-unused-parameter
include $(BUILD_STATIC_LIBRARY)
################### i2cdetect #########################
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := eng
LOCAL_MODULE:=i2cdetect
LOCAL_SRC_FILES:= \
tools/i2cdetect.c
LOCAL_C_INCLUDES += \
$(LOCAL_PATH) \
$(LOCAL_PATH)/include
LOCAL_SHARED_LIBRARIES:= \
libc
LOCAL_STATIC_LIBRARIES := \
i2c-tools
LOCAL_CPPFLAGS += -DANDROID
include $(BUILD_EXECUTABLE)
#################### i2cget ###########################
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := eng
LOCAL_MODULE:=i2cget
LOCAL_SRC_FILES:= \
tools/i2cget.c
LOCAL_C_INCLUDES += \
$(LOCAL_PATH) \
$(LOCAL_PATH)/include
LOCAL_SHARED_LIBRARIES:= \
libc
LOCAL_STATIC_LIBRARIES := \
i2c-tools
LOCAL_CPPFLAGS += -DANDROID
include $(BUILD_EXECUTABLE)
##################### i2cset ##########################
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := eng
LOCAL_MODULE:=i2cset
LOCAL_SRC_FILES:= \
tools/i2cset.c
LOCAL_C_INCLUDES += \
$(LOCAL_PATH) \
$(LOCAL_PATH)/include
LOCAL_SHARED_LIBRARIES:= \
libc
LOCAL_STATIC_LIBRARIES := \
i2c-tools
LOCAL_CPPFLAGS += -DANDROID
include $(BUILD_EXECUTABLE)
##################### i2cdump #########################
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := eng
LOCAL_MODULE:=i2cdump
LOCAL_SRC_FILES:= \
tools/i2cdump.c
LOCAL_C_INCLUDES += \
$(LOCAL_PATH) \
$(LOCAL_PATH)/include
LOCAL_SHARED_LIBRARIES:= \
libc
LOCAL_STATIC_LIBRARIES := \
i2c-tools
LOCAL_CPPFLAGS += -DANDROID
include $(BUILD_EXECUTABLE)
################### i2ctransfer #######################
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := eng
LOCAL_MODULE:=i2ctransfer
LOCAL_SRC_FILES:= \
tools/i2ctransfer.c
LOCAL_C_INCLUDES += \
$(LOCAL_PATH) \
$(LOCAL_PATH)/include
LOCAL_SHARED_LIBRARIES:= \
libc
LOCAL_STATIC_LIBRARIES := \
i2c-tools
LOCAL_CPPFLAGS += -DANDROID
include $(BUILD_EXECUTABLE)
然后在该目录下执行mm即可编译出应用i2cdetect、i2cdump、i2cset、i2cget、i2ctransfer,然后通过adb将生成的文件push到/system/bin目录下即可,或者重新打包固件烧录
i2cdetect用來列举I2C bus和上面所有的设备,可接受的参数有
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.
-V:输出当前版本号
root@Linux:/# i2cdetect -V
i2cdetect version 4.1
-l:输出所有 i2c 总线,如下总线编号有twi1和twi2,或者1和2
root@Linux:/# i2cdetect -l
i2c-1 i2c twi1 I2C adapter
i2c-2 i2c twi2 I2C adapter
root@Linux:/# ls -l /dev/i2c-*
crw------- 1 root root 89, 1 Jan 1 23:11 /dev/i2c-1
crw------- 1 root root 89, 2 Jan 1 23:11 /dev/i2c-2
I2CBUS:i2c总线编号
-F:此 i2c 支持的功能
root@Linux:/# i2cdetect -F 1
Functionalities implemented by /dev/i2c-1:
I2C yes
SMBus Quick Command yes
SMBus Send Byte yes
SMBus Receive Byte yes
SMBus Write Byte yes
SMBus Read Byte yes
SMBus Write Word yes
SMBus Read Word yes
SMBus Process Call yes
SMBus Block Write yes
SMBus Block Read no
SMBus Block Process Call no
SMBus PEC yes
I2C Block Write yes
I2C Block Read yes
-y:指令执行自动yes,否则会提示确认执行Continue? [Y/n] Y,不加参数y会有很多执行提示,可以帮助判断
-a:输出总线上所有地址(00-7f),没有 -a,只显示 08-77,UU 表示该设备在驱动中已使用,如下0x38的地址有设备
root@Linux:/# i2cdetect -a 1
WARNING! This program can confuse your I2C bus, cause data loss and worse!
I will probe file /dev/i2c-1.
I will probe address range 0x00-0x7f.
Continue? [Y/n] Y
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- UU -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
root@Linux:/# i2cdetect -y -a 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- UU -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
root@Linux:/# i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- UU -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
-q:快速写指令,-q和-r不能同时使用
-r:读指令,-q和-r不能同时使用
i2cdump读取设备上所有寄存器的值,可接受的参数有
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
-V:输出当前版本号
-f:强制使用此设备地址,即使此设备地址已经被使用;若不添加此参数,地址可能写失败
-y:指令执行自动 yes,否则会提示确认执行Continue? [Y/n] Y,不加参数y会有很多执行提示,可以帮助判断
-r:读取从 first-last 之间的寄存器值
-a:读取0x00-0xff范围的地址
I2CBUS:i2c总线编号
ADDRESS:设备地址,建议使用十六进制
MODE:不知道干嘛的
//0x38设备地址
root@Linux:/proc# i2cdump -f -y -a 1 0x38
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: 00 00 00 40 a8 00 b0 00 00 ff ff ff ff ff ff ff ...@?.?.........
10: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................
20: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................
30: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................
40: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................
50: ff ff ff 18 18 00 00 00 00 00 00 00 00 ff ff ff ...??...........
60: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................
70: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................
80: 1e 00 19 00 00 c8 01 1e 0a 28 00 00 00 00 00 00 ?.?..????(......
90: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 26 ...............&
a0: 01 30 0d 64 01 00 01 00 88 19 00 00 00 00 00 01 ?0?d?.?.??.....?
b0: 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 ............?...
c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
d0: 00 ff ff 00 00 ff ff ff ff ff 00 00 ff ff ff ff ................
e0: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................
f0: ff ff ff ff ff ff ff ff ff ff ff ff 01 ff ff ff ............?...
//0x38设备地址,只读取0x50-0x7f寄存器范围的值
root@Linux:/proc# i2cdump -f -y -r 0x50-0x7f 1 0x38
No size specified (using byte-data access)
0 1 2 3 4 5 6 7 8 9 a b c d e f 0123456789abcdef
50: ff ff ff 18 18 00 00 00 00 00 00 00 00 ff ff ff ...??...........
60: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................
70: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................
通过i2cdump读取寄存器的值有什么用呢,比如在某款TP驱动里,可以看看驱动里面对应寄存器的值是什么含义
#define MAX_ID 0x0F
#define TOUCH_X_H_POS 3
#define TOUCH_X_L_POS 4
#define TOUCH_Y_H_POS 5
#define TOUCH_Y_L_POS 6
#define TOUCH_PRE_POS 7
#define TOUCH_AREA_POS 8
#define TOUCH_POINT_NUM 2
#define TOUCH_EVENT_POS 3
#define TOUCH_ID_POS 5
#define COORDS_ARR_SIZE 4
#define TOUCH_DOWN 0
#define TOUCH_UP 1
#define TOUCH_CONTACT 2
event->point_num = buf[TOUCH_POINT_NUM] & 0x0F;
event->au8_touch_event[i] =buf[TOUCH_EVENT_POS + ONE_TCH_LEN * i] >> 6;
可以从上面读取的值,看到FTS_TOUCH_POINT_NUM 0x02的值是00,表示0个手指,FTS_TOUCH_EVENT_POS 0x03的值是40,驱动里面可以看到40>>6之后是1,也就是FTS_TOUCH_UP,然后还可以读取x,y坐标的值
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 (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
-V:输出当前版本号
-f:强制使用此设备地址,即使此设备地址已经被使用;若不添加此参数,地址可能写失败
-y:指令执行自动 yes,否则会提示确认执行Continue? [Y/n] Y,不加参数y会有很多执行提示,可以帮助判断
-m:添加掩码
-r:回显,显示是否写入成功,要写的值和读取的值
-a:允许使用0x00-0x02和0x78-0x7f之间的地址
I2CBUS:i2c总线编号
CHIP-ADDRESS:设备地址
DATA-ADDRESS:要写入的寄存器地址
VALUE:要写入的值
MODE:数据长度类型
//0x38设备地址,0x04要写入的寄存器,0x03要写入的值,从结果来看没有写入成功
root@Linux:/# i2cset -f -m 0xff -r 1 0x38 0x04 0x03
WARNING! This program can confuse your I2C bus, cause data loss and worse!
I will write to device file /dev/i2c-1, chip address 0x38, data address
0x04, data 0x03 (masked), mode byte.
Continue? [Y/n]
Old value 0x45, write mask 0xff: Will write 0x03 to register 0x04
Continue? [Y/n]
Warning - data mismatch - wrote 0x03, read back 0x45
i2cget读取设备上寄存器的值,可接受的参数有
Usage: i2cget [-f] [-y] [-a] I2CBUS CHIP-ADDRESS [DATA-ADDRESS [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:
b (read byte data, default)
w (read word data)
c (write byte/read byte)
Append p for SMBus PEC
-V:输出当前版本号
-f:强制使用此设备地址,即使此设备地址已经被使用;若不添加此参数,地址可能写失败
-y:指令执行自动 yes,否则会提示确认执行Continue? [Y/n] Y,不加参数y会有很多执行提示,可以帮助判断
-a:允许使用0x00-0x02和0x78-0x7f之间的地址
I2CBUS:i2c总线编号
CHIP-ADDRESS:设备地址
DATA-ADDRESS:要读取的寄存器地址
MODE:数据长度类型
//0x38设备地址,0x04要读取的寄存器
root@Linux:/# i2cget -f -y 1 0x38 0x04
0x45
i2ctransfer通过一次传输发送用户定义的I2C消息,用于创建I2C消息并将其作为一次传输合并发送。对于已读消息,已接收缓冲区的内容被打印到stdout,每条已读消息一行
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-
-V:输出当前版本号
-f:强制使用此设备地址,即使此设备地址已经被使用;若不添加此参数,地址可能写失败
-y:指令执行自动 yes,否则会提示确认执行Continue? [Y/n] Y,不加参数y会有很多执行提示,可以帮助判断
-v:启用详细输出
-a:允许使用0x00-0x02和0x78-0x7f之间的地址
I2CBUS:i2c总线编号
DESC:{r|w}<消息长度>[@设备地址]
如果I2C消息是写操作,则随后是带有要写数据的数据块。它由<消息长度> 个字节组成,这些字节可以用十六进制,八进制等的常用前缀进行标记。为了更轻松地轻松创建较大的数据块,该数据字节可以带有一个后缀
= 保持值恒定直到消息结束(即0 =表示0、0、0,...)
+ 将值增加1直到消息结束(即0+表示0、1、2,...)
- 将值减1直到消息结束(即0xff-表示0xff,0xfe,0xfd等)
p 将值用作8位伪随机序列的种子(即0p表示0x00、0x50、0xb0等)
//在总线0上,从地址0x50的EEPROM读取偏移量0x64的8个字节
//(第一条消息写入一个字节以将存储器指针设置为0x64,第二条消息从同一芯片读取)
#i2ctransfer 0 w1@0x50 0x64 r8
//对于相同的EEPROM,在偏移量0x42处写入0xff 0xfe ... 0xf0
//(一条写入消息;第一个字节将存储器指针设置为0x42,0xff是第一个数据字节,所有随后的数据字节均减一)
#i2ctransfer 0 w17@0x50 0x42 0xff-