|
8 | 8 | from uart_demo.ports import PortGroup
|
9 | 9 |
|
10 | 10 |
|
11 |
| -# TODO |
| 11 | +class LoopbackUARTTestCase(unittest.TestCase): |
| 12 | + def test_hello(self): |
| 13 | + ports = PortGroup() |
| 14 | + ports.rx = io.SimulationPort("i", 1) |
| 15 | + ports.tx = io.SimulationPort("o", 1) |
| 16 | + |
| 17 | + dut = LoopbackUART(ports, clk_freq=48e6, baudrate=115200) |
| 18 | + |
| 19 | + # send "hello" to the UART receiver |
| 20 | + async def rx_testbench(ctx): |
| 21 | + ctx.set(ports.rx.i, 1) |
| 22 | + await ctx.delay(1 / dut.baudrate) |
| 23 | + |
| 24 | + for rx_chr in "hello": |
| 25 | + # send the start bit |
| 26 | + ctx.set(ports.rx.i, 0) |
| 27 | + await ctx.delay(1 / dut.baudrate) |
| 28 | + |
| 29 | + # send data bits |
| 30 | + for rx_bit in reversed(f"{ord(rx_chr):08b}"): |
| 31 | + ctx.set(ports.rx.i, int(rx_bit)) |
| 32 | + await ctx.delay(1 / dut.baudrate) |
| 33 | + |
| 34 | + # send the stop bit |
| 35 | + ctx.set(ports.rx.i, 1) |
| 36 | + await ctx.delay(1 / dut.baudrate) |
| 37 | + |
| 38 | + # check that "hello" is transmitted back |
| 39 | + async def tx_testbench(ctx): |
| 40 | + for tx_chr in "hello": |
| 41 | + # wait for the start bit |
| 42 | + await ctx.negedge(ports.tx.o) |
| 43 | + await ctx.delay(1 / dut.baudrate) |
| 44 | + |
| 45 | + # check data bits |
| 46 | + for tx_bit in reversed(f"{ord(tx_chr):08b}"): |
| 47 | + self.assertEqual(ctx.get(ports.tx.o), int(tx_bit)) |
| 48 | + await ctx.delay(1 / dut.baudrate) |
| 49 | + |
| 50 | + # check the stop bit |
| 51 | + self.assertEqual(ctx.get(ports.tx.o), 1) |
| 52 | + |
| 53 | + sim = Simulator(dut) |
| 54 | + sim.add_clock(period=1 / 48e6) |
| 55 | + sim.add_testbench(rx_testbench) |
| 56 | + sim.add_testbench(tx_testbench) |
| 57 | + |
| 58 | + with sim.write_vcd(vcd_file="test.vcd"): |
| 59 | + sim.run() |
0 commit comments