28. Serial peripheral interface (SPI)

This section applies to the whole STM32F4xx family, unless otherwise specified.

28.1 SPI introduction

The SPI interface provides two main functions, supporting the SPI or the I2S audio protocol. By default, the SPI function is selected. It is possible to switch the interface from SPI to I2S by software.

The serial peripheral interface (SPI) allows half/ full-duplex, synchronous, serial communication with external devices. The interface can be configured as the master and in this case it provides the communication clock (SCK) to the external slave device. The interface is also capable of operating in multimaster configuration.

It may be used for a variety of purposes, including simplex synchronous transfers on two lines with a possible bidirectional data line or reliable communication using CRC checking.

The I2S is also a synchronous serial communication interface. It can address four different audio standards including the I2S Philips standard, the MSB- and LSB-justified standards, and the PCM standard. It can operate as a slave or a master device in full-duplex mode (using 4 pins) or in half-duplex mode (using 3 pins). Master clock can be provided by the interface to an external slave component when the I2S is configured as the communication master.


Warning: Since some SPI1 and SPI3/I2S3 pins may be mapped onto some pins used by the JTAG interface (SPI1_NSS onto JTDI, SPI3_NSS/I2S3_WS onto JTDI and SPI3_SCK/I2S3_CK onto JTDO), you may either:

For more information on the configuration of the JTAG/SWD interface pins, refer to Section 8.3.2: I/O pin multiplexer and mapping.


28.2 SPI and I2S main features

28.2.1 SPI features

28.2.2 I2S features

28.3 SPI functional description

28.3.1 General description

The block diagram of the SPI is shown in Figure 246.

Figure 246. SPI block diagram

SPI block diagram showing Address and data bus, Rx buffer, Shift register, Tx buffer, Baud rate generator, Master control logic, Communication control, and control/status registers SPI_CR1, SPI_CR2, and SPI_SR with signal connections to MOSI, MISO, SCK, and NSS pins.

MS51604V1

SPI block diagram showing Address and data bus, Rx buffer, Shift register, Tx buffer, Baud rate generator, Master control logic, Communication control, and control/status registers SPI_CR1, SPI_CR2, and SPI_SR with signal connections to MOSI, MISO, SCK, and NSS pins.

Usually, the SPI is connected to external devices through four pins:

A basic example of interconnections between a single master and a single slave is illustrated in Figure 247.

Figure 247. Single master/single slave application

Block diagram of a single master/single slave SPI application. The Master and Slave each have an 8-bit shift register with MSBit and LSBit labels. The Master's shift register is connected to an SPI clock generator. The Master's MOSI pin is connected to the Slave's MOSI pin. The Master's MISO pin is connected to the Slave's MISO pin. The Master's SCK pin is connected to the Slave's SCK pin. The Master's NSS pin is connected to VDD. The Slave's NSS pin is connected to ground. A note indicates that the NSS pin is not used if NSS is managed by software.

The diagram illustrates a single master/single slave SPI application. It shows two main components: a Master and a Slave. Each component has an 8-bit shift register with MSBit and LSBit labels. The Master's shift register is connected to an SPI clock generator. The Master's MOSI pin is connected to the Slave's MOSI pin. The Master's MISO pin is connected to the Slave's MISO pin. The Master's SCK pin is connected to the Slave's SCK pin. The Master's NSS pin is connected to VDD. The Slave's NSS pin is connected to ground. A note indicates that the NSS pin is not used if NSS is managed by software.

Block diagram of a single master/single slave SPI application. The Master and Slave each have an 8-bit shift register with MSBit and LSBit labels. The Master's shift register is connected to an SPI clock generator. The Master's MOSI pin is connected to the Slave's MOSI pin. The Master's MISO pin is connected to the Slave's MISO pin. The Master's SCK pin is connected to the Slave's SCK pin. The Master's NSS pin is connected to VDD. The Slave's NSS pin is connected to ground. A note indicates that the NSS pin is not used if NSS is managed by software.

1. Here, the NSS pin is configured as an input.

The MOSI pins are connected together and the MISO pins are connected together. In this way data is transferred serially between master and slave (most significant bit first).

The communication is always initiated by the master. When the master device transmits data to a slave device via the MOSI pin, the slave device responds via the MISO pin. This implies full-duplex communication with both data out and data in synchronized with the same clock signal (which is provided by the master device via the SCK pin).

Slave select (NSS) pin management

Hardware or software slave select management can be set using the SSM bit in the SPI_CR1 register.

Clock phase and clock polarity

Four possible timing relationships may be chosen by software, using the CPOL and CPHA bits in the SPI_CR1 register. The CPOL (clock polarity) bit controls the steady state value of the clock when no data is being transferred. This bit affects both master and slave modes. If CPOL is reset, the SCK pin has a low-level idle state. If CPOL is set, the SCK pin has a high-level idle state.

If the CPHA (clock phase) bit is set, the second edge on the SCK pin (falling edge if the CPOL bit is reset, rising edge if the CPOL bit is set) is the MSBit capture strobe. Data are

latched on the occurrence of the second clock transition. If the CPHA bit is reset, the first edge on the SCK pin (falling edge if CPOL bit is set, rising edge if CPOL bit is reset) is the MSBit capture strobe. Data are latched on the occurrence of the first clock transition.

The combination of the CPOL (clock polarity) and CPHA (clock phase) bits selects the data capture clock edge.

Figure 248, shows an SPI transfer with the four combinations of the CPHA and CPOL bits. The diagram may be interpreted as a master or slave timing diagram where the SCK pin, the MISO pin, the MOSI pin are directly connected between the master and the slave device.

Note: Prior to changing the CPOL/CPHA bits the SPI must be disabled by resetting the SPE bit.
Master and slave must be programmed with the same timing mode.
The idle state of SCK must correspond to the polarity selected in the SPI_CR1 register (by pulling up SCK if CPOL=1 or pulling down SCK if CPOL=0).
The Data Frame Format (8- or 16-bit) is selected through the DFF bit in SPI_CR1 register, and determines the data length during transmission/reception.

Figure 248. Data clock timing diagram

Figure 248: Data clock timing diagram illustrating waveforms for CPHA = 1 and CPHA = 0 configurations with CPOL = 1, CPOL = 0, MOSI, MISO, NSS (to slave), and Capture strobe.

Timing diagram detailing SPI modes:

Figure 248: Data clock timing diagram illustrating waveforms for CPHA = 1 and CPHA = 0 configurations with CPOL = 1, CPOL = 0, MOSI, MISO, NSS (to slave), and Capture strobe.

ai17154d

1. These timings are shown with the LSBFIRST bit reset in the SPI_CR1 register.

Data frame format

Data can be shifted out either MSB-first or LSB-first depending on the value of the LSBFIRST bit in the SPI_CR1 register.

Each data frame is 8 or 16 bits long depending on the size of the data programmed using the DFF bit in the SPI_CR1 register. The selected data frame format is applicable for transmission and/or reception.

28.3.2 Configuring the SPI in slave mode

In the slave configuration, the serial clock is received on the SCK pin from the master device. The value set in the BR[2:0] bits in the SPI_CR1 register, does not affect the data transfer rate.

Note: It is recommended to enable the SPI slave before the master sends the clock. If not, undesired data transmission might occur. The data register of the slave needs to be ready before the first edge of the communication clock or before the end of the ongoing communication. It is mandatory to have the polarity of the communication clock set to the steady state value before the slave and the master are enabled.

Follow the procedure below to configure the SPI in slave mode:

Procedure

  1. 1. Set the DFF bit to define 8- or 16-bit data frame format
  2. 2. Select the CPOL and CPHA bits to define one of the four relationships between the data transfer and the serial clock (see Figure 248). For correct data transfer, the CPOL and CPHA bits must be configured in the same way in the slave device and the master device. This step is not required when the TI mode is selected through the FRF bit in the SPI_CR2 register.
  3. 3. The frame format (MSB-first or LSB-first depending on the value of the LSBFIRST bit in the SPI_CR1 register) must be the same as the master device. This step is not required when TI mode is selected.
  4. 4. In Hardware mode (refer to Slave select (NSS) pin management), the NSS pin must be connected to a low level signal during the complete byte transmit sequence. In NSS software mode, set the SSM bit and clear the SSI bit in the SPI_CR1 register. This step is not required when TI mode is selected.
  5. 5. Set the FRF bit in the SPI_CR2 register to select the TI mode protocol for serial communications.
  6. 6. Clear the MSTR bit and set the SPE bit (both in the SPI_CR1 register) to assign the pins to alternate functions.

In this configuration the MOSI pin is a data input and the MISO pin is a data output.

Transmit sequence

The data byte is parallel-loaded into the Tx buffer during a write cycle.

The transmit sequence begins when the slave device receives the clock signal and the most significant bit of the data on its MOSI pin. The remaining bits (the 7 bits in 8-bit data frame format, and the 15 bits in 16-bit data frame format) are loaded into the shift-register. The TXE flag in the SPI_SR register is set on the transfer of data from the Tx Buffer to the shift register and an interrupt is generated if the TXEIE bit in the SPI_CR2 register is set.

Receive sequence

For the receiver, when data transfer is complete:

After the last sampling clock edge the RXNE bit is set, a copy of the data byte received in the shift register is moved to the Rx buffer. When the SPI_DR register is read, the SPI peripheral returns this buffered value.

Clearing of the RXNE bit is performed by reading the SPI_DR register.

SPI TI protocol in slave mode

In slave mode, the SPI interface is compatible with the TI protocol. The FRF bit of the SPI_CR2 register can be used to configure the slave SPI serial communications to be compliant with this protocol.

The clock polarity and phase are forced to conform to the TI protocol requirements whatever the values set in the SPI_CR1 register. NSS management is also specific to the TI protocol which makes the configuration of NSS management through the SPI_CR1 and SPI_CR2 registers (such as SSM, SSI, SSOE) transparent for the user.

In Slave mode (Figure 249 and Figure 250), the SPI baud rate prescaler is used to control the moment when the MISO pin state changes to HI-Z. Any baud rate can be used thus allowing to determine this moment with optimal flexibility. However, the baud rate is generally set to the external master clock baud rate. The time for the MISO signal to become HI-Z (\(t_{\text{release}}\)) depends on internal resynchronizations and on the baud rate value set in through BR[2:0] of SPI_CR1 register. It is given by the formula:

\[\frac{t_{\text{baud\_rate}}}{2} + 4 \times t_{\text{pclk}} < t_{\text{release}} < \frac{t_{\text{baud\_rate}}}{2} + 6 \times t_{\text{pclk}}\]

Note: This feature is not available for Motorola SPI communications (FRF bit set to 0).

To detect TI frame errors in Slave transmitter only mode by using the Error interrupt (ERRIE = 1), the SPI must be configured in 1-line bidirectional mode by setting BIDIMODE and BIDIOE to 1 in the SPI_CR1 register. When BIDIMODE is set to 0, OVR is set to 1 because the data register is never read and error interrupt are always generated, while when BIDIMODE is set to 1, data are not received and OVR is never set.

Figure 249. TI mode - Slave mode, single transfer

Timing diagram for SPI TI mode in slave mode, single transfer. It shows four signals: NSS input, SCK input, MOSI input, and MISO output. NSS input is an active-low signal that goes low to start the transfer and high to end it. SCK input is a clock signal. MOSI input shows data being shifted in (MSBIN, LSBIN) on the rising edges of SCK. MISO output shows data being shifted out (1 or 0, MSBOUT, LSBOUT) on the falling edges of SCK. The diagram includes labels for 'trigger edge' and 'sampling edge' on the SCK signal, and a 'Release' time interval for the MISO output after the last data bit is shifted out.

The diagram illustrates the timing relationship between the NSS input, SCK input, MOSI input, and MISO output signals in TI mode slave mode for a single transfer. The NSS input is an active-low signal that starts the transfer when it goes low and ends it when it goes high. The SCK input is a clock signal with rising edges labeled as 'trigger edge' and falling edges labeled as 'sampling edge'. The MOSI input shows data being shifted in (MSBIN, LSBIN) on the rising edges of SCK. The MISO output shows data being shifted out (1 or 0, MSBOUT, LSBOUT) on the falling edges of SCK. A 'Release' time interval is indicated for the MISO output after the last data bit is shifted out.

Timing diagram for SPI TI mode in slave mode, single transfer. It shows four signals: NSS input, SCK input, MOSI input, and MISO output. NSS input is an active-low signal that goes low to start the transfer and high to end it. SCK input is a clock signal. MOSI input shows data being shifted in (MSBIN, LSBIN) on the rising edges of SCK. MISO output shows data being shifted out (1 or 0, MSBOUT, LSBOUT) on the falling edges of SCK. The diagram includes labels for 'trigger edge' and 'sampling edge' on the SCK signal, and a 'Release' time interval for the MISO output after the last data bit is shifted out.

