forked from M-Labs/artiq
1
0
Fork 0
artiq/artiq/dashboard/moninj.py

798 lines
29 KiB
Python
Raw Normal View History

import asyncio
import logging
import textwrap
from collections import namedtuple
from PyQt5 import QtCore, QtWidgets, QtGui
2019-11-10 15:55:17 +08:00
from sipyco.sync_struct import Subscriber
from artiq.coredevice.comm_moninj import *
from artiq.coredevice.ad9910 import (
_AD9910_REG_PROFILE0, _AD9910_REG_PROFILE7,
_AD9910_REG_FTW, _AD9910_REG_CFR1
)
from artiq.coredevice.ad9912_reg import AD9912_POW1, AD9912_SER_CONF
from artiq.gui.tools import LayoutWidget
2016-03-17 18:18:31 +08:00
from artiq.gui.flowlayout import FlowLayout
logger = logging.getLogger(__name__)
class _CancellableLineEdit(QtWidgets.QLineEdit):
def escapePressedConnect(self, cb):
self.esc_cb = cb
def keyPressEvent(self, event):
key = event.key()
if key == QtCore.Qt.Key_Escape:
self.esc_cb(event)
QtWidgets.QLineEdit.keyPressEvent(self, event)
2016-11-04 16:20:46 +08:00
class _TTLWidget(QtWidgets.QFrame):
def __init__(self, dm, channel, force_out, title):
QtWidgets.QFrame.__init__(self)
2015-06-06 00:03:30 +08:00
self.channel = channel
self.set_mode = dm.ttl_set_mode
2015-06-05 19:11:41 +08:00
self.force_out = force_out
2016-11-04 16:20:46 +08:00
self.setFrameShape(QtWidgets.QFrame.Box)
self.setFrameShadow(QtWidgets.QFrame.Raised)
2015-06-05 19:11:41 +08:00
2016-02-15 07:23:47 +08:00
grid = QtWidgets.QGridLayout()
2016-11-04 16:20:46 +08:00
grid.setContentsMargins(0, 0, 0, 0)
grid.setHorizontalSpacing(0)
grid.setVerticalSpacing(0)
2015-06-05 19:11:41 +08:00
self.setLayout(grid)
2016-02-15 07:23:47 +08:00
label = QtWidgets.QLabel(title)
2015-06-05 19:11:41 +08:00
label.setAlignment(QtCore.Qt.AlignCenter)
2016-11-04 16:20:46 +08:00
label.setSizePolicy(QtWidgets.QSizePolicy.Ignored,
QtWidgets.QSizePolicy.Preferred)
2015-06-05 19:11:41 +08:00
grid.addWidget(label, 1, 1)
self.stack = QtWidgets.QStackedWidget()
grid.addWidget(self.stack, 2, 1)
self.direction = QtWidgets.QLabel()
self.direction.setAlignment(QtCore.Qt.AlignCenter)
self.stack.addWidget(self.direction)
grid_cb = LayoutWidget()
2016-11-04 16:20:46 +08:00
grid_cb.layout.setContentsMargins(0, 0, 0, 0)
grid_cb.layout.setHorizontalSpacing(0)
grid_cb.layout.setVerticalSpacing(0)
self.override = QtWidgets.QToolButton()
self.override.setText("OVR")
self.override.setCheckable(True)
self.override.setToolTip("Override")
grid_cb.addWidget(self.override, 3, 1)
2016-11-04 16:20:46 +08:00
self.level = QtWidgets.QToolButton()
self.level.setText("LVL")
self.level.setCheckable(True)
self.level.setToolTip("Level")
grid_cb.addWidget(self.level, 3, 2)
self.stack.addWidget(grid_cb)
self.value = QtWidgets.QLabel()
self.value.setAlignment(QtCore.Qt.AlignCenter)
grid.addWidget(self.value, 3, 1)
2015-06-06 00:03:30 +08:00
2015-09-02 04:25:20 +08:00
grid.setRowStretch(1, 1)
grid.setRowStretch(2, 0)
grid.setRowStretch(3, 0)
grid.setRowStretch(4, 1)
self.programmatic_change = False
2016-11-04 16:20:46 +08:00
self.override.clicked.connect(self.override_toggled)
self.level.clicked.connect(self.level_toggled)
2015-09-02 04:25:20 +08:00
2017-02-27 11:45:04 +08:00
self.cur_level = False
2017-02-27 00:59:31 +08:00
self.cur_oe = False
self.cur_override = False
self.cur_override_level = False
2017-02-27 00:59:31 +08:00
self.refresh_display()
2015-06-06 00:03:30 +08:00
def enterEvent(self, event):
self.stack.setCurrentIndex(1)
2016-11-04 16:20:46 +08:00
QtWidgets.QFrame.enterEvent(self, event)
def leaveEvent(self, event):
if not self.override.isChecked():
self.stack.setCurrentIndex(0)
2016-11-04 16:20:46 +08:00
QtWidgets.QFrame.leaveEvent(self, event)
def override_toggled(self, override):
if self.programmatic_change:
return
if override:
if self.level.isChecked():
2017-02-27 00:59:31 +08:00
self.set_mode(self.channel, "1")
else:
2017-02-27 00:59:31 +08:00
self.set_mode(self.channel, "0")
else:
2017-02-27 00:59:31 +08:00
self.set_mode(self.channel, "exp")
def level_toggled(self, level):
if self.programmatic_change:
return
if self.override.isChecked():
if level:
2017-02-27 00:59:31 +08:00
self.set_mode(self.channel, "1")
else:
2017-02-27 00:59:31 +08:00
self.set_mode(self.channel, "0")
2017-02-27 00:59:31 +08:00
def refresh_display(self):
level = self.cur_override_level if self.cur_override else self.cur_level
value_s = "1" if level else "0"
2017-02-27 00:59:31 +08:00
if self.cur_override:
2015-06-06 00:03:30 +08:00
value_s = "<b>" + value_s + "</b>"
2015-06-05 19:11:41 +08:00
color = " color=\"red\""
else:
color = ""
2016-11-04 16:20:46 +08:00
self.value.setText("<font size=\"5\"{}>{}</font>".format(
2015-06-06 00:03:30 +08:00
color, value_s))
2017-02-27 00:59:31 +08:00
oe = self.cur_oe or self.force_out
2015-06-05 19:11:41 +08:00
direction = "OUT" if oe else "IN"
2016-11-04 16:20:46 +08:00
self.direction.setText("<font size=\"2\">" + direction + "</font>")
self.programmatic_change = True
try:
2017-02-27 00:59:31 +08:00
self.override.setChecked(self.cur_override)
if self.cur_override:
self.stack.setCurrentIndex(1)
2017-02-27 11:45:04 +08:00
self.level.setChecked(self.cur_level)
finally:
self.programmatic_change = False
2015-06-05 19:11:41 +08:00
def sort_key(self):
return self.channel
2015-06-05 19:11:41 +08:00
2017-10-26 02:26:59 +08:00
class _SimpleDisplayWidget(QtWidgets.QFrame):
def __init__(self, title):
2016-11-04 16:20:46 +08:00
QtWidgets.QFrame.__init__(self)
self.setFrameShape(QtWidgets.QFrame.Box)
self.setFrameShadow(QtWidgets.QFrame.Raised)
2015-06-12 17:41:04 +08:00
2016-02-15 07:23:47 +08:00
grid = QtWidgets.QGridLayout()
2016-11-04 16:20:46 +08:00
grid.setContentsMargins(0, 0, 0, 0)
grid.setHorizontalSpacing(0)
grid.setVerticalSpacing(0)
2015-06-12 17:41:04 +08:00
self.setLayout(grid)
2016-02-15 07:23:47 +08:00
label = QtWidgets.QLabel(title)
2015-06-12 17:41:04 +08:00
label.setAlignment(QtCore.Qt.AlignCenter)
grid.addWidget(label, 1, 1)
self.value = QtWidgets.QLabel()
self.value.setAlignment(QtCore.Qt.AlignCenter)
grid.addWidget(self.value, 2, 1, 6, 1)
2015-06-12 17:41:04 +08:00
2015-09-02 04:25:20 +08:00
grid.setRowStretch(1, 1)
grid.setRowStretch(2, 0)
grid.setRowStretch(3, 1)
2017-02-27 00:59:31 +08:00
self.refresh_display()
2015-06-12 17:41:04 +08:00
2017-10-26 02:26:59 +08:00
def refresh_display(self):
raise NotImplementedError
def sort_key(self):
raise NotImplementedError
2022-07-07 10:52:53 +08:00
class _DDSModel:
def __init__(self, dds_type, ref_clk, cpld=None, pll=1, clk_div=0):
self.cpld = cpld
self.cur_frequency = 0
self.cur_reg = 0
self.dds_type = dds_type
self.is_urukul = dds_type in ["AD9910", "AD9912"]
if dds_type == "AD9914":
self.ftw_per_hz = 2**32 / ref_clk
else:
if dds_type == "AD9910":
max_freq = 1 << 32
clk_mult = [4, 1, 2, 4]
elif dds_type == "AD9912": # AD9912
max_freq = 1 << 48
clk_mult = [1, 1, 2, 4]
else:
raise NotImplementedError
sysclk = ref_clk / clk_mult[clk_div] * pll
self.ftw_per_hz = 1 / sysclk * max_freq
def monitor_update(self, probe, value):
if self.dds_type == "AD9912":
value = value << 16
self.cur_frequency = self._ftw_to_freq(value)
def _ftw_to_freq(self, ftw):
return ftw / self.ftw_per_hz
class _DDSWidget(QtWidgets.QFrame):
2022-07-07 10:52:53 +08:00
def __init__(self, dm, title, bus_channel=0, channel=0, dds_model=None):
self.dm = dm
2017-10-26 02:26:59 +08:00
self.bus_channel = bus_channel
self.channel = channel
self.dds_name = title
2017-10-26 02:26:59 +08:00
self.cur_frequency = 0
2022-07-07 10:52:53 +08:00
self.dds_model = dds_model
QtWidgets.QFrame.__init__(self)
self.setFrameShape(QtWidgets.QFrame.Box)
self.setFrameShadow(QtWidgets.QFrame.Raised)
grid = QtWidgets.QGridLayout()
grid.setContentsMargins(0, 0, 0, 0)
grid.setHorizontalSpacing(0)
grid.setVerticalSpacing(0)
self.setLayout(grid)
label = QtWidgets.QLabel(title)
label.setAlignment(QtCore.Qt.AlignCenter)
grid.addWidget(label, 1, 1)
# FREQ DATA/EDIT FIELD
self.data_stack = QtWidgets.QStackedWidget()
# page 1: display data
grid_disp = LayoutWidget()
grid_disp.layout.setContentsMargins(0, 0, 0, 0)
grid_disp.layout.setHorizontalSpacing(0)
grid_disp.layout.setVerticalSpacing(0)
self.value_label = QtWidgets.QLabel()
self.value_label.setAlignment(QtCore.Qt.AlignCenter)
grid_disp.addWidget(self.value_label, 0, 1, 1, 2)
unit = QtWidgets.QLabel("MHz")
unit.setAlignment(QtCore.Qt.AlignCenter)
grid_disp.addWidget(unit, 0, 3, 1, 1)
self.data_stack.addWidget(grid_disp)
# page 2: edit data
grid_edit = LayoutWidget()
grid_edit.layout.setContentsMargins(0, 0, 0, 0)
grid_edit.layout.setHorizontalSpacing(0)
grid_edit.layout.setVerticalSpacing(0)
self.value_edit = _CancellableLineEdit(self)
self.value_edit.setAlignment(QtCore.Qt.AlignRight)
grid_edit.addWidget(self.value_edit, 0, 1, 1, 2)
unit = QtWidgets.QLabel("MHz")
unit.setAlignment(QtCore.Qt.AlignCenter)
grid_edit.addWidget(unit, 0, 3, 1, 1)
self.data_stack.addWidget(grid_edit)
grid.addWidget(self.data_stack, 2, 1)
# BUTTONS
self.button_stack = QtWidgets.QStackedWidget()
# page 1: SET button
set_grid = LayoutWidget()
set_btn = QtWidgets.QToolButton()
set_btn.setText("Set")
set_btn.setToolTip("Set frequency")
set_grid.addWidget(set_btn, 0, 1, 1, 1)
# for urukuls also allow switching off RF
2022-07-07 10:52:53 +08:00
if self.dds_model.is_urukul:
off_btn = QtWidgets.QToolButton()
off_btn.setText("Off")
off_btn.setToolTip("Switch off the output")
set_grid.addWidget(off_btn, 0, 2, 1, 1)
self.button_stack.addWidget(set_grid)
# page 2: apply/cancel buttons
apply_grid = LayoutWidget()
apply = QtWidgets.QToolButton()
apply.setText("Apply")
apply.setToolTip("Apply changes")
apply_grid.addWidget(apply, 0, 1, 1, 1)
cancel = QtWidgets.QToolButton()
cancel.setText("Cancel")
cancel.setToolTip("Cancel changes")
apply_grid.addWidget(cancel, 0, 2, 1, 1)
self.button_stack.addWidget(apply_grid)
grid.addWidget(self.button_stack, 3, 1)
grid.setRowStretch(1, 1)
grid.setRowStretch(2, 1)
grid.setRowStretch(3, 1)
set_btn.clicked.connect(self.set_clicked)
apply.clicked.connect(self.apply_changes)
2022-07-07 10:52:53 +08:00
if self.dds_model.is_urukul:
off_btn.clicked.connect(self.off_clicked)
off_btn.setToolTip(textwrap.dedent(
"""Note: If TTL RTIO sw for the channel is switched high,
this button will not disable the channel.
Use the TTL override instead."""))
self.value_edit.returnPressed.connect(lambda: self.apply_changes(None))
self.value_edit.escapePressedConnect(self.cancel_changes)
cancel.clicked.connect(self.cancel_changes)
self.refresh_display()
def set_clicked(self, set):
self.data_stack.setCurrentIndex(1)
self.button_stack.setCurrentIndex(1)
self.value_edit.setText("{:.7f}"
.format(self.cur_frequency/1e6))
self.value_edit.setFocus()
self.value_edit.selectAll()
def off_clicked(self, set):
2022-07-07 10:52:53 +08:00
self.dm.dds_channel_toggle(self.dds_name, self.dds_model, sw=False)
def apply_changes(self, apply):
self.data_stack.setCurrentIndex(0)
self.button_stack.setCurrentIndex(0)
frequency = float(self.value_edit.text())*1e6
2022-07-07 10:52:53 +08:00
self.dm.dds_set_frequency(self.dds_name, self.dds_model, frequency)
def cancel_changes(self, cancel):
self.data_stack.setCurrentIndex(0)
self.button_stack.setCurrentIndex(0)
2017-10-26 02:26:59 +08:00
2017-02-27 00:59:31 +08:00
def refresh_display(self):
2022-07-07 10:52:53 +08:00
self.cur_frequency = self.dds_model.cur_frequency
self.value_label.setText("<font size=\"4\">{:.7f}</font>"
.format(self.cur_frequency/1e6))
self.value_edit.setText("{:.7f}"
2017-02-27 00:59:31 +08:00
.format(self.cur_frequency/1e6))
2015-06-12 17:41:04 +08:00
def sort_key(self):
return (self.bus_channel, self.channel)
2015-06-12 17:41:04 +08:00
2017-10-26 02:26:59 +08:00
class _DACWidget(_SimpleDisplayWidget):
def __init__(self, dm, spi_channel, channel, title):
2017-10-26 02:26:59 +08:00
self.spi_channel = spi_channel
self.channel = channel
self.cur_value = 0
_SimpleDisplayWidget.__init__(self, "{} ch{}".format(title, channel))
def refresh_display(self):
self.value.setText("<font size=\"4\">{:.3f}</font><font size=\"2\"> %</font>"
.format(self.cur_value*100/2**16))
def sort_key(self):
return (self.spi_channel, self.channel)
_WidgetDesc = namedtuple("_WidgetDesc", "uid comment cls arguments")
2017-02-27 00:59:31 +08:00
def setup_from_ddb(ddb):
mi_addr = None
mi_port = None
dds_sysclk = None
description = set()
for k, v in ddb.items():
2015-06-05 19:11:41 +08:00
try:
if isinstance(v, dict):
comment = v.get("comment")
if v["type"] == "local":
2022-03-19 22:50:36 +08:00
if v["module"] == "artiq.coredevice.ttl":
if "ttl_urukul" in k:
continue
channel = v["arguments"]["channel"]
force_out = v["class"] == "TTLOut"
widget = _WidgetDesc(k, comment, _TTLWidget, (channel, force_out, k))
description.add(widget)
elif (v["module"] == "artiq.coredevice.ad9914"
and v["class"] == "AD9914"):
bus_channel = v["arguments"]["bus_channel"]
channel = v["arguments"]["channel"]
dds_sysclk = v["arguments"]["sysclk"]
2022-07-07 10:52:53 +08:00
model = _DDSModel(v["class"], dds_sysclk)
widget = _WidgetDesc(k, comment, _DDSWidget, (k, bus_channel, channel, model))
description.add(widget)
elif (v["module"] == "artiq.coredevice.ad9910"
and v["class"] == "AD9910") or \
(v["module"] == "artiq.coredevice.ad9912"
and v["class"] == "AD9912"):
channel = v["arguments"]["chip_select"] - 4
if channel < 0:
continue
dds_cpld = v["arguments"]["cpld_device"]
spi_dev = ddb[dds_cpld]["arguments"]["spi_device"]
bus_channel = ddb[spi_dev]["arguments"]["channel"]
2022-07-07 10:52:53 +08:00
pll = v["arguments"]["pll_n"]
refclk = ddb[dds_cpld]["arguments"]["refclk"]
clk_div = v["arguments"].get("clk_div", 0)
model = _DDSModel( v["class"], refclk, dds_cpld, pll, clk_div)
widget = _WidgetDesc(k, comment, _DDSWidget, (k, bus_channel, channel, model))
description.add(widget)
elif ( (v["module"] == "artiq.coredevice.ad53xx" and v["class"] == "AD53xx")
or (v["module"] == "artiq.coredevice.zotino" and v["class"] == "Zotino")):
spi_device = v["arguments"]["spi_device"]
spi_device = ddb[spi_device]
while isinstance(spi_device, str):
spi_device = ddb[spi_device]
spi_channel = spi_device["arguments"]["channel"]
for channel in range(32):
widget = _WidgetDesc((k, channel), comment, _DACWidget, (spi_channel, channel, k))
description.add(widget)
2022-03-19 22:50:36 +08:00
elif v["type"] == "controller" and k == "core_moninj":
mi_addr = v["host"]
mi_port = v.get("port_proxy", 1383)
2015-06-05 19:11:41 +08:00
except KeyError:
pass
2022-07-07 10:52:53 +08:00
return mi_addr, mi_port, description
class _DeviceManager:
def __init__(self, schedule_ctl):
self.mi_addr = None
self.mi_port = None
self.reconnect_mi = asyncio.Event()
self.mi_connection = None
self.mi_connector_task = asyncio.ensure_future(self.mi_connector())
self.schedule_ctl = schedule_ctl
self.ddb = dict()
self.description = set()
self.widgets_by_uid = dict()
self.dds_sysclk = 0
self.ttl_cb = lambda: None
self.ttl_widgets = dict()
self.dds_cb = lambda: None
self.dds_widgets = dict()
self.dac_cb = lambda: None
self.dac_widgets = dict()
def init_ddb(self, ddb):
self.ddb = ddb
return ddb
def notify(self, mod):
2022-07-07 10:52:53 +08:00
mi_addr, mi_port, description = setup_from_ddb(self.ddb)
if (mi_addr, mi_port) != (self.mi_addr, self.mi_port):
self.mi_addr = mi_addr
self.mi_port = mi_port
self.reconnect_mi.set()
for to_remove in self.description - description:
widget = self.widgets_by_uid[to_remove.uid]
del self.widgets_by_uid[to_remove.uid]
if isinstance(widget, _TTLWidget):
self.setup_ttl_monitoring(False, widget.channel)
widget.deleteLater()
del self.ttl_widgets[widget.channel]
self.ttl_cb()
elif isinstance(widget, _DDSWidget):
self.setup_dds_monitoring(False, widget.bus_channel, widget.channel)
widget.deleteLater()
del self.dds_widgets[(widget.bus_channel, widget.channel)]
self.dds_cb()
elif isinstance(widget, _DACWidget):
2017-10-26 02:26:59 +08:00
self.setup_dac_monitoring(False, widget.spi_channel, widget.channel)
widget.deleteLater()
del self.dac_widgets[(widget.spi_channel, widget.channel)]
self.dac_cb()
else:
raise ValueError
for to_add in description - self.description:
widget = to_add.cls(self, *to_add.arguments)
if to_add.comment is not None:
widget.setToolTip(to_add.comment)
self.widgets_by_uid[to_add.uid] = widget
if isinstance(widget, _TTLWidget):
self.ttl_widgets[widget.channel] = widget
self.ttl_cb()
self.setup_ttl_monitoring(True, widget.channel)
elif isinstance(widget, _DDSWidget):
self.dds_widgets[(widget.bus_channel, widget.channel)] = widget
self.dds_cb()
self.setup_dds_monitoring(True, widget.bus_channel, widget.channel)
elif isinstance(widget, _DACWidget):
self.dac_widgets[(widget.spi_channel, widget.channel)] = widget
self.dac_cb()
self.setup_dac_monitoring(True, widget.spi_channel, widget.channel)
else:
raise ValueError
self.description = description
2017-02-27 00:59:31 +08:00
def ttl_set_mode(self, channel, mode):
if self.mi_connection is not None:
widget = self.ttl_widgets[channel]
2017-02-27 00:59:31 +08:00
if mode == "0":
widget.cur_override = True
widget.cur_level = False
self.mi_connection.inject(channel, TTLOverride.level.value, 0)
self.mi_connection.inject(channel, TTLOverride.oe.value, 1)
self.mi_connection.inject(channel, TTLOverride.en.value, 1)
2017-02-27 00:59:31 +08:00
elif mode == "1":
widget.cur_override = True
widget.cur_level = True
self.mi_connection.inject(channel, TTLOverride.level.value, 1)
self.mi_connection.inject(channel, TTLOverride.oe.value, 1)
self.mi_connection.inject(channel, TTLOverride.en.value, 1)
2017-02-27 00:59:31 +08:00
elif mode == "exp":
widget.cur_override = False
self.mi_connection.inject(channel, TTLOverride.en.value, 0)
2017-02-27 00:59:31 +08:00
else:
raise ValueError
# override state may have changed
widget.refresh_display()
2017-02-27 00:59:31 +08:00
async def _submit_by_content(self, content, class_name, title):
expid = {
"log_level": logging.WARNING,
"content": content,
"class_name": class_name,
"arguments": {}
}
scheduling = {
"pipeline_name": "main",
"priority": 0,
"due_date": None,
"flush": False
}
rid = await self.schedule_ctl.submit(
scheduling["pipeline_name"],
expid,
scheduling["priority"], scheduling["due_date"],
scheduling["flush"])
logger.info("Submitted '%s', RID is %d", title, rid)
def _dds_faux_injection(self, dds_channel, dds_model, action, title, log_msg):
# create kernel and fill it in and send-by-content
# initialize CPLD (if applicable)
2022-07-07 10:52:53 +08:00
if dds_model.is_urukul:
# urukuls need CPLD init and switch to on
cpld_dev = """self.setattr_device("core_cache")
2022-07-07 10:52:53 +08:00
self.setattr_device("{}")""".format(dds_model.cpld)
# `sta`/`rf_sw`` variables are guaranteed for urukuls
# so {action} can use it
# if there's no RF enabled, CPLD may have not been initialized
# but if there is, it has been initialised - no need to do again
cpld_init = """delay(15*ms)
was_init = self.core_cache.get("_{cpld}_init")
sta = self.{cpld}.sta_read()
rf_sw = urukul_sta_rf_sw(sta)
if rf_sw == 0 and len(was_init) == 0:
delay(15*ms)
self.{cpld}.init()
self.core_cache.put("_{cpld}_init", [1])
2022-07-07 10:52:53 +08:00
""".format(cpld=dds_model.cpld)
else:
cpld_dev = ""
cpld_init = ""
# AD9912/9910: init channel (if uninitialized)
if dds_model.dds_type == "AD9912":
# 0xFF before init, 0x99 after
channel_init = """
if self.{dds_channel}.read({cfgreg}, length=1) == 0xFF:
delay(10*ms)
self.{dds_channel}.init()
""".format(dds_channel=dds_channel, cfgreg=AD9912_SER_CONF)
elif dds_model.dds_type == "AD9910":
# -1 before init, 2 after
channel_init = """
if self.{dds_channel}.read32({cfgreg}) == -1:
delay(10*ms)
self.{dds_channel}.init()
""".format(dds_channel=dds_channel, cfgreg=AD9912_SER_CONF)
else:
channel_init = "self.{dds_channel}.init()".format(dds_channel=dds_channel)
dds_exp = textwrap.dedent("""
from artiq.experiment import *
from artiq.coredevice.urukul import *
class {title}(EnvExperiment):
def build(self):
self.setattr_device("core")
self.setattr_device("{dds_channel}")
{cpld_dev}
@kernel
def run(self):
self.core.break_realtime()
{cpld_init}
delay(10*ms)
{channel_init}
delay(15*ms)
{action}
""".format(title=title, action=action,
dds_channel=dds_channel,
cpld_dev=cpld_dev, cpld_init=cpld_init,
channel_init=channel_init))
asyncio.ensure_future(
self._submit_by_content(
dds_exp,
title,
log_msg))
def dds_set_frequency(self, dds_channel, dds_model, freq):
action = "self.{ch}.set({freq})".format(
freq=freq, ch=dds_channel)
if dds_model.is_urukul:
action += """
ch_no = self.{ch}.chip_select - 4
self.{cpld}.cfg_switches(rf_sw | 1 << ch_no)
""".format(ch=dds_channel, cpld=dds_model.cpld)
self._dds_faux_injection(
dds_channel,
dds_model,
action,
"SetDDS",
"Set DDS {} {}MHz".format(dds_channel, freq/1e6))
def dds_channel_toggle(self, dds_channel, dds_model, sw=True):
# urukul only
if sw:
switch = "| 1 << ch_no"
else:
switch = "& ~(1 << ch_no)"
action = """
ch_no = self.{dds_channel}.chip_select - 4
self.{cpld}.cfg_switches(rf_sw {switch})
""".format(
dds_channel=dds_channel,
cpld=dds_model.cpld,
switch=switch
)
self._dds_faux_injection(
dds_channel,
dds_model,
action,
"ToggleDDS",
"Toggle DDS {} {}".format(dds_channel, "on" if sw else "off"))
2017-02-27 00:59:31 +08:00
def setup_ttl_monitoring(self, enable, channel):
if self.mi_connection is not None:
self.mi_connection.monitor_probe(enable, channel, TTLProbe.level.value)
self.mi_connection.monitor_probe(enable, channel, TTLProbe.oe.value)
self.mi_connection.monitor_injection(enable, channel, TTLOverride.en.value)
self.mi_connection.monitor_injection(enable, channel, TTLOverride.level.value)
2017-02-27 11:45:04 +08:00
if enable:
self.mi_connection.get_injection_status(channel, TTLOverride.en.value)
2017-02-27 00:59:31 +08:00
def setup_dds_monitoring(self, enable, bus_channel, channel):
if self.mi_connection is not None:
self.mi_connection.monitor_probe(enable, bus_channel, channel)
2017-02-27 00:59:31 +08:00
2017-10-26 02:26:59 +08:00
def setup_dac_monitoring(self, enable, spi_channel, channel):
if self.mi_connection is not None:
self.mi_connection.monitor_probe(enable, spi_channel, channel)
2017-10-26 02:26:59 +08:00
2017-02-27 00:59:31 +08:00
def monitor_cb(self, channel, probe, value):
if channel in self.ttl_widgets:
widget = self.ttl_widgets[channel]
if probe == TTLProbe.level.value:
2017-02-27 00:59:31 +08:00
widget.cur_level = bool(value)
elif probe == TTLProbe.oe.value:
2017-02-27 00:59:31 +08:00
widget.cur_oe = bool(value)
widget.refresh_display()
2022-07-07 10:52:53 +08:00
elif (channel, probe) in self.dds_widgets:
widget = self.dds_widgets[(channel, probe)]
2022-07-07 10:52:53 +08:00
widget.dds_model.monitor_update(probe, value)
2017-02-27 00:59:31 +08:00
widget.refresh_display()
2022-07-07 10:52:53 +08:00
elif (channel, probe) in self.dac_widgets:
widget = self.dac_widgets[(channel, probe)]
2017-10-26 02:26:59 +08:00
widget.cur_value = value
widget.refresh_display()
2017-02-27 00:59:31 +08:00
def injection_status_cb(self, channel, override, value):
if channel in self.ttl_widgets:
widget = self.ttl_widgets[channel]
if override == TTLOverride.en.value:
widget.cur_override = bool(value)
if override == TTLOverride.level.value:
widget.cur_override_level = bool(value)
widget.refresh_display()
2017-02-27 00:59:31 +08:00
def disconnect_cb(self):
logger.error("lost connection to moninj")
self.reconnect_mi.set()
async def mi_connector(self):
2017-02-27 00:59:31 +08:00
while True:
await self.reconnect_mi.wait()
self.reconnect_mi.clear()
if self.mi_connection is not None:
await self.mi_connection.close()
self.mi_connection = None
new_mi_connection = CommMonInj(self.monitor_cb, self.injection_status_cb,
self.disconnect_cb)
2017-02-27 00:59:31 +08:00
try:
await new_mi_connection.connect(self.mi_addr, self.mi_port)
except asyncio.CancelledError:
logger.info("cancelled connection to moninj")
break
2017-02-27 00:59:31 +08:00
except:
2022-08-12 13:41:05 +08:00
logger.error("failed to connect to moninj. Is aqctl_moninj_proxy running?", exc_info=True)
await asyncio.sleep(10.)
self.reconnect_mi.set()
2017-02-27 00:59:31 +08:00
else:
2022-08-12 13:41:05 +08:00
logger.info("ARTIQ dashboard connected to moninj (%s)",
self.mi_addr)
self.mi_connection = new_mi_connection
for ttl_channel in self.ttl_widgets.keys():
2017-02-27 00:59:31 +08:00
self.setup_ttl_monitoring(True, ttl_channel)
for bus_channel, channel in self.dds_widgets.keys():
2017-02-27 00:59:31 +08:00
self.setup_dds_monitoring(True, bus_channel, channel)
for spi_channel, channel in self.dac_widgets.keys():
2017-10-26 02:26:59 +08:00
self.setup_dac_monitoring(True, spi_channel, channel)
2017-02-27 00:59:31 +08:00
async def close(self):
self.mi_connector_task.cancel()
try:
await asyncio.wait_for(self.mi_connector_task, None)
2017-02-27 00:59:31 +08:00
except asyncio.CancelledError:
pass
if self.mi_connection is not None:
await self.mi_connection.close()
class _MonInjDock(QtWidgets.QDockWidget):
2015-06-12 17:41:04 +08:00
def __init__(self, name):
QtWidgets.QDockWidget.__init__(self, name)
2016-02-14 20:46:15 +08:00
self.setObjectName(name)
self.setFeatures(QtWidgets.QDockWidget.DockWidgetMovable |
QtWidgets.QDockWidget.DockWidgetFloatable)
2015-06-12 17:41:04 +08:00
def layout_widgets(self, widgets):
2016-02-15 07:57:15 +08:00
scroll_area = QtWidgets.QScrollArea()
self.setWidget(scroll_area)
2016-03-17 18:18:31 +08:00
grid = FlowLayout()
2016-02-15 07:57:15 +08:00
grid_widget = QtWidgets.QWidget()
grid_widget.setLayout(grid)
2017-10-26 02:26:59 +08:00
for widget in sorted(widgets, key=lambda w: w.sort_key()):
grid.addWidget(widget)
2016-02-15 07:57:15 +08:00
2016-03-17 18:18:31 +08:00
scroll_area.setWidgetResizable(True)
2016-02-15 07:57:15 +08:00
scroll_area.setWidget(grid_widget)
2015-06-12 17:41:04 +08:00
2017-02-27 00:59:31 +08:00
class MonInj:
def __init__(self, schedule_ctl):
2015-06-12 17:41:04 +08:00
self.ttl_dock = _MonInjDock("TTL")
self.dds_dock = _MonInjDock("DDS")
2017-10-26 02:26:59 +08:00
self.dac_dock = _MonInjDock("DAC")
2015-06-12 17:41:04 +08:00
self.dm = _DeviceManager(schedule_ctl)
self.dm.ttl_cb = lambda: self.ttl_dock.layout_widgets(
self.dm.ttl_widgets.values())
self.dm.dds_cb = lambda: self.dds_dock.layout_widgets(
self.dm.dds_widgets.values())
self.dm.dac_cb = lambda: self.dac_dock.layout_widgets(
self.dm.dac_widgets.values())
self.subscriber = Subscriber("devices", self.dm.init_ddb, self.dm.notify)
2015-06-12 17:41:04 +08:00
2015-10-03 19:28:57 +08:00
async def start(self, server, port):
await self.subscriber.connect(server, port)
2015-10-03 19:28:57 +08:00
async def stop(self):
await self.subscriber.close()
2017-02-27 00:59:31 +08:00
if self.dm is not None:
await self.dm.close()