32. Serial peripheral interface / integrated interchip sound (SPI/I2S)

32.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 Motorola 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 integrated interchip 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.

32.2 SPI main features

32.3 I2S main features

32.4 SPI/I2S implementation

The following table describes all the SPI instances and their features embedded in the devices.

Table 207. STM32F75xxx and STM32F74xxx SPI and SPI/I2S implementation

SPI FeaturesSPI2S1SPI2S2SPI2S3SPI4SPI5SPI6
Enhanced NSSP & TI modesYesYesYesYesYesYes
Hardware CRC calculationYesYesYesYesYesYes
I 2 S supportYesYesYesNoNoNo
Data size configurablefrom 4 to 16 bitsfrom 4 to 16 bitsfrom 4 to 16 bitsfrom 4 to 16 bitsfrom 4 to 16 bitsfrom 4 to 16 bits
Rx/Tx FIFO size32 bits32 bits32 bits32 bits32 bits32 bits
Wake-up capability from Low-power SleepYesYesYesYesYesYes

32.5 SPI functional description

32.5.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 354 .

Figure 354. SPI block diagram. This block diagram illustrates the internal architecture of an SPI interface. On the left, four pins are shown: MOSI, MISO, SCK, and NSS. The MOSI and MISO pins are connected to a bidirectional shift register. Data is written to the shift register from a 'Tx FIFO' and read from the shift register to an 'Rx FIFO'. The 'Address and data bus' connects to the Rx FIFO. The shift register is connected to a 'Communication controller'. The communication controller is also connected to a 'CRC controller', a 'Baud rate generator', and 'NSS logic'. The communication controller has several control input pins: RXONLY, CPOL, CPHA, DS[0:3], and BIDIOE. The CRC controller has control input pins: CRCEN, CRCNEXT, and CRCL. The Baud rate generator has a control input pin: BR[2:0]. The communication controller has an internal connection to 'Internal NSS' logic. The NSS pin is connected to the 'NSS logic' block. The diagram is labeled MS30117V1 in the bottom right corner.

Figure 354. SPI block diagram

Figure 354. SPI block diagram. This block diagram illustrates the internal architecture of an SPI interface. On the left, four pins are shown: MOSI, MISO, SCK, and NSS. The MOSI and MISO pins are connected to a bidirectional shift register. Data is written to the shift register from a 'Tx FIFO' and read from the shift register to an 'Rx FIFO'. The 'Address and data bus' connects to the Rx FIFO. The shift register is connected to a 'Communication controller'. The communication controller is also connected to a 'CRC controller', a 'Baud rate generator', and 'NSS logic'. The communication controller has several control input pins: RXONLY, CPOL, CPHA, DS[0:3], and BIDIOE. The CRC controller has control input pins: CRCEN, CRCNEXT, and CRCL. The Baud rate generator has a control input pin: BR[2:0]. The communication controller has an internal connection to 'Internal NSS' logic. The NSS pin is connected to the 'NSS logic' block. The diagram is labeled MS30117V1 in the bottom right corner.

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

See Section 32.5.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.

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

Diagram of full-duplex single master/single slave application showing connections between Master and Slave SPI units.

The diagram illustrates a full-duplex SPI connection between a Master and a Slave. The Master unit on the left contains an 'SPI clock generator', an 'Rx shift register', and a 'Tx shift register'. The Slave unit on the right contains a 'Tx shift register' and an 'Rx shift register'. Four lines connect the Master and Slave: MISO (Master Input, Slave Output), MOSI (Master Output, Slave Input), SCK (Serial Clock), and NSS (1) (Slave Select). Arrows indicate the direction of data flow: from the Master's Tx shift register to the Slave's Rx shift register via the MOSI line, and from the Slave's Tx shift register to the Master's Rx shift register via the MISO line. The SCK line provides a synchronous clock for both units. The NSS line is shown connected between the Master and Slave. The diagram is labeled 'MSv39623V1' in the bottom right corner.

Diagram of full-duplex single master/single slave application showing connections between Master and Slave SPI units.
  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 32.5.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 356. Half-duplex single master/ single slave application

Figure 356: Half-duplex single master/ single slave application diagram. The diagram shows a Master and a Slave connected via SPI pins. 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 MOSI (Slave), MOSI (Master) to MISO (Slave) via 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, and from Slave's Tx shift register to Master's Rx shift register. The diagram is labeled MSV39624V1.
Figure 356: Half-duplex single master/ single slave application diagram. The diagram shows a Master and a Slave connected via SPI pins. 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 MOSI (Slave), MOSI (Master) to MISO (Slave) via 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, and from Slave's Tx shift register to Master's Rx shift register. 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 32.5.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_CR1 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 357. Simplex single master/single slave application (master in transmit-only/ slave in receive-only mode)

Diagram of a simplex single master/single slave application. The Master (left) has an Rx shift register, a Tx shift register, and an SPI clock generator. The Slave (right) has a Tx shift register and an Rx shift register. Connections: MISO (Master) to MISO (Slave); MOSI (Master) to MOSI (Slave) with a right-pointing arrow; SCK (Master) to SCK (Slave) with a right-pointing arrow; NSS (Master) to NSS (Slave). The Master's Tx shift register is connected to the MOSI line, and the Slave's Rx shift register is connected to the MOSI line. The Master's Rx shift register is connected to the MISO line, and the Slave's Tx shift register is connected to the MISO line. The Master's SPI clock generator is connected to the SCK line, and the Slave's SCK line is connected to the Master's SCK line. The Master's NSS line is connected to the Slave's NSS line. The diagram is labeled MSV39625V1.
Diagram of a simplex single master/single slave application. The Master (left) has an Rx shift register, a Tx shift register, and an SPI clock generator. The Slave (right) has a Tx shift register and an Rx shift register. Connections: MISO (Master) to MISO (Slave); MOSI (Master) to MOSI (Slave) with a right-pointing arrow; SCK (Master) to SCK (Slave) with a right-pointing arrow; NSS (Master) to NSS (Slave). The Master's Tx shift register is connected to the MOSI line, and the Slave's Rx shift register is connected to the MOSI line. The Master's Rx shift register is connected to the MISO line, and the Slave's Tx shift register is connected to the MISO line. The Master's SPI clock generator is connected to the SCK line, and the Slave's SCK line is connected to the Master's SCK line. The Master's NSS line is connected to the Slave's NSS line. The diagram is labeled MSV39625V1.
  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 32.5.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. OVR 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).

32.5.3 Standard multislave 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 358 ). 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 358. Master and three independent slaves

Diagram of SPI Master and three independent slaves. The Master is on the left, connected to three Slaves (Slave 1, Slave 2, Slave 3) on the right. The Master has pins NSS(1), MISO, MOSI, SCK, IO1, IO2, and IO3. The Slaves have pins MISO, MOSI, SCK, and NSS. The Master's MISO pin is connected to the Slaves' MISO pins. The Master's MOSI pin is connected to the Slaves' MOSI pins. The Master's SCK pin is connected to the Slaves' SCK pins. The Master's NSS pin is connected to the Slaves' NSS pins. The Master's IO1, IO2, and IO3 pins are connected to the Slaves' NSS pins. The Master has an internal SPI clock generator, Rx shift register, and Tx shift register. The Slaves have internal Tx shift register and Rx shift register. Arrows indicate data flow: MISO from Slave to Master, MOSI from Master to Slave, and SCK from Master to Slave. The diagram is labeled MSv39626V1.
Diagram of SPI Master and three independent slaves. The Master is on the left, connected to three Slaves (Slave 1, Slave 2, Slave 3) on the right. The Master has pins NSS(1), MISO, MOSI, SCK, IO1, IO2, and IO3. The Slaves have pins MISO, MOSI, SCK, and NSS. The Master's MISO pin is connected to the Slaves' MISO pins. The Master's MOSI pin is connected to the Slaves' MOSI pins. The Master's SCK pin is connected to the Slaves' SCK pins. The Master's NSS pin is connected to the Slaves' NSS pins. The Master's IO1, IO2, and IO3 pins are connected to the Slaves' NSS pins. The Master has an internal SPI clock generator, Rx shift register, and Tx shift register. The Slaves have internal Tx shift register and Rx shift register. Arrows indicate data flow: MISO from Slave to Master, MOSI from Master to Slave, and SCK from Master to Slave. The diagram is labeled MSv39626V1.
  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 I/O alternate function input/output section (GPIO)).

32.5.4 Multimaster communication

Unless SPI bus is not designed for a multimaster 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 359. Multimaster application

Diagram of a multimaster application showing two nodes connected via SPI lines. Each node contains an Rx (Tx) shift register, a Tx (Rx) shift register, and an SPI clock generator. The lines are labeled MISO, MOSI, SCK, GPIO, and NSS(1). Arrows indicate signal flow: MISO and MOSI are bidirectional, SCK is bidirectional, and NSS(1) is shown as a unidirectional input from the right node to the left node.

The diagram illustrates a multimaster application with two nodes. Each node contains an Rx (Tx) shift register, a Tx (Rx) shift register, and an SPI clock generator. The nodes are connected via five lines: MISO, MOSI, SCK, GPIO, and NSS (1) . MISO and MOSI lines are bidirectional. SCK is bidirectional. The NSS (1) line is shown as a unidirectional input from the right node to the left node. The left node is labeled 'Master (Slave)' and the right node is also labeled 'Master (Slave)'. The diagram is labeled MSv39628V1.

Diagram of a multimaster application showing two nodes connected via SPI lines. Each node contains an Rx (Tx) shift register, a Tx (Rx) shift register, and an SPI clock generator. The lines are labeled MISO, MOSI, SCK, GPIO, and NSS(1). Arrows indicate signal flow: MISO and MOSI are bidirectional, SCK is bidirectional, and NSS(1) is shown as a unidirectional input from the right node to the left node.
  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.

32.5.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 360. Hardware/software slave select management

Figure 360. Hardware/software slave select management diagram showing internal logic for NSS management including a multiplexer, GPIO logic, and an NSS Output Control block. It includes a table for NSS Input modes and a note about Master mode usage.

The diagram illustrates the internal logic for Slave Select (NSS) management. It features a multiplexer with inputs from the SSI control bit (input 1) and a GPIO logic block (input 0). The output of the multiplexer is the NSS Input. This input is connected to an NSS Output Control block, which also receives the SSOE control bit. The output of the NSS Output Control block is the NSS Output. The NSS pin is connected to the GPIO logic block. The diagram is divided into 'NSS external logic' and 'NSS internal logic' by a dashed line. A table is provided to show the NSS Input modes.

NSS Inp.Master modeSlave mode
VddOKNon active
VssConflictActive

(used in Master mode and NSS HW management only)

MSv35526V6

Figure 360. Hardware/software slave select management diagram showing internal logic for NSS management including a multiplexer, GPIO logic, and an NSS Output Control block. It includes a table for NSS Input modes and a note about Master mode usage.

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

