Introduction to the SPI
SPI stands for Serial Peripheral Interface. It is one of the most widely used interfaces between the microcontroller or other peripheral devices such as sensors, shift registers, etc. It uses a Synchronous communication protocol [It shares a clock signal for synchronizing bits between 2 devices] for the communication between a Master[ a microcontroller] and slaves[other peripheral devices]. Usually, a [Master]microcontroller sends a clock signal to the slaves to start the communication between them.
SPI uses either 3 or 4 logic signals to communicate
- SCLK– Shared clock signal [Synchronize data transmission]
- MOSI– Master Out Slave In [Master sending data to the peripherals]
- MISO– Master In Slave Out [Master receiving data from the peripherals]
- CS/SS–Chip/Slave Select [Master indicates that data is being sent]
How do SPI works?
- CS/SS – To start a new transmission the Master pulls the chip select to a low voltage level which means taking the voltage to 0. When it’s done the chip select goes back to its non-transmitting state which is a high voltage level. It is also used for selecting the slave to which master wants to communicate when we have multiple peripherals. When we have multiple slaves they are get connected by the Daisy chain.
In Daisy Chain ChipSelect for all the slaves is connected together as in fig below and data propagates from one slave to the next. All the slaves receive the clock signal at the same time. Master sends the data to the first slave and so on.
When the data is transferring from one slave to the next, The number of clock cycles required to transmit data is proportional to the slave position.
Ex: Assuming we have 2 slaves and we need to transfer the data to the second slave.
We have an 8-bit system then we need to send 16 clock pulses[8clockpulses at once as we are taking an 8-bit system] to get to the second slave.
But Daisy Chain is not supported by all devices. Reading Data Sheet is recommended for that.
- SCLK – In SPI the clock signal is generated by the Master. The Master configures the clock, using a frequency supported by the slave device, and sends the clock signal to synchronize the data.
Clock Polarity and Clock Phase are the main parameters to define a clock signal which is described later.
- MOSI/MISO – As SPI is full-duplex, the Master can transmit and receive the data bit by bit at the same time. Master sends the data in series usually MSB goes the first and in MISO, the slave sends the LSB first to the master.
Modes of transmission
CPOL[clock polarity]- It is used to determine the idle state of the clock.
CPHA[clock phase]- Clock Phase determines at which edge data read/write occurs which is described in modes of transmission.
- If CPOL = 0 , data read/right occurs at falling edge then CPHA=0
- If CPOL = 0 , data read/right occurs at rising edge then CPHA=1
- If CPOL = 1 , data read/right occurs at rising edge then CPHA=0
- If CPOL = 1 , data read/right occurs at falling edge then CPHA=1
There are 4 modes of transmission in SPI:
Data Transmission Steps
- The Master selects the chip select line associated with the slave.
- The Serial Clock line is then enabled.
- Master starts sending one bit of data on the MOSI line with each clock pulse. Slave reads the MSB first and stores it into memory.
- MISO line sends the data to the Master. Master reads the LSB first and stores it into the memory.
Applications of SPI
- Memory: SD Card, MMC, EEPROM, Flash.
- Sensors: Temperature and Pressure.
- Control Devices: ADC, DAC, digital POTS, and Audio Codec.
- Others: Camera Lens Mount, touchscreen, LCD, RTC, video game controller, etc