linux/drivers/staging/comedi/drivers/comedi_test.c
<<
>>
Prefs
   1/*
   2    comedi/drivers/comedi_test.c
   3
   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.
   8
   9    Copyright (C) 2002 Joachim Wuttke <Joachim.Wuttke@icn.siemens.de>
  10    Copyright (C) 2002 Frank Mori Hess <fmhess@users.sourceforge.net>
  11
  12    COMEDI - Linux Control and Measurement Device Interface
  13    Copyright (C) 2000 David A. Schleef <ds@schleef.org>
  14
  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.
  19
  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.
  24*/
  25/*
  26Driver: comedi_test
  27Description: generates fake waveforms
  28Author: Joachim Wuttke <Joachim.Wuttke@icn.siemens.de>, Frank Mori Hess
  29  <fmhess@users.sourceforge.net>, ds
  30Devices:
  31Status: works
  32Updated: Sat, 16 Mar 2002 17:34:48 -0800
  33
  34This driver is mainly for testing purposes, but can also be used to
  35generate sample waveforms on systems that don't have data acquisition
  36hardware.
  37
  38Configuration options:
  39  [0] - Amplitude in microvolts for fake waveforms (default 1 volt)
  40  [1] - Period in microseconds for fake waveforms (default 0.1 sec)
  41
  42Generates a sawtooth wave on channel 0, square wave on channel 1, additional
  43waveforms could be added to other channels (currently they return flatline
  44zero volts).
  45
  46*/
  47
  48#include <linux/module.h>
  49#include "../comedidev.h"
  50
  51#include <asm/div64.h>
  52
  53#include "comedi_fc.h"
  54#include <linux/timer.h>
  55#include <linux/ktime.h>
  56
  57#define N_CHANS 8
  58
  59enum waveform_state_bits {
  60        WAVEFORM_AI_RUNNING = 0
  61};
  62
  63/* Data unique to this driver */
  64struct waveform_private {
  65        struct timer_list timer;
  66        ktime_t last;   /* time last timer interrupt occurred */
  67        unsigned int uvolt_amplitude;   /* waveform amplitude in microvolts */
  68        unsigned long usec_period;      /* waveform period in microseconds */
  69        unsigned long usec_current;     /* current time (mod waveform period) */
  70        unsigned long usec_remainder;   /* usec since last scan */
  71        unsigned long state_bits;
  72        unsigned int scan_period;       /* scan period in usec */
  73        unsigned int convert_period;    /* conversion period in usec */
  74        unsigned int ao_loopbacks[N_CHANS];
  75};
  76
  77/* 1000 nanosec in a microsec */
  78static const int nano_per_micro = 1000;
  79
  80/* fake analog input ranges */
  81static const struct comedi_lrange waveform_ai_ranges = {
  82        2, {
  83                BIP_RANGE(10),
  84                BIP_RANGE(5)
  85        }
  86};
  87
  88static unsigned short fake_sawtooth(struct comedi_device *dev,
  89                                    unsigned int range_index,
  90                                    unsigned long current_time)
  91{
  92        struct waveform_private *devpriv = dev->private;
  93        struct comedi_subdevice *s = dev->read_subdev;
  94        unsigned int offset = s->maxdata / 2;
  95        u64 value;
  96        const struct comedi_krange *krange =
  97            &s->range_table->range[range_index];
  98        u64 binary_amplitude;
  99
 100        binary_amplitude = s->maxdata;
 101        binary_amplitude *= devpriv->uvolt_amplitude;
 102        do_div(binary_amplitude, krange->max - krange->min);
 103
 104        current_time %= devpriv->usec_period;
 105        value = current_time;
 106        value *= binary_amplitude * 2;
 107        do_div(value, devpriv->usec_period);
 108        value -= binary_amplitude;      /* get rid of sawtooth's dc offset */
 109
 110        return offset + value;
 111}
 112
 113static unsigned short fake_squarewave(struct comedi_device *dev,
 114                                      unsigned int range_index,
 115                                      unsigned long current_time)
 116{
 117        struct waveform_private *devpriv = dev->private;
 118        struct comedi_subdevice *s = dev->read_subdev;
 119        unsigned int offset = s->maxdata / 2;
 120        u64 value;
 121        const struct comedi_krange *krange =
 122            &s->range_table->range[range_index];
 123        current_time %= devpriv->usec_period;
 124
 125        value = s->maxdata;
 126        value *= devpriv->uvolt_amplitude;
 127        do_div(value, krange->max - krange->min);
 128
 129        if (current_time < devpriv->usec_period / 2)
 130                value *= -1;
 131
 132        return offset + value;
 133}
 134
 135static unsigned short fake_flatline(struct comedi_device *dev,
 136                                    unsigned int range_index,
 137                                    unsigned long current_time)
 138{
 139        return dev->read_subdev->maxdata / 2;
 140}
 141
 142/* generates a different waveform depending on what channel is read */
 143static unsigned short fake_waveform(struct comedi_device *dev,
 144                                    unsigned int channel, unsigned int range,
 145                                    unsigned long current_time)
 146{
 147        enum {
 148                SAWTOOTH_CHAN,
 149                SQUARE_CHAN,
 150        };
 151        switch (channel) {
 152        case SAWTOOTH_CHAN:
 153                return fake_sawtooth(dev, range, current_time);
 154        case SQUARE_CHAN:
 155                return fake_squarewave(dev, range, current_time);
 156        default:
 157                break;
 158        }
 159
 160        return fake_flatline(dev, range, current_time);
 161}
 162
 163/*
 164   This is the background routine used to generate arbitrary data.
 165   It should run in the background; therefore it is scheduled by
 166   a timer mechanism.
 167*/
 168static void waveform_ai_interrupt(unsigned long arg)
 169{
 170        struct comedi_device *dev = (struct comedi_device *)arg;
 171        struct waveform_private *devpriv = dev->private;
 172        struct comedi_subdevice *s = dev->read_subdev;
 173        struct comedi_async *async = s->async;
 174        struct comedi_cmd *cmd = &async->cmd;
 175        unsigned int i, j;
 176        /* all times in microsec */
 177        unsigned long elapsed_time;
 178        unsigned int num_scans;
 179        ktime_t now;
 180
 181        /* check command is still active */
 182        if (!test_bit(WAVEFORM_AI_RUNNING, &devpriv->state_bits))
 183                return;
 184
 185        now = ktime_get();
 186
 187        elapsed_time = ktime_to_us(ktime_sub(now, devpriv->last));
 188        devpriv->last = now;
 189        num_scans =
 190            (devpriv->usec_remainder + elapsed_time) / devpriv->scan_period;
 191        devpriv->usec_remainder =
 192            (devpriv->usec_remainder + elapsed_time) % devpriv->scan_period;
 193
 194        num_scans = comedi_nscans_left(s, num_scans);
 195        for (i = 0; i < num_scans; i++) {
 196                for (j = 0; j < cmd->chanlist_len; j++) {
 197                        unsigned short sample;
 198
 199                        sample = fake_waveform(dev, CR_CHAN(cmd->chanlist[j]),
 200                                               CR_RANGE(cmd->chanlist[j]),
 201                                               devpriv->usec_current +
 202                                                   i * devpriv->scan_period +
 203                                                   j * devpriv->convert_period);
 204                        comedi_buf_write_samples(s, &sample, 1);
 205                }
 206        }
 207
 208        devpriv->usec_current += elapsed_time;
 209        devpriv->usec_current %= devpriv->usec_period;
 210
 211        if (cmd->stop_src == TRIG_COUNT && async->scans_done >= cmd->stop_arg)
 212                async->events |= COMEDI_CB_EOA;
 213        else
 214                mod_timer(&devpriv->timer, jiffies + 1);
 215
 216        comedi_handle_events(dev, s);
 217}
 218
 219static int waveform_ai_cmdtest(struct comedi_device *dev,
 220                               struct comedi_subdevice *s,
 221                               struct comedi_cmd *cmd)
 222{
 223        int err = 0;
 224        unsigned int arg;
 225
 226        /* Step 1 : check if triggers are trivially valid */
 227
 228        err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW);
 229        err |= cfc_check_trigger_src(&cmd->scan_begin_src, TRIG_TIMER);
 230        err |= cfc_check_trigger_src(&cmd->convert_src, TRIG_NOW | TRIG_TIMER);
 231        err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
 232        err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
 233
 234        if (err)
 235                return 1;
 236
 237        /* Step 2a : make sure trigger sources are unique */
 238
 239        err |= cfc_check_trigger_is_unique(cmd->convert_src);
 240        err |= cfc_check_trigger_is_unique(cmd->stop_src);
 241
 242        /* Step 2b : and mutually compatible */
 243
 244        if (err)
 245                return 2;
 246
 247        /* Step 3: check if arguments are trivially valid */
 248
 249        err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
 250
 251        if (cmd->convert_src == TRIG_NOW)
 252                err |= cfc_check_trigger_arg_is(&cmd->convert_arg, 0);
 253
 254        if (cmd->scan_begin_src == TRIG_TIMER) {
 255                err |= cfc_check_trigger_arg_min(&cmd->scan_begin_arg,
 256                                                 nano_per_micro);
 257                if (cmd->convert_src == TRIG_TIMER)
 258                        err |= cfc_check_trigger_arg_min(&cmd->scan_begin_arg,
 259                                        cmd->convert_arg * cmd->chanlist_len);
 260        }
 261
 262        err |= cfc_check_trigger_arg_min(&cmd->chanlist_len, 1);
 263        err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, cmd->chanlist_len);
 264
 265        if (cmd->stop_src == TRIG_COUNT)
 266                err |= cfc_check_trigger_arg_min(&cmd->stop_arg, 1);
 267        else    /* TRIG_NONE */
 268                err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
 269
 270        if (err)
 271                return 3;
 272
 273        /* step 4: fix up any arguments */
 274
 275        if (cmd->scan_begin_src == TRIG_TIMER) {
 276                arg = cmd->scan_begin_arg;
 277                /* round to nearest microsec */
 278                arg = nano_per_micro *
 279                      ((arg + (nano_per_micro / 2)) / nano_per_micro);
 280                err |= cfc_check_trigger_arg_is(&cmd->scan_begin_arg, arg);
 281        }
 282        if (cmd->convert_src == TRIG_TIMER) {
 283                arg = cmd->convert_arg;
 284                /* round to nearest microsec */
 285                arg = nano_per_micro *
 286                      ((arg + (nano_per_micro / 2)) / nano_per_micro);
 287                err |= cfc_check_trigger_arg_is(&cmd->convert_arg, arg);
 288        }
 289
 290        if (err)
 291                return 4;
 292
 293        return 0;
 294}
 295
 296static int waveform_ai_cmd(struct comedi_device *dev,
 297                           struct comedi_subdevice *s)
 298{
 299        struct waveform_private *devpriv = dev->private;
 300        struct comedi_cmd *cmd = &s->async->cmd;
 301
 302        if (cmd->flags & CMDF_PRIORITY) {
 303                dev_err(dev->class_dev,
 304                        "commands at RT priority not supported in this driver\n");
 305                return -1;
 306        }
 307
 308        devpriv->scan_period = cmd->scan_begin_arg / nano_per_micro;
 309
 310        if (cmd->convert_src == TRIG_NOW)
 311                devpriv->convert_period = 0;
 312        else    /* TRIG_TIMER */
 313                devpriv->convert_period = cmd->convert_arg / nano_per_micro;
 314
 315        devpriv->last = ktime_get();
 316        devpriv->usec_current =
 317                ((u32)ktime_to_us(devpriv->last)) % devpriv->usec_period;
 318        devpriv->usec_remainder = 0;
 319
 320        devpriv->timer.expires = jiffies + 1;
 321        /* mark command as active */
 322        smp_mb__before_atomic();
 323        set_bit(WAVEFORM_AI_RUNNING, &devpriv->state_bits);
 324        smp_mb__after_atomic();
 325        add_timer(&devpriv->timer);
 326        return 0;
 327}
 328
 329static int waveform_ai_cancel(struct comedi_device *dev,
 330                              struct comedi_subdevice *s)
 331{
 332        struct waveform_private *devpriv = dev->private;
 333
 334        /* mark command as no longer active */
 335        clear_bit(WAVEFORM_AI_RUNNING, &devpriv->state_bits);
 336        smp_mb__after_atomic();
 337        /* cannot call del_timer_sync() as may be called from timer routine */
 338        del_timer(&devpriv->timer);
 339        return 0;
 340}
 341
 342static int waveform_ai_insn_read(struct comedi_device *dev,
 343                                 struct comedi_subdevice *s,
 344                                 struct comedi_insn *insn, unsigned int *data)
 345{
 346        struct waveform_private *devpriv = dev->private;
 347        int i, chan = CR_CHAN(insn->chanspec);
 348
 349        for (i = 0; i < insn->n; i++)
 350                data[i] = devpriv->ao_loopbacks[chan];
 351
 352        return insn->n;
 353}
 354
 355static int waveform_ao_insn_write(struct comedi_device *dev,
 356                                  struct comedi_subdevice *s,
 357                                  struct comedi_insn *insn, unsigned int *data)
 358{
 359        struct waveform_private *devpriv = dev->private;
 360        int i, chan = CR_CHAN(insn->chanspec);
 361
 362        for (i = 0; i < insn->n; i++)
 363                devpriv->ao_loopbacks[chan] = data[i];
 364
 365        return insn->n;
 366}
 367
 368static int waveform_attach(struct comedi_device *dev,
 369                           struct comedi_devconfig *it)
 370{
 371        struct waveform_private *devpriv;
 372        struct comedi_subdevice *s;
 373        int amplitude = it->options[0];
 374        int period = it->options[1];
 375        int i;
 376        int ret;
 377
 378        devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
 379        if (!devpriv)
 380                return -ENOMEM;
 381
 382        /* set default amplitude and period */
 383        if (amplitude <= 0)
 384                amplitude = 1000000;    /* 1 volt */
 385        if (period <= 0)
 386                period = 100000;        /* 0.1 sec */
 387
 388        devpriv->uvolt_amplitude = amplitude;
 389        devpriv->usec_period = period;
 390
 391        ret = comedi_alloc_subdevices(dev, 2);
 392        if (ret)
 393                return ret;
 394
 395        s = &dev->subdevices[0];
 396        dev->read_subdev = s;
 397        /* analog input subdevice */
 398        s->type = COMEDI_SUBD_AI;
 399        s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_CMD_READ;
 400        s->n_chan = N_CHANS;
 401        s->maxdata = 0xffff;
 402        s->range_table = &waveform_ai_ranges;
 403        s->len_chanlist = s->n_chan * 2;
 404        s->insn_read = waveform_ai_insn_read;
 405        s->do_cmd = waveform_ai_cmd;
 406        s->do_cmdtest = waveform_ai_cmdtest;
 407        s->cancel = waveform_ai_cancel;
 408
 409        s = &dev->subdevices[1];
 410        dev->write_subdev = s;
 411        /* analog output subdevice (loopback) */
 412        s->type = COMEDI_SUBD_AO;
 413        s->subdev_flags = SDF_WRITABLE | SDF_GROUND;
 414        s->n_chan = N_CHANS;
 415        s->maxdata = 0xffff;
 416        s->range_table = &waveform_ai_ranges;
 417        s->insn_write = waveform_ao_insn_write;
 418
 419        /* Our default loopback value is just a 0V flatline */
 420        for (i = 0; i < s->n_chan; i++)
 421                devpriv->ao_loopbacks[i] = s->maxdata / 2;
 422
 423        init_timer(&devpriv->timer);
 424        devpriv->timer.function = waveform_ai_interrupt;
 425        devpriv->timer.data = (unsigned long)dev;
 426
 427        dev_info(dev->class_dev,
 428                 "%s: %i microvolt, %li microsecond waveform attached\n",
 429                 dev->board_name,
 430                 devpriv->uvolt_amplitude, devpriv->usec_period);
 431
 432        return 0;
 433}
 434
 435static void waveform_detach(struct comedi_device *dev)
 436{
 437        struct waveform_private *devpriv = dev->private;
 438
 439        if (devpriv)
 440                del_timer_sync(&devpriv->timer);
 441}
 442
 443static struct comedi_driver waveform_driver = {
 444        .driver_name    = "comedi_test",
 445        .module         = THIS_MODULE,
 446        .attach         = waveform_attach,
 447        .detach         = waveform_detach,
 448};
 449module_comedi_driver(waveform_driver);
 450
 451MODULE_AUTHOR("Comedi http://www.comedi.org");
 452MODULE_DESCRIPTION("Comedi low-level driver");
 453MODULE_LICENSE("GPL");
 454