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.
The diagram shows the Interface and Direction between Master and Slave.
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.
For SPI Communication you need to configure following peripheral in MCU.
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.
Serial Peripheral Module is Inbuilt peripheral to configure following things:
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.
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.
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.
slave select line must be low until above three data does not transfer completely.
let ,
JOB2: Save Data into Flash memory
To accomplish this job following Data shall be transferred through MOSI.
slave select line must be low until above three data does not transfer completely.
let ,