RTL Design / Module 11: Capstone — I2C Controller
Lesson 11.3

I2C master architecture

Protocol knowledge from 11.1 and the micro-spec from 11.2 now become structure. A production I2C master splits into a CSR bank for software, a byte-level FSM for transactions, a bit engine for SCL timing, and an open-drain pad wrapper for the bus. This lesson defines every block, every interface, and the clock-stretch handshake that trips up first-time implementers.

Architecture principle

Byte FSM thinks in transactions. Bit engine thinks in quarter-phases. Pads think in wired-AND. Keep these layers separate — mixing them produces unmaintainable RTL and untestable bugs.

1

Hierarchy and responsibilities

I2C master RTL hierarchy

Commands flow down; status and sampled ACK flow up. Pad module is the only block that touches bidirectional pins.

ModuleClock domainResponsibility
i2c_csrpclkDecode APB; latch ADDR, TX, DIV; expose STAT, RX; pulse START
i2c_byte_fsmpclkSequence START → addr → data → ACK → STOP; byte counter
i2c_bit_enginepclkGenerate SCL quarter-phases; shift one bit; sample ACK
i2c_od_padasync pinsOpen-drain drive + synthesizable pull-up for sim
2

Byte FSM — transaction states

The byte FSM is the orchestrator. It knows what to send (address, TX data, read phase) but delegates how to wiggle pins to the bit engine.

I2C byte FSM state diagram

ACK_CHK branches: NACK → STOP (error); more bytes → DATA; last byte + read → master drives ACK/NACK.

StateEntry actionExit condition
IDLEBUSY=0; release bus (oen=1)CTRL.START pulse → START
STARTbit_cmd=GEN_STARTbit_done → ADDR (load shifted addr byte)
ADDRShift (addr<<1)|rd; 8 bits out8 bits done → ACK_CHK
DATALoad TX or receive into shift reg8 bits done → ACK_CHK
ACK_CHKSample ack_bit on 9th rising edgeNACK → STOP; else byte_cnt-- → DATA or STOP
REP_STARTbit_cmd=GEN_REP_START; flip rd bitbit_done → ADDR or DATA
STOPbit_cmd=GEN_STOPbit_done → IDLE; DONE=1
byte_fsm interface — contract to bit engine
// Byte FSM → Bit engine (command)
output reg        bit_go;
output reg [2:0]  bit_cmd;  // IDLE, START, STOP, REP_START, TX_BIT, RX_BIT
output reg        bit_wr;   // 1=master drives SDA this bit
output reg        tx_bit;   // data bit to transmit

// Bit engine → Byte FSM (status)
input  wire       bit_done;      // one bit (or START/STOP seq) complete
input  wire       ack_sample;    // SDA at 9th rising edge
input  wire       stretch_active; // SCL held low externally
3

Bit engine — quarter-phase timing

Each SCL period divides into four phases counted by a divider loaded from CSR DIV. This gives precise control over setup, sample point, and clock-stretch wait.

Quarter-phases within one bit (write data bit)

Quarter phase bit engine

PH_HIGH is where clock stretch happens — phase counter pauses until scl_i == 1.

clock divider — 50 MHz → 400 kHz
// Target f_scl = 400 kHz, pclk = 50 MHz
// DIV = 50_000_000 / (4 * 400_000) = 31.25 → use 31
// Actual f = 50M / (4*31) ≈ 403 kHz

localparam DIV_400K = 16'd31;

// In PH_HIGH: wait for external SCL release (clock stretch)
if (phase == PH_HIGH && !scl_i)
  phase_cnt <= phase_cnt;     // hold — do not advance
else if (phase_cnt == div_r - 1)
  phase_cnt <= 0; phase <= phase_next;
else
  phase_cnt <= phase_cnt + 1;
4

Open-drain pad model

The pad wrapper is the boundary between synchronous RTL and the asynchronous bus. In synthesis, it maps to tristate IO cells; in simulation, it models pull-ups.

Open drain pad RTL model

oen_n active-low is common in pad libraries: 0 = drive low, 1 = high-Z.

Trap

Using assign sda_i = sda_pad without resolving Z to 1 hides the fact that nobody is driving. Always model pull-ups in TB and pad wrapper for accurate ACK/NACK and stretch behavior.

5

Clock stretching — wait for SCL high

