2023-08-25 01:25:37 -04:00
|
|
|
#!/usr/bin/env python3
|
2023-01-12 20:57:27 -05:00
|
|
|
#
|
|
|
|
# This file is part of MiSoC and has been adapted/modified for Litex.
|
|
|
|
#
|
|
|
|
# Copyright 2007-2023 / M-Labs Ltd
|
|
|
|
# Copyright 2012-2015 / Enjoy-Digital
|
|
|
|
# Copyright from Misoc LICENCE file added above
|
|
|
|
#
|
|
|
|
# Copyright 2023 Andrew Dennison <andrew@motec.com.au>
|
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
2023-01-12 21:37:48 -05:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
from migen import *
|
|
|
|
from migen.fhdl.specials import Tristate
|
|
|
|
|
2023-01-12 20:57:27 -05:00
|
|
|
from litex.soc.cores.i2c import *
|
2023-01-12 21:37:48 -05:00
|
|
|
|
|
|
|
|
|
|
|
class _MockPads:
|
|
|
|
def __init__(self):
|
|
|
|
self.scl = Signal()
|
|
|
|
self.sda = Signal()
|
|
|
|
|
|
|
|
|
|
|
|
class _MockTristateImpl(Module):
|
|
|
|
def __init__(self, t):
|
2023-08-28 01:40:19 -04:00
|
|
|
t.i_mock = Signal(reset=True)
|
2023-01-12 21:37:48 -05:00
|
|
|
self.comb += [
|
2023-08-28 01:40:19 -04:00
|
|
|
If(t.oe,
|
2023-09-03 21:16:49 -04:00
|
|
|
t.target.eq(t.o),
|
|
|
|
t.i.eq(t.o),
|
2023-08-28 01:40:19 -04:00
|
|
|
).Else(
|
2023-09-03 21:16:49 -04:00
|
|
|
t.target.eq(t.i_mock),
|
|
|
|
t.i.eq(t.i_mock),
|
2023-08-28 01:40:19 -04:00
|
|
|
),
|
2023-01-12 21:37:48 -05:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class _MockTristate:
|
2023-08-28 01:40:19 -04:00
|
|
|
"""A mock `Tristate` for simulation
|
|
|
|
|
|
|
|
This simulation ensures the TriState input (_i) tracks the output (_o) when output enable
|
|
|
|
(_oe) = 1. A new i_mock `Signal` is added - this can be written to in the simulation to represent
|
|
|
|
input from the external device.
|
|
|
|
|
|
|
|
Example usage:
|
|
|
|
|
|
|
|
class TestMyModule(unittest.TestCase):
|
|
|
|
def test_mymodule(self):
|
|
|
|
dut = MyModule()
|
|
|
|
io = Signal()
|
|
|
|
dut.io_t = TSTriple()
|
|
|
|
self.io_tristate = self.io_t.get_tristate(io)
|
|
|
|
|
|
|
|
dut.comb += [
|
|
|
|
dut.io_t.oe.eq(signal_for_oe),
|
|
|
|
dut.io_t.o.eq(signal_for_o),
|
|
|
|
signal_for_i.eq(dut.io_t.i),
|
|
|
|
]
|
|
|
|
|
|
|
|
def generator()
|
|
|
|
yield dut.io_tristate.i_mock.eq(some_value)
|
|
|
|
if (yield dut.io_t.oe):
|
|
|
|
self.assertEqual((yield dut.scl_t.i), (yield dut.io_t.o))
|
|
|
|
else:
|
|
|
|
self.assertEqual((yield dut.scl_t.i), some_value)
|
|
|
|
|
|
|
|
"""
|
2023-09-03 21:16:49 -04:00
|
|
|
|
2023-01-12 21:37:48 -05:00
|
|
|
@staticmethod
|
|
|
|
def lower(t):
|
|
|
|
return _MockTristateImpl(t)
|
|
|
|
|
|
|
|
|
|
|
|
class TestI2C(unittest.TestCase):
|
|
|
|
def test_i2c(self):
|
|
|
|
pads = _MockPads()
|
|
|
|
dut = I2CMaster(pads)
|
|
|
|
|
2023-08-29 03:31:28 -04:00
|
|
|
def check_trans(scl, sda, msg=""):
|
2023-08-28 01:49:49 -04:00
|
|
|
scl, sda = int(scl), int(sda)
|
|
|
|
scl_init, sda_init = (yield dut.scl_t.i), (yield dut.sda_t.i)
|
2023-01-12 21:37:48 -05:00
|
|
|
timeout = 0
|
|
|
|
while True:
|
2023-08-28 01:49:49 -04:00
|
|
|
scl_now, sda_now = (yield dut.scl_t.i), (yield dut.sda_t.i)
|
|
|
|
if scl_now == scl and sda_now == sda:
|
2023-01-12 21:37:48 -05:00
|
|
|
return
|
2023-08-28 01:49:49 -04:00
|
|
|
timeout += 1
|
|
|
|
self.assertLess(timeout, 20,
|
2023-08-29 03:31:28 -04:00
|
|
|
f"\n*** {msg} timeout. Waiting for: " +
|
2023-08-28 01:49:49 -04:00
|
|
|
f"scl:{scl_now} checking:{scl_init}=>{scl} " +
|
|
|
|
f"sda:{sda_now} checking:{sda_init}=>{sda} ***"
|
|
|
|
)
|
2023-01-12 21:37:48 -05:00
|
|
|
yield
|
|
|
|
|
|
|
|
def wait_idle(do=lambda: ()):
|
|
|
|
timeout = 0
|
|
|
|
while True:
|
|
|
|
timeout += 1
|
|
|
|
self.assertLess(timeout, 20)
|
|
|
|
idle = ((yield from dut.bus.read(I2C_XFER_ADDR)) & I2C_IDLE) != 0
|
|
|
|
if idle:
|
|
|
|
return
|
|
|
|
yield
|
|
|
|
|
|
|
|
def write_bit(value):
|
2023-09-03 21:16:49 -04:00
|
|
|
# print(f"write_bit:{value}")
|
2023-01-12 21:37:48 -05:00
|
|
|
yield from check_trans(scl=False, sda=value)
|
2023-09-03 21:16:49 -04:00
|
|
|
yield from check_trans(scl=True, sda=value)
|
2023-01-12 21:37:48 -05:00
|
|
|
|
|
|
|
def write_ack(value):
|
2023-09-03 21:16:49 -04:00
|
|
|
# print(f"write_ack:{value}")
|
2023-01-12 21:37:48 -05:00
|
|
|
yield from check_trans(scl=False, sda=not value)
|
2023-09-03 21:16:49 -04:00
|
|
|
yield from check_trans(scl=True, sda=not value)
|
2023-01-12 21:37:48 -05:00
|
|
|
yield from wait_idle()
|
|
|
|
|
|
|
|
def read_bit(value):
|
2023-08-29 03:31:28 -04:00
|
|
|
print(f"read_bit:{value}")
|
2023-08-28 01:49:49 -04:00
|
|
|
yield dut.sda_tristate.i_mock.eq(value)
|
2023-08-29 03:31:28 -04:00
|
|
|
yield from check_trans(scl=True, sda=value)
|
|
|
|
yield from check_trans(scl=False, sda=value)
|
|
|
|
yield dut.sda_tristate.i_mock.eq(True)
|
2023-01-12 21:37:48 -05:00
|
|
|
|
|
|
|
def read_ack(value):
|
2023-08-28 01:49:49 -04:00
|
|
|
#print(f"read_ack:{value}")
|
2023-01-12 21:37:48 -05:00
|
|
|
yield from check_trans(scl=False, sda=True)
|
2023-08-28 01:49:49 -04:00
|
|
|
yield dut.sda_tristate.i_mock.eq(not value)
|
|
|
|
yield from check_trans(scl=True, sda=not value)
|
2023-01-12 21:37:48 -05:00
|
|
|
yield from wait_idle()
|
2023-08-28 01:49:49 -04:00
|
|
|
yield dut.sda_tristate.i_mock.eq(True)
|
2023-01-12 21:37:48 -05:00
|
|
|
ack = ((yield from dut.bus.read(I2C_XFER_ADDR)) & I2C_ACK) != 0
|
|
|
|
self.assertEqual(ack, value)
|
|
|
|
|
2023-08-29 03:31:28 -04:00
|
|
|
def i2c_restart():
|
|
|
|
yield from check_trans(scl=False, sda=True, msg="checking restart precondition")
|
|
|
|
yield from dut.bus.write(I2C_XFER_ADDR, I2C_START)
|
|
|
|
yield from check_trans(scl=False, sda=True, msg="checking restart0")
|
|
|
|
yield from check_trans(scl=True, sda=True, msg="checking restart1")
|
|
|
|
yield from check_trans(scl=True, sda=False, msg="checking start0")
|
|
|
|
yield from wait_idle()
|
|
|
|
|
|
|
|
def i2c_start():
|
|
|
|
yield from check_trans(scl=True, sda=True, msg="checking start precondition")
|
|
|
|
yield from dut.bus.write(I2C_XFER_ADDR, I2C_START)
|
|
|
|
yield from check_trans(scl=True, sda=False, msg="checking start0")
|
|
|
|
yield from wait_idle()
|
|
|
|
|
|
|
|
def i2c_stop():
|
|
|
|
yield from check_trans(scl=False, sda=True, msg="checking stop after read or write")
|
|
|
|
yield from dut.bus.write(I2C_XFER_ADDR, I2C_STOP)
|
|
|
|
yield from check_trans(scl=False, sda=False, msg="checking STOP0")
|
|
|
|
yield from check_trans(scl=True, sda=False, msg="checking STOP1")
|
|
|
|
yield from check_trans(scl=True, sda=True, msg="checking STOP2")
|
|
|
|
yield from wait_idle()
|
|
|
|
|
2023-08-28 01:49:49 -04:00
|
|
|
def i2c_write(value, ack=True):
|
|
|
|
value = int(value)
|
2023-09-03 21:16:49 -04:00
|
|
|
test_bin = "{0:08b}".format(value)
|
|
|
|
# print(f"I2C_WRITE | {hex(value)}:0x{test_bin}")
|
2023-08-28 01:49:49 -04:00
|
|
|
yield from dut.bus.write(I2C_XFER_ADDR, I2C_WRITE | value)
|
|
|
|
for i in list(test_bin):
|
|
|
|
yield from write_bit(int(i))
|
|
|
|
yield from read_ack(True)
|
|
|
|
|
|
|
|
def i2c_read(value, ack=True):
|
|
|
|
value = int(value)
|
2023-09-03 21:16:49 -04:00
|
|
|
test_bin = "{0:08b}".format(value)
|
2023-08-29 03:31:28 -04:00
|
|
|
print(f"I2C_READ | {hex(value)}:0x{test_bin}")
|
2023-08-28 01:49:49 -04:00
|
|
|
yield from dut.bus.write(I2C_XFER_ADDR, I2C_READ | (I2C_ACK if ack else 0))
|
|
|
|
for i in list(test_bin):
|
|
|
|
yield from read_bit(int(i))
|
|
|
|
yield dut.sda_tristate.i_mock.eq(True)
|
2023-09-03 21:16:49 -04:00
|
|
|
data = (yield from dut.bus.read(I2C_XFER_ADDR)) & 0xFF
|
2023-08-28 01:49:49 -04:00
|
|
|
self.assertEqual(data, value)
|
|
|
|
yield from write_ack(ack)
|
|
|
|
|
2023-01-12 21:37:48 -05:00
|
|
|
def check():
|
|
|
|
yield from dut.bus.write(I2C_CONFIG_ADDR, 4)
|
2023-09-03 21:16:49 -04:00
|
|
|
data = (yield from dut.bus.read(I2C_CONFIG_ADDR)) & 0xFF
|
2023-01-31 18:53:09 -05:00
|
|
|
self.assertEqual(data, 4)
|
2023-01-12 21:37:48 -05:00
|
|
|
|
2023-08-29 03:31:28 -04:00
|
|
|
print("write 1 byte 0x18 to address 0x41")
|
|
|
|
yield from i2c_start()
|
2023-09-03 21:16:49 -04:00
|
|
|
yield from i2c_write(0x41 << 1 | 0)
|
2023-08-28 01:49:49 -04:00
|
|
|
yield from i2c_write(0x18, ack=False)
|
2023-08-29 03:31:28 -04:00
|
|
|
yield from i2c_stop()
|
2023-01-12 21:37:48 -05:00
|
|
|
|
2023-08-29 03:31:28 -04:00
|
|
|
print("read 1 byte from address 0x41")
|
|
|
|
yield from i2c_start()
|
2023-09-03 21:16:49 -04:00
|
|
|
yield from i2c_write(0x41 << 1 | 1)
|
2023-08-28 01:49:49 -04:00
|
|
|
yield from i2c_read(0x18, ack=False)
|
2023-01-12 21:37:48 -05:00
|
|
|
|
2023-08-29 19:58:06 -04:00
|
|
|
print("write 2 bytes 0x10 0x00 to address 0x11")
|
|
|
|
yield from i2c_restart()
|
|
|
|
yield from i2c_write(0x11 << 1 | 0)
|
|
|
|
yield from i2c_write(0x10, ack=True)
|
|
|
|
yield from i2c_write(0x00, ack=False)
|
|
|
|
yield from i2c_stop()
|
|
|
|
|
|
|
|
print("read 1 byte from address 0x11")
|
|
|
|
yield from i2c_start()
|
|
|
|
yield from i2c_write(0x11 << 1 | 1)
|
|
|
|
yield from i2c_read(0x81, ack=False)
|
|
|
|
|
2023-08-29 03:31:28 -04:00
|
|
|
print("read 2 bytes from address 0x55")
|
|
|
|
yield from i2c_restart()
|
2023-09-03 21:16:49 -04:00
|
|
|
yield from i2c_write(0x55 << 1 | 1)
|
2023-08-29 03:31:28 -04:00
|
|
|
yield from i2c_read(0xDE, ack=True)
|
|
|
|
yield from i2c_read(0xAD, ack=False)
|
|
|
|
yield from i2c_stop()
|
2023-01-12 21:37:48 -05:00
|
|
|
|
2023-08-28 01:49:49 -04:00
|
|
|
clocks = {
|
|
|
|
"sys": 10,
|
|
|
|
"async": (10, 3),
|
|
|
|
}
|
|
|
|
generators = {
|
|
|
|
"sys": [
|
|
|
|
check(),
|
|
|
|
],
|
|
|
|
}
|
|
|
|
run_simulation(dut, generators, clocks, special_overrides={Tristate: _MockTristate}, vcd_name="i2c.vcd")
|
2023-08-25 01:25:37 -04:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|