Introduction

SPI Function : An SPI interface is a synchronous serial communication between the Microcontroller and the connected Peripheral Modules.
The Microcontroller is configured as a Master device which provides the main clock and the connected Module is configured as a slave device.
The SPI communication uses 4 pinouts,
- SDO is used for serial data output.
- SDI is used for the serial data input.
- SCLK is used for the clock signal from Master to Slave.
- CS is used for the chip select to select the slave Module chip.
The Shift Register shifts the data in and out of the device, the data is shifted out of the SDO pin of one device and input through the SDI pin of the other Module.
Once a Byte of the data is transferred through the shift register, it is copied to the buffer register, the buffer register contents are then read by the Modules.
The Master device initiates the data transfer and the slave device waits for the clock from the master device.
SPI flowchart : Master

Master SPI function : As shown in the flowchart the initial steps are required to setup the variables and define the SPI ports.
A loop checks if the 8 bit data is transferred, once the data is transferred the data is read. SPI is exchange of data and the data is read at the end of the transfer even if there is no use of the data for the master.
SPI flowchart : Slave

Slave SPI function : The flowchart for the slave setup is shown above, initially the variables need to be setup and ports need to be defined, Once the slave receives the 8 bit data input from the SDI pin, it transfers the data to the buffer register which can be used for further processing by the slave module.
Terminology
SPI : Serial Peripheral Interface
MOSI : Master Out slave Input is the data output from Master
MISO : Master In slave Output is the data output from the Slave
SCLK : Serial Clock ( Output from the Master)
CS : Chip Select ( typically active low output from Master)