55. Serial peripheral interface (SPI)

55.1 Introduction

The serial peripheral interface (SPI) can be used to communicate with external devices while using the specific synchronous protocol. The (SPI) interface supports a 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 topologies. In case of master configuration it provides the communication clock (SCK) to the external slave device. The slave select signal can be provided by the master and accepted by the slave optionally, too. The Motorola data format is used by default, but some other specific modes are supported as well.

55.2 SPI main features

55.3 SPI implementation

Table 412. STM32H7A3/7B3/7B0xx SPI features

SPI modes/featuresSPI2S1SPI2S2SPI2S3SPI4SPI5SPI2S6
Rx & Tx FIFO size [x 8-bit]161616888

a. 24- and 32-bit data width are not always available. Refer to Section 55.3: SPI implementation .

Table 412. STM32H7A3/7B3/7B0xx SPI features (continued)

SPI modes/featuresSPI2S1SPI2S2SPI2S3SPI4SPI5SPI2S6
Maximum configurable data and CRC size [bits]323232161616
I2S featureYesYesYesNoNoYes

55.4 SPI functional description

55.4.1 SPI block diagram

The SPI allows a synchronous, serial communication 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 elements of SPI and their interactions are shown in the following block diagram at Figure 583 .

Figure 583. SPI2S block diagram

Figure 583. SPI2S block diagram. This is a detailed block diagram of the SPI2S peripheral. On the left, a vertical '32-bit APB bus' is connected to several internal components: 'IRQ Interface', 'DMA Interface', 'COM Controller' (containing registers SPI_IER, SPI_SR, SPI_IFCR, SPI_CRCPOLY, SPI_TXCRC, SPI_RXCRC), 'SPI_TXDR', 'SPI_UDRDR', 'SPI_RXDR', 'SPI_I2SCFGR', 'SPI_CR1', 'SPI_CR2', and 'SPI_CFG[2:1]'. External pins on the left include 'spi_wkup', 'spi_it', 'spi_tx_dma', 'spi_rx_dma', 'spi_pclk', and 'spi_ker_ck'. The 'COM Controller' connects to 'SS Logic', 'SYNC' blocks, 'Tx FIFO', 'Rx FIFO', 'CRC Controller', 'TX Shift Reg', 'UDR Controller', 'RX Shift Reg', and 'Clock Generator'. The 'SYNC' blocks are associated with the 'spi_pclk clock domain'. The 'CRC Controller', 'TX Shift Reg', 'UDR Controller', and 'RX Shift Reg' are associated with the 'Serial interface clock domain'. The 'Clock Generator' is associated with the 'spi_ker_ck clock domain'. On the right, the 'SS Logic' connects to 'NSS (WS)', 'SYNC' blocks connect to 'MOSI (SDO)', 'RX Shift Reg' connects to 'MISO (SDI)', and the 'Clock Generator' connects to 'SCK (CK)' and 'MCK'. A large 'X' symbol in the center represents the data path between the shift registers and the pins. The diagram is labeled 'MSV40463V3' in the bottom right corner.
Figure 583. SPI2S block diagram. This is a detailed block diagram of the SPI2S peripheral. On the left, a vertical '32-bit APB bus' is connected to several internal components: 'IRQ Interface', 'DMA Interface', 'COM Controller' (containing registers SPI_IER, SPI_SR, SPI_IFCR, SPI_CRCPOLY, SPI_TXCRC, SPI_RXCRC), 'SPI_TXDR', 'SPI_UDRDR', 'SPI_RXDR', 'SPI_I2SCFGR', 'SPI_CR1', 'SPI_CR2', and 'SPI_CFG[2:1]'. External pins on the left include 'spi_wkup', 'spi_it', 'spi_tx_dma', 'spi_rx_dma', 'spi_pclk', and 'spi_ker_ck'. The 'COM Controller' connects to 'SS Logic', 'SYNC' blocks, 'Tx FIFO', 'Rx FIFO', 'CRC Controller', 'TX Shift Reg', 'UDR Controller', 'RX Shift Reg', and 'Clock Generator'. The 'SYNC' blocks are associated with the 'spi_pclk clock domain'. The 'CRC Controller', 'TX Shift Reg', 'UDR Controller', and 'RX Shift Reg' are associated with the 'Serial interface clock domain'. The 'Clock Generator' is associated with the 'spi_ker_ck clock domain'. On the right, the 'SS Logic' connects to 'NSS (WS)', 'SYNC' blocks connect to 'MOSI (SDO)', 'RX Shift Reg' connects to 'MISO (SDI)', and the 'Clock Generator' connects to 'SCK (CK)' and 'MCK'. A large 'X' symbol in the center represents the data path between the shift registers and the pins. The diagram is labeled 'MSV40463V3' in the bottom right corner.

The simplified scheme of Figure 583 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 any data underrun or overrun events only.

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

The SPI master needs the spi_ker_ck kernel clock coming from RCC active during communication to feed the serial interface SCK clock via the clock generator divider.

The SPI working in slave mode handles 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 55.5.2: SPI error flags for details). This cannot be done when the bus becomes into idle. In specific cases, the slave even requires the clock generator working (see Section 55.5.1: TI mode ).

55.4.2 SPI signals

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

See Section 55.4.7: Slave select pin (NSS) management for details.

The SPI bus allows the communication between one master device and one or more slave devices. The bus consists of at least two wires: one for the clock signal and the other for synchronous data transfer. Other signals can be added depending on the data exchange between SPI nodes and their slave select signal management. The functionality between MOSI and MISO pins can be inverted in any SPI mode (see the IOSWP bit at SPI_CFG2 register).

All these pins are shared with I2S if this mode is implemented at the instance. See Section 55.9.2: Pin sharing with SPI function .

55.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 3/4 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 on the MOSI and/or MISO lines. The direction of data flow is highlighted by black arrows at the following topology figures.

55.4.4 Communications between one master and one slave

The communication flow may use one of three possible modes: full-duplex (three wires), half-duplex (two wires) or simplex (two wires). 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 at this configuration to synchronize the data flow and it is used by default at some specific SPI modes (for example, TI mode).

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 584. Full-duplex single master/ single slave application

Diagram of full-duplex single master/single slave application showing Master and Slave connected via MISO, MOSI, SCK, and NSS lines. The Master contains an Rx shift register, Tx shift register, and SPI clock generator. The Slave contains a Tx shift register and Rx shift register. Arrows indicate data flow: MISO from Slave to Master, and MOSI from Master to Slave.

The diagram illustrates a full-duplex SPI connection between a Master and a Slave. 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'. Four lines connect them: MISO (Master In Slave Out), MOSI (Master Out Slave In), SCK (Serial Clock), and NSS (Slave Select). Arrows indicate data flow: from the Slave's Tx shift register through the MISO line to the Master's Rx shift register, and from the Master's Tx shift register through the MOSI line to the Slave's Rx shift register. The SCK line connects the SPI clock generator to both shift registers. The NSS lines are shown but not connected between the Master and Slave.

Diagram of full-duplex single master/single slave application showing Master and Slave connected via MISO, MOSI, SCK, and NSS lines. The Master contains an Rx shift register, Tx shift register, and SPI clock generator. The Slave contains a Tx shift register and Rx shift register. Arrows indicate data flow: MISO from Slave to Master, and MOSI from Master to Slave.
  1. 1. To apply NSS pins interconnection is not mandatory to make the SPI interface working (see Section 55.4.7: Slave select pin (NSS) management for details).

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 has to 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 585. Half-duplex single master/ single slave application

Figure 585: Half-duplex single master/ single slave application diagram. The diagram shows a Master and a Slave connected via SPI pins: 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 MOSI pin of the Master is connected to the MOSI pin of the Slave through a 1kΩ resistor. The MISO pin of the Master is connected to the MISO pin of the Slave. The SCK pin of the Master is connected to the SCK pin of the Slave. The NSS pin of the Master is connected to the NSS pin of the Slave. The diagram is labeled MSV40465V3.
Figure 585: Half-duplex single master/ single slave application diagram. The diagram shows a Master and a Slave connected via SPI pins: 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 MOSI pin of the Master is connected to the MOSI pin of the Slave through a 1kΩ resistor. The MISO pin of the Master is connected to the MISO pin of the Slave. The SCK pin of the Master is connected to the SCK pin of the Slave. The NSS pin of the Master is connected to the NSS pin of the Slave. The diagram is labeled MSV40465V3.
  1. 1. To apply NSS pins interconnection is not mandatory to make the SPI interface working (see Section 55.4.7: Slave select pin (NSS) management for details).
  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 communication direction is changed not synchronously between two nodes working at bidirectional mode, and the new transmitter accesses the common data line while the former transmitter still keeps an opposite value on the line (the value depends on SPI configuration and communicated data). Both nodes can temporarily fight with opposite output levels on the line until the next node changes its direction setting correspondingly, too. It is suggested to insert serial resistance between MISO and MOSI pins at this mode to protect the outputs and limit the current blowing between them at this situation,

Simplex communications

The SPI can communicate in simplex mode by setting the SPI in transmit-only or in receive-only using the 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 pins 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 55.4.7: Slave select pin (NSS) management ).

In master mode, the MOSI output is disabled and may be used as 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 55.4.7: Slave select pin (NSS) management ).

Note: At 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 may 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 transaction direction (bidirectional mode is enabled, while the HDDIR bit is never changed).

Figure 586. 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 contains an Rx shift register, a Tx shift register, and an SPI clock generator. The Slave side contains a Tx shift register and an Rx shift register. Connections include MISO, MOSI, SCK, and NSS(1) pins. Data flow is indicated by an arrow from the Master's Tx shift register to the Slave's Rx shift register.

The diagram illustrates a simplex single master/single slave SPI application. On the left, the 'Master' block contains an 'Rx shift register', a 'Tx shift register', and an 'SPI clock generator'. The 'Tx shift register' is connected to the 'MOSI' pin. On the right, the 'Slave' block contains a 'Tx shift register' and an 'Rx shift register'. The 'Rx shift register' is connected to the 'MOSI' pin. A large arrow points from the Master's 'Tx shift register' to the Slave's 'Rx shift register', indicating the direction of data flow. Both the Master and Slave have 'MISO', 'SCK', and 'NSS (1) ' pins. The 'SCK' pins are connected to the 'SPI clock generator'. The 'NSS (1) ' pins are connected together. The diagram is labeled 'MSv40466V3' in the bottom right corner.

Diagram of a simplex single master/single slave SPI application. The Master side contains an Rx shift register, a Tx shift register, and an SPI clock generator. The Slave side contains a Tx shift register and an Rx shift register. Connections include MISO, MOSI, SCK, and NSS(1) pins. Data flow is indicated by an arrow from the Master's Tx shift register to the Slave's Rx shift register.
  1. 1. To apply NSS pins interconnection is not mandatory to make the SPI interface working (see Section 55.4.7: Slave select pin (NSS) management for details).
  2. 2. In this configuration, both the MISO pins can be used as GPIOs.

55.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 587 ). 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 time). When this is done, a communication between the master and the selected slave is established. Except for 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 587. Master and three independent slaves at star topology

