20. Serial peripheral interface (SPI)

20.1 SPI introduction

The SPI interface provides two main functions, supporting the SPI or the I 2 S audio protocol. By default, the SPI function is selected. It is possible to switch the interface from SPI to I 2 S 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 I 2 S is also a synchronous serial communication interface. It can address four different audio standards including the I 2 S 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 I 2 S 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, please refer to Section 8.3.2: I/O pin multiplexer and mapping .


20.2 SPI and I 2 S main features

20.2.1 SPI features

20.2.2 I 2 S features

20.3 SPI functional description

20.3.1 General description

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

Figure 192. SPI block diagram. The diagram shows the internal architecture of an SPI peripheral. On the left, four pins are shown: MOSI, MISO, SCK, and NSS. The MOSI and MISO pins are connected to a 'Shift register' block. Data is written to the 'Tx buffer' and then shifted out through the MOSI pin. Data is shifted in through the MISO pin and stored in the 'Rx buffer'. The 'Address and data bus' is connected to both the 'Rx buffer' (via a 'Read' operation) and the 'Tx buffer' (via a 'Write' operation). The 'Shift register' is also connected to the 'Communication control' block and has a 'LSB first' input. The 'Communication control' block is connected to the 'Baud rate generator' (which receives 'BR[2:0]' from 'SPI_CR1'), the 'Master control logic', and the 'NSS' pin. The 'Master control logic' is connected to the 'Baud rate generator' and the 'Communication control' block. The 'SPI_CR1' and 'SPI_CR2' registers are shown as control registers. 'SPI_CR1' contains bits: LSB FIRST, SPE, BR2, BR1, BR0, MSTR, CPOL, CPHA, BIDI MODE, BIDI OE, CRCEN, CRC Next, DFF, RX ONLY, SSM, and SSI. 'SPI_CR2' contains bits: TXE IE, RXNE IE, ERR IE, 0, 0, SSOE, TXDM AEN, and RXDM AEN. The 'SPI_SR' register contains bits: BSY, OVR, MOD F, CRC ERR, 0, 0, TXE, and RXNE. The 'Communication control' block also has a multiplexer output labeled '0' and '1'.

Figure 192. SPI block diagram

Figure 192. SPI block diagram. The diagram shows the internal architecture of an SPI peripheral. On the left, four pins are shown: MOSI, MISO, SCK, and NSS. The MOSI and MISO pins are connected to a 'Shift register' block. Data is written to the 'Tx buffer' and then shifted out through the MOSI pin. Data is shifted in through the MISO pin and stored in the 'Rx buffer'. The 'Address and data bus' is connected to both the 'Rx buffer' (via a 'Read' operation) and the 'Tx buffer' (via a 'Write' operation). The 'Shift register' is also connected to the 'Communication control' block and has a 'LSB first' input. The 'Communication control' block is connected to the 'Baud rate generator' (which receives 'BR[2:0]' from 'SPI_CR1'), the 'Master control logic', and the 'NSS' pin. The 'Master control logic' is connected to the 'Baud rate generator' and the 'Communication control' block. The 'SPI_CR1' and 'SPI_CR2' registers are shown as control registers. 'SPI_CR1' contains bits: LSB FIRST, SPE, BR2, BR1, BR0, MSTR, CPOL, CPHA, BIDI MODE, BIDI OE, CRCEN, CRC Next, DFF, RX ONLY, SSM, and SSI. 'SPI_CR2' contains bits: TXE IE, RXNE IE, ERR IE, 0, 0, SSOE, TXDM AEN, and RXDM AEN. The 'SPI_SR' register contains bits: BSY, OVR, MOD F, CRC ERR, 0, 0, TXE, and RXNE. The 'Communication control' block also has a multiplexer output labeled '0' and '1'.

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 193 .

Figure 193. Single master/ single slave application

Diagram of a single master/single slave SPI application. A Master device and a Slave device are connected via four lines: MISO, MOSI, SCK, and NSS. The Master contains an 8-bit shift register (MSBit to LSBit) and an SPI clock generator. The Slave also contains an 8-bit shift register (MSBit to LSBit). The Master's MISO pin is connected to the Slave's MISO pin. The Master's MOSI pin is connected to the Slave's MOSI 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 is labeled ai14745.
Diagram of a single master/single slave SPI application. A Master device and a Slave device are connected via four lines: MISO, MOSI, SCK, and NSS. The Master contains an 8-bit shift register (MSBit to LSBit) and an SPI clock generator. The Slave also contains an 8-bit shift register (MSBit to LSBit). The Master's MISO pin is connected to the Slave's MISO pin. The Master's MOSI pin is connected to the Slave's MOSI 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 is labeled ai14745.
  1. 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.

The slave select information is driven internally by the value of the SSI bit in the SPI_CR1 register. The external NSS pin remains free for other application uses.

Two configurations are possible depending on the NSS output configuration (SSOE bit in register SPI_CR2).

This configuration is used only when the device operates in master mode. The NSS signal is driven low when the master starts the communication and is kept low until the SPI is disabled.

This configuration allows multimaster capability for devices operating in master mode. For devices set as slave, the NSS pin acts as a classical NSS input: the slave is selected when NSS is low and deselected when NSS high.

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 194 , 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 194. Data clock timing diagram

Figure 194. Data clock timing diagram. The diagram shows two sets of timing waveforms for SPI. The top set is for CPHA = 1, and the bottom set is for CPHA = 0. Each set shows the relationship between the clock (CPOL = 1 and CPOL = 0), MOSI, MISO, NSS (to slave), and Capture strobe signals. The MOSI and MISO signals are shown as a sequence of bits, with the MSB and LSB bits highlighted. The Capture strobe is shown as a series of pulses. The diagram is labeled ai17154d.

The figure illustrates the timing relationships for SPI data transfer. It is divided into two main sections based on the CPHA (Clock Phase) setting: CPHA = 1 (top) and CPHA = 0 (bottom). Each section shows the following signals over time:

The diagram is labeled ai17154d in the bottom right corner.

Figure 194. Data clock timing diagram. The diagram shows two sets of timing waveforms for SPI. The top set is for CPHA = 1, and the bottom set is for CPHA = 0. Each set shows the relationship between the clock (CPOL = 1 and CPOL = 0), MOSI, MISO, NSS (to slave), and Capture strobe signals. The MOSI and MISO signals are shown as a sequence of bits, with the MSB and LSB bits highlighted. The Capture strobe is shown as a series of pulses. The diagram is labeled 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.

20.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 194 ). 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 195: TI mode - Slave mode, single transfer and Figure 196: TI mode - Slave mode, continuous transfer), 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 195. TI mode - Slave mode, single transfer

