Understanding SPI Communication
Understanding SPI Communication
Other communication protocols include CAN, I2C, Ethernet, USB, RS232, and RS485, but SPI is
widely used due to its simplicity and speed.
SPI Pins
SPI communication requires four main pins:
MISO (Master In Slave Out) – Used for data transmission from slave to master.
MOSI (Master Out Slave In) – Used for data transmission from master to slave.
SCK (Serial Clock) – The clock signal generated by the master to synchronize communication.
SS (Slave Select) – Used to select a specific slave device when multiple slaves are connected.
Key Points
SPI is synchronous, meaning communication is controlled by a clock signal.
The master generates the clock, and the slave follows it.
If SPI communication is not working, the first debugging step is to check whether the master is
generating the clock signal.
The SS pin is only required when multiple slave devices are present.
Slave Selection:
The master selects the slave by pulling the Slave Select (SS) pin LOW (0) using a GPIO pin.
This is crucial because the slave remains inactive (high impedance state) until SS is pulled low.
Data Transmission:
Once selected, the slave activates its MISO and MOSI lines.
The master sends data along with a clock signal (SCK) to synchronize communication.
Upendra Kumar
Pulling SS LOW is mandatory; otherwise, the slave remains inactive.
In some cases, only MOSI (Master Out, Slave In) and SCK (Clock) are needed.
If the slave only receives data (does not send any data back), the MISO line is not required.
The SS (Slave Select) pin can be permanently grounded if only one slave is used.
Example:
2-wire SPI: Only MOSI and SCK are used when the slave only receives data.
3-wire SPI: MOSI, MISO, and SCK (no SS if only one slave).
The number of SPI lines depends on how many slaves are present and how data is exchanged.
The MOSI (Master Out, Slave In) and MISO (Master In, Slave Out) lines connect the devices.
The serial clock (SCK), generated by the master, synchronizes data transfer.
The master loads data (e.g., A0-A7) into its shift register.
After one clock cycle, the LSB (A0) is pushed out on MOSI and moves to the slave’s MSB.
Simultaneously, the slave's LSB (B0) moves out on MISO and enters the master's MSB.
Upendra Kumar
With each clock cycle, bits shift one position in both registers.
After 8 clock cycles, the master’s data is fully transferred to the slave, and the slave’s data is fully
transferred to the master.
SPI works on shift registers, where data moves bit-by-bit with each clock pulse.
SPI always exchanges data – when the master sends data, it also receives data (if the MISO line is
connected).
The process is full-duplex, meaning both transmission and reception happen simultaneously.
SPI is naturally full duplex, meaning data can be transmitted and received
simultaneously.
Clock (SCK) synchronizes the shift registers of both master and slave.
Uses only one data line (instead of two) to save GPIO pins.
Not all microcontrollers support Half Duplex SPI, so it’s important to check the reference
manual of the MCU.
Two types:
Upendra Kumar
Receive-Only Mode: Master only receives data, Slave only transmits.
Full Duplex: Two-way communication (default mode, uses MOSI & MISO).
2. Shift Register
The shift register width is up to 16 bits, supporting both 8-bit and 16-bit data transfers.
Generates the clock signal (SCK) when SPI operates in Master mode.
If the shift register is free, the data moves from TX buffer to the shift register, and
transmission begins.
A TX Buffer Empty interrupt is triggered when the TX buffer is ready for new data.
Upendra Kumar
5. RX Buffer (Receive Buffer)
Once a full frame (8 or 16 bits) is received, the data is moved to the RX buffer.
A RX Buffer Full interrupt is triggered when new data is available for reading.
SPI interacts with the APB (Advanced Peripheral Bus) for data transfer.
Transmission Path:
If the shift register is free, data moves from TX Buffer → Shift Register → MOSI Line.
Reception Path:
TX Buffer Empty Interrupt → Signals when the TX buffer is ready for new data.
Shift registers handle SPI data transfer, shifting bits on each clock pulse.
TX and RX buffers manage data flow, ensuring smooth transmission and reception.
Enabled by setting the SSM bit = 1 in the SPI Control Register (CR1).
Upendra Kumar
SSI = 1 → NSS is internally pulled high (Slave is deselected).
For successful communication, the master and slave must use the same settings; otherwise, data
corruption can occur.
Common formats:
Both master and slave must have the same frame size.
CPOL defines the idle state of the serial clock (SCK) when no data is being transferred.
Example:
Important Rule:
Upendra Kumar
If CPHA = 0, data is captured on the first clock edge.
SPI has four different modes, determined by CPOL and CPHA settings.
Simplified Rule:
Mode 1 (CPOL = 0, CPHA = 1) → Used when slave needs more time to stabilize data.
Mode 3 (CPOL = 1, CPHA = 1) → Used for specific peripherals that require different clock timing.
The internal SPI prescaler value configured in the SPI baud rate control register.
To determine the maximum SPI serial clock (SCK) speed, follow these steps:
Apply the internal SPI prescaler to determine the final SCK frequency.
However, in our application, we are using the internal 16 MHz HSI oscillator, meaning:
APB1 Bus runs at 16 MHz → SPI2 & SPI3 max SCK = 8 MHz
Upendra Kumar
The SPI peripheral has an internal prescaler that further divides the APB clock to set the
SPI clock speed.
/4 → 4 MHz
/8 → 2 MHz
If the APB1 clock is increased to 42 MHz, SPI2 & SPI3 can achieve up to 21 MHz.
3. User-Configurable Parameters
Clock Polarity (CPOL) and Clock Phase (CPHA) – Determines clock behavior.
SPI Speed (Baud Rate Prescaler) – Controls the SPI clock speed.
} SPI_RegDef_t;
Upendra Kumar
Step for SPI Init
1. Configure the Device Mode (Master/Slave)
2. Implementing SPI_SendData()
Upendra Kumar