Diagram of a Master and three independent slaves at star topology. The Master is on the left, connected to three Slaves (Slave 1, Slave 2, Slave 3) on the right. The Master has pins for NSS(1), MISO, MOSI, SCK, IO1, IO2, and IO3. The Slaves have pins for MISO, MOSI, SCK, and NSS. The Master's MISO pin is connected to the Slaves' MISO pins. The Master's MOSI pin is connected to the Slaves' MOSI pins. The Master's SCK pin is connected to the Slaves' SCK pins. The Master's IO1, IO2, and IO3 pins are connected to the Slaves' NSS pins. Each Slave has its own Tx shift register and Rx shift register. The Master has its own Rx shift register, Tx shift register, and SPI clock generator. Arrows indicate data flow: from Master's Tx shift register to Slaves' Rx shift registers, and from Slaves' Tx shift registers to Master's Rx shift register.
Diagram of a Master and three independent slaves at star topology. The Master is on the left, connected to three Slaves (Slave 1, Slave 2, Slave 3) on the right. The Master has pins for NSS(1), MISO, MOSI, SCK, IO1, IO2, and IO3. The Slaves have pins for MISO, MOSI, SCK, and NSS. The Master's MISO pin is connected to the Slaves' MISO pins. The Master's MOSI pin is connected to the Slaves' MOSI pins. The Master's SCK pin is connected to the Slaves' SCK pins. The Master's IO1, IO2, and IO3 pins are connected to the Slaves' NSS pins. Each Slave has its own Tx shift register and Rx shift register. The Master has its own Rx shift register, Tx shift register, and SPI clock generator. Arrows indicate data flow: from Master's Tx shift register to Slaves' Rx shift registers, and from Slaves' Tx shift registers to Master's Rx shift register.
  1. 1. Master single NSS pin hardware output functionality cannot support this topology (to be replaced by a set of GPIOs under software control) and the user should avoid SPI AF setting at the pin (see Section 55.4.7: Slave select pin (NSS) management 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 time, it is better to configure the MISO pins in open drain configuration with an external pull-up at MISO line to prevent any conflict between interconnected outputs of the slaves on the line. Else the push-pull configuration can be applied without an extra resistor for the slaves (see Section 11.3.7: I/O alternate function input/output on page 514 ).

The master can handle the SPI communication with all the slaves in time when a circular topology is applied (see Figure 588 ). All the slaves behave like simple shift registers applied in a serial chain under common slave select and clock control. All the information is shifted simultaneously around the circle while returning back to the master. Sessions have fixed the length where the number of data frames transacted by the master is equal to the number of slaves. Then when a first data frame is transacted in the chain, the master just sends information dedicated for the last slave node in the chain via the first slave node input while the first information received by the master comes from the last node output at this time. Correspondingly, the last transacted data finishing the session is dedicated for the first slave node while its first outgoing data just reaches the master input after its circling around the

chain passing through all the other slaves during the session. The data format configuration and clock setting has to be the same for all the nodes in the chain at this topology. As the receive and transmit shift registers are separated internally, a trick with intentional underrun has to be applied to the TxFIFO slaves when information is transacted between the receiver and the transmitter by hardware. In this case, the transmission underrun feature is configured at a mode repeating the last received data frame (UDRCFG[1:0]=01). A session can start optionally with a single data pattern written into the TxFIFO by each slave (usually slave status information is applied) before the session starts. In this case, the underrun happens in fact after this first data frame is transacted (underrun detection has to be set at end of data transaction at slaves UDRDET[1:0]=01). To be able to clear the internal underrun condition immediately and restart the session by the TxFIFO content again, the user has to disable and enable the SPI between sessions and fill the TxFIFO by a new single data pattern.

Figure 588. Master and three slaves at circular (daisy chain) topology

Diagram of a Master and three slaves connected in a circular (daisy chain) SPI topology. The Master is connected to Slave 1, which is connected to Slave 2, which is connected to Slave 3, which is connected back to the Master. The Master contains an SPI clock generator, a Tx shift register, and an Rx shift register. The slaves contain Tx shift registers (with underrun feature) and Rx shift registers. The connections are: MISO (Master In Slave Out) from Slave 1 to Master; MOSI (Master Out Slave In) from Master to Slave 1; SCK (Serial Clock) from Master to all slaves; and NSS (Slave Select) from Master to all slaves. The slaves are connected in a daisy chain: MISO from Slave 1 to Slave 2, MOSI from Slave 2 to Slave 1, SCK from Master to Slave 2, and NSS from Master to Slave 2. Similarly, Slave 2 is connected to Slave 3: MISO from Slave 2 to Slave 3, MOSI from Slave 3 to Slave 2, SCK from Master to Slave 3, and NSS from Master to Slave 3. The Master's Tx shift register is connected to the MOSI line, and the Rx shift register is connected to the MISO line. The slaves' Tx shift registers are connected to the MOSI line, and the Rx shift registers are connected to the MISO line. The diagram shows the flow of data from the Master to the slaves and back to the Master.

MSV40468V2

Diagram of a Master and three slaves connected in a circular (daisy chain) SPI topology. The Master is connected to Slave 1, which is connected to Slave 2, which is connected to Slave 3, which is connected back to the Master. The Master contains an SPI clock generator, a Tx shift register, and an Rx shift register. The slaves contain Tx shift registers (with underrun feature) and Rx shift registers. The connections are: MISO (Master In Slave Out) from Slave 1 to Master; MOSI (Master Out Slave In) from Master to Slave 1; SCK (Serial Clock) from Master to all slaves; and NSS (Slave Select) from Master to all slaves. The slaves are connected in a daisy chain: MISO from Slave 1 to Slave 2, MOSI from Slave 2 to Slave 1, SCK from Master to Slave 2, and NSS from Master to Slave 2. Similarly, Slave 2 is connected to Slave 3: MISO from Slave 2 to Slave 3, MOSI from Slave 3 to Slave 2, SCK from Master to Slave 3, and NSS from Master to Slave 3. The Master's Tx shift register is connected to the MOSI line, and the Rx shift register is connected to the MISO line. The slaves' Tx shift registers are connected to the MOSI line, and the Rx shift registers are connected to the MISO line. The diagram shows the flow of data from the Master to the slaves and back to the Master.
  1. 1. Underrun feature is used at slaves at this configuration when slaves are able to transmit data received previously into the Rx shift register once their TxFIFOs become empty.

55.4.6 Multimaster communication

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

When nodes are nonactive, both stay at slave mode by default. Once one node wants to overtake control on the bus, it switches itself into master mode and applies active level on the slave select input of the other node via the dedicated GPIO pin. After the session is completed, the active slave select signal is released and the node mastering the bus temporarily returns back to passive slave mode waiting for the next session start.

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

Figure 589. Multimaster application

Diagram of a multimaster application showing two nodes connected via SPI lines. Each node contains an Rx (Tx) shift register, a Tx (Rx) shift register, and an SPI clock generator. The lines are labeled MISO, MOSI, SCK, GPIO, and NSS(1). Arrows indicate bidirectional data flow on MISO and MOSI lines. The diagram is labeled MSv40469V3.

The diagram illustrates a multimaster application with two nodes connected via a common SPI bus. Each node is represented by a box containing its internal components: an Rx (Tx) shift register, a Tx (Rx) shift register, and an SPI clock generator. The bus consists of four main lines: MISO (Master In Slave Out), MOSI (Master Out Slave In), SCK (Serial Clock), and NSS (Slave Select). The MISO and MOSI lines are bidirectional, indicated by double-headed arrows. The SCK line is driven by the SPI clock generator in each node. The NSS line is labeled with '(1)' and is connected between the GPIO pin of one node and the NSS pin of the other. The nodes are labeled 'Master (Slave)' at the bottom, indicating their dual capability. The diagram is identified by the code MSv40469V3 in the bottom right corner.

Diagram of a multimaster application showing two nodes connected via SPI lines. Each node contains an Rx (Tx) shift register, a Tx (Rx) shift register, and an SPI clock generator. The lines are labeled MISO, MOSI, SCK, GPIO, and NSS(1). Arrows indicate bidirectional data flow on MISO and MOSI lines. The diagram is labeled MSv40469V3.
  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.

55.4.7 Slave select pin (NSS) 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 slave select logic can be managed internally (software slave select management) when the NSS pin is not taken into account, or externally when the slave select logic is 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 one by the SSIOP bit setting. The 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:

external NSS pin is free for other application uses (as GPIO or other alternate function).

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

Note: When the SPI slave is enabled at the hardware slave select management mode, all the traffics are ignored even if the NSS is found at active level 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 for 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 590. Scheme of slave select control logic

Figure 590: Scheme of slave select control logic. The diagram shows the internal logic for slave select (NSS) control. On the left, the NSS pin is connected to GPIO logic, which is then connected to an inverter. The output of the inverter is connected to a multiplexer (MUX) labeled '1' and '0'. The MUX is controlled by SSIOP control, SSI control, and SSM control. The output of the MUX is labeled SS_IN. Below the MUX, there is an SS output control block, which is connected to an SS output logic block. The SS output control block is controlled by SSOE control and SSOM, MIDI, MSSI control. The SS output logic block is also controlled by SSOM, MIDI, MSSI control. The SS output control block is labeled 'Master mode & HW SS management (SSM=0) only'. The diagram is divided into 'SS external logic' and 'SS internal logic' by a dashed line. A table is included in the top right corner.
SS_INMaster modeSlave mode
SS(SSI) <> SSIOPOKNon active
SS(SSI) = SSIOPConflictActive

MSV40470V3

Figure 590: Scheme of slave select control logic. The diagram shows the internal logic for slave select (NSS) control. On the left, the NSS pin is connected to GPIO logic, which is then connected to an inverter. The output of the inverter is connected to a multiplexer (MUX) labeled '1' and '0'. The MUX is controlled by SSIOP control, SSI control, and SSM control. The output of the MUX is labeled SS_IN. Below the MUX, there is an SS output control block, which is connected to an SS output logic block. The SS output control block is controlled by SSOE control and SSOM, MIDI, MSSI control. The SS output logic block is also controlled by SSOM, MIDI, MSSI control. The SS output control block is labeled 'Master mode & HW SS management (SSM=0) only'. The diagram is divided into 'SS external logic' and 'SS internal logic' by a dashed line. A table is included in the top right corner.

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

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

Figure 591: Data flow timing control (SSOE=1, SSOM=0, SSM=0). The diagram shows a timing diagram for SPI signals. The top signal is NSS, which is high before the first clock pulse and goes low at the start of the first data frame. The second signal is SCK, which is a continuous clock signal. The third signal is MOSI/MISO, which shows data frames. Each frame consists of a Most Significant Byte (MSB) followed by several other bytes, ending with a Least Significant Byte (LSB). The timing is defined by several intervals: MSSI[3:0] (time from NSS falling edge to first SCK rising edge), t_sck (one SCK period), DSIZE[4:0] + 1 (duration of one data frame), MIDI[3:0] (time from last SCK rising edge of one frame to NSS falling edge), and DSIZE[4:0] + 1 (duration of the next data frame). The diagram is labeled MSV40472V2.

MSV40472V2

Figure 591: Data flow timing control (SSOE=1, SSOM=0, SSM=0). The diagram shows a timing diagram for SPI signals. The top signal is NSS, which is high before the first clock pulse and goes low at the start of the first data frame. The second signal is SCK, which is a continuous clock signal. The third signal is MOSI/MISO, which shows data frames. Each frame consists of a Most Significant Byte (MSB) followed by several other bytes, ending with a Least Significant Byte (LSB). The timing is defined by several intervals: MSSI[3:0] (time from NSS falling edge to first SCK rising edge), t_sck (one SCK period), DSIZE[4:0] + 1 (duration of one data frame), MIDI[3:0] (time from last SCK rising edge of one frame to NSS falling edge), and DSIZE[4:0] + 1 (duration of the next data frame). The diagram is labeled MSV40472V2.
  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, SSOP=0, LSBFRST=0.

Additionally, bit SSOM=1 setting invokes a specific mode which interleaves pulses between data frames if there is a sufficient space to provide them (MIDI[3:0] has to be set greater than one SPI period). Some configuration examples are shown at Figure 592: NSS interleaving pulses between data (SSOE=1, SSOM=1, SSM=0) .

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

Timing diagram for SPI mode I. NSS is active-low. SCK starts low and pulses high. MOSI/MISO transmit MSB first. Timing parameters: MSSI[3:0], t_sck, DSIZE[4:0]+1, MIDI[3:0]. Timing diagram for SPI mode II. NSS is active-low. SCK starts low and pulses high. MOSI/MISO transmit MSB first. Timing parameters: MSSI[3:0], t_sck/2, DSIZE[4:0]+1, MIDI[3:0]. Timing diagram for SPI mode III. NSS is active-high. SCK starts high and pulses low. MOSI/MISO transmit LSB first. Timing parameters: MSSI[3:0], t_sck, DSIZE[4:0]+1, MIDI[3:0]. Timing diagram for SPI mode IV. NSS is active-high. SCK starts high and pulses low. MOSI/MISO transmit LSB first. Timing parameters: MSSI[3:0], t_sck/2, DSIZE[4:0]+1, MIDI[3:0].

I. CPHA=0, CPOL=0, SSOP=0, LSBFRST=0 MIDI[3:0] - 1

II. CPHA=1, CPOL=0, SSOP=0, LSBFRST=0 MIDI[3:0] - 1

III. CPHA=0, CPOL=1, SSOP=1, LSBFRST=1 MIDI[3:0] - 1

IV. CPHA=1, CPOL=1, SSOP=1, LSBFRST=1 MIDI[3:0] - 1

MSv40471V2

Timing diagram for SPI mode I. NSS is active-low. SCK starts low and pulses high. MOSI/MISO transmit MSB first. Timing parameters: MSSI[3:0], t_sck, DSIZE[4:0]+1, MIDI[3:0]. Timing diagram for SPI mode II. NSS is active-low. SCK starts low and pulses high. MOSI/MISO transmit MSB first. Timing parameters: MSSI[3:0], t_sck/2, DSIZE[4:0]+1, MIDI[3:0]. Timing diagram for SPI mode III. NSS is active-high. SCK starts high and pulses low. MOSI/MISO transmit LSB first. Timing parameters: MSSI[3:0], t_sck, DSIZE[4:0]+1, MIDI[3:0]. Timing diagram for SPI mode IV. NSS is active-high. SCK starts high and pulses low. MOSI/MISO transmit LSB first. Timing parameters: MSSI[3:0], t_sck/2, DSIZE[4:0]+1, MIDI[3:0].
  1. 1. MSS[3:0]=0010, MIDI[3:0]=0010.
  2. 2. NSS interleaves between data when MIDI[3:0]>1.

55.4.8 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 may 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 are being transferred. This bit affects both master and slave modes. If CPOL is reset, the SCK pin has a low-level idle state. If CPOL is set, the SCK pin has a high-level idle state.

If the CPHA bit is set, the second edge on the SCK pin captures the first data bit transacted (falling edge if the CPOL bit is reset, rising edge if the CPOL bit is set). Data are latched on each occurrence of this clock transition type. If the CPHA bit is reset, the first edge on the SCK pin captures the first data bit transacted (falling edge if the CPOL bit is set, rising edge if the CPOL bit is reset). Data are latched on each occurrence of this clock transition type.

The combination of the CPOL (clock polarity) and CPHA (clock phase) bits selects the data capture clock edges (dotted lines at Figure 593: Data clock timing diagram ).

Figure 593 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 593. Data clock timing diagram

Figure 593. Data clock timing diagram. The diagram illustrates two SPI communication scenarios based on the CPHA (Clock Phase) setting. The top section shows CPHA = 1, where data is sampled on the second clock edge of each frame. The bottom section shows CPHA = 0, where data is sampled on the first clock edge of each frame. Both sections show the relationship between the clock signal (CPOL = 1 and CPOL = 0), the Master Out Slave In (MOSI) signal, the Master In Slave Out (MISO) signal, and the Slave Select (NSS) signal. The MOSI and MISO signals are shown as a series of bits, with the Most Significant Bit (MSB) and Least Significant Bit (LSB) indicated. The NSS signal is shown as a low-active signal. The diagram is labeled 'ai17154f' in the bottom right corner.

The diagram illustrates the data clock timing for SPI communication, showing the relationship between the clock signal (CPOL), the Master Out Slave In (MOSI) signal, the Master In Slave Out (MISO) signal, and the Slave Select (NSS) signal. The diagram is divided into two sections based on the CPHA (Clock Phase) setting.

Top Section: CPHA = 1

Bottom Section: CPHA = 0

ai17154f

Figure 593. Data clock timing diagram. The diagram illustrates two SPI communication scenarios based on the CPHA (Clock Phase) setting. The top section shows CPHA = 1, where data is sampled on the second clock edge of each frame. The bottom section shows CPHA = 0, where data is sampled on the first clock edge of each frame. Both sections show the relationship between the clock signal (CPOL = 1 and CPOL = 0), the Master Out Slave In (MOSI) signal, the Master In Slave Out (MISO) signal, and the Slave Select (NSS) signal. The MOSI and MISO signals are shown as a series of bits, with the Most Significant Bit (MSB) and Least Significant Bit (LSB) indicated. The NSS signal is shown as a low-active signal. The diagram is labeled 'ai17154f' in the bottom right corner.

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 LSBFIRST bit in the SPI_CFG2 register. The data frame size is chosen by using the DSIZE[4:0] bits. It can be set from 4-bit up to 32-bit length and the setting applies for both transmission and reception. When the SPI_TXDR/SPI_RXDR registers are accessed, data frames are always right-aligned either to a byte (if the data fit into a byte), a half-word, or a word (see Figure 594).

If the access is a multiple of the configured data size, data packing is applied automatically. During communication, only bits within the data frame are clocked and transferred.

Figure 594. Data alignment when data size is not equal to 8-bit, 16-bit or 32-bit

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
Tx7 4 3 015 14 13 031 27 26 0
Rx7 4 3 015 13 12 031 27 26 0

MSV40473V1

Note: The minimum data length is 4 bits. If a data length of less than 4 bits is selected, it is forced to a 4-bit data frame size.

55.4.9 Configuration of 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 has to be initialized, perform these steps prior SPI is enabled:

  1. Write the proper GPIO registers: Configure GPIO alternate functions at MOSI, MISO, SCK, and NSS pins if applied.
  2. Write to the SPI_CFG1 and SPI_CFG2 registers to set up proper values of all not reserved bits and bitfields included there with the following exceptions:
    1. SSOM, SSOE, MBR[2:0], MIDI[3:0] and MSS[3:0] are required and taken into account in master mode exclusively.
    2. UDRDET[1:0] and UDRCFG[1:0] are required and taken into account in slave mode only. The MBR[2:0] setting is taken into account only when slave is configured at TI mode.
    3. CRCSIZE[4:0] is required if CRCEN is set,
    4. CPOL, CPHA, LSBFRST, SSOM, SSOE, SSIOP, MSS, MIDI, and SSM are not required at TI mode.
    5. 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 configurations changes of the SPI_CFG1 and SPI_CFG2 registers can affect the level of the signals on these pins.
    6. The I2SMOD bit at SPI_I2SCFGR register has to be kept cleared to prevent any unexpected influence of occasional I2S configuration.
  3. Write to the SPI_CR2 register to select the length of the transfer, if it is not known TSIZE has to be programmed to zero.
  4. Write to SPI_CRCPOLY and into TCRINI, RCRINI, and CRC33_17 bits at SPI_CR1 register to configure the CRC polynomial and CRC calculation if needed.
  5. Configure DMA streams dedicated for the SPI Tx and Rx in DMA registers if the DMA streams are used (see chapter Communication using DMA ).
  6. Configure SSI, HDDIR, and MASRX at SPI_CR1 register if required.
  7. Program the IOLOCK bit in the SPI_CFG1 register if the configuration protection is required (for safety).

55.4.10 Procedure for enabling SPI

It is recommended to configure and enable the SPI slave before the master sends the clock. However, there is no impact if the configuration and enabling procedure are done while a traffic is ongoing on the bus, provided the NSS signal is managed by hardware on the slave side, or kept inactive by the slave software when the NSS signal is managed by software (see Section 55.4.7: Slave select pin (NSS) management ). The data register of the slave transmitter must contain data to be sent before the master starts its clocking. The SCK signal must be settled to idle state level corresponding to the selected polarity before the SPI slave is selected by NSS, otherwise the following transaction may be desynchronized.

When the SPI slave is enabled at the hardware slave select management mode all the traffics are ignored even if the NSS is found at active level 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 for 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 at 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 55.4.14: Communication using DMA (direct memory addressing) .

55.4.11 SPI data transmission and reception procedures

The setting of data communication format follows the basic principle that the appropriate number of data with a flexible size must be transferred within a session (transaction) while, optionally, the data handling can be cumulated effectively into a single access of the SPI data registers (data packing) or even grouped into a sequence of such services if data is collected at consistent bigger data packets. The data handling services are based upon FIFO packet occupancy events. That is why the complete data packet must be serviced exclusively upon a dedicated packet flag.

To understand better the next detailed content of this section, the user should capture the configuration impact and meaning of the following items at first:

Data size (DSIZE) : defines the data frame (sets the number of bits at single data frame).

FIFO threshold (FTHLV) : defines the data packet, sets the number of data frames in a single data packet and so the occurrence of the packet occupancy events to handle SPI data registers either by software or by DMA.

Data access : a way how to handle the SPI data register content when the transfer data between the application and the SPI FIFOs upon a packet event. It depends on the packet size configuration. Optionally, multiple data can be handled effectively by a single access of the register (by data packing) or by sequence of such accesses (when servicing a bigger data packet).

FIFO size : capacity or space to absorb available data. It depends on the data size and the internal hardware efficiency how the data is compressed and organized within this space. The FTHLV setting must respect the FIFO capacity to store two data packets at least.

Transaction size (TSIZE) : defines the total number of data frames involved in a transaction session overall possibly covered by several data packet services. There is no need to align this number with the packet size (handling of a last not aligned data packet is supported if TSIZE is programmed properly).

Data handling via RxFIFO and TxFIFO

All SPI data transitions pass through the embedded FIFOs organized by bytes (N x 8-bit). The size of the FIFOs (N) is product and the peripheral instance dependent. This enables the SPI to work in a continuous flow, and prevents overruns when the data frame size is short or the interrupt/DMA latency is too long. Each direction has its own FIFO called TxFIFO and RxFIFO, respectively.

The handling of the FIFO content is based on servicing data packet events exclusively raised by dedicated FIFO packet occupancy flags (TXP, RXP, or DXP). The flags occurrence depends on the data exchange mode (duplex, simplex), the data frame size (number of bits in the frame) and how data are organized at data packets. The frequency of the packet events can be decreased significantly when data are organized into packets by defining the FIFO threshold. Several data frames grouped at packet can be then handled effectively based on a single FIFO occupancy packet event either by a single SPI data register access or their sequence what consumes less system performance. The user can control the access type by casting the data register address to force a concrete CPU instruction applied for the register read or write. The access then can be 8-bit, 16-bit, or 32-bit. However, a single data frame must be always accessed at least. It is crucial to keep the setting of the packet size (FTHLVL) and the data size (DSIZE) always balanced with the applied data registers access (no matter if a single access or their sequence is applied) just to apply and complete service of a single data packet upon its event. This principle, occurrence and clearing capabilities of the FIFO occupancy flags are common no matter if DMA, interrupt, or polling is applied.

A read access to the SPI_RXDR register returns the oldest value stored in the RxFIFO that has not been read yet. A write access to the SPI_TXDR stores the written data in the TxFIFO at the end of a send queue.

A read access to the SPI_RXDR register must be managed by the RXP event. This flag is set by hardware when at least one complete data packet (defined as receiver threshold by FTHLVL[3:0] bits at the SPI_CFG1 register) is available at the reception FIFO while reception is active. The RXP is cleared as soon as less data than a complete single packet is available in the RxFIFO, when reading SPI_RXDR by software or by DMA.

The RXP triggers an interrupt if the RXPIE bit is set.

Upon setting of the RXP flag, the application performs the due number of SPI data register reads to download the content of one data packet. Once a complete data packet is downloaded, the application software or DMA checks the RXP value to see if other packets are pending into the receive FIFO and, if so, downloads them packet by packet until the RXP reads 0. RxFIFO can store up to N data frames (for frame size <= 8-bit), N/2 data frames (for 8-bit < frame <= 16-bit), N/3 data frames (for 16-bit < frame <= 24-bit) or N/4 data frames (if data frame >24-bit) where N is the size of the FIFO in bytes.

At the end of a reception, it may happen that some data may still be available in the RxFIFO, without reaching the FTHLV level, thus the RXP is not set. In this case, the number of remaining RX data frames in the FIFO is indicated by RXWNE and RXPLVL fields in the SPI_SR register. It happens when the number of the last data received in a transfer cannot fully accomplish the configured packet size in the case transfer size and packet size are not aligned. Nevertheless, the application software can still perform the standard number of

reads from the RxFIFO used for the previous complete data packets without drawbacks: only the consistent data (completed data frames) are popped from the RxFIFO while redundant reads (or any incomplete data) are reading 0. Thanks to that, the application software can treat all the data in a transfer in the same way, and is off-loaded to foresee the reception of the last data in a transfer and from calculating the due number of reads to be popped from RxFIFO.

In a similar way, write access of a data frame to be transmitted is managed by the TXP event. This flag is set by hardware when there is enough space for the application to push at least one complete data packet (defined at FTHLV[3:0] bits at SPI_CFG1 register) into the transmission FIFO while transmission is active. The TXP is cleared as soon as the TxFIFO is filled by software or by DMA and the space currently available for any next complete data packet is lost. This can lead to oscillations of the TXP signal when data are released out from the TxFIFO while a new packet is stored frame by frame. Any write to the TxFIFO is ignored when there is no sufficient room to store at least a single data frame (TXP event is not respected), when TXTF is set or when the SPI is disabled.

The TXP triggers an interrupt if the TXPIE bit is set or a/o a DMA request if TXDMAEN is set. The TXPIE mask is cleared by hardware when the TXTF flag is set.

Upon setting of the TXP flag, the application software performs the due number of SPI data register writes to upload the content of one entire data packet. Once a new complete data packet is uploaded, the application software or DMA checks the TXP value to see if other packets can be pushed into the TxFIFO and, if so, uploads them packet by packet until TXP reads 0 at the end of any packet load.

The number of last data in a transfer can be shorter than the configured packet size in the case when the transfer size and the packet size are not aligned. Nevertheless, the application can still perform the standard number of data register writes used for the previous packets without drawbacks: only the consistent data are pushed into the TxFIFO while redundant writes are discarded. Thanks to that, the application software can treat all the data in a transfer in the same way and is off-loaded to foresee the transmission of the last data in a transfer and from calculating the due number of writes to push the last data into TxFIFO. Just for the last data case, the TXP event is asserted by SPI once there is enough space into TxFIFO to store remaining data to complete the current transfer.

Both TXP and RXP events can be polled or handled by interrupts. The DXP bit can be monitored as a common TXP and RXP event at full duplex mode.

Upon setting of the DXP flag the application software performs the due number of writes to the SPI data register to upload the content of one entire data packet for transmission, followed by the same number of reads from the SPI data register to download the content of one data packet. Once one data packet is uploaded and one is downloaded, the application software or DMA checks the DXP value to see if other packets can be pushed and popped in sequence and, if so, uploads/downloads them packet by packet until DXP reads 0.

The DXP triggers an interrupt if the DXPIE bit is set. The DXPIE mask is cleared by hardware when the TXTF flag is set.

The DXP is useful in Full-Duplex communication in order to optimize performance in data uploading/downloading, and reducing the number of interrupts or DMA sequences required to support an SPI transfer thus minimizing the request for CPU bandwidth and system power especially when SPI is operated in Stop mode.

When rely on the DXP interrupt exclusively, the user must consider the drawback of such a simplification when TXP and RXP events are serviced by common procedures because the TXP services are delayed by purpose in this case. This is due to the fact that the TXP

events occurs before the reception RXP ones to allow the TXP servicing prior transaction of the last frame fully emptying the TxFIFO, otherwise the master cannot provide a continuous SCK clock flow and the slave can even face an underrun condition. The possible solution is to prefill the TxFIFO with few data packets ahead prior to the session start, and to handle all the data received after the TXTF event by EOT exclusively at the end of the transaction (as TXTF suppresses the DXP interrupts at the end of the transaction). In case of CRC computation is enabled, the user must calculate with additional space to accommodate the CRC frame at RxFIFO when relying on EOT exclusively at the end of transaction.

Another way to manage the data exchange is to use DMA (see Communication using DMA (direct memory addressing) ).

If the next data is received when the RxFIFO is full, an overrun event occurs (see description of OVR flag at Section 55.5.2: SPI error flags ). An overrun event can be polled or handled by an interrupt.

This may happen in slave mode or master mode (full duplex or receive only with MASRX = 0). In master receive only mode, with MASRX = 1, the generated clock stops automatically when the RxFIFO is full, therefore overrun is prevented.

Both RxFIFO and TxFIFO content are kept flushed when SPI is disabled (SPE=0).

Transaction handling

A few data frames can be passed at single transaction to complete a message. The user can handle number of data within a message thanks to values stored into TSIZE and TSER fields. In principle, the transaction of a message starts when the SPI is enabled by setting CSTART bit and finishes when the total number of required data is transacted. The end of the transaction controls the CRC and the hardware slave select management when applied. To restart the internal state machine properly, SPI is strongly suggested to be disabled and re-enabled before the next transaction starts even though its setting is not changed. If TSIZE is kept at zero while CSTART is set, an endless transaction is initialized (no control of transfer size is applied). During an endless transaction, the number of transacted data aligned with the FIFO threshold is supported exclusively. If the number of data (or its grouping into packets) is unpredictable, the user must keep the FIFO threshold setting (packet size) at single data (FTHLV=0) to assure that each data frame raises its own packet event to be serviced by the application or DMA.

The transaction can be suspended at any time thanks to CSUSP which clears the CSTART bit. SPI must always be disabled after such software suspension and re-enabled before the next transaction starts.

In master mode, the user can extend the number of data within the current session. When the number of data programmed into TSIZE is transacted and if TSER contains a nonzero value, the content of TSER is copied into TSIZE, and the TSER value is cleared automatically. The transaction is then extended by a number of data corresponding to the value reloaded into TSIZE. The EOT event is not raised in this case as the transaction continues. After the reload operation, the TSERF flag is set and an interrupt is raised if TSERFIE is set. The user can write the next nonzero value into TSER after the TSER is cleared by hardware but still before the next reload occurs, so an unlimited number of data can be transacted while repeating this process.

When any data extension is applied, it always starts by aligned data packet. That is why it is suggested to keep the number of data to be extended always aligned with packet size else the last data packet just before the extension is applied has to be handled as an incomplete one (see data packing chapter). If the overall number of data is not aligned, the user must

implement the rest not aligned number of data into TSER just at the last extension cycle and then handle the last incomplete packet of data standardly within the EOT event handler.

For example, if the user wants to transfer 23 bytes while the data size is configured to 8 bits, the data packet size to 4 data, and 32-bit accesses to the FIFO are used, then any of the following TSIZE and TSER settings can be applied:

In this case the MSB byte within the last access (sixth access) to the FIFO is ignored.

The following TSIZE and TSER settings are incorrect because the MSB byte would be ignored within the fourth access to the FIFO, while the four data are transferred within the other accesses:

When the transmission is enabled, a sequence begins and continues while any data is present in the TxFIFO of the master. The clock signal is provided permanently by the master until TxFIFO becomes empty, then it stops, waiting for additional data.

In receive-only modes, half duplex (COMM[1:0]=11, HDDIR=0) or simplex (COMM[1:0]=10) the master starts the sequence when SPI is enabled and transaction is released by setting the CSTART bit. The clock signal is provided by the master and it does not stop until either SPI or receive-only mode is disabled/suspended by the master. The master receives data frames permanently up to this moment. The reception can be suspended either by SW control, writing 1 to the CSUSP bit in the SPI_CR1 register, or automatically when MASRX=1 and RxFIFO becomes full. The reception is automatically stopped also when the number of frames programmed in TSIZE and TSER fields of the SPI_CR2 register has been completed.

In order to disable the master receive only mode, the SPI must be suspended at first. When the SPI is suspended, the current frame is completed, before changing the configuration.

Caution: If SPE is written to 0 at master, while reception is ongoing without any suspending, the clock is stopped without completing the current frame, and the RxFIFO is flushed.

While the master can provide all the transactions in continuous mode (SCK signal is continuous) it has to respect slave capability to handle data flow and its content at anytime. When necessary, the master must slow down the communication and provide either a slower clock or separate frames or data sessions with sufficient delays by MIDI[3:0] bits setting or provide an initial delay by setting MSSI[1:0] which postpones any transaction start to give slave sufficient room for preparing data. Be aware data from the slave are always transacted and processed by the master even if the slave could not prepare it correctly in time. It is preferable for the slave to use DMA, especially when data frames are short, FIFO is accessed by bytes and the SPI bus rate is high.

In order to add some SW control on the SPI communication flow from a slave transmitter node, a specific value written in the SPI_UDRDR (SPI Underrun Data Register) may be used. On slave side, when TxFIFO becomes empty, this value is sent out automatically as next data and may be interpreted by SW on the master receiver side (either simply dropped or interpreted as a XOFF like command, in order to suspend the master receiver by SW).

At multislave star topology, a single slave can be only enabled for the output data at a time. The slave just selected for the communication with the master needs to detect a change of its NSS input into active level before the communication with the master starts. In a single

slave system it is not necessary to control the slave with NSS, but it is often better to provide the pulse here too, to synchronize the slave with the beginning of each data sequence. The slave select can be managed by both software and hardware ( Section 55.4.7: Slave select pin (NSS) management ).

55.4.12 Procedure for disabling the SPI

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

At 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 transactions may be corrupted in this case.

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 transaction when it stops providing data for transmission. In this case, the clock stops after the last data transaction. 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, in order to stop the peripheral, the SPI communication must be first suspended, by setting CSUSP to 1.

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

When SPI is disabled, RxFIFO is flushed. To prevent losing unread data, the user has to 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 transactions, for example:

When TSIZE>0, EOT and TXC signals are equal so the polling of EOT is reliable at whatever SPI communication mode to check end of the bus activity. When TSIZE=0, the user has to check TXC, SUSP or FIFO occupancy flags in according with the applied SPI mode and 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 CRC frame is completed in this case. When a transmission is suspended the software has to wait till CSTART bit is cleared.
  2. 2. Read all RxFIFO data (until RXWNE=0 and RXPLVL=00)
  3. 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 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 on going data are lost when disabling the SPI.

55.4.13 Data packing

From user point of view there are two ways of data packing which can overlay each other:

When the data frame size fits into one byte (less than or equal to 8 bits), the data packing is used automatically when any read or write 16-bit or 32-bit access is performed on the SPI_RXDR/SPI_TXDR register. The multiple data frame pattern is handled in parallel in this case. At first, the SPI operates using the pattern stored in the LSB of the accessed word, then with the other data stored in the MSB. Figure 595 provides an example of data packing mode sequence handling. While DSIZE[3:0] is configured to 4-bit there, two or four data frames are written in the TxFIFO after the single 16-bit or 32-bit access the SPI_TXDR register of the transmitter.

When the data frame size is between 9-bit and 16-bit, data packing is used automatically when a 32-bit access is done. the least significant half-word is used first. (regardless of the LSBFRST value)

This sequence can generate two or four RXP events in the receiver if the RxFIFO threshold is set to 1 frame (and data is read on a frame basis, unpacked), or it can generate a single RXP event if the FTHLV[3:0] field in the SPI_CFG1 register is programmed to a multiple of the frames to be read in a packed mode (16-bit or 32-bit read access).

The data are aligned in accordance with Figure 594: Data alignment when data size is not equal to 8-bit, 16-bit or 32-bit . The valid bits are performed on the bus exclusively. Unused bits are not cared at transmitter while padded by zeros at receiver.

When short data frames (<8-bit or < 16-bit) are used together with a larger data access mode (16-bit or 32-bit), the FTHLV value must be programmed as a multiple of the number of frames/data access (that is a multiple of 4 if 32-bit access is used to up to 8-bit frames or multiple of 2 if 16-bit access is used to up to 8-bit frames or 32-bit access to up to 16-bit frames.).

The RxFIFO threshold setting must always be higher than the following read access size, as spurious extra data would be read otherwise.

The FIFO data access less than the configured data size is forbidden. One complete data frame has to be always accessed at minimum.

A specific problem appears if an incomplete data packet is available at FIFO: less than 4x8-bit frames or one single 16-bit frame is available.

There are two ways of dealing with this problem:

A. without using TSIZE field

On transmitter side, writing the last data frame of any odd sequence with an 8-bit/16-bit access to SPI_TXDR is enough.

On receiver side, the remaining data may be read by any access. Any extra data read are padded with zeros. Polling the RXWNE and RXPLVL may be used to detect when the RX data are available in the RxFIFO. (a time out may be used at system level in order to detect the polling)

B. using the TSIZE field

On transmitter side, the transaction is stopped by the master when it faces EOT event.

In reception, the RXP flag is not set when EOT is set. In the case when the number of data to be received (TSIZE) is not a multiple of packet size, the number of remaining data is indicated by the RXWNE and RXPLVL fields in the SPI_SR register. The remaining data can be read by any access. Any extra read is padded by zeros.

Figure 595. Packing data in FIFO for transmission and reception

Figure 595: Packing data in FIFO for transmission and reception. The diagram shows the SPI communication process between a transmitter (TX) and a receiver (RX). The TX side includes a TXFIFO and an SPI_TXDR register. The RX side includes an RXFIFO and an SPI_RXDR register. The communication is controlled by NSS, SCK, and MOSI lines. The SPI fsm & shift block on the TX side takes data from the TXFIFO and shifts it out on the MOSI line. The SPI fsm & shift block on the RX side takes data from the MOSI line and shifts it into the RXFIFO. The data frames shown on the bus are 0x0A, 0x04, 0x07, and 0x00. The configuration for the SPI is (CPHA=0, CPOL=0, LSBFRST=0, DSIZE[4:0]=00011). The TXFIFO after write access to SPI_TXDR contains 4x 8-bit (0x0A, 0x04, 0x07, 0x00) or 2x 16-bit (0x040A, 0x0007) or 1x 32-bit (0x0007040A). The RXFIFO before read access from SPI_RXDR contains the same data frames. The diagram is labeled MSv40474V3.

TXFIFO after write access to SPI_TXDR:
4x 8-bit (0x0A, 0x04, 0x07, 0x00) or
2x 16-bit (0x040A, 0x0007) or
1x 32-bit (0x0007040A)

RXFIFO before read access from SPI_RXDR:
4x 8-bit (0x0A, 0x04, 0x07, 0x00) or
2x 16-bit (0x040A, 0x0007) or
1x 32-bit (0x0007040A)

MSv40474V3

Figure 595: Packing data in FIFO for transmission and reception. The diagram shows the SPI communication process between a transmitter (TX) and a receiver (RX). The TX side includes a TXFIFO and an SPI_TXDR register. The RX side includes an RXFIFO and an SPI_RXDR register. The communication is controlled by NSS, SCK, and MOSI lines. The SPI fsm & shift block on the TX side takes data from the TXFIFO and shifts it out on the MOSI line. The SPI fsm & shift block on the RX side takes data from the MOSI line and shifts it into the RXFIFO. The data frames shown on the bus are 0x0A, 0x04, 0x07, and 0x00. The configuration for the SPI is (CPHA=0, CPOL=0, LSBFRST=0, DSIZE[4:0]=00011). The TXFIFO after write access to SPI_TXDR contains 4x 8-bit (0x0A, 0x04, 0x07, 0x00) or 2x 16-bit (0x040A, 0x0007) or 1x 32-bit (0x0007040A). The RXFIFO before read access from SPI_RXDR contains the same data frames. The diagram is labeled MSv40474V3.
  1. 1. DSIZE[3:0] is configured to 4-bit, data is right aligned, valid bits are performed only on the bus, their order depends on LSBFRST, if it is set, the order is reversed at all the data frames.

55.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 in 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 and 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 at SPI_SR register), the EOT (or TXC at case TISEZE=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 DMA Rx buffer in the RXDMAEN bit in 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 DMA Tx buffer in the TXDMAEN bit in 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 request 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 in 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.

If the DMA channel PSIZE value is equal to 16-bit or 32-bit and SPI data size is less than or equal to 8-bit, then packing mode is enabled. Similarly, If the DMA channel PSIZE value is equal to 32-bit and SPI data size is less than or equal to 16-bit, then packing mode is enabled. The DMA then automatically manages the write operations to the SPI_TXDR register.

Regardless data packing mode is used and the number of data to transfer is not a multiple of the DMA data size (16-bit or 32-bit) while the frame size is smaller, DMA completes the transfer automatically in according with the TSIZE field setting.

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

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

55.5 SPI specific modes and control

55.5.1 TI mode

By specific setting of the SP[2:0] bit field at the SPI_CFG2 register the SPI can be configured to be compliant with TI protocol. The SCK and NSS signals polarity, phase and flow as well as the bits order are fixed so the setting of CPOL, CPHA, LSBFIRST, SSOM, SSOE, SSIOP and SSM is not required when the SPI is at TI mode configuration. The NSS signal synchronizes the protocol by pulses over the LSB data bit as it is shown at the Figure 596: TI mode transfer .

Timing diagram for TI mode transfer showing NSS, SCK, MOSI, and MISO signals. 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 NSS signal is active-low, shown as a pulse that goes low before the first clock edge and returns high after the last clock edge. The MOSI signal is driven by the master, with data bits (MSB and LSB) being sampled by the slave on the falling edges of SCK. The MISO signal is driven by the slave, with data bits (MSB and LSB) being driven onto the line before the falling edges of SCK. The diagram shows two bytes being transferred. The first byte has its MSB and LSB. The second byte also has its MSB and LSB. The time interval between the start of the first byte and the start of the second byte is labeled as DSIZE[4:0] + 1. The time interval from the start of the second byte to the release of the MISO signal is also labeled as DSIZE[4:0] + 1. The release of the MISO signal is indicated by a dashed line and labeled TRELEASE. The diagram is labeled MSV40475V2.

Figure 596. TI mode transfer

Timing diagram for TI mode transfer showing NSS, SCK, MOSI, and MISO signals. 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 NSS signal is active-low, shown as a pulse that goes low before the first clock edge and returns high after the last clock edge. The MOSI signal is driven by the master, with data bits (MSB and LSB) being sampled by the slave on the falling edges of SCK. The MISO signal is driven by the slave, with data bits (MSB and LSB) being driven onto the line before the falling edges of SCK. The diagram shows two bytes being transferred. The first byte has its MSB and LSB. The second byte also has its MSB and LSB. The time interval between the start of the first byte and the start of the second byte is labeled as DSIZE[4:0] + 1. The time interval from the start of the second byte to the release of the MISO signal is also labeled as DSIZE[4:0] + 1. The release of the MISO signal is indicated by a dashed line and labeled TRELEASE. The diagram is labeled MSV40475V2.

In slave mode, the clock generator is used to define time when the slave output at MISO pin becomes to HiZ when the current transaction finishes. The master baud rate setting (MBR[2:0] at 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 HiZ ( \( T_{RELEASE} \) ) depends on internal re-synchronization, too, which takes next additional 2-4 periods of the clock signal feeding the generator. It is given by 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 misplaced NSS pulse during data transaction the TIFRE flag is set.

55.5.2 SPI error flags

An SPI interrupt is generated if one of the following error flags is set and 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. OVR flag triggers an interrupt if OVRIE bit is set. Clearing the OVR bit is done by a writing 1 to the OVRC bit in the SPI_IFCR. To prevent any next overrun event the clearing must be done after RxFIFO is emptied by software reads. It is suggested to release the RxFIFO space as much as possible, this means to read out all the available data packets based on the RXP flag indication. At master mode, the user can prevent the RxFIFO overrun by automatic communication suspend (MASRX bit).

Underrun flag (UDR)

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

Once the underrun is captured next provided data for transmission depends on the UDRCFG bits. The slave can provide either the last data stored into its TxFIFO, the data received previously from the master, or a constant pattern stored by the user in the SPI_UDRDR register. The second configuration can be used at circular topography structure (see Figure 588 ). Standard transmission is re-enabled once the software clears the UDR flag and this clearing is propagated into SPI logic by hardware. The user must write some data into TxFIFO prior clearing UDR flag to prevent any next underrun condition occurrence capture.

When the configuration UDRDET[1:0]=00 is applied, the underrun condition is evaluated whenever master starts to communicate a new data frame while TxFIFO is empty. Then single additional dummy (accidental) data is always inserted between last valid data and proper underrun pattern defined by UDRCFG[1:0]. This does not happen when any other UDRDET[1:0] configuration is applied suppose the slave's TxFIFO is not empty when underrun condition is checked (see Figure 597: Optional configurations of slave behavior at detection of underrun condition ).

The data transacted by slave is unpredictable especially when the transaction starts or continues while TxFIFO is empty and underrun condition is either not yet captured or just cleared. Typically, this is the case when UDRDET[1:0]=00 or SPI is just enabled or when a transaction with a defined size just starts. First bits can be corrupted in this case, as well, when slave software writes first data into the empty TxFIFO too close prior the data transaction starts (propagation of the data into TxFIFO takes few APB clock cycles). If the user cannot ensure to write data into the empty TxFIFO in time the UDRDET[1:0]=00 setting must be avoided.

To handle the underrun control feature correctly the user must avoid next critical encroachments especially

Figure 597. Optional configurations of slave behavior at detection of underrun condition

Timing diagrams showing four optional configurations of slave behavior at detection of underrun condition based on UDRDET[1:0] settings: 00, 01, 01, and 10. Each diagram shows SCK, MOSI, MISO, Tx FIFO occupancy, and UDR signals over time.

UDRDET[1:0]= 00

The diagram shows the following signals over time:

UDRDET[1:0]= 01

The diagram shows the following signals over time:

UDRDET[1:0]= 01

The diagram shows the following signals over time:

UDRDET[1:0]= 10

The diagram shows the following signals over time:

*) Depends on UDRCFG[1:0] setting
MSV63472V2

Timing diagrams showing four optional configurations of slave behavior at detection of underrun condition based on UDRDET[1:0] settings: 00, 01, 01, and 10. Each diagram shows SCK, MOSI, MISO, Tx FIFO occupancy, and UDR signals over time.

Note: The hardware propagation of an UDR event needs additional traffic on the bus. It always takes 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 data frame transaction or when the SCK line is at idle in between the frames, the next extra underrun pattern is sent initially by slave prior the valid data from TxFIFO becomes transacted 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 slave select logic input (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 in the SPI_IFCR.

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

As a security, 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 SW procedure when master overtakes the bus at multimaster system must be the following one:

CRC error (CRCE)

This flag is used to verify the validity of the value received when the CRCEN bit in the SPI_CFG1 register is set. The CRCE flag in 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 RCEIE bit is set. Clearing the bit CRCE is done by a writing 1 to the CRCEC bit in the SPI_IFCR.

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 few data bytes.

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

55.5.3 CRC computation

Two separate 33-bit or two separate 17-bit CRC calculators are implemented in order to check the reliability of transmitted and received data. The SPI offers any CRC polynomial length ( number of bits involved in the CRC computation ) from 5 to 33 bits when maximum data size is 32-bit and from 5 to 17 bits for the peripheral instances where maximum data size is limited to 16-bit.

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 \) , write 0x11021 to the SPI_CRCPOLY register for a 32-bit instance, whereas to obtain the full size for a 16-bit instance, write 0x1021 with the CRC33_17 bit set.

The CRCSIZE field in the SPI_CFG1 then defines how many of the most significant bits from CRC calculation registers are transacted 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 while 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 pattern size, else the bits unused at the calculation are transacted and expected all zero at the end of the CRC pattern if its size is set greater than the polynomial length.

CRC principle

The CRC calculation is enabled by setting the CRCEN bit in 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 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 in 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 CRCERR 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 to be sent or received in the SPI_DR register.

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

TSIZE cannot be set to 0xFFFF value if CRC is enabled. A correct way of sending e.g. 65535 data with CRC is to set:

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

In reception, the RXCRC is also frozen when desired number of data is transacted. 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 completed, an automatic check is performed comparing the received CRC value and the value calculated in the SPI_RXCRC register. Software has to check the CRCERR flag in the SPI_SR register to determine if the data transfers were corrupted or not. Software clears the CRCERR flag by writing 1 to the CRCERRC.

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 in order 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 at SPI_CR1 register.

The CRC values are reset when the SPI is disabled.

55.6 Low-power mode management

The SPI has advanced low-power mode functions allowing it to transfer properly data between the FIFOs and the serial interface even when the spi_pclk clock is disabled.

In master mode the spi_ker_ck kernel clock is needed in order to provide the timings of the serial interface.

In slave mode, the spi_ker_ck clock can be removed as well during the transfer of data between the FIFOs and the serial interface. In this mode the clock is provided by the external SPI device.

When the spi_pclk clock is gated, (and the spi_ker_ck clock as well if the SPI is in slave), the SPI provides a wake-up event signal ( spi_wkup ) if a specific action requiring the activation of the spi_pclk clock is needed, such as:

The generation of spi_ker_ck and spi_pclk clock are controlled by the RCC block according to register settings and the processors modes. Refer to the RCC section for details.

The spi_pclk clock request stays pending till a flag with enabled interrupt is set. That 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 (by removing spi_pclk ).

The Figure 598 shows an example of the clock handling when the SPI2S is working in low-power mode. The example is given for a transmit mode.

In master mode the spi_ker_ck clock is required for the timing generation.

The Figure 598 shows two kinds of supported scenarios for the handling of the spi_ker_ck kernel clock in slave mode:

Figure 598. Low-power mode application example

Timing diagram for SPI low-power mode application example. The diagram shows the relationship between SPI communication, TxFIFO level, spi_wkup, spi_it, spi_pclk, and spi_ker_ck signals over time. The TxFIFO level is shown in two states: 'Less than FTHLV empty locations' (dark grey) and 'At least FTHLV empty locations' (light grey). The spi_wkup signal is active when the TxFIFO level is below the threshold. The spi_it signal is active when spi_pclk is active and the TxFIFO level is below the threshold. The spi_pclk signal is active when spi_wkup is active. The spi_ker_ck signal is active in master mode and in slave mode when the TxFIFO level is below the threshold. The diagram is divided into four steps: 1. TxFIFO level goes below FTHLV, spi_wkup becomes active. 2. spi_pclk becomes active, spi_it becomes active. 3. TxFIFO level goes above FTHLV, spi_wkup and spi_it become inactive. 4. spi_pclk becomes inactive. The legend indicates: Less than FTHLV empty locations (dark grey), At least FTHLV empty locations (light grey). MSV40476V2
Timing diagram for SPI low-power mode application example. The diagram shows the relationship between SPI communication, TxFIFO level, spi_wkup, spi_it, spi_pclk, and spi_ker_ck signals over time. The TxFIFO level is shown in two states: 'Less than FTHLV empty locations' (dark grey) and 'At least FTHLV empty locations' (light grey). The spi_wkup signal is active when the TxFIFO level is below the threshold. The spi_it signal is active when spi_pclk is active and the TxFIFO level is below the threshold. The spi_pclk signal is active when spi_wkup is active. The spi_ker_ck signal is active in master mode and in slave mode when the TxFIFO level is below the threshold. The diagram is divided into four steps: 1. TxFIFO level goes below FTHLV, spi_wkup becomes active. 2. spi_pclk becomes active, spi_it becomes active. 3. TxFIFO level goes above FTHLV, spi_wkup and spi_it become inactive. 4. spi_pclk becomes inactive. The legend indicates: Less than FTHLV empty locations (dark grey), At least FTHLV empty locations (light grey). MSV40476V2

The figure clearly shows that the spi_pclk must be provided to the SPI2S, when data need to be transferred from the memory to the SPI2S TxFIFO. Here is the description of the most important steps:

continue. Note that spi_wkup falling edge is aligned with the serial interface clock domain, and the falling edge of the spi_it is aligned with the spi_pclk clock domain.

The fill-up of the TxFIFO is completed; the software can switch the system back to low-power mode until the next spi_wkup occurs.

55.7 SPI wake-up and interrupts

Table 413 gives an overview of the SPI events capable to generate interrupt events ( spi_it ). Some of them feature wake-up from low-power mode capability additionally ( spi_wkup ).

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. All the pending interrupt requests stay active if the SPI is disabled. A not cleared request with an enabled interrupt generates a spi_pclk clock request and so increases the overall consumption.

Table 413. SPI wake-up and interrupt requests

Interrupt eventEvent flag (1)Enable Control bitEvent clear methodInterrupt/Wake-up activated
spi_itspi_wkup
TxFIFO 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
OverrunOVROVRIEWriting OVRC to 1YES
CRC ErrorCRCECRCEIEWriting CRCEC to 1YESYES
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 (2)TXC cleared by HW when a transmission activity starts on the busNO
TSER value transferred to TSIZE (new value may be loaded to TSER)TSERFTSERFIEWriting TSERFC to 1NO

1. Refer to SPI2S register description for more details about the event flags.

2. The TXC flag behavior depends on the TSIZE setting. When TSIZE>0, the flag fully follows the EOT one including its clearing by EOTC.

55.8 I2S main features

Note: \( F_{MCK} \) is the master clock frequency and \( F_{WS} \) is the audio sampling frequency.


a. 24- and 32-bit data width are not always available. Refer to Section 55.3: SPI implementation .

55.9 I2S functional description

55.9.1 I2S general description

The block diagram shown on Figure 583 also applies for I2S mode.

The SPI/I2S block can work on I2S/PCM mode, when the bit I2SMOD is set to 1. A dedicated register (SPI_I2SCFGR) is available for configuring the dedicated I2S parameters, which include the clock generator, and the serial link interface.

The I2S/PCM function uses the clock generator to produce the communication clock when the SPI/I2S is set in master mode. This clock generator is also the source of the master clock output (MCK).

Resources such as RxFIFO, TxFIFO, DMA and parts of interrupt signaling are shared with SPI function. The low-power mode function is also available in I2S mode, refer to Section 55.6: Low-power mode management and Section 55.10: I2S wake-up and interrupts .

55.9.2 Pin sharing with SPI function

The I2S shares four common pins with the SPI:

An additional pin can be used when a master clock output is needed for some external audio devices:

55.9.3 Bitfields usable in I2S/PCM mode

When the I2S/PCM mode is selected (I2SMOD = '1'), some bitfields are no longer relevant, and must be forced to a specific value in order to guarantee the behavior of the I2S/PCM function. Table 414 shows the list of bits and fields available in the I2S/PCM mode, and indicates which must be forced to a specific value.

Table 414. Bitfields usable in PCM/I2S mode

Register nameBitfields usable in PCM/I2S ModeConstraints on other bitfields
SPI/I2S control register 1 (SPI_CR1)IOLOCK, CSUSP, CSTART, SPEOther fields set to their reset values
SPI control register 2 (SPI_CR2)-Set to reset value
SPI configuration register 1 (SPI_CFG1)TXDMAEN, RXDMAEN, FTHLVOther fields set to their reset values
SPI configuration register 2 (SPI_CFG2)AFCNTR, LSBFRST, IOSWPOther fields set to their reset values
SPI/I2S interrupt enable register (SPI_IER)TIFREIE, OVRIE, UDRIE, TXPIE, RXPIE
SPI/I2S status register (SPI_SR)SUSP, TIFRE, OVR, UDR, TXP, RXPOther flags not relevant
SPI/I2S interrupt/status flags clear register (SPI_IFCR)SUSPC, TIFREC, OVRC, UDRCOther fields set to their reset values
SPI/I2S transmit data register (SPI_TXDR)The complete register-
SPI/I2S receive data register (SPI_RXDR)The complete register-
SPI polynomial register (SPI_CRCPOLY)-Set to reset value
SPI transmitter CRC register (SPI_TXCRC)-
SPI receiver CRC register (SPI_RXCRC)-
SPI underrun data register (SPI_UDRDR)-
SPI/I2S configuration register (SPI_I2SCFGR)The complete register

55.9.4 Slave and master modes

The SPI/I2S block supports master and slave mode for both I2S and PCM protocols.

In master mode, both CK, WS and MCK signals are set to output.

In slave mode, both CK and WS signals are set to input. The signal MCK cannot be used in slave mode.

In order to improve the robustness of the SPI/I2S block in slave mode, the peripheral re-synchronizes each reception and transmission on WS signal. This means that:

Note: This re-synchronization mechanism is not available for the I2S LSB justified standard.

Note: Note as well that there is no need to provide a kernel clock when the SPI/I2S is configured in slave mode.

55.9.5 Supported audio protocols

The I2S/PCM interface supports four audio standards, configurable using the I2SSTD[1:0] and PCMSYNC bits in the SPI_I2SCFGR register.

In the I2S protocol, the audio data are time-multiplexed on two channels: the left channel and the right channel. The WS signal is used to indicate which channel shall be considered as the left, and which one is the right.

In I2S master mode, four frames formats are supported:

In PCM master mode, three frames formats are supported:


a. 24- and 32-bit data width are not always available. Refer to Section 55.3: SPI implementation .

The figure hereafter shows the main definition used in this section: data length, channel length and frame length.

Figure 599. Waveform examples Figure 599. Waveform examples. The figure contains two timing diagrams. The top diagram is for I2S mode, showing MCK, WS, CK (CKPOL=0), and SD signals. It illustrates Data length, Channel length, and Frame length. The sampling rate is given as Fws = Fmck/256. The bottom diagram is for PCM mode, showing similar signals with a sampling rate of Fws = Fmck/128 and defining Channel length = Frame length.

MCK

\( F_{MCK} \)

Sampling rate at \( F_{WS} = F_{MCK}/256 \)

WS

CK
(CKPOL = 0)

SD
(In or Out)

Serial data

Data length

Channel length

Frame length

I2S

Sampling rate at \( F_{WS} = F_{MCK}/128 \)

WS

CK
(CKPOL = 0)

SD
(In or Out)

Serial data

Data length

Channel length = Frame length

PCM

MSv40477V3

Figure 599. Waveform examples. The figure contains two timing diagrams. The top diagram is for I2S mode, showing MCK, WS, CK (CKPOL=0), and SD signals. It illustrates Data length, Channel length, and Frame length. The sampling rate is given as Fws = Fmck/256. The bottom diagram is for PCM mode, showing similar signals with a sampling rate of Fws = Fmck/128 and defining Channel length = Frame length.
  1. 1. The channel length (CHLEN) must always be higher or equal to the data length (DATLEN).

I 2 S Philips standard

The I2S Philips standard is selected by setting I2SSTD to 0b00. This standard is supported in master and slave mode.

In this standard, the WS signal toggles one CK clock cycle before the first bit (MSb in I2S Philips standard) is available. A falling edge transition of WS indicates that the next data transferred is the left channel, and a rising edge transition indicates that the next data transferred is the right channel.

Figure 600. Master I2S Philips protocol waveforms (16/32-bit full accuracy)

Figure 600: Master I2S Philips protocol waveforms (16/32-bit full accuracy). The diagram shows three signal lines: WS (Word Select), CK (Clock), and SDO or SDI (Serial Data). The WS signal is a square wave that toggles one clock cycle before the first bit. The CK signal is a square wave. The SDO or SDI signal shows a sequence of bits, with the first bit being the MSb (Most Significant bit) and the last bit being the LSb (Least Significant bit). The data is divided into two channels: Left channel and Right channel. Each channel can be 16 or 32 bits long. The diagram also shows the 'Data stable' point, which is the falling edge of the WS signal. The reference MSv40481V3 is shown in the bottom right corner.
Figure 600: Master I2S Philips protocol waveforms (16/32-bit full accuracy). The diagram shows three signal lines: WS (Word Select), CK (Clock), and SDO or SDI (Serial Data). The WS signal is a square wave that toggles one clock cycle before the first bit. The CK signal is a square wave. The SDO or SDI signal shows a sequence of bits, with the first bit being the MSb (Most Significant bit) and the last bit being the LSb (Least Significant bit). The data is divided into two channels: Left channel and Right channel. Each channel can be 16 or 32 bits long. The diagram also shows the 'Data stable' point, which is the falling edge of the WS signal. The reference MSv40481V3 is shown in the bottom right corner.
  1. 1. 24- and 32-bit data width are not always supported (DATLEN = 01 or 10). Refer to Section 55.3: SPI implementation for the supported data sizes.

CKPOL is set to 0 in order to match the I2S Philips protocol. See Selection of the CK sampling edge for information concerning the handling of WS signal.

Figure 600 shows an example of waveform generated by the SPI/I2S in the case where the channel length is equal to the data length. More precisely, this is true when CHLEN = 0 and DATLEN = 0b00 or when CHLEN = 1 and DATLEN = 0b10.

See Control of the WS Inversion for information concerning the handling of WS signal.

Figure 601. I2S Philips standard waveforms

Figure 601: I2S Philips standard waveforms. The diagram shows three signal lines: WS (I/O), CK (I/O), and SDO or SDI (LSBFRST = 0). The WS signal is a square wave that toggles one clock cycle before the first bit. The CK signal is a square wave. The SDO or SDI signal shows a sequence of bits, with the first bit being the LSb (Least Significant bit) and the last bit being the MSb (Most Significant bit). The data is divided into two channels: Left channel and Right channel. Each channel can be 16 or 24 bits long, with the remaining bits being not significant. The diagram also shows the 'Data stable' point, which is the falling edge of the WS signal. The reference MSv40482V3 is shown in the bottom right corner.
Figure 601: I2S Philips standard waveforms. The diagram shows three signal lines: WS (I/O), CK (I/O), and SDO or SDI (LSBFRST = 0). The WS signal is a square wave that toggles one clock cycle before the first bit. The CK signal is a square wave. The SDO or SDI signal shows a sequence of bits, with the first bit being the LSb (Least Significant bit) and the last bit being the MSb (Most Significant bit). The data is divided into two channels: Left channel and Right channel. Each channel can be 16 or 24 bits long, with the remaining bits being not significant. The diagram also shows the 'Data stable' point, which is the falling edge of the WS signal. The reference MSv40482V3 is shown in the bottom right corner.
  1. 1. 24- and 32-bit data width are not always supported (DATLEN = 01 or 10). Refer to Section 55.3: SPI implementation for the supported data sizes.

In the case where the channel length is bigger than the data length, the remaining bits are forced to zero when the SPI/I2S is configured in transmit mode. This is applicable for both master and slave mode.

MSB justified standard

For this standard, the WS signal toggles when the first data bit, is provided. The data transferred represents the left channel if WS is high, and the right channel if WS is low.

Figure 602. Master MSB Justified 16-bit or 32-bit full-accuracy length

Timing diagram for Master MSB Justified 16-bit or 32-bit full-accuracy length. It shows the relationship between the Word Select (WS), Clock (CK), and Serial Data (SDO or SDI) signals. The WS signal is high for the left channel and low for the right channel. The CK signal is a square wave. The SDO or SDI signal shows a sequence of bits, with the Most Significant Bit (MSb) and Least Significant Bit (Lsb) indicated. The data is transferred in 16 or 32-bit chunks for each channel. The diagram is labeled with 'Data stable' and 'MSv40480V2'.

The diagram illustrates the timing for Master MSB Justified mode with 16-bit or 32-bit full-accuracy length. The top signal is the Word Select (WS) output, which is high for the left channel and low for the right channel. The second signal is the Clock (CK) output, shown as a square wave. The third signal is the Serial Data (SDO or SDI) output, which is a sequence of bits. The data is transferred in 16 or 32-bit chunks for each channel. The diagram shows the left channel data first, followed by the right channel data. The data is stable when the WS signal is high for the left channel and low for the right channel. The diagram is labeled with 'Data stable' and 'MSv40480V2'.

Timing diagram for Master MSB Justified 16-bit or 32-bit full-accuracy length. It shows the relationship between the Word Select (WS), Clock (CK), and Serial Data (SDO or SDI) signals. The WS signal is high for the left channel and low for the right channel. The CK signal is a square wave. The SDO or SDI signal shows a sequence of bits, with the Most Significant Bit (MSb) and Least Significant Bit (Lsb) indicated. The data is transferred in 16 or 32-bit chunks for each channel. The diagram is labeled with 'Data stable' and 'MSv40480V2'.
  1. 1. 24- and 32-bit data width are not always supported (DATLEN = 01 or 10). Refer to Section 55.3: SPI implementation for the supported data sizes.

CKPOL is set to 0 in order to match the I2S MSB justified protocol. See Selection of the CK sampling edge for information concerning the handling of WS signal.

See Control of the WS Inversion for information concerning the handling of WS signal.

Figure 603. Master MSB justified 16 or 24-bit data length

Timing diagram for Master MSB justified 16 or 24-bit data length. It shows the relationship between the Word Select (WS), Clock (CK), and Serial Data (SDO or SDI) signals. The WS signal is high for the left channel and low for the right channel. The CK signal is a square wave. The SDO or SDI signal shows a sequence of bits, with the Most Significant Bit (MSb) and Least Significant Bit (Lsb) indicated. The data is transferred in 16 or 24-bit chunks for each channel. The remaining bits are forced to 0. The diagram is labeled with 'Data stable' and 'MSv40479V3'.

The diagram illustrates the timing for Master MSB justified mode with 16 or 24-bit data length. The top signal is the Word Select (WS) output, which is high for the left channel and low for the right channel. The second signal is the Clock (CK) output, shown as a square wave. The third signal is the Serial Data (SDO or SDI) output, which is a sequence of bits. The data is transferred in 16 or 24-bit chunks for each channel. The remaining bits are forced to 0. The diagram shows the left channel data first, followed by the right channel data. The data is stable when the WS signal is high for the left channel and low for the right channel. The diagram is labeled with 'Data stable' and 'MSv40479V3'.

Timing diagram for Master MSB justified 16 or 24-bit data length. It shows the relationship between the Word Select (WS), Clock (CK), and Serial Data (SDO or SDI) signals. The WS signal is high for the left channel and low for the right channel. The CK signal is a square wave. The SDO or SDI signal shows a sequence of bits, with the Most Significant Bit (MSb) and Least Significant Bit (Lsb) indicated. The data is transferred in 16 or 24-bit chunks for each channel. The remaining bits are forced to 0. The diagram is labeled with 'Data stable' and 'MSv40479V3'.
  1. 1. 24- and 32-bit data width are not always supported (DATLEN = 01 or 10). Refer to Section 55.3: SPI implementation for the supported data sizes.

In the case where the channel length is bigger than the data length, the remaining bits are forced to zero when the SPI/I2S is configured in master transmit mode. In slave transmit the remaining bits are forced to the value of the first bit of the next data to be generated in order to avoid timing issues (see Figure 604 ).

Figure 604. Slave MSB justified

Timing diagram for Slave MSB justified SPI mode. It shows the relationship between the Word Select (WS), Clock (CK), Serial Data Out (SDO), and Serial Data In (SDI) signals. The WS signal is active low (WSINV = 0). The CK signal has a phase of 0 (CKPOL = 0). The SDO signal is shown as a sequence of bits, with the Most Significant Bit (MSb) and Least Significant Bit (LSb) indicated. The data is transferred in 16, 24, or 32-bit lengths. The remaining bits are forced to the MSb of the next right channel. The SDI signal is shown as a sequence of bits, with the MSb and LSb indicated. The data is not taken into account. The diagram is divided into Left and Right channels. The timing diagram is labeled MSv40478V3.
Timing diagram for Slave MSB justified SPI mode. It shows the relationship between the Word Select (WS), Clock (CK), Serial Data Out (SDO), and Serial Data In (SDI) signals. The WS signal is active low (WSINV = 0). The CK signal has a phase of 0 (CKPOL = 0). The SDO signal is shown as a sequence of bits, with the Most Significant Bit (MSb) and Least Significant Bit (LSb) indicated. The data is transferred in 16, 24, or 32-bit lengths. The remaining bits are forced to the MSb of the next right channel. The SDI signal is shown as a sequence of bits, with the MSb and LSb indicated. The data is not taken into account. The diagram is divided into Left and Right channels. The timing diagram is labeled MSv40478V3.
  1. 1. 24- and 32-bit data width are not always supported (DATLEN = 01 or 10). Refer to Section 55.3: SPI implementation for the supported data sizes.

LSB justified standard

This standard is similar to the MSB justified standard in master mode (no difference for the 16 and 32-bit full-accuracy frame formats). The LSB justified 16 or 32-bit full-accuracy format give similar waveforms than MSB justified mode (see Figure 602 ) because the channel and data have the same length.

Note:

In the LSB justified format, only 16 and 32-bit channel length are supported in master and slave mode. This is due to the fact that it is not possible to transfer properly the data if the channel length is not known by transmitter and receiver side.

Figure 605. LSB justified 16 or 24-bit data length

Timing diagram for LSB justified SPI mode with 16 or 24-bit data length. It shows the relationship between the Word Select (WS), Clock (CK), Serial Data Out (SDO), and Serial Data In (SDI) signals. The WS signal is active low (WSINV = 0). The CK signal has a phase of 0 (CKPOL = 0). The SDO signal is shown as a sequence of bits, with the Least Significant Bit (LSb) and Most Significant Bit (MSb) indicated. The data is transferred in 16 or 24-bit lengths. The remaining bits are forced to 0. The SDI signal is shown as a sequence of bits, with the LSb and MSb indicated. The data is not taken into account. The diagram is divided into Left and Right channels, each with a 32-bit channel length. The timing diagram is labeled MSv40483V3.
Timing diagram for LSB justified SPI mode with 16 or 24-bit data length. It shows the relationship between the Word Select (WS), Clock (CK), Serial Data Out (SDO), and Serial Data In (SDI) signals. The WS signal is active low (WSINV = 0). The CK signal has a phase of 0 (CKPOL = 0). The SDO signal is shown as a sequence of bits, with the Least Significant Bit (LSb) and Most Significant Bit (MSb) indicated. The data is transferred in 16 or 24-bit lengths. The remaining bits are forced to 0. The SDI signal is shown as a sequence of bits, with the LSb and MSb indicated. The data is not taken into account. The diagram is divided into Left and Right channels, each with a 32-bit channel length. The timing diagram is labeled MSv40483V3.
  1. 1. 24- and 32-bit data width are not always supported (DATLEN = 01 or 10). Refer to Section 55.3: SPI implementation for the supported data sizes.

CKPOL is set to 0 in order to match the I2S LSB justified protocol. See Selection of the CK sampling edge for information concerning the handling of WS signal.

See Control of the WS Inversion for information concerning the handling of WS signal.

PCM standard

For the PCM standard, there is no need to use channel-side information. The two PCM modes (short and long frame) are available and configurable using the PCMSYNC bit in SPI_I2SCFGR register.

Note: The difference between the PCM long and short frame, is just the width of the frame synchronization: for both protocols, the active edge of the frame is generated (or is expected for the Slave mode) one CK clock cycle before the first bit.

Figure 606. Master PCM when the frame length is equal the data length

Figure 606: Master PCM when the frame length is equal the data length. This timing diagram shows four signals: WS (O), CK (O), SDO or SDI, and Data stable. The WS signal is active low. Two cases are shown: PCMSYNC = 0, WSINV = 0 (short frame) where WS is one CK cycle high; and PCMSYNC = 1, WSINV = 0 (long frame) where WS is 13 bits high. The CK signal is a square wave with CKPOL = 0. The SDO or SDI signal shows 16-bit data frames, each starting with LSB and ending with MSB. The Data stable point is indicated at the rising edge of CK. The diagram is labeled MSV40484V1.
Figure 606: Master PCM when the frame length is equal the data length. This timing diagram shows four signals: WS (O), CK (O), SDO or SDI, and Data stable. The WS signal is active low. Two cases are shown: PCMSYNC = 0, WSINV = 0 (short frame) where WS is one CK cycle high; and PCMSYNC = 1, WSINV = 0 (long frame) where WS is 13 bits high. The CK signal is a square wave with CKPOL = 0. The SDO or SDI signal shows 16-bit data frames, each starting with LSB and ending with MSB. The Data stable point is indicated at the rising edge of CK. The diagram is labeled MSV40484V1.

For long frame synchronization, the WS signal assertion time is fixed to 13 bits in master mode.

A data size of 16 or 24 bits can be used when the channel length is set to 32 bits.

For short frame synchronization, the WS synchronization signal is only one cycle long.

See Control of the WS Inversion for information concerning the handling of WS signal.

Figure 607. Master PCM standard waveforms (16 or 24-bit data length)

Figure 607: Master PCM standard waveforms (16 or 24-bit data length). This timing diagram shows four signals: WS (O), CK (O), SDO, and SDI. The WS signal is active low. Two cases are shown: PCMSYNC = 0, WSINV = 0 (short frame) where WS is one CK cycle high; and PCMSYNC = 1, WSINV = 0 (long frame) where WS is 13 bits high. The CK signal is a square wave with CKPOL = 0. The SDO signal shows 16 or 24-bit data frames, each starting with MSB and ending with LSB, with remaining bits forced to 0. The SDI signal shows 32-bit frames, each starting with MSB and ending with LSB, with 'Don't care' bits in between. The Data stable point is indicated at the rising edge of CK. The diagram is labeled MSV40485V1.
Figure 607: Master PCM standard waveforms (16 or 24-bit data length). This timing diagram shows four signals: WS (O), CK (O), SDO, and SDI. The WS signal is active low. Two cases are shown: PCMSYNC = 0, WSINV = 0 (short frame) where WS is one CK cycle high; and PCMSYNC = 1, WSINV = 0 (long frame) where WS is 13 bits high. The CK signal is a square wave with CKPOL = 0. The SDO signal shows 16 or 24-bit data frames, each starting with MSB and ending with LSB, with remaining bits forced to 0. The SDI signal shows 32-bit frames, each starting with MSB and ending with LSB, with 'Don't care' bits in between. The Data stable point is indicated at the rising edge of CK. The diagram is labeled MSV40485V1.
  1. 1. 24- and 32-bit data width are not always supported (DATLEN = 01 or 10). Refer to Section 55.3: SPI implementation for the supported data sizes.

If the PCM protocol is used in slave mode, frame lengths can be different from 16 or 32 bits. As shown in Figure 608 , in slave mode various pulse widths of WS can be accepted as the start of frame is detected by a rising edge of WS. The only constraint is that the WS must go back to its inactive state for at least one CK cycle.

Figure 608. Slave PCM waveforms

Figure 608. Slave PCM waveforms. Timing diagram showing WS (I), CK (I), SDO, and SDI signals. WS (I) is active low (PCMSYNC = 0). CK (I) is active low (CKPOL = 0). SDO is output (LSBFIRST = 0). SDI is input. The diagram shows two consecutive 16 or 24-bit data frames. The falling edge of WS indicates the start of a frame. The falling edge of CK is the sampling edge for SDI and the driving edge for SDO. The data is transferred in bytes, with the MSB and LSB of each byte shown. The remaining bits are not significant. The minimum CK cycle time is 1 CK cycle. The diagram is labeled MSv40486V2.
Figure 608. Slave PCM waveforms. Timing diagram showing WS (I), CK (I), SDO, and SDI signals. WS (I) is active low (PCMSYNC = 0). CK (I) is active low (CKPOL = 0). SDO is output (LSBFIRST = 0). SDI is input. The diagram shows two consecutive 16 or 24-bit data frames. The falling edge of WS indicates the start of a frame. The falling edge of CK is the sampling edge for SDI and the driving edge for SDO. The data is transferred in bytes, with the MSB and LSB of each byte shown. The remaining bits are not significant. The minimum CK cycle time is 1 CK cycle. The diagram is labeled MSv40486V2.
  1. 1. 24- and 32-bit data width are not always supported (DATLEN = 01 or 10). Refer to Section 55.3: SPI implementation for the supported data sizes.

CKPOL is set to 0 in order to match the PCM protocol. See Selection of the CK sampling edge for information concerning the handling of WS signal.

55.9.6 Additional Serial Interface Flexibility

Variable frame length in slave

In slave mode, channel lengths different from 16 or 32 bits can be accepted, as long as the channel length is bigger than the data length. This is true for all protocols except for I2S LSB justified protocol.

Data ordering

For all data formats and communication standards, it is possible to select the data ordering (MSb or LSb first) thanks to the bit LSBFRST located into SPI configuration register 2 (SPI_CFG2) .

Selection of the CK sampling edge

The CKPOL bit located into SPI/I2S configuration register (SPI_I2SCFGR) allows the user to choose the sampling edge polarity of the CK for slave and master modes, for all protocols.

Control of the WS Inversion

It is possible to invert the default WS signal polarity for master and slave modes, for all protocols, by setting WSINV to 1. By default the WS polarity is the following:

When WSINV is set to 1, the WS polarity is inverted, then:

WSINV is located into SPI/I2S configuration register (SPI_I2SCFGR) .

Control of the I/Os

The SPI/I2S block allows the settling of the WS and CK signals to their inactive state before enabling the SPI/I2S thanks to the AFCNTR bit of SPI configuration register 2 (SPI_CFG2) .

This can be done by programming CKPOL and WSINV using the following sequence:

Assuming that AFCNTR is initially set to 0

Table 415 shows the level of WS and CK signals, when the AFCNTR bit is set to 1, and before the SPI/I2S block is enabled (inactive level). Note that the level of WS depends also on the protocol selected.

Table 415. WS and CK level before SPI/I2S is enabled when AFCNTR = 1

WSINVI2SSTDWS level before SPI/I2S is enabledCKPOLCK level before SPI/I2S is enabled
0I2S Std (00)High0Low
OthersLow1High
1I2S Std (00)Low
OthersHigh

Note: To avoid electrical conflicts on WS and CK, the AFCNTR bit must not be set to 1 when the SPI2S is in slave mode.

Serial Data Line swapping

The SPI/I2S offers the possibility to swap the function of SDI and SDO lines thanks to IOSWP bit located into SPI configuration register 2 (SPI_CFG2) . Table 416 gives details on this feature.

Table 416. Serial data line swapping

ConfigurationIOSWPSDI directionSDO direction
Master/slave RX0IN-
1-IN

Table 416. Serial data line swapping (continued)

ConfigurationIOSWPSDI directionSDO direction
Master/slave TX0-OUT
1OUT-
Master/slave Full-duplex0INOUT
1OUTIN

For simplification, the waveforms shown in the I2S functional description section have been done with IOSWP = 0.

55.9.7 Startup sequence

When the bit SPE is set to 0, the user is not allowed to read and write into the SPI_RXDR and SPI_TXDR registers, but the access to other registers is allowed.

When the application wants to use the SPI/I2S block the user has to proceed as follow:

  1. 1. Ensure that the SPE is set to 0, otherwise write SPE to 0.
  2. 2. Program all the configuration and control registers according to the wanted configuration. Refer to Section 55.9.16 for detailed programming examples.
  3. 3. Clear all the status flags by setting the SUSPC, TIFREC, OVRC and UDRC bits of SPI_IFCR register. Note that if the flag SUSP is not cleared (via SUSPC bit) the CSTART control bit has no effect.
  4. 4. Set the SPE bit to 1, in order to activate the SPI/I2S block. When this bit is set, the serial interface is still disabled, but the DMA and interrupt services are working, allowing for example, the data transfer into the TxFIFO.
  5. 5. Set bit CSTART to 1, in order to activate the serial interface.

As shown in Figure 609 , in I2S Philips standard master TX, the generation of the WS and CK signals starts after a resynchronization delay (SYNC_DLY2) when CSTART goes to 1 and the TxFIFO is not empty. Note that the bit clock CK is activated 4 rising edges before the falling edge of WS in order to ensure that the external slave device can detect properly WS transition. Other standards behave similarly.

Figure 609. Startup sequence, I2S Philips standard, master

Timing diagram for I2S Philips standard master startup sequence. The diagram shows the relationship between AFCNTR, MCK (O), WS(O) (WSINV = 0), CK(O) (CKPOL = 0), SDO (LSBFIRST = 0), CSTART, SPE, and Accesses to SPI2S block. The sequence is divided into three phases: SPI2S configuration, Data transfer to the TX_FIFO, and Enabling of the serial interface. The MCK signal is enabled before setting the SPE bit to 1. The WS signal is controlled by the SPI/I2S block during the configuration phase. The CK signal is generated by the SPI/I2S block. The SDO signal is driven by the SPI/I2S block. The CSTART bit is taken into account by the hardware after about 3 periods of CK clock (SYNC_DLY1). The SPE bit is set to 1 to enable the serial interface. The Accesses to SPI2S block are shown as a series of pulses. The Right channel bits are indicated by a legend.
Timing diagram for I2S Philips standard master startup sequence. The diagram shows the relationship between AFCNTR, MCK (O), WS(O) (WSINV = 0), CK(O) (CKPOL = 0), SDO (LSBFIRST = 0), CSTART, SPE, and Accesses to SPI2S block. The sequence is divided into three phases: SPI2S configuration, Data transfer to the TX_FIFO, and Enabling of the serial interface. The MCK signal is enabled before setting the SPE bit to 1. The WS signal is controlled by the SPI/I2S block during the configuration phase. The CK signal is generated by the SPI/I2S block. The SDO signal is driven by the SPI/I2S block. The CSTART bit is taken into account by the hardware after about 3 periods of CK clock (SYNC_DLY1). The SPE bit is set to 1 to enable the serial interface. The Accesses to SPI2S block are shown as a series of pulses. The Right channel bits are indicated by a legend.
  1. 1. In this figure, the MCK is enabled before setting the bit SPE to 1. See MCK generation for more information.
  2. 2. Note that the level of WS and CK signals are controlled by the SPI/I2S block during the configuration phase as soon as the AFCNTR bit is set to 1

Note: Due to clock domain resynchronization, the CSTART bit is taken into account by the hardware after about 3 periods of CK clock (SYNC_DLY1).

In slave mode, once the bit CSTART is set to 1, the data transfer starts when the start-of-frame condition is met:

Figure 610 shows an example of startup sequence in I2S Philips standard, slave mode.

Figure 610. Startup sequence, I2S Philips standard, slave

Timing diagram for I2S Philips standard slave startup sequence. The diagram shows the relationship between WS(I) (WSINV = 0), CK (I), SDO, SDI, CSTART, SPE, and SYNC_DLY. The sequence is divided into two phases: Searching for the start of frame and Start-of-frame condition found, the reception and transmission of data can start. The WS signal is controlled by the master. The CK signal is generated by the master. The SDO signal is driven by the master. The SDI signal is driven by the master. The CSTART bit is taken into account by the hardware after 2 periods of CK clock (SYNC_DLY). The SPE bit is set to 1 to enable the serial interface. The Searching for the start of frame phase is indicated by a shaded area. The Start-of-frame condition found, the reception and transmission of data can start phase is indicated by a text box. The Not stored into the RX-FIFO text box is shown.
Timing diagram for I2S Philips standard slave startup sequence. The diagram shows the relationship between WS(I) (WSINV = 0), CK (I), SDO, SDI, CSTART, SPE, and SYNC_DLY. The sequence is divided into two phases: Searching for the start of frame and Start-of-frame condition found, the reception and transmission of data can start. The WS signal is controlled by the master. The CK signal is generated by the master. The SDO signal is driven by the master. The SDI signal is driven by the master. The CSTART bit is taken into account by the hardware after 2 periods of CK clock (SYNC_DLY). The SPE bit is set to 1 to enable the serial interface. The Searching for the start of frame phase is indicated by a shaded area. The Start-of-frame condition found, the reception and transmission of data can start phase is indicated by a text box. The Not stored into the RX-FIFO text box is shown.

Note: Due to clock domain resynchronization, the CSTART bit is taken into account by the hardware after 2 periods of CK clock (SYNC_DLY).

55.9.8 Stop sequence

The application can stop the I2S/PCM transfers by setting the SPE bit to 0. In that case the communication is stopped immediately, without waiting for the end of the current frame.

In master mode it is also possible to stop the I2S/PCM transfers at the end of the current frame. For that purpose, the user has to set the bit CSUSP to 1, and polls the CSTART bit until it goes to 0. The CSTART bit goes to 0 when the current stereo (if an I2S mode was selected) or mono sample are completely shifted in or out. Then the SPE bit can be set to 0.

The Figure 611 shows an example of stop sequence in the case of master mode. The CSUSP bit is set to 1, during the transmission of left sample, the transfer continue until the last bit of the right sample is transferred. Then CSTART and CSUSP go back to 0, CK and WS signals go back to their inactive state, and the user can set SPE to 0.

Figure 611. Stop sequence, I2S Philips standard, master

Timing diagram for Figure 611 showing the stop sequence in master mode for I2S Philips standard. The diagram displays five signals over time: WS(O) (Word Synchronization, active low), CK(O) (Clock), SDO (Serial Data Out), CSUSP (Continuous Stream Suspension), and CSTART (Continuous Stream Start). The WS signal is high for the left (L) sample and low for the right (R) sample. The CK signal is a periodic clock. The SDO signal shows data bits (MSB, LSB) being transmitted. The CSUSP signal is set high (1) during the transmission of the left sample. The CSTART signal is high (1) during transmission and goes low (0) after the right sample is transmitted, indicating the end of the frame. The SPE (Serial Peripheral Enable) signal is shown going low (0) after the CSTART signal goes low.
Timing diagram for Figure 611 showing the stop sequence in master mode for I2S Philips standard. The diagram displays five signals over time: WS(O) (Word Synchronization, active low), CK(O) (Clock), SDO (Serial Data Out), CSUSP (Continuous Stream Suspension), and CSTART (Continuous Stream Start). The WS signal is high for the left (L) sample and low for the right (R) sample. The CK signal is a periodic clock. The SDO signal shows data bits (MSB, LSB) being transmitted. The CSUSP signal is set high (1) during the transmission of the left sample. The CSTART signal is high (1) during transmission and goes low (0) after the right sample is transmitted, indicating the end of the frame. The SPE (Serial Peripheral Enable) signal is shown going low (0) after the CSTART signal goes low.

Note: In slave mode, the stop sequence is only controlled by the SPE bit.

55.9.9 Clock generator

When the I2S or PCM is configured in master mode, the user needs to program the clock generator in order to produce the Frame Synchronization (WS), the bit clock (CK) and the master clock (MCK) at the desired frequency.

If the I2S or PCM is used in slave mode, there is no need to configure the clock generator.

Figure 612. I 2 S clock generator architecture Figure 612. I2S clock generator architecture diagram. The diagram shows a CLKGEN block containing an 8-bit linear divider + reshaping stage. Inputs to this stage are spi_ker_ck, I2SDIV[7:0], ODD, and I2SMOD. The output of the divider is connected to a ÷ 4 block, which is then connected to a ÷ 2 block. The output of the ÷ 2 block is connected to a multiplexer (MUX) with inputs 0 and 1. The MUX output is connected to the CK pin. The MUX is controlled by CHLEN and MCKOE. The MCKOE input is also connected to an AND gate. The other input of the AND gate is the output of the ÷ 2 block. The output of the AND gate is connected to the MCK pin. The diagram is labeled MSV40490V2.
Figure 612. I2S clock generator architecture diagram. The diagram shows a CLKGEN block containing an 8-bit linear divider + reshaping stage. Inputs to this stage are spi_ker_ck, I2SDIV[7:0], ODD, and I2SMOD. The output of the divider is connected to a ÷ 4 block, which is then connected to a ÷ 2 block. The output of the ÷ 2 block is connected to a multiplexer (MUX) with inputs 0 and 1. The MUX output is connected to the CK pin. The MUX is controlled by CHLEN and MCKOE. The MCKOE input is also connected to an AND gate. The other input of the AND gate is the output of the ÷ 2 block. The output of the AND gate is connected to the MCK pin. The diagram is labeled MSV40490V2.

The frequency generated on MCK, CK and WS depends mainly on I2SDIV, ODD, CHLEN and MCKOE. The bit MCKOE indicates if a master clock need to be generated or not. The master clock has a frequency 128 or 256 times higher than the frame synchronization. This master clock is often required to provide a reference clock to external audio codecs.

Note: In master mode, there is no specific constraints on the ratio between the bus clock rate ( \( F_{PCLK} \) ) and the bit clock ( \( F_{CK} \) ). The bus clock frequency must be high enough in order to support the data throughput.

When the master clock is generated (MCKOE = 1), the frequency of the frame synchronization is given by the following formula in I2S mode:

\[ F_{WS} = \frac{F_{I2S\_CLK}}{256 \times \{(2 \times I2SDIV) + ODD\}} \]

and by this formula in PCM mode:

\[ F_{WS} = \frac{F_{I2S\_CLK}}{128 \times \{(2 \times I2SDIV) + ODD\}} \]

In addition, the frequency of the MCK ( \( F_{MCK} \) ) is given by the formula:

\[ F_{MCK} = \frac{F_{I2S\_CLK}}{\{(2 \times I2SDIV) + ODD\}} \]

When the master clock is disabled (MCKOE = 0), the frequency of the frame synchronization is given by the following formula in I2S mode:

\[ F_{WS} = \frac{F_{I2S\_CLK}}{32 \times (CHLEN + 1) \times \{(2 \times I2SDIV) + ODD\}} \]

And by this formula in PCM mode:

\[ F_{WS} = \frac{F_{I2S\_CLK}}{16 \times (CHLEN + 1) \times \{(2 \times I2SDIV) + ODD\}} \]

Where \( F_{WS} \) is the frequency of the frame synchronization, and \( F_{i2s\_clk} \) is the frequency of the kernel clock provided to the SPI/I2S block.

Note: CHLEN and ODD can be either 0 or 1.
I2SDIV can take any values from 0 to 255 when ODD = 0, but when ODD = 1, the value I2SDIV = 1 is not allowed.

When I2SDIV = 0, then \( \{(2 \times I2SDIV) + ODD\} \) is forced to 1.

Note: When \( \{(2 \times I2SDIV) + ODD\} \) is odd, the duty cycle of MCK or the CK signals is not 50%. Care must be taken when odd ratio is used: it can impact margin on setup and hold time. For example if \( \{(2 \times I2SDIV) + ODD\} = 5 \) , then the duty cycle can be 40%.

Table 417 provides examples of clock generator programming for I2S modes.

MCK generation

The master clock MCK can be generated regardless to the SPE bit. The MCK generating is controlled by the following bits:

Table 417. CLKGEN programming examples for usual I2S frequencies

i2s_clk (MHz)Channel length (bits)I2SDIVODDMCKSampling rate: \( F_{WS} \) (kHz)
12.28816120No16
12.288326016
12.288166032
12.288323032
49.1521616048
49.152328048
49.152168096
49.152324096
49.1521640192
49.1523220192
4.09616 or 320-Yes16
24.57616 or 3230
49.15216 or 323032
12.28816 or 320-
49.15216 or 322048
61.4416 or 3221
98.30416 or 322096
196.60816 or 3220192

55.9.10 Internal FIFOs

The I2S interface can use a dedicated FIFO for the RX and the TX path. The samples to transmit can be written into the TxFIFO via the SPI_TXDR register. The reading of RxFIFO is performed via the SPI_RXDR register.

Data alignment and ordering

It is possible to select the data alignment into the SPI_RXDR and SPI_TXDR registers thanks to the DATFMT bit.

Note as well that the format of the data located into the SPI_RXDR or SPI_TXDR depends as well on the way those registers are accessed via the APB bus.

Figure 613 shows the allowed settings between APB access sizes, DATFMT and DATLEN.

Note:

Caution shall be taken when the APB access size is 32 bits, and DATLEN = 0. For read operation the RxFIFO must contain at least two data, otherwise the read data are invalid. In the same way, for write operation, the TxFIFO must have at least two empty locations, otherwise a data can be lost.

Figure 613. Data Format

APB Access SizeDATLENSPI_RXDR, SPI_TXDR
(DATFMT = 0)(DATFMT = 1)
16 bits0b00
(16 bits)
15 0
valid sample
15 0
valid sample
32 bits0b00
(16 bits)
31 16 15 0
valid sample N+1 valid sample N
31 16 15 0
valid sample N+1 valid sample N
32 bits0b01
(24 bits)
31 24 23 0
zeros valid sample
31 8 7 0
valid sample zeros
32 bits0b10
(32 bits)
31 0
valid sample
31 0
valid sample

MSV40491V1

  1. 1. In I2S mode, the sample N represents the left sample, and the sample N+1 is the right sample.
  2. 1. 24- and 32-bit data width are not always supported (DATLEN = 01 or 10). Refer to Section 55.3: SPI implementation for the supported data sizes.

It is possible to generate an interrupt or a DMA request according to a programmable FIFO threshold levels. The FIFO threshold is common to RX and TxFIFOs can be adjusted via FTHLV.

In I2S mode, the left and right audio samples are interleaved into the FIFOs. It means that for transmit operations, the user has to start to fill-up the TxFIFO with a left sample, followed by a right sample, and so on. For receive mode, the first data read from the RxFIFO is supposed to represent a left channel, the next one is a right channel, and so on.

Note that the read and write pointers of the FIFOs are reset when the bit SPE is set to 0.

Refer to Section 55.9.11 and Section 55.9.15 for additional information.

FIFO size optimization

The basic element of the FIFO is the byte. This allows an optimization of the FIFO locations. For example when the data size is fixed to 24 bits, each audio sample takes 3 basic FIFO elements.

For example, a FIFO with 16 basic elements can have a depth of:

55.9.11 FIFOs status flags

Two status flags are provided for the application to fully monitor the state of the I2S interface. Both flags can generate an interrupt request. The receive interrupt is generated if RXPIE bit is enabled, the transmit interrupt is generated if TXPIE bit is enabled. Those bits are located into the SPI_IER register.

TxFIFO threshold reached (TXP)

When set, this flag indicates that the TxFIFO contains at least FTHLV empty locations. thus FTHLV new data to be transmitted can be written into SPI_TXDR. The TXP flag is reset when the amount of empty locations is lower than FTHLV. Note that TXP = 1, when the I2S is disabled (SPE bit is reset).

RxFIFO threshold reached (RXP)

When set, this flag indicates that there is at least FTHLV valid data into the RxFIFO, thus the user can read those data via SPI_RXDR. It is reset when the RxFIFO contains less than FTHLV data.

See Section 55.10 for additional information on interrupt function in I2S mode.

55.9.12 Handling of underrun situation

In transmit mode, the UDR flag is set when a new data needs to be loaded into the shift register while the TxFIFO is already empty. In such a situation at least a data is lost.

In I2S mode, there is a hardware mechanism in order to prevent misalignment situation (left and right channel swapped). As shown in the following figure, when an underrun occurs, the peripheral re-plays the last valid data on left and right channels as long as conditions of restart are not met. The transmission restarts:


a. 24- and 32-bit data width are not always supported (DATLEN = 01 or 10). Refer to Section 55.3: SPI implementation for the supported data sizes.

Figure 614. Handling of underrun situation

Timing diagram illustrating the handling of an underrun situation in SPI. The diagram shows signals dma_req, dma_ack, TX-FIFO, WS, SD, CK, and UDR flag over time. The TX-FIFO contains data for left (L) and right (R) channels. An underrun is detected when the TX-FIFO is empty during transmission of the right channel. The UDR flag is set, and the last valid data is repeated. The underrun is acknowledged by software, and transmission resumes with the next right channel data.

The diagram illustrates the handling of an underrun situation in SPI. It shows the following signals and states:

The sequence of events is as follows:

  1. The TX-FIFO is initially populated with data for left and right channels (e.g., L4, R3, L3).
  2. Transmission begins. The WS signal goes low to indicate the start of a frame.
  3. The TX-FIFO becomes empty ("Empty !") while the right channel data (R3) is still being transmitted.
  4. The UDR flag is set, indicating an underrun situation.
  5. The last valid data (L4) is repeated in the TX-FIFO.
  6. The underrun is acknowledged by software (UDR flag cleared).
  7. Transmission resumes with the next right channel data (R4).

Annotations at the bottom of the diagram:

MSV40492V3

Timing diagram illustrating the handling of an underrun situation in SPI. The diagram shows signals dma_req, dma_ack, TX-FIFO, WS, SD, CK, and UDR flag over time. The TX-FIFO contains data for left (L) and right (R) channels. An underrun is detected when the TX-FIFO is empty during transmission of the right channel. The UDR flag is set, and the last valid data is repeated. The underrun is acknowledged by software, and transmission resumes with the next right channel data.

The UDR flag can trigger an interrupt if the UDRIE bit in the SPI_IER register is set. The UDR bit is cleared by writing UDRC bit of SPI_IFCR register to 1.

When the block is configured in PCM mode, the transmission restarts at the start of the next frame when there is enough data into the TxFIFO, and the UDR flag is cleared by software.

Note: An underrun situation can occur in master or slave mode. In master mode, when an underrun occurs, the WS, CK and MCK signal are not gated.

Due to resynchronization, any change on the UDR flag is taken into account by the hardware after at least 2 periods of CK clock.

55.9.13 Handling of overrun situation

The OVR flag is set when received data need to written into the RxFIFO, while the RxFIFO is already full. As a result, some incoming data are lost.

In I2S mode, there is a hardware mechanism in order to prevent misalignment situation (left and right channel swapped). As shown in the following figure, when an overrun occurs, the peripheral stops writing data into the RxFIFO as long as conditions of restart are not met. When there is enough room into the RxFIFO, and the OVR flag is cleared, the block starts by writing next the right channel into the RxFIFO if the overrun occurred when a right channel data was received or by writing the next left channel if the overrun occurred when a left channel data was received.

Figure 615. Handling of overrun situation

Timing diagram illustrating the handling of an overrun situation in SPI. The diagram shows signals dma_req, dma_ack, RX-FIFO, WS, SD, CK, and OVR flag over time. The RX-FIFO is shown as a series of slots, some containing data (L2, R2, L3, R3, etc.). An overrun is detected when the RX-FIFO is full and new data (L4, R4) arrives. The OVR flag is set. The diagram includes annotations: 'overrun detected during reception of left channel', 'L4 is lost due to RX-FIFO full. R4 is lost because the IP shall re-start the reception on left channel to avoid misalignments', 'Acknowledged by SW', and 'The reception re-start properly on the next left channel'. The diagram is labeled MSv40493V4.
Timing diagram illustrating the handling of an overrun situation in SPI. The diagram shows signals dma_req, dma_ack, RX-FIFO, WS, SD, CK, and OVR flag over time. The RX-FIFO is shown as a series of slots, some containing data (L2, R2, L3, R3, etc.). An overrun is detected when the RX-FIFO is full and new data (L4, R4) arrives. The OVR flag is set. The diagram includes annotations: 'overrun detected during reception of left channel', 'L4 is lost due to RX-FIFO full. R4 is lost because the IP shall re-start the reception on left channel to avoid misalignments', 'Acknowledged by SW', and 'The reception re-start properly on the next left channel'. The diagram is labeled MSv40493V4.

An interrupt may be generated if the OVRIE bit is set in the SPI_IER register. The OVR bit is cleared by writing OVRC bit of SPI_IFCR register to 1.

When the block is configured in PCM mode, after an overrun error, the peripheral stops writing data into the RxFIFO as long as restart conditions are not met. When there is enough room in the RxFIFO, and the OVR flag is cleared, the next received data are written into the RxFIFO.

Note: An overrun situation can occur in master or slave mode. In master mode, when an overrun occurs, the WS, CK and MCK signal are not gated.

55.9.14 Frame error detection

When configured in slave mode, the SPI/I2S block detects two kinds of frame errors:

In slave mode, if the frame length provided by the external master device is different from 32 or 64 bits, the user has to set FIXCH to 0. As the SPI/I2S synchronize each transfer with the WS there is no misalignment risk, but in a noisy environment, if a glitch occurs in the CK signal, a sample may be affected and the application is not aware of this.

If the frame length provided by the external master device is equal to 32 or 64 bits, then the user can set FIXCH to 1 and adjust accordingly CHLEN. As the SPI/I2S synchronize each transfer with the WS there is still no misalignment risk, and if the amount of bit clock between each channel boundary is different from CHLEN, the frame error flag (TIFRE) is set to 1.

Figure 616 shows an example of frame error detection. The SPI/I2S block is in slave mode and the amount of bit clock periods for left channel are not enough to shift-in or shift-out the data. The figure shows that the on-going transfer is interrupted and the next one is started in order to remain aligned to the WS signal.

Figure 616. Frame error detection, with FIXCH=0

Timing diagram for Figure 616 showing frame error detection with FIXCH=0. The diagram plots four signals over time: WS (I) (Word Select, Input), CK (I) (Clock, Input), SD (I/O) (Serial Data, Input/Output), and TIFRE flag. The SD line shows data frames: [R0 L15 L14], [L3 L2 L1], [R15 R14], [R3 R2 R1], [R0 L15 L14]. The WS signal is high for the first frame and low for the subsequent frames. The TIFRE flag is shown as a pulse that goes high when the WS signal changes while the SD line is still active (during the second frame). A callout box explains: 'The left channel is not completely shifted in or out while it is requested to shift-in or out the right channel è error'. Another callout box shows 'Acknowledged by SW' pointing to the flag's falling edge. The diagram is labeled MSv40494V2.
Timing diagram for Figure 616 showing frame error detection with FIXCH=0. The diagram plots four signals over time: WS (I) (Word Select, Input), CK (I) (Clock, Input), SD (I/O) (Serial Data, Input/Output), and TIFRE flag. The SD line shows data frames: [R0 L15 L14], [L3 L2 L1], [R15 R14], [R3 R2 R1], [R0 L15 L14]. The WS signal is high for the first frame and low for the subsequent frames. The TIFRE flag is shown as a pulse that goes high when the WS signal changes while the SD line is still active (during the second frame). A callout box explains: 'The left channel is not completely shifted in or out while it is requested to shift-in or out the right channel è error'. Another callout box shows 'Acknowledged by SW' pointing to the flag's falling edge. The diagram is labeled MSv40494V2.

An interrupt can be generated if the TIFREIE bit is set. The frame error flag (TIFRE) is cleared by writing the TIFREC bit of the SPI_IFCR register to 1.

It is possible to extend the coverage of the frame error flag by setting the bit FIXCH to 1. When this bit is set to 1, then the SPI/I2S is expecting fixed channel lengths in slave mode. This means that the expected channel length can be 16 or 32 bits, according to CHLEN. As shown in Figure 617, in this mode the SPI/I2S block is able to detect if the WS signal is changing at the expected moment (too early or too late).

Note: Figure 616 and Figure 617 show the mechanism for the slave transmit mode, but this is also true for slave receive and slave full-duplex.

Figure 617. Frame error detection, with FIXCH=1

Timing diagram for Figure 617 showing frame error detection with FIXCH=1. The diagram plots four signals: WS (I), CK (I), SD (O) (Serial Data, Output), and TIFRE flag. The CK signal has labels for clock periods: 31 32, 1 2 3 4, 16, 28 29 30, 31, 1 2 3 4, 31 32 33, 1 2 3. The SD line shows data: [R0 L15 L14], [L1 L0], [R15 R14], [R1 R0], [L15 L14]. The WS signal is high for the first frame (32 clocks) and changes to low after 31 clocks for the second frame. The TIFRE flag goes high at this point. Callout boxes indicate: 'Error: the WS signal is changing after 31 clock periods instead of 32.' and 'Note that the right sample is properly synchronized with WS.' for the first frame. For the second frame, callout boxes indicate: 'Error: the WS signal is changing after 33 clock periods instead of 32.' and 'Note that the left sample is properly synchronized with WS.' The flag is shown being 'Acknowledged by SW' at its falling edges. The diagram is labeled MSv40495V2.
Timing diagram for Figure 617 showing frame error detection with FIXCH=1. The diagram plots four signals: WS (I), CK (I), SD (O) (Serial Data, Output), and TIFRE flag. The CK signal has labels for clock periods: 31 32, 1 2 3 4, 16, 28 29 30, 31, 1 2 3 4, 31 32 33, 1 2 3. The SD line shows data: [R0 L15 L14], [L1 L0], [R15 R14], [R1 R0], [L15 L14]. The WS signal is high for the first frame (32 clocks) and changes to low after 31 clocks for the second frame. The TIFRE flag goes high at this point. Callout boxes indicate: 'Error: the WS signal is changing after 31 clock periods instead of 32.' and 'Note that the right sample is properly synchronized with WS.' for the first frame. For the second frame, callout boxes indicate: 'Error: the WS signal is changing after 33 clock periods instead of 32.' and 'Note that the left sample is properly synchronized with WS.' The flag is shown being 'Acknowledged by SW' at its falling edges. The diagram is labeled MSv40495V2.

The frame error detection can be generally due to noisy environment disturbing the good reception of WS or CK signals.

Note: The SPI/I2S is not able to recover properly if an overrun and an early frame occur within the same frame. In this case the user has to disable and re-enable the SPI/I2S.

55.9.15 DMA Interface

The I2S/PCM mode shares the same DMA requests lines than the SPI function. There is a separated DMA channel for TX and RX paths. Each DMA channel can be enabled via RXDMAEN and TXDMAEN bits of SPI_CFG1 register.

In receive mode, the DMA interface is working as follow:

  1. 1. The hardware evaluates the RxFIFO level,
  2. 2. If the RxFIFO contains at least FTHLV samples, then FTHLV DMA requests are generated,
    • – When the FTHLV DMA requests are completed, the hardware loops to step 1
  3. 3. If the RxFIFO contains less than FTHLV samples, no DMA request is generated, and the hardware loop to step 1

In transmit mode, the DMA interface is working as follow:

  1. 1. The hardware evaluates the TxFIFO level,
  2. 2. If the TxFIFO contains at least FTHLV empty locations, then FTHLV DMA requests are generated,
    • – When the FTHLV DMA requests are completed, the hardware loops to step 1
  3. 3. If the TxFIFO contains less than FTHLV empty locations, no DMA request is generated, and the hardware loop to step 1

55.9.16 Programing examples

Master I2S Philips standard, transmit

This example shows how to program the interface for supporting the Philips I2S standard in master transmit mode, with a sampling rate of 48 kHz, using the master clock. The assumption taken is that SPI/I2S is receiving a kernel clock (i2s_clk) of 61.44 MHz from the clock controller of the circuit.

Start Procedure

  1. 1. Enable the bus interface clock (pclk or hclk), release the reset signal if needed in order to be able to program the SPI/I2S block.
  2. 2. Ensure that the SPI/I2S block receives properly a kernel frequency (at 61.44 MHz in this example).
  3. 3. Ensure that SPE is set to 0.
  4. 4. Program the clock generator in order to provide the MCK clock and to have a frame synchronization rate at exactly 48 kHz. So I2SDIV = 2, ODD = 1, and MCKOE = 1.
  5. 5. Program the serial interface protocol: CKPOL = 0, WSINV = 0, LSBFRST = 0, CHLEN = 1 (32 bits per channel) DATLEN = 1 (24 bits), I2SSTD = 0 (Philips Standard), I2SCFG = 2 (master transmit), I2SMOD = 1, for I2S/PCM mode. The register SPI_I2SCFG must be updated before going to next steps.
  6. 6. Adjust the FIFO threshold, by setting the wanted value into FTHLV. For example if a threshold of 2 audio samples is required, FTHLV = 1.
  7. 7. Clear all status flag registers.
  8. 8. Enable the flags who shall generate an interrupt such as UDRIE. Note that TIFRE is not meaningful in master mode.
  9. 9. If the data transfer uses DMA:
    1. a) Program the DMA peripheral,
    2. b) Initialize the memory buffer with valid audio samples,
    3. c) Enable the DMA channel,
  10. 10. If the data transfer is done via interrupt, then the user has to enable the interrupt by setting the TXPIE bit to 1.
  11. 11. Set SPE to 1, as soon as this bit is set to one the following actions may happen:
    • – If the interrupt generation is enabled, the SPI/I2S generates an interrupt request allowing the interrupt handler to fill-up the TxFIFO.
    • – If the DMA transfer are enabled (TXDMAEN = 1), the SPI/I2S generates DMA requests in order to fill-up the TxFIFO
  12. 12. Finally, the user has to ensure that the TxFIFO is not empty before enabling the serial interface. This is important in order to avoid an underrun condition when the interface is enabled. Then the SPI/I2S block can be enabled by setting the bit CSTART to 1. CSTART bit is located into SPI_CR1 register.

