from litex.soc.interconnect import stream
from litex.soc.interconnect.wishbonebridge import WishboneStreamingBridge
+# Common -------------------------------------------------------------------------------------------
+
+def UARTPads():
+ return Record([("tx", 1), ("rx", 1)])
+
class UARTInterface:
def __init__(self):
self.sink = stream.Endpoint([("data", 8)])
self.source = stream.Endpoint([("data", 8)])
+ def connect(self, other):
+ return [
+ other.source.connect(self.sink),
+ self.source.connect(other.sink)
+ ]
+
# RS232 PHY ----------------------------------------------------------------------------------------
class RS232PHYInterface(UARTInterface):
else:
return RS232PHY(pads, clk_freq, baudrate)
-class UART(Module, AutoCSR):
- def __init__(self, phy,
+class UART(Module, AutoCSR, UARTInterface):
+ def __init__(self, phy=None,
tx_fifo_depth=16,
rx_fifo_depth=16,
phy_cd="sys"):
# # #
+ UARTInterface.__init__(self)
+
+ # PHY
+ if phy is not None:
+ self.comb += [
+ phy.source.connect(self.sink),
+ self.source.connect(phy.sink)
+ ]
+
# TX
tx_fifo = _get_uart_fifo(tx_fifo_depth, source_cd=phy_cd)
self.submodules += tx_fifo
tx_fifo.sink.valid.eq(self._rxtx.re),
tx_fifo.sink.data.eq(self._rxtx.r),
self._txfull.status.eq(~tx_fifo.sink.ready),
- tx_fifo.source.connect(phy.sink),
+ tx_fifo.source.connect(self.source),
# Generate TX IRQ when tx_fifo becomes non-full
self.ev.tx.trigger.eq(~tx_fifo.sink.ready)
]
self.submodules += rx_fifo
self.comb += [
- phy.source.connect(rx_fifo.sink),
+ self.sink.connect(rx_fifo.sink),
self._rxempty.status.eq(~rx_fifo.source.valid),
self._rxtx.w.eq(rx_fifo.source.data),
rx_fifo.source.ready.eq(self.ev.rx.clear),
self.ev.rx.trigger.eq(~rx_fifo.source.valid)
]
-
-class UARTStub(Module, AutoCSR):
- def __init__(self):
- self._rxtx = CSR(8)
- self._txfull = CSRStatus()
- self._rxempty = CSRStatus()
-
- self.submodules.ev = EventManager()
- self.ev.tx = EventSourceProcess()
- self.ev.rx = EventSourceProcess()
- self.ev.finalize()
-
- # # #
-
- self.comb += [
- self._txfull.status.eq(0),
- self.ev.tx.trigger.eq(~(self._rxtx.re & self._rxtx.r)),
- self._rxempty.status.eq(1)
- ]
-
-
class UARTWishboneBridge(WishboneStreamingBridge):
def __init__(self, pads, clk_freq, baudrate=115200):
self.submodules.phy = RS232PHY(pads, clk_freq, baudrate)
WishboneStreamingBridge.__init__(self, self.phy, clk_freq)
-# UART Mutltiplexer --------------------------------------------------------------------------------
-
-def UARTPads():
- return Record([("tx", 1), ("rx", 1)])
-
+# UART Multiplexer ---------------------------------------------------------------------------------
class UARTMultiplexer(Module):
def __init__(self, uarts, uart):
uarts[n].rx.eq(uart.rx)
]
self.comb += Case(self.sel, cases)
-
-# UART Emulator ------------------------------------------------------------------------------------
-
-class UARTEmulator(UART):
- """
- UART emulation over Wishbone bridge.
-
- Creates a fully compatible UART that can be used by the CPU as a regular UART and adds a second
- UART, cross-connected to the main one to allow terminal emulation over a Wishbone bridge.
- """
- def __init__(self, **kwargs):
- uart_phy = UARTInterface()
- emul_phy = UARTInterface()
- UART.__init__(self, uart_phy, **kwargs)
- self.submodules.emul = UART(emul_phy)
- self.comb += [
- uart_phy.source.connect(emul_phy.sink),
- emul_phy.source.connect(uart_phy.sink)
- ]