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">
8 <section id="introduction">
9 <title>Overview</title>
11 &comedi; is a <emphasis>free software</emphasis> project that develops
12 drivers, tools, and libraries for various forms of
13 <emphasis>data acquisition</emphasis>: reading and writing of analog
14 signals; reading and writing of digital inputs/outputs; pulse and
15 frequency counting; pulse generation; reading encoders; etc.
16 The source code is distributed in two main packages, comedi and
21 <emphasis role="strong">Comedi</emphasis> is a collection of drivers for a variety
22 of common data acquisition plug-in boards (which are called
23 <quote>devices</quote> in &comedi; terminology). The drivers are
24 implemented as the combination of (i) one single core Linux kernel module
25 (called <quote><literal>comedi</literal></quote>) providing common
26 functionality, and (ii) individual low-level driver modules for
32 <emphasis role="strong">Comedilib</emphasis> is a separately distributed package
33 containing a user-space library that provides a
34 developer-friendly interface to the &comedi; devices. Included in the
35 <emphasis>Comedilib</emphasis> package are documentation,
36 configuration and calibration utilities, and demonstration programs.
41 <emphasis role="strong">Kcomedilib</emphasis> is a Linux kernel module
42 (distributed with the <literal>comedi</literal> package) that provides
43 the same interface as <emphasis>comedilib</emphasis> in kernel space,
44 and suitable for use by <emphasis>real-time</emphasis> kernel modules. It is
45 effectively a <quote>kernel library</quote> for using &comedi; from
50 &comedi; works with standard Linux kernels, but also with its
51 real-time extensions <ulink url="http://www.rtai.org">RTAI</ulink> and
52 <ulink url="http://www.rtlinux-gpl.org/">RTLinux/GPL</ulink>.
55 This section gives a high-level introduction to which functionality
56 you can expect from the software. More technical details and
57 programming examples are given in the following sections of this
61 <section id="whatisdevicedriver">
63 What is a <quote>device driver</quote>?
66 A device driver is a piece of software that interfaces a particular
67 piece of hardware: a printer, a sound card, a motor drive, etc. It
68 translates the primitive, device-dependent commands with which the
69 hardware manufacturer allows you to configure, read and write the
70 electronics of the hardware interface into more abstract and generic
71 function calls and data structures for the application programmer.
75 David Schleef started the &comedi; project to put a generic interface
77 lots of different cards for measurement and control purposes. This
78 type of cards are often called <emphasis>data acquisition</emphasis>
79 (or <emphasis role="strong">DAQ</emphasis>) cards.
82 <emphasis>Analog input and output</emphasis> cards were the first goal
83 of the project, but now &comedi; also provides a device
84 independent interface to digital <emphasis>input and output</emphasis>
85 cards, and <emphasis>counter and timer</emphasis> cards (including
86 encoders, pulse generators, frequency and pulse timers, etc.).
89 Schleef designed a structure which is a balance between
90 <emphasis>modularity</emphasis> and <emphasis>complexity</emphasis>:
91 it's fairly easy to integrate a new card because most of the
92 infrastructure part of other, similar drivers can be reused, and
93 learning the generic and hence somewhat <quote>heavier</quote> &comedi;
94 API doesn't scare away new contributors from integrating their drivers
95 into the &comedi; framework.
100 <section id="policymechanism">
105 Device drivers are often written by application programmers, that have
106 only their particular application in mind; especially in real-time
107 applications. For example, one writes a
108 driver for the parallel port, because one wants to use it to generate
109 pulses that drive a stepper motor. This approach often leads to device
110 drivers that depend too much on that particular application, and are
111 not general enough to be re-used for other applications. One golden
112 rule for the device driver writer is to separate mechanism and policy:
117 <emphasis role="strong">Mechanism.</emphasis>
118 The mechanism part of the device interface is a faithful
119 representation of the bare functionality of the device, independent of
120 what part of the functionality an application will use.
126 <emphasis role="strong">Policy.</emphasis>
127 Once a device driver offers a software interface to the mechanism of
128 the device, an application writer can use this mechanism interface to
129 use the device in one particular fashion. That is, some of the data
130 stuctures offered by the mechanism are interpreted in specific
131 physical units, or some of them are taken together because this
132 composition is relevant for the application. For example, a analog
133 output card can be used to generate voltages that are the inputs for
134 the electronic drivers of the motors of a robot; these voltages can be
135 interpreted as setpoints for the desired velocity of these motors, and
136 six of them are taken together to steer one particular robot with
137 six-degrees of freedom. Some of the other outputs of the same physical
138 device can be used by another application program, for example to
139 generate a sine wave that drives a vibration shaker.
144 So, &comedi; focuses only on the <emphasis>mechanism</emphasis> part
145 of DAQ interfacing. The project does not provide the policy parts,
146 such as Graphical User Interfaces to program and display acquisitions,
147 signal processing libraries, or control algorithms.
153 <section id="generaldaqpackage">
155 A general DAQ device driver package
158 From the point of view of application developers, there are many
159 reasons to welcome the standardization of the API and the
160 architectural structure of DAQ software:
165 <emphasis role="strong">API</emphasis>: devices that offer similar functionalities, should have the same
166 software interface, and their differences should be coped with by
167 parameterizing the interfaces, not by changing the interface for
168 each new device in the family. However, the DAQ manufacturers
169 have never been able (or willing) to come up with such a
170 standardization effort themselves.
176 <emphasis role="strong">Architectural structure</emphasis>:
177 many electronic interfaces have more than one layer of
178 functionality between the hardware and the operating system, and
179 the device driver code should reflect this fact. For example, many
180 different interface cards use the same PCI driver chips, or use the
181 parallel port as an intermediate means to connect to the hardware
182 device. Hence, <quote>lower-level</quote> device drivers for
183 these PCI chips and parallel ports allow for an increased modularity
184 and re-useability of the software. Finding the generic
185 similarities and structure among different cards helps in developing
186 device drivers faster and with better documentation.
193 In the case of Linux as the host operating system, device driver
194 writers must keep the following issues in mind:
198 <emphasis role="strong">Kernel space vs. User space.</emphasis>
199 The Linux operating system has two levels that require
200 different programming approaches. Only privileged processes
201 can run in the kernel, where they have access to all hardware and to
202 all kernel data structures. Normal application
203 programs can run their processes only in user space, where these
204 processes are shielded from each other, and from direct access to
205 hardware and to critical data of the operating system; these user
206 space programs execute much of the operating system's functionality
207 through <emphasis>system calls</emphasis>.
210 Device drivers typically must access specific addresses on the bus,
211 and hence must (at least partially) run in kernel space. Normal users
212 program against the API of the <emphasis>Comedilib</emphasis>
213 user-space library. <emphasis>Comedilib</emphasis> then handles
214 the necessary communication with the <emphasis>Comedi</emphasis> modules
215 running in kernel-space.
221 <emphasis role="strong">Device files or device file system.</emphasis>
222 Users who write an application for a particular device,
223 must link their application to that device's device driver. Part of
224 this device driver, however, runs in kernel space, and the user
225 application in user space. So, the operating system provides an
226 interface between both. In Linux or Unix, these interfaces are in the
227 form of <quote>files</quote>
228 in the <filename class="directory">/dev</filename> directory.
229 Each device supported in the kernel may be
230 representated as such a user space device file, and its functionality can
231 may be accessed by classical Unix file I/O:
232 <function>open</function>,
233 <function>close</function>, <function>read</function>,
234 <function>write</function>, <function>ioctl</function>, and <function>mmap</function>.
240 <emphasis role="strong"><filename class="directory">/proc</filename> interface.</emphasis>
241 Linux (and some other UNIX operating systems) offer a file-like
242 interface to attached devices (and other OS-related information) via
243 the <filename class="directory">/proc</filename> directories. These
244 <quote>files</quote> do not really exist, but it gives a familiar
245 interface to users, with which they can inspect the current status of
252 <emphasis role="strong">Direct Memory Access (DMA) vs. Programmed
253 Input/Output (PIO).</emphasis>
254 Almost all devices can be interfaced in PIO mode: the processor is
255 responsible for directly accessing the bus addresses allocated to
256 the device whenever it needs
257 to read or write data. Some devices also allow DMA: the device and the
258 memory <quote>talk</quote> to each other directly, without needing the processor.
259 DMA is a feature of the bus, not of the operating system (which, of
261 to support its processes to use the feature).
267 <emphasis role="strong">Real-time vs. non real-time.</emphasis>
268 If the device is to be used in a
270 url="http://www.rtlinux-gpl.org/">RTLinux/GPL
272 or <ulink url="http://www.rtai.org">RTAI</ulink> application,
273 there are a few extra requirements, because not all system calls are
274 available in the kernel of the real-time operating systems
276 url="http://www.rtlinux-gpl.org/">RTLinux/GPL
278 or <ulink url="http://www.rtai.org">RTAI</ulink>.
279 The APIs of RTAI and RTLinux/Free differ in
280 different ways, so the &comedi; developers have spent a lot of efforts
281 to make generic wrappers to the required RTOS primitives: timers,
282 memory allocation, registration of interrupt handlers, etc.
290 <section id="comediosignals">
295 The cards supported in &comedi; have one or more of the following
296 <emphasis role="strong">signals</emphasis>: analog input, analog
297 output, digital input, digital output, counters input, counter output,
298 pulse input, pulse output:
303 <emphasis role="strong">Digital</emphasis> signals are conceptually quite simple, and don't need
304 much configuration: the number of channels, their addresses on the
305 bus, and their input or output direction.
311 <emphasis role="strong">Analog</emphasis> signals are a bit more complicated. Typically, an analog
312 acquisition channel can be programmed to generate or read a voltage between a
313 lower and an upper threshold (e.g., <literal>-10V</literal> and
314 <literal>+10V</literal>). The card's electronics may also allow
315 automatically sampling of a set of channels in a prescribed order.
321 <emphasis role="strong">Pulse</emphasis>-based signals (counters,
322 timers, encoders, etc.) are conceptually
323 only a bit more complex than digital inputs and outputs, in that
324 they only add some <emphasis>timing specifications</emphasis> to the
325 signal. &comedi; has still only a limited number of drivers for this
326 kind of signals, although most of the necessary API and support
327 functionality is available.
332 In addition to these <quote>real</quote> DAQ functions, &comedi; also
333 offers basic timer access.
337 <section id="comedidevices">
342 &comedi; organizes all hardware according to the following
348 <emphasis role="strong">Channel</emphasis>: the lowest-level hardware
349 component, that represents the properties of one single data channel;
350 for example, an analog input, or a digital output.
356 <emphasis role="strong">Subdevice</emphasis>: a set of functionally
357 identical channels. For example, a set of 16 identical analog
364 <emphasis role="strong">Device</emphasis>: a set of subdevices that
365 are physically implemented on the
366 same interface card; in other words, the interface card itself.
367 For example, the <literal>National Instruments 6024E</literal>
368 device has a subdevice with 16 analog input channels, another
369 subdevice with two analog output channels, and a
370 third subdevice with eight digital inputs/outputs.
375 Some interface cards have extra components that don't fit in the
376 above-mentioned classification, such as an EEPROM to store
377 configuration and board parameters, or calibration inputs. These
378 special components are also classified as <quote>sub-devices</quote> in
384 <section id="acquisitionterminology">
386 Acquisition terminology
390 This Section introduces the terminology that this document uses when
391 talking about Comedi <quote>commands</quote>, which are streaming asyncronous
392 acquisitions. <xref linkend="fig-acq-seq"/>
393 depicts a typical acquisition sequence when running a command:
398 The sequence has a <emphasis role="strong">start</emphasis> and an
399 <emphasis role="strong">end</emphasis>. At both sides, the software
400 and the hardware need some finite
401 <emphasis role="strong">initialization or settling time</emphasis>.
408 The sequence consists of a number of identically repeated
409 <emphasis role="strong">scans</emphasis>. This is where the actual
410 data acquisitions are taking place: data is read from the card, or
411 written to it. Each scan also has a
412 <emphasis role="strong">begin</emphasis>, an
413 <emphasis role="strong">end</emphasis>, and a finite
414 <emphasis role="strong">setup time</emphasis>. Possibly, there is also
416 (<quote><emphasis role="strong">scan delay</emphasis></quote>) at the
420 So, the hardware puts a
421 lower boundary (the <emphasis role="strong">scan interval</emphasis>)
422 on the minimum time needed to complete a full scan.
428 Each scan contains one or more
429 <anchor id="conversion"/>
430 <emphasis role="strong">conversions</emphasis> on particular channels,
431 i.e., the AD/DA converter is activated on each of the programmed
432 channels, and produces a sample, again in a finite
433 <emphasis role="strong">conversion time</emphasis>, starting from the
434 moment in time called the
435 <emphasis role="strong">sample time</emphasis>
436 in <xref linkend="fig-acq-seq"/>
437 (sometimes also called the <quote>timestamp</quote>),
439 triggering event, called <emphasis role="strong">convert</emphasis>.
442 In addition, some hardware has limits on the minimum
443 <emphasis role="strong">conversion interval</emphasis> it can achieve,
444 i.e., the minimum time it needs between
445 <emphasis>subsequent</emphasis> conversions.
446 For example, some A/D hardware must <emphasis>multiplex</emphasis>
447 the conversions from different input channels onto
448 one single A/D converter. Thus the conversions are done serially
449 in time (as shown on the <link linkend="fig-acq-seq">Figure</link>).
450 Other cards have the hardware to do two or more acquisitions in
451 parallel, and can perform all the conversions in a scan simultaneously.
452 The begin of each conversion is <quote>triggered</quote> by
453 some internally or externally generated pulse, e.g., a timer.
458 In general, not only the start of a <emphasis>conversion</emphasis> is
459 triggered, but also the start of a <emphasis>scan</emphasis> and of a
460 <emphasis>sequence</emphasis>. &comedi; provides the API to configure
461 what <link linkend="comedicmdsources">triggering source</link>
462 one wants to use in each case. The API also
463 allows you to specify the <emphasis role="strong">channel list</emphasis>,
464 i.e., the sequence of channels that needs to be acquired during each
469 <figure id="fig-acq-seq" float="0" pgwide="1">
471 Asynchronous Acquisition Sequence
475 <imagedata fileref="acq-seq.gif" format="GIF"/>
479 Figure courtesy of Kurt Müller.
489 <section id="comedifunctions">
495 The basic data acquisition functionalities that &comedi; offers work
496 on channels, or sets of channels:
501 <emphasis role="strong">Single acquisition</emphasis>: &comedi; has
502 function calls to synchronously perform
503 <emphasis>one single</emphasis> data acquisition on a specified
504 channel: <function>comedi_data_read</function>,
505 <function>comedi_data_read_delayed</function>,
506 <function>comedi_data_write</function>,
507 <function>comedi_dio_read</function>,
508 <function>comedi_dio_write</function>. In addition,
509 the lower-level <function>comedi_do_insn</function>
511 be used to perform an acquisition.
514 <quote>Synchronous</quote> means that the calling process
515 blocks until the data acquisition has finished.
521 <emphasis role="strong">Mutiple synchronous acquisition</emphasis>:
522 The <function>comedi_data_read_n</function> function
523 performs (possibly multiple) data acquisitions on a specified channel,
524 in a <emphasis role="strong">synchronous</emphasis> way. So, the
525 function call blocks until the whole acquisition has finished.
526 The precise timing between the acquisitions is not hardware controlled.
529 In addition, <function>comedi_do_insnlist()</function> executes a
530 <emphasis>list</emphasis> of instructions in
531 one single (blocking, synchronous) call, such that the overhead
532 involved in configuring each individual acquisition is reduced.
538 <emphasis role="strong">Command</emphasis>: a command is
539 <emphasis>sequence</emphasis> of
540 <emphasis>scans</emphasis>, for which conditions have been specified
541 that determine when the acquisition will start and stop, and
542 when each conversion in each scan should occur. A
543 <function>comedi_command</function> function call sets up the
544 <emphasis role="strong">aynchronous</emphasis> data acquisition:
545 as soon as the command information has been filled in, the
546 <function>comedi_command</function> function call returns.
547 The hardware of the card takes care of the sequencing and timing of
548 the data acquisition as it proceeds.
557 <section id="comedisupporting">
559 Supporting functionality
563 The command functionality cannot be offered by DAQ cards that
564 lack the hardware to autonomously sequence a series of
566 For these cards, the command functionality may be provided in
567 software. And because of the quite strict real-time requirements for a
568 command acquisition, a real-time operating system should be used to
569 translate the command specification into a correctly timed sequence of
570 instructions. &comedi; provides the <function>comedi_rt_timer</function> kernel
571 module to support such a
572 <emphasis role="strong">virtual command execution</emphasis> under
573 <acronym>RTAI</acronym> or <acronym>RTLinux/Free</acronym>.
577 &comedi; not only offers the API
578 <emphasis role="strong">to access</emphasis> the functionality of the
579 cards, but also <emphasis role="strong">to query</emphasis> the
580 capabilities of the installed devices. That is, a user process can
581 find out what channels are available, and
582 what their physical parameters are (range, direction of input/output,
587 <emphasis role="strong">Buffering</emphasis> is another important
588 aspect of device drivers: the acquired data has to be stored in such
589 buffers, because, in general, the application program cannot guarantee
590 to always be ready to provide or accept data as soon as the interface
591 board wants to do a read or write operation. &comedi; provides internal
592 buffers for data being streamed to/from devices via Comedi commands.
593 The buffer sizes are user-adjustable.