Figure 361. Data clock timing diagram. The diagram illustrates SPI full-duplex transfer timing for two CPHA settings: CPHA = 1 and CPHA = 0. For each CPHA setting, two CPOL settings are shown: CPOL = 1 and CPOL = 0. The diagram includes waveforms for SCK (Serial Clock), MOSI (Master Out Slave In), MISO (Master In Slave Out), and NSS (to slave). The MOSI and MISO signals are shown as a sequence of bits, with the MSB (Most Significant Bit) and LSB (Least Significant Bit) indicated. The Capture strobe is shown as a pulse that occurs on the rising edge of SCK for CPHA = 1 and on the falling edge of SCK for CPHA = 0. The diagram is labeled 'ai17154e' in the bottom right corner.
Figure 361. Data clock timing diagram. The diagram illustrates SPI full-duplex transfer timing for two CPHA settings: CPHA = 1 and CPHA = 0. For each CPHA setting, two CPOL settings are shown: CPOL = 1 and CPOL = 0. The diagram includes waveforms for SCK (Serial Clock), MOSI (Master Out Slave In), MISO (Master In Slave Out), and NSS (to slave). The MOSI and MISO signals are shown as a sequence of bits, with the MSB (Most Significant Bit) and LSB (Least Significant Bit) indicated. The Capture strobe is shown as a pulse that occurs on the rising edge of SCK for CPHA = 1 and on the falling edge of SCK for CPHA = 0. The diagram is labeled 'ai17154e' in the bottom right corner.
  1. 1. 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. The data frame size is chosen by using the DS bits. It can be set from 4-bit up to 16-bit length and the setting applies for both transmission and reception. Whatever the selected data frame size, read access to the FIFO must be aligned with the FRXTH level. When the SPIx_DR register is accessed, data frames are always right-aligned into either a byte (if the data fits into a byte) or a half-word (see Figure 362). During communication, only bits within the data frame are clocked and transferred.

Figure 362. Data alignment when data length is not equal to 8-bit or 16-bit

Figure 362: Data alignment when data length is not equal to 8-bit or 16-bit. The diagram shows two cases for data alignment in SPI. Case 1: DS <= 8 bits (e.g., DS = 5 bit). The TX frame is 8 bits: bits 7-5 are 'XXX', bits 4-0 are the 'Data frame'. The RX frame is 8 bits: bits 7-5 are '000', bits 4-0 are the 'Data frame'. Case 2: DS > 8 bits (e.g., DS = 14 bit). The TX frame is 16 bits: bits 15-14 are 'XX', bits 13-0 are the 'Data frame'. The RX frame is 16 bits: bits 15-14 are '00', bits 13-0 are the 'Data frame'. MS19589V2

DS <= 8 bits: data is right-aligned on byte
Example: DS = 5 bit

TX: [7:5: XXX] [4:0: Data frame]

RX: [7:5: 000] [4:0: Data frame]

DS > 8 bits: data is right-aligned on 16 bit
Example: DS = 14 bit

TX: [15:14: XX] [13:0: Data frame]

RX: [15:14: 00] [13:0: Data frame]

MS19589V2

Figure 362: Data alignment when data length is not equal to 8-bit or 16-bit. The diagram shows two cases for data alignment in SPI. Case 1: DS <= 8 bits (e.g., DS = 5 bit). The TX frame is 8 bits: bits 7-5 are 'XXX', bits 4-0 are the 'Data frame'. The RX frame is 8 bits: bits 7-5 are '000', bits 4-0 are the 'Data frame'. Case 2: DS > 8 bits (e.g., DS = 14 bit). The TX frame is 16 bits: bits 15-14 are 'XX', bits 13-0 are the 'Data frame'. The RX frame is 16 bits: bits 15-14 are '00', bits 13-0 are the 'Data frame'. MS19589V2

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

32.5.7 Configuration of SPI

The configuration procedure is almost the same for master and slave. For specific mode setups, follow the dedicated sections. 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: 4).
    2. b) Configure the CPOL and CPHA bits combination to define one of the four relationships between the data transfer and the serial clock (CPHA must be cleared in NSSP mode). (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 cannot be set at the same time).
    4. d) Configure the LSBFIRST bit to define the frame format (Note: 2).
    5. e) Configure the CRCL and CRCEN bits if CRC is needed (while SCK clock signal is at idle state).
    6. f) Configure SSM and SSI (Notes: 2 & 3).
    7. g) Configure the MSTR bit (in multimaster NSS configuration, avoid conflict state on NSS if master is configured to prevent MODF error).
  3. 3. Write to SPI_CR2 register:
    1. a) Configure the DS[3:0] bits to select the data length for the transfer.
    2. b) Configure SSOE (Notes: 1 & 2 & 3).
    3. c) Set the FRF bit if the TI protocol is required (keep NSSP bit cleared in TI mode).
    4. d) Set the NSSP bit if the NSS pulse mode between two data units is required (keep CHPA and TI bits cleared in NSSP mode).
    5. e) Configure the FRXTH bit. The RXFIFO threshold must be aligned to the read access size for the SPIx_DR register.
    6. f) Initialize LDMA_TX and LDMA_RX bits if DMA is used in packed mode.
  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) Step is not required in slave mode.
    • (2) Step is not required in TI mode.
    • (3) Step is not required in NSSP mode.
    • (4) The step is not required in slave mode except slave working at TI mode

32.5.8 Procedure for enabling SPI

It is recommended to enable the SPI slave before the master sends the clock. If not, undesired data transmission might occur. The data register of the slave 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.

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

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

For handling DMA, follow the dedicated section.

32.5.9 Data transmission and reception procedures

RXFIFO and TXFIFO

All SPI data transactions pass through the 32-bit embedded FIFOs. This enables the SPI to work in a continuous flow, and prevents overruns when the data frame size is short. Each direction has its own FIFO called TXFIFO and RXFIFO. These FIFOs are used in all SPI modes except for receiver-only mode (slave or master) with CRC calculation enabled (see Section 32.5.14: CRC calculation ).

The handling of FIFOs depends on the data exchange mode (duplex, simplex), data frame format (number of bits in the frame), access size performed on the FIFO data registers (8-bit or 16-bit), and whether or not data packing is used when accessing the FIFOs (see Section 32.5.13: TI mode ).

A read access to the SPIx_DR register returns the oldest value stored in RXFIFO that has not been read yet. A write access to the SPIx_DR stores the written data in the TXFIFO at the end of a send queue. The read access must be always aligned with the RXFIFO threshold configured by the FRXTH bit in SPIx_CR2 register. FTLVL[1:0] and FRLVL[1:0] bits indicate the current occupancy level of both FIFOs.

A read access to the SPIx_DR register must be managed by the RXNE event. This event is triggered when data is stored in RXFIFO and the threshold (defined by FRXTH bit) is reached. When RXNE is cleared, RXFIFO is considered to be empty. In a similar way, write access of a data frame to be transmitted is managed by the TXE event. This event is triggered when the TXFIFO level is less than or equal to half of its capacity. Otherwise TXE is cleared and the TXFIFO is considered as full. In this way, RXFIFO can store up to four data frames, whereas TXFIFO can only store up to three when the data frame format is not greater than 8 bits. This difference prevents possible corruption of 3x 8-bit data frames already stored in the TXFIFO when software tries to write more data in 16-bit mode into TXFIFO. Both TXE and RXNE events can be polled or handled by interrupts. See Figure 364 through Figure 367 .

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

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

The BSY bit being set indicates ongoing transaction of a current data frame. When the clock signal runs continuously, the BSY flag stays set between data frames at master but becomes low for a minimum duration of one SPI clock at slave between each data frame transfer.

Sequence handling

A few data frames can be passed at single sequence to complete a message. When transmission is enabled, a sequence begins and continues while any data is present in the TXFIFO of the master. The clock signal is provided continuously by the master until TXFIFO becomes empty, then it stops waiting for additional data.

In receive-only modes, half-duplex (BIDIMODE=1, BIDIOE=0) or simplex (BIDIMODE=0, RXONLY=1) the master starts the sequence immediately when both SPI is enabled and receive-only mode is activated. The clock signal is provided by the master and it does not stop until either SPI or 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 master or slave in SPI mode, and data from the slave is always transacted and processed by the master even if the slave could not prepare it correctly in time. It is preferable for the slave to use DMA, especially when data frames are 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 a single slave system it is not necessary to control the slave with NSS, but it is often better to provide the pulse here too, to synchronize the slave with the beginning of each data sequence. NSS can be managed by both software and hardware (see Section 32.5.5: Slave select (NSS) pin management ).

When the BSY bit is set it signifies an ongoing data frame transaction. When the dedicated frame transaction is finished, the RXNE flag is raised. The last bit is just sampled and the complete data frame is stored in the RXFIFO.

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. Special care must be taken in packing mode when an odd number of data frames are transacted to prevent some dummy byte exchange (refer to Data packing section). Before the SPI is disabled in these modes, the user must follow standard disable procedure. When

the SPI is disabled at the master transmitter while a frame transaction is ongoing or next data frame is stored in TXFIFO, the SPI behavior is not guaranteed.

When the master is in any receive only mode, the only way to stop the continuous clock is to disable the peripheral by SPE=0. This must occur in specific time window within last data frame transaction just between the sampling time of its first bit and before its last bit transfer starts (in order to receive a complete number of expected data frames and to prevent any additional “dummy” data reading after the last valid data frame). Specific procedure must be followed when disabling SPI in this mode.

Data received but not read remains stored in RXFIFO when the SPI is disabled, and must be processed the next time the SPI is enabled, before starting a new sequence. To prevent having unread data, ensure that RXFIFO is empty when disabling the SPI, by using the correct disabling procedure, or by initializing all the SPI registers with a software reset via the control of a specific register dedicated to peripheral reset (see the SPIIRST bits in the RCC_APBIRSTR registers).

