 Hello, and welcome to this presentation of the STM32 Serial Peripheral Interface, or SPI. The internal serial peripheral interface provides a simple communication interface, allowing the microcontroller to communicate with nearby external devices. This interface is highly configurable to support many synchronous standard communication protocols. Applications benefit from the simple and direct connection to components, which only requires a few pins. Thanks to the highly configurable capabilities of the peripheral, many devices can be simply accommodated in the existing project. The STM32 SPI offers various operating modes, which will be explained in more detail in this presentation. The communication speed cannot exceed half of the internal bus frequency, and a minimum of two wires is required to provide the serial data flow synchronized by clock signal in a single direction at one time. An optional hardware slave select control signal can be added. The data size and transmit shift order are configurable, as well as the clock signal's polarity and phase. At the protocol level, the user can use specific data buffers with an optional automatic cyclic redundancy check or CRC calculation and transfer data through the direct memory access or DMA. There are a wide range of SPI events that can generate interrupt requests. The simplified SPI block diagram shows its basic control mechanisms and functions. There are four IO signals associated with the SPI peripheral. All of the data passes through receive and transmit buffers via their specific interfaces. The control block features are enabled or disabled depending on the configuration. The SPI master always controls the bus traffic and provides the clock signal to the dedicated slave through the SCK line. The master can select the slave it wants to communicate with through the optional NSS signal. Data stored in the dedicated shift registers can be exchanged synchronously between master and slave through the MOSI master output slave input and the MISO master input slave output data lines. When master and slave nodes communicate in full duplex mode, both data lines are used and synchronous data flows in both directions at the same time. In simplex mode, one node transmits data while the other receives it. Data flows in one direction only and the dedicated data line is used for data transfers exclusively. Unused SPI pins can be used for other purposes. Half duplex mode integrates the two modes by sharing a single line for data exchanges and data flows in only one direction at a single time. There is a cross connection between the master MOSI and the slave MISO pins in this mode. The master and slave have to alternate their transmitting and receiving roles synchronously when using this common data line. It is common to add a serial resistor on the half duplex data line to prevent possible temporary short circuit connections since the reconfigurations of the data directions at master and slave nodes are not usually synchronized. When the SPI network includes more than one slave, a star topology is commonly used when all the SCK, MOSI and MISO signals are interconnected. Usually the master communicates with one slave at a time since just one slave can transmit data back to the master through the common MISO pin. In this case, when reading from slaves is required, a separate slave selects signal from the master has to be provided to each slave node so the master selects just a single slave for a communication session. When separate slave select signals are applied, a different SPI data and clock format configuration can be used optionally for the slave nodes. Another multi-slave configuration is the circular topology where all the data inputs and outputs of the nodes are connected together in a closed serial chain. A common slave select signal is used as communication occurs at the same time. This is why all the nodes have to keep the same data and clock format configuration. When the slave SPI nodes are provided by a microcontroller, the internal transmit and receive shift registers are usually physically separated. So the data transferred between them has to be handled by software in this circular mode while the master node has to provide sufficient time between data to provide these internal transfers. An SPI network can also operate in a multi-master environment. This mode is used to connect together two master nodes exclusively. By default, both nodes are in slave mode as long as the network is idle. When one node wants to take control of the bus, it switches itself into master mode and asserts the slave select signal on the other node through a GPIO pin. Both slave select pins work as a hardware input to detect potential bus collisions between nodes as only one master can use the SPI bus at a single time. After the session is completed, the master node releases the slave select signal and returns to passive slave mode waiting for the start of the next session. There are a few controls that are used to set up the data format. Users can define the data frame size and the transmit order of the data bits. The clock can be set to one of four basic configurations defined in the Motorola SPI specification. The combination of two bits controls the polarity and phase of the clock signal. When the phase control bit is cleared, data bits are sampled on the odd clock edges and the even clock edges synchronize the shifting of the next bit onto the data line. The opposite occurs when the phase control bit is set. The clock polarity bit defines the idle and initial state of the clock signal and therefore which clock edge is used for data sampling or shifting depending on the clock phase setting. When communication speeds are fast and data frames short, it can be a demanding task to ensure correct data flows when the clock signal becomes continuous and the full duplex mode is used. Slave nodes have to properly service all the transactions sent by the master to prevent any data overrun or underrun conditions. When the data frame size fits into a byte, packing mode can be used. Then multiple data patterns can be written or read in a single access to the FIFO registers. Together with the proper setting of the FIFO threshold event, the number of events to service will decrease to better control the data flow. When the DMA is used additionally, overall loading on the system is significantly reduced. In the figure shown, you can see the principle of how two short 4-bit data frames can be written and read by a single 16-bit access in the dedicated FIFO registers. The read or write data accesses are performed just by raising a single event. The SPI peripheral features two 32-bit FIFOs to handle the data flow. The FIFOs can be accessed by using either 8-bit or 16-bit data access instructions. During reception, the events generated from the FIFO depend on the threshold setting. This table gives an overview of how the event flag behavior changes depending on the configuration. It is important to keep the FIFO access balanced with the threshold setting so the data consistency is not lost. During transmission, the FIFO occupancy depends on the data access. The system can never predict the next access to the transmission FIFO, although the FIFO capability is not used when an 8-bit write access is applied to fill the second half of the FIFO. In this case, the TXC flag is cleared as a consequence, even though the TXFIFO is not fully occupied. During protocol level communication, the DMA can be used favorably to apply the CRC patterns or change the FIFO threshold setting correctly after an exact number of data is transferred. Before threshold control, the last odd data frame is correctly applied in packed mode when the number of frames is not aligned with the packet size. If the CRC is enabled, separated CRC calculators are used for the transmitter and receiver. The result of the CRC calculation is applied at the end of the transfer either automatically by the DMA or by software. Results from the transmitter's CRC calculation register are loaded directly into the shift register and the received CRC value is stored in the FIFO and compared with the result of the receiver's CRC calculation register. The CRC polynomial used for the calculation is programmable and the length of the CRC pattern can be set to either 8 or 16-bit frames. The slave select signal, or NSS, is commonly used by the master mode to select the slave node for communication. It is generally used in a multi-slave topology and can also be used to synchronize the data flow in a single master-slave pair. The slave select signal can operate either as an input or as an output. The NSS input can be managed externally by hardware or internally by software depending on the SSM and SSI bits. As a slave, it always works as an input and enables the slave for communication. With a master input, it indicates potential conflicts between masters in a multi-master system. As a master output, it is managed by hardware in a standard or specific control mode. Generally, in a standard mode, the master slave select outputs can be easily replaced by GPIOs under software control. There are a few enhanced modes when the slave select signal is under specific hardware control. The slave select signal can operate in a pulse mode where the master generates pulses on the NSS output signal between data frames for a duration of one SPI clock period when there is a continuous transfer of data. The data is then interleaved by two SPI clock periods. The clock phase is fixed in this mode. Another enhanced mode is the TI mode where the data flow is synchronized by the NSS pulses provided by the master on the last bit of data. The clock polarity and phase configuration is fixed and the slave data output is automatically switched into high impedance when the bus traffic stops and on a specific configurable timeout. Here is an overview of the SPI interrupt events. There are FIFO and error detection events to handle the data flow. DMA requests are triggered internally by FIFO threshold events. Here is an overview of the SPI status in specific low power modes. The device is not able to perform any communications in stop, standby or shutdown mode. It is important to ensure that all SPI traffic is completed before the peripheral enters stop or power down mode. The SPI performance depends mainly on the internal clock applied to the peripheral. The peripheral clock should be at least double the maximum achievable communication frequency. The actual rate of communication can be decreased by many application factors. The user has to consider SPI bus loads such as the number of nodes, the connection distance, input capacitance, as well as GPIO settings. Fast GPIO mode should be applied on the data and clock signals. Lower power supply voltages and extreme ambient temperatures also slow down the GPIO edges. Sometimes slower data hold or setup time requirements have to be respected between the nodes. Managing a fast data flow can be demanding for application software due to frequent servicing of exceptions. The DMA capacity has to be considered as well when the system uses more DMA channels, frequent interrupt services or executes non-interruptible instructions such as LDMIA. The SPI can be used in a wide range of applications where a simple data transfer is required without the need for a complex communication protocol. Secured transfers are also supported when used with smart cards. Here are some helpful tips. The user should be aware that traffic on the bus may still be ongoing even if the DMA transaction is completed or the transmit FIFO becomes empty. This is why the user has to carefully check the peripheral status and follow the suggested procedures before disabling the SPI or placing it in stop mode. Use the DMA if you have a specific control that requires CRC handling or receive FIFO threshold control when the number of data is not aligned in packet mode or to receive an exact amount of data in receive only mode. Such controls have to be applied during a specific time window and exclusively within a frame transaction so all the following transactions are handled properly. The use of the DMA and data packing can increase the system's overall performance. These features can help when data frames are short and a fast continuous communication flow is required. Hardware management of slave select signal is not quite necessary in a single master single slave pair but it can help synchronize the data flow and prevent conflicts in a multi master system. There are some additional specific aspects which should be taken into account when designing an SPI network. The receiver always loads CRC information into the receive FIFO. The user has to account for this in the buffer and flush it. The busy flag should not be used for any data handling but to check for ongoing traffic. The B-S-Y bit stays set between data frames during the master continuous data transactions. It always drops low for at least one SPI clock cycle between data frames in slave mode no matter if the communication is continuous or not. When the node transmits data only the receive flow stays active. The user should ignore all receive and associated overrun events in this case. The data size to be processed by the DMA when including CRC is dependent on the mode. This data size has to be configured differently when receiving or transmitting data in full duplex mode than in receive only mode. There are three SPI instances within the STM32L4 and each support all the features we have discussed. This is a list of peripherals related to the SPI. Please refer to these peripheral trainings for more information if needed. There are some dedicated SPI application notes. To learn more about the SPI generally there are many web pages that discuss SPI topics and SPI bus monitoring tools. Many digital oscilloscope support direct decoding of the SPI bus. Thank you.