artiq/doc/manual/getting_started_core.rst

240 lines
13 KiB
ReStructuredText
Raw Normal View History

Getting started with the core language
======================================
2014-09-29 14:50:29 +08:00
.. _connecting-to-the-core-device:
2014-09-29 14:50:29 +08:00
Connecting to the core device
-----------------------------
As a very first step, we will turn on a LED on the core device. Create a file ``led.py`` containing the following: ::
from artiq.experiment import *
2014-12-03 18:20:30 +08:00
2014-09-29 14:50:29 +08:00
2015-07-14 04:08:20 +08:00
class LED(EnvExperiment):
def build(self):
2015-10-04 00:18:21 +08:00
self.setattr_device("core")
self.setattr_device("led")
2014-09-29 14:50:29 +08:00
2014-09-30 18:10:40 +08:00
@kernel
def run(self):
2016-06-29 11:48:33 +08:00
self.core.reset()
self.led.on()
2014-09-29 14:50:29 +08:00
The central part of our code is our ``LED`` class, which derives from :class:`artiq.language.environment.EnvExperiment`. Among other features, :class:`~artiq.language.environment.EnvExperiment` calls our :meth:`~artiq.language.environment.Experiment.build` method and provides the :meth:`~artiq.language.environment.HasEnvironment.setattr_device` method that interfaces to the device database to create the appropriate device drivers and make those drivers accessible as ``self.core`` and ``self.led``. The :func:`~artiq.language.core.kernel` decorator (``@kernel``) tells the system that the :meth:`~artiq.language.environment.Experiment.run` method must be compiled for and executed on the core device (instead of being interpreted and executed as regular Python code on the host). The decorator uses ``self.core`` internally, which is why we request the core device using :meth:`~artiq.language.environment.HasEnvironment.setattr_device` like any other.
2014-12-03 18:20:30 +08:00
Copy the file ``device_db.py`` (containing the device database) from the ``examples/master`` folder of ARTIQ into the same directory as ``led.py`` (alternatively, you can use the ``--device-db`` option of ``artiq_run``). You will probably want to set the IP address of the core device in ``device_db.py`` so that the computer can connect to it (it is the ``host`` parameter of the ``comm`` entry). See :ref:`device-db` for more information. The example device database is designed for the ``nist_clock`` hardware adapter on the KC705; see :ref:`board-ports` for RTIO channel assignments if you need to adapt the device database to a different hardware platform.
.. note::
To obtain the examples, you can find where the ARTIQ package is installed on your machine with: ::
python3 -c "import artiq; print(artiq.__path__[0])"
2015-07-14 04:08:20 +08:00
Run your code using ``artiq_run``, which is part of the ARTIQ front-end tools: ::
2014-09-29 14:50:29 +08:00
2015-07-14 04:08:20 +08:00
$ artiq_run led.py
2014-09-29 14:50:29 +08:00
The process should terminate quietly and the LED of the device should turn on. Congratulations! You have a basic ARTIQ system up and running.
2014-09-29 14:50:29 +08:00
Host/core device interaction (RPC)
----------------------------------
2014-09-29 14:50:29 +08:00
A method or function running on the core device (which we call a "kernel") may communicate with the host by calling non-kernel functions that may accept parameters and may return a value. The "remote procedure call" (RPC) mechanisms handle automatically the communication between the host and the device of which function to call, with which parameters, and what the returned value is.
Modify the code as follows: ::
def input_led_state() -> TBool:
2016-06-04 10:56:12 +08:00
return input("Enter desired LED state: ") == "1"
2014-09-29 14:50:29 +08:00
2015-07-14 04:08:20 +08:00
class LED(EnvExperiment):
def build(self):
2015-10-04 00:18:21 +08:00
self.setattr_device("core")
self.setattr_device("led")
2014-09-29 14:50:29 +08:00
2014-09-30 18:10:40 +08:00
@kernel
def run(self):
2016-06-29 11:48:33 +08:00
self.core.reset()
2015-04-17 10:43:50 +08:00
s = input_led_state()
2015-05-03 20:42:42 +08:00
self.core.break_realtime()
2015-04-17 10:43:50 +08:00
if s:
self.led.on()
else:
self.led.off()
2014-09-29 14:50:29 +08:00
2015-04-17 10:43:50 +08:00
2014-09-29 14:50:29 +08:00
You can then turn the LED off and on by entering 0 or 1 at the prompt that appears: ::
2015-07-14 04:08:20 +08:00
$ artiq_run led.py
2014-09-30 18:10:40 +08:00
Enter desired LED state: 1
2015-07-14 04:08:20 +08:00
$ artiq_run led.py
2014-09-30 18:10:40 +08:00
Enter desired LED state: 0
2014-09-29 14:50:29 +08:00
What happens is the ARTIQ compiler notices that the :meth:`input_led_state` function does not have a ``@kernel`` decorator (:func:`~artiq.language.core.kernel`) and thus must be executed on the host. When the core device calls it, it sends a request to the host to execute it. The host displays the prompt, collects user input, and sends the result back to the core device, which sets the LED state accordingly.
2014-09-29 14:50:29 +08:00
RPC functions must always return a value of the same type. When they return a value that is not ``None``, the compiler should be informed in advance of the type of the value, which is what the ``-> TBool`` annotation is for.
Without the :meth:`~artiq.coredevice.core.Core.break_realtime` call, the RTIO events emitted by :func:`self.led.on()` or :func:`self.led.off()` would be scheduled at a fixed and very short delay after entering :meth:`~artiq.language.environment.Experiment.run()`.
These events would fail because the RPC to :meth:`input_led_state()` can take an arbitrary amount of time and therefore the deadline for submission of RTIO events would have long passed when :func:`self.led.on()` or :func:`self.led.off()` are called.
The :meth:`~artiq.coredevice.core.Core.break_realtime` call is necessary to waive the real-time requirements of the LED state change.
It advances the timeline far enough to ensure that events can meet the submission deadline.
2015-04-17 10:43:50 +08:00
Real-time Input/Output (RTIO)
-----------------------------
2014-09-29 14:50:29 +08:00
The point of running code on the core device is the ability to meet demanding real-time constraints. In particular, the core device can respond to an incoming stimulus or the result of a measurement with a low and predictable latency. We will see how to use inputs later; first, we must familiarize ourselves with how time is managed in kernels.
Create a new file ``rtio.py`` containing the following: ::
from artiq.experiment import *
2014-09-29 14:50:29 +08:00
2015-08-28 16:30:33 +08:00
2015-07-14 04:08:20 +08:00
class Tutorial(EnvExperiment):
def build(self):
2015-10-04 00:18:21 +08:00
self.setattr_device("core")
self.setattr_device("ttl0")
2014-09-29 14:50:29 +08:00
2014-09-30 18:10:40 +08:00
@kernel
def run(self):
2016-06-29 11:48:33 +08:00
self.core.reset()
self.ttl0.output()
2014-09-30 18:10:40 +08:00
for i in range(1000000):
delay(2*us)
self.ttl0.pulse(2*us)
2014-09-29 14:50:29 +08:00
In its :meth:`~artiq.language.environment.Experiment.build` method, the experiment obtains the core device and a TTL device called ``ttl0`` as defined in the device database.
In ARTIQ, TTL is used roughly synonymous with "a single generic digital signal" and does not refer to a specific signaling standard or voltage/current levels.
2014-09-29 14:50:29 +08:00
When :meth:`~artiq.language.environment.Experiment.run`, the experiment first ensures that ``ttl0`` is in output mode and actively driving the device it is connected to.
Bidirectional TTL channels (i.e. :class:`~artiq.devices.ttl.TTLInOut`) are in input (high impedance) mode by default, output-only TTL channels (:class:`~artiq.devices.ttl.TTLOut`) are always in output mode.
There are no input-only TTL channels.
The experiment then drives one million 2 µs long pulses separated by 2 µs each.
Connect an oscilloscope or logic analyzer to TTL0 and run ``artiq_run.py rtio.py``.
Notice that the generated signal's period is precisely 4 µs, and that it has a duty cycle of precisely 50%.
This is not what you would expect if the delay and the pulse were implemented with register-based general purpose input output (GPIO) that is CPU-controlled.
The signal's period would depend on CPU speed, and overhead from the loop, memory management, function calls, etc, all of which are hard to predict and variable.
Any asymmetry in the overhead would manifest itself in a distorted and variable duty cycle.
Instead, inside the core device, output timing is generated by the gateware and the CPU only programs switching commands with certain timestamps that the CPU computes.
2014-09-29 14:50:29 +08:00
This guarantees precise timing as long as the CPU can keep generating timestamps that are increasing fast enough. In case it fails to do that (and attempts to program an event with a timestamp smaller than the current RTIO clock timestamp), a :exc:`~artiq.coredevice.exceptions.RTIOUnderflow` exception is raised. The kernel causing it may catch it (using a regular ``try... except...`` construct), or it will be propagated to the host.
2014-09-29 20:20:14 +08:00
Try reducing the period of the generated waveform until the CPU cannot keep up with the generation of switching events and the underflow exception is raised. Then try catching it: ::
2016-01-26 07:41:27 +08:00
from artiq.experiment import *
2014-09-29 20:20:14 +08:00
2015-08-28 16:30:33 +08:00
2014-09-30 18:10:40 +08:00
def print_underflow():
print("RTIO underflow occured")
2014-09-29 20:20:14 +08:00
2015-07-14 04:08:20 +08:00
class Tutorial(EnvExperiment):
def build(self):
2015-10-04 00:18:21 +08:00
self.setattr_device("core")
self.setattr_device("ttl0")
2014-09-29 20:20:14 +08:00
@kernel
2014-09-30 18:10:40 +08:00
def run(self):
2016-06-29 11:48:33 +08:00
self.core.reset()
2014-09-30 18:10:40 +08:00
try:
for i in range(1000000):
2014-12-03 18:20:30 +08:00
self.ttl0.pulse(...)
2014-09-30 18:10:40 +08:00
delay(...)
except RTIOUnderflow:
print_underflow()
2014-09-29 20:20:14 +08:00
2016-02-16 05:43:02 +08:00
2016-02-23 15:53:41 +08:00
Parallel and sequential blocks
------------------------------
2014-09-29 20:20:14 +08:00
It is often necessary that several pulses overlap one another. This can be expressed through the use of ``with parallel`` constructs, in which the events generated by the individual statements are executed at the same time. The duration of the ``parallel`` block is the duration of its longest statement.
2014-09-29 20:20:14 +08:00
Try the following code and observe the generated pulses on a 2-channel oscilloscope or logic analyzer: ::
2014-09-30 18:10:40 +08:00
for i in range(1000000):
2016-02-23 15:53:41 +08:00
with parallel:
2014-12-03 18:20:30 +08:00
self.ttl0.pulse(2*us)
self.ttl1.pulse(4*us)
2014-09-30 18:10:40 +08:00
delay(4*us)
2014-09-29 20:20:14 +08:00
ARTIQ can implement ``with parallel`` blocks without having to resort to any of the typical parallel processing approaches.
It simply remembers the position on the timeline when entering the ``parallel`` block and then seeks back to that position after submitting the events generated by each statement.
In other words, the statements in the ``parallel`` block are actually executed sequentially, only the RTIO events generated by them are scheduled to be executed in parallel.
Note that if a statement takes a lot of CPU time to execute (this different from the events scheduled by a statement taking a long time), it may cause a subsequent statement to miss the deadline for timely submission of its events.
This then causes a :exc:`~artiq.coredevice.exceptions.RTIOUnderflow` exception to be raised.
2016-02-23 15:53:41 +08:00
Within a parallel block, some statements can be made sequential again using a ``with sequential`` construct. Observe the pulses generated by this code: ::
2014-09-29 20:20:14 +08:00
2014-09-30 18:10:40 +08:00
for i in range(1000000):
2016-02-23 15:53:41 +08:00
with parallel:
2014-09-30 18:10:40 +08:00
with sequential:
2014-12-03 18:20:30 +08:00
self.ttl0.pulse(2*us)
2014-09-30 18:10:40 +08:00
delay(1*us)
2014-12-03 18:20:30 +08:00
self.ttl0.pulse(1*us)
self.ttl1.pulse(4*us)
2014-09-30 18:10:40 +08:00
delay(4*us)
2014-09-29 20:20:14 +08:00
.. _rtio-analyzer-example:
RTIO analyzer
-------------
The core device records the real-time I/O waveforms into a circular buffer. It is possible to dump any Python object so that it appears alongside the waveforms using the ``rtio_log`` function, which accepts a channel name (i.e. a log target) as the first argument: ::
from artiq.experiment import *
class Tutorial(EnvExperiment):
def build(self):
self.setattr_device("core")
self.setattr_device("ttl0")
@kernel
def run(self):
self.core.reset()
for i in range(100):
self.ttl0.pulse(...)
rtio_log("ttl0", "i", i)
delay(...)
Afterwards, the recorded data can be extracted and written to a VCD file using ``artiq_coreanalyzer -w rtio.vcd`` (see: :ref:`core-device-rtio-analyzer-tool`). VCD files can be viewed using third-party tools such as GtkWave.
2017-04-12 00:43:09 +08:00
Direct Memory Access (DMA)
--------------------------
2017-04-12 00:43:09 +08:00
DMA allows you to store fixed sequences of pulses in system memory, and have the DMA core in the FPGA play them back at high speed. Pulse sequences that are too fast for the CPU (i.e. would cause RTIO underflows) can still be generated using DMA. The only modification of the sequence that the DMA core supports is shifting it in time (so it can be played back at any position of the timeline), everything else is fixed at the time of recording the sequence.
Try this: ::
from artiq.experiment import *
class DMAPulses(EnvExperiment):
def build(self):
self.setattr_device("core")
self.setattr_device("core_dma")
self.setattr_device("ttl0")
@kernel
def record(self):
with self.core_dma.record("pulses"):
# all RTIO operations now go to the "pulses"
# DMA buffer, instead of being executed immediately.
for i in range(100):
self.ttl0.pulse(100*ns)
delay(100*ns)
@kernel
def run(self):
self.core.reset()
self.record()
# prefetch the address of the DMA buffer
# for faster playback trigger
pulses_handle = self.core_dma.get_handle("pulses")
2017-04-12 00:43:09 +08:00
self.core.break_realtime()
while True:
# execute RTIO operations in the DMA buffer
# each playback advances the timeline by 50*(100+100) ns
self.core_dma.playback_handle(pulses_handle)