> For the complete documentation index, see [llms.txt](https://book.bsdcn.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://book.bsdcn.org/man/man8/i2c.8.md).

# i2c.8

`i2c` — 测试 I2C 总线及从设备

## 名称

`i2c`

## 概要

`i2c -a address [-f device] [-d r|w] [-w 0|8|16|16LE|16BE] [-o offset] [-c count] [-m tr|ss|rs|no] [-b] [-v]` `i2c -h` `i2c -i [-v] [cmd ...] [-]` `i2c -r [-f device] [-v]` `i2c -s [-f device] [-n skip_addr] [-v]`

## 描述

`i2c` 工具可用于对 I2C 总线上的设备执行原始数据传输（读或写）。它还可扫描总线上的可用设备并重置 I2C 控制器。

选项如下：

**`tr`** 完整传输 **`ss`** 停止后再开始 **`rs`** 重复开始 **`no`** 无

**`-a`** `address` 要操作的 I2C 设备的 7 位地址（十六进制）。

**`-b`** 二进制模式——执行读操作时，从设备读取的数据以二进制格式输出到 stdout。

**`-c`** `count` 要传输的字节数（十进制）。

**`-d`** `r|w` 传输方向：r 为读，w 为写。要写入的数据以二进制字节形式从 stdin 读取。

**`-f`** `device` 要使用的 I2C 总线（默认为 /dev/iic0）。

**`-i`** 解释模式

**`-h`** 帮助

**`-m`** `tr|ss|rs|no` 寻址模式，即在传输偏移量写入设备之后、实际读/写操作之前执行的 I2C 总线操作。某些 I2C 总线硬件不提供对单个开始、重复开始和停止操作的控制。此类硬件只能将偏移量和数据的完整传输作为单个操作执行。`tr` 模式创建描述传输的控制结构，并将其作为单个完整事务提交给驱动程序。此模式适用于所有类型的 I2C 硬件。

**`-n`** `skip_addr` 总线扫描时要跳过的地址。一个或多个地址（\[0x]xx）或地址范围（\[0x]xx-\[0x]xx 或 \[0x]xx..\[0x]xx），以逗号或冒号分隔。

**`-o`** `offset` 设备内用于数据传输的偏移量（十六进制）。默认为零。使用“-w 0”可禁止向从设备写入偏移量。

**`-r`** 重置控制器。

**`-s`** 扫描总线上的设备。

**`-v`** 详细输出。

**`-w`** `0|8|16|16LE|16BE` 设备偏移量宽度（以位为单位）。用于确定如何将 `-o` 指定的 `offset` 传递给从设备。零表示忽略偏移量，完全不传递给从设备。字节序默认为小端序。

## 解释模式

使用 `-i` 启动时，其余参数将被解释为命令；如果最后一个参数是 '-'，或没有参数，则命令也会从 stdin 读取。

可用命令：

**'r'** bus address \[0|8|16|16LE|16BE] offset count 读取命令，读取 count 字节并以十六进制转储到 stdout。 **'w'** bus address \[0|8|16|16LE|16BE] offset hexstring 写入命令，将 hexstring（允许空白字符）写入设备。 **'p'** anything 打印命令，整行打印到 stdout。（可用于同步。）

所有数字字段接受标准的十进制/八进制/十六进制表示法。

未指定 `-v` 选项时，所有错误都是致命的，退出状态非零。

指定 `-v` 选项时，没有错误是致命的，所有命令将在 stdout 上返回“OK\n”或“ERROR\n”。出错时，详细诊断信息将先于该结果输出到 stderr。

空行和以 '#' 开头的行将被忽略。

## 实例

* 扫描默认总线（/dev/iic0）上的设备：

```sh
i2c -s
```

* 扫描默认总线（/dev/iic0）上的设备，并跳过地址 0x45 到 0x47（含）以及 0x56：

```sh
i2c -s -n 0x56,45-47
```

* 从地址 0x56 的设备（例如 EEPROM）读取 8 字节数据：

```sh
i2c -a 0x56 -d r -c 8
```

* 将文件 data.bin 中的 16 字节数据写入设备 0x56 的偏移量 0x10 处：

```sh
i2c -a 0x56 -d w -c 16 -o 0x10 -b < data.bin
```

* 在两个 EEPROM 之间复制 4 字节（/dev/iic1 上的 0x56 到 /dev/iic0 上的 0x57）：

```sh
i2c -a 0x56 -f /dev/iic1 -d r -c 0x4 -b | i2c -a 0x57 -f /dev/iic0 -d w -c 4 -b
```

* 重置控制器：

```sh
i2c -f /dev/iic1 -r
```

* 读取 EEPROM 中地址 24 处的 8 字节：

```sh
i2c -i 'r 0 0x50 16BE 24 8'
```

* 读取 EEPROM 中地址 24 和 48 处的 2x8 字节：

```sh
echo 'r 0 0x50 16BE 48 8' | i2c -i 'r 0 0x50 16BE 24 8' -
```

## 警告

许多系统在 I2C 存储器中存储关键的低级信息，并可能包含其他 I2C 设备，如温度或电压传感器。读取这些数据可能干扰固件的运行，向其写入可能使硬件“变砖”。

## 参见

[iic(4)](https://github.com/FreeBSD-Ask/freebsd-man-sc/blob/main/man4/iic.4.md), [iicbus(4)](https://github.com/FreeBSD-Ask/freebsd-man-sc/blob/main/man4/iicbus.4.md), [smbus(4)](https://github.com/FreeBSD-Ask/freebsd-man-sc/blob/main/man4/smbus.4.md)

## 历史

`i2c` 工具出现于 FreeBSD 8.0。

## 作者

`i2c` 工具及本手册页由 Bartlomiej Sieka <tur@semihalf.com> 和 Michal Hajduk <mih@semihalf.com> 编写。

Poul-Henning Kamp <phk@FreeBSD.org> 添加了解释模式。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://book.bsdcn.org/man/man8/i2c.8.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