Stop Procedure in master mode

  1. 1. Set the bit CSUSP to 1, in order to stop on-going transfers
  2. 2. Check the value of CSTART bit until it goes to 0
  3. 3. Stop DMA peripheral, bus clock...
  4. 4. Set bit SPE to 0 in order to disable the SPI/I2S block

Slave I2S Philips standard, receive

This example shows how to program the interface for supporting the I2S Philips standard protocol in slave receiver mode, with a sampling rate of 48 kHz. Note that in slave mode the SPI/I2S block cannot control the sample rate of the received samples. In this example we took the assumption that the external master device is delivering an I2S frame structure with a channel length of 24 bits. So we cannot use the capability offered for frame error detection when FIXCH is set to 1.

Procedure

  1. 1. Via the RCC block, enable the bus interface and the kernel clocks, assert and release the reset signal if needed.
  2. 2. Program the AFMUX in order to select the wanted I/Os. In the current example CK, WS, SDI.
  3. 3. Program the serial interface protocol: CKPOL = 0, WSINV = 0, LSBFRST = 0, FIXCH = 0 (because channel length is different from 16 and 32 bits), DATLEn = 0 (16 bits), I2SSTD = 0 (Philips protocol), I2SCFG = 1 (slave RX), I2SMOD = 1, for I2S mode.
  4. 4. Adjust the FIFO threshold, by setting the wanted value into FTHLV. For example if a threshold of 2 audio samples is required, FTHLV = 1.
  5. 5. Clear all status flag registers.
  6. 6. Enable the flags who shall generate an interrupt such as UDRIE and TIFRE.
  7. 7. If the data transfer uses DMA:
    • – Program the DMA peripheral: one RX channel
    • – Enable the DMA channel,
    • – In the SPI/I2S block, enable the DMA by setting the RXDMAEN bit to 1.
  8. 8. If the data transfer is done via interrupt, then the user has to enable the interrupt by setting the RXPIE bit to 1.
  9. 9. Set SPE to 1.
  10. 10. Finally the user can set the bit CSTART to 1 in order to enable the serial interface. The SPI/I2S starts to store data into the RxFIFO on the next occurrence of left data transmitted by the external master device.

