0
0
Embedded Cprogramming~20 mins

I2C bus architecture (SDA, SCL) in Embedded C - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
I2C Bus Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
I2C SDA Line Behavior in Open-Drain Configuration

Consider the following embedded C code snippet simulating the behavior of the SDA line in an I2C bus with open-drain configuration. What will be the final state of the SDA line after executing this code?

Embedded C
/* SDA line simulated as a variable: 0 = low, 1 = released (pulled high by resistor) */
int SDA = 1; // Initially released (high)

// Device A pulls SDA low
SDA = 0;

// Device B tries to release SDA
SDA = 1;

// What is the actual SDA line state now?
int final_SDA = SDA;
Afinal_SDA = 0; // SDA remains low because open-drain lines are wired-AND
Bfinal_SDA = -1; // Error state, line conflict
Cfinal_SDA = 1; // SDA goes high because Device B released it
Dfinal_SDA = 2; // Undefined state
Attempts:
2 left
💡 Hint

Remember that in I2C, devices can only pull the line low or release it. The line state is the logical AND of all devices' outputs.

Predict Output
intermediate
2:00remaining
I2C Clock Stretching Effect on SCL Line

Given the following code snippet simulating clock stretching on the I2C SCL line, what will be the output value of SCL after Device B stretches the clock?

Embedded C
/* SCL line simulated as a variable: 0 = low, 1 = released (pulled high by resistor) */
int SCL = 1; // Initially released (high)

// Device A tries to release SCL
SCL = 1;

// Device B pulls SCL low to stretch clock
SCL = 0;

// What is the actual SCL line state now?
int final_SCL = SCL;
Afinal_SCL = 2; // Undefined state
Bfinal_SCL = 1; // SCL is high because Device A released it
Cfinal_SCL = -1; // Bus error due to conflict
Dfinal_SCL = 0; // SCL is low due to clock stretching by Device B
Attempts:
2 left
💡 Hint

Clock stretching means a device holds the clock line low to delay communication.

🔧 Debug
advanced
3:00remaining
Identify the Bug in I2C SDA Line Handling Code

The following embedded C code attempts to simulate the SDA line behavior in an I2C bus. However, it does not correctly represent the open-drain logic. What is the bug causing incorrect SDA line state?

Embedded C
int SDA = 1; // Released (high)

void deviceA_pull_low() {
    SDA = 0; // Device A pulls SDA low
}

void deviceB_release() {
    SDA = 1; // Device B releases SDA
}

void main() {
    deviceA_pull_low();
    deviceB_release();
    // Expected SDA to be low, but SDA is 1
}
AMissing delay after deviceB_release() causing race condition
BSDA variable is overwritten by last device instead of combining outputs with wired-AND logic
CDevice B should pull SDA low instead of releasing it
DSDA line should be initialized to 0 instead of 1
Attempts:
2 left
💡 Hint

Think about how multiple devices affect the line state simultaneously in open-drain wiring.

📝 Syntax
advanced
2:00remaining
Syntax Error in I2C SCL Line Control Code

Which option contains the correct syntax to set the SCL line low in embedded C, assuming SCL is a GPIO pin controlled by a macro GPIO_WritePin(port, pin, value)?

Embedded C
/* Assume port and pin are defined as I2C_PORT and I2C_SCL_PIN respectively */
AGPIO_WritePin(I2C_PORT, I2C_SCL_PIN, LOW);
BGPIO_WritePin(I2C_PORT, I2C_SCL_PIN == 0);
CGPIO_WritePin(I2C_PORT, I2C_SCL_PIN, 0);
DGPIO_WritePin(I2C_PORT, I2C_SCL_PIN 0);
Attempts:
2 left
💡 Hint

Check the function call syntax and parameters.

🚀 Application
expert
3:00remaining
Calculate Number of Devices on I2C Bus from SDA and SCL States

You have an I2C bus with multiple devices connected. The SDA and SCL lines are open-drain and pulled high by resistors. You observe the following states during a bus scan:

  • SDA line is low
  • SCL line is high

Assuming each device can pull SDA low independently, and the bus line state is the logical AND of all devices' outputs, what is the minimum number of devices pulling SDA low?

AAt least 1 device is pulling SDA low
BAt least 2 devices are pulling SDA low
CNo devices are pulling SDA low
DAll devices are releasing SDA
Attempts:
2 left
💡 Hint

If the SDA line is low, what does that tell you about devices connected?