4 Generates fake waveform signals that can be read through
5 the command interface. It does _not_ read from any board;
6 it just generates deterministic waveforms.
7 Useful for various testing purposes.
9 Copyright (C) 2002 Joachim Wuttke <Joachim.Wuttke@icn.siemens.de>
10 Copyright (C) 2002 Frank Mori Hess <fmhess@users.sourceforge.net>
12 COMEDI - Linux Control and Measurement Device Interface
13 Copyright (C) 2000 David A. Schleef <ds@schleef.org>
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; either version 2 of the License, or
18 (at your option) any later version.
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
27 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 ************************************************************************/
32 Description: generates fake waveforms
33 Author: Joachim Wuttke <Joachim.Wuttke@icn.siemens.de>, Frank Mori Hess
34 <fmhess@users.sourceforge.net>, ds
37 Updated: Sat, 16 Mar 2002 17:34:48 -0800
39 This driver is mainly for testing purposes, but can also be used to
40 generate sample waveforms on systems that don't have data acquisition
43 Configuration options:
44 [0] - Amplitude in microvolts for fake waveforms (default 1 volt)
45 [1] - Period in microseconds for fake waveforms (default 0.1 sec)
47 Generates a sawtooth wave on channel 0, square wave on channel 1, additional
48 waveforms could be added to other channels (currently they return flatline
53 #include <linux/comedidev.h>
55 #include <asm/div64.h>
57 #include "comedi_fc.h"
60 /* Board descriptions */
61 typedef struct waveform_board_struct{
70 static waveform_board waveform_boards[] = {
78 #define thisboard ((waveform_board *)dev->board_ptr)
80 /* Data unique to this driver */
82 struct timer_list timer;
83 struct timeval last; // time at which last timer interrupt occured
84 unsigned int uvolt_amplitude; // waveform amplitude in microvolts
85 unsigned long usec_period; // waveform period in microseconds
86 volatile unsigned long usec_current; // current time (modulo waveform period)
87 volatile unsigned long usec_remainder; // usec since last scan;
88 volatile unsigned long ai_count; // number of conversions remaining
89 unsigned int scan_period; // scan period in usec
90 unsigned int convert_period; // conversion period in usec
91 volatile unsigned timer_running : 1;
92 volatile lsampl_t ao_loopbacks[N_CHANS];
94 #define devpriv ((waveform_private *)dev->private)
96 static int waveform_attach(comedi_device *dev,comedi_devconfig *it);
97 static int waveform_detach(comedi_device *dev);
98 static comedi_driver driver_waveform={
99 driver_name: "comedi_test",
101 attach: waveform_attach,
102 detach: waveform_detach,
103 board_name: &waveform_boards[0].name,
104 offset: sizeof(waveform_board),
105 num_names: sizeof(waveform_boards) / sizeof(waveform_board),
107 COMEDI_INITCLEANUP(driver_waveform);
109 static int waveform_ai_cmdtest(comedi_device *dev,comedi_subdevice *s,
111 static int waveform_ai_cmd(comedi_device *dev, comedi_subdevice *s);
112 static int waveform_ai_cancel(comedi_device *dev, comedi_subdevice *s);
113 static int waveform_ai_insn_read(comedi_device *dev, comedi_subdevice *s, comedi_insn *insn, lsampl_t *data);
114 static int waveform_ao_insn_write(comedi_device *dev, comedi_subdevice *s, comedi_insn *insn, lsampl_t *data);
115 static sampl_t fake_sawtooth(comedi_device *dev, unsigned int range, unsigned long current_time);
116 static sampl_t fake_squarewave(comedi_device *dev, unsigned int range, unsigned long current_time);
117 static sampl_t fake_flatline(comedi_device *dev, unsigned int range, unsigned long current_time);
118 static sampl_t fake_waveform(comedi_device *dev, unsigned int channel,
119 unsigned int range, unsigned long current_time);
121 static const int nano_per_micro = 1000; // 1000 nanosec in a microsec
123 // fake analog input ranges
124 static comedi_lrange waveform_ai_ranges =
134 This is the background routine used to generate arbitrary data.
135 It should run in the background; therefore it is scheduled by
138 static void waveform_ai_interrupt(unsigned long arg)
140 comedi_device *dev = (comedi_device*) arg;
141 comedi_async *async = dev->read_subdev->async;
142 comedi_cmd *cmd = &async->cmd;
144 // all times in microsec
145 unsigned long elapsed_time;
146 unsigned int num_scans;
149 do_gettimeofday(&now);
151 elapsed_time = 1000000 * (now.tv_sec - devpriv->last.tv_sec) + now.tv_usec - devpriv->last.tv_usec;
153 num_scans = (devpriv->usec_remainder + elapsed_time) / devpriv->scan_period;
154 devpriv->usec_remainder = (devpriv->usec_remainder + elapsed_time) % devpriv->scan_period;
157 for(i = 0; i < num_scans; i++)
159 for( j = 0; j < cmd->chanlist_len; j++)
161 cfc_write_to_buffer( dev->read_subdev,
162 fake_waveform(dev, CR_CHAN(cmd->chanlist[j]), CR_RANGE(cmd->chanlist[j]),
163 devpriv->usec_current + i * devpriv->scan_period + j * devpriv->convert_period));
166 if(cmd->stop_src == TRIG_COUNT && devpriv->ai_count >= cmd->stop_arg)
168 async->events |= COMEDI_CB_EOA;
173 devpriv->usec_current += elapsed_time;
174 devpriv->usec_current %= devpriv->usec_period;
176 if((async->events & COMEDI_CB_EOA) == 0 && devpriv->timer_running)
177 mod_timer(&devpriv->timer, jiffies + 1);
179 del_timer(&devpriv->timer);
181 comedi_event(dev, dev->read_subdev, async->events);
184 static int waveform_attach(comedi_device *dev,comedi_devconfig *it)
187 int amplitude = it->options[0];
188 int period = it->options[1];
190 printk("comedi%d: comedi_test: ", dev->minor);
192 dev->board_name = thisboard->name;
194 if(alloc_private(dev, sizeof(waveform_private)) < 0)
197 // set default amplitude and period
199 amplitude = 1000000; // 1 volt
201 period = 100000; // 0.1 sec
203 devpriv->uvolt_amplitude = amplitude;
204 devpriv->usec_period = period;
206 printk("%i microvolt, %li microsecond waveform ", devpriv->uvolt_amplitude, devpriv->usec_period);
207 dev->n_subdevices = 2;
208 if(alloc_subdevices(dev, dev->n_subdevices) < 0) return -ENOMEM;
210 s = dev->subdevices + 0;
211 dev->read_subdev = s;
212 /* analog input subdevice */
213 s->type = COMEDI_SUBD_AI;
214 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_CMD_READ;
215 s->n_chan = thisboard->ai_chans;
216 s->maxdata = (1 << thisboard->ai_bits) - 1;
217 s->range_table = &waveform_ai_ranges;
218 s->len_chanlist = s->n_chan * 2;
219 s->insn_read = waveform_ai_insn_read;
220 s->do_cmd = waveform_ai_cmd;
221 s->do_cmdtest = waveform_ai_cmdtest;
222 s->cancel = waveform_ai_cancel;
224 s = dev->subdevices + 1;
225 dev->write_subdev = s;
226 /* analog output subdevice (loopback) */
227 s->type = COMEDI_SUBD_AO;
228 s->subdev_flags = SDF_WRITEABLE | SDF_GROUND;
229 s->n_chan = thisboard->ai_chans;
230 s->maxdata = (1 << thisboard->ai_bits) - 1;
231 s->range_table = &waveform_ai_ranges;
232 s->len_chanlist = s->n_chan * 2;
233 s->insn_write = waveform_ao_insn_write;
238 /* Our default loopback value is just a 0V flatline */
240 for (i = 0; i < s->n_chan; i++)
241 devpriv->ao_loopbacks[i] = s->maxdata / 2;
244 init_timer(&(devpriv->timer));
245 devpriv->timer.function = waveform_ai_interrupt;
246 devpriv->timer.data = (unsigned long) dev;
248 printk("attached\n");
253 static int waveform_detach(comedi_device *dev)
255 printk("comedi%d: comedi_test: remove\n",dev->minor);
259 waveform_ai_cancel(dev, dev->read_subdev);
265 static int waveform_ai_cmdtest(comedi_device *dev,comedi_subdevice *s,
271 /* step 1: make sure trigger sources are trivially valid */
273 tmp = cmd->start_src;
274 cmd->start_src &= TRIG_NOW;
275 if(!cmd->start_src || tmp != cmd->start_src) err++;
277 tmp = cmd->scan_begin_src;
278 cmd->scan_begin_src &= TRIG_TIMER;
279 if(!cmd->scan_begin_src || tmp != cmd->scan_begin_src) err++;
281 tmp = cmd->convert_src;
282 cmd->convert_src &= TRIG_NOW | TRIG_TIMER;
283 if(!cmd->convert_src || tmp != cmd->convert_src) err++;
285 tmp = cmd->scan_end_src;
286 cmd->scan_end_src &= TRIG_COUNT;
287 if(!cmd->scan_end_src || tmp != cmd->scan_end_src) err++;
290 cmd->stop_src &= TRIG_COUNT | TRIG_NONE;
291 if(!cmd->stop_src || tmp!=cmd->stop_src) err++;
295 /* step 2: make sure trigger sources are unique and mutually compatible */
297 if(cmd->convert_src != TRIG_NOW &&
298 cmd->convert_src != TRIG_TIMER) err++;
299 if(cmd->stop_src != TRIG_COUNT &&
300 cmd->stop_src != TRIG_NONE) err++;
304 /* step 3: make sure arguments are trivially compatible */
306 if(cmd->start_arg != 0)
311 if(cmd->convert_src == TRIG_NOW)
313 if(cmd->convert_arg != 0)
315 cmd->convert_arg = 0;
319 if(cmd->scan_begin_src == TRIG_TIMER)
321 if(cmd->scan_begin_arg < nano_per_micro)
323 cmd->scan_begin_arg = nano_per_micro;
326 if(cmd->convert_src == TRIG_TIMER &&
327 cmd->scan_begin_arg < cmd->convert_arg * cmd->chanlist_len)
329 cmd->scan_begin_arg = cmd->convert_arg * cmd->chanlist_len;
335 // XXX these checks are generic and should go in core if not there already
336 if(!cmd->chanlist_len)
338 cmd->chanlist_len = 1;
341 if(cmd->scan_end_arg != cmd->chanlist_len)
343 cmd->scan_end_arg = cmd->chanlist_len;
347 if(cmd->stop_src == TRIG_COUNT)
356 if(cmd->stop_arg != 0)
365 /* step 4: fix up any arguments */
367 if(cmd->scan_begin_src == TRIG_TIMER)
369 tmp = cmd->scan_begin_arg;
370 // round to nearest microsec
371 cmd->scan_begin_arg = nano_per_micro * ((tmp + (nano_per_micro / 2)) / nano_per_micro);
372 if(tmp != cmd->scan_begin_arg) err++;
374 if(cmd->convert_src == TRIG_TIMER)
376 tmp = cmd->convert_arg;
377 // round to nearest microsec
378 cmd->convert_arg = nano_per_micro * ((tmp + (nano_per_micro / 2)) / nano_per_micro);
379 if(tmp != cmd->convert_arg) err++;
387 static int waveform_ai_cmd(comedi_device *dev, comedi_subdevice *s)
389 comedi_cmd *cmd = &s->async->cmd;
391 if( cmd->flags & TRIG_RT )
393 comedi_error( dev, "commands at RT priority not supported in this driver" );
397 devpriv->timer_running = 1;
398 devpriv->ai_count = 0;
399 devpriv->scan_period = cmd->scan_begin_arg / nano_per_micro;
401 if(cmd->convert_src == TRIG_NOW)
402 devpriv->convert_period = 0;
403 else if( cmd->convert_src == TRIG_TIMER)
404 devpriv->convert_period = cmd->convert_arg / nano_per_micro;
407 comedi_error(dev, "bug setting conversion period");
411 do_gettimeofday(&devpriv->last);
412 devpriv->usec_current = devpriv->last.tv_usec % devpriv->usec_period;
413 devpriv->usec_remainder = 0;
415 devpriv->timer.expires = jiffies + 1;
416 add_timer(&devpriv->timer);
420 static int waveform_ai_cancel(comedi_device *dev, comedi_subdevice *s)
422 devpriv->timer_running = 0;
423 del_timer(&devpriv->timer);
427 static sampl_t fake_sawtooth(comedi_device *dev, unsigned int range_index, unsigned long current_time)
429 comedi_subdevice *s = dev->read_subdev;
430 unsigned int offset = s->maxdata / 2;
432 comedi_krange *krange = &s->range_table->range[range_index];
433 u64 binary_amplitude;
435 binary_amplitude = s->maxdata;
436 binary_amplitude *= devpriv->uvolt_amplitude;
437 do_div(binary_amplitude, krange->max - krange->min);
439 current_time %= devpriv->usec_period;
440 value = current_time;
441 value *= binary_amplitude * 2;
442 do_div(value, devpriv->usec_period);
443 value -= binary_amplitude; // get rid of sawtooth's dc offset
445 return offset + value;
447 static sampl_t fake_squarewave(comedi_device *dev, unsigned int range_index, unsigned long current_time)
449 comedi_subdevice *s = dev->read_subdev;
450 unsigned int offset = s->maxdata / 2;
452 comedi_krange *krange = &s->range_table->range[range_index];
453 current_time %= devpriv->usec_period;
456 value *= devpriv->uvolt_amplitude;
457 do_div(value, krange->max - krange->min);
459 if(current_time < devpriv->usec_period / 2)
462 return offset + value;
465 static sampl_t fake_flatline(comedi_device *dev, unsigned int range_index, unsigned long current_time)
467 return dev->read_subdev->maxdata / 2;
470 // generates a different waveform depending on what channel is read
471 static sampl_t fake_waveform(comedi_device *dev, unsigned int channel,
472 unsigned int range, unsigned long current_time)
482 return fake_sawtooth(dev, range, current_time);
485 return fake_squarewave(dev, range, current_time);
491 return fake_flatline(dev, range, current_time);
494 static int waveform_ai_insn_read(comedi_device *dev, comedi_subdevice *s, comedi_insn *insn, lsampl_t *data)
496 int i, chan = CR_CHAN(insn->chanspec);
498 for(i = 0; i < insn->n; i++)
499 data[i] = devpriv->ao_loopbacks[chan];
504 static int waveform_ao_insn_write(comedi_device *dev, comedi_subdevice *s, comedi_insn *insn, lsampl_t *data)
506 int i, chan = CR_CHAN(insn->chanspec);
508 for(i = 0; i < insn->n; i++)
509 devpriv->ao_loopbacks[chan] = data[i];