Standard disable procedure is based on pulling BSY status together with FTLVL[1:0] 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 FTLVL[1:0] = 00 (no more data to transmit).
  2. 2. Wait until BSY=0 (the last data frame is processed).
  3. 3. Disable the SPI (SPE=0).
  4. 4. Read data until FRLVL[1:0] = 00 (read all the received 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 data until FRLVL[1:0] = 00 (read all the received data).

Note: If packing mode is used and an odd number of data frames with a format less than or equal to 8 bits (fitting into one byte) has to be received, FRXTH must be set when FRLVL[1:0] = 01, in order to generate the RXNE event to read the last odd data frame and to keep good FIFO pointer alignment.

Data packing

When the data frame size fits into one byte (less than or equal to 8 bits), data packing is used automatically when any read or write 16-bit access is performed on the SPIx_DR register. The double data frame pattern is handled in parallel in this case. At first, the SPI operates using the pattern stored in the LSB of the accessed word, then with the other half stored in the MSB. Figure 363 provides an example of data packing mode sequence handling. Two data frames are sent after the single 16-bit access the SPIx_DR register of the transmitter. This sequence can generate just one RXNE event in the receiver if the RXFIFO threshold is set to 16 bits (FRXTH=0). The receiver then has to access both data frames by a single 16-bit read of SPIx_DR as a response to this single RXNE event. The

RxFIFO threshold setting and the following read access must be always kept aligned at the receiver side, as data can be lost if it is not in line.

A specific problem appears if an odd number of such “fit into one byte” data frames must be handled. On the transmitter side, writing the last data frame of any odd sequence with an 8-bit access to SPIx_DR is enough. The receiver has to change the Rx_FIFO threshold level for the last data frame received in the odd sequence of frames in order to generate the RXNE event.

Figure 363. Packing data in FIFO for transmission and reception

Figure 363: Packing data in FIFO for transmission and reception. The diagram illustrates the SPI communication process between a transmitter (left) and a receiver (right). On the transmitter side, a 16-bit write access to the SPIx_DR register (SPIx_DR = 0x40A) is performed at the TxE event. This data is packed into the TXFIFO, which contains two 8-bit bytes: 0x0A and 0x04. The SPI fsm & shift block then shifts these bytes onto the MOSI line. The receiver side shows the SPI fsm & shift block receiving data from the MOSI line and packing it into the RXFIFO. The RXFIFO contains two 8-bit bytes: 0x0A and 0x04. A 16-bit read access from the data register is performed at the RxNE event, reading the data as unit16_t var; var= SPI_DR /*var=0x040A*/. The diagram also shows the NSS, SCK, and MOSI lines. The SPIx_DR register is shown with two 8-bit bytes: 0x04 and 0x0A. The TXFIFO and RXFIFO are shown as 16-bit wide buffers. The SPI fsm & shift block is shown as a central processing unit. The diagram is labeled MS19590V2.

16-bit write access to data register at TxE event:
SPIx_DR= 0x40A;

16-bit read access from data register at RxNE event:
unit16_t var;
var= SPIx_DR /*var=0x040A*/

MS19590V2

Figure 363: Packing data in FIFO for transmission and reception. The diagram illustrates the SPI communication process between a transmitter (left) and a receiver (right). On the transmitter side, a 16-bit write access to the SPIx_DR register (SPIx_DR = 0x40A) is performed at the TxE event. This data is packed into the TXFIFO, which contains two 8-bit bytes: 0x0A and 0x04. The SPI fsm & shift block then shifts these bytes onto the MOSI line. The receiver side shows the SPI fsm & shift block receiving data from the MOSI line and packing it into the RXFIFO. The RXFIFO contains two 8-bit bytes: 0x0A and 0x04. A 16-bit read access from the data register is performed at the RxNE event, reading the data as unit16_t var; var= SPI_DR /*var=0x040A*/. The diagram also shows the NSS, SCK, and MOSI lines. The SPIx_DR register is shown with two 8-bit bytes: 0x04 and 0x0A. The TXFIFO and RXFIFO are shown as 16-bit wide buffers. The SPI fsm & shift block is shown as a central processing unit. The diagram is labeled MS19590V2.
  1. 1. In this example: Data size DS[3:0] is 4-bit configured, CPOL=0, CPHA=1 and LSBFIRST =0. The Data storage is always right aligned while the valid bits are performed on the bus only, the content of LSB byte goes first on the bus, the unused bits are not taken into account on the transmitter side and padded by zeros at the receiver side.

Communication using DMA (direct memory addressing)

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

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

See Figure 364 through Figure 367 .

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 FTLVL[1:0]=00 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.

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.

Packing with DMA

If the transfers are managed by DMA (TXDMAEN and RXDMAEN set in the SPIx_CR2 register) packing mode is enabled/disabled automatically depending on the PSIZE value configured for SPI TX and the SPI RX DMA channel. If the DMA channel PSIZE value is equal to 16-bit and SPI data size is less than or equal to 8-bit, then packing mode is enabled. The DMA then automatically manages the write operations to the SPIx_DR register.

If data packing mode is used and the number of data to transfer is not a multiple of two, the LDMA_TX/LDMA_RX bits must be set. The SPI then considers only one data for the transmission or reception to serve the last DMA transfer (for more details refer to Data packing on page 1087 .)

Communication diagrams

Some typical timing schemes are explained in this section. These schemes are valid no matter if the SPI events are handled by polling, interrupts or DMA. For simplicity, the LSBFIRST=0, CPOL=0 and CPHA=1 setting is used as a common assumption here. No complete configuration of DMA streams is provided.

The following numbered notes are common for Figure 364 on page 1091 through Figure 367 on page 1094 :

  1. 1. The slave starts to control MISO line as NSS is active and SPI is enabled, and is disconnected from the line when one of them is released. Sufficient time must be provided for the slave to prepare data dedicated to the master in advance before its transaction starts.
    At the master, the SPI peripheral takes control at MOSI and SCK signals (occasionally at NSS signal as well) only if SPI is enabled. If SPI is disabled the SPI peripheral is disconnected from GPIO logic, so the levels at these lines depends on GPIO setting exclusively.
  2. 2. At the master, BSY stays active between frames if the communication (clock signal) is continuous. At the slave, BSY signal always goes down for at least one clock cycle between data frames.
  3. 3. The TXE signal is cleared only if TXFIFO is full.
  4. 4. The DMA arbitration process starts just after the TXDMAEN bit is set. The TXE interrupt is generated just after the TXEIE is set. As the TXE signal is at an active level, data transfers to TxFIFO start, until TxFIFO becomes full or the DMA transfer completes.
  5. 5. If all the data to be sent can fit into TxFIFO, the DMA Tx TCIF flag can be raised even before communication on the SPI bus starts. This flag always rises before the SPI transaction is completed.
  6. 6. The CRC value for a package is calculated continuously frame by frame in the SPIx_TXCRCR and SPIx_RXCRCR registers. The CRC information is processed after the entire data package has completed, either automatically by DMA (Tx channel must be set to the number of data frames to be processed) or by SW (the user must handle CRCNEXT bit during the last data frame processing).
    While the CRC value calculated in SPIx_TXCRCR is simply sent out by transmitter, received CRC information is loaded into RxFIFO and then compared with the SPIx_RXCRCR register content (CRC error flag can be raised here if any difference). This is why the user must take care to flush this information from the FIFO, either by software reading out all the stored content of RxFIFO, or by DMA when the proper number of data frames is preset for Rx channel (number of data frames + number of CRC frames) (see the settings at the example assumption).
  7. 7. In data packed mode, TxE and RxNE events are paired and each read/write access to the FIFO is 16 bits wide until the number of data frames are even. If the TxFIFO is \( \frac{3}{4} \) full FTLVL status stays at FIFO full level. That is why the last odd data frame cannot be stored before the TxFIFO becomes \( \frac{1}{2} \) full. This frame is stored into TxFIFO with an 8-bit access either by software or automatically by DMA when LDMA_TX control is set.
  8. 8. To receive the last odd data frame in packed mode, the Rx threshold must be changed to 8-bit when the last data frame is processed, either by software setting FRXTH=1 or automatically by a DMA internal signal when LDMA_RX is set.

Figure 364. Master full-duplex communication

Timing diagram for master full-duplex communication showing signal transitions and data flow for NSS, SCK, BSY, MOSI, SPE, TXE, FTLVL, MISO, RXNE, and FRLVL lines over three data frames.

The diagram illustrates the timing for master full-duplex communication. The signals shown are:

Key events and annotations:

Timing diagram for master full-duplex communication showing signal transitions and data flow for NSS, SCK, BSY, MOSI, SPE, TXE, FTLVL, MISO, RXNE, and FRLVL lines over three data frames.

Assumptions for master full-duplex communication example:

If DMA is used:

See also : Communication diagrams on page 1090 for details about common assumptions and notes.

Figure 365. Slave full-duplex communication

Timing diagram for slave full-duplex communication showing signals NSS, SCK, BSY, MISO, SPE, TXE, FTLVL, MOSI, RXNE, and FRLVL over three data frames. It includes annotations for MSB/LSB, DMA events, and control signals.

The diagram illustrates the timing for slave full-duplex communication across three data frames. The signals shown are:

Other annotations include 'Enable Tx/Rx DMA or interrupts' and 'DMA or software control at Tx events'.

Timing diagram for slave full-duplex communication showing signals NSS, SCK, BSY, MISO, SPE, TXE, FTLVL, MOSI, RXNE, and FRLVL over three data frames. It includes annotations for MSB/LSB, DMA events, and control signals.

MSv32123V2

Assumptions for slave full-duplex communication example:

If DMA is used:

See also Communication diagrams on page 1090 for details about common assumptions and notes.

Figure 366. Master full-duplex communication with CRC

Timing diagram for master full-duplex communication with CRC. The diagram shows the relationship between NSS, SCK, BSY, MOSI, SPE, TXE, FTLVL, MISO, RXNE, and FRLVL signals over time. It illustrates the transmission of two 16-bit frames (DTx1, DTx2) with MSB and CRC on the MOSI line, and the reception of three 16-bit frames (DRx1, DRx2, DRx3) with LSB and CRC on the MISO line. DMA or software control events are shown for both transmission and reception. The diagram is numbered 1 through 6 to indicate key events.

The diagram illustrates the timing for master full-duplex communication with CRC. The signals shown are:

Key events and controls:

Other labels include "Enable Tx/Rx DMA or interrupts" and "DMA or software control at Rx events". The diagram is identified by the code MSV32124V2.

Timing diagram for master full-duplex communication with CRC. The diagram shows the relationship between NSS, SCK, BSY, MOSI, SPE, TXE, FTLVL, MISO, RXNE, and FRLVL signals over time. It illustrates the transmission of two 16-bit frames (DTx1, DTx2) with MSB and CRC on the MOSI line, and the reception of three 16-bit frames (DRx1, DRx2, DRx3) with LSB and CRC on the MISO line. DMA or software control events are shown for both transmission and reception. The diagram is numbered 1 through 6 to indicate key events.

Assumptions for master full-duplex communication with CRC example:

If DMA is used:

See also : Communication diagrams on page 1090 for details about common assumptions and notes.

Figure 367. Master full-duplex communication in packed mode

Timing diagram for master full-duplex communication in packed mode. It shows the relationship between NSS, SCK, BSY, MOSI, SPE, TXE, FTLVL, MISO, RXNE, and FRLVL signals over time. The diagram illustrates the flow of data (DTx and DRx) through the SPI/I2S interface, including DMA and software control events. Key elements include: 1. NSS (Slave Select) signal going low to start communication. 2. SCK (Serial Clock) signal generated by the master. 3. BSY (Busy) signal going high when the SPI is active. 4. MOSI (Master Out Slave In) signal showing transmitted data (DTx1-2, DTx3-4, DTx5). 5. SPE (Serial Peripheral Enable) signal going high to enable the SPI. 6. TXE (Transmit Buffer Empty) signal going high when the transmit buffer is empty, triggering DMA or software control. 7. FTLVL (Fill Level) signal showing the level of the transmit FIFO. 8. MISO (Master In Slave Out) signal showing received data (DRx1-2, DRx3-4, DRx5). 9. RXNE (Receive Buffer Not Empty) signal going high when the receive buffer is not empty, triggering DMA or software control. 10. FRLVL (Fill Level) signal showing the level of the receive FIFO. The diagram also includes annotations for 'Enable Tx/Rx DMA or interrupts', 'DMA or software control at Tx events', 'DMA or software control at Rx events', 'DMA Tx TICF', and 'DMA Rx TICF'. The data is shown in 5-bit words, and the FIFO levels are shown in 16-bit access units (00, 01, 10, 11).
Timing diagram for master full-duplex communication in packed mode. It shows the relationship between NSS, SCK, BSY, MOSI, SPE, TXE, FTLVL, MISO, RXNE, and FRLVL signals over time. The diagram illustrates the flow of data (DTx and DRx) through the SPI/I2S interface, including DMA and software control events. Key elements include: 1. NSS (Slave Select) signal going low to start communication. 2. SCK (Serial Clock) signal generated by the master. 3. BSY (Busy) signal going high when the SPI is active. 4. MOSI (Master Out Slave In) signal showing transmitted data (DTx1-2, DTx3-4, DTx5). 5. SPE (Serial Peripheral Enable) signal going high to enable the SPI. 6. TXE (Transmit Buffer Empty) signal going high when the transmit buffer is empty, triggering DMA or software control. 7. FTLVL (Fill Level) signal showing the level of the transmit FIFO. 8. MISO (Master In Slave Out) signal showing received data (DRx1-2, DRx3-4, DRx5). 9. RXNE (Receive Buffer Not Empty) signal going high when the receive buffer is not empty, triggering DMA or software control. 10. FRLVL (Fill Level) signal showing the level of the receive FIFO. The diagram also includes annotations for 'Enable Tx/Rx DMA or interrupts', 'DMA or software control at Tx events', 'DMA or software control at Rx events', 'DMA Tx TICF', and 'DMA Rx TICF'. The data is shown in 5-bit words, and the FIFO levels are shown in 16-bit access units (00, 01, 10, 11).

Assumptions for master full-duplex communication in packed mode example:

If DMA is used:

See also : Communication diagrams on page 1090 for details about common assumptions and notes.

32.5.10 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)

