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