# SPI

有主从机分别，一主多从，全双工，**通过CS引脚选择要通讯的从机**

主要引脚：MOSI（输出数据线），MISO（输入数据线），SCK（时钟，即SCL），NSS（片选，即CS引脚）

![image-20230410102556489](https://s2.loli.net/2023/04/10/CHtLjnNXT8YhGqp.png)

## 工作模式

![image-20230410102945729](https://s2.loli.net/2023/04/10/sPCKOfpk1oR6LYM.png)

**根据从机的要求选择工作模式**

* SPI的时序图可以分为四个阶段：片选、数据传输、时钟停止和空闲。在片选阶段，主设备通过SS信号将片选值拉低，选择一个从设备。在数据传输阶段，主设备通过SCLK时钟信号将数据传输给从设备，并且从设备通过MISO信号将数据返回给主设备。在时钟停止阶段，主设备停止发送时钟信号，从设备停止接收数据。在空闲阶段，主设备将SS信号拉高，释放片选信号，结束一次通信。
* 外设的写操作和读操作是同步完成的。如果只进行写操作，主机只需忽略接收到的字节；反之，若主机要读取从机的一个字节，就必须发送一个空字节来引发从机的传输。

STM32可以设置自己是主机或是从机


---

# 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:

```
GET https://oceanaparts.gitbook.io/halnote/spi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
