2 comedi/drivers/adq12b.c
3 driver for MicroAxial ADQ12-B data acquisition and control card
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: driver for MicroAxial ADQ12-B data acquisition and control card
26 Devices: [MicroAxial] ADQ12-B (adq12b)
27 Author: jeremy theler <thelerg@ib.cnea.gov.ar>
28 Updated: Thu, 21 Feb 2008 02:56:27 -0300
31 Driver for the acquisition card ADQ12-B (without any add-on).
33 - Analog input is subdevice 0 (16 channels single-ended or 8 differential)
34 - Digital input is subdevice 1 (5 channels)
35 - Digital output is subdevice 1 (8 channels)
36 - The PACER is not supported in this version
38 If you do not specify any options, they will default to
40 # comedi_config /dev/comedi0 adq12b 0x300,0,0
42 option 1: I/O base address. The following table is provided as a help
43 of the hardware jumpers.
46 0x300 1 (factory default)
53 option 2: unipolar/bipolar ADC selection: 0 -> bipolar, 1 -> unipolar
55 selection comedi_config option JUB
56 bipolar 0 2-3 (factory default)
59 option 3: single-ended/differential AI selection: 0 -> SE, 1 -> differential
61 selection comedi_config option JCHA JCHB
62 single-ended 0 1-2 1-2 (factory default)
63 differential 1 2-3 2-3
65 written by jeremy theler <thelerg@ib.cnea.gov.ar>
68 comision nacional de energia atomica
69 universidad nacional de cuyo
73 + changed supported devices string (missused the [] and ())
81 #include "../comedidev.h"
83 /* address scheme (page 2.17 of the manual) */
84 #define ADQ12B_SIZE 16
86 #define ADQ12B_CTREG 0x00
87 #define ADQ12B_STINR 0x00
88 #define ADQ12B_OUTBR 0x04
89 #define ADQ12B_ADLOW 0x08
90 #define ADQ12B_ADHIG 0x09
91 #define ADQ12B_CONT0 0x0c
92 #define ADQ12B_CONT1 0x0d
93 #define ADQ12B_CONT2 0x0e
94 #define ADQ12B_COWORD 0x0f
96 /* mask of the bit at STINR to check end of conversion */
97 #define ADQ12B_EOC 0x20
101 /* available ranges through the PGA gains */
102 static const struct comedi_lrange range_adq12b_ai_bipolar = { 4, {
110 static const struct comedi_lrange range_adq12b_ai_unipolar = { 4, {
119 struct adq12b_board {
128 static const struct adq12b_board adq12b_boards[] = {
136 /* potentially, more adq-based deviced will be added */
139 .ai_chans = 16, // this is just for reference, hardcoded again later
146 #define thisboard ((const struct adq12b_board *)dev->board_ptr)
148 struct adq12b_private {
149 int unipolar; /* option 2 of comedi_config (1 is iobase) */
150 int differential; /* option 3 of comedi_config */
153 unsigned int digital_state;
156 #define devpriv ((struct adq12b_private *)dev->private)
159 * The struct comedi_driver structure tells the Comedi core module
160 * which functions to call to configure/deconfigure (attach/detach)
161 * the board, and also about the kernel module that contains
164 static int adq12b_attach(struct comedi_device *dev,
165 struct comedi_devconfig *it);
166 static int adq12b_detach(struct comedi_device *dev);
167 static struct comedi_driver driver_adq12b = {
168 driver_name:"adq12b",
170 attach:adq12b_attach,
171 detach:adq12b_detach,
172 board_name:&adq12b_boards[0].name,
173 offset:sizeof(struct adq12b_board),
174 num_names:ARRAY_SIZE(adq12b_boards),
177 static int adq12b_ai_rinsn(struct comedi_device *dev,
178 struct comedi_subdevice *s, struct comedi_insn *insn,
180 static int adq12b_di_insn_bits(struct comedi_device *dev,
181 struct comedi_subdevice *s,
182 struct comedi_insn *insn, unsigned int *data);
183 static int adq12b_do_insn_bits(struct comedi_device *dev,
184 struct comedi_subdevice *s,
185 struct comedi_insn *insn, unsigned int *data);
188 * Attach is called by the Comedi core to configure the driver
189 * for a particular board. If you specified a board_name array
190 * in the driver structure, dev->board_ptr contains that
193 static int adq12b_attach(struct comedi_device *dev, struct comedi_devconfig *it)
195 struct comedi_subdevice *s;
196 unsigned long iobase;
197 int unipolar, differential;
199 iobase = it->options[0];
200 unipolar = it->options[1];
201 differential = it->options[2];
203 printk("comedi%d: adq12b called with options base=0x%03lx, %s and %s\n",
204 dev->minor, iobase, (unipolar == 1) ? "unipolar" : "bipolar",
205 (differential == 1) ? "differential" : "single-ended");
207 /* if no address was specified, try the default 0x300 */
210 ("comedi%d: adq12b warning: I/O base address not specified. Trying the default 0x300.\n",
215 printk("comedi%d: adq12b: 0x%04lx ", dev->minor, iobase);
216 if (!request_region(iobase, ADQ12B_SIZE, "adq12b")) {
217 printk("I/O port conflict\n");
220 dev->iobase = iobase;
223 * Initialize dev->board_name. Note that we can use the "thisboard"
224 * macro now, since we just initialized it in the last line.
226 dev->board_name = thisboard->name;
229 * Allocate the private structure area. alloc_private() is a
230 * convenient macro defined in comedidev.h.
232 if (alloc_private(dev, sizeof(struct adq12b_private)) < 0)
235 /* fill in devpriv structure */
236 devpriv->unipolar = unipolar;
237 devpriv->differential = differential;
238 devpriv->digital_state = 0;
239 /* initialize channel and range to -1 so we make sure we always write
240 at least once to the CTREG in the instruction */
241 devpriv->last_channel = -1;
242 devpriv->last_range = -1;
245 * Allocate the subdevice structures. alloc_subdevice() is a
246 * convenient macro defined in comedidev.h.
248 if (alloc_subdevices(dev, 3) < 0)
251 s = dev->subdevices + 0;
252 /* analog input subdevice */
253 s->type = COMEDI_SUBD_AI;
255 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_DIFF;
256 s->n_chan = thisboard->ai_diff_chans;
258 s->subdev_flags = SDF_READABLE | SDF_GROUND;
259 s->n_chan = thisboard->ai_se_chans;
263 s->range_table = &range_adq12b_ai_unipolar;
265 s->range_table = &range_adq12b_ai_bipolar;
268 s->maxdata = (1 << thisboard->ai_bits) - 1;
270 s->len_chanlist = 4; /* This is the maximum chanlist length that
271 the board can handle */
272 s->insn_read = adq12b_ai_rinsn;
274 s = dev->subdevices + 1;
275 /* digital input subdevice */
276 s->type = COMEDI_SUBD_DI;
277 s->subdev_flags = SDF_READABLE;
278 s->n_chan = thisboard->di_chans;
280 s->range_table = &range_digital;
281 s->insn_bits = adq12b_di_insn_bits;
283 s = dev->subdevices + 2;
284 /* digital output subdevice */
285 s->type = COMEDI_SUBD_DO;
286 s->subdev_flags = SDF_WRITABLE;
287 s->n_chan = thisboard->do_chans;
289 s->range_table = &range_digital;
290 s->insn_bits = adq12b_do_insn_bits;
292 printk("attached\n");
298 * _detach is called to deconfigure a device. It should deallocate
300 * This function is also called when _attach() fails, so it should be
301 * careful not to release resources that were not necessarily
302 * allocated by _attach(). dev->private and dev->subdevices are
303 * deallocated automatically by the core.
305 static int adq12b_detach(struct comedi_device *dev)
308 release_region(dev->iobase, ADQ12B_SIZE);
312 printk("comedi%d: adq12b: removed\n", dev->minor);
318 * "instructions" read/write data in "one-shot" or "software-triggered"
322 static int adq12b_ai_rinsn(struct comedi_device *dev,
323 struct comedi_subdevice *s, struct comedi_insn *insn,
328 unsigned char hi, lo, status;
330 /* change channel and range only if it is different from the previous */
331 range = CR_RANGE(insn->chanspec);
332 channel = CR_CHAN(insn->chanspec);
333 if (channel != devpriv->last_channel || range != devpriv->last_range) {
334 outb((range << 4) | channel, dev->iobase + ADQ12B_CTREG);
335 udelay(50); /* wait for the mux to settle */
338 /* trigger conversion */
339 status = inb(dev->iobase + ADQ12B_ADLOW);
341 /* convert n samples */
342 for (n = 0; n < insn->n; n++) {
344 /* wait for end of convertion */
348 status = inb(dev->iobase + ADQ12B_STINR);
349 status = status & ADQ12B_EOC;
350 } while (status == 0 && ++i < TIMEOUT);
351 /* } while (++i < 10); */
354 hi = inb(dev->iobase + ADQ12B_ADHIG);
355 lo = inb(dev->iobase + ADQ12B_ADLOW);
357 /* printk("debug: chan=%d range=%d status=%d hi=%d lo=%d\n", channel, range, status, hi, lo); */
358 data[n] = (hi << 8) | lo;
362 /* return the number of samples read/written */
366 static int adq12b_di_insn_bits(struct comedi_device *dev,
367 struct comedi_subdevice *s,
368 struct comedi_insn *insn, unsigned int *data)
371 /* only bits 0-4 have information about digital inputs */
372 data[1] = (inb(dev->iobase + ADQ12B_STINR) & (0x1f));
377 static int adq12b_do_insn_bits(struct comedi_device *dev,
378 struct comedi_subdevice *s,
379 struct comedi_insn *insn, unsigned int *data)
383 for (channel = 0; channel < 8; channel++)
384 if (((data[0] >> channel) & 0x01) != 0)
385 outb((((data[1] >> channel) & 0x01) << 3) | channel,
386 dev->iobase + ADQ12B_OUTBR);
388 /* store information to retrieve when asked for reading */
390 devpriv->digital_state &= ~data[0];
391 devpriv->digital_state |= (data[0] & data[1]);
394 data[1] = devpriv->digital_state;
400 * A convenient macro that defines init_module() and cleanup_module(),
403 COMEDI_INITCLEANUP(driver_adq12b);