在android使用i2c tools访问i2c,很方便,可以在https://launchpad.net/ubuntu/+source/i2c-tools 下载最新的i2c tools.
把i2c-tools-3.1.1 解压到external下,然后在i2c-tools-3.1.1根目录下创建Android.mk文件,内容为:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_C_INCLUDES += $(LOCAL_PATH)/include $(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 := optional
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)/include $(LOCAL_PATH) $(LOCAL_PATH)/$(KERNEL_DIR)/include
include $(BUILD_EXECUTABLE)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
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)/include $(LOCAL_PATH) $(LOCAL_PATH)/$(KERNEL_DIR)/include
include $(BUILD_EXECUTABLE)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
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)/include $(LOCAL_PATH) $(LOCAL_PATH)/$(KERNEL_DIR)/include
include $(BUILD_EXECUTABLE)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
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)/include $(LOCAL_PATH) $(LOCAL_PATH)/$(KERNEL_DIR)/include
include $(BUILD_EXECUTABLE)
然后在该目录下执行mm,即可编译成功.,生成生成i2cdetect,i2cdump,i2cset,i2cget等命令工具.
-----------------------下面是关于这四个命令的用法举例
i2c-tools中含有四個執行檔
- i2cdetect – 用來列舉I2C bus和上面所有的裝置
- i2cdump – 顯示裝置上所有register的值
- i2cget – 讀取裝置上某個register的值
- i2cset – 寫入裝置上某個register
以下是实验結果
/system/bin # i2cdetect -l
i2c-0 i2c s3c2410-i2c I2C adapter
i2c-1 i2c s3c2410-i2c I2C adapter
i2c-2 i2c s3c2410-i2c I2C adapter
/system/bin #
看到有三条I2C总线
/system/bin # i2cdetect -y -r 0
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- UU -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- 34 -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
发现 I2C 设备的位置显示为UU
或者表示设备地址的数值,UU
表示该设备在 driver 中被使用。
/system/bin #
查看总线0上面有哪些设备以及设备地址;
/system/bin # i2cdump -f -y 0 0x34
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: 38 70 00 21 00 00 00 00 00 00 00 00 00 00 00 00 8p.!............
10: 05 00 5f 00 00 00 00 00 00 00 00 00 00 00 00 00 ?._.............
20: 00 00 00 16 00 00 00 68 ff 68 00 00 00 00 00 00 ...?...h.h......
30: 61 02 42 c9 45 22 9d 08 a5 1f 68 5f fc 16 00 00 a?B?E"????h_??..
40: d8 ff 03 01 00 00 00 00 08 08 02 00 60 00 00 00 ?.??....???.`...
50: cf 0e 00 00 0e 1f 00 00 00 00 00 00 00 00 71 04 ??..??........q?
60: a2 05 00 08 00 00 00 00 00 00 00 00 00 00 00 00 ??.?............
70: 0c 27 a1 00 00 00 00 00 d0 02 00 00 0f 17 a2 00 ?'?.....??..???.
80: e0 fd 83 80 32 00 ff 00 00 00 00 00 00 00 00 21 ????2..........!
90: 07 a5 07 07 00 02 00 00 00 00 00 00 00 00 00 00 ????.?..........
a0: 00 00 00 00 00 01 1d 01 cd 06 00 00 0e 12 a2 0b .....?????..????
b0: 00 00 00 00 00 00 00 00 00 14 80 ba d2 0d 00 00 .........?????..
c0: 01 01 02 04 07 0d 10 1a 24 2e 35 3d 49 54 5c 63 ????????$.5=ITc
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 00 00 00 00 00 ................
f0: 41 00 80 03 00 00 00 00 00 00 00 00 00 00 00 00 A.??............
/system/bin # i2cget -f -y 0 0x34 0x28
0xff
/system/bin #
单独读0x28寄存器的值
/system/bin #
/system/bin #
/system/bin # i2cset -f -y 0 0x34 0x28 0xfb
/system/bin #
单独写0x28寄存器的值
/system/bin # i2cget -f -y 0 0x34 0x28
0xfb
/system/bin #
可以看出寄存器值已经由0xff改成为0xfb