NotePublic/Software/Applications/I2C/Linux_下_I2C_工具.md

94 lines
3.1 KiB
Markdown

# Linux 下 I2C 工具
i2c-tools 工具是开源 I2C 调试工具, 具有获取 I2C 总线挂载的设备列表及设备地址,可对指定设备指定寄存器进行读写的功能。
[源码下载地址](https://mirrors.edge.kernel.org/pub/software/utils/i2c-tools/)
## 1.I2C 工具的安装
```bas
# Ubuntu
sudo apt install libi2c-dev i2c-tools
```
## 2.基本使用方法
```bash
# 检测 I2C 设备
i2cdetect -y -r <bus num>
# 设置 I2C 设备寄存器值
i2cset -f -y <bus num> <dev addr> <reg addr> <value>...[c/b/w/i/s]
# 读取 I2C 设备寄存器的值
i2cget -f -y <bus num> <dev addr> [reg addr[b/w/c]]
# Dump I2C 设备寄存器
i2cdump -f -y <bus num> <dev addr> [b/w/W/s/i/c]
# 连续读写
i2ctransfer -f -y <bus num> {r|w}<length_of_message>[@address]
# 示例:连续写
i2ctransfer -f -y 3 w6@0x1a 0x80 0x01 0x00 0x00 0x00 0x82
```
## 3.i2cdetect
**Synopsis:**
```bash
i2cdetect [-y] [-a] [-q|-r] i2cbus [first last]
i2cdetect -F i2cbus
i2cdetect -V
i2cdetect -l
```
**Description:**
i2cdetect is a userspace program to scan an I2C bus for devices. It outputs a table with the list of detected devices on the specified bus. i2cbus indicates the number or name of the I2C bus to be scanned, and should correspond to one of the busses listed by i2cdetect -l. The optional parameters first and last restrict the scanning range (default: from 0x03 to 0x77).
i2cdetect can also be used to query the functionalities of an I2C bus (see option **-F**.)
Warning
This program can confuse your I2C bus, cause data loss and worse!
Interpreting the Output
Each cell in the output table will contain one of the following symbols:
* "--". The address was probed but no chip answered.
* "UU". Probing was skipped, because this address is currently in use by a driver. This strongly suggests that there is a chip at this address.
* An address number in hexadecimal, e.g. "2d" or "4e". A chip was found at this address.
**Options:**
* **-y**
Disable interactive mode. By default, i2cdetect will wait for a confirmation from the user before messing with the I2C bus. When this flag is used, it will perform the operation directly. This is mainly meant to be used in scripts.
* **-a**
Force scanning of non-regular addresses. Not recommended.
* **-q**
Use SMBus "quick write" commands for probing (by default, the command used is the one believed to be the safest for each address). Not recommended. This is known to corrupt the Atmel AT24RF08 EEPROM found on many IBM Thinkpad laptops.
* **-r**
Use SMBus "read byte" commands for probing (by default, the command used is the one believed to be the safest for each address). Not recommended. This is known to lock SMBus on various write-only chips (most notably clock chips at address 0x69).
* **-F**
Display the list of functionalities implemented by the adapter and exit.
* **-V**
Display the version and exit.
* **-l**
Output a list of installed busses.
**See Also:**
* [i2cdump(8)](https://linux.die.net/man/8/i2cdump)
* [sensors-detect(8)](https://linux.die.net/man/8/sensors-detect)
**Referenced By:**
* [i2c-stub-from-dump(8)](https://linux.die.net/man/8/i2c-stub-from-dump)