2 * Example of using commands - asynchronous input
5 * Copyright (c) 1999,2000 David A. Schleef <ds@schleef.org>
7 * This file may be freely modified, distributed, and combined with
8 * other software, as long as proper attribution is given in the
13 * An example for directly using Comedi commands. Comedi commands
14 * are used for asynchronous acquisition, with the timing controlled
15 * by on-board timers or external events.
19 #include <comedilib.h>
22 #include <sys/ioctl.h>
35 int aref = AREF_GROUND;
42 void do_cmd_1(comedi_t *dev);
43 void do_cmd_2(comedi_t *dev);
44 void do_cmd(comedi_t *dev,comedi_cmd *cmd);
45 void dump_cmd(comedi_cmd *cmd);
47 int main(int argc, char *argv[])
54 dev = comedi_open(fn);
60 fcntl(comedi_fileno(dev),F_SETFL,O_NONBLOCK);
67 void do_cmd(comedi_t *dev,comedi_cmd *cmd)
69 unsigned int *chanlist;
75 chanlist = cmd->chanlist;
76 n_chans = cmd->chanlist_len;
78 ret=ioctl(comedi_fileno(dev),COMEDI_CMDTEST,cmd);
80 printf("test ret=%d\n",ret);
82 printf("errno=%d\n",errno);
89 /* restoring the chanlist stuff in this way is only required
90 * for comedi versions before 0.7.56
92 cmd->chanlist = chanlist;
93 cmd->chanlist_len = n_chans;
95 ret=ioctl(comedi_fileno(dev),COMEDI_CMDTEST,cmd);
97 printf("test ret=%d\n",ret);
99 printf("errno=%d\n",errno);
100 comedi_perror("ioctl");
106 cmd->chanlist = chanlist;
107 cmd->chanlist_len = n_chans;
109 ret=ioctl(comedi_fileno(dev),COMEDI_CMD,cmd);
111 printf("ret=%d\n",ret);
113 printf("errno=%d\n",errno);
114 comedi_perror("ioctl");
120 ret=read(comedi_fileno(dev),buf,BUFSZ);
132 printf("read %d %d\n",ret,total);
138 * This part of the demo measures channels 1, 2, 3, 4 at a rate of
139 * 10 khz, with the inter-sample time at 10 us (100 khz). The number
140 * of scans measured is 10. This is analogous to the old mode2
143 void do_cmd_1(comedi_t *dev)
146 unsigned int chanlist[4];
148 memset(&cmd,0,sizeof(cmd));
150 /* the subdevice that the command is sent to */
151 cmd.subdev = subdevice;
155 /* the TRIG_RT flag will ask that the driver's interrupt handler be
156 * run at hard real time priority if you have a real time OS
158 //cmd.flags |= TRIG_RT;
160 /* each event requires a trigger, which is specified
161 by a source and an argument. For example, to specify
162 an external digital line 3 as a source, you would use
163 src=TRIG_EXT and arg=3. */
165 /* In this case, we specify using TRIG_NOW to start
166 * acquisition immediately when the command is issued.
167 * The argument of TRIG_NOW is "number of nsec after
168 * NOW", but no driver supports it yet. Also, no driver
169 * currently supports using a start_src other than
171 cmd.start_src = TRIG_NOW;
174 /* The timing of the beginning of each scan is controlled
175 * by scan_begin. TRIG_TIMER specifies that scan_start
176 * events occur periodically at a rate of scan_begin_arg
177 * nanoseconds between scans. */
178 cmd.scan_begin_src = TRIG_TIMER;
179 cmd.scan_begin_arg = 100000; /* in ns */
181 /* The timing between each sample in a scan is controlled
182 * by convert. Like above, TRIG_TIMER specifies that
183 * convert events occur periodically at a rate of convert_arg
184 * nanoseconds between scans. */
185 cmd.convert_src = TRIG_TIMER;
186 cmd.convert_arg = 10000; /* in ns */
188 /* The end of each scan is almost always specified using
189 * TRIG_COUNT, with the argument being the same as the
190 * number of channels in the chanlist. You could probably
191 * find a device that allows something else, but it would
193 cmd.scan_end_src = TRIG_COUNT;
194 cmd.scan_end_arg = 4; /* number of channels */
196 /* The end of acquisition is controlled by stop_src and
197 * stop_arg. The src will typically be TRIG_COUNT or
198 * TRIG_NONE. Specifying TRIG_COUNT will stop acquisition
199 * after stop_arg number of scans, or TRIG_NONE will
200 * cause acquisition to continue until stopped using
201 * comedi_cancel(). */
203 cmd.stop_src = TRIG_COUNT;
206 cmd.stop_src = TRIG_NONE;
210 /* the channel list determined which channels are sampled.
211 In general, chanlist_len is the same as scan_end_arg. Most
212 boards require this. */
213 cmd.chanlist = chanlist;
214 cmd.chanlist_len = 4;
216 chanlist[0]=CR_PACK(0,range,aref);
217 chanlist[1]=CR_PACK(1,range,aref);
218 chanlist[2]=CR_PACK(2,range,aref);
219 chanlist[3]=CR_PACK(3,range,aref);
224 void do_cmd_2(comedi_t *dev)
227 unsigned int chanlist[4];
229 memset(&cmd,0,sizeof(cmd));
231 /* the subdevice that the command is sent to */
232 cmd.subdev = subdevice;
237 /* each event requires a trigger, which is specified
238 by a source and an argument. For example, to specify
239 an external digital line 3 as a source, you would use
240 src=TRIG_EXT and arg=3. */
242 cmd.start_src = TRIG_NOW;
245 cmd.scan_begin_src = TRIG_TIMER;
246 cmd.scan_begin_arg = 1; /* in ns */
248 cmd.convert_src = TRIG_TIMER;
249 cmd.convert_arg = 1; /* in ns */
251 cmd.scan_end_src = TRIG_COUNT;
252 cmd.scan_end_arg = 4; /* number of channels */
255 cmd.stop_src = TRIG_COUNT;
258 cmd.stop_src = TRIG_NONE;
262 /* the channel list determined which channels are sampled.
263 In general, chanlist_len is the same as scan_end_arg. Most
264 boards require this. */
265 cmd.chanlist = chanlist;
266 cmd.chanlist_len = 4;
268 chanlist[0]=CR_PACK(0,range,aref);
269 chanlist[1]=CR_PACK(1,range,aref);
270 chanlist[2]=CR_PACK(2,range,aref);
271 chanlist[3]=CR_PACK(3,range,aref);
276 char *cmd_src(int src,char *buf)
280 if(src&TRIG_NONE)strcat(buf,"none|");
281 if(src&TRIG_NOW)strcat(buf,"now|");
282 if(src&TRIG_FOLLOW)strcat(buf, "follow|");
283 if(src&TRIG_TIME)strcat(buf, "time|");
284 if(src&TRIG_TIMER)strcat(buf, "timer|");
285 if(src&TRIG_COUNT)strcat(buf, "count|");
286 if(src&TRIG_EXT)strcat(buf, "ext|");
287 if(src&TRIG_INT)strcat(buf, "int|");
290 sprintf(buf,"unknown(0x%02x)",src);
292 buf[strlen(buf)-1]=0;
299 void dump_cmd(comedi_cmd *cmd)
303 printf("start: %s %d\n",
304 cmd_src(cmd->start_src,buf),
307 printf("scan_begin: %s %d\n",
308 cmd_src(cmd->scan_begin_src,buf),
309 cmd->scan_begin_arg);
311 printf("convert: %s %d\n",
312 cmd_src(cmd->convert_src,buf),
315 printf("scan_end: %s %d\n",
316 cmd_src(cmd->scan_end_src,buf),
319 printf("stop: %s %d\n",
320 cmd_src(cmd->stop_src,buf),