• admin@embedclogic.com

SPI Communication

SPI stands for Serial Peripheral Interface works on Master-slave concept.It is a synchronous serial communication used for short distance communication.

It is a full-duplex communication protocol as it has two separate lines for transmission(MOSI) and reception(MISO).SPI communication could be possible with a single master and a single slave as well as a single master with multiple slaves.

In multi-slave communication, Master will select a slave to establish communication through slave select (SS) line or chip select line(CS) by pulling it down.

It is also known as 4 wire protocol as it has 4 wire interface.

  1. MOSI (MTSR) – Master Out Slave In OR Master Transmit Slave Recieve
  2. MISO (MRST) – Master In Slave Out OR Master Recieve Slave Transmit
  3. SCLK (SCK)   – Serial clock
  4. CS(SS)           – Chip select or Slave Select(Active low Pin)

The diagram shows the Interface and Direction between Master and Slave.

SPI COMMUNICATION CONCEPT

Following Steps should be followed in SPI Communication.

Master should select Slave device through Slave select line by making it low—-SS

Master device configures the SPI peripheral clock supported by Slave also.——SCLK

Data availability on Data out(MOSI) and Data in(MISO) line controlled by the master generated Clock Pulse(SCLK).

Master generated clock pulse polarity(CPOL) and phase(CPHA) decides the data drive on the data line. see the below image.

CPOL: Decides the polarity of Clock.

CPHA: Determines the timing of the data bits relative to the clock pulse.

HARDWARE CONFIGURATION

For SPI Communication you need to configure following peripheral in MCU.

  1. Port Pins
  2. MCU clock
  3. SPI Peripheral Engine

Note: Here I am not Discussing DMA.

Port Pins: 

Direction and alternate function need to configure for the Port Pins those are used by SPI module. Suppose Following GPIO Pins are fixed by the manufacturer for SPI communication as an alternate function. See the following table –

MCU Clock:

We need to Configure the MCU clock and peripheral Clock to fix the SCLK frequency.

SPI Peripheral MODULE:

Serial Peripheral Module is Inbuilt peripheral to configure following things:

  1. SPI Hardware module (Some controllers have multiple SPI modules)
  2. SPI channel use for Communication
  3. SPI JOB and Sequence Configuration

SPI Hardware module :

Some microcontroller has multiple SPI modules. For Example, there are 5 SPI modules in Infineon Tricore microcontroller TC297X QSPI0, QSPI1, QSPI2…QPSI4.

We need to configure one of them to work as a Master or Slave through the API as per the Microcontroller Specific User Manual.

SPI channel Configuration:

Here channel refers to a Slave select line. Every SPI module has multiple channels i.e multiple Slave select lines i.e Master can communicate multiple slaves.

In below figure, three slave select lines(Channels) are present and each one is connected with different-2 slaves.

Master will take control of this slave select line and master can select or deselect the slave through SS line.

SPI JOB and Sequence Configuration:

Let’s take an example, suppose an MCU wants to save the data and Read that Data to/from Flash Memory. so here MCU as Master and Flash memory as a slave will perform following Jobs-

JOB1: Save Data into Flash memory

JOB2: Read the Data from flash memory

JOB1: Save Data into Flash memory


To accomplish this job following Data shall be transferred through MOSI.

  1. Write Command
  2. Address
  3. Data to write

slave select line must be low until above three data does not transfer completely.

let ,

  1. write command=0x12
  2. Address =0x00000000
  3. Data ={0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01}

JOB2: Save Data into Flash memory

To accomplish this job following Data shall be transferred through MOSI.

  1. Read Command
  2. Address

slave select line must be low until above three data does not transfer completely.

let ,

  1. Read command=0x13
  2. Address =0x00000000

Subscribe and stay updated with our latest articles.