Skip to content

Commit 486fe97

Browse files
committedSep 7, 2016
ttl: add level-based APIs (#218)
1 parent a7dd356 commit 486fe97

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed
 

‎artiq/coredevice/ttl.py

+85
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@
55
from artiq.coredevice.rtio import rtio_output, rtio_input_timestamp
66

77

8+
# RTIO TTL address map:
9+
# 0 Output level
10+
# 1 Output enable
11+
# 2 Set input sensitivity
12+
# 3 Set input sensitivity and sample
13+
14+
815
class TTLOut:
916
"""RTIO TTL output driver.
1017
@@ -94,6 +101,10 @@ class TTLInOut:
94101
the time in your setup, it is a good idea to call ``output`` in the
95102
startup kernel.
96103
104+
There are three input APIs: gating, sampling and watching. When one
105+
API is active (e.g. the gate is open, or the input events have not been
106+
fully read out), another API must not be used simultaneously.
107+
97108
:param channel: channel number
98109
"""
99110
kernel_invariants = {"core", "channel"}
@@ -105,6 +116,7 @@ def __init__(self, dmgr, channel, core_device="core"):
105116
# in RTIO cycles
106117
self.o_previous_timestamp = numpy.int64(0)
107118
self.i_previous_timestamp = numpy.int64(0)
119+
self.queued_samples = 0
108120

109121
@kernel
110122
def set_oe(self, oe):
@@ -180,6 +192,7 @@ def pulse(self, duration):
180192
delay(duration)
181193
self.off()
182194

195+
# Input API: gating
183196
@kernel
184197
def _set_sensitivity(self, value):
185198
rtio_output(now_mu(), self.channel, 2, value)
@@ -266,6 +279,78 @@ def timestamp_mu(self):
266279
This function does not interact with the time cursor."""
267280
return rtio_input_timestamp(self.i_previous_timestamp, self.channel)
268281

282+
# Input API: sampling
283+
@kernel
284+
def sample_input(self):
285+
"""Instructs the RTIO core to read the value of the TTL input at the
286+
position of the time cursor.
287+
288+
The time cursor is not modified by this function."""
289+
rtio_output(now_mu(), self.channel, 3, 0)
290+
291+
@kernel
292+
def sample_get(self):
293+
"""Returns the value of a sample previously obtained with
294+
``sample_input``.
295+
296+
Multiple samples may be queued (using multiple calls to
297+
``sample_input``) into the RTIO FIFOs and subsequently read out using
298+
multiple calls to this function.
299+
300+
This function does not interact with the time cursor."""
301+
return rtio_input_data(self.channel)
302+
303+
@kernel
304+
def sample_get_nonrt(self):
305+
"""Convenience function that obtains the value of a sample
306+
at the position of the time cursor, breaks realtime, and
307+
returns the sample value."""
308+
self.sample_input()
309+
r = self.sample_get()
310+
self.core.break_realtime()
311+
return r
312+
313+
# Input API: watching
314+
@kernel
315+
def watch_stay_on(self):
316+
"""Checks that the input is at a high level at the position
317+
of the time cursor and keep checking until ``watch_done``
318+
is called.
319+
320+
Returns ``True`` if the input is high. A call to this function
321+
must always be followed by an eventual call to ``watch_done``
322+
(use e.g. a try/finally construct to ensure this).
323+
324+
The time cursor is not modified by this function.
325+
"""
326+
rtio_output(now_mu(), self.channel, 3, 2) # gate falling
327+
return rtio_input_data(self.channel) == 1
328+
329+
@kernel
330+
def watch_stay_off(self):
331+
"""Like ``watch_stay_on``, but for low levels."""
332+
rtio_output(now_mu(), self.channel, 3, 1) # gate rising
333+
return rtio_input_data(self.channel) == 0
334+
335+
@kernel
336+
def watch_done(self):
337+
"""Stop watching the input at the position of the time cursor.
338+
339+
Returns ``True`` if the input has not changed state while it
340+
was being watched.
341+
342+
The time cursor is not modified by this function. This function
343+
always makes the slack negative.
344+
"""
345+
rtio_output(now_mu(), self.channel, 2, 0)
346+
success = True
347+
try:
348+
while rtio_input_timestamp(now_mu(), self.channel) != -1:
349+
success = False
350+
except RTIOOverflow:
351+
success = False
352+
return success
353+
269354

270355
class TTLClockGen:
271356
"""RTIO TTL clock generator driver.

0 commit comments

Comments
 (0)
Please sign in to comment.