23. Serial peripheral interface (SPI)
23.1 Introduction
The SPI interface can be used to communicate with external devices using the SPI protocol.
The serial peripheral interface (SPI) protocol supports half-duplex, full-duplex and simplex synchronous, serial communication with external devices. The interface can be configured as master and in this case it provides the communication clock (SCK) to the external slave device. The interface is also capable of operating in multimaster configuration.
23.1.1 SPI main features
- • Master or slave operation
- • Full-duplex synchronous transfers on three lines
- • Half-duplex synchronous transfer on two lines (with bidirectional data line)
- • Simplex synchronous transfers on two lines (with unidirectional data line)
- • 8-bit to 16-bit transfer frame format selection
- • Multimaster mode capability
- • 8 master mode baud rate prescalers up to \( f_{PCLK}/2 \) .
- • Slave mode frequency up to \( f_{PCLK}/2 \) .
- • NSS management by hardware or software for both master and slave: dynamic change of master/slave operations
- • Programmable clock polarity and phase
- • Programmable data order with MSB-first or LSB-first shifting
- • Dedicated transmission and reception flags with interrupt capability
- • SPI bus busy status flag
- • SPI Motorola support
- • Hardware CRC feature for reliable communication:
- – CRC value can be transmitted as last byte in Tx mode
- – Automatic CRC error checking for last received byte
- • Master mode fault, overrun flags with interrupt capability
- • CRC Error flag
- • 1-byte/word transmission and reception buffer with DMA capability: Tx and Rx requests
23.1.2 SPI extended features
- • SPI TI mode support
23.2 SPI implementation
This manual describes the full set of features implemented in SPI1.
Table 119. STM32L010xx SPI implementation
| SPI Features (1) | SPI1 |
|---|---|
| Hardware CRC calculation | X |
| I2S mode | - |
| TI mode | X |
1. X = supported.
23.3 SPI functional description
23.3.1 General description
The SPI allows synchronous, serial communication between the MCU and external devices. Application software can manage the communication by polling the status flag or using dedicated SPI interrupt. The main elements of SPI and their interactions are shown in the following block diagram Figure 217 .
![Figure 217. SPI block diagram. This block diagram illustrates the internal architecture of an SPI peripheral. On the left, four pins are shown: MOSI, MISO, SCK, and NSS. The MOSI and MISO pins are connected to a bidirectional data bus. The SCK pin is connected to a Baud rate generator. The NSS pin is connected to NSS logic. The internal components include a Read block, an Rx buffer, a Shift register, a TX buffer, a Write block, a CRC controller, a Communication controller, and a Baud rate generator. The Read block is connected to the Address and data bus and the Rx buffer. The Rx buffer is connected to the Shift register. The Shift register is connected to the TX buffer. The TX buffer is connected to the Write block. The Write block is connected to the Address and data bus. The CRC controller is connected to the Address and data bus and the Communication controller. The Communication controller is connected to the Shift register, the Baud rate generator, and the NSS logic. The Baud rate generator is connected to the SCK pin and the Communication controller. The NSS logic is connected to the NSS pin and the Communication controller. Various control signals are shown, including LSBFIRST, CPOL, CPHA, DFF, BIDIOE, BIDIMODE, RXOLNY, CRCEN, CRCNEXT, and BR[2:0]. The diagram is labeled MSV33711V1 in the bottom right corner.](/RM0451-STM32L0x0/0ae2ada1abfae130b05dac620ed47fdf_img.jpg)
Figure 217. SPI block diagram
Four I/O pins are dedicated to SPI communication with external devices.
- • MISO: Master In / Slave Out data. In the general case, this pin is used to transmit data in slave mode and receive data in master mode.
- • MOSI: Master Out / Slave In data. In the general case, this pin is used to transmit data in master mode and receive data in slave mode.
- • SCK: Serial Clock output pin for SPI masters and input pin for SPI slaves.
- •
NSS:
Slave select pin. Depending on the SPI and NSS settings, this pin can be used to either:
- – select an individual slave device for communication
- – synchronize the data frame or
- – detect a conflict between multiple masters
See Section 23.3.5: Slave select (NSS) pin management for details.
The SPI bus allows the communication between one master device and one or more slave devices. The bus consists of at least two wires - one for the clock signal and the other for synchronous data transfer. Other signals can be added depending on the data exchange between SPI nodes and their slave select signal management.
23.3.2 Communications between one master and one slave
The SPI allows the MCU to communicate using different configurations, depending on the device targeted and the application requirements. These configurations use 2 or 3 wires (with software NSS management) or 3 or 4 wires (with hardware NSS management). Communication is always initiated by the master.
Full-duplex communication
By default, the SPI is configured for full-duplex communication. In this configuration, the shift registers of the master and slave are linked using two unidirectional lines between the MOSI and the MISO pins. During SPI communication, data is shifted synchronously on the SCK clock edges provided by the master. The master transmits the data to be sent to the slave via the MOSI line and receives data from the slave via the MISO line. When the data frame transfer is complete (all the bits are shifted) the information between the master and slave is exchanged.
Figure 218. Full-duplex single master/ single slave application

- 1. The NSS pins can be used to provide a hardware control flow between master and slave. Optionally, the pins can be left unused by the peripheral. Then the flow has to be handled internally for both master and slave. For more details see Section 23.3.5: Slave select (NSS) pin management .
Half-duplex communication
The SPI can communicate in half-duplex mode by setting the BIDIMODE bit in the SPIx_CR1 register. In this configuration, one single cross connection line is used to link the shift registers of the master and slave together. During this communication, the data is synchronously shifted between the shift registers on the SCK clock edge in the transfer direction selected reciprocally by both master and slave with the BDIOE bit in their SPIx_CR1 registers. In this configuration, the master's MISO pin and the slave's MOSI pin are free for other application uses and act as GPIOs.
Figure 219. Half-duplex single master/ single slave application

- 1. The NSS pins can be used to provide a hardware control flow between master and slave. Optionally, the pins can be left unused by the peripheral. Then the flow has to be handled internally for both master and slave. For more details see Section 23.3.5: Slave select (NSS) pin management .
- 2. In this configuration, the master's MISO pin and the slave's MOSI pin can be used as GPIOs.
- 3. A critical situation can happen when communication direction is changed not synchronously between two nodes working at bidirectional mode and new transmitter accesses the common data line while former transmitter still keeps an opposite value on the line (the value depends on SPI configuration and communication data). Both nodes then fight while providing opposite output levels on the common line temporary till next node changes its direction settings correspondingly, too. It is suggested to insert a serial resistance between MISO and MOSI pins at this mode to protect the outputs and limit the current blowing between them at this situation.
Simplex communications
The SPI can communicate in simplex mode by setting the SPI in transmit-only or in receive-only using the RXONLY bit in the SPIx_CR2 register. In this configuration, only one line is used for the transfer between the shift registers of the master and slave. The remaining MISO and MOSI pins pair is not used for communication and can be used as standard GPIOs.
- • Transmit-only mode (RXONLY=0): The configuration settings are the same as for full-duplex. The application has to ignore the information captured on the unused input pin. This pin can be used as a standard GPIO.
- • Receive-only mode (RXONLY=1): The application can disable the SPI output function by setting the RXONLY bit. In slave configuration, the MISO output is disabled and the pin can be used as a GPIO. The slave continues to receive data from the MOSI pin while its slave select signal is active (see 23.3.5: Slave select (NSS) pin management ). Received data events appear depending on the data buffer configuration. In the master configuration, the MOSI output is disabled and the pin can be used as a GPIO. The clock signal is generated continuously as long as the SPI is enabled. The only way to stop the clock is to clear the RXONLY bit or the SPE bit and wait until the incoming pattern from the MISO pin is finished and fills the data buffer structure, depending on its configuration.
Figure 220. Simplex single master/single slave application (master in transmit-only/ slave in receive-only mode)

