Skip to main content

I2C Master Controller FSM

I2C Master Controller FSM

Designing the 6-State Machine

Part 2 of 5: SystemVerilog I2C Master Design

FSM Overview & State Diagram

The I2C Master Controller uses a 6-state finite state machine to manage all bus operations. Each state corresponds to a distinct phase of communication.

IDLE ↑ ↓ START_COND ↑ ↓ TX_DATA (or RX_DATA) ↑ ↓ ACK_BIT ↑ ↓ STOP_COND ↑ ↓ (back to IDLE)

State Machine Properties

  • Total states: 6 (IDLE, START_COND, STOP_COND, TX_DATA, RX_DATA, ACK_BIT)
  • Clock domain: System clock (10 MHz in our design)
  • Output control: SCL and SDA driven independently via open-drain logic
  • Collision detection: Monitors for unexpected SDA changes
  • Clock stretching: Holds SCL LOW until all slaves release it

Detailed State Descriptions

State 1: IDLE

Purpose: Bus is free; no transaction in progress.

Signal Value Meaning
SCL 1 (HIGH) Released via pull-up
SDA 1 (HIGH) Released via pull-up
busy 0 No transaction
error 0 No errors detected

Transitions:

  • → START_COND: When start_cond_req asserted

State 2: START_COND

Purpose: Generate START condition (SDA falls while SCL HIGH).

Sequence:

  1. Wait for SCL to reach HIGH (SCL = 1 after release)
  2. Pull SDA LOW (sda_out = 0)
  3. Wait for SDA to stabilize LOW
  4. Transition to next state
Timing Note: START condition requires tSU:DAT ≥ 250 ns (SDA setup time before SCL rise). In a 10 MHz system, each clock = 100 ns, so 3 clock cycles = sufficient margin.

State 3: TX_DATA

Purpose: Transmit 8-bit data byte (most significant bit first).

Bit Transmission Sequence (for each bit):

  1. Set up SDA (sda_out = bit_to_send)
  2. Release SCL (scl_out = 1) → Pull-up brings SCL HIGH
  3. Wait for SCL to stabilize HIGH (with clock stretching tolerance)
  4. Check for collision: if SDA changed unexpectedly → error = 1
  5. Pull SCL LOW (scl_out = 0)
  6. Move to next bit
Collision Detection: If we release SDA (sda_out=1) but SDA_IN reads 0, another master is driving it. This triggers error = 1.

State 4: RX_DATA

Purpose: Receive 8-bit data byte from slave.

Bit Reception Sequence (for each bit):

  1. Release SDA (sda_out = 1) → Slave can drive if bit = 0
  2. Release SCL (scl_out = 1) → Slave can hold it LOW for clock stretching
  3. Wait for SCL HIGH
  4. Sample SDA (sda_in) and store in receive buffer
  5. Pull SCL LOW (scl_out = 0)
  6. Move to next bit

State 5: ACK_BIT

Purpose: Generate or monitor the 9th bit (ACK/NACK).

Master Send ACK (slave expects more data):

  • Pull SDA LOW (sda_out = 0)
  • Release SCL → Slave reads ACK

Master Send NACK (slave should stop):

  • Release SDA (sda_out = 1)
  • Release SCL → Slave reads NACK

Transitions:

  • → TX_DATA: Next byte to transmit
  • → RX_DATA: Next byte to receive
  • → STOP_COND: End of transaction

State 6: STOP_COND

Purpose: Generate STOP condition (SDA rises while SCL HIGH).

Sequence:

  1. Pull SCL LOW (scl_out = 0)
  2. Release SCL (scl_out = 1) → SCL goes HIGH
  3. Wait for SCL stabilization
  4. Release SDA (sda_out = 1) → SDA rises to HIGH
  5. Return to IDLE
Why SDA waits for SCL? If we released both simultaneously, a glitch could trigger a START instead of STOP. Strict sequencing ensures clean transitions.

Edge Detection & Synchronization

Since SCL and SDA are asynchronous inputs (pulled via external resistors), we must synchronize them before using in the FSM.

Double-Flop Synchronizer

// Metastability protection always @(posedge clk) begin scl_sync[1] <= scl_sync[0]; scl_sync[0] <= scl_in; sda_sync[1] <= sda_sync[0]; sda_sync[0] <= sda_in; end // Stable version for FSM assign scl_stable = scl_sync[1]; assign sda_stable = sda_sync[1];

Rising/Falling Edge Detection

always @(posedge clk) begin scl_r <= scl_stable; sda_r <= sda_stable; end assign scl_rising = (~scl_r & scl_stable); assign scl_falling = (scl_r & ~scl_stable); assign sda_rising = (~sda_r & sda_stable); assign sda_falling = (sda_r & ~sda_stable);
Why 2 Flops? First flop samples the async input; second flop prevents metastability issues. After 2 clock cycles, you have a stable, edge-detected signal safe for FSM logic.

