CIO-DAS16/M1 driver
Author: Frank Mori Hess, based on code from the das16
driver.
+ Copyright (C) 2001 Frank Mori Hess <fmhess@users.sourceforge.net>
COMEDI - Linux Control and Measurement Device Interface
- Copyright (C) 2000 David A. Schleef <ds@stm.lbl.gov>
+ Copyright (C) 2000 David A. Schleef <ds@schleef.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
************************************************************************
+*/
+/*
+Driver: das16m1.o
+Description: CIO-DAS16/M1
+Author: Frank Mori Hess <fmhess@uiuc.edu>
+Devices: [MeasurementComputing] CIO-DAS16/M1 (cio-das16/m1)
+Status: works
+
+This driver supports a single board - the CIO-DAS16/M1.
+As far as I know, there are no other boards that have
+the same register layout. Even the CIO-DAS16/M1/16 is
+significantly different.
+
+I was _barely_ able to reach the full 1 MHz capability
+of this board, using a hard real-time interrupt
+(set the TRIG_RT flag in your comedi_cmd and use
+rtlinux or RTAI). The board can't do dma, so the bottleneck is
+pulling the data across the ISA bus. I timed the interrupt
+handler, and it took my computer ~470 microseconds to pull 512
+samples from the board. So at 1 Mhz sampling rate,
+expect your CPU to be spending almost all of its
+time in the interrupt handler.
-This driver is for the (freakish) Measurement Computing (Computer Boards)
-CIO-DAS16/M1 board. The similarly namced CIO-DAS16/M1/16 board actually
-has a different register layout and is not supported by this driver
-(although it might go nicely in the das16.c driver.)
-
-Options:
- [0] - base io address
- [1] - irq (optional, required for timed or externally triggered conversions)
-
-irq can be omitted, although the cmd interface will not work without it.
-
-NOTES:
This board has some unusual restrictions for its channel/gain list. If the
list has 2 or more channels in it, then two conditions must be satisfied:
(1) - even/odd channels must appear at even/odd indices in the list
(2) - the list must have an even number of entries.
-*/
+Options:
+ [0] - base io address
+ [1] - irq (optional, but you probably want it)
+irq can be omitted, although the cmd interface will not work without it.
+*/
-#include <linux/kernel.h>
-#include <linux/module.h>
#include <linux/comedidev.h>
-#include <linux/errno.h>
#include <linux/ioport.h>
-#include <asm/io.h>
-#include <linux/malloc.h>
-#include <linux/delay.h>
+
#include "8255.h"
#include "8253.h"
+#include "comedi_fc.h"
#define DAS16M1_SIZE 16
#define DAS16M1_SIZE2 8
#define DAS16M1_XTAL 100 //10 MHz master clock
-#define HALF_FIFO 512 // 1024 sample fifo
+#define FIFO_SIZE 1024 // 1024 sample fifo
/*
CIO-DAS16_M1.pdf
#define DAS16M1_AI 0 // 16-bit wide register
#define AI_CHAN(x) ((x) & 0xf)
-#define AI_DATA(x) (((x) >> 4) & 0xfff)
#define DAS16M1_CS 2
#define EXT_TRIG_BIT 0x1
#define OVRUN 0x20
#define Q_CHAN(x) ((x) & 0x7)
#define Q_RANGE(x) (((x) & 0xf) << 4)
#define UNIPOLAR 0x40
-#define DAS16M1_8254_FIRST 8
+#define DAS16M1_8254_FIRST 0x8
+#define DAS16M1_8254_FIRST_CNTRL 0xb
+#define TOTAL_CLEAR 0x30
#define DAS16M1_8254_SECOND 0xc
#define DAS16M1_82C55 0x400
#define DAS16M1_8254_THIRD 0x404
static int das16m1_cmd_exec(comedi_device *dev,comedi_subdevice *s);
static int das16m1_cancel(comedi_device *dev, comedi_subdevice *s);
+static int das16m1_poll(comedi_device *dev, comedi_subdevice *s);
static void das16m1_interrupt(int irq, void *d, struct pt_regs *regs);
+static void das16m1_handler(comedi_device *dev, unsigned int status);
static unsigned int das16m1_set_pacer(comedi_device *dev, unsigned int ns, int round_flag);
static int das16m1_attach(comedi_device *dev, comedi_devconfig *it);
static int das16m1_detach(comedi_device *dev);
-comedi_driver driver_das16m1={
+static comedi_driver driver_das16m1={
driver_name: "das16m1",
module: THIS_MODULE,
attach: das16m1_attach,
struct das16m1_private_struct {
unsigned int control_state;
- volatile unsigned int adc_count; // number of samples remaining
+ volatile unsigned int adc_count; // number of samples completed
+ /* initial value in lower half of hardware conversion counter,
+ * needed to keep track of whether new count has been loaded into
+ * counter yet (loaded by first sample conversion) */
+ u16 initial_hw_count;
+ sampl_t ai_buffer[ FIFO_SIZE ];
unsigned int do_bits; // saves status of digital output bits
unsigned int divisor1; // divides master clock to obtain conversion speed
unsigned int divisor2; // divides master clock to obtain conversion speed
COMEDI_INITCLEANUP(driver_das16m1);
+static inline sampl_t munge_sample( sampl_t data )
+{
+ return ( data >> 4 ) & 0xfff;
+}
+
static int das16m1_cmd_test(comedi_device *dev,comedi_subdevice *s, comedi_cmd *cmd)
{
unsigned int err=0, tmp, i;
err++;
}
}
- // check chanlist agains board's peculiarities
- if(cmd->chanlist_len > 1)
+
+ if(err) return 3;
+
+ /* step 4: fix up arguments */
+
+ if(cmd->convert_src == TRIG_TIMER)
+ {
+ tmp = cmd->convert_arg;
+ /* calculate counter values that give desired timing */
+ i8253_cascade_ns_to_timer_2div(DAS16M1_XTAL, &(devpriv->divisor1),
+ &(devpriv->divisor2), &(cmd->convert_arg), cmd->flags & TRIG_ROUND_MASK);
+ if(tmp != cmd->convert_arg) err++;
+ }
+
+ if(err) return 4;
+
+ // check chanlist against board's peculiarities
+ if(cmd->chanlist && cmd->chanlist_len > 1)
{
for(i = 0; i < cmd->chanlist_len; i++)
{
}
}
- if(err) return 3;
-
- /* step 4: fix up arguments */
-
- if(cmd->convert_src == TRIG_TIMER)
- {
- tmp = cmd->convert_arg;
- /* calculate counter values that give desired timing */
- i8253_cascade_ns_to_timer_2div(DAS16M1_XTAL, &(devpriv->divisor1),
- &(devpriv->divisor2), &(cmd->convert_arg), cmd->flags & TRIG_ROUND_MASK);
- if(tmp != cmd->convert_arg) err++;
- }
-
- if(err) return 4;
+ if(err) return 5;
return 0;
}
return -1;
}
- devpriv->adc_count = cmd->stop_arg * cmd->chanlist_len;
+ /* disable interrupts and internal pacer */
+ devpriv->control_state &= ~INTE & ~PACER_MASK;
+ outb(devpriv->control_state, dev->iobase + DAS16M1_INTR_CONTROL);
+ // set software count
+ devpriv->adc_count = 0;
+ /* Initialize lower half of hardware counter, used to determine how
+ * many samples are in fifo. Value doesn't actually load into counter
+ * until counter's next clock (the next a/d conversion) */
+ i8254_load(dev->iobase + DAS16M1_8254_FIRST, 1, 0, 2);
+ /* remember current reading of counter so we know when counter has
+ * actually been loaded */
+ devpriv->initial_hw_count = i8254_read(dev->iobase + DAS16M1_8254_FIRST, 1);
/* setup channel/gain queue */
for(i = 0; i < cmd->chanlist_len; i++)
{
/* set counter mode and counts */
cmd->convert_arg = das16m1_set_pacer(dev, cmd->convert_arg, cmd->flags & TRIG_ROUND_MASK);
- async->events = 0;
-
// set control & status register
byte = 0;
/* if we are using external start trigger (also board dislikes having
for(n = 0; n < insn->n; n++)
{
+ /* clear IRQDATA bit */
+ outb(0, dev->iobase + DAS16M1_CLEAR_INTR);
/* trigger conversion */
outb(0, dev->iobase);
comedi_error(dev, "timeout");
return -ETIME;
}
- data[n] = AI_DATA(inw(dev->iobase));
+ data[n] = munge_sample( inw( dev->iobase ) );
}
return n;
return 2;
}
+static int das16m1_poll(comedi_device *dev, comedi_subdevice *s)
+{
+ unsigned long flags;
+ unsigned int status;
+
+ // prevent race with interrupt handler
+ comedi_spin_lock_irqsave(&dev->spinlock, flags);
+ status = inb(dev->iobase + DAS16M1_CS);
+ das16m1_handler(dev, status);
+ comedi_spin_unlock_irqrestore(&dev->spinlock, flags);
+
+ return s->async->buf_write_count - s->async->buf_read_count;
+}
+
static void das16m1_interrupt(int irq, void *d, struct pt_regs *regs)
{
- int i, status;
- sampl_t data[HALF_FIFO];
+ int status;
comedi_device *dev = d;
- comedi_subdevice *s = dev->subdevices;
- comedi_async *async;
if(dev->attached == 0)
{
return;
}
+ // prevent race with comedi_poll()
+ spin_lock(&dev->spinlock);
+
status = inb(dev->iobase + DAS16M1_CS);
if((status & (IRQDATA | OVRUN)) == 0)
{
comedi_error(dev, "spurious interrupt");
+ spin_unlock(&dev->spinlock);
return;
}
- // initialize async here to avoid freak out on premature interrupt
+
+ das16m1_handler(dev, status);
+
+ /* clear interrupt */
+ outb(0, dev->iobase + DAS16M1_CLEAR_INTR);
+
+ spin_unlock(&dev->spinlock);
+}
+
+static void munge_sample_array( sampl_t *array, unsigned int num_elements )
+{
+ unsigned int i;
+
+ for(i = 0; i < num_elements; i++)
+ {
+ array[i] = munge_sample( array[i] );
+ }
+}
+
+static void das16m1_handler(comedi_device *dev, unsigned int status)
+{
+ comedi_subdevice *s;
+ comedi_async *async;
+ comedi_cmd *cmd;
+ u16 num_samples;
+ u16 hw_counter;
+
+ s = dev->read_subdev;
async = s->async;
+ async->events = 0;
+ cmd = &async->cmd;
- insw(dev->iobase, data, HALF_FIFO);
- for(i = 0; i < HALF_FIFO; i++)
+ // figure out how many samples are in fifo
+ hw_counter = i8254_read(dev->iobase + DAS16M1_8254_FIRST, 1);
+ /* make sure hardware counter reading is not bogus due to initial value
+ * not having been loaded yet */
+ if(devpriv->adc_count == 0 && hw_counter == devpriv->initial_hw_count)
{
- comedi_buf_put(async, AI_DATA(data[i]));
- if(async->cmd.stop_src == TRIG_COUNT)
- {
- if(--devpriv->adc_count == 0)
- { /* end of acquisition */
- das16m1_cancel(dev, s);
- async->events |= COMEDI_CB_EOA;
- break;
- }
+ num_samples = 0;
+ }else
+ {
+ /* The calculation of num_samples looks odd, but it uses the following facts.
+ * 16 bit hardware counter is initialized with value of zero (which really
+ * means 0x1000). The counter decrements by one on each conversion
+ * (when the counter decrements from zero it goes to 0xffff). num_samples
+ * is a 16 bit variable, so it will roll over in a similar fashion to the
+ * hardware counter. Work it out, and this is what you get. */
+ num_samples = - hw_counter - devpriv->adc_count;
+ }
+ // check if we only need some of the points
+ if(cmd->stop_src == TRIG_COUNT)
+ {
+ if(num_samples > cmd->stop_arg * cmd->chanlist_len)
+ num_samples = cmd->stop_arg * cmd->chanlist_len;
+ }
+ // make sure we dont try to get too many points if fifo has overrun
+ if(num_samples > FIFO_SIZE)
+ num_samples = FIFO_SIZE;
+ insw( dev->iobase, devpriv->ai_buffer, num_samples );
+ munge_sample_array( devpriv->ai_buffer, num_samples );
+ cfc_write_array_to_buffer( s, devpriv->ai_buffer, num_samples * sizeof( sampl_t ) );
+ devpriv->adc_count += num_samples;
+
+ if(cmd->stop_src == TRIG_COUNT)
+ {
+ if(devpriv->adc_count >= cmd->stop_arg * cmd->chanlist_len)
+ { /* end of acquisition */
+ das16m1_cancel(dev, s);
+ async->events |= COMEDI_CB_EOA;
}
}
+ /* this probably won't catch overruns since the card doesn't generate
+ * overrun interrupts, but we might as well try */
if(status & OVRUN)
{
das16m1_cancel(dev, s);
comedi_error(dev, "fifo overflow");
}
- async->events |= COMEDI_CB_BLOCK;
comedi_event(dev, s, async->events);
- async->events = 0;
- /* clear interrupt */
- outb(0, dev->iobase + DAS16M1_CLEAR_INTR);
}
/* This function takes a time in nanoseconds and sets the *
s->type = COMEDI_SUBD_AI;
s->subdev_flags = SDF_READABLE;
s->n_chan = 8;
- s->subdev_flags |= SDF_DIFF;
+ s->subdev_flags = SDF_DIFF;
s->len_chanlist = 256;
s->maxdata = (1 << 12) - 1;
s->range_table = &range_das16m1;
s->do_cmdtest = das16m1_cmd_test;
s->do_cmd = das16m1_cmd_exec;
s->cancel = das16m1_cancel;
+ s->poll = das16m1_poll;
s = dev->subdevices + 1;
/* di */
s = dev->subdevices + 2;
/* do */
s->type = COMEDI_SUBD_DO;
- s->subdev_flags = SDF_WRITEABLE;
+ s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
s->n_chan = 4;
s->maxdata = 1;
s->range_table = &range_digital;
s = dev->subdevices + 3;
/* 8255 */
- subdev_8255_init(dev, s, NULL, (void*)(dev->iobase + DAS16M1_82C55));
+ subdev_8255_init(dev, s, NULL, (unsigned long)(dev->iobase + DAS16M1_82C55));
+
+ // disable upper half of hardware conversion counter so it doesn't mess with us
+ outb(TOTAL_CLEAR, dev->iobase + DAS16M1_8254_FIRST_CNTRL);
// initialize digital output lines
outb(devpriv->do_bits, dev->iobase + DAS16M1_DIO);