i2c-tools的使用方法

一、下载i2c-tools,交叉编译
1、从开源网站http://dl.lm-sensors.org/i2c-tools/releases/ 下载i2c-tools
2、编译生成对应的可执行文件
i2cdetect, i2cdump, i2cget, i2cset
二、使用举例
1、用i2cdetect检测有几组i2c总线在系统上,输入:

:/ # i2cdetect                                                           
Error: No i2c-bus specified!
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.
:/ # i2cdetect -l                                                        
i2c-1   i2c         aml_i2c_adap1                       I2C adapter
i2c-2   i2c         aml_i2c_adap2                       I2C adapter
i2c-3   i2c         aml_i2c_adap3                       I2C adapter

2、用i2cdetect检测挂载在i2c总线上器件,以总线3为例,输入

:/ # i2cdetect -r -y 3
 -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- UU -- -- 
20: -- -- -- -- -- -- -- -- UU -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- 43 -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --  

可以看到上面的输出是有识别的0x43 这个器件,这个也是我实际环境上的一个器件
3、用i2cdump查看器件所有寄存器的值,以总线3上0x43 这个器件为例,输入

:/ # i2cdump                                                           
Error: No i2c-bus specified!
Usage: i2cdump [-f] [-y] [-r first-last] I2CBUS ADDRESS [MODE [BANK [BANKREG]]]
  I2CBUS is an integer or an I2C bus name
  ADDRESS is an integer (0x03 - 0x77)
  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

:/ # i2cdump -f -y 3 0x43                                                
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: 00 a0 00 00 00 00 00 ff 00 00 00 ff 00 00 00 6d    .?.............m
10: 00 00 00 00 00 00 00 00 83 01 00 00 00 00 00 00    ........??......
20: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
30: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
40: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
50: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
60: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
70: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
90: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
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: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................

4、用i2cget 看单个寄存器地址, 以总线3上0x43 0x01这个寄存器为例

:/ #  i2cget                                                              
Usage: i2cget [-f] [-y] I2CBUS CHIP-ADDRESS [DATA-ADDRESS [MODE]]
  I2CBUS is an integer or an I2C bus name
  ADDRESS is an integer (0x03 - 0x77)
  MODE is one of:
    b (read byte data, default)
    w (read word data)
    c (write byte/read byte)
    Append p for SMBus PEC
:/ # i2cget -f -y 3 0x43 0x01                                          
0xa0

读出来的值跟上面dump是一样的
5、i2cset 操作寄存器值, 以总线3上0x43 0x03这个寄存器为例

:/ # i2cset                                                              
Usage: i2cset [-f] [-y] [-m MASK] [-r] I2CBUS CHIP-ADDRESS DATA-ADDRESS [VALUE] ... [MODE]
  I2CBUS is an integer or an I2C bus name
  ADDRESS is an integer (0x03 - 0x77)
  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
:/ # i2cset -f -y 3 0x43 0x03 0x7f                                     
:/ # 
:/ # i2cget -f -y 3 0x43 0x03                                            
0x7f

设置为0x03 0x7f 后,读取0x03为0x7f 修改成功

三、注意事项
Error: Could not set address to 0x49: Device or resource busy

From the manpage it looks like I can use the f option to force the connection
(source: http://manpages.ubuntu.com/manpages/hardy/man8/i2cget.8.html)
-f
Force access to the device even if it is already busy. By
default, i2cget will refuse to access a device which is already
under the control of a kernel driver. Using this flag is dangerous, it can seriously confuse the kernel driver in question. It can also cause i2cget to return an invalid value. So use at your own risk and only if you know what you’re doing.

因为设置已经在kernel driver的控制下了,所以i2c tool返回 Device or resource busy
所以有两个方法
一个rmmod dirver
一个是 使用i2c tool带上 -f参数

# i2cdump -y -f 3 0x28                                                

你可能感兴趣的:(i2c-tools的使用方法)