- 1. The NSS pins can be used to provide a hardware control flow between master and slave. Optionally, the pins can be left unused by the peripheral. Then the flow has to be handled internally for both master and slave. For more details see Section 23.3.5: Slave select (NSS) pin management .
- 2. An accidental input information is captured at the input of transmitter Rx shift register. All the events associated with the transmitter receive flow must be ignored in standard transmit only mode (e.g. OVF flag).
- 3. In this configuration, both the MISO pins can be used as GPIOs.
Note: Any simplex communication can be alternatively replaced by a variant of the half-duplex communication with a constant setting of the transaction direction (bidirectional mode is enabled while BDIO bit is not changed).
23.3.3 Standard multi-slave communication
In a configuration with two or more independent slaves, the master uses GPIO pins to manage the chip select lines for each slave (see Figure 221 ). The master must select one of the slaves individually by pulling low the GPIO connected to the slave NSS input. When this is done, a standard master and dedicated slave communication is established.
Figure 221. Master and three independent slaves

- 1. NSS pin is not used on master side at this configuration. It has to be managed internally (SSM=1, SSI=1) to prevent any MODF error.
- 2. As MISO pins of the slaves are connected together, all slaves must have the GPIO configuration of their MISO pin set as alternate function open-drain (see Section 8.3.7: I/O alternate function input/output on page 196 ).
23.3.4 Multi-master communication
Unless SPI bus is not designed for a multi-master capability primarily, the user can use built-in feature which detects a potential conflict between two nodes trying to master the bus at the same time. For this detection, NSS pin is used configured at hardware input mode.
The connection of more than two SPI nodes working at this mode is impossible as only one node can apply its output on a common data line at time.
When nodes are non active, both stay at slave mode by default. Once one node wants to overtake control on the bus, it switches itself into master mode and applies active level on the slave select input of the other node via dedicated GPIO pin. After the session is completed, the active slave select signal is released and the node mastering the bus temporarily returns back to passive slave mode waiting for next session start.
If potentially both nodes raised their mastering request at the same time a bus conflict event appears (see mode fault MODF event). Then the user can apply some simple arbitration process (e.g. to postpone next attempt by predefined different time-outs applied at both nodes).
Figure 222. Multi-master application

The diagram illustrates a multi-master application where two SPI nodes are connected to a common bus. Each node is represented by a box containing its internal components: an Rx (Tx) shift register, a Tx (Rx) shift register, and an SPI clock generator. The bus consists of four lines: MISO, MOSI, SCK, and NSS.
- MISO lines from both nodes are connected together and have bidirectional arrows.
- MOSI lines from both nodes are connected together and have bidirectional arrows.
- SCK lines from both nodes are connected together and have bidirectional arrows.
- The NSS line of the left node is labeled 'NSS (1) ' and has an arrow pointing to the right node's 'GPIO' input.
- The NSS line of the right node is labeled 'NSS (1) ' and has an arrow pointing to the left node's 'GPIO' input.
- 1. The NSS pin is configured at hardware input mode at both nodes. Its active level enables the MISO line output control as the passive node is configured as a slave.
23.3.5 Slave select (NSS) pin management
In slave mode, the NSS works as a standard “chip select” input and lets the slave communicate with the master. In master mode, NSS can be used either as output or input. As an input it can prevent multimaster bus collision, and as an output it can drive a slave select signal of a single slave.
Hardware or software slave select management can be set using the SSM bit in the SPIx_CR1 register:
- • Software NSS management (SSM = 1) : in this configuration, slave select information is driven internally by the SSI bit value in register SPIx_CR1. The external NSS pin is free for other application uses.
- • Hardware NSS management (SSM = 0) : in this case, there are two possible configurations. The configuration used depends on the NSS output configuration (SSOE bit in register SPIx_CR1).
- – NSS output enable (SSM=0,SSOE = 1) : this configuration is only used when the MCU is set as master. The NSS pin is managed by the hardware. The NSS signal is driven low as soon as the SPI is enabled in master mode (SPE=1), and is kept low until the SPI is disabled (SPE =0).
- – NSS output disable (SSM=0, SSOE = 0) : if the microcontroller is acting as the master on the bus, this configuration allows multimaster capability. If the NSS pin is pulled low in this mode, the SPI enters master mode fault state and the device is automatically reconfigured in slave mode. In slave mode, the NSS pin works as a standard “chip select” input and the slave is selected while NSS line is at low level.
Figure 223. Hardware/software slave select management

The diagram illustrates the internal logic for slave select management. It is divided into 'NSS external logic' (left) and 'NSS internal logic' (right) by a dashed vertical line. The 'NSS pin' is connected to 'GPIO logic' in the external logic section. The 'GPIO logic' is connected to a multiplexer (MUX) in the internal logic section. The MUX has two inputs: '1' (connected to the 'SSI control bit') and '0' (connected to the 'NSS pin' via the 'GPIO logic'). The MUX is controlled by the 'SSM control bit'. The output of the MUX is the 'NSS Input'. The 'NSS Input' is also connected to a table. The 'NSS Input' is also connected to the 'NSS Output Control' block. The 'NSS Output Control' block is connected to the 'NSS Output' line. The 'NSS Output' line is labeled '(used in Master mode & NSS HW management only)'. The 'NSS Output Control' block is also connected to the 'SSOE control bit'.
| NSS Inp. | Master mode | Slave mode |
|---|---|---|
| Vdd | OK | Non active |
| Vss | Conflict | Active |
aiv14746e
23.3.6 Communication formats
During SPI communication, receive and transmit operations are performed simultaneously. The serial clock (SCK) synchronizes the shifting and sampling of the information on the data lines. The communication format depends on the clock phase, the clock polarity and the data frame format. To be able to communicate together, the master and slaves devices must follow the same communication format.
Clock phase and polarity controls
Four possible timing relationships may be chosen by software, using the CPOL and CPHA bits in the SPIx_CR1 register. The CPOL (clock polarity) bit controls the idle state value of the clock when no data is being transferred. This bit affects both master and slave modes. If CPOL is reset, the SCK pin has a low-level idle state. If CPOL is set, the SCK pin has a high-level idle state.
If the CPHA bit is set, the second edge on the SCK pin captures the first data bit transacted (falling edge if the CPOL bit is reset, rising edge if the CPOL bit is set). Data are latched on each occurrence of this clock transition type. If the CPHA bit is reset, the first edge on the SCK pin captures the first data bit transacted (falling edge if the CPOL bit is set, rising edge if the CPOL bit is reset). Data are latched on each occurrence of this clock transition type.
The combination of CPOL (clock polarity) and CPHA (clock phase) bits selects the data capture clock edge.
Figure 224 , shows an SPI full-duplex transfer with the four combinations of the CPHA and CPOL bits.
Note: Prior to changing the CPOL/CPHA bits the SPI must be disabled by resetting the SPE bit. The idle state of SCK must correspond to the polarity selected in the SPIx_CR1 register (by pulling up SCK if CPOL=1 or pulling down SCK if CPOL=0).
Figure 224. Data clock timing diagram

