UART Drivers
Native UART Source (transmitter) and Sink (receiver) BFMs for cocotb testbenches. Pure-Python implementation with configurable baud rate, data width (5–9 bits), parity, and stop bits. Includes error injection methods for testing UART receiver robustness.
Quick Start
import cocotb
from routertl.sim import Tb, UartSource
from routertl.sim.cocotb.tb.drivers.uart_master import UartSink
@cocotb.test()
async def test_uart_loopback(dut):
tb = Tb(dut)
await tb.start_clock()
await tb.reset()
# Transmit data into the DUT's RX pin
uart_tx = UartSource(dut.UART_RX, baud=115200, bits=8, parity="none")
await uart_tx.write_bytes([0xDE, 0xAD, 0xBE, 0xEF])
# Capture data coming out of the DUT's TX pin
uart_rx = UartSink(dut.UART_TX, baud=115200, bits=8, parity="none")
cocotb.start_soon(uart_rx.start())
# Wait and read
for _ in range(4):
byte = await uart_rx.read(timeout_us=1000)
tb.log.info(f"Received: 0x{byte:02X}")
Error Injection
The UartSource includes methods for testing receiver error handling:
# Send a byte with a bad stop bit (framing error)
await uart_tx.inject_framing_error(0xAA)
# Send a byte with inverted parity (parity error)
await uart_tx.inject_parity_error(0x55)
# Send a UART break condition (line held low)
await uart_tx.send_break(duration_bits=12)
Configuration
| Parameter | Type | Default | Description |
|---|---|---|---|
baud | int | 115200 | Baud rate in bits/second |
bits | int | 8 | Data bits per frame (5–9) |
stop_bits | float | 1 | Stop bit duration (1 or 1.5 or 2) |
parity | str | "none" | Parity mode: "none", "even", or "odd" |
Classes
::: sim.cocotb.tb.drivers.uart_master.UartConfig options: show_root_heading: true
::: sim.cocotb.tb.drivers.uart_master.UartSource options: show_root_heading: true show_source: true members: - init - write - write_bytes - send_break - inject_framing_error - inject_parity_error
::: sim.cocotb.tb.drivers.uart_master.UartSink options: show_root_heading: true show_source: true members: - init - start - read - read_nowait - empty - count