I2C bus architecture (SDA, SCL) in Embedded C - Time & Space Complexity
We want to understand how the time to communicate on an I2C bus changes as we send more data.
How does the number of bits sent affect the total time taken?
Analyze the time complexity of the following code snippet.
// Send one byte over I2C
void i2c_send_byte(uint8_t data) {
for (int i = 0; i < 8; i++) {
// Set SDA line to bit value
SDA = (data & 0x80) ? 1 : 0;
data <<= 1;
// Toggle SCL line to clock the bit
SCL = 1;
delay();
SCL = 0;
delay();
}
}
This code sends one byte (8 bits) over the I2C bus by setting the data line (SDA) and toggling the clock line (SCL) for each bit.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that sends each bit of the byte.
- How many times: Exactly 8 times, once for each bit in the byte.
Each byte sent requires 8 clock cycles. If we send more bytes, the time grows by 8 times the number of bytes.
| Input Size (bytes) | Approx. Operations (bit clocks) |
|---|---|
| 10 | 80 |
| 100 | 800 |
| 1000 | 8000 |
Pattern observation: The time grows linearly with the number of bytes sent.
Time Complexity: O(n)
This means the time to send data grows directly in proportion to the number of bytes you send.
[X] Wrong: "Sending more bytes takes the same time as sending one byte because the clock speed is fixed."
[OK] Correct: Even though the clock speed is fixed, each byte requires multiple clock cycles, so more bytes mean more total time.
Understanding how communication time grows with data size helps you design efficient embedded systems and explain your reasoning clearly in interviews.
"What if we changed the code to send 16 bits instead of 8 bits per data unit? How would the time complexity change?"
Practice
Solution
Step 1: Recall I2C bus lines
I2C uses two specific lines: SDA and SCL.Step 2: Identify roles of lines
SDA carries data, and SCL carries the clock signal.Final Answer:
SDA for data and SCL for clock -> Option CQuick Check:
I2C lines = SDA + SCL [OK]
- Confusing I2C lines with SPI lines
- Mixing data and clock line names
- Assuming more than two lines are used
Solution
Step 1: Understand line behavior on I2C bus
Lines are open-drain and pulled high by resistors when released.Step 2: Determine line state when idle
When no device pulls lines low, both SDA and SCL remain high.Final Answer:
Both lines are high -> Option DQuick Check:
Idle I2C lines = high [OK]
- Assuming lines default to low
- Confusing line states during communication
- Ignoring pull-up resistor effect
if (SDA == 1 && SCL == 1) {
// Start condition detected
} else if (SDA == 0 && SCL == 1) {
// Stop condition detected
}
What is the output behavior of this code snippet?Solution
Step 1: Recall I2C start and stop conditions
Start condition: SDA falls low while SCL is high. Stop condition: SDA rises high while SCL is high.Step 2: Analyze code conditions
The code checks if SDA and SCL are both high for start, which is incorrect; start is when SDA falls low while SCL is high.Final Answer:
Detects start when SDA goes low while SCL is high -> Option AQuick Check:
Start = SDA low + SCL high [OK]
- Mixing start and stop conditions
- Checking both lines high for start
- Ignoring SDA line change direction
if (SDA == 1 && SCL == 0) {
// Stop condition detected
}
What is the error in this code?Solution
Step 1: Recall stop condition timing
Stop condition occurs when SDA rises from low to high while SCL is high.Step 2: Check code condition
The code checks SDA high and SCL low, which is incorrect; SCL must be high for stop.Final Answer:
Stop condition requires SCL to be high, not low -> Option AQuick Check:
Stop = SDA high + SCL high [OK]
- Confusing clock line state for stop
- Assuming SDA low means stop
- Ignoring line state transitions
Solution
Step 1: Understand start condition in I2C
Start condition is when SDA line falls from high to low while SCL line is high.Step 2: Match logic to condition
Wait until SDA changes from high to low while SCL is high correctly waits for SDA falling edge with SCL high, matching start condition.Final Answer:
Wait until SDA changes from high to low while SCL is high -> Option BQuick Check:
Start = SDA high-to-low + SCL high [OK]
- Waiting for SDA rising edge instead of falling
- Checking SCL low instead of high
- Confusing start with stop condition
Ready to prove it?
Pass the test and this pattern moves unknown
