2013-03-18 16:45:07 -04:00
|
|
|
from migen.fhdl.structure import *
|
2013-03-18 18:03:52 -04:00
|
|
|
from migen.fhdl.module import *
|
2013-03-18 16:45:07 -04:00
|
|
|
from migen.bus import csr
|
2013-03-18 18:03:52 -04:00
|
|
|
from migen.genlib.fsm import *
|
2013-03-18 16:45:07 -04:00
|
|
|
|
|
|
|
from miscope.bridges.uart2csr.uart import *
|
|
|
|
|
|
|
|
WRITE_CMD = 0x01
|
|
|
|
READ_CMD = 0x02
|
|
|
|
CLOSE_CMD = 0x03
|
|
|
|
|
|
|
|
class Uart2Csr(Module):
|
|
|
|
def __init__(self, clk_freq, baud):
|
|
|
|
# Uart interface
|
|
|
|
self.rx = Signal()
|
|
|
|
self.tx = Signal()
|
|
|
|
|
|
|
|
# Csr interface
|
|
|
|
self.csr = csr.Interface(32, 8)
|
|
|
|
|
|
|
|
###
|
|
|
|
|
2013-03-18 18:03:52 -04:00
|
|
|
self.submodules.uart = UART(clk_freq, baud)
|
|
|
|
uart = self.uart
|
2013-03-18 16:45:07 -04:00
|
|
|
|
|
|
|
#
|
|
|
|
# In/Out
|
|
|
|
#
|
|
|
|
self.comb +=[
|
|
|
|
uart.rx.eq(self.rx),
|
|
|
|
self.tx.eq(uart.tx)
|
|
|
|
]
|
|
|
|
|
|
|
|
cmd = Signal(8)
|
|
|
|
cnt = Signal(3)
|
|
|
|
sr = Signal(32)
|
|
|
|
burst_cnt = Signal(8)
|
|
|
|
addr = Signal(32)
|
|
|
|
data = Signal(8)
|
|
|
|
|
2013-03-18 18:03:52 -04:00
|
|
|
|
|
|
|
# FSM
|
|
|
|
self.submodules.fsm = FSM("IDLE",
|
|
|
|
"GET_BL", "GET_ADDR",
|
|
|
|
"GET_DATA", "WRITE_CSR",
|
|
|
|
"READ_CSR", "SEND_DATA")
|
|
|
|
|
|
|
|
fsm = self.fsm
|
2013-03-18 16:45:07 -04:00
|
|
|
#
|
|
|
|
# Global
|
|
|
|
#
|
|
|
|
self.sync +=[
|
|
|
|
If(fsm.ongoing(fsm.IDLE), cnt.eq(0)
|
2013-03-18 18:03:52 -04:00
|
|
|
).Elif(uart.rx_ev, cnt.eq(cnt + 1)),
|
2013-03-18 16:45:07 -04:00
|
|
|
|
|
|
|
sr.eq(Cat(uart.rx_dat, sr[0:24]))
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
# State done
|
|
|
|
get_bl_done = Signal()
|
|
|
|
get_addr_done = Signal()
|
|
|
|
get_data_done = Signal()
|
|
|
|
send_data_done = Signal()
|
|
|
|
|
|
|
|
#
|
|
|
|
# Idle
|
|
|
|
#
|
|
|
|
fsm.act(fsm.IDLE,
|
2013-03-18 18:03:52 -04:00
|
|
|
If(uart.rx_ev & ((uart.rx_dat == WRITE_CMD) | (uart.rx_dat == READ_CMD)),
|
2013-03-18 16:45:07 -04:00
|
|
|
fsm.next_state(fsm.GET_BL)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
self.sync +=[
|
2013-03-18 18:03:52 -04:00
|
|
|
If(fsm.ongoing(fsm.IDLE) & uart.rx_ev,
|
2013-03-18 16:45:07 -04:00
|
|
|
cmd.eq(uart.rx_dat)
|
|
|
|
)
|
|
|
|
|
|
|
|
]
|
|
|
|
|
|
|
|
#
|
|
|
|
# Get burst length
|
|
|
|
#
|
|
|
|
fsm.act(fsm.GET_BL,
|
|
|
|
If(get_bl_done,
|
|
|
|
fsm.next_state(fsm.GET_ADDR)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2013-03-18 18:03:52 -04:00
|
|
|
self.comb += get_bl_done.eq(uart.rx_ev & fsm.ongoing(fsm.GET_BL))
|
2013-03-18 16:45:07 -04:00
|
|
|
|
|
|
|
self.sync +=[
|
|
|
|
If(get_bl_done,
|
|
|
|
burst_cnt.eq(uart.rx_dat)
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|
|
|
|
#
|
|
|
|
# Get address
|
|
|
|
#
|
|
|
|
fsm.act(fsm.GET_ADDR,
|
2013-03-18 18:03:52 -04:00
|
|
|
If(get_addr_done & (cmd == WRITE_CMD),
|
2013-03-18 16:45:07 -04:00
|
|
|
fsm.next_state(fsm.GET_DATA)
|
2013-03-18 18:03:52 -04:00
|
|
|
).Elif(get_addr_done & (cmd == READ_CMD),
|
2013-03-18 16:45:07 -04:00
|
|
|
fsm.next_state(fsm.READ_CSR)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2013-03-18 18:03:52 -04:00
|
|
|
self.comb += get_addr_done.eq(uart.rx_ev & (cnt == 4) & fsm.ongoing(fsm.GET_ADDR))
|
2013-03-18 16:45:07 -04:00
|
|
|
|
|
|
|
self.sync +=[
|
|
|
|
If(get_addr_done,
|
|
|
|
addr.eq(sr)
|
2013-03-18 18:03:52 -04:00
|
|
|
).Elif(get_data_done | send_data_done,
|
2013-03-18 16:45:07 -04:00
|
|
|
addr.eq(addr + 4)
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|
|
|
|
#
|
|
|
|
# Get data
|
|
|
|
#
|
|
|
|
fsm.act(fsm.GET_DATA,
|
|
|
|
If(get_data_done,
|
|
|
|
fsm.next_state(fsm.IDLE)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2013-03-18 18:03:52 -04:00
|
|
|
self.comb += get_data_done.eq(uart.rx_ev & fsm.ongoing(fsm.GET_DATA))
|
2013-03-18 16:45:07 -04:00
|
|
|
|
|
|
|
self.sync +=[
|
|
|
|
If(get_data_done,
|
|
|
|
burst_cnt.eq(burst_cnt-1),
|
|
|
|
data.eq(uart.rx_dat)
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|
|
|
|
#
|
|
|
|
# Write Csr
|
|
|
|
#
|
|
|
|
fsm.act(fsm.WRITE_CSR,
|
|
|
|
If((not burst_cnt),
|
|
|
|
fsm.next_state(fsm.IDLE)
|
|
|
|
).Else(fsm.next_state(fsm.GET_DATA))
|
|
|
|
)
|
|
|
|
|
|
|
|
#
|
|
|
|
# Read Csr
|
|
|
|
#
|
|
|
|
fsm.act(fsm.READ_CSR,
|
|
|
|
fsm.next_state(fsm.SEND_DATA)
|
|
|
|
)
|
|
|
|
|
|
|
|
#
|
|
|
|
# Send Data
|
|
|
|
#
|
|
|
|
fsm.act(fsm.SEND_DATA,
|
2013-03-18 18:03:52 -04:00
|
|
|
If(send_data_done & (not burst_cnt),
|
2013-03-18 16:45:07 -04:00
|
|
|
fsm.next_state(fsm.IDLE)
|
|
|
|
).Elif(send_data_done,
|
|
|
|
fsm.next_state(fsm.READ_CSR)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
self.comb += [
|
2013-03-18 18:03:52 -04:00
|
|
|
uart.tx_dat.eq(self.csr.dat_r),
|
|
|
|
uart.tx_we.eq(fsm.entering(fsm.SEND_DATA)),
|
|
|
|
send_data_done.eq(~uart.tx_we | uart.tx_ev)
|
2013-03-18 16:45:07 -04:00
|
|
|
]
|
|
|
|
|
|
|
|
#
|
|
|
|
# Csr access
|
|
|
|
#
|
|
|
|
self.sync +=[
|
|
|
|
self.csr.adr.eq(addr),
|
|
|
|
self.csr.dat_w.eq(data),
|
2013-03-18 18:03:52 -04:00
|
|
|
If(fsm.ongoing(fsm.WRITE_CSR),
|
2013-03-18 16:45:07 -04:00
|
|
|
self.csr.we.eq(1)
|
|
|
|
).Else(
|
|
|
|
self.csr.we.eq(0)
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|
|
|
|
|