Stop Procedure in slave mode

  1. 1. Set bit SPE to 0 in order to disable the SPI/I2S block
  2. 2. Stop DMA peripheral, bus clock...

55.10 I2S wake-up and interrupts

In PCM/I2S mode an interrupt ( spi_it ) or a wake-up event signal ( spi_wkup ) can be generated according to the events described in the Table 418 .

Interrupt events can be enabled and disabled separately.

Table 418. I2S interrupt requests

Interrupt eventEvent flagEnable control bitEvent clear methodInterrupt/Wake-up activated
spi_itspi_wkup
TxFIFO threshold reachedTXPTXPIETXP flag is cleared when the TxFIFO contains less than FTHLV empty locationsYESYES
RxFIFO threshold reachedRXPRXPIERXP flag is cleared when the RxFIFO contains less than FTHLV samples
Overrun errorOVROVRIEOVR is cleared by writing OVR to 1
Underrun errorUDRUDRIEUDR is cleared by writing UDRC to 1
Frame error flagTIFRETIFREIETIFRE is cleared by writing TIFREC to 1NO

55.11 SPI/I2S registers

55.11.1 SPI/I2S control register 1 (SPI_CR1)

Address offset: 0x00

Reset value: 0x0000 0000

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.IO LOCK
rs
1514131211109876543210
TCRC INIRCRC INICRC33_17SSIHDDIRCSUSPC STARTMAS RXRes.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 is set by software and cleared by hardware whenever SPE bit is changed from 1 to 0.

