53. Serial peripheral interface (SPI)

53.1 SPI introduction

The serial peripheral interface (SPI) can be used to communicate with external devices while using the specific synchronous protocol. The SPI protocol supports half-duplex, full-duplex, and simplex synchronous, serial communication with external devices. The interface can be configured as master or slave and can operate in multislave or multimaster configurations. The device configured as master provides a communication clock (SCK) to the slave device. The slave select (NSS) and ready (RDY) signals can be applied optionally just to set up communication with a concrete slave and to ensure it handles the data flow properly. The Motorola data format is used by default, but some other specific modes are supported as well.

53.2 SPI main features

53.3 SPI implementation

The table below describes the SPI implementation.

Table 577. SPI features

SPI featureSPI1, SPI2
(full-featured instances)
SPI3, SPI4 (1)
(limited-featured instances)
Data sizeConfigurable from 4 to 32 bits8/16 bits
CRC computationCRC polynomial length configurable from 5 to 33 bitsCRC polynomial length 9/17 bits
FIFO size16 x 8 bits8 x 8 bits
FIFO threshold1 - 16 data1 - 4 data
Number of data controlUp to 65535Up to 1023, no remaining data counter (CTSIZE)
Autonomous in Stop mode with wake-up capabilityYesYes
Autonomous in Standby mode with wake-up capabilityNoNo

1. SPI4 is available only on STM32U3B5/3C5 devices.

Note: For detailed information about instance capabilities to exit from Stop and Standby modes, refer to Table 583: SPI wake-up and interrupt requests and Table 87: Functionalities depending on the working mode .

53.4 SPI functional description

53.4.1 SPI block diagram

The SPI enables synchronous, serial communications between the MCU and external devices. The application software can manage the communication by polling the status flag or using a dedicated SPI interrupt. The main SPI elements and their interactions are shown in Figure 758.

Figure 758. SPI block diagram

Figure 758. SPI block diagram. This is a complex block diagram of the SPI peripheral. On the left, a vertical '32-bit APB bus' is shown. Various signals connect to it: spi_wkup, spi_it, spi_pclk_req, spi_tx_dma, spi_rx_dma, spi_trg[15:0], spi_pclk, spi_ker_ck, and spi_ker_ck_req. The SPI block itself is divided into three clock domains: 'spi_pclk clock domain' (containing the APB interface, COM Controller with registers SPI_IER, SPI_SR, SPI_IFCR, SPI_CRCPOLY, SPI_TXCRC, SPI_RXCRC, SPI_TXDR, SPI_UDRDR, SPI_RXDR, SPI_AUTOCR, SPI_CR1, SPI_CR2, SPI_CFG[2:1]), 'spi_ker_ck clock domain' (containing the Clock Generator with a Prescaler), and 'Serial interface clock domain' (containing RDY Logic, NSS Logic, CRC Controller, TX Shift Reg, RX Shift Reg, and a transceiver block). The transceiver block connects to external pins: RDY, NSS, MOSI, MISO, and SCK. Synchronization blocks labeled 'SYNC' are placed at the boundaries between the clock domains.
Figure 758. SPI block diagram. This is a complex block diagram of the SPI peripheral. On the left, a vertical '32-bit APB bus' is shown. Various signals connect to it: spi_wkup, spi_it, spi_pclk_req, spi_tx_dma, spi_rx_dma, spi_trg[15:0], spi_pclk, spi_ker_ck, and spi_ker_ck_req. The SPI block itself is divided into three clock domains: 'spi_pclk clock domain' (containing the APB interface, COM Controller with registers SPI_IER, SPI_SR, SPI_IFCR, SPI_CRCPOLY, SPI_TXCRC, SPI_RXCRC, SPI_TXDR, SPI_UDRDR, SPI_RXDR, SPI_AUTOCR, SPI_CR1, SPI_CR2, SPI_CFG[2:1]), 'spi_ker_ck clock domain' (containing the Clock Generator with a Prescaler), and 'Serial interface clock domain' (containing RDY Logic, NSS Logic, CRC Controller, TX Shift Reg, RX Shift Reg, and a transceiver block). The transceiver block connects to external pins: RDY, NSS, MOSI, MISO, and SCK. Synchronization blocks labeled 'SYNC' are placed at the boundaries between the clock domains.

The simplified scheme of Figure 758 shows three fully independent clock domains:

All the control and status signals between these domains are strictly synchronized. There is no specific constraint concerning the frequency ratio between these clock signals. The user has to consider a ratio compatible with the data flow speed to avoid data underrun or overrun events.

The spi_pclk clock signal feeds the peripheral bus interface. It must be active when accesses to the SPI registers are required.

The SPI working in slave mode handles a data flow using the serial interface clock derived from the external SCK signal provided by the external master SPI device. That is why the SPI slave is able to receive and send data even when the spi_pclk and spi_ker_ck clock signals are inactive. As a consequence, a specific slave logic working within the serial interface clock domain needs some additional traffic to be set up correctly (for example when underrun or overrun is evaluated, see Section 53.5.2 for details). This cannot be done when the bus becomes idle. In some specific cases, the slave even requires the clock generator working (see Section 53.5.1 ).

When the SPI works as a master, the RCC must provide the spi_ker_ck kernel clock to the peripheral during communication to feed the serial interface clock via the clock generator where it can be divided by prescaler or bypassed. The signal is then provided to the slaves via the SCK pin and internally to the serial interface domain of the master.

53.4.2 SPI pins and internal signals

Up to five I/O pins are dedicated to SPI communication with external devices.

See Section 53.4.7 for details.

The SPI bus enables 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 are optional and can be added depending on the data exchange between SPI nodes and their communication control management.

Refer to Table 578 and Table 579 for the list of SPI input / output pins and internal signals.

Table 578. SPI input/output pins

Pin nameI/O typeDescription
MISO (1)Input/outputMaster data input / slave data output
MOSI (1)Input/outputMaster data output / slave data input
SCKInput/outputMaster clock output / slave clock input
NSSInput/outputMaster output / slave selection input
RDYInput/outputSPI master input / slave FIFOs status occupancy output
  1. 1. Functionality of MOSI and MISO pins can be swapped. Their directions may vary in SPI bidirectional half-duplex mode.

Description of SPI input/output signals

Table 579. SPI internal input/output signals

Signal nameSignal typeDescription
spi_pclkInputSPI clock signal feeds the peripheral bus interface
spi_ker_ckInputSPI kernel clock
spi_ker_ck_reqOutputSPI kernel clock request
spi_pclk_reqOutputSPI clock request
spi_wkupOutputSPI provides a wake-up interrupt
spi_itOutputSPI global interrupt
spi_tx_dmaInput/outputSPI transmit DMA request
spi_rx_dmaInput/outputSPI receive DMA request
spi_trg[15:0]InputSPI triggers

Description of SPI interconnections

Table 580. SPI interconnection (SPI1 and SPI2)

Signal nameTrigger source
spi_trg0gpdma1_ch0_tcf
spi_trg1gpdma1_ch1_tcf
spi_trg2gpdma1_ch2_tcf
spi_trg3gpdma1_ch3_tcf
spi_trg4exti4
spi_trg5exti9
spi_trg6lptim1_ch1
spi_trg7lptim2_ch1
spi_trg8comp1_out
spi_trg9comp2_out
spi_trg10rtc_alra_trg
spi_trg11rtc_wut_trg
spi_trg12-
spi_trg13-
spi_trg14-
spi_trg15-
Table 581. SPI interconnection (SPI3 and SPI4 (1) )
Signal nameTrigger source
spi_trg0gpdma1_ch0_tcf
spi_trg1gpdma1_ch1_tcf
spi_trg2gpdma1_ch2_tcf
spi_trg3gpdma1_ch3_tcf
spi_trg4exti4
spi_trg5exti8
spi_trg6lptim1_ch1
spi_trg7lptim3_ch1
spi_trg8comp1_out
spi_trg9comp2_out
spi_trg10rtc_alra_trg
spi_trg11rtc_wut_trg
spi_trg12-
spi_trg13-
spi_trg14-
spi_trg15-

1. SPI4 is available only on STM32U3B5/3C5 devices.

53.4.3 SPI communication general aspects

The SPI allows the MCU to communicate using different configurations, depending on the device targeted and the application requirements. These configurations use two or three wires (with software slave select management) or three or four wires (with hardware slave select management). The communication is always initiated and controlled by the master. The master provides a clock signal on the SCK line and selects or synchronizes slaves for communication by NSS line when it is managed by hardware.

The data between the master and the slave flow synchronously on the MOSI and/or MISO lines.

53.4.4 Communications between one master and one slave

The communication flow can use one of three possible modes: the full-duplex (three wires) mode, half-duplex (two wires) mode, or the simplex (two wires) mode. The NSS signal is optional in single master-slave configuration and is often not connected between the two communication nodes. Nevertheless, the NSS signal can be helpful in this configuration to synchronize the data flow and it is used by default for some specific SPI modes (for example the TI mode).

The next optional RDY signal can help to ensure the correct management of all the transferred data at slave side.

Full-duplex communication

By default, the SPI is configured for full-duplex communication (bits COMM[1:0] = 00 in the SPI_CFG2 register). 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 the SPI communication, the data are 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 simultaneously. When the data frame transfer is complete (all the bits are shifted) the information between the master and slave is exchanged.

Figure 759. Full-duplex single master/ single slave application

Diagram of a full-duplex single master/single slave SPI application. The Master side includes an Rx shift register, a Tx shift register, and an SPI clock generator. The Slave side includes a Tx shift register and an Rx shift register. Connections: MISO (Master) ← MISO (Slave); MOSI (Master) → MOSI (Slave); SCK (Master) → SCK (Slave); NSS(1) (Master) -.-> NSS(1) (Slave); RDY(2) (Slave) -.-> RDY(2) (Master).

The diagram illustrates a full-duplex SPI connection between a Master and a Slave. The Master contains an Rx shift register, a Tx shift register, and an SPI clock generator. The Slave contains a Tx shift register and an Rx shift register. The connections are as follows:

Diagram of a full-duplex single master/single slave SPI application. The Master side includes an Rx shift register, a Tx shift register, and an SPI clock generator. The Slave side includes a Tx shift register and an Rx shift register. Connections: MISO (Master) ← MISO (Slave); MOSI (Master) → MOSI (Slave); SCK (Master) → SCK (Slave); NSS(1) (Master) -.-> NSS(1) (Slave); RDY(2) (Slave) -.-> RDY(2) (Master).
  1. 1. The NSS pin interconnection is optional. The slave can be configured to be permanently selected to operate in a single master-slave pair (see Section 53.4.7 ).
  2. 2. The RDY signal provided by the slave can be read by the master optionally.

Half-duplex communication

The SPI can communicate in half-duplex mode by setting COMM[1:0] = 11 in the SPI_CFG2 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 are synchronously shifted between the shift registers on the SCK clock edge in the transfer direction selected reciprocally by both master and slave with the HDDIR bit in their SPI_CR1 registers. Note that the SPI must be disabled when changing the direction of the communication. In this configuration, the MISO pin at master and the MOSI pin at slave are free for other application uses and act as GPIOs.

Figure 760. Half-duplex single master/ single slave application

Figure 760. Half-duplex single master/ single slave application diagram. The diagram shows a Master and a Slave connected by four lines: MISO, MOSI, SCK, and NSS. The Master contains an Rx shift register, a Tx shift register, and an SPI clock generator. The Slave contains a Tx shift register and an Rx shift register. The MISO line is bidirectional, with a 1kΩ resistor on the Master's side. The MOSI line is bidirectional. The SCK line is unidirectional from Master to Slave. The NSS line is unidirectional from Master to Slave. The RDY line is unidirectional from Slave to Master. The diagram is labeled MSV50505V2.
Figure 760. Half-duplex single master/ single slave application diagram. The diagram shows a Master and a Slave connected by four lines: MISO, MOSI, SCK, and NSS. The Master contains an Rx shift register, a Tx shift register, and an SPI clock generator. The Slave contains a Tx shift register and an Rx shift register. The MISO line is bidirectional, with a 1kΩ resistor on the Master's side. The MOSI line is bidirectional. The SCK line is unidirectional from Master to Slave. The NSS line is unidirectional from Master to Slave. The RDY line is unidirectional from Slave to Master. The diagram is labeled MSV50505V2.
  1. 1. The NSS pin interconnection is optional. The slave can be configured to be permanently selected to operate in a single master-slave pair (see Section 53.4.7 ).
  2. 2. In this configuration, the MISO pin at master and MOSI pin at slave can be used as GPIOs
  3. 3. A critical situation can happen when the communication direction is not changed synchronously between two nodes working in bidirectional mode. The new transmitter accesses the common data line while the former transmitter still keeps an opposite value on the line (the value depends on the SPI configuration and communicated data). The nodes can conflict temporarily with opposite output levels on the line until the former transmitter changes its data direction setting. It is suggested to insert a serial resistance between MISO and MOSI pins in this mode to protect the conflicting outputs and limit the current flow between them.
  4. 4. The RDY signal provided by the slave can be read by the master optionally.

Simplex communications

The SPI can communicate in simplex mode by setting the SPI in transmit-only or in receive-only using the COMM[1:0] field in the SPI_CFG2 register. In this configuration, only one line is used for the transfer between the shift registers of the master and slave. The remaining MISO or MOSI pin pair is not used for communication and can be used as standard GPIOs.

