SPI and I2C Protocols: Timing and Practical Use
What You'll Learn
- 1Configure SPI mode and clock parameters
- 2Address I2C devices and manage acknowledgements
- 3Diagnose bus hangs and perform recovery
- 4Choose SPI or I2C for a given peripheral
SPI and I2C cover most chip to chip needs on a board. SPI is fast, full duplex, and simple, but every device needs its own chip select line and the timing rules are configured manually. I2C uses two wires with addresses and acknowledgements, which scales to many devices at lower speed. Both protocols fail in characteristic ways: SPI mode mismatches corrupt data, while I2C devices can wedge the bus and need an explicit recovery sequence.
SPI Modes and Timing
SPI configures clock polarity and clock phase into four modes that decide when data is sampled relative to the clock edge. Bit order, clock frequency, and frame length must match the peripheral datasheet, and the chip select line must be asserted before the first clock edge and held through the last.
Signal integrity sets the practical maximum speed: long traces, heavy capacitance, and poor ground return round off edges. Reading a device while another shares the same bus requires tri state outputs so two drivers never fight.
I2C Addressing and Transfers
I2C devices appear at seven bit addresses and the bus uses start and stop conditions plus an acknowledge bit after every byte. A typical transfer writes a register pointer then reads data with a repeated start, which keeps the transaction atomic for the device.
Pull up resistors set rise time against bus capacitance; too weak a value fails rise time specs, too strong wastes current and may exceed sink limits. Clock stretching lets a slow device hold the clock low between bytes.
Bus Hangs and Recovery
A device interrupted mid transfer can hold the data line low forever, blocking everyone. Recovery clocks the data line high for nine pulses while the master generates clock edges manually, then issues a stop condition to free the bus.
Designs also add buffers for long buses, separate domains with level shifters, and a power on check that verifies each expected address acknowledges before normal operation begins. Those checks turn a silent wiring fault into a clear diagnostic message during start up instead of a mystery later in the field.
SPI and I2C Compared
| Aspect | SPI | I2C |
|---|---|---|
| Wires | Clock, data in, data out, select | Serial data and clock |
| Speed | Tens of megahertz typical | 100 kilohertz to 3.4 megahertz |
| Addressing | Chip select line per device | Seven bit address per device |
| Flow | No acknowledgement | Acknowledge after each byte |
| Best fit | Displays, flash, ADCs | Sensors, EEPROMs, PMICs |
Real-World Examples
- Flash memory read â Chip select low, mode 0 clock, command and address bytes, then data clocked back in a burst.
- Sensor register read â Write register pointer with a repeated start, then read bytes with acknowledgements from the sensor.
- Multiple sensors on one bus â Three devices at distinct addresses share the two wires with one shared pull up pair.
- Bus recovery sequence â Nine clock pulses plus a stop condition release a slave that stalled mid byte after a reset.
â ī¸Match SPI Mode Exactly
Common Mistakes
Oversized I2C pull-ups
Fix: Strong pull-ups exceed slave sink current and slow edges; calculate from bus capacitance.
Forgetting shared bus tri-state
Fix: Two devices driving the same data line corrupt each other without proper chip select discipline.
Ignoring clock stretching
Fix: Blocking reads from a stretching slave will time out if the driver assumes a fixed clock.
Key Takeaways
- SPI mode, bit order, and chip select discipline define correctness.
- I2C adds addressing and acknowledgement on just two wires.
- Pull-up sizing and capacitance set practical I2C limits.
- Recovery sequences restore a wedged bus without a power cycle.
Frequently Asked Questions
Why does my SPI device respond only at low speed?
Signal integrity limits usually appear first at high clock rates; shorten traces, improve ground return, and check mode settings.
What happens if two I2C devices share an address?
Both acknowledge, corrupting data; use address pins, a multiplexer, or a different part.
Do I2C buses need a common ground?
Yes, and they also need pull-ups on the segment that matches the logic level of the devices.
Interview Questions
Q1: How do you recover an I2C bus stuck low?
Toggle the data line manually nine times while generating clock pulses, then send a stop condition, and verify the bus idles high.
Q2: When would you choose SPI over I2C?
When throughput matters, transfer length is long, or you need full duplex, and you have enough pins for chip selects.
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