The TXE flag is set when transmission TXFIFO has enough space to store data to send. TXE flag is linked to the TXFIFO level. The flag goes high and stays high until the TXFIFO level is lower or equal to 1/2 of the FIFO depth. An interrupt can be generated if the TXEIE bit in the SPIx_CR2 register is set. The bit is cleared automatically when the TXFIFO level becomes greater than 1/2.

Rx buffer not empty (RXNE)

The RXNE flag is set depending on the FRXTH bit value in the SPIx_CR2 register:

An interrupt can be generated if the RXNEIE bit in the SPIx_CR2 register is set.

The RXNE is cleared by hardware automatically when the above conditions are no longer true.

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

The BSY flag can be used in certain modes to detect the end of a transfer so that the software can disable the SPI or its peripheral clock before entering a low-power mode which does not provide a clock for the peripheral. This avoids corrupting the last transfer.

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: When the next transmission can be handled immediately by the master (e.g. if the master is in Receive-only mode or its Transmit FIFO is not empty), communication is continuous and the BSY flag remains set to '1' between transfers on the master side. Although this is not the case with a slave, it is recommended to use always the TXE and RXNE flags (instead of the BSY flags) to handle data transmission or reception operations.

32.5.11 SPI error flags

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

Overrun flag (OVR)

An overrun condition occurs when data is received by a master or slave and the RXFIFO has not enough space to store this received data. This can happen if the software or the DMA did not have enough time to read the previously received data (stored in the RXFIFO) or when space for data storage is limited e.g. the RXFIFO is not available when CRC is enabled in receive only mode so in this case the reception buffer is limited into a single data frame buffer (see Section 32.5.14: CRC calculation ).

When an overrun condition occurs, the newly received value does not overwrite the previous one in the RXFIFO. The newly received value is discarded and all data transmitted subsequently is 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_RXCRCR 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 ERRRIE 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 reinitiated by the master when the slave SPI is enabled again.

32.5.12 NSS pulse mode

This mode is activated by the NSSP bit in the SPIx_CR2 register and it takes effect only if the SPI interface is configured as Motorola SPI master (FRF=0) with capture on the first edge (SPIx_CR1 CPHA = 0, CPOL setting is ignored). When activated, an NSS pulse is generated between two consecutive data frame transfers when NSS stays at high level for the duration of one clock period at least. This mode allows the slave to latch data. NSSP pulse mode is designed for applications with a single master-slave pair.

Figure 368 illustrates NSS pin management when NSSP pulse mode is enabled.

Figure 368. NSSP pulse generation in Motorola SPI master mode

Timing diagram for NSSP pulse generation in Motorola SPI master mode. The diagram shows four waveforms: NSS output, SCK output, MOSI output, and MISO input. The NSS output is high before the first SCK edge and goes low at the first SCK edge, returning high after the last SCK edge of a frame. The SCK output is a continuous square wave. The MOSI output shows data frames (MSB and LSB) being transmitted. The MISO input shows data frames being received, with 'Do not care' periods at the beginning and end of each frame. Sampling edges are marked on the SCK waveform. The duration of a data frame is indicated as 4-bits to 16-bits. The diagram is titled 'Master continuous transfer (CPOL = 1; CPHA = 0; NSSP= 1)'.
Timing diagram for NSSP pulse generation in Motorola SPI master mode. The diagram shows four waveforms: NSS output, SCK output, MOSI output, and MISO input. The NSS output is high before the first SCK edge and goes low at the first SCK edge, returning high after the last SCK edge of a frame. The SCK output is a continuous square wave. The MOSI output shows data frames (MSB and LSB) being transmitted. The MISO input shows data frames being received, with 'Do not care' periods at the beginning and end of each frame. Sampling edges are marked on the SCK waveform. The duration of a data frame is indicated as 4-bits to 16-bits. The diagram is titled 'Master continuous transfer (CPOL = 1; CPHA = 0; NSSP= 1)'.

Note: Similar behavior is encountered when CPOL = 0. In this case the sampling edge is the rising edge of SCK, and NSS assertion and deassertion refer to this sampling edge.

32.5.13 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 369). 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.

If the data size is equal to 4-bits or 5-bits, the master in full-duplex mode or transmit-only mode uses a protocol with one more dummy data bit added after LSB. TI NSS pulse is generated above this dummy bit clock cycle instead of the LSB in each period.

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

Figure 369: TI mode transfer shows the SPI communication waveforms when TI mode is selected.

Figure 369. 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) of data transfer. The SCK signal is a square wave. The MOSI signal shows data bits (MSB, LSB) being transmitted. The MISO signal shows data bits (1 or 0, MSB, LSB) being received. The NSS signal is a pulse that goes low to start a frame and high to end it. The timing parameters trigger, sampling, and t_RELEASE are indicated.

The diagram shows the timing for TI mode transfer. The NSS signal is a pulse that goes low to start a frame and high to end it. The SCK signal is a square wave. The MOSI signal shows data bits (MSB, LSB) being transmitted. The MISO signal shows data bits (1 or 0, MSB, LSB) being received. The timing parameters trigger, sampling, and t_RELEASE are indicated. 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) of data transfer. The SCK signal is a square wave. The MOSI signal shows data bits (MSB, LSB) being transmitted. The MISO signal shows data bits (1 or 0, MSB, LSB) being received. The NSS signal is a pulse that goes low to start a frame and high to end it. The timing parameters trigger, sampling, and t_RELEASE are indicated.

32.5.14 CRC calculation

Two separate CRC calculators are implemented in order to check the reliability of transmitted and received data. The SPI offers CRC8 or CRC16 calculation independently of the frame data length, which can be fixed to 8-bit or 16-bit. For all the other data frame lengths, no CRC is available.

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 follows 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 RXFIFO like a data byte or word. That is why in CRC mode only, the reception buffer has to be considered as a single 16-bit buffer used to receive only one data frame at a time.

A CRC-format transaction usually takes one more data frame to communicate at the end of data sequence. However, when setting an 8-bit data frame checked by 16-bit CRC, two more frames are necessary to send the complete CRC.

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 RXFIFO 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 received CRC information from RXFIFO as it is always loaded into it. In full-duplex mode, the counter of the reception DMA channel can be set to the number of data frames to receive including the CRC, which means, for example, in the specific case of an 8-bit data frame checked by 16-bit CRC:

\[ \text{DMA\_RX} = \text{Numb\_of\_data} + 2 \]

In receive only mode, the DMA reception channel counter should contain only the amount of data transferred, excluding the CRC calculation. Then based on the complete transfer from DMA, all the CRC values must be read back by software from FIFO as it works as a single buffer in this mode.

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.

If packing mode is used, the LDMA_RX bit needs managing if the number of data is odd.

Resetting the SPIx_TXCRC and SPIx_RXCRC values

The SPIx_TXCRC and SPIx_RXCRC values are cleared automatically when new data is sampled after a CRC phase. This allows the use of DMA circular mode (not available in receive-only mode) in order to transfer data without any interruption, (several data blocks covered by intermediate CRC checking phases).

If the SPI is disabled during a communication the following sequence must be followed:

  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. That is why the CRC calculation cannot be used at NSS Pulse mode when NSS hardware mode should be applied at slave normally.

At TI mode, despite the fact that clock phase and clock polarity setting is fixed and independent on 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 SPI disable sequence with re-enable the CRCEN bit described above at both master and slave side, else CRC calculation can be corrupted at this specific mode.

32.6 SPI interrupts

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

Interrupts can be enabled and disabled separately.

Table 208. SPI interrupt requests

Interrupt eventEvent flagEnable Control bit
Transmit TXFIFO ready to be loadedTXETXEIE
Data received in RXFIFORXNERXNEIE
Master Mode fault eventMODFERRIE
Overrun errorOVR
TI frame format errorFRE
CRC protocol errorCRCERR

32.7 I2S functional description

32.7.1 I2S general description

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

Figure 370. I2S block diagram

