artiq/artiq/gateware/drtio/rt_controller_master.py

268 lines
9.2 KiB
Python
Raw Normal View History

"""Real-time controller for master"""
2016-10-24 19:50:13 +08:00
from migen import *
2016-10-31 00:53:01 +08:00
from migen.genlib.cdc import MultiReg
from migen.genlib.misc import WaitTimer
2016-12-12 18:48:10 +08:00
from migen.genlib.resetsync import AsyncResetSynchronizer
2016-10-24 19:50:13 +08:00
from misoc.interconnect.csr import *
2017-09-16 14:36:27 +08:00
from artiq.gateware.rtio.cdc import GrayCodeTransfer
2016-11-22 22:46:50 +08:00
from artiq.gateware.rtio import cri
2016-10-24 19:50:13 +08:00
2016-10-31 18:09:36 +08:00
class _CSRs(AutoCSR):
def __init__(self):
self.link_up = CSRStorage()
self.protocol_error = CSR(3)
2016-10-24 19:50:13 +08:00
self.set_time = CSR()
self.underflow_margin = CSRStorage(16, reset=300)
2016-10-24 19:50:13 +08:00
2017-09-24 12:23:47 +08:00
self.o_get_buffer_space = CSR()
self.o_dbg_buffer_space = CSRStatus(16)
self.o_dbg_buffer_space_req_cnt = CSRStatus(32)
2016-10-31 18:09:36 +08:00
self.o_wait = CSRStatus()
2016-10-24 19:50:13 +08:00
2017-09-16 14:13:42 +08:00
class RTIOCounter(Module):
def __init__(self, width):
self.width = width
# Timestamp counter in RTIO domain
self.value_rtio = Signal(width)
# Timestamp counter resynchronized to sys domain
# Lags behind value_rtio, monotonic and glitch-free
self.value_sys = Signal(width)
# # #
# note: counter is in rtio domain and never affected by the reset CSRs
self.sync.rtio += self.value_rtio.eq(self.value_rtio + 1)
gt = GrayCodeTransfer(width)
self.submodules += gt
self.comb += gt.i.eq(self.value_rtio), self.value_sys.eq(gt.o)
2016-10-24 19:50:13 +08:00
class RTController(Module):
2017-03-13 00:10:07 +08:00
def __init__(self, rt_packet, channel_count, fine_ts_width):
2016-10-31 18:09:36 +08:00
self.csrs = _CSRs()
2016-11-22 22:46:50 +08:00
self.cri = cri.Interface()
2016-10-31 18:09:36 +08:00
# reset
local_reset = Signal(reset=1)
self.sync += local_reset.eq(~self.csrs.link_up.storage)
local_reset.attr.add("no_retiming")
self.clock_domains.cd_sys_with_rst = ClockDomain()
self.clock_domains.cd_rtio_with_rst = ClockDomain()
self.comb += [
self.cd_sys_with_rst.clk.eq(ClockSignal()),
self.cd_sys_with_rst.rst.eq(local_reset)
]
self.comb += self.cd_rtio_with_rst.clk.eq(ClockSignal("rtio"))
self.specials += AsyncResetSynchronizer(self.cd_rtio_with_rst, local_reset)
# protocol errors
err_unknown_packet_type = Signal()
err_packet_truncated = Signal()
2017-09-24 12:23:47 +08:00
signal_buffer_space_timeout = Signal()
err_buffer_space_timeout = Signal()
self.sync.sys_with_rst += [
If(self.csrs.protocol_error.re,
If(self.csrs.protocol_error.r[0], err_unknown_packet_type.eq(0)),
If(self.csrs.protocol_error.r[1], err_packet_truncated.eq(0)),
2017-09-24 12:23:47 +08:00
If(self.csrs.protocol_error.r[2], err_buffer_space_timeout.eq(0))
),
If(rt_packet.err_unknown_packet_type, err_unknown_packet_type.eq(1)),
If(rt_packet.err_packet_truncated, err_packet_truncated.eq(1)),
2017-09-24 12:23:47 +08:00
If(signal_buffer_space_timeout, err_buffer_space_timeout.eq(1))
]
self.comb += self.csrs.protocol_error.w.eq(
2017-09-24 12:23:47 +08:00
Cat(err_unknown_packet_type, err_packet_truncated, err_buffer_space_timeout))
2016-10-24 19:50:13 +08:00
# master RTIO counter and counter synchronization
2016-10-26 11:48:47 +08:00
self.submodules.counter = RTIOCounter(64-fine_ts_width)
self.comb += [
2018-03-09 10:36:17 +08:00
self.cri.counter.eq(self.counter.value_sys << fine_ts_width),
rt_packet.tsc_value.eq(self.counter.value_rtio),
2017-03-13 00:10:07 +08:00
self.csrs.set_time.w.eq(rt_packet.set_time_stb)
2016-10-24 19:50:13 +08:00
]
self.sync += [
2017-03-13 00:10:07 +08:00
If(rt_packet.set_time_ack, rt_packet.set_time_stb.eq(0)),
If(self.csrs.set_time.re, rt_packet.set_time_stb.eq(1))
2016-10-24 19:50:13 +08:00
]
# common packet fields
2017-09-24 12:23:47 +08:00
chan_sel = self.cri.chan_sel[:16]
rt_packet_buffer_request = Signal()
2017-03-13 23:54:44 +08:00
rt_packet_read_request = Signal()
2016-10-24 19:50:13 +08:00
self.comb += [
2017-03-13 00:10:07 +08:00
rt_packet.sr_channel.eq(chan_sel),
rt_packet.sr_address.eq(self.cri.o_address),
rt_packet.sr_data.eq(self.cri.o_data),
rt_packet.sr_timestamp.eq(self.cri.timestamp),
2017-09-24 12:23:47 +08:00
If(rt_packet_buffer_request,
2017-03-13 00:10:07 +08:00
rt_packet.sr_notwrite.eq(1),
rt_packet.sr_address.eq(0)
2017-03-13 23:54:44 +08:00
),
If(rt_packet_read_request,
rt_packet.sr_notwrite.eq(1),
rt_packet.sr_address.eq(1)
2016-10-24 19:50:13 +08:00
)
]
2017-03-13 23:54:44 +08:00
# output status
o_status_wait = Signal()
o_status_underflow = Signal()
2016-10-31 18:09:36 +08:00
self.comb += [
2016-11-22 22:46:50 +08:00
self.cri.o_status.eq(Cat(
2018-03-04 23:20:13 +08:00
o_status_wait, o_status_underflow, ~self.csrs.link_up.storage)),
2017-03-13 23:54:44 +08:00
self.csrs.o_wait.status.eq(o_status_wait)
2016-10-31 18:09:36 +08:00
]
2017-03-13 23:54:44 +08:00
o_underflow_set = Signal()
2016-12-13 14:19:49 +08:00
self.sync.sys_with_rst += [
If(self.cri.cmd == cri.commands["write"],
2017-09-24 12:23:47 +08:00
o_status_underflow.eq(0)
),
2017-09-24 12:23:47 +08:00
If(o_underflow_set, o_status_underflow.eq(1))
]
2016-10-24 19:50:13 +08:00
timeout_counter = WaitTimer(8191)
self.submodules += timeout_counter
2017-09-24 12:23:47 +08:00
cond_underflow = Signal()
self.comb += cond_underflow.eq((self.cri.timestamp[fine_ts_width:]
2016-10-31 18:09:36 +08:00
- self.csrs.underflow_margin.storage[fine_ts_width:]) < self.counter.value_sys)
2016-10-24 19:50:13 +08:00
2017-09-24 12:23:47 +08:00
buffer_space = Signal(16)
2017-03-13 23:54:44 +08:00
# input status
i_status_wait_event = Signal()
i_status_overflow = Signal()
i_status_wait_status = Signal()
self.comb += self.cri.i_status.eq(Cat(
2018-03-04 23:20:13 +08:00
i_status_wait_event, i_status_overflow, i_status_wait_status,
~self.csrs.link_up.storage))
2017-03-13 23:54:44 +08:00
load_read_reply = Signal()
self.sync.sys_with_rst += [
If(load_read_reply,
i_status_wait_event.eq(0),
i_status_overflow.eq(0),
If(rt_packet.read_no_event,
If(rt_packet.read_is_overflow,
i_status_overflow.eq(1)
).Else(
i_status_wait_event.eq(1)
)
),
self.cri.i_data.eq(rt_packet.read_data),
self.cri.i_timestamp.eq(rt_packet.read_timestamp)
)
]
# FSM
fsm = ClockDomainsRenamer("sys_with_rst")(FSM())
self.submodules += fsm
2016-10-24 19:50:13 +08:00
fsm.act("IDLE",
2016-11-22 22:46:50 +08:00
If(self.cri.cmd == cri.commands["write"],
2017-09-24 12:23:47 +08:00
If(cond_underflow,
2017-03-13 23:54:44 +08:00
o_underflow_set.eq(1)
2016-10-24 19:50:13 +08:00
).Else(
NextState("WRITE")
)
),
If(self.cri.cmd == cri.commands["read"], NextState("READ")),
2017-09-24 12:23:47 +08:00
If(self.csrs.o_get_buffer_space.re, NextState("GET_BUFFER_SPACE"))
2016-10-24 19:50:13 +08:00
)
fsm.act("WRITE",
2017-03-13 23:54:44 +08:00
o_status_wait.eq(1),
2017-03-13 00:10:07 +08:00
rt_packet.sr_stb.eq(1),
If(rt_packet.sr_ack,
2017-09-24 12:23:47 +08:00
NextValue(buffer_space, buffer_space - 1),
If(buffer_space <= 1,
NextState("GET_BUFFER_SPACE")
2016-10-24 19:50:13 +08:00
).Else(
NextState("IDLE")
)
)
)
2017-09-24 12:23:47 +08:00
fsm.act("GET_BUFFER_SPACE",
2017-03-13 23:54:44 +08:00
o_status_wait.eq(1),
2017-09-24 12:23:47 +08:00
rt_packet.buffer_space_not_ack.eq(1),
rt_packet_buffer_request.eq(1),
2017-03-13 00:10:07 +08:00
rt_packet.sr_stb.eq(1),
If(rt_packet.sr_ack,
2017-09-24 12:23:47 +08:00
NextState("GET_BUFFER_SPACE_REPLY")
2016-10-24 19:50:13 +08:00
)
)
2017-09-24 12:23:47 +08:00
fsm.act("GET_BUFFER_SPACE_REPLY",
2017-03-13 23:54:44 +08:00
o_status_wait.eq(1),
2017-09-24 12:23:47 +08:00
NextValue(buffer_space, rt_packet.buffer_space),
rt_packet.buffer_space_not_ack.eq(1),
If(rt_packet.buffer_space_not,
If(rt_packet.buffer_space != 0,
2016-10-24 19:50:13 +08:00
NextState("IDLE")
).Else(
2017-09-24 12:23:47 +08:00
NextState("GET_BUFFER_SPACE")
2016-10-24 19:50:13 +08:00
)
),
timeout_counter.wait.eq(1),
If(timeout_counter.done,
2017-09-24 12:23:47 +08:00
signal_buffer_space_timeout.eq(1),
NextState("GET_BUFFER_SPACE")
2016-10-24 19:50:13 +08:00
)
)
2017-03-13 23:54:44 +08:00
fsm.act("READ",
i_status_wait_status.eq(1),
2017-03-14 14:14:43 +08:00
rt_packet.read_not_ack.eq(1),
2017-03-13 23:54:44 +08:00
rt_packet_read_request.eq(1),
rt_packet.sr_stb.eq(1),
If(rt_packet.sr_ack,
NextState("GET_READ_REPLY")
)
)
fsm.act("GET_READ_REPLY",
i_status_wait_status.eq(1),
2017-03-14 14:14:43 +08:00
rt_packet.read_not_ack.eq(1),
2017-03-13 23:54:44 +08:00
If(rt_packet.read_not,
load_read_reply.eq(1),
NextState("IDLE")
)
)
2016-10-24 19:50:13 +08:00
2017-09-24 12:23:47 +08:00
# debug CSRs
self.comb += self.csrs.o_dbg_buffer_space.status.eq(buffer_space),
self.sync += \
2017-09-24 12:23:47 +08:00
If((rt_packet.sr_stb & rt_packet.sr_ack & rt_packet_buffer_request),
self.csrs.o_dbg_buffer_space_req_cnt.status.eq(
self.csrs.o_dbg_buffer_space_req_cnt.status + 1)
)
2016-10-24 19:50:13 +08:00
2016-10-31 18:09:36 +08:00
def get_csrs(self):
return self.csrs.get_csrs()
class RTManager(Module, AutoCSR):
2017-03-13 00:10:07 +08:00
def __init__(self, rt_packet):
self.request_echo = CSR()
self.update_packet_cnt = CSR()
self.packet_cnt_tx = CSRStatus(32)
self.packet_cnt_rx = CSRStatus(32)
# # #
2017-03-13 00:10:07 +08:00
self.comb += self.request_echo.w.eq(rt_packet.echo_stb)
self.sync += [
2017-03-13 00:10:07 +08:00
If(rt_packet.echo_ack, rt_packet.echo_stb.eq(0)),
If(self.request_echo.re, rt_packet.echo_stb.eq(1))
]
self.sync += \
If(self.update_packet_cnt.re,
2017-03-13 00:10:07 +08:00
self.packet_cnt_tx.status.eq(rt_packet.packet_cnt_tx),
self.packet_cnt_rx.status.eq(rt_packet.packet_cnt_rx)
)