> 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/spi.8.md).

# spi.8

`spi` — 与 SPI 总线上的从设备通信

## 名称

`spi`

## 概要

`spi [-A] [-b] [-L] [-v] [-C command-bytes] [-c count] [-d r|w|rw] [-f device] [-m mode] [-S] [-s max-speed]` `spi [-i] [-v] [-f device]` `spi [-h]`

## 描述

`spi` 工具可用于通过 [spigen(4)](https://github.com/FreeBSD-Ask/freebsd-man-sc/blob/main/man4/spigen.4.md) 设备与 SPI 总线上的设备执行原始数据传输（读、写或同时读写）。

每个 [spigen(4)](https://github.com/FreeBSD-Ask/freebsd-man-sc/blob/main/man4/spigen.4.md) 设备都与 spibus 上一个特定的“片选”（cs）引脚相关联，因此需要指定。如果未在命令行指定设备名，`spi` 默认使用 “spigen0.0”。

关于 spigen 设备的更多信息，参见 [spigen(4)](https://github.com/FreeBSD-Ask/freebsd-man-sc/blob/main/man4/spigen.4.md)。

选项如下：

**`-A`** 指定 ASCII 模式。读和写的数据均以 2 字符十六进制值的形式输入和输出，可选择以空白字符分隔，例如 00 01 02 等。与 `-b` 标志组合使用时，stdin 上的数据仍为 ASCII 十六进制字节值序列，但输出恢复为二进制模式。

**`-b`** 二进制（输出）模式。仅在指定了 `-A` 时生效。将输出恢复为二进制（而非 ASCII），同时保持输入格式不变。与 `-A` 组合使用，可以像 “echo” 那样向 SPI 设备传递十六进制值，同时在 stdout 上以二进制形式输出接收到的数据。

**`-C`** `command-bytes` 发送一个或多个命令字节，跳过传输过程中读取的任何字节。字节值应以引号参数的形式指定，格式类似于 `-A` 中 stdin 上的数据格式，即 2 字符十六进制值，可选择以空白字符分隔。SPI 设备通常要求先发送命令，后跟数据字节。可使用此选项发送命令，而不在命令序列期间接收任何数据字节。

**`-c`** `count` 要传输的总字节数，以十进制整数表示。如果执行写或读/写事务，且从 stdin 读取的字节数少于此数目，剩余字节将以 “0” 值发送。如果能从输入文件大小确定长度，可使用 `count` 值 “-1” 以输入文件大小为基准进行传输。

**`-d`** `r`|`w`|`rw` 传输方向：`r` 为读，`w` 为写，`rw` 为同时读写。

**`-f`** `device` 要使用的 SPI 设备（默认为 **/dev/spigen0**）。

**`-h`** 将帮助文本打印到 stderr，解释命令行选项。

**`-i`** 将 SPI 设备信息显示到 stderr。指定此标志时，不读取或写入任何数据，模式和时钟速度也不会改变。

**`-L`** LSB 位序。默认为 MSB，即最高位先传输。指定 `-L` 使 LSB 先传输和读取。

**`-m`** `0`|`1`|`2`|`3` SPI 模式，0 到 3。根据 SPI 规范，定义读取和写入数据时的时钟相位和时序。

**`-S`** 持续从 stdin(3) 流式传输到 `spi` 总线。

**`-s`** `speed` 指定 SPI 时钟的最大速度，单位为 Hz。总线将在不超过此最大值的最高可用速度下运行。

**`-v`** 指定详细模式。诊断和信息写入 stderr。可多次指定 `-v` 以提高详细程度。

## 实例

以下是使用 spi 工具的几个示例：

```sh
spi -i
```

```sh
spi -f spigen0.1 -s 200000 -m 3
```

```sh
spi -d r -C "00 01" -c 2
```

```sh
echo "05" | spi -A -d rw -m 2 -s 500000 -c 2
```

```sh
spi -d rw -c -1 <input_file.bin | od -An -t x1
```

```sh
echo "00 01" | spi -A -b -d rw -c 4 | od -t x2
```

```sh
spi -f spigen0.0 -m 0 -s 1000000 -d r -c 3 -A -C 9f
```

* 获取默认 SPI 设备的信息
* 将 spigen0.1 的最大时钟速度设置为 200Khz、模式设置为 3，但不发送或接收任何数据
* 在默认设备上使用当前模式和速度，发送由 2 字节组成的命令序列，并从 SPI 设备读取额外的 2 字节
* 以模式 2、最大时钟速度 500khz 传输字节值 5，接收 2 字节，并以 2 字节 ASCII 十六进制显示其值
* 发送一个二进制文件，并通过 od(1) 将 SPI 结果以十六进制字节形式输出，使用当前最大时钟速度和 SPI 模式
* 发送 2 字节数据，共接收 4 字节，并以二进制数据形式输出 SPI 结果，通过管道传递给 od(1)，显示为两个十六进制无符号短整数值
* 通过发送命令字节 0x9f 并以 ASCII 十六进制显示 3 字节应答，查询标准 spiflash 设备的制造商 ID 和大小

## 参见

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

## 历史

`spi` 工具出现于 FreeBSD 11.3。


---

# 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/spi.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.