ai18434

Figure 250. TI mode - Slave mode, continuous transfer

Timing diagram for TI mode - Slave mode, continuous transfer. It shows four signals: NSS input, SCK input, MOSI input, and MISO output. NSS input is high during idle and low during active transfer. SCK input is a square wave clock. MOSI input shows data bits being sampled on the SCK edges, with labels 'trigger' and 'sampling' indicating the timing. MISO output shows data bits being shifted out, with labels '1 or 0' and 'MSBOUT' indicating the data. The diagram is divided into two frames, FRAME 1 and FRAME 2, by vertical dashed lines. The MOSI input shows 'DONTCARE' for the first bit of each frame. The MISO output shows 'MSBOUT' and 'LSBOUT' for the data bits. The diagram is labeled 'ai18435' in the bottom right corner.
Timing diagram for TI mode - Slave mode, continuous transfer. It shows four signals: NSS input, SCK input, MOSI input, and MISO output. NSS input is high during idle and low during active transfer. SCK input is a square wave clock. MOSI input shows data bits being sampled on the SCK edges, with labels 'trigger' and 'sampling' indicating the timing. MISO output shows data bits being shifted out, with labels '1 or 0' and 'MSBOUT' indicating the data. The diagram is divided into two frames, FRAME 1 and FRAME 2, by vertical dashed lines. The MOSI input shows 'DONTCARE' for the first bit of each frame. The MISO output shows 'MSBOUT' and 'LSBOUT' for the data bits. The diagram is labeled 'ai18435' in the bottom right corner.

28.3.3 Configuring the SPI in master mode

In the master configuration, the serial clock is generated on the SCK pin.

Procedure

  1. 1. Select the BR[2:0] bits to define the serial clock baud rate (see SPI_CR1 register).
  2. 2. Select the CPOL and CPHA bits to define one of the four relationships between the data transfer and the serial clock (see Figure 248). This step is not required when the TI mode is selected.
  3. 3. Set the DFF bit to define 8- or 16-bit data frame format
  4. 4. Configure the LSBFIRST bit in the SPI_CR1 register to define the frame format. This step is not required when the TI mode is selected.
  5. 5. If the NSS pin is required in input mode, in hardware mode, connect the NSS pin to a high-level signal during the complete byte transmit sequence. In NSS software mode, set the SSM and SSI bits in the SPI_CR1 register. If the NSS pin is required in output mode, the SSOE bit only should be set. This step is not required when the TI mode is selected.
  6. 6. Set the FRF bit in SPI_CR2 to select the TI protocol for serial communications.
  7. 7. The MSTR and SPE bits must be set (they remain set only if the NSS pin is connected to a high-level signal).

In this configuration the MOSI pin is a data output and the MISO pin is a data input.

Transmit sequence

The transmit sequence begins when a byte is written in the Tx Buffer.

The data byte is parallel-loaded into the shift register (from the internal bus) during the first bit transmission and then shifted out serially to the MOSI pin MSB first or LSB first depending on the LSBFIRST bit in the SPI_CR1 register. The TXE flag is set on the transfer of data from the Tx Buffer to the shift register and an interrupt is generated if the TXEIE bit in the SPI_CR2 register is set.

Receive sequence

For the receiver, when data transfer is complete:

At the last sampling clock edge the RXNE bit is set, a copy of the data byte received in the shift register is moved to the Rx buffer. When the SPI_DR register is read, the SPI peripheral returns this buffered value.

Clearing the RXNE bit is performed by reading the SPI_DR register.

A continuous transmit stream can be maintained if the next data to be transmitted is put in the Tx buffer once the transmission is started. Note that TXE flag should be '1' before any attempt to write the Tx buffer is made.

Note: When a master is communicating with SPI slaves which need to be de-selected between transmissions, the NSS pin must be configured as GPIO or another GPIO must be used and toggled by software.

SPI TI protocol in master mode

In master mode, the SPI interface is compatible with the TI protocol. The FRF bit of the SPI_CR2 register can be used to configure the master SPI serial communications to be compliant with this protocol.

The clock polarity and phase are forced to conform to the TI protocol requirements whatever the values set in the SPI_CR1 register. NSS management is also specific to the TI protocol which makes the configuration of NSS management through the SPI_CR1 and SPI_CR2 registers (SSM, SSI, SSOE) transparent for the user.

Figure 251 and Figure 252 show the SPI master communication waveforms when the TI mode is selected in master mode.

Figure 251. TI mode - master mode, single transfer

Timing diagram for SPI TI mode in master mode, single transfer. It shows four signals: NSS output, SCK output, MOSI input, and MISO output. NSS output is active low and goes high before the transfer and low again after. SCK output is a square wave clock. MOSI input shows data being shifted in (MSBIN, LSBIN) on the rising edges of SCK. MISO output shows data being shifted out (MSBOUT, LSBOUT) on the falling edges of SCK. The diagram includes labels for 'trigger edge' and 'sampling edge' on the SCK signal.

The diagram illustrates the timing relationship between the NSS output, SCK output, MOSI input, and MISO output signals during a single transfer in TI mode. The NSS output is active low and is pulled high before the transfer begins and low again after. The SCK output is a square wave clock. The MOSI input shows data being shifted in (MSBIN, LSBIN) on the rising edges of SCK. The MISO output shows data being shifted out (MSBOUT, LSBOUT) on the falling edges of SCK. The diagram includes labels for 'trigger edge' and 'sampling edge' on the SCK signal.

ai18436

Timing diagram for SPI TI mode in master mode, single transfer. It shows four signals: NSS output, SCK output, MOSI input, and MISO output. NSS output is active low and goes high before the transfer and low again after. SCK output is a square wave clock. MOSI input shows data being shifted in (MSBIN, LSBIN) on the rising edges of SCK. MISO output shows data being shifted out (MSBOUT, LSBOUT) on the falling edges of SCK. The diagram includes labels for 'trigger edge' and 'sampling edge' on the SCK signal.

Figure 252. TI mode - master mode, continuous transfer

Timing diagram for TI mode - master mode, continuous transfer. It shows four signals: NSS output, SCK output, MOSI output, and MISO input. NSS output is an active-low signal that starts high, goes low for the duration of the transfer, and then goes high again. SCK output is a clock signal. MOSI output shows data being shifted out, starting with DONT CARE, then MSBOUT, followed by LSBOUT. MISO input shows data being shifted in, starting with 1 or 0, then MSBIN, followed by LSBIN, and ending with DONT CARE. The diagram is divided into two frames, FRAME 1 and FRAME 2, by vertical dashed lines. The MISO input signal is labeled with '1 or 0' for the first bit, 'MSBIN' for the next, and 'LSBIN' for the last. The MOSI output signal is labeled with 'DONT CARE' for the first bit, 'MSBOUT' for the next, and 'LSBOUT' for the last. The SCK output signal is labeled with 'trigger' and 'sampling' points. The NSS output signal is labeled with 'trigger' and 'sampling' points. The diagram is labeled 'ai18437' in the bottom right corner.
Timing diagram for TI mode - master mode, continuous transfer. It shows four signals: NSS output, SCK output, MOSI output, and MISO input. NSS output is an active-low signal that starts high, goes low for the duration of the transfer, and then goes high again. SCK output is a clock signal. MOSI output shows data being shifted out, starting with DONT CARE, then MSBOUT, followed by LSBOUT. MISO input shows data being shifted in, starting with 1 or 0, then MSBIN, followed by LSBIN, and ending with DONT CARE. The diagram is divided into two frames, FRAME 1 and FRAME 2, by vertical dashed lines. The MISO input signal is labeled with '1 or 0' for the first bit, 'MSBIN' for the next, and 'LSBIN' for the last. The MOSI output signal is labeled with 'DONT CARE' for the first bit, 'MSBOUT' for the next, and 'LSBOUT' for the last. The SCK output signal is labeled with 'trigger' and 'sampling' points. The NSS output signal is labeled with 'trigger' and 'sampling' points. The diagram is labeled 'ai18437' in the bottom right corner.

28.3.4 Configuring the SPI for half-duplex communication

The SPI is capable of operating in half-duplex mode in 2 configurations.

1 clock and 1 bidirectional data wire (BIDIMODE = 1)

This mode is enabled by setting the BIDIMODE bit in the SPI_CR1 register. In this mode SCK is used for the clock and MOSI in master or MISO in slave mode is used for data communication. The transfer direction (Input/Output) is selected by the BIDIOE bit in the SPI_CR1 register. When this bit is 1, the data line is output otherwise it is input.

1 clock and 1 unidirectional data wire (BIDIMODE = 0)

In this mode, the application can use the SPI either in transmit-only mode or in receive-only mode.

To start the communication in receive-only mode, configure and enable the SPI:

28.3.5 Data transmission and reception procedures

Rx and Tx buffers

In reception, data are received and then stored into an internal Rx buffer while In transmission, data are first stored into an internal Tx buffer before being transmitted.

A read access of the SPI_DR register returns the Rx buffered value whereas a write access to the SPI_DR stores the written data into the Tx buffer.

Start sequence in master mode

Start sequence in slave mode

Handling data transmission and reception

The TXE flag (Tx buffer empty) is set when the data are transferred from the Tx buffer to the shift register. It indicates that the internal Tx buffer is ready to be loaded with the next data. An interrupt can be generated if the TXEIE bit in the SPI_CR2 register is set. Clearing the TXE bit is performed by writing to the SPI_DR register.

Note: The software must ensure that the TXE flag is set to 1 before attempting to write to the Tx buffer. Otherwise, it overwrites the data previously written to the Tx buffer.

The RXNE flag (Rx buffer not empty) is set on the last sampling clock edge, when the data are transferred from the shift register to the Rx buffer. It indicates that data are ready to be read from the SPI_DR register. An interrupt can be generated if the RXNEIE bit in the SPI_CR2 register is set. Clearing the RXNE bit is performed by reading the SPI_DR register.

For some configurations, the BSY flag can be used during the last data transfer to wait until the completion of the transfer.

Full-duplex transmit and receive procedure in master or slave mode (BIDIMODE=0 and RXONLY=0)

The software has to follow this procedure to transmit and receive data (see Figure 253 and Figure 254):

  1. 1. Enable the SPI by setting the SPE bit to 1.
  2. 2. Write the first data item to be transmitted into the SPI_DR register (this clears the TXE flag).
  3. 3. Wait until TXE=1 and write the second data item to be transmitted. Then wait until RXNE=1 and read the SPI_DR to get the first received data item (this clears the RXNE bit). Repeat this operation for each data item to be transmitted/received until the n–1 received data.
  4. 4. Wait until RXNE=1 and read the last received data.
  5. 5. Wait until TXE=1 and then wait until BSY=0 before disabling the SPI.

This procedure can also be implemented using dedicated interrupt subroutines launched at each rising edges of the RXNE or TXE flag.

Figure 253. TXE/RXNE/BSY behavior in Master / full-duplex mode (BIDIMODE=0 and RXONLY=0) in case of continuous transfers

Timing diagram for SPI Master mode showing SCK, MISO/MOSI (out), TXE flag, Tx buffer, BSY flag, MISO/MOSI (in), RXNE flag, and Rx buffer. It illustrates the sequence of writing to the Tx buffer, waiting for TXE=1, and reading from the Rx buffer when RXNE=1 for three consecutive data transfers (DATA1=0xF1, DATA2=0xF2, DATA3=0xF3 and DATA1=0xA1, DATA2=0xA2, DATA3=0xA3).

Example in Master mode with CPOL=1, CPHA=1

The diagram illustrates the behavior of the TXE, RXNE, and BSY flags during continuous SPI transfers in Master mode. It shows the SCK clock, MISO/MOSI (out) and (in) data lines, TXE and RXNE flags, Tx buffer (write SPI_DR), and Rx buffer (read SPI_DR). The sequence of operations is as follows:

The BSY flag is set by hardware when a transfer starts and reset by hardware when it ends. The TXE flag is set by hardware when the Tx buffer is empty and cleared by software when data is written. The RXNE flag is set by hardware when data is received and cleared by software when data is read.

ai17343

Timing diagram for SPI Master mode showing SCK, MISO/MOSI (out), TXE flag, Tx buffer, BSY flag, MISO/MOSI (in), RXNE flag, and Rx buffer. It illustrates the sequence of writing to the Tx buffer, waiting for TXE=1, and reading from the Rx buffer when RXNE=1 for three consecutive data transfers (DATA1=0xF1, DATA2=0xF2, DATA3=0xF3 and DATA1=0xA1, DATA2=0xA2, DATA3=0xA3).

Figure 254. TXE/RXNE/BSY behavior in Slave / full-duplex mode (BIDIMODE=0, RXONLY=0) in case of continuous transfers