The figure displays two timing diagrams for SPI data clock configurations. The top diagram is for CPHA = 1, and the bottom diagram is for CPHA = 0. Each diagram shows the relationship between the clock signal (CPOL), data lines (MOSI, MISO), and the slave select signal (NSS).
Top Diagram: CPHA = 1
- CPOL = 1: The clock signal is high when idle. The capture strobe is shown as a rising edge.
- CPOL = 0: The clock signal is low when idle. The capture strobe is shown as a falling edge.
- MOSI: Master Out Slave In. The first bit is labeled MSBit and the last bit is labeled LSBit. Data is shifted out on the falling edge of the clock (for CPOL=1) or rising edge (for CPOL=0).
- MISO: Master In Slave Out. The first bit is labeled MSBit and the last bit is labeled LSBit. Data is shifted in on the rising edge of the clock (for CPOL=1) or falling edge (for CPOL=0).
- NSS (to slave): Slave Select signal, active low. It is shown as a low pulse during the data transfer.
- Capture strobe: The signal used to capture the data from the MISO line. It is a narrow pulse aligned with the clock edges.
Bottom Diagram: CPHA = 0
- CPOL = 1: The clock signal is high when idle. The capture strobe is shown as a falling edge.
- CPOL = 0: The clock signal is low when idle. The capture strobe is shown as a rising edge.
- MOSI: Master Out Slave In. The first bit is labeled MSBit and the last bit is labeled LSBit. Data is shifted out on the rising edge of the clock (for CPOL=0) or falling edge (for CPOL=1).
- MISO: Master In Slave Out. The first bit is labeled MSBit and the last bit is labeled LSBit. Data is shifted in on the falling edge of the clock (for CPOL=1) or rising edge (for CPOL=0).
- NSS (to slave): Slave Select signal, active low. It is shown as a low pulse during the data transfer.
- Capture strobe: The signal used to capture the data from the MISO line. It is a narrow pulse aligned with the clock edges.
ai17154d
Note: The order of data bits depends on LSBFIRST bit setting.
Data frame format
The SPI shift register can be set up to shift out MSB-first or LSB-first, depending on the value of the LSBFIRST bit. Each data frame is 8 or 16 bit long depending on the size of the data programmed using the DFF bit in the SPI_CR1 register. The selected data frame format is applicable both for transmission and reception.
23.3.7 SPI configuration
The configuration procedure is almost the same for master and slave. For specific mode setups, follow the dedicated chapters. When a standard communication is to be initialized, perform these steps:
- 1. Write proper GPIO registers: Configure GPIO for MOSI, MISO and SCK pins.
- 2. Write to the SPI_CR1 register:
- a) Configure the serial clock baud rate using the BR[2:0] bits ( Note: 3 ).
- b) Configure the CPOL and CPHA bits combination to define one of the four relationships between the data transfer and the serial clock. ( Note: 2 - except the case when CRC is enabled at TI mode).
- c) Select simplex or half-duplex mode by configuring RXONLY or BIDIMODE and BIDIOE (RXONLY and BIDIMODE can't be set at the same time).
- d) Configure the LSBFIRST bit to define the frame format ( Note: 2 ).
- e) Configure the CRCEN and CRCEN bits if CRC is needed (while SCK clock signal is at idle state).
- f) Configure SSM and SSI ( Note: 2 ).
- g) Configure the MSTR bit (in multimaster NSS configuration, avoid conflict state on NSS if master is configured to prevent MODF error).
- h) Set the DFF bit to configure the data frame format (8 or 16 bits).
- 3. Write to SPI_CR2 register:
- a) Configure SSOE ( Note: 1 & 2 ).
- b) Set the FRF bit if the TI protocol is required.
- 4. Write to SPI_CRCPR register: Configure the CRC polynomial if needed.
- 5. Write proper DMA registers: Configure DMA streams dedicated for SPI Tx and Rx in DMA registers if the DMA streams are used.
- Note:
- (1) Step is not required in slave mode.
- (2) Step is not required in TI mode.
- (3) The step is not required in slave mode except slave working at TI mode.
For code example, refer to A.17.1: SPI master configuration code example and A.17.2: SPI slave configuration code example .
23.3.8 Procedure for enabling SPI
It is recommended to enable the SPI slave before the master sends the clock. Otherwise, undesired data transmission might occur. The slave data register must already contain data to be sent before starting communication with the master (either on the first edge of the communication clock, or before the end of the ongoing communication if the clock signal is continuous). The SCK signal must be settled at an idle state level corresponding to the selected polarity before the SPI slave is enabled.
At full-duplex (or in any transmit-only mode), the master starts communicating when the SPI is enabled and data to be sent is written in the Tx Buffer.
In any master receive-only mode (RXONLY=1 or BIDIMODE=1 & BIDIOE=0), the master starts communicating and the clock starts running immediately after the SPI is enabled.
The slave starts communicating when it receives a correct clock signal from the master. The slave software must write the data to be sent before the SPI master initiates the transfer.
Refer to Section 23.3.11: Communication using DMA (direct memory addressing) for details on how to handle DMA.
23.3.9 Data transmission and reception procedures
Rx and Tx buffers
In reception, data are received and then stored into an internal Rx buffer while in transmission, data are first stored into an internal Tx buffer before being transmitted. A read access to the SPI_DR register returns the Rx buffered value whereas a write access to the SPI_DR stores the written data into the Tx buffer.
Tx buffer handling
The data frame is loaded from the Tx buffer into the shift register during the first bit transmission. Bits are then shifted out serially from the shift register to a dedicated output pin depending on LSBFIRST bit setting. The TXE flag (Tx buffer empty) is set when the data are transferred from the Tx buffer to the shift register. It indicates that the internal Tx buffer is ready to be loaded with the next data. An interrupt can be generated if the TXEIE bit of the SPI_CR2 register is set. Clearing the TXE bit is performed by writing to the SPI_DR register.
A continuous transmit stream can be achieved if the next data to be transmitted are stored in the Tx buffer while previous frame transmission is still ongoing. When the software writes to Tx buffer while the TXE flag is not set, the data waiting for transaction is overwritten.
Rx buffer handling
The RXNE flag (Rx buffer not empty) is set on the last sampling clock edge, when the data are transferred from the shift register to the Rx buffer. It indicates that data are ready to be read from the SPI_DR register. An interrupt can be generated if the RXNEIE bit in the SPI_CR2 register is set. Clearing the RXNE bit is performed by reading the SPI_DR register.
If a device has not cleared the RXNE bit resulting from the previous data byte transmitted, an overrun condition occurs when the next value is buffered. The OVR bit is set and an interrupt is generated if the ERRIE bit is set.
Another way to manage the data exchange is to use DMA (see Section 10.2: DMA main features ).
Sequence handling
The BSY bit is set when a current data frame transaction is ongoing. When the clock signal runs continuously, the BSY flag remains set between data frames on the master side. However, on the slave side, it becomes low for a minimum duration of one SPI clock cycle between each data frame transfer.
For some configurations, the BSY flag can be used during the last data transfer to wait until the completion of the transfer.
When a receive-only mode is configured on the master side, either in half-duplex (BIDIMODE=1, BIDIOE=0) or simplex configuration (BIDIMODE=0, RXONLY=1), the master starts the receive sequence as soon as the SPI is enabled. Then the clock signal is provided by the master and it does not stop until either the SPI or the receive-only mode is disabled by the master. The master receives data frames continuously up to this moment.
While the master can provide all the transactions in continuous mode (SCK signal is continuous), it has to respect slave capability to handle data flow and its content at anytime. When necessary, the master must slow down the communication and provide either a slower clock or separate frames or data sessions with sufficient delays. Be aware there is no underflow error signal for slave operating in SPI mode, and that data from the slave are always transacted and processed by the master even if the slave cannot not prepare them correctly in time. It is preferable for the slave to use DMA, especially when data frames are shorter and bus rate is high.
Each sequence must be encased by the NSS pulse in parallel with the multislave system to select just one of the slaves for communication. In single slave systems, using NSS to control the slave is not necessary. However, the NSS pulse can be used to synchronize the slave with the beginning of each data transfer sequence. NSS can be managed either by software or by hardware (see Section 23.3.4: Multi-master communication ).
Refer to Figure 225 and Figure 226 for a description of continuous transfers in master / full-duplex and slave full-duplex mode.
Figure 225. TXE/RXNE/BSY behavior in master / full-duplex mode (BIDIMODE=0, RXONLY=0) in the case of continuous transfers