The master in transmit-only mode generates the clock as long as there are data available in the TxFIFO and the master transfer is ongoing.

The slave in transmit-only mode sends data as long as it receives a clock on the SCK pin and the NSS pin (or software managed internal signal) is active (see Section 53.4.7 ).

In master mode, the MOSI output is disabled and can be used as a GPIO. The clock signal is generated continuously as long as the SPI is enabled and the CSTART bit in the SPI_CR1 register is set. The clock is stopped either by software explicitly requesting this by setting the CSUSP bit in the SPI_CR1 register or automatically when the RxFIFO is full, when the MASRX bit in the SPI_CR1 is set.

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 Section 53.4.7 ).

Note: In whatever master and slave modes, the data pin dedicated for transmission can be replaced by the data pin dedicated for reception and vice versa by changing the IOSWP bit value in the SPI_CFG2 register (this bit can only be modified when the SPI is disabled).

Any simplex communication can be replaced by a variant of the half-duplex communication with a constant setting of the transfer direction (bidirectional mode is enabled, while the

HDDIR bit is never changed) or by full-duplex control when unused data line and corresponding data flow is ignored.

Figure 761. Simplex single master / single slave application
(master in transmit-only / slave in receive-only mode)

Diagram of a simplex single master / single slave SPI application. The Master side includes an Rx shift register, a Tx shift register, and an SPI clock generator. The Slave side includes a Tx shift register and an Rx shift register. Connections include MISO, MOSI, SCK, NSS(1), and RDY(3). Arrows indicate data flow from Master to Slave on MOSI and SCK lines. The diagram is labeled MSV50506V3.

The diagram illustrates a simplex SPI connection between a Master and a Slave. On the Master side, there is an 'Rx shift register', a 'Tx shift register', and an 'SPI clock generator'. The 'Tx shift register' is connected to the 'MOSI' line, and the 'SPI clock generator' is connected to the 'SCK' line. On the Slave side, there is a 'Tx shift register' and an 'Rx shift register'. The 'MOSI' line from the Master's 'Tx shift register' connects to the Slave's 'Rx shift register'. The 'SCK' line from the Master's 'SPI clock generator' connects to the Slave's 'Rx shift register'. The 'MISO' lines on both sides are shown as unconnected pins. The 'NSS(1)' and 'RDY(3)' lines are also shown, with dashed arrows indicating optional connections between them. The diagram is labeled 'MSV50506V3' in the bottom right corner.

Diagram of a simplex single master / single slave SPI application. The Master side includes an Rx shift register, a Tx shift register, and an SPI clock generator. The Slave side includes a Tx shift register and an Rx shift register. Connections include MISO, MOSI, SCK, NSS(1), and RDY(3). Arrows indicate data flow from Master to Slave on MOSI and SCK lines. The diagram is labeled MSV50506V3.
  1. 1. The NSS pin interconnection is optional. The slave can be configured to be permanently selected to operate in a single master-slave pair (see Section 53.4.7 ).
  2. 2. In this configuration, both the MISO pins can be used as GPIOs.
  3. 3. The RDY signal provided by the slave can be read by the master optionally.

53.4.5 Standard multislave communication

In a configuration with two or more independent slaves, the master uses a star topology with dedicated GPIO pins to manage the chip select lines for each slave separately (see Figure 762 ).

The master must select one of the slaves individually by pulling low the GPIO connected to the slave NSS input (only one slave can control data on a common MISO line at a given time).

When this is done, a communication between the master and the selected slave is established. In addition to the simplicity, the advantage of this topology is that a specific SPI configuration can be applied for each slave as all the communication sessions are performed separately just within a single master-slave pair. Optionally, when there is no need to read any information from slaves, the master can transmit the same information to the multiple slaves.

Figure 762. Master and three independent slaves connected in star topology

Diagram of SPI star topology with one master and three slaves. The master has an SPI clock generator, Tx and Rx shift registers, and pins for NSS(1), MISO, MOSI, SCK, IO1, IO2, and IO3. Three slaves (Slave 1, Slave 2, Slave 3) are connected to the master's MOSI, SCK, and NSS pins. Each slave has its own Tx and Rx shift registers and pins for MISO, MOSI, SCK, and NSS. Arrows indicate data flow: MISO from slaves to master, MOSI from master to slaves, SCK from master to slaves, and NSS from master to slaves. Slave 3's NSS pin is connected to master's IO3 pin.
Diagram of SPI star topology with one master and three slaves. The master has an SPI clock generator, Tx and Rx shift registers, and pins for NSS(1), MISO, MOSI, SCK, IO1, IO2, and IO3. Three slaves (Slave 1, Slave 2, Slave 3) are connected to the master's MOSI, SCK, and NSS pins. Each slave has its own Tx and Rx shift registers and pins for MISO, MOSI, SCK, and NSS. Arrows indicate data flow: MISO from slaves to master, MOSI from master to slaves, SCK from master to slaves, and NSS from master to slaves. Slave 3's NSS pin is connected to master's IO3 pin.
  1. 1. The master single NSS pin hardware output functionality cannot support this topology (to be replaced by a set of GPIOs under software control). The NSS pin is free for other application uses (such as GPIO or other alternate functions). Refer to Section 53.4.7 for details.
  2. 2. If the application cannot ensure that no more than a single NSS active signal is provided by the master at a given time, it is better to configure the MISO pins in an open-drain configuration with an external pull-up on the MISO line to prevent conflicts between the interconnected outputs of the slaves. Else, a push-pull configuration can be applied without an extra resistor (see I/O alternate function input/output (GPIO) section).
  3. 3. The RDY signals can be read by the master from the slaves optionally.

53.4.6 Multimaster communication

Unless the SPI bus is not designed primarily for a multimaster capability, the user can use a built-in feature that detects a potential conflict between two nodes trying to master the bus at the same time. For this detection, the NSS pin is configured in hardware input mode. The connection of more than two SPI nodes working in this mode is impossible, as only one node can apply its output on a common data line at a given time.

When the nodes are not active, both stay in slave mode by default. Once a 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 the dedicated GPIO pin. After the session is complete, the active slave select signal is released and the node mastering the bus temporarily returns back to passive slave mode waiting for the next session to start.

If both nodes raise their mastering request at the same time, a bus conflict event appears (see mode fault MODF event). The user can apply some simple arbitration process (for example postpone the next attempt by different predefined timeouts applied to both nodes).

Figure 763. Multimaster application

Diagram of a multimaster SPI application showing two nodes connected via MISO, MOSI, and SCK lines. Each node has an Rx (Tx) shift register, a Tx (Rx) shift register, and an SPI clock generator. The NSS pin is connected to a GPIO pin on the other node. The diagram shows the Master (Slave) configuration for both nodes.

The diagram illustrates a multimaster SPI application with two nodes. Each node contains an 'Rx (Tx) shift register', a 'Tx (Rx) shift register', and an 'SPI clock generator'. The nodes are connected via three main lines: MISO (Master In Slave Out), MOSI (Master Out Slave In), and SCK (Serial Clock). The MISO and MOSI lines are bidirectional, while the SCK line is also bidirectional. Each node has a GPIO pin connected to the other node's NSS (1) pin. The diagram shows the Master (Slave) configuration for both nodes. The text 'MSv50502V2' is visible in the bottom right corner of the diagram.

Diagram of a multimaster SPI application showing two nodes connected via MISO, MOSI, and SCK lines. Each node has an Rx (Tx) shift register, a Tx (Rx) shift register, and an SPI clock generator. The NSS pin is connected to a GPIO pin on the other node. The diagram shows the Master (Slave) configuration for both nodes.
  1. 1. The NSS pin is configured at hardware input mode at both nodes. Its active level enables the MISO line output control since the passive node is configured as a slave.
  2. 2. The RDY signal is not used in this communication.

53.4.7 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, the NSS can be used either as an output or an input. As an input it can prevent a multimaster bus collision, and as an output it can drive a slave select signal of a single slave. The NSS signal can be managed internally (software management of the NSS input) or externally when both the NSS input and output are associated with the NSS pin (hardware slave select management). The user can configure which level of this input/output external signal (present on the NSS pin) is considered as active by the SSIOP bit setting. NSS level is considered as active if it is equal to SSIOP.

The hardware or software slave select management can be set using the SSM bit in the SPI_CFG2 register:

proper TSIZE > 0 setting to control the NSS output correctly. Even if SPI AF is not applied at the NSS pin (it can be used as a standard GPIO then), keep anyway SSOE = 1 to ensure the default NSS input level and prevent any mode fault evaluation at the input of the master NSS internal logic applicable at a multimaster topology exclusively.

    • a) When SSOM = 0 and SP = 000, the NSS signal is driven to the active level as soon as the master transfer starts (CSTART = 1) and it is kept active until its EOT flag is set or the transmission is suspended.
    • b) When SP = 001, a pulse is generated as defined by the TI mode.
    • c) When SSOM = 1, SP = 000 and MIDI > 1 the NSS is pulsed inactive between data frames, and kept inactive for a number of SPI clock periods defined by the MIDI value decremented by one (from 1 to 14).
    • d) NSS input is forced to nonactive state internally at master to prevent any mode fault.
  1. NSS output disable (SSM = 0, SSOE = 0):
    • a) If the microcontroller is acting as the master on the bus, this configuration allows multimaster capability. If the NSS pin is pulled into an active level in this mode, the SPI enters master mode fault state and the SPI device is automatically reconfigured in slave mode (MASTER = 0).
    • b) In slave mode, the NSS pin works as a standard 'chip select' input and the slave is selected while the NSS line is at its active level.

Note: The purpose of automatic switching into slave mode when a mode fault occurs is to avoid the possible conflicts on data and clock lines. As the SPE is automatically reset in this condition, both Rx and Tx FIFOs are flushed and current data is lost.

When the SPI slave is enabled in the hardware slave select management mode, all the transfers are ignored even in case of the NSS is found at active level. They are ignored until the slave detects a start of the NSS signal (transition from nonactive to active level) just synchronizing the slave with the master. This is because the hardware management mode cannot be used when the external NSS pin is fixed. There is no such protection in the slave select software management. Then the SSI bit must be changed when there is no traffic on the bus and the SCK signal is in idle state level between transfers exclusively in this case.

Figure 764. Scheme of NSS control logic

Figure 764: Scheme of NSS control logic. This block diagram shows the internal logic for the Slave Select (NSS) signal. On the left, an 'NSS pin' is connected to 'GPIO logic', which is part of 'NSS external logic'. The 'GPIO logic' output is inverted and connected to a 2-to-1 multiplexer. The multiplexer's inputs are labeled '1' and '0'. The '1' input is connected to 'SSIOP control', and the '0' input is connected to 'SSI control'. The output of the multiplexer is 'SS_IN'. A table to the right of the multiplexer shows the relationship between 'SS_IN', 'Master mode', and 'Slave mode'. Below the multiplexer, 'NSS output control' is connected to the '0' input of another 2-to-1 multiplexer. This second multiplexer's output is 'SS_OUT', which is then processed by 'NSS output logic'. 'NSS output control' is also connected to 'SSOE control'. 'NSS output logic' is connected to 'SSOM, MIDI, MSSI control'. A note indicates that 'Master mode & HW SS management (SSM=0) only' applies to the 'SS_OUT' path. The entire internal logic section is labeled 'NSS internal logic'.
SS_INMaster modeSlave mode
SS(SSI) <> SSIOPOKNon active
SS(SSI) = SSIOPConflictActive
Figure 764: Scheme of NSS control logic. This block diagram shows the internal logic for the Slave Select (NSS) signal. On the left, an 'NSS pin' is connected to 'GPIO logic', which is part of 'NSS external logic'. The 'GPIO logic' output is inverted and connected to a 2-to-1 multiplexer. The multiplexer's inputs are labeled '1' and '0'. The '1' input is connected to 'SSIOP control', and the '0' input is connected to 'SSI control'. The output of the multiplexer is 'SS_IN'. A table to the right of the multiplexer shows the relationship between 'SS_IN', 'Master mode', and 'Slave mode'. Below the multiplexer, 'NSS output control' is connected to the '0' input of another 2-to-1 multiplexer. This second multiplexer's output is 'SS_OUT', which is then processed by 'NSS output logic'. 'NSS output control' is also connected to 'SSOE control'. 'NSS output logic' is connected to 'SSOM, MIDI, MSSI control'. A note indicates that 'Master mode & HW SS management (SSM=0) only' applies to the 'SS_OUT' path. The entire internal logic section is labeled 'NSS internal logic'.

When the hardware output NSS control is applied (SSM = 0, SSOE = 1), by configuration of the MIDI[3:0] and MSSI[3:0] bitfields, the user can control the timing of the NSS signal between data frames and can insert an extra delay at the beginning of every transfer (to separate the NSS and clock starts). This can be useful when the slave needs to slow down the flow to obtain sufficient room for correct data handling (see Figure 765).

Figure 765. Data flow timing control (SSOE = 1, SSOM = 0, SSM = 0)