Timing diagram for SPI Slave mode in full-duplex continuous transfers. It shows the SCK clock, MISO/MOSI (out) and (in) data lines, TXE and RXNE flags, BSY flag, Tx buffer, and Rx buffer. The diagram illustrates three data transfers: DATA 1 = 0xF1, DATA 2 = 0xF2, and DATA 3 = 0xF3. The TXE flag is set by hardware and cleared by software. The RXNE flag is set by hardware and cleared by software. The BSY flag is set by hardware and cleared by software. The Tx buffer is written to by software and read from by software. The Rx buffer is read from by software. The diagram includes callouts for software actions: writing to SPI_DR, waiting for TXE=1, waiting for RXNE=1, and reading from SPI_DR.

Example in Slave mode with CPOL=1, CPHA=1

The diagram illustrates the behavior of the TXE, RXNE, and BSY flags in Slave mode with CPOL=1 and CPHA=1 during continuous transfers. It shows the SCK clock, MISO/MOSI (out) and (in) data lines, TXE and RXNE flags, BSY flag, Tx buffer, and Rx buffer. The TXE flag is set by hardware and cleared by software. The RXNE flag is set by hardware and cleared by software. The BSY flag is set by hardware and cleared by software. The Tx buffer is written to by software and read from by software. The Rx buffer is read from by software. The diagram includes callouts for software actions: writing to SPI_DR, waiting for TXE=1, waiting for RXNE=1, and reading from SPI_DR.

MSv76802V1

Timing diagram for SPI Slave mode in full-duplex continuous transfers. It shows the SCK clock, MISO/MOSI (out) and (in) data lines, TXE and RXNE flags, BSY flag, Tx buffer, and Rx buffer. The diagram illustrates three data transfers: DATA 1 = 0xF1, DATA 2 = 0xF2, and DATA 3 = 0xF3. The TXE flag is set by hardware and cleared by software. The RXNE flag is set by hardware and cleared by software. The BSY flag is set by hardware and cleared by software. The Tx buffer is written to by software and read from by software. The Rx buffer is read from by software. The diagram includes callouts for software actions: writing to SPI_DR, waiting for TXE=1, waiting for RXNE=1, and reading from SPI_DR.

Transmit-only procedure (BIDIMODE=0 RXONLY=0)

In this mode, the procedure can be reduced as described below and the BSY bit can be used to wait until the completion of the transmission (see Figure 255 and Figure 256).

  1. 1. Enable the SPI by setting the SPE bit to 1.
  2. 2. Write the first data item to send into the SPI_DR register (this clears the TXE bit).
  3. 3. Wait until TXE=1 and write the next data item to be transmitted. Repeat this step for each data item to be transmitted.
  4. 4. After writing the last data item into the SPI_DR register, wait until TXE=1, then wait until BSY=0, this indicates that the transmission of the last data is complete.

This procedure can be also implemented using dedicated interrupt subroutines launched at each rising edge of the TXE flag.

Note: During discontinuous communications, there is a 2 APB clock period delay between the write operation to SPI_DR and the BSY bit setting. As a consequence, in transmit-only mode, it is mandatory to wait first until TXE is set and then until BSY is cleared after writing the last data.

After transmitting two data items in transmit-only mode, the OVR flag is set in the SPI_SR register since the received data are never read.

Figure 255. TXE/BSY behavior in Master transmit-only mode (BIDIMODE=0 and RXONLY=0) in case of continuous transfers

Timing diagram for Master transmit-only mode showing SCK, MISO/MOSI (out), TXE flag, Tx buffer, and BSY flag over three data transfers (DATA 1 = 0xF1, DATA 2 = 0xF2, DATA 3 = 0xF3).

Example in Master mode with CPOL=1, CPHA=1

The diagram illustrates the following sequence for continuous transfers in Master mode:

Key signals and events:

ai17345

Timing diagram for Master transmit-only mode showing SCK, MISO/MOSI (out), TXE flag, Tx buffer, and BSY flag over three data transfers (DATA 1 = 0xF1, DATA 2 = 0xF2, DATA 3 = 0xF3).

Figure 256. TXE/BSY in Slave transmit-only mode (BIDIMODE=0 and RXONLY=0) in case of continuous transfers

Timing diagram for Slave transmit-only mode showing SCK, MISO/MOSI (out), TXE flag, Tx buffer, and BSY flag over three data transfers (DATA 1 = 0xF1, DATA 2 = 0xF2, DATA 3 = 0xF3).

Example in slave mode with CPOL=1, CPHA=1

The diagram illustrates the following sequence for continuous transfers in Slave mode:

Key signals and events:

ai17346

Timing diagram for Slave transmit-only mode showing SCK, MISO/MOSI (out), TXE flag, Tx buffer, and BSY flag over three data transfers (DATA 1 = 0xF1, DATA 2 = 0xF2, DATA 3 = 0xF3).

Bidirectional transmit procedure (BIDIMODE=1 and BIDIOE=1)

In this mode, the procedure is similar to the procedure in Transmit-only mode except that the BIDIMODE and BIDIOE bits both have to be set in the SPI_CR2 register before enabling the SPI.

Unidirectional receive-only procedure (BIDIMODE=0 and RXONLY=1)

In this mode, the procedure can be reduced as described below (see Figure 257):

  1. 1. Set the RXONLY bit in the SPI_CR1 register.
  2. 2. Enable the SPI by setting the SPE bit to 1:
  1. a) In master mode, this immediately activates the generation of the SCK clock, and data are serially received until the SPI is disabled (SPE=0).
  2. b) In slave mode, data are received when the SPI master device drives NSS low and generates the SCK clock.
  3. 3. Wait until RXNE=1 and read the SPI_DR register to get the received data (this clears the RXNE bit). Repeat this operation for each data item to be received.

This procedure can also be implemented using dedicated interrupt subroutines launched at each rising edge of the RXNE flag.

Note: If it is required to disable the SPI after the last transfer, follow the recommendation described in Section 28.3.8.

Figure 257. RXNE behavior in receive-only mode (BIDIRMODE=0 and RXONLY=1) in case of continuous transfers

Timing diagram for RXNE behavior in receive-only mode. It shows three data transfers: DATA 1 = 0xA1, DATA 2 = 0xA2, and DATA 3 = 0xA3. The SCK signal is a continuous clock. The MISO/MOSI (in) signal shows the serial data bits (b0 to b7) for each transfer. The RXNE flag is set by hardware at the end of each transfer and cleared by software. The Rx buffer (read from SPI_DR) shows the data values 0xA1, 0xA2, and 0xA3. Callouts indicate that software waits until RXNE=1 and then reads the data from SPI_DR.

Example with CPOL=1, CPHA=1, RXONLY=1

The diagram illustrates the RXNE behavior in receive-only mode (BIDIRMODE=0 and RXONLY=1) for continuous transfers. It shows the SCK clock, the MISO/MOSI (in) signal, the RXNE flag, and the Rx buffer (read from SPI_DR). The SCK clock is continuous. The MISO/MOSI (in) signal shows the serial data bits (b0 to b7) for three data transfers: DATA 1 = 0xA1, DATA 2 = 0xA2, and DATA 3 = 0xA3. The RXNE flag is set by hardware at the end of each transfer and cleared by software. The Rx buffer (read from SPI_DR) shows the data values 0xA1, 0xA2, and 0xA3. Callouts indicate that software waits until RXNE=1 and then reads the data from SPI_DR.

ai17347

Timing diagram for RXNE behavior in receive-only mode. It shows three data transfers: DATA 1 = 0xA1, DATA 2 = 0xA2, and DATA 3 = 0xA3. The SCK signal is a continuous clock. The MISO/MOSI (in) signal shows the serial data bits (b0 to b7) for each transfer. The RXNE flag is set by hardware at the end of each transfer and cleared by software. The Rx buffer (read from SPI_DR) shows the data values 0xA1, 0xA2, and 0xA3. Callouts indicate that software waits until RXNE=1 and then reads the data from SPI_DR.

Bidirectional receive procedure (BIDIMODE=1 and BIDIOE=0)

In this mode, the procedure is similar to the Receive-only mode procedure except that the BIDIMODE bit has to be set and the BIDIOE bit cleared in the SPI_CR2 register before enabling the SPI.

Continuous and discontinuous transfers

When transmitting data in master mode, if the software is fast enough to detect each rising edge of TXE (or TXE interrupt) and to immediately write to the SPI_DR register before the ongoing data transfer is complete, the communication is said to be continuous. In this case, there is no discontinuity in the generation of the SPI clock between each data item and the BSY bit is never cleared between each data transfer.

On the contrary, if the software is not fast enough, this can lead to some discontinuities in the communication. In this case, the BSY bit is cleared between each data transmission (see Figure 258).

In Master receive-only mode (RXONLY=1), the communication is always continuous and the BSY flag is always read at 1.

In slave mode, the continuity of the communication is decided by the SPI master device. In any case, even if the communication is continuous, the BSY flag goes low between each transfer for a minimum duration of one SPI clock cycle (see Figure 256).

Figure 258. TXE/BSY behavior when transmitting (BIDIRMODE=0 and RXONLY=0)
in case of discontinuous transfers

Figure 258: TXE/BSY behavior when transmitting (BIDIRMODE=0 and RXONLY=0) in case of discontinuous transfers (Example with CPOL=1, CPHA=1)

Diagram depicting timing waveforms for SCK, MOSI (out), TXE flag, Tx buffer (write to SPI_DR), and BSY flag during three discontinuous 8-bit data transfers (DATA 1 = 0xF1, DATA 2 = 0xF2, DATA 3 = 0xF3) with annotations for software action triggers.

Figure 258: TXE/BSY behavior when transmitting (BIDIRMODE=0 and RXONLY=0) in case of discontinuous transfers (Example with CPOL=1, CPHA=1)

28.3.6 CRC calculation

A CRC calculator has been implemented for communication reliability. Separate CRC calculators are implemented for transmitted data and received data. The CRC is calculated using a programmable polynomial serially on each bit. It is calculated on the sampling clock edge defined by the CPHA and CPOL bits in the SPI_CR1 register.

Note: This SPI offers two kinds of CRC calculation standard which depend directly on the data frame format selected for the transmission and/or reception: 8-bit data (CR8) and 16-bit data (CRC16).

CRC calculation is enabled by setting the CRCEN bit in the SPI_CR1 register. This action resets the CRC registers (SPI_RXCRCR and SPI_TXCRCR). In full duplex or transmitter only mode, when the transfers are managed by the software (CPU mode), it is necessary to write the bit CRCNEXT immediately after the last data to be transferred is written to the SPI_DR. At the end of this last data transfer, the SPI_TXCRCR value is transmitted.

In receive only mode and when the transfers are managed by software (CPU mode), it is necessary to write the CRCNEXT bit after the second last data has been received. The CRC is received just after the last data reception and the CRC check is then performed.

At the end of data and CRC transfers, the CRCERR flag in the SPI_SR register is set if corruption occurs during the transfer.

If data are present in the TX buffer, the CRC value is transmitted only after the transmission of the data byte. During CRC transmission, the CRC calculator is switched off and the register value remains unchanged.

SPI communication using the CRC is possible through the following procedure:

  1. 1. Program the CPOL, CPHA, LSBFirst, BR, SSM, SSI and MSTR values.
  2. 2. Program the polynomial in the SPI_CRCPR register.
  3. 3. Enable the CRC calculation by setting the CRCEN bit in the SPI_CR1 register. This also clears the SPI_RXCRCR and SPI_TXCRCR registers.
  1. 4. Enable the SPI by setting the SPE bit in the SPI_CR1 register.
  2. 5. Start the communication and sustain the communication until all but one byte or half-word have been transmitted or received.
    • – In full duplex or transmitter-only mode, when the transfers are managed by software, when writing the last byte or half word to the Tx buffer, set the CRCNEXT bit in the SPI_CR1 register to indicate that the CRC is transmitted after the transmission of the last byte.
    • – In receiver only mode, set the bit CRCNEXT just after the reception of the second to last data to prepare the SPI to enter in CRC Phase at the end of the reception of the last data. CRC calculation is frozen during the CRC transfer.
  3. 6. After the transfer of the last byte or half word, the SPI enters the CRC transfer and check phase. In full duplex mode or receiver-only mode, the received CRC is compared to the SPI_RXCRCR value. If the value does not match, the CRCERR flag in SPI_SR is set and an interrupt can be generated when the ERRIE bit in the SPI_CR2 register is set.

Note: When the SPI is in slave mode, be careful to enable CRC calculation only when the clock is stable, that is, when the clock is in the steady state. If not, a wrong CRC calculation may be done. In fact, the CRC is sensitive to the SCK slave input clock as soon as CRCEN is set, and this, whatever the value of the SPE bit.

