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.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 ************************************************************************
29 Description: CIO-DAS16/M1
30 Author: Frank Mori Hess <fmhess@users.sourceforge.net>
31 Devices: [Measurement Computing] CIO-DAS16/M1 (cio-das16/m1)
34 This driver supports a single board - the CIO-DAS16/M1.
35 As far as I know, there are no other boards that have
36 the same register layout. Even the CIO-DAS16/M1/16 is
37 significantly different.
39 I was _barely_ able to reach the full 1 MHz capability
40 of this board, using a hard real-time interrupt
41 (set the TRIG_RT flag in your struct comedi_cmd and use
42 rtlinux or RTAI). The board can't do dma, so the bottleneck is
43 pulling the data across the ISA bus. I timed the interrupt
44 handler, and it took my computer ~470 microseconds to pull 512
45 samples from the board. So at 1 Mhz sampling rate,
46 expect your CPU to be spending almost all of its
47 time in the interrupt handler.
49 This board has some unusual restrictions for its channel/gain list. If the
50 list has 2 or more channels in it, then two conditions must be satisfied:
51 (1) - even/odd channels must appear at even/odd indices in the list
52 (2) - the list must have an even number of entries.
56 [1] - irq (optional, but you probably want it)
58 irq can be omitted, although the cmd interface will not work without it.
61 #include <linux/ioport.h>
62 #include "../comedidev.h"
66 #include "comedi_fc.h"
68 #define DAS16M1_SIZE 16
69 #define DAS16M1_SIZE2 8
71 #define DAS16M1_XTAL 100 /* 10 MHz master clock */
73 #define FIFO_SIZE 1024 /* 1024 sample fifo */
80 0 a/d bits 0-3, mux start 12 bit
81 1 a/d bits 4-11 unused
84 4 unused clear interrupt
86 6 channel/gain queue address
87 7 channel/gain queue data
95 #define DAS16M1_AI 0 /* 16-bit wide register */
96 #define AI_CHAN(x) ((x) & 0xf)
98 #define EXT_TRIG_BIT 0x1
101 #define DAS16M1_DIO 3
102 #define DAS16M1_CLEAR_INTR 4
103 #define DAS16M1_INTR_CONTROL 5
104 #define EXT_PACER 0x2
105 #define INT_PACER 0x3
106 #define PACER_MASK 0x3
108 #define DAS16M1_QUEUE_ADDR 6
109 #define DAS16M1_QUEUE_DATA 7
110 #define Q_CHAN(x) ((x) & 0x7)
111 #define Q_RANGE(x) (((x) & 0xf) << 4)
112 #define UNIPOLAR 0x40
113 #define DAS16M1_8254_FIRST 0x8
114 #define DAS16M1_8254_FIRST_CNTRL 0xb
115 #define TOTAL_CLEAR 0x30
116 #define DAS16M1_8254_SECOND 0xc
117 #define DAS16M1_82C55 0x400
118 #define DAS16M1_8254_THIRD 0x404
120 static const struct comedi_lrange range_das16m1
= { 9,
134 static int das16m1_do_wbits(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
135 struct comedi_insn
*insn
, unsigned int *data
);
136 static int das16m1_di_rbits(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
137 struct comedi_insn
*insn
, unsigned int *data
);
138 static int das16m1_ai_rinsn(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
139 struct comedi_insn
*insn
, unsigned int *data
);
141 static int das16m1_cmd_test(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
142 struct comedi_cmd
*cmd
);
143 static int das16m1_cmd_exec(struct comedi_device
*dev
, struct comedi_subdevice
*s
);
144 static int das16m1_cancel(struct comedi_device
*dev
, struct comedi_subdevice
*s
);
146 static int das16m1_poll(struct comedi_device
*dev
, struct comedi_subdevice
*s
);
147 static irqreturn_t
das16m1_interrupt(int irq
, void *d
);
148 static void das16m1_handler(struct comedi_device
*dev
, unsigned int status
);
150 static unsigned int das16m1_set_pacer(struct comedi_device
*dev
, unsigned int ns
,
153 static int das16m1_irq_bits(unsigned int irq
);
155 struct das16m1_board
{
157 unsigned int ai_speed
;
160 static const struct das16m1_board das16m1_boards
[] = {
162 .name
= "cio-das16/m1", /* CIO-DAS16_M1.pdf */
163 .ai_speed
= 1000, /* 1MHz max speed */
167 static int das16m1_attach(struct comedi_device
*dev
, struct comedi_devconfig
*it
);
168 static int das16m1_detach(struct comedi_device
*dev
);
169 static struct comedi_driver driver_das16m1
= {
170 .driver_name
= "das16m1",
171 .module
= THIS_MODULE
,
172 .attach
= das16m1_attach
,
173 .detach
= das16m1_detach
,
174 .board_name
= &das16m1_boards
[0].name
,
175 .num_names
= ARRAY_SIZE(das16m1_boards
),
176 .offset
= sizeof(das16m1_boards
[0]),
179 struct das16m1_private_struct
{
180 unsigned int control_state
;
181 volatile unsigned int adc_count
; /* number of samples completed */
182 /* initial value in lower half of hardware conversion counter,
183 * needed to keep track of whether new count has been loaded into
184 * counter yet (loaded by first sample conversion) */
185 u16 initial_hw_count
;
186 short ai_buffer
[FIFO_SIZE
];
187 unsigned int do_bits
; /* saves status of digital output bits */
188 unsigned int divisor1
; /* divides master clock to obtain conversion speed */
189 unsigned int divisor2
; /* divides master clock to obtain conversion speed */
191 #define devpriv ((struct das16m1_private_struct *)(dev->private))
192 #define thisboard ((const struct das16m1_board *)(dev->board_ptr))
194 COMEDI_INITCLEANUP(driver_das16m1
);
196 static inline short munge_sample(short data
)
198 return (data
>> 4) & 0xfff;
201 static int das16m1_cmd_test(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
202 struct comedi_cmd
*cmd
)
204 unsigned int err
= 0, tmp
, i
;
206 /* make sure triggers are valid */
207 tmp
= cmd
->start_src
;
208 cmd
->start_src
&= TRIG_NOW
| TRIG_EXT
;
209 if (!cmd
->start_src
|| tmp
!= cmd
->start_src
)
212 tmp
= cmd
->scan_begin_src
;
213 cmd
->scan_begin_src
&= TRIG_FOLLOW
;
214 if (!cmd
->scan_begin_src
|| tmp
!= cmd
->scan_begin_src
)
217 tmp
= cmd
->convert_src
;
218 cmd
->convert_src
&= TRIG_TIMER
| TRIG_EXT
;
219 if (!cmd
->convert_src
|| tmp
!= cmd
->convert_src
)
222 tmp
= cmd
->scan_end_src
;
223 cmd
->scan_end_src
&= TRIG_COUNT
;
224 if (!cmd
->scan_end_src
|| tmp
!= cmd
->scan_end_src
)
228 cmd
->stop_src
&= TRIG_COUNT
| TRIG_NONE
;
229 if (!cmd
->stop_src
|| tmp
!= cmd
->stop_src
)
235 /* step 2: make sure trigger sources are unique and mutually compatible */
236 if (cmd
->stop_src
!= TRIG_COUNT
&& cmd
->stop_src
!= TRIG_NONE
)
238 if (cmd
->start_src
!= TRIG_NOW
&& cmd
->start_src
!= TRIG_EXT
)
240 if (cmd
->convert_src
!= TRIG_TIMER
&& cmd
->convert_src
!= TRIG_EXT
)
246 /* step 3: make sure arguments are trivially compatible */
247 if (cmd
->start_arg
!= 0) {
252 if (cmd
->scan_begin_src
== TRIG_FOLLOW
) {
253 /* internal trigger */
254 if (cmd
->scan_begin_arg
!= 0) {
255 cmd
->scan_begin_arg
= 0;
260 if (cmd
->convert_src
== TRIG_TIMER
) {
261 if (cmd
->convert_arg
< thisboard
->ai_speed
) {
262 cmd
->convert_arg
= thisboard
->ai_speed
;
267 if (cmd
->scan_end_arg
!= cmd
->chanlist_len
) {
268 cmd
->scan_end_arg
= cmd
->chanlist_len
;
272 if (cmd
->stop_src
== TRIG_COUNT
) {
273 /* any count is allowed */
276 if (cmd
->stop_arg
!= 0) {
285 /* step 4: fix up arguments */
287 if (cmd
->convert_src
== TRIG_TIMER
) {
288 tmp
= cmd
->convert_arg
;
289 /* calculate counter values that give desired timing */
290 i8253_cascade_ns_to_timer_2div(DAS16M1_XTAL
,
291 &(devpriv
->divisor1
), &(devpriv
->divisor2
),
292 &(cmd
->convert_arg
), cmd
->flags
& TRIG_ROUND_MASK
);
293 if (tmp
!= cmd
->convert_arg
)
300 /* check chanlist against board's peculiarities */
301 if (cmd
->chanlist
&& cmd
->chanlist_len
> 1) {
302 for (i
= 0; i
< cmd
->chanlist_len
; i
++) {
303 /* even/odd channels must go into even/odd queue addresses */
304 if ((i
% 2) != (CR_CHAN(cmd
->chanlist
[i
]) % 2)) {
305 comedi_error(dev
, "bad chanlist:\n"
306 " even/odd channels must go have even/odd chanlist indices");
310 if ((cmd
->chanlist_len
% 2) != 0) {
312 "chanlist must be of even length or length 1");
323 static int das16m1_cmd_exec(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
325 struct comedi_async
*async
= s
->async
;
326 struct comedi_cmd
*cmd
= &async
->cmd
;
327 unsigned int byte
, i
;
330 comedi_error(dev
, "irq required to execute comedi_cmd");
334 /* disable interrupts and internal pacer */
335 devpriv
->control_state
&= ~INTE
& ~PACER_MASK
;
336 outb(devpriv
->control_state
, dev
->iobase
+ DAS16M1_INTR_CONTROL
);
338 /* set software count */
339 devpriv
->adc_count
= 0;
340 /* Initialize lower half of hardware counter, used to determine how
341 * many samples are in fifo. Value doesn't actually load into counter
342 * until counter's next clock (the next a/d conversion) */
343 i8254_load(dev
->iobase
+ DAS16M1_8254_FIRST
, 0, 1, 0, 2);
344 /* remember current reading of counter so we know when counter has
345 * actually been loaded */
346 devpriv
->initial_hw_count
=
347 i8254_read(dev
->iobase
+ DAS16M1_8254_FIRST
, 0, 1);
348 /* setup channel/gain queue */
349 for (i
= 0; i
< cmd
->chanlist_len
; i
++) {
350 outb(i
, dev
->iobase
+ DAS16M1_QUEUE_ADDR
);
351 byte
= Q_CHAN(CR_CHAN(cmd
->
352 chanlist
[i
])) | Q_RANGE(CR_RANGE(cmd
->
354 outb(byte
, dev
->iobase
+ DAS16M1_QUEUE_DATA
);
357 /* set counter mode and counts */
359 das16m1_set_pacer(dev
, cmd
->convert_arg
,
360 cmd
->flags
& TRIG_ROUND_MASK
);
362 /* set control & status register */
364 /* if we are using external start trigger (also board dislikes having
365 * both start and conversion triggers external simultaneously) */
366 if (cmd
->start_src
== TRIG_EXT
&& cmd
->convert_src
!= TRIG_EXT
) {
367 byte
|= EXT_TRIG_BIT
;
369 outb(byte
, dev
->iobase
+ DAS16M1_CS
);
370 /* clear interrupt bit */
371 outb(0, dev
->iobase
+ DAS16M1_CLEAR_INTR
);
373 /* enable interrupts and internal pacer */
374 devpriv
->control_state
&= ~PACER_MASK
;
375 if (cmd
->convert_src
== TRIG_TIMER
) {
376 devpriv
->control_state
|= INT_PACER
;
378 devpriv
->control_state
|= EXT_PACER
;
380 devpriv
->control_state
|= INTE
;
381 outb(devpriv
->control_state
, dev
->iobase
+ DAS16M1_INTR_CONTROL
);
386 static int das16m1_cancel(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
388 devpriv
->control_state
&= ~INTE
& ~PACER_MASK
;
389 outb(devpriv
->control_state
, dev
->iobase
+ DAS16M1_INTR_CONTROL
);
394 static int das16m1_ai_rinsn(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
395 struct comedi_insn
*insn
, unsigned int *data
)
399 const int timeout
= 1000;
401 /* disable interrupts and internal pacer */
402 devpriv
->control_state
&= ~INTE
& ~PACER_MASK
;
403 outb(devpriv
->control_state
, dev
->iobase
+ DAS16M1_INTR_CONTROL
);
405 /* setup channel/gain queue */
406 outb(0, dev
->iobase
+ DAS16M1_QUEUE_ADDR
);
407 byte
= Q_CHAN(CR_CHAN(insn
->chanspec
)) | Q_RANGE(CR_RANGE(insn
->
409 outb(byte
, dev
->iobase
+ DAS16M1_QUEUE_DATA
);
411 for (n
= 0; n
< insn
->n
; n
++) {
412 /* clear IRQDATA bit */
413 outb(0, dev
->iobase
+ DAS16M1_CLEAR_INTR
);
414 /* trigger conversion */
415 outb(0, dev
->iobase
);
417 for (i
= 0; i
< timeout
; i
++) {
418 if (inb(dev
->iobase
+ DAS16M1_CS
) & IRQDATA
)
422 comedi_error(dev
, "timeout");
425 data
[n
] = munge_sample(inw(dev
->iobase
));
431 static int das16m1_di_rbits(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
432 struct comedi_insn
*insn
, unsigned int *data
)
436 bits
= inb(dev
->iobase
+ DAS16M1_DIO
) & 0xf;
443 static int das16m1_do_wbits(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
444 struct comedi_insn
*insn
, unsigned int *data
)
448 /* only set bits that have been masked */
450 wbits
= devpriv
->do_bits
;
451 /* zero bits that have been masked */
453 /* set masked bits */
454 wbits
|= data
[0] & data
[1];
455 devpriv
->do_bits
= wbits
;
458 outb(devpriv
->do_bits
, dev
->iobase
+ DAS16M1_DIO
);
463 static int das16m1_poll(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
468 /* prevent race with interrupt handler */
469 spin_lock_irqsave(&dev
->spinlock
, flags
);
470 status
= inb(dev
->iobase
+ DAS16M1_CS
);
471 das16m1_handler(dev
, status
);
472 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
474 return s
->async
->buf_write_count
- s
->async
->buf_read_count
;
477 static irqreturn_t
das16m1_interrupt(int irq
, void *d
)
480 struct comedi_device
*dev
= d
;
482 if (dev
->attached
== 0) {
483 comedi_error(dev
, "premature interrupt");
486 /* prevent race with comedi_poll() */
487 spin_lock(&dev
->spinlock
);
489 status
= inb(dev
->iobase
+ DAS16M1_CS
);
491 if ((status
& (IRQDATA
| OVRUN
)) == 0) {
492 comedi_error(dev
, "spurious interrupt");
493 spin_unlock(&dev
->spinlock
);
497 das16m1_handler(dev
, status
);
499 /* clear interrupt */
500 outb(0, dev
->iobase
+ DAS16M1_CLEAR_INTR
);
502 spin_unlock(&dev
->spinlock
);
506 static void munge_sample_array(short *array
, unsigned int num_elements
)
510 for (i
= 0; i
< num_elements
; i
++) {
511 array
[i
] = munge_sample(array
[i
]);
515 static void das16m1_handler(struct comedi_device
*dev
, unsigned int status
)
517 struct comedi_subdevice
*s
;
518 struct comedi_async
*async
;
519 struct comedi_cmd
*cmd
;
523 s
= dev
->read_subdev
;
528 /* figure out how many samples are in fifo */
529 hw_counter
= i8254_read(dev
->iobase
+ DAS16M1_8254_FIRST
, 0, 1);
530 /* make sure hardware counter reading is not bogus due to initial value
531 * not having been loaded yet */
532 if (devpriv
->adc_count
== 0 && hw_counter
== devpriv
->initial_hw_count
) {
535 /* The calculation of num_samples looks odd, but it uses the following facts.
536 * 16 bit hardware counter is initialized with value of zero (which really
537 * means 0x1000). The counter decrements by one on each conversion
538 * (when the counter decrements from zero it goes to 0xffff). num_samples
539 * is a 16 bit variable, so it will roll over in a similar fashion to the
540 * hardware counter. Work it out, and this is what you get. */
541 num_samples
= -hw_counter
- devpriv
->adc_count
;
543 /* check if we only need some of the points */
544 if (cmd
->stop_src
== TRIG_COUNT
) {
545 if (num_samples
> cmd
->stop_arg
* cmd
->chanlist_len
)
546 num_samples
= cmd
->stop_arg
* cmd
->chanlist_len
;
548 /* make sure we dont try to get too many points if fifo has overrun */
549 if (num_samples
> FIFO_SIZE
)
550 num_samples
= FIFO_SIZE
;
551 insw(dev
->iobase
, devpriv
->ai_buffer
, num_samples
);
552 munge_sample_array(devpriv
->ai_buffer
, num_samples
);
553 cfc_write_array_to_buffer(s
, devpriv
->ai_buffer
,
554 num_samples
* sizeof(short));
555 devpriv
->adc_count
+= num_samples
;
557 if (cmd
->stop_src
== TRIG_COUNT
) {
558 if (devpriv
->adc_count
>= cmd
->stop_arg
* cmd
->chanlist_len
) { /* end of acquisition */
559 das16m1_cancel(dev
, s
);
560 async
->events
|= COMEDI_CB_EOA
;
564 /* this probably won't catch overruns since the card doesn't generate
565 * overrun interrupts, but we might as well try */
566 if (status
& OVRUN
) {
567 das16m1_cancel(dev
, s
);
568 async
->events
|= COMEDI_CB_EOA
| COMEDI_CB_ERROR
;
569 comedi_error(dev
, "fifo overflow");
572 comedi_event(dev
, s
);
576 /* This function takes a time in nanoseconds and sets the *
577 * 2 pacer clocks to the closest frequency possible. It also *
578 * returns the actual sampling period. */
579 static unsigned int das16m1_set_pacer(struct comedi_device
*dev
, unsigned int ns
,
582 i8253_cascade_ns_to_timer_2div(DAS16M1_XTAL
, &(devpriv
->divisor1
),
583 &(devpriv
->divisor2
), &ns
, rounding_flags
& TRIG_ROUND_MASK
);
585 /* Write the values of ctr1 and ctr2 into counters 1 and 2 */
586 i8254_load(dev
->iobase
+ DAS16M1_8254_SECOND
, 0, 1, devpriv
->divisor1
,
588 i8254_load(dev
->iobase
+ DAS16M1_8254_SECOND
, 0, 2, devpriv
->divisor2
,
594 static int das16m1_irq_bits(unsigned int irq
)
636 static int das16m1_attach(struct comedi_device
*dev
, struct comedi_devconfig
*it
)
638 struct comedi_subdevice
*s
;
641 unsigned long iobase
;
643 iobase
= it
->options
[0];
645 printk("comedi%d: das16m1:", dev
->minor
);
647 ret
= alloc_private(dev
, sizeof(struct das16m1_private_struct
));
651 dev
->board_name
= thisboard
->name
;
653 printk(" io 0x%lx-0x%lx 0x%lx-0x%lx",
654 iobase
, iobase
+ DAS16M1_SIZE
,
655 iobase
+ DAS16M1_82C55
, iobase
+ DAS16M1_82C55
+ DAS16M1_SIZE2
);
656 if (!request_region(iobase
, DAS16M1_SIZE
, driver_das16m1
.driver_name
)) {
657 printk(" I/O port conflict\n");
660 if (!request_region(iobase
+ DAS16M1_82C55
, DAS16M1_SIZE2
,
661 driver_das16m1
.driver_name
)) {
662 release_region(iobase
, DAS16M1_SIZE
);
663 printk(" I/O port conflict\n");
666 dev
->iobase
= iobase
;
668 /* now for the irq */
669 irq
= it
->options
[1];
670 /* make sure it is valid */
671 if (das16m1_irq_bits(irq
) >= 0) {
672 ret
= request_irq(irq
, das16m1_interrupt
, 0,
673 driver_das16m1
.driver_name
, dev
);
675 printk(", irq unavailable\n");
679 printk(", irq %u\n", irq
);
680 } else if (irq
== 0) {
681 printk(", no irq\n");
683 printk(", invalid irq\n"
684 " valid irqs are 2, 3, 5, 7, 10, 11, 12, or 15\n");
688 ret
= alloc_subdevices(dev
, 4);
692 s
= dev
->subdevices
+ 0;
693 dev
->read_subdev
= s
;
695 s
->type
= COMEDI_SUBD_AI
;
696 s
->subdev_flags
= SDF_READABLE
| SDF_CMD_READ
;
698 s
->subdev_flags
= SDF_DIFF
;
699 s
->len_chanlist
= 256;
700 s
->maxdata
= (1 << 12) - 1;
701 s
->range_table
= &range_das16m1
;
702 s
->insn_read
= das16m1_ai_rinsn
;
703 s
->do_cmdtest
= das16m1_cmd_test
;
704 s
->do_cmd
= das16m1_cmd_exec
;
705 s
->cancel
= das16m1_cancel
;
706 s
->poll
= das16m1_poll
;
708 s
= dev
->subdevices
+ 1;
710 s
->type
= COMEDI_SUBD_DI
;
711 s
->subdev_flags
= SDF_READABLE
;
714 s
->range_table
= &range_digital
;
715 s
->insn_bits
= das16m1_di_rbits
;
717 s
= dev
->subdevices
+ 2;
719 s
->type
= COMEDI_SUBD_DO
;
720 s
->subdev_flags
= SDF_WRITABLE
| SDF_READABLE
;
723 s
->range_table
= &range_digital
;
724 s
->insn_bits
= das16m1_do_wbits
;
726 s
= dev
->subdevices
+ 3;
728 subdev_8255_init(dev
, s
, NULL
, dev
->iobase
+ DAS16M1_82C55
);
730 /* disable upper half of hardware conversion counter so it doesn't mess with us */
731 outb(TOTAL_CLEAR
, dev
->iobase
+ DAS16M1_8254_FIRST_CNTRL
);
733 /* initialize digital output lines */
734 outb(devpriv
->do_bits
, dev
->iobase
+ DAS16M1_DIO
);
736 /* set the interrupt level */
738 devpriv
->control_state
= das16m1_irq_bits(dev
->irq
);
740 devpriv
->control_state
= 0;
741 outb(devpriv
->control_state
, dev
->iobase
+ DAS16M1_INTR_CONTROL
);
746 static int das16m1_detach(struct comedi_device
*dev
)
748 printk("comedi%d: das16m1: remove\n", dev
->minor
);
750 /* das16m1_reset(dev); */
753 subdev_8255_cleanup(dev
, dev
->subdevices
+ 3);
756 free_irq(dev
->irq
, dev
);
759 release_region(dev
->iobase
, DAS16M1_SIZE
);
760 release_region(dev
->iobase
+ DAS16M1_82C55
, DAS16M1_SIZE2
);