SPI Introduction
Introduction
Serial Peripheral Interface (SPI) is a synchronous serial communication protocol that can be used for short-distance and high-speed synchronous data transfer between embedded systems. SPI is a three or four-wire bus and SPI devices communicate in full-duplex mode with dedicated channels for transmitting and receiving data.
Advantages
- Higher speed (20MHz) than I2C (1MHz), SWI (2MHz)
- Dual, quad SPI up to 144MHz
- Widely used; NOR flash, EEPROM, EERAM, Serial SRAM, SecureDigital, LCD, ADC, DAC, RTCC
- Push-pull protocol - no pull-up resistors required
- Does not use unique addressing
- Protocol easily performed in software (bit-banged)
- Transceivers not required
Disadvantages
- No acknowledgement for received data
- No formal specification
Description
The SPI bus is a synchronous serial data communication bus that operates in Full Duplex mode, using one channel for transmitting and another channel for receiving data. The devices communicate in a host-client environment, with a single host at a time and one or more clients.
The SPI bus consists of four signal connections:
- SCK: Serial Clock (output from host)
- SDO: Serial Data Out (data output)
- SDI: Serial Data Input (data input)
- /SS: Serial Select
The host device is the only one that can generate a clock. Therefore, it is typically the initiator of the data exchange. The SPI host device uses the same SCK, SDO and SDI channels for all the clients, but usually individual lines of SS for each of the clients. However, the daisy-chain feature offers the possibility of using only one SS line to control more than one client device. With either the host or the client device, data is always shifted out one bit at a time, on the programmed clock edge and with the Most Significant bit (MSb) shifted out first. At the same time, a new Least Significant bit (LSb) is shifted into the same register.
Modes
SPI supports 4 modes. These modes represent the way in which data are transmitted with respect to the clock generation. The clock polarity and the clock edge are the important parameters for data modes. The clock polarity refers to the level of the signal in Idle state. The signal can be either low in Idle state, and start with a rising edge when transmitting data, or high in Idle state and start with a falling edge when transmitting data.
- SPI modes 0 and 1 SCK idles low:
- SPI modes 2 and 3 SCK idles high:
- SPI modes 0 and 3:
- Sample data on SDI on the rising edge of SCK:
- SPI modes 0 and 3:
- Output data on SDO on the falling edge of SCK:
- SPI modes 1 and 2:
- Sample data on SDI on the falling edge of SCK:
- SPI modes 1 and 2:
- Output data on SDO on the rising edge of SCK:
The accompanying image shows comparison for the 4 SPI modes:
Multidrop Configuration
Multidrop configuration uses a dedicated serial select pin for each client node. The host device selects the desired client by pulling the serial select signal low then transfers data to the selected client.
Daisy Chain Configuration
Daisy chain SPI is commonly used in applications where multiple SPI devices need to be connected to a single SPI host, but there are limited available SPI pins. By using daisy chain SPI, multiple devices can be connected in a series, reducing the number of pins and simplifying the overall system hardware design. In this configuration, the output of one device is connected to the input of the next device, and so on. The first device in the chain is the SPI host, which controls the communication between all the devices in the chain.
When data is to be transmitted, the shared /SS connection is driven low and data is sent from the SPI host to the first device in the chain. The first device processes the data and then passes it on to the next device in the chain. This process continues until the data reaches the last device in the chain. The Host transmits data in the reverse order of the daisy chained client nodes. This means the data for the last client node in the chain is transmitted first and the data for the for the first client node is transmitted last. After the number of transfers match the number of client nodes, all client nodes will have received their respective data.
Learn More
- Any applicable MCU datasheet with SPI or SPI capable peripheral
- Getting Started with SPI Using MSSP on PIC18
- TB3157: Serial Peripheral Interface (SPI) Communications on 8-Bit PIC® Microcontrollers
- AVR151: Setup and Use of the SPI