Codiode

I2C Start and Stop Detector

MediumVerilog / SystemVerilogBuild

In I2C, sda normally changes only while scl is low. A change while scl is high is a signal in itself: falling means Start, rising means Stop.

The module continuously samples the scl and sda inputs using a high-frequency system clock (clk). It must output a single-cycle high pulse on start_det when sda transitions from high to low while scl is high. Conversely, it must output a single-cycle high pulse on stop_det when sda transitions from low to high while scl is high. All sda transitions that occur while scl is low represent normal data transfer and must be ignored.

Positive-edge triggered clock clk. Asynchronous active-low reset rst_n. On reset, both start_det and stop_det are 0. The internal register tracking the previous state of sda must reset to 1 (the I2C idle state) to prevent false Stop condition detections when the module comes out of reset.

Worked Trace:

  • Cycle 1: rst_n=0 → start_det=0, stop_det=0
  • Cycle 2: rst_n=1, scl=1, sda=1 → start_det=0, stop_det=0
  • Cycle 3: rst_n=1, scl=1, sda=0 → start_det=1, stop_det=0 (SDA fell while SCL high)
  • Cycle 4: rst_n=1, scl=0, sda=0 → start_det=0, stop_det=0 (Start pulse is 1 cycle)
  • Cycle 5: rst_n=1, scl=0, sda=1 → start_det=0, stop_det=0 (Data change ignored)
  • Cycle 6: rst_n=1, scl=1, sda=1 → start_det=0, stop_det=0
  • Cycle 7: rst_n=1, scl=1, sda=0 → start_det=1, stop_det=0 (Repeated start)
  • Cycle 8: rst_n=1, scl=1, sda=1 → start_det=0, stop_det=1 (Stop condition)
Signal Direction Width Description
clk input 1 Positive-edge triggered system clock
rst_n input 1 Asynchronous active-low reset; outputs go to 0
scl input 1 I2C clock line
sda input 1 I2C data line
start_det output 1 Pulses high for 1 clk cycle upon Start detection
stop_det output 1 Pulses high for 1 clk cycle upon Stop detection

Constraints

  • The start_det and stop_det outputs must be exactly 1 clock cycle wide.
  • Internal registers tracking the previous state of the sda line must reset to 1.
  • Both outputs must be registered and updated on the positive edge of clk.
  • Transitions on sda must be ignored if scl is 0.

Topics

Edge DetectionInterfacesProtocols

Solve this problem

Write the module in Verilog, SystemVerilog or VHDL. Your submission is compiled and simulated against the problem's testbench. You get the waveform back, not a stored answer.

This problem is part of Codiode Pro. The statement above is free to read.

The circuit builder and code editor need a desktop screen. On a phone, read the problem here and open it on a laptop to solve.

Part of Interfaces and Protocols

Problem 16 of 19 in the Interfaces and Protocols track.

Related problems

Browse all problems · Verilog practice problems · Learning tracks

Solve this on a laptopSend yourself the link and continue there.