Timing diagram for TI mode - Slave mode, single transfer. The diagram shows four signals over time: NSS input, SCK input, MOSI input, and MISO output. The NSS input is high before the first SCK edge and goes low after the last sampling edge. The SCK input is a periodic square wave. The MOSI input is labeled 'DONT CARE' before the first trigger edge, 'MSBIN' between the first and second sampling edges, 'LSBIN' between the third and fourth sampling edges, and 'DONT CARE' after the last sampling edge. The MISO output is labeled '1 or 0' before the first trigger edge, 'MSBOUT' between the first and second sampling edges, and 'LSBOUT' between the third and fourth sampling edges. The diagram also marks 'trigger edge' and 'sampling edge' for the SCK signal. A time interval 't_Release' is indicated between the last sampling edge and the MISO output going to high-impedance state. The diagram is labeled 'ai18434' in the bottom right corner.
Timing diagram for TI mode - Slave mode, single transfer. The diagram shows four signals over time: NSS input, SCK input, MOSI input, and MISO output. The NSS input is high before the first SCK edge and goes low after the last sampling edge. The SCK input is a periodic square wave. The MOSI input is labeled 'DONT CARE' before the first trigger edge, 'MSBIN' between the first and second sampling edges, 'LSBIN' between the third and fourth sampling edges, and 'DONT CARE' after the last sampling edge. The MISO output is labeled '1 or 0' before the first trigger edge, 'MSBOUT' between the first and second sampling edges, and 'LSBOUT' between the third and fourth sampling edges. The diagram also marks 'trigger edge' and 'sampling edge' for the SCK signal. A time interval 't_Release' is indicated between the last sampling edge and the MISO output going to high-impedance state. The diagram is labeled 'ai18434' in the bottom right corner.

Figure 196. TI mode - Slave mode, continuous transfer

Timing diagram for TI mode - Slave mode, continuous transfer. The diagram shows four signals: NSS input, SCK input, MOSI input, and MISO output. The NSS input is high during the transfer. The SCK input is a periodic clock signal. The MOSI input shows data frames (FRAME 1 and FRAME 2) being received. Each frame consists of a DONT CARE bit, followed by MSBIN and LSBIN bits. The MISO output shows data frames (FRAME 1 and FRAME 2) being transmitted. Each frame consists of a 1 or 0 bit, followed by MSBOUT and LSBOUT bits. Sampling points are indicated on the SCK signal.

The diagram illustrates the timing for TI mode - Slave mode, continuous transfer. It shows four signals: NSS input, SCK input, MOSI input, and MISO output. The NSS input is high during the transfer. The SCK input is a periodic clock signal. The MOSI input shows data frames (FRAME 1 and FRAME 2) being received. Each frame consists of a DONT CARE bit, followed by MSBIN and LSBIN bits. The MISO output shows data frames (FRAME 1 and FRAME 2) being transmitted. Each frame consists of a 1 or 0 bit, followed by MSBOUT and LSBOUT bits. Sampling points are indicated on the SCK signal.

Timing diagram for TI mode - Slave mode, continuous transfer. The diagram shows four signals: NSS input, SCK input, MOSI input, and MISO output. The NSS input is high during the transfer. The SCK input is a periodic clock signal. The MOSI input shows data frames (FRAME 1 and FRAME 2) being received. Each frame consists of a DONT CARE bit, followed by MSBIN and LSBIN bits. The MISO output shows data frames (FRAME 1 and FRAME 2) being transmitted. Each frame consists of a 1 or 0 bit, followed by MSBOUT and LSBOUT bits. Sampling points are indicated on the SCK signal.

20.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 194). 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 197: TI mode - master mode, single transfer and Figure 198: TI mode - master mode, continuous transfer show the SPI master communication waveforms when the TI mode is selected in master mode.

Figure 197. TI mode - master mode, single transfer

Timing diagram for SPI TI mode master mode single transfer. The diagram shows four waveforms: NSS output, SCK output, MOSI input, and MISO output. The NSS output is high before the first clock edge and goes low after the last clock edge. The SCK output is a square wave. The MOSI input shows DONT CARE, MSBIN, LSBIN, and DONT CARE segments. The MISO output shows 1 or 0, MSBOUT, and LSBOUT segments. Vertical dashed lines indicate trigger edges and sampling edges for the clock signal.

The diagram illustrates the timing for a single transfer in SPI TI mode master mode. It features four horizontal lines representing different signals over time:

Vertical dashed lines indicate the timing points: the first rising edge of SCK is the 'trigger edge', and subsequent rising edges are 'sampling trigger edges'. Falling edges of SCK are 'sampling edges'.

ai18436

Timing diagram for SPI TI mode master mode single transfer. The diagram shows four waveforms: NSS output, SCK output, MOSI input, and MISO output. The NSS output is high before the first clock edge and goes low after the last clock edge. The SCK output is a square wave. The MOSI input shows DONT CARE, MSBIN, LSBIN, and DONT CARE segments. The MISO output shows 1 or 0, MSBOUT, and LSBOUT segments. Vertical dashed lines indicate trigger edges and sampling edges for the clock signal.

Figure 198. TI mode - master mode, continuous transfer

Timing diagram for TI mode - master mode, continuous transfer. The diagram shows four signals over time: NSS (output), SCK (output), MOSI (output), and MISO (input). The SCK signal is a periodic square wave. The NSS signal is high before the first SCK edge and goes low at the first SCK rising edge, then goes high again after the last SCK edge. The MOSI signal is a continuous stream of data. The MISO signal is also a continuous stream of data. The diagram is divided into two frames, FRAME 1 and FRAME 2, by vertical dashed lines. Each frame contains two clock cycles. In each clock cycle, the MOSI signal is sampled at the rising edge of the SCK signal (labeled 'sampling trigger') and the MISO signal is driven at the falling edge of the SCK signal (labeled 'trigger'). The data is transferred in bytes. The first byte in each frame is split into two nibbles: the first nibble is labeled '1 or 0' or 'MSBIN' and the second nibble is labeled 'MSBOUT' or 'LSBIN'. The second byte in each frame is also split into two nibbles: the first nibble is labeled 'LSBOUT' or 'LSBIN' and the second nibble is labeled 'DONT CARE' or 'DONT CARE'. The diagram is labeled 'ai18437' in the bottom right corner.
Timing diagram for TI mode - master mode, continuous transfer. The diagram shows four signals over time: NSS (output), SCK (output), MOSI (output), and MISO (input). The SCK signal is a periodic square wave. The NSS signal is high before the first SCK edge and goes low at the first SCK rising edge, then goes high again after the last SCK edge. The MOSI signal is a continuous stream of data. The MISO signal is also a continuous stream of data. The diagram is divided into two frames, FRAME 1 and FRAME 2, by vertical dashed lines. Each frame contains two clock cycles. In each clock cycle, the MOSI signal is sampled at the rising edge of the SCK signal (labeled 'sampling trigger') and the MISO signal is driven at the falling edge of the SCK signal (labeled 'trigger'). The data is transferred in bytes. The first byte in each frame is split into two nibbles: the first nibble is labeled '1 or 0' or 'MSBIN' and the second nibble is labeled 'MSBOUT' or 'LSBIN'. The second byte in each frame is also split into two nibbles: the first nibble is labeled 'LSBOUT' or 'LSBIN' and the second nibble is labeled 'DONT CARE' or 'DONT CARE'. The diagram is labeled 'ai18437' in the bottom right corner.

20.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:

20.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

pin. The software must have written the data to be sent before the SPI master device initiates the transfer.

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 199 and Figure 200 ):

  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 199. TXE/RXNE/BSY behavior in Master / full-duplex mode (BIDIMODE=0 and RXONLY=0) in case of continuous transfers

