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?
/* 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;
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.
In an open-drain configuration, the SDA line is pulled low if any device pulls it low. Even if Device B releases the line, Device A pulling it low keeps the line low.
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?
/* 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;
Clock stretching means a device holds the clock line low to delay communication.
When Device B pulls SCL low, the line stays low regardless of Device A releasing it, effectively stretching the clock.
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?
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 }
Think about how multiple devices affect the line state simultaneously in open-drain wiring.
The code overwrites SDA directly, losing the combined effect of multiple devices. The SDA line state should be the logical AND of all devices' outputs, not just the last assignment.
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)?
/* Assume port and pin are defined as I2C_PORT and I2C_SCL_PIN respectively */Check the function call syntax and parameters.
Option C correctly calls the function with three parameters: port, pin, and value 0 to set low. Option C uses comparison instead of parameter. Option C uses undefined LOW. Option C misses a comma.
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?
If the SDA line is low, what does that tell you about devices connected?
Since the SDA line is low, at least one device must be pulling it low. The line is low if any device pulls it low due to open-drain wiring.