85fddf4aa2052faf19c63a6ab958a126a75a9372
[comedilib.git] / doc / other.xml
1 <?xml version="1.0" encoding="utf-8"?>
2 <!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN"
3         "http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd" [
4 <!ENTITY % comedilib_entities SYSTEM "comedilib.ent">
5 %comedilib_entities;
6 ]>
7
8 <section id="acquisitionfunctions">
9 <title>
10 Acquisition and configuration functions
11 </title>
12
13 <para>
14 This Section gives an overview of all &comedi; functions with which
15 application programmers can implement their data acquisition. (With
16 <quote>acquisition</quote> we mean all possible kinds of interfacing
17 with the cards: input, output, configuration, streaming, etc.)
18 <xref linkend="comedireference"/> explains the function calls in full
19 detail.
20 </para>
21
22 <section id="singleacquisition">
23 <title>
24 Functions for single acquisition
25 </title>
26
27 <para>
28 The simplest form of using &comedi; is to get one single sample to or
29 from an interface card. This sections explains how to do such simple
30 <link linkend="dio">digital</link> and
31 <link linkend="singleanalog">analog</link> acquisitions.
32 </para>
33
34 <section id="dio">
35 <title>
36 Single digital acquisition
37 </title>
38
39 <para>
40 Many boards supported by &comedi; have digital input and output
41 channels; i.e., channels that can only produce a <literal>0</literal>
42 or a <literal>1</literal>.
43 Some boards allow the <emphasis>direction</emphasis> (input or output)
44 of each channel to be specified independently in software.
45 </para>
46
47 <para>
48 &comedi; groups digital channels into a
49 <emphasis>subdevice</emphasis>, which is a group of digital channels
50 that have the same characteristics.  For example, digital output lines
51 will be grouped into a digital
52 output subdevice, bidirectional digital lines will be grouped
53 into a digital I/O subdevice.  Thus, there can be multiple
54 digital subdevices on a particular board.
55 </para>
56
57 <para>
58 Individual bits on a digital I/O device can be read and written using
59 the functions <function><link linkend="func-ref-comedi-dio-read">comedi_dio_read</link></function>
60 and <function><link linkend="func-ref-comedi-dio-write">comedi_dio_write</link></function>:
61
62 <funcsynopsis><funcprototype>
63 <funcdef>int <function>comedi_dio_read</function></funcdef>
64 <paramdef>comedi_t *<parameter>device</parameter></paramdef>
65 <paramdef>unsigned int <parameter>subdevice</parameter></paramdef>
66 <paramdef>unsigned int <parameter>channel</parameter></paramdef>
67 <paramdef>unsigned int *<parameter>bit</parameter></paramdef>
68 </funcprototype></funcsynopsis>
69
70 <funcsynopsis><funcprototype>
71 <funcdef>int <function>comedi_dio_write</function></funcdef>
72 <paramdef>comedi_t *<parameter>device</parameter></paramdef>
73 <paramdef>unsigned int <parameter>subdevice</parameter></paramdef>
74 <paramdef>unsigned int <parameter>channel</parameter></paramdef>
75 <paramdef>unsigned int <parameter>bit</parameter></paramdef>
76 </funcprototype></funcsynopsis>
77
78 The <parameter class="function">device</parameter> parameter is a
79 <link linkend="ref-type-comedi-t">pointer</link>
80 to a successfully opened &comedi; device.
81 The <parameter class="function">subdevice</parameter> and
82 <parameter class="function">channel</parameter> parameters are positive
83 integers that indicate which subdevice and channel is used in the
84 acquisition. The integer <parameter class="function">bit</parameter>
85 contains the value of the acquired bit.
86 </para>
87 <para>
88 The direction of bidirectional lines can be configured using the function
89 <function><link linkend="func-ref-comedi-dio-config">comedi_dio_config</link></function>:
90
91 <funcsynopsis><funcprototype>
92 <funcdef>int <function>comedi_dio_config</function></funcdef>
93 <paramdef>comedi_t *<parameter>device</parameter></paramdef>
94 <paramdef>unsigned int <parameter>subdevice</parameter></paramdef>
95 <paramdef>unsigned int <parameter>channel</parameter></paramdef>
96 <paramdef>unsigned int <parameter>dir</parameter></paramdef>
97 </funcprototype></funcsynopsis>
98
99 The parameter <parameter class="function">dir</parameter> should be
100 either <constant>COMEDI_INPUT</constant> or
101 <constant>COMEDI_OUTPUT</constant>.
102 Many digital I/O subdevices group channels into blocks for
103 configuring direction.  Changing one channel in a block changes
104 the entire block.
105 </para>
106
107 <para>
108 Multiple channels can be read and written simultaneously using the
109 function <function><link linkend="func-ref-comedi-dio-bitfield2">comedi_dio_bitfield2</link></function>:
110
111 <funcsynopsis><funcprototype>
112 <funcdef>int <function>comedi_dio_bitfield2</function></funcdef>
113 <paramdef>comedi_t *<parameter>device</parameter></paramdef>
114 <paramdef>unsigned int <parameter>subdevice</parameter></paramdef>
115 <paramdef>unsigned int <parameter>write_mask</parameter></paramdef>
116 <paramdef>unsigned int *<parameter>bits</parameter></paramdef>
117 <paramdef>unsigned int <parameter>base_channel</parameter></paramdef>
118 </funcprototype></funcsynopsis>
119
120 Each channel from <parameter class="function">base_channel</parameter>
121 to <parameter class="function">base_channel</parameter> &plus;
122 <literal>31</literal> is assigned to a bit in the
123 <parameter class="function">write_mask</parameter> and
124 <parameter class="function">bits</parameter>
125 bitfield with bit 0 assigned to channel
126 <parameter class="function">base_channel</parameter>, bit 1 assigned to channel
127 <parameter class="function">base_channel</parameter> &plus;
128 <literal>1</literal>, etc.  If a bit in
129 <parameter class="function">write_mask</parameter> is set, the
130 corresponding bit in <parameter class="function">*bits</parameter> will
131 be written to the digital output line corresponding to the channel given by
132 <parameter class="function">base_channel</parameter> plus the bit number.
133 Each digital line is then read and placed into
134 <parameter class="function">*bits</parameter>.  The value
135 of bits in <parameter class="function">*bits</parameter> corresponding
136 to digital output lines is undefined and device-specific.  Channel
137 <parameter class="function">base_channel</parameter> &plus;
138 <literal>0</literal> is the least significant bit in the bitfield.  No
139 more than 32 channels at once can be accessed using this method.
140 <emphasis role="strong">Warning!</emphasis> Older versions of &comedi;
141 may ignore <parameter class="function">base_channel</parameter> and treat
142 it as <literal>0</literal> unless the subdevice has more than 32 channels.
143 </para>
144
145 <para>
146 The digital acquisition functions seem to be very simple, but, behind
147 the implementation screens of the &comedi; kernel module, they are
148 executed as special cases of the general
149 <link linkend="instructions">instruction</link> command.
150 </para>
151
152
153 </section>
154
155
156 <section id="singleanalog">
157 <title>
158 Single analog acquisition
159 </title>
160 <para>
161 Analog &comedi; channels can produce data values that are
162 <emphasis>samples</emphasis> from continuous analog signals.
163 These samples are integers with a significant content in
164 the range of, typically, 8, 10, 12, or 16 bits.
165 </para>
166 <para>
167 Single samples can be read from an analog channel using the function
168 <function><link linkend="func-ref-comedi-data-read">comedi_data_read</link></function>:
169
170 <funcsynopsis><funcprototype>
171 <funcdef>int <function>comedi_data_read</function></funcdef>
172 <paramdef>comedi_t *<parameter>device</parameter></paramdef>
173 <paramdef>unsigned int <parameter>subdevice</parameter></paramdef>
174 <paramdef>unsigned int <parameter>channel</parameter></paramdef>
175 <paramdef>unsigned int <parameter>range</parameter></paramdef>
176 <paramdef>unsigned int <parameter>aref</parameter></paramdef>
177 <paramdef>lsampl_t *<parameter>data</parameter></paramdef>
178 </funcprototype></funcsynopsis>
179
180 This reads one such data value from a &comedi; channel, and puts it in
181 the user-specified <parameter>data</parameter> buffer.
182 </para>
183
184 <para>
185 In the opposite direction, single samples can be written to an analog output
186 channel using the function
187 <function><link linkend="func-ref-comedi-data-write">comedi_data_write</link></function>:
188
189 <funcsynopsis><funcprototype>
190 <funcdef>int <function>comedi_data_write</function></funcdef>
191 <paramdef>comedi_t *<parameter>device</parameter></paramdef>
192 <paramdef>unsigned int <parameter>subdevice</parameter></paramdef>
193 <paramdef>unsigned int <parameter>channel</parameter></paramdef>
194 <paramdef>unsigned int <parameter>range</parameter></paramdef>
195 <paramdef>unsigned int <parameter>aref</parameter></paramdef>
196 <paramdef>lsampl_t <parameter>data</parameter></paramdef>
197 </funcprototype></funcsynopsis>
198 </para>
199
200 <para>
201 Raw data values read or written by the above functions
202 are unsigned integers less than, or equal to, the maximum sample value
203 of the channel, which can be determined using the function
204 <function><link linkend="func-ref-comedi-get-maxdata">comedi_get_maxdata</link></function>:
205
206 <funcsynopsis><funcprototype>
207 <funcdef>lsampl_t <function>comedi_get_maxdata</function></funcdef>
208 <paramdef>comedi_t *<parameter>device</parameter></paramdef>
209 <paramdef>unsigned int <parameter>subdevice</parameter></paramdef>
210 <paramdef>unsigned int <parameter>channel</parameter></paramdef>
211 </funcprototype></funcsynopsis>
212
213 Conversion between raw data values and uncalibrated physical units can
214 be performed by the functions
215 <function><link linkend="func-ref-comedi-to-phys">comedi_to_phys</link></function>
216 and <function><link linkend="func-ref-comedi-from-phys">comedi_from_phys</link></function>:
217
218 <funcsynopsis><funcprototype>
219 <funcdef>double <function>comedi_to_phys</function></funcdef>
220 <paramdef>lsampl_t <parameter>data</parameter></paramdef>
221 <paramdef>comedi_range *<parameter>range</parameter></paramdef>
222 <paramdef>lsampl_t <parameter>maxdata</parameter></paramdef>
223 </funcprototype></funcsynopsis>
224
225 <funcsynopsis><funcprototype>
226 <funcdef>lsampl_t <function>comedi_from_phys</function></funcdef>
227 <paramdef>double <parameter>data</parameter></paramdef>
228 <paramdef>comedi_range *<parameter>range</parameter></paramdef>
229 <paramdef>lsampl_t <parameter>maxdata</parameter></paramdef>
230 </funcprototype></funcsynopsis>
231 </para>
232
233 <para>
234 There are two data structures in these commands that are not fully
235 self-explanatory:
236 <itemizedlist>
237
238 <listitem>
239 <para>
240 <type><link linkend="ref-type-comedi-t">comedi_t</link></type>: this data structure
241 contains all information that a user program has to know about an
242 <emphasis>open</emphasis> &comedi; device. The programmer doesn't have
243 to fill in this data structure manually: it gets filled in by opening
244 the device.
245 </para>
246 </listitem>
247
248 <listitem>
249 <para>
250 <type><link linkend="ref-type-lsampl-t">lsampl_t</link></type>: this
251 <quote>data structure</quote> represents one single sample. On most
252 architectures, it's nothing more than a 32 bits value. Internally,
253 &comedi; does some conversion from raw sample data to
254 <quote>correct</quote> integers. This is called <quote>data
255 munging</quote>.
256 </para>
257 </listitem>
258
259 </itemizedlist>
260 </para>
261 <para>
262 Each single acquisition by, for example,
263 <function><link linkend="func-ref-comedi-data-read">comedi_data_read</link></function>
264 requires quite some overhead, because all the arguments of the
265 function call are checked. If multiple acquisitions must be done on
266 the same channel, this overhead can be avoided by using a function
267 that can read more than one sample,
268 <function><link linkend="func-ref-comedi-data-read-n">comedi_data_read_n</link></function>:
269
270 <funcsynopsis><funcprototype>
271 <funcdef>int <function>comedi_data_read_n</function></funcdef>
272 <paramdef>comedi_t *<parameter>device</parameter></paramdef>
273 <paramdef>unsigned int <parameter>subdevice</parameter></paramdef>
274 <paramdef>unsigned int <parameter>channel</parameter></paramdef>
275 <paramdef>unsigned int <parameter>range</parameter></paramdef>
276 <paramdef>unsigned int <parameter>aref</parameter></paramdef>
277 <paramdef>lsampl_t *<parameter>data</parameter></paramdef>
278 <paramdef>unsigned int <parameter>n</parameter></paramdef>
279 </funcprototype></funcsynopsis>
280
281 The number of samples, <parameter class="function">n</parameter>, is
282 limited by the &comedi; implementation (to a maximum of 100 samples),
283 because the call is blocking.
284 </para>
285 <para>
286 The start of the a single data acquisition can also be delayed by a specified
287 number of nano-seconds using the function
288 <function><link linkend="func-ref-comedi-data-read-delayed">comedi_data_read_delayed</link></function>:
289
290 <funcsynopsis><funcprototype>
291 <funcdef>int <function>comedi_data_read_delayed</function></funcdef>
292 <paramdef>comedi_t *<parameter>device</parameter></paramdef>
293 <paramdef>unsigned int <parameter>subdevice</parameter></paramdef>
294 <paramdef>unsigned int <parameter>channel</parameter></paramdef>
295 <paramdef>unsigned int <parameter>range</parameter></paramdef>
296 <paramdef>unsigned int <parameter>aref</parameter></paramdef>
297 <paramdef>lsampl_t *<parameter>data</parameter></paramdef>
298 <paramdef>unsigned int <parameter>nano_sec</parameter></paramdef>
299 </funcprototype></funcsynopsis>
300 </para>
301
302 <para>
303 All these read and write acquisition functions are implemented on top
304 of the generic <link linkend="instructions">instruction</link>
305 command.
306 </para>
307
308 </section>
309
310 </section>
311
312
313 <section id="instructions">
314 <title>
315 Instructions for multiple acquisitions
316 </title>
317 <para>
318 The <emphasis>instruction</emphasis> is one of the most generic,
319 overloaden and flexible functions in the &comedi; API. It is used to
320 execute a multiple of identical acquisitions on the same channel, but
321 also to perform a
322 <link linkend="instructionsconfiguration">configuration</link> of a
323 channel.
324 <anchor id="anchor.instruction.list"/>
325 An <emphasis>instruction list</emphasis> is a list of instructions,
326 possibly on different channels. Both instructions and instructions
327 lists are executed <emphasis>synchronously</emphasis>, i.e., while
328 <emphasis role="strong">blocking</emphasis> the calling process.
329 This is one of the limitations of instructions; the other one is that
330 they cannot code an acquisition involving timers or external events.
331 These limits are eliminated by the
332 <link linkend="commandsstreaming">command</link> acquisition
333 primitive.
334 </para>
335
336
337 <section id="comediinsnstructure">
338 <title>
339 The instruction data structure
340 </title>
341 <para>
342 All the information needed to execute an instruction is stored in the
343 <type><link linkend="ref-type-comedi-insn">comedi_insn</link></type>
344 data structure:
345 <programlisting>
346 typedef struct <anchor id="insn-data-structure"/>comedi_insn_struct {
347   <anchor id="insn-data-structure-insn"/>unsigned int insn;      // integer encoding the type of acquisition
348                           // (or configuration)
349   unsigned int n;         // number of elements in data array
350   <link linkend="ref-type-lsampl-t">lsampl_t</link> <anchor id="insn-data-structure-data"/>*data;         // pointer to data buffer
351   unsigned int subdev;    // subdevice
352   unsigned int <anchor id="insn-data-structure-chanspec"/><link linkend="ref-macro-CR-PACK">chanspec</link>; // encoded channel specification
353   unsigned int unused[3];
354 } comedi_insn;
355 </programlisting>
356 Because of the large flexibility of the instruction function, many
357 types of instruction do not need to fill in all fields, or attach
358 different meanings to the same field. But the current implementation
359 of &comedi; requires the
360 <structfield><link linkend="insn-data-structure-data">data</link></structfield>
361 field to be at least one byte long.
362 </para>
363
364 <para>
365 The <structfield><link linkend="insn-data-structure-insn">insn</link></structfield> member of the
366 <link linkend="insn-data-structure">instruction data structure</link>
367 determines the type of acquisition executed in the corresponding
368 instruction:
369 <itemizedlist>
370
371 <listitem>
372 <para>
373 <constant>INSN_READ</constant>: the instruction executes a read on an
374 analog channel.
375 </para>
376 </listitem>
377
378 <listitem>
379 <para>
380 <constant>INSN_WRITE</constant>: the instruction executes a write on an
381 analog channel.
382 </para>
383 </listitem>
384
385 <listitem>
386 <para>
387 <constant>INSN_BITS</constant>: indicates that the instruction must
388 read or write values on multiple digital I/O channels.
389 </para>
390 </listitem>
391
392 <listitem>
393 <para>
394 <constant>INSN_GTOD</constant>: the instruction performs a
395 <quote>Get Time Of Day</quote> acquisition.
396 </para>
397 </listitem>
398
399 <listitem>
400 <para>
401 <constant>INSN_WAIT</constant>: the instruction blocks for a specified
402 number of nanoseconds.
403 </para>
404 </listitem>
405
406 </itemizedlist>
407 </para>
408
409 </section>
410
411
412 <section id="instructionexecution">
413 <title>
414 Instruction execution
415 </title>
416 <para>
417 Once an instruction data structure has been filled in, the
418 corresponding instruction is executed with the function
419 <function><link linkend="func-ref-comedi-do-insn">comedi_do_insn</link></function>:
420
421 <funcsynopsis><funcprototype>
422 <funcdef>int <function>comedi_do_insn</function></funcdef>
423 <paramdef>comedi_t *<parameter>device</parameter></paramdef>
424 <paramdef>comedi_insn *<parameter>instruction</parameter></paramdef>
425 </funcprototype></funcsynopsis>
426
427 Many &comedi; instructions are shortcuts that relieve the programmer
428 from explicitly filling in the data structure and calling the
429 <function><link linkend="func-ref-comedi-do-insn">comedi_do_insn</link></function>
430 function.
431 </para>
432 <para>
433 A list of instructions can be executed in one function call using the function
434 <function><link linkend="func-ref-comedi-do-insnlist">comedi_do_insnlist</link></function>:
435
436 <funcsynopsis><funcprototype>
437 <funcdef>int <function>comedi_do_insnlist</function></funcdef>
438 <paramdef>comedi_t *<parameter>device</parameter></paramdef>
439 <paramdef>comedi_insnlist *<parameter>list</parameter></paramdef>
440 </funcprototype></funcsynopsis>
441
442 The parameter <parameter class="function">list</parameter> is a pointer to a
443 <type><link linkend="insnlist-data-structure">comedi_insnlist</link></type>
444 data structure holding a pointer to an array of <type>comedi_insn</type>
445 and the number of instructions in the list:
446 <programlisting>
447 typedef struct <anchor id="insnlist-data-structure"/>comedi_insnlist_struct {
448   unsigned int n_insns;
449   comedi_insn *insns;
450 } comedi_insnlist;
451 </programlisting>
452 </para>
453 <para>
454 The number of instructions in the list is limited in the
455 implementation, because instructions are executed
456 <emphasis>synchronously</emphasis>, i.e., the call blocks until the
457 whole instruction (list) has finished.
458 </para>
459
460 </section>
461
462 </section>
463
464
465 <section id="instructionsconfiguration">
466 <title>
467 Instructions for configuration
468 </title>
469 <para>
470 <xref linkend="instructions"/> explains how instructions are used to do
471 <emphasis>acquisition</emphasis> on channels. This section explains
472 how they are used to <emphasis>configure</emphasis> a subdevice.
473 There are various sorts of configurations, and the
474 specific information for each different configuration possibility is
475 to be specified via the
476 <structfield><link linkend="insn-data-structure-data">data</link></structfield>
477 buffer of the
478 <link linkend="insn-data-structure">instruction data structure</link>.
479 (So, the pointer to a
480 <type><link linkend="ref-type-lsampl-t">lsampl_t</link></type>
481 is misused as a pointer to an array with board-specific information.)
482 </para>
483
484 <para>
485 Using <constant>INSN_CONFIG</constant> as the
486 <structfield><link linkend="insn-data-structure-insn">insn</link></structfield>
487 member in an
488 <link linkend="insn-data-structure">instruction data structure</link>
489 indicates that the instruction will
490 <emphasis>not perform acquisition</emphasis> on a
491 channel, but will <emphasis>configure</emphasis> that channel.
492 The
493 <structfield><link linkend="ref-macro-CR-PACK">chanspec</link></structfield>
494 member in the
495 <type><link linkend="insn-data-structure-chanspec">comedi_insn</link></type>
496 data structure, contains the channel to be configured.
497 The zeroth element of the data array
498 is always an id that specifies
499 what type of configuration instruction is being performed.  The
500 meaning of rest of the elements in the data array
501 depend on the configuration instruction id.
502 Some of the
503 possible ids are summarised in the table below, along with the
504 meanings of the data array elements for
505 each type of configuration instruction.
506 </para>
507
508 <informaltable>
509 <tgroup cols='4' align='left'>
510 <colspec colwidth='4*' />
511 <colspec colwidth='4*' />
512 <colspec colwidth='1*' />
513 <colspec colwidth='4*' />
514 <thead>
515 <row>
516 <entry>data[0]</entry>
517 <entry>Description</entry>
518 <entry>n (number of elements in data array)</entry>
519 <entry>Meanings of data[1], ..., data[n-1]</entry>
520 </row>
521 </thead>
522 <tbody>
523 <row>
524 <entry><constant>INSN_CONFIG_DIO_INPUT</constant></entry>
525 <entry>
526 Configure a DIO line as input.  It is easier to use
527 <function><link linkend="func-ref-comedi-dio-config">comedi_dio_config</link></function>
528 than to use this configuration instruction directly.
529 </entry>
530 <entry>1</entry>
531 <entry>
532 n/a
533 </entry>
534 </row>
535 <row>
536 <entry><constant>INSN_CONFIG_DIO_OUTPUT</constant></entry>
537 <entry>
538 Configure a DIO line as output.  It is easier to use
539 <function><link linkend="func-ref-comedi-dio-config">comedi_dio_config</link></function>
540 than to use this configuration instruction directly.
541 </entry>
542 <entry>1</entry>
543 <entry>
544 n/a
545 </entry>
546 </row>
547 <row>
548 <entry><constant>INSN_CONFIG_ALT_SOURCE</constant></entry>
549 <entry>
550 Select an alternate input source.  This instruction is used by calibration
551 programs to configure analog input channels
552 which can be redirected to read internal calibration
553 references.  You need to set the <constant>CR_ALT_SOURCE</constant> flag in the chanspec
554 when reading to actually read from the configured alternate input source.
555 If you are using <function>comedi_data_read</function>, then the channel parameter can be
556 bitwise or'd with the <constant>CR_ALT_SOURCE</constant> flag.
557 </entry>
558 <entry>2</entry>
559 <entry>
560 data[1]: alternate input source.
561 </entry>
562 </row>
563 <row>
564 <entry><constant>INSN_CONFIG_BLOCK_SIZE</constant></entry>
565 <entry>
566 Specify block size for asynchonous command data.
567 When performing streaming input, many boards accumulate
568 samples in internal fifos and transfer them to the host
569 computer in chunks.  Some drivers let you suggest a size in bytes for how big a
570 the chunks should be.  This lets you tune how often the host computer is
571 interrupted with a new chunk of data.
572 </entry>
573 <entry>2</entry>
574 <entry>
575 data[1]: The desired block size in bytes.  The actual configured block size is
576 writen back to data[1] after the instruction completes.  This instruction
577 acts purely as a query if the block size is set to zero.
578 </entry>
579 </row>
580 <row>
581 <entry><constant>INSN_CONFIG_DIO_QUERY</constant></entry>
582 <entry>
583 Queries the configuration of a DIO line to see if it is an input or output.
584 It is probably easier to use the comedilib function
585 <function><link linkend="func-ref-comedi-dio-get-config">comedi_dio_get_config</link></function>
586 than to use this instruction directly.
587 </entry>
588 <entry>2</entry>
589 <entry>
590 data[1]: The instruction sets this element to either
591 <constant>COMEDI_INPUT</constant> or <constant>COMEDI_OUTPUT</constant>.
592 </entry>
593 </row>
594 </tbody>
595 </tgroup>
596 </informaltable>
597
598 <para>
599 See the comedilib demo program <filename>demo/choose_clock.c</filename> for an example
600 of using a configuration instruction.
601 </para>
602
603 </section>
604
605
606 <section id="inttrigconfiguration">
607 <title>
608 Instruction for internal triggering
609 </title>
610 <para>
611 This special instruction has
612 <anchor id="insn-inttrig"/><constant>INSN_INTTRIG</constant> as the
613 <structfield><link linkend="insn-data-structure-insn">insn</link></structfield>
614 member in its
615 <link linkend="insn-data-structure">instruction data structure</link>.
616 Its execution causes an
617 <link linkend="trig-int-start-src">internal triggering event</link>. This
618 event can, for example, cause the device driver to start a conversion,
619 or to stop an ongoing acquisition. The exact meaning of the triggering
620 depends on the card and its particular driver.
621 </para>
622 <para>
623 The
624 <structfield><link linkend="insn-data-structure-data">data</link></structfield>[0] element of the
625 <constant>INSN_INTTRIG</constant> instruction is reserved for future use,
626 and should be set to <literal>0</literal>.
627 </para>
628
629 </section>
630
631
632 <section id="commandsstreaming">
633 <title>
634 Commands for streaming acquisition
635 </title>
636
637 <para>
638 The most powerful &comedi; acquisition primitive is the
639 <emphasis>command</emphasis>. It's powerful because, with one single
640 command, the programmer launches:
641 <itemizedlist>
642
643 <listitem>
644 <para>
645 a possibly infinite <emphasis>sequence of acquisitions</emphasis>,
646 </para>
647 </listitem>
648
649 <listitem>
650 <para>
651 accompanied with various <emphasis>callback</emphasis> functionalities
652 (DMA, interrupts, driver-specific callback functions),
653 </para>
654 </listitem>
655
656 <listitem>
657 <para>
658 for <emphasis>any number of channels</emphasis>,
659 </para>
660 </listitem>
661
662 <listitem>
663 <para>
664 with an <emphasis>arbitrary order</emphasis> of channels in each scan
665 (possibly even with repeated channels per scan),
666 </para>
667 </listitem>
668
669 <listitem>
670 <para>
671 and with various scan <emphasis>triggering sources</emphasis>,
672 external (i.e., hardware pulses) as well as internal (i.e., pulses
673 generated on the DAQ card itself, or generated by a
674 <link linkend="inttrigconfiguration">software trigger instruction</link>).
675 </para>
676 </listitem>
677
678 </itemizedlist>
679 This command functionality exists in the &comedi; API, because various
680 data acquisition devices have the capability to perform this kind of
681 complex acquisition, driven by either on-board or
682 off-board timers and triggers.
683 </para>
684
685 <para>
686 A command specifies a particular data
687 <link linkend="fig-acq-seq">acquisition sequence</link>, which
688 consists of a number of <emphasis>scans</emphasis>, and each scan is
689 comprised of a number of <emphasis>conversions</emphasis>, which
690 usually corresponds to a single A/D or D/A conversion. So, for
691 example, a scan could consist of sampling channels 1, 2 and 3 of a
692 particular device, and this scan should be repeated 1000 times, at
693 intervals of 1 millisecond apart.
694 </para>
695 <para>
696 The command function is complementary to the
697 <link linkend="instructionsconfiguration">configuration instruction</link>
698 function: each channel in the command's
699 <structfield><link linkend="command-data-struct-chanlist">chanlist</link></structfield>
700 should first be configured by an appropriate instruction.
701 </para>
702
703
704 <section id="executingcommand">
705 <title>
706 Executing a command
707 </title>
708
709 <para>
710 A command is executed by the function
711 <function><link linkend="func-ref-comedi-command">comedi_command</link></function>:
712
713 <funcsynopsis><funcprototype>
714 <funcdef>int <function>comedi_command</function></funcdef>
715 <paramdef>comedi_t *<parameter>device</parameter></paramdef>
716 <paramdef>comedi_cmd *<parameter>command</parameter></paramdef>
717 </funcprototype></funcsynopsis>
718
719 The following sections explain the meaning of the
720 <type><link linkend="ref-type-comedi-cmd">comedi_cmd</link></type> data structure.
721 Filling in this structure can be quite complicated, and
722 requires good knowledge about the exact functionalities of the DAQ
723 card.  So, before launching a command, the application programmer is
724 adviced to check whether this complex command data structure can be
725 successfully parsed. So, the typical sequence for executing a command is
726 to first send the command through
727 <function><link linkend="func-ref-comedi-command-test">comedi_command_test</link></function>
728 once or twice.  The test will check that the command is valid for the
729 particular device, and often makes some adjustments to the command
730 arguments, which can then be read back by the user to see the actual
731 values used.
732 </para>
733 <para>
734 A &comedi; program can find out on-line what the command capabilities
735 of a specific device are, by means of the
736 <function><link linkend="func-ref-comedi-get-cmd-src-mask">comedi_get_cmd_src_mask</link></function>
737 function.
738 </para>
739
740 </section>
741
742
743 <section id="comedicmdstructure">
744 <title>
745 The command data structure
746 </title>
747 <para>
748 The command executes according to the information about the requested
749 acquisition, which is stored in the
750 <type><link linkend="ref-type-comedi-cmd">comedi_cmd</link></type>
751 <anchor id="command-data-struct"/>data structure:
752 <programlisting>
753 typedef struct comedi_cmd_struct comedi_cmd;
754
755 struct comedi_cmd_struct {
756   unsigned int subdev;         // which subdevice to sample
757   unsigned int <anchor id="command-data-struct-flags"/>flags;          // encode some configuration possibilities
758                                // of the command execution; e.g.,
759                                // whether a callback routine is to be
760                                // called at the end of the command
761
762   unsigned int <anchor id="command-data-struct-start-src"/>start_src;      // event to make the acquisition start
763   unsigned int <anchor id="command-data-struct-start-arg"/>start_arg;      // parameters that influence this start
764
765   unsigned int <anchor id="command-data-struct-scan-begin-src"/>scan_begin_src; // event to make a particular scan start
766   unsigned int <anchor id="command-data-struct-scan-begin-arg"/>scan_begin_arg; // parameters that influence this start`
767
768   unsigned int <anchor id="command-data-struct-convert-src"/>convert_src;    // event to make a particular conversion start
769   unsigned int <anchor id="command-data-struct-convert-arg"/>convert_arg;    // parameters that influence this start
770
771   unsigned int <anchor id="command-data-struct-scan-end-src"/>scan_end_src;   // event to make a particular scan terminate
772   unsigned int <anchor id="command-data-struct-scan-end-arg"/>scan_end_arg;   // parameters that influence this termination
773
774   unsigned int <anchor id="command-data-struct-stop-src"/>stop_src;       // what make the acquisition terminate
775   unsigned int <anchor id="command-data-struct-stop-arg"/>stop_arg;       // parameters that influence this termination
776
777   unsigned int <anchor id="command-data-struct-chanlist"/>*chanlist;      // pointer to list of channels to be sampled
778   unsigned int <anchor id="command-data-struct-chanlist-len"/>chanlist_len;   // number of channels to be sampled
779
780   sampl_t *<anchor id="command-data-struct-data"/>data;               // address of buffer
781   unsigned int <anchor id="command-data-struct-data-len"/>data_len;       // number of samples to acquire
782 };
783 </programlisting>
784 The start and end of the whole command acquisition sequence, and the
785 start and end of each scan and of each conversion, is triggered by a
786 so-called <emphasis>event</emphasis>. More on these in
787 <xref linkend="comedicmdsources"/>.
788 </para>
789
790 <para>
791 The <parameter class="function">subdev</parameter> member of the
792 <type><link linkend="ref-type-comedi-cmd">comedi_cmd</link></type> structure is
793 the index of the subdevice the command is intended for.  The
794 <function><link linkend="func-ref-comedi-find-subdevice-by-type">comedi_find_subdevice_by_type</link></function>
795 function can be useful in discovering the index of your desired subdevice.
796 </para>
797
798 <para>
799 The <structfield><link linkend="command-data-struct-chanlist">chanlist</link></structfield>
800 member of the
801 <type><link linkend="ref-type-comedi-cmd">comedi_cmd</link></type> data
802 structure should point to an array whose number of elements is
803 specified by
804 <structfield><link linkend="command-data-struct-chanlist-len">chanlist_len</link></structfield>
805 (this will generally be the same as the
806 <structfield><link linkend="command-data-struct-scan-end-arg">scan_end_arg</link></structfield>).
807 The
808 <structfield><link linkend="command-data-struct-chanlist">chanlist</link></structfield>
809 specifies the sequence of channels and gains (and analog references)
810 that should be stepped through for each scan.  The elements of the
811 <structfield><link linkend="command-data-struct-chanlist">chanlist</link></structfield> array should be
812 initialized by <quote>packing</quote> the channel, range and reference
813 information together with the
814 <function><link linkend="ref-macro-CR-PACK">CR_PACK</link></function>
815 macro.
816 </para>
817
818 <para>
819 The <structfield><link linkend="command-data-struct-data">data</link></structfield> and
820 <structfield><link linkend="command-data-struct-data-len">data_len</link></structfield>
821 members can be safely ignored when issueing commands from a user-space
822 program.  They only have meaning when a command is sent from a
823 <emphasis role="strong">kernel</emphasis> module using the
824 <systemitem>kcomedilib</systemitem> interface, in which case they specify
825 the buffer where the driver should write/read its data to/from.
826 </para>
827
828 <para>
829 The final member of the
830 <type><link linkend="command-data-struct">comedi_cmd</link></type> structure is the
831 <structfield><link linkend="command-data-struct-flags">flags</link></structfield> field,
832 i.e., bits in a word that can be bitwise-or'd together. The meaning of
833 these bits are explained in
834 <xref linkend="comedicmdflags"/>.
835 </para>
836
837 </section>
838
839
840 <section id="comedicmdsources">
841 <title>
842 The command trigger events
843 <anchor id="source.trigger.anchor"/>
844 </title>
845 <para>
846 A command is a very versatile acquisition instruction, in the sense
847 that it offers lots of possibilities to let different hardware and
848 software sources determine when acquisitions are started, performed,
849 and stopped. More specifically, the command
850 <link linkend="command-data-struct">data structure</link>
851 has <emphasis>five</emphasis> types of events: start the
852 <link linkend="acquisitionterminology">acquisition</link>,
853 start a <link linkend="scan">scan</link>, start a
854 <link linkend="conversion">conversion</link>, stop a scan, and stop
855 the acquisition.  Each event can be given its own
856 <emphasis><link linkend="source.trigger.anchor">source</link></emphasis>
857 (the <parameter class="function">&hellip;_src</parameter> members in the
858 <type><link linkend="ref-type-comedi-cmd">comedi_cmd</link></type> data
859 structure). And each event source can have a corresponding
860 argument (the <parameter class="function">&hellip;_arg</parameter> members of
861 the <type><link linkend="ref-type-comedi-cmd">comedi_cmd</link></type> data
862 structure) whose meaning depends on the type of source trigger.
863 For example, to specify an external digital line <quote>3</quote> as a
864 source (in general, <emphasis>any</emphasis> of the five event
865 sources), you would use
866 <parameter>src</parameter>=<constant><link linkend="trig-ext">TRIG_EXT</link></constant>
867 and <parameter>arg</parameter>=<literal>3</literal>.
868 </para>
869 <para>
870 The following paragraphs discuss in somewhat more detail the trigger
871 event sources(<parameter class="function">&hellip;_src</parameter>), and the
872 corresponding arguments (<parameter class="function">&hellip;_arg</parameter>).
873 </para>
874 <para>
875 The start of an acquisition is controlled by the
876 <structfield><link linkend="command-data-struct-start-src">start_src</link></structfield> events.
877 The available options are:
878 <itemizedlist>
879
880 <listitem>
881 <para>
882 <anchor id="trig-now-start-src"/>
883 <constant>TRIG_NOW</constant>: the <quote>start</quote> event occurs
884 <structfield><link linkend="command-data-struct-start-arg">start_arg</link></structfield>
885 nanoseconds after the command is set up. Currently, only
886 <structfield><link linkend="command-data-struct-start-arg">start_arg</link></structfield>=<literal>0</literal> is
887 supported.
888 </para>
889 </listitem>
890
891 <listitem>
892 <para>
893 <anchor id="trig-follow-start-src"/>
894 <constant>TRIG_FOLLOW</constant>:  (For an output device.) The <quote>start</quote>
895 event occurs when data is written to the buffer.
896 </para>
897 </listitem>
898
899 <listitem>
900 <para>
901 <anchor id="trig-ext-start-src"/>
902 <constant>TRIG_EXT</constant>: the <quote>start</quote> event occurs when an
903 external trigger signal occurs; e.g., a rising edge of a digital line.
904 <structfield><link linkend="command-data-struct-start-arg">start_arg</link></structfield>
905 chooses the particular digital line.
906 </para>
907 </listitem>
908
909 <listitem>
910 <para>
911 <anchor id="trig-int-start-src"/>
912 <constant>TRIG_INT</constant>: the <quote>start</quote> event occurs on a &comedi;
913 internal signal, which is typically caused by an
914 <constant><link linkend="insn-inttrig">INSN_INTTRIG</link></constant>
915 instruction.
916 </para>
917 </listitem>
918
919 </itemizedlist>
920 The start of the beginning of each
921 <link linkend="scan">scan</link> is controlled by the
922 <structfield><link linkend="command-data-struct-scan-begin-src">scan_begin_src</link></structfield> events.
923 The available options are:
924 <itemizedlist>
925
926 <listitem>
927 <para>
928 <anchor id="trig-timer-start-scan"/>
929 <constant>TRIG_TIMER</constant>: <quote>scan begin</quote>
930 events occur periodically.  The time between <quote>scan begin</quote>
931 events is
932 <structfield><link linkend="command-data-struct-scan-begin-arg">scan_begin_arg</link></structfield>
933 nanoseconds.
934 </para>
935 </listitem>
936
937 <listitem>
938 <para>
939 <anchor id="trig-follow-start-scan"/>
940 <constant>TRIG_FOLLOW</constant>:  The <quote>scan begin</quote>
941 event occurs immediately after a <quote>scan end</quote>
942 event occurs.
943 </para>
944 </listitem>
945
946 <listitem>
947 <para>
948 <anchor id="trig-ext-start-scan"/>
949 <constant>TRIG_EXT</constant>: the <quote>scan begin</quote>
950 event occurs when an external trigger signal
951 occurs; e.g., a rising edge of a digital line.
952 <structfield><link linkend="command-data-struct-scan-begin-arg">scan_begin_arg</link></structfield>
953 chooses the particular digital line.
954 </para>
955 </listitem>
956
957 </itemizedlist>
958 The
959 <structfield><link linkend="command-data-struct-scan-begin-arg">scan_begin_arg</link></structfield>
960 used here may not be supported exactly by the device, but it
961 will be adjusted to the nearest supported value by
962 <function><link linkend="func-ref-comedi-command-test">comedi_command_test</link></function>.
963 </para>
964 <para>
965 The timing between each sample in a
966 <link linkend="scan">scan</link> is controlled by the
967 <structfield><link linkend="command-data-struct-convert-src">convert_src</link></structfield>
968 events.
969 The available options are:
970 <itemizedlist>
971
972 <listitem>
973 <para>
974 <anchor id="convert-trig-timer"/>
975 <anchor id="trig-timer"/>
976 <constant>TRIG_TIMER</constant>: the conversion events occur periodically.
977 The time between <quote>convert</quote> events is
978 <structfield><link linkend="command-data-struct-convert-arg">convert_arg</link></structfield>
979 nanoseconds.
980 </para>
981 </listitem>
982
983 <listitem>
984 <para>
985 <anchor id="convert-trig-ext"/>
986 <anchor id="trig-ext"/>
987 <constant>TRIG_EXT</constant>: the conversion events occur when an
988 external trigger signal occurs, e.g., a rising edge of a digital line.
989 <structfield><link linkend="command-data-struct-convert-arg">convert_arg</link></structfield>
990 chooses the particular digital line.
991 </para>
992 </listitem>
993
994 <listitem>
995 <para>
996 <anchor id="convert-trig-now"/>
997 <anchor id="trig-now"/>
998 <constant>TRIG_NOW</constant>: All conversion events in a
999 <link linkend="scan">scan</link> occur simultaneously.
1000 </para>
1001 </listitem>
1002
1003 </itemizedlist>
1004 The <emphasis>end</emphasis> of each scan is almost always specified
1005 by setting the
1006 <structfield><link linkend="command-data-struct-scan-end-src">scan_end_src</link></structfield>
1007 event to
1008 <constant><link linkend="trig-count">TRIG_COUNT</link></constant>,
1009 with the argument being the same as the number of channels in the
1010 <structfield><link linkend="command-data-struct-chanlist">chanlist</link></structfield>.  You
1011 could probably find a device that allows something else, but it would
1012 be strange.
1013 </para>
1014 <para>
1015 The end of an
1016 <link linkend="acquisitionterminology">acquisition</link> is
1017 controlled by
1018 <structfield><link linkend="command-data-struct-stop-src">stop_src</link></structfield> event.
1019 The available options are:
1020 <itemizedlist>
1021
1022 <listitem>
1023 <para>
1024 <anchor id="acquisition-end-trig-count"/>
1025 <anchor id="trig-count"/>
1026 <constant>TRIG_COUNT</constant>: stop the acquisition after
1027 <structfield><link linkend="command-data-struct-stop-arg">stop_arg</link></structfield>
1028 scans.
1029 </para>
1030 </listitem>
1031
1032 <listitem>
1033 <para>
1034 <anchor id="acquisition-end-trig-none"/>
1035 <anchor id="trig-none"/>
1036 <constant>TRIG_NONE</constant>: perform continuous acquisition,
1037 until stopped using
1038 <function><link linkend="func-ref-comedi-cancel">comedi_cancel</link></function>.
1039 </para>
1040 <para>
1041 Its <structfield>stop_arg</structfield> argument is reserved and should be set to <literal>0</literal>.
1042 (<quote>Reserved</quote>
1043 means that unspecified things could happen if it is set to something
1044 else but <literal>0</literal>.)
1045 </para>
1046 </listitem>
1047
1048 </itemizedlist>
1049 There are a couple of less usual or not yet implemented events:
1050 <itemizedlist>
1051
1052 <listitem>
1053 <para>
1054 <anchor id="trig-time"/>
1055 <constant>TRIG_TIME</constant>:
1056 cause an event to occur at a particular time.
1057 </para>
1058 <para>
1059 (This event source is reserved for future use.)
1060 </para>
1061 </listitem>
1062
1063 <listitem>
1064 <para>
1065 <anchor id="trigother-event"/>
1066 <constant>TRIG_OTHER</constant>: driver specific event trigger.
1067 </para>
1068 <para>
1069 This event can be useful as any of the trigger sources.  Its exact
1070 meaning is driver specific, because it implements a feature that
1071 otherwise does not fit into the generic &comedi; command interface.
1072 Configuration of <constant>TRIG_OTHER</constant> features are done by
1073 <constant><link linkend="instructionsconfiguration">INSN_CONFIG</link></constant>
1074 instructions.
1075 </para>
1076 <para>
1077 The argument is reserved and should be set to <literal>0</literal>.
1078 </para>
1079 </listitem>
1080
1081 </itemizedlist>
1082 Not all event sources are applicable to all events.  Supported
1083 trigger sources for specific events depend significantly on your
1084 particular device, and even more on the current state of its device
1085 driver. The
1086 <function><link linkend="func-ref-comedi-get-cmd-src-mask">comedi_get_cmd_src_mask</link></function>
1087 function is useful for determining what trigger sources a subdevice
1088 supports.
1089 </para>
1090
1091 </section>
1092
1093
1094 <section id="comedicmdflags">
1095 <title>
1096 The command flags
1097 <anchor id="source.flags.anchor"/>
1098 </title>
1099
1100 <para>
1101 The
1102 <structfield><link linkend="command-data-struct-flags">flags</link></structfield>
1103 field in the
1104 <link linkend="ref-type-comedi-cmd">command data structure</link>
1105 is used to specify some <quote>behaviour</quote> of the acquisitions in
1106 a command.
1107 The meaning of the field is as follows:
1108 <itemizedlist>
1109
1110 <listitem>
1111 <para>
1112 <anchor id="trig-rt"/>
1113 <constant>TRIG_RT</constant>: ask the driver to use a
1114 <emphasis role="strong">hard real-time</emphasis> interrupt handler.
1115 This will reduce latency in handling interrupts from your data
1116 aquisition
1117 hardware.  It can be useful if you are sampling at high frequency, or
1118 if your hardware has a small onboard data buffer.  You must have a
1119 real-time kernel (<ulink url="http://www.rtai.org">RTAI</ulink> or
1120 <ulink url="http://www.rtlinux-gpl.org/">RTLinux/GPL</ulink>)
1121 and must compile &comedi; with real-time support, or this flag will do
1122 nothing.
1123 </para>
1124 </listitem>
1125
1126 <listitem>
1127 <para>
1128 <anchor id="trig-wake-eos"/>
1129 <constant>TRIG_WAKE_EOS</constant>:
1130 where <quote>EOS</quote> stands for <quote>End of Scan</quote>. Some
1131 drivers will change their behaviour when this flag is set, trying to
1132 transfer data at the end of every scan (instead of, for example,
1133 passing data in chunks whenever the board's hardware data buffer is
1134 half full).  This flag may degrade a driver's performance at high
1135 frequencies, because the end of a scan is, in general, a much more
1136 frequent event than the filling up of the data buffer.
1137 </para>
1138 </listitem>
1139
1140 <listitem>
1141 <para>
1142 <anchor id="trig-round-nearest"/>
1143 <constant>TRIG_ROUND_NEAREST</constant>:
1144 round to nearest supported timing period, the default.
1145 This flag (as well as the following three), indicates how timing
1146 arguments should be rounded if the hardware cannot achieve the exact
1147 timing requested.
1148 </para>
1149 </listitem>
1150
1151 <listitem>
1152 <para>
1153 <anchor id="trig-round-down"/>
1154 <constant>TRIG_ROUND_DOWN</constant>: round period down.
1155 </para>
1156 </listitem>
1157
1158 <listitem>
1159 <para>
1160 <anchor id="trig-round-up"/>
1161 <constant>TRIG_ROUND_UP</constant>: round period up.
1162 </para>
1163 </listitem>
1164
1165 <listitem>
1166 <para>
1167 <anchor id="trig-round-up-next"/>
1168 <constant>TRIG_ROUND_UP_NEXT</constant>:
1169 this one doesn't do anything, and I don't know what it was intended
1170 to do&hellip;?
1171 </para>
1172 </listitem>
1173
1174 <listitem>
1175 <para>
1176 <anchor id="trig-dither"/>
1177 <constant>TRIG_DITHER</constant>: enable dithering? Dithering is
1178 a software technique to smooth the influence of discretization
1179 <quote>noise</quote>.
1180 </para>
1181 </listitem>
1182
1183 <listitem>
1184 <para>
1185 <anchor id="trig-deglitch"/>
1186 <constant>TRIG_DEGLITCH</constant>: enable deglitching?
1187 Another <quote>noise</quote> smoothing technique.
1188 </para>
1189 </listitem>
1190
1191 <listitem>
1192 <para>
1193 <anchor id="trig-write"/>
1194 <constant>TRIG_WRITE</constant>:
1195 write to bidirectional devices.  Could be useful, in principle, if
1196 someone wrote a driver that supported commands for a digital I/O
1197 device that could do either input or output.
1198 </para>
1199 </listitem>
1200
1201 <listitem>
1202 <para>
1203 <anchor id="trig-bogus"/>
1204 <constant>TRIG_BOGUS</constant>: do the motions?
1205 </para>
1206 </listitem>
1207
1208 <listitem>
1209 <para>
1210 <anchor id="trig-other"/>
1211 <constant>TRIG_CONFIG</constant>: perform configuration, not triggering.
1212 This is a legacy of the deprecated
1213 <type><link linkend="ref-type-comedi-trig">comedi_trig_struct</link></type>
1214 data structure, and has no function at present.
1215 </para>
1216 </listitem>
1217
1218 </itemizedlist>
1219 </para>
1220
1221 </section>
1222
1223 <section>
1224 <title>
1225 Anti-aliasing
1226 </title>
1227 <para>
1228 If you wish to aquire accurate waveforms, it is vital that you use an
1229 anti-alias filter.  An anti-alias filter is a low-pass filter used to
1230 remove all    frequencies higher than the Nyquist frequency (half your sampling rate)
1231 from your analog input signal
1232 before you convert it to digital.  If you fail to filter your input signal,
1233 any high frequency components in the original analog signal will create
1234 artifacts in your recorded    digital waveform that cannot be corrected.
1235 </para>
1236 <para>
1237 For example, suppose you are sampling an analog input channel at a rate of
1238 1000 Hz.  If you were to apply a 900 Hz sine wave to the input, you
1239 would find that your
1240 sampling rate is not high enough to faithfully record the 900 Hz input,
1241 since it is above your Nyquist frequency of 500 Hz.  Instead, what you
1242 will see in your recorded digital waveform is a 100 Hz sine wave!  If you
1243 don't use an anti-alias filter, it is impossible to tell whether the 100
1244 Hz sine wave you see in your digital signal was really produced by a
1245 100 Hz input signal, or a 900 Hz signal aliased to 100 Hz, or a 1100 Hz
1246 signal, etc.
1247 </para>
1248 <para>
1249 In practice, the cutoff frequency for the anti-alias filter is usually
1250 set 10% to 20% below the Nyquist frequency due to fact that real filters
1251 do not have infinitely sharp cutoffs.
1252 </para>
1253 </section>
1254 </section>
1255
1256
1257 <section id="slowlyvarying">
1258 <title>
1259 Slowly-varying inputs
1260 </title>
1261
1262 <para>
1263 <emphasis role="strong">Note: The functions described here use an old
1264 feature that is no longer implemented by the &comedi;
1265 kernel layer. THEY WILL NOT WORK!</emphasis>
1266 </para>
1267
1268 <para>
1269 Sometimes, your input channels change slowly enough that
1270 you are able to average many successive input values to get a
1271 more accurate measurement of the actual value.  In general,
1272 the more samples you average, the better your estimate
1273 gets, roughly by a factor of <function>sqrt</function>(number_of_samples).
1274 Obviously, there are limitations to this:
1275 </para>
1276
1277 <itemizedlist>
1278
1279 <listitem>
1280 <para>
1281 you are ultimately limited by <quote>Spurious Free Dynamic
1282 Range</quote>. This SFDR is one of the popular measures to quantify how
1283 much noise a signal carries. If you take a Fourier transform of your
1284 signal, you will see several <quote>peaks</quote> in the transform: one
1285 or more of the fundamental harmonics of the measured signal, and lots
1286 of little <quote>peaks</quote> (called <quote>spurs</quote>) caused by
1287 noise. The SFDR is then the difference between the amplitude of the
1288 fundamental harmonic and of the largest spur (at frequencies below
1289 half of the Nyquist frequency of the DAQ sampler!).
1290 </para>
1291 </listitem>
1292
1293 <listitem>
1294 <para>
1295 you need to have <emphasis>some</emphasis> noise on the input channel,
1296 otherwise you will be averaging the same number <literal>N</literal>
1297 times. (Of course, this only holds if the noise is large enough to
1298 cause at least a one-bit discretization.)
1299 </para>
1300 </listitem>
1301
1302 <listitem>
1303 <para>
1304 the more noise you have, the greater your SFDR, but it
1305 takes many more samples to compensate for the increased
1306 noise.
1307 </para>
1308 </listitem>
1309
1310 <listitem>
1311 <para>
1312 if you feel the need to average samples for, for example, two seconds,
1313 your signal will need to be <emphasis>very</emphasis> slowly-varying,
1314 i.e., not varying more than your target uncertainty for the entire two
1315 seconds.
1316 </para>
1317 </listitem>
1318
1319 </itemizedlist>
1320
1321 <para>
1322 As you might have guessed, the &comedi; library has functions
1323 to help you in your quest to accurately measure slowly varying
1324 inputs:
1325
1326 <funcsynopsis><funcprototype>
1327 <funcdef>int <function>comedi_sv_init</function></funcdef>
1328 <paramdef>comedi_sv_t *<parameter>sv</parameter></paramdef>
1329 <paramdef>comedi_t *<parameter>device</parameter></paramdef>
1330 <paramdef>unsigned int <parameter>subdevice</parameter></paramdef>
1331 <paramdef>unsigned int <parameter>channel</parameter></paramdef>
1332 </funcprototype></funcsynopsis>
1333
1334 The above function <function><link linkend="func-ref-comedi-sv-init">comedi_sv_init</link></function> initializes the
1335 <type><link linkend="ref-type-comedi-sv-t">comedi_sv_t</link></type> data structure, used
1336 to do the averaging acquisition:
1337 <programlisting>
1338 typedef struct comedi_sv_struct {
1339   <link linkend="ref-type-comedi-t">comedi_t</link> *dev;
1340   unsigned int subdevice;
1341   unsigned int chan;
1342
1343   /* range policy */
1344   int range;
1345   int aref;
1346
1347   /* number of measurements to average (for analog inputs) */
1348   int n;
1349
1350   lsampl_t maxdata;
1351 } comedi_sv_t;
1352 </programlisting>
1353
1354 The actual acquisition is done with the function
1355 <function><link linkend="func-ref-comedi-sv-measure">comedi_sv_measure</link></function>:
1356
1357 <funcsynopsis><funcprototype>
1358 <funcdef>int <function>comedi_sv_measure</function></funcdef>
1359 <paramdef>comedi_sv_t *<parameter>sv</parameter></paramdef>
1360 <paramdef>double *<parameter>data</parameter></paramdef>
1361 </funcprototype></funcsynopsis>
1362
1363 The number of samples over which the function
1364 <function>comedi_sv_measure</function> averages is limited by the
1365 implementation (currently the limit is 100 samples).
1366 </para>
1367
1368 <para>
1369 One typical use for this function is the measurement of thermocouple
1370 voltages.
1371 And the &comedi; self-calibration utility also uses these functions.
1372 On some hardware, it is possible to tell it to measure an
1373 internal stable voltage reference, which is typically going
1374 to be very slowly varying; on the kilosecond time scale
1375 or more.  So, it is reasonable to measure millions of samples,
1376 to get a very accurate measurement of the A/D converter output
1377 value that corresponds to the voltage reference.  Sometimes,
1378 however, this is overkill, since there is no need to
1379 perform a part-per-million calibration to a standard that
1380 is only accurate to a part-per-thousand.
1381 </para>
1382
1383 </section>
1384
1385 <section id="experimentalfunctionality">
1386 <title>
1387 Experimental functionality
1388 </title>
1389
1390 <para>
1391 The following subsections document functionality that has not yet
1392 matured. Most of this functionality has even not been implemented yet
1393 in any single device driver. This information is included here, in
1394 order to stimulate discussion about their API, and to encourage
1395 pioneering implementations.
1396 </para>
1397
1398 <section id="digitalinputcombining">
1399 <title>
1400 Digital input combining machines
1401 </title>
1402
1403 <para>
1404 (<emphasis role="strong">Status: experimental (i.e., no driver implements
1405 this yet)</emphasis>)
1406 </para>
1407 <para>
1408 When one or several digital inputs are used to modify an output
1409 value, either an accumulator or a single digital line or bit,
1410 a bitfield structure is typically used in the &comedi; interface.
1411 The digital inputs have two properties, <quote>sensitive</quote> inputs
1412 and <quote>modifier</quote> inputs.  Edge transitions on sensitive
1413 inputs cause changes in the output signal, whereas modifier inputs
1414 change the effect of edge transitions on sensitive inputs.  Note that
1415 inputs can be both modifier inputs and sensitive inputs.
1416 </para>
1417
1418 <para>
1419 For simplification purposes, it is assumed that multiple digital
1420 inputs do not change simultaneously.
1421 </para>
1422
1423 <para>
1424 The combined state of the modifier inputs determine a modifier
1425 state.  For each combination of modifier state and sensitive
1426 input, there is a set of bits that determine the effect on the
1427 output value due to positive or negative transitions of the
1428 sensitive input.  For each transition direction, there are two
1429 bits defined as follows:
1430
1431 <variablelist spacing="compact">
1432  <varlistentry>
1433   <term>00</term>
1434   <listitem>transition is ignored.</listitem>
1435  </varlistentry>
1436  <varlistentry>
1437   <term>01</term>
1438   <listitem>accumulator is incremented, or output is set.</listitem>
1439  </varlistentry>
1440  <varlistentry>
1441   <term>10</term>
1442   <listitem>accumulator is decremented, or output is cleared.</listitem>
1443  </varlistentry>
1444  <varlistentry>
1445   <term>11</term>
1446   <listitem>reserved.</listitem>
1447  </varlistentry>
1448 </variablelist>
1449
1450 For example, a simple digital follower is specified by the bit
1451 pattern 01 10, because it sets the output on positive transitions
1452 of the input, and clears the output on negative transitions.  A
1453 digital inverter is similarily 10 01.  These systems have only
1454 one sensitive input.
1455 </para>
1456
1457 <para>
1458 As another example, a simple up counter, which increments on
1459 positive transitions of one input, is specified by 01 00.  This
1460 system has only one sensitive input.
1461 </para>
1462
1463 <para>
1464 When multiple digital inputs are used, the inputs are divided
1465 into two types, inputs which cause changes in the accumulator, and
1466 those that only modify the meaning of transitions on other inputs.
1467 Modifier inputs do not require bitfields, but there needs to be
1468 a bitfield of length 4*(2^(N-1)) for each edge sensitive input,
1469 where N is the total number of inputs.  Since N is usually 2 or
1470 3, with only one edge sensitive input, the scaling issues are
1471 not significant.
1472 </para>
1473
1474 </section>
1475
1476
1477 <section id="analogconversion">
1478 <title>
1479 Analog filtering configuration
1480 </title>
1481
1482 <para>
1483 <emphasis role="strong">(Status: design (i.e., no driver implements
1484 this yet).)</emphasis>
1485 </para>
1486
1487 <para>
1488 The <structfield><link linkend="insn-data-structure-insn">insn</link></structfield>
1489 field of the
1490 <link linkend="insn-data-structure">instruction data structure</link>
1491 has not been assigned yet.
1492 </para>
1493 <para>
1494 The <structfield><link linkend="insn-data-structure-chanspec">chanspec</link></structfield> field
1495 of the <link linkend="insn-data-structure">instruction data
1496 structure</link> is ignored.
1497 </para>
1498
1499 <para>
1500 Some devices have the capability to add white noise (dithering) to
1501 analog input measurement.  This additional noise can then be averaged
1502 out, to get a more accurate measurement of the input signal.  It
1503 should not be assumed that channels can be separately configured.
1504 A simple design can use 1 bit to turn this feature on/off.
1505 </para>
1506
1507 <para>
1508 Some devices have the capability of changing the glitch characteristics
1509 of analog output subsytems.  The default (off) case should be where
1510 the average settling time is lowest.  A simple design can use 1 bit
1511 to turn this feature on/off.
1512 </para>
1513
1514 <para>
1515 Some devices have a configurable analog filters as part of the analog
1516 input stage.  A simple design can use 1 bit to enable/disable the
1517 filter.  Default is disabled, i.e., the filter being bypassed, or if
1518 the choice is between two filters, the filter with the largest
1519 bandwidth.
1520 </para>
1521 </section>
1522
1523 <section id="waveformgeneration">
1524 <title>
1525 Analog Output Waveform Generation
1526 </title>
1527
1528 <para>
1529 <emphasis role="strong">(Status: design (i.e., no driver implements
1530 this yet).)</emphasis>
1531 </para>
1532 <para>
1533 The <structfield><link linkend="insn-data-structure-insn">insn</link></structfield> field of the
1534 <link linkend="insn-data-structure">instruction data structure</link>
1535 has not been assigned yet.
1536 </para>
1537 <para>
1538 The <structfield><link linkend="insn-data-structure-chanspec">chanspec</link></structfield> field
1539 of the <link linkend="insn-data-structure">instruction data
1540 structure</link> is ignored.
1541 </para>
1542
1543 <para>
1544 Some devices have the ability to cyclicly loop through samples kept in
1545 an on-board analog output FIFO.  This config should allow the user to
1546 enable/disable this mode.
1547 </para>
1548
1549 <para>
1550 This config should allow the user to configure the number of samples
1551 to loop through.  It may be necessary to configure the channels used.
1552 </para>
1553
1554 </section>
1555
1556 <section id="extendedtriggering">
1557 <title>
1558 Extended Triggering
1559 </title>
1560 <para>
1561 <emphasis role="strong">(Status: alpha.)</emphasis>
1562 </para>
1563
1564 <para>
1565 The <structfield><link linkend="insn-data-structure-insn">insn</link></structfield> field of the
1566 <link linkend="insn-data-structure">instruction data structure</link>
1567 has not been assigned yet.
1568 </para>
1569 <para>
1570 The <structfield><link linkend="insn-data-structure-chanspec">chanspec</link></structfield> field
1571 of the <link linkend="insn-data-structure">instruction data
1572 structure</link> is ignored.
1573 </para>
1574
1575 <para>
1576 This section covers common information for all extended
1577 triggering configuration, and doesn't describe a particular
1578 type of extended trigger.
1579 </para>
1580
1581 <para>
1582 Extended triggering is used to configure triggering engines that
1583 do not fit into commands.  In a typical programming sequence, the
1584 application will use
1585 <link linkend="instructionsconfiguration">configuration instructions</link>
1586 to configure an extended trigger, and a
1587 <link linkend="commandsstreaming">command</link>,
1588 specifying
1589 <constant><link linkend="trig-other">TRIG_OTHER</link></constant>
1590 as one of the trigger sources.
1591 </para>
1592
1593 <para>
1594 Extended trigger configuration should be designed in such a way
1595 that the user can probe for valid parameters, similar to how
1596 command testing works.  An extended trigger configuration instruction
1597 should not configure the hardware directly, rather, the configuration
1598 should be saved until the subsequent command is issued.  This
1599 allows more flexibility for future interface changes.
1600 </para>
1601
1602 <para>
1603 It has not been decided whether the configuration stage should return a
1604 token that is then used as the trigger argument in the command.
1605 Using tokens is one method to satisfy the problem that extended
1606 trigger configurations may have subtle compatiblity issues with
1607 other trigger sources/arguments that can only be determined at
1608 command test time.  Passing all stages of a command test should
1609 only be allowed with a properly configured extended trigger.
1610 </para>
1611
1612 <para>
1613 Extended triggers must use
1614 <structfield><link linkend="insn-data-structure-data">data</link></structfield>[1] as flags.  The
1615 upper 16 bits are reserved and used only for flags that are common to
1616 all extended triggers.  The lower 16 bits may be defined by the
1617 particular type of extended trigger.
1618 </para>
1619
1620 <para>
1621 Various types of extended triggers must use
1622 <structfield><link linkend="insn-data-structure-data">data</link></structfield>[1] to know which
1623 event the extended trigger will be assigned to in the command
1624 structure.  The possible values are an OR'd mask of the following:
1625 </para>
1626
1627 <itemizedlist>
1628   <listitem>
1629     <para>
1630 <constant>COMEDI_EV_START</constant>
1631     </para>
1632   </listitem>
1633   <listitem>
1634     <para>
1635 <constant>COMEDI_EV_SCAN_BEGIN</constant>
1636     </para>
1637   </listitem>
1638   <listitem>
1639     <para>
1640 <constant>COMEDI_EV_CONVERT</constant>
1641     </para>
1642   </listitem>
1643   <listitem>
1644     <para>
1645 <constant>COMEDI_EV_SCAN_END</constant>
1646     </para>
1647   </listitem>
1648   <listitem>
1649     <para>
1650 <constant>COMEDI_EV_STOP</constant>
1651     </para>
1652   </listitem>
1653 </itemizedlist>
1654
1655 </section>
1656
1657 <section id="analogtriggering">
1658 <title>
1659 Analog Triggering
1660 </title>
1661 <para>
1662 <emphasis role="strong">
1663 (Status: alpha. The <filename>ni_mio_common.c</filename> driver
1664 implements this feature.)
1665 </emphasis>
1666 </para>
1667
1668 <para>
1669 The <structfield><link linkend="insn-data-structure-insn">insn</link></structfield> field of the
1670 <link linkend="insn-data-structure">instruction data structure</link>
1671 has not been assigned yet.
1672 </para>
1673 <para>
1674 The <structfield><link linkend="insn-data-structure-chanspec">chanspec</link></structfield> field
1675 of the <link linkend="insn-data-structure">instruction data
1676 structure</link> is ignored.
1677 </para>
1678
1679 <para>
1680 The <structfield><link linkend="insn-data-structure-data">data</link></structfield> field
1681 of the <link linkend="insn-data-structure">instruction data
1682 structure</link> is used as follows:
1683 <variablelist spacing="compact">
1684  <varlistentry>
1685   <term>data[1]</term>
1686   <listitem>trigger and combining machine configuration.</listitem>
1687  </varlistentry>
1688  <varlistentry>
1689   <term>data[2]</term>
1690   <listitem>analog triggering signal chanspec.</listitem>
1691  </varlistentry>
1692  <varlistentry>
1693   <term>data[3]</term>
1694   <listitem>primary analog level.</listitem>
1695  </varlistentry>
1696  <varlistentry>
1697   <term>data[4]</term>
1698   <listitem>secondary analog level.</listitem>
1699  </varlistentry>
1700 </variablelist>
1701 </para>
1702 <para>
1703 Analog triggering is described by a digital combining machine that
1704 has two sensitive digital inputs.  The sensitive digital inputs are
1705 generated by configurable analog comparators.  The analog comparators
1706 generate a digital 1 when the analog triggering signal is greater
1707 than the comparator level.  The digital inputs are not modifier
1708 inputs.  Note, however, there is an effective modifier due to the
1709 restriction that the primary analog comparator level must be less
1710 than the secondary analog comparator level.
1711 </para>
1712
1713 <para>
1714 If only one analog comparator signal is used, the combining machine
1715 for the secondary input should be set to ignored, and the secondary
1716 analog level should be set to <literal>0</literal>.
1717 </para>
1718
1719 <para>
1720 The interpretation of the chanspec and voltage levels is device
1721 dependent, but should correspond to similar values of the analog
1722 input subdevice, if possible.
1723 </para>
1724
1725 <para>
1726 Notes:  Reading range information is not addressed.  This makes it
1727 difficult to convert comparator voltages to data values.
1728 </para>
1729
1730 <para>
1731 Possible extensions: A parameter that specifies the necessary time
1732 that the set condition has to be true before the trigger is generated.
1733 A parameter that specifies the necessary time that the reset condition
1734 has to be true before the state machine is reset.
1735 </para>
1736
1737 </section>
1738
1739 <section id="bitfieldmatching">
1740 <title>
1741 Bitfield Pattern Matching Extended Trigger
1742 </title>
1743 <para>
1744 <emphasis role="strong">
1745 (Status: design. No driver implements this feature yet.)
1746 </emphasis>
1747 </para>
1748
1749 <para>
1750 The <structfield><link linkend="insn-data-structure-insn">insn</link></structfield> field of the
1751 <link linkend="insn-data-structure">instruction data structure</link>
1752 has not been assigned yet.
1753 </para>
1754 <para>
1755 The <structfield><link linkend="insn-data-structure-chanspec">chanspec</link></structfield> field
1756 of the <link linkend="insn-data-structure">instruction data
1757 structure</link> is ignored.
1758 </para>
1759
1760 <para>
1761 The <structfield><link linkend="insn-data-structure-data">data</link></structfield> field
1762 of the <link linkend="insn-data-structure">instruction data
1763 structure</link> is used as follows:
1764 </para>
1765 <variablelist spacing="compact">
1766  <varlistentry>
1767   <term>data[1]</term>
1768   <listitem>trigger flags.</listitem>
1769  </varlistentry>
1770  <varlistentry>
1771   <term>data[2]</term>
1772   <listitem>mask.</listitem>
1773  </varlistentry>
1774  <varlistentry>
1775   <term>data[3]</term>
1776   <listitem>pattern.</listitem>
1777  </varlistentry>
1778 </variablelist>
1779
1780 <para>
1781 The pattern matching trigger issues a trigger when all of a specifed
1782 set of input lines match a specified pattern.  If the device allows,
1783 the input lines should correspond to the input lines of a digital input
1784 subdevice, however, this will necessarily be device dependent.  Each
1785 possible digital line that can be matched is assigned a bit in the
1786 mask and pattern.  A bit set in the mask indicates that the
1787 input line must match the corresponding bit in the pattern.
1788 A bit cleared in the mask indicates that the input line is ignored.
1789 </para>
1790
1791 <para>
1792 Notes: This only allows 32 bits in the pattern/mask, which may be
1793 too few.  Devices may support selecting different sets of lines from
1794 which to match a pattern.
1795 </para>
1796
1797 <para>
1798 Discovery: The number of bits can be discovered by setting the mask
1799 to all 1's.  The driver must modify this value and return
1800 <constant>-EAGAIN</constant>.
1801 </para>
1802
1803 </section>
1804
1805 <section id="countertimer">
1806 <title>
1807 Counter configuration
1808 </title>
1809 <para>
1810 <emphasis role="strong">
1811 (Status: design. No driver implements this feature yet.)
1812 </emphasis>
1813 </para>
1814
1815 <para>
1816 The <structfield><link linkend="insn-data-structure-insn">insn</link></structfield> field of the
1817 <link linkend="insn-data-structure">instruction data structure</link>
1818 has not been assigned yet.
1819 </para>
1820 <para>
1821 The <structfield><link linkend="insn-data-structure-chanspec">chanspec</link></structfield> field
1822 of the <link linkend="insn-data-structure">instruction data
1823 structure</link> is used to specify which counter to use. (I.e., the
1824 counter is a &comedi; channel.)
1825 </para>
1826
1827 <para>
1828 The <structfield><link linkend="insn-data-structure-data">data</link></structfield> field
1829 of the <link linkend="insn-data-structure">instruction data
1830 structure</link> is used as follows:
1831 </para>
1832 <variablelist spacing="compact">
1833  <varlistentry>
1834   <term>data[1]</term>
1835   <listitem>trigger configuration.</listitem>
1836  </varlistentry>
1837  <varlistentry>
1838   <term>data[2]</term>
1839   <listitem>primary input chanspec.</listitem>
1840  </varlistentry>
1841  <varlistentry>
1842   <term>data[3]</term>
1843   <listitem>primary combining machine configuration.</listitem>
1844  </varlistentry>
1845  <varlistentry>
1846   <term>data[4]</term>
1847   <listitem>secondary input chanspec.</listitem>
1848  </varlistentry>
1849  <varlistentry>
1850   <term>data[5]</term>
1851  <listitem>secondary combining machine configuration.</listitem>
1852  </varlistentry>
1853  <varlistentry>
1854   <term>data[6]</term>
1855   <listitem>latch configuration.</listitem>
1856  </varlistentry>
1857 </variablelist>
1858
1859 <para>
1860 Note that this configuration is only useful if the counting has to be
1861 done in <emphasis>software</emphasis>. Many cards offer configurable
1862 counters in hardware; e.g., general purpose timer cards can be
1863 configured to act as pulse generators, frequency counters, timers,
1864 encoders, etc.
1865 </para>
1866 <para>
1867 Counters can be operated either in synchronous mode (using
1868 <constant><link linkend="insn-read">INSN_READ</link></constant>)
1869 or asynchronous mode (using
1870 <link linkend="commandsstreaming">commands</link>), similar to analog
1871 input subdevices.
1872 The input signal for both modes is the accumulator.
1873 Commands on counter subdevices are almost always specified using
1874 <structfield><link linkend="command-data-struct-scan-begin-src">scan_begin_src</link></structfield>
1875 = <constant><link linkend="trigother-event">TRIG_OTHER</link></constant>,
1876 with the counter configuration also serving as the extended configuration for
1877 the <quote>scan begin</quote> source.
1878 </para>
1879
1880 <para>
1881 Counters are made up of an accumulator and a combining machine that
1882 determines when the accumulator should be incremented or decremented
1883 based on the values of the input signals.  The combining machine
1884 optionally determines when the accumulator should be latched and
1885 put into a buffer.  This feature is used in asynchronous mode.
1886 </para>
1887
1888 <para>
1889 Note: How to access multiple pieces of data acquired at each event?
1890 </para>
1891
1892 </section>
1893
1894 <section id="auxcounter">
1895 <title>
1896 One source plus auxiliary counter configuration
1897 </title>
1898 <para>
1899 <emphasis role="strong">
1900 (Status: design. No driver implements this feature yet.)
1901 </emphasis>
1902 </para>
1903
1904 <para>
1905 The <structfield><link linkend="insn-data-structure-insn">insn</link></structfield> field of the
1906 <link linkend="insn-data-structure">instruction data structure</link>
1907 has not been assigned yet.
1908 </para>
1909 <para>
1910 The <structfield><link linkend="insn-data-structure-chanspec">chanspec</link></structfield> field
1911 of the <link linkend="insn-data-structure">instruction data
1912 structure</link> is used to &hellip;
1913 </para>
1914
1915 <para>
1916 The <structfield><link linkend="insn-data-structure-data">data</link></structfield> field
1917 of the <link linkend="insn-data-structure">instruction data
1918 structure</link> is used as follows:
1919 </para>
1920
1921 <para>
1922 <variablelist spacing="compact">
1923  <varlistentry>
1924   <term>data[1]</term>
1925   <listitem>
1926 is flags, including the flags for the command triggering
1927 configuration.  If a command is not subsequently issued on the
1928 subdevice, the command triggering portion of the flags are ignored.
1929   </listitem>
1930  </varlistentry>
1931  <varlistentry>
1932   <term>data[2]</term>
1933   <listitem>
1934 determines the mode of operation.  The mode of operation
1935 is actually a bitfield that encodes what to do for various
1936 transitions of the source signals.
1937   </listitem>
1938  </varlistentry>
1939  <varlistentry>
1940   <term>data[3]</term>
1941   <term>data[4]</term>
1942   <listitem>
1943 determine the primary source for the counter, similar to the
1944 <structfield><link linkend="command-data-struct-scan-begin-src">&hellip;_src</link></structfield> and the
1945 <structfield><link linkend="command-data-struct-scan-begin-arg">&hellip;_arg</link></structfield> fields
1946 used in the
1947 <link linkend="command-data-struct">command data structure</link>.
1948   </listitem>
1949  </varlistentry>
1950 </variablelist>
1951 </para>
1952
1953 <para>
1954 Notes: How to specify which events cause a latch and push, and what
1955 should get latched?
1956 </para>
1957
1958 </section>
1959
1960 <section id="RTSI">
1961 <title>
1962 National instruments RTSI trigger bus
1963 </title>
1964 <para>
1965 A number of NI boards support the RTSI (Real Time System Integration) bus.
1966 It's primary use is to synchronize multiple DAQ cards.
1967 On PXI boards, the RTSI lines correspond to the PXI trigger lines 0 to 7.  PCI
1968 boards use cables to connect to their RTSI ports.
1969 The RTSI bus consists of 8 digital signal lines numbered 0 to 7 that are bi-directional.
1970 Each of these signal lines
1971 can be configured as an input or output, and the signal appearing on the output
1972 of each line can be configured to one of several internal board timing signals
1973 (although on older boards RTSI line 7 can only be used for the clock signal).
1974 The <systemitem>ni_pcimio</systemitem>, <systemitem>ni_atmio</systemitem>, and
1975 <systemitem>ni_mio_cs</systemitem> drivers expose the RTSI bus
1976 as a digital I/O subdevice (subdevice number 10).
1977 </para>
1978 <para>
1979 The functions <function>comedi_dio_config</function> and
1980 <function>comedi_dio_get_config</function> can be used on
1981 the RTSI subdevice to
1982 set/query the direction (input or output) of each of the RTSI lines individually.
1983 </para>
1984 <para>
1985 The subdevice also supports the
1986 <constant>INSN_CONFIG_SET_CLOCK_SRC</constant> and
1987 <constant>INSN_CONFIG_GET_CLOCK_SRC</constant> configuration
1988 instructions, which can be
1989 used to configure/query what source the board uses to synchronize its
1990 master clock to.  The various possibilities are defined in the <filename>comedi.h</filename>
1991 header file:
1992 </para>
1993 <informaltable>
1994 <tgroup cols='2' align='left'>
1995 <thead>
1996 <row>
1997 <entry>Clock Source</entry>
1998 <entry>Description</entry>
1999 </row>
2000 </thead>
2001 <tbody>
2002 <row>
2003 <entry><constant>NI_MIO_INTERNAL_CLOCK</constant></entry>
2004 <entry>
2005 Use the board's internal oscillator.
2006 </entry>
2007 </row>
2008 <row>
2009 <entry><constant>NI_MIO_RTSI_CLOCK</constant></entry>
2010 <entry>
2011 Use the RTSI line 7 as the master clock.  This source is
2012 only supported on pre-m-series boards.  The newer m-series boards
2013 use <function>NI_MIO_PLL_RTSI_CLOCK</function> instead.
2014 </entry>
2015 </row>
2016 <row>
2017 <entry><constant>NI_MIO_PLL_PXI_STAR_TRIGGER_CLOCK</constant></entry>
2018 <entry>
2019 Only available for newer m-series PXI boards.  Synchronizes the board's
2020 phased-locked loop (which runs at 80MHz) to the PXI star trigger
2021 line.
2022 </entry>
2023 </row>
2024 <row>
2025 <entry><constant>NI_MIO_PLL_PXI10_CLOCK</constant></entry>
2026 <entry>
2027 Only available for newer m-series PXI boards.
2028 Synchronizes the board's
2029 phased-locked loop (which runs at 80MHz) to the 10 MHz PXI backplane
2030 clock.
2031 </entry>
2032 </row>
2033 <row>
2034 <entry>
2035 <function>NI_MIO_PLL_RTSI_CLOCK<parameter>n</parameter></function>
2036 </entry>
2037 <entry>
2038 Only available for newer m-series boards.
2039 The function returns a clock source which will cause the board's
2040 phased-locked loop (which runs at 80MHz) to syncronize to the RTSI
2041 line specified in the function argument.
2042 </entry>
2043 </row>
2044 </tbody>
2045 </tgroup>
2046 </informaltable>
2047
2048 <para>
2049 For all clock sources except <constant>NI_MIO_INTERNAL_CLOCK</constant>
2050 and <constant>NI_MIO_PLL_PXI10_CLOCK</constant>,
2051 you should pass the period of the clock your are feeding to the board when
2052 using <constant>INSN_CONFIG_SET_CLOCK_SRC</constant>.
2053 </para>
2054 <para>
2055 Finally, the configuration instructions
2056 <constant>INSN_CONFIG_SET_ROUTING</constant> and
2057 <constant>INSN_CONFIG_GET_ROUTING</constant>
2058 can be used to select/query which internal signal
2059 will appear on a given RTSI output line.  The header file <filename>comedi.h</filename> defines
2060 the following signal sources which can be routed to an RTSI line:
2061 </para>
2062
2063 <informaltable>
2064 <tgroup cols='2' align='left'>
2065 <thead>
2066 <row>
2067 <entry>Signal Source</entry>
2068 <entry>Description</entry>
2069 </row>
2070 </thead>
2071 <tbody>
2072 <row>
2073 <entry><constant>NI_RTSI_OUTPUT_ADR_START1</constant></entry>
2074 <entry>
2075 ADR_START1, an analog input start signal.  See the NI's
2076 DAQ-STC Technical Reference Manual for more information.
2077 </entry>
2078 </row>
2079 <row>
2080 <entry><constant>NI_RTSI_OUTPUT_ADR_START2</constant></entry>
2081 <entry>
2082 ADR_START2, an analog input stop signal.  See the NI's
2083 DAQ-STC Technical Reference Manual for more information.
2084 </entry>
2085 </row>
2086 <row>
2087 <entry><constant>NI_RTSI_OUTPUT_SCLKG</constant></entry>
2088 <entry>
2089 SCLKG, a sample clock signal.  See the NI's
2090 DAQ-STC Technical Reference Manual for more information.
2091 </entry>
2092 </row>
2093 <row>
2094 <entry><constant>NI_RTSI_OUTPUT_DACUPDN</constant></entry>
2095 <entry>
2096 DACUPDN, a dac update signal.  See the NI's
2097 DAQ-STC Technical Reference Manual for more information.
2098 </entry>
2099 </row>
2100 <row>
2101 <entry><constant>NI_RTSI_OUTPUT_DA_START1</constant></entry>
2102 <entry>
2103 DA_START1, an analog output start signal.  See the NI's
2104 DAQ-STC Technical Reference Manual for more information.
2105 </entry>
2106 </row>
2107 <row>
2108 <entry><constant>NI_RTSI_OUTPUT_G_SRC0</constant></entry>
2109 <entry>
2110 G_SRC0, the source signal to general purpose counter 0.  See the NI's
2111 DAQ-STC Technical Reference Manual for more information.
2112 </entry>
2113 </row>
2114 <row>
2115 <entry><constant>NI_RTSI_OUTPUT_G_GATE0</constant></entry>
2116 <entry>
2117 G_GATE0, the gate signal to general purpose counter 0.  See the NI's
2118 DAQ-STC Technical Reference Manual for more information.
2119 </entry>
2120 </row>
2121 <row>
2122 <entry><constant>NI_RTSI_OUTPUT_RGOUT0</constant></entry>
2123 <entry>
2124 RGOUT0, the output signal of general purpose counter 0.  See the NI's
2125 DAQ-STC Technical Reference Manual for more information.
2126 </entry>
2127 </row>
2128 <row>
2129 <entry>
2130 <function>NI_RTSI_OUTPUT_RTSI_BRD<parameter>n</parameter></function>
2131 </entry>
2132 <entry>
2133 RTSI_BRD0 though RTSI_BRD3 are four internal signals which can
2134 have various other signals routed to them in turn.  Currently, comedi
2135 provides no way to configure the signals routed to the RTSI_BRD lines.
2136 See the NI's DAQ-STC Technical Reference Manual for more information.
2137 </entry>
2138 </row>
2139 <row>
2140 <entry><constant>NI_RTSI_OUTPUT_RTSI_OSC</constant></entry>
2141 <entry>
2142 The RTSI clock signal.  On pre-m-series boards, this signal is always
2143 routed to RTSI line 7, and cannot be routed to lines 0 through 6.  On
2144 m-series boards, any RTSI line can be configured to output the clock
2145 signal.
2146 </entry>
2147 </row>
2148 </tbody>
2149 </tgroup>
2150 </informaltable>
2151
2152 <para>
2153 The RTSI bus pins may be used as trigger inputs for many of the
2154 &comedi; trigger functions. To use the RTSI bus pins, set the source to be
2155 <constant>TRIG_EXT</constant> and the source argument using the return values
2156 from the <function>NI_EXT_RTSI<parameter>n</parameter></function> function (or similarly the
2157 <function>NI_EXT_PFI<parameter>n</parameter></function> function if you want
2158 to trigger from a PFI line).  The <constant>CR_EDGE</constant> and
2159 <constant>CR_INVERT</constant> flags may
2160 also be set on the trigger source argument to specify edge and
2161 falling edge/low level triggering.
2162
2163 </para>
2164 <para>
2165 An example to set up a device as a master is given below.
2166 </para>
2167
2168 <programlisting><![CDATA[
2169 void comediEnableMaster(comedi_t *dev){
2170         comedi_insn   configCmd;
2171         lsampl_t      configData[2];
2172         int           ret;
2173         unsigned int  d = 0;
2174         static const unsigned rtsi_subdev = 10;
2175         static const unsigned rtsi_clock_line = 7;
2176
2177         /* Route RTSI clock to line 7 (not needed on pre-m-series boards since their
2178            clock is always on line 7). */
2179         memset(&configCmd, 0, sizeof(configCmd));
2180         memset(&configData, 0, sizeof(configData));
2181         configCmd.insn = INSN_CONFIG;
2182         configCmd.subdev = rtsi_subdev;
2183         configCmd.chanspec = rtsi_clock_line;
2184         configCmd.n = 2;
2185         configCmd.data = configData;
2186         configCmd.data[0] = INSN_CONFIG_SET_ROUTING;
2187         configCmd.data[1] = NI_RTSI_OUTPUT_RTSI_OSC;
2188         ret = comedi_do_insn(dev, &configCmd);
2189         if(ret < 0){
2190                 comedi_perror("comedi_do_insn: INSN_CONFIG");
2191                 exit(1);
2192         }
2193         // Set clock RTSI line as output
2194         ret = comedi_dio_config(dev, rtsi_subdev, rtsi_clock_line, INSN_CONFIG_DIO_OUTPUT);
2195         if(ret < 0){
2196                 comedi_perror("comedi_dio_config");
2197                 exit(1);
2198         }
2199
2200         /* Set routing of the 3 main AI RTSI signals and their direction to output.
2201            We're reusing the already initialized configCmd instruction here since
2202            it's mostly the same. */
2203         configCmd.chanspec = 0;
2204         configCmd.data[1] =  NI_RTSI_OUTPUT_ADR_START1;
2205         ret = comedi_do_insn(dev, &configCmd);
2206         if(ret < 0){
2207                 comedi_perror("comedi_do_insn: INSN_CONFIG");
2208                 exit(1);
2209         }
2210         ret = comedi_dio_config(dev, rtsi_subdev, 0, INSN_CONFIG_DIO_OUTPUT);
2211         if(ret < 0){
2212                 comedi_perror("comedi_dio_config");
2213                 exit(1);
2214         }
2215
2216         configCmd.chanspec = 1;
2217         configCmd.data[1] =  NI_RTSI_OUTPUT_ADR_START2;
2218         ret = comedi_do_insn(dev, &configCmd);
2219         if(ret < 0){
2220                 comedi_perror("comedi_do_insn: INSN_CONFIG");
2221                 exit(1);
2222         }
2223         ret = comedi_dio_config(dev, rtsi_subdev, 1, INSN_CONFIG_DIO_OUTPUT);
2224         if(ret < 0){
2225                 comedi_perror("comedi_dio_config");
2226                 exit(1);
2227         }
2228
2229         configCmd.chanspec = 2;
2230         configCmd.data[1] =  NI_RTSI_OUTPUT_SCLKG;
2231         ret = comedi_do_insn(dev, &configCmd);
2232         if(ret < 0){
2233                 comedi_perror("comedi_do_insn: INSN_CONFIG");
2234                 exit(1);
2235         }
2236         ret = comedi_dio_config(dev, rtsi_subdev, 2, INSN_CONFIG_DIO_OUTPUT);
2237         if(ret < 0){
2238                 comedi_perror("comedi_dio_config");
2239                 exit(1);
2240         }
2241 }
2242 ]]></programlisting>
2243
2244 <para>
2245 An example to slave a m-series device from this master follows.  A pre-m-series
2246 device would need to use <constant>NI_MIO_RTSI_CLOCK</constant> for
2247 the clock source instead.  In
2248 your code, you may also wish to configure the master device to use the
2249 external clock source instead of using its internal clock directly (for
2250 best syncronization).
2251 </para>
2252 <programlisting><![CDATA[
2253 void comediEnableSlave(comedi_t *dev){
2254         comedi_insn   configCmd;
2255         lsampl_t      configData[3];
2256         int           ret;
2257         unsigned int  d = 0;;
2258         static const unsigned rtsi_subdev = 10;
2259         static const unsigned rtsi_clock_line = 7;
2260
2261         memset(&configCmd, 0, sizeof(configCmd));
2262         memset(&configData, 0, sizeof(configData));
2263         configCmd.insn = INSN_CONFIG;
2264         configCmd.subdev = rtsi_subdev;
2265         configCmd.chanspec = 0;
2266         configCmd.n = 3;
2267         configCmd.data = configData;
2268         configCmd.data[0] = INSN_CONFIG_SET_CLOCK_SRC;
2269         configCmd.data[1] = NI_MIO_PLL_RTSI_CLOCK(rtsi_clock_line);
2270         configCmd.data[2] = 100;        /* need to give it correct external clock period */
2271         ret = comedi_do_insn(dev, &configCmd);
2272         if(ret < 0){
2273                 comedi_perror("comedi_do_insn: INSN_CONFIG");
2274                 exit(1);
2275         }
2276         /* configure RTSI clock line as input */
2277         ret = comedi_dio_config(dev, rtsi_subdev, rtsi_clock_line, INSN_CONFIG_DIO_INPUT);
2278         if(ret < 0){
2279                 comedi_perror("comedi_dio_config");
2280                 exit(1);
2281         }
2282         /* Configure RTSI lines we are using for AI signals as inputs. */
2283         ret = comedi_dio_config(dev, rtsi_subdev, 0, INSN_CONFIG_DIO_INPUT);
2284         if(ret < 0){
2285                 comedi_perror("comedi_dio_config");
2286                 exit(1);
2287         }
2288         ret = comedi_dio_config(dev, rtsi_subdev, 1, INSN_CONFIG_DIO_INPUT);
2289         if(ret < 0){
2290                 comedi_perror("comedi_dio_config");
2291                 exit(1);
2292         }
2293         ret = comedi_dio_config(dev, rtsi_subdev, 2, INSN_CONFIG_DIO_INPUT);
2294         if(ret < 0){
2295                 comedi_perror("comedi_dio_config");
2296                 exit(1);
2297         }
2298 }
2299
2300 int comediSlaveStart(comedi_t *dev){
2301         comedi_cmd     cmd;
2302         unsigned int   nChannels = 8;
2303         double         sampleRate = 50000;
2304         unsigned int   chanList[8];
2305         int            i;
2306
2307         // Setup chan list
2308         for(i = 0; i < nChannels; i++){
2309                 chanList[i] = CR_PACK(i, 0, AREF_GROUND);
2310         }
2311         // Set up command
2312         memset(&cmd, 0, sizeof(cmd));
2313         ret = comedi_get_cmd_generic_timed(dev, subdevice, &cmd,
2314                 (int)(1e9/(nChannels * sampleRate)));
2315         if(ret<0){
2316                 printf("comedi_get_cmd_generic_timed failed\n");
2317                 return ret;
2318         }
2319         cmd.chanlist        = chanList;
2320         cmd.chanlist_len    = nChannels;
2321         cmd.scan_end_arg    = nChannels;
2322         cmd.start_src        = TRIG_EXT;
2323         cmd.start_arg        = CR_EDGE | NI_EXT_RTSI(0);
2324         cmd.convert_src    = TRIG_EXT;
2325         cmd.convert_arg    = CR_INVERT | CR_EDGE | NI_EXT_RTSI(2);
2326         cmd.stop_src        = TRIG_NONE;
2327
2328         ret = comedi_command(dev0, &cmd0);
2329         if(ret<0){
2330                 printf("comedi_command failed\n");
2331                 return ret;
2332         }
2333         return 0;
2334 }
2335 ]]></programlisting>
2336
2337
2338 </section>
2339
2340 </section>
2341
2342 </section>
2343