26. Serial peripheral interface/ inter-IC sound (SPI/I2S)

26.1 Introduction

The SPI/I 2 S interface can be used to communicate with external devices using the SPI protocol or the I 2 S audio protocol. SPI or I 2 S mode is selectable by software. SPI mode is selected by default after a device reset.

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

The Inter-IC sound (I 2 S) protocol is also a synchronous serial communication interface. It can operate in slave or master mode with half-duplex communication. Full duplex operations are possible by combining two I2S blocks.

It can address four different audio standards including the Philips I 2 S standard, the MSB- and LSB-justified standards and the PCM standard.

26.1.1 SPI main features

26.1.2 SPI extended features

26.1.3 I2S features

26.2 SPI/I2S implementation

This manual describes the full set of features implemented in SPI1 and SPI2.

Table 134. STM32L0x1 SPI implementation

SPI Features (1)SPI1SPI2
Hardware CRC calculationXX
I2S mode-X
TI modeXX

1. X = supported.

26.3 SPI functional description

26.3.1 General description

The SPI allows synchronous, serial communication between the MCU and external devices. Application software can manage the communication by polling the status flag or using dedicated SPI interrupt. The main elements of SPI and their interactions are shown in the following block diagram Figure 250 .

Figure 250. SPI block diagram. This block diagram illustrates the internal architecture of an SPI peripheral. At the top, an 'Address and data bus' is shown with bidirectional arrows. Below it, a 'Read' path leads from the bus to an 'Rx buffer', which then connects to a 'Shift register'. A 'Write' path leads from the bus to a 'TX buffer', which also connects to the 'Shift register'. The 'Shift register' is connected to two pins on the left: 'MOSI' (Master Out / Slave In) and 'MISO' (Master In / Slave Out). Below these pins is the 'SCK' (Serial Clock) pin, which is connected to a 'Baud rate generator'. The 'Baud rate generator' is controlled by 'BR[2:0]' signals from the 'Communication controller'. Below the 'SCK' pin is the 'NSS' (Slave Select) pin, which is connected to 'NSS logic'. The 'NSS logic' is also controlled by 'Internal NSS' signals from the 'Communication controller'. The 'Communication controller' is connected to the 'CRC controller' and the 'Baud rate generator'. It has several control input pins on the right: 'LSBFIRST', 'CPOL', 'CPHA', 'DFF', 'BIDIOE', 'BIDIMODE', and 'RXOLNY'. The 'CRC controller' has control input pins 'CRCEN' and 'CRCNEXT'.

Figure 250. SPI block diagram

Figure 250. SPI block diagram. This block diagram illustrates the internal architecture of an SPI peripheral. At the top, an 'Address and data bus' is shown with bidirectional arrows. Below it, a 'Read' path leads from the bus to an 'Rx buffer', which then connects to a 'Shift register'. A 'Write' path leads from the bus to a 'TX buffer', which also connects to the 'Shift register'. The 'Shift register' is connected to two pins on the left: 'MOSI' (Master Out / Slave In) and 'MISO' (Master In / Slave Out). Below these pins is the 'SCK' (Serial Clock) pin, which is connected to a 'Baud rate generator'. The 'Baud rate generator' is controlled by 'BR[2:0]' signals from the 'Communication controller'. Below the 'SCK' pin is the 'NSS' (Slave Select) pin, which is connected to 'NSS logic'. The 'NSS logic' is also controlled by 'Internal NSS' signals from the 'Communication controller'. The 'Communication controller' is connected to the 'CRC controller' and the 'Baud rate generator'. It has several control input pins on the right: 'LSBFIRST', 'CPOL', 'CPHA', 'DFF', 'BIDIOE', 'BIDIMODE', and 'RXOLNY'. The 'CRC controller' has control input pins 'CRCEN' and 'CRCNEXT'.

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

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

The SPI bus allows the communication between one master device and one or more slave devices. The bus consists of at least two wires - one for the clock signal and the other for synchronous data transfer. Other signals can be added depending on the data exchange between SPI nodes and their slave select signal management.

26.3.2 Communications between one master and one slave

The SPI allows the MCU to communicate using different configurations, depending on the device targeted and the application requirements. These configurations use 2 or 3 wires (with software NSS management) or 3 or 4 wires (with hardware NSS management). Communication is always initiated by the master.

Full-duplex communication

By default, the SPI is configured for full-duplex communication. In this configuration, the shift registers of the master and slave are linked using two unidirectional lines between the MOSI and the MISO pins. During SPI communication, data is shifted synchronously on the SCK clock edges provided by the master. The master transmits the data to be sent to the slave via the MOSI line and receives data from the slave via the MISO line. When the data frame transfer is complete (all the bits are shifted) the information between the master and slave is exchanged.

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

Diagram of a full-duplex single master/single slave SPI application. The Master (left) contains an Rx shift register, a Tx shift register, and an SPI clock generator. The Slave (right) contains a Tx shift register and an Rx shift register. Connections: MISO (Master Rx to Slave Tx), MOSI (Master Tx to Slave Rx), SCK (Master clock to Slave), and NSS (Master to Slave). Arrows indicate data flow: MISO from Slave to Master, MOSI from Master to Slave, SCK from Master to Slave. MSV39623V1 is noted in the bottom right.
Diagram of a full-duplex single master/single slave SPI application. The Master (left) contains an Rx shift register, a Tx shift register, and an SPI clock generator. The Slave (right) contains a Tx shift register and an Rx shift register. Connections: MISO (Master Rx to Slave Tx), MOSI (Master Tx to Slave Rx), SCK (Master clock to Slave), and NSS (Master to Slave). Arrows indicate data flow: MISO from Slave to Master, MOSI from Master to Slave, SCK from Master to Slave. MSV39623V1 is noted in the bottom right.
  1. 1. The NSS pins can be used to provide a hardware control flow between master and slave. Optionally, the pins can be left unused by the peripheral. Then the flow has to be handled internally for both master and slave. For more details see Section 26.3.5: Slave select (NSS) pin management .

Half-duplex communication

The SPI can communicate in half-duplex mode by setting the BIDIMODE bit in the SPIx_CR1 register. In this configuration, one single cross connection line is used to link the shift registers of the master and slave together. During this communication, the data is synchronously shifted between the shift registers on the SCK clock edge in the transfer direction selected reciprocally by both master and slave with the BDIOE bit in their SPIx_CR1 registers. In this configuration, the master's MISO pin and the slave's MOSI pin are free for other application uses and act as GPIOs.

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

Figure 252: Half-duplex single master/ single slave application diagram. The diagram shows a Master and a Slave connected via SPI lines. The Master contains an Rx shift register, a Tx shift register, and an SPI clock generator. The Slave contains a Tx shift register and an Rx shift register. The connections are: MISO (Master) to MISO (Slave), MOSI (Master) to MOSI (Slave) through a 1kΩ resistor, SCK (Master) to SCK (Slave), and NSS (Master) to NSS (Slave). Arrows indicate data flow: from Master's Tx shift register to Slave's Rx shift register via MOSI, and from Slave's Tx shift register to Master's Rx shift register via MISO. The diagram is labeled MSv39624V1.
Figure 252: Half-duplex single master/ single slave application diagram. The diagram shows a Master and a Slave connected via SPI lines. The Master contains an Rx shift register, a Tx shift register, and an SPI clock generator. The Slave contains a Tx shift register and an Rx shift register. The connections are: MISO (Master) to MISO (Slave), MOSI (Master) to MOSI (Slave) through a 1kΩ resistor, SCK (Master) to SCK (Slave), and NSS (Master) to NSS (Slave). Arrows indicate data flow: from Master's Tx shift register to Slave's Rx shift register via MOSI, and from Slave's Tx shift register to Master's Rx shift register via MISO. The diagram is labeled MSv39624V1.
  1. 1. The NSS pins can be used to provide a hardware control flow between master and slave. Optionally, the pins can be left unused by the peripheral. Then the flow has to be handled internally for both master and slave. For more details see Section 26.3.5: Slave select (NSS) pin management .
  2. 2. In this configuration, the master's MISO pin and the slave's MOSI pin can be used as GPIOs.
  3. 3. A critical situation can happen when communication direction is changed not synchronously between two nodes working at bidirectional mode and new transmitter accesses the common data line while former transmitter still keeps an opposite value on the line (the value depends on SPI configuration and communication data). Both nodes then fight while providing opposite output levels on the common line temporary till next node changes its direction settings correspondingly, too. It is suggested to insert a serial resistance between MISO and MOSI pins at this mode to protect the outputs and limit the current blowing between them at this situation.

Simplex communications

The SPI can communicate in simplex mode by setting the SPI in transmit-only or in receive-only using the RXONLY bit in the SPIx_CR2 register. In this configuration, only one line is used for the transfer between the shift registers of the master and slave. The remaining MISO and MOSI pins pair is not used for communication and can be used as standard GPIOs.

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

Figure 253: Simplex single master/single slave application diagram. It shows a Master on the left and a Slave on the right. The Master contains an Rx shift register, a Tx shift register, and an SPI clock generator. The Slave contains a Tx shift register and an Rx shift register. They are connected by four lines: MISO, MOSI, SCK, and NSS. Arrows show MOSI and SCK signals originating from the Master and going to the Slave. The NSS line connects both. The Master's MISO pin is marked with (2). Both NSS pins are marked with (1).
Figure 253: Simplex single master/single slave application diagram. It shows a Master on the left and a Slave on the right. The Master contains an Rx shift register, a Tx shift register, and an SPI clock generator. The Slave contains a Tx shift register and an Rx shift register. They are connected by four lines: MISO, MOSI, SCK, and NSS. Arrows show MOSI and SCK signals originating from the Master and going to the Slave. The NSS line connects both. The Master's MISO pin is marked with (2). Both NSS pins are marked with (1).
  1. 1. The NSS pins can be used to provide a hardware control flow between master and slave. Optionally, the pins can be left unused by the peripheral. Then the flow has to be handled internally for both master and slave. For more details see Section 26.3.5: Slave select (NSS) pin management .
  2. 2. An accidental input information is captured at the input of transmitter Rx shift register. All the events associated with the transmitter receive flow must be ignored in standard transmit only mode (e.g. OVF flag).
  3. 3. In this configuration, both the MISO pins can be used as GPIOs.