Timing diagram showing SPI Master mode full-duplex continuous transfers. It displays SCK, MISO/MOSI (out), TXE flag, Tx buffer, BSY flag, MISO/MOSI (in), RXNE flag, and Rx buffer over time. Data items DATA1=0xF1, DATA2=0xF2, DATA3=0xF3 are transmitted, and DATA1=0xA1, DATA2=0xA2, DATA3=0xA3 are received. Software actions are shown at the bottom, such as writing 0xF1, waiting for TXE, writing 0xF2, waiting for RXNE, reading 0xA1, etc.

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

The diagram illustrates the timing and control signals for an SPI Master in full-duplex mode. The SCK signal is a continuous square wave. The MISO/MOSI (out) line transmits 8-bit data items: DATA1 = 0xF1, DATA2 = 0xF2, and DATA3 = 0xF3, each consisting of bits b0 through b7. The TXE flag is set by hardware when the Tx buffer is empty and cleared by software when a new data item is written. The Tx buffer (write SPI_DR) contains the data items 0xF1, 0xF2, and 0xF3. The BSY flag is set by hardware when the SPI is active and reset by hardware when it is inactive. The MISO/MOSI (in) line receives 8-bit data items: DATA1 = 0xA1, DATA2 = 0xA2, and DATA3 = 0xA3. The RXNE flag is set by hardware when the Rx buffer is not empty and cleared by software when the data is read. The Rx buffer (read SPI_DR) contains the data items 0xA1, 0xA2, and 0xA3. The software actions are as follows:

ai17343

Timing diagram showing SPI Master mode full-duplex continuous transfers. It displays SCK, MISO/MOSI (out), TXE flag, Tx buffer, BSY flag, MISO/MOSI (in), RXNE flag, and Rx buffer over time. Data items DATA1=0xF1, DATA2=0xF2, DATA3=0xF3 are transmitted, and DATA1=0xA1, DATA2=0xA2, DATA3=0xA3 are received. Software actions are shown at the bottom, such as writing 0xF1, waiting for TXE, writing 0xF2, waiting for RXNE, reading 0xA1, etc.

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

Timing diagram showing SPI signals (SCK, MISO/MOSI) and flags (TXE, BSY, RXNE) during continuous data transfers in Slave mode. The diagram illustrates the sequence of events for three data items (DATA 1 = 0xF1, DATA 2 = 0xF2, DATA 3 = 0xF3) being transmitted and received. The TXE flag is set by hardware and cleared by software. The BSY flag is set by hardware and reset by hardware. The RXNE flag is set by hardware and cleared by software. The Tx buffer and Rx buffer are shown with their respective data items. The software procedure is described in the text below the diagram.

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

The diagram illustrates the SPI signals and flags during continuous data transfers in Slave mode. The signals shown are SCK, MISO/MOSI (out), TXE flag, Tx buffer (write to SPI_DR), BSY flag, MISO/MOSI (in), RXNE flag, and Rx buffer (read from SPI_DR). The data items transferred are DATA 1 = 0xF1, DATA 2 = 0xF2, DATA 3 = 0xF3 for transmission and DATA 1 = 0xA1, DATA 2 = 0xA2, DATA 3 = 0xA3 for reception. The TXE flag is set by hardware and cleared by software. The BSY flag is set by hardware and reset by hardware. The RXNE flag is set by hardware and cleared by software. The software procedure is described in the text below the diagram.

software writes 0xF1 into SPI_DR

software waits until TXE=1 and writes 0xF2 into SPI_DR

software waits until RXNE=1 and reads 0xA1 from SPI_DR

software waits until TXE=1 and writes 0xF3 into SPI_DR

software waits until RXNE=1 and reads 0xA2 from SPI_DR

software waits until RXNE=1 and reads 0xA3 from SPI_DR

MSv76802V1

Timing diagram showing SPI signals (SCK, MISO/MOSI) and flags (TXE, BSY, RXNE) during continuous data transfers in Slave mode. The diagram illustrates the sequence of events for three data items (DATA 1 = 0xF1, DATA 2 = 0xF2, DATA 3 = 0xF3) being transmitted and received. The TXE flag is set by hardware and cleared by software. The BSY flag is set by hardware and reset by hardware. The RXNE flag is set by hardware and cleared by software. The Tx buffer and Rx buffer are shown with their respective data items. The software procedure is described in the text below the diagram.

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 201 and Figure 202 ).

  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 201. TXE/BSY behavior in Master transmit-only mode (BIDIMODE=0 and RXONLY=0) in case of continuous transfers

Timing diagram for Figure 201 showing SPI signals in Master transmit-only mode. SCK is the clock. MISO/MOSI (out) shows three data bytes: DATA 1 = 0xF1, DATA 2 = 0xF2, and DATA 3 = 0xF3, each with bits b0-b7. The TXE flag is set by hardware and cleared by software writes to the Tx buffer (SPI_DR). The BSY flag is set by hardware when transmission starts and reset by hardware when it finishes. Software actions include writing 0xF1, waiting for TXE=1 to write 0xF2, waiting for TXE=1 to write 0xF3, and finally waiting for BSY=0.

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

ai17345

Timing diagram for Figure 201 showing SPI signals in Master transmit-only mode. SCK is the clock. MISO/MOSI (out) shows three data bytes: DATA 1 = 0xF1, DATA 2 = 0xF2, and DATA 3 = 0xF3, each with bits b0-b7. The TXE flag is set by hardware and cleared by software writes to the Tx buffer (SPI_DR). The BSY flag is set by hardware when transmission starts and reset by hardware when it finishes. Software actions include writing 0xF1, waiting for TXE=1 to write 0xF2, waiting for TXE=1 to write 0xF3, and finally waiting for BSY=0.

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

Timing diagram for Figure 202 showing SPI signals in Slave transmit-only mode. Similar to Figure 201, it shows SCK, MISO/MOSI (out) with three data bytes (0xF1, 0xF2, 0xF3), TXE flag behavior, Tx buffer writes, and BSY flag behavior. The software flow is: write 0xF1, wait for TXE=1 to write 0xF2, wait for TXE=1 to write 0xF3, wait for TXE=1, and wait for BSY=0.

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

ai17346

Timing diagram for Figure 202 showing SPI signals in Slave transmit-only mode. Similar to Figure 201, it shows SCK, MISO/MOSI (out) with three data bytes (0xF1, 0xF2, 0xF3), TXE flag behavior, Tx buffer writes, and BSY flag behavior. The software flow is: write 0xF1, wait for TXE=1 to write 0xF2, wait for TXE=1 to write 0xF3, wait for TXE=1, and wait for BSY=0.

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 203 ):

  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 20.3.8 .

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

Timing diagram for SPI receive-only mode showing SCK, MISO/MOSI, RXNE flag, and Rx buffer behavior for three consecutive data transfers (0xA1, 0xA2, 0xA3).

The diagram shows the timing relationship between signals in SPI receive-only mode (CPOL=1, CPHA=1, RXONLY=1).
- SCK : Continuous clock signal.
- MISO/MOSI (in) : Shows three bytes of data: DATA 1 = 0xA1, DATA 2 = 0xA2, and DATA 3 = 0xA3. Each byte is composed of bits b0 through b7.
- RXNE flag : Goes high (set by hardware) after the 8th bit of each byte is received. It is cleared by software when the data is read.
- Rx buffer (read from SPI_DR) : Shows the values 0xA1, 0xA2, and 0xA3 becoming available in the buffer.
- Software actions : Annotations indicate 'software waits until RXNE=1 and reads [value] from SPI_DR' for each of the three data items.
Label: ai17347

