ArticleslgStudy

computer science

Serial Peripheral Interface

Serial Peripheral Interface is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Serial Peripheral Interface rather than just read about it. In short: 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, pe…

Serial Peripheral Interface — main illustration
Serial Peripheral Interface — illustration

Key takeaways

  • Serial Peripheral Interface belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Serial Peripheral Interface to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Serial Peripheral Interface from memory before moving on to harder problems.

Reference excerpt

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.

Illustrations

Serial Peripheral Interface: A typical hardware setup using two shift registers to form an inter-chip circular buffer
A typical hardware setup using two shift registers to form an inter-chip circular buffer
Serial Peripheral Interface: Serial Peripheral Interface animation. This animation shows a scenario where the master sends 8-bit data and simultaneously reads 16-bit data from the slave. The lower boxes represent the device's memories.
Serial Peripheral Interface animation. This animation shows a scenario where the master sends 8-bit data and simultaneously reads 16-bit data from the slave. The lower boxes represent the device's memories.
Serial Peripheral Interface: SPI timing diagram for both clock polarities and phases. Data bits output on blue lines if CPHA=0, or on red lines if CPHA=1, and sample on opposite-colored lines. Numbers identify data bits. Z indicates high impedance.
SPI timing diagram for both clock polarities and phases. Data bits output on blue lines if CPHA=0, or on red lines if CPHA=1, and sample on opposite-colored lines. Numbers identify data bits. Z indicates high impedance.
Serial Peripheral Interface: Multidrop SPI bus
Multidrop SPI bus
Serial Peripheral Interface: Daisy-chained SPI
Daisy-chained SPI

Worked examples

Example 1 — a first encounter with Serial Peripheral Interface

Start with the simplest possible case. Write down what Serial Peripheral Interface claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Serial Peripheral Interface before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Serial Peripheral Interface ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Serial Peripheral Interface

In research
Serial Peripheral Interface appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Serial Peripheral Interface in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Serial Peripheral Interface is common in secondary-school and first-year university syllabi. It links to neighbouring topics Computer buses, Computer hardware standards, Interfaces, so understanding it makes those chapters shorter.
In everyday life
Look for Serial Peripheral Interface outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Serial Peripheral Interface” →

How to study Serial Peripheral Interface in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Serial Peripheral Interface means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Serial Peripheral Interface out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Serial Peripheral Interface in simple terms?

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…

Why does Serial Peripheral Interface matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Serial Peripheral Interface?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Serial Peripheral Interface.

Tags

  • Computer buses
  • Computer hardware standards
  • Interfaces
  • Serial buses

Keep exploring