litex/misoclib/com/uart/__init__.py

51 lines
1.9 KiB
Python
Raw Normal View History

2013-05-22 11:10:13 -04:00
from migen.fhdl.std import *
from migen.bank.description import *
from migen.bank.eventmanager import *
from migen.genlib.record import Record
2015-07-19 03:23:35 -04:00
from migen.actorlib.fifo import SyncFIFO, AsyncFIFO
2011-12-13 11:33:12 -05:00
2015-04-13 10:47:22 -04:00
class UART(Module, AutoCSR):
def __init__(self, phy,
tx_fifo_depth=16,
2015-07-19 03:23:35 -04:00
rx_fifo_depth=16,
phy_cd="sys"):
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()
# # #
2015-07-19 03:23:35 -04:00
if phy_cd == "sys":
tx_fifo = SyncFIFO([("data", 8)], tx_fifo_depth)
rx_fifo = SyncFIFO([("data", 8)], rx_fifo_depth)
# Generate TX IRQ when tx_fifo becomes empty
tx_irq = tx_fifo.source.stb
else:
tx_fifo = ClockDomainsRenamer({"write": "sys", "read": phy_cd})(
AsyncFIFO([("data", 8)], tx_fifo_depth))
rx_fifo = ClockDomainsRenamer({"write": phy_cd, "read": "sys"})(
AsyncFIFO([("data", 8)], rx_fifo_depth))
# Generate TX IRQ when tx_fifo becomes non-full
tx_irq = ~tx_fifo.sink.ack
self.submodules += tx_fifo, rx_fifo
self.comb += [
tx_fifo.sink.stb.eq(self._rxtx.re),
tx_fifo.sink.data.eq(self._rxtx.r),
self._txfull.status.eq(~tx_fifo.sink.ack),
2015-07-19 03:23:35 -04:00
Record.connect(tx_fifo.source, phy.sink),
self.ev.tx.trigger.eq(tx_irq),
Record.connect(phy.source, rx_fifo.sink),
self._rxempty.status.eq(~rx_fifo.source.stb),
self._rxtx.w.eq(rx_fifo.source.data),
2015-07-19 03:23:35 -04:00
rx_fifo.source.ack.eq(self.ev.rx.clear),
# Generate RX IRQ when rx_fifo becomes non-empty
self.ev.rx.trigger.eq(~rx_fifo.source.stb),
]