Note: Any simplex communication can be alternatively replaced by a variant of the half-duplex communication with a constant setting of the transaction direction (bidirectional mode is enabled while BDIO bit is not changed).

26.3.3 Standard multi-slave communication

In a configuration with two or more independent slaves, the master uses GPIO pins to manage the chip select lines for each slave (see Figure 254 ). The master must select one of the slaves individually by pulling low the GPIO connected to the slave NSS input. When this is done, a standard master and dedicated slave communication is established.

Figure 254. Master and three independent slaves

Diagram of standard multi-slave communication showing a master connected to three independent slaves (Slave 1, Slave 2, Slave 3). The master has an SPI clock generator, Tx and Rx shift registers, and pins for NSS(1), MISO, MOSI, SCK, IO1, IO2, and IO3. The slaves have Tx and Rx shift registers and pins for MISO, MOSI, SCK, and NSS. The master's SCK pin is connected to all slaves' SCK pins. The master's MOSI pin is connected to all slaves' MOSI pins. The master's MISO pin is connected to all slaves' MISO pins. The master's NSS(1) pin is connected to all slaves' NSS pins. The master's IO1, IO2, and IO3 pins are connected to the slaves' NSS pins to manage chip select lines.

The diagram illustrates a master device connected to three independent slave devices (Slave 1, Slave 2, and Slave 3) in a standard multi-slave SPI configuration. The master device contains an SPI clock generator, a Tx shift register, and an Rx shift register. It has pins for NSS (1) , MISO, MOSI, SCK, IO1, IO2, and IO3. The slaves (Slave 1, Slave 2, and Slave 3) each contain a Tx shift register and an Rx shift register. They have pins for MISO, MOSI, SCK, and NSS. The master's SCK pin is connected to all slaves' SCK pins. The master's MOSI pin is connected to all slaves' MOSI pins. The master's MISO pin is connected to all slaves' MISO pins. The master's NSS (1) pin is connected to all slaves' NSS pins. The master's IO1, IO2, and IO3 pins are connected to the slaves' NSS pins to manage chip select lines. Arrows indicate the direction of data flow: MISO from slaves to master, and MOSI and SCK from master to slaves. The diagram is labeled MSV39626V1.

Diagram of standard multi-slave communication showing a master connected to three independent slaves (Slave 1, Slave 2, Slave 3). The master has an SPI clock generator, Tx and Rx shift registers, and pins for NSS(1), MISO, MOSI, SCK, IO1, IO2, and IO3. The slaves have Tx and Rx shift registers and pins for MISO, MOSI, SCK, and NSS. The master's SCK pin is connected to all slaves' SCK pins. The master's MOSI pin is connected to all slaves' MOSI pins. The master's MISO pin is connected to all slaves' MISO pins. The master's NSS(1) pin is connected to all slaves' NSS pins. The master's IO1, IO2, and IO3 pins are connected to the slaves' NSS pins to manage chip select lines.
  1. 1. NSS pin is not used on master side at this configuration. It has to be managed internally (SSM=1, SSI=1) to prevent any MODF error.
  2. 2. As MISO pins of the slaves are connected together, all slaves must have the GPIO configuration of their MISO pin set as alternate function open-drain (see Section 8.3.7: I/O alternate function input/output on page 221 ).

26.3.4 Multi-master communication

Unless SPI bus is not designed for a multi-master capability primarily, the user can use built-in feature which detects a potential conflict between two nodes trying to master the bus at the same time. For this detection, NSS pin is used configured at hardware input mode.

The connection of more than two SPI nodes working at this mode is impossible as only one node can apply its output on a common data line at time.

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

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

Figure 255. Multi-master application

Diagram of a multi-master application showing two SPI nodes connected via MISO, MOSI, SCK, and NSS lines. Each node contains an Rx (Tx) shift register, a Tx (Rx) shift register, and an SPI clock generator. The left node is labeled 'Master (Slave)' and the right node is also labeled 'Master (Slave)'. Arrows indicate signal flow: MISO lines are bidirectional, MOSI lines are bidirectional, SCK lines are bidirectional, and NSS lines are unidirectional from the left node's GPIO to the right node's NSS(1) and from the right node's GPIO to the left node's NSS(1).
Diagram of a multi-master application showing two SPI nodes connected via MISO, MOSI, SCK, and NSS lines. Each node contains an Rx (Tx) shift register, a Tx (Rx) shift register, and an SPI clock generator. The left node is labeled 'Master (Slave)' and the right node is also labeled 'Master (Slave)'. Arrows indicate signal flow: MISO lines are bidirectional, MOSI lines are bidirectional, SCK lines are bidirectional, and NSS lines are unidirectional from the left node's GPIO to the right node's NSS(1) and from the right node's GPIO to the left node's NSS(1).
  1. 1. The NSS pin is configured at hardware input mode at both nodes. Its active level enables the MISO line output control as the passive node is configured as a slave.

26.3.5 Slave select (NSS) pin management

In slave mode, the NSS works as a standard “chip select” input and lets the slave communicate with the master. In master mode, NSS can be used either as output or input. As an input it can prevent multimaster bus collision, and as an output it can drive a slave select signal of a single slave.

Hardware or software slave select management can be set using the SSM bit in the SPIx_CR1 register:

Figure 256. Hardware/software slave select management

Diagram of hardware/software slave select management showing internal logic, control bits, and a table for NSS input levels.

The diagram illustrates the internal logic for slave select management. It shows the NSS pin connected to GPIO logic, which is part of the NSS external logic. The GPIO logic is connected to a multiplexer (MUX) that selects between the NSS pin (input 0) and the SSI control bit (input 1). The output of the MUX is the NSS Input. The NSS Input is also connected to the NSS Output Control block, which is part of the NSS internal logic. The NSS Output Control block is controlled by the SSOE control bit and the SSM control bit. The SSM control bit is also connected to the MUX. The SSI control bit is connected to the MUX and the NSS Output Control block. The NSS Output Control block outputs the NSS Output signal, which is used in Master mode & NSS HW management only. The diagram is divided into NSS external logic and NSS internal logic by a dashed line.

NSS Inp.Master modeSlave mode
VddOKNon active
VssConflictActive

aiv14746e

Diagram of hardware/software slave select management showing internal logic, control bits, and a table for NSS input levels.

26.3.6 Communication formats

During SPI communication, receive and transmit operations are performed simultaneously. The serial clock (SCK) synchronizes the shifting and sampling of the information on the data lines. The communication format depends on the clock phase, the clock polarity and the data frame format. To be able to communicate together, the master and slaves devices must follow the same communication format.

Clock phase and polarity controls

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

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

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

Figure 257 , shows an SPI full-duplex transfer with the four combinations of the CPHA and CPOL bits.

Note: Prior to changing the CPOL/CPHA bits the SPI must be disabled by resetting the SPE bit. The idle state of SCK must correspond to the polarity selected in the SPIx_CR1 register (by pulling up SCK if CPOL=1 or pulling down SCK if CPOL=0).

Figure 257. Data clock timing diagram

Figure 257: Data clock timing diagram showing SPI waveforms for CPHA=1 and CPHA=0. For each CPHA setting, it shows CPOL=1 and CPOL=0 clock variants, MOSI and MISO data lines with MSBit and LSBit markers, the NSS slave select line, and capture strobe pulses aligned with data sampling edges.
Figure 257: Data clock timing diagram showing SPI waveforms for CPHA=1 and CPHA=0. For each CPHA setting, it shows CPOL=1 and CPOL=0 clock variants, MOSI and MISO data lines with MSBit and LSBit markers, the NSS slave select line, and capture strobe pulses aligned with data sampling edges.

Note: The order of data bits depends on LSBFIRST bit setting.

Data frame format

The SPI shift register can be set up to shift out MSB-first or LSB-first, depending on the value of the LSBFIRST bit. Each data frame is 8 or 16 bit long depending on the size of the data programmed using the DFF bit in the SPI_CR1 register. The selected data frame format is applicable both for transmission and reception.

26.3.7 SPI configuration

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

  1. 1. Write proper GPIO registers: Configure GPIO for MOSI, MISO and SCK pins.
  2. 2. Write to the SPI_CR1 register:
    1. a) Configure the serial clock baud rate using the BR[2:0] bits ( Note: 3 ).
    2. b) Configure the CPOL and CPHA bits combination to define one of the four relationships between the data transfer and the serial clock. ( Note: 2 - except the case when CRC is enabled at TI mode).
    3. c) Select simplex or half-duplex mode by configuring RXONLY or BIDIMODE and BIDIOE (RXONLY and BIDIMODE can't be set at the same time).
    4. d) Configure the LSBFIRST bit to define the frame format ( Note: 2 ).
    5. e) Configure the CRCEN and CRCEN bits if CRC is needed (while SCK clock signal is at idle state).
    6. f) Configure SSM and SSI ( Note: 2 ).
    7. g) Configure the MSTR bit (in multimaster NSS configuration, avoid conflict state on NSS if master is configured to prevent MODF error).
    8. h) Set the DFF bit to configure the data frame format (8 or 16 bits).
  3. 3. Write to SPI_CR2 register:
    1. a) Configure SSOE ( Note: 1 & 2 ).
    2. b) Set the FRF bit if the TI protocol is required.
  4. 4. Write to SPI_CRCPR register: Configure the CRC polynomial if needed.
  5. 5. Write proper DMA registers: Configure DMA streams dedicated for SPI Tx and Rx in DMA registers if the DMA streams are used.
  1. Note:
    1. (1) Step is not required in slave mode.
    2. (2) Step is not required in TI mode.
    3. (3) The step is not required in slave mode except slave working at TI mode.

For code example, refer to A.17.1: SPI master configuration code example and A.17.2: SPI slave configuration code example .

26.3.8 Procedure for enabling SPI

It is recommended to enable the SPI slave before the master sends the clock. Otherwise, undesired data transmission might occur. The slave data register must already contain data to be sent before starting communication with the master (either on the first edge of the communication clock, or before the end of the ongoing communication if the clock signal is continuous). The SCK signal must be settled at an idle state level corresponding to the selected polarity before the SPI slave is enabled.

