SPI Protocol Basics.

SPI (Serial Peripheral Interface) is a serial peripheral communication protocol. It was developed by Motorola but is now used by many manufacturers. It is designed for communication between microcontrollers, as well as with all kinds of peripherals: sensors, ADCs, memory chips, clocks. But still, the most common use of SPI is writing a program into a microcontroller's memory. With AVR microcontrollers, SPI lets you program the microcontroller without removing it from the board; this programming method is called ISP (In-System Programming). Although the names SPI and ISP sound very similar, they're not the same thing: on AVR, SPI is used as the physical layer of ISP, i.e. the SPI lines are used to transfer data, but the protocol itself (the software layer) is different.

SPI uses three lines to transfer data:
 
MISO (Master Input Slave Output) – on this line the Master receives data from the Slave.
 
MOSI (Master Output Slave Input) – on this line the Master sends data to the Slave.
 
SCK (Serial Clock) – used to send the clock signal to the slave device.
 
The SS (Slave Select) line is also used; it determines which device the Master will exchange data with.
SPI Protocol Basics.

Because many manufacturers use SPI in their devices, the pin names may differ somewhat. Below is a table of alternative names.
SPI Protocol Basics.

SPI comes in two kinds: hardware and software. With a software SPI implementation, we must manually set the signals on the pins corresponding to MISO, MOSI, SS while toggling SCK. With a hardware SPI implementation, we pass data to a special register, and the microcontroller performs the manipulations described above by itself, according to the pre-configured settings.

The physical implementation of SPI is two shift registers connected together.
SPI Protocol Basics.

Depending on which logic level of the SCK signal the Master and Slave are synchronized on, and on which edge data is captured and shifted, there are 4 possible SPI modes.
SPI Protocol Basics.

  • CPOL = 0 — the clock signal starts at a low level;
  • CPOL = 1 — the clock signal starts at a high level;
  • CPHA = 0 — data is sampled on the leading edge of the clock signal;
  • CPHA = 1 — data is sampled on the trailing edge of the clock signal.

The oscillograms below show what sending 0x17 looks like in the different modes.
CPOL = 0 CPHA = 0
SPI Protocol Basics.

CPOL = 1 CPHA = 0
SPI Protocol Basics.

CPOL = 0 CPHA = 1
SPI Protocol Basics.

CPOL = 1 CPHA = 1
SPI Protocol Basics.


 SPI is a synchronous interface, i.e. to get any data from the Slave, the Master must send something. Everything seems clear, but what if the Master sends one byte and the Slave has to return two? In that case the Master must send it something twice, for example 0x00.

//send a command; two bytes should come back in response
                Spi_Master_Transmit(chx);
//send something in order to receive the first byte
		Spi_Master_Transmit(0X00);
		touch_x = SPDR;
		touch_x <<= 8;
//send something in order to receive the second byte		
		Spi_Master_Transmit(0X00);
		touch_x |= SPDR;
		touch_x >>= 3;

That's all; above is an example of code taken from a working project.