With high bitrate frequencies, be careful when transmitting the CRC. As the number of used CPU cycles has to be as low as possible in the CRC transfer phase, it is forbidden to call software functions in the CRC transmission sequence to avoid errors in the last data and CRC reception. In fact, CRCNEXT bit has to be written before the end of the transmission/reception of the last data.

For high bit rate frequencies, it is advised to use the DMA mode to avoid the degradation of the SPI speed performance due to CPU accesses impacting the SPI bandwidth.

When the devices are configured as slaves and the NSS hardware mode is used, the NSS pin needs to be kept low between the data phase and the CRC phase.

When the SPI is configured in slave mode with the CRC feature enabled, CRC calculation takes place even if a high level is applied on the NSS pin. This may happen for example in case of a multislave environment where the communication master addresses slaves alternately.

Between a slave deselection (high level on NSS) and a new slave selection (low level on NSS), the CRC value should be cleared on both master and slave sides in order to resynchronize the master and slave for their respective CRC calculation.

To clear the CRC, follow the procedure below:

  1. 1. Disable SPI (SPE = 0)
  2. 2. Clear the CRCEN bit
  3. 3. Set the CRCEN bit
  4. 4. Enable the SPI (SPE = 1)

28.3.7 Status flags

Four status flags are provided for the application to completely monitor the state of the SPI bus.

Tx buffer empty flag (TXE)

When it is set, this flag indicates that the Tx buffer is empty and the next data to be transmitted can be loaded into the buffer. The TXE flag is cleared when writing to the SPI_DR register.

Rx buffer not empty (RXNE)

When set, this flag indicates that there are valid received data in the Rx buffer. It is cleared when SPI_DR is read.

BUSY flag

This BSY flag is set and cleared by hardware (writing to this flag has no effect). The BSY flag indicates the state of the communication layer of the SPI.

When BSY is set, it indicates that the SPI is busy communicating. There is one exception in master mode / bidirectional receive mode (MSTR=1 and BDM=1 and BDOE=0) where the BSY flag is kept low during reception.

The BSY flag is useful to detect the end of a transfer if the software wants to disable the SPI and enter Halt mode (or disable the peripheral clock). This avoids corrupting the last transfer. For this, the procedure described below must be strictly respected.

The BSY flag is also useful to avoid write collisions in a multimaster system.

The BSY flag is set when a transfer starts, with the exception of master mode / bidirectional receive mode (MSTR=1 and BDM=1 and BDOE=0).

It is cleared:

When communication is not continuous, the BSY flag is low between each communication.

When communication is continuous:

Note: Do not use the BSY flag to handle each data transmission or reception. It is better to use the TXE and RXNE flags instead.

28.3.8 Disabling the SPI

When a transfer is terminated, the application can stop the communication by disabling the SPI peripheral. This is done by clearing the SPE bit.

For some configurations, disabling the SPI and entering the Halt mode while a transfer is ongoing can cause the current transfer to be corrupted and/or the BSY flag might become unreliable.

To avoid any of those effects, it is recommended to respect the following procedure when disabling the SPI:

In master or slave full-duplex mode (BIDIMODE=0, RXONLY=0)

  1. 1. Wait until RXNE=1 to receive the last data
  2. 2. Wait until TXE=1
  3. 3. Then wait until BSY=0
  4. 4. Disable the SPI (SPE=0) and, eventually, enter the Halt mode (or disable the peripheral clock)

In master or slave unidirectional transmit-only mode (BIDIMODE=0, RXONLY=0) or bidirectional transmit mode (BIDIMODE=1, BIDIOE=1)

After the last data is written into the SPI_DR register:

  1. 1. Wait until TXE=1
  2. 2. Then wait until BSY=0
  3. 3. Disable the SPI (SPE=0) and, eventually, enter the Halt mode (or disable the peripheral clock)

In master unidirectional receive-only mode (MSTR=1, BIDIMODE=0, RXONLY=1) or bidirectional receive mode (MSTR=1, BIDIMODE=1, BIDIOE=0)

This case must be managed in a particular way to ensure that the SPI does not initiate a new transfer. The sequence below is valid only for SPI Motorola configuration (FRF bit set to 0):

  1. 1. Wait for the second to last occurrence of RXNE=1 (n-1)
  2. 2. Then wait for one SPI clock cycle (using a software loop) before disabling the SPI (SPE=0)
  3. 3. Then wait for the last RXNE=1 before entering the Halt mode (or disabling the peripheral clock)

When the SPI is configured in TI mode (Bit FRF set to 1), the following procedure has to be respected to avoid generating an undesired pulse on NSS when the SPI is disabled:

  1. 1. Wait for the second to last occurrence of RXNE = 1 (n-1).
  2. 2. Disable the SPI (SPE = 0) in the following window frame using a software loop:
    • – After at least one SPI clock cycle,
    • – Before the beginning of the LSB data transfer.

Note: In master bidirectional receive mode (MSTR=1 and BDM=1 and BDOE=0), the BSY flag is kept low during transfers.

In slave receive-only mode (MSTR=0, BIDIMODE=0, RXONLY=1) or bidirectional receive mode (MSTR=0, BIDIMODE=1, BIDOE=0)

  1. 1. You can disable the SPI (write SPE=1) at any time: the current transfer completes before the SPI is effectively disabled
  2. 2. Then, if you want to enter the Halt mode, you must first wait until BSY = 0 before entering the Halt mode (or disabling the peripheral clock).

28.3.9 SPI communication using DMA (direct memory addressing)

To operate at its maximum speed, the SPI needs to be fed with the data for transmission and the data received on the Rx buffer should be read to avoid overrun. To facilitate the

transfers, the SPI features a DMA capability implementing a simple request/acknowledge protocol.

A DMA access is requested when the enable bit in the SPI_CR2 register is enabled. Separate requests must be issued to the Tx and Rx buffers (see Figure 259 and Figure 260):

When the SPI is used only to transmit data, it is possible to enable only the SPI Tx DMA channel. In this case, the OVR flag is set because the data received are not read.

When the SPI is used only to receive data, it is possible to enable only the SPI Rx DMA channel.

In transmission mode, when the DMA has written all the data to be transmitted (flag TCIF is set in the DMA_ISR register), the BSY flag can be monitored to ensure that the SPI communication is complete. This is required to avoid corrupting the last transmission before disabling the SPI or entering the Stop mode. The software must first wait until TXE=1 and then until BSY=0.

Note: During discontinuous communications, there is a 2 APB clock period delay between the write operation to SPI_DR and the BSY bit setting. As a consequence, it is mandatory to wait first until TXE=1 and then until BSY=0 after writing the last data.

Figure 259. Transmission using DMA

Timing diagram for SPI transmission using DMA. It shows the relationship between SCK, MISO/MOSI (out), TXE flag, BSY flag, DMA request, Tx buffer (write to SPI_DR), DMA writes to SPI_DR, and DMA TCIF flag (DMA transfer complete). The diagram illustrates three data transfers: DATA 1 = 0xF1, DATA 2 = 0xF2, and DATA 3 = 0xF3. Each transfer consists of 8 bits (b0 to b7). The TXE flag is set by hardware and cleared by DMA write. The BSY flag is set by hardware and reset by hardware. The DMA request is set by hardware and cleared by software. The DMA TCIF flag is set by hardware and cleared by software. The diagram also shows the software configuration of the DMA SPI Tx channel and the waiting for TXE=1 and BSY=0.

Example with CPOL=1, CPHA=1

SCK

MISO/MOSI (out)

TXE flag

BSY flag

DMA request

Tx buffer (write to SPI_DR)

DMA writes to SPI_DR

DMA TCIF flag (DMA transfer complete)

DATA 1 = 0xF1

DATA 2 = 0xF2

DATA 3 = 0xF3

software configures the DMA SPI Tx channel to send 3 data items and enables the SPI

DMA writes DATA1 into SPI_DR

DMA writes DATA2 into SPI_DR

DMA writes DATA3 into SPI_DR

DMA transfer is complete (TCIF=1 in DMA_ISR)

software waits until TXE=1

software waits until BSY=0

ai17349

Timing diagram for SPI transmission using DMA. It shows the relationship between SCK, MISO/MOSI (out), TXE flag, BSY flag, DMA request, Tx buffer (write to SPI_DR), DMA writes to SPI_DR, and DMA TCIF flag (DMA transfer complete). The diagram illustrates three data transfers: DATA 1 = 0xF1, DATA 2 = 0xF2, and DATA 3 = 0xF3. Each transfer consists of 8 bits (b0 to b7). The TXE flag is set by hardware and cleared by DMA write. The BSY flag is set by hardware and reset by hardware. The DMA request is set by hardware and cleared by software. The DMA TCIF flag is set by hardware and cleared by software. The diagram also shows the software configuration of the DMA SPI Tx channel and the waiting for TXE=1 and BSY=0.

Figure 260. Reception using DMA

Timing diagram for SPI reception using DMA. It shows the SCK clock, MISO/MOSI (in) data lines for three data items (DATA 1 = 0xA1, DATA 2 = 0xA2, DATA 3 = 0xA3), RXNE flag, DMA request, Rx buffer (read from SPI_DR), DMA read from SPI_DR, and flag DMA TCIF (DMA transfer complete). The diagram illustrates the sequence of events: software configures the DMA SPI Rx channel, DMA reads DATA1 from SPI_DR, DMA reads DATA2 from SPI_DR, DMA reads DATA3 from SPI_DR, and the DMA transfer is complete (TCIF=1 in DMA_ISR).

Example with CPOL=1, CPHA=1

The diagram illustrates the reception of three data items (DATA 1 = 0xA1, DATA 2 = 0xA2, DATA 3 = 0xA3) using DMA. The SCK clock is shown at the top. The MISO/MOSI (in) line shows the data being received. The RXNE flag is set by hardware and cleared by DMA read. The DMA request is generated when the RXNE flag is set. The Rx buffer (read from SPI_DR) shows the data being read. The DMA read from SPI_DR shows the DMA reading the data. The flag DMA TCIF (DMA transfer complete) is set by hardware and cleared by software.

software configures the DMA SPI Rx channel to receive 3 data items and enables the SPI

DMA reads DATA1 from SPI_DR

DMA reads DATA2 from SPI_DR

DMA reads DATA3 from SPI_DR

The DMA transfer is complete (TCIF=1 in DMA_ISR)

ai17350

Timing diagram for SPI reception using DMA. It shows the SCK clock, MISO/MOSI (in) data lines for three data items (DATA 1 = 0xA1, DATA 2 = 0xA2, DATA 3 = 0xA3), RXNE flag, DMA request, Rx buffer (read from SPI_DR), DMA read from SPI_DR, and flag DMA TCIF (DMA transfer complete). The diagram illustrates the sequence of events: software configures the DMA SPI Rx channel, DMA reads DATA1 from SPI_DR, DMA reads DATA2 from SPI_DR, DMA reads DATA3 from SPI_DR, and the DMA transfer is complete (TCIF=1 in DMA_ISR).

DMA capability with CRC

When SPI communication is enabled with CRC communication and DMA mode, the transmission and reception of the CRC at the end of communication are automatic that is without using the bit CRCNEXT. After the CRC reception, the CRC must be read in the SPI_DR register to clear the RXNE flag.

At the end of data and CRC transfers, the CRCERR flag in SPI_SR is set if corruption occurs during the transfer.

28.3.10 Error flags

Master mode fault (MODF)

Master mode fault occurs when the master device has its NSS pin pulled low (in NSS hardware mode) or SSI bit low (in NSS software mode), this automatically sets the MODF bit. Master mode fault affects the SPI peripheral in the following ways:

Use the following software sequence to clear the MODF bit:

  1. 1. Make a read or write access to the SPI_SR register while the MODF bit is set.
  2. 2. Then write to the SPI_CR1 register.

To avoid any multiple slave conflicts in a system comprising several MCUs, the NSS pin must be pulled high during the MODF bit clearing sequence. The SPE and MSTR bits can be restored to their original state after this clearing sequence.

As a security, hardware does not allow the setting of the SPE and MSTR bits while the MODF bit is set.

In a slave device the MODF bit cannot be set. However, in a multimaster configuration, the device can be in slave mode with this MODF bit set. In this case, the MODF bit indicates that there might have been a multimaster conflict for system control. An interrupt routine can be used to recover cleanly from this state by performing a reset or returning to a default state.

Overrun condition

An overrun condition occurs when the master device has sent data bytes and the slave device has not cleared the RXNE bit resulting from the previous data byte transmitted. When an overrun condition occurs:

In this case, the receiver buffer contents are not updated with the newly received data from the master device. A read from the SPI_DR register returns this byte. All other subsequently transmitted bytes are lost.

Clearing the OVR bit is done by a read from the SPI_DR register followed by a read access to the SPI_SR register.

CRC error