Example in Master mode with CPOL=1, CPHA=1
The diagram illustrates the timing and control signals for SPI master full-duplex communication. The SCK signal is a continuous square wave. The MISO/MOSI (out) line shows the transmission of three 8-bit data frames: DATA1 = 0xF1, DATA2 = 0xF2, and DATA3 = 0xF3. The TXE flag is set by hardware when the Tx buffer is empty and cleared by software when data is written. The BSY flag is set by hardware when the SPI is active and reset by hardware when it is inactive. The MISO/MOSI (in) line shows the reception of three 8-bit data frames: DATA 1 = 0xA1, DATA 2 = 0xA2, and DATA 3 = 0xA3. The RXNE flag is set by hardware when the Rx buffer is full and cleared by software when data is read. The Rx buffer (read SPI_DR) shows the received data being read by software. The software actions are: write 0xF1 into SPI_DR, wait until TXE=1 and write 0xF2 into SPI_DR, wait until RXNE=1 and read 0xA1 from SPI_DR, wait until TXE=1 and write 0xF3 into SPI_DR, wait until RXNE=1 and read 0xA2 from SPI_DR, and wait until RXNE=1 and read 0xA3 from SPI_DR.
For code example, refer to A.17.3: SPI full duplex communication code example .
Figure 226. TXE/RXNE/BSY behavior in slave / full-duplex mode (BIDIMODE=0, RXONLY=0) in the case of continuous transfers

The diagram illustrates the timing and state changes for an SPI slave in full-duplex mode. The SCK signal is a continuous square wave. The MISO/MOSI (out) line transmits 8-bit data (b0-b7) for DATA 1 (0xF1), DATA 2 (0xF2), and DATA 3 (0xF3). The TXE flag is set by hardware when the Tx buffer is empty and cleared by software. The Tx buffer (write to SPI_DR) contains the data to be transmitted. The BSY flag is set by hardware when a transfer starts and cleared by software, reset by hardware when the transfer ends. The MISO/MOSI (in) line receives 8-bit data (b0-b7) for DATA 1 (0xA1), DATA 2 (0xA2), and DATA 3 (0xA3). The RXNE flag is set by hardware when the Rx buffer is not empty and cleared by software. The Rx buffer (read from SPI_DR) contains the received data. The diagram shows the sequence of events: software writes 0xF1 into SPI_DR, software waits until TXE=1 and writes 0xF2 into SPI_DR, software waits until RXNE=1 and reads 0xA1 from SPI_DR, software waits until TXE=1 and writes 0xF3 into SPI_DR, software waits until RXNE=1 and reads 0xA2 from SPI_DR, software waits until RXNE=1 and reads 0xA3 from SPI_DR.
23.3.10 Procedure for disabling the SPI
When SPI is disabled, it is mandatory to follow the disable procedures described in this paragraph. It is important to do this before the system enters a low-power mode when the peripheral clock is stopped. Ongoing transactions can be corrupted in this case. In some modes the disable procedure is the only way to stop continuous communication running.
Master in full-duplex or transmit only mode can finish any transaction when it stops providing data for transmission. In this case, the clock stops after the last data transaction.
Standard disable procedure is based on pulling BSY status together with TXE flag to check if a transmission session is fully completed. This check can be done in specific cases, too, when it is necessary to identify the end of ongoing transactions, for example:
- • When NSS signal is managed by an arbitrary GPIO toggle and the master has to provide proper end of NSS pulse for slave, or
- • When transactions' streams from DMA are completed while the last data frame or CRC frame transaction is still ongoing in the peripheral bus.
The correct disable procedure is (except when receive-only mode is used):
- 1. Wait until RXNE=1 to receive the last data.
- 2. Wait until TXE=1 and then wait until BSY=0 before disabling the SPI.
- 3. Read received data.
Note: During discontinuous communications, there is a 2 APB clock period delay between the write operation to the SPI_DR register and BSY bit setting. As a consequence it is mandatory to wait first until TXE is set and then until BSY is cleared after writing the last data.
The correct disable procedure for certain receive-only modes is:
- 1. Interrupt the receive flow by disabling SPI (SPE=0) in the specific time window while the last data frame is ongoing.
- 2. Wait until BSY=0 (the last data frame is processed).
- 3. Read received data.
Note: To stop a continuous receive sequence, a specific time window must be respected during the reception of the last data frame. It starts when the first bit is sampled and ends before the last bit transfer starts.
23.3.11 Communication using DMA (direct memory addressing)
To operate at its maximum speed and to facilitate the data register read/write process required to avoid overrun, the SPI features a DMA capability, which implements a simple request/acknowledge protocol.
A DMA access is requested when the TXE or RXNE enable bit in the SPIx_CR2 register is set. Separate requests must be issued to the Tx and Rx buffers.
- • In transmission, a DMA request is issued each time TXE is set to 1. The DMA then writes to the SPIx_DR register.
- • In reception, a DMA request is issued each time RXNE is set to 1. The DMA then reads the SPIx_DR register.
Refer to Figure 227 and Figure 228 for a description of the DMA transmission and reception waveforms.
When the SPI is used only to transmit data, it is possible to enable only the SPI Tx DMA channel. In this case, the OVR flag is set because the data received is not read. When the SPI is used only to receive data, it is possible to enable only the SPI Rx DMA channel.
In transmission mode, when the DMA has written all the data to be transmitted (the TCIF flag is set in the DMA_ISR register), the BSY flag can be monitored to ensure that the SPI communication is complete. This is required to avoid corrupting the last transmission before disabling the SPI or entering the Stop mode. The software must first wait until TXE = 1 and then until BSY = 0.
When starting communication using DMA, to prevent DMA channel management raising error events, these steps must be followed in order:
- 1. Enable DMA Rx buffer in the RXDMAEN bit in the SPI_CR2 register, if DMA Rx is used.
- 2. Enable DMA streams for Tx and Rx in DMA registers, if the streams are used.
- 3. Enable DMA Tx buffer in the TXDMAEN bit in the SPI_CR2 register, if DMA Tx is used.
- 4. Enable the SPI by setting the SPE bit.
For code example, refer to and A.17.4: SPI master configuration with DMA code example .
To close communication it is mandatory to follow these steps in order:
- 1. Disable DMA streams for Tx and Rx in the DMA registers, if the streams are used.
- 2. Disable the SPI by following the SPI disable procedure.
- 3. Disable DMA Tx and Rx buffers by clearing the TXDMAEN and RXDMAEN bits in the SPI_CR2 register, if DMA Tx and/or DMA Rx are used.
Figure 227. Transmission using DMA