Figure 370. I2S block diagram. This is a detailed block diagram of the I2S interface within an STMicroelectronics microcontroller. It shows the internal architecture including the Tx buffer, Shift register, Rx buffer, Master control logic, SPI baud rate generator, and I2S clock generator. The diagram is connected to external pins: MOSI/SD, MISO, NSS/WS, CK, I2S_CK, and MCK. The I2S_CK pin is connected to the I2S clock generator. The MCK pin is connected to the I2S clock generator via a MCKOE bit. The MOSI/SD and MISO pins are connected to the SPI baud rate generator and the Master control logic. The NSS/WS pin is connected to the Master control logic. The CK pin is connected to the SPI baud rate generator. The diagram also shows various control registers and flags: BSY, OVR, MODF, CRC ERR, UDR, CH SIDE, TxE, RxNE, FRE, I2SCFG [1:0], I2SSTD [1:0], CK POL, DATLEN [1:0], CH LEN, I2S MOD, I2S SE, Bidi mode, Bidi OE, CRC EN, CRC Next, DFF, Rx only, SSM, SSI, LSB First, SPE, BR2, BR1, BR0, MSTR, CPOL, CPHA, MCKOE, ODD, I2SDIV[7:0]. The Address and data bus is shown at the top, connected to the Tx buffer and Master control logic. The Shift register is labeled '16-bit' and 'LSB first'. The Rx buffer is also labeled '16-bit'. The I2S clock generator is connected to the I2S_CK pin and the I2S clock generator. The SPI baud rate generator is connected to the CK pin and the Master control logic. The Master control logic is connected to the Tx buffer, Shift register, Rx buffer, SPI baud rate generator, and I2S clock generator. The diagram is labeled MS32126V1 in the bottom right corner.
Figure 370. I2S block diagram. This is a detailed block diagram of the I2S interface within an STMicroelectronics microcontroller. It shows the internal architecture including the Tx buffer, Shift register, Rx buffer, Master control logic, SPI baud rate generator, and I2S clock generator. The diagram is connected to external pins: MOSI/SD, MISO, NSS/WS, CK, I2S_CK, and MCK. The I2S_CK pin is connected to the I2S clock generator. The MCK pin is connected to the I2S clock generator via a MCKOE bit. The MOSI/SD and MISO pins are connected to the SPI baud rate generator and the Master control logic. The NSS/WS pin is connected to the Master control logic. The CK pin is connected to the SPI baud rate generator. The diagram also shows various control registers and flags: BSY, OVR, MODF, CRC ERR, UDR, CH SIDE, TxE, RxNE, FRE, I2SCFG [1:0], I2SSTD [1:0], CK POL, DATLEN [1:0], CH LEN, I2S MOD, I2S SE, Bidi mode, Bidi OE, CRC EN, CRC Next, DFF, Rx only, SSM, SSI, LSB First, SPE, BR2, BR1, BR0, MSTR, CPOL, CPHA, MCKOE, ODD, I2SDIV[7:0]. The Address and data bus is shown at the top, connected to the Tx buffer and Master control logic. The Shift register is labeled '16-bit' and 'LSB first'. The Rx buffer is also labeled '16-bit'. The I2S clock generator is connected to the I2S_CK pin and the I2S clock generator. The SPI baud rate generator is connected to the CK pin and the Master control logic. The Master control logic is connected to the Tx buffer, Shift register, Rx buffer, SPI baud rate generator, and I2S clock generator. The diagram is labeled MS32126V1 in the bottom right corner.
  1. 1. MCK is mapped on the MISO pin.

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

The I2S 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 I2S uses its own clock generator to produce the communication clock when it is set in master mode. This clock generator is also the source of the master clock output. Two additional registers are available in I 2 S mode. One is linked to the clock generator configuration SPIx_I2SPR and the other one is a generic I2S 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 I2S uses the same SPI register for data transfer (SPIx_DR) in 16-bit wide mode.

32.7.2 I2S full duplex

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

For the master full-duplex mode, one of the SPI/I2S block must be programmed in master (I2SCFG = ‘10’ or ‘11’), and the other SPI/I2S 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 SPI/I2S 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: I 2 S Philips, MSB-justified, LSB-justified and PCM.

For the full-duplex mode, both SPI/I2S 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 371. Full-duplex communication

Figure 371. 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 the SPI/I2Sx (MASTER-TX) and SPI/I2Sy (SLAVE-RX) blocks connected to an external slave device via MCK (O), SD (O), CK (O), and WS (O) pins. The right configuration shows the SPI/I2Sx (MASTER-RX) and SPI/I2Sy (SLAVE-TX) blocks connected to an external slave device via MCK (O), SD (I), CK (O), and WS (O) pins. The bottom section shows 'SLAVE full-duplex configurations' where the STM32 acts as the slave. The SPI/I2Sx (SLAVE-TX) and SPI/I2Sy (SLAVE-RX) blocks are connected to an external master device via SD (O), CK (I), WS (I), and WS (I) pins. A legend indicates that dashed lines represent optional connections, pink lines represent master connections, and blue lines represent slave connections. The diagram is labeled MSv42093V2.
Figure 371. 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 the SPI/I2Sx (MASTER-TX) and SPI/I2Sy (SLAVE-RX) blocks connected to an external slave device via MCK (O), SD (O), CK (O), and WS (O) pins. The right configuration shows the SPI/I2Sx (MASTER-RX) and SPI/I2Sy (SLAVE-TX) blocks connected to an external slave device via MCK (O), SD (I), CK (O), and WS (O) pins. The bottom section shows 'SLAVE full-duplex configurations' where the STM32 acts as the slave. The SPI/I2Sx (SLAVE-TX) and SPI/I2Sy (SLAVE-RX) blocks are connected to an external master device via SD (O), CK (I), WS (I), and WS (I) pins. A legend indicates that dashed lines represent optional connections, pink lines represent master connections, and blue lines represent slave connections. The diagram is labeled MSv42093V2.

32.7.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 372. I 2 S Philips protocol waveforms (16/32-bit full accuracy)

Timing diagram for I2S Philips standard (16/32-bit full accuracy). It shows three signals: CK (clock), WS (word select), and SD (serial data). The CK signal is a periodic square wave. The WS signal is a pulse that goes low for one clock cycle before the start of each channel's data. The SD signal shows a sequence of bits, with the first bit labeled MSB and the last bit of a channel labeled LSB. The diagram is divided into 'transmission' and 'reception' phases. A horizontal arrow indicates the data length 'Can be 16-bit or 32-bit'. Below the SD line, two arrows indicate 'Channel left' and 'Channel right' data streams. The diagram is labeled MS19591V1.
Timing diagram for I2S Philips standard (16/32-bit full accuracy). It shows three signals: CK (clock), WS (word select), and SD (serial data). The CK signal is a periodic square wave. The WS signal is a pulse that goes low for one clock cycle before the start of each channel's data. The SD signal shows a sequence of bits, with the first bit labeled MSB and the last bit of a channel labeled LSB. The diagram is divided into 'transmission' and 'reception' phases. A horizontal arrow indicates the data length 'Can be 16-bit or 32-bit'. Below the SD line, two arrows indicate 'Channel left' and 'Channel right' data streams. 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 373. I 2 S Philips standard waveforms (24-bit frame)

Timing diagram for I2S Philips standard waveforms (24-bit frame). It shows the same CK, WS, and SD signals. The SD signal is divided into two parts: '24-bit data' and '8-bit remaining 0 forced'. The first part starts at the MSB and ends at the LSB. The second part is shown as a continuation of the SD line. Below the SD line, an arrow labeled 'Channel left 32-bit' spans the entire 32-bit period (24 bits of data + 8 bits of zeros). Another arrow labeled 'Channel right' starts at the beginning of the next channel's data. The diagram is labeled MS19592V1.
Timing diagram for I2S Philips standard waveforms (24-bit frame). It shows the same CK, WS, and SD signals. The SD signal is divided into two parts: '24-bit data' and '8-bit remaining 0 forced'. The first part starts at the MSB and ends at the LSB. The second part is shown as a continuation of the SD line. Below the SD line, an arrow labeled 'Channel left 32-bit' spans the entire 32-bit period (24 bits of data + 8 bits of zeros). Another arrow labeled 'Channel right' starts at the beginning of the next channel's data. The diagram is labeled MS19592V1.

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

Figure 374. Transmitting 0x8EAA33

Figure 374: Transmitting 0x8EAA33. Diagram showing two writes to the Data register. The first write is 0x8EAA and the second is 0x33XX. A note indicates that only the 8 MSBs are sent to compare the 24 bits, and the 8 LSBs have no meaning and can be anything. MS19593V2

First write to Data register

0x8EAA

Second write to Data register

0x33XX

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

MS19593V2

Figure 374: Transmitting 0x8EAA33. Diagram showing two writes to the Data register. The first write is 0x8EAA and the second is 0x33XX. A note indicates that only the 8 MSBs are sent to compare the 24 bits, and the 8 LSBs have no meaning and can be anything. MS19593V2

Figure 375. Receiving 0x8EAA33

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

First read to Data register

0x8EAA

Second read to Data register

0x33XX

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

MS19594V1

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

CK

WS

SD

Transmission

Reception

16-bit data

MSB

LSB

16-bit remaining 0 forced

Channel left 32-bit

Channel right

MS19599V1

Figure 376: I2S Philips standard (16-bit extended to 32-bit packet frame). Timing diagram showing CK, WS, and SD signals. The SD signal is divided into 'Transmission' and 'Reception' phases. The 'Transmission' phase shows a 16-bit data frame (MSB to LSB) followed by '16-bit remaining 0 forced'. The 'Reception' phase shows a 16-bit data frame (MSB to LSB) followed by '16-bit remaining 0 forced'. The entire 32-bit frame is labeled 'Channel left 32-bit' and 'Channel right'. MS19599V1

When 16-bit data frame extended to 32-bit channel frame is selected during the I2S 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 377 is required.

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

Figure 377: Example of 16-bit data frame extended to 32-bit channel frame. Diagram showing a single write to the SPIx_DR register with the value 0x76A3. A note indicates that only one access to SPIx_DR is required. MS19595V1

Only one access to SPIx_DR

0x76A3

MS19595V1

Figure 377: Example of 16-bit data frame extended to 32-bit channel frame. Diagram showing a single write to the SPIx_DR register with the value 0x76A3. A note indicates that only one access to SPIx_DR is required. 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 MSB.

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

Timing diagram for MSB Justified 16-bit or 32-bit full-accuracy length. It shows three waveforms: CK (clock), WS (word select), and SD (serial data). The CK signal is a periodic square wave. The WS signal is a pulse that goes high at the start of the transmission and low at the start of the reception. The SD signal shows a sequence of bits, with the first bit labeled MSB and the last bit labeled LSB. The data is divided into two channels: Channel left and Channel right. The diagram indicates that the data is 16- or 32-bit. The label MS30100 V1 is in the bottom right corner.
Timing diagram for MSB Justified 16-bit or 32-bit full-accuracy length. It shows three waveforms: CK (clock), WS (word select), and SD (serial data). The CK signal is a periodic square wave. The WS signal is a pulse that goes high at the start of the transmission and low at the start of the reception. The SD signal shows a sequence of bits, with the first bit labeled MSB and the last bit labeled LSB. The data is divided into two channels: Channel left and Channel right. The diagram indicates that the data is 16- or 32-bit. The label MS30100 V1 is in the bottom right corner.

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

Figure 379. MSB justified 24-bit frame length

Timing diagram for MSB justified 24-bit frame length. It shows three waveforms: CK (clock), WS (word select), and SD (serial data). The CK signal is a periodic square wave. The WS signal is a pulse that goes high at the start of the transmission and low at the start of the reception. The SD signal shows a sequence of bits, with the first bit labeled MSB and the last bit labeled LSB. The data is divided into two channels: Channel left 32-bit and Channel right. The diagram indicates that the data is 24-bit, with the remaining 8 bits being 0 forced. The label MS30101V1 is in the bottom right corner.
Timing diagram for MSB justified 24-bit frame length. It shows three waveforms: CK (clock), WS (word select), and SD (serial data). The CK signal is a periodic square wave. The WS signal is a pulse that goes high at the start of the transmission and low at the start of the reception. The SD signal shows a sequence of bits, with the first bit labeled MSB and the last bit labeled LSB. The data is divided into two channels: Channel left 32-bit and Channel right. The diagram indicates that the data is 24-bit, with the remaining 8 bits being 0 forced. The label MS30101V1 is in the bottom right corner.

