Serial Peripheral Interface (SPI) is a de facto standard (with many variants) for synchronous serial communication, used primarily in embedded systems for short-distance wired communication between integrated circuits. SPI follows a master–slave architecture, where a device (variously called master, leader, controller, main...) orchestrates communication with one or more devices (variously called slave, follower, peripheral, target...) by driving the clock and chip select signals. As there is no formal standard, some manufacturers introduce devices with the ability to change their roles on the fly. Motorola's original specification (from the early 1980s) uses four logic signals, aka lines or wires, to support full duplex communication. It is sometimes called a four-wire serial bus to contrast with three-wire variants which are half duplex, and with the two-wire I²C and 1-Wire serial buses. Typical applications include interfacing microcontrollers with peripheral chips for Secure Digital cards, liquid crystal displays, analog-to-digital and digital-to-analog converters, flash and EEPROM memory, and various communication chips. Although SPI is a synchronous serial interface, it is different from Synchronous Serial Interface (SSI). SSI employs differential signaling and provides only a single simplex communication channel.
Operation
Commonly, SPI has four logic signals. Variations may use different names or have different signals.
MOSI on a master outputs to MOSI on a slave. MISO on a slave outputs to MISO on a master. Each device internally uses a shift register for serial communication, which together forms an inter-chip circular buffer. To support Multidrop bus, slave devices should use tri-state outputs so their MISO signal becomes high impedance (electrically disconnected) when the device is not selected. Slaves without tri-state outputs cannot share a MISO line with other slaves without using an external tri-state buffer.
Data transmission
To begin communication, the SPI master first selects the device it wants to communicate with by pulling its SS low. (The bar above SS indicates it is an active low signal, so a low voltage means "selected", while a high voltage means "not selected") If a waiting period is required, such as for an analog-to-digital conversion, the master must wait for at least that period of time before issuing clock cycles. During each SPI clock cycle, full-duplex transmission of a single bit occurs. The master sends a bit on the MOSI line while the slave sends a bit on the MISO line, and then each reads their corresponding incoming bit. This sequence is maintained even when only one-directional data transfer is intended. Transmission using a single slave involves one shift register in the master and one shift register in the slave, both of some given word size (e.g. 8 bits). The transmissions often consist of eight-bit words, but other word-sizes are also common, for example, sixteen-bit words for touch-screen controllers or audio codecs, such as the TSC2101 by Texas Instruments, or twelve-bit words for many digital-to-analog or analog-to-digital converters. Data is usually shifted out with the most-significant bit (MSB) first but the original specification has a LSBFE ("LSB-First Enable") to control whether data is transferred least (LSB) or most significant bit (MSB) first. On the clock edge, both master and slave shift out a bit to its counterpart. On the next clock edge, each receiver samples the transmitted bit and stores it in the shift register as the new least-significant bit. After all bits have been shifted out and in, the master and slave have exchanged register values. If more data needs to be exchanged, the shift registers are reloaded and the process repeats. Transmission may continue for any number of clock cycles. When complete, the master stops toggling the clock signal, and typically deselects the slave. If a single slave device is used, its SS pin may be fixed to logic low if the slave permits it. With multiple slave devices, a multidrop configuration requires an independent SS signal from the master for each slave device, while a daisy-chain configuration only requires one SS signal. Every slave on the bus that has not been selected should disregard the input clock and MOSI signals. And to prevent contention on MISO, non-selected slaves must use tristate output. Slaves that are not already tristate will need external tristate buffers to ensure this.
Clock polarity and phase In addition to setting the clock frequency, the master must also configure the clock polarity and phase with respect to the data. Motorola named these two options as CPOL and CPHA (for clock polarity and clock phase) respectively, a convention most vendors have also adopted.
The SPI timing diagram shown is further described below:
CPOL represents the polarity of the clock. Polarities can be converted with a simple inverter. SCLKCPOL=0 is a clock which idles at the logical low voltage. SCLKCPOL=1 is a clock which idles at the logical high voltage. CPHA represents the phase of each data bit's transmission cycle relative to SCLK. For CPHA=0: The first data bit is output immediately when SS activates. Subsequent bits are output when SCLK transitions to its idle voltage level. Sampling occurs when SCLK transitions from its idle voltage level. For CPHA=1: The first data bit is output on SCLK's first clock edge after SS activates. Subsequent bits are output when SCLK transitions from its idle voltage level. Sampling occurs when SCLK transitions to its idle voltage level. Conversion between these two phases is non-trivial. MOSI and MISO signals are usually stable (at their reception points) for the half cycle until the next bit's transmission cycle starts, so SPI master and slave devices may sample data at different points in that half cycle, for flexibility, despite the original specification.
Mode numbers The combinations of polarity and phases are referred to by these "SPI mode" numbers with CPOL as the high order bit and CPHA as the low order bit:
Notes:
Another commonly used notation represents the mode as a (CPOL, CPHA) tuple; e.g., the value '(0, 1)' would indicate CPOL=0 and CPHA=1. In Full Duplex operation, the master device could transmit and receive with different modes. For instance, it could transmit in Mode 0 and be receiving in Mode 1 at the same time. Different vendors may use different naming schemes, like CKE for clock edge or NCPHA for the inversion of CPHA.
… excerpt ends here. Continue reading the full article.