Example with CPOL=1, CPHA=1
The diagram illustrates the timing for SPI transmission using DMA for three data items: DATA 1 = 0xF1, DATA 2 = 0xF2, and DATA 3 = 0xF3. Each data item is transmitted as 8 bits (b0 to b7).
- SCK: Shows a continuous square wave clock signal.
- MISO/MOSI (out): Shows the data being transmitted. For DATA 1, bits b0-b7 are sent. For DATA 2, bits b0-b7 are sent. For DATA 3, bits b0-b7 are sent.
- TXE flag:
- Initially low.
- Set by hardware when the first data item (DATA 1) is ready to be written.
- Cleared by DMA write (when DATA 1 is written to SPI_DR).
- Set by hardware when the second data item (DATA 2) is ready to be written.
- Cleared by DMA write (when DATA 2 is written to SPI_DR).
- Set by hardware when the third data item (DATA 3) is ready to be written.
- Remains high until the last data item is transmitted.
- BSY flag:
- Set by hardware when the first data item is ready to be written.
- Remains high until the last data item is transmitted.
- Reset by hardware after the last data item is transmitted.
- DMA request:
- Active low pulse when TXE flag is set and BSY flag is low.
- Ignored by the DMA because the DMA transfer is complete (when TXE flag is still high).
- Tx buffer (write to SPI_DR):
- Contains 0xF1, 0xF2, and 0xF3 at different times.
- DMA writes to SPI_DR:
- Three separate writes are shown, corresponding to the three data items.
- DMA TCIF flag (DMA transfer complete):
- Set by hardware after the last data item is transmitted.
- Cleared by software.
Software sequence:
- software configures the DMA SPI Tx channel to send 3 data items and enables the SPI
- DMA writes DATA1 into SPI_DR
- DMA writes DATA2 into SPI_DR
- DMA writes DATA3 into SPI_DR
- DMA transfer is complete (TCIF=1 in DMA_ISR)
- software waits until TXE=1
- software waits until BSY=0
ai17349
Figure 228. Reception using DMA

Example with CPOL=1, CPHA=1
The diagram illustrates the timing for SPI reception using DMA. The SCK signal is a continuous square wave. The MISO/MOSI (in) signal shows three data items: DATA 1 = 0xA1, DATA 2 = 0xA2, and DATA 3 = 0xA3, each consisting of 8 bits (b0 to b7). The RXNE flag is set by hardware when the first data item (DATA 1) is received and cleared by the DMA read. The DMA request is generated when the RXNE flag is set. The Rx buffer (read from SPI_DR) contains the received data items: 0xA1, 0xA2, and 0xA3. The DMA read from SPI_DR is performed by the DMA controller. The flag DMA TCIF (DMA transfer complete) is set by hardware when the DMA transfer is complete and cleared by software.
software configures the DMA SPI Rx channel to receive 3 data items and enables the SPI
DMA reads DATA1 from SPI_DR
DMA reads DATA2 from SPI_DR
DMA reads DATA3 from SPI_DR
The DMA transfer is complete (TCIF=1 in DMA_ISR)
ai17350
23.3.12 SPI status flags
Three status flags are provided for the application to completely monitor the state of the SPI bus.
Tx buffer empty flag (TXE)
When it is set, the TXE flag indicates that the Tx buffer is empty and that the next data to be transmitted can be loaded into the buffer. The TXE flag is cleared by writing to the SPI_DR register.
Rx buffer not empty (RXNE)
When set, the RXNE flag indicates that there are valid received data in the Rx buffer. It is cleared by reading from the SPI_DR register.
Busy flag (BSY)
The BSY flag is set and cleared by hardware (writing to this flag has no effect).
When BSY is set, it indicates that a data transfer is in progress on the SPI (the SPI bus is busy). There is one exception in master bidirectional receive mode (MSTR=1 and BDM=1 and BDOE=0) where the BSY flag is kept low during reception.
The BSY flag can be used in certain modes to detect the end of a transfer, thus preventing corruption of the last transfer when the SPI peripheral clock is disabled before entering a low-power mode or an NSS pulse end is handled by software.
The BSY flag is also useful for preventing write collisions in a multimaster system.
The BSY flag is cleared under any one of the following conditions:
- • When the SPI is correctly disabled
- • When a fault is detected in Master mode (MODF bit set to 1)
- • In Master mode, when it finishes a data transmission and no new data is ready to be sent
- • In Slave mode, when the BSY flag is set to '0' for at least one SPI clock cycle between each data transfer.
Note: It is recommended to use always the TXE and RXNE flags (instead of the BSY flags) to handle data transmission or reception operations.
23.3.13 SPI error flags
An SPI interrupt is generated if one of the following error flags is set and interrupt is enabled by setting the ERRIE bit.
Overrun flag (OVR)
An overrun condition occurs when the master or the slave completes the reception of the next data frame while the read operation of the previous frame from the Rx buffer has not completed (case RXNE flag is set).
In this case, the content of the Rx buffer is not updated with the new data received. A read operation from the SPI_DR register returns the frame previously received. All other subsequently transmitted data are lost.
Clearing the OVR bit is done by a read access to the SPI_DR register followed by a read access to the SPI_SR register.
Mode fault (MODF)
Mode fault occurs when the master device has its internal NSS signal (NSS pin in NSS hardware mode, or SSI bit in NSS software mode) pulled low. This automatically sets the MODF bit. Master mode fault affects the SPI interface in the following ways:
- • The MODF bit is set and an SPI interrupt is generated if the ERRIE bit is set.
- • The SPE bit is cleared. This blocks all output from the device and disables the SPI interface.
- • The MSTR bit is cleared, thus forcing the device into slave mode.
Use the following software sequence to clear the MODF bit:
- 1. Make a read or write access to the SPIx_SR register while the MODF bit is set.
- 2. Then write to the SPIx_CR1 register.
To avoid any multiple slave conflicts in a system comprising several MCUs, the NSS pin must be pulled high during the MODF bit clearing sequence. The SPE and MSTR bits can be restored to their original state after this clearing sequence. As a security, hardware does not allow the SPE and MSTR bits to be set while the MODF bit is set. In a slave device the MODF bit cannot be set except as the result of a previous multimaster conflict.
CRC error (CRCERR)
This flag is used to verify the validity of the value received when the CRCEN bit in the SPIx_CR1 register is set. The CRCERR flag in the SPIx_SR register is set if the value received in the shift register does not match the receiver SPIx_RXCRC value. The flag is cleared by the software.
TI mode frame format error (FRE)
A TI mode frame format error is detected when an NSS pulse occurs during an ongoing communication when the SPI is operating in slave mode and configured to conform to the TI mode protocol. When this error occurs, the FRE flag is set in the SPIx_SR register. The SPI is not disabled when an error occurs, the NSS pulse is ignored, and the SPI waits for the next NSS pulse before starting a new transfer. The data may be corrupted since the error detection may result in the loss of two data bytes.
The FRE flag is cleared when SPIx_SR register is read. If the ERRIE bit is set, an interrupt is generated on the NSS error detection. In this case, the SPI should be disabled because data consistency is no longer guaranteed and communications should be re-initiated by the master when the slave SPI is enabled again.
23.4 SPI special features
23.4.1 TI mode
TI protocol in master mode
The SPI interface is compatible with the TI protocol. The FRF bit of the SPIx_CR2 register can be used to configure the SPI to be compliant with this protocol.
The clock polarity and phase are forced to conform to the TI protocol requirements whatever the values set in the SPIx_CR1 register. NSS management is also specific to the TI protocol which makes the configuration of NSS management through the SPIx_CR1 and SPIx_CR2 registers (SSM, SSI, SSOE) impossible in this case.
In slave mode, the SPI baud rate prescaler is used to control the moment when the MISO pin state changes to HiZ when the current transaction finishes (see Figure 229 ). Any baud rate can be used, making it possible to determine this moment with optimal flexibility. However, the baud rate is generally set to the external master clock baud rate. The delay for the MISO signal to become HiZ ( \( t_{\text{release}} \) ) depends on internal resynchronization and on the baud rate value set in through the BR[2:0] bits in the SPIx_CR1 register. It is given by the formula:
If the slave detects a misplaced NSS pulse during a data frame transaction the TIFRE flag is set.
This feature is not available for Motorola SPI communications (FRF bit set to 0).
Note: To detect TI frame errors in slave transmitter only mode by using the Error interrupt (ERRIE=1), the SPI must be configured in 2-line unidirectional mode by setting BIDIMODE and BIDIOE to 1 in the SPI_CR1 register. When BIDIMODE is set to 0, OVR is set to 1 because the data register is never read and error interrupts are always generated, while when BIDIMODE is set to 1, data are not received and OVR is never set.
Figure 229 shows the SPI communication waveforms when TI mode is selected.
Figure 229. TI mode transfer

