1. 下載和安裝
下載地址: https://www.kernel.org/pub/software/utils/i2c-tools/
解壓后, 進入解壓后的目錄, 然后編譯:
cd i2c-tools-4.1
make
安裝:
sudo make install
使用
終端輸入i2c按兩次Tab鍵會出現可選命令:
i2cdetect
- 顯示所有可用的I2C總線
i2cdetect -l
- 使用
-y 總線號或總線名
選項, 查看該總線下所有設備及設備地址:
i2cdetect -y 1 # 查看i2c-1總線下的設備
(0x36是我i2c-1下掛載的攝像頭的地址)
如果報錯:
Error: Could not open file `/dev/XXX`: Permission denied
Run as root?
請使用root權限運行:
sudo i2cdetect -y 1
如果報錯:
error while loading shared libraries: libi2c.so.0: cannot open shared object file:
No such file or directory
將i2c-tools-4.0/lib/libi2c.so.0.1.1
復制到/usr/lib/
下, 重命名為libi2c.so.0
, 並修改權限為777:
sudo cp lib/libi2c.so.0.1.1 /usr/lib/libi2c.so.0
sudo chmod 777 /usr/lib/libi2c.so.0
i2ctransfer
- 寫
sudo i2ctransfer -f -y 1 w3@0x36 0x50 0x81 0x01
0x36為I2C設備的地址, 0x5081為要寫的寄存器地址, 0x01為寫入的值。
- 讀
sudo i2ctransfer -f -y 1 w2@0x36 0x30 0x0A r3
0x36為I2C設備的地址, 0x300A為要讀的寄存器地址, r3為連續讀3Byte, 0x56 0x08 0x41 為讀到的寄存器的值。
其他命令
i2cdump、i2cget、i2cset只適用於讀寫8位的寄存器地址, 功能完全可由i2ctransfer代替, 所以不做介紹。