Interface
The SPI bus specifies four logic signals: * SCLK: Serial Clock (output from master) * MOSI: Master Out Slave In (data output from master) * MISO: Master In Slave Out (data output from slave) * CS /SS: Chip/Slave Select (oftenOperation
The SPI bus can operate with a single master device and with one or more slave devices. If a single slave device is used, the SS pin ''may'' be fixed to logic low if the slave permits it. Some slaves require a falling edge of the chip select signal to initiate an action. An example is the Maxim MAX1242 ADC, which starts conversion on a high→low transition. With multiple slave devices, an independent SS signal is required from the master for each slave device. Most slave devices have tri-state outputs so their MISO signal becomes high impedance (''electrically disconnected'') when the device is not selected. Devices without tri-state outputs cannot share SPI bus segments with other devices without using an external tri-state buffer.Data transmission
''(Note: refer to the ‘ Variations’ section later in this article for further details.)'' To begin communication, the bus master configures the clock, using a frequency supported by the slave device, typically up to a few MHz. The master then selects the slave device with a logic level 0 on the select line. 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, a full-duplex data transmission occurs. The master sends a bit on the MOSI line and the slave reads it, while the slave sends a bit on the MISO line and the master reads it. This sequence is maintained even when only one-directional data transfer is intended. Transmissions normally involve two shift registers of some given word-size, such as eight bits, one in the master and one in the slave; they are connected in a virtual ring topology. Data is usually shifted out with the most significant bit first. On the clock edge, both master and slave shift out a bit and output it on the transmission line to the counterpart. On the next clock edge, at each receiver the bit is sampled from the transmission line and set as a new least-significant bit of the shift register. After the register 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. Transmissions often consist of eight-bit words. However, 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. Multiple SPI devices may also be daisy-chained to conserve pins. Every slave on the bus that has not been activated using its chip select line must disregard the input clock and MOSI signals and should not drive MISO (i.e., must have a tristate output) although some devices need external tristate buffers to implement 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 SPI Block Guide names these two options as CPOL and CPHA (for clock polarity and phase) respectively, a convention most vendors have also adopted. TheMode numbers
The combinations of polarity and phases are often referred to as modes which are commonly numbered according to the following convention, with CPOL as the high order bit and CPHA as the low order bit: For "Independent slave configuration
In the independent slave configuration, there is an independent chip select line for each slave. This is the way SPI is normally used. The master asserts only one chip select at a time. Pull-up resistors between power source and chip select lines are recommended for systems where the master's chip select pins may default to an undefined state.Better SPI Bus Design in 3 StepsDaisy chain configuration
Some products that implement SPI may be connected in a daisy chain configuration, the first slave output being connected to the second slave input, etc. The SPI port of each slave is designed to send out during the second group of clock pulses an exact copy of the data it received during the first group of clock pulses. The whole chain acts as a communication shift register; daisy chaining is often done with shift registers to provide a bank of inputs or outputs through SPI. Each slave copies input to output in the next clock cycle until active low SS line goes high. Such a feature only requires a single SS line from the master, rather than a separate SS line for each slave. Other applications that can potentially interoperate with SPI that require a daisy chain configuration include SGPIO,Expander configuration
Another variation uses exactly two chip selects. One chip select controls a block of selection logic, the other is routed by the selection logic. The application is common enough that there are available serial-controlled multiplexers. This can standardize and future-proof a connector, so that a controller can support many devices with a change of software. A similar application pairs serial controlled multiplexer with a merchant USB-to-SPI controller, controlled by a PC or smart-phone. This permits many types of "dumb" electronic devices to be controlled by inexpensive mass-produced computers.High reliability modifications
In electrically noisy environments, the SPI standard has few signals, and it can be economical to reduce the effects of common mode noise by adapting SPI to use low-voltage differential signaling. Another advantage is that the controlled devices can be designed to loop-back to test signal integrity.Valid communications
Some slave devices are designed to ignore any SPI communications in which the number of clock pulses is greater than specified. Others do not care, ignoring extra inputs and continuing to shift the same output bit. It is common for different devices to use SPI communications with different lengths, as, for example, when SPI is used to access the scan chain of a digital IC by issuing a command word of one size (perhaps 32 bits) and then getting a response of a different size (perhaps 153 bits, one for each pin in that scan chain).Interrupts
SPI devices sometimes use another signal line to send an interrupt signal to a host CPU. Examples include pen-down interrupts from touchscreen sensors, thermal limit alerts from temperature sensors, alarms issued by real time clock chips,Software design
SPI lends itself to a "bus driver" software design. Software for attached devices is written to call a "bus driver" that handles the actual low-level SPI hardware. This permits the driver code for attached devices to port easily to other hardware, including a bit-banging design.Example of bit-banging the master protocol
Below is an example ofPros and cons
Advantages
* Full duplex communication in the default version of this protocol * Push-pull drivers (as opposed to open drain) provide good signal integrity and high speed * Higher throughput than I²C or SMBus. Not limited to any maximum clock speed, enabling potentially high speed * Complete protocol flexibility for the bits transferred ** Not limited to 8-bit words ** Arbitrary choice of message size, content, and purpose * Extremely simple hardware interfacing ** Typically lower power requirements than I²C or SMBus due to less circuitry (including pull up resistors) ** No arbitration or associated failure modes - unlike CAN-bus ** Slaves use the master's clock and do not need precision oscillators ** Slaves do not need a unique address unlike I²C or GPIB orDisadvantages
* Requires more pins on IC packages than I²C, even in the '' three-wire'' variant * No in-band addressing; out-of-band chip select signals are required on shared buses * Extensibility severely reduced when multiple slaves using different SPI Modes are required. Access is slowed down when master frequently needs to reinitialize in different modes. * No hardware flow control by the slave (but the master can delay the next clock edge to slow the transfer rate) * No hardware slave acknowledgment (the master could be transmitting to nowhere and not know it) * Typically supports only one master device (depends on device's hardware implementation) * No error-checking protocol is defined * Without a formal standard, validating conformance is not possible * Only handles short distances compared to RS-232, RS-485, or CAN-bus. (Its distance can be extended with the use of transceivers like RS-422.) * Opto-isolators in the signal path limit the clock speed for MISO transfer because of the added delays between clock and data * Many existing variations, making it difficult to find development tools like host adapters that support those variations * SPI does not support hot swapping (dynamically adding nodes). * Interrupts must either be implemented with out-of-band signals or be faked by using periodic polling similarly to USB 1.1 and 2.0. * Some variants like dual SPI, quad SPI, and three-wire serial buses defined below are half-duplex.Applications
The board real estate savings compared to a parallel I/O bus are significant, and have earned SPI a solid role in embedded systems. That is true for most system-on-a-chip processors, both with higher end 32-bit processors such as those using ARM, MIPS, orStandards
The SPI bus is a ''de facto'' standard. However, the lack of a formal standard is reflected in a wide variety of protocol options. Different word sizes are common. Every device defines its own protocol, including whether it supports commands at all. Some devices are transmit-only; others are receive-only. Chip selects are sometimes active-high rather than active-low. Some protocols send the least significant bit first. SPI Bus was originally defined by Motorola. The original defining document was Motorola Application Node AN991. When Freescale spun out of Motorola, the responsibility for AN991 went with Freescale. WhenVariations
Some devices have minor variances from the CPOL/CPHA modes described above. Sending data from slave to master may use the opposite clock edge as master to slave. Devices often require extra clock idle time before the first clock or after the last one, or between a command and its response. Some devices have two clocks, one to read data, and another to transmit it into the device. Many of the read clocks run from the chip select line. Some devices require an additional flow control signal from slave to master, indicating when data is ready. This leads to a 5-wire protocol instead of the usual 4. Such a ''ready'' or ''enable'' signal is often active-low, and needs to be enabled at key points such as after commands or between words. Without such a signal, data transfer rates may need to be slowed down significantly, or protocols may need to have dummy bytes inserted, to accommodate the worst case for the slave response time. Examples include initiating an ADC conversion, addressing the right page of flash memory, and processing enough of a command that device firmware can load the first word of the response. (Many SPI masters do not support that signal directly, and instead rely on fixed delays.) Many SPI chips only support messages that are multiples of 8 bits. Such chips can not interoperate with theDevelopment tools
When developing or troubleshooting systems using SPI, visibility at the level of hardware signals can be important.Host adapters
There are a number of USB hardware solutions to provide computers, running Linux, Mac, or Windows, SPI master or slave capabilities. Many of them also provide scripting or programming capabilities (Visual Basic, C/C++, VHDL, etc.). An SPI host adapter lets the user play the role of a master on an SPI bus directly from a PC. They are used for embedded systems, chips (FPGA, ASIC, and SoC) and peripheral testing, programming and debugging. The key parameters of SPI are: the maximum supported frequency for the serial interface, command-to-command latency and the maximum length for SPI commands. It is possible to find SPI adapters on the market today that support up to 100 MHz serial interfaces, with virtually unlimited access length. SPI protocol being a de facto standard, some SPI host adapters also have the ability of supporting other protocols beyond the traditional 4-wire SPI (for example, support of quad-SPI protocol or other custom serial protocol that derive from SPI).Protocol analyzers
SPI protocol analyzers are tools which sample an SPI bus and decode the electrical signals to provide a higher-level view of the data being transmitted on a specific bus.Oscilloscopes
Most oscilloscope vendors offer oscilloscope-based triggering and protocol decoding for SPI. Most support 2-, 3-, and 4-wire SPI. The triggering and decoding capability is typically offered as an optional extra. SPI signals can be accessed via analog oscilloscope channels or with digital MSO channels.Logic analyzers
When developing or troubleshooting the SPI bus, examination of hardware signals can be very important. Logic analyzers are tools which collect, analyze, decode, and store signals so people can view the high-speed waveforms at their leisure. Logic analyzers display time-stamps of each signal level change, which can help find protocol problems. Most logic analyzers have the capability to decode bus signals into high-level protocol data and show ASCII data.Related terms
Intelligent SPI controllers
A Queued Serial Peripheral Interface (QSPI; see also Quad SPI) is a type of SPI controller that uses a data queue to transfer data across the SPI bus. It has a wrap-around mode allowing continuous transfers to and from the queue with only intermittent attention from the CPU. Consequently, the peripherals appear to the CPU as memory-mapped parallel devices. This feature is useful in applications such as control of an A/D converter. Other programmable features in QSPI are chip selects and transfer length/delay. SPI controllers from different vendors support different feature sets; such DMA queues are not uncommon, although they may be associated with separate DMA engines rather than the SPI controller itself, such as used by Multichannel Buffered Serial Port (MCBSP). Most SPI master controllers integrate support for up to four chip selects, although some require chip selects to be managed separately through GPIO lines.Microwire
Microwire, often spelled μWire, is essentially a predecessor of SPI and a trademark of National Semiconductor. It's a strict subset of SPI: half-duplex, and using SPI mode 0. Microwire chips tend to need slower clock rates than newer SPI versions; perhaps 2 MHz vs. 20 MHz. Some Microwire chips also support a three-wire mode.Microwire/Plus
Microwire/Plus is an enhancement of Microwire and features full-duplex communication and support for SPI modes 0 and 1. There was no specified improvement in serial clock speed.Three-wire serial buses
As mentioned, one variant of SPI uses a single bidirectional data line (slave out/slave in, called SISO or master out/master in, called MOMI) instead of two unidirectional ones (MOSI and MISO). This variant is restricted to a half duplex mode. It tends to be used for lower performance parts, such as small EEPROMs used only during system startup and certain sensors, and Microwire. Few SPI master controllers support this mode; although it can often be easily bit-banged in software.Dual SPI
For instances where the full-duplex nature of SPI is not used, an extension uses both data pins in a half-duplex configuration to send two bits per clock cycle. Typically a command byte is sent requesting a response in dual mode, after which the MOSI line becomes SIO0 (serial I/O 0) and carries even bits, while the MISO line becomes SIO1 and carries odd bits. Data is still transmitted msbit-first, but SIO1 carries bits 7, 5, 3 and 1 of each byte, while SIO0 carries bits 6, 4, 2 and 0. This is particularly popular among SPI ROMs, which have to send a large amount of data, and comes in two variants: * Dual read commands the send and address from the master in single mode, and return the data in dual mode. * Dual I/O commands send the command in single mode, then send the address and return data in dual mode.Quad SPI
Quad SPI (QSPI; see also Queued SPI) goes beyond dual SPI, adding two more I/O lines (SIO2 and SIO3) and sends 4 data bits per clock cycle. Again, it is requested by special commands, which enable quad mode after the command itself is sent in single mode. ; SQI Type 1: Commands sent on single line but addresses and data sent on four lines ; SQI Type 2: Commands and addresses sent on a single line but data sent/received on four linesQPI/SQI
Further extending quad SPI, some devices support a "quad everything" mode where ''all'' communication takes place over 4 data lines, including commands. This is variously called "QPI" (not to be confused with Intel QuickPath Interconnect) or "serial quad I/O" (SQI) This requires programming a configuration bit in the device and requires care after reset to establish communication.Double data rate
In addition to using multiple lines for I/O, some devices increase the transfer rate by usingIntel Enhanced Serial Peripheral Interface Bus
Intel has developed a successor to its Low Pin Count (LPC) bus that it calls the Enhanced Serial Peripheral Interface Bus, or eSPI for short. Intel aims to allow the reduction in the number of pins required on motherboards compared to systems using LPC, have more available throughput than LPC, reduce the working voltage to 1.8 volts to facilitate smaller chip manufacturing processes, allow eSPI peripherals to share SPI flash devices with the host (the LPC bus did not allow firmware hubs to be used by LPC peripherals), tunnel previous out-of-band pins through the eSPI bus, and allow system designers to trade off cost and performance. The eSPI bus can either be shared with SPI devices to save pins or be separate from the SPI bus to allow more performance, especially when eSPI devices need to use SPI flash devices. This standard defines an Alert# signal that is used by an eSPI slave to request service from the master. In a performance-oriented design or a design with only one eSPI slave, each eSPI slave will have its Alert# pin connected to an Alert# pin on the eSPI master that is dedicated to each slave, allowing the eSPI master to grant low-latency service because the eSPI master will know which eSPI slave needs service and will not need to poll all of the slaves to determine which device needs service. In a budget design with more than one eSPI slave, all of the Alert# pins of the slaves are connected to one Alert# pin on the eSPI master in a wired-OR connection, which will require the master to poll all the slaves to determine which ones need service when the Alert# signal is pulled low by one or more peripherals that need service. Only after all of the devices are serviced will the Alert# signal be pulled high due to none of the eSPI slaves needing service and therefore pulling the Alert# signal low. This standard allows designers to use 1-bit, 2-bit, or 4-bit communications at speeds from 20 to 66 MHz to further allow designers to trade off performance and cost. All communications that were out-of-band of the LPC bus like general-purpose input/output (GPIO) andSee also
*References
External links