2011-12-16 15:30:22 -05:00
|
|
|
from migen.fhdl.structure import *
|
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-17 18:29:37 -05:00
|
|
|
from migen.bank import csrgen
|
2011-12-13 11:33:12 -05:00
|
|
|
|
2012-01-21 06:25:22 -05:00
|
|
|
class UART:
|
2011-12-17 18:29:37 -05:00
|
|
|
def __init__(self, address, clk_freq, baud=115200):
|
2012-02-06 11:45:31 -05:00
|
|
|
self._rxtx = RegisterRaw("rxtx", 8)
|
|
|
|
self._divisor = RegisterField("divisor", 16, reset=int(clk_freq/baud/16))
|
2011-12-17 18:29:37 -05:00
|
|
|
|
2012-02-06 11:45:31 -05:00
|
|
|
self._tx_event = EventSourceLevel()
|
2012-02-07 08:12:23 -05:00
|
|
|
self._rx_event = EventSourcePulse()
|
|
|
|
self.events = EventManager(self._tx_event, self._rx_event)
|
2012-02-06 11:45:31 -05:00
|
|
|
self.bank = csrgen.Bank([self._rxtx, self._divisor] + self.events.get_registers(),
|
|
|
|
address=address)
|
|
|
|
|
2011-12-18 16:02:05 -05:00
|
|
|
self.tx = Signal(reset=1)
|
|
|
|
self.rx = Signal()
|
2011-12-13 11:33:12 -05:00
|
|
|
|
2011-12-16 10:02:49 -05:00
|
|
|
def get_fragment(self):
|
2011-12-18 16:02:05 -05:00
|
|
|
enable16 = Signal()
|
|
|
|
enable16_counter = Signal(BV(16))
|
|
|
|
comb = [
|
2012-11-28 17:18:53 -05:00
|
|
|
enable16.eq(enable16_counter == 0)
|
2011-12-18 16:02:05 -05:00
|
|
|
]
|
|
|
|
sync = [
|
|
|
|
enable16_counter.eq(enable16_counter - 1),
|
|
|
|
If(enable16,
|
2012-02-06 11:45:31 -05:00
|
|
|
enable16_counter.eq(self._divisor.field.r - 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
|
2011-12-18 16:02:05 -05:00
|
|
|
tx_reg = Signal(BV(8))
|
|
|
|
tx_bitcount = Signal(BV(4))
|
|
|
|
tx_count16 = Signal(BV(4))
|
2012-02-07 08:12:23 -05:00
|
|
|
tx_busy = self._tx_event.trigger
|
2011-12-18 16:02:05 -05:00
|
|
|
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),
|
|
|
|
self.tx.eq(0)
|
|
|
|
).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,
|
|
|
|
self.tx.eq(1)
|
|
|
|
).Elif(tx_bitcount == 9,
|
|
|
|
self.tx.eq(1),
|
|
|
|
tx_busy.eq(0)
|
|
|
|
).Else(
|
|
|
|
self.tx.eq(tx_reg[0]),
|
|
|
|
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
|
|
|
|
rx0 = Signal() # sychronize
|
|
|
|
rx = Signal()
|
|
|
|
sync += [
|
|
|
|
rx0.eq(self.rx),
|
|
|
|
rx.eq(rx0)
|
|
|
|
]
|
|
|
|
rx_r = Signal()
|
|
|
|
rx_reg = Signal(BV(8))
|
|
|
|
rx_bitcount = Signal(BV(4))
|
|
|
|
rx_count16 = Signal(BV(4))
|
|
|
|
rx_busy = Signal()
|
|
|
|
rx_done = self._rx_event.trigger
|
|
|
|
rx_data = self._rxtx.w
|
|
|
|
sync += [
|
|
|
|
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))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
]
|
2011-12-18 16:02:05 -05:00
|
|
|
|
2012-02-06 11:45:31 -05:00
|
|
|
return self.bank.get_fragment() \
|
|
|
|
+ self.events.get_fragment() \
|
2012-04-02 13:22:17 -04:00
|
|
|
+ Fragment(comb, sync)
|