×
嵌入式 > 其他资讯 > 详情

SPI总线详解笔记

发布时间:2020-06-19 发布时间:
|

1. 简介

图1-1给出了SPI架构的概述。 SPI的主要部分是状态,控制和数据寄存器,移位器逻辑,波特率发生器,主/从控制逻辑和端口控制逻辑。

1.1 概述

SPI模块允许MCU和外围设备之间的双工,同步,串行通信。 软件可以轮询SPI状态标志,或者SPI操作可以是中断驱动的。


1.2 特征

SPI包含这些独特的功能:


主模式和从模式

双向模式

从站选择输出

具有CPU中断功能的模式故障错误标志

双缓冲数据寄存器

串行时钟,具有可编程极性和相位

在等待模式下控制SPI操作

1.3 操作模式

SPI在三种模式下运行: 运行(RUN),等待(WAIT )和停止(STOP)


运行模式

这是基本的操作方式。


等待模式

等待模式下的SPI操作是可配置的低功耗模式,由位于SPICR2寄存器中的SPISWAI位控制。在等待模式下,如果SPISWAI位清零,则SPI在运行模式下运行。 如果SPISWAI位置1,则SPI进入功耗保持状态,SPI时钟产生关闭。 如果SPI配置为主站,则任何正在进行的传输将停止,但在CPU进入运行模式后将恢复。 如果SPI配置为从器件,则继续接收和发送一个字节,以便从器件与主器件保持同步。


停止模式

SPI在停止模式下处于非活动状态,以降低功耗。 如果SPI配置为主站,则任何正在进行的传输将停止,但在CPU进入运行模式后将恢复。 如果SPI配置为从器件,则继续接收和发送一个字节,以便从器件与主器件保持同步。


2. 外部信号说明

2.1 概述

本节列出了所有端口的名称和描述,包括可以或可能在芯片外连接的输入和输出。 SPI模块共有4个外部引脚。

2.2 信号详细说明

2.2.1 MOSI

该引脚用于在SPI模块配置为主器件时将数据传输出SPI模块,并在配置为从器件时接收数据。


2.2.2 MISO

该引脚用于在SPI模块配置为从器件时将数据传输出SPI模块,并在配置为主器件时接收数据.


2.2.3 SS

该引脚用于将选择信号从SPI模块输出到另一个外设,当配置为主机时,将其作为数据传输进行,当SPI配置为从机时,它用作接收从机选择信号的输入。


2.2.4 SCK

该引脚用于输出SPI传输数据或接收时钟的时钟。


3. 存储器映射/寄存器定义

本节提供SPI使用的地址空间和寄存器的详细说明。


SPI的存储器映射如下表3-1所示。 为每个寄存器列出的地址是基址和地址偏移的总和。 基址在SoC级定义,地址偏移在模块级定义。 从保留位读取返回零并且写入保留位无效。


注意:


1.某些位是不可写的。


2.忽略对该寄存器的写入。


3.从该寄存器读取将返回全零。


3.1 寄存器说明

本节包含地址顺序中的寄存器描述。 每个描述包括带有相关图号的标准寄存器图。 寄存器位和字段功能的详细信息遵循寄存器图,按位顺序。


3.1.1 SPI Control Register 1

 SPIE — SPI Interrupt Enable Bit

This bit enables SPI interrupt requests, if SPIF or MODF status flag is set.


1 = SPI 中断使能.


0 = SPI 中断失能.


SPE — SPI System Enable Bit

该位使能SPI系统,并将SPI端口引脚专用于SPI系统功能。 如果SPE清零,SPI被禁止并强制进入空闲状态,SPISR寄存器中的状态位将被复位。


1 = SPI enabled, port pins are dedicated to SPI functions.


0 = SPI disabled (lower power consumption).


SPTIE — SPI Transmit Interrupt Enable

This bit enables SPI interrupt requests, if SPTEF flag is set.


1 = SPTEF interrupt enabled.


0 = SPTEF interrupt disabled.


MSTR — SPI Master/Slave Mode Select Bit(模式选择)

This bit selects, if the SPI operates in master or slave mode. Switching the SPI from master to slave or vice versa forces the SPI system into idle state.


1 = SPI is in Master mode


0 = SPI is in Slave mode


CPOL — SPI Clock Polarity Bit(时钟极性选择)

This bit selects an inverted or non-inverted SPI clock. To transmit data between SPI modules, the SPI modules must have identical CPOL values. In master mode, a change of this bit will abort a transmission in progress and force the SPI system into idle state.


1 = Active-low clocks selected. In idle state SCK is high.


0 = Active-high clocks selected. In idle state SCK is low.


CPHA — SPI Clock Phase Bit(时钟相位选择)

This bit is used to select the SPI clock format. In master mode, a change of this bit will abort a transmission in progress and force the SPI system into idle state.


1 = Sampling of data occurs at even edges (2,4,6,...,16) of the SCK clock


0 = Sampling of data occurs at odd edges (1,3,5,...,15) of the SCK clock


SSOE — Slave Select Output Enable

The SS output feature is enabled only in master mode, if MODFEN is set, by asserting the SSOE as shown in Table 3-2. In master mode, a change of this bit will abort a transmission in progress and force the SPI system into idle state.