Figure 765: Data flow timing control (SSOE = 1, SSOM = 0, SSM = 0). This timing diagram shows the relationship between the NSS, SCK, MOSI, and MISO signals during a data transfer. The NSS signal is shown as a horizontal line. The SCK signal is a periodic square wave. The MOSI and MISO signals are shown as a sequence of data frames, with the Most Significant Bit (MSB) and Least Significant Bit (LSB) indicated. The timing is defined by several parameters: MSSI[3:0] (the time from the falling edge of NSS to the first rising edge of SCK), t_sck (the period of the SCK signal), DSIZE[4:0] + 1 (the number of SCK periods for one data frame), and MIDI[3:0] (the time from the last falling edge of SCK to the rising edge of NSS).
Figure 765: Data flow timing control (SSOE = 1, SSOM = 0, SSM = 0). This timing diagram shows the relationship between the NSS, SCK, MOSI, and MISO signals during a data transfer. The NSS signal is shown as a horizontal line. The SCK signal is a periodic square wave. The MOSI and MISO signals are shown as a sequence of data frames, with the Most Significant Bit (MSB) and Least Significant Bit (LSB) indicated. The timing is defined by several parameters: MSSI[3:0] (the time from the falling edge of NSS to the first rising edge of SCK), t_sck (the period of the SCK signal), DSIZE[4:0] + 1 (the number of SCK periods for one data frame), and MIDI[3:0] (the time from the last falling edge of SCK to the rising edge of NSS).
  1. 1. MSSI[3:0] = 0011, MIDI[3:0] = 0011 (SCK flow is continuous when MIDI[3:0] = 0).
  2. 2. CPHA = 0, CPOL = 0, SSIOP = 0, LSBFRST = 0.

Additionally, setting the SSOM bit invokes a specific mode, which interleaves pulses between data frames if there is a sufficient space to provide them (MIDI[3:0] must be set greater than one SPI period). Some configuration examples are shown in Figure 766.

Figure 766. NSS interleaving pulses between data (SSOE = 1, SSOM = 1, SSM = 0)

Timing diagrams for four SPI modes showing NSS, SCK, MOSI, and MISO signals. The diagrams illustrate the relationship between data bits (MSB/LSB) and clock edges for different CPHA and CPOL settings. Timing parameters like t_SCK, MSSI[3:0], DSIZE[4:0], and MIDI[3:0] are indicated.

I. CPHA=0, CPOL=0, SSIOP=0, LSBFRST=0

II. CPHA=1, CPOL=0, SSIOP=0, LSBFRST=0

III. CPHA=0, CPOL=1, SSIOP=1, LSBFRST=1

IV. CPHA=1, CPOL=1, SSIOP=1, LSBFRST=1

MSv76717V1

Timing diagrams for four SPI modes showing NSS, SCK, MOSI, and MISO signals. The diagrams illustrate the relationship between data bits (MSB/LSB) and clock edges for different CPHA and CPOL settings. Timing parameters like t_SCK, MSSI[3:0], DSIZE[4:0], and MIDI[3:0] are indicated.
  1. 1. MSS[3:0] = 0010, MIDI[3:0] = 0010.
  2. 2. NSS interleaves between data when MIDI[3:0] > 1 wide of the interleaving pulse is always one SCK period less than the gap provided between the frames (defined by the MIDI parameter). If MIDI is set, the frames are separated by a single SCK period but no interleaving pulse appears on NSS.

53.4.8 Ready pin (RDY) management

The status of the slave capability to handle data can be checked on the RDY pin. By default, a low level indicates that the slave is not ready for transfer. The reason can be that the slave TxFIFO is empty, RxFIFO full or the SPI is disabled. An active level of the signal can be selected by the RDIOP bit. If the master continues or starts to communicate with the slave when it indicates a not ready status, it is highly probable that the transfer fails.

The logic to control the RDY output is rather complex, tied closely with the TSIZE and DSIZE settings. The RDY reaction is more pessimistic and sensitive to TxFIFO becoming nearly empty and/or RxFIFO nearly full during a frame transfer. This pessimistic logic is suppressed at the end of a transfer only when RDY stays active, despite TxFIFO becomes fully empty and/or RxFIFO becomes fully occupied. The target is to prevent any data corruption and inform the master in time that it is necessary to suspend the transfer temporarily until the next transferred data can be processed safely again. When the RDY signal input is enabled at master side, the master suspends the communication once the slave indicates not ready status. This prevents the master to complete the transfer of an ongoing frame, which just empties the slave TxFIFO or full fills its RxFIFO until a next data is written and/or read there (despite the frame still can be completed without any constraint). It can make a problem if the TSIZE = 0 configuration is applied at slave because slave then never evaluates the end of the transfer (which suppresses the not ready status just when the last data is sent). Then the user has to release the RxFIFO and/or write additional (even dummy) data to TxFIFO by software at slave side to release the not RDY signal, unblock ST master and so enable it to continue at the communication suspended at middle of a frame occasionally.

When RDY is not used by the master, it must be disabled (RDIOM = 0). Then an internal logic of the master simulates the slave status always ready. In this case, the RDIOP bit setting has no meaning.

Due to synchronization between clock domains and evaluation of the RDY logic on both master and slave sides, the RDY pin feature is not reliable and cannot be used when the size of data frames is configured shorter than 8 bits.

53.4.9 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 slave devices must follow the same communication format and be synchronized correctly.

Clock phase and polarity controls

