What is SPI?
SPI —> Serial Peripheral Interface is a 4-wire (sometimes 3) communication protocol. It is a higher speed, full-duplex, synchronous protocol used to communicate between a master (controller) device and one or more peripheral devices.
The 4 original wires of SPI are:
MOSI = Master Out Slave In
MISO = Master In Slave Out
SCLK = Serial Clock
CS = Chip Select
You might also see this terminology, but it is the same functionality:
PICO = Peripheral In Controller Out
POCI = Peripheral Out Controller In
SCLK = Serial Clock
CS = Chip Select
MOSI / PICO — is the data that is sent out from the controller.
MISO / POCI — is the data that is received at the controller shifted out by the peripheral device.
SCLK — is the serial clock provided by the controller device. This clock system keeps the system in time.
CS — is the chip select line. For SPI systems with multiple peripherals, a chip select signal is needed for each peripheral. CS “selects” the peripheral that the controller wants to talk to.
A SPI Controller and SPI Peripheral in point-to-point communication
Controller / Peripheral Architecture:
The SPI interface can be used between a controller and one or more peripheral devices.
The TMP125 is a SPI interface temperature sensor. A SPI controller can read temperature data from the TMP125 through the SPI interface protocol.
Each peripheral in a SPI system will need a chip-select (CS) signal.
4 peripherals in a SPI system each require 1 CS line
Full-Duplex:
SPI is a full-duplex communication protocol, meaning that data can be transferred and received at the same time. Each clock pulse provided on SCLK shifts data out from the controller, and simultaneously shifts data in.
A half-duplex protocol like I2C can only send or receive data at one point in time but never both. It uses one data line to send or receive data on. SPI uses two separate lines, one for data output, and one for data input.
SPI vs. I2C, SPI can transmit and receive data at the same time.
I2C can only send data or receive data, but not both at the same time.
Synchronous:
SPI is synchronous meaning it is "timed” based off a clock signal. Every rising edge on SCLK increments the next step in the SPI frame. The communication on the SPI bus only proceeds forward based off the timing of the clock. Data is sampled on every rising edge of SCLK.
I2C is another example of a synchronous protocol.
High-Speed:
The higher speeds of SPI are most commonly in the Mega Hertz (MHz) range.
Arduino’s maximum clock speed for the SPI clock is theoretically 8 MHz. The default setting is 4 MHz, and can operate down to 125 kHz.
The MSPM0 microcontroller from Texas Instruments can run a 12 Mbps clock for SPI.
Several FPGA’s can be used to write bit-by-bit SPI data at even higher data rates reaching speeds in the 10’s of MHz.
A device like TXE8116-Q1 can communicate with a controller clock speed of up to 10 MHz.
Simplicity:
SPI is easy to implement and only requires 4-wires. There are many tutorials online on how to implement SPI, and many devices that support the generalized protocol.
Additional Features:
SPI has additional features added by the manufacturer of the device being used. For example, some device in the market use a feature called daisy chaining. This reduces the number of connections needed by the controller and streamlines each peripheral in the chain. More on this later.
CRC or a cyclical redundancy check is used to ensure data transfers were successful and correct.
SPI can send data in different word lengths. 8-bit and 16-bit SPI frames are the most common, with 24-bit and 32-bit being supported by some SPI interface devices.
There are other more advanced techniques for SPI. Dual SPI, Quad SPI (QSPI), and Octal SPI (OSPI) are advanced techniques used for increased data through-put in systems that have large information payloads.
eSPI, Intelligent SPI controllers, DMA, FIFO buffers, programmable settings and registers maps, multi-controller support, and more. There is so much that SPI offers, but even the basic functionality of SPI is often times enough to solve most problems at hand.