At full-duplex (or in any transmit-only mode), the master starts communicating when the SPI is enabled and data to be sent is written in the Tx Buffer.

In any master receive-only mode (RXONLY=1 or BIDIMODE=1 & BIDIOE=0), the master starts communicating and the clock starts running immediately after the SPI is enabled.

The slave starts communicating when it receives a correct clock signal from the master. The slave software must write the data to be sent before the SPI master initiates the transfer.

Refer to Section 26.3.11: Communication using DMA (direct memory addressing) for details on how to handle DMA.

26.3.9 Data transmission and reception procedures

Rx and Tx buffers

In reception, data are received and then stored into an internal Rx buffer while in transmission, data are first stored into an internal Tx buffer before being transmitted. A read access to the SPI_DR register returns the Rx buffered value whereas a write access to the SPI_DR stores the written data into the Tx buffer.

Tx buffer handling

The data frame is loaded from the Tx buffer into the shift register during the first bit transmission. Bits are then shifted out serially from the shift register to a dedicated output pin depending on LSBFIRST bit setting. The TXE flag (Tx buffer empty) is set when the data are transferred from the Tx buffer to the shift register. It indicates that the internal Tx buffer is ready to be loaded with the next data. An interrupt can be generated if the TXEIE bit of the SPI_CR2 register is set. Clearing the TXE bit is performed by writing to the SPI_DR register.

A continuous transmit stream can be achieved if the next data to be transmitted are stored in the Tx buffer while previous frame transmission is still ongoing. When the software writes to Tx buffer while the TXE flag is not set, the data waiting for transaction is overwritten.

Rx buffer handling

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

If a device has not cleared the RXNE bit resulting from the previous data byte transmitted, an overrun condition occurs when the next value is buffered. The OVR bit is set and an interrupt is generated if the ERRIE bit is set.

Another way to manage the data exchange is to use DMA (see Section 10.2: DMA main features ).

Sequence handling

The BSY bit is set when a current data frame transaction is ongoing. When the clock signal runs continuously, the BSY flag remains set between data frames on the master side. However, on the slave side, it becomes low for a minimum duration of one SPI clock cycle between each data frame transfer.

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

When a receive-only mode is configured on the master side, either in half-duplex (BIDIMODE=1, BIDIOE=0) or simplex configuration (BIDIMODE=0, RXONLY=1), the master starts the receive sequence as soon as the SPI is enabled. Then the clock signal is provided by the master and it does not stop until either the SPI or the receive-only mode is disabled by the master. The master receives data frames continuously up to this moment.

While the master can provide all the transactions in continuous mode (SCK signal is continuous), it has to respect slave capability to handle data flow and its content at anytime. When necessary, the master must slow down the communication and provide either a slower clock or separate frames or data sessions with sufficient delays. Be aware there is no underflow error signal for slave operating in SPI mode, and that data from the slave are always transacted and processed by the master even if the slave cannot prepare them correctly in time. It is preferable for the slave to use DMA, especially when data frames are shorter and bus rate is high.

Each sequence must be encased by the NSS pulse in parallel with the multislave system to select just one of the slaves for communication. In single slave systems, using NSS to control the slave is not necessary. However, the NSS pulse can be used to synchronize the slave with the beginning of each data transfer sequence. NSS can be managed either by software or by hardware (see Section 26.3.4: Multi-master communication ).

Refer to Figure 258 and Figure 259 for a description of continuous transfers in master / full-duplex and slave full-duplex mode.

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

Timing diagram for SPI master full-duplex mode. Shows SCK, MISO/MOSI (out), TXE flag, Tx buffer, BSY flag, MISO/MOSI (in), RXNE flag, and Rx buffer. Three data frames are shown: DATA1 (0xF1 out, 0xA1 in), DATA2 (0xF2 out, 0xA2 in), and DATA3 (0xF3 out, 0xA3 in). Software actions are indicated: write 0xF1, wait TXE=1 then write 0xF2, wait RXNE=1 then read 0xA1, wait TXE=1 then write 0xF3, wait RXNE=1 then read 0xA2, and finally wait RXNE=1 then read 0xA3.

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

The diagram illustrates the timing and control signals for a master SPI device in full-duplex mode. The SCK signal is a continuous square wave. The MISO/MOSI (out) line transmits data bytes b0-b7 for DATA1=0xF1, DATA2=0xF2, and DATA3=0xF3. The TXE flag is set by hardware when the Tx buffer is empty and cleared by software when the buffer is written. The BSY flag is set by hardware when the SPI is active and reset by hardware when it is inactive. The MISO/MOSI (in) line receives data bytes b0-b7 for 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 buffer is read. The Tx buffer (write SPI_DR) and Rx buffer (read SPI_DR) are shown with their respective data values. The software sequence is: write 0xF1 into SPI_DR, wait until TXE=1 and write 0xF2 into SPI_DR, wait until RXNE=1 and read 0xA1 from SPI_DR, wait until TXE=1 and write 0xF3 into SPI_DR, wait until RXNE=1 and read 0xA2 from SPI_DR, wait until RXNE=1 and read 0xA3 from SPI_DR.

ai17343

Timing diagram for SPI master full-duplex mode. Shows SCK, MISO/MOSI (out), TXE flag, Tx buffer, BSY flag, MISO/MOSI (in), RXNE flag, and Rx buffer. Three data frames are shown: DATA1 (0xF1 out, 0xA1 in), DATA2 (0xF2 out, 0xA2 in), and DATA3 (0xF3 out, 0xA3 in). Software actions are indicated: write 0xF1, wait TXE=1 then write 0xF2, wait RXNE=1 then read 0xA1, wait TXE=1 then write 0xF3, wait RXNE=1 then read 0xA2, and finally wait RXNE=1 then read 0xA3.

For code example, refer to A.17.3: SPI full duplex communication code example .

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

Timing diagram showing SPI slave mode behavior with continuous transfers. It displays SCK, MISO/MOSI (out), TXE flag, Tx buffer, BSY flag, MISO/MOSI (in), RXNE flag, and Rx buffer over time. The diagram shows three data transfers: 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 Tx buffer and Rx buffer are shown with their respective data values. The software sequence is: write 0xF1 into SPI_DR, wait until TXE=1 and write 0xF2 into SPI_DR, wait until RXNE=1 and read 0xA1 from SPI_DR, wait until TXE=1 and write 0xF3 into SPI_DR, wait until RXNE=1 and read 0xA2 from SPI_DR, wait until RXNE=1 and read 0xA3 from SPI_DR.

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

The diagram illustrates the timing and state changes for an SPI slave in full-duplex mode. The SCK line is a continuous square wave. The MISO/MOSI (out) line shows the transmission of 8-bit data frames: DATA 1 = 0xF1, DATA 2 = 0xF2, and DATA 3 = 0xF3. The TXE flag is set by hardware when the Tx buffer is empty and cleared by software when the buffer is loaded. The BSY flag is set by hardware when a transfer starts and reset by hardware when the transfer is complete. The MISO/MOSI (in) line shows the reception of 8-bit data frames: DATA 1 = 0xA1, DATA 2 = 0xA2, and DATA 3 = 0xA3. The RXNE flag is set by hardware when the Rx buffer is full and cleared by software when the buffer is read. The Tx buffer and Rx buffer are shown with their respective data values. The software sequence is: write 0xF1 into SPI_DR, wait until TXE=1 and write 0xF2 into SPI_DR, wait until RXNE=1 and read 0xA1 from SPI_DR, wait until TXE=1 and write 0xF3 into SPI_DR, wait until RXNE=1 and read 0xA2 from SPI_DR, wait until RXNE=1 and read 0xA3 from SPI_DR.

ai17344

Timing diagram showing SPI slave mode behavior with continuous transfers. It displays SCK, MISO/MOSI (out), TXE flag, Tx buffer, BSY flag, MISO/MOSI (in), RXNE flag, and Rx buffer over time. The diagram shows three data transfers: 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 Tx buffer and Rx buffer are shown with their respective data values. The software sequence is: write 0xF1 into SPI_DR, wait until TXE=1 and write 0xF2 into SPI_DR, wait until RXNE=1 and read 0xA1 from SPI_DR, wait until TXE=1 and write 0xF3 into SPI_DR, wait until RXNE=1 and read 0xA2 from SPI_DR, wait until RXNE=1 and read 0xA3 from SPI_DR.

26.3.10 Procedure for disabling the SPI

When SPI is disabled, it is mandatory to follow the disable procedures described in this paragraph. It is important to do this before the system enters a low-power mode when the peripheral clock is stopped. Ongoing transactions can be corrupted in this case. In some modes the disable procedure is the only way to stop continuous communication running.

Master in full-duplex or transmit only mode can finish any transaction when it stops providing data for transmission. In this case, the clock stops after the last data transaction.

Standard disable procedure is based on pulling BSY status together with TXE flag to check if a transmission session is fully completed. This check can be done in specific cases, too, when it is necessary to identify the end of ongoing transactions, for example:

The correct disable procedure is (except when receive-only mode is used):

  1. 1. Wait until RXNE=1 to receive the last data.
  2. 2. Wait until TXE=1 and then wait until BSY=0 before disabling the SPI.
  3. 3. Read received data.

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

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

  1. 1. Interrupt the receive flow by disabling SPI (SPE=0) in the specific time window while the last data frame is ongoing.
  2. 2. Wait until BSY=0 (the last data frame is processed).
  3. 3. Read received data.

Note: To stop a continuous receive sequence, a specific time window must be respected during the reception of the last data frame. It starts when the first bit is sampled and ends before the last bit transfer starts.

26.3.11 Communication using DMA (direct memory addressing)

To operate at its maximum speed and to facilitate the data register read/write process required to avoid overrun, the SPI features a DMA capability, which implements a simple request/acknowledge protocol.

A DMA access is requested when the TXE or RXNE enable bit in the SPIx_CR2 register is set. Separate requests must be issued to the Tx and Rx buffers.

Refer to Figure 260 and Figure 261 for a description of the DMA transmission and reception waveforms.

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

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

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

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

For code example, refer to A.17.4: SPI master configuration with DMA code example and A.17.5: SPI slave configuration with DMA code example .

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

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

Figure 260. Transmission using DMA