Timing diagram for SPI receive-only mode showing SCK, MISO/MOSI, RXNE flag, and Rx buffer behavior for three consecutive data transfers (0xA1, 0xA2, 0xA3).

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 204 ).

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 202).

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

Timing diagram for SPI discontinuous transfers showing SCK, MOSI (out), TXE flag, Tx buffer (write to SPI_DR), and BSY flag. Three data bytes (0xF1, 0xF2, 0xF3) are transmitted. TXE goes high when the buffer is empty. BSY is high during active transmission. Software writes to SPI_DR are shown relative to TXE and BSY states.

Example with CPOL=1, CPHA=1

The diagram shows the relationship between the SPI clock (SCK), data output (MOSI), transmit empty flag (TXE), the transmit buffer, and the busy flag (BSY) during three separate byte transfers (0xF1, 0xF2, 0xF3). Annotations at the bottom describe software interactions: 'software writes 0xF1 into SPI_DR', 'software waits until TXE=1 but is late to write 0xF2 into SPI_DR', 'software waits until TXE=1 but is late to write 0xF3 into SPI_DR', 'software waits until TXE=1', and 'software waits until BSY=0'.

ai17348

Timing diagram for SPI discontinuous transfers showing SCK, MOSI (out), TXE flag, Tx buffer (write to SPI_DR), and BSY flag. Three data bytes (0xF1, 0xF2, 0xF3) are transmitted. TXE goes high when the buffer is empty. BSY is high during active transmission. Software writes to SPI_DR are shown relative to TXE and BSY states.

20.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.
  4. 4. Enable the SPI by setting the SPE bit in the SPI_CR1 register.
  5. 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.
  6. 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)

20.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.

20.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).

20.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 205 and Figure 206 ):

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 205. Transmission using DMA

Timing diagram for SPI transmission using DMA. It shows SCK, MISO/MOSI (out), TXE flag, BSY flag, DMA request, Tx buffer, and DMA TCIF flag over time. Data items DATA 1 = 0xF1, DATA 2 = 0xF2, and DATA 3 = 0xF3 are transmitted. The diagram includes annotations for flag states and software actions.

Example with CPOL=1, CPHA=1

The diagram illustrates the sequence of events for SPI transmission using DMA. The SCK line is a continuous square wave. The MISO/MOSI (out) line shows the transmission of three data items: DATA 1 = 0xF1, DATA 2 = 0xF2, and DATA 3 = 0xF3, each split into two bytes (b0-b7). The TXE flag is set by hardware when the Tx buffer is empty and cleared by a DMA write. The BSY flag is set by hardware when the SPI is active and reset by hardware when it is inactive. The DMA request is generated when the TXE flag is set and is ignored by the DMA if the transfer is already complete. The Tx buffer (write to SPI_DR) contains the data items 0xF1, 0xF2, and 0xF3. The DMA TCIF flag is set by hardware when the DMA transfer is complete and cleared by software. The sequence of events is: 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 SCK, MISO/MOSI (out), TXE flag, BSY flag, DMA request, Tx buffer, and DMA TCIF flag over time. Data items DATA 1 = 0xF1, DATA 2 = 0xF2, and DATA 3 = 0xF3 are transmitted. The diagram includes annotations for flag states and software actions.

Figure 206. Reception using DMA

Timing diagram for SPI reception using DMA. It shows SCK, MISO/MOSI (in), RXNE flag, DMA request, Rx buffer, DMA read from SPI_DR, and DMA TCIF flag over time. Data items DATA 1 = 0xA1, DATA 2 = 0xA2, and DATA 3 = 0xA3 are received. The diagram includes annotations for flag states and software actions.

Example with CPOL=1, CPHA=1

The diagram illustrates the sequence of events for SPI reception using DMA. The SCK line is a continuous square wave. The MISO/MOSI (in) line shows the reception of three data items: DATA 1 = 0xA1, DATA 2 = 0xA2, and DATA 3 = 0xA3, each split into two bytes (b0-b7). The RXNE flag is set by hardware when the Rx buffer is not empty and clear by a DMA read. The DMA request is generated when the RXNE flag is set. The Rx buffer (read from SPI_DR) contains the data items 0xA1, 0xA2, and 0xA3. The DMA TCIF flag is set by hardware when the DMA transfer is complete and cleared by software. The sequence of events is: 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 SCK, MISO/MOSI (in), RXNE flag, DMA request, Rx buffer, DMA read from SPI_DR, and DMA TCIF flag over time. Data items DATA 1 = 0xA1, DATA 2 = 0xA2, and DATA 3 = 0xA3 are received. The diagram includes annotations for flag states and software actions.

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.

20.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 207. TI mode frame format error detection

Timing diagram for TI mode frame format error detection showing NSS, SCK, MOSI, and MISO signals over time. The diagram shows a sequence of data transfers. In the first transfer, the MOSI input contains DONT CARE, MSBIN, and LSBIN. The MISO output contains 1 or 0, MSBOUT, and LSBOUT. The NSS output is shown as a series of pulses. The TIFRFE flag is shown as a signal that goes high during the second transfer, indicating a frame format error. The diagram is labeled ai18438.
Timing diagram for TI mode frame format error detection showing NSS, SCK, MOSI, and MISO signals over time. The diagram shows a sequence of data transfers. In the first transfer, the MOSI input contains DONT CARE, MSBIN, and LSBIN. The MISO output contains 1 or 0, MSBOUT, and LSBOUT. The NSS output is shown as a series of pulses. The TIFRFE flag is shown as a signal that goes high during the second transfer, indicating a frame format error. The diagram is labeled ai18438.

20.3.11 SPI interrupts

Table 90. 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

20.4 I 2 S functional description

20.4.1 I 2 S general description

The block diagram of the I 2 S is shown in Figure 208 .

Figure 208. I 2 S block diagram

