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 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 comedi_lrange range_das16m1
= { 9,
134 static int das16m1_do_wbits(struct comedi_device
* dev
, struct comedi_subdevice
* s
,
135 comedi_insn
* insn
, unsigned int * data
);
136 static int das16m1_di_rbits(struct comedi_device
* dev
, struct comedi_subdevice
* s
,
137 comedi_insn
* insn
, unsigned int * data
);
138 static int das16m1_ai_rinsn(struct comedi_device
* dev
, struct comedi_subdevice
* s
,
139 comedi_insn
* insn
, unsigned int * data
);
141 static int das16m1_cmd_test(struct comedi_device
* dev
, struct comedi_subdevice
* s
,
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 PT_REGS_ARG
);
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 typedef struct das16m1_board_struct
{
157 unsigned int ai_speed
;
160 static const das16m1_board das16m1_boards
[] = {
162 name
: "cio-das16/m1", // CIO-DAS16_M1.pdf
163 ai_speed
:1000, // 1MHz max speed
167 #define das16m1_num_boards ((sizeof(das16m1_boards)) / (sizeof(das16m1_boards[0])))
169 static int das16m1_attach(struct comedi_device
* dev
, comedi_devconfig
* it
);
170 static int das16m1_detach(struct comedi_device
* dev
);
171 static comedi_driver driver_das16m1
= {
172 driver_name
:"das16m1",
174 attach
:das16m1_attach
,
175 detach
:das16m1_detach
,
176 board_name
:&das16m1_boards
[0].name
,
177 num_names
:das16m1_num_boards
,
178 offset
:sizeof(das16m1_boards
[0]),
181 struct das16m1_private_struct
{
182 unsigned int control_state
;
183 volatile unsigned int adc_count
; // number of samples completed
184 /* initial value in lower half of hardware conversion counter,
185 * needed to keep track of whether new count has been loaded into
186 * counter yet (loaded by first sample conversion) */
187 u16 initial_hw_count
;
188 short ai_buffer
[FIFO_SIZE
];
189 unsigned int do_bits
; // saves status of digital output bits
190 unsigned int divisor1
; // divides master clock to obtain conversion speed
191 unsigned int divisor2
; // divides master clock to obtain conversion speed
193 #define devpriv ((struct das16m1_private_struct *)(dev->private))
194 #define thisboard ((const das16m1_board *)(dev->board_ptr))
196 COMEDI_INITCLEANUP(driver_das16m1
);
198 static inline short munge_sample(short data
)
200 return (data
>> 4) & 0xfff;
203 static int das16m1_cmd_test(struct comedi_device
* dev
, struct comedi_subdevice
* s
,
206 unsigned int err
= 0, tmp
, i
;
208 /* make sure triggers are valid */
209 tmp
= cmd
->start_src
;
210 cmd
->start_src
&= TRIG_NOW
| TRIG_EXT
;
211 if (!cmd
->start_src
|| tmp
!= cmd
->start_src
)
214 tmp
= cmd
->scan_begin_src
;
215 cmd
->scan_begin_src
&= TRIG_FOLLOW
;
216 if (!cmd
->scan_begin_src
|| tmp
!= cmd
->scan_begin_src
)
219 tmp
= cmd
->convert_src
;
220 cmd
->convert_src
&= TRIG_TIMER
| TRIG_EXT
;
221 if (!cmd
->convert_src
|| tmp
!= cmd
->convert_src
)
224 tmp
= cmd
->scan_end_src
;
225 cmd
->scan_end_src
&= TRIG_COUNT
;
226 if (!cmd
->scan_end_src
|| tmp
!= cmd
->scan_end_src
)
230 cmd
->stop_src
&= TRIG_COUNT
| TRIG_NONE
;
231 if (!cmd
->stop_src
|| tmp
!= cmd
->stop_src
)
237 /* step 2: make sure trigger sources are unique and mutually compatible */
238 if (cmd
->stop_src
!= TRIG_COUNT
&& cmd
->stop_src
!= TRIG_NONE
)
240 if (cmd
->start_src
!= TRIG_NOW
&& cmd
->start_src
!= TRIG_EXT
)
242 if (cmd
->convert_src
!= TRIG_TIMER
&& cmd
->convert_src
!= TRIG_EXT
)
248 /* step 3: make sure arguments are trivially compatible */
249 if (cmd
->start_arg
!= 0) {
254 if (cmd
->scan_begin_src
== TRIG_FOLLOW
) {
255 /* internal trigger */
256 if (cmd
->scan_begin_arg
!= 0) {
257 cmd
->scan_begin_arg
= 0;
262 if (cmd
->convert_src
== TRIG_TIMER
) {
263 if (cmd
->convert_arg
< thisboard
->ai_speed
) {
264 cmd
->convert_arg
= thisboard
->ai_speed
;
269 if (cmd
->scan_end_arg
!= cmd
->chanlist_len
) {
270 cmd
->scan_end_arg
= cmd
->chanlist_len
;
274 if (cmd
->stop_src
== TRIG_COUNT
) {
275 /* any count is allowed */
278 if (cmd
->stop_arg
!= 0) {
287 /* step 4: fix up arguments */
289 if (cmd
->convert_src
== TRIG_TIMER
) {
290 tmp
= cmd
->convert_arg
;
291 /* calculate counter values that give desired timing */
292 i8253_cascade_ns_to_timer_2div(DAS16M1_XTAL
,
293 &(devpriv
->divisor1
), &(devpriv
->divisor2
),
294 &(cmd
->convert_arg
), cmd
->flags
& TRIG_ROUND_MASK
);
295 if (tmp
!= cmd
->convert_arg
)
302 // check chanlist against board's peculiarities
303 if (cmd
->chanlist
&& cmd
->chanlist_len
> 1) {
304 for (i
= 0; i
< cmd
->chanlist_len
; i
++) {
305 // even/odd channels must go into even/odd queue addresses
306 if ((i
% 2) != (CR_CHAN(cmd
->chanlist
[i
]) % 2)) {
307 comedi_error(dev
, "bad chanlist:\n"
308 " even/odd channels must go have even/odd chanlist indices");
312 if ((cmd
->chanlist_len
% 2) != 0) {
314 "chanlist must be of even length or length 1");
325 static int das16m1_cmd_exec(struct comedi_device
* dev
, struct comedi_subdevice
* s
)
327 struct comedi_async
*async
= s
->async
;
328 comedi_cmd
*cmd
= &async
->cmd
;
329 unsigned int byte
, i
;
332 comedi_error(dev
, "irq required to execute comedi_cmd");
336 /* disable interrupts and internal pacer */
337 devpriv
->control_state
&= ~INTE
& ~PACER_MASK
;
338 outb(devpriv
->control_state
, dev
->iobase
+ DAS16M1_INTR_CONTROL
);
340 // set software count
341 devpriv
->adc_count
= 0;
342 /* Initialize lower half of hardware counter, used to determine how
343 * many samples are in fifo. Value doesn't actually load into counter
344 * until counter's next clock (the next a/d conversion) */
345 i8254_load(dev
->iobase
+ DAS16M1_8254_FIRST
, 0, 1, 0, 2);
346 /* remember current reading of counter so we know when counter has
347 * actually been loaded */
348 devpriv
->initial_hw_count
=
349 i8254_read(dev
->iobase
+ DAS16M1_8254_FIRST
, 0, 1);
350 /* setup channel/gain queue */
351 for (i
= 0; i
< cmd
->chanlist_len
; i
++) {
352 outb(i
, dev
->iobase
+ DAS16M1_QUEUE_ADDR
);
353 byte
= Q_CHAN(CR_CHAN(cmd
->
354 chanlist
[i
])) | Q_RANGE(CR_RANGE(cmd
->
356 outb(byte
, dev
->iobase
+ DAS16M1_QUEUE_DATA
);
359 /* set counter mode and counts */
361 das16m1_set_pacer(dev
, cmd
->convert_arg
,
362 cmd
->flags
& TRIG_ROUND_MASK
);
364 // set control & status register
366 /* if we are using external start trigger (also board dislikes having
367 * both start and conversion triggers external simultaneously) */
368 if (cmd
->start_src
== TRIG_EXT
&& cmd
->convert_src
!= TRIG_EXT
) {
369 byte
|= EXT_TRIG_BIT
;
371 outb(byte
, dev
->iobase
+ DAS16M1_CS
);
372 /* clear interrupt bit */
373 outb(0, dev
->iobase
+ DAS16M1_CLEAR_INTR
);
375 /* enable interrupts and internal pacer */
376 devpriv
->control_state
&= ~PACER_MASK
;
377 if (cmd
->convert_src
== TRIG_TIMER
) {
378 devpriv
->control_state
|= INT_PACER
;
380 devpriv
->control_state
|= EXT_PACER
;
382 devpriv
->control_state
|= INTE
;
383 outb(devpriv
->control_state
, dev
->iobase
+ DAS16M1_INTR_CONTROL
);
388 static int das16m1_cancel(struct comedi_device
* dev
, struct comedi_subdevice
* s
)
390 devpriv
->control_state
&= ~INTE
& ~PACER_MASK
;
391 outb(devpriv
->control_state
, dev
->iobase
+ DAS16M1_INTR_CONTROL
);
396 static int das16m1_ai_rinsn(struct comedi_device
* dev
, struct comedi_subdevice
* s
,
397 comedi_insn
* insn
, unsigned int * data
)
401 const int timeout
= 1000;
403 /* disable interrupts and internal pacer */
404 devpriv
->control_state
&= ~INTE
& ~PACER_MASK
;
405 outb(devpriv
->control_state
, dev
->iobase
+ DAS16M1_INTR_CONTROL
);
407 /* setup channel/gain queue */
408 outb(0, dev
->iobase
+ DAS16M1_QUEUE_ADDR
);
409 byte
= Q_CHAN(CR_CHAN(insn
->chanspec
)) | Q_RANGE(CR_RANGE(insn
->
411 outb(byte
, dev
->iobase
+ DAS16M1_QUEUE_DATA
);
413 for (n
= 0; n
< insn
->n
; n
++) {
414 /* clear IRQDATA bit */
415 outb(0, dev
->iobase
+ DAS16M1_CLEAR_INTR
);
416 /* trigger conversion */
417 outb(0, dev
->iobase
);
419 for (i
= 0; i
< timeout
; i
++) {
420 if (inb(dev
->iobase
+ DAS16M1_CS
) & IRQDATA
)
424 comedi_error(dev
, "timeout");
427 data
[n
] = munge_sample(inw(dev
->iobase
));
433 static int das16m1_di_rbits(struct comedi_device
* dev
, struct comedi_subdevice
* s
,
434 comedi_insn
* insn
, unsigned int * data
)
438 bits
= inb(dev
->iobase
+ DAS16M1_DIO
) & 0xf;
445 static int das16m1_do_wbits(struct comedi_device
* dev
, struct comedi_subdevice
* s
,
446 comedi_insn
* insn
, unsigned int * data
)
450 // only set bits that have been masked
452 wbits
= devpriv
->do_bits
;
453 // zero bits that have been masked
456 wbits
|= data
[0] & data
[1];
457 devpriv
->do_bits
= wbits
;
460 outb(devpriv
->do_bits
, dev
->iobase
+ DAS16M1_DIO
);
465 static int das16m1_poll(struct comedi_device
* dev
, struct comedi_subdevice
* s
)
470 // prevent race with interrupt handler
471 comedi_spin_lock_irqsave(&dev
->spinlock
, flags
);
472 status
= inb(dev
->iobase
+ DAS16M1_CS
);
473 das16m1_handler(dev
, status
);
474 comedi_spin_unlock_irqrestore(&dev
->spinlock
, flags
);
476 return s
->async
->buf_write_count
- s
->async
->buf_read_count
;
479 static irqreturn_t
das16m1_interrupt(int irq
, void *d PT_REGS_ARG
)
482 struct comedi_device
*dev
= d
;
484 if (dev
->attached
== 0) {
485 comedi_error(dev
, "premature interrupt");
488 // prevent race with comedi_poll()
489 spin_lock(&dev
->spinlock
);
491 status
= inb(dev
->iobase
+ DAS16M1_CS
);
493 if ((status
& (IRQDATA
| OVRUN
)) == 0) {
494 comedi_error(dev
, "spurious interrupt");
495 spin_unlock(&dev
->spinlock
);
499 das16m1_handler(dev
, status
);
501 /* clear interrupt */
502 outb(0, dev
->iobase
+ DAS16M1_CLEAR_INTR
);
504 spin_unlock(&dev
->spinlock
);
508 static void munge_sample_array(short * array
, unsigned int num_elements
)
512 for (i
= 0; i
< num_elements
; i
++) {
513 array
[i
] = munge_sample(array
[i
]);
517 static void das16m1_handler(struct comedi_device
* dev
, unsigned int status
)
519 struct comedi_subdevice
*s
;
520 struct comedi_async
*async
;
525 s
= dev
->read_subdev
;
530 // figure out how many samples are in fifo
531 hw_counter
= i8254_read(dev
->iobase
+ DAS16M1_8254_FIRST
, 0, 1);
532 /* make sure hardware counter reading is not bogus due to initial value
533 * not having been loaded yet */
534 if (devpriv
->adc_count
== 0 && hw_counter
== devpriv
->initial_hw_count
) {
537 /* The calculation of num_samples looks odd, but it uses the following facts.
538 * 16 bit hardware counter is initialized with value of zero (which really
539 * means 0x1000). The counter decrements by one on each conversion
540 * (when the counter decrements from zero it goes to 0xffff). num_samples
541 * is a 16 bit variable, so it will roll over in a similar fashion to the
542 * hardware counter. Work it out, and this is what you get. */
543 num_samples
= -hw_counter
- devpriv
->adc_count
;
545 // check if we only need some of the points
546 if (cmd
->stop_src
== TRIG_COUNT
) {
547 if (num_samples
> cmd
->stop_arg
* cmd
->chanlist_len
)
548 num_samples
= cmd
->stop_arg
* cmd
->chanlist_len
;
550 // make sure we dont try to get too many points if fifo has overrun
551 if (num_samples
> FIFO_SIZE
)
552 num_samples
= FIFO_SIZE
;
553 insw(dev
->iobase
, devpriv
->ai_buffer
, num_samples
);
554 munge_sample_array(devpriv
->ai_buffer
, num_samples
);
555 cfc_write_array_to_buffer(s
, devpriv
->ai_buffer
,
556 num_samples
* sizeof(short));
557 devpriv
->adc_count
+= num_samples
;
559 if (cmd
->stop_src
== TRIG_COUNT
) {
560 if (devpriv
->adc_count
>= cmd
->stop_arg
* cmd
->chanlist_len
) { /* end of acquisition */
561 das16m1_cancel(dev
, s
);
562 async
->events
|= COMEDI_CB_EOA
;
566 /* this probably won't catch overruns since the card doesn't generate
567 * overrun interrupts, but we might as well try */
568 if (status
& OVRUN
) {
569 das16m1_cancel(dev
, s
);
570 async
->events
|= COMEDI_CB_EOA
| COMEDI_CB_ERROR
;
571 comedi_error(dev
, "fifo overflow");
574 comedi_event(dev
, s
);
578 /* This function takes a time in nanoseconds and sets the *
579 * 2 pacer clocks to the closest frequency possible. It also *
580 * returns the actual sampling period. */
581 static unsigned int das16m1_set_pacer(struct comedi_device
* dev
, unsigned int ns
,
584 i8253_cascade_ns_to_timer_2div(DAS16M1_XTAL
, &(devpriv
->divisor1
),
585 &(devpriv
->divisor2
), &ns
, rounding_flags
& TRIG_ROUND_MASK
);
587 /* Write the values of ctr1 and ctr2 into counters 1 and 2 */
588 i8254_load(dev
->iobase
+ DAS16M1_8254_SECOND
, 0, 1, devpriv
->divisor1
,
590 i8254_load(dev
->iobase
+ DAS16M1_8254_SECOND
, 0, 2, devpriv
->divisor2
,
596 static int das16m1_irq_bits(unsigned int irq
)
638 static int das16m1_attach(struct comedi_device
* dev
, comedi_devconfig
* it
)
640 struct comedi_subdevice
*s
;
643 unsigned long iobase
;
645 iobase
= it
->options
[0];
647 printk("comedi%d: das16m1:", dev
->minor
);
649 if ((ret
= alloc_private(dev
,
650 sizeof(struct das16m1_private_struct
))) < 0)
653 dev
->board_name
= thisboard
->name
;
655 printk(" io 0x%lx-0x%lx 0x%lx-0x%lx",
656 iobase
, iobase
+ DAS16M1_SIZE
,
657 iobase
+ DAS16M1_82C55
, iobase
+ DAS16M1_82C55
+ DAS16M1_SIZE2
);
658 if (!request_region(iobase
, DAS16M1_SIZE
, driver_das16m1
.driver_name
)) {
659 printk(" I/O port conflict\n");
662 if (!request_region(iobase
+ DAS16M1_82C55
, DAS16M1_SIZE2
,
663 driver_das16m1
.driver_name
)) {
664 release_region(iobase
, DAS16M1_SIZE
);
665 printk(" I/O port conflict\n");
668 dev
->iobase
= iobase
;
670 /* now for the irq */
671 irq
= it
->options
[1];
672 // make sure it is valid
673 if (das16m1_irq_bits(irq
) >= 0) {
674 ret
= comedi_request_irq(irq
, das16m1_interrupt
, 0,
675 driver_das16m1
.driver_name
, dev
);
677 printk(", irq unavailable\n");
681 printk(", irq %u\n", irq
);
682 } else if (irq
== 0) {
683 printk(", no irq\n");
685 printk(", invalid irq\n"
686 " valid irqs are 2, 3, 5, 7, 10, 11, 12, or 15\n");
690 if ((ret
= alloc_subdevices(dev
, 4)) < 0)
693 s
= dev
->subdevices
+ 0;
694 dev
->read_subdev
= s
;
696 s
->type
= COMEDI_SUBD_AI
;
697 s
->subdev_flags
= SDF_READABLE
| SDF_CMD_READ
;
699 s
->subdev_flags
= SDF_DIFF
;
700 s
->len_chanlist
= 256;
701 s
->maxdata
= (1 << 12) - 1;
702 s
->range_table
= &range_das16m1
;
703 s
->insn_read
= das16m1_ai_rinsn
;
704 s
->do_cmdtest
= das16m1_cmd_test
;
705 s
->do_cmd
= das16m1_cmd_exec
;
706 s
->cancel
= das16m1_cancel
;
707 s
->poll
= das16m1_poll
;
709 s
= dev
->subdevices
+ 1;
711 s
->type
= COMEDI_SUBD_DI
;
712 s
->subdev_flags
= SDF_READABLE
;
715 s
->range_table
= &range_digital
;
716 s
->insn_bits
= das16m1_di_rbits
;
718 s
= dev
->subdevices
+ 2;
720 s
->type
= COMEDI_SUBD_DO
;
721 s
->subdev_flags
= SDF_WRITABLE
| SDF_READABLE
;
724 s
->range_table
= &range_digital
;
725 s
->insn_bits
= das16m1_do_wbits
;
727 s
= dev
->subdevices
+ 3;
729 subdev_8255_init(dev
, s
, NULL
, dev
->iobase
+ DAS16M1_82C55
);
731 // disable upper half of hardware conversion counter so it doesn't mess with us
732 outb(TOTAL_CLEAR
, dev
->iobase
+ DAS16M1_8254_FIRST_CNTRL
);
734 // initialize digital output lines
735 outb(devpriv
->do_bits
, dev
->iobase
+ DAS16M1_DIO
);
737 /* set the interrupt level */
739 devpriv
->control_state
= das16m1_irq_bits(dev
->irq
);
741 devpriv
->control_state
= 0;
742 outb(devpriv
->control_state
, dev
->iobase
+ DAS16M1_INTR_CONTROL
);
747 static int das16m1_detach(struct comedi_device
* dev
)
749 printk("comedi%d: das16m1: remove\n", dev
->minor
);
751 // das16m1_reset(dev);
754 subdev_8255_cleanup(dev
, dev
->subdevices
+ 3);
757 comedi_free_irq(dev
->irq
, dev
);
760 release_region(dev
->iobase
, DAS16M1_SIZE
);
761 release_region(dev
->iobase
+ DAS16M1_82C55
, DAS16M1_SIZE2
);