Timing diagram for SPI transmission using DMA. It shows the relationship between SCK, MISO/MOSI (out), TXE flag, BSY flag, DMA request, Tx buffer, DMA writes to SPI_DR, and DMA TCIF flag over three data items: DATA 1 = 0xF1, DATA 2 = 0xF2, and DATA 3 = 0xF3. Each data item is transmitted as 8 bits (b0 to b7). The diagram includes annotations for flag states (set by hardware, cleared by DMA write, clear by software) and software actions (configures DMA, writes data, waits for flags, handles complete transfer).

Example with CPOL=1, CPHA=1

The diagram illustrates the timing for SPI transmission using DMA for three data items: DATA 1 = 0xF1, DATA 2 = 0xF2, and DATA 3 = 0xF3. Each data item is transmitted as 8 bits (b0 to b7).

Software actions and timing:

ai17349

Timing diagram for SPI transmission using DMA. It shows the relationship between SCK, MISO/MOSI (out), TXE flag, BSY flag, DMA request, Tx buffer, DMA writes to SPI_DR, and DMA TCIF flag over three data items: DATA 1 = 0xF1, DATA 2 = 0xF2, and DATA 3 = 0xF3. Each data item is transmitted as 8 bits (b0 to b7). The diagram includes annotations for flag states (set by hardware, cleared by DMA write, clear by software) and software actions (configures DMA, writes data, waits for flags, handles complete transfer).

Figure 261. Reception using DMA

Timing diagram for SPI reception using DMA. It shows the SCK, MISO/MOSI (in), RXNE flag, DMA request, Rx buffer, DMA read from SPI_DR, and flag DMA TCIF over time. The diagram illustrates the reception of three data items: DATA 1 = 0xA1, DATA 2 = 0xA2, and DATA 3 = 0xA3. The RXNE flag is set by hardware when the first byte is received and cleared by a DMA read. The DMA request is generated when the RXNE flag is set. The Rx buffer is updated with the received data. The DMA read from SPI_DR is performed by the DMA controller. The flag DMA TCIF is set by hardware when the last byte is received and cleared by software in the DMA_ISR.

Example with CPOL=1, CPHA=1

The diagram illustrates the timing and sequence of events for SPI reception using DMA. The SCK signal is a periodic square wave. The MISO/MOSI (in) signal shows the reception of three data items: DATA 1 = 0xA1, DATA 2 = 0xA2, and DATA 3 = 0xA3. Each data item is an 8-bit byte (b0 to b7). The RXNE flag is set by hardware when the first byte of a data item is received and cleared by a DMA read. The DMA request is generated when the RXNE flag is set. The Rx buffer (read from SPI_DR) is updated with the received data. The DMA read from SPI_DR is performed by the DMA controller. The flag DMA TCIF (DMA transfer complete) is set by hardware when the last byte of the third data item is received and cleared by software in the DMA_ISR.

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

DMA reads DATA1 from SPI_DR

DMA reads DATA2 from SPI_DR

DMA reads DATA3 from SPI_DR

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

ai17350

Timing diagram for SPI reception using DMA. It shows the SCK, MISO/MOSI (in), RXNE flag, DMA request, Rx buffer, DMA read from SPI_DR, and flag DMA TCIF over time. The diagram illustrates the reception of three data items: DATA 1 = 0xA1, DATA 2 = 0xA2, and DATA 3 = 0xA3. The RXNE flag is set by hardware when the first byte is received and cleared by a DMA read. The DMA request is generated when the RXNE flag is set. The Rx buffer is updated with the received data. The DMA read from SPI_DR is performed by the DMA controller. The flag DMA TCIF is set by hardware when the last byte is received and cleared by software in the DMA_ISR.

26.3.12 SPI status flags

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

Tx buffer empty flag (TXE)

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

Rx buffer not empty (RXNE)

When set, the RXNE flag indicates that there are valid received data in the Rx buffer. It is cleared by reading from the SPI_DR register.

Busy flag (BSY)

The BSY flag is set and cleared by hardware (writing to this flag has no effect).

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

The BSY flag can be used in certain modes to detect the end of a transfer, thus preventing corruption of the last transfer when the SPI peripheral clock is disabled before entering a low-power mode or an NSS pulse end is handled by software.

The BSY flag is also useful for preventing write collisions in a multimaster system.

The BSY flag is cleared under any one of the following conditions:

Note: It is recommended to use always the TXE and RXNE flags (instead of the BSY flags) to handle data transmission or reception operations.

26.3.13 SPI error flags

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

Overrun flag (OVR)

An overrun condition occurs when the master or the slave completes the reception of the next data frame while the read operation of the previous frame from the Rx buffer has not completed (case RXNE flag is set).

In this case, the content of the Rx buffer is not updated with the new data received. A read operation from the SPI_DR register returns the frame previously received. All other subsequently transmitted data are lost.

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

Mode fault (MODF)

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

Use the following software sequence to clear the MODF bit:

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

To avoid any multiple slave conflicts in a system comprising several MCUs, the NSS pin must be pulled high during the MODF bit clearing sequence. The SPE and MSTR bits can be restored to their original state after this clearing sequence. As a security, hardware does not allow the SPE and MSTR bits to be set while the MODF bit is set. In a slave device the MODF bit cannot be set except as the result of a previous multimaster conflict.

CRC error (CRCERR)

This flag is used to verify the validity of the value received when the CRCEN bit in the SPIx_CR1 register is set. The CRCERR flag in the SPIx_SR register is set if the value received in the shift register does not match the receiver SPIx_RXCRC value. The flag is cleared by the software.

TI mode frame format error (FRE)

A TI mode frame format error is detected when an NSS pulse occurs during an ongoing communication when the SPI is operating in slave mode and configured to conform to the TI mode protocol. When this error occurs, the FRE flag is set in the SPIx_SR register. The SPI is not disabled when an error occurs, the NSS pulse is ignored, and the SPI waits for the next NSS pulse before starting a new transfer. The data may be corrupted since the error detection may result in the loss of two data bytes.

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

26.4 SPI special features

26.4.1 TI mode

TI protocol in master mode

The SPI interface is compatible with the TI protocol. The FRF bit of the SPIx_CR2 register can be used to configure the SPI to be compliant with this protocol.

The clock polarity and phase are forced to conform to the TI protocol requirements whatever the values set in the SPIx_CR1 register. NSS management is also specific to the TI protocol which makes the configuration of NSS management through the SPIx_CR1 and SPIx_CR2 registers (SSM, SSI, SSOE) impossible in this case.

In slave mode, the SPI baud rate prescaler is used to control the moment when the MISO pin state changes to HiZ when the current transaction finishes (see Figure 262 ). Any baud rate can be used, making it possible to determine this moment with optimal flexibility. However, the baud rate is generally set to the external master clock baud rate. The delay for the MISO signal to become HiZ ( \( t_{\text{release}} \) ) depends on internal resynchronization and on the baud rate value set in through the BR[2:0] bits in the SPIx_CR1 register. It is given by the formula:

\[ \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}} \]

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

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

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

Figure 262 shows the SPI communication waveforms when TI mode is selected.

Figure 262. TI mode transfer

Timing diagram for TI mode transfer showing waveforms for NSS, SCK, MOSI, and MISO signals. The diagram illustrates two frames (FRAME 1 and FRAME 2) being transferred. The SCK signal is a square wave. The MOSI signal shows data being transmitted, with 'trigger' and 'sampling' points marked. The MISO signal shows data being received, with '1 or 0' and 'MSB' (Most Significant Bit) and 'LSB' (Least Significant Bit) labels. The NSS signal is active low. The diagram is labeled MS19835V2.

The diagram shows the timing for a TI mode transfer. The top signal is NSS (Slave Select), which pulses high at the start of each frame. The second signal is SCK (Serial Clock), which starts toggling after the NSS pulse. The third signal is MOSI (Master Out Slave In), showing data being transmitted; it transitions on the 'trigger' edge and is sampled on the 'sampling' edge. The fourth signal is MISO (Master In Slave Out), showing data being received, starting with a '1 or 0' bit followed by MSB and ending with LSB. The diagram is divided into FRAME 1 and FRAME 2. A release time t RELEASE is shown at the end of the second frame. The diagram is labeled MS19835V2.

Timing diagram for TI mode transfer showing waveforms for NSS, SCK, MOSI, and MISO signals. The diagram illustrates two frames (FRAME 1 and FRAME 2) being transferred. The SCK signal is a square wave. The MOSI signal shows data being transmitted, with 'trigger' and 'sampling' points marked. The MISO signal shows data being received, with '1 or 0' and 'MSB' (Most Significant Bit) and 'LSB' (Least Significant Bit) labels. The NSS signal is active low. The diagram is labeled MS19835V2.

26.4.2 CRC calculation

Two separate CRC calculators (on transmission and reception data flows) are implemented in order to check the reliability of transmitted and received data. The SPI offers CRC8 or CRC16 calculation depending on the data format selected through the DFF bit. The CRC is calculated serially using the polynomial programmed in the SPI_CRCPR register.

CRC principle

CRC calculation is enabled by setting the CRCEN bit in the SPIx_CR1 register before the SPI is enabled (SPE = 1). The CRC value is calculated using an odd programmable polynomial on each bit. The calculation is processed on the sampling clock edge defined by the CPHA and CPOL bits in the SPIx_CR1 register. The calculated CRC value is checked automatically at the end of the data block as well as for transfer managed by CPU or by the DMA. When a mismatch is detected between the CRC calculated internally on the received data and the CRC sent by the transmitter, a CRCERR flag is set to indicate a data corruption error. The right procedure for handling the CRC calculation depends on the SPI configuration and the chosen transfer management.

Note: The polynomial value should only be odd. No even values are supported.

CRC transfer managed by CPU

Communication starts and continues normally until the last data frame has to be sent or received in the SPIx_DR register. Then CRCNEXT bit has to be set in the SPIx_CR1 register to indicate that the CRC frame transaction will follow after the transaction of the currently processed data frame. The CRCNEXT bit must be set before the end of the last data frame transaction. CRC calculation is frozen during CRC transaction.

The received CRC is stored in the Rx buffer like any other data frame.

A CRC-format transaction takes one more data frame to communicate at the end of data sequence.