0: AF configuration is not locked

1: AF configuration is locked

When this bit is set, the SPI_CFG2 register content cannot be modified. This bit can be set when SPI is disabled only else it is write protected. It is cleared and cannot be set when the MODF bit is set.

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 : 32-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 NSS 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 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, 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 transaction.

The master mode communication must be suspended (using this bit or keeping TXDR empty) before disabling the SPI or going to low-power mode. This bit can be used in SPI or I2S mode.

After software suspension, the SUSP flag has to be cleared and the SPI disabled and re-enabled before any next transaction starts.

Bit 9 CSTART: master transfer start

This bit is set by software to start an SPI or I2S/PCM communication. In SPI mode, it is cleared by hardware when End Of Transfer (EOT) flag is set or when a transaction suspend request is accepted. In I2S/PCM mode, it is also cleared by hardware as described in the section stop sequence.

0: master transfer is at idle

1: master transfer is on-going or temporary suspended by automatic suspend

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.

Bit 8 MASRX: master automatic SUSP 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.

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 SPI communication is suspended.

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

That 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 transaction without any next constraint. Prior 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.

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.

0: serial peripheral disabled.

1: serial peripheral enabled

When SPE=1, the SPI data transfer is enabled, the configuration registers SPI_CFG1, SPI_CFG2, CRCPOLY and UDRDR and the IOLOCK bit in SPI_CR1 are write protected.

