linux i2c tools

最近要操做eeprom,因此瞭解一下i2c-tool的使用方法,記錄於此。html

參考連接:linux

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

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

一、源碼下載

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

二、解壓,交叉編譯

tar xjf i2c-tools-3.0.3.tar.bz2code

cd i2c-tools-3.0.3-5/htm

make CC=arm-linux-gnueabihf-gccblog

三、工具使用

將tools目錄下的i2cdetect、i2cdump、i2cget、i2cset拷貝到開發板。ci

四、操做

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: -- -- -- -- -- -- -- --

三、讀寫操做
對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

相關文章
相關標籤/搜索