When the last CRC data is received, an automatic check is performed comparing the received value and the value in the SPIx_RXCRC register. Software has to check the CRCERR flag in the SPIx_SR register to determine if the data transfers were corrupted or not. Software clears the CRCERR flag by writing '0' to it.

After the CRC reception, the CRC value is stored in the Rx buffer and must be read in the SPIx_DR register in order to clear the RXNE flag.

CRC transfer managed by DMA

When SPI communication is enabled with CRC communication and DMA mode, the transmission and reception of the CRC at the end of communication is automatic (with the exception of reading CRC data in receive-only mode). The CRCNEXT bit does not have to be handled by the software. The counter for the SPI transmission DMA channel has to be set to the number of data frames to transmit excluding the CRC frame. On the receiver side, the received CRC value is handled automatically by DMA at the end of the transaction but user must take care to flush out the CRC frame received from SPI_DR as it is always loaded into it.

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

Resetting the SPIx_TXCRC and SPIx_RXCRC values

The SPIx_TXCRC and SPIx_RXCRC values are cleared automatically when CRC calculation is enabled.

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

Between a slave disabling (high level on NSS) and a new slave enabling (low level on NSS), the CRC value should be cleared on both master and slave sides to resynchronize the master and slave respective CRC calculation.

To clear the CRC, follow the below sequence:

  1. 1. Disable the SPI
  2. 2. Clear the CRCEN bit
  3. 3. Enable the CRCEN bit
  4. 4. Enable the SPI

Note: When the SPI interface is configured as a slave, the NSS internal signal needs to be kept low during transaction of the CRC phase once the CRCNEXT signal is released, (see more details at the product errata sheet).

At TI mode, despite the fact that the clock phase and clock polarity setting is fixed and independent on the SPIx_CR1 register, the corresponding setting CPOL=0 CPHA=1 has to be kept at the SPIx_CR1 register anyway if CRC is applied. In addition, the CRC calculation has to be reset between sessions by the SPI disable sequence by re-enabling the CRCEN bit described above at both master and slave sides, else the CRC calculation can be corrupted at this specific mode.

26.5 SPI interrupts

During SPI communication an interrupts can be generated by the following events:

Interrupts can be enabled and disabled separately.

Table 135. SPI interrupt requests

Interrupt eventEvent flagEnable Control bit
Transmit Tx buffer ready to be loadedTXETXEIE
Data received in Rx bufferRXNERXNEIE
Master Mode fault eventMODFERRIE
Overrun errorOVR
CRC errorCRCERR
TI frame format errorFRE

For code example, refer to A.17.6: SPI interrupt code example .

26.6 I 2 S functional description

26.6.1 I 2 S general description

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

Figure 263. I 2 S block diagram

Figure 263. I2S block diagram. This is a complex block diagram of the I2S interface. At the top, an 'Address and data bus' connects to a 'Tx buffer' and a 'Shift register'. The 'Tx buffer' is 16-bit wide and connects to a 'Communication control' block. The 'Shift register' is also 16-bit wide, with 'LSB first' input, and connects to an 'Rx buffer'. The 'Rx buffer' is 16-bit wide and connects to the 'Communication control' block. The 'Communication control' block has various status flags: BSY, OVR, MODF, CRC ERR, UDR, CH SIDE, TxE, RxNE, and FRE. Below the communication control is 'Master control logic', which is connected to several configuration registers. These registers include: I2SCFG [1:0], I2SSTD [1:0], CK POL, DATLEN [1:0], CH LEN, a row of reserved bits, I2S MOD, and I2SE. Another register contains: Bidi mode, Bidi OE, CRC EN, CRC Next, DFF, Rx only, SSM, and SSI. A third register contains: LSB First, SPE, BR2, BR1, BR0, MSTR, CPOL, and CPHA. Below the master control logic is the 'SPI baud rate generator', which is connected to the 'CK' pin and the 'I2S clock generator'. The 'I2S clock generator' is connected to the 'MCK' pin (labeled 'I2SMOD' in the diagram) and has configuration registers: MCKOE, ODD, and I2SDIV[7:0]. The 'I2S clock generator' outputs 'I2SxCLK' to the 'Communication control' block. On the left, pins are labeled: MOSI/SD, MISO, NSS/WS, CK, and MCK. The diagram also shows internal signal paths like 'I2S_CK' and 'I2SMOD'.
Figure 263. I2S block diagram. This is a complex block diagram of the I2S interface. At the top, an 'Address and data bus' connects to a 'Tx buffer' and a 'Shift register'. The 'Tx buffer' is 16-bit wide and connects to a 'Communication control' block. The 'Shift register' is also 16-bit wide, with 'LSB first' input, and connects to an 'Rx buffer'. The 'Rx buffer' is 16-bit wide and connects to the 'Communication control' block. The 'Communication control' block has various status flags: BSY, OVR, MODF, CRC ERR, UDR, CH SIDE, TxE, RxNE, and FRE. Below the communication control is 'Master control logic', which is connected to several configuration registers. These registers include: I2SCFG [1:0], I2SSTD [1:0], CK POL, DATLEN [1:0], CH LEN, a row of reserved bits, I2S MOD, and I2SE. Another register contains: Bidi mode, Bidi OE, CRC EN, CRC Next, DFF, Rx only, SSM, and SSI. A third register contains: LSB First, SPE, BR2, BR1, BR0, MSTR, CPOL, and CPHA. Below the master control logic is the 'SPI baud rate generator', which is connected to the 'CK' pin and the 'I2S clock generator'. The 'I2S clock generator' is connected to the 'MCK' pin (labeled 'I2SMOD' in the diagram) and has configuration registers: MCKOE, ODD, and I2SDIV[7:0]. The 'I2S clock generator' outputs 'I2SxCLK' to the 'Communication control' block. On the left, pins are labeled: MOSI/SD, MISO, NSS/WS, CK, and MCK. The diagram also shows internal signal paths like 'I2S_CK' and 'I2SMOD'.
  1. 1. MCK is mapped on the MISO pin.

The SPI can function as an audio I 2 S interface when the I 2 S capability is enabled (by setting the I2SMOD bit in the SPIx_I2SCFGFR register). This interface mainly uses the same pins, flags and interrupts as the SPI.

The I 2 S shares three common pins with the SPI:

An additional pin can 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 SPIx_I2SPR and the other one is a generic I 2 S configuration register SPIx_I2SCFG (audio standard, slave/master mode, data format, packet frame, clock polarity, etc.).

The SPIx_CR1 register and all CRC registers are not used in the I 2 S mode. Likewise, the SSOE bit in the SPIx_CR2 register and the MODF and CRCERR bits in the SPIx_SR are not used.

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

26.6.2 I2S full-duplex

Figure 264 shows how to perform full-duplex communications using two SPI/I2S instances. In this case, the WS and CK IOs of both SPI2S must be connected together.

For the master full-duplex mode, one of the SPI2S block must be programmed in master (I2SCFG = ‘10’ or ‘11’), and the other SPI2S block must be programmed in slave (I2SCFG = ‘00’ or ‘01’). The MCK can be generated or not, depending on the application needs.

For the slave full-duplex mode, both SPI2S blocks must be programmed in slave. One of them in the slave receiver (I2SCFG = ‘01’), and the other in the slave transmitter (I2SCFG = ‘00’). The master external device then provides the bit clock (CK) and the frame synchronization (WS).

Note that the full-duplex mode can be used for all the supported standards: I2S Philips, MSB justified, LSB justified and PCM.

For the full-duplex mode, both SPI2S instances must use the same standard, with the same parameters: I2SMOD, I2SSTD, CKPOL, PCMSYNC, DATLEN and CHLEN must contain the same value on both instances.

Figure 264. Full-duplex communication

Figure 264. Full-duplex communication. The diagram illustrates four full-duplex communication configurations between an STM32 microcontroller and an external device. The top section shows 'MASTER full-duplex configurations' where the STM32 acts as the master. The left configuration shows SPI2Sx (MASTER-TX) and SPI2Sy (SLAVE-RX) connected to an external slave device. The right configuration shows SPI2Sx (MASTER-RX) and SPI2Sy (SLAVE-TX) connected to an external slave device. The bottom section shows 'SLAVE full-duplex configurations' where the STM32 acts as the slave. The STM32's SPI2Sx (SLAVE-TX) and SPI2Sy (SLAVE-RX) are connected to an external master device. Pins are labeled MCK (O), SD (O), CK (O), WS (O), WS (I), CK (I), and SD (I). Arrows indicate data flow, with dashed lines for optional connections. A legend indicates: dashed line = Optional, pink line = Master, blue line = Slave. MSV42093V1 is noted in the bottom right.
Figure 264. Full-duplex communication. The diagram illustrates four full-duplex communication configurations between an STM32 microcontroller and an external device. The top section shows 'MASTER full-duplex configurations' where the STM32 acts as the master. The left configuration shows SPI2Sx (MASTER-TX) and SPI2Sy (SLAVE-RX) connected to an external slave device. The right configuration shows SPI2Sx (MASTER-RX) and SPI2Sy (SLAVE-TX) connected to an external slave device. The bottom section shows 'SLAVE full-duplex configurations' where the STM32 acts as the slave. The STM32's SPI2Sx (SLAVE-TX) and SPI2Sy (SLAVE-RX) are connected to an external master device. Pins are labeled MCK (O), SD (O), CK (O), WS (O), WS (I), CK (I), and SD (I). Arrows indicate data flow, with dashed lines for optional connections. A legend indicates: dashed line = Optional, pink line = Master, blue line = Slave. MSV42093V1 is noted in the bottom right.

26.6.3 Supported audio protocols

The three-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 transmission or reception. So, it is up to the software to write into the data register the appropriate value corresponding to each channel side, or to read the data from the data register and to identify the corresponding channel by checking the CHSIDE bit in the SPIx_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 SPIx_DR register or two DMA operations if the DMA is preferred for the application. For 24-bit data frame specifically, the 8 non significant 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 SPIx_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 265. I 2 S Philips protocol waveforms (16/32-bit full accuracy, CPOL = 0)

