2011-12-16 15:30:22 -05:00
|
|
|
from migen.fhdl.structure import *
|
2013-03-10 14:32:38 -04:00
|
|
|
from migen.fhdl.module import Module
|
|
|
|
from migen.genlib.cdc import MultiReg
|
2011-12-17 18:29:37 -05:00
|
|
|
from migen.bank.description import *
|
2012-02-06 11:45:31 -05:00
|
|
|
from migen.bank.eventmanager import *
|
2011-12-13 11:33:12 -05:00
|
|
|
|
2013-03-30 12:28:15 -04:00
|
|
|
class UART(Module, AutoCSR):
|
2013-03-26 12:57:17 -04:00
|
|
|
def __init__(self, pads, clk_freq, baud=115200):
|
2013-03-30 12:28:15 -04:00
|
|
|
self._rxtx = CSR(8)
|
|
|
|
self._divisor = CSRStorage(16, reset=int(clk_freq/baud/16))
|
2011-12-17 18:29:37 -05:00
|
|
|
|
2013-03-10 14:32:38 -04:00
|
|
|
self.submodules.ev = EventManager()
|
|
|
|
self.ev.tx = EventSourceLevel()
|
|
|
|
self.ev.rx = EventSourcePulse()
|
|
|
|
self.ev.finalize()
|
2011-12-13 11:33:12 -05:00
|
|
|
|
2013-03-10 14:32:38 -04:00
|
|
|
###
|
|
|
|
|
2013-03-26 12:57:17 -04:00
|
|
|
pads.tx.reset = 1
|
|
|
|
|
2011-12-18 16:02:05 -05:00
|
|
|
enable16 = Signal()
|
2012-11-29 17:38:04 -05:00
|
|
|
enable16_counter = Signal(16)
|
2013-03-10 14:32:38 -04:00
|
|
|
self.comb += enable16.eq(enable16_counter == 0)
|
|
|
|
self.sync += [
|
2011-12-18 16:02:05 -05:00
|
|
|
enable16_counter.eq(enable16_counter - 1),
|
|
|
|
If(enable16,
|
2013-03-30 12:28:15 -04:00
|
|
|
enable16_counter.eq(self._divisor.storage - 1))
|
2011-12-18 16:02:05 -05:00
|
|
|
]
|
2011-12-17 18:29:37 -05:00
|
|
|
|
2012-02-07 08:12:23 -05:00
|
|
|
# TX
|
2012-11-29 17:38:04 -05:00
|
|
|
tx_reg = Signal(8)
|
|
|
|
tx_bitcount = Signal(4)
|
|
|
|
tx_count16 = Signal(4)
|
2013-03-10 14:32:38 -04:00
|
|
|
tx_busy = self.ev.tx.trigger
|
|
|
|
self.sync += [
|
2012-02-06 11:45:31 -05:00
|
|
|
If(self._rxtx.re,
|
|
|
|
tx_reg.eq(self._rxtx.r),
|
2011-12-18 16:02:05 -05:00
|
|
|
tx_bitcount.eq(0),
|
|
|
|
tx_count16.eq(1),
|
|
|
|
tx_busy.eq(1),
|
2013-03-26 12:57:17 -04:00
|
|
|
pads.tx.eq(0)
|
2011-12-18 16:02:05 -05:00
|
|
|
).Elif(enable16 & tx_busy,
|
|
|
|
tx_count16.eq(tx_count16 + 1),
|
2012-11-28 17:18:53 -05:00
|
|
|
If(tx_count16 == 0,
|
2011-12-18 16:02:05 -05:00
|
|
|
tx_bitcount.eq(tx_bitcount + 1),
|
|
|
|
If(tx_bitcount == 8,
|
2013-03-26 12:57:17 -04:00
|
|
|
pads.tx.eq(1)
|
2011-12-18 16:02:05 -05:00
|
|
|
).Elif(tx_bitcount == 9,
|
2013-03-26 12:57:17 -04:00
|
|
|
pads.tx.eq(1),
|
2011-12-18 16:02:05 -05:00
|
|
|
tx_busy.eq(0)
|
|
|
|
).Else(
|
2013-03-26 12:57:17 -04:00
|
|
|
pads.tx.eq(tx_reg[0]),
|
2011-12-18 16:02:05 -05:00
|
|
|
tx_reg.eq(Cat(tx_reg[1:], 0))
|
|
|
|
)
|
2011-12-17 18:29:37 -05:00
|
|
|
)
|
|
|
|
)
|
2011-12-18 16:02:05 -05:00
|
|
|
]
|
2012-02-07 08:12:23 -05:00
|
|
|
|
|
|
|
# RX
|
|
|
|
rx = Signal()
|
2013-03-26 12:57:17 -04:00
|
|
|
self.specials += MultiReg(pads.rx, rx)
|
2012-02-07 08:12:23 -05:00
|
|
|
rx_r = Signal()
|
2012-11-29 17:38:04 -05:00
|
|
|
rx_reg = Signal(8)
|
|
|
|
rx_bitcount = Signal(4)
|
|
|
|
rx_count16 = Signal(4)
|
2012-02-07 08:12:23 -05:00
|
|
|
rx_busy = Signal()
|
2013-03-10 14:32:38 -04:00
|
|
|
rx_done = self.ev.rx.trigger
|
2012-02-07 08:12:23 -05:00
|
|
|
rx_data = self._rxtx.w
|
2013-03-10 14:32:38 -04:00
|
|
|
self.sync += [
|
2012-02-07 08:12:23 -05:00
|
|
|
rx_done.eq(0),
|
|
|
|
If(enable16,
|
|
|
|
rx_r.eq(rx),
|
|
|
|
If(~rx_busy,
|
|
|
|
If(~rx & rx_r, # look for start bit
|
|
|
|
rx_busy.eq(1),
|
|
|
|
rx_count16.eq(7),
|
|
|
|
rx_bitcount.eq(0)
|
|
|
|
)
|
|
|
|
).Else(
|
|
|
|
rx_count16.eq(rx_count16 + 1),
|
|
|
|
If(rx_count16 == 0,
|
|
|
|
rx_bitcount.eq(rx_bitcount + 1),
|
|
|
|
|
|
|
|
If(rx_bitcount == 0,
|
|
|
|
If(rx, # verify start bit
|
|
|
|
rx_busy.eq(0)
|
|
|
|
)
|
|
|
|
).Elif(rx_bitcount == 9,
|
|
|
|
rx_busy.eq(0),
|
|
|
|
If(rx, # verify stop bit
|
|
|
|
rx_data.eq(rx_reg),
|
|
|
|
rx_done.eq(1)
|
|
|
|
)
|
|
|
|
).Else(
|
|
|
|
rx_reg.eq(Cat(rx_reg[1:], rx))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
]
|