I2C Slave Responder & Pull-up Logic
Implementing the Slave Side
Part 3 of 5: SystemVerilog I2C Master Design
Slave vs. Master Responsibilities
In I2C, the master initiates and controls timing, while the slave responds. Let's break down who does what:
| Task |
Master |
Slave |
| Generate START condition |
✓ |
— |
| Generate SCL clock pulses |
✓ |
Can stretch by holding LOW |
| Transmit address byte |
✓ |
Monitor & match address |
| Generate STOP condition |
✓ |
— |
| Send data bits (write) |
✓ |
Sample & store |
| Receive data bits (read) |
Sample & store |
✓ Drive SDA |
| Generate ACK/NACK |
Receives ACK after write |
✓ Sends ACK after receive |
Passive Responder:
In our testbench, the slave is a passive simulator that responds to master-generated clock. Real silicon slaves use state machines to handle clock stretching and multi-byte transactions independently.
Address Matching Logic
After START, the master sends an 8-bit address byte. The slave must listen to bits [7:1] and compare against its programmed address.
Address Matching State Machine
Phase 1: Receive 7 bits
- Master sends bits [7:1] of address
- Slave samples SDA on each SCL rising edge
- Shifts bits into a receive register
Phase 2: Compare
- After 7th bit, compare received address with programmed
slave_addr
- If match: set
address_match = 1
- If no match: slave ignores rest of transaction
Phase 3: R/W Bit (8th bit)
- Master sends R/W flag (0 = write, 1 = read)
- Slave stores this to determine transaction type
SystemVerilog Address Matching
always @(posedge scl_in) begin
if (address_phase) begin
if (bit_counter < 7) begin
rx_addr[7-bit_counter] <= sda_in;
bit_counter <= bit_counter + 1;
end else if (bit_counter == 7) begin
rw_bit <= sda_in;
if (rx_addr[7:1] == slave_addr) begin
address_match <= 1'b1;
end
bit_counter <= 0;
address_phase <= 1'b0;
end
end
end
ACK Bit Generation
After receiving 8 bits, the slave must pull SDA LOW during the 9th clock pulse to signal ACK.
Timing Requirements for ACK
Bits: 1-8 (data/addr) 9 (ACK)
SCL: _|‾‾|_|‾‾|_|‾‾|_ ... |_|‾‾|_
SDA: data_bits |delay| ACK
|---|
t_SU:ACK
Setup Time (tSU:ACK): SDA must go LOW before SCL rises for the 9th bit. Minimum = 250 ns (in real hardware).
ACK Generation Logic
always @(posedge scl_in) begin
if (bit_counter == 8) begin
ack_pending <= 1'b1;
end
else if (bit_counter == 9 && ack_pending) begin
sda_out <= 1'b0;
ack_pending <= 1'b0;
end
else if (scl_falling) begin
sda_out <= 1'b1;
end
end
Timing Pitfall:
ACK must be generated before SCL rises for bit 9. Generating it after SCL is already HIGH will violate setup time, and the master won't see it.
Pull-up Resistor Simulation
In synthesis, I2C lines use real pull-up resistors. In simulation, we model them as continuous assignments:
assign scl_in = (scl_out == 1'b0 || scl_held_low) ? 1'b0 : 1'b1;
assign sda_in = (sda_out == 1'b0 || sda_held_low) ? 1'b0 : 1'b1;
Why This Model?
- Wired-AND behavior: If any device pulls the line LOW, it stays LOW.
- No explicit resistor: We don't need to simulate RC charging curves; a simple "open-drain OR gate" is sufficient.
- Fast simulation: No analog computations; purely digital logic.
Extended Pull-up Model (with Slew Rate)
For timing-critical designs, you can add a simple slew-rate model:
real scl_voltage = 3.3;
real scl_rise_time = 100e-9;
always @(posedge clk) begin
if (scl_out == 1'b0) begin
scl_voltage <= 0;
end else if (scl_voltage < 3.3) begin
scl_voltage <= scl_voltage + 3.3 / (scl_rise_time / 1e-8);
end
end
assign scl_logic = (scl_voltage > 1.65) ? 1'b1 : 1'b0;
For Testbenches:
Start with the simple model. Only add slew-rate modeling if timing-based bugs appear in simulation.
SDA Data Bit Driving
When the master sends a READ command, the slave drives data bits onto SDA while the master controls SCL.
Read Transaction Timing
Master: SCL: _|‾|_|‾|_|‾|_ (controls clock)
Slave (read): SDA: ‾‾\__/‾‾\__/‾ (drives data)
bit_0 bit_1
Slave Read-Mode Logic
always @(posedge clk) begin
if (rw_bit == 1'b1) begin
if (scl_falling) begin
sda_out <= data_to_send[7 - bit_counter];
end
end
end
Sequence for Slave-Driven Bit
- SCL falls: Slave sets up SDA for next bit (setup time)
- SCL rises: Master samples SDA and reads bit
- SCL falls again: Slave prepares next bit
No Clock Stretching from Slave in Read:
In our simple testbench slave, the slave doesn't hold SCL. In real designs, a slave might hold SCL LOW if it needs processing time between bits.
Slave Responder SystemVerilog
Here's a complete slave module skeleton:
module i2c_slave_responder (
input clk,
input rst_n,
input scl_in,
input sda_in,
output reg sda_out,
input [6:0] slave_addr,
input [7:0] data_to_send,
output reg address_match,
output [7:0] data_received
);
reg [1:0] scl_sync, sda_sync;
wire scl_stable = scl_sync[1];
wire sda_stable = sda_sync[1];
reg [3:0] bit_counter;
reg [7:0] rx_addr;
reg rw_bit;
reg address_phase;
reg ack_pending;
always @(posedge clk or negedge rst_n) begin
if (~rst_n) begin
address_match <= 1'b0;
sda_out <= 1'b1;
address_phase <= 1'b1;
end else begin
if (scl_rising) begin
if (address_phase) begin
rx_addr[7-bit_counter] <= sda_stable;
if (bit_counter == 7) begin
rw_bit <= sda_stable;
address_match <= (rx_addr == {slave_addr, 1'b0});
address_phase <= 1'b0;
end
bit_counter <= bit_counter + 1;
end
end
if (ack_pending) begin
sda_out <= 1'b0;
ack_pending <= 1'b0;
end
else if (scl_falling) begin
sda_out <= 1'b1;
end
end
end
endmodule
Key Takeaways
Key Takeaways
- Master controls timing; slave responds passively (in standard mode).
- Address matching happens on first 7 bits after START; slave ignores rest if no match.
- ACK/NACK is generated by pulling SDA LOW during the 9th clock pulse (ACK) or releasing it (NACK).
- Pull-up simulation uses wired-AND logic: if ANY device pulls LOW, the line is LOW.
- Data bit timing: Slave sets up data on SCL falling edge; master samples on SCL rising edge.
- Double-flop synchronizers prevent metastability when sampling async bus lines.
- Open-drain outputs ensure multiple slaves and masters can share the bus safely.
What's Next?
In Part 4, we'll design a comprehensive testbench with 10 test cases covering START/STOP, address matching, data transmission, ACK/NACK, and error scenarios.
Ready to test it all? See you in Part 4!
About the author: Upalparna is a VLSI engineer and design verification specialist with expertise in functional verification, UVM testbenches, and protocol design. She runs The Silicon Stack, a VLSI education platform, and teaches embedded systems and semiconductor design.
Questions or feedback? Drop a comment below or connect on LinkedIn.
Series Navigation:
Part 1: Protocol Fundamentals →
Part 2: Master Controller FSM →
Part 3: Slave Responder & Pull-up Logic (You are here)
Part 4: Testbench Design & Test Cases
Part 5: Interview Prep & Optimization
Comments
Post a Comment