Figure 265: I2S Philips protocol waveforms (16/32-bit full accuracy, CPOL = 0). The diagram shows three signal lines: CK (clock), WS (word select), and SD (serial data). The CK line is a periodic square wave. The WS line is a horizontal line that goes low for one clock cycle before the start of the transmission. The SD line shows a sequence of bits. The first bit is labeled MSB, and the last bit of the first channel is labeled LSB. The first channel is labeled 'Channel left' and the second is 'Channel right'. The transmission and reception periods are indicated by arrows. A note indicates that the data can be 16-bit or 32-bit. The diagram is labeled MS19591V1.
Figure 265: I2S Philips protocol waveforms (16/32-bit full accuracy, CPOL = 0). The diagram shows three signal lines: CK (clock), WS (word select), and SD (serial data). The CK line is a periodic square wave. The WS line is a horizontal line that goes low for one clock cycle before the start of the transmission. The SD line shows a sequence of bits. The first bit is labeled MSB, and the last bit of the first channel is labeled LSB. The first channel is labeled 'Channel left' and the second is 'Channel right'. The transmission and reception periods are indicated by arrows. A note indicates that 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 266. I 2 S Philips standard waveforms (24-bit frame with CPOL = 0)

Figure 266: I2S Philips standard waveforms (24-bit frame with CPOL = 0). The diagram shows three signal lines: CK (clock), WS (word select), and SD (serial data). The CK line is a periodic square wave. The WS line is a horizontal line that goes low for one clock cycle before the start of the transmission. The SD line shows a sequence of bits. The first bit is labeled MSB, and the last bit of the first channel is labeled LSB. The first channel is labeled 'Channel left 32-bit' and the second is 'Channel right'. The transmission and reception periods are indicated by arrows. A note indicates that the first 24 bits are data, and the remaining 8 bits are forced to 0. The diagram is labeled MS19592V1.
Figure 266: I2S Philips standard waveforms (24-bit frame with CPOL = 0). The diagram shows three signal lines: CK (clock), WS (word select), and SD (serial data). The CK line is a periodic square wave. The WS line is a horizontal line that goes low for one clock cycle before the start of the transmission. The SD line shows a sequence of bits. The first bit is labeled MSB, and the last bit of the first channel is labeled LSB. The first channel is labeled 'Channel left 32-bit' and the second is 'Channel right'. The transmission and reception periods are indicated by arrows. A note indicates that the first 24 bits are data, and the remaining 8 bits are forced to 0. The diagram is labeled MS19592V1.

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

Figure 267. Transmitting 0x8EAA33

Figure 267: Transmitting 0x8EAA33. Diagram showing two sequential writes to the Data register. The first write is '0x8EAA'. The second write is '0x33XX'. A note explains: 'Only the 8 MSB are sent to compare the 24 bits. 8 LSBs have no meaning and can be anything.'

First write to Data register

0x8EAA

Second write to Data register

0x33XX

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

MS19593V1

Figure 267: Transmitting 0x8EAA33. Diagram showing two sequential writes to the Data register. The first write is '0x8EAA'. The second write is '0x33XX'. A note explains: 'Only the 8 MSB are sent to compare the 24 bits. 8 LSBs have no meaning and can be anything.'

Figure 268. Receiving 0x8EAA33

Figure 268: Receiving 0x8EAA33. Diagram showing two sequential reads from the Data register. The first read is '0x8EAA'. The second read is '0x33XX'. A note explains: 'Only the 8 MSB are sent to compare the 24 bits. 8 LSBs have no meaning and can be anything.'

First read to Data register

0x8EAA

Second read to Data register

0x33XX

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

MS19594V1

Figure 268: Receiving 0x8EAA33. Diagram showing two sequential reads from the Data register. The first read is '0x8EAA'. The second read is '0x33XX'. A note explains: 'Only the 8 MSB are sent to compare the 24 bits. 8 LSBs have no meaning and can be anything.'
Figure 269. I 2 S Philips standard (16-bit extended to 32-bit packet frame with CPOL = 0) Figure 269: I2S Philips standard timing diagram. Shows CK (clock), WS (word select), and SD (serial data) signals. The SD line shows a 32-bit frame for the left channel where the first 16 bits are data (MSB to LSB) and the remaining 16 bits are forced to 0. It then transitions to the right channel.

CK

WS

SD

Transmission

Reception

16-bit data

16-bit remaining 0 forced

MSB

LSB

Channel left 32-bit

Channel right

MS19599V1

Figure 269: I2S Philips standard timing diagram. Shows CK (clock), WS (word select), and SD (serial data) signals. The SD line shows a 32-bit frame for the left channel where the first 16 bits are data (MSB to LSB) and the remaining 16 bits are forced to 0. It then transitions to the right channel.

When 16-bit data frame extended to 32-bit channel frame is selected during the I 2 S configuration phase, only one access to the SPIx_DR register 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 270 is required.

Figure 270. Example of 16-bit data frame extended to 32-bit channel frame

Diagram showing a 16-bit data frame (0x76A3) extended to a 32-bit channel frame. The text 'Only one access to SPIx_DR' is present above the data value box containing 0x76A3. The diagram is labeled MS19595V1.

Only one access to SPIx_DR

0x76A3

MS19595V1

Diagram showing a 16-bit data frame (0x76A3) extended to a 32-bit channel frame. The text 'Only one access to SPIx_DR' is present above the data value box containing 0x76A3. The diagram is labeled MS19595V1.

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

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

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

MSB justified standard

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

Figure 271. 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 the relationship between the Clock (CK), Word Select (WS), and Serial Data (SD) lines. The diagram illustrates the transmission and reception of 16- or 32-bit data, with the MSB and LSB bits clearly marked. The data is divided into Channel left and Channel right. The diagram is labeled MS30100 V1.

CK

WS

SD

Transmission

Reception

16- or 32-bit data

MSB

LSB

Channel left

Channel right

MS30100 V1

Timing diagram for MSB Justified 16-bit or 32-bit full-accuracy length with CPOL = 0. It shows the relationship between the Clock (CK), Word Select (WS), and Serial Data (SD) lines. The diagram illustrates the transmission and reception of 16- or 32-bit data, with the MSB and LSB bits clearly marked. The data is divided into Channel left and Channel right. 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 272. MSB justified 24-bit frame length with CPOL = 0

Timing diagram for MSB justified 24-bit frame length with CPOL = 0. It shows the relationship between the Clock (CK), Word Select (WS), and Serial Data (SD) lines. The diagram illustrates the transmission and reception of 24-bit data, with the MSB and LSB bits clearly marked. The data is divided into Channel left 32-bit and Channel right. The diagram is labeled MS30101V1.

CK

WS

SD

Transmission

Reception

24-bit data

8-bit remaining 0 forced

MSB

LSB

Channel left 32-bit

Channel right

MS30101V1

Timing diagram for MSB justified 24-bit frame length with CPOL = 0. It shows the relationship between the Clock (CK), Word Select (WS), and Serial Data (SD) lines. The diagram illustrates the transmission and reception of 24-bit data, with the MSB and LSB bits clearly marked. The data is divided into Channel left 32-bit and Channel right. The diagram is labeled MS30101V1.

Figure 273. 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. The diagram shows three signals: CK (clock), WS (word select), and SD (serial data). CK is a periodic square wave. WS is a signal that goes high for the duration of the 32-bit frame. SD is a 32-bit data bus. The first 16 bits are labeled '16-bit data' and are transmitted/received during the first half of the frame. The remaining 16 bits are labeled '16-bit remaining 0 forced' and are transmitted/received during the second half of the frame. The first bit of the 16-bit data is labeled 'MSB' and the last bit is labeled 'LSB'. The entire 32-bit frame is labeled 'Channel left 32-bit'. The second half of the frame is labeled 'Channel right'. The diagram is labeled 'MS30102V1' in the bottom right corner.
Timing diagram for MSB justified 16-bit extended to 32-bit packet frame with CPOL = 0. The diagram shows three signals: CK (clock), WS (word select), and SD (serial data). CK is a periodic square wave. WS is a signal that goes high for the duration of the 32-bit frame. SD is a 32-bit data bus. The first 16 bits are labeled '16-bit data' and are transmitted/received during the first half of the frame. The remaining 16 bits are labeled '16-bit remaining 0 forced' and are transmitted/received during the second half of the frame. The first bit of the 16-bit data is labeled 'MSB' and the last bit is labeled 'LSB'. The entire 32-bit frame is labeled 'Channel left 32-bit'. The second half of the frame is labeled 'Channel right'. The diagram is labeled 'MS30102V1' in the bottom right corner.

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

Timing diagram for LSB justified 16-bit or 32-bit full-accuracy with CPOL = 0. The diagram shows three signals: CK (clock), WS (word select), and SD (serial data). CK is a periodic square wave. WS is a signal that goes high for the duration of the 16- or 32-bit data. SD is a data bus. The data is labeled '16- or 32-bit data'. The first bit is labeled 'MSB' and the last bit is labeled 'LSB'. The data is transmitted/received during the first half of the frame. The second half of the frame is labeled 'Channel right'. The diagram is labeled 'MS30103V1' in the bottom right corner.
Timing diagram for LSB justified 16-bit or 32-bit full-accuracy with CPOL = 0. The diagram shows three signals: CK (clock), WS (word select), and SD (serial data). CK is a periodic square wave. WS is a signal that goes high for the duration of the 16- or 32-bit data. SD is a data bus. The data is labeled '16- or 32-bit data'. The first bit is labeled 'MSB' and the last bit is labeled 'LSB'. The data is transmitted/received during the first half of the frame. The second half of the frame is labeled 'Channel right'. The diagram is labeled 'MS30103V1' in the bottom right corner.

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

Timing diagram for LSB justified 24-bit frame length with CPOL = 0. The diagram shows three signals: CK (clock), WS (word select), and SD (serial data). CK is a periodic square wave. WS is a signal that goes high for the duration of the 24-bit frame. SD is a 32-bit data bus. The first 8 bits are labeled '8-bit data 0 forced' and are transmitted/received during the first half of the frame. The remaining 24 bits are labeled '24-bit remaining' and are transmitted/received during the second half of the frame. The first bit of the 24-bit data is labeled 'MSB' and the last bit is labeled 'LSB'. The entire 32-bit frame is labeled 'Channel left 32-bit'. The second half of the frame is labeled 'Channel right'. The diagram is labeled 'MS30104V1' in the bottom right corner.
Timing diagram for LSB justified 24-bit frame length with CPOL = 0. The diagram shows three signals: CK (clock), WS (word select), and SD (serial data). CK is a periodic square wave. WS is a signal that goes high for the duration of the 24-bit frame. SD is a 32-bit data bus. The first 8 bits are labeled '8-bit data 0 forced' and are transmitted/received during the first half of the frame. The remaining 24 bits are labeled '24-bit remaining' and are transmitted/received during the second half of the frame. The first bit of the 24-bit data is labeled 'MSB' and the last bit is labeled 'LSB'. The entire 32-bit frame is labeled 'Channel left 32-bit'. The second half of the frame is labeled 'Channel right'. The diagram is labeled 'MS30104V1' in the bottom right corner.