Four possible timing relationships can be chosen by software, using the CPOL and CPHA bits in the SPI_CFG2 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 transferred (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 transferred (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 the CPOL (clock polarity) and CPHA (clock phase) bits selects the data capture clock edges (dotted lines in Figure 767).

Figure 767 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 SPI_CFG2 register (by pulling the SCK pin up if CPOL = 1 or pulling it down if CPOL = 0).

Figure 767. Data clock timing diagram

Figure 767. Data clock timing diagram. The diagram illustrates SPI full-duplex transfer timing for two CPHA settings. The top section shows CPHA = 1, where data is captured on the second clock edge. The bottom section shows CPHA = 0, where data is captured on the first clock edge. Both sections show the relationship between the SCK signal (CPOL = 1 and CPOL = 0), MOSI (Master Out Slave In), MISO (Master In Slave Out), and NSS (to slave) signals. Data bits are labeled MSB (Most Significant Bit) and LSB (Least Significant Bit).

The diagram illustrates the timing for SPI full-duplex transfers. It is divided into two main sections based on the CPHA bit setting.

The diagram includes the following signals and labels:

ai17154f

Figure 767. Data clock timing diagram. The diagram illustrates SPI full-duplex transfer timing for two CPHA settings. The top section shows CPHA = 1, where data is captured on the second clock edge. The bottom section shows CPHA = 0, where data is captured on the first clock edge. Both sections show the relationship between the SCK signal (CPOL = 1 and CPOL = 0), MOSI (Master Out Slave In), MISO (Master In Slave Out), and NSS (to slave) signals. Data bits are labeled MSB (Most Significant Bit) and LSB (Least Significant Bit).
  1. 1. The order of data bits depends on the 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 LSBFRST bit of the SPI_CFG2 register.

The data frame size is configured through the DSIZE bitfield of the SPI_CFG1 register to a range that depends on the SPI instance (see Section 53.3: SPI implementation ). The setting applies to both transmission and reception. The data bits in the SPI_TXDR/SPI_RXDR registers are right-aligned with 8, 16, or 32 bits, depending on the data size (see Figure 768 ). These registers can consequently be accessed by bytes, half-words, or words. FIFO accesses smaller than a single data are forbidden. When the FIFO occupancy flag is raised, a FIFO access greater than the FIFO threshold can lead to spurious data being read or written data being lost. When the access to/from the SPI_TXDR/SPI_RXDR registers is a multiple of the configured data size, data packing is applied automatically, while the lowest significant bits/bytes are communicated first. For more details, see Section 53.4.12: SPI data transmission and reception procedures .

Figure 768. Data alignment when data size is not equal to 8, 16 or 32 bits

DSIZE ≤ 8-bits
data is right aligned on byte
Example: DSIZE[4:0]=00011
9-bits ≤ DSIZE ≤ 16-bits
data is right aligned on half-word
Example: DSIZE[4:0]=01101
17-bits ≤ DSIZE ≤ 32-bits
data is right aligned on word
Example: DSIZE[4:0]=11010
7      43      015 14 13031 27 260
TxX X X XX XX X X X X
Rx0 0 0 00 00 0 0 0 0

MSV40473V1

53.4.10 Configuring the SPI

The configuration procedure is almost the same for the master and the slave. For specific mode setups, follow the dedicated chapters. When a standard communication must be initialized, perform these steps:

  1. Write the proper GPIO registers: configure GPIO alternate functions at MOSI, MISO, SCK, NSS, and RDY pins if applied.
  2. Write into the SPI_CFG1 and SPI_CFG2 registers and set up the proper values of all ‘not reserved’ bits and bitfields, prior to enabling the SPI, with the following exceptions:
    1. The SSOM, MASRX, SSOE, RDIOM, MBR[2:0], BPASS, MIDI[3:0], MSSI[3:0] bits are taken into account in master mode only, the MSSI[3:0] bits take effect when the SSOE bit is set, the RDIOP bit takes no effect when the RDIOM bit is not set in master mode. When the slave is configured in TI mode, the MBR[2:0] setting is also considered.
    2. UDRCFG is taken into account in slave mode only.
    3. CRCSIZE[4:0] bitfield must be configured if CRCEN is set.
    4. CPOL, CPHA, LSBFRST, SSOM, SSOE, SSIOP, SSM, RDIOP, RDIOM, MSSI, and MIDI are not required in TI mode.
    • e) Once the AFCNTR bit is set in the SPI_CFG2 register, all the SPI outputs start to be propagated onto the associated GPIO pins regardless of the peripheral enable. So, any later configuration changes of the SPI_CFG1 and SPI_CFG2 registers can affect the level of signals on these pins.
    1. 3. Write to the SPI_CR2 register to select the length of the transfer, if it is not known TSIZE must be programmed to zero.
    2. 4. Write to the SPI_CRCPOLY and into the TCRCINI, RCRCINI, and CRC33_17 bits of the SPI_CR1 register to configure the CRC polynomial and CRC calculation if needed.
    3. 5. Configure DMA streams dedicated for the SPI Tx and Rx in DMA registers if the DMA streams are used (see Section 53.4.14: Communication using DMA (direct memory addressing) ).
    4. 6. Configure SSI, HDDIR, and MASRX in the SPI_CR1 register if required.
    5. 7. Program the IOLOCK bit in the SPI_CR1 register if the configuration protection is required (for safety).

53.4.11 Enabling the SPI

It is recommended to configure and enable the SPI slave before the master sends the clock. But there is no impact if the configuration and enabling procedure is done while traffic is ongoing on the bus, assuming that the NSS signal is managed by hardware at slave or kept inactive by the slave software when the software management of the NSS signal is applied (see Section 53.4.7 ). To prevent any risk of any data underrun, all the data to be sent have to be written to the slave transmitter data register before the master starts its clocking. The SCK signal must be settled to the idle state level corresponding to the selected polarity, before the SPI slave is selected by NSS, else the following transfer may be desynchronized.

When the SPI slave is enabled at the hardware slave select management mode, all the transfers are ignored even in case of the NSS is found at active level. They are ignored until the slave detects a start of the NSS signal (its transition from nonactive to active level) just synchronizing the slave with the master. That is why the hardware management mode cannot be used when the external NSS pin is fixed. There is no such protection at the slave select software management. In this case, the SSI bit must be changed when there is no traffic on the bus and the SCK signal is at idle state level between transfers exclusively in this case.

The master in full duplex (or in any transmit-only mode) starts to communicate when the SPI is enabled, the CSTART bit is set, and the TxFIFO is not empty, or with the next write to TxFIFO.

In any master receive-only mode, the master starts to communicate and the clock starts running after the SPI is enabled and the CSTART bit is set.

For handling DMA, see Section 53.4.14 .

53.4.12 SPI data transmission and reception procedures

The SPI can transfer data at a very high communication speed. Even though the data is FIFO buffered, handling a continuous data flow by servicing data frames individually leads to an enormous CPU or DMA load, in particular when the data size is small. This potentially leads to a significant limitation of the overall system performance, as well as communication errors such as data overrun or underrun that may be raised when the system latencies in servicing requests become comparable to single data frame transfer time.

The SPI offers advanced hardware control features to prevent these issues from occurring:

The number of data frames per packet (FIFO threshold) can be configured. The complete data packet generates a FIFO occupancy event, which is then handled within a single, compact, service.

Note: Using these features is optional. Nothing prevents the application from handling the data flow frame by frame.

The following sections give more details and describe specific cases for using these advanced hardware control features to handle data transfers.

Data packet control

The data frame size (number of bits in the frame) is defined through the DSIZE bitfield of the SPI_CFG1 register. The number of data frames per packet can be configured by selecting the FIFO threshold through the FTHLV bitfield of the SPI_CFG1 register. If the threshold value is set to zero, each completed data frame raises the FIFO occupancy event. The data packet occupancy must not exceed half of the FIFO size, which depends on the SPI instance of the product. The FIFO capacity (multiple of 8 bits) is consumed according to the following rules, depending on the data frame size:

For example, if the FIFO capacity is 16 bytes, it accommodates up to five 24-bit frames. In this case, the data packet configuration must not exceed two data frames.

The SPI features two separate FIFOs to handle the reception and transmission data flow, the RxFIFO, and the TxFIFO. Their content can be handled by monitoring the occupancy

flags RXP, TXP, and DXP of the SPI_SR register according to the SPI mode (duplex or simplex):

These flags can be polled by software, or they can trigger an interrupt and/or a DMA request (if enabled through the RXPIE, TXPIE, and DXPIE bits of the SPI_EIR register or the RXDMAEN and TXDMAEN bits of the SPI_CFG1 register).

Once an occupancy flag is set, the software or the DMA must read and/or write a complete data packet before checking the flag again to verify if the next packet can be handled. This cycle can be repeated until the corresponding flag is read at zero.

Both RxFIFO and TxFIFO contents are flushed and cannot be accessed when the SPI is disabled (SPE cleared in the SPI_CR1 register).

Data packing versus data register access control

The content of the RxFIFO and TxFIFO can be accessed by reading/writing from/to the SPI data registers SPI_RXDR and SPI_TXDR, respectively. A read access from the SPI_RXDR register returns the oldest values stored in the RxFIFO that have not been read yet. A write access to the SPI_TXDR register stores the data written at the end of the send queue in the TxFIFO.

These data registers can be accessed by 8-, 16-, or 32-bit read and write CPU instructions, forced by the register address casting applied by the software. Data packing is performed automatically by hardware if the data access applied by the software is a multiple of the data size. It allows handling more than one data in parallel in a single data register access. Then the SPI operates using the lowest significant byte or half-word first. FIFO data accesses of less than the configured data size are forbidden. To avoid spurious data being read or written data being lost, a complete data packet (configured through the FIFO threshold) must be serviced when the corresponding FIFO occupancy flag is set. If the data pattern is not byte-, half-word, or word-aligned, only the valid data bits are stored right-aligned to the FIFO and transferred on the bus. Unused bits are discarded on the transmitter side and padded with zeros on the receiver side.

For example, if the data frame size fits into one byte, the data packing is used automatically when a 16-bit or 32-bit read/write access is performed by software from/to the SPI_RXDR/SPI_TXDR register. In this case, two respectively four data are handled by a single 16-bit respectively 32-bit access. Additionally, if such data frames are grouped into packets of four via the FIFO threshold setting, the packet to be serviced is signalized by raising the corresponding FIFO occupancy flag (TXP, RXP, or DXP). If the instance FIFO threshold is sufficient, a packet containing eight 8-bit data frames can be handled by two consecutive 32-bit accesses upon the same single threshold event. The most efficient data handling is achieved when the data packet size (defined by DSIZE and FTHLV bitfields) is aligned with the read/write access from/to the data registers.

Concurrent read and write services

In full-duplex mode, both TxFIFO and RxFIFO packet occupancies can be monitored through a common FIFO flag (DXP). When the DXP flag is set, the application performs the specified number of writes to SPI_TXDR to upload the content of one entire data packet for transmission, followed by the same number of reads from SPI_RXDR to download the

content of one received data packet. Once one data packet is uploaded and one packet is downloaded, the application software or the DMA checks again the DXP flag and repeats the data packet read and write operation sequence until DXP is read as zero.

The drawback of services based on DXP exclusively is that servicing the TxFIFO is delayed on purpose due to the SPI nature since the TXP events precedes the RXP ones. To allow continuous SCK clock flow on the master side and prevent underrun on the slave side, it is recommended to prefill a few data ahead to the TxFIFO at the transfer start, and wait until the transfer is complete to read the last received data.

Hardware data counter

If a hardware data counter is used (TSIZE bitfield of the SPI_CR2 register set to a nonzero value), the application software does not need to calculate the remaining number of data to be handled. The end of transfer automatically controls the CRC, as well as the hardware slave select management, when used. The user application does not need to ensure that the overall number of data is a multiple of the packet size. If the last data packet is incomplete, it is serviced in the same way as any full packet. The unused part of this last packet is not handled by the peripheral. Only the valid data are written into the TxFIFO and/or read from the RxFIFO, and the redundant writes and reads are discarded. When the hardware counter reaches zero, the EOT flag is raised, and the RXP flag is not set for the last data packet. If the last packet is not aligned with the packet size, the TXP and EOT occupancy events are not related to the configured packet size but to the number of remaining data calculated by hardware.

If TSIZE is kept at zero (for example due to an unpredictable number of data), only the number of transferred data corresponding to the FIFO thresholds is supported. If some data are not aligned with the configured packet size, they may remain pending and available in the RxFIFO. In this case, the FTHLV level is not reached and the RXP flag is not set. Then the number of remaining received data frames in the RxFIFO is indicated by the RXWNE and RXPLVL bitfields of the SPI_SR register. Nevertheless, the application software can still read the complete data packet from the RxFIFO and the redundant data are read as zero. To prevent such an unaligned data reception, the user must configure the FIFO threshold to a single data (FTHLV = 0). In this case, each data frame is serviced by its own RXP occupancy event.

Data transfer handling

Data are transferred using MOSI and MISO lines, depending on the SPI communication mode and associated configuration. The mode is configured through the COMM[1:0] bitfield of the SPI_CFG2 register. The HDDIR bit of the SPI_CR1 register controls the data flow direction in half-duplex mode. The communication flow is handled by the master via the NSS and SCK signals. The slave selected for the communication is fully subordinated to the master communication activity, no matter if it handles the data flow on time or not. The slave can only temporarily suspend the master communication by using the RDY signal. The active levels of RDY and NSS signals can be changed, or MISO and MOSI functionality swapped (via the RDIOP, SSIOP and IOSWP bits of the SPI_CFG2 register).

The SPI master transmitter can operate in full-duplex, simplex, or half-duplex mode. In full-duplex mode, data are received synchronously. The master starts the data transfer once the CSTART bit of the SPI_CR1 register is set, provided the SPI is enabled and the TxFIFO

content is not empty. The master then provides the serial clock signal continuously on the SCK pin until:

An automatic temporary suspension of the master transmission occurs when:

When an automatic suspension occurs, the master stops providing the clock, and the transfer proceeds depending on the cause of the suspension, when:

The SPI master can receive data only when it operates in simplex receiver or half-duplex receiver mode. In these modes, the master starts the data transfer when the SPI is enabled, and the transfer is released by setting the CSTART bit. The serial clock signal is then provided continuously on the SCK pin by the master until:

An automatic temporary suspension of the reception occurs when:

When an automatic suspension occurs, the master stops providing the clock and the transfer proceeds depending on the cause of the suspension, when:

A preferable way to terminate a transfer is to program the TSIZE bitfield to generate an EOT event. Hardware NSS signal or CRC handshake can then be controlled. To restart the internal state machine properly, it is recommended to disable the SPI and enable it again when the transfer is complete, even if the SPI configuration is not changed.

A transfer can be suspended at any time by setting the CSUSP bit of the SPI_CR1 register, which clears the CSTART bit. This software suspension control ensures the completion of any ongoing data frame. To restart the internal state machine properly, the SPI must be disabled and enabled again before the next transfer starts.

Oppositely, a temporary automatic suspension controlled by hardware typically results in a frozen and incomplete data frame transfer. This depends on baud rate setting, but usually, due to internal synchronization delays, the SCK signal stops when a few bits from the next data frame are already transferred on the bus. Once the suspension is released, the data frame transmission completes by transferring the remaining bits. That is why this automatic suspension is not quite reliable when the data frame size is less than eight bits. When shorter data frames are used, to prevent any data loss and assure proper RDY and/or

MASRX operation, the user can interleave and so extend the transfer time by inserting additional dummy clock cycles so that the period for a single data frame is higher or equal to eight SCK duration. This is done by setting the MIDI[3:0] bitfield of the SPI_CFG2 register.

Caution: If the SPE bit is cleared in master mode while the transfer is ongoing without any suspension, the clock is stopped even if the current frame transmission is not complete, and the content of the FIFOs is flushed and lost.

53.4.13 Disabling the SPI

To disable the SPI, it is mandatory to follow the disable procedures described in this paragraph.

In the master mode, it is important to do this before the system enters a low-power mode when the peripheral clock is stopped, otherwise, ongoing transfers may be corrupted.

In slave mode, the SPI communication can continue when the spi_pclk and spi_ker_ck clocks are stopped, without interruption, until any end of communication or data service request condition is reached. The spi_pclk can generally be stopped by setting the system into Stop mode. Refer to the RCC section for further information.

The master in full-duplex or transmit-only mode can finish any transfers when it stops providing data for transmission. In this case, the clock stops after the last data transfer. TXC flag can be polled (or interrupt enabled with EOTIE = 1) in order to wait for the last data frame to be sent.

When the master is in any receive-only mode, to stop the peripheral, the SPI communication must first be suspended, by setting the CSUSP bit.

The data received but not read remain stored in RxFIFO when the SPI is suspended.

After such a software suspension, SPI must always be disabled to restart the internal state machine properly.

When SPI is disabled, RxFIFO is flushed. To prevent losing unread data, the user must ensure that RxFIFO is empty when disabling the SPI, by reading all remaining data (as indicated by the RXP, RXWNE, and RXPLVL fields in the SPI_SR register).

The standard disable procedure is based on polling EOT and/or TXC status 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 transfers, for example:

When TSIZE > 0, EOT and TXC signals are equal so polling of EOT is reliable at whatever SPI communication mode to check the end of the bus activity. When TSIZE = 0, the user has to check TXC, SUSP, or FIFO occupancy flags according to the applied SPI mode and the way of the data flow termination.

The correct disable procedure in master mode, except when receive-only mode is used, is:

  1. 1. Wait until TXC = 1 and/or EOT = 1 (no more data to transmit and last data frame sent). When CRC is used, it is sent automatically after the last data in the block is processed.

TXC/EOT is set when the CRC frame is complete in this case. When a transmission is suspended the software has to wait until the CSTART bit is cleared.

  1. 2. Read all RxFIFO data (until RXWNE = 0 and RXPLVL = 00).
  2. 3. Disable the SPI (SPE = 0).

The correct disable procedure for master receive-only modes is:

  1. 1. Wait on EOT or break the receive flow by suspending SPI (CSUSP = 1).
  2. 2. Wait until SUSP = 1 (the last data frame is processed) if the receive flow is suspended.
  3. 3. Read all RxFIFO data (until RXWNE = 0 and RXPLVL = 00).
  4. 4. Disable the SPI (SPE = 0).

In slave mode, any ongoing data are lost when disabling the SPI.

Controlling the I/Os

As soon as the SPI is disabled, the associated and enabled AF outputs can still be driven by the device depending on the AFCNTR bit of the SPI_CFG2 register. When active output control is applied (AFCNTR = 1) and SPI has just been disabled (SPE = 0), the enabled outputs associated with SPI control signals (like NSS and SCK at master and RDY at slave) can immediately toggle to inactive level (according to SSIOP and CPOL settings at master and RDIOP at slave respectively). Instead, the data line output (MOSI at master and MISO at slave) can immediately change its level depending on the actual TxFIFO content, with the effect of potentially making invalid and no more guaranteed the value of the latest transferred bit on the bus. If necessary, the user has to take care about proper data hold time at the data line and avoid any eventual fast SPI disable just after the last data transfer is complete.

Note: Despite stability of the latest bit is guaranteed by design during the sampling edge of the clock, some devices can require even extension of this data bit stability interval during the sampling. It can be done, for example by inserting a small software delay between EOT event occurrence and SPI disable action.

53.4.14 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 TXDMAEN or RXDMAEN enable bits of the SPI_CFG1 register are set. Separate requests must be issued to the Tx and Rx buffers to fulfill the service of the defined packet.

If the SPI is programmed in receive-only mode, UDR is never set.

If the SPI is programmed in a transmit mode, TXP and UDR can be eventually set at slave side, because transmit data may not be available. In this case, some data are sent on the TX line according with the UDR management selection.

When the SPI is used at a simplex mode, the user must enable the adequate DMA channel only while keeping the complementary unused channel disabled.

If the SPI is programmed in transmit-only mode, RXP and OVR are never set.

If the SPI is programmed in full-duplex mode, RXP and OVR are eventually set, because received data are not read.

In transmission mode, when the DMA or the user has written all the data to be transmitted (the TXTF flag is set in the SPI_SR register), the EOT (or TXC at case TSIZE = 0) 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 before disabling the spi_pclk in master mode. The software must first wait until EOT = 1 and/or TXC = 1.

When starting communication using DMA, to prevent DMA channel management raising error events, these steps must be followed in order:

  1. 1. Enable the DMA Rx buffer in the RXDMAEN bit of the SPI_CFG1 register, if DMA Rx is used.
  2. 2. Enable DMA requests for Tx and Rx in DMA registers, if the DMA is used.
  3. 3. Enable the DMA Tx buffer in the TXDMAEN bit of the SPI_CFG1 register, if DMA Tx is used.
  4. 4. Enable the SPI by setting the SPE bit.

To close communication, it is mandatory to follow these steps in order:

  1. 1. Disable DMA requests for Tx and Rx in the DMA registers, if the DMA issued.
  2. 2. Disable the SPI by following the SPI disable procedure.
  3. 3. Disable DMA Tx and Rx buffers by clearing the TXDMAEN and RXDMAEN bits of the SPI_CFG1 register, if DMA Tx and/or DMA Rx are used.

Data packing with DMA

If the transfers are managed by DMA (TXDMAEN and RXDMAEN set in the SPI_CFG1 register) the packing mode is enabled/disabled automatically depending on the PSIZE value configured for SPI TX and the SPI RX DMA channel.

The packing mode is enabled if the DMA channel PSIZE value is a multiple of the data size. Then the DMA automatically manages the sequences of write and read operations to/from the SPI data registers, based on FIFO occupancy flags, and depending on the FIFO threshold and data size configurations.

The DMA completes the transfer automatically according to the TSIZE field setting, whatever the data packing mode used, and even if the number of data to transfer is not a multiple of the DMA data size (16 bits or 32 bits) while the frame size is smaller.

Alternatively, the last data frames can be written by software, in the single/unpacked mode.

Configuring any DMA data access to less than the configured data size is forbidden. One complete data frame must be always accessed at minimum.

53.4.15 Autonomous mode

The SPI is capable of handling and initializing transfers autonomously. It requires no specific system execution interaction until the ongoing transfer ends.

Such autonomous transfers can be handled not only in Run or Sleep mode but even in Stop mode when the SPI logic is able to provide temporary clock requests addressed to the reset

and clock controller (RCC) to ensure clocking of those SPI domains just necessary for handling the data flow between the memory and the peripheral interface at dependency in the SPI mode.

In Stop mode, the APB clock is requested by the peripheral each time the SPI registers need to be updated based on specific traffic events (mainly TXP and RXP). The required clock is provided by RCC if SPI autonomous mode is enabled at the RCC configuration and the SPI is clocked by an internal oscillator available in Stop mode.

Interrupts or DMA requests are then generated, depending on the SPI configuration. If no interrupt is enabled, the device remains in Stop mode. If DMA requests are enabled, the data are directly transferred to/from the SRAM thanks to the DMA while the device remains in Stop mode. If an enabled interrupt occurs, the device wakes up from Stop mode.

Note: The peripheral clock request stays pending until the flag with enabled interrupt is set. This is why it is important to service these pending requests and clear their flag as soon as possible at system sensitive to the low-power consumption especially, and the application must acknowledge all pending interrupts events before switching the SPI to low-power mode.

Slave mode

When the SPI is configured as a standard slave and the device is at Stop mode, the SPI kernel clock and the SPI APB clock are not provided permanently. All the data flow between the SPI interface and associated FIFOs is handled by an external SCK clock provided by the outer master device within the serial interface clock domain. APB clock temporal requests are then based upon specific traffic events at dependency on the SPI configuration. As the slave never initializes a transfer, there is no need to synchronize any transfer start in this mode.

Note: The peripheral clock request stays pending until the flag with enabled interrupt stays set. This is why it is important to service these pending requests and clear their flag as soon as possible to achieve the lowest power consumption, and the application must acknowledge all pending interrupt events before switching the SPI to low-power mode.

Master mode

The SPI operating in master mode provides the SCK signal for outer slaves until the transfer is complete. The SCK signal is derived from the SPI clock generator running within the kernel clock domain fed from the RCC upon kernel clock request provided by the SPI when the device is in Stop mode. Temporal requests for the APB clock are then based upon specific traffic events, depending on the SPI configuration. The SPI master always initializes a transfer.

To minimize consumption in Stop mode, it is suggested to combine communication starts triggered by hardware (the TRIGEN bit is set in the SPI_AUTOCR register) and transfers of predefined data size (TSIZE > 0 in the SPI_CR2 register). This ensures that any APB clock request is suppressed between EOT handling and the next trigger event.

A transfer starts once the CSTART bit is set. In the case of master transmitter, the TxFIFO must be filled by data, too. The CSTART bit can be written either by software or by hardware when a synchronous trigger is detected at Run, Sleep, or Stop mode. The trigger source is selected by the TRIGSEL bits and enabled by the TRIGEN bit of the SPI_AUTOCR register. When the enabled trigger is detected, the transfer starts and continues by handling data until the EOT event or the transfer suspension. When the TRIGEN bit is changed, the user must prevent any trigger event occurrence. If the user cannot prevent that, the TRIGEN bit

must be written while the SPI is disabled otherwise the peripheral behavior is not guaranteed.

53.5 SPI specific modes and control

53.5.1 TI mode

With a specific SP[2:0] bitfield setting of the SPI_CFG2 register, the SPI can be configured compliant with the TI protocol. The SCK and NSS signals polarity, phase and flow, as well as the bit order are fixed, so the setting of CPOL, CPHA, LSBFRST, SSOM, SSOE, SSIOP, SSM, RDIOP, RDIOM, MSSO, and MIDI is not required when the SPI is in TI mode configuration. The NSS signal synchronizes the protocol by pulses over the LSB data bit as it is shown in Figure 769.

Figure 769. TI mode transfer

Timing diagram for TI mode transfer showing signals NSS, SCK, MOSI, and MISO. The diagram illustrates the relationship between the slave select (NSS), serial clock (SCK), master out slave in (MOSI), and master in slave out (MISO) signals during a data transfer. The SCK signal is a periodic square wave. The MOSI signal is shown as a series of bytes, with the most significant bit (MSB) and least significant bit (LSB) indicated. The MISO signal is shown as a series of bytes, with the MSB and LSB indicated. The NSS signal is shown as a series of pulses. The diagram also indicates the data size (DSIZE[4:0] + 1) and the release time (TRELEASE) for the MISO signal.
Timing diagram for TI mode transfer showing signals NSS, SCK, MOSI, and MISO. The diagram illustrates the relationship between the slave select (NSS), serial clock (SCK), master out slave in (MOSI), and master in slave out (MISO) signals during a data transfer. The SCK signal is a periodic square wave. The MOSI signal is shown as a series of bytes, with the most significant bit (MSB) and least significant bit (LSB) indicated. The MISO signal is shown as a series of bytes, with the MSB and LSB indicated. The NSS signal is shown as a series of pulses. The diagram also indicates the data size (DSIZE[4:0] + 1) and the release time (TRELEASE) for the MISO signal.

In slave mode, the clock generator is used to define the time when the slave output at MISO pin becomes high-Z when the current transfer finishes. The master baud rate setting (MBR[2:0] of SPI_CFG1) is applied and any baud rate can be used to determine this moment with optimal flexibility. The delay for the MISO signal to become high-Z (T RELEASE ) depends on internal resynchronization, too, which takes the next additional 2-4 periods of the clock signal feeding the generator. It is given by the following formula:

\[ \frac{T_{baud}}{2} + 2 \times T_{spi\_ker\_ck} \leq T_{release} \leq \frac{T_{baud}}{2} + 4 \times T_{spi\_ker\_ck} \]

If the slave detects a misplaced NSS pulse during a data transfer the TIFRE flag is set.

53.5.2 SPI error flags

An SPI interrupt is generated if one of the following error flags is set and the interrupt is enabled by setting the corresponding interrupt enable bit.

Overrun flag (OVR)

An overrun condition occurs when data are received by a master or slave and the RxFIFO has not enough space to store these received data. This can happen if the software or the DMA did not have enough time to read the previously received data (stored in the RxFIFO).

When an overrun condition occurs, the OVR flag is set and the newly received value does not overwrite the previous one in the RxFIFO. The newly received value is discarded and all data transmitted subsequently are lost. The OVR flag triggers an interrupt if the OVRRIE bit is set. Clearing the OVR bit is done by setting the OVRC bit of the SPI_IFCR register. Clearing the RxFIFO content by performing software reads before the OVR bit is cleared reduces the risk of any immediate repetition of its next overrun. It is suggested to release the RxFIFO space as much as possible, this means to read out all the available data packets based on RXP flag indication.

In master mode, the user can prevent the RxFIFO overrun by automatic communication suspend (MASRX bit).

Underrun flag (UDR)

In a slave-transmitting mode, the underrun condition is captured internally by hardware if no data is available for transmission in the slave TxFIFO commonly. The UDR flag setting is then propagated into the status register by hardware (see note below). UDR triggers an interrupt if the UDRRIE bit is set.

Underrun detection logic and system behavior depend on the UDRCFG bit. When an underrun is detected by the slave, it can provide out either a constant pattern stored by the user at the UDRDR register or the data received previously from the master. When the first configuration (UDRCFG = 0) is applied, the underrun condition is evaluated whenever the master starts to communicate a new data frame while TxFIFO is empty. Then single additional dummy (accidental) data is always inserted between the last valid data and the constant pattern defined at the UDRDR register (see Figure 770 ). Assuming that TxFIFO is not empty when the master starts the communication, the underrun condition is evaluated just once the FIFO becomes empty during the next data flow. Valid data from TxFIFO is then appended by the last received data immediately.

The standard transmission is reenabled once the software clears the UDR flag and this clearing is propagated into SPI logic by hardware. Writing some data to the TxFIFO before the UVR bit is cleared reduces the risk of any immediate repetition of its next underrun.

The data transferred by the slave is unpredictable especially when the transfer starts or continues while TxFIFO is empty and the underrun condition is either not yet captured or just cleared. Typically, this is the case when SPI is just enabled or when a transfer with a defined size just starts. First bits can be corrupted in this case, as well, when the slave software writes the first data into the empty TxFIFO too close prior to starting the data transfer (propagation of the data into TxFIFO takes a few APB clock cycles).

Figure 770. Optional configurations of the slave behavior when an underrun condition is detected

Timing diagrams for SPI slave underrun behavior in two configurations: UDRCFG=0 and UDRCFG=1. Both show SCK, MOSI, MISO, Tx FIFO occupancy, and UDR signals over time. In UDRCFG=0, an underrun occurs after DO1, leading to a dummy byte and then [UDRDR]. In UDRCFG=1, an underrun occurs after DO1, leading to DI1 and then DI2. The UDR signal is shown with propagation latency in both cases.

The diagram illustrates two optional configurations for slave behavior during an underrun condition in SPI. Both configurations show the relationship between the Serial Clock (SCK), Master In Slave Out (MOSI), Master Out Slave In (MISO), Transmit FIFO occupancy, and the Underrun (UDR) flag.

UDRCFG=0

UDRCFG=1

MSV63460V1

Timing diagrams for SPI slave underrun behavior in two configurations: UDRCFG=0 and UDRCFG=1. Both show SCK, MOSI, MISO, Tx FIFO occupancy, and UDR signals over time. In UDRCFG=0, an underrun occurs after DO1, leading to a dummy byte and then [UDRDR]. In UDRCFG=1, an underrun occurs after DO1, leading to DI1 and then DI2. The UDR signal is shown with propagation latency in both cases.

Note: The hardware propagation of an UDR event needs additional traffic on the bus. It always takes a few extra SPI clock cycles after the event happens (both underrun captured by hardware and cleared by software). If clearing of the UDR flag by software is applied close to the end of the data frame transfer or when the SCK line is at idle in between the frames, the next extra underrun pattern is sent initially by the slave before the valid data from TxFIFO becomes transferred again. The user can prevent this by SPI disable/enable action between sessions to restart the underrun logic and so initiate the next session by the valid data.

Mode fault (MODF)

Mode fault occurs when the master device has its internal NSS signal (NSS pin in slave select hardware mode, or SSI bit in slave select software mode) pulled low. This automatically affects the SPI interface in the following ways:

MODF is cleared by writing 1 to the MODFC bit of the SPI_IFCR register.

To avoid any multiple slave conflicts in a system comprising several MCUs, the NSS pin must be pulled to its nonactive level before reenabling the SPI, by setting the SPE bit.

As a security, the hardware does not allow the SPE bit 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.

A correct software procedure when a master overtakes the bus in multimaster systems must be the following one:

  1. 1. Switch into master mode while SSOE = 0 (potential conflict can appear when another master occupies the bus. In this case, MODF is raised, which prevents any next node switching into master mode).
  2. 2. Put GPIO pin dedicated for another master slave select control into active level.
  3. 3. Perform a data transfer.
  4. 4. Put GPIO pin dedicated for another master slave select control into nonactive level.
  5. 5. Switch back to slave mode.

CRC error (CRCE)

This flag is used to verify the validity of the value received when the CRCEN bit of the SPI_CFG1 register is set. The CRCE flag of the SPI_SR register is set if the value received in the shift register does not match the receiver SPI_RXCRC value, after the last data is received (as defined by TSIZE). The CRCE flag triggers an interrupt if the CRCEIE bit is set. Clearing the bit CRCE is done by a writing 1 to the CRCEC bit of the SPI_IFCR register.

TI mode frame format error (TIFRE)

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 TIFRE flag is set in the SPI_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 a few data frames.

The TIFRE flag is cleared by writing 1 to the TIFREC bit of the SPI_IFCR register. If the TIFREIE bit is set, an interrupt is generated on the NSS error detection. As data consistency is no longer guaranteed, communication must be reinitiated by software between master and slave.

53.5.3 CRC computation

Two separate CRC calculators are implemented to check the reliability of transmitted and received data. The CRC polynomial configuration depends on the instance. Refer to Section 53.3: SPI implementation for more information.

The length of the polynomial is defined by the most significant bit of the value stored in the SPI_CRCPOLY register. It must be greater than the data frame size (in bits) defined in the DSIZE[4:0] bitfield of the SPI_CFG1 register. To obtain a full-size polynomial, the polynomial length must exceed the maximum data size of the peripheral instance, and the CRC33_17 bit of the SPI_CR1 register must be set to select the most significant bit of the polynomial string.

For example, to select the standard CRC16-CCITT (XMODEM) polynomial \( x^{16} + x^{12} + x^5 + 1 \) :

The CRCSIZE field of the SPI_CFG1 register then defines how many most significant bits from the CRC calculation registers are transferred and compared as CRC frame. It is defined independently from the data frame length, but it must be either equal or an integer multiple of the data frame size. Its size cannot exceed the maximum data size of the instance.

To fully benefit from the CRC calculation capability, the polynomial length setting must correspond to the CRC frame size, else the bits unused at the calculation are transferred and expected all zero at the end of the CRC frame if its size is set greater than the polynomial length.

CRC principle

The CRC calculation is enabled by setting the CRCEN bit of the SPI_CFG1 register before the SPI is enabled (SPE = 1). The CRC value is then calculated using the CRC polynomial defined by the CRCPOLY register and CRC33_17 bit. When SPI is enabled, the CRC polynomial can be changed but only in the case when there is no traffic on the bus.

The CRC computation is done, bit by bit, on the sampling clock edge defined by the CPHA and CPOL bits of the SPI_CR1 register. The calculated CRC value is checked automatically at the end of the data block defined by the SPI_CR2 register exclusively.

When a mismatch is detected between the CRC calculated internally on the received data and the CRC received from the transmitter, a CRCE flag is set to indicate a data corruption error. The right procedure for handling the CRC depends on the SPI configuration and the chosen transfer management.

CRC transfer management

Communication starts and continues normally until the last data frame has been sent or received in the SPI_DR register.

The length of the transfer must be defined by TSIZE. When the desired number of data is transferred, the TXCRC is transmitted and the data received on the line are compared to the RXCRC value.

Whatever the CRCSIZE configuration, TSIZE cannot be set to 0xFFFF for a full-featured instance, and to 0x3FF for a limited-featured instance, if CRC is enabled.

In transmission, the CRC computation is frozen during the CRC transfer and the TXCRC is transmitted, in a frame of length equal to the CRCSIZE field value.

In reception, the RXCRC is also frozen when the desired number of data is transferred. Information to be compared with the RXCRC register content is then received in a frame of length equal to the CRCSIZE value.

Once the CRC frame is complete, an automatic check is performed comparing the received CRC value and the value calculated in the SPI_RXCRC register. The software has to check the CRCE flag of the SPI_SR register to determine if the data transfers were corrupted or not. Software clears the CRCE flag by writing 1 to the CRCEC.

The user takes no care about any flushing redundant CRC information, it is done automatically.

Resetting the SPI_TXCRC and SPI_RXCRC values

The SPI_TXCRC and SPI_RXCRC values are initialized automatically when new data is sampled after a CRC phase. This allows the use of DMA circular mode to transfer data without any interruption (several data blocks covered by intermediate CRC checking phases). Initialization patterns for receiver and transmitter can be configured either to zero or to all ones in dependency on setting bits TCRCINI and RCRCINI of the SPI_CR1 register.

The CRC values are reset when the SPI is disabled.

53.6 SPI in low-power modes

The SPI supports autonomous operation down to Stop mode, refer to Section 53.4.15: Autonomous mode .

Table 582. Effect of low-power modes on the SPI

ModeDescription
SleepNo effect. SPI interrupts cause the device to exit Sleep mode.
Stop (1)The content of the SPI registers is kept.
If the autonomous mode is enabled at RCC configuration, and the SPI is clocked by an internal oscillator available in Stop mode, transfers are functional. The DMA requests are functional, and the interrupts in this mode cause the device to exit Stop mode.
StandbyThe SPI instance is not functional in this mode. It is powered down, and must be reinitialized after exiting Standby mode.
  1. 1. Refer to Section 53.3: SPI implementation for information about wake-up from Stop mode support per instance as well as Standby mode availability. If an instance is not functional in a Stop mode, it must be disabled before entering this Stop mode.

53.7 SPI interrupts

Table 583 gives an overview of the SPI events capable of generating interrupts if enabled. Some of them feature wake-up from low-power mode capability, additionally. Most of them can be enabled and disabled independently while using specific interrupt enable control bits. The flags associated with the events are cleared by specific methods. Refer to the description of SPI registers for more details about the event flags. When SPI is disabled, all

the pending interrupt requests are blocked to prevent their propagation into the interrupt services, except the MODF interrupt request.

Table 583. SPI wake-up and interrupt requests

Interrupt vectorInterrupt eventEvent flagEnable Control bitEvent clear methodExit from Stop and Standby modes capability (1)(2)
SPITxFIFO ready to be loaded (space available for one data packet - FIFO threshold)TXPTXPIETXP cleared by hardware when TxFIFO contains less than FTHLV empty locationsYes
Data received in RxFIFO (one data packet available - FIFO threshold)RXPRXPIERXP cleared by hardware when RxFIFO contains less than FTHLV samplesYes
Both TXP and RXP activeDXPDXPIEWhen TXP or RXP are clearedYes
Transmission Transfer FilledTXTFTXTFIEWriting TXTFC to 1No
UnderrunUDRUDRIEWriting UDRC to 1Yes
OverrunOVROVRRIEWriting OVRC to 1Yes
CRC ErrorCRCECRCEIEWriting CRCEC to 1Yes
TI Frame Format ErrorTIFRETIFREIEWriting TIFREC to 1No
Mode FaultMODFMODFIEWriting MODFC to 1No
End Of Transfer (full transfer sequence completed - based on TSIZE value)EOTEOTIEWriting EOTC to 1Yes
Master mode suspendedSUSPWriting SUSPC to 1Yes
TxFIFO transmission complete (TxFIFO empty)TXC (3)TXC cleared by hardware when a transmission activity starts on the busNo
  1. 1. All the interrupt events can wake up the system from Sleep mode at each instance. For detailed information about instances capabilities to exit from concrete Stop and Standby mode refer to the Functionalities depending on the working mode table.
  2. 2. Refer to Section 53.3: SPI implementation for information about Standby mode availability.
  3. 3. The TXC flag behavior depends on the TSIZE setting. When TSIZE>0, the flag fully follows the EOT one including its clearing by EOTC.

53.8 SPI registers

53.8.1 SPI control register 1 (SPI_CR1)

Address offset: 0x000

Reset value: 0x0000 0000

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.IOLOCK
rs
1514131211109876543210
TCRCINIRCRCINICRC33_17SSIHDDIRCSUSPCSTARTMASRXRes.Res.Res.Res.Res.Res.Res.SPE
rwrwrwrwrwwrsrwrw

Bits 31:17 Reserved, must be kept at reset value.

Bit 16 IOLOCK : locking the AF configuration of associated I/Os

This bit can be set by software only when SPI is disabled (SPE = 0). It is cleared by hardware exclusively whenever the SPE bit is changed from 1 to 0, either by hardware or software.

When this bit is set, the SPI_CFG2 register content cannot be modified. This bit is write-protected when SPI is enabled (SPE = 1).

0: AF configuration is not locked

1: AF configuration is locked

Bit 15 TCRCINI : CRC calculation initialization pattern control for transmitter

0: all zero pattern is applied

1: all ones pattern is applied

Bit 14 RCRCINI : CRC calculation initialization pattern control for receiver

0: All zero pattern is applied

1: All ones pattern is applied

Bit 13 CRC33_17 : Full size (33-bit or 17-bit) CRC polynomial configuration

0: Full size (33-bit or 17-bit) CRC polynomial is not used

1: Full size (33-bit or 17-bit) CRC polynomial is used

Bit 12 SSI : Internal slave select signal input level

This bit has an effect only when the SSM bit is set. The value of this bit is forced onto the peripheral NSS input internally and the I/O value of the NSS pin is ignored.

Bit 11 HDDIR : Rx/Tx direction at half-duplex mode

In half-duplex configuration the HDDIR bit establishes the Rx/Tx direction of the data transfer. This bit is ignored in Full-Duplex or any Simplex configuration.

0: SPI is receiver

1: SPI is transmitter

Bit 10 CSUSP: master suspend request

This bit reads as zero.

In master mode, when this bit is set by software, the CSTART bit is reset at the end of the current frame and communication is suspended. The user has to check SUSP flag to check end of the frame transfer.

The master mode communication must be suspended (using this bit or keeping TXDR empty) before going to Low-power mode.

After software suspension, SUSP flag must be cleared and SPI disabled and re-enabled before the next transfer starts.

Bit 9 CSTART: master transfer start

This bit can be set by software if SPI is enabled only to start an SPI communication. it is cleared by hardware when end of transfer (EOT) flag is set or when a transfer suspend request is accepted.

In SPI mode, the bit is taken into account at master mode only. If transmission is enabled, communication starts or continues only if any data is available in the transmission FIFO.

0: master transfer is at idle

1: master transfer is ongoing or temporary suspended by automatic suspend

Bit 8 MASRX: master automatic suspension in Receive mode

This bit is set and cleared by software to control continuous SPI transfer in master receiver mode and automatic management in order to avoid overrun condition.

When SPI communication is suspended by hardware automatically, it may happen that few bits of next frame are already clocked out due to internal synchronization delay.

This is why, the automatic suspension is not quite reliable when size of data drops below 8 bits. In this case, a safe suspension can be achieved by combination with delay inserted between data frames applied when MIDI parameter keeps a non zero value; sum of data size and the interleaved SPI cycles must always produce interval at length of 8 SPI clock periods at minimum. After software clearing of the SUSP bit, the communication resumes and continues by subsequent bits transfer without any next constraint. Before the SUSP bit is cleared, the user must release the RxFIFO space as much as possible by reading out all the data packets available at RxFIFO based on the RXP flag indication to prevent any subsequent suspension.

0: SPI flow/clock generation is continuous, regardless of overrun condition. (data are lost)

1: SPI flow is suspended temporary on RxFIFO full condition, before reaching overrun condition. The SUSP flag is set when the SPI communication is suspended.

Bits 7:1 Reserved, must be kept at reset value.

Bit 0 SPE: serial peripheral enable

This bit is set by and cleared by software.

When SPE = 1, SPI data transfer is enabled, the SPI_CFG1 and SPI_CFG2 configuration registers, CRCPOLY, UDRDR, part of the SPI_AUTOCR register and IOLOCK bit in the SPI_CR1 register are write protected. They can be changed only when SPE = 0.

When SPE = 0 any SPI operation is stopped and disabled, all the pending requests of the events with enabled interrupt are blocked except the MODF interrupt request (but their pending still propagates the request of the spi_plck clock), the NSS output is deactivated at master, the RDY signal keeps not ready status at slave, the internal state machine is reseted, all the FIFOs content is flushed, CRC calculation initialized, receive data register is read zero. SPE is cleared and cannot be set when MODF error flag is active.

0: Serial peripheral disabled.

1: Serial peripheral enabled

53.8.2 SPI control register 2 (SPI_CR2)

Address offset: 0x004

Reset value: 0x0000 0000

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.
1514131211109876543210
TSIZE[15:0]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

Bits 31:16 Reserved, must be kept at reset value.

Bits 15:0 TSIZE[15:0] : number of data at current transfer

When these bits are changed by software, the SPI must be disabled.

Endless transfer is initialized when CSTART is set while zero value is stored in TSIZE. TSIZE cannot be set to 0xFFFF for a full-featured instance and 0x3FF for a limited-featured instance, when CRC is enabled.

Note: TSIZE[15:10] bits are reserved for limited-featured instances, and must be kept at reset value.

53.8.3 SPI configuration register 1 (SPI_CFG1)

Address offset: 0x008

Reset value: 0x0007 0007

The content of this register is write protected when SPI is enabled.

31302928272625242322212019181716
BPASSMBR[2:0]Res.Res.Res.Res.Res.RCRCENRes.CRCSIZE[4:0]
rwrwrwrwrwrwrwrwrwrw
1514131211109876543210
TXDMA ENRXDMA ENRes.Res.Res.Res.UDRCF GFTHLV[3:0]DSIZE[4:0]
rwrwrwrwrwrwrwrwrwrwrwrw

Bit 31 BPASS : bypass of the prescaler at master baud rate clock generator

0: bypass is disabled

1: bypass is enabled

Bits 30:28 MBR[2:0] : master baud rate prescaler setting

Note: MBR setting is considered at slave working at TI mode, too (see Section 53.5.1: TI mode ).

Bits 27:23 Reserved, must be kept at reset value.

Bit 22 CRCCEN : hardware CRC computation enable

Bit 21 Reserved, must be kept at reset value.

Bits 20:16 CRCSIZE[4:0] : length of CRC frame to be transferred and compared

Most significant bits are taken into account from polynomial calculation when CRC result is transferred or compared. The length of the polynomial is not affected by this setting. The value must be equal or a multiple of the data size configured through the DSIZE bitfield. Its maximum size corresponds to the instance maximum data size.

Note: The most significant bit at CRCSIZE bit field is reserved for the peripheral instances where data size is limited to 16 bits.

The CRCSIZE[2:0] bits are fixed to 1 for the peripheral instances with a limited set of features.

Bit 15 TXDMAEN : Tx DMA stream enable

Bit 14 RXDMAEN : Rx DMA stream enable

Bits 13:10 Reserved, must be kept at reset value.

Bit 9 UDRCFG : behavior of slave transmitter at underrun condition

For more details see Figure 770: Optional configurations of the slave behavior when an underrun condition is detected .

0: slave sends a constant pattern defined by the user in the SPI_UDRDR register

1: Slave repeats the last received data from master. When slave is configured at transmit only mode (COMM[1:0] = 01), all zeros pattern is repeated.

Bits 8:5 FTHLV[3:0] : FIFO threshold level

Defines number of data frames in a single data packet. It is recommended that the size of the packet does not exceed 1/2 of FIFO space.

SPI interface is more efficient if configured packet sizes are aligned with data register access parallelism:

–If SPI data register is accessed as a 16-bit register and DSIZE \( \leq \) 8 bits, better to select FTHLV = 2, 4, 6.

–If SPI data register is accessed as a 32-bit register and DSIZE \( > \) 8 bits, better to select FTHLV = 2, 4, 6, while if DSIZE \( \leq \) 8 bits, better to select FTHLV = 4, 8, 12.

0000: 1-data

0001: 2-data

0010: 3-data

0011: 4-data

0100: 5-data

0101: 6-data

0110: 7-data

0111: 8-data

1000: 9-data

1001: 10-data

1010: 11-data

1011: 12-data

1100: 13-data

1101: 14-data

1110: 15-data

1111: 16-data

Note: FTHLV[3:2] bits are reserved for instances with a limited set of features.

Bits 4:0 DSIZE[4:0] : number of bits in a single SPI data frame

00000: not used

00001: not used

00010: not used

00011: 4 bits

00100: 5 bits

00101: 6 bits

00110: 7 bits

00111: 8 bits

.....

11101: 30 bits

11110: 31 bits

11111: 32 bits

Note: The most significant bit DSIZE[4] is reserved for instances which data size is limited to 16 bits.

DSIZE[2:0] bits are fixed to 1 for instances with a limited set of features.

53.8.4 SPI configuration register 2 (SPI_CFG2)

Address offset: 0x00C

Reset value: 0x0000 0000

The content of this register is write-protected when SPI is enabled or the IOLOCK bit is set in the SPI_CR1 register.

31302928272625242322212019181716
AFCNTRSSOMSSOESSIOPRes.SSMCPOLCPHALSBFRSTMASTERSP[2:0]COMM[1:0]Res.
rwrwrwrwrwrwrwrwrwrwrwrwrwrw

1514131211109876543210
IOSWPRDIOPRDIOMRes.Res.Res.Res.Res.MIDI[3:0]MSSI[3:0]
rwrwrwrwrwrwrwrwrwrwrw

Bit 31 AFCNTR: alternate function GPIOs control

This bit is taken into account when SPE = 0 only.

When SPI must be disabled temporary for a specific configuration reason (for example CRC reset, CPHA or HDDIR change) setting this bit prevents any glitches on the associated outputs configured at alternate function mode by keeping them forced at state corresponding the current SPI configuration.

0: The peripheral takes no control of GPIOs while it is disabled

1: The peripheral keeps always control of all associated GPIOs

Bit 30 SSOM: NSS output management in master mode

This bit is taken into account in master mode when SSOE is enabled. It allows the NSS output to be configured between two consecutive data transfers.

0: NSS is kept at active level until data transfer is complete, it becomes inactive with EOT flag

1: SPI data frames are interleaved with NSS nonactive pulses when MIDI[3:0] > 1

Bit 29 SSOE: NSS output enable

This bit is taken into account in master mode only

0: NSS output is disabled and the SPI can work in multimaster configuration

1: NSS output is enabled. The SPI cannot work in a multimaster environment. It forces the NSS pin at inactive level after the transfer is complete or SPI is disabled with respect to SSOM, MIDI, MSSI, SSIOP bits setting

Bit 28 SSIOP: NSS input/output polarity

0: low level is active for NSS signal

1: high level is active for NSS signal

Bit 27 Reserved, must be kept at reset value.

Bit 26 SSM: Software management of internal slave select signal input

0: Input value of the internal slave select signal is determined by the external NSS hardware pin

1: Input value of the internal slave select signal is determined by the SSI bit controlled by software

Note: When master uses hardware NSS output (SSM = 0 and SSOE = 1), the internal slave select signal input is forced to not active state internally to prevent master mode fault error.

  1. Bit 25 CPOL : clock polarity
    0: SCK signal is at 0 when idle
    1: SCK signal is at 1 when idle
  2. Bit 24 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
  3. Bit 23 LSBFRST : data frame format
    0: MSB transmitted first
    1: LSB transmitted first
  4. Bit 22 MASTER : SPI master
    0: SPI slave
    1: SPI master
  5. Bits 21:19 SP[2:0] : serial protocol
    000: SPI Motorola
    001: SPI TI
    others: reserved, must not be used
  6. Bits 18:17 COMM[1:0] : SPI Communication Mode
    00: full-duplex
    01: simplex transmitter
    10: simplex receiver
    11: half-duplex
  7. Bit 16 Reserved, must be kept at reset value.
  8. Bit 15 IOSWP : swap functionality of MISO and MOSI pins
    When this bit is set, the function of MISO and MOSI pins alternate functions are inverted.
    Original MISO pin becomes MOSI and original MOSI pin becomes MISO.
    0: no swap
    1: MOSI and MISO are swapped
  9. Bit 14 RDIOP : RDY signal input/output polarity
    0: high level of the signal means the slave is ready for communication
    1: low level of the signal means the slave is ready for communication
  10. Bit 13 RDIOM : RDY signal input/output management
    0: RDY signal is defined internally fixed as permanently active (RDIOP setting has no effect)
    1: RDY signal is overtaken from alternate function input (at master case) or output (at slave case) of the dedicated pin (RDIOP setting takes effect)
    Note: When DSIZE in the SPI_CFG1 register is configured shorter than 8 bits, the RDIOM bit must be kept at zero.
  11. Bits 12:8 Reserved, must be kept at reset value.
  12. Bits 7:4 MIDI[3:0] : Master Inter-Data Idleness
    Specifies minimum time delay (expressed in SPI clock cycles periods) inserted between two consecutive data frames in master mode.
    0000: no delay
    0001: 1 clock cycle period delay
    ...
    1111: 15 clock cycle periods delay
    Note: This feature is not supported in TI mode.

Bits 3:0 MSSI[3:0] : Master NSS Idleness

Specifies an extra delay, expressed in number of SPI clock cycle periods, inserted additionally between active edge of NSS opening a session and the beginning of the first data frame of the session in master mode when SSOE is enabled.

0000: no extra delay

0001: 1 clock cycle period delay added

...

1111: 15 clock cycle periods delay added

Note: This feature is not supported in TI mode.

To include the delay, the SPI must be disabled and re-enabled between sessions.

53.8.5 SPI interrupt enable register (SPI_IER)

Address offset: 0x010

Reset value: 0x0000 0000

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.
1514131211109876543210
Res.Res.Res.Res.Res.Res.MODFIETIFREIECRCEIEOVRIEUDRIETXTFIEEOPIEDXPIETXPIERXPIE
rwrwrwrwrwrwrwrwrwrw

Bits 31:10 Reserved, must be kept at reset value.

Bit 9 MODFIE : mode Fault interrupt enable

0: MODF interrupt disabled

1: MODF interrupt enabled

Bit 8 TIFREIE : TIFRE interrupt enable

0: TIFRE interrupt disabled

1: TIFRE interrupt enabled

Bit 7 CRCEIE : CRC error interrupt enable

0: CRC interrupt disabled

1: CRC interrupt enabled

Bit 6 OVRIE : OVR interrupt enable

0: OVR interrupt disabled

1: OVR interrupt enabled

Bit 5 UDRIE : UDR interrupt enable

0: UDR interrupt disabled

1: UDR interrupt enabled

Bit 4 TXTFIE : TXTF interrupt enable

0: TXTF interrupt disabled

1: TXTF interrupt enabled

Bit 3 EOPIE : EOT, SUSP and TXC interrupt enable

0: EOT/SUSP/TXC interrupt disabled

1: EOT/SUSP/TXC interrupt enabled

Bit 2 DXPIE : DXP interrupt enabled

DXPIE is set by software and cleared by TXTF flag set event.

0: DXP interrupt disabled

1: DXP interrupt enabled

Bit 1 TXPIE : TXP interrupt enable

TXPIE is set by software and cleared by TXTF flag set event.

0: TXP interrupt disabled

1: TXP interrupt enabled

Bit 0 RXPIE : RXP interrupt enable

0: RXP interrupt disabled

1: RXP interrupt enabled

53.8.6 SPI status register (SPI_SR)

Address offset: 0x014

Reset value: 0x0000 1002

All the flags of this register are not cleared automatically when the SPI is reenabled. They require specific clearing access exclusively via the flag clearing register as noted in the bits descriptions below.

31302928272625242322212019181716
CTSIZE[15:0]
rrrrrrrrrrrrrrrr
1514131211109876543210
RXWNERXPLVL[1:0]TXCSUSPRes.MODFTIFRECRCEOVRUDRTXTFEOTDXPTXPRXP
rrrrrrrrrrrrrrr

Bits 31:16 CTSIZE[15:0] : number of data frames remaining in current TSIZE session

The value is not quite reliable when traffic is ongoing on bus or during autonomous operation in low-power mode.

Note: CTSIZE[15:0] bits are not available in instances with limited set of features.

Bit 15 RXWNE : RxFIFO word not empty

0: less than four bytes of RxFIFO space is occupied by data

1: at least four bytes of RxFIFO space is occupied by data

Note: This bit value does not depend on DSIZE setting and keeps together with RXPLVL[1:0] information about RxFIFO occupancy by residual data.

Bits 14:13 RXPLVL[1:0] : RxFIFO packing level

When RXWNE = 0 and data size is set up to 16 bits, the value gives number of remaining data frames persisting at RxFIFO.

When data size is greater than 16 bits, these bits are always read as 00. As a result, the single data frame received in the FIFO cannot be detected neither by RWNE nor by RXPLVL bits if the data size is set from 17 to 24 bits. The user must then apply other methods to detect the number of data received, such as monitor the EOT event when TSIZE > 0 or RXP events when FTHLV = 0.

00: no next frame is available at RxFIFO

01: 1 frame is available

10: 2 frames are available*

11: 3 frames are available*

Note: (*) Possible value when data size is set up to 8 bits only.

Bit 12 TXC : TxFIFO transmission complete

The flag behavior depends on TSIZE setting.

When TSIZE = 0, the TXC is changed by hardware exclusively and it raises each time the TxFIFO becomes empty and there is no activity on the bus.

If TSIZE ≠ 0 there is no specific reason to monitor TXC as it just copies the EOT flag value including its software clearing. The TXC generates an interrupt when EOTIE is set.

This flag is set when SPI is reset or disabled.

0: current data transfer is still ongoing, data is available in TxFIFO or last frame transmission is on going.

1: last TxFIFO frame transmission complete

Bit 11 SUSP : suspension status

In master mode, SUSP is set by hardware either as soon as the current frame is complete after CSUSP request is done or at master automatic suspend receive mode (the MASRX bit is set in the SPI_CR1 register) on RxFIFO full condition.

SUSP generates an interrupt when EOTIE is set.

This bit must be cleared prior to disabling the SPI. This is done by setting the SUSPC bit of SPI_IFCR exclusively.

0: SPI not suspended (master mode active or other mode).

1: master mode is suspended (current frame completed).

Bit 10 Reserved, must be kept at reset value.

Bit 9 MODF : mode fault

When MODF is set, SPE and IOLOCK bits of the SPI_CR1 register are reset and setting SPE again is blocked until MODF is cleared.

This bit is cleared by writing 1 to the MODFC bit of the SPI_IFCR exclusively.

0: no mode fault

1: mode fault detected.

Bit 8 TIFRE : TI frame format error

This bit is cleared by writing 1 to the TIFREC bit of the SPI_IFCR exclusively.

0: no TI Frame Error

1: TI frame error detected

Bit 7 CRCE : CRC error

This bit is cleared when SPI is re-enabled or by writing 1 to the CRCEC bit of the SPI_IFCR optionally.

0: no CRC error

1: CRC error detected

Bit 6 OVR: overrun

This bit is cleared when SPI is re-enabled or by writing 1 to the OVRC bit of the SPI_IFCR optionally.

0: no overrun

1: overrun detected

Bit 5 UDR: underrun

This bit is cleared when SPI is re-enabled or by writing 1 to the UDRC bit of the SPI_IFCR optionally.

0: no underrun

1: underrun detected

Note: In SPI mode, the UDR flag applies to slave mode only.

Bit 4 TXTF: transmission transfer filled

TXTF is set by hardware as soon as all of the data packets in a transfer have been submitted for transmission by application software or DMA, that is when TSIZE number of data have been pushed into the TxFIFO.

This bit is cleared by software write 1 to the TXTFC bit of the SPI_IFCR exclusively.

The TXTF flag triggers an interrupt if the TXTFIE bit is set.

TXTF setting clears the TXPIE and DXPIE masks so to off-load application software from calculating when to disable TXP and DXP interrupts.

0: upload of TxFIFO is ongoing or not started

1: TxFIFO upload is finished

Bit 3 EOT: end of transfer

EOT is set by hardware as soon as a full transfer is complete, that is when SPI is re-enabled or when TSIZE number of data have been transmitted and/or received on the SPI. EOT is cleared when SPI is re-enabled or by writing 1 to the EOTC bit of the SPI_IFCR register optionally.

EOT flag triggers an interrupt if the EOTIE bit is set.

If DXP flag is used until TXTF flag is set and DXPIE is cleared, EOT can be used to download the last packets contained into RxFIFO in one-shot.

In master, EOT event terminates the data transfer and handles NSS output optionally. When CRC is applied, the EOT event is extended over the CRC frame transfer.

To restart the internal state machine properly, SPI is strongly suggested to be disabled and re-enabled before next transfer starts despite its setting is not changed.

0: transfer is ongoing or not started

1: transfer complete

Bit 2 DXP: duplex packet

DXP flag is set whenever both TXP and RXP flags are set regardless SPI mode.

0: TxFIFO is Full and/or RxFIFO is Empty

1: both TxFIFO has space for write and RxFIFO contains for read a single packet at least

Bit 1 TXP: Tx-packet space available

TXP flag can be changed only by hardware. Its value depends on the physical size of the FIFO and its threshold (FTHLV[3:0]), data frame size (DSIZE[4:0] in SPI mode), and actual communication flow. If the data packet is stored by performing consecutive write operations to SPI_TXDR, TXP flag must be checked again once a complete data packet is stored at TxFIFO. TXP is set despite SPI TxFIFO becomes inaccessible when SPI is reset or disabled.

0: not enough free space at TxFIFO to host next data packet

1: enough free space at TxFIFO to host at least one data packet

Bit 0 RXP : Rx-packet available

The flag is changed by hardware. It monitors the total number of data currently available at RxFIFO if SPI is enabled. RXP value depends on the FIFO threshold (FTHLV[3:0]), data frame size (DSIZE[4:0] in SPI mode), and actual communication flow. If the data packet is read by performing consecutive read operations from SPI_RXDR, RXP flag must be checked again once a complete data packet is read out from RxFIFO.

0: RxFIFO is empty or an incomplete data packet is received

1: RxFIFO contains at least one data packet

53.8.7 SPI interrupt/status flags clear register (SPI_IFCR)

Address offset: 0x018

Reset value: 0x0000 0000

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.
1514131211109876543210
Res.Res.Res.Res.SUSPCRes.MODFCTIFRECCRCECOVRCUDRCTXTFCEOTCRes.Res.Res.
wwwwwwww

Bits 31:12 Reserved, must be kept at reset value.

Bit 11 SUSPC : Suspend flag clear

Writing a 1 into this bit clears SUSP flag of the SPI_SR register

Bit 10 Reserved, must be kept at reset value.

Bit 9 MODFC : mode fault flag clear

Writing a 1 into this bit clears MODF flag of the SPI_SR register

Bit 8 TIFREC : TI frame format error flag clear

Writing a 1 into this bit clears TIFRE flag of the SPI_SR register

Bit 7 CRCEC : CRC error flag clear

Writing a 1 into this bit clears CRCE flag of the SPI_SR register

Bit 6 OVRC : overrun flag clear

Writing a 1 into this bit clears OVR flag of the SPI_SR register

Bit 5 UDRC : underrun flag clear

Writing a 1 into this bit clears UDR flag of the SPI_SR register

Bit 4 TXTFC : transmission transfer filled flag clear

Writing a 1 into this bit clears TXTF flag of the SPI_SR register

Bit 3 EOTC : end of transfer flag clear

Writing a 1 into this bit clears EOT flag of the SPI_SR register

Bits 2:0 Reserved, must be kept at reset value.

53.8.8 SPI autonomous mode control register (SPI_AUTOCR)

Address offset: 0x01C

Reset value: 0x0000 0000

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.TRIGENTRIGPOLTRIGSEL[3:0]
rwrwrwrwrwrw

1514131211109876543210
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.

Bits 31:22 Reserved, must be kept at reset value.

Bit 21 TRIGEN : Hardware control of CSTART triggering enable

0: Hardware control disabled
1: Hardware control enabled

Note: if user cannot prevent trigger event during write, the TRIGEN must be changed when SPI is disabled

Bit 20 TRIGPOL : Trigger polarity

0: trigger is active on raising edge
1: trigger is active on falling edge

Note: This bit can be written only when SPE = 0.

Bits 19:16 TRIGSEL[3:0] : Trigger selection (refer Description of SPI interconnections ).

0000: spi_trg0 is selected
0001: spi_trg1 is selected
...
1111: spi_trg15 is selected

Note: these bits can be written only when SPE = 0.

Bits 15:0 Reserved, must be kept at reset value.

53.8.9 SPI transmit data register (SPI_TXDR)

Address offset: 0x020

Reset value: 0x0000 0000

31302928272625242322212019181716
TXDR[31:16]
wwwwwwwwwwwwwwww

1514131211109876543210
TXDR[15:0]
wwwwwwwwwwwwwwww

Bits 31:0 TXDR[31:0] : transmit data register

The register serves as an interface with TxFIFO. A write to it accesses TxFIFO.

Note: data is always right-aligned. Unused bits are ignored when writing to the register, and read as zero when the register is read.

Note: DR can be accessed byte-wise (8-bit access): in this case only one data-byte is written by single access.

half-word-wise (16 bit access) in this case 2 data-bytes or 1 half-word-data can be written by single access.

word-wise (32 bit access). In this case 4 data-bytes or 2 half-word-data or word-data can be written by single access.

Write access of this register less than the configured data size is forbidden.

53.8.10 SPI receive data register (SPI_RXDR)

Address offset: 0x030

Reset value: 0x0000 0000

31302928272625242322212019181716
RXDR[31:16]
rrrrrrrrrrrrrrrr
1514131211109876543210
RXDR[15:0]
rrrrrrrrrrrrrrrr

Bits 31:0 RXDR[31:0] : receive data register

The register serves as an interface with RxFIFO. When it is read, RxFIFO is accessed.

Note: data is always right-aligned. Unused bits are read as zero when the register is read. Writing to the register is ignored.

Note: DR can be accessed byte-wise (8-bit access): in this case only one data-byte is read by single access

half-word-wise (16 bit access) in this case 2 data-bytes or 1 half-word data can be read by single access

word-wise (32 bit access). In this case 4 data-bytes or 2 half-word data or word-data can be read by single access.

Read access of this register less than the configured data size is forbidden.

53.8.11 SPI polynomial register (SPI_CRCPOLY)

Address offset: 0x040

Reset value: 0x0000 0107

The content of this register is write protected when SPI is enabled.

31302928272625242322212019181716
CRCPOLY[31:16]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw
1514131211109876543210
CRCPOLY[15:0]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

Bits 31:0 CRCPOLY[31:0] : CRC polynomial register

This register contains the polynomial for the CRC calculation.

The default 9-bit polynomial setting 0x107 corresponds to default 8-bit setting of DSIZE. It is compatible with setting 0x07 used in other ST products with fixed length of the polynomial string, where the most significant bit of the string is always kept hidden.

Length of the polynomial is given by the most significant bit of the value stored in this register. It must be set greater than DSIZE. CRC33_17 bit must be set additionally with CRCPOLY register when DSIZE is configured to maximum data size and CRC is enabled (to keep polynomial length greater than data size).

Note: CRCPOLY[31:16] bits are reserved for instances with data size limited to 16 bits. There is no constrain when 32-bit access is applied at these addresses. Reserved bits 31-16 are always read zero while any write to them is ignored.

53.8.12 SPI transmitter CRC register (SPI_TXCRC)

Address offset: 0x044

Reset value: 0x0000 0000

31302928272625242322212019181716
TXCRC[31:16]
rrrrrrrrrrrrrrrr
1514131211109876543210
TXCRC[15:0]
rrrrrrrrrrrrrrrr

Bits 31:0 TXCRC[31:0] : CRC register for transmitter

When CRC calculation is enabled, the TXCRC[31:0] bits contain the computed CRC value of the subsequently transmitted bytes. CRC calculation is initialized when the CRCEN bit of the SPI_CR1 register is set or when a data block is transferred completely. The CRC is calculated serially using the polynomial programmed in the SPI_CRCPOLY register.

The number of bits considered at calculation depends on the SPI_CRCPOLY register and CRCSIZE bits settings in the SPI_CFG1 register.

Note: A read to this register when the communication is ongoing may return an incorrect value.

Note: TXCRC[31:16] bits are reserved for instances with data size limited to 16 bits. There is no constrain when 32-bit access is applied at these addresses. Reserved bits 31-16 are always read zero while any write to them is ignored.

Note: The configuration of the CRCSIZE bitfield is not taken into account when the content of this register is read by software. No masking is applied for unused bits in this case.

53.8.13 SPI receiver CRC register (SPI_RXCRC)

Address offset: 0x048

Reset value: 0x0000 0000

31302928272625242322212019181716
RXCRC[31:16]
rrrrrrrrrrrrrrrr
1514131211109876543210
RXCRC[15:0]
rrrrrrrrrrrrrrrr

Bits 31:0 RXCRC[31:0] : CRC register for receiver

When CRC calculation is enabled, the RXCRC[31:0] bits contain the computed CRC value of the subsequently received bytes. CRC calculation is initialized when the CRCEN bit of the SPI_CR1 register is set or when a data block is transferred completely. The CRC is calculated serially using the polynomial programmed in the SPI_CRCPOLY register.

The number of bits considered at calculation depends on the SPI_CRCPOLY register and CRCSIZE bits settings in the SPI_CFG1 register.

Note: A read to this register when the communication is ongoing may return an incorrect value.

Note: RXCRC[31-16] bits are reserved for the peripheral instances with data size limited to 16 bits. There is no constrain when 32-bit access is applied at these addresses. Reserved bits 31-16 are always read zero while any write to them is ignored.

Note: The configuration of CRCSIZE bit field is not taken into account when the content of this register is read by software. No masking is applied for unused bits in this case.

53.8.14 SPI underrun data register (SPI_UDRDR)

Address offset: 0x04C

Reset value: 0x0000 0000

The content of this register is write protected when SPI is enabled.

31302928272625242322212019181716
UDRDR[31:16]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw
1514131211109876543210
UDRDR[15:0]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

Bits 31:0 UDRDR[31:0] : data at slave underrun condition

The register is taken into account in slave mode and at underrun condition only. The number of bits considered depends on the DSIZE bit setting in the SPI_CFG1 register. Underrun condition handling depends on the UDRCFG bit setting in the SPI_CFG1 register.

Note: UDRDR[31-16] bits are reserved for the peripheral instances with data size limited to 16 bits. There is no constraint when 32-bit access is applied at these addresses. Reserved bits 31-16 are always read zero while any write to them is ignored.

53.8.15 SPI register map

Table 584. SPI register map and reset values

OffsetRegister name313029282726252423222120191817161514131211109876543210
0x000SPI_CR1Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.IOLOCKTCRCINIRRCRCINICRC33_17SSIHDDIRCSUSPCSTARTMASRXRes.Res.Res.Res.Res.Res.Res.SPE
Reset value000000000
0x004SPI_CR2Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.TSIZE[15:0] (1)
Reset value000000000000000
0x008SPI_CFG1BPASSMBR[2:0]Res.Res.Res.Res.Res.CRCENRes.CRC SIZE[4:0] (1)TXDMAENRXDMAENRes.Res.Res.Res.Res.UDRCFGFTHLV[3:0] (1)DSIZE[4:0] (1)
Reset value0000000111000000000111
0x00CSPI_CFG2AFCONTRSSOMSSOESSIOPRes.SSMCPOLCPHALSBFRSTMASTERSP[2:0]COMM[1:0]Res.IOSWPRDIOPRDIOMRes.Res.Res.Res.MIDI[3:0]MSSI[3:0]
Reset value0000000000000000000000000
0x010SPI_IERRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.MODFIETIFREIECRCEIEOVRIEUDRIETXTFIEEOTIEDXPIETXPIERXPIE
Reset value0000000000
0x014SPI_SRCTSIZE[15:0] (1)RXWNERXPVLV[1:0]TXCSUSPRes.Res.MODFTIFRECRCEOVRUDRTXTFEOTDXPTXPRXP
Reset value000000000000000000010000000010
0x018SPI_IFCRRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.MODFCTIFRECCRCECOVRCUDRCTXTFCEOTCRes.Res.Res.
Reset value0000000
0x01CSPI_AUTOCRRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.TRIGENTRIGPOLTRIGSEL[3:0]Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.
Reset value000000
0x020SPI_TXDRTXDR[31:16]
Reset value00000000000000000000000000000000
0x024-0x02CReservedRes.
0x030SPI_RXDRRXDR[31:16]
Reset value00000000000000000000000000000000
0x034 - 0x03CReservedRes.
0x040SPI_CRCPOLYCRCPOLY[31:16] (2)
Reset value00000000000000000000000010000011
0x044SPI_TXCRCTXCRC[31:16] (2)
Reset value00000000000000000000000000000000
0x048SPI_RXCRCRXCRC[31:16] (2)
Reset value00000000000000000000000000000000
0x04CSPI_UDRDRUDRDR[31:16] (2)
Reset value00000000000000000000000000000000
  1. 1. The configuration of this bitfield depends on the features of the SPI instance. For more details, refer to Section 53.3: SPI implementation .
  2. 2. The bits 31-16 are reserved for the peripheral instances with data size limited to 16 bits. There is no constraint when the 32-bit access is applied at these addresses. The bits 31-16, when reserved, are always read to zero while any write to them is ignored.

Refer to Section 2.3: Memory organization for the register boundary addresses.