Introduction
SPI, or Serial Peripheral Interface, is one of the popular communications protocols typically implemented in microcontrollers and commonly used to interact with peripheral devices (sensors, displays, etc).
SPI is a synchronous communications protocol, meaning that it has a dedicated clock line (please see our article on Synchronous vs Asynchronous Communications Protocols for more information). The communications lines in SPI are:
- MISO = Master In Slave Out
- MOSI = Master Out Slave In
- SS = Slave Select
- SCK or SCLK = Serial Clock
As per the naming, there are Master and Slave devices in SPI. The Master is the device which controls the clock line, and therefore controls the communication.
SPI is a full duplex protocol, as there are two data lines - one for each communication direction (master-to-slave and slave-to-master) – meaning that data can be both sent and received at the same time.
The SPI Bus
Multiple slave devices can be connected to the same master using the same SCK, MISO and MOSI communication lines (called the SPI Bus). A separate Slave Select (SS) line is however required for each slave; the master sets the Slave Select lines (which are active low - i.e. low to select, high to deselect) depending on which slave it wishes to communicate with.
In practice it is rare that data will be both sent and received on the SPI bus at the same time (usually the scenario is instruction followed by response, or just the master reading from the slave). However one of the advantages of SPI is that it is straightforward to debug and troubleshoot (such as with an oscilloscope) due to having dedicated and clearly defined communications lines. Protocols like I2C require fewer communications lines, however are more difficult to troubleshoot due to their complexity.
Another advantage of SPI is that all the communications lines are actively driven either high or low, as opposed to e.g. I2C where the communications lines are only ever driven low and pull-up resistors are used to passively pull the lines back to high when they are not actively being driven (for more information please see our article on I2C). Having the lines always actively driven means that higher data rates can be achieved.
Dual and Quad SPI
There are also variations on SPI called Dual and Quad SPI, where more than one data line can be used to transmit data in a single direction at the same time (technically in parallel), which further increasing data throughput (overall communication speed). These variations are popular with e.g. FLASH memory chips where high data throughput can be required. With Dual SPI communication starts off as per usual (the same as Single SPI), however can be changed to Dual mode after a command is sent to the slave device (the details of this will be available in the slave device datasheet). In Dual mode the MISO and MOSI lines each become bi-directional (half-duplex) communication lines called SIO0 (Serial I/O 0) and SIO1; the even bits of each byte are transmitted on SIO0 and the odd bits of each byte are transmitted on SIO1.
Quad SPI is similar, except that there are an additional two communication lines – SIO2 and SIO3, and half-a-byte is transmitted at a time.
SPI Configuration
Some parameters in the SPI protocol are settable; such as whether data is sent/received on the rising or falling edge of the clock signal and whether the clock idles high or low. It is also configurable whether data is transmitted Least Significant Bit (LSB) first or Most Significant Bit (MSB) first – MSB first is the usual case.
In practice it is rare that data will be both sent and received on the SPI bus at the same time (usually the scenario is instruction followed by response, or just the master reading from the slave). However one of the advantages of SPI is that it is straightforward to debug and troubleshoot (such as with an oscilloscope) due to having dedicated and clearly defined communications lines. Protocols like I2C require fewer communications lines, however are more difficult to troubleshoot due to their complexity.
If you are not familiar with the terms least significant bit and most significant bit then please see our article on Endianness & Bit Significance.
Check out the Proteus Sample Design Library for loads of examples of SPI sensor communications and general protocol usage with various microcontrollers.
All content Copyright Labcenter Electronics Ltd. 2024. Please acknowledge Labcenter copyright on any translation and provide a link to the source content on www.labcenter.com with any usage.Get our articles in your inbox
Never miss a blog article with our mailchimp emails
Advanced Simulation
Learn more about our built in graphing and advanced simulation features. Harness the mixed-mode simulation engine in Proteus to quickly test your analogue or digital circuitry directly on the schematic.
Ask An Expert
Have a Question? Ask one of Labcenters' expert technical team directly.