When a slave is not ready, it holds SCL low after the master releases it. The master must not advance to the next bit or sample SDA until scl_i returns high. Stretching can occur during any SCL high phase — most commonly during ACK or while the slave prepares read data.

Clock stretch during ACK bit

  • Detection: in PH_RISE or PH_HIGH, if master released SCL (oen=1) but scl_i==0, stretch is active.
  • Response: freeze phase counter; do not increment bit index; assert stretch_active for debug.
  • Timeout (SHOULD): if stretch exceeds 25 ms at 400 kHz, abort and set STAT.ERR — prevents permanent hang.

Verification tip: directed test A5 from the 11.2 spec deliberately holds SCL low in TB. If your master completes without waiting, the bit engine PH_HIGH logic is broken.

6

CSR bank and interrupt logic

The CSR block bridges CPU clock domain and the FSM. Register offsets match Lesson 11.2; implementation lands in 11.4.

SignalSourceConsumer
start_pulseCTRL.START write-1Byte FSM: IDLE → START
addr_r[6:0]ADDR CSRByte FSM: forms addr byte
div_r[15:0]DIV CSRBit engine: phase length
tx_dataTX CSRByte FSM: write payload
rx_dataByte FSM shift regRX CSR readback
busy, done, nackByte FSMSTAT CSR; W1C on done/nack
irq glue — level-sensitive
assign irq = irq_en & (stat_done | stat_nack | stat_timeout);

// Firmware ISR:
// 1. Read STAT
// 2. If NACK → handle error path
// 3. Write STAT to W1C clear done/nack
// 4. Read RX if read transaction
7

Interface contracts between blocks

Clear contracts let you unit-test each module in isolation before top-level integration.

BoundaryProducer guaranteesConsumer assumes
CSR → FSMSTART is a 1-cycle pulse; ADDR stable until BUSYFSM ignores START when BUSY=1
FSM → bit enginebit_go pulse per bit; bit_cmd stable during bitEngine completes with exactly one bit_done
bit engine → padsSDA only changes in PH_SETUP or START/STOP seqPad resolves Z; returns scl_i/sda_i
bit engine → FSMack_sample valid only at end of 9th bit PH_HIGHFSM latches ACK/NACK on bit_done
🧩

Unit test bit engine

Feed bit_cmd sequences; check SCL/SDA waveforms without CSR or FSM present.

🔄

Unit test FSM

Stub bit_done/ack_sample; verify state transitions for A1–A4 scenarios.

🔌

Pad model TB

Two masters driving — verify wired-AND and stretch with conflicting oen.

▶ Prototype bit engine FSM in simulator
8

Read path — shift register and master ACK

Write and read share the bit engine but differ in who drives SDA during data bits and who drives the 9th bit. The byte FSM must track rd_phase to swap drivers correctly.

Phasebit_wrSDA driver (data bits)9th bit driver
ADDR (write)1MasterSlave ACK
ADDR (read)1MasterSlave ACK
DATA write1MasterSlave ACK
DATA read0SlaveMaster ACK/NACK
read_shift — sample on PH_HIGH
// In bit engine PH_HIGH during RX_BIT:
if (bit_cmd == RX_BIT)
  shift_reg <= {shift_reg[6:0], sda_i};  // MSB first

// Byte FSM during read ACK_CHK on last byte:
if (byte_cnt == 0)
  bit_wr <= 1; tx_bit <= 1;  // master NACK (release SDA → high)
else
  bit_wr <= 1; tx_bit <= 0;  // master ACK (pull SDA low)
8

START / STOP — bit engine sequences

START and STOP are not ordinary data bits — they violate the "change SDA only when SCL low" rule on purpose. The bit engine needs dedicated micro-sequences for these bus events.

EventSCL sequenceSDA sequencePhase count
STARTHold high → fall → stay lowHigh → fall (while SCL high) → hold low3–4 custom phases
STOPRise → hold highLow → rise (while SCL high)3–4 custom phases
REP_STARTSame as STARTSame as START (no prior STOP)Reuse START seq
Data bitStandard 4-phaseSetup in PH_SETUP4 × DIV

START micro-sequence (conceptual)

start_stop_seq — bit engine states
typedef enum logic [2:0] {
  BIT_IDLE,
  BIT_START_SDA_FALL,  // SDA↓, SCL still high
  BIT_START_SCL_FALL,  // SCL↓
  BIT_PH_SETUP,
  BIT_PH_RISE,
  BIT_PH_HIGH,
  BIT_PH_FALL,
  BIT_STOP_SCL_RISE,   // SCL↑ first
  BIT_STOP_SDA_RISE    // SDA↑ while SCL high → bit_done
} bit_state_e;