They can be changed only when SPE=0.

When SPE=0 any SPI operation is stopped and disabled, all the not cleared requests with enabled interrupt stay pending and propagates the spi_plck clock request, the NSS output is deactivated at master, 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.

55.11.2 SPI control register 2 (SPI_CR2)

Address offset: 0x04

Reset value: 0x0000 0000

31302928272625242322212019181716
TSER[15:0]
rsrsrsrsrsrsrsrsrsrsrsrsrsrsrsrs
1514131211109876543210
TSIZE[15:0]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

Bits 31:16 TSER[15:0] : number of data transfer extension to be reload into TSIZE just when a previous number of data stored at TSIZE is transacted

This register can be set by software when its content is cleared only. It is cleared by hardware once TSIZE reload is done. The TSER value must be programmed in advance before CTSIZE counter reaches zero otherwise the reload is not taken into account and traffic terminates with normal EOT event.

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

When these bits are changed by software, the SPI must be disabled. The field can be updated by hardware optionally, too, to be reloaded by the TSER value if applicable.

Endless transaction is initialized when CSTART is set while zero value is stored at TSIZE. TSIZE cannot be set to 0xFFFF value when CRC is enabled.

55.11.3 SPI configuration register 1 (SPI_CFG1)

Address offset: 0x08

Reset value: 0x0007 0007

