Content deleted Content added
Stevebroshar (talk | contribs) →Alternative terminology: organize better |
|||
(25 intermediate revisions by 17 users not shown) | |||
Line 1:
{{Short description|Synchronous serial communication interface}}
{{Cleanup rewrite|[[WP:NOTTEXTBOOK|it reads like a guide or textbook]]|article|date=March 2021}}
{{Infobox connector <!-- NOTE: Not a perfect template match, but good enough for now -->
| name = Serial Peripheral Interface (SPI)
Line 8 ⟶ 7:
| data_style = [[Full-duplex]] [[serial communication|serial]]
| daisy_chain = [[#Daisy chain configuration|Depends]] on devices
| manufacturer =
| data_devices = [[#Multidrop configuration|Multidrop]] limited by
| data_bit_width = 1 bit (bidirectional)
| high_freq = yes
Line 17 ⟶ 16:
| pin_custom2_name = MISO
| pin_custom3_name = SCLK
| pin_custom4_name = {{Overline|
| pin_name_custom3 = Serial Clock
| pin_name_custom4 = [[
| pinout_notes = (pins may have [[#Alternative terminology|alternative names]])
| maximum_voltage = Unspecified
| maximum_current = Unspecified
| physical_connector = Unspecified
| design_date = Around early 1980s{{NoteTag|The earliest definitive mention of a "Serial Peripheral Interface" in bitsavers archives of Motorola manuals is from 1983 (see {{slink||Original definition}}). While some sources on the web allege that Motorola introduced SPI when 68000 was introduced in 1979, however many of those appear to be [[citogenesis]] or speculation, and Motorola's 1983 68000 manual has no mention of "Serial Peripheral Interface", so the alleged 1979 date
}}
'''Serial Peripheral Interface''' ('''SPI''') is a
SPI follows a [[master–slave (technology)|master–slave architecture]],
[[Motorola]]'s original specification (from the early 1980s) uses four [[
Typical [[#Applications|applications]] include interfacing [[microcontrollers]] with peripheral chips for [[Secure Digital]] cards, [[liquid crystal display]]s, [[analog-to-digital]] and [[digital-to-analog converters]], [[Flash memory#Serial flash|flash]] and [[EEPROM#Serial bus devices|EEPROM]] memory, and various communication chips.
Line 38 ⟶ 37:
==Operation==
[[File:
Commonly, SPI has four
:{| class="wikitable"
! Abbr.!! Name !! Description
|-
| {{center|{{Overline|
|-
| {{center|SCLK}} || {{center|
|-
| {{center|MOSI}} || {{center|
|-
| {{center|MISO}} || {{center|
|}
MOSI on a
Each device internally uses a [[shift register]] for serial communication, which together forms an inter-chip [[circular buffer]].
===Data transmission===
[[File:SPI 8-bit circular transfer.svg|upright=1.8|thumb|A typical hardware setup using two [[shift register]]s to form an inter-chip [[circular buffer]] ]]
To begin communication, the SPI
If a waiting period is required, such as for an analog-to-digital conversion, the
During each SPI clock cycle, full-duplex transmission of a single bit occurs. The
Transmission using a single
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 sub device is used, its {{Overline|CS}} pin ''may'' be fixed to [[logic level|logic low]] if the sub permits it. With multiple sub devices, a [[#Multidrop configuration|multidrop configuration]] requires an independent {{Overline|CS}} signal from the main for each sub device, while a [[#Daisy chain configuration|daisy-chain configuration]] only requires one {{Overline|CS}} signal.▼
▲If a single
Every sub on the bus that has not been selected should disregard the input clock and MOSI signals. And to prevent [[Bus contention|contention]] on MISO, non-selected subs must use [[Three-state logic|tristate]] output. Subs that aren't already tristate will need external tristate buffers to ensure this.<ref name="Better SPI Bus Design in 3 Steps" />▼
▲Every
===Clock polarity and phase===
In addition to setting the clock frequency, the
[[File:SPI timing diagram CS.svg|thumb|338x338px|SPI [[Digital timing diagram|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]].]]
Line 80 ⟶ 82:
The SPI [[digital timing diagram|timing diagram]] shown is further described below:
* CPOL represents the polarity of the clock. Polarities can be converted with a simple [[inverter (logic gate)|inverter]].
** SCLK{{Subscript|1=CPOL=0}} is a clock
** SCLK{{Subscript|1=CPOL=1}} is a clock
* CPHA represents the [[Phase (waves)|phase]] of each data bit's transmission cycle relative to SCLK.
** For CPHA=0:
*** The first data bit is output ''immediately'' when {{Overline|
*** 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'' {{Overline|
*** 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.
**
===Mode numbers ===
Line 105 ⟶ 107:
|-
| 0 || 0 || 0
|falling SCLK, and when {{Overline|
|-
| 1 || 0 || 1
Line 111 ⟶ 113:
|-
| 2 || 1 || 0
| rising SCLK, and when {{Overline|
|-
| 3 || 1 || 1
Line 120 ⟶ 122:
* 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
* Different vendors may use different naming schemes, like CKE for clock edge or NCPHA for the inversion of CPHA.
=== Valid communications ===
Some
===Interrupts===
Interrupts are outside the scope of SPI; their usage is neither forbidden nor specified, and so may
==== From
Microcontrollers configured as
==== From
SPI
===Software design===
Line 139 ⟶ 141:
===Bit-banging the protocol===
The [[pseudocode]] below outlines a software
* Initialize SCLK as low and {{Overline|
* Pull {{Overline|
* Loop for however many number of bytes to transfer:{{NoteTag|Peripherals may allow or require a particular number (or any number) of transfer bytes while selected, as specified in their datasheet.}}
** Initialize{{Code|byte_out|C}}with the next output byte to transmit
** Loop 8 times:
*** [[Left-shift operator|Left-Shift]]{{NoteTag|Left-shifts are used because SPI normally transmits the most-significant bit first. Right-shifts could instead be used to transfer least-significant bit first.}} the next output bit from{{Code|byte_out|C}}to MOSI
*** [[NOP (code)|NOP]] for the
*** Pull SCLK high
*** Left-Shift the next input bit from MISO into{{Code|byte_in|C}}
*** NOP for the
*** Pull SCLK low
** {{Code|byte_in|C}}now contains that recently-received byte and can be used as desired
* Pull {{Overline|
Bit-banging a
== Bus topologies ==
Though the previous operation section focused on a basic interface with a single
===Multidrop configuration===
[[File:
In the [[multidrop bus]] configuration, each
Since the MISO pins of the
===Daisy chain configuration===
[[File:
Some products that implement SPI may be connected in a [[Daisy chain (electrical engineering)|daisy chain]] configuration, where the first
In addition to using SPI-specific
===Expander configurations===
Expander configurations use SPI-controlled addressing units (e.g. [[Binary decoder|binary decoders]], [[Demultiplexer|demultiplexers]], or shift registers) to add chip selects.
For example, one {{Overline|
==Pros and cons==
Line 189 ⟶ 191:
** Arbitrary choice of message size, content, and purpose
* Simple hardware and interfacing
** Hardware implementation for
***
***
***
*** Results in simple bit-banged software implementation
** Uses only four pins on IC packages, and wires in board layouts or connectors, much fewer than [[Parallel communication|parallel interfaces]]
*** At most one unique signal per device ({{Overline|
****
** Typically lower power requirements than [[I²C]] or SMBus due to less circuitry (including pull up resistors)
** Single
** Transceivers are not needed - unlike [[CAN-bus]]
** Signals are unidirectional, allowing for easy [[galvanic isolation]]
Line 205 ⟶ 207:
* Requires more pins on IC packages than [[I²C]], even in [[Serial Peripheral Interface#Three-wire|three-wire]] variants
* Only handles short distances compared to [[RS-232]], [[RS-485]], or [[CAN-bus]] (though distance can be extended with the use of transceivers like [[RS-422]])
* Extensibility severely reduced when multiple
** Access is slowed down when
* No formal standard
** So validating conformance is not possible
** Many existing variations complicate support
* No built-in protocol support for some conveniences:
** No hardware [[flow control (data)|flow control]] by the
** No hardware
** No error-checking protocol
** No [[hot swapping]] (dynamically adding nodes)
Line 232 ⟶ 234:
* Control devices: [[audio codec]]s, [[digital potentiometers]], [[digital-to-analog converter|DACs]]
* Camera lenses: [[Canon EF lens mount]]
* Memory: [[flash memory#Serial flash|flash]] and [[EEPROM#Serial bus devices|EEPROMs]]
* [[Real-time clock|Real-time clocks]]
* [[LCD|LCDs]], sometimes even for managing image data
* Any [[MultiMediaCard|MMC]] or [[Secure Digital|SD]] card (including [[Secure Digital#SDIO|SDIO]] variant{{NoteTag|Not to be confused with the SDIO (Serial Data I/O) line of the half-duplex implementation of SPI sometimes also called "3-wire" SPI. Here e.g. MOSI (via a resistor) and MISO (no resistor) of a
* [[Shift registers]] for additional I/O<ref name=":3" /><ref name=":2" />
[[Printed circuit board|Board]] real estate and wiring savings compared to a [[Parallel communication|parallel]] 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 architecture|ARM]], [[MIPS architecture|MIPS]], or [[PowerPC]] and with lower-end microcontrollers such as the [[Atmel AVR|AVR]], [[PIC microcontroller|PIC]], and [[MSP430]]. These chips usually include SPI controllers capable of running in either
Chip or [[FPGA]] based designs sometimes use SPI to communicate between internal components; on-chip real estate can be as costly as its on-board cousin. And for high-performance systems, [[FPGA]]s sometimes use SPI to interface as a
The full-duplex capability makes SPI very simple and efficient for single
==Variations==
<!-- regardless of lack of standards -->SPI implementations have a wide variety of protocol variations. Some devices are transmit-only; others are receive-only.
=== Original definition ===
Line 254 ⟶ 255:
===Timing variations===
Some devices have timing variations from Motorola's CPOL/CPHA modes. Sending data from
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
=== Transmission size ===
Different transmission word sizes are common. Many SPI chips only support messages that are multiples of 8 bits. Such chips can not interoperate with the [[JTAG]] or [[SGPIO]] protocols, or any other protocol that requires messages that are not multiples of 8 bits.
=== No
Some devices
=== Connectors ===
Line 268 ⟶ 269:
=== Flow control ===
Some devices require an additional [[Flow control (data)|flow control]] signal from
=== SafeSPI ===
SafeSPI<ref>[http://SafeSPI.org SafeSPI.org]</ref> is an high industry standard for SPI in automotive applications. Its main focus is the transmission of sensor data between different devices.
=== High reliability modifications ===
Line 279 ⟶ 280:
A '''Queued Serial Peripheral Interface''' ('''QSPI'''; different to but has same abbreviation as ''Quad SPI'' described in {{Slink|2=Quad SPI|nopage=y}}) is a type of SPI controller that uses a [[queue (data structure)|data queue]] to transfer data across an SPI bus.<ref>{{Cite web |date=1996 |orig-date=1991 |title=Freescale Semiconductor, Inc. - QSM - Queued Serial Module - Reference Manual |url=https://www.nxp.com/docs/en/reference-manual/QSMRM.pdf |url-status=dead |archive-url=https://web.archive.org/web/20190824080750/https://www.nxp.com/docs/en/reference-manual/QSMRM.pdf |archive-date=2019-08-24 |website=[[NXP]]}}</ref> It has a [[circular buffer|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 [[virtual memory|memory-mapped]] parallel devices. This feature is useful in applications such as control of an [[analog-to-digital converter|A/D converter]]. Other programmable features in Queued SPI are chip selects and transfer length/delay.
SPI controllers from different vendors support different feature sets; such [[direct memory access]] (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''').{{NoteTag|Such as with the MultiChannel Serial Port Interface, or McSPI, used in Texas Instruments OMAP chips. (https://www.ti.com/product/OMAP3530)}} Most SPI
Note that ''Queued SPI'' is different from ''Quad SPI'', and some processors even confusingly allow a single "QSPI" interface to operate in either quad or queued mode!<ref>{{Cite web |date=2023-01-11 |title=Quad-SPI Brings Fast Parallel Data Transmission |url=https://resources.pcb.cadence.com/blog/quad-spi-brings-fast-parallel-data-transmission |url-status=live |archive-url=https://web.archive.org/web/20230601194620/https://resources.pcb.cadence.com/blog/quad-spi-brings-fast-parallel-data-transmission |archive-date=2023-06-01 |access-date=2023-06-30 |website=[[Cadence Design Systems]] |language=en-US}}</ref>
Line 290 ⟶ 291:
=== Three-wire ===
Three-wire variants of SPI restricted to a [[half-duplex]] mode use a single bidirectional data line called SISO (
=== Dual SPI ===
Line 297 ⟶ 298:
This is particularly popular among SPI ROMs, which have to send a large amount of data, and comes in two variants:<ref name="W25Q16JV">{{cite web |title=W25Q16JV 3V 16M-bit serial flash memory with Dual/Quad SPI |date=12 August 2016 |version=Revision D |publisher=[[Winbond]] |type=data sheet |url=https://www.winbond.com/resource-files/w25q16jv%20spi%20revd%2008122016.pdf |access-date=2017-02-10}}</ref><ref name="D25LQ64">{{cite web |title=D25LQ64 1.8V Uniform Sector Dual and Quad SPI Flash |type=data sheet |date=11 February 2011 |version=version 0.1 |publisher=GigaDevice |url=https://www.sst-ic.com/File/DataSheet/KC909742-1112151725544ed0c4ce-8225-4c46-8d18-b81422086247.pdf |archive-url=https://web.archive.org/web/20170212090900/http://www.sst-ic.com/File/DataSheet/KC909742-1112151725544ed0c4ce-8225-4c46-8d18-b81422086247.pdf |url-status=dead |archive-date=12 February 2017 |access-date=2017-02-10}}</ref>
* Dual read
* Dual I/O
=== Quad SPI ===
Line 327 ⟶ 328:
An eSPI bus can either be shared with SPI devices to save pins or be separate from an SPI bus to allow more performance, especially when eSPI devices need to use SPI flash devices.<ref name="eSPI" />
This standard defines an Alert# signal that is used by an eSPI
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.<ref name="eSPI" />
This standard supports standard memory cycles with lengths of 1 byte to 4 kilobytes of data, short memory cycles with lengths of 1, 2, or 4 bytes that have much less overhead compared to standard memory cycles, and I/O cycles with lengths of 1, 2, or 4 bytes of data which are low overhead as well. This significantly reduces overhead compared to the LPC bus, where all cycles except for the 128-byte firmware hub read cycle spends more than one-half of all of the bus's throughput and time in overhead. The standard memory cycle allows a length of anywhere from 1 byte to 4 kilobytes in order to allow its larger overhead to be amortised over a large transaction. eSPI
eSPI
64-bit memory addressing is also added, but is only permitted when there is no equivalent 32-bit address.<ref name="eSPI" />
Line 341 ⟶ 342:
The Intel [[Z170|Z170 chipset]] can be configured to implement either this bus or a variant of the LPC bus that is missing its ISA-style DMA capability and is underclocked to 24 MHz instead of the standard 33 MHz.<ref>{{Cite web | url = https://www.intel.com/content/dam/www/public/us/en/documents/datasheets/100-series-chipset-datasheet-vol-1.pdf | title = Intel® 100 Series Chipset Family PCH Datasheet, Vol. 1 | access-date = April 15, 2015}}</ref>
The eSPI bus is also
==Development tools==
Line 349 ⟶ 350:
=== USB to SPI adapters ===
There are a number of [[USB]] adapters that allow a desktop [[Personal computer|PC]] or [[smartphone]] with [[USB]] to communicate with SPI chips (e.g. CH341A/B<ref>{{cite web |title=USB Bridge Controller CH341 with UART, SPI and I2C | url=https://wch-ic.com/products/CH341.html |access-date=27 February 2025 |website=WCH}}</ref> based or [[FTDI|FT]]221xs<ref>{{cite web |title=USB to SPI converter |url=https://ftdichip.com/products/ft221xs/ |access-date=14 February 2021 |website=FTDI|date=2 August 2020 }}</ref>). They are used for embedded systems, chips ([[FPGA]], [[Application-specific integrated circuit|ASIC]], and [[System on a chip|SoC]]) and peripheral testing, programming and debugging. Many of them also provide scripting or programming capabilities (e.g. [[Visual Basic]], [[C (programming language)|C]]/[[C++]], [[VHDL]]) and can be used with open source programs like [[Flashrom_(utility)|flashrom]], IMSProg, SNANDer or avrdude for [[Flash_memory|flash]], [[EEPROM]], [[Bootloader|bootloader]] and [[BIOS]] programming.
The key SPI parameters 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.
Line 367 ⟶ 368:
* Serial clock
** SCK, SCLK, CLK, SCL
* Master Out Slave In (MOSI)
** SIMO, MTSR, SPID - correspond to MOSI on both master and slave devices, connects to each other
**
**
** COPI, PICO for
* Master In Slave Out (MISO)
▲** COPI, PICO for "peripheral and controller",<ref name="OSHWA">[https://www.oshwa.org/a-resolution-to-redefine-spi-signal-names/ SPI; OSHWA.]</ref><ref name=":5">https://www.ti.com/lit/an/scea091a/scea091a.pdf {{Bare URL PDF|date=August 2024}}</ref> or COTI for "controller" and "target"<ref name=":1">{{Cite web |title=Serial Peripheral Interface (SPI) Devices |url=https://www.nxp.com/products/interfaces/ic-spi-i3c-interface-devices/www.nxp.com/pages/:SPI |url-status=live |archive-url=https://web.archive.org/web/20230601020101/https://www.nxp.com/products/interfaces/ic-spi-i3c-interface-devices/spi-interface-devices:SPI |archive-date=2023-06-01 |access-date=2023-07-22 |website=[[NXP]]}}</ref>
** SOMI, MRST, SPIQ - correspond to MISO on both master and slave devices, connects to each other
** SDO, DO, DOUT, SO - on
**
▲** SDO, DO, DOUT, SO - on sub-only devices; connects to MISO (or alternative names) on main
** CIPO, POCI,<ref name="OSHWA" /><ref name=":5" /> or CITO<ref name=":1" />
* Slave Select (SS)
**
** CE (chip enable)
** Historical:
[[Microchip Technology|Microchip]] uses ''host'' and ''client
==See also==
|