Figure 208. I2S block diagram. This is a detailed block diagram of the I2S interface within a microcontroller. At the top, an 'Address and data bus' is shown as a thick double-headed arrow. Below it, a 'Tx buffer' (transmit buffer) and an 'Rx buffer' (receive buffer) are connected to this bus. A 'Shift register' is positioned between the buffers, with '16-bit' data paths indicated. The shift register is controlled by 'LSB first' and 'Communication control' blocks. The 'Communication control' block is connected to a series of registers: BSY, OVR, MODF, CRC ERR, UDR, CH SIDE, TxE, RxNE, and FRE. On the left, pins are labeled MOSI/SD, MISO/ I2S2ext_SD/ I2S3ext_SD(1), and NSS/WS. The 'Master control logic' block is connected to the communication control and to a set of configuration registers: I2SCFG [1:0], I2SSTD [1:0], CK POL, DATLEN [1:0], CH LEN, and a second row with I2S MOD and I2SE. Below these are another set of registers: Bidi mode, Bidi OE, CRC EN, CRC Next, DFF, Rx only, SSM, and SSI. Further down are registers for LSB First, SPE, BR2, BR1, BR0, MSTR, CPOL, and CPHA. A 'SPI baud rate generator' is connected to the master control logic. At the bottom, an 'I2S clock generator' is shown, connected to 'CK' and 'I2S_CK' pins. It has control registers MCKOE/ODD and I2SDIV[7:0]. An 'MCK' pin is also shown at the bottom left. The diagram is labeled MS19909V1 in the bottom right corner.
Figure 208. I2S block diagram. This is a detailed block diagram of the I2S interface within a microcontroller. At the top, an 'Address and data bus' is shown as a thick double-headed arrow. Below it, a 'Tx buffer' (transmit buffer) and an 'Rx buffer' (receive buffer) are connected to this bus. A 'Shift register' is positioned between the buffers, with '16-bit' data paths indicated. The shift register is controlled by 'LSB first' and 'Communication control' blocks. The 'Communication control' block is connected to a series of registers: BSY, OVR, MODF, CRC ERR, UDR, CH SIDE, TxE, RxNE, and FRE. On the left, pins are labeled MOSI/SD, MISO/ I2S2ext_SD/ I2S3ext_SD(1), and NSS/WS. The 'Master control logic' block is connected to the communication control and to a set of configuration registers: I2SCFG [1:0], I2SSTD [1:0], CK POL, DATLEN [1:0], CH LEN, and a second row with I2S MOD and I2SE. Below these are another set of registers: Bidi mode, Bidi OE, CRC EN, CRC Next, DFF, Rx only, SSM, and SSI. Further down are registers for LSB First, SPE, BR2, BR1, BR0, MSTR, CPOL, and CPHA. A 'SPI baud rate generator' is connected to the master control logic. At the bottom, an 'I2S clock generator' is shown, connected to 'CK' and 'I2S_CK' pins. It has control registers MCKOE/ODD and I2SDIV[7:0]. An 'MCK' pin is also shown at the bottom left. The diagram is labeled MS19909V1 in the bottom right corner.

1. I2S2ext_SD and I2S3ext_SD are the extended SD pins that control the I 2 S full duplex mode.

The SPI could function as an audio I 2 S interface when the I 2 S 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 I 2 S 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 I 2 S 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 I 2 S mode. One is linked to the clock generator configuration SPI_I2SPR and the other one is a generic I 2 S 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 I 2 S mode. Likewise, the SSOE bit in the SPI_CR2 register and the MODF and CRCERR bits in the SPI_SR are not used.

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

20.4.2 I2S full duplex

To support I2S full duplex mode, two extra I 2 S instances called extended I2Ss (I2S2_ext, I2S3_ext) are available in addition to I2S2 and I2S3 (see Figure 209 ). 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 and I2S3_ext are used only in full-duplex mode.

Figure 209. I2S full duplex block diagram

Block diagram of I2S full duplex mode showing two I2S instances, SPI/I2Sx and I2Sx_ext, connected to common I2Sx_SCK and I2Sx_WS pins. SPI/I2Sx has a bidirectional data pin SPIx_MOSI/I2Sx_SD(in/out). I2Sx_ext has a bidirectional data pin I2Sx_extSD(in/out).

The diagram shows two rectangular blocks. The top block is labeled 'SPI/I2Sx' and the bottom block is labeled 'I2Sx_ext'. Both blocks are connected to a common vertical bus line on the left. This bus line has two connection points: the top one is labeled 'I2Sx_SCK' and the bottom one is labeled 'I2Sx_WS'. The 'SPI/I2Sx' block has a horizontal output line on its right side labeled 'SPIx_MOSI/I2Sx_SD(in/out)'. The 'I2Sx_ext' block has a horizontal output line on its right side labeled 'I2Sx_extSD(in/out)'. In the bottom right corner of the diagram area, the text 'MS19910V1' is present.

Block diagram of I2S full duplex mode showing two I2S instances, SPI/I2Sx and I2Sx_ext, connected to common I2Sx_SCK and I2Sx_WS pins. SPI/I2Sx has a bidirectional data pin SPIx_MOSI/I2Sx_SD(in/out). I2Sx_ext has a bidirectional data pin I2Sx_extSD(in/out).

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.

20.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 I 2 S interface supports four audio standards, configurable using the I2SSTD[1:0] and PCMSYNC bits in the SPI_I2SCFGR register.

I 2 S 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 210. I 2 S Philips protocol waveforms (16/32-bit full accuracy, CPOL = 0)

Timing diagram for I2S Philips protocol waveforms showing CK, WS, and SD signals. The diagram illustrates transmission and reception phases. The SD signal is labeled with MSB and LSB. A note indicates the data can be 16-bit or 32-bit. The diagram is labeled MS19591V1.
Timing diagram for I2S Philips protocol waveforms showing CK, WS, and SD signals. The diagram illustrates transmission and reception phases. The SD signal is labeled with MSB and LSB. A note indicates the data can be 16-bit or 32-bit. The diagram is labeled MS19591V1.

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 211. I 2 S Philips standard waveforms (24-bit frame with CPOL = 0)

Timing diagram for I2S Philips standard waveforms showing a 24-bit frame. It includes CK, WS, and SD signals. The SD signal shows 24-bit data transmission followed by 8-bit remaining 0 forced. The diagram is labeled MS19592V1.
Timing diagram for I2S Philips standard waveforms showing a 24-bit frame. It includes CK, WS, and SD signals. The SD signal shows 24-bit data transmission followed by 8-bit remaining 0 forced. The diagram is labeled MS19592V1.

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

Figure 212. Transmitting 0x8EAA33

Diagram showing the two-step process of transmitting 0x8EAA33. First write to Data register: 0x8EAA. Second write to Data register: 0x33XX. A note states: 'Only the 8 MSB are sent to compare the 24 bits. 8 LSBs have no meaning and can be anything'. The diagram is labeled MS19593V1.
Diagram showing the two-step process of transmitting 0x8EAA33. First write to Data register: 0x8EAA. Second write to Data register: 0x33XX. A note states: 'Only the 8 MSB are sent to compare the 24 bits. 8 LSBs have no meaning and can be anything'. The diagram is labeled MS19593V1.

Figure 213. Receiving 0x8EAA33

Figure 213: Receiving 0x8EAA33. Diagram showing two reads from the Data register. The first read returns 0x8EAA. The second read returns 0x33XX. A note indicates that only the 8 MSB are sent to compare the 24 bits, and the 8 LSBs have no meaning and can be anything. Reference MS19594V1.

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

Figure 213: Receiving 0x8EAA33. Diagram showing two reads from the Data register. The first read returns 0x8EAA. The second read returns 0x33XX. A note indicates that only the 8 MSB are sent to compare the 24 bits, and the 8 LSBs have no meaning and can be anything. Reference MS19594V1.
Figure 214. I 2 S Philips standard (16-bit extended to 32-bit packet frame with CPOL = 0) Figure 214: I2S Philips standard (16-bit extended to 32-bit packet frame with CPOL = 0). Timing diagram showing CK, WS, and SD signals. The SD signal is divided into 'Transmission' and 'Reception' phases. The first 16 bits are '16-bit data' (MSB to LSB), and the next 16 bits are '16-bit remaining 0 forced'. The full 32-bit sequence is labeled 'Channel left 32-bit', followed by 'Channel right'. Reference MS19599V1.

CK

WS

SD

Transmission

Reception

16-bit data

MSB

LSB

16-bit remaining 0 forced

Channel left 32-bit

Channel right

MS19599V1

