# Multiple drivers on I2C bus

**URL:** <https://verificationacademy.com/forums/t/multiple-drivers-on-i2c-bus/48371>\
**Category:** SystemVerilog\
**Created:** [August 30, 2024, 12:25pm UTC](https://verificationacademy.com/forums/t/multiple-drivers-on-i2c-bus/48371 "2024-08-30T12:25:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![abasili](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/ecb155/32.png) [@abasili](https://verificationacademy.com/forums/u/abasili)\
**Post date:** [August 30, 2024, 12:25pm UTC](https://verificationacademy.com/forums/t/multiple-drivers-on-i2c-bus/48371/1 "2024-08-30T12:25:14Z")

</div>

I have two different types of interfaces that are both trying to drive a signal to the dut on an I2C interface:

```auto
interface i2c_if_type0 (inout wire sda, inout wire clk);
    logic sda_in, sda_out, clk_in, clk_out;
    // skip cod for out_cfg which set's the output in either push-pull or in open-drain
    assign sda = out_cfg ? sda_out : (sda_out ? 1'bz : 0);
    assign clk = out_cfg ? clk_out : (clk_out ? 1'bz : 0);
    sda_in = sda;
    clk_in = clk;
endinterface

interface i2c_if_type1 (inout wire sda, inout wire clk);
    logic sda_in, sda_out, clk_in, clk_out;
    assign (weak1, strong0) sda = sda_out;
    assign (weak1, strong0) clk = clk_out;
    sda_in = sda;
    clk_in = clk;
endinterface

```

```auto
module tb;
    wire i2c_sda;
    wire i2c_clk;

    // instantiate the interfaces
    i2c_if_type0 i2c_if0();
    i2c_if_type1 i2c_if1();

    // I thought of using 'tran' in order to pass the resolution to the interface pins as well
    tran u_tran0 (i2c_if0.sda, i2c_sda);
    tran u_tran0 (i2c_if1.sda, i2c_sda);
    tran u_tran0 (i2c_if0.clk, i2c_clk);
    tran u_tran0 (i2c_if1.clk, i2c_clk);

    // here it should be sufficient to specify the strength, believing that i2c_sda and i2c_clk are
    // going to be declared at some point.
    pullup (pull1) pullup_sda (i2c_sda);
    pullup (pull1) pullup_clk (i2c_clk);
    
    // connectivity to dut
    dut u_dut(
      .sda(i2c_sda),
      .clk(i2c_clk)
    );

endmodule

```

Well, everything works just fine as long as the DUT doesn’t try to drive the PAD. The PAD turns ‘x’ when it’s being driven by the dut. I suspect something to do with the ‘tran’ not correctly propagating 1’bz value… maybe the strength of the signals is messed up. It’s something that should be trivial, I’ve just spent too much time looking at it and now I’m highly biased.

Any idea?

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [August 30, 2024, 4:17pm UTC](https://verificationacademy.com/forums/t/multiple-drivers-on-i2c-bus/48371/2 "2024-08-30T16:17:43Z")

</div>

You declared `sda` as an interface port, why not use it? No need for tran primitives.

What mode is `out_cfg` in when the DUT is driving?

What code is the DUT using to drive `i2c_sda`?

---

<div class="post-metadata">

**Author:** ![abasili](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/ecb155/32.png) [@abasili](https://verificationacademy.com/forums/u/abasili)\
**Post date:** [August 30, 2024, 6:42pm UTC](https://verificationacademy.com/forums/t/multiple-drivers-on-i2c-bus/48371/3 "2024-08-30T18:42:19Z")

</div>

> You declared `sda` as an interface port, why not use it? No need for tran primitives.

you are totally right, here’s the new code:

```auto
module tb;
    wire i2c_sda;
    wire i2c_clk;

    // instantiate the interfaces
    i2c_if_type0 i2c_if0(.sda(i2c_sda), .clk(i2c_clk));
    i2c_if_type1 i2c_if1(.sda(i2c_sda), .clk(i2c_clk));

    // here it should be sufficient to specify the strength, believing that i2c_sda and i2c_clk are
    // going to be declared at some point.
    pullup (pull1) pullup_sda (i2c_sda);
    pullup (pull1) pullup_clk (i2c_clk);
    
    // connectivity to dut
    dut u_dut(
      .sda(i2c_sda),
      .clk(i2c_clk)
    );

endmodule

```

> What mode is `out_cfg` in when the DUT is driving?

you can assume `out_cfg` is statically set at reset (and in the current case it’s `1'b0`).

> What code is the DUT using to drive `i2c_sda` ?

After some digging, it looks like the driver is this one:

```auto
// while dut is driving a 0 on PUi the PAD goes to 1'bx
bufif1 (weak0, weak1) (PAD, 1'b1, PUi);

```

so on the `i2c_sda` we end up with one driver setting a `weak0` while the i2c\_if0 is driving a `highZ` and the `pullup` is driving a `pull1`. I’m not entirely sure who wins between `weak0` and `pull1`… Is that going to lead to an `1'bx` ?

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [August 30, 2024, 7:08pm UTC](https://verificationacademy.com/forums/t/multiple-drivers-on-i2c-bus/48371/4 "2024-08-30T19:08:30Z")

</div>

Change how you model open drain in `i2c_if_type1`

> [@Push\_pull, open drain in verilog](https://verificationacademy.com/forums/t/push-pull-open-drain-in-verilog/34443/2):
>
> In reply to [Dhee\_27](https://verificationacademy.com/forums/t/push-pull-open-drain-in-verilog/34443): Here’s a [good explanation](https://www.edaboard.com/showthread.php?97365-question-push-pull-or-open-drain-mode&p=424236&viewfull=1#post424236) about the concepts. Push/Pull is the normal mode of most gate outputs - drive a zero or one. Open-drain can drive a zero or high-Z. In Verilog, this can be done with a continuous assignment. assign push\_pull = data\_reg; // implicit assign (strong0, strong1) push\_pull = data\_reg; // explicit assign (strong0, highz1) open\_drain = data\_reg; If this needs to be programmable, you can use a mode bit. assign bus = mode ? push\_pull : open\_drain; …

---

<div class="post-metadata">

**Author:** ![abasili](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/ecb155/32.png) [@abasili](https://verificationacademy.com/forums/u/abasili)\
**Post date:** [August 31, 2024, 8:21am UTC](https://verificationacademy.com/forums/t/multiple-drivers-on-i2c-bus/48371/5 "2024-08-31T08:21:52Z")

</div>

After some further investigation, I’ve realized the driver for `i2c_if_type0` was driving `sda_out` to `1'bz`, which led the assignment go to `1'bx`, so I had to make the following change:

```auto
interface i2c_if_type0 (inout wire sda, inout wire clk);
    logic sda_in, sda_out, clk_in, clk_out;
    // skip cod for out_cfg which set's the output in either push-pull or in open-drain
    // assign sda = out_cfg ? sda_out : (sda_out ? 1'bz : 0);
    // assign clk = out_cfg ? clk_out : (clk_out ? 1'bz : 0);
    assign sda = out_cfg ? sda_out : (sda_out === 0 ? 0 : 1'bz); // <----
    assign clk = out_cfg ? clk_out : (clk_out === 0 ? 0 : 1'bz); // <----
    sda_in = sda;
    clk_in = clk;
endinterface

```