This flag is used to verify the validity of the value received when the CRCEN bit in the SPI_CR1 register is set. The CRCERR flag in the SPI_SR register is set if the value received in the shift register does not match the receiver SPI_RXCRCR value.

TI mode frame format error

A TI mode frame format error is detected when an NSS pulse occurs during an ongoing communication when the SPI is acting in slave mode and configured to conform to the TI mode protocol. When this error occurs, the FRE 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 lost of two data bytes.

The FRE flag is cleared when SPI_SR register is read. If the bit ERRIE is set, an interrupt is generated on the NSS error detection. In this case, the SPI should be disabled because data consistency is no more guaranteed and communications should be reinitiated by the master when the slave SPI is re-enabled.

Figure 261. TI mode frame format error detection

Timing diagram for TI mode frame format error detection showing NSS output, SCK output, MOSI input, MISO output, and TIFRFE signal. The diagram illustrates a frame format error where the MISO output is sampled incorrectly by the MOSI input during a sampling phase, triggering the TIFRFE interrupt.

The diagram shows the following signals over time:

ai18438

Timing diagram for TI mode frame format error detection showing NSS output, SCK output, MOSI input, MISO output, and TIFRFE signal. The diagram illustrates a frame format error where the MISO output is sampled incorrectly by the MOSI input during a sampling phase, triggering the TIFRFE interrupt.

28.3.11 SPI interrupts

Table 126. SPI interrupt requests

Interrupt eventEvent flagEnable Control bit
Transmit buffer empty flagTXETXEIE
Receive buffer not empty flagRXNERXNEIE
Master mode fault eventMODFERRIE
Overrun errorOVR
CRC error flagCRCERR
TI frame format errorFREERRIE

28.4 I2S functional description

28.4.1 I2S general description

The block diagram of the I2S is shown in Figure 262.

Figure 262. I2S block diagram

Figure 262: I²S block diagram showing the internal architecture including Tx buffer, Rx buffer, Shift register, Communication control, Master control logic, SPI baud rate generator, I2S clock generator, configuration registers, and interface pins (MOSI/SD, MISO/I2S2ext_SD/I2S3ext_SD, NSS/WS, CK, MCK).
Figure 262: I²S block diagram showing the internal architecture including Tx buffer, Rx buffer, Shift register, Communication control, Master control logic, SPI baud rate generator, I2S clock generator, configuration registers, and interface pins (MOSI/SD, MISO/I2S2ext_SD/I2S3ext_SD, NSS/WS, CK, MCK).

MS19909V1

1. I2S2ext_SD and I2S3ext_SD are the extended SD pins that control the I2S full duplex mode.

The SPI could function as an audio I2S interface when the I2S capability is enabled (by setting the I2SMOD bit in the SPI_I2SCFGR register). This interface uses almost the same pins, flags and interrupts as the SPI.

The I2S shares three common pins with the SPI:

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

The I2S uses its own clock generator to produce the communication clock when it is set in master mode. This clock generator is also the source of the master clock output. Two additional registers are available in I2S mode. One is linked to the clock generator configuration SPI_I2SPR and the other one is a generic I2S configuration register SPI_I2SCFGR (audio standard, slave/master mode, data format, packet frame, clock polarity, etc.).

The SPI_CR1 register and all CRC registers are not used in the I2S mode. Likewise, the SSOE bit in the SPI_CR2 register and the MODF and CRCERR bits in the SPI_SR are not used.

The I2S uses the same SPI register for data transfer (SPI_DR) in 16-bit wide mode.

28.4.2 I2S full duplex

To support I2S full duplex mode, two extra I2S instances called extended I2Ss (I2S2_ext, I2S3_ext) are available in addition to I2S2 and I2S3 (see Figure 263). The first I2S full-duplex interface is consequently based on I2S2 and I2S2_ext, and the second one on I2S3 and I2S3_ext.

Note: I2S2_ext an I2S3_ext are used only in full-duplex mode.

Figure 263. I2S full duplex block diagram

Block diagram of I2S full duplex configuration showing two parallel I2S instances (SPI/I2Sx and I2Sx_ext) sharing common control signals (I2Sx_SCK and I2S_WS) and having separate data inputs/outputs (SPIx_MOSI/I2Sx_SD and I2Sx_extSD).
graph LR
    I2Sx_SCK --> SPI_I2Sx
    I2Sx_SCK --> I2Sx_ext
    I2S_WS --> SPI_I2Sx
    I2S_WS --> I2Sx_ext
    SPI_I2Sx --> SPIx_MOSI_I2Sx_SD
    I2Sx_ext --> I2Sx_extSD
  

The diagram illustrates the I2S full duplex block structure. It features two parallel blocks: 'SPI/I2Sx' and 'I2Sx_ext'. Both blocks share two common input signals: 'I2Sx_SCK' (Serial Clock) and 'I2S_WS' (Word Select). The 'SPI/I2Sx' block has a bidirectional data interface labeled 'SPIx_MOSI/I2Sx_SD(in/out)'. The 'I2Sx_ext' block has a bidirectional data interface labeled 'I2Sx_extSD(in/out)'. The diagram is labeled 'MS19910V1' in the bottom right corner.

Block diagram of I2S full duplex configuration showing two parallel I2S instances (SPI/I2Sx and I2Sx_ext) sharing common control signals (I2Sx_SCK and I2S_WS) and having separate data inputs/outputs (SPIx_MOSI/I2Sx_SD and I2Sx_extSD).

1. Where x can be 2 or 3.

I2Sx can operate in master mode. As a result:

The extended I2Ss (I2Sx_ext) can be used only in full duplex mode. The I2Sx_ext operate always in slave mode.

Both I2Sx and I2Sx_ext can be configured as transmitters or receivers.

28.4.3 Supported audio protocols

The four-line bus has to handle only audio data generally time-multiplexed on two channels: the right channel and the left channel. However there is only one 16-bit register for the transmission and the reception. So, it is up to the software to write into the data register the adequate value corresponding to the considered channel side, or to read the data from the data register and to identify the corresponding channel by checking the CHSIDE bit in the SPI_SR register. Channel Left is always sent first followed by the channel right (CHSIDE has no meaning for the PCM protocol).

Four data and packet frames are available. Data may be sent with a format of:

When using 16-bit data extended on 32-bit packet, the first 16 bits (MSB) are the significant bits, the 16-bit LSB is forced to 0 without any need for software action or DMA request (only one read/write operation).

The 24-bit and 32-bit data frames need two CPU read or write operations to/from the SPI_DR or two DMA operations if the DMA is preferred for the application. For 24-bit data frame specifically, the 8 nonsignificant bits are extended to 32 bits with 0-bits (by hardware).

For all data formats and communication standards, the most significant bit is always sent first (MSB first).

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

I2S Philips standard

For this standard, the WS signal is used to indicate which channel is being transmitted. It is activated one CK clock cycle before the first bit (MSB) is available.

Figure 264. I2S Philips protocol waveforms (16/32-bit full accuracy, CPOL = 0) Figure 264. I2S Philips protocol waveforms (16/32-bit full accuracy, CPOL = 0). Timing diagram showing CK, WS, and SD signals. CK is the clock signal. WS switches between Channel left and Channel right with transmission and reception markers. SD indicates data transmitted/received starting with MSB and ending with LSB, noting data length 'Can be 16-bit or 32-bit'. Reference MS19591V1 at bottom right.
Figure 264. I2S Philips protocol waveforms (16/32-bit full accuracy, CPOL = 0). Timing diagram showing CK, WS, and SD signals. CK is the clock signal. WS switches between Channel left and Channel right with transmission and reception markers. SD indicates data transmitted/received starting with MSB and ending with LSB, noting data length 'Can be 16-bit or 32-bit'. Reference MS19591V1 at bottom right.

Data are latched on the falling edge of CK (for the transmitter) and are read on the rising edge (for the receiver). The WS signal is also latched on the falling edge of CK.

Figure 265. I2S Philips standard waveforms (24-bit frame with CPOL = 0) Figure 265. I2S Philips standard waveforms (24-bit frame with CPOL = 0). Timing diagram with CK, WS, and SD lines. Shows 24-bit data transmission/reception followed by 8-bit remaining 0 forced, forming 'Channel left 32-bit' before transitioning to 'Channel right'. Reference MS19592V1 at bottom right.
Figure 265. I2S Philips standard waveforms (24-bit frame with CPOL = 0). Timing diagram with CK, WS, and SD lines. Shows 24-bit data transmission/reception followed by 8-bit remaining 0 forced, forming 'Channel left 32-bit' before transitioning to 'Channel right'. Reference MS19592V1 at bottom right.

This mode needs two write or read operations to/from the SPI_DR.

Figure 266. Transmitting 0x8EAA33 Figure 266. Transmitting 0x8EAA33. Block diagram showing 'First write to Data register' as '0x8EAA' and 'Second write to Data register' as '0x33XX', with text 'Only the 8 MSB are sent to compare the 24 bits 8 LSBs have no meaning and can be anything'. Reference MS19593V1 at bottom right.
Figure 266. Transmitting 0x8EAA33. Block diagram showing 'First write to Data register' as '0x8EAA' and 'Second write to Data register' as '0x33XX', with text 'Only the 8 MSB are sent to compare the 24 bits 8 LSBs have no meaning and can be anything'. Reference MS19593V1 at bottom right.

Figure 267. Receiving 0x8EAA33

Diagram of Figure 267 showing two reads to the Data register. The first read returns 0x8EAA. The second read returns 0x33XX, with a note stating that only the 8 MSB are sent to compare the 24 bits and the 8 LSBs have no meaning and can be anything.

First read to Data register

0x8EAA

Second read to Data register

0x33XX

Only the 8 MSB are sent to compare the 24 bits
8 LSBs have no meaning and can be anything

MS19594V1

Diagram of Figure 267 showing two reads to the Data register. The first read returns 0x8EAA. The second read returns 0x33XX, with a note stating that only the 8 MSB are sent to compare the 24 bits and the 8 LSBs have no meaning and can be anything.
Figure 268. I2 S Philips standard (16-bit extended to 32-bit packet frame with CPOL = 0) Timing diagram for Figure 268 showing CK, WS, and SD signals. It illustrates a 32-bit channel frame divided into a 16-bit data transmission phase and a 16-bit remaining 0 forced reception phase. The SD signal shows the MSB and LSB of the data.

CK

WS

SD

Transmission

Reception

16-bit data

16-bit remaining 0 forced

MSB

LSB

Channel left 32-bit

Channel right

MS19599V1

Timing diagram for Figure 268 showing CK, WS, and SD signals. It illustrates a 32-bit channel frame divided into a 16-bit data transmission phase and a 16-bit remaining 0 forced reception phase. The SD signal shows the MSB and LSB of the data.

When 16-bit data frame extended to 32-bit channel frame is selected during the I2S configuration phase, only one access to SPI_DR is required. The 16 remaining bits are forced by hardware to 0x0000 to extend the data to 32-bit format.

If the data to transmit or the received data are 0x76A3 (0x76A30000 extended to 32-bit), the operation shown in Figure 269 is required.

Figure 269. Example

Diagram of Figure 269 showing a single access to the SPIx_DR register containing the value 0x76A3.

Only one access to SPIx_DR

0x76A3

MS19595V1

Diagram of Figure 269 showing a single access to the SPIx_DR register containing the value 0x76A3.

For transmission, each time an MSB is written to SPI_DR, the TXE flag is set and its interrupt, if allowed, is generated to load SPI_DR with the new value to send. This takes place even if 0x0000 have not yet been sent because it is done by hardware.

For reception, the RXNE flag is set and its interrupt, if allowed, is generated when the first 16 MSB half-word is received.

In this way, more time is provided between two write or read operations, which prevents underrun or overrun conditions (depending on the direction of the data transfer).

MSB justified standard

For this standard, the WS signal is generated at the same time as the first data bit, which is the MSBit.

Figure 270. MSB justified 16-bit or 32-bit full-accuracy length with CPOL = 0

Timing diagram: Figure 270. MSB justified 16-bit or 32-bit full-accuracy length with CPOL = 0. Shows CK, WS (Transmission, Reception), and SD (MSB, 16- or 32 bit data, LSB, MSB) across Channel left and Channel right.

MS30100 V1

Timing diagram: Figure 270. MSB justified 16-bit or 32-bit full-accuracy length with CPOL = 0. Shows CK, WS (Transmission, Reception), and SD (MSB, 16- or 32 bit data, LSB, MSB) across Channel left and Channel right.

Data are latched on the falling edge of CK (for transmitter) and are read on the rising edge (for the receiver).

Figure 271. MSB justified 24-bit frame length with CPOL = 0

Timing diagram: Figure 271. MSB justified 24-bit frame length with CPOL = 0. Shows CK, WS (Transmission, Reception), and SD with 24 bit data (MSB to LSB) and 8-bit remaining 0 forced for Channel left 32-bit, followed by Channel right.