Figure 276. Operations required to transmit 0x3478AE

Diagram showing two write operations to the Data register for transmission. The first write is 0xxx34 (conditioned by TXE=1) and the second is 0x78AE (conditioned by TXE=1). Notes indicate only the 8 LSB of the half-word are significant and the MSBs are forced to 0x00.

First write to Data register conditioned by TXE=1

0xxx34

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

Second write to Data register conditioned by TXE=1

0x78AE

MS19596V1

Diagram showing two write operations to the Data register for transmission. The first write is 0xxx34 (conditioned by TXE=1) and the second is 0x78AE (conditioned by TXE=1). Notes indicate only the 8 LSB of the half-word are significant and the MSBs are forced to 0x00.

Figure 277. Operations required to receive 0x3478AE

Diagram showing two read operations from the Data register for reception. The first read is 0xxx34 (conditioned by RXNE=1) and the second is 0x78AE (conditioned by RXNE=1). Notes indicate only the 8 LSB of the half-word are significant and the MSBs are forced to 0x00.

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

Diagram showing two read operations from the Data register for reception. The first read is 0xxx34 (conditioned by RXNE=1) and the second is 0x78AE (conditioned by RXNE=1). Notes indicate only the 8 LSB of the half-word are significant and the MSBs are forced to 0x00.

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

Timing diagram showing CK, WS, and SD signals. It illustrates the transmission and reception of a 16-bit data frame extended to a 32-bit channel frame. The first 16 bits are '16-bit data 0 forced' and the next 16 bits are '16-bit remaining'. The diagram is divided into 'Channel left 32-bit' and 'Channel right'.

CK

WS

SD

Transmission

Reception

16-bit data 0 forced

16-bit remaining

MSB

LSB

Channel left 32-bit

Channel right

MS30105V1

Timing diagram showing CK, WS, and SD signals. It illustrates the transmission and reception of a 16-bit data frame extended to a 32-bit channel frame. The first 16 bits are '16-bit data 0 forced' and the next 16 bits are '16-bit remaining'. The diagram is divided into 'Channel left 32-bit' and 'Channel right'.

When 16-bit data frame extended to 32-bit channel frame is selected during the I 2 S configuration phase, Only one access to the SPIx_DR register 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 279 is required.

Figure 279. Example of 16-bit data frame extended to 32-bit channel frame

Diagram showing a 16-bit data frame (0x76A3) being transmitted. It indicates that only one access to the SPIx-DR register is needed for the entire 32-bit channel frame.

Only one access to the SPIx-DR register

0x76A3

MS19598V1

Diagram showing a 16-bit data frame (0x76A3) being transmitted. It indicates that only one access to the SPIx-DR register is needed for the entire 32-bit channel frame.

In transmission mode, when a TXE event occurs, the application has to write the data to be transmitted (in this case 0x76A3). The 0x0000 field is transmitted first (extension on 32-bit). The TXE flag is set 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 SPIx_I2SCFGR register.

Figure 280. PCM standard waveforms (16-bit)

Timing diagram for PCM standard waveforms (16-bit). It shows four signals: CK (clock), WS (word synchronization) for short and long frames, and SD (serial data). The SD signal shows a 16-bit data frame with MSB and LSB labels. The WS long frame is asserted for 13 bits. The diagram is labeled MS30106V1.

MS30106V1

Timing diagram for PCM standard waveforms (16-bit). It shows four signals: CK (clock), WS (word synchronization) for short and long frames, and SD (serial data). The SD signal shows a 16-bit data frame with MSB and LSB labels. The WS long frame is asserted for 13 bits. The diagram is labeled MS30106V1.

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

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

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

Timing diagram for PCM standard waveforms (16-bit extended to 32-bit packet frame). It shows four signals: CK (clock), WS (word synchronization) for short and long frames, and SD (serial data). The SD signal shows a 32-bit data frame with MSB and LSB labels. The WS long frame is asserted for up to 13 bits. The diagram is labeled MS30107V1.

MS30107V1

Timing diagram for PCM standard waveforms (16-bit extended to 32-bit packet frame). It shows four signals: CK (clock), WS (word synchronization) for short and long frames, and SD (serial data). The SD signal shows a 32-bit data frame with MSB and LSB labels. The WS long frame is asserted for up to 13 bits. 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 SPIx_I2SCFGR register) even in slave mode.

26.6.4 Clock generator

The I 2 S bitrate determines the data flow 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 will be: I 2 S bitrate = 32 × 2 × f s if the packet length is 32-bit wide.

Figure 282. Audio sampling frequency definition

Figure 282: Audio sampling frequency definition. The diagram shows two consecutive data channels: '16-or 32-bit left channel' followed by '16-or 32-bit right channel'. A horizontal double-headed arrow spans from the start of the left channel to the start of the right channel, labeled '32- or 64-bits' and 'F_s'. Vertical arrows at the start of each channel are labeled 'sampling point'. Below the diagram, the legend defines 'F_s : audio sampling frequency'. Reference code MS30108V1 is in the bottom right.
Figure 282: Audio sampling frequency definition. The diagram shows two consecutive data channels: '16-or 32-bit left channel' followed by '16-or 32-bit right channel'. A horizontal double-headed arrow spans from the start of the left channel to the start of the right channel, labeled '32- or 64-bits' and 'F_s'. Vertical arrows at the start of each channel are labeled 'sampling point'. Below the diagram, the legend defines 'F_s : audio sampling frequency'. Reference code MS30108V1 is in the bottom right.

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 283 presents the communication clock architecture. The I2SxCLK clock is provided by the RCC block, refer to the RCC section for details.

Figure 283. I 2 S clock generator architecture

Figure 283: I2S clock generator architecture. A block diagram showing the clock path. Input I2SxCLK enters an '8-bit linear divider + reshaping stage'. This stage is controlled by a register containing MCKOE, ODD, and I2SDIV[7:0] bits. The output of the divider stage splits: one path goes to a 'Divider by 4' followed by a 'Div2' block, then to input 0 of a 2-to-1 multiplexer. The other path from the divider stage goes directly to input 1 of the same multiplexer. This multiplexer is controlled by the I2SMOD bit. The output of this multiplexer goes to input 0 of a second multiplexer. Input 1 of the second multiplexer comes from the 'Divider by 4' output. This second multiplexer is controlled by the MCKOE bit. The final output is CK. There is also an MCK output taken before the final stage. A CHLEN bit is also shown in the register block. Reference code MS30109V1 is in the bottom right.
Figure 283: I2S clock generator architecture. A block diagram showing the clock path. Input I2SxCLK enters an '8-bit linear divider + reshaping stage'. This stage is controlled by a register containing MCKOE, ODD, and I2SDIV[7:0] bits. The output of the divider stage splits: one path goes to a 'Divider by 4' followed by a 'Div2' block, then to input 0 of a 2-to-1 multiplexer. The other path from the divider stage goes directly to input 1 of the same multiplexer. This multiplexer is controlled by the I2SMOD bit. The output of this multiplexer goes to input 0 of a second multiplexer. Input 1 of the second multiplexer comes from the 'Divider by 4' output. This second multiplexer is controlled by the MCKOE bit. The final output is CK. There is also an MCK output taken before the final stage. A CHLEN bit is also shown in the register block. Reference code MS30109V1 is in the bottom right.

1. Where x = 2.

The audio sampling frequency may be 192 KHz, 96 kHz, 48 kHz, 44.1 kHz, 32 kHz, 22.05 kHz, 16 kHz, 11.025 kHz or 8 kHz (or any other value within this range). 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 SPIx_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 136 provides example precision values for different clock configurations.

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

Table 136. Audio-frequency precision using standard 8 MHz HSE

I2SxCLK (MHz)Data lengthI2SDIVI2SODDMCLKTarget fs(Hz)Real f s (kHz)Error
321650No960001004.1667%
323220No960001004.1667%
3216101No4800047.6190.7937%
323250No48000504.1667%
3216111No4410043.4781.4098%
323251No4410045.4543.0715%
3216151No3200032.2580.8065%
323280No3200031.252.3430%
3216221No2205022.2220.7811%
3232111No2205021.7391.4098%
3216311No1600015.8730.7937%
3232151No1600016.1290.8065%
3216451No1102510.9890.3264%
3232221No1102511.1110.7811%
3216621No800080.0000%
3232311No80007.9360.7937%
321620Yes3200031.252.3430%
323220Yes3200031.252.3430%
321630Yes2205020.8335.5170%
323230Yes2205020.8335.5170%
321640Yes1600015.6252.3428%
323240Yes1600015.6252.3428%
321651Yes1102511.3633.0715%
Table 136. Audio-frequency precision using standard 8 MHz HSE (continued)
I2SxCLK (MHz)Data lengthI2SDIVI2SODDMCLKTarget fs(Hz)Real f s (kHz)Error
323251Yes1102511.3633.0715%
321680Yes80007.8122.3428%
323280Yes80007.8122.3428%

26.6.5 I 2 S master mode

The I 2 S can be configured in master mode. 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, controlled by the MCKOE bit in the SPIx_I2SPR register.

Procedure

  1. 1. Select the I2SDIV[7:0] bits in the SPIx_I2SPR register to define the serial clock baud rate to reach the proper audio sample frequency. The ODD bit in the SPIx_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 SPIx_I2SPR register if the master clock MCK needs to be provided to the external 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 26.6.4: Clock generator ).
  3. 3. Set the I2SMOD bit in the SPIx_I2SCFGR register to activate the I 2 S functions 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 SPIx_I2SCFGR register.
  4. 4. If needed, select all the potential interrupt sources and the DMA capabilities by writing the SPIx_CR2 register.
  5. 5. The I2SE bit in SPIx_I2SCFGR register must be set.

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