Open-Drain Output Logic

The key to I2C's wired-AND property is open-drain outputs:

// Open-drain: device pulls LOW or releases (HIGH-Z) assign scl = (scl_out == 1'b0) ? 1'b0 : 1'bz; assign sda = (sda_out == 1'b0) ? 1'b0 : 1'bz; // Pull-up simulation (in testbench) 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;

Collision Detection Logic

If master releases SDA but reads it as LOW, another device is driving it:

always @(posedge clk) begin if (sda_out == 1'b1 && sda_stable == 1'b0) begin collision_detected <= 1'b1; error <= 1'b1; end end

SCL Clock Generation

SCL frequency is derived from system clock using a divider counter:

parameter CLK_DIV_100K = 50; // For 100 kHz @ 10 MHz system parameter CLK_DIV_400K = 12; // For 400 kHz @ 10 MHz system always @(posedge clk) begin if (clk_div_counter == 0) begin scl_toggle <= ~scl_toggle; clk_div_counter <= clk_div - 1; end else begin clk_div_counter <= clk_div_counter - 1; end end
Frequency Calculation: SCL_freq = System_Clock / (2 × CLK_DIV) 100 kHz = 10 MHz / (2 × 50) ✓ 400 kHz = 10 MHz / (2 × 12.5) ≈ 400 kHz ✓

SystemVerilog Implementation

Here's a simplified FSM excerpt showing state transitions:

always @(posedge clk or negedge rst_n) begin if (~rst_n) begin state <= IDLE; scl_out <= 1'b1; sda_out <= 1'b1; busy <= 1'b0; end else begin case (state) IDLE: begin busy <= 1'b0; if (start_cond_req) begin state <= START_COND; busy <= 1'b1; end end START_COND: begin if (scl_stable) begin sda_out <= 1'b0; state <= TX_DATA; end end // ... other states ... STOP_COND: begin if (sda_rising) begin state <= IDLE; busy <= 1'b0; end end endcase end end
Best Practice: Use case statements with clear state naming (enums in SV). Avoid mixing synchronous and asynchronous logic in the same always block. Keep FSM state transitions event-driven (edge detection preferred over level sensing).

Key Takeaways

  • The I2C Master uses a 6-state FSM to manage all bus phases.
  • Edge detection via double-flop synchronizers prevents metastability.
  • Open-drain logic enables wired-AND behavior and collision detection.
  • SCL clock generation uses dividers to derive I2C speed from system clock.
  • Each state has clear entry/exit conditions for deterministic behavior.
  • Collision detection monitors for unexpected line changes during transmission.
  • Clock stretching tolerance allows slaves to hold SCL LOW indefinitely.

What's Next?

In Part 3, we'll build the Slave Responder and dive into how pull-ups, open-drain simulation, and slave-side ACK logic work in SystemVerilog.

See you in Part 3!

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 (You are here)
Part 3: Slave Responder & Pull-up Logic
Part 4: Testbench Design & Test Cases
Part 5: Interview Prep & Optimization

Comments

Popular posts from this blog

Module 1 — SystemVerilog Basics