Content of this register is write protected when SPI is enabled, except TXDMAEN and RXDMAEN bits.

31302928272625242322212019181716
Res.MBR[2:0]Res.Res.Res.Res.Res.CRC ENRes.CRC SIZE[4:0]
rwrwrwrwrwrwrwrwrw
1514131211109876543210
TX DMA ENRX DMA ENRes.UDR DET[1:0]UDR CFG[1:0]FTH LV[3:0]DSIZE[4:0]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

Bit 31 Reserved, must be kept at reset value.

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

000: SPI master clock/2
001: SPI master clock/4
010: SPI master clock/8
011: SPI master clock/16
100: SPI master clock/32
101: SPI master clock/64
110: SPI master clock/128
111: SPI master clock/256

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

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

Bit 22 CRCEN : hardware CRC computation enable

0: CRC calculation disabled
1: CRC calculation Enabled

Bit 21 Reserved, must be kept at reset value.

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

Most significant bits are taken into account from polynomial calculation when CRC result is transacted or compared. The length of the polynomial is not affected by this setting.

00000: reserved
00001: reserved
00010: reserved
00011: 4-bits
00100: 5-bits
00101: 6-bits
00110: 7-bits
00111: 8-bits
.....

11101: 30-bits
11110: 31-bits
11111: 32-bits

The value must be set to a value equal or multiple of the data size (DSIZE[4:0]). Its maximum size cannot exceed the DSIZE maximum at the instance.

