Main Content

Support SPI Communication

SPI, or Serial Peripheral Interface, is a synchronous, full duplex serial communication protocol between high-speed devices over short distances. The SPI protocol supports a single Controller with one or more peripherals. The Controller can communicate to any peripheral on the bus, but each peripheral can communicate only with the Controller.

The SPI Controller Transfer block in the support package library enables communication with other SPI devices. You can use this block only when you use your hardware as the Controller device.

With SPI, you can:

  • Connect various sensors to boards to measure different quantities such as temperature, pressure.

  • Connect various shields to boards to enhance capabilities such as Wi-Fi® shield.

  • Access an SD card to store data or extend the available memory.

You can set SPI properties such as the SPI clock out frequency (in MHz), SPI mode, and the Bit order in the Configuration Parameters > Hardware Implementation > SPI properties section.

SPI Lines

SPI uses a four-wire serial bus for communication: SDI, SDO, SCK, and CS. The SDI, SDO, and the SCK lines are common to all devices. The SS line is specific to each peripheral.

  • SDI (Serial Data In) – This line is the peripheral line for sending data to the SPI Controller.

  • SDO (Serial Data Out) – This line is the Controller line for sending data to the SPI peripherals.

  • SCK (Serial Clock) –The Controller generates the clock pulses that synchronize the data transmission.

  • CS (Chip Select) – This is specific to the device. This is the pin on each device that the SPI Controller can use to enable and disable the device. This signal is an ‘active low’ signal which means a device becomes a peripheral when its SS pin is set to low.

The SPI lines over In Circuit Serial Programming (ICSP) header are consistent across all the boards shown as follows.

Data Transmission

  • The SPI Controller sets the clock with a frequency supported by the SPI peripheral with which the Controller wants to communicate.

  • The Controller selects the peripheral by setting the SS pin of peripheral to low (0). The Controller can select only one peripheral at a time.

  • As each SPI transfer is a full duplex transmission, the Controller sends a bit on SDO line and the peripheral reads it. The peripheral also sends a bit on the SDI line and the Controller reads it.

    When the Controller makes a data transfer, the peripheral cannot opt out of sending data. However, the peripheral device sends dummy bytes (usually all 1s or all 0s) when communication is one-way. Similarly, when the Controller reads data from a peripheral, the peripheral knows to ignore the data that the Controller sends.

  • When the transfer is complete, the Controller stops toggling the SCK and mostly pulls up the SS to deselect the peripheral.

  • During the data transmission, the other peripherals on the SPI bus that have not been selected by the Controller ignore the SCK and the SDO signals, and do not drive the SDI.

SPI Transfer Modes

An SPI Controller sets the clock polarity and the clock phase.

Clock Polarity (CPOL) – is the default value (HIGH/LOW) of SCK signal when the bus is idle.

CPOL = 0 means a default LOW value of SCK when bus is idle. CPOL = 1 means default HIGH value of SCK when bus is idle.

Clock Phase (CPHA) – indicates, if the clock data is sampled at LEADING (first) or TRAILING (second) edge of SCK.

CPHA = 0 means sample at LEADING edge of SCK and CPHA = 1 means sample at TRAILING edge of SCK, regardless of whether the clock edge is RISING or FALLING.

The combination of polarity and phase are referred to as SPI modes. The SPI modes 0–3 are shown in the table.

ModeClock Polarity (CPOL)Clock Phase (CPHA)

Mode 0

Mode 0

Mode 1

Mode 1

Mode 2

Mode 2

Mode 3

Mode 3