Transmission sequence

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

Lets assume the first data written into the Tx buffer corresponds to the left channel data. When data are transferred from the Tx buffer to the shift register, TXE is set and data corresponding to the right channel 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 SPIx_CR2 register is set.

For more details about the write operations depending on the I 2 S Standard-mode selected, refer to Section 26.6.3: Supported audio protocols .

To ensure a continuous audio data transmission, it is mandatory to write the SPIx_DR register 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 transmission mode except for the point 3 (refer to the procedure described in Section 26.6.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 SPIx_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 SPIx_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 26.6.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 SPIx_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.

26.6.6 I 2 S slave mode

For the slave configuration, the I 2 S can be configured in transmission or reception mode.

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 SPIx_I2SCFGR register to select I 2 S mode 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 SPIx_I2SCFGR register.
  2. 2. If needed, select all the potential interrupt sources and the DMA capabilities by writing the SPIx_CR2 register.
  3. 3. The I2SE bit in SPIx_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 SPIx_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 26.6.3: Supported audio protocols .

To secure a continuous audio data transmission, it is mandatory to write the SPIx_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 SPIx_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 SPIx_CR2 register, an interrupt is generated when the UDR flag in the SPIx_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 26.6.6: I 2 S slave mode ), where the configuration should set the master reception mode using the I2SCFG[1:0] bits in the SPIx_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 SPIx_SR register is set and an interrupt is generated if the RXNEIE bit is set in the SPIx_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 the SPIx_DR register. It is sensitive to the external WS line managed by the external master component.

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

For more details about the read operations depending on the I 2 S Standard-mode selected, refer to Section 26.6.3: Supported audio protocols .

If data are received while the preceding 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 SPIx_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.

26.6.7 I 2 S 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 SPIx_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 SPIx_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 SPIx_SR is set and the ERRIE bit in SPIx_CR2 is also set, an interrupt is generated. This interrupt can be cleared by reading the SPIx_SR status register (once the interrupt source has been cleared).

26.6.8 I 2 S 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 SPIx_DR. It is available when the I2SMOD bit in the SPIx_I2SCFGR register is set. An interrupt may be generated if the

ERRIE bit in the SPIx_CR2 register is set.

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

Overrun flag (OVR)

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

In this case, the receive buffer contents are not updated with the newly received data from the transmitter device. A read operation to the SPIx_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 SPIx_DR register followed by a read access to the SPIx_SR register.

Frame error flag (FRE)

This flag can be set by hardware only if the I 2 S is configured in Slave mode. It is set if the external master is changing the WS line while the slave is not expecting this change. If the synchronization is lost, the following steps are required to recover from this state and resynchronize the external master device with the I 2 S slave device:

  1. 1. Disable the I 2 S.
  2. 2. Enable it again when the correct level is detected on the WS line (WS line is high in I 2 S mode or low for MSB- or LSB-justified or PCM modes).

Desynchronization between master and slave devices 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.

26.6.9 I 2 S interrupts

Table 137 provides the list of I 2 S interrupts.

Table 137. 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 flagFRE

26.6.10 DMA features

In I 2 S mode, the DMA works in exactly the same way as it does in SPI mode. There is no difference except that the CRC feature is not available in I 2 S mode since there is no data transfer protection system.

26.7 SPI and I 2 S registers

The peripheral registers can be accessed by half-words (16-bit) or words (32-bit). In addition, SPI_DR can be accessed by 8-bit.

Refer to Section 1.2 for a list of abbreviations used in register descriptions.

26.7.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]MSTRCPOLCPHA
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

Bit 15 BIDIMODE : Bidirectional data mode enable
This bit enables half-duplex communication using common single bidirectional data line.
Keep RXONLY bit clear when bidirectional mode is active.
0: 2-line unidirectional data mode selected
1: 1-line bidirectional data mode selected
Note: 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: In master mode, the MOSI pin is used while the MISO pin is used in slave mode.
This bit is not used in I 2 S 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 mode enable

This bit enables simplex communication using a single unidirectional line to receive data exclusively. Keep BIDIMODE bit clear when receive only mode is active.

This bit is also useful in a multislave system in which this particular slave is not accessed, the output from the accessed slave is not corrupted.

0: full-duplex (Transmit and receive)

1: Output disabled (Receive-only mode)

Note: 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: When disabling the SPI, follow the procedure described in Section 26.3.10: Procedure for disabling the SPI .

This bit is not used in I 2 S mode.

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

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

It is not used in I 2 S mode.

Bit 1 CPOL : Clock polarity

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

It is not used in I 2 S mode and SPI TI mode except the case when CRC is applied at TI mode.

Bit 0 CPHA : Clock phase

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

It is not used in I 2 S mode and SPI TI mode except the case when CRC is applied at TI mode.

26.7.2 SPI control register 2 (SPI_CR2)

Address offset: 0x04

Reset value: 0x0000

1514131211109876543210
Res.Res.Res.Res.Res.Res.Res.Res.TXEIERXNEIEERRIEFRFRes.SSOETXDMAENRXDMAEN
rwrwrwrwrwrwrw

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

Bit 7 TXEIE : Tx buffer empty interrupt enable

Bit 6 RXNEIE : RX buffer not empty interrupt enable

Bit 5 ERRIE : Error interrupt enable

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

Bit 4 FRF : Frame format

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

26.7.3 SPI status register (SPI_SR)

Address offset: 0x08

Reset value: 0x0002

1514131211109876543210
Res.Res.Res.Res.Res.Res.Res.FREBSYOVRMODFCRC
ERR
UDRCHSIDETXERXNE
rrrrrc_w0rrrr

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

Bit 8 FRE : Frame Error

0: No frame error

1: Frame error occurred.

This bit is set by hardware and cleared by software when the SPI_SR register is read.

This bit is used in SPI TI mode or in I2S mode whatever the audio protocol selected. It detects a change on NSS or WS line which takes place in slave mode at a non expected time, informing about a desynchronization between the external master device and the slave.

Bit 7 BSY : Busy flag

0: SPI (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 26.3.12: SPI status flags and Section 26.3.10: Procedure for disabling the SPI .

Bit 6 OVR : Overrun flag

0: No overrun occurred

1: Overrun occurred

This flag is set by hardware and reset by a software sequence. Refer to Section 26.3.13: SPI error flags for the software sequence.

Bit 5 MODF : Mode fault

0: No mode fault occurred

1: Mode fault occurred

This flag is set by hardware and reset by a software sequence. Refer to Section 26.4 on page 787 for the software sequence.

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 26.6.8: I 2 S 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

26.7.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 will write into the Tx buffer and a read from the data register will return the value held in the Rx buffer.

Note: These notes apply to SPI mode:

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

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

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

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

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

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

26.7.8 SPI_I 2 S configuration register (SPI_I2SCFGR)

Address offset: 0x1C

Reset value: 0x0000

1514131211109876543210
Res.Res.Res.Res.I2SMODI2SEI2SCFGPCMSY
NC
Res.I2SSTDCKPOLDATLENCHLEN
rwrwrw rwrwrw rwrwrw rwrw

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

Bit 11 I2SMOD : I2S mode selection

0: SPI mode is selected

1: I2S mode is selected

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

Bit 10 I2SE : I2S Enable

0: I 2 S peripheral is disabled

1: I 2 S peripheral is enabled

Note: This bit is not used in SPI mode.

Bits 9:8 I2SCFG : I2S configuration mode

00: Slave - transmit

01: Slave - receive

10: Master - transmit

11: Master - receive

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

It is not used in SPI mode.

Bit 7 PCMSYNC : PCM frame synchronization

0: Short frame synchronization

1: Long frame synchronization

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

It is not used in SPI mode.

Bit 6 Reserved: forced at 0 by hardware

Bits 5:4 I2SSTD : I2S standard selection

00: I 2 S Philips standard.

01: MSB justified standard (left justified)

10: LSB justified standard (right justified)

11: PCM standard

For more details on I 2 S standards, refer to Section 26.6.3 on page 793 . 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

0: I 2 S clock steady state is low level

1: I 2 S clock steady state is high level

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

00: 16-bit data length

01: 24-bit data length

10: 32-bit data length

11: Not allowed

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

This bit is not used in SPI mode.

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

0: 16-bit wide

1: 32-bit wide

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

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

26.7.9 SPI_I 2 S prescaler register (SPI_I2SPR)

Address offset: 0x20

Reset value: 0000 0010 (0x0002)

1514131211109876543210
Res.Res.Res.Res.Res.Res.MCKOEODDI2SDIV
rwrwrw

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

Bit 9 MCKOE : Master clock output enable

0: Master clock output is disabled

1: Master clock output is enabled

Note: This bit should be configured when the 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 26.6.4 on page 800 . 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 26.6.4 on page 800 . 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.

26.7.10 SPI register map

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

Table 138. SPI register map and reset values

OffsetRegister313029282726252423222120191817161514131211109876543210
0x00SPI_CR1Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.BIDIMODEBIDIOECRCENCRCNEXTDFFRXONLYSSMSSILSBFIRSTSPEBR
[2:0]
MSTRCPOLCPHA
Reset value0000000000000000
0x04SPI_CR2Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.TXEIERXNEIEERRIEFRFRes.SSOETXDMAENRXDMAEN
Reset value0000000
0x08SPI_SRRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.FREBSYOVRMODFCRCERRUDRCHSIDETXERXNE
Reset value000000010
0x0CSPI_DRRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.DR[15:0]
Reset value000000000000000
0x10SPI_CRCPRRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.CRCPOLY[15:0]
Reset value000000000000111
0x14SPI_RXCRCRRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.RxCRC[15:0]
Reset value000000000000000
0x18SPI_TXCRCRRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.TxCRC[15:0]
Reset value000000000000000
0x1CSPI_I2SCFGRRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.I2SMODI2SEI2SCFGPOMSYNCRes.I2SSTDCKPOLDATLENCHLEN
Reset value0000000000
0x20SPI_I2SPRRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.MCKOEODDI2SDIV
Reset value0000000010

Refer to Section 2.2 on page 51 for the register boundary addresses.