I2C Peripheral: Addressing, Clock Stretching, and Errors
What You'll Learn
- 1Generate start, restart, and stop sequences correctly
- 2Handle seven bit addressing and acknowledge bits
- 3Detect and recover from bus errors
- 4Explain clock stretching and slave timing
The inter integrated circuit bus is a two wire, multi master capable link with addressing built into the protocol. Both lines are open drain with pull-up resistors, so any device can pull low and the wired logic resolves the high level. Every transaction starts with a start condition, carries an address byte with a read or write bit, and ends with a stop condition or a repeated start. Its simplicity hides real timing and error handling work, which is where most implementations go wrong.
Addressing and Transfer Phases
A seven bit address plus the direction bit makes the first byte of every transaction, and ten bit addressing exists for parts that need more devices. After the address byte the addressed slave pulls the data line low during the acknowledge slot; a high level there is a non acknowledge that means the device is absent or busy. Multi byte reads usually use a repeated start so the master keeps control of the bus between the register write and the data read.
Reserved addresses exist for general call, high speed mode, and other purposes, so a scan that reports unexpected addresses often means a device is answering on a pattern you did not intend. Many sensor data sheets show the address with the read bit set, which is a frequent source of an off by one address in code.
Clock Stretching and Timing
A slave that needs more time holds the clock line low after the master releases it, and the master must wait rather than continue, which is clock stretching. The low and high phases of the clock have minimum times set by the standard mode, fast mode, and fast mode plus specifications, and they depend on the pull-up value and bus capacitance. Too weak a pull-up gives slow rise times and missed clock edges at higher speed.
Rise time is the part people forget: a bus with four hundred picofarads of capacitance and weak pull-ups will not reach the logic threshold in time, producing intermittent failures that depend on how many devices and how much cable are attached. Sizing pull-ups from the standard tables is a hardware task that software assumptions cannot fix.
Error Detection and Bus Recovery
The peripheral reports acknowledge failure, bus error on a misplaced stop or start, arbitration loss when another master wins, and overrun in the data register. Arbitration loss is not a fault in multi master systems, but on a single master bus it usually means noise or a stuck line. If the bus wedges with the data line held low, a recovery sequence clocks the line up to nine times until the slave releases it, then issues a stop.
Timeout logic on parts that support it converts a hung transaction into an error flag instead of a firmware lockup. The robust pattern is to treat every I2C call as fallible, wrap transfers in a bounded retry, and run a bus clear routine when errors repeat.
I2C Transaction Steps
| Step | Bus Action | Master Responsibility |
|---|---|---|
| Start | Pull data low while clock is high | Begin every transaction |
| Address byte | Seven bits plus direction bit | Match the device data sheet |
| Acknowledge | Slave pulls data low in the ninth clock | Check the flag, do not assume success |
| Data bytes | Clocked with acknowledges | Read or write as the direction says |
| Repeated start | New start without a stop | Keep control between write and read |
| Stop | Data goes high while clock is high | Release the bus for other masters |
Real-World Examples
- Read a temperature register â Send start, address with write, register index, repeated start, address with read, then read bytes and stop.
- Detect missing devices â Probe each address and treat a non acknowledge as absent rather than retrying forever.
- Recover a stuck bus â Toggle the clock line up to nine times, then generate a stop, then retry the transfer.
- Size the pull-ups â Compute the rise time from bus capacitance and pull-up value and pick a resistor that meets the mode specification.
â ī¸Check the Acknowledge on Every Address Byte
Common Mistakes
Forgetting the repeated start between write and read
Fix: Sending a stop in between lets other masters or slave logic change state and corrupts register reads.
Using pull-ups that are too weak
Fix: Slow rise times cause missed clock edges and work only at low speed or on a lightly loaded bus.
Retrying without clearing error flags
Fix: Stale error state keeps the peripheral reporting failure even after the bus is healthy.
Key Takeaways
- I2C frames every transaction with start, address, acknowledge, and stop.
- The acknowledge slot is the primary device presence check.
- Clock stretching and rise time are the usual timing failure sources.
- Bus recovery routines convert a wedged bus into a recoverable error.
Frequently Asked Questions
Why does my device not acknowledge?
Check power, the exact seven bit address, the read or write bit, and whether the device needs time after power up before it answers.
Can two masters share one I2C bus?
Yes, arbitration is defined, but most designs use single master to avoid the complexity of clock synchronization.
What does clock stretching mean for the master?
The master must pause and wait for the slave to release the clock, so its driver must tolerate a stretched clock period.
Interview Questions
Q1: Describe a register read over I2C.
Start, write the device address and register index, repeated start, read the address with the read bit, receive bytes with the required acknowledge pattern, then stop.
Q2: How do you recover a bus stuck low?
Clock the line manually up to nine times to let a slave finish shifting out its byte, issue a stop condition, then retry the transfer.
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