linux i2c tools

最近要操作eeprom,所以了解一下i2c-tool的使用方法,记录于此。

参考链接:

  http://www.myir-tech.com/bbs/thread-7567-1-1.html

  http://minix-i2c.blogspot.com/2013/07/using-i2c-tools-with-angstrom-linux-on.html

1、源码下载

  https://launchpad.net/ubuntu/+source/i2c-tools/3.0.3-5

2、解压,交叉编译

tar xjf i2c-tools-3.0.3.tar.bz2

cd i2c-tools-3.0.3-5/

make CC=arm-linux-gnueabihf-gcc

3、工具使用

将tools目录下的i2cdetect、i2cdump、i2cget、i2cset拷贝到开发板。

4、操作

1. 查看i2c总线
root@freescale ~$ 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

2. 查看i2c总线上设备的地址
读取的结果显示地址为0x1a的设备忙,'UU'表示连接的设备忙.
root@freescale ~$ 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: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

root@freescale ~$ i2cdetect -y -r 3
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: UU -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

3、读写操作
对i2c-3,地址0x50的eeprom(at24c02)进行操作。

root@freescale ~$ i2cdump  -f 3 0x50 c
WARNING! This program can confuse your I2C bus, cause data loss and worse!
I will probe file /dev/i2c-3, address 0x50, mode byte consecutive read
Continue? [Y/n]

由于设备忙,加上-f选项,表示force, -y 表示yes,否者就会出现上面的情况。

3.1 读取i2c-3上,地址为0x50的设备, at24c02共256字节
root@freescale ~$ i2cdump  -f -y 3 0x50 c
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f    0123456789abcdef
00: 01 02 03 39 34 3a 65 66 3a 31 35 3a 33 61 3a 65    ???94:ef:15:3a:e
10: 35 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    5...............
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    ................

3.1 读取i2c-3上,地址为0x50,寄存器地址为0的数据
root@freescale ~$ i2cget -f -y 3 0x50 0
0x01
root@freescale ~$ i2cget -f -y 3 0x50 0 c
0x01
root@freescale ~$ i2cget -f -y 3 0x50 0 b
0x01
root@freescale ~$ i2cget -f -y 3 0x50 0 w
0x0201

3.2 写入
a. 写进去的内容是16进制, 通过读取结果可知。
root@freescale ~$ i2cset -f -y 3 0x50 0 12 b
root@freescale ~$ i2cdump -f -y 3 0x50
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: 0c 02 03 39 34 3a 65 66 3a 31 35 3a 33 61 3a 65    ???94:ef:15:3a:e
10: 35 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    5...............
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    ................

b. 能够识别0x开头的16进制作为参数,但是不能识别字符如 a, 或者'a'.
root@freescale ~$ i2cset -f -y 3 0x50 0 0xa b
root@freescale ~$ i2cdump -f -y 3 0x50
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: 0a 02 03 39 34 3a 65 66 3a 31 35 3a 33 61 3a 65    ???94:ef:15:3a:e
10: 35 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    5...............
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    ................

c. 连续写一整块设备
root@freescale ~$ i2cset -f -y 3 0x50 0 0xa 0xb 0xc 0xd 0xe 0xf i
root@freescale ~$ i2cdump -f -y 3 0x50
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: 0a 0b 0c 0d 0e 0f 65 66 3a 31 35 3a 33 61 3a 65    ??????ef:15:3a:e
10: 35 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    5...............
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    ................

Tony Liu

2016-10-11, Shenzhen

posted @ 2016-10-11 18:01  SuperTao1024  阅读(3664)  评论(0编辑  收藏  举报