Figure 380. MSB justified 16-bit extended to 32-bit packet frame

Timing diagram for MSB justified 16-bit extended to 32-bit packet frame. It shows three waveforms: CK (clock), WS (word select), and SD (serial data). CK is a periodic square wave. WS is a pulse that goes high during the transmission phase and low during the reception phase. SD shows data being transmitted and received. The transmission phase consists of a 16-bit data segment (MSB to LSB) followed by a 16-bit remaining segment where all bits are forced to 0. This 32-bit sequence is labeled 'Channel left 32-bit'. The reception phase shows 'Channel right' data. The diagram is labeled MS30102V1.
Timing diagram for MSB justified 16-bit extended to 32-bit packet frame. It shows three waveforms: CK (clock), WS (word select), and SD (serial data). CK is a periodic square wave. WS is a pulse that goes high during the transmission phase and low during the reception phase. SD shows data being transmitted and received. The transmission phase consists of a 16-bit data segment (MSB to LSB) followed by a 16-bit remaining segment where all bits are forced to 0. This 32-bit sequence is labeled 'Channel left 32-bit'. The reception phase shows 'Channel right' data. The diagram is labeled MS30102V1.

LSB justified standard

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

The sampling of the input and output signals is the same as for the I 2 S Philips standard.

Figure 381. LSB justified 16-bit or 32-bit full-accuracy

Timing diagram for LSB justified 16-bit or 32-bit full-accuracy. It shows three waveforms: CK (clock), WS (word select), and SD (serial data). CK is a periodic square wave. WS is a pulse that goes high during the transmission phase and low during the reception phase. SD shows data being transmitted and received. The transmission phase consists of a 16- or 32-bit data segment (MSB to LSB). This is followed by another segment labeled 'Channel right'. The diagram is labeled MS30103V1.
Timing diagram for LSB justified 16-bit or 32-bit full-accuracy. It shows three waveforms: CK (clock), WS (word select), and SD (serial data). CK is a periodic square wave. WS is a pulse that goes high during the transmission phase and low during the reception phase. SD shows data being transmitted and received. The transmission phase consists of a 16- or 32-bit data segment (MSB to LSB). This is followed by another segment labeled 'Channel right'. The diagram is labeled MS30103V1.

Figure 382. LSB justified 24-bit frame length

Timing diagram for LSB justified 24-bit frame length. It shows three waveforms: CK (clock), WS (word select), and SD (serial data). CK is a periodic square wave. WS is a pulse that goes high during the transmission phase and low during the reception phase. SD shows data being transmitted and received. The transmission phase consists of an 8-bit data segment where all bits are forced to 0, followed by a 24-bit remaining segment (MSB to LSB). This 32-bit sequence is labeled 'Channel left 32-bit'. The reception phase shows 'Channel right' data. The diagram is labeled MS30104V1.
Timing diagram for LSB justified 24-bit frame length. It shows three waveforms: CK (clock), WS (word select), and SD (serial data). CK is a periodic square wave. WS is a pulse that goes high during the transmission phase and low during the reception phase. SD shows data being transmitted and received. The transmission phase consists of an 8-bit data segment where all bits are forced to 0, followed by a 24-bit remaining segment (MSB to LSB). This 32-bit sequence is labeled 'Channel left 32-bit'. The reception phase shows 'Channel right' data. The diagram is labeled MS30104V1.

Figure 383. Operations required to transmit 0x3478AE

Diagram showing two write operations to the Data register for transmission. The first write is 0xxx34 and the second is 0x78AE. Text indicates only the 8 LSB of the half-word are significant and 0x00 is forced for the MSBs.

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 and the second is 0x78AE. Text indicates only the 8 LSB of the half-word are significant and 0x00 is forced for the MSBs.

Figure 384. Operations required to receive 0x3478AE

Diagram showing two read operations from the Data register for reception. The first read is 0xxx34 and the second is 0x78AE. Text indicates only the 8 LSB of the half-word are significant and 0x00 is forced for the MSBs.

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 and the second is 0x78AE. Text indicates only the 8 LSB of the half-word are significant and 0x00 is forced for the MSBs.

Figure 385. LSB justified 16-bit extended to 32-bit packet frame

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 second 16 bits are '16-bit remaining'. The MSB and LSB are marked. The first 32 bits correspond to 'Channel left 32-bit' and the second 32 bits to 'Channel right'.

CK

WS

SD

Transmission

Reception

16-bit data 0 forced

MSB

16-bit remaining

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 second 16 bits are '16-bit remaining'. The MSB and LSB are marked. The first 32 bits correspond to 'Channel left 32-bit' and the second 32 bits to 'Channel right'.

When 16-bit data frame extended to 32-bit channel frame is selected during the I2S 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 386 is required.

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

Diagram showing a 16-bit data frame (0x76A3) being transmitted as a 32-bit channel frame. The text 'Only one access to the SPIx-DR register' is above the data value 0x76A3, which is enclosed in a box. The reference MS19598V1 is in the bottom right corner.

Only one access to the SPIx-DR register

0x76A3

MS19598V1

Diagram showing a 16-bit data frame (0x76A3) being transmitted as a 32-bit channel frame. The text 'Only one access to the SPIx-DR register' is above the data value 0x76A3, which is enclosed in a box. The reference MS19598V1 is in the bottom right corner.

In transmission mode, when a TXE event occurs, the application has to write the data to be transmitted (in this case 0x76A3). The 0x000 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.

In PCM mode, the output signals (WS, SD) are sampled on the rising edge of CK signal. The input signals (WS, SD) are captured on the falling edge of CK.

Note that CK and WS are configured as output in MASTER mode.

Figure 387. 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 CK signal is a periodic square wave. The WS short frame signal is a pulse that is high for one clock cycle. The WS long frame signal is a pulse that is high for 13 clock cycles. The SD signal shows a 16-bit data frame with MSB (Most Significant Bit) and LSB (Least Significant Bit) labels. The diagram is labeled MS30106V1 in the bottom right corner.

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 CK signal is a periodic square wave. The WS short frame signal is a pulse that is high for one clock cycle. The WS long frame signal is a pulse that is high for 13 clock cycles. The SD signal shows a 16-bit data frame with MSB (Most Significant Bit) and LSB (Least Significant Bit) labels. The diagram is labeled MS30106V1 in the bottom right corner.

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 388. PCM standard waveforms (16-bit extended to 32-bit packet frame)

Timing diagram showing CK, WS (short and long frames), and SD signals for PCM standard waveforms. CK is the clock signal. WS short frame shows a single clock cycle pulse. WS long frame shows a pulse lasting multiple clock cycles. SD shows data bits starting with MSB and ending with LSB. Annotations indicate 'Up to 13-bits' and '16 bits' durations.

The figure illustrates the timing relationship between the following signals:

Annotations show a gap of 'Up to 13-bits' between the WS pulse and the start of data, and a '16 bits' duration for the data itself. The diagram is identified as MS30107V1.

Timing diagram showing CK, WS (short and long frames), and SD signals for PCM standard waveforms. CK is the clock signal. WS short frame shows a single clock cycle pulse. WS long frame shows a pulse lasting multiple clock cycles. SD shows data bits starting with MSB and ending with LSB. Annotations indicate 'Up to 13-bits' and '16 bits' durations.

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.

32.7.4 Start-up description

The Figure 389 shows how the serial interface is handled in MASTER mode, when the SPI/I2S is enabled (via I2SE bit). It shows as well the effect of CKPOL on the generated signals.

Figure 389. Start sequence in master mode

Timing diagram for Master I2S Philips Standard. It shows four signal lines: WS (O), CK (O) with CKPOL=0 and CKPOL=1, SD (O), and I2SE. The WS signal is a square wave. The CK signal is a high-frequency square wave. The SD signal shows a 'dum' (don't care) period followed by 'Left sample' and 'Right sample' blocks. The I2SE signal is a horizontal line indicating the interface is enabled. Timing diagram for Master I2S MSB or LSB justified. It shows four signal lines: WS (O), CK (O) with CKPOL=0 and CKPOL=1, SD (O), and I2SE. The WS signal is a square wave. The CK signal is a high-frequency square wave. The SD signal shows a 'dum' (don't care) period followed by 'Left sample' and 'Right sample' blocks. The I2SE signal is a horizontal line indicating the interface is enabled. Timing diagram for Master PCM short frame. It shows four signal lines: WS (O), CK (O) with CKPOL=0 and CKPOL=1, SD (O), and I2SE. The WS signal is a square wave. The CK signal is a high-frequency square wave. The SD signal shows a 'dum' (don't care) period followed by 'Sample1' and 'Sample 2' blocks. The I2SE signal is a horizontal line indicating the interface is enabled. Timing diagram for Master PCM long frame. It shows four signal lines: WS (O), CK (O) with CKPOL=0 and CKPOL=1, SD (O), and I2SE. The WS signal is a square wave. The CK signal is a high-frequency square wave. The SD signal shows a 'dum' (don't care) period followed by 'Sample1' and 'Sample 2' blocks. The I2SE signal is a horizontal line indicating the interface is enabled.

Master I2S Philips Standard

Master I2S MSB or LSB justified

Master PCM short frame

Master PCM long frame

dum: not significant data

MSV37520V2

Timing diagram for Master I2S Philips Standard. It shows four signal lines: WS (O), CK (O) with CKPOL=0 and CKPOL=1, SD (O), and I2SE. The WS signal is a square wave. The CK signal is a high-frequency square wave. The SD signal shows a 'dum' (don't care) period followed by 'Left sample' and 'Right sample' blocks. The I2SE signal is a horizontal line indicating the interface is enabled. Timing diagram for Master I2S MSB or LSB justified. It shows four signal lines: WS (O), CK (O) with CKPOL=0 and CKPOL=1, SD (O), and I2SE. The WS signal is a square wave. The CK signal is a high-frequency square wave. The SD signal shows a 'dum' (don't care) period followed by 'Left sample' and 'Right sample' blocks. The I2SE signal is a horizontal line indicating the interface is enabled. Timing diagram for Master PCM short frame. It shows four signal lines: WS (O), CK (O) with CKPOL=0 and CKPOL=1, SD (O), and I2SE. The WS signal is a square wave. The CK signal is a high-frequency square wave. The SD signal shows a 'dum' (don't care) period followed by 'Sample1' and 'Sample 2' blocks. The I2SE signal is a horizontal line indicating the interface is enabled. Timing diagram for Master PCM long frame. It shows four signal lines: WS (O), CK (O) with CKPOL=0 and CKPOL=1, SD (O), and I2SE. The WS signal is a square wave. The CK signal is a high-frequency square wave. The SD signal shows a 'dum' (don't care) period followed by 'Sample1' and 'Sample 2' blocks. The I2SE signal is a horizontal line indicating the interface is enabled.

In slave mode, the way the frame synchronization is detected, depends on the value of ASTRTEN bit.