Note: If CRC calculation is disabled by CRCEN=0, the CRCSIZE field must be kept at its default setting.

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

Bit 15 TXDMAEN : Tx DMA stream enable

0: Tx DMA disabled
1: Tx DMA enabled

Bit 14 RXDMAEN : Rx DMA stream enable

0: Rx-DMA disabled
1: Rx-DMA enabled

Bit 13 Reserved, must be kept at reset value.

Bits 12:11 UDRDET[1:0] : detection of underrun condition at slave transmitter

00: underrun is detected at begin of data frame (no protection of 1-st bit)

01: underrun is detected at end of last data frame

10: underrun is detected by begin of active NSS signal

11: reserved

For more details see Figure 597: Optional configurations of slave behavior at detection of underrun condition .

Bits 10:9 UDRCFG[1:0] : behavior of slave transmitter at underrun condition

00: slave sends a constant pattern defined by the user at SPI_UDRDR register

01: slave repeats lastly received data frame from master

10: slave repeats its lastly transmitted data frame

11: reserved

When slave is configured at transmit only mode (COMM[1:0]=01), slave repeats all zeros pattern at UDRCFG[1:0]=01 setting.

For more details see Figure 597: Optional configurations of slave behavior at detection of underrun condition .

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

Defines number of data frames at single data packet. The size of the packet must not exceed 1/2 of FIFO space.

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

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

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 at DSIZE bit field is reserved at the peripheral instances where data size is limited to 16-bit.

55.11.4 SPI configuration register 2 (SPI_CFG2)

Address offset: 0x0C

Reset value: 0x0000 0000

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

31302928272625242322212019181716
AF CNTRSSOMSSOESSIOPRes.SSMCPOLCPHALSB FRSTMAS TERSP[2:0]COMM[1:0]Res.
rwrwrwrwrwrwrwrwrwrwrwrwrwrw
1514131211109876543210
IOSWPRes.Res.Res.Res.Res.Res.Res.MIDI[3:0]MSSI[3:0]
rwrwrwrwrwrwrwrwrw

Bit 31 AFCNTR : alternate function GPIOs control

This bit is taken into account when SPE=0 only

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

1: the peripheral keeps always control of all associated GPIOs

When SPI master has to be disabled temporary for a specific configuration reason (e.g. 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. This bit must be never used at slave mode as any slave transmitter must not force its MISO output once the SPI is disabled.

Note: This bit can be also used in PCM and I2S modes.

Bit 30 SSOM : NSS output management in master mode

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

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

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

Bit 29 SSOE : NSS output enable

This bit is taken into account at 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 completed 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 NSS signal input

0: NSS input value is determined by the NSS pad

1: NSS input value is determined by the SSI bit

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

Bit 25 CPOL : clock polarity

0: SCK signal is at 0 when idle

1: SCK signal is at 1 when idle

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

Bit 23 LSBFRST : data frame format

0: MSB transmitted first

1: LSB transmitted first

Note: This bit can be also used in PCM and I2S modes.

Bit 22 MASTER : SPI master

0: SPI Slave

1: SPI Master

Bits 21:19 SP[2:0] : Serial protocol

000: SPI Motorola

001: SPI TI

others: Reserved, must not be used

Bits 18:17 COMM[1:0] : SPI communication mode

00: full-duplex

01: simplex transmitter

10: simplex receiver

11: half-duplex

Bit 16 Reserved, must be kept at reset value.

Bit 15 IOSWP : swap functionality of MISO and MOSI pins

0: no swap

1: MOSI and MISO are swapped

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.

Note: This bit can be also used in PCM and I2S modes.

Bits 14:8 Reserved, must be kept at reset value.

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 and first data of a session start 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.

55.11.5 SPI/I2S interrupt enable register (SPI_IER)

Address offset: 0x10

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.TSERF IEMODF IETIFRE IECRCE IEOVRIEUDRIETXTFIEEOTIEDXPIETXPIERXPIE
rwrwrwrwrwrwrwrwrsrsrw

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

Bit 10 TSERFIE : additional number of transactions reload interrupt enable
0: TSERF interrupt disabled
1: TSERF interrupt enabled

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

55.11.6 SPI/I2S status register (SPI_SR)

Address offset: 0x14

Reset value: 0x0000 1002

31302928272625242322212019181716
CTSIZE[15:0]
rrrrrrrrrrrrrrrr
1514131211109876543210
RXWNERXPLVL[1:0]TXCSUSPTSERFMODFTIFRECRCEOVRUDRTXTFEOTDXPTXPRXP
rrrrrrrrrrrrrrrr

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 and at LP mode too.

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-bit, the value gives number of remaining data frames persisting at RxFIFO.

00: no next frame is available at RxFIFO

01: 1 frame is available

10: 2 frames are available*

11: 3 frames are available*

(*) optional count when the data size is set up to 8-bit.

When the frame size is greater than 16-bit, these bits read as 00. In consequence, the single data frame received at the FIFO cannot be detected neither by RWNE nor by RXPLVL bits if data size is set from 17 to 24 bits. The user then must apply other methods like TSIZE>0 or FTHLV=0.

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.

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

1: Last TxFIFO frame transmission completed

Bit 11 SUSP : suspension status

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

SUSP generates an interrupt when EOTIE is set.

This bit has to be cleared prior SPI is disabled by write 1 to SUSPC bit at SPI_IFCR

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

1: Master mode is suspended (current frame completed)

Bit 10 TSERF : additional number of SPI data to be transacted was reload

This bit is cleared by write 1 to TSERFC bit at SPI_IFCR or by writing the TSER[15:0] (SPI_CR2) register

0: no acceptance

1: additional number of data accepted, current transaction continues

Bit 9 MODF : mode fault

0: no mode fault

1: mode fault detected

This bit is cleared by write 1 to MODFC bit at SPI_IFCR. When MODF is set, the SPE and IOLOCK bits at the SPI_CR1 register are reset and their setting is blocked.

Bit 8 TIFRE : TI frame format error

0: no TI Frame Error

1: TI Frame Error detected

This bit is cleared by write 1 to TIFREC bit at SPI_IFCR

Bit 7 CRCE : CRC error

0: no CRC error

1: CRC error detected

This bit is cleared by write 1 to CRCEC bit at SPI_IFCR

Bit 6 OVR : overrun

0: no overrun

1: overrun detected

This bit is cleared by write 1 to OVRC bit at SPI_IFCR

Bit 5 UDR : underrun

0: no underrun

1: underrun detected

This bit is cleared by write 1 to UDRC bit at SPI_IFCR

Note: In SPI mode, the UDR flag applies to Slave mode only. In I2S/PCM mode, (when available) this flag applies to Master and Slave mode.

Bit 4 TXTF : transmission transfer filled

0: upload of TxFIFO is on-going or not started

1: TxFIFO upload is finished

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 TXTFC bit at SPI_IFCR

TXTF flag triggers an interrupt if TXTFIE bit is set.

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

Bit 3 EOT : end of transfer

EOT is set by hardware as soon as a full transfer is complete, that is when TSIZE number of data have been transmitted and/or received on the SPI. EOT is cleared by software write 1 to EOTC bit at SPI_IFCR.

EOT flag triggers an interrupt if 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.

0: transfer is on-going or not started

1: transfer complete

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

Bit 2 DXP : duplex packet

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

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

Bit 1 TXP : Tx-packet space available

0: there is not enough space to locate next data packet at TxFIFO

1: TxFIFO has enough free location to host 1 data packet

TXP flag is changed by hardware. It monitors overall space currently available at TxFIFO if SPI is enabled. It has to be checked once a complete data packet is stored at TxFIFO.

Bit 0 RXP : Rx-packet available

0: RxFIFO is empty or a not complete data packet is received

1: RxFIFO contains at least 1 data packet

RXP flag is changed by hardware. It monitors number of overall data currently available at RxFIFO if SPI is enabled. It has to be checked once a data packet is completely read out from RxFIFO.

55.11.7 SPI/I2S interrupt/status flags clear register (SPI_IFCR)

Address offset: 0x18

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.SUSP
C
TSERF
C
MODF
C
TIFRE
C
CRCE
C
OVR
C
UDR
C
TXTF
C
EOT
C
Res.Res.Res.
wwwwwwwww

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 in the SPI_SR register

Bit 10 TSERFC : TSERFC flag clear

Writing a 1 into this bit clears TSERF flag in the SPI_SR register

Note: TSERF is also reset by writing the TSERF[15:0] (SPI_CR2) register

Bit 9 MODFC : mode fault flag clear

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

Bit 8 TIFREC : TI frame format error flag clear

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

Bit 7 CRCEC : CRC error flag clear

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

Bit 6 OVRC : overrun flag clear

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

Bit 5 UDRC : underrun flag clear

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

Bit 4 TXTFC : transmission Transfer Filled flag clear

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

Bit 3 EOTC : end of transfer flag clear

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

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

55.11.8 SPI/I2S transmit data register (SPI_TXDR)

Address offset: 0x20

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.

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

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

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

55.11.9 SPI/I2S receive data register (SPI_RXDR)

Address offset: 0x30

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.

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

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

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

55.11.10 SPI polynomial register (SPI_CRCPOLY)

Address offset: 0x40

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 at some other ST products with fixed length of the polynomial string where the most significant bit of the string is always kept hidden.

The length of the polynomial is given by the most significant bit of the value stored at this register. It has to be set greater than DSIZE. CRC33_17 bit has to be set additionally with CRCPOLY register when DSIZE is configured to maximum 32-bit or 16-bit size and CRC is enabled (to keep polynomial length greater than data size).

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

55.11.11 SPI transmitter CRC register (SPI_TXCRC)

Address offset: 0x44

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 SPI_CR1 is written to 1 or when a data block is transacted completely. The CRC is calculated serially using the polynomial programmed in the SPI_CRCPOLY register. The number of bits considered at calculation depends on SPI_CRCPOLY register and CRCSIZE bits settings at SPI_CFG1 register.

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

Not used for the I 2 S mode.

Bits 31-16 are reserved at the peripheral instances with data size limited to 16-bit.

There is no constrain when 32-bit access is applied to 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 at this case.

55.11.12 SPI receiver CRC register (SPI_RXCRC)

Address offset: 0x48

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 SPI_CR1 is written to 1 or when a data block is transacted completely. The CRC is calculated serially using the polynomial programmed in the SPI_CRCPOLY register. The number of bits considered at calculation depends on SPI_CRCPOLY register and CRCSIZE bits settings at SPI_CFG1 register.

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

Not used for the I 2 S mode.

Bits 31-16 are reserved at the peripheral instances with data size limited to 16-bit.

There is no constrain when 32-bit access is applied to 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 at this case.

55.11.13 SPI underrun data register (SPI_UDRDR)

Address offset: 0x4C

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 at slave mode and at underrun condition only. The number of bits considered depends on DSIZE bit settings at SPI_CFG1 register. Underrun condition handling depends on setting if UDRDET and UDRCFG bits at SPI_CFG1 register. Bits 31-16 are reserved at the peripheral instances with data size limited to 16-bit. There is no constrain when 32-bit access is applied to these addresses. Reserved bits 31-16 are always read zero while any write to them is ignored.

55.11.14 SPI/I2S configuration register (SPI_I2SCFGR)

Address offset: 0x50

Reset value: 0x0000 0000

Note: All documented bits in this register must be configured when the I2S is disabled (SPE = 0). These bits are not used in SPI mode except for I2SMOD which needs to be kept at 0 in SPI mode.

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.MCK
OE
ODDI2SDIV[7:0]
rwrwrwrwrwrwrwrwrwrw
1514131211109876543210
Res.DAT
FMT
WSINVFIXCHCKPOLCHLENDATLEN[1:0]PCM
SYNC
Res.I2SSTD[1:0]I2SCFG[2:0]I2S
MOD
rwrwrwrwrwrwrwrwrwrwrwrwrwrw

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

Bit 25 MCKOE : master clock output enable

0: Master clock output is disabled

1: Master clock output is enabled

Bit 24 ODD : odd factor for the prescaler

0: Real divider value is = I2SDIV *2

1: Real divider value is = (I2SDIV * 2) + 1

Refer to Section 55.9.9: Clock generator for details

Bits 23:16 I2SDIV[7:0] : I 2 S linear prescaler

I2SDIV can take any values except the value 1, when ODD is also equal to 1.

Refer to Section 55.9.9: Clock generator for details

Bit 15 Reserved, must be kept at reset value.

Bit 14 DATFMT : data format

0: the data inside the SPI_RXDR or SPI_TXDR are right aligned

1: the data inside the SPI_RXDR or SPI_TXDR are left aligned.

Bit 13 WSINV : Word select inversion

This bit is used to invert the default polarity of WS signal.

0: In I2S Philips standard, the left channel transfer starts one CK cycle after the WS falling edge, and the right channel one CK cycle after the WS rising edge.

In MSB or LSB justified mode, the left channel is transferred when WS is HIGH, and the right channel when WS is LOW.

In PCM mode, the data transfer starts one CK cycle after the rising edge of WS.

1: In I2S Philips standard, the left channel transfer starts one CK cycle after the WS rising edge, and the right channel one CK cycle after the WS falling edge.

In MSB or LSB justified mode, the left channel is transferred when WS is LOW, and right channel when WS is HIGH.

In PCM mode, the data transfer starts one CK cycle after the rising edge of WS.

Bit 12 FIXCH : fixed channel length in slave

0: the channel length in slave mode is different from 16 or 32 bits (CHLEN not taken into account)

1: the channel length in slave mode is supposed to be 16 or 32 bits (according to CHLEN)

Bit 11 CKPOL : serial audio clock polarity

0: the signals generated by the SPI/I2S (SDO and WS) are changed on the falling edge of CK and the signals received by the SPI/I2S (SDI and WS) are read of the rising edge of CK.

1: the signals generated by the SPI/I2S (SDO and WS) are changed on the rising edge of CK and the signals received by the SPI/I2S (SDI and WS) are read of the falling edge of CK.

Bit 10 CHLEN : channel length (number of bits per audio channel)

0: 16-bit wide

1: 32-bit wide

The selected channel length must always be higher or equal to the data length (DATLEN).

Bits 9:8 DATLEN[1:0] : data length to be transferred

00: 16-bit data length

01: 24-bit data length

10: 32-bit data length

11: not allowed

The selected data length must never be higher than the channel length (CHLEN).

Note: Data width of 24 and 32 bits are not always supported, (DATLEN = 01 or 10), refer to Section 55.3: SPI implementation to check the supported data size.

Bit 7 PCMSYNC : PCM frame synchronization

0: short frame synchronization

1: long frame synchronization

Bit 6 Reserved, must be kept at reset value.

Bits 5:4 I2SSTD[1:0] : I 2 S standard selection

For more details on I 2 S standards, refer to Section 55.9.5: Supported audio protocols

Bits 3:1 I2SCFG[2:0] : I2S configuration mode

Bit 0 I2SMOD : I2S mode selection

55.12 SPI register map and reset values

Table 419. SPI register map and reset values

OffsetRegister name
reset value
313029282726252423222120191817161514131211109876543210
0x00SPI_CR1Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.IOLOCKTCRINIRCRCINICRC33_17SSIHDDIRCSUSPCSTARTMASRXRes.Res.Res.Res.Res.Res.Res.SPE
Reset value0000000000
0x04SPI_CR2TSER[15:0]TSIZE[15:0]
Reset value00000000000000000000000000000000
0x08SPI_CFG1Res.MBR[2:0]Res.Res.Res.Res.Res.RCRCENRes.CRCSIZE[4:0]TXDMAENRXDMAENRes.Res.UDRDET[1:0]UDRCFG[1:0]FTHLV[3:0]DSIZE[4:0]
Reset value00001111100000000000111
0x0CSPI_CFG2AFCNTRSSOMSSOESSIOPRes.SSMCPOLCPHALSBFRSTMASTERSP[2:0]COMM[1:0]Res.IOSWPRes.Res.Res.Res.Res.Res.Res.MIDI[3:0]MSSI[3:0]
Reset value00000000000000000000000
0x10SPI_IERRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.TSERFIEMODFIETIFREIECRCEIEOVRIEUDRIETXTFIEEOTIEDXPIETXPIERXPIE
Reset value00000000000
0x14SPI_SRCTSIZE[15:0]Res.RXWNERXPLVL[1:0]TXCSUSPTSERFCMODFTIFRECRCEOVRUDRTXTFCEOTDPXPTXPRXP
Reset value00000000000000000001000000000010
0x18SPI_IFCRRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.SUSPCTSERFCMODFCTIFRECCRCECOVRCUDRCTXTFCEOTCRes.Res.
Reset value000000000
0x20SPI_TXDRTXDR[31:16]TXDR[15:0]
Reset value00000000000000000000000000000000
0x24 - 0x2CReservedRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.
Reset value
0x30SPI_RXDRRXDR[31:16]RXDR[15:0]
Reset value00000000000000000000000000000000
0x34 - 0x3CReservedRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.
Reset value
0x40SPI_CRCPOLYCRCPOLY[31:16] (1)CRCPOLY[15:0]
Reset value00000000000000000000000001000111
0x44SPI_TXCRCTXCRC[31:16] (1)TXCRC[15:0]
Reset value00000000000000000000000000000000
0x48SPI_RXCRCRXCRC[31:16] (1)RXCRC[15:0]
Reset value00000000000000000000000000000000
0x4CSPI_UDRDRUDRDR[31:16] (1)UDRDR[15:0]
Reset value00000000000000000000000000000000

Table 419. SPI register map and reset values (continued)

OffsetRegister name
reset value
313029282726252423222120191817161514131211109876543210
0x50SPI_I2SCFGGRRes.Res.Res.Res.Res.Res.MCKOEODDI2SDIV[7:0]Res.DATFMTWSINVFIXCHCKPOLCHLENDATLEN[1:0]PCMSYNCRes.I2SSTD[1:0]I2SCFG[2:0]I2SMOD
Reset value000000000000000000000000
  1. 1. Bits 31-16 are reserved at the peripheral instances with data size limited to 16-bit. There is no constrain when 32-bit access is applied to these addresses. Reserved bits 31-16 are always read zero while any write to them is ignored. Refer to Table register boundary addresses.

Refer to Section 2.3 on page 131 for the register boundary addresses.