Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Serial Peripheral Interface
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Operation== [[File:SPI_single_slave.svg|thumb|368x368px|Single master to single slave: basic SPI wiring]] Commonly, SPI has four logic signals. [[#Variations|Variations]] may use different [[#Alternative terminology|names]] or have different signals. :{| class="wikitable" ! Abbr.!! Name !! Description |- | {{center|{{Overline|SS}}}} || {{center|Slave Select}} || [[Logic level#Active state|Active-low]] [[chip select]] signal from master to<br />enable communication with a specific slave device |- | {{center|SCLK}} || {{center|Serial Clock}} || [[Clock signal]] from master |- | {{center|MOSI}} || {{center|Master Out Slave In}} || [[Serial communication|Serial data]] output from master |- | {{center|MISO}} || {{center|Master In Slave Out}} || [[Serial communication|Serial data]] output from slave |} 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]]. Slave devices should use [[tri-state output]]s 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=== [[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 master first selects a slave device by pulling its {{Overline|SS}} low. (Note: the bar above {{Overline|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.{{NoteTag|Some slaves require a falling edge of the {{Overline|Slave Select}} signal to initiate an action. An example is the Maxim MAX1242 ADC, which starts conversion on a highโlow transition.}} 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 {{Overline|SS}} pin ''may'' be fixed to [[logic level|logic low]] if the slave permits it. With multiple slave devices, a [[#Multidrop configuration|multidrop configuration]] requires an independent {{Overline|SS}} signal from the master for each slave device, while a [[#Daisy chain configuration|daisy-chain configuration]] only requires one {{Overline|SS}} signal. Every slave 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 slaves must use [[Three-state logic|tristate]] output. Slaves that aren't already tristate will need external tristate buffers to ensure this.<ref name="Better SPI Bus Design in 3 Steps" /> ===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<ref>[https://web.archive.org/web/20150413003534/http://www.ee.nmt.edu/~teare/ee308l/datasheets/S12SPIV3.pdf SPI Block Guide v3.06; Motorola/Freescale/NXP; 2003.]</ref><ref name=":4" /> named these two options as CPOL and CPHA (for '''c'''lock '''pol'''arity and '''c'''lock '''pha'''se) respectively, a convention most vendors have also adopted. [[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]].]] 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 which idles at the [[logical low]] voltage. ** SCLK{{Subscript|1=CPOL=1}} is a clock which idles at the logical high voltage. * 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|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'' {{Overline|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. ** Note: 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: {| class="wikitable" |- ! SPI mode ! Clock polarity<br />(CPOL) ! Clock phase<br />(CPHA) !Data is shifted out on ! Data is sampled on |- | 0 || 0 || 0 |falling SCLK, and when {{Overline|SS}} activates || rising SCLK |- | 1 || 0 || 1 | rising SCLK || falling SCLK |- | 2 || 1 || 0 | rising SCLK, and when {{Overline|SS}} activates || falling SCLK |- | 3 || 1 || 1 |falling SCLK|| rising SCLK |} 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. === 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 an IC's [[scan chain]] 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=== Interrupts are outside the scope of SPI; their usage is neither forbidden nor specified, and so may optionally be implemented. ==== From master to slave ==== Microcontrollers configured as slave devices may have hardware support for generating interrupt signals to themselves when data words are received or overflow occurs in a receive [[FIFO (computing and electronics)|FIFO]] buffer,<ref>{{Cite web |date=2002 |title=TMS320x281x Serial Peripheral Interface Reference Guide |url=https://www.ti.com/lit/pdf/spru059 |website=[[Texas Instruments]] |pages=16โ17}}</ref> and may also set up an interrupt routine when their slave select input line is pulled low or high. ==== From slave to master ==== SPI slaves sometimes use an [[out-of-band signal]] (another wire) to send an interrupt signal to a master. Examples include pen-down interrupts from [[touchscreen]] sensors, thermal limit alerts from [[List of temperature sensors|temperature sensors]], alarms issued by [[real-time clock]] chips, [[Secure Digital#SDIO|SDIO]]{{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 master is connected to the SDIO line of a slave.|name=3wireSDI}} and [[audio jack]] insertions for an [[audio codec]]. Interrupts to master may also be faked by using [[Polling (computer science)|polling]] (similarly to [[USB 1.1]] and [[USB 2.0|2.0]]). ===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 or a [[bit-banging]] software implementation. ===Bit-banging the protocol=== The [[pseudocode]] below outlines a software implementation ("[[bit-banging]]") of SPI's protocol as a master with simultaneous output and input. This pseudocode is for CPHA=0 and CPOL=0, thus SCLK is pulled low before {{Overline|SS}} is activated and bits are inputted on SCLK's rising edge while bits are outputted on SCLK's falling edge. * Initialize SCLK as low and {{Overline|SS}} as high * Pull {{Overline|SS}} low to select the slave * 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 slave's [[setup time]] *** Pull SCLK high *** Left-Shift the next input bit from MISO into{{Code|byte_in|C}} *** NOP for the slave's hold time *** Pull SCLK low ** {{Code|byte_in|C}}now contains that recently-received byte and can be used as desired * Pull {{Overline|SS}} high to unselect the slave Bit-banging a slave's protocol is similar but different from above. An implementation might involve [[busy waiting]] for {{Overline|SS}} to fall or triggering an [[Interrupt handler|interrupt routine]] when {{Overline|SS}} falls, and then shifting in and out bits when the received SCLK changes appropriately for however long the transfer size is.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)