Figure 214: I2S Philips standard (16-bit extended to 32-bit packet frame with CPOL = 0). Timing diagram showing CK, WS, and SD signals. The SD signal is divided into 'Transmission' and 'Reception' phases. The first 16 bits are '16-bit data' (MSB to LSB), and the next 16 bits are '16-bit remaining 0 forced'. The full 32-bit sequence is labeled 'Channel left 32-bit', followed by 'Channel right'. Reference MS19599V1.

When 16-bit data frame extended to 32-bit channel frame is selected during the I 2 S 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 215 is required.

Figure 215. Example

Figure 215: Example. Diagram showing a single access to SPIx_DR with the value 0x76A3. Reference MS19595V1.

Only one access to SPIx_DR

0x76A3

MS19595V1

Figure 215: Example. Diagram showing a single access to SPIx_DR with the value 0x76A3. Reference MS19595V1.

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 MSBbit.

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

Timing diagram for MSB justified 16-bit or 32-bit full-accuracy length with CPOL = 0. It shows three waveforms: CK (clock), WS (word select), and SD (serial data). The CK signal is a periodic square wave. The WS signal is a pulse that goes high during the transmission and reception phases. The SD signal shows a sequence of bits, with the first bit labeled MSB and the last bit of the first channel labeled LSB. The data is divided into two channels: Channel left and Channel right. The diagram indicates that data are latched on the falling edge of CK for transmission and read on the rising edge for reception. The text '16- or 32-bit data' is present. The diagram is labeled MS30100 V1.
Timing diagram for MSB justified 16-bit or 32-bit full-accuracy length with CPOL = 0. It shows three waveforms: CK (clock), WS (word select), and SD (serial data). The CK signal is a periodic square wave. The WS signal is a pulse that goes high during the transmission and reception phases. The SD signal shows a sequence of bits, with the first bit labeled MSB and the last bit of the first channel labeled LSB. The data is divided into two channels: Channel left and Channel right. The diagram indicates that data are latched on the falling edge of CK for transmission and read on the rising edge for reception. The text '16- or 32-bit data' is present. The diagram is labeled MS30100 V1.

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

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

Timing diagram for MSB justified 24-bit frame length with CPOL = 0. It shows three waveforms: CK, WS, and SD. The CK signal is a periodic square wave. The WS signal is a pulse that goes high during the transmission and reception phases. The SD signal shows a sequence of bits, with the first bit labeled MSB and the last bit of the first channel labeled LSB. The data is divided into two channels: Channel left 32-bit and Channel right. The diagram indicates that data are latched on the falling edge of CK for transmission and read on the rising edge for reception. The text '24-bit data' and '8-bit remaining 0 forced' is present. The diagram is labeled MS30101V1.
Timing diagram for MSB justified 24-bit frame length with CPOL = 0. It shows three waveforms: CK, WS, and SD. The CK signal is a periodic square wave. The WS signal is a pulse that goes high during the transmission and reception phases. The SD signal shows a sequence of bits, with the first bit labeled MSB and the last bit of the first channel labeled LSB. The data is divided into two channels: Channel left 32-bit and Channel right. The diagram indicates that data are latched on the falling edge of CK for transmission and read on the rising edge for reception. The text '24-bit data' and '8-bit remaining 0 forced' is present. The diagram is labeled MS30101V1.

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

Timing diagram for MSB justified 16-bit extended to 32-bit packet frame with CPOL = 0. It shows three waveforms: CK, WS, and SD. The CK signal is a periodic square wave. The WS signal is a pulse that goes high during the transmission and reception phases. The SD signal shows a sequence of bits, with the first bit labeled MSB and the last bit of the first channel labeled LSB. The data is divided into two channels: Channel left 32-bit and Channel right. The diagram indicates that data are latched on the falling edge of CK for transmission and read on the rising edge for reception. The text '16-bit data' and '16-bit remaining 0 forced' is present. The diagram is labeled MS30102V1.
Timing diagram for MSB justified 16-bit extended to 32-bit packet frame with CPOL = 0. It shows three waveforms: CK, WS, and SD. The CK signal is a periodic square wave. The WS signal is a pulse that goes high during the transmission and reception phases. The SD signal shows a sequence of bits, with the first bit labeled MSB and the last bit of the first channel labeled LSB. The data is divided into two channels: Channel left 32-bit and Channel right. The diagram indicates that data are latched on the falling edge of CK for transmission and read on the rising edge for reception. The text '16-bit data' and '16-bit remaining 0 forced' is present. The diagram is labeled MS30102V1.

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 219. LSB justified 16-bit or 32-bit full-accuracy with CPOL = 0

Timing diagram for Figure 219 showing CK, WS, and SD signals. The diagram illustrates a transmission followed by a reception of 16- or 32-bit data. The SD signal is shown as a sequence of bits with MSB and LSB labels. The transmission and reception are separated by a gap in the SD signal. The diagram is labeled MS30103V1.
Timing diagram for Figure 219 showing CK, WS, and SD signals. The diagram illustrates a transmission followed by a reception of 16- or 32-bit data. The SD signal is shown as a sequence of bits with MSB and LSB labels. The transmission and reception are separated by a gap in the SD signal. The diagram is labeled MS30103V1.

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

Timing diagram for Figure 220 showing CK, WS, and SD signals. The diagram illustrates a transmission of 24-bit data, split into an 8-bit data part (0 forced) and a 24-bit remaining part. The SD signal is shown as a sequence of bits with MSB and LSB labels. The transmission and reception are separated by a gap in the SD signal. The diagram is labeled MS30104V1.
Timing diagram for Figure 220 showing CK, WS, and SD signals. The diagram illustrates a transmission of 24-bit data, split into an 8-bit data part (0 forced) and a 24-bit remaining part. The SD signal is shown as a sequence of bits with MSB and LSB labels. The transmission and reception are separated by a gap in the SD signal. The diagram is labeled MS30104V1.

Figure 221. Operations required to transmit 0x3478AE

First write to Data register conditioned by TXE=1Second write to Data register conditioned by TXE=1
0xXX340x78AE
Only the 8 LSB of the half-word are significant. A field of 0x00 is forced instead of the 8 MSBs.

MS19596V1

Figure 222. Operations required to receive 0x3478AE

Figure 222: Operations required to receive 0x3478AE. The diagram shows two reads from the Data register. The first read, conditioned by RXNE=1, shows the value 0xXX34, with a note that only the 8 LSB of the half-word are significant and a field of 0x00 is forced instead of the 8 MSBs. The second read, also conditioned by RXNE=1, shows the value 0x78AE. The diagram is labeled MS19597V1.

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

Figure 222: Operations required to receive 0x3478AE. The diagram shows two reads from the Data register. The first read, conditioned by RXNE=1, shows the value 0xXX34, with a note that only the 8 LSB of the half-word are significant and a field of 0x00 is forced instead of the 8 MSBs. The second read, also conditioned by RXNE=1, shows the value 0x78AE. The diagram is labeled MS19597V1.

Figure 223. LSB justified 16-bit extended to 32-bit packet frame with CPOL = 0

