SPI Peripheral: Modes, Clock Polarity, and Transfers
What You'll Learn
- 1Match SPI mode to a device data sheet
- 2Explain full duplex shift register behavior
- 3Configure baud prescaling and frame format
- 4Move bulk SPI data with FIFO and DMA
The serial peripheral interface is a synchronous, full duplex link built around one clock and a shift register on each side. Master and slave exchange bits simultaneously, so a transfer of n bits moves n bits in each direction at once. The two configuration bits clock polarity and clock phase define four modes, and a device data sheet always specifies one of them. Because chip select is a plain GPIO in most designs, framing and timing are entirely the firmware responsibility.
Clock Polarity, Phase, and Modes
Clock polarity sets the idle level of the clock and clock phase decides whether data is sampled on the first or the second edge. Mode zero idles low and samples on the rising edge, mode three idles high and samples on the falling edge, and these two cover the majority of devices. Getting the mode wrong usually yields plausible but shifted data rather than an obvious failure.
The baud rate prescaler divides the peripheral clock into a fixed set of rates, so the achievable clock is rarely exact. Always check the maximum clock of the slave and of the trace, because a chip select setup time or a long cable can limit you long before the peripheral does.
Frame Format and Chip Select
Frames are usually eight or sixteen bits with the most significant bit sent first, although some devices want least significant first or a nine bit frame. Chip select is asserted before the first clock edge and released after the last, with setup and hold times specified by the slave. Software must therefore control the select pin with correct ordering, and hardware select modes can help only for simple single slave cases.
A common failure is changing the select between bytes of a multi byte command, which restarts the slave transaction. The fix is a single chip select asserted around the whole message, sometimes combined with a small delay after assertion for devices that need time to wake.
FIFO, Errors, and DMA
Modern SPI peripherals include a transmit and receive FIFO with a threshold interrupt, so the CPU moves bursts instead of single bytes. Receive overrun occurs when a byte arrives while the receive buffer is full, and mode fault flags a select conflict in multi master systems. Both must be cleared before restarting, or the peripheral stays latched in an error state.
For displays, flash, and sensors that move kilobytes, DMA is the normal choice: fill a transmit buffer, set the receive buffer to a discard address or a real capture buffer, and interrupt at completion. That frees the CPU and guarantees the clock never pauses mid frame, which some devices treat as an error.
SPI Modes at a Glance
| Mode | Clock Polarity | Sampling Edge |
|---|---|---|
| Mode 0 | Idle low | Sample on rising edge |
| Mode 1 | Idle low | Sample on falling edge |
| Mode 2 | Idle high | Sample on falling edge |
| Mode 3 | Idle high | Sample on rising edge |
| Typical use | Sensors and flash | Display and ADC devices |
Real-World Examples
- Talk to a flash chip â Assert chip select, send the command byte, keep select low for the address and data phases, then release it.
- Read an accelerometer â Send a read command with the register address while simultaneously clocking out the response byte.
- Drive a display â Use DMA with a large transmit buffer so pixel data streams without gaps in the clock.
- Debug a silent device â Scope clock polarity and phase against the data sheet timing diagram before touching the code.
â ī¸Chip Select Framing Is Part of the Protocol
Common Mistakes
Choosing the SPI mode by guesswork
Fix: The data sheet defines polarity and phase; the wrong mode gives shifted or inverted data instead of a clear error.
Exceeding the slave maximum clock
Fix: Slower parts mis-sample above their rated rate even when the master is happy.
Ignoring receive overrun
Fix: Bytes arriving while the receive buffer is full are dropped silently, so bulk transfers need FIFO or DMA.
Key Takeaways
- SPI is full duplex with one clock and a shift register on each side.
- Polarity and phase select one of four modes defined by the device.
- Chip select framing and setup times belong to the driver.
- FIFO and DMA keep the clock running during bulk transfers.
Frequently Asked Questions
Why does SPI work with one device but not another?
Each device defines its own mode, bit order, and select framing, so a shared bus needs per device configuration.
Is SPI faster than I2C?
Yes, because it is full duplex with no addressing overhead and can run at tens of megahertz on short traces.
Does SPI have flow control?
Not inherently; some devices add a busy line or require the master to poll a status register between bytes.
Interview Questions
Q1: Explain SPI mode zero versus mode three.
Mode zero idles the clock low and samples data on the rising edge, while mode three idles high and samples on the falling edge; both sample on the first edge after select asserts.
Q2: How do you make SPI non blocking?
Use the transmit or receive FIFO threshold interrupts, or hand the whole buffer to DMA and interrupt on completion.
Extended Learning: Beyond the Basics
Key Concepts Summary
This lesson covers fundamental concepts that serve as building blocks for more advanced topics. Master these basics before moving to intermediate and advanced material.
Prerequisite Knowledge
Before studying this topic in depth, ensure you understand:
- Basic definitions and terminology
- Core formulas and their derivations
- Common applications in real-world scenarios
- Typical pitfalls and misconceptions
Step-by-Step Walkthrough
Let us work through a complete example to solidify understanding:
Step 1: Identify the problem type. Determine which category this problem falls into and which approach is most appropriate.
Step 2: Apply the relevant formula. Substitute known values into the appropriate equation, paying attention to units and constraints.
Step 3: Simplify and solve. Work through the algebra step by step, checking each intermediate result.
Step 4: Verify the answer. Check that the solution satisfies the original conditions and makes logical sense.
Common Mistakes to Avoid
- Misidentifying the problem type â Always start by categorizing the problem
- Incorrect formula application â Verify the formula applies to your specific case
- Unit errors â Always check units are consistent throughout
- Sign errors â Pay careful attention to positive and negative values
- Rounding too early â Keep full precision until the final answer
- Forgetting constraints â Check that solutions satisfy all given conditions
Real-World Applications
Understanding this concept is essential for:
- Advanced problem solving in examinations
- Building intuition for more complex topics
- Developing analytical thinking skills
- Applying mathematical reasoning to practical situations
Practice Exercises
- Basic: Solve 5 problems of increasing difficulty using the core concept.
- Intermediate: Apply the concept to word problems with multiple steps.
- Advanced: Combine this concept with 2-3 other topics in a comprehensive problem.
- Challenge: Create your own problem that requires this concept plus additional knowledge.
Interview and Exam Tips
- Common question patterns: Identify which exam or interview formats test this topic
- Time management: Practice completing problems within time constraints
- Common traps: Know the typical mistakes examiners set as distractors
- Verification strategy: Develop a quick method to check your answer
Related Topics
- Previous Topic â Build on the foundation laid here
- Next Topic â See how this concept extends to more advanced material
- Practice Problems â Additional exercises with solutions
- Video Explanation â Visual walkthrough of key concepts
Quick Reference Card
| Concept | Formula/Rule | Key Insight | Common Use |
|---|---|---|---|
| Core Definition | See main text | Understand the intuition | Foundation for all applications |
| Key Formula | See main text | Memorize the derivation | Direct problem solving |
| Special Case | See main text | Recognize when it applies | Simplifies complex problems |
| Edge Case | See main text | Know the limitations | Avoids errors |
Further Reading
For students who want to go deeper:
- Standard textbooks on the subject
- Research papers applying this concept
- Online courses with video explanations
- Practice problem collections with detailed solutions