add simple test for UART

This commit is contained in:
Sebastien Bourdeauducq 2019-03-28 19:39:12 +08:00
parent 472114c136
commit 3dd10e6b9b
3 changed files with 56 additions and 17 deletions

View File

View File

@ -0,0 +1,41 @@
import unittest
from nmigen import *
from nmigen.back.pysim import *
from heavycomps import uart
class Loopback:
def __init__(self, tuning_word=2**31):
self.tx = uart.RS232TX(tuning_word)
self.rx = uart.RS232RX(tuning_word)
def elaborate(self, platform):
m = Module()
m.submodules.tx = self.tx
m.submodules.rx = self.rx
m.d.comb += self.rx.rx.eq(self.tx.tx)
return m
class TestUART(unittest.TestCase):
def test_loopback(self):
dut = Loopback()
test_vector = [32, 129, 201, 39, 0, 255]
with Simulator(Fragment.get(dut, None)) as sim:
sim.add_clock(1e-6)
def send():
for value in test_vector:
yield from dut.tx.write(value)
def receive():
for value in test_vector:
received = yield from dut.rx.read()
self.assertEqual(received, value)
sim.add_sync_process(send)
sim.add_sync_process(receive)
sim.run()

View File

@ -1,6 +1,5 @@
from nmigen import * from nmigen import *
from nmigen.lib.cdc import MultiReg from nmigen.lib.cdc import MultiReg
from nmigen.cli import main
class RS232RX: class RS232RX:
@ -50,6 +49,14 @@ class RS232RX:
return m return m
def read(self):
while not (yield self.stb):
yield
value = yield self.data
# clear stb, otherwise multiple calls to this generator keep returning the same value
yield
return value
class RS232TX: class RS232TX:
def __init__(self, tuning_word): def __init__(self, tuning_word):
@ -92,19 +99,10 @@ class RS232TX:
return m return m
def write(self, data):
class Loopback: yield self.stb.eq(1)
def elaborate(self, platform): yield self.data.eq(data)
m = Module() yield
tuning_word = 2**31 while not (yield self.ack):
tx = RS232TX(tuning_word) yield
rx = RS232RX(tuning_word) yield self.stb.eq(0)
m.submodules += tx, rx
m.d.comb += rx.rx.eq(tx.tx)
m.d.comb += tx.data.eq(42)
m.d.comb += tx.stb.eq(1)
return m
if __name__ == "__main__":
uart = Loopback()
main(uart)