VLSI Verification  •  SystemVerilog Series Module 1 — SystemVerilog Basics A complete, example-driven walkthrough of SystemVerilog fundamentals: data types, variables vs nets, arrays of every flavor, strings, enums, structs, unions, and typedef — the building blocks every verification engineer needs before UVM. 🧩 Beginner → Intermediate ⏱ ~25 min read 💻 Code Examples Included 📑 Table of Contents Introduction to SystemVerilog Verilog vs SystemVerilog Data Types: 2-State vs 4-State Logic Variables and Nets Arrays: Packed vs Unpacked Dynamic Arrays Queues Associative Arrays Strings Enumerations Structures Unions Typedef 1 Introduction to SystemVerilog SystemVerilog (IEEE 1800) is a unified hardware description and verification language (HDVL). It extends Verilog (IEEE 1364) with powerful constructs for design (interfaces, always_comb/always_ff, enhanced d...

Clock Domain Crossing (CDC): A Complete Beginner's Guide to Metastability, Synchronizers & Safe Data Transfer

Clock Domain Crossing (CDC): A Complete Beginner's Guide to Metastability, Synchronizers & Safe Data Transfer Modern System-on-Chip (SoC) designs rarely operate with a single clock. Instead, different modules such as CPUs, GPUs, memory controllers, communication interfaces, and peripherals often run at different clock frequencies or even use completely independent clock sources . When data moves from one clock domain to another, timing uncertainty can cause serious problems such as metastability , data corruption , and unpredictable system behaviour . This challenge is known as Clock Domain Crossing (CDC) . CDC is one of the most important topics in ASIC Design , RTL Design , ASIC Verification , FPGA Design , and SoC Architecture . Every modern chip—from smartphones to automotive processors—uses CDC techniques to ensure reliable communication between multiple clock domains. In this article, we'll explore CDC fundamentals, metastability, synchronization techniques, asynchro...

GPU Memory System & Interconnect: How Data Travels Inside a Modern GPU

GPU Memory System & Interconnect: How Data Travels Inside a Modern GPU Modern GPUs deliver incredible computing power by processing thousands of operations simultaneously. While compute cores often receive the spotlight, the real performance of a GPU depends heavily on how efficiently data moves between memory and compute units. A GPU memory system is designed to provide extremely high bandwidth while keeping latency as low as possible. This is achieved through a collection of specialized hardware components working together, including the Memory Controller, HBM/GDDR memory, Crossbar, Network-on-Chip (NoC), DMA Engine, PCIe Interface, and NVLink. In this article, we'll explore each component in detail and understand how they collectively enable high-performance parallel computing. Why Does GPU Memory Matter? Every GPU application—from gaming and graphics rendering to AI model training and scientific simulations—requires continuous movement of enormous amounts of data. For examp...

AXI Verification Interview Questions and Real-World Debugging

AMBA Protocol Series · Part 10 of 10 AXI Verification Interview Questions and Real-World Debugging Common protocol violations, how to read them off a waveform, and a curated set of interview questions with concise, correct answers — the capstone of the core 10-day series. AMBA Protocol Series: From APB & AHB to AXI — A Complete Guide for VLSI Engineers This wraps up the core 10-day series. Everything here draws directly on Days 1–9 — if any answer below feels unfamiliar, that's a good signal for which earlier post to revisit. (If you want to go further, see the 20-day extended list at the end of this post.) In This Article Common Protocol Violations Debugging From a Waveform Interview Questions and Answers Series Recap Extending the Series 1. Common Protocol Violations VALID deasserted before handshake — a source drops VALID before READY ever went high (violates the Day 8 stabi...

Timing Basics in Static Timing Analysis (STA): Delay Models, Sequential Timing, and Wire Delays

Timing Basics in Static Timing Analysis (STA) Static Timing Analysis (STA) is one of the most important verification techniques used in modern VLSI design. Before a chip is manufactured, engineers must verify that every signal reaches its destination within the required clock period. Even if a design passes functional simulation, it may still fail after fabrication because of timing violations. Unlike simulation, Static Timing Analysis mathematically analyzes every timing path without applying input test vectors. This makes STA significantly faster and capable of analyzing millions of timing paths efficiently. Table of Contents What is Static Timing Analysis? Why STA is Required Propagation Delay Contamination Delay Cell Delay Models Combinational Gate Delay Sequential Timing Setup Time Hold Time Wire Delay RC Delay Model PVT Corners Worked Examples Interview Questions Key Takeaways What is Static Timing Analysis? Static Ti...

Cache Basics and Cache Mapping Techniques

  Cache Basics and Cache Mapping Techniques  Modern processors execute billions of instructions every second. However, processor speed has increased much faster than main memory (RAM) speed. This difference, known as the memory wall , creates a performance bottleneck because the CPU often has to wait for data from slower memory. To solve this problem, computer systems use cache memory —a small, high-speed memory located between the CPU and the main memory. In this article, we will understand the fundamentals of cache memory, the principle of locality, cache organization, and the three major cache mapping techniques. What is Cache Memory? Cache memory is a small, high-speed memory that stores copies of frequently accessed data from the main memory. Since accessing cache is much faster than accessing RAM, the CPU first checks whether the required data is available in the cache. If the data is found in the cache, it is called a cache hit . Otherwise, the CPU fetches the dat...

CMOS Power & Scaling Explained: Dynamic Power, Leakage Power, Short-Circuit Power & Technology Scaling

  CMOS Power & Scaling Explained: Dynamic Power, Leakage Power, Short-Circuit Power & Technology Scaling Day 6 | CMOS Design Fundamentals | The Silicon Stack As integrated circuits continue to become smaller, faster, and more powerful, power consumption has emerged as one of the biggest challenges in modern VLSI design. While early digital circuits primarily focused on functionality and speed, today's processors, AI accelerators, smartphones, and IoT devices must deliver high performance while operating within strict power and thermal limits. A modern System-on-Chip (SoC) may contain billions of CMOS transistors , each switching billions of times per second. Even a tiny amount of power consumed by each transistor adds up to significant energy consumption across the entire chip. Excessive power not only drains batteries but also generates heat, reduces reliability, and limits performance. Understanding where CMOS power comes from and how it can be reduced is therefore ...