-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_i2c.py
115 lines (91 loc) · 3.81 KB
/
test_i2c.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import unittest
from migen import *
from migen.fhdl.specials import Tristate
from i2c import *
class _MockPads:
def __init__(self):
self.scl = Signal()
self.sda = Signal()
class _MockTristate(Module):
def __init__(self, t):
oe = Signal()
self.comb += [
t.target.eq(t.o),
oe.eq(t.oe),
]
Tristate.lower = _MockTristate
class TestI2C(unittest.TestCase):
def test_i2c(self):
pads = _MockPads()
dut = I2CMaster(pads)
def check_trans(scl, sda):
scl_init, sda_init = (yield dut.i2c.scl_o), (yield dut.i2c.sda_o)
timeout = 0
while True:
timeout += 1
self.assertLess(timeout, 20)
scl_now, sda_now = (yield dut.i2c.scl_o), (yield dut.i2c.sda_o)
if scl_now != scl_init or sda_now != sda_init:
self.assertEqual(scl_now, scl)
self.assertEqual(sda_now, sda)
return
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):
yield from check_trans(scl=False, sda=value)
yield from check_trans(scl=True, sda=value)
def write_ack(value):
yield from check_trans(scl=False, sda=not value)
yield from check_trans(scl=True, sda=not value)
yield from wait_idle()
def read_bit(value):
yield from check_trans(scl=False, sda=True)
yield dut.sda_t.i.eq(value)
yield from check_trans(scl=True, sda=True)
def read_ack(value):
yield from check_trans(scl=False, sda=True)
yield dut.sda_t.i.eq(not value)
yield from check_trans(scl=True, sda=True)
yield from wait_idle()
ack = ((yield from dut.bus.read(I2C_XFER_ADDR)) & I2C_ACK) != 0
self.assertEqual(ack, value)
def check():
yield from dut.bus.write(I2C_CONFIG_ADDR, 4)
yield from dut.bus.write(I2C_XFER_ADDR, I2C_START)
yield from check_trans(scl=True, sda=False)
yield from wait_idle()
yield from dut.bus.write(I2C_XFER_ADDR, I2C_WRITE | 0x82)
for i in [True, False, False, False, False, False, True, False]:
yield from write_bit(i)
yield from read_ack(True)
yield from dut.bus.write(I2C_XFER_ADDR, I2C_WRITE | 0x18)
for i in [False, False, False, True, True, False, False, False]:
yield from write_bit(i)
yield from read_ack(False)
yield from dut.bus.write(I2C_XFER_ADDR, I2C_START | I2C_STOP)
yield from check_trans(scl=True, sda=False)
yield from wait_idle()
yield from dut.bus.write(I2C_XFER_ADDR, I2C_READ)
for i in [False, False, False, True, True, False, False, False]:
yield from read_bit(i)
data = (yield from dut.bus.read(I2C_XFER_ADDR)) & 0xff
self.assertEqual(data, 0x18)
yield from write_ack(False)
yield from dut.bus.write(I2C_XFER_ADDR, I2C_READ | I2C_ACK)
for i in [True, False, False, False, True, False, False, False]:
yield from read_bit(i)
data = (yield dut.i2c.data)
self.assertEqual(data, 0x88)
yield from write_ack(True)
yield from dut.bus.write(I2C_XFER_ADDR, I2C_STOP)
yield from check_trans(scl=False, sda=False)
yield from wait_idle()
run_simulation(dut, check())