The diagram shows the timing for SPI TI mode transfer. The signals are:
- NSS (Slave Select): An active-low signal that goes low to start a transfer and high to end it. A release time \( t_{RELEASE} \) is indicated at the end.
- SCK (Serial Clock): A square wave clock signal.
- MOSI (Master Out Slave In): Data transmitted from master to slave. It shows 'Do not care' at the start, followed by data blocks with MSB and LSB labels. 'trigger' and 'sampling' points are marked on the rising edges of SCK.
- MISO (Master In Slave Out): Data transmitted from slave to master. It shows '1 or 0' at the start, followed by data blocks with MSB and LSB labels.
23.4.2 CRC calculation
Two separate CRC calculators (on transmission and reception data flows) are implemented in order to check the reliability of transmitted and received data. The SPI offers CRC8 or CRC16 calculation depending on the data format selected through the DFF bit. The CRC is calculated serially using the polynomial programmed in the SPI_CRCPR register.
CRC principle
CRC calculation is enabled by setting the CRCEN bit in the SPIx_CR1 register before the SPI is enabled (SPE = 1). The CRC value is calculated using an odd programmable polynomial on each bit. The calculation is processed on the sampling clock edge defined by the CPHA and CPOL bits in the SPIx_CR1 register. The calculated CRC value is checked automatically at the end of the data block as well as for transfer managed by CPU or by the DMA. When a mismatch is detected between the CRC calculated internally on the received data and the CRC sent by the transmitter, a CRCERR flag is set to indicate a data corruption error. The right procedure for handling the CRC calculation depends on the SPI configuration and the chosen transfer management.
Note: The polynomial value should only be odd. No even values are supported.
CRC transfer managed by CPU
Communication starts and continues normally until the last data frame has to be sent or received in the SPIx_DR register. Then CRCNEXT bit has to be set in the SPIx_CR1 register to indicate that the CRC frame transaction will follow after the transaction of the currently processed data frame. The CRCNEXT bit must be set before the end of the last data frame transaction. CRC calculation is frozen during CRC transaction.
The received CRC is stored in the Rx buffer like any other data frame.
A CRC-format transaction takes one more data frame to communicate at the end of data sequence.
When the last CRC data is received, an automatic check is performed comparing the received value and the value in the SPIx_RXCRC register. Software has to check the CRCERR flag in the SPIx_SR register to determine if the data transfers were corrupted or not. Software clears the CRCERR flag by writing '0' to it.
After the CRC reception, the CRC value is stored in the Rx buffer and must be read in the SPIx_DR register in order to clear the RXNE flag.
CRC transfer managed by DMA
When SPI communication is enabled with CRC communication and DMA mode, the transmission and reception of the CRC at the end of communication is automatic (with the exception of reading CRC data in receive-only mode). The CRCNEXT bit does not have to be handled by the software. The counter for the SPI transmission DMA channel has to be set to the number of data frames to transmit excluding the CRC frame. On the receiver side, the received CRC value is handled automatically by DMA at the end of the transaction but user must take care to flush out the CRC frame received from SPI_DR as it is always loaded into it.
At the end of the data and CRC transfers, the CRCERR flag in the SPIx_SR register is set if corruption occurred during the transfer.
Resetting the SPIx_TXCRC and SPIx_RXCRC values
The SPIx_TXCRC and SPIx_RXCRC values are cleared automatically when CRC calculation is enabled.
When the SPI is configured in slave mode with the CRC feature enabled, a CRC calculation is performed even if a high level is applied on the NSS pin. This may happen for example in case of a multislave environment where the communication master addresses slaves alternately.
Between a slave disabling (high level on NSS) and a new slave enabling (low level on NSS), the CRC value should be cleared on both master and slave sides to resynchronize the master and slave respective CRC calculation.
To clear the CRC, follow the below sequence:
- 1. Disable the SPI
- 2. Clear the CRCEN bit
- 3. Enable the CRCEN bit
- 4. Enable the SPI
Note: When the SPI interface is configured as a slave, the NSS internal signal needs to be kept low during transaction of the CRC phase once the CRCNEXT signal is released, (see more details at the product errata sheet).
At TI mode, despite the fact that the clock phase and clock polarity setting is fixed and independent on the SPIx_CR1 register, the corresponding setting CPOL=0 CPHA=1 has to be kept at the SPIx_CR1 register anyway if CRC is applied. In addition, the CRC calculation has to be reset between sessions by the SPI disable sequence by re-enabling the CRCEN bit described above at both master and slave sides, else the CRC calculation can be corrupted at this specific mode.
23.5 SPI interrupts
During SPI communication an interrupts can be generated by the following events:
- • Transmit Tx buffer ready to be loaded
- • Data received in Rx buffer
- • Master mode fault
- • Overrun error
- • TI frame format error
Interrupts can be enabled and disabled separately.
Table 120. SPI interrupt requests
| Interrupt event | Event flag | Enable Control bit |
|---|---|---|
| Transmit Tx buffer ready to be loaded | TXE | TXEIE |
| Data received in Rx buffer | RXNE | RXNEIE |
| Master Mode fault event | MODF | ERRIE |
| Overrun error | OVR | |
| CRC error | CRCERR | |
| TI frame format error | FRE |
For code example, refer to A.17.6: SPI interrupt code example .
23.6 SPI registers
The peripheral registers can be accessed by half-words (16-bit) or words (32-bit). In addition, SPI_DR can be accessed by 8-bit.
Refer to Section 1.2 for a list of abbreviations used in register descriptions.
23.6.1 SPI control register 1 (SPI_CR1)
Address offset: 0x00
Reset value: 0x0000
| 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| BIDI MODE | BIDI OE | CRC EN | CRC NEXT | DFF | RX ONLY | SSM | SSI | LSB FIRST | SPE | BR [2:0] | MSTR | CPOL | CPHA | ||
| rw | rw | rw | rw | rw | rw | rw | rw | rw | rw | rw | rw | rw | rw | rw | rw |
Bit 15 BIDIMODE : Bidirectional data mode enable
This bit enables half-duplex communication using common single bidirectional data line. Keep RXONLY bit clear when bidirectional mode is active.
0: 2-line unidirectional data mode selected
1: 1-line bidirectional data mode selected
Note:
Bit 14 BIDIOE : Output enable in bidirectional mode
This bit combined with the BIDIMODE bit selects the direction of transfer in bidirectional mode
0: Output disabled (receive-only mode)
1: Output enabled (transmit-only mode)
Note: In master mode, the MOSI pin is used while the MISO pin is used in slave mode.
Bit 13 CRCEN : Hardware CRC calculation enable
0: CRC calculation disabled
1: CRC calculation enabled
Note: This bit should be written only when SPI is disabled (SPE = '0') for correct operation.
Bit 12 CRCNEXT : CRC transfer next
0: Data phase (no CRC phase)
1: Next transfer is CRC (CRC phase)
Note: When the SPI is configured in full-duplex or transmitter only modes, CRCNEXT must be written as soon as the last data is written to the SPI_DR register.
When the SPI is configured in receiver only mode, CRCNEXT must be set after the second last data reception.
This bit should be kept cleared when the transfers are managed by DMA.
Bit 11 DFF : Data frame format
0: 8-bit data frame format is selected for transmission/reception
1: 16-bit data frame format is selected for transmission/reception
Note: This bit should be written only when SPI is disabled (SPE = '0') for correct operation.
Bit 10 RXONLY: Receive only mode enableThis bit enables simplex communication using a single unidirectional line to receive data exclusively. Keep BIDIMODE bit clear when receive only mode is active.
This bit is also useful in a multislave system in which this particular slave is not accessed, the output from the accessed slave is not corrupted.
0: full-duplex (Transmit and receive)
1: Output disabled (Receive-only mode)
Note:
Bit 9 SSM: Software slave managementWhen the SSM bit is set, the NSS pin input is replaced with the value from the SSI bit.
0: Software slave management disabled
1: Software slave management enabled
Note: This bit is not used in SPI TI mode
Bit 8 SSI: Internal slave selectThis bit has an effect only when the SSM bit is set. The value of this bit is forced onto the NSS pin and the IO value of the NSS pin is ignored.
Note: This bit is not used in SPI TI mode
Bit 7 LSBFIRST: Frame format0: MSB transmitted first
1: LSB transmitted first
Note: This bit should not be changed when communication is ongoing.
It is not used in SPI TI mode
Bit 6 SPE: SPI enable0: Peripheral disabled
1: Peripheral enabled
Note: When disabling the SPI, follow the procedure described in Section 23.3.10: Procedure for disabling the SPI .
Bits 5:3 BR[2:0]: Baud rate control000:
\(
f_{PCLK}/2
\)
001:
\(
f_{PCLK}/4
\)
010:
\(
f_{PCLK}/8
\)
011:
\(
f_{PCLK}/16
\)
100:
\(
f_{PCLK}/32
\)
101:
\(
f_{PCLK}/64
\)
110:
\(
f_{PCLK}/128
\)
111:
\(
f_{PCLK}/256
\)
Note: These bits should not be changed when communication is ongoing.
Bit 2 MSTR : Master selection
- 0: Slave configuration
- 1: Master configuration
Note: This bit should not be changed when communication is ongoing.
Bit 1 CPOL : Clock polarity
- 0: CK to 0 when idle
- 1: CK to 1 when idle
Note: This bit should not be changed when communication is ongoing.
It is not used in SPI TI mode except the case when CRC is applied at TI mode.
Bit 0 CPHA : Clock phase
- 0: The first clock transition is the first data capture edge
- 1: The second clock transition is the first data capture edge
Note: This bit should not be changed when communication is ongoing.
It is not used in SPI TI mode except the case when CRC is applied at TI mode.
23.6.2 SPI control register 2 (SPI_CR2)
Address offset: 0x04
Reset value: 0x0000
| 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | TXEIE | RXNEIE | ERRIE | FRF | Res. | SSOE | TXDMAEN | RXDMAEN |
| rw | rw | rw | rw | rw | rw | rw |
Bits 15:8 Reserved, must be kept at reset value.
Bit 7 TXEIE : Tx buffer empty interrupt enable
- 0: TXE interrupt masked
- 1: TXE interrupt not masked. Used to generate an interrupt request when the TXE flag is set.
Bit 6 RXNEIE : RX buffer not empty interrupt enable
- 0: RXNE interrupt masked
- 1: RXNE interrupt not masked. Used to generate an interrupt request when the RXNE flag is set.
Bit 5 ERRIE : Error interrupt enable
This bit controls the generation of an interrupt when an error condition occurs (OVR, CRCERR, MODF, FRE).
- 0: Error interrupt is masked
- 1: Error interrupt is enabled
Bit 4 FRF : Frame format
- 0: SPI Motorola mode
- 1: SPI TI mode
Note:
Bit 3 Reserved. Forced to 0 by hardware.
Bit 2 SSOE : SS output enable
0: SS output is disabled in master mode and the cell can work in multimaster configuration
1: SS output is enabled in master mode and when the cell is enabled. The cell cannot work in a multimaster environment.
Note: This bit is not used in SPI TI mode.
Bit 1 TXDMAEN : Tx buffer DMA enable
When this bit is set, the DMA request is made whenever the TXE flag is set.
0: Tx buffer DMA disabled
1: Tx buffer DMA enabled
Bit 0 RXDMAEN : Rx buffer DMA enable
When this bit is set, the DMA request is made whenever the RXNE flag is set.
0: Rx buffer DMA disabled
1: Rx buffer DMA enabled
23.6.3 SPI status register (SPI_SR)
Address offset: 0x08
Reset value: 0x0002
| 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Res. | Res. | Res. | Res. | Res. | Res. | Res. | FRE | BSY | OVR | MODF | CRC ERR | Res. | Res. | TXE | RXNE |
| r | r | r | r | rc_w0 | r | r |
Bits 15:9 Reserved. Forced to 0 by hardware.
Bit 8 FRE : Frame Error
0: No frame error
1: Frame error occurred.
This bit is set by hardware and cleared by software when the SPI_SR register is read.
This bit is used in SPI TI mode whatever the audio protocol selected. It detects a change on NSS or WS line which takes place in slave mode at a non expected time, informing about a desynchronization between the external master device and the slave.
Bit 7 BSY : Busy flag
0: SPI not busy
1: SPI is busy in communication or Tx buffer is not empty
This flag is set and cleared by hardware.
Note: BSY flag must be used with caution: refer to Section 23.3.12: SPI status flags and Section 23.3.10: Procedure for disabling the SPI .
Bit 6 OVR : Overrun flag
0: No overrun occurred
1: Overrun occurred
This flag is set by hardware and reset by a software sequence. Refer to Section 23.3.13: SPI error flags for the software sequence.
Bit 5 MODF : Mode fault
0: No mode fault occurred
1: Mode fault occurred
This flag is set by hardware and reset by a software sequence. Refer to Section 23.4 on page 703 for the software sequence.
Note:
Bit 4 CRCERR : CRC error flag
0: CRC value received matches the SPI_RXCRCR value
1: CRC value received does not match the SPI_RXCRCR value
This flag is set by hardware and cleared by software writing 0.
Note:
Bits 3:2 Reserved. Forced to 0 by hardware.
Bit 1 TXE : Transmit buffer empty
0: Tx buffer not empty
1: Tx buffer empty
Bit 0 RXNE : Receive buffer not empty
0: Rx buffer empty
1: Rx buffer not empty
23.6.4 SPI data register (SPI_DR)
Address offset: 0x0C
Reset value: 0x0000