If ASTRTEN = 0, when the audio interface is enabled (I2SE = 1), then the hardware waits for the appropriate transition on the incoming WS signal, using the CK signal.

The appropriate transition is a falling edge on WS signal when I 2 S Philips Standard is used, or a rising edge for other standards. The falling edge is detected by sampling first WS to 1 and then to 0, and vice-versa for the rising edge detection.

If ASTRTEN = 1, the user has to enable the audio interface before the WS becomes active. This means that the I2SE bit must be set to 1 when WS = 1 for I 2 S Philips standard, or when WS = 0 for other standards.

32.7.5 Clock generator

The I 2 S bit rate determines the data flow on the I 2 S data line and the I 2 S clock signal frequency.

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

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

\[ \text{I}^2\text{S bit rate} = 16 \times 2 \times f_s \]

It is: I 2 S bit rate = 32 × 2 × \( f_s \) if the packet length is 32-bit wide.

Figure 390. Audio sampling frequency definition

Figure 390: Audio sampling frequency definition diagram. It shows a sequence of audio samples: '16-or 32-bit left channel' followed by '16-or 32-bit right channel'. A double-headed arrow between the start of the left channel and the start of the right channel is labeled '32- or 64-bits' and 'F_s'. Vertical arrows point to the start of each channel, labeled 'sampling point'. Below the diagram, it says 'F_s : audio sampling frequency'. The diagram is labeled 'MS30108V1' in the bottom right corner.
Figure 390: Audio sampling frequency definition diagram. It shows a sequence of audio samples: '16-or 32-bit left channel' followed by '16-or 32-bit right channel'. A double-headed arrow between the start of the left channel and the start of the right channel is labeled '32- or 64-bits' and 'F_s'. Vertical arrows point to the start of each channel, labeled 'sampling point'. Below the diagram, it says 'F_s : audio sampling frequency'. The diagram is labeled 'MS30108V1' in the bottom right corner.

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

Figure 391. I 2 S clock generator architecture

Figure 391: I2S clock generator architecture diagram. It shows a block diagram of the clock generator. An input 'I2SxCLK' enters a '3-bit linear divider + reshaping stage'. The output of this stage goes to a 'Divider by 4', which then goes to a 'Div2' block. The output of 'Div2' is connected to a multiplexer (MUX) with inputs '0' and '1'. The MUX output is labeled 'CK'. Another input to the MUX is 'MCKOE'. The '3-bit linear divider' also has a control register 'I2SDIV[7:0]' with bits 'MCKOE', 'ODD', and 'I2SDIV[7:0]'. The 'Div2' block has a control register 'I2SMOD' with bits 'I2SMOD' and 'CHLEN'. The MUX output 'CK' is also connected to an output 'MCK' via a buffer. The diagram is labeled 'MS30109V1' in the bottom right corner.
Figure 391: I2S clock generator architecture diagram. It shows a block diagram of the clock generator. An input 'I2SxCLK' enters a '3-bit linear divider + reshaping stage'. The output of this stage goes to a 'Divider by 4', which then goes to a 'Div2' block. The output of 'Div2' is connected to a multiplexer (MUX) with inputs '0' and '1'. The MUX output is labeled 'CK'. Another input to the MUX is 'MCKOE'. The '3-bit linear divider' also has a control register 'I2SDIV[7:0]' with bits 'MCKOE', 'ODD', and 'I2SDIV[7:0]'. The 'Div2' block has a control register 'I2SMOD' with bits 'I2SMOD' and 'CHLEN'. The MUX output 'CK' is also connected to an output 'MCK' via a buffer. The diagram is labeled 'MS30109V1' in the bottom right corner.
  1. 1. Where x can be 2 or 3.

Figure 391 presents the communication clock architecture. The I2SxCLK clock is provided by the reset and clock controller (RCC) of the product. The I2SxCLK clock can be asynchronous with respect to the SPI/I2S APB clock.


Warning: In addition, it is mandatory to keep the I2SxCLK frequency higher or equal to the APB clock used by the SPI/I2S block. If this condition is not respected the SPI/I2S does not work properly.


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:

For I 2 S modes:

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

\[ F_S = \frac{F_{I2SxCLK}}{256 \times ((2 \times I2SDIV) + ODD)} \]

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

\[ F_S = \frac{F_{I2SxCLK}}{32 \times (CHLEN + 1) \times ((2 \times I2SDIV) + ODD)} \]

CHLEN = 0 when the channel frame is 16-bit wide and,

CHLEN = 1 when the channel frame is 32-bit wide.

For PCM modes:

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

\[ F_S = \frac{F_{I2SxCLK}}{128 \times ((2 \times I2SDIV) + ODD)} \]

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

\[ F_S = \frac{F_{I2SxCLK}}{16 \times (CHLEN + 1) \times ((2 \times I2SDIV) + ODD)} \]

CHLEN = 0 when the channel frame is 16-bit wide and,

CHLEN = 1 when the channel frame is 32-bit wide.

Where \( F_S \) is the audio sampling frequency, and \( F_{I2SxCLK} \) is the frequency of the kernel clock provided to the SPI/I2S block.

Note: I2SDIV must be strictly higher than 1.

The following table provides example precision values for different clock configurations.

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

Table 209. Audio-frequency precision using 48 MHz clock derived from HSE (1)

SYSCLOCK (MHz)Data lengthI2SDIVI2SODDMCLKTarget fs (Hz)Real fs (kHz)Error
481680No96000937502.3438%
483240No96000937502.3438%
4816151No4800048387.09680.8065%
483280No48000468752.3438%
4816170No4410044117.6470.0400%
483281No4410044117.6470.0400%
4816231No3200031914.89360.2660%
4832111No3200032608.6961.9022%
4816340No2205022058.82350.0400%
4832170No2205022058.82350.0400%
4816470No1600015957.44680.2660%
4832231No1600015957.4470.2660%
4816680No1102511029.41180.0400%
4832340No1102511029.4120.0400%
4816940No80007978.72340.2660%
4832470No80007978.72340.2660%
481620Yes48000468752.3430%
483220Yes48000468752.3430%
481620Yes44100468756.2925%
483220Yes44100468756.2925%
481630Yes32000312502.3438%
483230Yes32000312502.3438%
481641Yes2205020833.3335.5178%
483241Yes2205020833.3335.5178%
481660Yes16000156252.3438%
483260Yes16000156252.3438%
481681Yes1102511029.41180.0400%
483281Yes1102511029.41180.0400%
4816111Yes80008152.173911.9022%
4832111Yes80008152.173911.9022%
  1. 1. This table gives only example values for different clock configurations. Other configurations allowing optimum clock precision are possible.

32.7.6 I 2 S master mode

The I2S 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 DAC/ADC audio component (the I2SDIV and ODD values should be computed depending on the state of the MCK output, for more details refer to Section 32.7.5: Clock generator ).
  3. 3. Set the I2SMOD bit in the SPIx_I2SCFGR register to activate the I2S 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 32.7.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 I2S, by clearing I2SE, it is mandatory to wait for TXE = 1 and BSY = 0.

Reception sequence

