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
41 * The previous block comment is used to automatically generate
42 * documentation in Comedi and Comedilib. The fields:
44 * Driver: the name of the driver
45 * Description: a short phrase describing the driver. Don't list boards.
46 * Devices: a full list of the boards that attempt to be supported by
47 * the driver. Format is "(manufacturer) board name [comedi name]",
48 * where comedi_name is the name that is used to configure the board.
49 * See the comment near board_name: in the struct comedi_driver structure
50 * below. If (manufacturer) or [comedi name] is missing, the previous
53 * Updated: date when the _documentation_ was last updated. Use 'date -R'
54 * to get a value for this.
55 * Status: a one-word description of the status. Valid values are:
56 * works - driver works correctly on most boards supported, and
58 * unknown - unknown. Usually put there by ds.
59 * experimental - may not work in any particular release. Author
60 * probably wants assistance testing it.
61 * bitrotten - driver has not been update in a long time, probably
62 * doesn't work, and probably is missing support for significant
63 * Comedi interface features.
64 * untested - author probably wrote it "blind", and is believed to
65 * work, but no confirmation.
67 * These headers should be followed by a blank line, and any comments
68 * you wish to say about the driver. The comment area is the place
69 * to put any known bugs, limitations, unsupported features, supported
70 * command triggers, whether or not commands are supported on particular
73 * Somewhere in the comment should be information about configuration
74 * options that are used with comedi_config.
77 #include "../comedidev.h"
78 #include <linux/ioport.h>
80 /* Board register addresses */
82 #define DMM32AT_MEMSIZE 0x10
84 #define DMM32AT_CONV 0x00
85 #define DMM32AT_AILSB 0x00
86 #define DMM32AT_AUXDOUT 0x01
87 #define DMM32AT_AIMSB 0x01
88 #define DMM32AT_AILOW 0x02
89 #define DMM32AT_AIHIGH 0x03
91 #define DMM32AT_DACLSB 0x04
92 #define DMM32AT_DACSTAT 0x04
93 #define DMM32AT_DACMSB 0x05
95 #define DMM32AT_FIFOCNTRL 0x07
96 #define DMM32AT_FIFOSTAT 0x07
98 #define DMM32AT_CNTRL 0x08
99 #define DMM32AT_AISTAT 0x08
101 #define DMM32AT_INTCLOCK 0x09
103 #define DMM32AT_CNTRDIO 0x0a
105 #define DMM32AT_AICONF 0x0b
106 #define DMM32AT_AIRBACK 0x0b
108 #define DMM32AT_CLK1 0x0d
109 #define DMM32AT_CLK2 0x0e
110 #define DMM32AT_CLKCT 0x0f
112 #define DMM32AT_DIOA 0x0c
113 #define DMM32AT_DIOB 0x0d
114 #define DMM32AT_DIOC 0x0e
115 #define DMM32AT_DIOCONF 0x0f
117 #define dmm_inb(cdev,reg) inb((cdev->iobase)+reg)
118 #define dmm_outb(cdev,reg,valu) outb(valu,(cdev->iobase)+reg)
120 /* Board register values. */
122 /* DMM32AT_DACSTAT 0x04 */
123 #define DMM32AT_DACBUSY 0x80
125 /* DMM32AT_FIFOCNTRL 0x07 */
126 #define DMM32AT_FIFORESET 0x02
127 #define DMM32AT_SCANENABLE 0x04
129 /* DMM32AT_CNTRL 0x08 */
130 #define DMM32AT_RESET 0x20
131 #define DMM32AT_INTRESET 0x08
132 #define DMM32AT_CLKACC 0x00
133 #define DMM32AT_DIOACC 0x01
135 /* DMM32AT_AISTAT 0x08 */
136 #define DMM32AT_STATUS 0x80
138 /* DMM32AT_INTCLOCK 0x09 */
139 #define DMM32AT_ADINT 0x80
140 #define DMM32AT_CLKSEL 0x03
142 /* DMM32AT_CNTRDIO 0x0a */
143 #define DMM32AT_FREQ12 0x80
145 /* DMM32AT_AICONF 0x0b */
146 #define DMM32AT_RANGE_U10 0x0c
147 #define DMM32AT_RANGE_U5 0x0d
148 #define DMM32AT_RANGE_B10 0x08
149 #define DMM32AT_RANGE_B5 0x00
150 #define DMM32AT_SCINT_20 0x00
151 #define DMM32AT_SCINT_15 0x10
152 #define DMM32AT_SCINT_10 0x20
153 #define DMM32AT_SCINT_5 0x30
155 /* DMM32AT_CLKCT 0x0f */
156 #define DMM32AT_CLKCT1 0x56 /* mode3 counter 1 - write low byte only */
157 #define DMM32AT_CLKCT2 0xb6 /* mode3 counter 2 - write high and low byte */
159 /* DMM32AT_DIOCONF 0x0f */
160 #define DMM32AT_DIENABLE 0x80
161 #define DMM32AT_DIRA 0x10
162 #define DMM32AT_DIRB 0x02
163 #define DMM32AT_DIRCL 0x01
164 #define DMM32AT_DIRCH 0x08
166 /* board AI ranges in comedi structure */
167 static const struct comedi_lrange dmm32at_airanges = {
177 /* register values for above ranges */
178 static const unsigned char dmm32at_rangebits[] = {
185 /* only one of these ranges is valid, as set by a jumper on the
186 * board. The application should only use the range set by the jumper
188 static const struct comedi_lrange dmm32at_aoranges = {
199 * Board descriptions for two imaginary boards. Describing the
200 * boards in this way is optional, and completely driver-dependent.
201 * Some drivers use arrays such as this, other do not.
203 typedef struct dmm32at_board_struct {
207 const struct comedi_lrange *ai_ranges;
210 const struct comedi_lrange *ao_ranges;
214 static const dmm32at_board dmm32at_boards[] = {
219 ai_ranges:&dmm32at_airanges,
222 ao_ranges:&dmm32at_aoranges,
229 * Useful for shorthand access to the particular board structure
231 #define thisboard ((const dmm32at_board *)dev->board_ptr)
233 /* this structure is for data unique to this hardware driver. If
234 * several hardware drivers keep similar information in this structure,
235 * feel free to suggest moving the variable to the struct comedi_device struct.
241 unsigned int ai_scans_left;
243 /* Used for AO readback */
244 unsigned int ao_readback[4];
245 unsigned char dio_config;
250 * most drivers define the following macro to make it easy to
251 * access the private structure.
253 #define devpriv ((dmm32at_private *)dev->private)
256 * The struct comedi_driver structure tells the Comedi core module
257 * which functions to call to configure/deconfigure (attach/detach)
258 * the board, and also about the kernel module that contains
261 static int dmm32at_attach(struct comedi_device * dev, struct comedi_devconfig * it);
262 static int dmm32at_detach(struct comedi_device * dev);
263 static struct comedi_driver driver_dmm32at = {
264 driver_name:"dmm32at",
266 attach:dmm32at_attach,
267 detach:dmm32at_detach,
268 /* It is not necessary to implement the following members if you are
269 * writing a driver for a ISA PnP or PCI card */
270 /* Most drivers will support multiple types of boards by
271 * having an array of board structures. These were defined
272 * in dmm32at_boards[] above. Note that the element 'name'
273 * was first in the structure -- Comedi uses this fact to
274 * extract the name of the board without knowing any details
275 * about the structure except for its length.
276 * When a device is attached (by comedi_config), the name
277 * of the device is given to Comedi, and Comedi tries to
278 * match it by going through the list of board names. If
279 * there is a match, the address of the pointer is put
280 * into dev->board_ptr and driver->attach() is called.
282 * Note that these are not necessary if you can determine
283 * the type of board in software. ISA PnP, PCI, and PCMCIA
284 * devices are such boards.
286 board_name:&dmm32at_boards[0].name,
287 offset:sizeof(dmm32at_board),
288 num_names:sizeof(dmm32at_boards) / sizeof(dmm32at_board),
291 /* prototypes for driver functions below */
292 static int dmm32at_ai_rinsn(struct comedi_device * dev, struct comedi_subdevice * s,
293 struct comedi_insn * insn, unsigned int * data);
294 static int dmm32at_ao_winsn(struct comedi_device * dev, struct comedi_subdevice * s,
295 struct comedi_insn * insn, unsigned int * data);
296 static int dmm32at_ao_rinsn(struct comedi_device * dev, struct comedi_subdevice * s,
297 struct comedi_insn * insn, unsigned int * data);
298 static int dmm32at_dio_insn_bits(struct comedi_device * dev, struct comedi_subdevice * s,
299 struct comedi_insn * insn, unsigned int * data);
300 static int dmm32at_dio_insn_config(struct comedi_device * dev, struct comedi_subdevice * s,
301 struct comedi_insn * insn, unsigned int * data);
302 static int dmm32at_ai_cmdtest(struct comedi_device * dev, struct comedi_subdevice * s,
303 struct comedi_cmd * cmd);
304 static int dmm32at_ai_cmd(struct comedi_device * dev, struct comedi_subdevice * s);
305 static int dmm32at_ai_cancel(struct comedi_device * dev, struct comedi_subdevice * s);
306 static int dmm32at_ns_to_timer(unsigned int *ns, int round);
307 static irqreturn_t dmm32at_isr(int irq, void *d PT_REGS_ARG);
308 void dmm32at_setaitimer(struct comedi_device * dev, unsigned int nansec);
311 * Attach is called by the Comedi core to configure the driver
312 * for a particular board. If you specified a board_name array
313 * in the driver structure, dev->board_ptr contains that
316 static int dmm32at_attach(struct comedi_device * dev, struct comedi_devconfig * it)
319 struct comedi_subdevice *s;
320 unsigned char aihi, ailo, fifostat, aistat, intstat, airback;
321 unsigned long iobase;
324 iobase = it->options[0];
325 irq = it->options[1];
327 printk("comedi%d: dmm32at: attaching\n", dev->minor);
328 printk("dmm32at: probing at address 0x%04lx, irq %u\n", iobase, irq);
330 /* register address space */
331 if (!request_region(iobase, DMM32AT_MEMSIZE, thisboard->name)) {
332 printk("I/O port conflict\n");
335 dev->iobase = iobase;
337 /* the following just makes sure the board is there and gets
338 it to a known state */
340 /* reset the board */
341 dmm_outb(dev, DMM32AT_CNTRL, DMM32AT_RESET);
343 /* allow a millisecond to reset */
346 /* zero scan and fifo control */
347 dmm_outb(dev, DMM32AT_FIFOCNTRL, 0x0);
349 /* zero interrupt and clock control */
350 dmm_outb(dev, DMM32AT_INTCLOCK, 0x0);
352 /* write a test channel range, the high 3 bits should drop */
353 dmm_outb(dev, DMM32AT_AILOW, 0x80);
354 dmm_outb(dev, DMM32AT_AIHIGH, 0xff);
356 /* set the range at 10v unipolar */
357 dmm_outb(dev, DMM32AT_AICONF, DMM32AT_RANGE_U10);
359 /* should take 10 us to settle, here's a hundred */
362 /* read back the values */
363 ailo = dmm_inb(dev, DMM32AT_AILOW);
364 aihi = dmm_inb(dev, DMM32AT_AIHIGH);
365 fifostat = dmm_inb(dev, DMM32AT_FIFOSTAT);
366 aistat = dmm_inb(dev, DMM32AT_AISTAT);
367 intstat = dmm_inb(dev, DMM32AT_INTCLOCK);
368 airback = dmm_inb(dev, DMM32AT_AIRBACK);
370 printk("dmm32at: lo=0x%02x hi=0x%02x fifostat=0x%02x\n",
371 ailo, aihi, fifostat);
372 printk("dmm32at: aistat=0x%02x intstat=0x%02x airback=0x%02x\n",
373 aistat, intstat, airback);
375 if ((ailo != 0x00) || (aihi != 0x1f) || (fifostat != 0x80) ||
376 (aistat != 0x60 || (intstat != 0x00) || airback != 0x0c)) {
377 printk("dmmat32: board detection failed\n");
381 /* board is there, register interrupt */
383 ret = comedi_request_irq(irq, dmm32at_isr, 0, thisboard->name,
386 printk("irq conflict\n");
393 * If you can probe the device to determine what device in a series
394 * it is, this is the place to do it. Otherwise, dev->board_ptr
395 * should already be initialized.
397 //dev->board_ptr = dmm32at_probe(dev);
400 * Initialize dev->board_name. Note that we can use the "thisboard"
401 * macro now, since we just initialized it in the last line.
403 dev->board_name = thisboard->name;
406 * Allocate the private structure area. alloc_private() is a
407 * convenient macro defined in comedidev.h.
409 if (alloc_private(dev, sizeof(dmm32at_private)) < 0)
413 * Allocate the subdevice structures. alloc_subdevice() is a
414 * convenient macro defined in comedidev.h.
416 if (alloc_subdevices(dev, 3) < 0)
419 s = dev->subdevices + 0;
420 dev->read_subdev = s;
421 /* analog input subdevice */
422 s->type = COMEDI_SUBD_AI;
423 /* we support single-ended (ground) and differential */
424 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_DIFF | SDF_CMD_READ;
425 s->n_chan = thisboard->ai_chans;
426 s->maxdata = (1 << thisboard->ai_bits) - 1;
427 s->range_table = thisboard->ai_ranges;
428 s->len_chanlist = 32; /* This is the maximum chanlist length that
429 the board can handle */
430 s->insn_read = dmm32at_ai_rinsn;
431 s->do_cmd = dmm32at_ai_cmd;
432 s->do_cmdtest = dmm32at_ai_cmdtest;
433 s->cancel = dmm32at_ai_cancel;
435 s = dev->subdevices + 1;
436 /* analog output subdevice */
437 s->type = COMEDI_SUBD_AO;
438 s->subdev_flags = SDF_WRITABLE;
439 s->n_chan = thisboard->ao_chans;
440 s->maxdata = (1 << thisboard->ao_bits) - 1;
441 s->range_table = thisboard->ao_ranges;
442 s->insn_write = dmm32at_ao_winsn;
443 s->insn_read = dmm32at_ao_rinsn;
445 s = dev->subdevices + 2;
446 /* digital i/o subdevice */
447 if (thisboard->have_dio) {
449 /* get access to the DIO regs */
450 dmm_outb(dev, DMM32AT_CNTRL, DMM32AT_DIOACC);
451 /* set the DIO's to the defualt input setting */
452 devpriv->dio_config = DMM32AT_DIRA | DMM32AT_DIRB |
453 DMM32AT_DIRCL | DMM32AT_DIRCH | DMM32AT_DIENABLE;
454 dmm_outb(dev, DMM32AT_DIOCONF, devpriv->dio_config);
456 /* set up the subdevice */
457 s->type = COMEDI_SUBD_DIO;
458 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
459 s->n_chan = thisboard->dio_chans;
462 s->range_table = &range_digital;
463 s->insn_bits = dmm32at_dio_insn_bits;
464 s->insn_config = dmm32at_dio_insn_config;
466 s->type = COMEDI_SUBD_UNUSED;
470 printk("comedi%d: dmm32at: attached\n", dev->minor);
477 * _detach is called to deconfigure a device. It should deallocate
479 * This function is also called when _attach() fails, so it should be
480 * careful not to release resources that were not necessarily
481 * allocated by _attach(). dev->private and dev->subdevices are
482 * deallocated automatically by the core.
484 static int dmm32at_detach(struct comedi_device * dev)
486 printk("comedi%d: dmm32at: remove\n", dev->minor);
488 comedi_free_irq(dev->irq, dev);
490 release_region(dev->iobase, DMM32AT_MEMSIZE);
496 * "instructions" read/write data in "one-shot" or "software-triggered"
500 static int dmm32at_ai_rinsn(struct comedi_device * dev, struct comedi_subdevice * s,
501 struct comedi_insn * insn, unsigned int * data)
505 unsigned char status;
506 unsigned short msb, lsb;
510 /* get the channel and range number */
512 chan = CR_CHAN(insn->chanspec) & (s->n_chan - 1);
513 range = CR_RANGE(insn->chanspec);
515 //printk("channel=0x%02x, range=%d\n",chan,range);
517 /* zero scan and fifo control and reset fifo */
518 dmm_outb(dev, DMM32AT_FIFOCNTRL, DMM32AT_FIFORESET);
520 /* write the ai channel range regs */
521 dmm_outb(dev, DMM32AT_AILOW, chan);
522 dmm_outb(dev, DMM32AT_AIHIGH, chan);
523 /* set the range bits */
524 dmm_outb(dev, DMM32AT_AICONF, dmm32at_rangebits[range]);
526 /* wait for circuit to settle */
527 for (i = 0; i < 40000; i++) {
528 status = dmm_inb(dev, DMM32AT_AIRBACK);
529 if ((status & DMM32AT_STATUS) == 0)
537 /* convert n samples */
538 for (n = 0; n < insn->n; n++) {
539 /* trigger conversion */
540 dmm_outb(dev, DMM32AT_CONV, 0xff);
541 /* wait for conversion to end */
542 for (i = 0; i < 40000; i++) {
543 status = dmm_inb(dev, DMM32AT_AISTAT);
544 if ((status & DMM32AT_STATUS) == 0)
553 lsb = dmm_inb(dev, DMM32AT_AILSB);
554 msb = dmm_inb(dev, DMM32AT_AIMSB);
556 /* invert sign bit to make range unsigned, this is an
557 idiosyncracy of the diamond board, it return
558 conversions as a signed value, i.e. -32768 to
559 32767, flipping the bit and interpreting it as
560 signed gives you a range of 0 to 65535 which is
562 d = ((msb ^ 0x0080) << 8) + lsb;
567 /* return the number of samples read/written */
571 static int dmm32at_ai_cmdtest(struct comedi_device * dev, struct comedi_subdevice * s,
572 struct comedi_cmd * cmd)
576 int start_chan, gain, i;
578 //printk("dmmat32 in command test\n");
580 /* cmdtest tests a particular command to see if it is valid.
581 * Using the cmdtest ioctl, a user can create a valid cmd
582 * and then have it executes by the cmd ioctl.
584 * cmdtest returns 1,2,3,4 or 0, depending on which tests
585 * the command passes. */
587 /* step 1: make sure trigger sources are trivially valid */
589 tmp = cmd->start_src;
590 cmd->start_src &= TRIG_NOW;
591 if (!cmd->start_src || tmp != cmd->start_src)
594 tmp = cmd->scan_begin_src;
595 cmd->scan_begin_src &= TRIG_TIMER /*| TRIG_EXT */ ;
596 if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src)
599 tmp = cmd->convert_src;
600 cmd->convert_src &= TRIG_TIMER /*| TRIG_EXT */ ;
601 if (!cmd->convert_src || tmp != cmd->convert_src)
604 tmp = cmd->scan_end_src;
605 cmd->scan_end_src &= TRIG_COUNT;
606 if (!cmd->scan_end_src || tmp != cmd->scan_end_src)
610 cmd->stop_src &= TRIG_COUNT | TRIG_NONE;
611 if (!cmd->stop_src || tmp != cmd->stop_src)
617 /* step 2: make sure trigger sources are unique and mutually compatible */
619 /* note that mutual compatiblity is not an issue here */
620 if (cmd->scan_begin_src != TRIG_TIMER &&
621 cmd->scan_begin_src != TRIG_EXT)
623 if (cmd->convert_src != TRIG_TIMER && cmd->convert_src != TRIG_EXT)
625 if (cmd->stop_src != TRIG_COUNT && cmd->stop_src != TRIG_NONE)
631 /* step 3: make sure arguments are trivially compatible */
633 if (cmd->start_arg != 0) {
637 #define MAX_SCAN_SPEED 1000000 /* in nanoseconds */
638 #define MIN_SCAN_SPEED 1000000000 /* in nanoseconds */
640 if (cmd->scan_begin_src == TRIG_TIMER) {
641 if (cmd->scan_begin_arg < MAX_SCAN_SPEED) {
642 cmd->scan_begin_arg = MAX_SCAN_SPEED;
645 if (cmd->scan_begin_arg > MIN_SCAN_SPEED) {
646 cmd->scan_begin_arg = MIN_SCAN_SPEED;
650 /* external trigger */
651 /* should be level/edge, hi/lo specification here */
652 /* should specify multiple external triggers */
653 if (cmd->scan_begin_arg > 9) {
654 cmd->scan_begin_arg = 9;
658 if (cmd->convert_src == TRIG_TIMER) {
659 if (cmd->convert_arg >= 17500)
660 cmd->convert_arg = 20000;
661 else if (cmd->convert_arg >= 12500)
662 cmd->convert_arg = 15000;
663 else if (cmd->convert_arg >= 7500)
664 cmd->convert_arg = 10000;
666 cmd->convert_arg = 5000;
669 /* external trigger */
671 if (cmd->convert_arg > 9) {
672 cmd->convert_arg = 9;
677 if (cmd->scan_end_arg != cmd->chanlist_len) {
678 cmd->scan_end_arg = cmd->chanlist_len;
681 if (cmd->stop_src == TRIG_COUNT) {
682 if (cmd->stop_arg > 0xfffffff0) {
683 cmd->stop_arg = 0xfffffff0;
686 if (cmd->stop_arg == 0) {
692 if (cmd->stop_arg != 0) {
701 /* step 4: fix up any arguments */
703 if (cmd->scan_begin_src == TRIG_TIMER) {
704 tmp = cmd->scan_begin_arg;
705 dmm32at_ns_to_timer(&cmd->scan_begin_arg,
706 cmd->flags & TRIG_ROUND_MASK);
707 if (tmp != cmd->scan_begin_arg)
710 if (cmd->convert_src == TRIG_TIMER) {
711 tmp = cmd->convert_arg;
712 dmm32at_ns_to_timer(&cmd->convert_arg,
713 cmd->flags & TRIG_ROUND_MASK);
714 if (tmp != cmd->convert_arg)
716 if (cmd->scan_begin_src == TRIG_TIMER &&
717 cmd->scan_begin_arg <
718 cmd->convert_arg * cmd->scan_end_arg) {
719 cmd->scan_begin_arg =
720 cmd->convert_arg * cmd->scan_end_arg;
728 /* step 5 check the channel list, the channel list for this
729 board must be consecutive and gains must be the same */
732 gain = CR_RANGE(cmd->chanlist[0]);
733 start_chan = CR_CHAN(cmd->chanlist[0]);
734 for (i = 1; i < cmd->chanlist_len; i++) {
735 if (CR_CHAN(cmd->chanlist[i]) !=
736 (start_chan + i) % s->n_chan) {
738 "entries in chanlist must be consecutive channels, counting upwards\n");
741 if (CR_RANGE(cmd->chanlist[i]) != gain) {
743 "entries in chanlist must all have the same gain\n");
755 static int dmm32at_ai_cmd(struct comedi_device * dev, struct comedi_subdevice * s)
757 struct comedi_cmd *cmd = &s->async->cmd;
759 unsigned char chanlo, chanhi, status;
764 /* get the channel list and range */
765 chanlo = CR_CHAN(cmd->chanlist[0]) & (s->n_chan - 1);
766 chanhi = chanlo + cmd->chanlist_len - 1;
767 if (chanhi >= s->n_chan)
769 range = CR_RANGE(cmd->chanlist[0]);
772 dmm_outb(dev, DMM32AT_FIFOCNTRL, DMM32AT_FIFORESET);
774 /* set scan enable */
775 dmm_outb(dev, DMM32AT_FIFOCNTRL, DMM32AT_SCANENABLE);
777 /* write the ai channel range regs */
778 dmm_outb(dev, DMM32AT_AILOW, chanlo);
779 dmm_outb(dev, DMM32AT_AIHIGH, chanhi);
781 /* set the range bits */
782 dmm_outb(dev, DMM32AT_AICONF, dmm32at_rangebits[range]);
784 /* reset the interrupt just in case */
785 dmm_outb(dev, DMM32AT_CNTRL, DMM32AT_INTRESET);
787 if (cmd->stop_src == TRIG_COUNT)
788 devpriv->ai_scans_left = cmd->stop_arg;
789 else { /* TRIG_NONE */
790 devpriv->ai_scans_left = 0xffffffff; /* indicates TRIG_NONE to isr */
793 /* wait for circuit to settle */
794 for (i = 0; i < 40000; i++) {
795 status = dmm_inb(dev, DMM32AT_AIRBACK);
796 if ((status & DMM32AT_STATUS) == 0)
804 if (devpriv->ai_scans_left > 1) {
805 /* start the clock and enable the interrupts */
806 dmm32at_setaitimer(dev, cmd->scan_begin_arg);
808 /* start the interrups and initiate a single scan */
809 dmm_outb(dev, DMM32AT_INTCLOCK, DMM32AT_ADINT);
810 dmm_outb(dev, DMM32AT_CONV, 0xff);
813 /* printk("dmmat32 in command\n"); */
815 /* for(i=0;i<cmd->chanlist_len;i++) */
816 /* comedi_buf_put(s->async,i*100); */
818 /* s->async->events |= COMEDI_CB_EOA; */
819 /* comedi_event(dev, s); */
825 static int dmm32at_ai_cancel(struct comedi_device * dev, struct comedi_subdevice * s)
827 devpriv->ai_scans_left = 1;
831 static irqreturn_t dmm32at_isr(int irq, void *d PT_REGS_ARG)
833 unsigned char intstat;
835 unsigned short msb, lsb;
837 struct comedi_device *dev = d;
839 if (!dev->attached) {
840 comedi_error(dev, "spurious interrupt");
844 intstat = dmm_inb(dev, DMM32AT_INTCLOCK);
846 if (intstat & DMM32AT_ADINT) {
847 struct comedi_subdevice *s = dev->read_subdev;
848 struct comedi_cmd *cmd = &s->async->cmd;
850 for (i = 0; i < cmd->chanlist_len; i++) {
852 lsb = dmm_inb(dev, DMM32AT_AILSB);
853 msb = dmm_inb(dev, DMM32AT_AIMSB);
855 /* invert sign bit to make range unsigned */
856 samp = ((msb ^ 0x0080) << 8) + lsb;
857 comedi_buf_put(s->async, samp);
860 if (devpriv->ai_scans_left != 0xffffffff) { /* TRIG_COUNT */
861 devpriv->ai_scans_left--;
862 if (devpriv->ai_scans_left == 0) {
863 /* disable further interrupts and clocks */
864 dmm_outb(dev, DMM32AT_INTCLOCK, 0x0);
865 /* set the buffer to be flushed with an EOF */
866 s->async->events |= COMEDI_CB_EOA;
870 /* flush the buffer */
871 comedi_event(dev, s);
874 /* reset the interrupt */
875 dmm_outb(dev, DMM32AT_CNTRL, DMM32AT_INTRESET);
879 /* This function doesn't require a particular form, this is just
880 * what happens to be used in some of the drivers. It should
881 * convert ns nanoseconds to a counter value suitable for programming
882 * the device. Also, it should adjust ns so that it cooresponds to
883 * the actual time that the device will use. */
884 static int dmm32at_ns_to_timer(unsigned int *ns, int round)
887 /* if your timing is done through two cascaded timers, the
888 * i8253_cascade_ns_to_timer() function in 8253.h can be
889 * very helpful. There are also i8254_load() and i8254_mm_load()
890 * which can be used to load values into the ubiquitous 8254 counters
896 static int dmm32at_ao_winsn(struct comedi_device * dev, struct comedi_subdevice * s,
897 struct comedi_insn * insn, unsigned int * data)
900 int chan = CR_CHAN(insn->chanspec);
901 unsigned char hi, lo, status;
903 /* Writing a list of values to an AO channel is probably not
904 * very useful, but that's how the interface is defined. */
905 for (i = 0; i < insn->n; i++) {
907 devpriv->ao_readback[chan] = data[i];
909 /* get the low byte */
910 lo = data[i] & 0x00ff;
911 /* high byte also contains channel number */
912 hi = (data[i] >> 8) + chan * (1 << 6);
913 //printk("writing 0x%02x 0x%02x\n",hi,lo);
914 /* write the low and high values to the board */
915 dmm_outb(dev, DMM32AT_DACLSB, lo);
916 dmm_outb(dev, DMM32AT_DACMSB, hi);
918 /* wait for circuit to settle */
919 for (i = 0; i < 40000; i++) {
920 status = dmm_inb(dev, DMM32AT_DACSTAT);
921 if ((status & DMM32AT_DACBUSY) == 0)
928 /* dummy read to update trigger the output */
929 status = dmm_inb(dev, DMM32AT_DACMSB);
933 /* return the number of samples read/written */
937 /* AO subdevices should have a read insn as well as a write insn.
938 * Usually this means copying a value stored in devpriv. */
939 static int dmm32at_ao_rinsn(struct comedi_device * dev, struct comedi_subdevice * s,
940 struct comedi_insn * insn, unsigned int * data)
943 int chan = CR_CHAN(insn->chanspec);
945 for (i = 0; i < insn->n; i++)
946 data[i] = devpriv->ao_readback[chan];
951 /* DIO devices are slightly special. Although it is possible to
952 * implement the insn_read/insn_write interface, it is much more
953 * useful to applications if you implement the insn_bits interface.
954 * This allows packed reading/writing of the DIO channels. The
955 * comedi core can convert between insn_bits and insn_read/write */
956 static int dmm32at_dio_insn_bits(struct comedi_device * dev, struct comedi_subdevice * s,
957 struct comedi_insn * insn, unsigned int * data)
959 unsigned char diobits;
964 /* The insn data is a mask in data[0] and the new data
965 * in data[1], each channel cooresponding to a bit. */
967 s->state &= ~data[0];
968 s->state |= data[0] & data[1];
969 /* Write out the new digital output lines */
970 //outw(s->state,dev->iobase + DMM32AT_DIO);
973 /* get access to the DIO regs */
974 dmm_outb(dev, DMM32AT_CNTRL, DMM32AT_DIOACC);
976 /* if either part of dio is set for output */
977 if (((devpriv->dio_config & DMM32AT_DIRCL) == 0) ||
978 ((devpriv->dio_config & DMM32AT_DIRCH) == 0)) {
979 diobits = (s->state & 0x00ff0000) >> 16;
980 dmm_outb(dev, DMM32AT_DIOC, diobits);
982 if ((devpriv->dio_config & DMM32AT_DIRB) == 0) {
983 diobits = (s->state & 0x0000ff00) >> 8;
984 dmm_outb(dev, DMM32AT_DIOB, diobits);
986 if ((devpriv->dio_config & DMM32AT_DIRA) == 0) {
987 diobits = (s->state & 0x000000ff);
988 dmm_outb(dev, DMM32AT_DIOA, diobits);
991 /* now read the state back in */
992 s->state = dmm_inb(dev, DMM32AT_DIOC);
994 s->state |= dmm_inb(dev, DMM32AT_DIOB);
996 s->state |= dmm_inb(dev, DMM32AT_DIOA);
999 /* on return, data[1] contains the value of the digital
1000 * input and output lines. */
1001 //data[1]=inw(dev->iobase + DMM32AT_DIO);
1002 /* or we could just return the software copy of the output values if
1003 * it was a purely digital output subdevice */
1009 static int dmm32at_dio_insn_config(struct comedi_device * dev, struct comedi_subdevice * s,
1010 struct comedi_insn * insn, unsigned int * data)
1012 unsigned char chanbit;
1013 int chan = CR_CHAN(insn->chanspec);
1019 chanbit = DMM32AT_DIRA;
1021 chanbit = DMM32AT_DIRB;
1023 chanbit = DMM32AT_DIRCL;
1025 chanbit = DMM32AT_DIRCH;
1027 /* The input or output configuration of each digital line is
1028 * configured by a special insn_config instruction. chanspec
1029 * contains the channel to be changed, and data[0] contains the
1030 * value COMEDI_INPUT or COMEDI_OUTPUT. */
1032 /* if output clear the bit, otherwise set it */
1033 if (data[0] == COMEDI_OUTPUT) {
1034 devpriv->dio_config &= ~chanbit;
1036 devpriv->dio_config |= chanbit;
1038 /* get access to the DIO regs */
1039 dmm_outb(dev, DMM32AT_CNTRL, DMM32AT_DIOACC);
1040 /* set the DIO's to the new configuration setting */
1041 dmm_outb(dev, DMM32AT_DIOCONF, devpriv->dio_config);
1046 void dmm32at_setaitimer(struct comedi_device * dev, unsigned int nansec)
1048 unsigned char lo1, lo2, hi2;
1049 unsigned short both2;
1051 /* based on 10mhz clock */
1053 both2 = nansec / 20000;
1054 hi2 = (both2 & 0xff00) >> 8;
1055 lo2 = both2 & 0x00ff;
1057 /* set the counter frequency to 10mhz */
1058 dmm_outb(dev, DMM32AT_CNTRDIO, 0);
1060 /* get access to the clock regs */
1061 dmm_outb(dev, DMM32AT_CNTRL, DMM32AT_CLKACC);
1063 /* write the counter 1 control word and low byte to counter */
1064 dmm_outb(dev, DMM32AT_CLKCT, DMM32AT_CLKCT1);
1065 dmm_outb(dev, DMM32AT_CLK1, lo1);
1067 /* write the counter 2 control word and low byte then to counter */
1068 dmm_outb(dev, DMM32AT_CLKCT, DMM32AT_CLKCT2);
1069 dmm_outb(dev, DMM32AT_CLK2, lo2);
1070 dmm_outb(dev, DMM32AT_CLK2, hi2);
1072 /* enable the ai conversion interrupt and the clock to start scans */
1073 dmm_outb(dev, DMM32AT_INTCLOCK, DMM32AT_ADINT | DMM32AT_CLKSEL);
1078 * A convenient macro that defines init_module() and cleanup_module(),
1081 COMEDI_INITCLEANUP(driver_dmm32at);