// START entry: from BIT_IDLE when bit_cmd == GEN_START
// STOP entry: after last bit when bit_cmd == GEN_STOP
Trap

Reusing the data-bit FSM for START/STOP produces illegal intermediate states — SCL may fall before SDA, or SDA may glitch high during a data byte. Give START/STOP their own states.

9

Reset and bus-safe defaults

On reset, the I2C master must not leave the bus in an illegal state. A slave left mid-transaction may hang until it sees STOP.

  • MUST Deassert all oen_n (release SCL and SDA) within 1 cycle of reset
  • MUST Clear BUSY; byte FSM returns to IDLE
  • SHOULD Optionally generate STOP if reset occurs mid-transaction (advanced — document if implemented)
  • MAY Hold irq low during reset regardless of sticky STAT
reset defaults — pad and FSM
always_ff @(posedge pclk or negedge preset_n) begin
  if (!preset_n) begin
    scl_oen_n <= 1'b1;   // release SCL
    sda_oen_n <= 1'b1;   // release SDA
    byte_state <= IDLE;
    busy       <= 1'b0;
  end
end
10

CDC note — async I2C pads

scl_i and sda_i are asynchronous to pclk — they can change at any time relative to the CPU clock. For a master that samples on internally generated SCL edges, you typically sample in PH_HIGH after sufficient delay, which provides implicit synchrony to your own clock generation.

  • Do NOT double-flop sda_i for every bit without timing analysis — it adds metastability risk at the exact sample point.
  • DO sample only in PH_HIGH when your phase counter is near the end (stable bus after rise time).
  • DO treat scl_i stretch detection as async: if scl_i==0 after release, wait — no edge sync needed.
  • If the I2C block runs from a separate clock (i2c_clk), synchronize start_pulse and status back to pclk with pulse stretchers or APB handshake.

For this capstone, everything runs on pclk — the bit engine generates SCL as a logical output, and pad feedback is sampled mid-phase. Full CDC to a divided i2c_clk domain is an optional stretch goal, not required for sign-off.

11

Synthesis and constraints notes

The I2C master is a slow serial interface — timing closure is usually easy on pclk. The critical paths to watch are APB decode and the divider counter, not the pad outputs.

  • False paths: scl_i, sda_i are async inputs — do not time them to pclk without sync registers (and we intentionally sample mid-phase).
  • IO timing: pad cell insertion happens at top level; your RTL outputs oen_n and o — constrain in PAD .sdc separately.
  • Clock gating: MAY gate pclk to bit engine when IDLE to save dynamic power — ensure BUSY=0 and FSM in IDLE before gate enable drops.
  • Lint: run clock-domain and reset checks; verify all FSM states have default recovery to IDLE on reset.
Trap

Synthesizing push-pull on SCL/SDA because the tool "optimizes away" tristate — force open-drain IO cell mapping in constraints or the chip will fight the bus.

12

Architecture review checklist

  • Four modules: csr, byte_fsm, bit_engine, od_pad — clean hierarchy
  • Byte FSM states cover START, ADDR, DATA, ACK_CHK, REP_START, STOP, IDLE
  • Bit engine uses 4 quarter-phases; DIV register sets frequency
  • PH_HIGH waits for scl_i during clock stretch; timeout optional
  • Open-drain: oen_n=1 releases; Z resolves to 1 with pull-up model
  • ACK sampled on 9th bit; NACK sets STAT and triggers STOP path
  • Interface contracts documented between every block pair
  • irq driven by done | nack | timeout with enable mask
  • START/STOP have dedicated bit-engine states — not reused data-bit phases
  • Read path: slave drives data; master drives 9th-bit ACK/NACK
  • Reset releases bus (oen_n=1); FSM returns to IDLE

🎯 What to remember

  • Layer separation: CSR (software), byte FSM (transactions), bit engine (timing), pads (electrical).
  • Quarter-phase engine: setup → rise → high (sample + stretch wait) → fall.
  • Clock stretch = freeze PH_HIGH until scl_i reads high — never skip this wait.
  • Open-drain pads never drive high — only pull low or release to Z.
  • Next lesson: RTL implementation (11.4) — turn this architecture into synthesizable Verilog.
Advertisement