RM0008 Serial peripheral interface (SPI)
25 Serial peripheral interface (SPI)
Low-density devices are STM32F101xx, STM32F102xx and STM32F103xx
microcontrollers where the Flash memory density ranges between 16 and 32 Kbytes.
Medium-density devices are STM32F101xx, STM32F102xx and STM32F103xx
microcontrollers where the Flash memory density ranges between 64 and 128 Kbytes.
High-density devices are STM32F101xx and STM32F103xx microcontrollers where the
Flash memory density ranges between 256 and 512 Kbytes.
XL-density devices are STM32F101xx and STM32F103xx microcontrollers where the
Flash memory density ranges between 768 Kbytes and 1 Mbyte.
Connectivity line devices are STM32F105xx and STM32F107xx microcontrollers.
25.1 SPI introduction
In high-density, XL-density and connectivity line devices, the SPI interface provides two
main functions, supporting either the SPI protocol or the I2S audio protocol. By default, it is
the SPI function that is selected. It is possible to switch the interface from SPI to I2S by
software.
In Cat.1 and Cat.2 devices, the I2S protocol is not available.
The serial peripheral interface (SPI) allows half/ full-duplex, synchronous, serial
communication with external devices. The interface can be configured as the master and in
this case it provides the communication clock (SCK) to the external slave device. The
interface is also capable of operating in multimaster configuration.
It may be used for a variety of purposes, including simplex synchronous transfers on two
lines with a possible bidirectional data line or reliable communication using CRC checking.
The I2S is also a synchronous serial communication interface. It can address four different
audio standards including the I2S Philips standard, the MSB- and LSB-justified standards,
and the PCM standard. It can operate as a slave or a master device in full-duplex mode
(using 4 pins) or in half-duplex mode (using 6 pins). Master clock can be provided by the
interface to an external slave component when the I2S is configured as the communication
master.
Warning: Since some SPI3/I2S3 pins are shared with JTAG pins
(SPI3_NSS/I2S3_WS with JTDI and SPI3_SCK/I2S3_CK with
JTDO), they are not controlled by the IO controller and are
reserved for JTAG usage (after each Reset).
For this purpose, prior to configure the SPI3/I2S3 pins, the
user has to disable the JTAG and use the SWD interface
(when debugging the application), or disable both JTAG/SWD
interfaces (for standalone applications). For more
information on the configuration of JTAG/SWD interface pins
refer to Section 9.3.5: JTAG/SWD alternate function
remapping.
RM0008 Rev 20 699/1134
751
Serial peripheral interface (SPI) RM0008
25.2 SPI and I2S main features
25.2.1 SPI features
• Full-duplex synchronous transfers on three lines
• Simplex synchronous transfers on two lines with or without a bidirectional data line
• 8- or 16-bit transfer frame format selection
• Master or slave operation
• Multimaster mode capability
• 8 master mode baud rate prescalers (fPCLK/2 max.)
• Slave mode frequency (fPCLK/2 max)
• Faster communication for both master and slave
• NSS management by hardware or software for both master and slave: dynamic change
of master/slave operations
• Programmable clock polarity and phase
• Programmable data order with MSB-first or LSB-first shifting
• Dedicated transmission and reception flags with interrupt capability
• SPI bus busy status flag
• Hardware CRC feature for reliable communication:
– CRC value can be transmitted as last byte in Tx mode
– Automatic CRC error checking for last received byte
• Master mode fault, overrun and CRC error flags with interrupt capability
• 1-byte transmission and reception buffer with DMA capability: Tx and Rx requests
700/1134 RM0008 Rev 20
RM0008 Serial peripheral interface (SPI)
25.2.2 I2S features
• Half-duplex communication (only transmitter or receiver)
• Master or slave operations
• 8-bit programmable linear prescaler to reach accurate audio sample frequencies (from
8 kHz to 192 kHz)
• Data format may be 16-bit, 24-bit or 32-bit
• Packet frame is fixed to 16-bit (16-bit data frame) or 32-bit (16-bit, 24-bit, 32-bit data
frame) by audio channel
• Programmable clock polarity (steady state)
• Underrun flag in slave transmission mode and Overrun flag in reception mode (master
and slave)
• 16-bit register for transmission and reception with one data register for both channel
sides
• Supported I2S protocols:
– I2S Phillps standard
– MSB-justified standard (left-justified)
– LSB-justified standard (right-justified)
– PCM standard (with short and long frame synchronization on 16-bit channel frame
or 16-bit data frame extended to 32-bit channel frame)
• Data direction is always MSB first
• DMA capability for transmission and reception (16-bit wide)
• Master clock may be output to drive an external audio component. Ratio is fixed at
256 × FS (where FS is the audio sampling frequency)
• In connectivity line devices, both I2S (I2S2 and I2S3) have a dedicated PLL (PLL3) to
generate an even more accurate clock.
RM0008 Rev 20 701/1134
751
Serial peripheral interface (SPI) RM0008
25.3 SPI functional description
25.3.1 General description
The block diagram of the SPI is shown in Figure 238.
Figure 238. SPI block diagram
$GGUHVVDQGGDWDEXV
5HDG
5[EXIIHU
63,B&5
026,
7;( 5;1( (55 7;'0 5;'0
662(
,( ,( ,( $(1 $(1
0,62 6KLIWUHJLVWHU
/6%ILUVW 63,B65
02' &5&
%6< 295 7;( 5;1(
7[EXIIHU ) (55
:ULWH
&RPPXQLFDWLRQFRQWURO
6&. %5>@
%DXGUDWHJHQHUDWRU
/6%
63( %5 %5 %5 0675 &32/ &3+$
),567
63,B&5
0DVWHUFRQWUROORJLF %,', %,',
&5&(1
&5&
'))
5;
21/< 660 66,
02'( 2( 1H[W
166
069
Usually, the SPI is connected to external devices through four pins:
• MISO: Master In / Slave Out data. This pin can be used to transmit data in slave mode
and receive data in master mode.
• MOSI: Master Out / Slave In data. This pin can be used to transmit data in master
mode and receive data in slave mode.
• SCK: Serial Clock output for SPI masters and input for SPI slaves.
• NSS: Slave select. This is an optional pin to select a slave device. This pin acts as a
‘chip select’ to let the SPI master communicate with slaves individually and to avoid
contention on the data lines. Slave NSS inputs can be driven by standard IO ports on
the master device. The NSS pin may also be used as an output if enabled (SSOE bit)
and driven low if the SPI is in master configuration. In this manner, all NSS pins from
devices connected to the Master NSS pin see a low level and become slaves when
they are configured in NSS hardware mode. When configured in master mode with
NSS configured as an input (MSTR=1 and SSOE=0) and if NSS is pulled low, the SPI
enters the master mode fault state: the MSTR bit is automatically cleared and the
device is configured in slave mode (refer to Section 25.3.10).
A basic example of interconnections between a single master and a single slave is
illustrated in Figure 239.
702/1134 RM0008 Rev 20
RM0008 Serial peripheral interface (SPI)
Figure 239. Single master/ single slave application
0DVWHU 6ODYH
06%LW /6%LW 06%LW /6%LW
0,62 0,62
ELWVKLIWUHJLVWHU ELWVKLIWUHJLVWHU
026, 026,
63,FORFN 6&. 6&.
JHQHUDWRU
166 166
9''
1RWXVHGLI166LVPDQDJHG
E\VRIWZDUH
DL
1. Here, the NSS pin is configured as an input.
The MOSI pins are connected together and the MISO pins are connected together. In this
way data is transferred serially between master and slave (most significant bit first).
The communication is always initiated by the master. When the master device transmits
data to a slave device via the MOSI pin, the slave device responds via the MISO pin. This
implies full-duplex communication with both data out and data in synchronized with the
same clock signal (which is provided by the master device via the SCK pin).
Slave select (NSS) pin management
Hardware or software slave select management can be set using the SSM bit in the
SPI_CR1 register.
• Software NSS management (SSM = 1)
The slave select information is driven internally by the value of the SSI bit in the
SPI_CR1 register. The external NSS pin remains free for other application uses.
• Hardware NSS management (SSM = 0)
Two configurations are possible depending on the NSS output configuration (SSOE bit
in register SPI_CR2).
– NSS output enabled (SSM = 0, SSOE = 1)
This configuration is used only when the device operates in master mode. The
NSS signal is driven low when the master starts the communication and is kept
low until the SPI is disabled.
– NSS output disabled (SSM = 0, SSOE = 0)
This configuration allows multimaster capability for devices operating in master
mode. For devices set as slave, the NSS pin acts as a classical NSS input: the
slave is selected when NSS is low and deselected when NSS high.
RM0008 Rev 20 703/1134
751
Serial peripheral interface (SPI) RM0008
Clock phase and clock polarity
Four possible timing relationships may be chosen by software, using the CPOL and CPHA
bits in the SPI_CR1 register. The CPOL (clock polarity) bit controls the steady state value of
the clock when no data is being transferred. This bit affects both master and slave modes. If
CPOL is reset, the SCK pin has a low-level idle state. If CPOL is set, the SCK pin has a
high-level idle state.
If the CPHA (clock phase) bit is set, the second edge on the SCK pin (falling edge if the
CPOL bit is reset, rising edge if the CPOL bit is set) is the MSBit capture strobe. Data are
latched on the occurrence of the second clock transition. If the CPHA bit is reset, the first
edge on the SCK pin (falling edge if CPOL bit is set, rising edge if CPOL bit is reset) is the
MSBit capture strobe. Data are latched on the occurrence of the first clock transition.
The combination of the CPOL (clock polarity) and CPHA (clock phase) bits selects the data
capture clock edge.
Figure 240, shows an SPI transfer with the four combinations of the CPHA and CPOL bits.
The diagram may be interpreted as a master or slave timing diagram where the SCK pin,
the MISO pin, the MOSI pin are directly connected between the master and the slave
device.
Note: Prior to changing the CPOL/CPHA bits the SPI must be disabled by resetting the SPE bit.
Master and slave must be programmed with the same timing mode.
The idle state of SCK must correspond to the polarity selected in the SPI_CR1 register (by
pulling up SCK if CPOL=1 or pulling down SCK if CPOL=0).
The Data Frame Format (8- or 16-bit) is selected through the DFF bit in SPI_CR1 register,
and determines the data length during transmission/reception.
704/1134 RM0008 Rev 20
RM0008 Serial peripheral interface (SPI)
Figure 240. Data clock timing diagram
&3+$
&32/
&32/
026, 06%LW /6%LW
0,62 06%LW /6%LW
166
WRVODYH
&DSWXUHVWUREH
&3+$
&32/
&32/
026, 06%LW /6%LW
0,62 06%LW /6%LW
166
WRVODYH
&DSWXUHVWUREH
DLG
1. These timings are shown with the LSBFIRST bit reset in the SPI_CR1 register.
Data frame format
Data can be shifted out either MSB-first or LSB-first depending on the value of the
LSBFIRST bit in the SPI_CR1 Register.
Each data frame is 8 or 16 bits long depending on the size of the data programmed using
the DFF bit in the SPI_CR1 register. The selected data frame format is applicable for
transmission and/or reception.
RM0008 Rev 20 705/1134
751
Serial peripheral interface (SPI) RM0008
25.3.2 Configuring the SPI in slave mode
In the slave configuration, the serial clock is received on the SCK pin from the master
device. The value set in the BR[2:0] bits in the SPI_CR1 register, does not affect the data
transfer rate.
Note: It is recommended to enable the SPI slave before the master sends the clock. If not,
undesired data transmission might occur. The data register of the slave needs to be ready
before the first edge of the communication clock or before the end of the ongoing
communication. It is mandatory to have the polarity of the communication clock set to the
steady state value before the slave and the master are enabled.
Follow the procedure below to configure the SPI in slave mode:
Procedure
1. Set the DFF bit to define 8- or 16-bit data frame format
2. Select the CPOL and CPHA bits to define one of the four relationships between the
data transfer and the serial clock (see Figure 240). For correct data transfer, the CPOL
and CPHA bits must be configured in the same way in the slave device and the master
device.
3. The frame format (MSB-first or LSB-first depending on the value of the LSBFIRST bit in
the SPI_CR1 register) must be the same as the master device.
4. In Hardware mode (refer to Slave select (NSS) pin management), the NSS pin must be
connected to a low level signal during the complete byte transmit sequence. In NSS
software mode, set the SSM bit and clear the SSI bit in the SPI_CR1 register.
5. Clear the MSTR bit and set the SPE bit (both in the SPI_CR1 register) to assign the
pins to alternate functions.
In this configuration the MOSI pin is a data input and the MISO pin is a data output.
Transmit sequence
The data byte is parallel-loaded into the Tx buffer during a write cycle.
The transmit sequence begins when the slave device receives the clock signal and the most
significant bit of the data on its MOSI pin. The remaining bits (the 7 bits in 8-bit data frame
format, and the 15 bits in 16-bit data frame format) are loaded into the shift-register. The
TXE flag in the SPI_SR register is set on the transfer of data from the Tx Buffer to the shift
register and an interrupt is generated if the TXEIE bit in the SPI_CR2 register is set.
Receive sequence
For the receiver, when data transfer is complete:
• The Data in shift register is transferred to Rx Buffer and the RXNE flag (SPI_SR
register) is set
• An Interrupt is generated if the RXNEIE bit is set in the SPI_CR2 register.
After the last sampling clock edge the RXNE bit is set, a copy of the data byte received in
the shift register is moved to the Rx buffer. When the SPI_DR register is read, the SPI
peripheral returns this buffered value.
Clearing of the RXNE bit is performed by reading the SPI_DR register.
706/1134 RM0008 Rev 20
RM0008 Serial peripheral interface (SPI)
25.3.3 Configuring the SPI in master mode
In the master configuration, the serial clock is generated on the SCK pin.
Procedure
1. Select the BR[2:0] bits to define the serial clock baud rate (see SPI_CR1 register).
2. Select the CPOL and CPHA bits to define one of the four relationships between the
data transfer and the serial clock (see Figure 240).
3. Set the DFF bit to define 8- or 16-bit data frame format
4. Configure the LSBFIRST bit in the SPI_CR1 register to define the frame format.
5. If the NSS pin is required in input mode, in hardware mode, connect the NSS pin to a
high-level signal during the complete byte transmit sequence. In NSS software mode,
set the SSM and SSI bits in the SPI_CR1 register. If the NSS pin is required in output
mode, the SSOE bit only should be set.
6. The MSTR and SPE bits must be set (they remain set only if the NSS pin is connected
to a high-level signal).
In this configuration the MOSI pin is a data output and the MISO pin is a data input.
Transmit sequence
The transmit sequence begins when a byte is written in the Tx Buffer.
The data byte is parallel-loaded into the shift register (from the internal bus) during the first
bit transmission and then shifted out serially to the MOSI pin MSB first or LSB first
depending on the LSBFIRST bit in the SPI_CR1 register. The TXE flag is set on the transfer
of data from the Tx Buffer to the shift register and an interrupt is generated if the TXEIE bit in
the SPI_CR2 register is set.
Receive sequence
For the receiver, when data transfer is complete:
• The data in the shift register is transferred to the RX Buffer and the RXNE flag is set
• An interrupt is generated if the RXNEIE bit is set in the SPI_CR2 register
At the last sampling clock edge the RXNE bit is set, a copy of the data byte received in the
shift register is moved to the Rx buffer. When the SPI_DR register is read, the SPI
peripheral returns this buffered value.
Clearing the RXNE bit is performed by reading the SPI_DR register.
A continuous transmit stream can be maintained if the next data to be transmitted is put in
the Tx buffer once the transmission is started. Note that TXE flag should be ‘1 before any
attempt to write the Tx buffer is made.
Note: When a master is communicating with SPI slaves which need to be de-selected between
transmissions, the NSS pin must be configured as GPIO or another GPIO must be used and
toggled by software.
25.3.4 Configuring the SPI for half-duplex communication
The SPI is capable of operating in half-duplex mode in 2 configurations.
• 1 clock and 1 bidirectional data wire
• 1 clock and 1 data wire (receive-only or transmit-only)
RM0008 Rev 20 707/1134
751
Serial peripheral interface (SPI) RM0008
1 clock and 1 bidirectional data wire (BIDIMODE = 1)
This mode is enabled by setting the BIDIMODE bit in the SPI_CR1 register. In this mode
SCK is used for the clock and MOSI in master or MISO in slave mode is used for data
communication. The transfer direction (Input/Output) is selected by the BIDIOE bit in the
SPI_CR1 register. When this bit is 1, the data line is output otherwise it is input.
1 clock and 1 unidirectional data wire (BIDIMODE = 0)
In this mode, the application can use the SPI either in transmit-only mode or in receive-only
mode.
• Transmit-only mode is similar to full-duplex mode (BIDIMODE=0, RXONLY=0): the
data are transmitted on the transmit pin (MOSI in master mode or MISO in slave mode)
and the receive pin (MISO in master mode or MOSI in slave mode) can be used as a
general-purpose IO. In this case, the application just needs to ignore the Rx buffer (if
the data register is read, it does not contain the received value).
• In receive-only mode, the application can disable the SPI output function by setting the
RXONLY bit in the SPI_CR1 register. In this case, it frees the transmit IO pin (MOSI in
master mode or MISO in slave mode), so it can be used for other purposes.
To start the communication in receive-only mode, configure and enable the SPI:
• In master mode, the communication starts immediately and stops when the SPE bit is
cleared and the current reception stops. There is no need to read the BSY flag in this
mode. It is always set when an SPI communication is ongoing.
• In slave mode, the SPI continues to receive as long as the NSS is pulled down (or the
SSI bit is cleared in NSS software mode) and the SCK is running.
25.3.5 Data transmission and reception procedures
Rx and Tx buffers
In reception, data are received and then stored into an internal Rx buffer while In
transmission, data are first stored into an internal Tx buffer before being transmitted.
A read access of the SPI_DR register returns the Rx buffered value whereas a write access
to the SPI_DR stores the written data into the Tx buffer.
708/1134 RM0008 Rev 20
RM0008 Serial peripheral interface (SPI)
Start sequence in master mode
• In full-duplex (BIDIMODE=0 and RXONLY=0)
– The sequence begins when data are written into the SPI_DR register (Tx buffer).
– The data are then parallel loaded from the Tx buffer into the 8-bit shift register
during the first bit transmission and then shifted out serially to the MOSI pin.
– At the same time, the received data on the MISO pin is shifted in serially to the 8-
bit shift register and then parallel loaded into the SPI_DR register (Rx buffer).
• In unidirectional receive-only mode (BIDIMODE=0 and RXONLY=1)
– The sequence begins as soon as SPE=1
– Only the receiver is activated and the received data on the MISO pin are shifted in
serially to the 8-bit shift register and then parallel loaded into the SPI_DR register
(Rx buffer).
• In bidirectional mode, when transmitting (BIDIMODE=1 and BIDIOE=1)
– The sequence begins when data are written into the SPI_DR register (Tx buffer).
– The data are then parallel loaded from the Tx buffer into the 8-bit shift register
during the first bit transmission and then shifted out serially to the MOSI pin.
– No data are received.
• In bidirectional mode, when receiving (BIDIMODE=1 and BIDIOE=0)
– The sequence begins as soon as SPE=1 and BIDIOE=0.
– The received data on the MOSI pin are shifted in serially to the 8-bit shift register
and then parallel loaded into the SPI_DR register (Rx buffer).
– The transmitter is not activated and no data are shifted out serially to the MOSI
pin.
Start sequence in slave mode
• In full-duplex mode (BIDIMODE=0 and RXONLY=0)
– The sequence begins when the slave device receives the clock signal and the first
bit of the data on its MOSI pin. The 7 remaining bits are loaded into the shift
register.
– At the same time, the data are parallel loaded from the Tx buffer into the 8-bit shift
register during the first bit transmission, and then shifted out serially to the MISO
pin. The software must have written the data to be sent before the SPI master
device initiates the transfer.
• In unidirectional receive-only mode (BIDIMODE=0 and RXONLY=1)
– The sequence begins when the slave device receives the clock signal and the first
bit of the data on its MOSI pin. The 7 remaining bits are loaded into the shift
register.
– The transmitter is not activated and no data are shifted out serially to the MISO
pin.
• In bidirectional mode, when transmitting (BIDIMODE=1 and BIDIOE=1)
– The sequence begins when the slave device receives the clock signal and the first
bit in the Tx buffer is transmitted on the MISO pin.
– The data are then parallel loaded from the Tx buffer into the 8-bit shift register
during the first bit transmission and then shifted out serially to the MISO pin. The
RM0008 Rev 20 709/1134
751
Serial peripheral interface (SPI) RM0008
software must have written the data to be sent before the SPI master device
initiates the transfer.
– No data are received.
• In bidirectional mode, when receiving (BIDIMODE=1 and BIDIOE=0)
– The sequence begins when the slave device receives the clock signal and the first
bit of the data on its MISO pin.
– The received data on the MISO pin are shifted in serially to the 8-bit shift register
and then parallel loaded into the SPI_DR register (Rx buffer).
– The transmitter is not activated and no data are shifted out serially to the MISO
pin.
Handling data transmission and reception
The TXE flag (Tx buffer empty) is set when the data are transferred from the Tx buffer to the
shift register. It indicates that the internal Tx buffer is ready to be loaded with the next data.
An interrupt can be generated if the TXEIE bit in the SPI_CR2 register is set. Clearing the
TXE bit is performed by writing to the SPI_DR register.
Note: The software must ensure that the TXE flag is set to 1 before attempting to write to the Tx
buffer. Otherwise, it overwrites the data previously written to the Tx buffer.
The RXNE flag (Rx buffer not empty) is set on the last sampling clock edge, when the data
are transferred from the shift register to the Rx buffer. It indicates that data are ready to be
read from the SPI_DR register. An interrupt can be generated if the RXNEIE bit in the
SPI_CR2 register is set. Clearing the RXNE bit is performed by reading the SPI_DR
register.
For some configurations, the BSY flag can be used during the last data transfer to wait until
the completion of the transfer.
Full-duplex transmit and receive procedure in master or slave mode (BIDIMODE=0 and
RXONLY=0)
The software has to follow this procedure to transmit and receive data (see Figure 241 and
Figure 242):
1. Enable the SPI by setting the SPE bit to 1.
2. Write the first data item to be transmitted into the SPI_DR register (this clears the TXE
flag).
3. Wait until TXE=1 and write the second data item to be transmitted. Then wait until
RXNE=1 and read the SPI_DR to get the first received data item (this clears the RXNE
bit). Repeat this operation for each data item to be transmitted/received until the n–1
received data.
4. Wait until RXNE=1 and read the last received data.
5. Wait until TXE=1 and then wait until BSY=0 before disabling the SPI.
This procedure can also be implemented using dedicated interrupt subroutines launched at
each rising edges of the RXNE or TXE flag.
710/1134 RM0008 Rev 20
RM0008 Serial peripheral interface (SPI)
Figure 241. TXE/RXNE/BSY behavior in Master / full-duplex mode (BIDIMODE=0 and
RXONLY=0) in case of continuous transfers
([DPSOHLQ0DVWHUPRGHZLWK&32/ &3+$
6&.
'$7$ [) '$7$ [) '$7$ [)
0,62026,RXW E E E E E E E E E E E E E E E E E E E E E E E E
VHWE\KDUGZDUH VHWE\KDUGZDUH
7;(IODJ FOHDUHGE\VRIWZDUH FOHDUHGE\VRIWZDUH VHWE\KDUGZDUH
7[EXIIHU [) [) [)
ZULWH63,B'5
%6<IODJ VHWE\KDUGZDUH UHVHWE\KDUGZDUH
'$7$ [$ '$7$ [$ '$7$ [$
0,62026,LQ E E E E E E E E E E E E E E E E E E E E E E E E
VHWE\KDUGZDUH FOHDUHGE\VRIWZDUH
5;1(IODJ
5[EXIIHU [$ [$ [$
UHDG63,B'5
VRIWZDUH VRIWZDUHZDLWV VRIWZDUHZDLWV VRIWZDUHZDLWV VRIWZDUHZDLWV VRIWZDUHZDLWV
ZULWHV[) XQWLO7;( DQG XQWLO5;1( XQWLO7;( DQG XQWLO5;1( XQWLO5;1(
LQWR63,B'5 ZULWHV[)LQWR DQGUHDGV[$ ZULWHV[)LQWR DQGUHDGV[$ DQGUHDGV[$
63,B'5 IURP63,B'5 63,B'5 IURP63,B'5 IURP63,B'5
DL
RM0008 Rev 20 711/1134
751
Serial peripheral interface (SPI) RM0008
Figure 242. TXE/RXNE/BSY behavior in Slave / full-duplex mode (BIDIMODE=0,
RXONLY=0) in case of continuous transfers
([DPSOHLQ6ODYHPRGHZLWK&32/ &3+$
6&.
'$7$ [) '$7$ [) '$7$ [)
0,62026,RXW E E E E E E E E E E E E E E E E E E E E E E E E
VHWE\KDUGZDUH VHWE\KDUGZDUH
7;(IODJ FOHDUHGE\VRIWZDUH FOHDUHGE\VRIWZDUH VHWE\KDUGZDUH
7[EXIIHU [) [) [)
ZULWHWR63,B'5
%6<IODJ VHWE\FOHDUHGE\VRIWZDUH UHVHWE\KDUGZDUH
'$7$ [$ '$7$ [$ '$7$ [$
0,62026,LQ E E E E E E E E E E E E E E E E E E E E E E E E
VHWE\KDUGZDUH FOHDUHGE\VRIWZDUH
5;1(IODJ
5[EXIIHU [$ [$ [$
UHDGIURP63,B'5
VRIWZDUH VRIWZDUHZDLWV VRIWZDUHZDLWV VRIWZDUHZDLWV VRIWZDUHZDLWV VRIWZDUHZDLWV
ZULWHV[) XQWLO7;( DQG XQWLO5;1( XQWLO7;( DQG XQWLO5;1( XQWLO5;1(
LQWR63,B'5 ZULWHV[)LQWR DQGUHDGV[$ ZULWHV[)LQWR DQGUHDGV[$ DQGUHDGV[$
63,B'5 IURP63,B'5 63,B'5 IURP63,B'5 IURP63,B'5
DL
Transmit-only procedure (BIDIMODE=0 RXONLY=0)
In this mode, the procedure can be reduced as described below and the BSY bit can be
used to wait until the completion of the transmission (see Figure 243 and Figure 244).
1. Enable the SPI by setting the SPE bit to 1.
2. Write the first data item to send into the SPI_DR register (this clears the TXE bit).
3. Wait until TXE=1 and write the next data item to be transmitted. Repeat this step for
each data item to be transmitted.
4. After writing the last data item into the SPI_DR register, wait until TXE=1, then wait until
BSY=0, this indicates that the transmission of the last data is complete.
This procedure can be also implemented using dedicated interrupt subroutines launched at
each rising edge of the TXE flag.
Note: During discontinuous communications, there is a 2 APB clock period delay between the
write operation to SPI_DR and the BSY bit setting. As a consequence, in transmit-only
mode, it is mandatory to wait first until TXE is set and then until BSY is cleared after writing
the last data.
After transmitting two data items in transmit-only mode, the OVR flag is set in the SPI_SR
register since the received data are never read.
712/1134 RM0008 Rev 20
RM0008 Serial peripheral interface (SPI)
Figure 243. TXE/BSY behavior in Master transmit-only mode (BIDIMODE=0 and RXONLY=0)
in case of continuous transfers
([DPSOHLQ0DVWHUPRGHZLWK&32/ &3+$
6&.
'$7$ [) '$7$ [) '$7$ [)
0,62026,RXW E E E E E E E E E E E E E E E E E E E E E E E E
VHWE\KDUGZDUH VHWE\KDUGZDUH
7;(IODJ FOHDUHGE\VRIWZDUH FOHDUHGE\VRIWZDUH VHWE\KDUGZDUH
7[EXIIHU [) [) [)
ZULWHWR63,B'5
%6<IODJ VHWE\KDUGZDUH UHVHWE\KDUGZDUH
VRIWZDUHZULWHV VRIWZDUHZDLWV VRIWZDUHZDLWV
[)LQWR XQWLO7;( DQG XQWLO7;( DQG
VRIWZDUHZDLWVXQWLO7;( VRIWZDUHZDLWVXQWLO%6<
63,B'5 ZULWHV[)LQWR ZULWHV[)LQWR
63,B'5 63,B'5
DL
Figure 244. TXE/BSY in Slave transmit-only mode (BIDIMODE=0 and RXONLY=0) in case of
continuous transfers
([DPSOHLQVODYHPRGHZLWK&32/ &3+$
6&.
'$7$ [) '$7$ [) '$7$ [)
0,62026,RXW E E E E E E E E E E E E E E E E E E E E E E E E
VHWE\KDUGZDUH VHWE\KDUGZDUH
7;(IODJ FOHDUHGE\VRIWZDUH FOHDUHGE\VRIWZDUH VHWE\KDUGZDUH
7[EXIIHU [) [) [)
ZULWHWR63,B'5
%6<IODJ VHWE\KDUGZDUH UHVHWE\KDUGZDUH
VRIWZDUHZULWHV VRIWZDUHZDLWV VRIWZDUHZDLWV
[)LQWR XQWLO7;( DQG XQWLO7;( DQG
VRIWZDUHZDLWVXQWLO7;( VRIWZDUHZDLWVXQWLO%6<
63,B'5 ZULWHV[)LQWR ZULWHV[)LQWR
63,B'5 63,B'5 DL
Bidirectional transmit procedure (BIDIMODE=1 and BIDIOE=1)
In this mode, the procedure is similar to the procedure in Transmit-only mode except that
the BIDIMODE and BIDIOE bits both have to be set in the SPI_CR2 register before enabling
the SPI.
Unidirectional receive-only procedure (BIDIMODE=0 and RXONLY=1)
In this mode, the procedure can be reduced as described below (see Figure 245):
RM0008 Rev 20 713/1134
751
Serial peripheral interface (SPI) RM0008
1. Set the RXONLY bit in the SPI_CR1 register.
2. Enable the SPI by setting the SPE bit to 1:
a) In master mode, this immediately activates the generation of the SCK clock, and
data are serially received until the SPI is disabled (SPE=0).
b) In slave mode, data are received when the SPI master device drives NSS low and
generates the SCK clock.
3. Wait until RXNE=1 and read the SPI_DR register to get the received data (this clears
the RXNE bit). Repeat this operation for each data item to be received.
This procedure can also be implemented using dedicated interrupt subroutines launched at
each rising edge of the RXNE flag.
Note: If it is required to disable the SPI after the last transfer, follow the recommendation
described in Section 25.3.8.
Figure 245. RXNE behavior in receive-only mode (BIDIRMODE=0 and RXONLY=1)
in case of continuous transfers
([DPSOHZLWK&32/ &3+$ 5;21/<
6&.
'$7$ [$ '$7$ [$ '$7$ [$
0,62026,LQ E E E E E E E E E E E E E E E E E E E E E E E E
VHWE\KDUGZDUH FOHDUHGE\VRIWZDUH
5;1(IODJ
5[EXIIHU [$ [$ [$
UHDGIURP63,B'5
VRIWZDUHZDLWVXQWLO5;1( VRIWZDUHZDLWVXQWLO5;1( VRIWZDUHZDLWVXQWLO5;1(
DQGUHDGV[$IURP63,B'5 DQGUHDGV[$IURP63,B'5 DQGUHDGV[$IURP63,B'5
DL
Bidirectional receive procedure (BIDIMODE=1 and BIDIOE=0)
In this mode, the procedure is similar to the Receive-only mode procedure except that the
BIDIMODE bit has to be set and the BIDIOE bit cleared in the SPI_CR2 register before
enabling the SPI.
Continuous and discontinuous transfers
When transmitting data in master mode, if the software is fast enough to detect each rising
edge of TXE (or TXE interrupt) and to immediately write to the SPI_DR register before the
ongoing data transfer is complete, the communication is said to be continuous. In this case,
there is no discontinuity in the generation of the SPI clock between each data item and the
BSY bit is never cleared between each data transfer.
On the contrary, if the software is not fast enough, this can lead to some discontinuities in
the communication. In this case, the BSY bit is cleared between each data transmission
(see Figure 246).
In Master receive-only mode (RXONLY=1), the communication is always continuous and
the BSY flag is always read at 1.
714/1134 RM0008 Rev 20
RM0008 Serial peripheral interface (SPI)
In slave mode, the continuity of the communication is decided by the SPI master device. In
any case, even if the communication is continuous, the BSY flag goes low between each
transfer for a minimum duration of one SPI clock cycle (see Figure 244).
Figure 246. TXE/BSY behavior when transmitting (BIDIRMODE=0 and RXONLY=0)
in case of discontinuous transfers
([DPSOHZLWK&32/ &3+$
6&.
'$7$ [) '$7$ [) '$7$ [)
026,RXW E E E E E E E E E E E E E E E E E E E E E E E E
7;(IODJ
7[EXIIHU [) [) [)
ZULWHWR63,B'5
%6<IODJ
VRIWZDUHZULWHV[) VRIWZDUHZDLWVXQWLO7;( EXWLV VRIWZDUHZDLWVXQWLO7;( EXW VRIWZDUHZDLWV VRIWZDUHZDLWVXQWLO%6<
LQWR63,B'5 ODWHWRZULWH[)LQWR63,B'5 LVODWHWRZULWH[)LQWR XQWLO7;(
63,B'5
DL
25.3.6 CRC calculation
A CRC calculator has been implemented for communication reliability. Separate CRC
calculators are implemented for transmitted data and received data. The CRC is calculated
using a programmable polynomial serially on each bit. It is calculated on the sampling clock
edge defined by the CPHA and CPOL bits in the SPI_CR1 register.
Note: This SPI offers two kinds of CRC calculation standard which depend directly on the data
frame format selected for the transmission and/or reception: 8-bit data (CR8) and 16-bit data
(CRC16).
CRC calculation is enabled by setting the CRCEN bit in the SPI_CR1 register. This action
resets the CRC registers (SPI_RXCRCR and SPI_TXCRCR). In full duplex or transmitter
only mode, when the transfers are managed by the software (CPU mode), it is necessary to
write the bit CRCNEXT immediately after the last data to be transferred is written to the
SPI_DR. At the end of this last data transfer, the SPI_TXCRCR value is transmitted.
In receive only mode and when the transfers are managed by software (CPU mode), it is
necessary to write the CRCNEXT bit after the second last data has been received. The CRC
is received just after the last data reception and the CRC check is then performed.
At the end of data and CRC transfers, the CRCERR flag in the SPI_SR register is set if
corruption occurs during the transfer.
If data are present in the TX buffer, the CRC value is transmitted only after the transmission
of the data byte. During CRC transmission, the CRC calculator is switched off and the
register value remains unchanged.
SPI communication using the CRC is possible through the following procedure:
RM0008 Rev 20 715/1134
751
Serial peripheral interface (SPI) RM0008
1. Program the CPOL, CPHA, LSBFirst, BR, SSM, SSI and MSTR values.
2. Program the polynomial in the SPI_CRCPR register.
3. Enable the CRC calculation by setting the CRCEN bit in the SPI_CR1 register. This
also clears the SPI_RXCRCR and SPI_TXCRCR registers.
4. Enable the SPI by setting the SPE bit in the SPI_CR1 register.
5. Start the communication and sustain the communication until all but one byte or half-
word have been transmitted or received.
– In full duplex or transmitter-only mode, when the transfers are managed by
software, when writing the last byte or half word to the Tx buffer, set the
CRCNEXT bit in the SPI_CR1 register to indicate that the CRC will be transmitted
after the transmission of the last byte.
– In receiver only mode, set the bit CRCNEXT just after the reception of the second
to last data to prepare the SPI to enter in CRC Phase at the end of the reception of
the last data. CRC calculation is frozen during the CRC transfer.
6. After the transfer of the last byte or half word, the SPI enters the CRC transfer and
check phase. In full duplex mode or receiver-only mode, the received CRC is
compared to the SPI_RXCRCR value. If the value does not match, the CRCERR flag in
SPI_SR is set and an interrupt can be generated when the ERRIE bit in the SPI_CR2
register is set.
Note: When the SPI is in slave mode, be careful to enable CRC calculation only when the clock is
stable, that is, when the clock is in the steady state. If not, a wrong CRC calculation may be
done. In fact, the CRC is sensitive to the SCK slave input clock as soon as CRCEN is set,
and this, whatever the value of the SPE bit.
With high bitrate frequencies, be careful when transmitting the CRC. As the number of used
CPU cycles has to be as low as possible in the CRC transfer phase, it is forbidden to call
software functions in the CRC transmission sequence to avoid errors in the last data and
CRC reception. In fact, CRCNEXT bit has to be written before the end of the
transmission/reception of the last data.
For high bit rate frequencies, it is advised to use the DMA mode to avoid the degradation of
the SPI speed performance due to CPU accesses impacting the SPI bandwidth.
When the devices are configured as slaves and the NSS hardware mode is used, the NSS
pin needs to be kept low between the data phase and the CRC phase.
When the SPI is configured in slave mode with the CRC feature enabled, CRC calculation
takes place even if a high level is applied on the NSS pin. This may happen for example in
case of a multislave environment where the communication master addresses slaves
alternately.
Between a slave deselection (high level on NSS) and a new slave selection (low level on
NSS), the CRC value should be cleared on both master and slave sides in order to
resynchronize the master and slave for their respective CRC calculation.
To clear the CRC, follow the procedure below:
1. Disable SPI (SPE = 0)
2. Clear the CRCEN bit
3. Set the CRCEN bit
4. Enable the SPI (SPE = 1)
716/1134 RM0008 Rev 20
RM0008 Serial peripheral interface (SPI)
25.3.7 Status flags
Four status flags are provided for the application to completely monitor the state of the SPI
bus.
Tx buffer empty flag (TXE)
When it is set, this flag indicates that the Tx buffer is empty and the next data to be
transmitted can be loaded into the buffer. The TXE flag is cleared when writing to the
SPI_DR register.
Rx buffer not empty (RXNE)
When set, this flag indicates that there are valid received data in the Rx buffer. It is cleared
when SPI_DR is read.
BUSY flag
This BSY flag is set and cleared by hardware (writing to this flag has no effect). The BSY
flag indicates the state of the communication layer of the SPI.
When BSY is set, it indicates that the SPI is busy communicating. There is one exception in
master mode / bidirectional receive mode (MSTR=1 and BDM=1 and BDOE=0) where the
BSY flag is kept low during reception.
The BSY flag is useful to detect the end of a transfer if the software wants to disable the SPI
and enter Halt mode (or disable the peripheral clock). This avoids corrupting the last
transfer. For this, the procedure described below must be strictly respected.
The BSY flag is also useful to avoid write collisions in a multimaster system.
The BSY flag is set when a transfer starts, with the exception of master mode / bidirectional
receive mode (MSTR=1 and BDM=1 and BDOE=0).
It is cleared:
• when a transfer is finished (except in master mode if the communication is continuous)
• when the SPI is disabled
• when a master mode fault occurs (MODF=1)
When communication is not continuous, the BSY flag is low between each communication.
When communication is continuous:
• in master mode, the BSY flag is kept high during all the transfers
• in slave mode, the BSY flag goes low for one SPI clock cycle between each transfer
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.
RM0008 Rev 20 717/1134
751
Serial peripheral interface (SPI) RM0008
25.3.8 Disabling the SPI
When a transfer is terminated, the application can stop the communication by disabling the
SPI peripheral. This is done by clearing the SPE bit.
For some configurations, disabling the SPI and entering the Halt mode while a transfer is
ongoing can cause the current transfer to be corrupted and/or the BSY flag might become
unreliable.
To avoid any of those effects, it is recommended to respect the following procedure when
disabling the SPI:
In master or slave full-duplex mode (BIDIMODE=0, RXONLY=0)
1. Wait until RXNE=1 to receive the last data
2. Wait until TXE=1
3. Then wait until BSY=0
4. Disable the SPI (SPE=0) and, eventually, enter the Halt mode (or disable the peripheral
clock)
In master or slave unidirectional transmit-only mode (BIDIMODE=0,
RXONLY=0) or bidirectional transmit mode (BIDIMODE=1, BIDIOE=1)
After the last data is written into the SPI_DR register:
1. Wait until TXE=1
2. Then wait until BSY=0
3. Disable the SPI (SPE=0) and, eventually, enter the Halt mode (or disable the peripheral
clock)
In master unidirectional receive-only mode (MSTR=1, BIDIMODE=0,
RXONLY=1) or bidirectional receive mode (MSTR=1, BIDIMODE=1, BIDIOE=0)
This case must be managed in a particular way to ensure that the SPI does not initiate a
new transfer:
1. Wait for the second to last occurrence of RXNE=1 (n–1)
2. Then wait for one SPI clock cycle (using a software loop) before disabling the SPI
(SPE=0)
3. Then wait for the last RXNE=1 before entering the Halt mode (or disabling the
peripheral clock)
Note: In master bidirectional receive mode (MSTR=1 and BDM=1 and BDOE=0), the BSY flag is
kept low during transfers.
In slave receive-only mode (MSTR=0, BIDIMODE=0, RXONLY=1) or
bidirectional receive mode (MSTR=0, BIDIMODE=1, BIDOE=0)
1. You can disable the SPI (write SPE=1) at any time: the current transfer will complete
before the SPI is effectively disabled
2. Then, if you want to enter the Halt mode, you must first wait until BSY = 0 before
entering the Halt mode (or disabling the peripheral clock).
718/1134 RM0008 Rev 20
RM0008 Serial peripheral interface (SPI)
25.3.9 SPI communication using DMA (direct memory addressing)
To operate at its maximum speed, the SPI needs to be fed with the data for transmission
and the data received on the Rx buffer should be read to avoid overrun. To facilitate the
transfers, the SPI features a DMA capability implementing a simple request/acknowledge
protocol.
A DMA access is requested when the enable bit in the SPI_CR2 register is enabled.
Separate requests must be issued to the Tx and Rx buffers (see Figure 247 and
Figure 248):
• In transmission, a DMA request is issued each time TXE is set to 1. The DMA then
writes to the SPI_DR register (this clears the TXE flag).
• In reception, a DMA request is issued each time RXNE is set to 1. The DMA then reads
the SPI_DR register (this clears the RXNE flag).
When the SPI is used only to transmit data, it is possible to enable only the SPI Tx DMA
channel. In this case, the OVR flag is set because the data received are not read.
When the SPI is used only to receive data, it is possible to enable only the SPI Rx DMA
channel.
In transmission mode, when the DMA has written all the data to be transmitted (flag TCIF is
set in the DMA_ISR register), the BSY flag can be monitored to ensure that the SPI
communication is complete. This is required to avoid corrupting the last transmission before
disabling the SPI or entering the Stop mode. The software must first wait until TXE=1 and
then until BSY=0.
Note: During discontinuous communications, there is a 2 APB clock period delay between the
write operation to SPI_DR and the BSY bit setting. As a consequence, it is mandatory to
wait first until TXE=1 and then until BSY=0 after writing the last data.
RM0008 Rev 20 719/1134
751
Serial peripheral interface (SPI) RM0008
Figure 247. Transmission using DMA
([DPSOHZLWK&32/ &3+$
6&.
'$7$ [) '$7$ [) '$7$ [)
0,62026,RXW E E E E E E E E E E E E E E E E E E E E E E E E
VHWE\KDUGZDUH VHWE\KDUGZDUH
7;(IODJ FOHDUHGE\'0$ZULWH FOHDUE\'0$ZULWH VHWE\KDUGZDUH
UHVHW
%6<IODJ VHWE\KDUGZDUH E\KDUGZDUH
'0$UHTXHVW LJQRUHGE\WKH'0$EHFDXVH
'0$WUDQVIHULVFRPSOHWH
7[EXIIHU [) [) [)
ZULWHWR63,B'5
'0$ZULWHVWR63,B'5
VHWE\KDUGZDUH FOHDUE\VRIWZDUH
'0$7&,)IODJ
'0$WUDQVIHUFRPSOHWH
VRIWZDUHFRQILJXUHVWKH '0$ZULWHV '0$ZULWHV '0$ZULWHV '0$WUDQVIHULV VRIWZDUHZDLWV VRIWZDUHZDLWVXQWLO%6<
'0$63,7[FKDQQHO '$7$LQWR '$7$LQWR '$7$LQWR FRPSOHWH7&,) LQ XQWLO7;(
WRVHQGGDWDLWHPV 63,B'5 63,B'5 63,B'5 '0$B,65
DQGHQDEOHVWKH63,
DL
Figure 248. Reception using DMA
([DPSOHZLWK&32/ &3+$
6&.
'$7$ [$ '$7$ [$ '$7$ [$
0,62026,LQ E E E E E E E E E E E E E E E E E E E E E E E E
VHWE\KDUGZDUH FOHDUE\'0$UHDG
5;1(IODJ
'0$UHTXHVW
5[EXIIHU
[$ [$ [$
UHDGIURP63,B'5
'0$UHDGIURP63,B'5
FOHDU
VHWE\KDUGZDUH
IODJ'0$7&,) E\VRIWZDUH
'0$WUDQVIHUFRPSOHWH
VRIWZDUHFRQILJXUHVWKH '0$UHDGV '0$UHDGV '0$UHDGV 7KH'0$WUDQVIHULV
'0$63,5[FKDQQHO '$7$IURP '$7$IURP '$7$IURP FRPSOHWH7&,) LQ
WRUHFHLYHGDWDLWHPV 63,B'5 63,B'5 63,B'5 '0$B,65
DQGHQDEOHVWKH63,
DL
720/1134 RM0008 Rev 20
RM0008 Serial peripheral interface (SPI)
DMA capability with CRC
When SPI communication is enabled with CRC communication and DMA mode, the
transmission and reception of the CRC at the end of communication are automatic that is
without using the bit CRCNEXT. After the CRC reception, the CRC must be read in the
SPI_DR register to clear the RXNE flag.
At the end of data and CRC transfers, the CRCERR flag in SPI_SR is set if corruption
occurs during the transfer.
25.3.10 Error flags
Master mode fault (MODF)
Master mode fault occurs when the master device has its NSS pin pulled low (in NSS
hardware mode) or SSI bit low (in NSS software mode), this automatically sets the MODF
bit. Master mode fault affects the SPI peripheral in the following ways:
• The MODF bit is set and an SPI interrupt is generated if the ERRIE bit is set.
• The SPE bit is cleared. This blocks all output from the device and disables the SPI
interface.
• The MSTR bit is cleared, thus forcing the device into slave mode.
Use the following software sequence to clear the MODF bit:
1. Make a read or write access to the SPI_SR register while the MODF bit is set.
2. Then write to the SPI_CR1 register.
To avoid any multiple slave conflicts in a system comprising several MCUs, the NSS pin
must be pulled high during the MODF bit clearing sequence. The SPE and MSTR bits can
be restored to their original state after this clearing sequence.
As a security, hardware does not allow the setting of the SPE and MSTR bits while the
MODF bit is set.
In a slave device the MODF bit cannot be set. However, in a multimaster configuration, the
device can be in slave mode with this MODF bit set. In this case, the MODF bit indicates
that there might have been a multimaster conflict for system control. An interrupt routine can
be used to recover cleanly from this state by performing a reset or returning to a default
state.
Overrun condition
An overrun condition occurs when the master device has sent data bytes and the slave
device has not cleared the RXNE bit resulting from the previous data byte transmitted.
When an overrun condition occurs:
• the OVR bit is set and an interrupt is generated if the ERRIE bit is set.
In this case, the receiver buffer contents will not be updated with the newly received data
from the master device. A read from the SPI_DR register returns this byte. All other
subsequently transmitted bytes are lost.
Clearing the OVR bit is done by a read from the SPI_DR register followed by a read access
to the SPI_SR register.
RM0008 Rev 20 721/1134
751
Serial peripheral interface (SPI) RM0008
CRC error
This flag is used to verify the validity of the value received when the CRCEN bit in the
SPI_CR1 register is set. The CRCERR flag in the SPI_SR register is set if the value
received in the shift register does not match the receiver SPI_RXCRCR value.
25.3.11 SPI interrupts
Table 182. SPI interrupt requests
Interrupt event Event flag Enable Control bit
Transmit buffer empty flag TXE TXEIE
Receive buffer not empty flag RXNE RXNEIE
Master Mode fault event MODF
Overrun error OVR ERRIE
CRC error flag CRCERR
722/1134 RM0008 Rev 20
RM0008 Serial peripheral interface (SPI)
25.4 I2S functional description
The I2S audio protocol is not available in low- and medium-density devices. This section
concerns only high-density, XL-density and connectivity line devices.
25.4.1 I2S general description
The block diagram of the I2S is shown in Figure 249.
Figure 249. I2S block diagram
!DDRESS AND DATA BUS
4X BUFFER
#(
"39 /62 -/$& #2# 5$2 4X% 2X.%
%22 3)$%
BIT
-/3) 3$
3HIFT REGISTER
-)3/
,3"