I2c Advanced Concepts Toughest Interview Questions - 1
I2c Advanced Concepts Toughest Interview Questions - 1
INTERVIEW
PREPARATION
I2C PROTOCOL
Your Complete Experience-Level Guide
By ProV Logic
I2C Communication
Why I2C ?
How I2C Works ?
Steps in I2C Data transmission
Protocol Basics
Advanced Protocol Topics
Tricky & Advanced Level Interview Questions
I2C COMMUNICATION
I2C combines the best features of SPI and UARTs.
With I2C, you can connect multiple slaves to a single master
(like SPI) and you can have multiple masters controlling
single, or multiple slaves.
This is really useful when you want to have more than one
microcontroller logging data to a single memory card or
displaying text to a single LCD.
Like UART communication, I2C only uses two wires to
transmit data between devices:
SDA (Serial Data) – The line for the master and slave to send
and receive data
SCL (Serial Clock) – The line that carries the clock signal.
I2C is a serial communication protocol, so data is transferred
bit by bit along a single wire (the SDA line).
Like SPI, I2C is synchronous, so the output of bits is
synchronized to the sampling of bits by a clock signal shared
between the master and the slave.
The clock signal is always controlled by the master.
WHY USE I2C?
Because serial ports are asynchronous (no clock data is
transmitted), devices using them must agree ahead of time
on a data rate.
The two devices must also have clocks that are close to the
same rate, and will remain so excessive differences between
clock rates on either end will cause garbled data.
Asynchronous serial ports require hardware overhead the
UART at either end is relatively complex and difficult to
accurately implement in software if necessary.
At least one start and stop bit is a part of each frame of
data, meaning that 10 bits of transmission time are required
for each 8 bits of data sent, which eats into the data rate.
ADDRESSING
I2C doesn’t have slave select lines like SPI, so it needs
another way to let the slave know that data is being sent to
it, and not another slave.
It does this by addressing. The address frame is always the
first frame after the start bit in a new message.
The master sends the address of the slave it wants to
communicate with to every slave connected to it.
Each slave then compares the address sent from the master
to its own address.
If the address matches, it sends a low voltage ACK bit back
to the master. If the address doesn’t match, the slave does
nothing and the SDA line remains high.
READ/WRITE BIT
The address frame includes a single bit at the end that
informs the slave whether the master wants to write data to
it or receive data from it.
If the master wants to send data to the slave, the read/write
bit is a low voltage level.
If the master is requesting data from the slave, the bit is a
high voltage level.
THE DATA FRAME
After the master detects the ACK bit from the slave, the first
data frame is ready to be sent.
The data frame is always 8 bits long, and sent with the most
significant bit first.
Each data frame is immediately followed by an ACK/NACK
bit to verify that the frame has been received successfully.
The ACK bit must be received by either the master or the
slave (depending on who is sending the data) before the next
data frame can be sent.
After all of the data frames have been sent, the master can
send a stop condition to the slave to halt the transmission.
The stop condition is a voltage transition from low to high
on the SDA line after a low to high transition on the SCL line,
with the SCL line remaining high.
STEP 4:
The master sends or receives the data frame:
STEP 6:
To stop the data transmission, the master sends a stop
condition to the slave by switching SCL high before switching
SDA high:
SINGLE MASTER WITH MULTIPLE SLAVES
Because I2C uses addressing, multiple slaves can be
controlled from a single master.
With a 7 bit address, 128 (27) unique address are available.
Using 10 bit addresses is uncommon, but provides 1,024 (210)
unique addresses.
To connect multiple slaves to a single master, wire them like
this, with 4.7K Ohm pull-up resistors connecting the SDA and
SCL lines to Vcc:
ADDRESS FRAME
The address frame is always first in any new communication
sequence.
For a 7-bit address, the address is clocked out most
significant bit (MSB) first, followed by a R/W bit indicating
whether this is a read (1) or write (0) operation.
The 9th bit of the frame is the NACK/ACK bit.
This is the case for all frames (data or address).
Once the first 8 bits of the frame are sent, the receiving
device is given control over SDA.
If the receiving device does not pull the SDA line low before
the 9th clock pulse, it can be inferred that the receiving
device either did not receive the data or did not know how
to parse the message.
In that case, the exchange halts, and it's up to the controller
of the system to decide how to proceed.
DATA FRAMES
After the address frame has been sent, data can begin being
transmitted.
The controller will simply continue generating clock pulses at
a regular interval, and the data will be placed on SDA by
either the controller or the peripheral, depending on whether
the R/W bit indicated a read or write operation.
The number of data frames is arbitrary, and most peripheral
devices will auto-increment the internal register, meaning
that subsequent reads or writes will come from the next
register in line.
STOP CONDITION
Once all the data frames have been sent, the controller will
generate a stop condition.
Stop conditions are defined by a 0->1 (low to high) transition
on SDA after a 0->1 transition on SCL, with SCL remaining
high.
During normal data writing operation, the value on SDA
should not change when SCL is high, to avoid false stop
conditions.
ADVANCED PROTOCOL TOPICS
10-BIT ADDRESSES
In a 10-bit addressing system, two frames are required to
transmit the peripheral address.
The first frame will consist of the code b11110xyz, where 'x'
is the MSB of the peripheral address, y is bit 8 of the
peripheral address, and z is the read/write bit as described
above.
The first frame's ACK bit will be asserted by all peripherals
which match the first two bits of the address.
DISADVANTAGES
Slower data transfer rate than SPI
The size of the data frame is limited to 8 bits
More complicated hardware needed to implement than SPI
TRICKY INTERVIEW QUESTIONS & SOLUTIONS
1. What are the different operating speeds of I2C, and how
does the clock stretching mechanism work?
I2C supports multiple speed modes:
Standard Mode: 100 kHz
Fast Mode: 400 kHz
Fast Mode Plus: 1 MHz
High-Speed Mode: 3.4 MHz
Ultra-Fast Mode: 5 MHz
reg is_master;
reg lost_arbitration;
endmodule
HOW IT WORKS:
1. Master Arbitration:
When two devices attempt to transmit simultaneously,
the one detecting a mismatch on SDA loses arbitration
and switches to slave mode.
2. Slave Requests Master Role:
If a slave detects SDA and SCL both HIGH (bus idle), it
can request master mode.
3. Dynamic Switching:
A master losing arbitration automatically becomes a
slave.
A slave can become a master when the bus is idle.
// Recovery mechanism
if (recovery_mode) begin
if (recovery_counter < 4'd9) begin
scl_out <= ~scl_out; // Generate clock pulses
recovery_counter <= recovery_counter + 1;
end else begin
recovery_mode <= 1'b0;
bus_reset <= 1'b1; // Signal bus reset
end
end else begin
bus_reset <= 1'b0;
scl_out <= 1'b1; // Default state
end
end
end
endmodule
HOW IT WORKS:
1. Detects Deadlocks:
Monitors SDA and SCL continuously.
If both SDA = 0 and SCL = 0 for an extended period, a
deadlock is detected.
2. Recovers Automatically:
Generates 9 clock pulses to free any stuck slave device.
If the deadlock persists, bus reset is triggered.
3. Ensures Robust I2C Communication:
Prevents hanging devices from stalling the system.
Supports multi-master arbitration with recovery.
12. Develop an I2C arbitration system that handles multiple
masters sending conflicting data.
module i2c_arbitration (
input wire clk, // System Clock
input wire rst_n, // Active-low Reset
inout wire sda, // Serial Data Line (Bidirectional)
inout wire scl, // Serial Clock Line (Bidirectional)
input wire start_tx, // Start Transmission Request
input wire [7:0] data_in, // Data Byte to Transmit
output reg arbitration_lost // Indicates Arbitration Loss
);
reg is_master;
reg [3:0] bit_counter;
reg [7:0] shift_reg;
if (is_master) begin
if (bit_counter < 8) begin
sda_out <= shift_reg[7]; // Send MSB first
HOW IT WORKS:
1. Challenge Generation:
2. A pseudo-random challenge (8-bit LFSR) is sent to the device.
3. Expected Response Calculation:
15. Design a high-reliability I2C system that detects and
corrects clock stretching failures
module i2c_high_reliability (
input wire clk, // System Clock
input wire rst_n, // Active-low Reset
inout wire sda, // Serial Data Line (Bidirectional)
inout wire scl, // Serial Clock Line (Bidirectional)
output reg clock_stretch_fail, // Flag for clock stretching
failure
output reg bus_recovered // Flag indicating successful
recovery
);
CHECK_SCL: begin
if (!scl_in) begin
stretch_timer <= stretch_timer + 1;
if (stretch_timer > 16'd50000) begin //
Timeout threshold
clock_stretch_fail <= 1'b1;
state <= TIMEOUT;
end
end else begin
state <= IDLE;
end
end
TIMEOUT: begin
sda_out <= 1'b0; // Send STOP condition
scl_out <= 1'b0;
state <= RECOVER;
end
RECOVER: begin
sda_out <= 1'b1; // Release bus
scl_out <= 1'b1;
bus_recovered <= 1'b1;
clock_stretch_fail <= 1'b0;
state <= IDLE;
end
endcase
end
end
endmodule
// UART Transmission
if (write_ptr != read_ptr && !uart_tx_start) begin
uart_data_out <= fifo[read_ptr]; // Read data from
FIFO
read_ptr <= (read_ptr + 1) % 16;
uart_tx_start <= 1;
end
endmodule
+91- 9182280927