2017-07-20 12:01:29 +08:00
|
|
|
"""
|
|
|
|
Drivers for TTL signals on RTIO.
|
|
|
|
|
|
|
|
TTL channels (including the clock generator) all support output event
|
2018-09-26 12:12:37 +08:00
|
|
|
replacement. For example, pulses of "zero" length (e.g. :meth:`TTLInOut.on`
|
|
|
|
immediately followed by :meth:`TTLInOut.off`, without a delay) are suppressed.
|
2017-07-20 12:01:29 +08:00
|
|
|
"""
|
|
|
|
|
2016-07-06 12:44:21 +08:00
|
|
|
import numpy
|
|
|
|
|
2014-09-12 15:28:02 +08:00
|
|
|
from artiq.language.core import *
|
2015-08-11 00:25:48 +08:00
|
|
|
from artiq.language.types import *
|
2016-09-07 17:37:14 +08:00
|
|
|
from artiq.coredevice.rtio import (rtio_output, rtio_input_timestamp,
|
|
|
|
rtio_input_data)
|
|
|
|
from artiq.coredevice.exceptions import RTIOOverflow
|
2014-09-12 15:28:02 +08:00
|
|
|
|
|
|
|
|
2016-09-07 16:55:21 +08:00
|
|
|
# RTIO TTL address map:
|
|
|
|
# 0 Output level
|
|
|
|
# 1 Output enable
|
|
|
|
# 2 Set input sensitivity
|
|
|
|
# 3 Set input sensitivity and sample
|
|
|
|
|
|
|
|
|
2015-07-14 04:08:20 +08:00
|
|
|
class TTLOut:
|
2015-05-02 10:35:21 +08:00
|
|
|
"""RTIO TTL output driver.
|
2014-09-30 16:42:07 +08:00
|
|
|
|
2015-05-02 10:35:21 +08:00
|
|
|
This should be used with output-only channels.
|
2014-09-30 16:42:07 +08:00
|
|
|
|
|
|
|
:param channel: channel number
|
|
|
|
"""
|
2018-11-07 23:41:43 +08:00
|
|
|
kernel_invariants = {"core", "channel", "target_o"}
|
2016-04-03 02:20:51 +08:00
|
|
|
|
2016-03-09 13:01:34 +08:00
|
|
|
def __init__(self, dmgr, channel, core_device="core"):
|
|
|
|
self.core = dmgr.get(core_device)
|
2015-07-14 04:08:20 +08:00
|
|
|
self.channel = channel
|
2018-11-07 23:41:43 +08:00
|
|
|
self.target_o = channel << 8
|
2015-04-14 19:44:45 +08:00
|
|
|
|
2022-12-02 16:27:03 +08:00
|
|
|
@staticmethod
|
|
|
|
def get_rtio_channels(channel, **kwargs):
|
|
|
|
return [(channel, None)]
|
|
|
|
|
2016-03-01 19:03:10 +08:00
|
|
|
@kernel
|
|
|
|
def output(self):
|
|
|
|
pass
|
|
|
|
|
2015-04-14 19:44:45 +08:00
|
|
|
@kernel
|
2015-07-02 04:22:53 +08:00
|
|
|
def set_o(self, o):
|
2018-11-08 18:29:24 +08:00
|
|
|
rtio_output(self.target_o, 1 if o else 0)
|
2014-09-12 15:28:02 +08:00
|
|
|
|
|
|
|
@kernel
|
|
|
|
def on(self):
|
2019-01-12 20:01:55 +08:00
|
|
|
"""Set the output to a logic high state at the current position
|
2016-06-12 13:05:54 +08:00
|
|
|
of the time cursor.
|
|
|
|
|
|
|
|
The time cursor is not modified by this function."""
|
2015-07-02 04:22:53 +08:00
|
|
|
self.set_o(True)
|
2014-09-12 15:28:02 +08:00
|
|
|
|
|
|
|
@kernel
|
|
|
|
def off(self):
|
2016-06-12 13:05:54 +08:00
|
|
|
"""Set the output to a logic low state at the current position
|
|
|
|
of the time cursor.
|
|
|
|
|
|
|
|
The time cursor is not modified by this function."""
|
2015-07-02 04:22:53 +08:00
|
|
|
self.set_o(False)
|
|
|
|
|
|
|
|
@kernel
|
|
|
|
def pulse_mu(self, duration):
|
2015-07-05 00:36:01 +08:00
|
|
|
"""Pulse the output high for the specified duration
|
2016-06-12 13:05:54 +08:00
|
|
|
(in machine units).
|
|
|
|
|
|
|
|
The time cursor is advanced by the specified duration."""
|
2015-07-02 04:22:53 +08:00
|
|
|
self.on()
|
|
|
|
delay_mu(duration)
|
|
|
|
self.off()
|
2014-09-12 15:28:02 +08:00
|
|
|
|
|
|
|
@kernel
|
|
|
|
def pulse(self, duration):
|
2015-07-05 00:36:01 +08:00
|
|
|
"""Pulse the output high for the specified duration
|
2016-06-12 13:05:54 +08:00
|
|
|
(in seconds).
|
|
|
|
|
|
|
|
The time cursor is advanced by the specified duration."""
|
2014-09-12 15:28:02 +08:00
|
|
|
self.on()
|
|
|
|
delay(duration)
|
|
|
|
self.off()
|
2014-09-13 19:37:57 +08:00
|
|
|
|
|
|
|
|
2015-07-14 04:08:20 +08:00
|
|
|
class TTLInOut:
|
2015-05-02 10:35:21 +08:00
|
|
|
"""RTIO TTL input/output driver.
|
|
|
|
|
|
|
|
In output mode, provides functions to set the logic level on the signal.
|
2014-09-30 16:42:07 +08:00
|
|
|
|
2015-05-02 10:35:21 +08:00
|
|
|
In input mode, provides functions to analyze the incoming signal, with
|
|
|
|
real-time gating to prevent overflows.
|
|
|
|
|
|
|
|
RTIO TTLs supports zero-length transition suppression. For example, if
|
|
|
|
two pulses are emitted back-to-back with no delay between them, they will
|
|
|
|
be merged into a single pulse with a duration equal to the sum of the
|
|
|
|
durations of the original pulses.
|
|
|
|
|
|
|
|
This should be used with bidirectional channels.
|
2014-09-30 16:42:07 +08:00
|
|
|
|
2016-03-01 00:28:40 +08:00
|
|
|
Note that the channel is in input mode by default. If you need to drive a
|
2018-09-26 12:12:37 +08:00
|
|
|
signal, you must call :meth:`output`. If the channel is in output mode most of
|
|
|
|
the time in your setup, it is a good idea to call :meth:`output` in the
|
2016-03-01 00:28:40 +08:00
|
|
|
startup kernel.
|
|
|
|
|
2016-09-07 16:55:21 +08:00
|
|
|
There are three input APIs: gating, sampling and watching. When one
|
|
|
|
API is active (e.g. the gate is open, or the input events have not been
|
|
|
|
fully read out), another API must not be used simultaneously.
|
|
|
|
|
2014-09-30 16:42:07 +08:00
|
|
|
:param channel: channel number
|
|
|
|
"""
|
2018-11-19 11:54:50 +08:00
|
|
|
kernel_invariants = {"core", "channel", "gate_latency_mu",
|
2018-11-07 23:41:43 +08:00
|
|
|
"target_o", "target_oe", "target_sens", "target_sample"}
|
2016-04-03 02:20:51 +08:00
|
|
|
|
2018-11-17 21:45:45 +08:00
|
|
|
def __init__(self, dmgr, channel, gate_latency_mu=None,
|
|
|
|
core_device="core"):
|
2016-03-09 13:01:34 +08:00
|
|
|
self.core = dmgr.get(core_device)
|
2015-07-14 04:08:20 +08:00
|
|
|
self.channel = channel
|
2018-11-19 11:54:50 +08:00
|
|
|
|
2018-11-17 21:45:45 +08:00
|
|
|
# With TTLs inputs, the gate control is connected to a high-latency
|
|
|
|
# path through SED. When looking at the RTIO counter to determine if
|
|
|
|
# the gate has closed, we need to take this latency into account.
|
|
|
|
# See: https://github.com/m-labs/artiq/issues/1137
|
|
|
|
if gate_latency_mu is None:
|
|
|
|
gate_latency_mu = 13*self.core.ref_multiplier
|
|
|
|
self.gate_latency_mu = gate_latency_mu
|
2015-04-14 19:44:45 +08:00
|
|
|
|
2018-11-07 23:41:43 +08:00
|
|
|
self.target_o = (channel << 8) + 0
|
|
|
|
self.target_oe = (channel << 8) + 1
|
|
|
|
self.target_sens = (channel << 8) + 2
|
|
|
|
self.target_sample = (channel << 8) + 3
|
2015-04-14 19:44:45 +08:00
|
|
|
|
2022-12-02 16:27:03 +08:00
|
|
|
@staticmethod
|
|
|
|
def get_rtio_channels(channel, **kwargs):
|
|
|
|
return [(channel, None)]
|
|
|
|
|
2015-04-14 19:44:45 +08:00
|
|
|
@kernel
|
2015-07-02 04:22:53 +08:00
|
|
|
def set_oe(self, oe):
|
2018-11-08 18:29:24 +08:00
|
|
|
rtio_output(self.target_oe, 1 if oe else 0)
|
2015-05-02 10:35:21 +08:00
|
|
|
|
|
|
|
@kernel
|
|
|
|
def output(self):
|
2016-06-12 13:05:54 +08:00
|
|
|
"""Set the direction to output at the current position of the time
|
|
|
|
cursor.
|
2016-03-01 00:28:40 +08:00
|
|
|
|
|
|
|
There must be a delay of at least one RTIO clock cycle before any
|
2019-07-24 10:04:45 +08:00
|
|
|
other command can be issued.
|
|
|
|
|
|
|
|
This method only configures the direction at the FPGA. When using
|
|
|
|
buffered I/O interfaces, such as the Sinara TTL cards, the buffer
|
|
|
|
direction must be configured separately in the hardware."""
|
2015-07-02 04:22:53 +08:00
|
|
|
self.set_oe(True)
|
2015-05-02 10:35:21 +08:00
|
|
|
|
|
|
|
@kernel
|
|
|
|
def input(self):
|
2016-06-12 13:05:54 +08:00
|
|
|
"""Set the direction to input at the current position of the time
|
|
|
|
cursor.
|
2016-03-01 00:28:40 +08:00
|
|
|
|
|
|
|
There must be a delay of at least one RTIO clock cycle before any
|
2019-07-24 10:04:45 +08:00
|
|
|
other command can be issued.
|
|
|
|
|
|
|
|
This method only configures the direction at the FPGA. When using
|
|
|
|
buffered I/O interfaces, such as the Sinara TTL cards, the buffer
|
|
|
|
direction must be configured separately in the hardware."""
|
2015-07-02 04:22:53 +08:00
|
|
|
self.set_oe(False)
|
2015-05-02 10:35:21 +08:00
|
|
|
|
|
|
|
@kernel
|
2015-07-02 04:22:53 +08:00
|
|
|
def set_o(self, o):
|
2018-11-08 18:29:24 +08:00
|
|
|
rtio_output(self.target_o, 1 if o else 0)
|
2015-05-02 10:35:21 +08:00
|
|
|
|
|
|
|
@kernel
|
|
|
|
def on(self):
|
2016-06-12 13:05:54 +08:00
|
|
|
"""Set the output to a logic high state at the current position of the
|
|
|
|
time cursor.
|
|
|
|
|
|
|
|
The channel must be in output mode.
|
2015-08-13 12:20:12 +08:00
|
|
|
|
2016-06-12 13:05:54 +08:00
|
|
|
The time cursor is not modified by this function."""
|
2015-07-02 04:22:53 +08:00
|
|
|
self.set_o(True)
|
2015-05-02 10:35:21 +08:00
|
|
|
|
|
|
|
@kernel
|
|
|
|
def off(self):
|
2016-06-12 13:05:54 +08:00
|
|
|
"""Set the output to a logic low state at the current position of the
|
|
|
|
time cursor.
|
2015-08-13 12:20:12 +08:00
|
|
|
|
2016-06-12 13:05:54 +08:00
|
|
|
The channel must be in output mode.
|
|
|
|
|
|
|
|
The time cursor is not modified by this function."""
|
2015-07-02 04:22:53 +08:00
|
|
|
self.set_o(False)
|
|
|
|
|
|
|
|
@kernel
|
|
|
|
def pulse_mu(self, duration):
|
2019-01-12 20:01:55 +08:00
|
|
|
"""Pulse the output high for the specified duration
|
2016-06-12 13:05:54 +08:00
|
|
|
(in machine units).
|
|
|
|
|
|
|
|
The time cursor is advanced by the specified duration."""
|
2015-07-02 04:22:53 +08:00
|
|
|
self.on()
|
|
|
|
delay_mu(duration)
|
|
|
|
self.off()
|
2015-05-02 10:35:21 +08:00
|
|
|
|
|
|
|
@kernel
|
|
|
|
def pulse(self, duration):
|
2019-01-12 20:01:55 +08:00
|
|
|
"""Pulse the output high for the specified duration
|
2016-06-12 13:05:54 +08:00
|
|
|
(in seconds).
|
|
|
|
|
|
|
|
The time cursor is advanced by the specified duration."""
|
2015-05-02 10:35:21 +08:00
|
|
|
self.on()
|
|
|
|
delay(duration)
|
|
|
|
self.off()
|
2015-04-14 19:44:45 +08:00
|
|
|
|
2016-09-07 16:55:21 +08:00
|
|
|
# Input API: gating
|
2015-04-14 19:44:45 +08:00
|
|
|
@kernel
|
|
|
|
def _set_sensitivity(self, value):
|
2018-11-08 18:29:24 +08:00
|
|
|
rtio_output(self.target_sens, value)
|
2015-07-02 04:22:53 +08:00
|
|
|
|
|
|
|
@kernel
|
|
|
|
def gate_rising_mu(self, duration):
|
|
|
|
"""Register rising edge events for the specified duration
|
2016-06-12 13:05:54 +08:00
|
|
|
(in machine units).
|
|
|
|
|
2018-07-23 22:34:18 +08:00
|
|
|
The time cursor is advanced by the specified duration.
|
|
|
|
|
2018-10-26 23:08:08 +08:00
|
|
|
:return: The timeline cursor at the end of the gate window, for
|
|
|
|
convenience when used with :meth:`count`/:meth:`timestamp_mu`.
|
2018-07-23 22:34:18 +08:00
|
|
|
"""
|
2015-07-02 04:22:53 +08:00
|
|
|
self._set_sensitivity(1)
|
|
|
|
delay_mu(duration)
|
|
|
|
self._set_sensitivity(0)
|
2018-07-23 22:34:18 +08:00
|
|
|
return now_mu()
|
2015-07-02 04:22:53 +08:00
|
|
|
|
|
|
|
@kernel
|
|
|
|
def gate_falling_mu(self, duration):
|
|
|
|
"""Register falling edge events for the specified duration
|
2016-06-12 13:05:54 +08:00
|
|
|
(in machine units).
|
|
|
|
|
2018-07-23 22:34:18 +08:00
|
|
|
The time cursor is advanced by the specified duration.
|
|
|
|
|
2018-10-26 23:08:08 +08:00
|
|
|
:return: The timeline cursor at the end of the gate window, for
|
|
|
|
convenience when used with :meth:`count`/:meth:`timestamp_mu`.
|
2018-07-23 22:34:18 +08:00
|
|
|
"""
|
2015-07-02 04:22:53 +08:00
|
|
|
self._set_sensitivity(2)
|
|
|
|
delay_mu(duration)
|
|
|
|
self._set_sensitivity(0)
|
2018-07-23 22:34:18 +08:00
|
|
|
return now_mu()
|
2015-07-02 04:22:53 +08:00
|
|
|
|
|
|
|
@kernel
|
|
|
|
def gate_both_mu(self, duration):
|
|
|
|
"""Register both rising and falling edge events for the specified
|
2016-06-12 13:05:54 +08:00
|
|
|
duration (in machine units).
|
|
|
|
|
2018-07-23 22:34:18 +08:00
|
|
|
The time cursor is advanced by the specified duration.
|
|
|
|
|
2018-10-26 23:08:08 +08:00
|
|
|
:return: The timeline cursor at the end of the gate window, for
|
|
|
|
convenience when used with :meth:`count`/:meth:`timestamp_mu`.
|
2018-07-23 22:34:18 +08:00
|
|
|
"""
|
2015-07-02 04:22:53 +08:00
|
|
|
self._set_sensitivity(3)
|
|
|
|
delay_mu(duration)
|
|
|
|
self._set_sensitivity(0)
|
2018-07-23 22:34:18 +08:00
|
|
|
return now_mu()
|
2014-09-13 19:37:57 +08:00
|
|
|
|
|
|
|
@kernel
|
2014-09-30 16:42:07 +08:00
|
|
|
def gate_rising(self, duration):
|
2015-07-02 04:22:53 +08:00
|
|
|
"""Register rising edge events for the specified duration
|
2016-06-12 13:05:54 +08:00
|
|
|
(in seconds).
|
|
|
|
|
2018-07-23 22:34:18 +08:00
|
|
|
The time cursor is advanced by the specified duration.
|
|
|
|
|
2018-10-26 23:08:08 +08:00
|
|
|
:return: The timeline cursor at the end of the gate window, for
|
|
|
|
convenience when used with :meth:`count`/:meth:`timestamp_mu`.
|
2018-07-23 22:34:18 +08:00
|
|
|
"""
|
2015-04-14 19:44:45 +08:00
|
|
|
self._set_sensitivity(1)
|
2014-09-15 22:48:22 +08:00
|
|
|
delay(duration)
|
2015-04-14 19:44:45 +08:00
|
|
|
self._set_sensitivity(0)
|
2018-07-23 22:34:18 +08:00
|
|
|
return now_mu()
|
2014-09-15 22:48:22 +08:00
|
|
|
|
|
|
|
@kernel
|
2014-09-30 16:42:07 +08:00
|
|
|
def gate_falling(self, duration):
|
2015-07-02 04:22:53 +08:00
|
|
|
"""Register falling edge events for the specified duration
|
2016-06-12 13:05:54 +08:00
|
|
|
(in seconds).
|
|
|
|
|
2018-07-23 22:34:18 +08:00
|
|
|
The time cursor is advanced by the specified duration.
|
|
|
|
|
2018-10-26 23:08:08 +08:00
|
|
|
:return: The timeline cursor at the end of the gate window, for
|
|
|
|
convenience when used with :meth:`count`/:meth:`timestamp_mu`.
|
|
|
|
|
2018-07-23 22:34:18 +08:00
|
|
|
"""
|
2015-04-14 19:44:45 +08:00
|
|
|
self._set_sensitivity(2)
|
2014-09-15 22:48:22 +08:00
|
|
|
delay(duration)
|
2015-04-14 19:44:45 +08:00
|
|
|
self._set_sensitivity(0)
|
2018-07-23 22:34:18 +08:00
|
|
|
return now_mu()
|
2014-09-15 22:48:22 +08:00
|
|
|
|
|
|
|
@kernel
|
2015-07-29 06:14:26 +08:00
|
|
|
def gate_both(self, duration):
|
2014-09-30 16:42:07 +08:00
|
|
|
"""Register both rising and falling edge events for the specified
|
2016-06-12 13:05:54 +08:00
|
|
|
duration (in seconds).
|
|
|
|
|
2018-07-23 22:34:18 +08:00
|
|
|
The time cursor is advanced by the specified duration.
|
|
|
|
|
2018-10-26 23:08:08 +08:00
|
|
|
:return: The timeline cursor at the end of the gate window, for
|
|
|
|
convenience when used with :meth:`count`/:meth:`timestamp_mu`.
|
2018-07-23 22:34:18 +08:00
|
|
|
"""
|
2015-04-14 19:44:45 +08:00
|
|
|
self._set_sensitivity(3)
|
2014-09-15 22:48:22 +08:00
|
|
|
delay(duration)
|
2015-04-14 19:44:45 +08:00
|
|
|
self._set_sensitivity(0)
|
2018-07-23 22:34:18 +08:00
|
|
|
return now_mu()
|
2014-10-21 23:14:01 +08:00
|
|
|
|
2014-09-13 19:37:57 +08:00
|
|
|
@kernel
|
2018-07-23 22:34:18 +08:00
|
|
|
def count(self, up_to_timestamp_mu):
|
2018-10-26 23:08:08 +08:00
|
|
|
"""Consume RTIO input events until the hardware timestamp counter has
|
|
|
|
reached the specified timestamp and return the number of observed
|
|
|
|
events.
|
|
|
|
|
|
|
|
This function does not interact with the timeline cursor.
|
|
|
|
|
|
|
|
See the ``gate_*()`` family of methods to select the input transitions
|
|
|
|
that generate events, and :meth:`timestamp_mu` to obtain the timestamp
|
|
|
|
of the first event rather than an accumulated count.
|
|
|
|
|
|
|
|
:param up_to_timestamp_mu: The timestamp up to which execution is
|
|
|
|
blocked, that is, up to which input events are guaranteed to be
|
|
|
|
taken into account. (Events with later timestamps might still be
|
|
|
|
registered if they are already available.)
|
|
|
|
|
|
|
|
:return: The number of events before the timeout elapsed (0 if none
|
|
|
|
observed).
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
To count events on channel ``ttl_input``, up to the current timeline
|
|
|
|
position::
|
|
|
|
|
|
|
|
ttl_input.count(now_mu())
|
|
|
|
|
|
|
|
If other events are scheduled between the end of the input gate
|
|
|
|
period and when the number of events is counted, using ``now_mu()``
|
|
|
|
as timeout consumes an unnecessary amount of timeline slack. In
|
|
|
|
such cases, it can be beneficial to pass a more precise timestamp,
|
|
|
|
for example::
|
2016-06-12 13:05:54 +08:00
|
|
|
|
2018-10-26 23:08:08 +08:00
|
|
|
gate_end_mu = ttl_input.gate_rising(100 * us)
|
|
|
|
|
|
|
|
# Schedule a long pulse sequence, represented here by a delay.
|
|
|
|
delay(10 * ms)
|
|
|
|
|
|
|
|
# Get number of rising edges. This will block until the end of
|
|
|
|
# the gate window, but does not wait for the long pulse sequence
|
|
|
|
# afterwards, thus (likely) completing with a large amount of
|
|
|
|
# slack left.
|
|
|
|
num_rising_edges = ttl_input.count(gate_end_mu)
|
|
|
|
|
|
|
|
The ``gate_*()`` family of methods return the cursor at the end
|
|
|
|
of the window, allowing this to be expressed in a compact fashion::
|
|
|
|
|
|
|
|
ttl_input.count(ttl_input.gate_rising(100 * us))
|
|
|
|
"""
|
2014-09-15 22:48:22 +08:00
|
|
|
count = 0
|
2018-11-17 21:45:45 +08:00
|
|
|
while rtio_input_timestamp(up_to_timestamp_mu + self.gate_latency_mu, self.channel) >= 0:
|
2014-09-15 22:48:22 +08:00
|
|
|
count += 1
|
|
|
|
return count
|
2014-10-14 15:54:10 +08:00
|
|
|
|
|
|
|
@kernel
|
2018-07-23 22:34:18 +08:00
|
|
|
def timestamp_mu(self, up_to_timestamp_mu):
|
2018-10-26 23:08:08 +08:00
|
|
|
"""Return the timestamp of the next RTIO input event, or -1 if the
|
|
|
|
hardware timestamp counter reaches the given value before an event is
|
|
|
|
received.
|
|
|
|
|
|
|
|
This function does not interact with the timeline cursor.
|
2014-10-14 15:54:10 +08:00
|
|
|
|
2018-10-26 23:08:08 +08:00
|
|
|
See the ``gate_*()`` family of methods to select the input transitions
|
|
|
|
that generate events, and :meth:`count` for usage examples.
|
2016-06-12 13:05:54 +08:00
|
|
|
|
2018-10-26 23:08:08 +08:00
|
|
|
:param up_to_timestamp_mu: The timestamp up to which execution is
|
|
|
|
blocked, that is, up to which input events are guaranteed to be
|
|
|
|
taken into account. (Events with later timestamps might still be
|
|
|
|
registered if they are already available.)
|
|
|
|
|
|
|
|
:return: The timestamp (in machine units) of the first event received;
|
|
|
|
-1 on timeout.
|
|
|
|
"""
|
2018-11-17 21:45:45 +08:00
|
|
|
return rtio_input_timestamp(up_to_timestamp_mu + self.gate_latency_mu, self.channel)
|
2015-07-05 00:36:01 +08:00
|
|
|
|
2016-09-07 16:55:21 +08:00
|
|
|
# Input API: sampling
|
|
|
|
@kernel
|
|
|
|
def sample_input(self):
|
|
|
|
"""Instructs the RTIO core to read the value of the TTL input at the
|
|
|
|
position of the time cursor.
|
|
|
|
|
|
|
|
The time cursor is not modified by this function."""
|
2018-11-08 18:29:24 +08:00
|
|
|
rtio_output(self.target_sample, 0)
|
2016-09-07 16:55:21 +08:00
|
|
|
|
|
|
|
@kernel
|
|
|
|
def sample_get(self):
|
|
|
|
"""Returns the value of a sample previously obtained with
|
2018-09-26 12:12:37 +08:00
|
|
|
:meth:`sample_input`.
|
2016-09-07 16:55:21 +08:00
|
|
|
|
|
|
|
Multiple samples may be queued (using multiple calls to
|
2018-09-26 12:12:37 +08:00
|
|
|
:meth:`sample_input`) into the RTIO FIFOs and subsequently read out using
|
2016-09-07 16:55:21 +08:00
|
|
|
multiple calls to this function.
|
|
|
|
|
|
|
|
This function does not interact with the time cursor."""
|
|
|
|
return rtio_input_data(self.channel)
|
|
|
|
|
|
|
|
@kernel
|
|
|
|
def sample_get_nonrt(self):
|
|
|
|
"""Convenience function that obtains the value of a sample
|
|
|
|
at the position of the time cursor, breaks realtime, and
|
|
|
|
returns the sample value."""
|
|
|
|
self.sample_input()
|
|
|
|
r = self.sample_get()
|
|
|
|
self.core.break_realtime()
|
|
|
|
return r
|
|
|
|
|
|
|
|
# Input API: watching
|
|
|
|
@kernel
|
|
|
|
def watch_stay_on(self):
|
|
|
|
"""Checks that the input is at a high level at the position
|
2018-09-26 12:12:37 +08:00
|
|
|
of the time cursor and keep checking until :meth:`watch_done`
|
2016-09-07 16:55:21 +08:00
|
|
|
is called.
|
|
|
|
|
|
|
|
Returns ``True`` if the input is high. A call to this function
|
2018-09-26 12:12:37 +08:00
|
|
|
must always be followed by an eventual call to :meth:`watch_done`
|
2016-09-07 16:55:21 +08:00
|
|
|
(use e.g. a try/finally construct to ensure this).
|
|
|
|
|
|
|
|
The time cursor is not modified by this function.
|
|
|
|
"""
|
2018-11-08 18:29:24 +08:00
|
|
|
rtio_output(self.target_sample, 2) # gate falling
|
2016-09-07 16:55:21 +08:00
|
|
|
return rtio_input_data(self.channel) == 1
|
|
|
|
|
|
|
|
@kernel
|
|
|
|
def watch_stay_off(self):
|
2018-09-26 12:12:37 +08:00
|
|
|
"""Like :meth:`watch_stay_on`, but for low levels."""
|
2018-11-08 18:29:24 +08:00
|
|
|
rtio_output(self.target_sample, 1) # gate rising
|
2016-09-07 16:55:21 +08:00
|
|
|
return rtio_input_data(self.channel) == 0
|
|
|
|
|
|
|
|
@kernel
|
|
|
|
def watch_done(self):
|
|
|
|
"""Stop watching the input at the position of the time cursor.
|
|
|
|
|
|
|
|
Returns ``True`` if the input has not changed state while it
|
|
|
|
was being watched.
|
|
|
|
|
|
|
|
The time cursor is not modified by this function. This function
|
|
|
|
always makes the slack negative.
|
|
|
|
"""
|
2018-11-08 18:29:24 +08:00
|
|
|
rtio_output(self.target_sens, 0)
|
2016-09-07 16:55:21 +08:00
|
|
|
success = True
|
|
|
|
try:
|
2018-11-17 21:45:45 +08:00
|
|
|
while rtio_input_timestamp(now_mu() + self.gate_latency_mu, self.channel) != -1:
|
2016-09-07 16:55:21 +08:00
|
|
|
success = False
|
|
|
|
except RTIOOverflow:
|
|
|
|
success = False
|
|
|
|
return success
|
|
|
|
|
2015-07-05 00:36:01 +08:00
|
|
|
|
2015-07-14 04:08:20 +08:00
|
|
|
class TTLClockGen:
|
2015-07-05 00:36:01 +08:00
|
|
|
"""RTIO TTL clock generator driver.
|
|
|
|
|
|
|
|
This should be used with TTL channels that have a clock generator
|
|
|
|
built into the gateware (not compatible with regular TTL channels).
|
|
|
|
|
2016-06-12 13:05:54 +08:00
|
|
|
The time cursor is not modified by any function in this class.
|
|
|
|
|
2015-07-05 00:36:01 +08:00
|
|
|
:param channel: channel number
|
2018-10-25 17:52:00 +08:00
|
|
|
:param acc_width: accumulator width in bits
|
2015-07-05 00:36:01 +08:00
|
|
|
"""
|
2018-11-07 23:41:43 +08:00
|
|
|
kernel_invariants = {"core", "channel", "target", "acc_width"}
|
2016-04-03 02:20:51 +08:00
|
|
|
|
2018-10-25 17:52:00 +08:00
|
|
|
def __init__(self, dmgr, channel, acc_width=24, core_device="core"):
|
2016-03-09 13:01:34 +08:00
|
|
|
self.core = dmgr.get(core_device)
|
2015-07-14 04:08:20 +08:00
|
|
|
self.channel = channel
|
2018-11-07 23:41:43 +08:00
|
|
|
self.target = channel << 8
|
2015-07-05 00:36:01 +08:00
|
|
|
|
2018-10-25 17:52:00 +08:00
|
|
|
self.acc_width = numpy.int64(acc_width)
|
2015-07-05 00:36:01 +08:00
|
|
|
|
2022-12-02 16:27:03 +08:00
|
|
|
@staticmethod
|
|
|
|
def get_rtio_channels(channel, **kwargs):
|
|
|
|
return [(channel, None)]
|
|
|
|
|
2015-07-05 00:36:01 +08:00
|
|
|
@portable
|
|
|
|
def frequency_to_ftw(self, frequency):
|
|
|
|
"""Returns the frequency tuning word corresponding to the given
|
|
|
|
frequency.
|
|
|
|
"""
|
2016-03-04 16:59:59 +08:00
|
|
|
return round(2**self.acc_width*frequency*self.core.coarse_ref_period)
|
2015-07-05 00:36:01 +08:00
|
|
|
|
|
|
|
@portable
|
|
|
|
def ftw_to_frequency(self, ftw):
|
|
|
|
"""Returns the frequency corresponding to the given frequency tuning
|
|
|
|
word.
|
|
|
|
"""
|
2016-03-04 16:59:59 +08:00
|
|
|
return ftw/self.core.coarse_ref_period/2**self.acc_width
|
2015-07-05 00:36:01 +08:00
|
|
|
|
|
|
|
@kernel
|
|
|
|
def set_mu(self, frequency):
|
2016-06-12 13:05:54 +08:00
|
|
|
"""Set the frequency of the clock, in machine units, at the current
|
|
|
|
position of the time cursor.
|
2015-07-05 00:36:01 +08:00
|
|
|
|
|
|
|
This also sets the phase, as the time of the first generated rising
|
|
|
|
edge corresponds to the time of the call.
|
|
|
|
|
|
|
|
The clock generator contains a 24-bit phase accumulator operating on
|
|
|
|
the RTIO clock. At each RTIO clock tick, the frequency tuning word is
|
|
|
|
added to the phase accumulator. The most significant bit of the phase
|
|
|
|
accumulator is connected to the TTL line. Setting the frequency tuning
|
|
|
|
word has the additional effect of setting the phase accumulator to
|
|
|
|
0x800000.
|
2015-07-06 01:07:13 +08:00
|
|
|
|
|
|
|
Due to the way the clock generator operates, frequency tuning words
|
|
|
|
that are not powers of two cause jitter of one RTIO clock cycle at the
|
2016-06-12 13:05:54 +08:00
|
|
|
output."""
|
2018-11-08 18:29:24 +08:00
|
|
|
rtio_output(self.target, frequency)
|
2015-07-05 00:36:01 +08:00
|
|
|
|
|
|
|
@kernel
|
|
|
|
def set(self, frequency):
|
2018-09-26 12:12:37 +08:00
|
|
|
"""Like :meth:`set_mu`, but using Hz."""
|
2015-07-05 00:36:01 +08:00
|
|
|
self.set_mu(self.frequency_to_ftw(frequency))
|
|
|
|
|
|
|
|
@kernel
|
|
|
|
def stop(self):
|
|
|
|
"""Stop the toggling of the clock and set the output level to 0."""
|
|
|
|
self.set_mu(0)
|