2 comedi/drivers/das16m1.c
4 Author: Frank Mori Hess, based on code from the das16
6 Copyright (C) 2001 Frank Mori Hess <fmhess@users.sourceforge.net>
8 COMEDI - Linux Control and Measurement Device Interface
9 Copyright (C) 2000 David A. Schleef <ds@schleef.org>
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
23 Description: CIO-DAS16/M1
24 Author: Frank Mori Hess <fmhess@users.sourceforge.net>
25 Devices: [Measurement Computing] CIO-DAS16/M1 (das16m1)
28 This driver supports a single board - the CIO-DAS16/M1.
29 As far as I know, there are no other boards that have
30 the same register layout. Even the CIO-DAS16/M1/16 is
31 significantly different.
33 I was _barely_ able to reach the full 1 MHz capability
34 of this board, using a hard real-time interrupt
35 (set the TRIG_RT flag in your struct comedi_cmd and use
36 rtlinux or RTAI). The board can't do dma, so the bottleneck is
37 pulling the data across the ISA bus. I timed the interrupt
38 handler, and it took my computer ~470 microseconds to pull 512
39 samples from the board. So at 1 Mhz sampling rate,
40 expect your CPU to be spending almost all of its
41 time in the interrupt handler.
43 This board has some unusual restrictions for its channel/gain list. If the
44 list has 2 or more channels in it, then two conditions must be satisfied:
45 (1) - even/odd channels must appear at even/odd indices in the list
46 (2) - the list must have an even number of entries.
50 [1] - irq (optional, but you probably want it)
52 irq can be omitted, although the cmd interface will not work without it.
55 #include <linux/ioport.h>
56 #include <linux/interrupt.h>
57 #include "../comedidev.h"
61 #include "comedi_fc.h"
63 #define DAS16M1_SIZE 16
64 #define DAS16M1_SIZE2 8
66 #define DAS16M1_XTAL 100 /* 10 MHz master clock */
68 #define FIFO_SIZE 1024 /* 1024 sample fifo */
75 0 a/d bits 0-3, mux start 12 bit
76 1 a/d bits 4-11 unused
79 4 unused clear interrupt
81 6 channel/gain queue address
82 7 channel/gain queue data
90 #define DAS16M1_AI 0 /* 16-bit wide register */
91 #define AI_CHAN(x) ((x) & 0xf)
93 #define EXT_TRIG_BIT 0x1
97 #define DAS16M1_CLEAR_INTR 4
98 #define DAS16M1_INTR_CONTROL 5
100 #define INT_PACER 0x3
101 #define PACER_MASK 0x3
103 #define DAS16M1_QUEUE_ADDR 6
104 #define DAS16M1_QUEUE_DATA 7
105 #define Q_CHAN(x) ((x) & 0x7)
106 #define Q_RANGE(x) (((x) & 0xf) << 4)
107 #define UNIPOLAR 0x40
108 #define DAS16M1_8254_FIRST 0x8
109 #define DAS16M1_8254_FIRST_CNTRL 0xb
110 #define TOTAL_CLEAR 0x30
111 #define DAS16M1_8254_SECOND 0xc
112 #define DAS16M1_82C55 0x400
113 #define DAS16M1_8254_THIRD 0x404
115 static const struct comedi_lrange range_das16m1 = { 9,
129 struct das16m1_private_struct {
130 unsigned int control_state;
131 volatile unsigned int adc_count; /* number of samples completed */
132 /* initial value in lower half of hardware conversion counter,
133 * needed to keep track of whether new count has been loaded into
134 * counter yet (loaded by first sample conversion) */
135 u16 initial_hw_count;
136 short ai_buffer[FIFO_SIZE];
137 unsigned int do_bits; /* saves status of digital output bits */
138 unsigned int divisor1; /* divides master clock to obtain conversion speed */
139 unsigned int divisor2; /* divides master clock to obtain conversion speed */
140 unsigned long extra_iobase;
143 static inline short munge_sample(short data)
145 return (data >> 4) & 0xfff;
148 static void munge_sample_array(short *array, unsigned int num_elements)
152 for (i = 0; i < num_elements; i++)
153 array[i] = munge_sample(array[i]);
156 static int das16m1_cmd_test(struct comedi_device *dev,
157 struct comedi_subdevice *s, struct comedi_cmd *cmd)
159 struct das16m1_private_struct *devpriv = dev->private;
160 unsigned int err = 0, tmp, i;
162 /* Step 1 : check if triggers are trivially valid */
164 err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW | TRIG_EXT);
165 err |= cfc_check_trigger_src(&cmd->scan_begin_src, TRIG_FOLLOW);
166 err |= cfc_check_trigger_src(&cmd->convert_src, TRIG_TIMER | TRIG_EXT);
167 err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
168 err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
173 /* Step 2a : make sure trigger sources are unique */
175 err |= cfc_check_trigger_is_unique(cmd->start_src);
176 err |= cfc_check_trigger_is_unique(cmd->convert_src);
177 err |= cfc_check_trigger_is_unique(cmd->stop_src);
179 /* Step 2b : and mutually compatible */
184 /* Step 3: check if arguments are trivially valid */
186 err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
188 if (cmd->scan_begin_src == TRIG_FOLLOW) /* internal trigger */
189 err |= cfc_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
191 if (cmd->convert_src == TRIG_TIMER)
192 err |= cfc_check_trigger_arg_min(&cmd->convert_arg, 1000);
194 err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, cmd->chanlist_len);
196 if (cmd->stop_src == TRIG_COUNT) {
197 /* any count is allowed */
200 err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
206 /* step 4: fix up arguments */
208 if (cmd->convert_src == TRIG_TIMER) {
209 tmp = cmd->convert_arg;
210 /* calculate counter values that give desired timing */
211 i8253_cascade_ns_to_timer_2div(DAS16M1_XTAL,
212 &(devpriv->divisor1),
213 &(devpriv->divisor2),
215 cmd->flags & TRIG_ROUND_MASK);
216 if (tmp != cmd->convert_arg)
223 /* check chanlist against board's peculiarities */
224 if (cmd->chanlist && cmd->chanlist_len > 1) {
225 for (i = 0; i < cmd->chanlist_len; i++) {
226 /* even/odd channels must go into even/odd queue addresses */
227 if ((i % 2) != (CR_CHAN(cmd->chanlist[i]) % 2)) {
228 comedi_error(dev, "bad chanlist:\n"
229 " even/odd channels must go have even/odd chanlist indices");
233 if ((cmd->chanlist_len % 2) != 0) {
235 "chanlist must be of even length or length 1");
246 /* This function takes a time in nanoseconds and sets the *
247 * 2 pacer clocks to the closest frequency possible. It also *
248 * returns the actual sampling period. */
249 static unsigned int das16m1_set_pacer(struct comedi_device *dev,
250 unsigned int ns, int rounding_flags)
252 struct das16m1_private_struct *devpriv = dev->private;
254 i8253_cascade_ns_to_timer_2div(DAS16M1_XTAL, &(devpriv->divisor1),
255 &(devpriv->divisor2), &ns,
256 rounding_flags & TRIG_ROUND_MASK);
258 /* Write the values of ctr1 and ctr2 into counters 1 and 2 */
259 i8254_load(dev->iobase + DAS16M1_8254_SECOND, 0, 1, devpriv->divisor1,
261 i8254_load(dev->iobase + DAS16M1_8254_SECOND, 0, 2, devpriv->divisor2,
267 static int das16m1_cmd_exec(struct comedi_device *dev,
268 struct comedi_subdevice *s)
270 struct das16m1_private_struct *devpriv = dev->private;
271 struct comedi_async *async = s->async;
272 struct comedi_cmd *cmd = &async->cmd;
273 unsigned int byte, i;
276 comedi_error(dev, "irq required to execute comedi_cmd");
280 /* disable interrupts and internal pacer */
281 devpriv->control_state &= ~INTE & ~PACER_MASK;
282 outb(devpriv->control_state, dev->iobase + DAS16M1_INTR_CONTROL);
284 /* set software count */
285 devpriv->adc_count = 0;
286 /* Initialize lower half of hardware counter, used to determine how
287 * many samples are in fifo. Value doesn't actually load into counter
288 * until counter's next clock (the next a/d conversion) */
289 i8254_load(dev->iobase + DAS16M1_8254_FIRST, 0, 1, 0, 2);
290 /* remember current reading of counter so we know when counter has
291 * actually been loaded */
292 devpriv->initial_hw_count =
293 i8254_read(dev->iobase + DAS16M1_8254_FIRST, 0, 1);
294 /* setup channel/gain queue */
295 for (i = 0; i < cmd->chanlist_len; i++) {
296 outb(i, dev->iobase + DAS16M1_QUEUE_ADDR);
298 Q_CHAN(CR_CHAN(cmd->chanlist[i])) |
299 Q_RANGE(CR_RANGE(cmd->chanlist[i]));
300 outb(byte, dev->iobase + DAS16M1_QUEUE_DATA);
303 /* set counter mode and counts */
305 das16m1_set_pacer(dev, cmd->convert_arg,
306 cmd->flags & TRIG_ROUND_MASK);
308 /* set control & status register */
310 /* if we are using external start trigger (also board dislikes having
311 * both start and conversion triggers external simultaneously) */
312 if (cmd->start_src == TRIG_EXT && cmd->convert_src != TRIG_EXT)
313 byte |= EXT_TRIG_BIT;
315 outb(byte, dev->iobase + DAS16M1_CS);
316 /* clear interrupt bit */
317 outb(0, dev->iobase + DAS16M1_CLEAR_INTR);
319 /* enable interrupts and internal pacer */
320 devpriv->control_state &= ~PACER_MASK;
321 if (cmd->convert_src == TRIG_TIMER)
322 devpriv->control_state |= INT_PACER;
324 devpriv->control_state |= EXT_PACER;
326 devpriv->control_state |= INTE;
327 outb(devpriv->control_state, dev->iobase + DAS16M1_INTR_CONTROL);
332 static int das16m1_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
334 struct das16m1_private_struct *devpriv = dev->private;
336 devpriv->control_state &= ~INTE & ~PACER_MASK;
337 outb(devpriv->control_state, dev->iobase + DAS16M1_INTR_CONTROL);
342 static int das16m1_ai_rinsn(struct comedi_device *dev,
343 struct comedi_subdevice *s,
344 struct comedi_insn *insn, unsigned int *data)
346 struct das16m1_private_struct *devpriv = dev->private;
349 const int timeout = 1000;
351 /* disable interrupts and internal pacer */
352 devpriv->control_state &= ~INTE & ~PACER_MASK;
353 outb(devpriv->control_state, dev->iobase + DAS16M1_INTR_CONTROL);
355 /* setup channel/gain queue */
356 outb(0, dev->iobase + DAS16M1_QUEUE_ADDR);
358 Q_CHAN(CR_CHAN(insn->chanspec)) | Q_RANGE(CR_RANGE(insn->chanspec));
359 outb(byte, dev->iobase + DAS16M1_QUEUE_DATA);
361 for (n = 0; n < insn->n; n++) {
362 /* clear IRQDATA bit */
363 outb(0, dev->iobase + DAS16M1_CLEAR_INTR);
364 /* trigger conversion */
365 outb(0, dev->iobase);
367 for (i = 0; i < timeout; i++) {
368 if (inb(dev->iobase + DAS16M1_CS) & IRQDATA)
372 comedi_error(dev, "timeout");
375 data[n] = munge_sample(inw(dev->iobase));
381 static int das16m1_di_rbits(struct comedi_device *dev,
382 struct comedi_subdevice *s,
383 struct comedi_insn *insn, unsigned int *data)
387 bits = inb(dev->iobase + DAS16M1_DIO) & 0xf;
394 static int das16m1_do_wbits(struct comedi_device *dev,
395 struct comedi_subdevice *s,
396 struct comedi_insn *insn, unsigned int *data)
398 struct das16m1_private_struct *devpriv = dev->private;
401 /* only set bits that have been masked */
403 wbits = devpriv->do_bits;
404 /* zero bits that have been masked */
406 /* set masked bits */
407 wbits |= data[0] & data[1];
408 devpriv->do_bits = wbits;
411 outb(devpriv->do_bits, dev->iobase + DAS16M1_DIO);
416 static void das16m1_handler(struct comedi_device *dev, unsigned int status)
418 struct das16m1_private_struct *devpriv = dev->private;
419 struct comedi_subdevice *s;
420 struct comedi_async *async;
421 struct comedi_cmd *cmd;
425 s = dev->read_subdev;
430 /* figure out how many samples are in fifo */
431 hw_counter = i8254_read(dev->iobase + DAS16M1_8254_FIRST, 0, 1);
432 /* make sure hardware counter reading is not bogus due to initial value
433 * not having been loaded yet */
434 if (devpriv->adc_count == 0 && hw_counter == devpriv->initial_hw_count) {
437 /* The calculation of num_samples looks odd, but it uses the following facts.
438 * 16 bit hardware counter is initialized with value of zero (which really
439 * means 0x1000). The counter decrements by one on each conversion
440 * (when the counter decrements from zero it goes to 0xffff). num_samples
441 * is a 16 bit variable, so it will roll over in a similar fashion to the
442 * hardware counter. Work it out, and this is what you get. */
443 num_samples = -hw_counter - devpriv->adc_count;
445 /* check if we only need some of the points */
446 if (cmd->stop_src == TRIG_COUNT) {
447 if (num_samples > cmd->stop_arg * cmd->chanlist_len)
448 num_samples = cmd->stop_arg * cmd->chanlist_len;
450 /* make sure we dont try to get too many points if fifo has overrun */
451 if (num_samples > FIFO_SIZE)
452 num_samples = FIFO_SIZE;
453 insw(dev->iobase, devpriv->ai_buffer, num_samples);
454 munge_sample_array(devpriv->ai_buffer, num_samples);
455 cfc_write_array_to_buffer(s, devpriv->ai_buffer,
456 num_samples * sizeof(short));
457 devpriv->adc_count += num_samples;
459 if (cmd->stop_src == TRIG_COUNT) {
460 if (devpriv->adc_count >= cmd->stop_arg * cmd->chanlist_len) { /* end of acquisition */
461 das16m1_cancel(dev, s);
462 async->events |= COMEDI_CB_EOA;
466 /* this probably won't catch overruns since the card doesn't generate
467 * overrun interrupts, but we might as well try */
468 if (status & OVRUN) {
469 das16m1_cancel(dev, s);
470 async->events |= COMEDI_CB_EOA | COMEDI_CB_ERROR;
471 comedi_error(dev, "fifo overflow");
474 comedi_event(dev, s);
478 static int das16m1_poll(struct comedi_device *dev, struct comedi_subdevice *s)
483 /* prevent race with interrupt handler */
484 spin_lock_irqsave(&dev->spinlock, flags);
485 status = inb(dev->iobase + DAS16M1_CS);
486 das16m1_handler(dev, status);
487 spin_unlock_irqrestore(&dev->spinlock, flags);
489 return s->async->buf_write_count - s->async->buf_read_count;
492 static irqreturn_t das16m1_interrupt(int irq, void *d)
495 struct comedi_device *dev = d;
497 if (!dev->attached) {
498 comedi_error(dev, "premature interrupt");
501 /* prevent race with comedi_poll() */
502 spin_lock(&dev->spinlock);
504 status = inb(dev->iobase + DAS16M1_CS);
506 if ((status & (IRQDATA | OVRUN)) == 0) {
507 comedi_error(dev, "spurious interrupt");
508 spin_unlock(&dev->spinlock);
512 das16m1_handler(dev, status);
514 /* clear interrupt */
515 outb(0, dev->iobase + DAS16M1_CLEAR_INTR);
517 spin_unlock(&dev->spinlock);
521 static int das16m1_irq_bits(unsigned int irq)
562 static int das16m1_attach(struct comedi_device *dev,
563 struct comedi_devconfig *it)
565 struct das16m1_private_struct *devpriv;
566 struct comedi_subdevice *s;
570 devpriv = kzalloc(sizeof(*devpriv), GFP_KERNEL);
573 dev->private = devpriv;
575 ret = comedi_request_region(dev, it->options[0], DAS16M1_SIZE);
578 /* Request an additional region for the 8255 */
579 ret = __comedi_request_region(dev, dev->iobase + DAS16M1_82C55,
583 devpriv->extra_iobase = dev->iobase + DAS16M1_82C55;
585 /* now for the irq */
586 irq = it->options[1];
587 /* make sure it is valid */
588 if (das16m1_irq_bits(irq) >= 0) {
589 ret = request_irq(irq, das16m1_interrupt, 0,
590 dev->driver->driver_name, dev);
596 } else if (irq == 0) {
600 comedi_error(dev, "invalid irq\n"
601 " valid irqs are 2, 3, 5, 7, 10, 11, 12, or 15\n");
605 ret = comedi_alloc_subdevices(dev, 4);
609 s = &dev->subdevices[0];
610 dev->read_subdev = s;
612 s->type = COMEDI_SUBD_AI;
613 s->subdev_flags = SDF_READABLE | SDF_CMD_READ;
615 s->subdev_flags = SDF_DIFF;
616 s->len_chanlist = 256;
617 s->maxdata = (1 << 12) - 1;
618 s->range_table = &range_das16m1;
619 s->insn_read = das16m1_ai_rinsn;
620 s->do_cmdtest = das16m1_cmd_test;
621 s->do_cmd = das16m1_cmd_exec;
622 s->cancel = das16m1_cancel;
623 s->poll = das16m1_poll;
625 s = &dev->subdevices[1];
627 s->type = COMEDI_SUBD_DI;
628 s->subdev_flags = SDF_READABLE;
631 s->range_table = &range_digital;
632 s->insn_bits = das16m1_di_rbits;
634 s = &dev->subdevices[2];
636 s->type = COMEDI_SUBD_DO;
637 s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
640 s->range_table = &range_digital;
641 s->insn_bits = das16m1_do_wbits;
643 s = &dev->subdevices[3];
645 ret = subdev_8255_init(dev, s, NULL, devpriv->extra_iobase);
649 /* disable upper half of hardware conversion counter so it doesn't mess with us */
650 outb(TOTAL_CLEAR, dev->iobase + DAS16M1_8254_FIRST_CNTRL);
652 /* initialize digital output lines */
653 outb(devpriv->do_bits, dev->iobase + DAS16M1_DIO);
655 /* set the interrupt level */
657 devpriv->control_state = das16m1_irq_bits(dev->irq);
659 devpriv->control_state = 0;
660 outb(devpriv->control_state, dev->iobase + DAS16M1_INTR_CONTROL);
665 static void das16m1_detach(struct comedi_device *dev)
667 struct das16m1_private_struct *devpriv = dev->private;
669 if (devpriv && devpriv->extra_iobase)
670 release_region(devpriv->extra_iobase, DAS16M1_SIZE2);
671 comedi_legacy_detach(dev);
674 static struct comedi_driver das16m1_driver = {
675 .driver_name = "das16m1",
676 .module = THIS_MODULE,
677 .attach = das16m1_attach,
678 .detach = das16m1_detach,
680 module_comedi_driver(das16m1_driver);
682 MODULE_AUTHOR("Comedi http://www.comedi.org");
683 MODULE_DESCRIPTION("Comedi low-level driver");
684 MODULE_LICENSE("GPL");