Figure 223: LSB justified 16-bit extended to 32-bit packet frame with CPOL = 0. This timing diagram shows the relationship between the Clock (CK), Word Select (WS), and Serial Data (SD) lines. The CK line is a periodic square wave. The WS line is active-low. The SD line shows the transmission of a 32-bit frame. The first 16 bits are '16-bit data 0 forced', and the next 16 bits are '16-bit remaining'. The first 16 bits are further divided into 'Channel left 32-bit' (MSB) and 'Channel right' (LSB). The reception of the data is indicated by an arrow labeled 'Reception'. The diagram is labeled MS30105V1.

CK

WS

SD

Transmission

Reception

16-bit data 0 forced

16-bit remaining

MSB

LSB

Channel left 32-bit

Channel right

MS30105V1

Figure 223: LSB justified 16-bit extended to 32-bit packet frame with CPOL = 0. This timing diagram shows the relationship between the Clock (CK), Word Select (WS), and Serial Data (SD) lines. The CK line is a periodic square wave. The WS line is active-low. The SD line shows the transmission of a 32-bit frame. The first 16 bits are '16-bit data 0 forced', and the next 16 bits are '16-bit remaining'. The first 16 bits are further divided into 'Channel left 32-bit' (MSB) and 'Channel right' (LSB). The reception of the data is indicated by an arrow labeled 'Reception'. The diagram is labeled MS30105V1.

When 16-bit data frame extended to 32-bit channel frame is selected during the \( I^2S \) 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 224 is required.

Figure 224. Example of LSB justified 16-bit extended to 32-bit packet frame

Figure 224: Example of LSB justified 16-bit extended to 32-bit packet frame. The diagram shows a single access to the SPIx-DR register with the value 0x76A3. The diagram is labeled MS19598V1.

Only one access to the SPIx-DR register

0x76A3

MS19598V1

Figure 224: Example of LSB justified 16-bit extended to 32-bit packet frame. The diagram shows a single access to the SPIx-DR register with the value 0x76A3. The diagram is labeled MS19598V1.

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 225. PCM standard waveforms (16-bit)

Figure 225: PCM standard waveforms (16-bit). The diagram shows four signal lines: CK (clock), WS (word synchronization), SD (serial data), and a reference line. CK is a periodic square wave. WS short frame is a pulse that is high for one clock cycle and low for the rest of the frame. WS long frame is a pulse that is high for 13 clock cycles and low for the rest of the frame. SD is a serial data stream with MSB (Most Significant Bit) and LSB (Least Significant Bit) indicated. The data is shown in two consecutive 16-bit words. The first word has its MSB and LSB marked. The second word also has its MSB and LSB marked. The time between the rising edges of the WS signal for consecutive frames is 13 bits in the long frame mode. The diagram is labeled MS30106V1.
Figure 225: PCM standard waveforms (16-bit). The diagram shows four signal lines: CK (clock), WS (word synchronization), SD (serial data), and a reference line. CK is a periodic square wave. WS short frame is a pulse that is high for one clock cycle and low for the rest of the frame. WS long frame is a pulse that is high for 13 clock cycles and low for the rest of the frame. SD is a serial data stream with MSB (Most Significant Bit) and LSB (Least Significant Bit) indicated. The data is shown in two consecutive 16-bit words. The first word has its MSB and LSB marked. The second word also has its MSB and LSB marked. The time between the rising edges of the WS signal for consecutive frames is 13 bits in the long frame mode. The diagram is 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 226. PCM standard waveforms (16-bit extended to 32-bit packet frame)

Figure 226: PCM standard waveforms (16-bit extended to 32-bit packet frame). The diagram shows four signal lines: CK (clock), WS (word synchronization), SD (serial data), and a reference line. CK is a periodic square wave. WS short frame is a pulse that is high for one clock cycle and low for the rest of the frame. WS long frame is a pulse that is high for 'Up to 13-bits' and low for the rest of the frame. SD is a serial data stream with MSB (Most Significant Bit) and LSB (Least Significant Bit) indicated. The data is shown in two consecutive 16-bit words, which are part of a 32-bit packet frame. The first word has its MSB and LSB marked. The second word also has its MSB and LSB marked. The time between the rising edges of the WS signal for consecutive frames is 16 bits in the long frame mode. The diagram is labeled MS30107V1.
Figure 226: PCM standard waveforms (16-bit extended to 32-bit packet frame). The diagram shows four signal lines: CK (clock), WS (word synchronization), SD (serial data), and a reference line. CK is a periodic square wave. WS short frame is a pulse that is high for one clock cycle and low for the rest of the frame. WS long frame is a pulse that is high for 'Up to 13-bits' and low for the rest of the frame. SD is a serial data stream with MSB (Most Significant Bit) and LSB (Least Significant Bit) indicated. The data is shown in two consecutive 16-bit words, which are part of a 32-bit packet frame. The first word has its MSB and LSB marked. The second word also has its MSB and LSB marked. The time between the rising edges of the WS signal for consecutive frames is 16 bits in the long frame mode. The diagram is 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.

20.4.4 Clock generator

The I 2 S bitrate determines the dataflow on the I 2 S data line and the I 2 S clock signal frequency.

I 2 S bitrate = number of bits per channel × number of channels × sampling audio frequency

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

\[ \text{I}^2\text{S bitrate} = 16 \times 2 \times F_S \]

It is I 2 S bitrate = 32 × 2 × F S if the packet length is 32-bit wide.

Figure 227. Audio sampling frequency definition

Figure 227: Audio sampling frequency definition diagram. It shows a sequence of audio samples: '16- or 32-bit left channel' followed by '16- or 32-bit right channel'. The total duration for one pair of channels is labeled '32- or 64-bits'. The time interval between the 'sampling point' of one pair and the next is labeled 'F_s'. A note below states 'F_s : audio sampling frequency'. The diagram is labeled 'MS30108V1' in the bottom right corner.
Figure 227: Audio sampling frequency definition diagram. It shows a sequence of audio samples: '16- or 32-bit left channel' followed by '16- or 32-bit right channel'. The total duration for one pair of channels is labeled '32- or 64-bits'. The time interval between the 'sampling point' of one pair and the next is labeled 'F_s'. A note below states 'F_s : audio sampling frequency'. The diagram is labeled 'MS30108V1' in the bottom right corner.

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 228. I 2 S clock generator architecture Figure 228: I2S clock generator architecture diagram. The diagram shows the internal clock generation logic. An input 'I2SxCLK' enters a block labeled '8-bit linear divider + reshaping stage'. This stage is controlled by registers 'MCKOE/ODD' and 'I2SDIV[7:0]'. The output of this stage goes to a 'Divider by 4' block, which then goes to a 'Div2' block. The output of 'Div2' is connected to a multiplexer (MUX) with inputs '0' and '1'. The MUX is controlled by 'MCKOE' and 'CHLEN' bits from the 'I2SMOD' register. The output of the MUX is 'CK'. Another output 'MCK' is also shown. The diagram is labeled 'MS30109V1' in the bottom right corner.
Figure 228: I2S clock generator architecture diagram. The diagram shows the internal clock generation logic. An input 'I2SxCLK' enters a block labeled '8-bit linear divider + reshaping stage'. This stage is controlled by registers 'MCKOE/ODD' and 'I2SDIV[7:0]'. The output of this stage goes to a 'Divider by 4' block, which then goes to a 'Div2' block. The output of 'Div2' is connected to a multiplexer (MUX) with inputs '0' and '1'. The MUX is controlled by 'MCKOE' and 'CHLEN' bits from the 'I2SMOD' register. The output of the MUX is 'CK'. Another output 'MCK' is also shown. The diagram is labeled 'MS30109V1' in the bottom right corner.
  1. 1. Where x could be 2 or 3.