MS30101V1

Timing diagram: Figure 271. MSB justified 24-bit frame length with CPOL = 0. Shows CK, WS (Transmission, Reception), and SD with 24 bit data (MSB to LSB) and 8-bit remaining 0 forced for Channel left 32-bit, followed by Channel right.

Figure 272. MSB justified 16-bit extended to 32-bit packet frame with CPOL = 0

Timing diagram: Figure 272. MSB justified 16-bit extended to 32-bit packet frame with CPOL = 0. Shows CK, WS (Transmission, Reception), and SD with 16-bit data (MSB to LSB) and 16-bit remaining 0 forced for Channel left 32-bit, followed by Channel right.

MS30102V1

Timing diagram: Figure 272. MSB justified 16-bit extended to 32-bit packet frame with CPOL = 0. Shows CK, WS (Transmission, Reception), and SD with 16-bit data (MSB to LSB) and 16-bit remaining 0 forced for Channel left 32-bit, followed by Channel right.

LSB justified standard

This standard is similar to the MSB justified standard (no difference for the 16-bit and 32-bit full-accuracy frame formats).

Figure 273. LSB justified 16-bit or 32-bit full-accuracy with CPOL = 0

Timing diagram for LSB justified 16-bit or 32-bit full-accuracy with CPOL = 0 showing CK, WS, SD with transmission/reception labeled and Channel left / Channel right segments. Ref: MS30103V1.
Timing diagram for LSB justified 16-bit or 32-bit full-accuracy with CPOL = 0 showing CK, WS, SD with transmission/reception labeled and Channel left / Channel right segments. Ref: MS30103V1.

Figure 274. LSB justified 24-bit frame length with CPOL = 0

Timing diagram for LSB justified 24-bit frame length with CPOL = 0 showing CK, WS, SD with 8-bit data '0 forced', 24-bit remaining transmission, reception, Channel left 32-bit, and Channel right. Ref: MS30104V1.
Timing diagram for LSB justified 24-bit frame length with CPOL = 0 showing CK, WS, SD with 8-bit data '0 forced', 24-bit remaining transmission, reception, Channel left 32-bit, and Channel right. Ref: MS30104V1.

Figure 275. Operations required to transmit 0x3478AE

Operations required to transmit 0x3478AE: First write to Data register conditioned by TXE=1 (0xXX34, only 8 LSB of the half-word are significant, field of 0x00 is forced instead of 8 MSBs); Second write to Data register conditioned by TXE=1 (0x78AE). Ref: MS19596V1.
Operations required to transmit 0x3478AE: First write to Data register conditioned by TXE=1 (0xXX34, only 8 LSB of the half-word are significant, field of 0x00 is forced instead of 8 MSBs); Second write to Data register conditioned by TXE=1 (0x78AE). Ref: MS19596V1.
Figure 276. Operations required to receive 0x3478AE Operations required to receive 0x3478AE: First read from Data register conditioned by RXNE=1 gives 0xXX34 (Only the 8 LSB of the half-word are significant. A field of 0x00 is forced instead of the 8 MSBs). Second read from Data register conditioned by RXNE=1 gives 0x78AE.

First read from Data register
conditioned by RXNE=1

0xXX34

Only the 8 LSB of the
half-word are significant.
A field of 0x00 is forced
instead of the 8 MSBs.

Second read from Data register
conditioned by RXNE=1

0x78AE

MS19597V1

Operations required to receive 0x3478AE: First read from Data register conditioned by RXNE=1 gives 0xXX34 (Only the 8 LSB of the half-word are significant. A field of 0x00 is forced instead of the 8 MSBs). Second read from Data register conditioned by RXNE=1 gives 0x78AE.
Figure 277. LSB justified 16-bit extended to 32-bit packet frame with CPOL = 0 Timing diagram of LSB justified 16-bit extended to 32-bit packet frame with CPOL = 0 showing CK clock pulses, WS word select signal, SD serial data with 16-bit data 0 forced and 16-bit remaining data from MSB to LSB across Channel left 32-bit and Channel right, with Transmission and Reception markers.

CK

WS

SD

16-bit data
0 forced

Transmission

16-bit remaining

MSB

LSB

Reception

Channel left 32-bit

Channel right

MS30105V1

Timing diagram of LSB justified 16-bit extended to 32-bit packet frame with CPOL = 0 showing CK clock pulses, WS word select signal, SD serial data with 16-bit data 0 forced and 16-bit remaining data from MSB to LSB across Channel left 32-bit and Channel right, with Transmission and Reception markers.

When 16-bit data frame extended to 32-bit channel frame is selected during the I2S configuration phase, Only one access to SPI_DR is required. The 16 remaining bits are forced by hardware to 0x0000 to extend the data to 32-bit format. In this case it corresponds to the half-word MSB.

If the data to transmit or the received data are 0x76A3 (0x0000 76A3 extended to 32-bit), the operation shown in Figure 278 is required.

Figure 278. Example of LSB justified 16-bit extended to 32-bit packet frame Diagram showing 'Only one access to the SPIx-DR register' with block value 0x76A3.

Only one access to the SPIx-DR register

0x76A3

MS19598V1

Diagram showing 'Only one access to the SPIx-DR register' with block value 0x76A3.

In transmission mode, when TXE is asserted, the application has to write the data to be transmitted (in this case 0x76A3). The 0x000 field is transmitted first (extension on 32-bit). TXE is asserted again as soon as the effective data (0x76A3) is sent on SD.

In reception mode, RXNE is asserted as soon as the significant half-word is received (and not the 0x0000 field).

In this way, more time is provided between two write or read operations to prevent underrun or overrun conditions.

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.

Figure 279. PCM standard waveforms (16-bit)

Figure 279. PCM standard waveforms (16-bit): Timing diagram illustrating CK, WS (short frame and long frame), and SD data lines with 13-bits long frame assertion time and 16-bit data words (MSB to LSB). Labeled MS30106V1.
Figure 279. PCM standard waveforms (16-bit): Timing diagram illustrating CK, WS (short frame and long frame), and SD data lines with 13-bits long frame assertion time and 16-bit data words (MSB to LSB). Labeled MS30106V1.

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

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

Figure 280. PCM standard waveforms (16-bit extended to 32-bit packet frame)

Figure 280. PCM standard waveforms (16-bit extended to 32-bit packet frame): Timing diagram depicting CK, WS (short frame and long frame up to 13-bits), and SD data lines with 16 bits active payload within 32-bit packet frame. Labeled MS30107V1.
Figure 280. PCM standard waveforms (16-bit extended to 32-bit packet frame): Timing diagram depicting CK, WS (short frame and long frame up to 13-bits), and SD data lines with 16 bits active payload within 32-bit packet frame. Labeled MS30107V1.

Note: For both modes (master and slave) and for both synchronizations (short and long), the number of bits between two consecutive pieces of data (and so two synchronization signals) needs to be specified (DATLEN and CHLEN bits in the SPI_I2SCFGR register) even in slave mode.

28.4.4 Clock generator

The I2S bitrate determines the dataflow on the I2S data line and the I2S clock signal frequency.

I2S bitrate = number of bits per channel × number of channels × sampling audio frequency

For a 16-bit audio, left and right channel, the I2S bitrate is calculated as follows:

I2S bitrate = 16 × 2 × FS

It is I2S bitrate = 32 x 2 x FS if the packet length is 32-bit wide.

Figure 281. Audio sampling frequency definition

Figure 281: Audio sampling frequency definition diagram. It shows two overlapping waveforms representing 16- or 32-bit left and right channels. A horizontal double-headed arrow labeled 'Fs' spans the duration of one frame, which is 32- or 64-bits long. Sampling points are marked at the beginning of each frame.

MS30108V1

Figure 281: Audio sampling frequency definition diagram. It shows two overlapping waveforms representing 16- or 32-bit left and right channels. A horizontal double-headed arrow labeled 'Fs' spans the duration of one frame, which is 32- or 64-bits long. Sampling points are marked at the beginning of each frame.

When the master mode is configured, a specific action needs to be taken to properly program the linear divider in order to communicate with the desired audio frequency.

Figure 282. I2 S clock generator architecture Figure 282: I2S clock generator architecture block diagram. The I2SxCLK input goes into an 8-bit linear divider + reshaping stage. The output of this stage goes to a Divider by 4, then a Div2 block, and then a multiplexer (0/1) controlled by MCKOE. The output of this multiplexer goes to another multiplexer (0/1) controlled by CHLEN. The final output is CK. The I2SDIV[7:0] register provides the divider value, and MCKOE and CHLEN are control signals.

MS30109V1

Figure 282: I2S clock generator architecture block diagram. The I2SxCLK input goes into an 8-bit linear divider + reshaping stage. The output of this stage goes to a Divider by 4, then a Div2 block, and then a multiplexer (0/1) controlled by MCKOE. The output of this multiplexer goes to another multiplexer (0/1) controlled by CHLEN. The final output is CK. The I2SDIV[7:0] register provides the divider value, and MCKOE and CHLEN are control signals.

1. Where x could be 2 or 3.

Figure 281 presents the communication clock architecture. To achieve high-quality audio performance, the I2SxCLK clock source can be either the PLLI2S output (through R division factor) or an external clock (mapped to I2S_CKIN pin).

The audio sampling frequency can be 192 kHz, 96 kHz, or 48 kHz. In order to reach the desired frequency, the linear divider needs to be programmed according to the formulas below:

When the master clock is generated (MCKOE in the SPI_I2SPR register is set):

\[F_S = I2SxCLK / [(16*2)*((2*I2SDIV)+ODD)*8] \text{ when the channel frame is 16-bit wide}\]

\[F_S = I2SxCLK / [(32*2)*((2*I2SDIV)+ODD)*4] \text{ when the channel frame is 32-bit wide}\]

When the master clock is disabled (MCKOE bit cleared):

\[F_S = I2SxCLK / [(16*2)*((2*I2SDIV)+ODD)] \text{ when the channel frame is 16-bit wide}\]

\[F_S = I2SxCLK / [(32*2)*((2*I2SDIV)+ODD)] \text{ when the channel frame is 32-bit wide}\]

Table 127 provides example precision values for different clock configurations.

Note: Other configurations are possible that allow optimum clock precision.

Table 127. Audio frequency precision (for PLLM VCO = 1 MHz or 2 MHz)(1)
Master clockTarget fS (Hz)Data formatPLLI2SNPLLI2SRI2SDIVI2SODDReal fS (Hz)Error
Disabled800016-bit1922187180000.0000%
32-bit192362180000.0000%
1600016-bit1923621160000.0000%
32-bit2562621160000.0000%
3200016-bit2562621320000.0000%
32-bit2565121320000.0000%
4800016-bit1925121480000.0000%
32-bit3845121480000.0000%
9600016-bit3845121960000.0000%
32-bit424311196014.492190.0151%
2205016-bit290368122049.876950.0006%
32-bit302253122050.234380.0011%
4410016-bit302253144100.468750.0011%
32-bit429419044099.507810.0011%
19200016-bit4243111192028.98440.0151%
32-bit258331191964.28130.0186%
Enabled8000don't care256512180000.0000%
16000don't care213213016000.600590.0038%
32000don't care21326132001.201170.0038%
48000don't care25833147991.070310.0186%
96000don't care34423195982.140630.0186%
22050don't care42949122049.753910.0011%
44100don't care27126044108.074220.0183%

1. This table gives only example values for different clock configurations. Other configurations allowing optimum clock precision are possible.

28.4.5 I2S master mode

The I2S can be configured as follows:

This means that the serial clock is generated on the CK pin as well as the Word Select signal WS. Master clock (MCK) may be output or not, thanks to the MCKOE bit in the SPI_I2SPR register.

Procedure

  1. 1. Select the I2SDIV[7:0] bits in the SPI_I2SPR register to define the serial clock baud rate to reach the proper audio sample frequency. The ODD bit in the SPI_I2SPR register also has to be defined.
  2. 2. Select the CKPOL bit to define the steady level for the communication clock. Set the MCKOE bit in the SPI_I2SPR register if the master clock MCK needs to be provided to the external DAC/ADC audio component (the I2SDIV and ODD values should be computed depending on the state of the MCK output, for more details refer to Section 28.4.4: Clock generator).
  3. 3. Set the I2SMOD bit in SPI_I2SCFGR to activate the I2S functionalities and choose the I2S standard through the I2SSTD[1:0] and PCMSYNC bits, the data length through the DATLEN[1:0] bits and the number of bits per channel by configuring the CHLEN bit. Select also the I2S master mode and direction (Transmitter or Receiver) through the I2SCFG[1:0] bits in the SPI_I2SCFGR register.
  4. 4. If needed, select all the potential interruption sources and the DMA capabilities by writing the SPI_CR2 register.
  5. 5. The I2SE bit in SPI_I2SCFGR register must be set.

