i2c-tools工具的使用

Android-i2ctools 下载:

https://github.com/skyxiaoyan1/android-i2ctool

这是某位大佬根据 i2c-tools 4.1 移植的,关于 Android 下的编译博主上次写过 Android 系统编译技巧,大概流程如下:

1、源码包放在 /external/i2c-tools/...
2、在Android源码根目录输入
    source build/envsetup.sh
    lunch rk3399_mid-userdebug(自己的工程名字)
3、在 /external/i2c-tools/ 目录下输入 mm 模块编译
4、在 Android 根目录输入 make snod 重新打包 system.img
5、烧写 system.img
这样就不需要使用的时候手动 adb push 工具进去

i2cdetect:用于扫描 i2c 总线上的设备,并显示地址
i2cset:向i2c设备某个寄存器写入值
i2cget:读取i2c设备某个寄存器的值
i2cdump:读取某个i2c设备所有寄存器的值
i2ctransfer:一次性读写多个字节

使用示例

i2cdetect:用于扫描 i2c 总线上的设备

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.
  
  y:关闭交互式,不会显示警告信息
  a:扫描总线上所有设备
  q:使用SMBus的"quick write"命令进行检测,不建议使用
  r:使用SMBus的"receive byte"命令进行检测,不建议使用
  i2cbus:指定查询某个总线编号
  first、last:扫描的地址范围

./i2cdetect -r -y 0

-- 表示该地址被检测,但是没有芯片应答,UU 表示该地址被当前内核驱动使用。

如上图显示,在i2c0总线上,有三个器件,设备地址分别是:0x1b、0x40、0x41

./i2cdetect -F i2cbus 查询总线的功能

./i2cdetect -V 打印软件版本号

./i2cdetect -l 检测当前系统有几组i2c总线

i2cset:向i2c设备某个寄存器写入值

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
    
    f:强制访问
    y:指令执行自动yes,否则会提示确认执行Continue? [Y/n] Y,不加参数y会有很多执行提示,可以帮助判断
    r:写入后立即回读寄存器的值,并将结果与写入的值进行比较
    i2cbus:总线编号
    chip-address:i2c设备地址
    data-address:i2c寄存器地址
    value 要写入的值
    mode:指定读取的大小,b字节,w字,s是SMBus块,i是i2c块

设置i2c-1上0x20器件的0x77寄存器值为0x3f
./i2cset -f -y 1 0x20 0x77 0x3f

i2cget:读取i2c设备某个寄存器的值

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
    
    f:强制访问
    y:关闭交互模式,不会提示警告信息
    i2cbus:总线编号
    chip-address:i2c设备地址
    data-address:i2c寄存器地址
    mode:指定读取的大小,b字节,w字,s是SMBus块,i是i2c块

读取i2c-1上0x20器件的0x77寄存器值
./i2cget -f -y 1 0x20 0x77 

i2cdump:读取某个i2c设备所有寄存器的值

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
    
    r:指定寄存器范围,只能扫描从 first 到 last 区域
    f:强制访问设备
    y:关闭人机交互模式
    i2cbus:总线编号
    address:指定设备地址
    mode:指定读取的大小,b字节,w字,s是SMBus块,i是i2c块

读取i2c1上0x38器件的所有寄存器
./i2cdump -f -y -a 1 0x38

一般寄存器都是8位地址的,i2cdump、i2cget、i2cset 也是设置读取8位的地址,如果一次超过8位,需要用 i2ctransfer。

i2ctransfer:一次性读写多个字节。

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)

查看系统上存在的 i2c 总线及其设备地址

ls /sys/bus/i2c/devices/

可以在这里查看实际的设备,然后对比 i2c-tools 扫描出来的对不对。

备注:

i2c-tools 是通过操作 /dev 路径 i2c-× 设备文件完成,因此你的 kernel 必须开启 CONFIG_I2C_CHARDEV 宏控,否者会报找不到节点。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值