1
<!-- <!DOCTYPE section PUBLIC "-//OASIS//DTD DocBook V3.1//EN"> -->
4
<section id="acquisitionfunctions">
6
Acquisition and configuration functions
10
This Section gives an overview of all &comedi; functions with which
11
application programmers can implement their data acquisition. (With
12
<quote>acquisition</quote> we mean all possible kinds of interfacing
13
with the cards: input, output, configuration, streaming, etc.)
14
<xref linkend="comedireference"> explains the function calls in full
18
<section id="singleacquisition">
20
Functions for single acquisition
24
The simplest form of using &comedi; is to get one single sample to or
25
from an interface card. This sections explains how to do such simple
26
<link linkend="dio">digital</link> and
27
<link linkend="singleanalog">analog</link> acquisitions.
32
Single digital acquisition
36
Many boards supported by &comedi; have digital input and output
37
channels; i.e., channels that can only produce a <literal>0</literal>
38
or a <literal>1</literal>.
39
Some boards allow the <emphasis>direction</emphasis> (input or output)
40
of each channel to be specified independently in software.
44
&comedi; groups digital channels into a
45
<emphasis>subdevice</emphasis>, which is a group of digital channels
46
that have the same characteristics. For example, digital output lines
47
will be grouped into a digital
48
output subdevice, bidirectional digital lines will be grouped
49
into a digital I/O subdevice. Thus, there can be multiple
50
digital subdevices on a particular board.
54
Individual bits on a digital I/O device can be read and written using
57
int <link linkend="func-ref-comedi-dio-read">comedi_dio_read</link>(device,subdevice,channel,unsigned int *bit);
58
int <link linkend="func-ref-comedi-dio-write">comedi_dio_write</link>(device,subdevice,channel,unsigned int bit);
60
The <parameter class=function>device</parameter> parameter is a
61
<link linkend="ref-type-comedi-t">pointer</link>
62
to a successfully opened &comedi; device.
63
The <parameter class=function>subdevice</parameter> and
64
<parameter class=function>channel</parameter> parameters are positive
65
integers that indicate which subdevice and channel is used in the
66
acquisition. The integer <parameter class=function>bit</parameter>
67
contains the value of the acquired bit.
69
The direction of bidirectional lines can be configured using
72
<link linkend="func-ref-comedi-dio-config">comedi_dio_config</link>(device,subdevice,channel,unsigned int dir);
74
The parameter <parameter class=function>dir</parameter> should be
75
either <literal>COMEDI_INPUT</literal> or
76
<literal>COMEDI_OUTPUT</literal>.
77
Many digital I/O subdevices group channels into blocks for
78
configuring direction. Changing one channel in a block changes
83
Multiple channels can be read and written simultaneously using the
86
<link linkend="func-ref-comedi-dio-bitfield">comedi_dio_bitfield</link>(device,subdevice,unsigned int write_mask,unsigned int *bits);
88
Each channel is assigned to a bit in the
89
<parameter class=function>write_mask</parameter> and
90
<parameter class=function>bits</parameter>
92
<parameter class=function>write_mask</parameter> is set, the
93
corresponding bit in <parameter class=function>*bits</parameter> will
94
be written to the corresponding digital output line.
95
Each digital line is then read and placed into
96
<parameter class=function>*bits</parameter>. The value
97
of bits in <parameter class=function>*bits</parameter> corresponding
98
to digital output lines is undefined and device-specific. Channel
99
<literal>0</literal> is the least significant bit in the bitfield;
100
channel <literal>31</literal> is the most significant bit. Channels
101
higher than <literal>31</literal> cannot be accessed using this method.
105
The digital acquisition functions seem to be very simple, but, behind
106
the implementation screens of the &comedi; kernel module, they are
107
executed as special cases of the general
108
<link linkend="instructions">instruction</link> command.
115
<section id="singleanalog">
117
Single analog acquisition
120
Analog &comedi; channels can produce data values that are
121
<emphasis>samples</emphasis> from continuous analog signals.
122
These samples are integers with a significant content in
123
the range of, typically, <literal>8</literal>, <literal>10</literal>,
124
<literal>12</literal>, or <literal>16</literal> bits.
129
int <link linkend="func-ref-comedi-data-read">comedi_data_read</link>(<link linkend="ref-type-comedi-t">comedi_t</link> * device, unsigned int subdevice, unsigned int channel,
130
unsigned int range, unsigned int aref, <link linkend="ref-type-lsampl-t">lsampl_t</link> * data);
132
function reads one such data value from a &comedi; channel, and puts it in
133
the user-specified <parameter>data</parameter> buffer. The
135
int <link linkend="func-ref-comedi-data-write">comedi_data_write</link>(<link linkend="ref-type-comedi-t">comedi_t</link> * device, unsigned int subdevice, unsigned int channel,
136
unsigned int range, unsigned int aref, <link linkend="ref-type-lsampl-t">lsampl_t</link> data);
138
works in the opposite direction. Data values returned by this function
139
are unsigned integers less than, or equal to, the maximum sample value
140
of the channel, which can be determined using the function
142
<link linkend="ref-type-lsampl-t">lsampl_t</link> <link linkend="func-ref-comedi-get-maxdata">comedi_get_maxdata</link>(<link linkend="ref-type-comedi-t">comedi_t</link> * device, unsigned int subdevice, unsigned int channel);
144
Conversion of data values to physical units can be performed by the
147
double <link linkend="func-ref-comedi-to-phys">comedi_to_phys</link>(<link linkend="ref-type-lsampl-t">lsampl_t</link> data, comedi_range * range, <link linkend="ref-type-lsampl-t">lsampl_t</link> maxdata);
149
There are two data structures in these commands that are not fully
155
<link linkend="ref-type-comedi-t">comedi_t</link>: this data structure
156
contains all information that a user program has to know about an
157
<emphasis>open</emphasis> &comedi; device. The programmer doesn't have
158
to fill in this data structure manually: it gets filled in by opening
165
<link linkend="ref-type-lsampl-t">lsampl_t</link>: this
166
<quote>data structure</quote> represents one single sample. On most
167
architectures, it's nothing more than a 32 bits value. Internally,
168
&comedi; does some conversion from raw sample data to
169
<quote>correct</quote> integers. This is called <quote>data
177
Each single acquisition by, for example,
179
<link linkend="func-ref-comedi-data-read">comedi_data_read()</link>
181
requires quite some overhead, because all the arguments of the
182
function call are checked. If multiple acquisitions must be done on
183
the same channel, this overhead can be avoided by using a function
184
that can read more than one sample:
186
int <link linkend="func-ref-comedi-dio-read">comedi_data_read_n</link>(<link linkend="ref-type-comedi-t">comedi_t</link> *it, unsigned int subdev, unsigned int chan, unsigned int range,
187
unsigned int aref, <link linkend="ref-type-lsampl-t">lsampl_t</link> *data, unsigned int n)
189
The number of samples, <parameter class=function>n</parameter>, is
190
limited by the &comedi; implementation (to a maximum of 100 samples),
191
because the call is blocking.
194
The start of the data acquisition can also be delayed by a specified
195
number of nano-seconds:
197
int <link linkend="func-ref-comedi-data-read-delayed">comedi_data_read_delayed</link>(<link linkend="ref-type-comedi-t">comedi_t</link> *it, unsigned int subdev, unsigned int chan, unsigned int range,
198
unsigned int aref, <link linkend="ref-type-lsampl-t">lsampl_t</link> *data, unsigned int nano_sec)
200
All these read and write acquisition functions are implemented on top
201
of the generic <link linkend="instructions">instruction</link>
210
<section id="instructions">
212
Instructions for multiple acquisitions
215
The <emphasis>instruction</emphasis> is one of the most generic,
216
overloaden and flexible functions in the &comedi; API. It is used to
217
execute a multiple of identical acquisitions on the same channel, but
219
<link linkend="instructionsconfiguration">configuration</link> of a
221
<anchor id="anchor.instruction.list">
222
An <emphasis>instruction list</emphasis> is a list of instructions,
223
possibly on different channels. Both instructions and instructions
224
lists are executed <emphasis>synchronously</emphasis>, i.e., while
225
<emphasis role="strong">blocking</emphasis> the calling process.
226
This is one of the limitations of instructions; the other one is that
227
they cannot code an acquisition involving timers or external events.
228
These limits are eliminated by the
229
<link linkend="commandsstreaming">command</link> acquisition
234
<section id="comediinsnstructure">
236
The instruction data structure
239
All the information needed to execute an instruction is stored in the
240
<link linkend="ref-type-comedi-insn">comedi_insn</link>
243
struct <anchor id="insn-data-structure">comedi_insn_struct{
244
<anchor id="insn-data-structure-insn">unsigned int insn; // integer encoding the type of acquisition
245
// (or configuration)
246
unsigned int n; // number of samples
247
<link linkend="ref-type-lsampl-t">lsampl_t</link> <anchor id="insn-data-structure-data">*data; // pointer to data buffer
248
unsigned int subdev; // subdevice
249
unsigned int <anchor id="insn-data-structure-chanspec"><link linkend="ref-macro-CR-PACK">chanspec</link>; // encoded channel specification
250
unsigned int unused[3];
253
Because of the large flexibility of the instruction function, many
254
types of instruction do not need to fill in all fields, or attach
255
different meanings to the same field. But the current implementation
256
of &comedi; requires the
257
<link linkend="insn-data-structure-data">data</link> field to be at
262
The <link linkend="insn-data-structure-insn">insn</link> flag of the
263
<link linkend="insn-data-structure">instruction data structure</link>
264
determines the type of acquisition executed in the corresponding
270
INSN_READ: the instruction executes a read on an analog channel.
276
INSN_WRITE: the instruction executes a write on an analog channel.
282
INSN_BITS: indicates that the instruction must
283
read or write values on multiple digital I/O channels.
289
INSN_GTOD: the instruction performs a <quote>Get Time Of Day</quote>
296
INSN_WAIT: the instruction blocks for a specified number of
307
<section id="instructionexecution">
309
Instruction execution
312
Once an instruction data structure has been filled in, the
313
corresponding instruction is executed as follows:
315
int <link linkend="func-ref-comedi-do-insn">comedi_do_insn</link>(<link linkend="ref-type-comedi-t">comedi_t</link> *it, <link linkend="ref-type-comedi-insn">comedi_insn</link> * instruction);
317
Many &comedi; instructions are shortcuts that relieve the programmer
318
from explicitly filling in the data structure and calling the
319
<link linkend="func-ref-comedi-do-insn">comedi_do_insn</link>
325
int <link linkend="func-ref-comedi-do-insnlist">comedi_do_insnlist</link><link linkend="ref-type-comedi-t">comedi_t</link> *it, <link linkend="ref-type-comedi-insnlist">comedi_insnlist</link> * list)
327
instruction allows to perform a list of instructions in one function
328
call. The number of instructions in the list is limited in the
329
implementation, because instructions are executed
330
<emphasis>synchronously</emphasis>, i.e., the call blocks until the
331
whole instruction (list) has finished.
339
<section id="instructionsconfiguration">
341
Instructions for configuration
344
<xref linkend="instructions"> explains how instructions are used to do
345
<emphasis>acquisition</emphasis> on channels. This section explains
346
how they are used to <emphasis>configure</emphasis> a device.
347
There are various sorts of configurations, and the
348
specific information for each different configuration possibility is
349
to be specified via the
350
<link linkend="insn-data-structure-data">data</link> buffer of the
351
<link linkend="insn-data-structure">instruction data structure</link>.
352
(So, the pointer to a
353
<link linkend="ref-type-lsampl-t">lsampl_t</link>
354
is misused as a pointer to an array with board-specific information.)
358
Using INSN_CONFIG as the
359
<link linkend="insn-data-structure-insn">insn</link> flag in an
360
<link linkend="insn-data-structure">instruction data structure</link>
361
indicates that the instruction will
362
<emphasis>not perform acquisition</emphasis> on a
363
channel, but will <emphasis>configure</emphasis> that channel.
364
For example, the configuration of digital I/O channels is done as
366
<link linkend="ref-macro-CR-PACK">chanspec</link> field in the
367
<link linkend="insn-data-structure-chanspec">comedi_insn</link>
368
data structure, contains the channel to be configured. And
369
<link linkend="insn-data-structure-data">data</link>[0] contains
370
either COMEDI_INPUT or COMEDI_OUTPUT, depending on the desired
371
direction of the digital I/O lines.
372
On typical devices, multiple channels are grouped together in blocks
373
for determining their direction. And configuring one channel in a
374
block configures the entire block.
378
Another example of an INSN_CONFIG instruction is the configuration of
379
the <link linkend="trigother-event">TRIG_OTHER</link> event source.
385
<section id="inttrigconfiguration">
387
Instruction for internal triggering
390
This special instruction has
391
<anchor id="insn-inttrig">INSN_INTTRIG as the
392
<link linkend="insn-data-structure-insn">insn</link> flag in its
393
<link linkend="insn-data-structure">instruction data structure</link>.
394
Its execution causes an
395
<link linkend="trig-int-start-src">internal triggering event</link>. This
396
event can, for example, cause the device driver to start a conversion,
397
or to stop an ongoing acquisition. The exact meaning of the triggering
398
depends on the card and its particular driver.
402
<link linkend="insn-data-structure-data">data</link>[0] field of the
403
INSN_INTTRIG instruction is reserved for future use, and should be set
410
<section id="commandsstreaming">
412
Commands for streaming acquisition
416
The most powerful &comedi; acquisition primitive is the
417
<emphasis>command</emphasis>. It's powerful because, with one single
418
command, the programmer launches:
423
a possibly infinite <emphasis>sequence of acquisitions</emphasis>,
429
accompanied with various <emphasis>callback</emphasis> functionalities
430
(DMA, interrupts, driver-specific callback functions),
436
for <emphasis>any number of channels</emphasis>,
442
with an <emphasis>arbitrary order</emphasis> of channels in each scan
443
(possibly even with repeated channels per scan),
449
and with various scan <emphasis>triggering sources</emphasis>,
450
external (i.e., hardware pulses) as well as internal (i.e., pulses
451
generated on the DAQ card itself, or generated by a
452
<link linkend="inttrigconfiguration">software trigger instruction</link>).
457
This command functionality exists in the &comedi; API, because various
458
data acquisition devices have the capability to perform this kind of
459
complex acquisition, driven by either on-board or
460
off-board timers and triggers.
464
A command specifies a particular data
465
<link linkend="fig-acq-seq">acquisition sequence</link>, which
466
consists of a number of <emphasis>scans</emphasis>, and each scan is
467
comprised of a number of <emphasis>conversions</emphasis>, which
468
usually corresponds to a single A/D or D/A conversion. So, for
469
example, a scan could consist of sampling channels 1, 2 and 3 of a
470
particular device, and this scan should be repeated 1000 times, at
471
intervals of 1 millisecond apart.
474
The command function is complementary to the
475
<link linkend="instructionsconfiguration">configuration instruction</link>
476
function: each channel in the command's
477
<link linkend="command-data-struct-chanlist">chanlist</link>
478
should first be configured by an appropriate instruction.
482
<section id="executingcommand">
488
A commands is executed by the following &comedi; function:
490
int <link linkend="func-ref-comedi-command">comedi_command</link>(<link linkend="ref-type-comedi-t">comedi_t</link> * device, <link linkend="ref-type-comedi-cmd">comedi_cmd</link> * command);
492
The following sections explain the meaning of the
493
<link linkend="ref-type-comedi-cmd">comedi_cmd</link> data structure.
494
Filling in this structure can be quite complicated, and
495
requires good knowledge about the exact functionalities of the DAQ
496
card. So, before launching a command, the application programmer is
497
adviced to check whether this complex command data structure can be
498
successfully parsed. So, the typical sequence for executing a command is
499
to first send the command through
500
<link linkend="func-ref-comedi-command-test">comedi_command_test()</link>
501
once or twice. The test will check that the command is valid for the
502
particular device, and often makes some adjustments to the command
503
arguments, which can then be read back by the user to see the actual
507
A &comedi; program can find out on-line what the command capabilities
508
of a specific device are, by means of the
509
<link linkend="func-ref-comedi-get-cmd-src-mask">comedi_get_cmd_src_mask()</link>
516
<section id="comedicmdstructure">
518
The command data structure
521
The command executes according to the information about the requested
522
acquisition, which is stored in the
523
<link linkend="ref-type-comedi-cmd">comedi_cmd</link>
524
<anchor id="command-data-struct">data structure:
526
typedef struct comedi_cmd_struct comedi_cmd;
528
struct comedi_cmd_struct{
529
unsigned int subdev; // which subdevice to sample
530
unsigned int <anchor id="command-data-struct-flags">flags; // encode some configuration possibilities
531
// of the command execution; e.g.,
532
// whether a callback routine is to be
533
// called at the end of the command
535
unsigned int <anchor id="command-data-struct-start-src">start_src; // event to make the acquisition start
536
unsigned int <anchor id="command-data-struct-start-arg">start_arg; // parameters that influence this start
538
unsigned int <anchor id="command-data-struct-scan-begin-src">scan_begin_src; // event to make a particular scan start
539
unsigned int <anchor id="command-data-struct-scan-begin-arg">scan_begin_arg; // parameters that influence this start`
541
unsigned int <anchor id="command-data-struct-convert-src">convert_src; // event to make a particular conversion start
542
unsigned int <anchor id="command-data-struct-convert-arg">convert_arg; // parameters that influence this start
544
unsigned int <anchor id="command-data-struct-scan-end-src">scan_end_src; // event to make a particular scan terminate
545
unsigned int <anchor id="command-data-struct-scan-end-arg">scan_end_arg; // parameters that influence this termination
547
unsigned int <anchor id="command-data-struct-stop-src">stop_src; // what make the acquisition terminate
548
unsigned int <anchor id="command-data-struct-stop-arg">stop_arg; // parameters that influence this termination
550
unsigned int <anchor id="command-data-struct-chanlist">*chanlist; // pointer to list of channels to be sampled
551
unsigned int <anchor id="command-data-struct-chanlist-len">chanlist_len; // number of channels to be sampled
553
sampl_t *<anchor id="command-data-struct-data">data; // address of buffer
554
unsigned int <anchor id="command-data-struct-data-len">data_len; // number of samples to acquire
557
The start and end of the whole command acquisition sequence, and the
558
start and end of each scan and of each conversion, is triggered by a
559
so-called <emphasis>event</emphasis>. More on these in
560
<xref linkend="comedicmdsources">.
564
The <parameter class=function>subdev</parameter> member of the
565
<link linkend="ref-type-comedi-cmd">comedi_cmd()</link> structure is
566
the index of the subdevice the command is intended for. The
567
<link linkend="func-ref-comedi-find-subdevice-by-type">comedi_find_subdevice_by_type()</link>
568
function can be useful in discovering the index of your desired subdevice.
572
The <link linkend="command-data-struct-chanlist">chanlist</link>
574
<link linkend="ref-type-comedi-cmd">comedi_cmd</link> data
575
structure should point to an array whose number of elements is
577
<link linkend="command-data-struct-chanlist-len">chanlist_len</link>
578
(this will generally be the same as the
579
<link linkend="command-data-struct-scan-end-arg">scan_end_arg</link>).
581
<link linkend="command-data-struct-chanlist">chanlist</link>
582
specifies the sequence of channels and gains (and analog references)
583
that should be stepped through for each scan. The elements of the
584
<link linkend="command-data-struct-chanlist">chanlist</link> array should be
585
initialized by <quote>packing</quote> the channel, range and reference
586
information together with the
587
<parameter class=function>
588
<link linkend="ref-macro-CR-PACK">CR_PACK()</link>
594
The <link linkend="command-data-struct-data">data</link> and
595
<link linkend="command-data-struct-data-len">data_len</link>
596
members can be safely ignored when issueing commands from a user-space
597
program. They only have meaning when a command is sent from a
598
<emphasis role="strong">kernel</emphasis> module using the
599
<function>kcomedilib</function> interface, in which case they specify
600
the buffer where the driver should write/read its data to/from.
604
The final member of the
605
<link linkend="command-data-struct">comedi_cmd</link> structure is the
606
<link linkend="command-data-struct-flags">flags</link> field,
607
i.e., bits in a word that can be bitwise-or'd together. The meaning of
608
these bits are explained in a
609
<link linkend="source.flags.anchor">later section</link>.
615
<section id="comedicmdsources">
617
The command trigger events
618
<anchor id="source.trigger.anchor">
621
A command is a very versatile acquisition instruction, in the sense
622
that it offers lots of possibilities to let different hardware and
623
software sources determine when acquisitions are started, performed,
624
and stopped. More specifically, the command
625
<link linkend="command-data-struct">data structure</link>
626
has <emphasis>five</emphasis> types of events: start the
627
<link linkend="acquisitionterminology">acquisition</link>,
628
start a <link linkend="scan">scan</link>, start a
629
<link linkend="conversion">conversion</link>, stop a scan, and stop
630
the acquisition. Each event can be given its own
631
<emphasis><link linkend="source.trigger.anchor">source</link></emphasis>
632
(the <parameter class=function>*_src</parameter> members in the
633
<link linkend="ref-type-comedi-cmd">comedi_cmd</link> data
634
structure). And each event source can have a corresponding
635
argument (the <parameter class=function>*_arg</parameter> members of
636
the <link linkend="ref-type-comedi-cmd">comedi_cmd</link> data
637
structure) whose meaning depends on the type of source trigger.
638
For example, to specify an external digital line <quote>3</quote> as a
639
source (in general, <emphasis>any</emphasis> of the five event
640
sources), you would use
641
<parameter>src</parameter>=<link linkend="trig-ext">TRIG_EXT</link> and
642
<parameter>arg</parameter>=3.
645
The following paragraphs discuss in somewhat more detail the trigger
646
event sources(<parameter class=function>*_src</parameter>), and the
647
corresponding arguments (<parameter class=function>*_arg</parameter>).
650
The start of an acquisition is controlled by the
651
<link linkend="command-data-struct-start-src">start_src</link> events.
652
The available options are:
657
<anchor id="trig-now-start-src">
659
<link linkend="command-data-struct-start-src">start_src</link>
661
<link linkend="command-data-struct-start-arg">start_arg</link>
662
nanoseconds after the
663
<link linkend="ref-type-comedi-cmd">comedi_cmd</link>
664
is called. Currently, only
665
<link linkend="command-data-struct-start-arg">start_arg</link>=0 is
672
<anchor id="trig-follow-start-src">
673
TRIG_FOLLOW: (For an output device.) The
674
<link linkend="command-data-struct-start-src">start_src</link>
675
event occurs when data is written to the buffer.
681
<anchor id="trig-ext-start-src">
682
TRIG_EXT: the start event occurs when an external trigger signal
683
occurs; e.g., a rising edge of a digital line.
684
<link linkend="command-data-struct-start-arg">start_arg</link>
685
chooses the particular digital line.
691
<anchor id="trig-int-start-src">
692
TRIG_INT: the start event occurs on a &comedi; internal signal, which
693
is typically caused by an
694
<link linkend="insn-inttrig">INSN_INTTRIG instruction</link>.
699
The start of the beginning of each
700
<link linkend="scan">scan</link> is controlled by the
701
<link linkend="command-data-struct-scan-begin-src">scan_begin</link> events.
702
The available options are:
707
<anchor id="trig-timer-start-scan">
709
<link linkend="command-data-struct-scan-begin-src">scan_begin</link>
710
events occur periodically. The time between
711
<link linkend="command-data-struct-scan-begin-src">scan_begin</link>
713
<link linkend="command-data-struct-convert-arg">convert_arg</link>
720
<anchor id="trig-follow-start-scan">
722
<link linkend="command-data-struct-scan-begin-src">scan_begin</link>
723
event occurs immediately after a
724
<link linkend="command-data-struct-scan-end-src">scan_end</link>
731
<anchor id="trig-ext-start-scan">
733
<link linkend="command-data-struct-scan-begin-src">scan_begin</link>
734
event occurs when an external trigger signal
735
occurs; e.g., a rising edge of a digital line.
736
<link linkend="command-data-struct-scan-begin-arg">scan_begin_arg</link>
737
chooses the particular digital line.
743
<link linkend="command-data-struct-scan-begin-arg">scan_begin_arg</link>
744
used here may not be supported exactly by the device, but it
745
will be adjusted to the nearest supported value by
746
<link linkend="func-ref-comedi-command-test">comedi_command_test()</link>.
749
The timing between each sample in a
750
<link linkend="scan">scan</link> is controlled by the
751
<link linkend="command-data-struct-convert-src">convert_*</link>
757
<anchor id="convert-trig-timer">
758
<anchor id="trig-timer">
759
TRIG_TIMER: the conversion events occur periodically. The time
760
between convert events is
761
<link linkend="command-data-struct-convert-arg">convert_arg</link>
768
<anchor id="convert-trig-ext">
769
<anchor id="trig-ext">
770
TRIG_EXT: the conversion events occur when an external trigger signal
771
occurs, e.g., a rising edge of a digital line.
772
<link linkend="command-data-struct-convert-arg">convert_arg</link>
773
chooses the particular digital line.
779
<anchor id="convert-trig-now">
780
<anchor id="trig-now">
781
TRIG_NOW: All conversion events in a
782
<link linkend="scan">scan</link> occur simultaneously.
787
The <emphasis>end</emphasis> of each scan is almost always specified
789
<link linkend="trig-count">TRIG_COUNT</link>, with the argument being
790
the same as the number of channels in the
791
<link linkend="command-data-struct-chanlist">chanlist</link>. You
792
could probably find a device that allows something else, but it would
797
<link linkend="acquisitionterminology">acquisition</link> is
799
<link linkend="command-data-struct-stop-src">stop_src</link>
800
and <link linkend="command-data-struct-stop-arg">stop_arg</link>:
805
<anchor id="acquisition-end-trig-count">
806
<anchor id="trig-count">
807
TRIG_COUNT: stop the acquisition after
808
<link linkend="command-data-struct-stop-arg">stop_arg</link>
815
<anchor id="acquisition-end-trig-none">
816
<anchor id="trig-none">
817
TRIG_NONE: perform continuous acquisition, until stopped using
818
<link linkend="func-ref-comedi-cancel">comedi_cancel()</link>.
821
Its argument is reserved and should be set to 0.
822
(<quote>Reserved</quote>
823
means that unspecified things could happen if it is set to something
829
There are a couple of less usual or not yet implemented events:
834
<anchor id="trig-time">
836
cause an event to occur at a particular time.
839
(This event source is reserved for future use.)
845
<anchor id="trigother-event">
846
TRIG_OTHER: driver specific event trigger.
849
This event can be useful as any of the trigger sources. Its exact
850
meaning is driver specific, because it implements a feature that
851
otherwise does not fit into the generic &comedi; command interface.
852
Configuration of TRIG_OTHER features are done by
853
<link linkend="instructionsconfiguration">INSN_CONFIG</link>
857
The argument is reserved and should be set to 0.
862
Not all event sources are applicable to all events. Supported
863
trigger sources for specific events depend significantly on your
864
particular device, and even more on the current state of its device
866
<link linkend="func-ref-comedi-get-cmd-src-mask">comedi_get_cmd_src_mask()</link>
867
function is useful for determining what trigger sources a subdevice
874
<section id="comedicmdflags">
877
<anchor id="source.flags.anchor">
882
<link linkend="command-data-struct-flags">flags</link> field in the
883
<link linkend="ref-type-comedi-cmd">command data structure</link>
884
is used to specify some <quote>behaviour</quote> of the acquisitions in
886
The meaning of the field is as follows:
891
<anchor id="trig-rt">
892
TRIG_RT: ask the driver to use a
893
<emphasis role="strong">hard real-time</emphasis> interrupt handler.
894
This will reduce latency in handling interrupts from your data
896
hardware. It can be useful if you are sampling at high frequency, or
897
if your hardware has a small onboard data buffer. You must have a
898
real-time kernel (<ulink url="http://www.rtai.org">RTAI</ulink> or
899
<ulink url="http://fsmlabs.com/community/">RTLinux/Free</ulink>)
900
and must compile &comedi; with real-time support, or this flag will do
907
<anchor id="trig-wake-eos">
909
where <quote>EOS</quote> stands for <quote>End of Scan</quote>. Some
910
drivers will change their behaviour when this flag is set, trying to
911
transfer data at the end of every scan (instead of, for example,
912
passing data in chunks whenever the board's hardware data buffer is
913
half full). This flag may degrade a driver's performance at high
914
frequencies, because the end of a scan is, in general, a much more
915
frequent event than the filling up of the data buffer.
921
<anchor id="trig-round-nearest">
923
round to nearest supported timing period, the default.
924
This flag (as well as the following three), indicates how timing
925
arguments should be rounded if the hardware cannot achieve the exact
932
<anchor id="trig-round-down">
933
TRIG_ROUND_DOWN: round period down.
939
<anchor id="trig-round-up">
940
TRIG_ROUND_UP: round period up.
946
<anchor id="trig-round-up-next">
948
this one doesn't do anything, and I don't know what it was intended
955
<anchor id="trig-dither">
956
TRIG_DITHER: enable dithering? Dithering is a software technique to
957
smooth the influence of discretization <quote>noise</quote>.
963
<anchor id="trig-deglitch">
964
TRIG_DEGLITCH: enable deglitching? Another <quote>noise</quote>
971
<anchor id="trig-write">
973
write to bidirectional devices. Could be useful, in principle, if
974
someone wrote a driver that supported commands for a digital I/O
975
device that could do either input or output.
981
<anchor id="trig-bogus">
982
TRIG_BOGUS: do the motions?
988
<anchor id="trig-other">
989
TRIG_CONFIG: perform configuration, not triggering. This is a legacy
991
<link linkend="ref-type-comedi-cmd">comedi_trig_struct</link>
992
data structure, and has no function at present.
1006
If you wish to aquire accurate waveforms, it is vital that you use an
1007
anti-alias filter. An anti-alias filter is a low-pass filter used to
1008
remove all frequencies higher than the Nyquist frequency (half your sampling rate)
1009
from your analog input signal
1010
before you convert it to digital. If you fail to filter your input signal,
1011
any high frequency components in the original analog signal will create
1012
artifacts in your recorded digital waveform that cannot be corrected.
1015
For example, suppose you are sampling an analog input channel at a rate of
1016
1000 Hz. If you were to apply a 900 Hz sine wave to the input, you
1017
would find that your
1018
sampling rate is not high enough to faithfully record the 900 Hz input,
1019
since it is above your Nyquist frequency of 500 Hz. Instead, what you
1020
will see in your recorded digital waveform is a 100 Hz sine wave! If you
1021
don't use an anti-alias filter, it is impossible to tell whether the 100
1022
Hz sine wave you see in your digital signal was really produced by a
1023
100 Hz input signal, or a 900 Hz signal aliased to 100 Hz, or a 1100 Hz
1027
In practice, the cutoff frequency for the anti-alias filter is usually
1028
set 10% to 20% below the Nyquist frequency due to fact that real filters
1029
do not have infinitely sharp cutoffs.
1035
<section id="slowlyvarying">
1037
Slowly-varying inputs
1041
Sometimes, your input channels change slowly enough that
1042
you are able to average many successive input values to get a
1043
more accurate measurement of the actual value. In general,
1044
the more samples you average, the better your estimate
1045
gets, roughly by a factor of sqrt(number_of_samples).
1046
Obviously, there are limitations to this:
1053
you are ultimately limited by <quote>Spurious Free Dynamic
1054
Range</quote>. This SFDR is one of the popular measures to quantify how
1055
much noise a signal carries. If you take a Fourier transform of your
1056
signal, you will see several <quote>peaks</quote> in the transform: one
1057
or more of the fundamental harmonics of the measured signal, and lots
1058
of little <quote>peaks</quote> (called <quote>spurs</quote>) caused by
1059
noise. The SFDR is then the difference between the amplitude of the
1060
fundamental harmonic and of the largest spur (at frequencies below
1061
half of the Nyquist frequency of the DAQ sampler!).
1067
you need to have <emphasis>some</emphasis> noise on the input channel,
1068
otherwise you will be averaging the same number <literal>N</literal>
1069
times. (Of course, this only holds if the noise is large enough to
1070
cause at least a one-bit discretization.)
1076
the more noise you have, the greater your SFDR, but it
1077
takes many more samples to compensate for the increased
1084
if you feel the need to average samples for, for example, two seconds,
1085
your signal will need to be <emphasis>very</emphasis> slowly-varying,
1086
i.e., not varying more than your target uncertainty for the entire two
1094
As you might have guessed, the &comedi; library has functions
1095
to help you in your quest to accurately measure slowly varying
1098
int <link linkend="func-ref-comedi-sv-init">comedi_sv_init</link>(<link linkend="ref-type-comedi-sv-t">comedi_sv_t</link> * sv, <link linkend="ref-type-comedi-t">comedi_t</link> * device, unsigned int subdevice, unsigned int channel);
1100
This function initializes the
1101
<link linkend="ref-type-comedi-sv-t">comedi_sv_t</link> data structure, used
1102
to do the averaging acquisition:
1104
struct comedi_sv_struct{
1105
<link linkend="ref-type-comedi-t">comedi_t</link> *dev;
1106
unsigned int subdevice;
1113
/* number of measurements to average (for analog inputs) */
1119
The actual acquisition is done with:
1121
int <link linkend="func-ref-comedi-sv-measure">comedi_sv_measure</link>(<link linkend="ref-type-comedi-sv-t">comedi_sv_t</link> * sv, double * data);
1123
The number of samples over which the
1124
<function>comedi_sv_measure()</function> averages is limited by the
1125
implementation (currently the limit is 100 samples).
1129
One typical use for this function is the measurement of thermocouple
1131
And the &comedi; self-calibration utility also uses these functions.
1132
On some hardware, it is possible to tell it to measure an
1133
internal stable voltage reference, which is typically going
1134
to be very slowly varying; on the kilosecond time scale
1135
or more. So, it is reasonable to measure millions of samples,
1136
to get a very accurate measurement of the A/D converter output
1137
value that corresponds to the voltage reference. Sometimes,
1138
however, this is overkill, since there is no need to
1139
perform a part-per-million calibration to a standard that
1140
is only accurate to a part-per-thousand.
1145
<section id="experimentalfunctionality">
1147
Experimental functionality
1151
The following subsections document functionality that has not yet
1152
matured. Most of this functionality has even not been implemented yet
1153
in any single device driver. This information is included here, in
1154
order to stimulate discussion about their API, and to encourage
1155
pioneering implementations.
1158
<section id="digitalinputcombining">
1160
Digital input combining machines
1164
(<emphasis role="strong">Status: experimental (i.e., no driver implements
1165
this yet)</emphasis>)
1168
When one or several digital inputs are used to modify an output
1169
value, either an accumulator or a single digital line or bit,
1170
a bitfield structure is typically used in the &comedi; interface.
1171
The digital inputs have two properties, <quote>sensitive</quote> inputs
1172
and <quote>modifier</quote> inputs. Edge transitions on sensitive
1173
inputs cause changes in the output signal, whereas modifier inputs
1174
change the effect of edge transitions on sensitive inputs. Note that
1175
inputs can be both modifier inputs and sensitive inputs.
1179
For simplification purposes, it is assumed that multiple digital
1180
inputs do not change simultaneously.
1184
The combined state of the modifier inputs determine a modifier
1185
state. For each combination of modifier state and sensitive
1186
input, there is a set of bits that determine the effect on the
1187
output value due to positive or negative transitions of the
1188
sensitive input. For each transition direction, there are two
1189
bits defined as follows:
1193
00: transition is ignored.
1197
01: accumulator is incremented, or output is set.
1201
10: accumulator is decremented, or output is cleared.
1209
For example, a simple digital follower is specified by the bit
1210
pattern 01 10, because it sets the output on positive transitions
1211
of the input, and clears the output on negative transitions. A
1212
digital inverter is similarily 10 01. These systems have only
1213
one sensitive input.
1217
As another example, a simple up counter, which increments on
1218
positive transitions of one input, is specified by 01 00. This
1219
system has only one sensitive input.
1223
When multiple digital inputs are used, the inputs are divided
1224
into two types, inputs which cause changes in the accumulator, and
1225
those that only modify the meaning of transitions on other inputs.
1226
Modifier inputs do not require bitfields, but there needs to be
1227
a bitfield of length 4*(2^(N-1)) for each edge sensitive input,
1228
where N is the total number of inputs. Since N is usually 2 or
1229
3, with only one edge sensitive input, the scaling issues are
1236
<section id="analogconversion">
1238
Analog filtering configuration
1242
<emphasis role="strong">(Status: design (i.e., no driver implements
1243
this yet).)</emphasis>
1247
The <link linkend="insn-data-structure-insn">insn</link> field of the
1248
<link linkend="insn-data-structure">instruction data structure</link>
1249
has not been assigned yet.
1252
The <link linkend="insn-data-structure-chanspec">chanspec</link> field
1253
of the <link linkend="insn-data-structure">instruction data
1254
structure</link> is ignored.
1258
Some devices have the capability to add white noise (dithering) to
1259
analog input measurement. This additional noise can then be averaged
1260
out, to get a more accurate measurement of the input signal. It
1261
should not be assumed that channels can be separately configured.
1262
A simple design can use 1 bit to turn this feature on/off.
1266
Some devices have the capability of changing the glitch characteristics
1267
of analog output subsytems. The default (off) case should be where
1268
the average settling time is lowest. A simple design can use 1 bit
1269
to turn this feature on/off.
1273
Some devices have a configurable analog filters as part of the analog
1274
input stage. A simple design can use 1 bit to enable/disable the
1275
filter. Default is disabled, i.e., the filter being bypassed, or if
1276
the choice is between two filters, the filter with the largest
1281
<section id="waveformgeneration">
1283
Analog Output Waveform Generation
1287
<emphasis role="strong">(Status: design (i.e., no driver implements
1288
this yet).)</emphasis>
1291
The <link linkend="insn-data-structure-insn">insn</link> field of the
1292
<link linkend="insn-data-structure">instruction data structure</link>
1293
has not been assigned yet.
1296
The <link linkend="insn-data-structure-chanspec">chanspec</link> field
1297
of the <link linkend="insn-data-structure">instruction data
1298
structure</link> is ignored.
1302
Some devices have the ability to cyclicly loop through samples kept in
1303
an on-board analog output FIFO. This config should allow the user to
1304
enable/disable this mode.
1308
This config should allow the user to configure the number of samples
1309
to loop through. It may be necessary to configure the channels used.
1314
<section id="extendedtriggering">
1319
<emphasis role="strong">(Status: alpha.)</emphasis>
1323
The <link linkend="insn-data-structure-insn">insn</link> field of the
1324
<link linkend="insn-data-structure">instruction data structure</link>
1325
has not been assigned yet.
1328
The <link linkend="insn-data-structure-chanspec">chanspec</link> field
1329
of the <link linkend="insn-data-structure">instruction data
1330
structure</link> is ignored.
1334
This section covers common information for all extended
1335
triggering configuration, and doesn't describe a particular
1336
type of extended trigger.
1340
Extended triggering is used to configure triggering engines that
1341
do not fit into commands. In a typical programming sequence, the
1342
application will use
1343
<link linkend="instructionsconfiguration">configuration instructions</link>
1344
to configure an extended trigger, and a
1345
<link linkend="commandsstreaming">command</link>,
1347
<link linkend="trig-other">TRIG_OTHER</link> as one of the trigger
1352
Extended trigger configuration should be designed in such a way
1353
that the user can probe for valid parameters, similar to how
1354
command testing works. An extended trigger configuration instruction
1355
should not configure the hardware directly, rather, the configuration
1356
should be saved until the subsequent command is issued. This
1357
allows more flexibility for future interface changes.
1361
It has not been decided whether the configuration stage should return a
1362
token that is then used as the trigger argument in the command.
1363
Using tokens is one method to satisfy the problem that extended
1364
trigger configurations may have subtle compatiblity issues with
1365
other trigger sources/arguments that can only be determined at
1366
command test time. Passing all stages of a command test should
1367
only be allowed with a properly configured extended trigger.
1371
Extended triggers must use
1372
<link linkend="insn-data-structure-data">data[1]</link> as flags. The
1373
upper 16 bits are reserved and used only for flags that are common to
1374
all extended triggers. The lower 16 bits may be defined by the
1375
particular type of extended trigger.
1379
Various types of extended triggers must use
1380
<link linkend="insn-data-structure-data">data[1]</link> to know which
1381
event the extended trigger will be assigned to in the command
1382
structure. The possible values are an OR'd mask of the following:
1393
COMEDI_EV_SCAN_BEGIN
1415
<section id="analogtriggering">
1420
<emphasis role="strong">
1421
(Status: alpha. The <function>ni_mio_common.c</function> driver
1422
implements this feature.)
1427
The <link linkend="insn-data-structure-insn">insn</link> field of the
1428
<link linkend="insn-data-structure">instruction data structure</link>
1429
has not been assigned yet.
1432
The <link linkend="insn-data-structure-chanspec">chanspec</link> field
1433
of the <link linkend="insn-data-structure">instruction data
1434
structure</link> is ignored.
1438
The <link linkend="insn-data-structure-data">data</link> field
1439
of the <link linkend="insn-data-structure">instruction data
1440
structure</link> is used as follows:
1443
data[1]: trigger and combining machine configuration.
1446
data[2]: analog triggering signal chanspec.
1449
data[3]: primary analog level.
1452
data[4]: secondary analog level.
1457
Analog triggering is described by a digital combining machine that
1458
has two sensitive digital inputs. The sensitive digital inputs are
1459
generated by configurable analog comparators. The analog comparators
1460
generate a digital 1 when the analog triggering signal is greater
1461
than the comparator level. The digital inputs are not modifier
1462
inputs. Note, however, there is an effective modifier due to the
1463
restriction that the primary analog comparator level must be less
1464
than the secondary analog comparator level.
1468
If only one analog comparator signal is used, the combining machine
1469
for the secondary input should be set to ignored, and the secondary
1470
analog level should be set to 0.
1474
The interpretation of the chanspec and voltage levels is device
1475
dependent, but should correspond to similar values of the analog
1476
input subdevice, if possible.
1480
Notes: Reading range information is not addressed. This makes it
1481
difficult to convert comparator voltages to data values.
1485
Possible extensions: A parameter that specifies the necessary time
1486
that the set condition has to be true before the trigger is generated.
1487
A parameter that specifies the necessary time that the reset condition
1488
has to be true before the state machine is reset.
1493
<section id="bitfieldmatching">
1495
Bitfield Pattern Matching Extended Trigger
1498
<emphasis role="strong">
1499
(Status: design. No driver implements this feature yet.)
1504
The <link linkend="insn-data-structure-insn">insn</link> field of the
1505
<link linkend="insn-data-structure">instruction data structure</link>
1506
has not been assigned yet.
1509
The <link linkend="insn-data-structure-chanspec">chanspec</link> field
1510
of the <link linkend="insn-data-structure">instruction data
1511
structure</link> is ignored.
1515
The <link linkend="insn-data-structure-data">data</link> field
1516
of the <link linkend="insn-data-structure">instruction data
1517
structure</link> is used as follows:
1521
data[1]: trigger flags.
1532
The pattern matching trigger issues a trigger when all of a specifed
1533
set of input lines match a specified pattern. If the device allows,
1534
the input lines should correspond to the input lines of a digital input
1535
subdevice, however, this will necessarily be device dependent. Each
1536
possible digital line that can be matched is assigned a bit in the
1537
mask and pattern. A bit set in the mask indicates that the
1538
input line must match the corresponding bit in the pattern.
1539
A bit cleared in the mask indicates that the input line is ignored.
1543
Notes: This only allows 32 bits in the pattern/mask, which may be
1544
too few. Devices may support selecting different sets of lines from
1545
which to match a pattern.
1549
Discovery: The number of bits can be discovered by setting the mask
1550
to all 1's. The driver must modify this value and return -EAGAIN.
1555
<section id="countertimer">
1557
Counter configuration
1560
<emphasis role="strong">
1561
(Status: design. No driver implements this feature yet.)
1566
The <link linkend="insn-data-structure-insn">insn</link> field of the
1567
<link linkend="insn-data-structure">instruction data structure</link>
1568
has not been assigned yet.
1571
The <link linkend="insn-data-structure-chanspec">chanspec</link> field
1572
of the <link linkend="insn-data-structure">instruction data
1573
structure</link> is used to specify which counter to use. (I.e., the
1574
counter is a &comedi; channel.)
1578
The <link linkend="insn-data-structure-data">data</link> field
1579
of the <link linkend="insn-data-structure">instruction data
1580
structure</link> is used as follows:
1584
data[1]: trigger configuration.
1587
data[2]: primary input chanspec.
1590
data[3]: primary combining machine configuration.
1593
data[4]: secondary input chanspec.
1596
data[5]: secondary combining machine configuration.
1599
data[6]: latch configuration.
1604
Note that this configuration is only useful if the counting has to be
1605
done in <emphasis>software</emphasis>. Many cards offer configurable
1606
counters in hardware; e.g., general purpose timer cards can be
1607
configured to act as pulse generators, frequency counters, timers,
1611
Counters can be operated either in synchronous mode (using
1612
<link linkend="comediinsnstructure">INSN_READ</link>)
1613
or asynchronous mode (using
1614
<link linkend="commandsstreaming">commands</link>), similar to analog
1616
The input signal for both modes is the accumulator.
1617
Commands on counter subdevices are almost always specified using
1618
<link linkend="command-data-struct-scan-begin-src">scan_begin_src</link>
1619
= <link linkend="trigother-event">TRIG_OTHER</link>, with the
1620
counter configuration also serving as the extended configuration for
1621
the scan begin source.
1625
Counters are made up of an accumulator and a combining machine that
1626
determines when the accumulator should be incremented or decremented
1627
based on the values of the input signals. The combining machine
1628
optionally determines when the accumulator should be latched and
1629
put into a buffer. This feature is used in asynchronous mode.
1633
Note: How to access multiple pieces of data acquired at each event?
1638
<section id="auxcounter">
1640
One source plus auxiliary counter configuration
1643
<emphasis role="strong">
1644
(Status: design. No driver implements this feature yet.)
1649
The <link linkend="insn-data-structure-insn">insn</link> field of the
1650
<link linkend="insn-data-structure">instruction data structure</link>
1651
has not been assigned yet.
1654
The <link linkend="insn-data-structure-chanspec">chanspec</link> field
1655
of the <link linkend="insn-data-structure">instruction data
1656
structure</link> is used to …
1660
The <link linkend="insn-data-structure-data">data</link> field
1661
of the <link linkend="insn-data-structure">instruction data
1662
structure</link> is used as follows:
1668
data[1]: is flags, including the flags for the command triggering
1669
configuration. If a command is not subsequently issued on the
1670
subdevice, the command triggering portion of the flags are ignored.
1673
data[2]: determines the mode of operation. The mode of operation
1674
is actually a bitfield that encodes what to do for various
1675
transitions of the source signals.
1679
data[3], data[4]: determine the primary source for the counter,
1681
<link linkend="command-data-struct-scan-begin-src">_src</link> and the
1682
<link linkend="command-data-struct-scan-begin-arg">_arg</link> fields
1684
<link linkend="command-data-struct">command data structure</link>.
1691
Notes: How to specify which events cause a latch and push, and what