The operating mode is the same as for transmission mode except for the point 3 (refer to the procedure described in Section 32.7.6: 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 I2S cell.

For more details about the read operations depending on the I 2 S standard mode selected, refer to Section 32.7.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 I2S, specific actions are required to ensure that the I2S completes the transfer cycle properly without initiating a new data transfer. The sequence depends on the configuration of the data and channel lengths, and on the audio protocol mode selected. In the case of:

Note: The BSY flag is kept low during transfers.

32.7.7 I 2 S slave mode

For the slave configuration, the I2S 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 I2S interface. The clock and WS signals are input from the external master connected to the I2S interface. There is then no need, for the user, to configure the clock.

The configuration steps to follow are listed below:

  1. 1. Set the I2SMOD bit in the 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 I2S data register has to be loaded before the master initiates the communication.

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

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

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

The data half-word is parallel-loaded into the 16-bit shift register (from the internal bus) during the first bit transmission, and then shifted out serially to the MOSI/SD pin MSB first. The TXE flag is set after each transfer from the Tx buffer to the shift register and an interrupt is generated if the TXEIE bit in the 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 32.7.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 I2S and to restart a data transfer starting from the left channel.

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

Reception sequence

The operating mode is the same as for the transmission mode except for the point 1 (refer to the procedure described in Section 32.7.7: 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 the I 2 S standard mode selected, refer to Section 32.7.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 I2S in reception mode, I2SE has to be cleared immediately after receiving the last RXNE = 1.

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

32.7.8 I2S status flags

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

Busy flag (BSY)

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

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

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

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

The BSY flag is cleared:

When communication is continuous:

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

Tx buffer empty flag (TXE)

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

RX buffer not empty (RXNE)

When set, this flag indicates that there are valid received data in the RX Buffer. It is reset when 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 I2S needs to be switched off and switched on before resuming the communication.

In reception mode, this flag is refreshed when data are received into 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 I2S should be reset by disabling and then enabling it (with configuration if it needs changing).

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

When the OVR or UDR flag in the 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).

32.7.9 I2S error flags

There are three error flags for the I2S cell.

Underrun flag (UDR)

In slave transmission mode this flag is set when the first clock for data transmission appears while the software has not yet loaded any value into 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 I2S 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 I2S slave device:

  1. 1. Disable the I2S.
  2. 2. Re-enable the I2S interface again (Keeping ASTRTEN=0).

Desynchronization between master and slave devices may be due to noisy environment on the CK 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.

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

32.8 I2S interrupts

Table 210 provides the list of I2S interrupts.

Table 210. I2S interrupt requests

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

32.9 SPI and I2S registers

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

32.9.1 SPI control register 1 (SPIx_CR1)

Address offset: 0x00

Reset value: 0x0000

1514131211109876543210
BIDI
MODE
BIDIOECRC
EN
CRCN
EXT
CRCLRX
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 and in slave mode, the MISO pin is used.

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.

This bit is not used in I 2 S mode.

Bit 12 CRCNEXT : Transmit CRC next

0: Next transmit value is from Tx buffer.

1: Next transmit value is from Tx CRC register.

Note: This bit has to be written as soon as the last data is written in the SPIx_DR register.

This bit is not used in I 2 S mode.

Bit 11 CRCL : CRC length

This bit is set and cleared by software to select the CRC length.

0: 8-bit CRC length

1: 16-bit CRC length

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

This bit is not used in I 2 S mode.

Bit 10 RXONLY : Receive only mode enabled.

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 I/O 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: data is transmitted / received with the MSB first

1: data is transmitted / received with the LSB first

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

2. This bit 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 Procedure for disabling the SPI on page 1086 .

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.

These bits are not used in I 2 S mode.

Bit 2 MSTR : Master selection

0: Slave configuration

1: Master configuration

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

This bit is not used in I 2 S mode.

Bit 1 CPOL : Clock polarity

0: CK to 0 when idle

1: CK to 1 when idle

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

This bit is not used in SPI TI mode except the case when CRC is applied at TI mode.

Bit 0 CPHA : Clock phase

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

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

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

This bit is not used in SPI TI mode except the case when CRC is applied at TI mode.

32.9.2 SPI control register 2 (SPIx_CR2)

Address offset: 0x04

Reset value: 0x0700

1514131211109876543210
Res.LDMA_TXLDMA_RXFRXTHDS[3:0]TXEIERXNEIEERRIEFRFNSSPSSOETXDMAENRXDMAEN
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

Bit 15 Reserved, must be kept at reset value.

Bit 14 LDMA_TX : Last DMA transfer for transmission

This bit is used in data packing mode, to define if the total number of data to transmit by DMA is odd or even. It has significance only if the TXDMAEN bit in the SPIx_CR2 register is set and if packing mode is used (data length <= 8-bit and write access to SPIx_DR is 16-bit wide). It has to be written when the SPI is disabled (SPE = 0 in the SPIx_CR1 register).

0: Number of data to transfer is even

1: Number of data to transfer is odd

Note: Refer to Procedure for disabling the SPI on page 1086 if the CRCEN bit is set.

This bit is not used in I 2 S mode.

Bit 13 LDMA_RX : Last DMA transfer for reception

This bit is used in data packing mode, to define if the total number of data to receive by DMA is odd or even. It has significance only if the RXDMAEN bit in the SPIx_CR2 register is set and if packing mode is used (data length <= 8-bit and write access to SPIx_DR is 16-bit wide). It has to be written when the SPI is disabled (SPE = 0 in the SPIx_CR1 register).

0: Number of data to transfer is even

1: Number of data to transfer is odd

Note: Refer to Procedure for disabling the SPI on page 1086 if the CRCEN bit is set.

This bit is not used in I 2 S mode.

Bit 12 FRXTH : FIFO reception threshold

This bit is used to set the threshold of the RXFIFO that triggers an RXNE event

0: RXNE event is generated if the FIFO level is greater than or equal to 1/2 (16-bit)

1: RXNE event is generated if the FIFO level is greater than or equal to 1/4 (8-bit)

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

Bits 11:8 DS[3:0] : Data size

These bits configure the data length for SPI transfers.

0000: Not used

0001: Not used

0010: Not used

0011: 4-bit

0100: 5-bit

0101: 6-bit

0110: 7-bit

0111: 8-bit

1000: 9-bit

1001: 10-bit

1010: 11-bit

1011: 12-bit

1100: 13-bit

1101: 14-bit

1110: 15-bit

1111: 16-bit

If software attempts to write one of the “Not used” values, they are forced to the value “0111” (8-bit)

Note: These bits are not used in I 2 S mode.

Bit 7 TXEIE : Tx buffer empty interrupt enable

0: TXE interrupt masked

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

Bit 6 RXNEIE : RX buffer not empty interrupt enable

0: RXNE interrupt masked

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

Bit 5 ERRIE : Error interrupt enable

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

0: Error interrupt is masked

1: Error interrupt is enabled

Bit 4 FRF : Frame format

0: SPI Motorola mode

1: SPI TI mode

Note: This bit must be written only when the SPI is disabled (SPE=0).

This bit is not used in I 2 S mode.

Bit 3 NSSP : NSS pulse management

This bit is used in master mode only. It allows the SPI to generate an NSS pulse between two consecutive data when doing continuous transfers. In the case of a single data transfer, it forces the NSS pin high level after the transfer.

It has no meaning if CPHA = '1', or FRF = '1'.

0: No NSS pulse

1: NSS pulse generated

Note: 1. This bit must be written only when the SPI is disabled (SPE=0).

2. This bit is not used in I 2 S mode and SPI TI mode.

Bit 2 SSOE : SS output enable

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

1: SS output is enabled in master mode and when the SPI interface is enabled. The SPI interface 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, a DMA request is generated 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, a DMA request is generated whenever the RXNE flag is set.

0: Rx buffer DMA disabled

1: Rx buffer DMA enabled

32.9.3 SPI status register (SPIx_SR)

Address offset: 0x08

Reset value: 0x0002

1514131211109876543210
Res.Res.Res.FTLVL[1:0]FRLVL[1:0]FREBSYOVRMODFCRCE
RR
UDRCHSIDETXERXNE
r rr rrrrrrc_w0rrrr

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

Bits 12:11 FTLVL[1:0] : FIFO transmission level

These bits are set and cleared by hardware.

00: FIFO empty

01: 1/4 FIFO

10: 1/2 FIFO

11: FIFO full (considered as FULL when the FIFO threshold is greater than 1/2)

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

Bits 10:9 FRLVL[1:0] : FIFO reception level

These bits are set and cleared by hardware.

00: FIFO empty

01: 1/4 FIFO

10: 1/2 FIFO

11: FIFO full

Note: These bits are not used in I 2 S mode and in SPI receive-only mode while CRC calculation is enabled.

Bit 8 FRE : Frame format error

This flag is used for SPI in TI slave mode and I 2 S slave mode. Refer to Section 32.5.11: SPI error flags and Section 32.7.9: I 2 S error flags .

This flag is set by hardware and reset when SPIx_SR is read by software.

0: No frame format error

1: A frame format error occurred

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: The BSY flag must be used with caution: refer to Section 32.5.10: SPI status flags and Procedure for disabling the SPI on page 1086 .

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 I2S error flags on page 1119 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 : Mode fault (MODF) on page 1096 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 SPIx_RXCRCR value

1: CRC value received does not match the SPIx_RXCRCR value

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

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 I2S error flags on page 1119 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 in SPI mode. It has no significance 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

32.9.4 SPI data register (SPIx_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 serves as an interface between the Rx and Tx FIFOs. When the data register is read, RxFIFO is accessed while the write to data register accesses TxFIFO (See Section 32.5.9: Data transmission and reception procedures ).

Note: Data is always right-aligned. Unused bits are ignored when writing to the register, and read as zero when the register is read. The Rx threshold setting must always correspond with the read access currently used.

32.9.5 SPI CRC polynomial register (SPIx_CRCPR)

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 (0x0007) is the reset value of this register. Another polynomial can be configured as required.

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

32.9.6 SPI Rx CRC register (SPIx_RXCRCR)

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 SPIx_CR1 register is written to 1. The CRC is calculated serially using the polynomial programmed in the SPIx_CRCPR register.

Only the 8 LSB bits are considered when the CRC frame format is set to be 8-bit length (CRCL bit in the SPIx_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 CRC frame format is selected (CRCL bit in the SPIx_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 in I 2 S mode.

32.9.7 SPI Tx CRC register (SPIx_TXCRCR)

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 SPIx_CR1 is written to 1. The CRC is calculated serially using the polynomial programmed in the SPIx_CRCPR register.

Only the 8 LSB bits are considered when the CRC frame format is set to be 8-bit length (CRCL bit in the SPIx_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 CRC frame format is selected (CRCL bit in the SPIx_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 in I 2 S mode.

32.9.8 SPIx_I2S configuration register (SPIx_I2SCFGR)

Address offset: 0x1C

Reset value: 0x0000

1514131211109876543210
Res.Res.Res.ASTRTENI2SMODI2SEI2SCFG[1:0]PCMSYNCRes.I2SSTD[1:0]CKPOLDATLEN[1:0]CHLEN
rwrwrwrwrwrwrwrwrw

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

Bit 12 ASTRTEN : Asynchronous start enable.

0: The Asynchronous start is disabled.

When the I2S is enabled in slave mode, the hardware starts the transfer when the I2S clock is received and an appropriate transition is detected on the WS signal.

1: The Asynchronous start is enabled.

When the I2S is enabled in slave mode, the hardware starts the transfer when the I2S clock is received and the appropriate level is detected on the WS signal.

Note: The appropriate transition is a falling edge on WS signal when I 2 S Philips Standard is used, or a rising edge for other standards.

The appropriate level is a low level on WS signal when I 2 S Philips Standard is used, or a high level for other standards.

Please refer to Section 32.7.4: Start-up description for additional information.

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 is disabled.

Bit 10 I2SE : I2S enable

0: I2S peripheral is disabled

1: I2S peripheral is enabled

Note: This bit is not used in SPI mode.

Bits 9:8 I2SCFG[1:0] : I2S configuration mode

00: Slave - transmit

01: Slave - receive

10: Master - transmit

11: Master - receive

Note: These bits should be configured when the I2S is disabled.

They are 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, must be kept at reset value.

Bits 5:4 I2SSTD[1:0] : 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 32.7.3 on page 1103

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

They are not used in SPI mode.

Bit 3 CKPOL : Inactive state clock polarity

0: I2S clock inactive state is low level

1: I2S clock inactive state is high level

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

It is not used in SPI mode.

The bit CKPOL does not affect the CK edge sensitivity used to receive or transmit the SD and WS signals.

Bits 2:1 DATLEN[1:0] : Data length to be transferred

00: 16-bit data length

01: 24-bit data length

10: 32-bit data length

11: Not allowed

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

They are 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.

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

It is not used in SPI mode.

32.9.9 SPIx_I2S prescaler register (SPIx_I2SPR)

Address offset: 0x20

Reset value: 0x0002

1514131211109876543210
Res.Res.Res.Res.Res.Res.MCKOEODDI2SDIV[7:0]
rwrwrwrwrwrwrwrwrwrw

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

Bit 9 MCKOE : Master clock output enable

0: Master clock output is disabled

1: Master clock output is enabled

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

It 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 32.7.4 on page 1110 .

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

It is not used in SPI mode.

Bits 7:0 I2SDIV[7:0] : I2S linear prescaler

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

Refer to Section 32.7.4 on page 1110 .

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

They are not used in SPI mode.

32.9.10 SPI/I2S register map

Table 211 shows the SPI/I2S register map and reset values.

Table 211. SPI/I2S register map and reset values

OffsetRegister name
reset value
1514131211109876543210
0x00SPIx_CR1BIDIMODEBIDIOECRCENCRCNEXTCRCLRXONLYSSMSSILSBFIRSTSPEBR [2:0]MSTRCPOLCPHA
Reset value0000000000000000
0x04SPIx_CR2Res.LDMA_TXLDMA_RXFRXTHDS[3:0]TXEIERXNEIEERRIEFRFNSSPSSOETXDMAENRXDMAEN
Reset value000011100000000
0x08SPIx_SRRes.Res.Res.FTLVL[1:0]FRLVL[1:0]FREBSYOVRMODFORERRUDRCHSIDETXERXNE
Reset value0000000000010
0x0CSPIx_DRDR[15:0]
Reset value0000000000000000
0x10SPIx_CRCPRCRCPOLY[15:0]
Reset value0000000000000111
0x14SPIx_RXCRCRRXCRC[15:0]
Reset value0000000000000000
0x18SPIx_TXCRCRTXCRC[15:0]
Reset value0000000000000000
0x1CSPIx_I2SCFGRRes.Res.Res.ASTRTENI2SMODI2SEI2SCFG[1:0]PCMSYNCRes.I2SSTDCKPOLDATLEN[1:0]CHLEN
Reset value000000000000
0x20SPIx_I2SPRRes.Res.Res.Res.Res.Res.MCKOEODDI2SDIV[7:0]
Reset value0000000010

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