LSBFE — LSB-First Enable

该位不影响数据寄存器中MSB和LSB的位置。 数据寄存器的读取和写入始终在第7位具有MSB。在主模式下,更改此位将中止正在进行的传输并强制SPI系统进入空闲状态。


1 = Data is transferred least significant bit first.


0 = Data is transferred most significant bit first.


3.1.2 SPI Control Register 2

MODFEN — Mode Fault Enable Bit(通常不使用,直接硬件管脚控制)

This bit allows the MODF failure being detected. If the SPI is in Master mode and MODFEN is cleared, then the SS port pin is not used by the SPI. In Slave mode, the SS is available only as an input regardless of the value of MODFEN. For an overview on the impact of the MODFEN bit on the SS port pin configuration refer to Table 3-2. In master mode, a change of this bit will abort a transmission in progress and force the SPI system into idle state.


1 = SS port pin with MODF feature


0 = SS port pin is not used by the SPI


BIDIROE — Output enable in the Bidirectional mode of operation(通常单向,不起作用)

This bit controls the MOSI and MISO output buffer of the SPI, when in bidirectional mode of operation (SPC0 is set). In master mode this bit controls the output buffer of the MOSI port, in slave mode it controls the output buffer of the MISO port. In master mode, with SPC0 set, a change of this bit will abort a transmission in progress and force the SPI into idle state.


1 = Output buffer enabled


0 = Output buffer disabled


SPISWAI — SPI Stop in Wait Mode Bit

在等待模式下,该位用于节能。


1 = Stop SPI clock generation when in wait mode


0 = SPI clock operates normally in wait mode


SPC0 — Serial Pin Control Bit 0(通常使用正常模式)

该位使能双向引脚配置,如表3-3所示。 在主模式下,该位的更改将中止正在进行的传输并强制SPI系统进入空闲状态。

 3.1.3 SPI Baud Rate Register

SPPR2–SPPR0 — SPI Baud Rate Preselection Bits

SPR2–SPR0 — SPI Baud Rate Selection Bits

详细波特率配置对照表见手册


 3.1.4 SPI Status Register

SPIF — SPIF Interrupt Flag

接收到的数据字节传输到SPI数据寄存器后,该位置1。 通过读SPISR寄存器(SPIF置1),然后对SPI数据寄存器进行读访问,可以清除该位。


1 = New data copied to SPIDR


0 = Transfer not yet complete


 SPTEF — SPI Transmit Empty Interrupt Flag

如果置位,该位指示发送数据寄存器为空。 要清除该位并将数据放入发送数据寄存器,必须使用SPTEF = 1读取SPISR,然后写入SPIDR。 在不读取SPTEF = 1的情况下对SPI数据寄存器进行任何写操作都会被忽略。


1 = SPI Data register empty


0 = SPI Data register not empty


MODF — Mode Fault Flag

This bit is set if the SS input becomes low while the SPI is configured as a master and mode fault detection is enabled, MODFEN bit of SPICR2 register is set. Refer to MODFEN bit description inSPI Control Register 2. The flag is cleared automatically by a read of the SPI Status Register(with MODF set) followed by a write to the SPI Control Register 1.


1 = Mode fault has occurred.


0 = Mode fault has not occurred.


3.1.5 SPI Data Register

SPI数据寄存器是SPI数据的输入和输出寄存器。 写入该寄存器允许对数据字节进行排队和传输。 对于配置为主设备的SPI,在前一次传输完成后立即发送排队的数据字节。 SPISR寄存器中的SPI发送器空标志SPTEF指示SPI数据寄存器何时准备接受新数据。


当设置SPIF时,SPIDR中的接收数据有效。


如果SPIF清零并且接收到一个字节,则接收的字节从接收移位寄存器传送到SPIDR,并且SPIF置1.


如果SPIF置1且未被服务,并且已接收到第二个字节,则第二个接收字节将作为有效字节保留在接收移位寄存器中,直到另一个传输开始。 SPIDR中的字节不会更改。


如果SPIF置1且有效字节在接收移位寄存器中,并且SPIF在第三次发送开始之前被服务,则接收移位寄存器中的字节被传送到SPIDR并且SPIF保持置1(见图3-6)。


如果SPIF置1且接收移位寄存器中有效字节,并且在第三次发送开始后SPIF服务,则接收移位寄存器中的字节无效并且不会传输到SPIDR中(see Figure 3-7 Reception with SPIF serviced too late)


4. 功能描述

4.1 概述

SPI模块允许MCU和外围设备之间的双工,同步,串行通信。 软件可以轮询SPI状态标志或SPI操作可以是中断驱动的。


 通过设置SPI控制寄存器1中的SPI使能位(SPE)来使能SPI系统。当SPE位置1时,四个相关的SPI端口引脚专用于SPI功能:


• Slave select (SS)


• Serial clock (SCK)


• Master out/slave in (MOSI)


• Master in/slave out (MISO)


SPI系统的主要元件是SPI数据寄存器。 主器件中的8位数据寄存器和从器件中的8位数据寄存器由MOSI和MISO引脚相连,形成分布式16位寄存器。 当执行数据传输操作时,该16位寄存器通过S时钟从主机串行移位8位,

 

『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
如何提升手机电磁兼容的性能