[Android] i2c-toos 在 Android 上使用
CPU:RK3399
系统:Android 7.1
i2c-tools 是一款免费开源的工具,可以检测 i2c 总线上的设备,可以读写寄存器等等
可以从下面路径下载需要的版本:
https://www.kernel.org/pub/software/utils/i2c-tools/
如果系统安装了 git,也可以使用 git 下载:
git clone git://git.kernel.org/pub/scm/utils/i2c‐tools/i2c‐tools.git
在 android 平台使用 i2c-tools,就需要进行交叉编译或者直接编译成可执行程序
1、修改 Makefile 进行交叉编译
暂时没有使用这种方式
2、编译成可执行程序
(1) 将 i2c-tools 文件夹拷贝到安卓任意目录
(2) 在 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)
(3) 使用 mmm 指令进行模块化编译,编译后会在 tools 目录下生成 5 个可执行程序
i2cdetect i2cdump i2cget i2cset i2ctransfer
(4) 通过 adb 将生成的文件 push 到 /system/bin 目录下即可
指令使用方法:
i2cdetect:
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:输出当前版本号
# i2cdetect -V
-y:指令执行自动 yes,否则会提示确认执行Continue? [Y/n] Y
-a:输出总线上所有地址(00-7f),没有 -a,只显示 08-77,UU 表示该设备在驱动中已使用
// 输出 i2c0 总线上所有设备 # i2cdetect -y -a 0
# i2cdetect -y 0 // 输出 i2c0 总线上地址从 0x10-0x55 之间的设备 # i2cdetect -y 0 0x10 0x55
-q:快速写指令,-q 和 -r 不能同时使用
-r:读指令,-q 和 -r 不能同时使用
I2CBUS:i2c 总线编号
-F:此 i2c 支持的功能
// i2c0 支持的功能 # i2cdetect -F 0
-l:输出所有 i2c 总线
# i2cdetect -l
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 (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) i (I2C block) c (consecutive byte) Append p for SMBus PEC
-V:输出当前版本号
# i2cdump -V
I2CBUS:i2c 总线编号
ADDRESS:设备地址,建议使用十六进制
-f:强制使用此设备地址,即使此设备地址已经被使用;若不添加此参数,地址可能写失败
-y:指令执行自动 yes,否则会提示确认执行Continue? [Y/n] Y
-r:读取从 first-last 之间的寄存器值
// 读取 i2c1 上设备地址为 0x10,从 0x50-0x70 之间的寄存器值 # i2cdump -f -y -r 0x50-0x7f 1 0x10
-a:暂时不知道什么含义
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 (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) Append p for SMBus PEC
-V:输出当前版本号
# i2cget -V
-f:强制使用此设备地址,即使此设备地址已经被使用;若不添加此参数,地址可能写失败
-y:指令执行自动 yes,否则会提示确认执行Continue? [Y/n] Y
// 读取i2c1总线上设备地址为0x10,寄存器0x03的值 # i2cget -f -y 1 0x10 0x03
-b:读取长度是一个字节
-w:读取长度是一个字
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
-V:输出当前版本号
# i2cget -V
-f:强制使用此设备地址,即使此设备地址已经被使用;若不添加此参数,地址可能写失败
-y:指令执行自动 yes,否则会提示确认执行Continue? [Y/n] Y
// 设置i2c1总线上设备地址为0x10,寄存器0x03的值为0x20 # i2cget -f -y 1 0x10 0x03 0x20