WS and CK are configured in output mode. MCK is also an output, if the MCKOE bit in SPI_I2SPR is set.

Transmission sequence

The transmission sequence begins when a half-word is written into the Tx buffer.

Assumedly, the first data written into the Tx buffer correspond to the channel Left data. When data are transferred from the Tx buffer to the shift register, TXE is set and data corresponding to the channel Right have to be written into the Tx buffer. The CHSIDE flag indicates which channel is to be transmitted. It has a meaning when the TXE flag is set because the CHSIDE flag is updated when TXE goes high.

A full frame has to be considered as a Left channel data transmission followed by a Right channel data transmission. It is not possible to have a partial frame where only the left channel is sent.

The data half-word is parallel loaded into the 16-bit shift register during the first bit transmission, and then shifted out, serially, to the MOSI/SD pin, MSB first. The TXE flag is set after each transfer from the Tx buffer to the shift register and an interrupt is generated if the TXEIE bit in the SPI_CR2 register is set.

For more details about the write operations depending on the I2S standard mode selected, refer to Section 28.4.3: Supported audio protocols.

To ensure a continuous audio data transmission, it is mandatory to write the SPI_DR with the next data to transmit before the end of the current transmission.

To switch off the I2S, by clearing I2SE, it is mandatory to wait for TXE = 1 and BSY = 0.

Reception sequence

The operating mode is the same as for the transmission mode except for the point 3 (refer to the procedure described in Section 28.4.5), where the configuration should set the master reception mode through the I2SCFG[1:0] bits.

Whatever the data or channel length, the audio data are received by 16-bit packets. This means that each time the Rx buffer is full, the RXNE flag is set and an interrupt is generated

if the RXNEIE bit is set in SPI_CR2 register. Depending on the data and channel length configuration, the audio value received for a right or left channel may result from one or two receptions into the Rx buffer.

Clearing the RXNE bit is performed by reading the SPI_DR register.

CHSIDE is updated after each reception. It is sensitive to the WS signal generated by the I2S cell.

For more details about the read operations depending on the I2S standard mode selected, refer to Section 28.4.3: Supported audio protocols.

If data are received while the previously received data have not been read yet, an overrun is generated and the OVR flag is set. If the ERRIE bit is set in the SPI_CR2 register, an interrupt is generated to indicate the error.

To switch off the I2S, specific actions are required to ensure that the I2S completes the transfer cycle properly without initiating a new data transfer. The sequence depends on the configuration of the data and channel lengths, and on the audio protocol mode selected. In the case of:

Note: The BSY flag is kept low during transfers.

28.4.6 I2S slave mode

The I2S can be configured as follows:

The operating mode is following mainly the same rules as described for the I2S master configuration. In slave mode, there is no clock to be generated by the I2S interface. The clock and WS signals are input from the external master connected to the I2S interface. There is then no need, for the user, to configure the clock.

The configuration steps to follow are listed below:

  1. 1. Set the I2SMOD bit in the SPI_I2SCFGR register to reach the I2S functionalities and choose the I2S standard through the I2SSTD[1:0] bits, the data length through the DATLEN[1:0] bits and the number of bits per channel for the frame configuring the CHLEN bit. Select also the mode (transmission or reception) for the slave through the I2SCFG[1:0] bits in SPI_I2SCFGR register.
  2. 2. If needed, select all the potential interrupt sources and the DMA capabilities by writing the SPI_CR2 register.
  3. 3. The I2SE bit in SPI_I2SCFGR register must be set.

Transmission sequence

The transmission sequence begins when the external master device sends the clock and when the NSS_WS signal requests the transfer of data. The slave has to be enabled before the external master starts the communication. The I2S data register has to be loaded before the master initiates the communication.

For the I2S, MSB justified and LSB justified modes, the first data item to be written into the data register corresponds to the data for the left channel. When the communication starts, the data are transferred from the Tx buffer to the shift register. The TXE flag is then set in order to request the right channel data to be written into the I2S data register.

The CHSIDE flag indicates which channel is to be transmitted. Compared to the master transmission mode, in slave mode, CHSIDE is sensitive to the WS signal coming from the external master. This means that the slave needs to be ready to transmit the first data before the clock is generated by the master. WS assertion corresponds to left channel transmitted first.

Note: The I2SE has to be written at least two PCLK cycles before the first clock of the master comes on the CK line.

The data half-word is parallel-loaded into the 16-bit shift register (from the internal bus) during the first bit transmission, and then shifted out serially to the MOSI/SD pin MSB first. The TXE flag is set after each transfer from the Tx buffer to the shift register and an interrupt is generated if the TXEIE bit in the SPI_CR2 register is set.

Note that the TXE flag should be checked to be at 1 before attempting to write the Tx buffer.

For more details about the write operations depending on the I2S standard mode selected, refer to Section 28.4.3.

To secure a continuous audio data transmission, it is mandatory to write the SPI_DR register with the next data to transmit before the end of the current transmission. An underrun flag is set and an interrupt may be generated if the data are not written into the SPI_DR register before the first clock edge of the next data communication. This indicates to the software that the transferred data are wrong. If the ERRIE bit is set into the SPI_CR2 register, an interrupt is generated when the UDR flag in the SPI_SR register goes high. In this case, it is mandatory to switch off the I2S and to restart a data transfer starting from the left channel.

To switch off the I2S, by clearing the I2SE bit, it is mandatory to wait for TXE = 1 and BSY = 0.

Reception sequence

The operating mode is the same as for the transmission mode except for the point 1 (refer to the procedure described in Section 28.4.6), where the configuration should set the master reception mode using the I2SCFG[1:0] bits in the SPI_I2SCFGR register.

Whatever the data length or the channel length, the audio data are received by 16-bit packets. This means that each time the RX buffer is full, the RXNE flag in the SPI_SR register is set and an interrupt is generated if the RXNEIE bit is set in the SPI_CR2 register. Depending on the data length and channel length configuration, the audio value received for a right or left channel may result from one or two receptions into the RX buffer.

The CHSIDE flag is updated each time data are received to be read from SPI_DR. It is sensitive to the external WS line managed by the external master component.

Clearing the RXNE bit is performed by reading the SPI_DR register.

For more details about the read operations depending the I2S standard mode selected, refer to Section 28.4.3.

If data are received while the precedent received data have not yet been read, an overrun is generated and the OVR flag is set. If the bit ERRIE is set in the SPI_CR2 register, an interrupt is generated to indicate the error.

To switch off the I2S in reception mode, I2SE has to be cleared immediately after receiving the last RXNE = 1.

Note: The external master components should have the capability of sending/receiving data in 16-bit or 32-bit packets via an audio channel.

28.4.7 Status flags

Three status flags are provided for the application to fully monitor the state of the I2S bus.

Busy flag (BSY)

The BSY flag is set and cleared by hardware (writing to this flag has no effect). It indicates the state of the communication layer of the I2S.

When BSY is set, it indicates that the I2S is busy communicating. There is one exception in master receive mode (I2SCFG = 11) where the BSY flag is kept low during reception.

The BSY flag is useful to detect the end of a transfer if the software needs to disable the I2S. This avoids corrupting the last transfer. For this, the procedure described below must be strictly respected.

The BSY flag is set when a transfer starts, except when the I2S is in master receiver mode.

The BSY flag is cleared:

When communication is continuous:

Note: Do not use the BSY flag to handle each data transmission or reception. It is better to use the TXE and RXNE flags instead.

Tx buffer empty flag (TXE)

When set, this flag indicates that the Tx buffer is empty and the next data to be transmitted can then be loaded into it. The TXE flag is reset when the Tx buffer already contains data to be transmitted. It is also reset when the I2S is disabled (I2SE bit is reset).

RX buffer not empty (RXNE)

When set, this flag indicates that there are valid received data in the RX Buffer. It is reset when SPI_DR register is read.

Channel Side flag (CHSIDE)

In transmission mode, this flag is refreshed when TXE goes high. It indicates the channel side to which the data to transfer on SD has to belong. In case of an underrun error event in slave transmission mode, this flag is not reliable and I2S needs to be switched off and switched on before resuming the communication.

In reception mode, this flag is refreshed when data are received into SPI_DR. It indicates from which channel side data have been received. Note that in case of error (like OVR) this flag becomes meaningless and the I2S should be reset by disabling and then enabling it (with configuration if it needs changing).

This flag has no meaning in the PCM standard (for both Short and Long frame modes).

When the OVR or UDR flag in the SPI_SR is set and the ERRIE bit in SPI_CR2 is also set, an interrupt is generated. This interrupt can be cleared by reading the SPI_SR status register (once the interrupt source has been cleared).

28.4.8 Error flags

There are three error flags for the I2S cell.

Underrun flag (UDR)

In slave transmission mode this flag is set when the first clock for data transmission appears while the software has not yet loaded any value into SPI_DR. It is available when the I2SMOD bit in SPI_I2SCFGR is set. An interrupt may be generated if the ERRIE bit in SPI_CR2 is set.

The UDR bit is cleared by a read operation on the SPI_SR register.

Overrun flag (OVR)

This flag is set when data are received and the previous data have not yet been read from SPI_DR. As a result, the incoming data are lost. An interrupt may be generated if the ERRIE bit is set in SPI_CR2.

In this case, the receive buffer contents are not updated with the newly received data from the transmitter device. A read operation to the SPI_DR register returns the previous correctly received data. All other subsequently transmitted half-words are lost.

Clearing the OVR bit is done by a read operation on the SPI_DR register followed by a read access to the SPI_SR register.

Frame error flag (FRE)

This flag can be set by hardware only if the I2S is configured in Slave mode. It is set if the external master is changing the WS line at a moment when the slave is not expected this

change. If the synchronization is lost, to recover from this state and resynchronize the external master device with the I2S slave device, follow the steps below:

  1. 1. Disable the I2S
  2. 2. Re-enable it when the correct level is detected on the WS line (WS line is high in I2S mode, or low for MSB- or LSB-justified or PCM modes).

Desynchronization between the master and slave device may be due to noisy environment on the SCK communication clock or on the WS frame synchronization line. An error interrupt can be generated if the ERRIE bit is set. The desynchronization flag (FRE) is cleared by software when the status register is read.

28.4.9 I2S interrupts

Table 128 provides the list of I2S interrupts.

Table 128. I2S interrupt requests

Interrupt eventEvent flagEnable Control bit
Transmit buffer empty flagTXETXEIE
Receive buffer not empty flagRXNERXNEIE
Overrun errorOVRERRIE
Underrun errorUDR
Frame error flagFREERRIE

28.4.10 DMA features

DMA is working in exactly the same way as for the SPI mode. There is no difference on the I2S. Only the CRC feature is not available in I2S mode since there is no data transfer protection system.

28.5 SPI and I2S registers

The peripheral registers must be accessed by half-words (16 bits) or words (32 bits).

28.5.1 SPI control register 1 (SPI_CR1) (not used in I2S mode)

Address offset: 0x00

Reset value: 0x0000

1514131211109876543210
BIDI
MODE
BIDI
OE
CRC
EN
CRC
NEXT
DFFRX
ONLY
SSMSSILSB
FIRST
SPEBR [2:0]MSTRCPOLCPHA
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

Bit 15 BIDIMODE: Bidirectional data mode enable

0: 2-line unidirectional data mode selected

1: 1-line bidirectional data mode selected

Note: This bit is not used in I2S mode

Bit 14 BIDIOE: Output enable in bidirectional mode

This bit combined with the BIDImode bit selects the direction of transfer in bidirectional mode

0: Output disabled (receive-only mode)

1: Output enabled (transmit-only mode)

Note: This bit is not used in I2S mode.

In master mode, the MOSI pin is used while the MISO pin is used in slave mode.

Bit 13 CRCEN: Hardware CRC calculation enable

0: CRC calculation disabled

1: CRC calculation enabled

Note: This bit should be written only when SPI is disabled (SPE = '0') for correct operation.

It is not used in I2S mode.

Bit 12 CRCNEXT: CRC transfer next

0: Data phase (no CRC phase)

1: Next transfer is CRC (CRC phase)

Note: When the SPI is configured in full duplex or transmitter only modes, CRCNEXT must be written as soon as the last data is written to the SPI_DR register.

When the SPI is configured in receiver only mode, CRCNEXT must be set after the second last data reception.

This bit should be kept cleared when the transfers are managed by DMA.

It is not used in I2S mode.

Bit 11 DFF: Data frame format

0: 8-bit data frame format is selected for transmission/reception

1: 16-bit data frame format is selected for transmission/reception

Note: This bit should be written only when SPI is disabled (SPE = '0') for correct operation.

It is not used in I2S mode.