| 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| DR[15:0] | |||||||||||||||
| rw | rw | rw | rw | rw | rw | rw | rw | rw | rw | rw | rw | rw | rw | rw | rw |
Bits 15:0 DR[15:0] : Data register
Data received or to be transmitted.
The data register is split into 2 buffers - one for writing (Transmit Buffer) and another one for reading (Receive buffer). A write to the data register will write into the Tx buffer and a read from the data register will return the value held in the Rx buffer.
Note: These notes apply to SPI mode:
Depending on the data frame format selection bit (DFF in SPI_CR1 register), the data sent or received is either 8-bit or 16-bit. This selection has to be made before enabling the SPI to ensure correct operation.
For an 8-bit data frame, the buffers are 8-bit and only the LSB of the register (SPI_DR[7:0]) is used for transmission/reception. When in reception mode, the MSB of the register (SPI_DR[15:8]) is forced to 0.
For a 16-bit data frame, the buffers are 16-bit and the entire register, SPI_DR[15:0] is used for transmission/reception.
23.6.5 SPI CRC polynomial register (SPI_CRCPR)
Address offset: 0x10
Reset value: 0x0007

| 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| CRCPOLY[15:0] | |||||||||||||||
| rw | rw | rw | rw | rw | rw | rw | rw | rw | rw | rw | rw | rw | rw | rw | rw |
Bits 15:0 CRCPOLY[15:0] : CRC polynomial register
This register contains the polynomial for the CRC calculation.
The CRC polynomial (0007h) is the reset value of this register. Another polynomial can be configured as required.
Note:
23.6.6 SPI RX CRC register (SPI_RXCRCR)
Address offset: 0x14
Reset value: 0x0000

