一、無需編寫驅動程序即可訪問 I2C 設備
????????APP 訪問硬件肯定是需要驅動程序的,對于 I2C 設備,內核提供了驅動程序 drivers/i2c/i2c-dev.c,通過它可以直接使用下面的 I2C 控制器驅動程序來訪問 I2C 設備。
i2c-tools 是一套好用的工具,也是一套示例代碼。
1.體驗 I2C-Tools
????????使用一句話概括 I2C 傳輸:APP 通過 I2C Controller 與 I2C Device 傳 輸數據。 所以使用 I2C-Tools 時也需要指定:
?????????哪個 I2C 控制器(或稱為 I2C BUS、I2C Adapter)
???????? 哪個 I2C 設備(設備地址)
???????? 數據:讀還是寫、數據本身
(1)交叉編譯
????????配置環境:vim ~/.bashrc
export ARCH=arm
export CROSS_COMPILE= arm-buildroot-linux-gnueabihf-gccexport PATH=$PATH:/home/book/100ask_imx6ull-sdk/ToolChain/arm-buildroot-linux-gnueabihf_sdk-buildroot/bin
????????修改 I2C-Tools 的 Makefile 指定交叉編譯工具鏈
CC ?= gcc
AR ?= ar
STRIP ?= strip
????????改為(指定交叉編譯工具鏈前綴, 去掉問號):
CC = $(CROSS_COMPILE)gcc
AR = $(CROSS_COMPILE)ar
STRIP = $(CROSS_COMPILE)strip
????????在 Makefile 中,“?=”在第一次設置變量時才會起效果,如果之前設置過 該變量,則不會起效果。
(2)執行 make
????????執行 make 時,是動態鏈接,需要把 libi2c.so 也放到單板上
????????想靜態鏈接的話,執行:make USE_STATIC_LIB=1
(3)用法
???????? i2cdetect:I2C 檢測
// 列出當前的 I2C Adapter(或稱為 I2C Bus、I2C Controller)
i2cdetect -l
// 打印某個 I2C Adapter 的 Functionalities, I2CBUS 為 0、1、2 等整數
i2cdetect -F I2CBUS
// 看看有哪些 I2C 設備, I2CBUS 為 0、1、2 等整數
i2cdetect -y -a I2CBUS
// 效果如下
# i2cdetect -l
i2c-1 i2c STM32F7 I2C(0x40013000) I2C adapter
i2c-2 i2c STM32F7 I2C(0x5c002000) I2C adapter
i2c-0 i2c STM32F7 I2C(0x40012000) I2C adapter
# i2cdetect -F 0
Functionalities implemented by /dev/i2c-0:
I2C yes
SMBus Quick Command yes
SMBus Send Byte yes
SMBus Receive Byte yes
SMBus Write Byte yes
SMBus Read Byte yes
SMBus Write Word yes
SMBus Read Word yes
SMBus Process Call yes
SMBus Block Write yes
SMBus Block Read yes
SMBus Block Process Call yes
SMBus PEC yes
I2C Block Write yes
I2C Block Read yes
// --表示沒有該地址對應的設備, UU 表示有該設備并且它已經有驅動程序,
// 數值表示有該設備但是沒有對應的設備驅動
# i2cdetect -y -a 0 0 1 2 3 4 5 6 7 8 9 a b c d e f
00: 00 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- UU -- -- -- 1e --
20: -- -- UU -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
(3)i2cget:I2C 讀
? 使用說明:
# i2cget
Usage: i2cget [-f] [-y] [-a] I2CBUS CHIP-ADDRESS [DATA-ADDRESS [MODE]]I2CBUS is an integer or an I2C bus nameADDRESS 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
? 使用示例:
// 讀一個字節: I2CBUS 為 0、1、2 等整數, 表示 I2C Bus; CHIP-ADDRESS 表示設備地址
i2cget -f -y I2CBUS CHIP-ADDRESS
// 讀某個地址上的一個字節:
// I2CBUS 為 0、1、2 等整數, 表示 I2C Bus
// CHIP-ADDRESS 表示設備地址
// DATA-ADDRESS: 芯片上寄存器地址
// MODE:有 2 個取值, b-使用`SMBus Read Byte`先發出 DATA-ADDRESS, 再讀一個字節, 中間無
P 信號
// c-先 write byte, 在 read byte,中間有 P 信號
i2cget -f -y I2CBUS CHIP-ADDRESS DATA-ADDRESS MODE
// 讀某個地址上的 2 個字節:
// I2CBUS 為 0、1、2 等整數, 表示 I2C Bus
// CHIP-ADDRESS 表示設備地址
// DATA-ADDRESS: 芯片上寄存器地址
// MODE:w-表示先發出 DATA-ADDRESS,再讀 2 個字節
i2cget -f -y I2CBUS CHIP-ADDRESS DATA-ADDRESS MODE
(4)i2cset:I2C 寫
? 使用說明:
# i2cset
Usage: i2cset [-f] [-y] [-m MASK] [-r] [-a] I2CBUS CHIP-ADDRESS DATA-ADDRESS [VALUE]
... [MODE]I2CBUS is an integer or an I2C bus nameADDRESS 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
? 使用示例:
// 寫一個字節: I2CBUS 為 0、1、2 等整數, 表示 I2C Bus; CHIP-ADDRESS 表示設備地址// DATA-ADDRESS 就是要寫的數據i2cset -f -y I2CBUS CHIP-ADDRESS DATA-ADDRESS// 給 address 寫 1 個字節(address, value):// I2CBUS 為 0、1、2 等整數, 表示 I2C Bus; CHIP-ADDRESS 表示設備地址// DATA-ADDRESS: 8 位芯片寄存器地址; // VALUE: 8 位數值// MODE: 可以省略,也可以寫為 bi2cset -f -y I2CBUS CHIP-ADDRESS DATA-ADDRESS VALUE [b]// 給 address 寫 2 個字節(address, value):// I2CBUS 為 0、1、2 等整數, 表示 I2C Bus; CHIP-ADDRESS 表示設備地址// DATA-ADDRESS: 8 位芯片寄存器地址; // VALUE: 16 位數值// MODE: wi2cset -f -y I2CBUS CHIP-ADDRESS DATA-ADDRESS VALUE w// SMBus Block Write:給 address 寫 N 個字節的數據// 發送的數據有:address, N, value1, value2, ..., valueN// 跟`I2C Block Write`相比, 需要發送長度 N// I2CBUS 為 0、1、2 等整數, 表示 I2C Bus; CHIP-ADDRESS 表示設備地址// DATA-ADDRESS: 8 位芯片寄存器地址; // VALUE1~N: N 個 8 位數值// MODE: si2cset -f -y I2CBUS CHIP-ADDRESS DATA-ADDRESS VALUE1 ... VALUEN s// I2C Block Write:給 address 寫 N 個字節的數據// 發送的數據有:address, value1, value2, ..., valueN// 跟`SMBus Block Write`相比, 不需要發送長度 N// I2CBUS 為 0、1、2 等整數, 表示 I2C Bus; CHIP-ADDRESS 表示設備地址// DATA-ADDRESS: 8 位芯片寄存器地址; // VALUE1~N: N 個 8 位數值// MODE: ii2cset -f -y I2CBUS CHIP-ADDRESS DATA-ADDRESS VALUE1 ... VALUEN i
(5)i2ctransfer:I2C 傳輸(不是基于 SMBus)
? 使用說明:
# i2ctransfer
Usage: i2ctransfer [-f] [-y] [-v] [-V] [-a] I2CBUS DESC [DATA] [DESC [DATA]]...I2CBUS is an integer or an I2C bus nameDESC 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 omit
ted)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)
Example (bus 0, read 8 byte at offset 0x64 from EEPROM at 0x50):# i2ctransfer 0 w1@0x50 0x64 r8
Example (same EEPROM, at offset 0x42 write 0xff 0xfe ... 0xf0):# i2ctransfer 0 w17@0x50 0x42 0xff
? 使用舉例:
// Example (bus 0, read 8 byte at offset 0x64 from EEPROM at 0x50):
# i2ctransfer -f -y 0 w1@0x50 0x64 r8
// Example (bus 0, write 3 byte at offset 0x64 from EEPROM at 0x50):
# i2ctransfer -f -y 0 w9@0x50 0x64 val1 val2 val3
// Example
// first: (bus 0, write 3 byte at offset 0x64 from EEPROM at 0x50)
// and then: (bus 0, read 3 byte at offset 0x64 from EEPROM at 0x50)
# i2ctransfer -f -y 0 w9@0x50 0x64 val1 val2 val3 r3@0x50
# i2ctransfer -f -y 0 w9@0x50 0x64 val1 val2 val3 r3 //如果設備地址不變,后面的設備地址可
省略
2.使用 I2C-Tools 操作傳感器 AP3216C
開發板上有光感芯片 AP3216C:
AP3216C 是紅外、光強、距離三合一的傳感器,以讀出光強、距離值為例,步驟如下:
???????? 復位:往寄存器 0 寫入 0x4
? ? ? ? ?使能:往寄存器 0 寫入 0x3
? ? ? ? ?讀光強:讀寄存器 0xC、0xD 得到 2 字節的光強
? ? ? ? ?讀距離:讀寄存器 0xE、0xF 得到 2 字節的距離值 AP3216C 的設備地址是 0x1E,假設節在 I2C BUS0 上,操作命令如下:?
(1)使用 SMBus 協議?
i2cset -f -y 0 0x1e 0 0x4
i2cset -f -y 0 0x1e 0 0x3
i2cget -f -y 0 0x1e 0xc w
i2cget -f -y 0 0x1e 0xe w
(2)使用 I2C 協議
i2ctransfer -f -y 0 w2@0x1e 0 0x4
i2ctransfer -f -y 0 w2@0x1e 0 0x3
i2ctransfer -f -y 0 w1@0x1e 0xc r2
i2ctransfer -f -y 0 w1@0x1e 0xe r2
(3) I2C-Tools 訪問 I2C 設備的 2 種方式
????????I2C-Tools 可以通過 SMBus 來訪問 I2C 設備,也可以使用一般的 I2C 協議 來訪問 I2C 設備。
????????使用一句話概括 I2C 傳輸:APP 通過 I2C Controller 與 I2C Device 傳輸數據。
????????在 APP 里,有這幾個問題:
怎么指定 I2C 控制器?
???????? i2c-dev.c 為每個 I2C 控制器(I2C Bus、I2C Adapter)都生成一個設備節 點:/dev/i2c-0、/dev/i2c-1 等等;
???????? open 某個/dev/i2c-X 節點,就是去訪問該 I2C 控制器下的設備;
怎么指定 I2C 設備?
????????通過 ioctl 指定 I2C 設備的地址
????????ioctl(file, I2C_SLAVE, address)
???????????????? 如果該設備已經有了對應的設備驅動程序,則返回失敗。
????????ioctl(file, I2C_SLAVE_FORCE, address)
???????????????? 如果該設備已經有了對應的設備驅動程序但是還是想通過 i2c-dev 驅 動來訪問它,則使用這個 ioctl 來指定 I2C 設備地址。
怎么傳輸數據?
????????兩種方式:
???????????????? 一般的 I2C 方式:ioctl(file, I2C_RDWR, &rdwr)
???????????????? SMBus 方式:ioctl(file, I2C_SMBUS, &args)
3.源碼流程分析?
(1)使用 I2C 方式
示例代碼:i2ctransfer.c
(2)使用 SMBus 方式
示例代碼:i2cget.c、i2cset.c
?