I2C_coding
I2C_coding
INTERVIEW QUESTIONS
PROJECT IMPLEMENTATION
Prasanthi Chanda
1. How would you implement clock stretching in an
I2C slave controller in Verilog?
Clock stretching involves the slave holding the SCL line low when it needs
more time to process data.
Use a signal in the slave's FSM to pull SCL low.
KEY CONCEPT: The SCL signal is driven low by the slave's control logic until it is
ready to continue communication.
IMPLEMENTATION EXAMPLE:
always @(posedge clk or posedge reset) begin
if (reset) scl_out <= 1; // Default state
else if (need_more_time) scl_out <= 0; // Hold SCL low
else scl_out <= 1; // Release SCL
end
reg sda_prev;
always @(posedge clk or posedge reset) begin
if (reset) begin
sda_prev <= 1;
start_detected <= 0;
end else begin
if (scl == 1 && sda_prev == 1 && sda == 0) begin
start_detected <= 1; // START condition detected
end else begin
start_detected <= 0;
end
sda_prev <= sda;
end
end
endmodule
10. How do you implement a glitch filter for I2C
signals in hardware?
Use a shift register to filter out glitches shorter than a specific time.
Only consider SDA/SCL stable when their value is consistent over multiple
clock cycles.
IMPLEMENTATION EXAMPLE :
module glitch_filter(clk, reset, sda_in, sda_out);
input clk, reset, sda_in;
output reg sda_out;
reg sync1;
always @(posedge clk_dest) begin
sync1 <= signal_src;
signal_dest <= sync1;
end
endmodule
I hope this document has provided valuable insights into the design and
functionality of I2C communication in hardware. If you have any
questions, need the source code, or wish to discuss further details, feel
free to contact me. I look forward to any feedback or collaboration
opportunities.