2 comedi/drivers/comedi_test.c
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 "../comedidev.h"
55 #include <asm/div64.h>
57 #include "comedi_fc.h"
58 #include <linux/timer.h>
60 /* Board descriptions */
61 struct waveform_board {
70 static const struct waveform_board waveform_boards[] = {
72 .name = "comedi_test",
79 #define thisboard ((const struct waveform_board *)dev->board_ptr)
81 /* Data unique to this driver */
82 struct waveform_private {
83 struct timer_list timer;
84 struct timeval last; /* time at which last timer interrupt occured */
85 unsigned int uvolt_amplitude; /* waveform amplitude in microvolts */
86 unsigned long usec_period; /* waveform period in microseconds */
87 unsigned long usec_current; /* current time (modulo waveform period) */
88 unsigned long usec_remainder; /* usec since last scan; */
89 unsigned long ai_count; /* number of conversions remaining */
90 unsigned int scan_period; /* scan period in usec */
91 unsigned int convert_period; /* conversion period in usec */
92 unsigned timer_running:1;
93 unsigned int ao_loopbacks[N_CHANS];
95 #define devpriv ((struct waveform_private *)dev->private)
97 static int waveform_attach(struct comedi_device *dev,
98 struct comedi_devconfig *it);
99 static int waveform_detach(struct comedi_device *dev);
100 static struct comedi_driver driver_waveform = {
101 .driver_name = "comedi_test",
102 .module = THIS_MODULE,
103 .attach = waveform_attach,
104 .detach = waveform_detach,
105 .board_name = &waveform_boards[0].name,
106 .offset = sizeof(struct waveform_board),
107 .num_names = ARRAY_SIZE(waveform_boards),
110 COMEDI_INITCLEANUP(driver_waveform);
112 static int waveform_ai_cmdtest(struct comedi_device *dev,
113 struct comedi_subdevice *s,
114 struct comedi_cmd *cmd);
115 static int waveform_ai_cmd(struct comedi_device *dev,
116 struct comedi_subdevice *s);
117 static int waveform_ai_cancel(struct comedi_device *dev,
118 struct comedi_subdevice *s);
119 static int waveform_ai_insn_read(struct comedi_device *dev,
120 struct comedi_subdevice *s,
121 struct comedi_insn *insn, unsigned int *data);
122 static int waveform_ao_insn_write(struct comedi_device *dev,
123 struct comedi_subdevice *s,
124 struct comedi_insn *insn, unsigned int *data);
125 static short fake_sawtooth(struct comedi_device *dev, unsigned int range,
126 unsigned long current_time);
127 static short fake_squarewave(struct comedi_device *dev, unsigned int range,
128 unsigned long current_time);
129 static short fake_flatline(struct comedi_device *dev, unsigned int range,
130 unsigned long current_time);
131 static short fake_waveform(struct comedi_device *dev, unsigned int channel,
132 unsigned int range, unsigned long current_time);
134 /* 1000 nanosec in a microsec */
135 static const int nano_per_micro = 1000;
137 /* fake analog input ranges */
138 static const struct comedi_lrange waveform_ai_ranges = {
147 This is the background routine used to generate arbitrary data.
148 It should run in the background; therefore it is scheduled by
151 static void waveform_ai_interrupt(unsigned long arg)
153 struct comedi_device *dev = (struct comedi_device *)arg;
154 struct comedi_async *async = dev->read_subdev->async;
155 struct comedi_cmd *cmd = &async->cmd;
157 /* all times in microsec */
158 unsigned long elapsed_time;
159 unsigned int num_scans;
162 do_gettimeofday(&now);
165 1000000 * (now.tv_sec - devpriv->last.tv_sec) + now.tv_usec -
166 devpriv->last.tv_usec;
169 (devpriv->usec_remainder + elapsed_time) / devpriv->scan_period;
170 devpriv->usec_remainder =
171 (devpriv->usec_remainder + elapsed_time) % devpriv->scan_period;
174 for (i = 0; i < num_scans; i++) {
175 for (j = 0; j < cmd->chanlist_len; j++) {
176 cfc_write_to_buffer(dev->read_subdev,
185 devpriv->scan_period +
191 if (cmd->stop_src == TRIG_COUNT
192 && devpriv->ai_count >= cmd->stop_arg) {
193 async->events |= COMEDI_CB_EOA;
198 devpriv->usec_current += elapsed_time;
199 devpriv->usec_current %= devpriv->usec_period;
201 if ((async->events & COMEDI_CB_EOA) == 0 && devpriv->timer_running)
202 mod_timer(&devpriv->timer, jiffies + 1);
204 del_timer(&devpriv->timer);
206 comedi_event(dev, dev->read_subdev);
209 static int waveform_attach(struct comedi_device *dev,
210 struct comedi_devconfig *it)
212 struct comedi_subdevice *s;
213 int amplitude = it->options[0];
214 int period = it->options[1];
217 dev->board_name = thisboard->name;
219 if (alloc_private(dev, sizeof(struct waveform_private)) < 0)
222 /* set default amplitude and period */
224 amplitude = 1000000; /* 1 volt */
226 period = 100000; /* 0.1 sec */
228 devpriv->uvolt_amplitude = amplitude;
229 devpriv->usec_period = period;
231 dev->n_subdevices = 2;
232 if (alloc_subdevices(dev, dev->n_subdevices) < 0)
235 s = dev->subdevices + 0;
236 dev->read_subdev = s;
237 /* analog input subdevice */
238 s->type = COMEDI_SUBD_AI;
239 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_CMD_READ;
240 s->n_chan = thisboard->ai_chans;
241 s->maxdata = (1 << thisboard->ai_bits) - 1;
242 s->range_table = &waveform_ai_ranges;
243 s->len_chanlist = s->n_chan * 2;
244 s->insn_read = waveform_ai_insn_read;
245 s->do_cmd = waveform_ai_cmd;
246 s->do_cmdtest = waveform_ai_cmdtest;
247 s->cancel = waveform_ai_cancel;
249 s = dev->subdevices + 1;
250 dev->write_subdev = s;
251 /* analog output subdevice (loopback) */
252 s->type = COMEDI_SUBD_AO;
253 s->subdev_flags = SDF_WRITEABLE | SDF_GROUND;
254 s->n_chan = thisboard->ai_chans;
255 s->maxdata = (1 << thisboard->ai_bits) - 1;
256 s->range_table = &waveform_ai_ranges;
257 s->len_chanlist = s->n_chan * 2;
258 s->insn_write = waveform_ao_insn_write;
260 s->do_cmdtest = NULL;
263 /* Our default loopback value is just a 0V flatline */
264 for (i = 0; i < s->n_chan; i++)
265 devpriv->ao_loopbacks[i] = s->maxdata / 2;
267 init_timer(&(devpriv->timer));
268 devpriv->timer.function = waveform_ai_interrupt;
269 devpriv->timer.data = (unsigned long)dev;
271 printk(KERN_INFO "comedi%d: comedi_test: "
272 "%i microvolt, %li microsecond waveform attached\n", dev->minor,
273 devpriv->uvolt_amplitude, devpriv->usec_period);
277 static int waveform_detach(struct comedi_device *dev)
279 printk("comedi%d: comedi_test: remove\n", dev->minor);
282 waveform_ai_cancel(dev, dev->read_subdev);
287 static int waveform_ai_cmdtest(struct comedi_device *dev,
288 struct comedi_subdevice *s,
289 struct comedi_cmd *cmd)
294 /* step 1: make sure trigger sources are trivially valid */
296 tmp = cmd->start_src;
297 cmd->start_src &= TRIG_NOW;
298 if (!cmd->start_src || tmp != cmd->start_src)
301 tmp = cmd->scan_begin_src;
302 cmd->scan_begin_src &= TRIG_TIMER;
303 if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src)
306 tmp = cmd->convert_src;
307 cmd->convert_src &= TRIG_NOW | TRIG_TIMER;
308 if (!cmd->convert_src || tmp != cmd->convert_src)
311 tmp = cmd->scan_end_src;
312 cmd->scan_end_src &= TRIG_COUNT;
313 if (!cmd->scan_end_src || tmp != cmd->scan_end_src)
317 cmd->stop_src &= TRIG_COUNT | TRIG_NONE;
318 if (!cmd->stop_src || tmp != cmd->stop_src)
325 * step 2: make sure trigger sources are unique and mutually compatible
328 if (cmd->convert_src != TRIG_NOW && cmd->convert_src != TRIG_TIMER)
330 if (cmd->stop_src != TRIG_COUNT && cmd->stop_src != TRIG_NONE)
336 /* step 3: make sure arguments are trivially compatible */
338 if (cmd->start_arg != 0) {
342 if (cmd->convert_src == TRIG_NOW) {
343 if (cmd->convert_arg != 0) {
344 cmd->convert_arg = 0;
348 if (cmd->scan_begin_src == TRIG_TIMER) {
349 if (cmd->scan_begin_arg < nano_per_micro) {
350 cmd->scan_begin_arg = nano_per_micro;
353 if (cmd->convert_src == TRIG_TIMER &&
354 cmd->scan_begin_arg <
355 cmd->convert_arg * cmd->chanlist_len) {
356 cmd->scan_begin_arg =
357 cmd->convert_arg * cmd->chanlist_len;
362 * XXX these checks are generic and should go in core if not there
365 if (!cmd->chanlist_len) {
366 cmd->chanlist_len = 1;
369 if (cmd->scan_end_arg != cmd->chanlist_len) {
370 cmd->scan_end_arg = cmd->chanlist_len;
374 if (cmd->stop_src == TRIG_COUNT) {
375 if (!cmd->stop_arg) {
379 } else { /* TRIG_NONE */
380 if (cmd->stop_arg != 0) {
389 /* step 4: fix up any arguments */
391 if (cmd->scan_begin_src == TRIG_TIMER) {
392 tmp = cmd->scan_begin_arg;
393 /* round to nearest microsec */
394 cmd->scan_begin_arg =
395 nano_per_micro * ((tmp +
396 (nano_per_micro / 2)) / nano_per_micro);
397 if (tmp != cmd->scan_begin_arg)
400 if (cmd->convert_src == TRIG_TIMER) {
401 tmp = cmd->convert_arg;
402 /* round to nearest microsec */
404 nano_per_micro * ((tmp +
405 (nano_per_micro / 2)) / nano_per_micro);
406 if (tmp != cmd->convert_arg)
416 static int waveform_ai_cmd(struct comedi_device *dev,
417 struct comedi_subdevice *s)
419 struct comedi_cmd *cmd = &s->async->cmd;
421 if (cmd->flags & TRIG_RT) {
423 "commands at RT priority not supported in this driver");
427 devpriv->timer_running = 1;
428 devpriv->ai_count = 0;
429 devpriv->scan_period = cmd->scan_begin_arg / nano_per_micro;
431 if (cmd->convert_src == TRIG_NOW)
432 devpriv->convert_period = 0;
433 else if (cmd->convert_src == TRIG_TIMER)
434 devpriv->convert_period = cmd->convert_arg / nano_per_micro;
436 comedi_error(dev, "bug setting conversion period");
440 do_gettimeofday(&devpriv->last);
441 devpriv->usec_current = devpriv->last.tv_usec % devpriv->usec_period;
442 devpriv->usec_remainder = 0;
444 devpriv->timer.expires = jiffies + 1;
445 add_timer(&devpriv->timer);
449 static int waveform_ai_cancel(struct comedi_device *dev,
450 struct comedi_subdevice *s)
452 devpriv->timer_running = 0;
453 del_timer(&devpriv->timer);
457 static short fake_sawtooth(struct comedi_device *dev, unsigned int range_index,
458 unsigned long current_time)
460 struct comedi_subdevice *s = dev->read_subdev;
461 unsigned int offset = s->maxdata / 2;
463 const struct comedi_krange *krange =
464 &s->range_table->range[range_index];
465 u64 binary_amplitude;
467 binary_amplitude = s->maxdata;
468 binary_amplitude *= devpriv->uvolt_amplitude;
469 do_div(binary_amplitude, krange->max - krange->min);
471 current_time %= devpriv->usec_period;
472 value = current_time;
473 value *= binary_amplitude * 2;
474 do_div(value, devpriv->usec_period);
475 value -= binary_amplitude; /* get rid of sawtooth's dc offset */
477 return offset + value;
480 static short fake_squarewave(struct comedi_device *dev,
481 unsigned int range_index,
482 unsigned long current_time)
484 struct comedi_subdevice *s = dev->read_subdev;
485 unsigned int offset = s->maxdata / 2;
487 const struct comedi_krange *krange =
488 &s->range_table->range[range_index];
489 current_time %= devpriv->usec_period;
492 value *= devpriv->uvolt_amplitude;
493 do_div(value, krange->max - krange->min);
495 if (current_time < devpriv->usec_period / 2)
498 return offset + value;
501 static short fake_flatline(struct comedi_device *dev, unsigned int range_index,
502 unsigned long current_time)
504 return dev->read_subdev->maxdata / 2;
507 /* generates a different waveform depending on what channel is read */
508 static short fake_waveform(struct comedi_device *dev, unsigned int channel,
509 unsigned int range, unsigned long current_time)
517 return fake_sawtooth(dev, range, current_time);
520 return fake_squarewave(dev, range, current_time);
526 return fake_flatline(dev, range, current_time);
529 static int waveform_ai_insn_read(struct comedi_device *dev,
530 struct comedi_subdevice *s,
531 struct comedi_insn *insn, unsigned int *data)
533 int i, chan = CR_CHAN(insn->chanspec);
535 for (i = 0; i < insn->n; i++)
536 data[i] = devpriv->ao_loopbacks[chan];
541 static int waveform_ao_insn_write(struct comedi_device *dev,
542 struct comedi_subdevice *s,
543 struct comedi_insn *insn, unsigned int *data)
545 int i, chan = CR_CHAN(insn->chanspec);
547 for (i = 0; i < insn->n; i++)
548 devpriv->ao_loopbacks[chan] = data[i];