Android 系统 i2c-tools 编译与使用

文章目录

  • 前言
  • 一、源码下载
  • 二、移植到Android平台
    • 1.修改Makefile进行交叉编译
    • 2.编译成可执行程序
  • 三、命令使用语法
    • 1. i2cdetect
    • 2. i2cdump
    • 3. i2cget、i2cset、i2ctransfer


前言

i2c-tools 是一款免费开源的工具,是linux系统下一款i2c调试利器,方便对i2c总线和i2c设备调试。


一、源码下载

i2c-tools 源码下载地址
选择要安装的版本:
Android 系统 i2c-tools 编译与使用_第1张图片

二、移植到Android平台

在Android平台使用i2c-tools ,需要进行交叉编译或者直接编译成可执行程序。

1.修改Makefile进行交叉编译

修改Makefile,选择rk3399 的交叉编译器,然后执行make指令:

# CC	?= gcc
# AR	?= ar
# STRIP	?= strip
修改为:
COMPILE_PREFIX = $(sdk源码路径)/prebuilts/gcc/linux-x86/aarch64/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-
CC	= $(COMPILE_PREFIX)gcc
AR	= $(COMPILE_PREFIX)ar
STRIP	= $(COMPILE_PREFIX)strip

编译完成后,在tools目录下生成可执行文件:i2cdetect 、 i2cdump 、 i2cget 、 i2cset i2ctransfer;
在这里插入图片描述
通过adb 将该可执行文件拷贝到rk3399板子任意目录下:

1.在rk3399板子中新建文件夹tools;
2.将生成的可执行文件通过adb push到/data/local/tools/目录下;
3.分配权限:chmod 777 i2cdetect 
4.测试,查看板子总线:./i2cdetect -l 

Android 系统 i2c-tools 编译与使用_第2张图片

2.编译成可执行程序

  • 将i2c-tools 拷贝到安卓工程external/目录下
  • 在i2c-tools 下新增Android.mk 编译文件
  • 在i2c-tools 版本为4.x时,Android.m 文件如下:
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)

  • 在i2c-tools 版本为3.x时,Android.m 文件如下:
LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE_TAGS := eng
LOCAL_C_INCLUDES += $(LOCAL_PATH) $(LOCAL_PATH)/$(KERNEL_DIR)/include
LOCAL_SRC_FILES := tools/i2cbusses.c tools/util.c
LOCAL_MODULE := i2c-tools
include $(BUILD_STATIC_LIBRARY)

include $(CLEAR_VARS)

LOCAL_MODULE_TAGS := eng
LOCAL_SRC_FILES:=tools/i2cdetect.c
LOCAL_MODULE:=i2cdetect
LOCAL_CPPFLAGS += -DANDROID
LOCAL_SHARED_LIBRARIES:=libc
LOCAL_STATIC_LIBRARIES := i2c-tools
LOCAL_C_INCLUDES += $(LOCAL_PATH) $(LOCAL_PATH)/$(KERNEL_DIR)/include
include $(BUILD_EXECUTABLE)

include $(CLEAR_VARS)

LOCAL_MODULE_TAGS := eng
LOCAL_SRC_FILES:=tools/i2cget.c
LOCAL_MODULE:=i2cget
LOCAL_CPPFLAGS += -DANDROID
LOCAL_SHARED_LIBRARIES:=libc
LOCAL_STATIC_LIBRARIES := i2c-tools
LOCAL_C_INCLUDES += $(LOCAL_PATH) $(LOCAL_PATH)/$(KERNEL_DIR)/include
include $(BUILD_EXECUTABLE)

include $(CLEAR_VARS)

LOCAL_MODULE_TAGS := eng
LOCAL_SRC_FILES:=tools/i2cset.c
LOCAL_MODULE:=i2cset
LOCAL_CPPFLAGS += -DANDROID
LOCAL_SHARED_LIBRARIES:=libc
LOCAL_STATIC_LIBRARIES := i2c-tools
LOCAL_C_INCLUDES += $(LOCAL_PATH) $(LOCAL_PATH)/$(KERNEL_DIR)/include
include $(BUILD_EXECUTABLE)

include $(CLEAR_VARS)

LOCAL_MODULE_TAGS := eng
LOCAL_SRC_FILES:=tools/i2cdump.c
LOCAL_MODULE:=i2cdump
LOCAL_CPPFLAGS += -DANDROID
LOCAL_SHARED_LIBRARIES:=libc
LOCAL_STATIC_LIBRARIES := i2c-tools
LOCAL_C_INCLUDES += $(LOCAL_PATH) $(LOCAL_PATH)/$(KERNEL_DIR)/include
include $(BUILD_EXECUTABLE)

  • 在安卓工程目录下使用mmm指令模块化编译,步骤如下:
1. source build/envsetup.sh
2. lunch rk3399_xxx_userdebug
3. mmm external/i2c-tools/
  • 编译完成后,会在out/target/product/rk3399_xxx/system/bin/生成可执行文件:
    在这里插入图片描述
  • 通过adb 将可执行文件 push 到rk3399设备上;

三、命令使用语法

1. i2cdetect

1. 查看板子总线:./i2cdetect -l
2. 查看i2c总线挂载的设备 : ./i2cdetect -r -y i2c-no
如果i2c设备驱动已经被系统加载,地址显示“UU”。
如图:i2c0上挂载了3个设备,地址分别是0x1b0x400x41;

Android 系统 i2c-tools 编译与使用_第3张图片

2. i2cdump

1. 读取i2c设备所有寄存器值
./i2cdump -f -y i2c-no i2c-addr
./i2cdump -f -y 0 0x41	/* 读取i2c0上的0x41所有寄存器值 */

Android 系统 i2c-tools 编译与使用_第4张图片

3. i2cget、i2cset、i2ctransfer

1. 读取i2c设备制定寄存器
./i2cget -f -y i2c-no i2c-addr reg

2. 写i2c设备指定寄存器
./i2ctransfer -f -y i2c-no w-num@i2c-addr data-0 data-1 data-2	

3. 读取i2c设备多个字节数据
./i2ctransfer -f -y i2c-no w-num@i2c-addr data-0 data-1 data-n r-num	

4. 写i2c设备多个字节数据
./i2ctransfer -f -y i2c-no w-num@i2c-addr data-0 data-1 data-2

你可能感兴趣的:(android)