Figure 227 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 91 provides example precision values for different clock configurations.

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

Table 91. Audio frequency precision (for PLLM VCO = 1 MHz or 2 MHz) (1)
Master clockTarget f s (Hz)Data formatPLL12SNPLL12SRI2SDIVI2SODDReal f s (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.

20.4.5 I 2 S master mode

The I 2 S 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 20.4.4: Clock generator ).
  3. 3. Set the I2SMOD bit in SPI_I2SCFGR to activate the I 2 S functionalities and choose the I 2 S 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 I 2 S 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 I 2 S standard mode selected, refer to Section 20.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 I 2 S, 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 20.4.5: I 2 S master mode ), 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 I 2 S cell.

For more details about the read operations depending on the I 2 S standard mode selected, refer to Section 20.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 I 2 S, specific actions are required to ensure that the I 2 S 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.

20.4.6 I 2 S slave mode

The I 2 S can be configured as follows:

The operating mode is following mainly the same rules as described for the I 2 S master configuration. In slave mode, there is no clock to be generated by the I 2 S interface. The clock and WS signals are input from the external master connected to the I 2 S 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 I 2 S functionalities and choose the I 2 S 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 I 2 S data register has to be loaded before the master initiates the communication.

For the I 2 S, 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 I 2 S 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 I 2 S standard mode selected, refer to Section 20.4.3: Supported audio protocols .

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 I 2 S and to restart a data transfer starting from the left channel.

To switch off the I 2 S, 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 20.4.6: I 2 S slave mode ), 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 I 2 S standard mode selected, refer to Section 20.4.3: Supported audio protocols .

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 I 2 S 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.

20.4.7 Status flags

Three status flags are provided for the application to fully monitor the state of the I 2 S 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 I 2 S.

When BSY is set, it indicates that the I 2 S 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 I 2 S. 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 I 2 S 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 I 2 S 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 I 2 S 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 I 2 S 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).

20.4.8 Error flags

There are three error flags for the I 2 S 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.

20.4.9 I 2 S interrupts

Table 92 provides the list of I 2 S interrupts.

Table 92. I 2 S interrupt requests

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

20.4.10 DMA features

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

20.5 SPI and I 2 S registers

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

20.5.1 SPI control register 1 (SPI_CR1)(not used in I 2 S mode)

Address offset: 0x00

Reset value: 0x0000

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

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 I 2 S 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 I 2 S 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 I 2 S 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 I 2 S 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 I 2 S 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 I 2 S 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 I 2 S 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 I 2 S 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 I 2 S mode and SPI TI mode

Bit 6 SPE: SPI enable

0: Peripheral disabled

1: Peripheral enabled

Note: This bit is not used in I 2 S mode.

When disabling the SPI, follow the procedure described in Section 20.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 I 2 S 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 I 2 S 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 I 2 S 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 I 2 S mode and SPI TI mode.

20.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 I 2 S 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 I 2 S 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 I 2 S 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

20.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 20.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 20.3.7 and Section 20.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 20.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 20.3.10).

Note: This bit is not used in I 2 S 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 I 2 S mode.

Bit 3 UDR : Underrun flag

0: No underrun occurred

1: Underrun occurred

This flag is set by hardware and reset by a software sequence. Refer to Section 20.4.8: Error flags for the software sequence.

Note: This bit is not used in SPI mode.

Bit 2 CHSIDE : Channel side

0: Channel Left has to be transmitted or has been received

1: Channel Right has to be transmitted or has been received

Note: This bit is not used for SPI mode and is meaningless in PCM mode.

Bit 1 TXE : Transmit buffer empty

0: Tx buffer not empty

1: Tx buffer empty

Bit 0 RXNE : Receive buffer not empty

0: Rx buffer empty

1: Rx buffer not empty

20.5.4 SPI data register (SPI_DR)

Address offset: 0x0C

Reset value: 0x0000

1514131211109876543210
DR[15:0]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

Bits 15:0 DR[15:0] : Data register

Data received or to be transmitted.

The data register is split into 2 buffers - one for writing (Transmit Buffer) and another one for reading (Receive buffer). A write to the data register writes into the Tx buffer and a read from the data register returns the value held in the Rx buffer.

Note: These notes apply to SPI mode:

Depending on the data frame format selection bit (DFF in SPI_CR1 register), the data sent or received is either 8-bit or 16-bit. This selection has to be made before enabling the SPI to ensure correct operation.

For an 8-bit data frame, the buffers are 8-bit and only the LSB of the register (SPI_DR[7:0]) is used for transmission/reception. When in reception mode, the MSB of the register (SPI_DR[15:8]) is forced to 0.

For a 16-bit data frame, the buffers are 16-bit and the entire register, SPI_DR[15:0] is used for transmission/reception.

20.5.5 SPI CRC polynomial register (SPI_CRCPR)(not used in I 2 S 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 I 2 S mode.

20.5.6 SPI RX CRC register (SPI_RXCRCR)(not used in I 2 S 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 I 2 S mode

20.5.7 SPI TX CRC register (SPI_TXCRCR)(not used in I 2 S 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 I 2 S mode.

20.5.8 SPI_I 2 S 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

Note: This bit should be configured when the SPI or I 2 S is disabled

Bit 10 I2SE : I2S Enable

Note: This bit is not used in SPI mode.

Bits 9:8 I2SCFG : I2S configuration mode

Note: This bit should be configured when the I 2 S is disabled.
It is not used in SPI mode.

Bit 7 PCMSYNC : PCM 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

For more details on I 2 S standards, refer to Section 20.4.3: Supported audio protocols . Not used in SPI mode.

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

Bit 3 CKPOL : Steady state clock polarity

Note: For correct operation, this bit should be configured when the I 2 S is disabled.
This bit is not used in SPI mode.

Bits 2:1 DATLEN : Data length to be transferred

Note: For correct operation, these bits should be configured when the I 2 S is disabled.
This bit is not used in SPI mode.

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

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 I 2 S is disabled.

20.5.9 SPI_I 2 S prescaler register (SPI_I2SPR)

Address offset: 0x20

Reset value: 0000 0010 (0x0002)

1514131211109876543210
ReservedMCKOE
rw
ODD
rw
I2SDIV
rw

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 I 2 S is disabled. It is used only when the I 2 S 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 20.4.4: Clock generator . Not used in SPI mode.

Note: This bit should be configured when the I 2 S is disabled. It is used only when the I 2 S 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 20.4.4: Clock generator . Not used in SPI mode.

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

20.5.10 SPI register map

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

Table 93. SPI register map and reset values

OffsetRegister313029282726252423222120191817161514131211109876543210
0x00SPI_CR1ReservedBIDIMODEBIDIOECRCENCRCNEXTDFFRXONLYSSMSSILSBFIRSTSPEBR [2:0]MSTRCPOLCPHA
Reset value0000000000000000
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_I2SCFGRReservedI2SMODI2SEI2SCFGPCMSYNCRes.I2SSTDCKPOLDATLENCHLEN
Reset value00000000000
0x20SPI_I2SPRReservedMCKOEODDI2SDIV
Reset value0000000010

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