I.MX6 Android i2c-tools porting

时间:2021-08-01 09:23:22
/**************************************************************************
 *                   I.MX6 Android i2c-tools porting
 * 说明:
 *     判断I2C总线设备是否存在,每次都去查dmesg,实在是烦了,于是还是移植
 * 一个i2c-tools比较好,不用翻那长长内核信息。
 *  
 *                                        2016-3-31 深圳 南山平山村 曾剑锋
 *************************************************************************/


一、参考资料:
    1. Android 平台下使用 i2c-tools
        http://my.oschina.net/luoly/blog/368881
    2. 树莓派学习笔记——I2C Tools 学习笔记
        http://blog.csdn.net/xukai871105/article/details/15029843
    3. Linux I2C工具查看配置I2C设备 
        http://blog.chinaunix.net/uid-26895763-id-3478882.html

二、download:
    groeck/i2c-tools:
        https://github.com/groeck/i2c-tools

三、Decompress:
    zengjf@zengjf:~$ unzip i2c-tools-master.zip
    zengjf@zengjf:~$ mv i2c-tools-master ~/myandroid/external/i2c-tools/
    
四、cat external/i2c-tools/Android.mk
    # external/i2c-tools/Android.mk
    
    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/util.c tools/i2cbusses.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)

五、compile error:
    1. 现象:
        ......
        PRODUCT_COPY_FILES frameworks/base/data/sounds/ringtones/ogg/Solarium.ogg:system/media/audio/ringtones/Solarium.ogg ignored.
        No private recovery resources for TARGET_DEVICE sabresd_6dq
        make: Entering directory `/home/zengjf/myandroid'
        target thumb C: i2c-tools <= /home/zengjf/myandroid/external/i2c-tools//lib/smbus.c
        target StaticLib: i2c-tools (out/target/product/sabresd_6dq/obj/STATIC_LIBRARIES/i2c-tools_intermediates/i2c-tools.a)
        target thumb C: i2cdetect <= /home/zengjf/myandroid/external/i2c-tools/lib/smbus.c
        target thumb C: i2cdetect <= /home/zengjf/myandroid/external/i2c-tools/tools/i2cdetect.c
        /home/zengjf/myandroid/external/i2c-tools/tools/i2cdetect.c:157:13: error: 'I2C_FUNC_SMBUS_PEC' undeclared here (not in a function)
        make: *** [out/target/product/sabresd_6dq/obj/EXECUTABLES/i2cdetect_intermediates/tools/i2cdetect.o] Error 1
        make: Leaving directory `/home/zengjf/myandroid'
    2. 解决办法:
        cat external/i2c-tools/include/i2c/smbus.h
            .....
            /* Compatibility defines  添加宏声明*/
            #ifndef I2C_SMBUS_I2C_BLOCK_BROKEN
            #define I2C_SMBUS_I2C_BLOCK_BROKEN I2C_SMBUS_I2C_BLOCK_DATA
            #endif
            #ifndef I2C_FUNC_SMBUS_PEC
            #define I2C_FUNC_SMBUS_PEC I2C_FUNC_SMBUS_HWPEC_CALC
            #endif
            
            extern __s32 i2c_smbus_access(int file, char read_write, __u8 command,
                              int size, union i2c_smbus_data *data);
            
            extern __s32 i2c_smbus_write_quick(int file, __u8 value);
            ......
    3. 正常现象:
        No private recovery resources for TARGET_DEVICE sabresd_6dq
        make: Entering directory `/home/zengjf/myandroid'
        target thumb C: i2cdetect <= /home/zengjf/myandroid/external/i2c-tools/tools/i2cdetect.c
        target Executable: i2cdetect (out/target/product/sabresd_6dq/obj/EXECUTABLES/i2cdetect_intermediates/LINKED/i2cdetect)
        target Symbolic: i2cdetect (out/target/product/sabresd_6dq/symbols/system/bin/i2cdetect)
        Export includes file: /home/zengjf/myandroid/external/i2c-tools/Android.mk -- out/target/product/sabresd_6dq/obj/EXECUTABLES/i2cdetect_intermediates/export_includes
        target Strip: i2cdetect (out/target/product/sabresd_6dq/obj/EXECUTABLES/i2cdetect_intermediates/i2cdetect)
        Install: out/target/product/sabresd_6dq/system/bin/i2cdetect
        Import includes file: out/target/product/sabresd_6dq/obj/EXECUTABLES/i2cget_intermediates/import_includes
        target thumb C: i2cget <= /home/zengjf/myandroid/external/i2c-tools/tools/i2cget.c
        target Executable: i2cget (out/target/product/sabresd_6dq/obj/EXECUTABLES/i2cget_intermediates/LINKED/i2cget)
        target Symbolic: i2cget (out/target/product/sabresd_6dq/symbols/system/bin/i2cget)
        Export includes file: /home/zengjf/myandroid/external/i2c-tools/Android.mk -- out/target/product/sabresd_6dq/obj/EXECUTABLES/i2cget_intermediates/export_includes
        target Strip: i2cget (out/target/product/sabresd_6dq/obj/EXECUTABLES/i2cget_intermediates/i2cget)
        Install: out/target/product/sabresd_6dq/system/bin/i2cget
        Import includes file: out/target/product/sabresd_6dq/obj/EXECUTABLES/i2cset_intermediates/import_includes
        target thumb C: i2cset <= /home/zengjf/myandroid/external/i2c-tools/tools/i2cset.c
        target Executable: i2cset (out/target/product/sabresd_6dq/obj/EXECUTABLES/i2cset_intermediates/LINKED/i2cset)
        target Symbolic: i2cset (out/target/product/sabresd_6dq/symbols/system/bin/i2cset)
        Export includes file: /home/zengjf/myandroid/external/i2c-tools/Android.mk -- out/target/product/sabresd_6dq/obj/EXECUTABLES/i2cset_intermediates/export_includes
        target Strip: i2cset (out/target/product/sabresd_6dq/obj/EXECUTABLES/i2cset_intermediates/i2cset)
        Install: out/target/product/sabresd_6dq/system/bin/i2cset
        Import includes file: out/target/product/sabresd_6dq/obj/EXECUTABLES/i2cdump_intermediates/import_includes
        target thumb C: i2cdump <= /home/zengjf/myandroid/external/i2c-tools/tools/i2cdump.c
        target Executable: i2cdump (out/target/product/sabresd_6dq/obj/EXECUTABLES/i2cdump_intermediates/LINKED/i2cdump)
        target Symbolic: i2cdump (out/target/product/sabresd_6dq/symbols/system/bin/i2cdump)
        Export includes file: /home/zengjf/myandroid/external/i2c-tools/Android.mk -- out/target/product/sabresd_6dq/obj/EXECUTABLES/i2cdump_intermediates/export_includes
        target Strip: i2cdump (out/target/product/sabresd_6dq/obj/EXECUTABLES/i2cdump_intermediates/i2cdump)
        Install: out/target/product/sabresd_6dq/system/bin/i2cdump
        make: Leaving directory `/home/zengjf/myandroid'
        zengjf@zengjf:~/myandroid$ 
    4. adb push <编译出来的可执行程序> /data/local

六、i2cdetect test:
    1. test1:
        1|root@android:/data/local # ./i2cdetect -l                                    
        i2c-3   i2c             i2c-gpio3        I2C adapter
        i2c-0   i2c             imx-i2c          I2C adapter
        i2c-1   i2c             imx-i2c          I2C adapter
        i2c-2   i2c             imx-i2c          I2C adapter
        root@android:/data/local # 
    2. test2:
        root@android:/data/local # ./i2cdetect -y -r 2                                 
 1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
        00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
        10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
        20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
        30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
        40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
        50: UU -- -- -- -- UU -- -- -- -- -- -- -- -- -- -- 
        60: -- -- -- -- -- -- -- -- UU -- -- -- -- -- -- -- 
        70: -- -- -- -- -- -- -- --

七、i2cget test:
    1|root@android:/data/local # ./i2cget -y 1 0x08 0x03                           
    Error: Could not set address to 0x08: Device or resource busy
    1|root@android:/data/local # ./i2cget -f -y 1 0x08 0x03                        
    0x21
    1|root@android:/data/local #