| 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| RXCRC[15:0] | |||||||||||||||
| r | r | r | r | r | r | r | r | r | r | r | r | r | r | r | r |
Bits 15:0 RXCRC[15:0] : Rx CRC register
When CRC calculation is enabled, the RxCRC[15:0] bits contain the computed CRC value of the subsequently received bytes. This register is reset when the CRCEN bit in SPI_CR1 register is written to 1. The CRC is calculated serially using the polynomial programmed in the SPI_CRCPR register.
Only the 8 LSB bits are considered when the data frame format is set to be 8-bit data (DFF bit of SPI_CR1 is cleared). CRC calculation is done based on any CRC8 standard.
The entire 16-bits of this register are considered when a 16-bit data frame format is selected (DFF bit of the SPI_CR1 register is set). CRC calculation is done based on any CRC16 standard.
Note: A read to this register when the BSY Flag is set could return an incorrect value.
23.6.7 SPI TX CRC register (SPI_TXCRCR)
Address offset: 0x18
Reset value: 0x0000

| 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| TXCRC[15:0] | |||||||||||||||
| r | r | r | r | r | r | r | r | r | r | r | r | r | r | r | r |
Bits 15:0 TXCRC[15:0] : Tx CRC register
When CRC calculation is enabled, the TxCRC[7:0] bits contain the computed CRC value of the subsequently transmitted bytes. This register is reset when the CRCEN bit of SPI_CR1 is written to 1. The CRC is calculated serially using the polynomial programmed in the SPI_CRCPR register.
Only the 8 LSB bits are considered when the data frame format is set to be 8-bit data (DFF bit of SPI_CR1 is cleared). CRC calculation is done based on any CRC8 standard.
The entire 16-bits of this register are considered when a 16-bit data frame format is selected (DFF bit of the SPI_CR1 register is set). CRC calculation is done based on any CRC16 standard.
Note: A read to this register when the BSY flag is set could return an incorrect value.
23.6.8 SPI register map
The table provides shows the SPI register map and reset values.
Table 121. SPI register map and reset values
| Offset | Register | 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0x00 | SPI_CR1 | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | BIDIMODE | BIDIOE | CRCEN | CRCNEXT | DFF | RXONLY | SSM | SSI | LSBFIRST | SPE | BR [2:0] | MSTR | CPOL | CPHA | ||
| Reset value | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ||||||||||||||||||
| 0x04 | SPI_CR2 | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | TXEIE | RXNEIE | ERRIE | FRF | Res. | SSOE | TXDMAEN | RXDMAEN |
| Reset value | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ||||||||||||||||||||||||||
| 0x08 | SPI_SR | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | FRE | BSY | OVR | MODF | CRCERR | Res. | TXE | RXNE | |
| Reset value | 0 | 0 | 0 | 0 | 0 | 0 | 1 | ||||||||||||||||||||||||||
| 0x0C | SPI_DR | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | DR[15:0] | |||||||||||||||
| Reset value | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | |||||||||||||||||||
| 0x10 | SPI_CRCPR | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | CRCPOLY[15:0] | |||||||||||||||
| Reset value | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | |||||||||||||||||||
| 0x14 | SPI_RXCRCR | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | RxCRC[15:0] | |||||||||||||||
| Reset value | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | |||||||||||||||||||
| 0x18 | SPI_TXCRCR | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | TxCRC[15:0] | |||||||||||||||
| Reset value | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | |||||||||||||||||||
Refer to Section 2.2 on page 39 for the register boundary addresses.