2 comedi/drivers/dmm32at.c
3 Diamond Systems mm32at code for a Comedi driver
5 COMEDI - Linux Control and Measurement Device Interface
6 Copyright (C) 2000 David A. Schleef <ds@schleef.org>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 Description: Diamond Systems mm32at driver.
27 Author: Perry J. Piplani <perry.j.piplani@nasa.gov>
28 Updated: Fri Jun 4 09:13:24 CDT 2004
31 This driver is for the Diamond Systems MM-32-AT board
32 http://www.diamondsystems.com/products/diamondmm32at It is being used
33 on serveral projects inside NASA, without problems so far. For analog
34 input commands, TRIG_EXT is not yet supported at all..
36 Configuration Options:
37 comedi_config /dev/comedi0 dmm32at baseaddr,irq
40 #include <linux/interrupt.h>
41 #include "../comedidev.h"
42 #include <linux/ioport.h>
44 /* Board register addresses */
46 #define DMM32AT_MEMSIZE 0x10
48 #define DMM32AT_CONV 0x00
49 #define DMM32AT_AILSB 0x00
50 #define DMM32AT_AUXDOUT 0x01
51 #define DMM32AT_AIMSB 0x01
52 #define DMM32AT_AILOW 0x02
53 #define DMM32AT_AIHIGH 0x03
55 #define DMM32AT_DACLSB 0x04
56 #define DMM32AT_DACSTAT 0x04
57 #define DMM32AT_DACMSB 0x05
59 #define DMM32AT_FIFOCNTRL 0x07
60 #define DMM32AT_FIFOSTAT 0x07
62 #define DMM32AT_CNTRL 0x08
63 #define DMM32AT_AISTAT 0x08
65 #define DMM32AT_INTCLOCK 0x09
67 #define DMM32AT_CNTRDIO 0x0a
69 #define DMM32AT_AICONF 0x0b
70 #define DMM32AT_AIRBACK 0x0b
72 #define DMM32AT_CLK1 0x0d
73 #define DMM32AT_CLK2 0x0e
74 #define DMM32AT_CLKCT 0x0f
76 #define DMM32AT_DIOA 0x0c
77 #define DMM32AT_DIOB 0x0d
78 #define DMM32AT_DIOC 0x0e
79 #define DMM32AT_DIOCONF 0x0f
81 #define dmm_inb(cdev, reg) inb((cdev->iobase)+reg)
82 #define dmm_outb(cdev, reg, valu) outb(valu, (cdev->iobase)+reg)
84 /* Board register values. */
86 /* DMM32AT_DACSTAT 0x04 */
87 #define DMM32AT_DACBUSY 0x80
89 /* DMM32AT_FIFOCNTRL 0x07 */
90 #define DMM32AT_FIFORESET 0x02
91 #define DMM32AT_SCANENABLE 0x04
93 /* DMM32AT_CNTRL 0x08 */
94 #define DMM32AT_RESET 0x20
95 #define DMM32AT_INTRESET 0x08
96 #define DMM32AT_CLKACC 0x00
97 #define DMM32AT_DIOACC 0x01
99 /* DMM32AT_AISTAT 0x08 */
100 #define DMM32AT_STATUS 0x80
102 /* DMM32AT_INTCLOCK 0x09 */
103 #define DMM32AT_ADINT 0x80
104 #define DMM32AT_CLKSEL 0x03
106 /* DMM32AT_CNTRDIO 0x0a */
107 #define DMM32AT_FREQ12 0x80
109 /* DMM32AT_AICONF 0x0b */
110 #define DMM32AT_RANGE_U10 0x0c
111 #define DMM32AT_RANGE_U5 0x0d
112 #define DMM32AT_RANGE_B10 0x08
113 #define DMM32AT_RANGE_B5 0x00
114 #define DMM32AT_SCINT_20 0x00
115 #define DMM32AT_SCINT_15 0x10
116 #define DMM32AT_SCINT_10 0x20
117 #define DMM32AT_SCINT_5 0x30
119 /* DMM32AT_CLKCT 0x0f */
120 #define DMM32AT_CLKCT1 0x56 /* mode3 counter 1 - write low byte only */
121 #define DMM32AT_CLKCT2 0xb6 /* mode3 counter 2 - write high and low byte */
123 /* DMM32AT_DIOCONF 0x0f */
124 #define DMM32AT_DIENABLE 0x80
125 #define DMM32AT_DIRA 0x10
126 #define DMM32AT_DIRB 0x02
127 #define DMM32AT_DIRCL 0x01
128 #define DMM32AT_DIRCH 0x08
130 /* board AI ranges in comedi structure */
131 static const struct comedi_lrange dmm32at_airanges
= {
141 /* register values for above ranges */
142 static const unsigned char dmm32at_rangebits
[] = {
149 /* only one of these ranges is valid, as set by a jumper on the
150 * board. The application should only use the range set by the jumper
152 static const struct comedi_lrange dmm32at_aoranges
= {
163 * Board descriptions for two imaginary boards. Describing the
164 * boards in this way is optional, and completely driver-dependent.
165 * Some drivers use arrays such as this, other do not.
167 struct dmm32at_board
{
171 const struct comedi_lrange
*ai_ranges
;
174 const struct comedi_lrange
*ao_ranges
;
178 static const struct dmm32at_board dmm32at_boards
[] = {
183 .ai_ranges
= &dmm32at_airanges
,
186 .ao_ranges
= &dmm32at_aoranges
,
193 * Useful for shorthand access to the particular board structure
195 #define thisboard ((const struct dmm32at_board *)dev->board_ptr)
197 /* this structure is for data unique to this hardware driver. If
198 * several hardware drivers keep similar information in this structure,
199 * feel free to suggest moving the variable to the struct comedi_device struct.
201 struct dmm32at_private
{
205 unsigned int ai_scans_left
;
207 /* Used for AO readback */
208 unsigned int ao_readback
[4];
209 unsigned char dio_config
;
214 * most drivers define the following macro to make it easy to
215 * access the private structure.
217 #define devpriv ((struct dmm32at_private *)dev->private)
220 * The struct comedi_driver structure tells the Comedi core module
221 * which functions to call to configure/deconfigure (attach/detach)
222 * the board, and also about the kernel module that contains
225 static int dmm32at_attach(struct comedi_device
*dev
,
226 struct comedi_devconfig
*it
);
227 static int dmm32at_detach(struct comedi_device
*dev
);
228 static struct comedi_driver driver_dmm32at
= {
229 .driver_name
= "dmm32at",
230 .module
= THIS_MODULE
,
231 .attach
= dmm32at_attach
,
232 .detach
= dmm32at_detach
,
233 /* It is not necessary to implement the following members if you are
234 * writing a driver for a ISA PnP or PCI card */
235 /* Most drivers will support multiple types of boards by
236 * having an array of board structures. These were defined
237 * in dmm32at_boards[] above. Note that the element 'name'
238 * was first in the structure -- Comedi uses this fact to
239 * extract the name of the board without knowing any details
240 * about the structure except for its length.
241 * When a device is attached (by comedi_config), the name
242 * of the device is given to Comedi, and Comedi tries to
243 * match it by going through the list of board names. If
244 * there is a match, the address of the pointer is put
245 * into dev->board_ptr and driver->attach() is called.
247 * Note that these are not necessary if you can determine
248 * the type of board in software. ISA PnP, PCI, and PCMCIA
249 * devices are such boards.
251 .board_name
= &dmm32at_boards
[0].name
,
252 .offset
= sizeof(struct dmm32at_board
),
253 .num_names
= ARRAY_SIZE(dmm32at_boards
),
256 /* prototypes for driver functions below */
257 static int dmm32at_ai_rinsn(struct comedi_device
*dev
,
258 struct comedi_subdevice
*s
,
259 struct comedi_insn
*insn
, unsigned int *data
);
260 static int dmm32at_ao_winsn(struct comedi_device
*dev
,
261 struct comedi_subdevice
*s
,
262 struct comedi_insn
*insn
, unsigned int *data
);
263 static int dmm32at_ao_rinsn(struct comedi_device
*dev
,
264 struct comedi_subdevice
*s
,
265 struct comedi_insn
*insn
, unsigned int *data
);
266 static int dmm32at_dio_insn_bits(struct comedi_device
*dev
,
267 struct comedi_subdevice
*s
,
268 struct comedi_insn
*insn
, unsigned int *data
);
269 static int dmm32at_dio_insn_config(struct comedi_device
*dev
,
270 struct comedi_subdevice
*s
,
271 struct comedi_insn
*insn
,
273 static int dmm32at_ai_cmdtest(struct comedi_device
*dev
,
274 struct comedi_subdevice
*s
,
275 struct comedi_cmd
*cmd
);
276 static int dmm32at_ai_cmd(struct comedi_device
*dev
,
277 struct comedi_subdevice
*s
);
278 static int dmm32at_ai_cancel(struct comedi_device
*dev
,
279 struct comedi_subdevice
*s
);
280 static int dmm32at_ns_to_timer(unsigned int *ns
, int round
);
281 static irqreturn_t
dmm32at_isr(int irq
, void *d
);
282 void dmm32at_setaitimer(struct comedi_device
*dev
, unsigned int nansec
);
285 * Attach is called by the Comedi core to configure the driver
286 * for a particular board. If you specified a board_name array
287 * in the driver structure, dev->board_ptr contains that
290 static int dmm32at_attach(struct comedi_device
*dev
,
291 struct comedi_devconfig
*it
)
294 struct comedi_subdevice
*s
;
295 unsigned char aihi
, ailo
, fifostat
, aistat
, intstat
, airback
;
296 unsigned long iobase
;
299 iobase
= it
->options
[0];
300 irq
= it
->options
[1];
302 printk(KERN_INFO
"comedi%d: dmm32at: attaching\n", dev
->minor
);
303 printk(KERN_DEBUG
"dmm32at: probing at address 0x%04lx, irq %u\n",
306 /* register address space */
307 if (!request_region(iobase
, DMM32AT_MEMSIZE
, thisboard
->name
)) {
308 printk(KERN_ERR
"comedi%d: dmm32at: I/O port conflict\n",
312 dev
->iobase
= iobase
;
314 /* the following just makes sure the board is there and gets
315 it to a known state */
317 /* reset the board */
318 dmm_outb(dev
, DMM32AT_CNTRL
, DMM32AT_RESET
);
320 /* allow a millisecond to reset */
323 /* zero scan and fifo control */
324 dmm_outb(dev
, DMM32AT_FIFOCNTRL
, 0x0);
326 /* zero interrupt and clock control */
327 dmm_outb(dev
, DMM32AT_INTCLOCK
, 0x0);
329 /* write a test channel range, the high 3 bits should drop */
330 dmm_outb(dev
, DMM32AT_AILOW
, 0x80);
331 dmm_outb(dev
, DMM32AT_AIHIGH
, 0xff);
333 /* set the range at 10v unipolar */
334 dmm_outb(dev
, DMM32AT_AICONF
, DMM32AT_RANGE_U10
);
336 /* should take 10 us to settle, here's a hundred */
339 /* read back the values */
340 ailo
= dmm_inb(dev
, DMM32AT_AILOW
);
341 aihi
= dmm_inb(dev
, DMM32AT_AIHIGH
);
342 fifostat
= dmm_inb(dev
, DMM32AT_FIFOSTAT
);
343 aistat
= dmm_inb(dev
, DMM32AT_AISTAT
);
344 intstat
= dmm_inb(dev
, DMM32AT_INTCLOCK
);
345 airback
= dmm_inb(dev
, DMM32AT_AIRBACK
);
347 printk(KERN_DEBUG
"dmm32at: lo=0x%02x hi=0x%02x fifostat=0x%02x\n",
348 ailo
, aihi
, fifostat
);
350 "dmm32at: aistat=0x%02x intstat=0x%02x airback=0x%02x\n",
351 aistat
, intstat
, airback
);
353 if ((ailo
!= 0x00) || (aihi
!= 0x1f) || (fifostat
!= 0x80) ||
354 (aistat
!= 0x60 || (intstat
!= 0x00) || airback
!= 0x0c)) {
355 printk(KERN_ERR
"dmmat32: board detection failed\n");
359 /* board is there, register interrupt */
361 ret
= request_irq(irq
, dmm32at_isr
, 0, thisboard
->name
, dev
);
363 printk(KERN_ERR
"dmm32at: irq conflict\n");
370 * If you can probe the device to determine what device in a series
371 * it is, this is the place to do it. Otherwise, dev->board_ptr
372 * should already be initialized.
374 /* dev->board_ptr = dmm32at_probe(dev); */
377 * Initialize dev->board_name. Note that we can use the "thisboard"
378 * macro now, since we just initialized it in the last line.
380 dev
->board_name
= thisboard
->name
;
383 * Allocate the private structure area. alloc_private() is a
384 * convenient macro defined in comedidev.h.
386 if (alloc_private(dev
, sizeof(struct dmm32at_private
)) < 0)
390 * Allocate the subdevice structures. alloc_subdevice() is a
391 * convenient macro defined in comedidev.h.
393 if (alloc_subdevices(dev
, 3) < 0)
396 s
= dev
->subdevices
+ 0;
397 dev
->read_subdev
= s
;
398 /* analog input subdevice */
399 s
->type
= COMEDI_SUBD_AI
;
400 /* we support single-ended (ground) and differential */
401 s
->subdev_flags
= SDF_READABLE
| SDF_GROUND
| SDF_DIFF
| SDF_CMD_READ
;
402 s
->n_chan
= thisboard
->ai_chans
;
403 s
->maxdata
= (1 << thisboard
->ai_bits
) - 1;
404 s
->range_table
= thisboard
->ai_ranges
;
405 s
->len_chanlist
= 32; /* This is the maximum chanlist length that
406 the board can handle */
407 s
->insn_read
= dmm32at_ai_rinsn
;
408 s
->do_cmd
= dmm32at_ai_cmd
;
409 s
->do_cmdtest
= dmm32at_ai_cmdtest
;
410 s
->cancel
= dmm32at_ai_cancel
;
412 s
= dev
->subdevices
+ 1;
413 /* analog output subdevice */
414 s
->type
= COMEDI_SUBD_AO
;
415 s
->subdev_flags
= SDF_WRITABLE
;
416 s
->n_chan
= thisboard
->ao_chans
;
417 s
->maxdata
= (1 << thisboard
->ao_bits
) - 1;
418 s
->range_table
= thisboard
->ao_ranges
;
419 s
->insn_write
= dmm32at_ao_winsn
;
420 s
->insn_read
= dmm32at_ao_rinsn
;
422 s
= dev
->subdevices
+ 2;
423 /* digital i/o subdevice */
424 if (thisboard
->have_dio
) {
426 /* get access to the DIO regs */
427 dmm_outb(dev
, DMM32AT_CNTRL
, DMM32AT_DIOACC
);
428 /* set the DIO's to the defualt input setting */
429 devpriv
->dio_config
= DMM32AT_DIRA
| DMM32AT_DIRB
|
430 DMM32AT_DIRCL
| DMM32AT_DIRCH
| DMM32AT_DIENABLE
;
431 dmm_outb(dev
, DMM32AT_DIOCONF
, devpriv
->dio_config
);
433 /* set up the subdevice */
434 s
->type
= COMEDI_SUBD_DIO
;
435 s
->subdev_flags
= SDF_READABLE
| SDF_WRITABLE
;
436 s
->n_chan
= thisboard
->dio_chans
;
439 s
->range_table
= &range_digital
;
440 s
->insn_bits
= dmm32at_dio_insn_bits
;
441 s
->insn_config
= dmm32at_dio_insn_config
;
443 s
->type
= COMEDI_SUBD_UNUSED
;
447 printk(KERN_INFO
"comedi%d: dmm32at: attached\n", dev
->minor
);
454 * _detach is called to deconfigure a device. It should deallocate
456 * This function is also called when _attach() fails, so it should be
457 * careful not to release resources that were not necessarily
458 * allocated by _attach(). dev->private and dev->subdevices are
459 * deallocated automatically by the core.
461 static int dmm32at_detach(struct comedi_device
*dev
)
463 printk(KERN_INFO
"comedi%d: dmm32at: remove\n", dev
->minor
);
465 free_irq(dev
->irq
, dev
);
467 release_region(dev
->iobase
, DMM32AT_MEMSIZE
);
473 * "instructions" read/write data in "one-shot" or "software-triggered"
477 static int dmm32at_ai_rinsn(struct comedi_device
*dev
,
478 struct comedi_subdevice
*s
,
479 struct comedi_insn
*insn
, unsigned int *data
)
483 unsigned char status
;
484 unsigned short msb
, lsb
;
488 /* get the channel and range number */
490 chan
= CR_CHAN(insn
->chanspec
) & (s
->n_chan
- 1);
491 range
= CR_RANGE(insn
->chanspec
);
493 /* printk("channel=0x%02x, range=%d\n",chan,range); */
495 /* zero scan and fifo control and reset fifo */
496 dmm_outb(dev
, DMM32AT_FIFOCNTRL
, DMM32AT_FIFORESET
);
498 /* write the ai channel range regs */
499 dmm_outb(dev
, DMM32AT_AILOW
, chan
);
500 dmm_outb(dev
, DMM32AT_AIHIGH
, chan
);
501 /* set the range bits */
502 dmm_outb(dev
, DMM32AT_AICONF
, dmm32at_rangebits
[range
]);
504 /* wait for circuit to settle */
505 for (i
= 0; i
< 40000; i
++) {
506 status
= dmm_inb(dev
, DMM32AT_AIRBACK
);
507 if ((status
& DMM32AT_STATUS
) == 0)
511 printk(KERN_WARNING
"dmm32at: timeout\n");
515 /* convert n samples */
516 for (n
= 0; n
< insn
->n
; n
++) {
517 /* trigger conversion */
518 dmm_outb(dev
, DMM32AT_CONV
, 0xff);
519 /* wait for conversion to end */
520 for (i
= 0; i
< 40000; i
++) {
521 status
= dmm_inb(dev
, DMM32AT_AISTAT
);
522 if ((status
& DMM32AT_STATUS
) == 0)
526 printk(KERN_WARNING
"dmm32at: timeout\n");
531 lsb
= dmm_inb(dev
, DMM32AT_AILSB
);
532 msb
= dmm_inb(dev
, DMM32AT_AIMSB
);
534 /* invert sign bit to make range unsigned, this is an
535 idiosyncrasy of the diamond board, it return
536 conversions as a signed value, i.e. -32768 to
537 32767, flipping the bit and interpreting it as
538 signed gives you a range of 0 to 65535 which is
540 d
= ((msb
^ 0x0080) << 8) + lsb
;
545 /* return the number of samples read/written */
549 static int dmm32at_ai_cmdtest(struct comedi_device
*dev
,
550 struct comedi_subdevice
*s
,
551 struct comedi_cmd
*cmd
)
555 int start_chan
, gain
, i
;
557 /* printk("dmmat32 in command test\n"); */
559 /* cmdtest tests a particular command to see if it is valid.
560 * Using the cmdtest ioctl, a user can create a valid cmd
561 * and then have it executes by the cmd ioctl.
563 * cmdtest returns 1,2,3,4 or 0, depending on which tests
564 * the command passes. */
566 /* step 1: make sure trigger sources are trivially valid */
568 tmp
= cmd
->start_src
;
569 cmd
->start_src
&= TRIG_NOW
;
570 if (!cmd
->start_src
|| tmp
!= cmd
->start_src
)
573 tmp
= cmd
->scan_begin_src
;
574 cmd
->scan_begin_src
&= TRIG_TIMER
/*| TRIG_EXT */ ;
575 if (!cmd
->scan_begin_src
|| tmp
!= cmd
->scan_begin_src
)
578 tmp
= cmd
->convert_src
;
579 cmd
->convert_src
&= TRIG_TIMER
/*| TRIG_EXT */ ;
580 if (!cmd
->convert_src
|| tmp
!= cmd
->convert_src
)
583 tmp
= cmd
->scan_end_src
;
584 cmd
->scan_end_src
&= TRIG_COUNT
;
585 if (!cmd
->scan_end_src
|| tmp
!= cmd
->scan_end_src
)
589 cmd
->stop_src
&= TRIG_COUNT
| TRIG_NONE
;
590 if (!cmd
->stop_src
|| tmp
!= cmd
->stop_src
)
596 /* step 2: make sure trigger sources are unique and mutually
599 /* note that mutual compatibility is not an issue here */
600 if (cmd
->scan_begin_src
!= TRIG_TIMER
&&
601 cmd
->scan_begin_src
!= TRIG_EXT
)
603 if (cmd
->convert_src
!= TRIG_TIMER
&& cmd
->convert_src
!= TRIG_EXT
)
605 if (cmd
->stop_src
!= TRIG_COUNT
&& cmd
->stop_src
!= TRIG_NONE
)
611 /* step 3: make sure arguments are trivially compatible */
613 if (cmd
->start_arg
!= 0) {
617 #define MAX_SCAN_SPEED 1000000 /* in nanoseconds */
618 #define MIN_SCAN_SPEED 1000000000 /* in nanoseconds */
620 if (cmd
->scan_begin_src
== TRIG_TIMER
) {
621 if (cmd
->scan_begin_arg
< MAX_SCAN_SPEED
) {
622 cmd
->scan_begin_arg
= MAX_SCAN_SPEED
;
625 if (cmd
->scan_begin_arg
> MIN_SCAN_SPEED
) {
626 cmd
->scan_begin_arg
= MIN_SCAN_SPEED
;
630 /* external trigger */
631 /* should be level/edge, hi/lo specification here */
632 /* should specify multiple external triggers */
633 if (cmd
->scan_begin_arg
> 9) {
634 cmd
->scan_begin_arg
= 9;
638 if (cmd
->convert_src
== TRIG_TIMER
) {
639 if (cmd
->convert_arg
>= 17500)
640 cmd
->convert_arg
= 20000;
641 else if (cmd
->convert_arg
>= 12500)
642 cmd
->convert_arg
= 15000;
643 else if (cmd
->convert_arg
>= 7500)
644 cmd
->convert_arg
= 10000;
646 cmd
->convert_arg
= 5000;
649 /* external trigger */
651 if (cmd
->convert_arg
> 9) {
652 cmd
->convert_arg
= 9;
657 if (cmd
->scan_end_arg
!= cmd
->chanlist_len
) {
658 cmd
->scan_end_arg
= cmd
->chanlist_len
;
661 if (cmd
->stop_src
== TRIG_COUNT
) {
662 if (cmd
->stop_arg
> 0xfffffff0) {
663 cmd
->stop_arg
= 0xfffffff0;
666 if (cmd
->stop_arg
== 0) {
672 if (cmd
->stop_arg
!= 0) {
681 /* step 4: fix up any arguments */
683 if (cmd
->scan_begin_src
== TRIG_TIMER
) {
684 tmp
= cmd
->scan_begin_arg
;
685 dmm32at_ns_to_timer(&cmd
->scan_begin_arg
,
686 cmd
->flags
& TRIG_ROUND_MASK
);
687 if (tmp
!= cmd
->scan_begin_arg
)
690 if (cmd
->convert_src
== TRIG_TIMER
) {
691 tmp
= cmd
->convert_arg
;
692 dmm32at_ns_to_timer(&cmd
->convert_arg
,
693 cmd
->flags
& TRIG_ROUND_MASK
);
694 if (tmp
!= cmd
->convert_arg
)
696 if (cmd
->scan_begin_src
== TRIG_TIMER
&&
697 cmd
->scan_begin_arg
<
698 cmd
->convert_arg
* cmd
->scan_end_arg
) {
699 cmd
->scan_begin_arg
=
700 cmd
->convert_arg
* cmd
->scan_end_arg
;
708 /* step 5 check the channel list, the channel list for this
709 board must be consecutive and gains must be the same */
712 gain
= CR_RANGE(cmd
->chanlist
[0]);
713 start_chan
= CR_CHAN(cmd
->chanlist
[0]);
714 for (i
= 1; i
< cmd
->chanlist_len
; i
++) {
715 if (CR_CHAN(cmd
->chanlist
[i
]) !=
716 (start_chan
+ i
) % s
->n_chan
) {
718 "entries in chanlist must be consecutive channels, counting upwards\n");
721 if (CR_RANGE(cmd
->chanlist
[i
]) != gain
) {
723 "entries in chanlist must all have the same gain\n");
735 static int dmm32at_ai_cmd(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
737 struct comedi_cmd
*cmd
= &s
->async
->cmd
;
739 unsigned char chanlo
, chanhi
, status
;
744 /* get the channel list and range */
745 chanlo
= CR_CHAN(cmd
->chanlist
[0]) & (s
->n_chan
- 1);
746 chanhi
= chanlo
+ cmd
->chanlist_len
- 1;
747 if (chanhi
>= s
->n_chan
)
749 range
= CR_RANGE(cmd
->chanlist
[0]);
752 dmm_outb(dev
, DMM32AT_FIFOCNTRL
, DMM32AT_FIFORESET
);
754 /* set scan enable */
755 dmm_outb(dev
, DMM32AT_FIFOCNTRL
, DMM32AT_SCANENABLE
);
757 /* write the ai channel range regs */
758 dmm_outb(dev
, DMM32AT_AILOW
, chanlo
);
759 dmm_outb(dev
, DMM32AT_AIHIGH
, chanhi
);
761 /* set the range bits */
762 dmm_outb(dev
, DMM32AT_AICONF
, dmm32at_rangebits
[range
]);
764 /* reset the interrupt just in case */
765 dmm_outb(dev
, DMM32AT_CNTRL
, DMM32AT_INTRESET
);
767 if (cmd
->stop_src
== TRIG_COUNT
)
768 devpriv
->ai_scans_left
= cmd
->stop_arg
;
769 else { /* TRIG_NONE */
770 devpriv
->ai_scans_left
= 0xffffffff; /* indicates TRIG_NONE to
774 /* wait for circuit to settle */
775 for (i
= 0; i
< 40000; i
++) {
776 status
= dmm_inb(dev
, DMM32AT_AIRBACK
);
777 if ((status
& DMM32AT_STATUS
) == 0)
781 printk(KERN_WARNING
"dmm32at: timeout\n");
785 if (devpriv
->ai_scans_left
> 1) {
786 /* start the clock and enable the interrupts */
787 dmm32at_setaitimer(dev
, cmd
->scan_begin_arg
);
789 /* start the interrups and initiate a single scan */
790 dmm_outb(dev
, DMM32AT_INTCLOCK
, DMM32AT_ADINT
);
791 dmm_outb(dev
, DMM32AT_CONV
, 0xff);
794 /* printk("dmmat32 in command\n"); */
796 /* for(i=0;i<cmd->chanlist_len;i++) */
797 /* comedi_buf_put(s->async,i*100); */
799 /* s->async->events |= COMEDI_CB_EOA; */
800 /* comedi_event(dev, s); */
806 static int dmm32at_ai_cancel(struct comedi_device
*dev
,
807 struct comedi_subdevice
*s
)
809 devpriv
->ai_scans_left
= 1;
813 static irqreturn_t
dmm32at_isr(int irq
, void *d
)
815 unsigned char intstat
;
817 unsigned short msb
, lsb
;
819 struct comedi_device
*dev
= d
;
821 if (!dev
->attached
) {
822 comedi_error(dev
, "spurious interrupt");
826 intstat
= dmm_inb(dev
, DMM32AT_INTCLOCK
);
828 if (intstat
& DMM32AT_ADINT
) {
829 struct comedi_subdevice
*s
= dev
->read_subdev
;
830 struct comedi_cmd
*cmd
= &s
->async
->cmd
;
832 for (i
= 0; i
< cmd
->chanlist_len
; i
++) {
834 lsb
= dmm_inb(dev
, DMM32AT_AILSB
);
835 msb
= dmm_inb(dev
, DMM32AT_AIMSB
);
837 /* invert sign bit to make range unsigned */
838 samp
= ((msb
^ 0x0080) << 8) + lsb
;
839 comedi_buf_put(s
->async
, samp
);
842 if (devpriv
->ai_scans_left
!= 0xffffffff) { /* TRIG_COUNT */
843 devpriv
->ai_scans_left
--;
844 if (devpriv
->ai_scans_left
== 0) {
845 /* disable further interrupts and clocks */
846 dmm_outb(dev
, DMM32AT_INTCLOCK
, 0x0);
847 /* set the buffer to be flushed with an EOF */
848 s
->async
->events
|= COMEDI_CB_EOA
;
852 /* flush the buffer */
853 comedi_event(dev
, s
);
856 /* reset the interrupt */
857 dmm_outb(dev
, DMM32AT_CNTRL
, DMM32AT_INTRESET
);
861 /* This function doesn't require a particular form, this is just
862 * what happens to be used in some of the drivers. It should
863 * convert ns nanoseconds to a counter value suitable for programming
864 * the device. Also, it should adjust ns so that it cooresponds to
865 * the actual time that the device will use. */
866 static int dmm32at_ns_to_timer(unsigned int *ns
, int round
)
869 /* if your timing is done through two cascaded timers, the
870 * i8253_cascade_ns_to_timer() function in 8253.h can be
871 * very helpful. There are also i8254_load() and i8254_mm_load()
872 * which can be used to load values into the ubiquitous 8254 counters
878 static int dmm32at_ao_winsn(struct comedi_device
*dev
,
879 struct comedi_subdevice
*s
,
880 struct comedi_insn
*insn
, unsigned int *data
)
883 int chan
= CR_CHAN(insn
->chanspec
);
884 unsigned char hi
, lo
, status
;
886 /* Writing a list of values to an AO channel is probably not
887 * very useful, but that's how the interface is defined. */
888 for (i
= 0; i
< insn
->n
; i
++) {
890 devpriv
->ao_readback
[chan
] = data
[i
];
892 /* get the low byte */
893 lo
= data
[i
] & 0x00ff;
894 /* high byte also contains channel number */
895 hi
= (data
[i
] >> 8) + chan
* (1 << 6);
896 /* printk("writing 0x%02x 0x%02x\n",hi,lo); */
897 /* write the low and high values to the board */
898 dmm_outb(dev
, DMM32AT_DACLSB
, lo
);
899 dmm_outb(dev
, DMM32AT_DACMSB
, hi
);
901 /* wait for circuit to settle */
902 for (i
= 0; i
< 40000; i
++) {
903 status
= dmm_inb(dev
, DMM32AT_DACSTAT
);
904 if ((status
& DMM32AT_DACBUSY
) == 0)
908 printk(KERN_WARNING
"dmm32at: timeout\n");
911 /* dummy read to update trigger the output */
912 status
= dmm_inb(dev
, DMM32AT_DACMSB
);
916 /* return the number of samples read/written */
920 /* AO subdevices should have a read insn as well as a write insn.
921 * Usually this means copying a value stored in devpriv. */
922 static int dmm32at_ao_rinsn(struct comedi_device
*dev
,
923 struct comedi_subdevice
*s
,
924 struct comedi_insn
*insn
, unsigned int *data
)
927 int chan
= CR_CHAN(insn
->chanspec
);
929 for (i
= 0; i
< insn
->n
; i
++)
930 data
[i
] = devpriv
->ao_readback
[chan
];
935 /* DIO devices are slightly special. Although it is possible to
936 * implement the insn_read/insn_write interface, it is much more
937 * useful to applications if you implement the insn_bits interface.
938 * This allows packed reading/writing of the DIO channels. The
939 * comedi core can convert between insn_bits and insn_read/write */
940 static int dmm32at_dio_insn_bits(struct comedi_device
*dev
,
941 struct comedi_subdevice
*s
,
942 struct comedi_insn
*insn
, unsigned int *data
)
944 unsigned char diobits
;
949 /* The insn data is a mask in data[0] and the new data
950 * in data[1], each channel cooresponding to a bit. */
952 s
->state
&= ~data
[0];
953 s
->state
|= data
[0] & data
[1];
954 /* Write out the new digital output lines */
955 /* outw(s->state,dev->iobase + DMM32AT_DIO); */
958 /* get access to the DIO regs */
959 dmm_outb(dev
, DMM32AT_CNTRL
, DMM32AT_DIOACC
);
961 /* if either part of dio is set for output */
962 if (((devpriv
->dio_config
& DMM32AT_DIRCL
) == 0) ||
963 ((devpriv
->dio_config
& DMM32AT_DIRCH
) == 0)) {
964 diobits
= (s
->state
& 0x00ff0000) >> 16;
965 dmm_outb(dev
, DMM32AT_DIOC
, diobits
);
967 if ((devpriv
->dio_config
& DMM32AT_DIRB
) == 0) {
968 diobits
= (s
->state
& 0x0000ff00) >> 8;
969 dmm_outb(dev
, DMM32AT_DIOB
, diobits
);
971 if ((devpriv
->dio_config
& DMM32AT_DIRA
) == 0) {
972 diobits
= (s
->state
& 0x000000ff);
973 dmm_outb(dev
, DMM32AT_DIOA
, diobits
);
976 /* now read the state back in */
977 s
->state
= dmm_inb(dev
, DMM32AT_DIOC
);
979 s
->state
|= dmm_inb(dev
, DMM32AT_DIOB
);
981 s
->state
|= dmm_inb(dev
, DMM32AT_DIOA
);
984 /* on return, data[1] contains the value of the digital
985 * input and output lines. */
986 /* data[1]=inw(dev->iobase + DMM32AT_DIO); */
987 /* or we could just return the software copy of the output values if
988 * it was a purely digital output subdevice */
989 /* data[1]=s->state; */
994 static int dmm32at_dio_insn_config(struct comedi_device
*dev
,
995 struct comedi_subdevice
*s
,
996 struct comedi_insn
*insn
, unsigned int *data
)
998 unsigned char chanbit
;
999 int chan
= CR_CHAN(insn
->chanspec
);
1005 chanbit
= DMM32AT_DIRA
;
1007 chanbit
= DMM32AT_DIRB
;
1009 chanbit
= DMM32AT_DIRCL
;
1011 chanbit
= DMM32AT_DIRCH
;
1013 /* The input or output configuration of each digital line is
1014 * configured by a special insn_config instruction. chanspec
1015 * contains the channel to be changed, and data[0] contains the
1016 * value COMEDI_INPUT or COMEDI_OUTPUT. */
1018 /* if output clear the bit, otherwise set it */
1019 if (data
[0] == COMEDI_OUTPUT
)
1020 devpriv
->dio_config
&= ~chanbit
;
1022 devpriv
->dio_config
|= chanbit
;
1023 /* get access to the DIO regs */
1024 dmm_outb(dev
, DMM32AT_CNTRL
, DMM32AT_DIOACC
);
1025 /* set the DIO's to the new configuration setting */
1026 dmm_outb(dev
, DMM32AT_DIOCONF
, devpriv
->dio_config
);
1031 void dmm32at_setaitimer(struct comedi_device
*dev
, unsigned int nansec
)
1033 unsigned char lo1
, lo2
, hi2
;
1034 unsigned short both2
;
1036 /* based on 10mhz clock */
1038 both2
= nansec
/ 20000;
1039 hi2
= (both2
& 0xff00) >> 8;
1040 lo2
= both2
& 0x00ff;
1042 /* set the counter frequency to 10mhz */
1043 dmm_outb(dev
, DMM32AT_CNTRDIO
, 0);
1045 /* get access to the clock regs */
1046 dmm_outb(dev
, DMM32AT_CNTRL
, DMM32AT_CLKACC
);
1048 /* write the counter 1 control word and low byte to counter */
1049 dmm_outb(dev
, DMM32AT_CLKCT
, DMM32AT_CLKCT1
);
1050 dmm_outb(dev
, DMM32AT_CLK1
, lo1
);
1052 /* write the counter 2 control word and low byte then to counter */
1053 dmm_outb(dev
, DMM32AT_CLKCT
, DMM32AT_CLKCT2
);
1054 dmm_outb(dev
, DMM32AT_CLK2
, lo2
);
1055 dmm_outb(dev
, DMM32AT_CLK2
, hi2
);
1057 /* enable the ai conversion interrupt and the clock to start scans */
1058 dmm_outb(dev
, DMM32AT_INTCLOCK
, DMM32AT_ADINT
| DMM32AT_CLKSEL
);
1063 * A convenient macro that defines init_module() and cleanup_module(),
1066 static int __init
driver_dmm32at_init_module(void)
1068 return comedi_driver_register(&driver_dmm32at
);
1071 static void __exit
driver_dmm32at_cleanup_module(void)
1073 comedi_driver_unregister(&driver_dmm32at
);
1076 module_init(driver_dmm32at_init_module
);
1077 module_exit(driver_dmm32at_cleanup_module
);
1079 MODULE_AUTHOR("Comedi http://www.comedi.org");
1080 MODULE_DESCRIPTION("Comedi low-level driver");
1081 MODULE_LICENSE("GPL");