2014-09-12 15:28:02 +08:00
|
|
|
from artiq.language.core import *
|
2015-01-12 18:51:23 +08:00
|
|
|
from artiq.language.db import *
|
2014-09-12 15:28:02 +08:00
|
|
|
|
|
|
|
|
2015-05-02 10:35:21 +08:00
|
|
|
class LLTTLOut(AutoDB):
|
|
|
|
"""Low-level RTIO TTL output driver.
|
2014-10-17 00:12:53 +08:00
|
|
|
|
2015-05-02 10:35:21 +08:00
|
|
|
Allows setting RTIO TTL outputs at arbitrary times, without time
|
|
|
|
unit conversion.
|
2014-10-17 00:12:53 +08:00
|
|
|
|
|
|
|
This is meant to be used mostly in drivers; consider using
|
2015-05-02 10:35:21 +08:00
|
|
|
``TTLOut`` instead.
|
|
|
|
|
|
|
|
This should be used with output-only channels.
|
2014-10-17 00:12:53 +08:00
|
|
|
"""
|
2015-01-12 18:51:23 +08:00
|
|
|
class DBKeys:
|
2015-03-08 18:37:53 +08:00
|
|
|
core = Device()
|
2015-01-12 18:51:23 +08:00
|
|
|
channel = Argument()
|
2014-10-17 00:12:53 +08:00
|
|
|
|
|
|
|
@kernel
|
2015-04-14 19:44:45 +08:00
|
|
|
def set_o(self, t, value):
|
|
|
|
"""Sets the output value of the RTIO channel.
|
2014-10-17 00:12:53 +08:00
|
|
|
|
|
|
|
:param t: timestamp in RTIO cycles (64-bit integer).
|
|
|
|
:param value: value to set at the output.
|
|
|
|
"""
|
2015-05-08 16:20:12 +08:00
|
|
|
syscall("ttl_set_o", t, self.channel, value)
|
2014-10-17 00:12:53 +08:00
|
|
|
|
|
|
|
@kernel
|
|
|
|
def on(self, t):
|
|
|
|
"""Turns the RTIO channel on.
|
|
|
|
|
|
|
|
:param t: timestamp in RTIO cycles (64-bit integer).
|
|
|
|
"""
|
2015-05-02 10:35:21 +08:00
|
|
|
self.set_o(t, True)
|
2014-10-17 00:12:53 +08:00
|
|
|
|
|
|
|
@kernel
|
|
|
|
def off(self, t):
|
|
|
|
"""Turns the RTIO channel off.
|
|
|
|
|
|
|
|
:param t: timestamp in RTIO cycles (64-bit integer).
|
|
|
|
"""
|
2015-05-02 10:35:21 +08:00
|
|
|
self.set_o(t, False)
|
2014-12-01 17:32:36 +08:00
|
|
|
|
2014-09-30 16:42:07 +08:00
|
|
|
|
2015-05-02 10:35:21 +08:00
|
|
|
class TTLOut(AutoDB):
|
|
|
|
"""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 core: core device
|
|
|
|
:param channel: channel number
|
|
|
|
"""
|
2015-04-14 19:44:45 +08:00
|
|
|
class DBKeys:
|
|
|
|
core = Device()
|
|
|
|
channel = Argument()
|
|
|
|
|
|
|
|
|
2015-05-02 10:35:21 +08:00
|
|
|
def build(self):
|
|
|
|
# in RTIO cycles
|
|
|
|
self.o_previous_timestamp = int64(0)
|
2015-04-14 19:44:45 +08:00
|
|
|
|
|
|
|
@kernel
|
2015-05-02 10:35:21 +08:00
|
|
|
def _set_o(self, o):
|
2015-05-08 16:20:12 +08:00
|
|
|
syscall("ttl_set_o", time_to_cycles(now()), self.channel, o)
|
2015-05-02 10:35:21 +08:00
|
|
|
self.o_previous_timestamp = time_to_cycles(now())
|
2014-09-15 22:48:22 +08:00
|
|
|
|
2014-09-12 15:28:02 +08:00
|
|
|
@kernel
|
|
|
|
def sync(self):
|
2015-05-02 10:35:21 +08:00
|
|
|
"""Busy-waits until all programmed level switches have been effected."""
|
|
|
|
while syscall("rtio_get_counter") < self.o_previous_timestamp:
|
2014-11-30 00:13:54 +08:00
|
|
|
pass
|
2014-09-12 15:28:02 +08:00
|
|
|
|
|
|
|
@kernel
|
|
|
|
def on(self):
|
2015-04-14 19:44:45 +08:00
|
|
|
"""Sets the output to a logic high state."""
|
2015-05-02 10:35:21 +08:00
|
|
|
self._set_o(True)
|
2014-09-12 15:28:02 +08:00
|
|
|
|
|
|
|
@kernel
|
|
|
|
def off(self):
|
2015-04-14 19:44:45 +08:00
|
|
|
"""Sets the output to a logic low state."""
|
2015-05-02 10:35:21 +08:00
|
|
|
self._set_o(False)
|
2014-09-12 15:28:02 +08:00
|
|
|
|
|
|
|
@kernel
|
|
|
|
def pulse(self, duration):
|
2015-04-15 13:53:00 +08:00
|
|
|
"""Pulses the output high for 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-05-02 10:35:21 +08:00
|
|
|
class TTLInOut(AutoDB):
|
|
|
|
"""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
|
|
|
|
|
|
|
:param core: core device
|
|
|
|
:param channel: channel number
|
|
|
|
"""
|
2015-04-14 19:44:45 +08:00
|
|
|
class DBKeys:
|
|
|
|
core = Device()
|
|
|
|
channel = Argument()
|
|
|
|
|
2014-09-15 22:48:22 +08:00
|
|
|
def build(self):
|
2015-05-02 10:35:21 +08:00
|
|
|
# in RTIO cycles
|
|
|
|
self.o_previous_timestamp = int64(0)
|
|
|
|
self.i_previous_timestamp = int64(0)
|
2015-04-14 19:44:45 +08:00
|
|
|
|
|
|
|
@kernel
|
2015-05-02 10:35:21 +08:00
|
|
|
def _set_oe(self, oe):
|
2015-05-08 16:20:12 +08:00
|
|
|
syscall("ttl_set_oe", time_to_cycles(now()), self.channel, oe)
|
2015-05-02 10:35:21 +08:00
|
|
|
|
|
|
|
@kernel
|
|
|
|
def output(self):
|
|
|
|
self._set_oe(True)
|
|
|
|
|
|
|
|
@kernel
|
|
|
|
def input(self):
|
|
|
|
self._set_oe(False)
|
|
|
|
|
|
|
|
@kernel
|
|
|
|
def _set_o(self, o):
|
2015-05-08 16:20:12 +08:00
|
|
|
syscall("ttl_set_o", time_to_cycles(now()), self.channel, o)
|
2015-05-02 10:35:21 +08:00
|
|
|
self.o_previous_timestamp = time_to_cycles(now())
|
|
|
|
|
|
|
|
@kernel
|
|
|
|
def sync(self):
|
|
|
|
"""Busy-waits until all programmed level switches have been effected."""
|
|
|
|
while syscall("rtio_get_counter") < self.o_previous_timestamp:
|
|
|
|
pass
|
|
|
|
|
|
|
|
@kernel
|
|
|
|
def on(self):
|
|
|
|
"""Sets the output to a logic high state."""
|
|
|
|
self._set_o(True)
|
|
|
|
|
|
|
|
@kernel
|
|
|
|
def off(self):
|
|
|
|
"""Sets the output to a logic low state."""
|
|
|
|
self._set_o(False)
|
|
|
|
|
|
|
|
@kernel
|
|
|
|
def pulse(self, duration):
|
|
|
|
"""Pulses the output high for the specified duration."""
|
|
|
|
self.on()
|
|
|
|
delay(duration)
|
|
|
|
self.off()
|
2015-04-14 19:44:45 +08:00
|
|
|
|
|
|
|
@kernel
|
|
|
|
def _set_sensitivity(self, value):
|
2015-05-08 16:20:12 +08:00
|
|
|
syscall("ttl_set_sensitivity", time_to_cycles(now()), self.channel, value)
|
2015-05-02 10:35:21 +08:00
|
|
|
self.i_previous_timestamp = time_to_cycles(now())
|
2014-09-13 19:37:57 +08:00
|
|
|
|
|
|
|
@kernel
|
2014-09-30 16:42:07 +08:00
|
|
|
def gate_rising(self, duration):
|
2015-04-15 13:53:00 +08:00
|
|
|
"""Register rising edge events for the specified duration."""
|
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)
|
2014-09-15 22:48:22 +08:00
|
|
|
|
|
|
|
@kernel
|
2014-09-30 16:42:07 +08:00
|
|
|
def gate_falling(self, duration):
|
2015-04-15 13:53:00 +08:00
|
|
|
"""Register falling edge events for the specified duration."""
|
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)
|
2014-09-15 22:48:22 +08:00
|
|
|
|
|
|
|
@kernel
|
2014-09-30 16:42:07 +08:00
|
|
|
def gate_both(self, duration):
|
|
|
|
"""Register both rising and falling edge events for the specified
|
2015-04-15 13:53:00 +08:00
|
|
|
duration."""
|
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)
|
2014-10-21 23:14:01 +08:00
|
|
|
|
2014-09-13 19:37:57 +08:00
|
|
|
@kernel
|
2014-09-30 16:42:07 +08:00
|
|
|
def count(self):
|
|
|
|
"""Poll the RTIO input during all the previously programmed gate
|
2015-04-15 13:53:00 +08:00
|
|
|
openings, and returns the number of registered events."""
|
2014-09-15 22:48:22 +08:00
|
|
|
count = 0
|
2015-05-08 16:20:12 +08:00
|
|
|
while syscall("ttl_get", self.channel,
|
2015-05-02 10:35:21 +08:00
|
|
|
self.i_previous_timestamp) >= 0:
|
2014-09-15 22:48:22 +08:00
|
|
|
count += 1
|
|
|
|
return count
|
2014-10-14 15:54:10 +08:00
|
|
|
|
|
|
|
@kernel
|
|
|
|
def timestamp(self):
|
|
|
|
"""Poll the RTIO input and returns an event timestamp, according to
|
|
|
|
the gating.
|
|
|
|
|
|
|
|
If the gate is permanently closed, returns a negative value.
|
|
|
|
"""
|
2015-05-08 16:20:12 +08:00
|
|
|
return cycles_to_time(syscall("ttl_get", self.channel,
|
2015-05-02 10:35:21 +08:00
|
|
|
self.i_previous_timestamp))
|