Bit 10 RXONLY: Receive only

This bit combined with the BIDImode bit selects the direction of transfer in 2-line unidirectional mode. This bit is also useful in a multislave system in which this particular slave is not accessed, the output from the accessed slave is not corrupted.

0: Full duplex (Transmit and receive)

1: Output disabled (Receive-only mode)

Note: This bit is not used in I2S mode

Bit 9 SSM: Software slave management

When the SSM bit is set, the NSS pin input is replaced with the value from the SSI bit.

0: Software slave management disabled

1: Software slave management enabled

Note: This bit is not used in I2S mode and SPI TI mode

Bit 8 SSI: Internal slave select

This bit has an effect only when the SSM bit is set. The value of this bit is forced onto the NSS pin and the IO value of the NSS pin is ignored.

Note: This bit is not used in I2S mode and SPI TI mode

Bit 7 LSBFIRST: Frame format

0: MSB transmitted first

1: LSB transmitted first

Note: This bit should not be changed when communication is ongoing.

It is not used in I2S mode and SPI TI mode

Bit 6 SPE: SPI enable

0: Peripheral disabled

1: Peripheral enabled

Note: This bit is not used in I2S mode.

When disabling the SPI, follow the procedure described in Section 28.3.8.

Bits 5:3 BR[2:0]: Baud rate control

000: \(f_{PCLK}/2\)

001: \(f_{PCLK}/4\)

010: \(f_{PCLK}/8\)

011: \(f_{PCLK}/16\)

100: \(f_{PCLK}/32\)

101: \(f_{PCLK}/64\)

110: \(f_{PCLK}/128\)

111: \(f_{PCLK}/256\)

Note: These bits should not be changed when communication is ongoing.

They are not used in I2S mode.

Bit 2 MSTR: Master selection

0: Slave configuration

1: Master configuration

Note: This bit should not be changed when communication is ongoing.

It is not used in I2S mode.

Bit1 CPOL: Clock polarity

0: CK to 0 when idle

1: CK to 1 when idle

Note: This bit should not be changed when communication is ongoing.
It is not used in I2S mode and SPI TI mode.

Bit 0 CPHA: Clock phase

0: The first clock transition is the first data capture edge

1: The second clock transition is the first data capture edge

Note: This bit should not be changed when communication is ongoing.
It is not used in I2S mode and SPI TI mode.

28.5.2 SPI control register 2 (SPI_CR2)

Address offset: 0x04

Reset value: 0x0000

1514131211109876543210
ReservedTXEIERXNEIEERRIEFRFRes.SSOETXDMAENRXDMAEN
rwrwrwrwrwrwrw

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

Bit 7 TXEIE: Tx buffer empty interrupt enable

0: TXE interrupt masked

1: TXE interrupt not masked. Used to generate an interrupt request when the TXE flag is set.

Bit 6 RXNEIE: RX buffer not empty interrupt enable

0: RXNE interrupt masked

1: RXNE interrupt not masked. Used to generate an interrupt request when the RXNE flag is set.

Bit 5 ERRIE: Error interrupt enable

This bit controls the generation of an interrupt when an error condition occurs )(CRCERR, OVR, MODF in SPI mode, FRE in TI mode and UDR, OVR, and FRE in I2S mode).

0: Error interrupt is masked

1: Error interrupt is enabled

Bit 4 FRF: Frame format

0: SPI Motorola mode

1 SPI TI mode

Note: This bit is not used in I2S mode.

Bit 3 Reserved. Forced to 0 by hardware.

Bit 2 SSOE: SS output enable

0: SS output is disabled in master mode and the cell can work in multimaster configuration

1: SS output is enabled in master mode and when the cell is enabled. The cell cannot work in a multimaster environment.

Note: This bit is not used in I2S mode and SPI TI mode.

Bit 1 TXDMAEN: Tx buffer DMA enable

When this bit is set, the DMA request is made whenever the TXE flag is set.

0: Tx buffer DMA disabled

1: Tx buffer DMA enabled

Bit 0 RXDMAEN: Rx buffer DMA enable

When this bit is set, the DMA request is made whenever the RXNE flag is set.

0: Rx buffer DMA disabled

1: Rx buffer DMA enabled

28.5.3 SPI status register (SPI_SR)

Address offset: 0x08

Reset value: 0x0002

1514131211109876543210
ReservedFREBSYOVRMODFCRC
ERR
UDRCHSIDETXERXNE
rrrrrc_w0rrrr

Bits 15:9 Reserved. Forced to 0 by hardware.

Bit 8 FRE: Frame format error

0: No frame format error

1: A frame format error occurred

This flag is set by hardware and cleared by software when the SPIx_SR register is read.

Note: This flag is used when the SPI operates in TI slave mode or I2S slave mode (refer to Section 28.3.10).

Bit 7 BSY: Busy flag

0: SPI (or I2S) not busy

1: SPI (or I2S) is busy in communication or Tx buffer is not empty

This flag is set and cleared by hardware.

Note: BSY flag must be used with caution: refer to Section 28.3.7 and Section 28.3.8.

Bit 6 OVR: Overrun flag

0: No overrun occurred

1: Overrun occurred

This flag is set by hardware and reset by a software sequence (see Section 28.3.10).

Bit 5 MODF: Mode fault

0: No mode fault occurred

1: Mode fault occurred

This flag is set by hardware and reset by a software sequence (see Section 28.3.10).

Note: This bit is not used in I2S mode

Bit 4 CRCERR: CRC error flag

0: CRC value received matches the SPI_RXCRCR value

1: CRC value received does not match the SPI_RXCRCR value

This flag is set by hardware and cleared by software writing 0.

Note: This bit is not used in I2S mode.

28.5.4 SPI data register (SPI_DR)

Address offset: 0x0C

Reset value: 0x0000

1514131211109876543210
DR[15:0]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

28.5.5 SPI CRC polynomial register (SPI_CRCPR) (not used in I2S mode)

Address offset: 0x10

Reset value: 0x0007

1514131211109876543210
CRCPOLY[15:0]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

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

This register contains the polynomial for the CRC calculation.

The CRC polynomial (0007h) is the reset value of this register. Another polynomial can be configured as required.

Note: These bits are not used for the I2S mode.

28.5.6 SPI RX CRC register (SPI_RXCRCR) (not used in I2S mode)

Address offset: 0x14

Reset value: 0x0000

1514131211109876543210
RXCRC[15:0]
rrrrrrrrrrrrrrrr

Bits 15:0 RXCRC[15:0]: Rx CRC register

When CRC calculation is enabled, the RxCRC[15:0] bits contain the computed CRC value of the subsequently received bytes. This register is reset when the CRCEN bit in SPI_CR1 register is written to 1. The CRC is calculated serially using the polynomial programmed in the SPI_CRCPR register.

Only the 8 LSB bits are considered when the data frame format is set to be 8-bit data (DFF bit of SPI_CR1 is cleared). CRC calculation is done based on any CRC8 standard.

The entire 16-bits of this register are considered when a 16-bit data frame format is selected (DFF bit of the SPI_CR1 register is set). CRC calculation is done based on any CRC16 standard.

Note: A read to this register when the BSY Flag is set could return an incorrect value. These bits are not used for I2S mode.

28.5.7 SPI TX CRC register (SPI_TXCRCR) (not used in I2S mode)

Address offset: 0x18

Reset value: 0x0000

1514131211109876543210
TXCRC[15:0]
rrrrrrrrrrrrrrrr

Bits 15:0 TXCRC[15:0]: Tx CRC register

When CRC calculation is enabled, the TxCRC[7:0] bits contain the computed CRC value of the subsequently transmitted bytes. This register is reset when the CRCEN bit of SPI_CR1 is written to 1. The CRC is calculated serially using the polynomial programmed in the SPI_CRCPR register.

Only the 8 LSB bits are considered when the data frame format is set to be 8-bit data (DFF bit of SPI_CR1 is cleared). CRC calculation is done based on any CRC8 standard.

The entire 16-bits of this register are considered when a 16-bit data frame format is selected (DFF bit of the SPI_CR1 register is set). CRC calculation is done based on any CRC16 standard.

Note: A read to this register when the BSY flag is set could return an incorrect value. These bits are not used for I2S mode.

28.5.8 SPI_I2S configuration register (SPI_I2SCFGR)

Address offset: 0x1C

Reset value: 0x0000

1514131211109876543210
ReservedI2SMODI2SEI2SCFGPCMSY
NC
Res.I2SSTDCKPOLDATLENCHLEN
rwrwrwrwrwrwrwrwrwrwrw

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

Bit 11 I2SMOD: I2S mode selection

0: SPI mode is selected

1: I2S mode is selected

Note: This bit should be configured when the SPI or I2S is disabled

Bit 10 I2SE: I2S Enable

0: I2S peripheral is disabled

1: I2S peripheral is enabled

Note: This bit is not used in SPI mode.

Bits 9:8 I2SCFG: I2S configuration mode

00: Slave - transmit

01: Slave - receive

10: Master - transmit

11: Master - receive

Note: This bit should be configured when the I2S is disabled.

It is not used in SPI mode.

Bit 7 PCMSYNC: PCM frame synchronization

0: Short frame synchronization

1: Long frame synchronization

Note: This bit has a meaning only if I2SSTD = 11 (PCM standard is used)

It is not used in SPI mode.

Bit 6 Reserved: forced at 0 by hardware

Bits 5:4 I2SSTD: I2S standard selection

00: I2S Philips standard.

01: MSB justified standard (left justified)

10: LSB justified standard (right justified)

11: PCM standard

For more details on I2S standards, refer to Section 28.4.3: Supported audio protocols. Not used in SPI mode.

Note: For correct operation, these bits should be configured when the I2S is disabled.

Bit 3 CKPOL: Steady state clock polarity

0: I2S clock steady state is low level

1: I2S clock steady state is high level

Note: For correct operation, this bit should be configured when the I2S is disabled.

This bit is not used in SPI mode

Bits 2:1 DATLEN: Data length to be transferred

00: 16-bit data length

01: 24-bit data length

10: 32-bit data length

11: Not allowed

Note: For correct operation, these bits should be configured when the I2S is disabled.

This bit is not used in SPI mode.

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

0: 16-bit wide

1: 32-bit wide

The bit write operation has a meaning only if DATLEN = 00 otherwise the channel length is fixed to 32-bit by hardware whatever the value filled in. Not used in SPI mode.

Note: For correct operation, this bit should be configured when the I2S is disabled.

28.5.9 SPI_I2S prescaler register (SPI_I2SPR)

Address offset: 0x20

Reset value: 0000 0010 (0x0002)

1514131211109876543210
ReservedMCKOEODDI2SDIV
rwrwrw

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

Bit 9 MCKOE: Master clock output enable

0: Master clock output is disabled

1: Master clock output is enabled

Note: This bit should be configured when the I2S is disabled. It is used only when the I2S is in master mode.

This bit is not used in SPI mode.

Bit 8 ODD: Odd factor for the prescaler

0: real divider value is = I2SDIV *2

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

Refer to Section 28.4.4: Clock generator. Not used in SPI mode.

Note: This bit should be configured when the I2S is disabled. It is used only when the I2S is in master mode.

Bits 7:0 I2SDIV: I2S Linear prescaler

I2SDIV [7:0] = 0 or I2SDIV [7:0] = 1 are forbidden values.

Refer to Section 28.4.4: Clock generator. Not used in SPI mode.

Note: These bits should be configured when the I2S is disabled. It is used only when the I2S is in master mode.

28.5.10 SPI register map

The table provides shows the SPI register map and reset values.

Table 129. SPI register map and reset values
OffsetRegister313029282726252423222120191817161514131211109876543210
0x00SPI_CR1ReservedBIDIMODEBIDIOECRCENCRCNEXTDFFRXONLYSSMSSILSBFIRSTSPEBR [2:0]MSTRCPOLCPHA
Reset value0000000000000000
0x04SPI_CR2ReservedTXEIERXNEIEERRIEFRFReservedSSOETXDMAENRXDMAEN
Reset value0000000
0x08SPI_SRReservedFREBSYOVRMODFCRCERRUDRCHSIDETXERXNE
Reset value000000010
0x0CSPI_DRReservedDR[15:0]
Reset value0000000000000000
0x10SPI_CRCPRReservedCRCPOLY[15:0]
Reset value0000000000000111
0x14SPI_RXCRCRReservedRxCRC[15:0]
Reset value0000000000000000
0x18SPI_TXCRCRReservedTxCRC[15:0]
Reset value0000000000000000
0x1CSPI_I2SCFGRReservedI2SMODI2SEI2SCFGPCMSYNCReservedI2SSTDCKPOLDATLENCHLEN
Reset value00000000000
0x20SPI_I2SPRReservedMCKOEODDI2SDIV
Reset value0000000010

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