Commit | Line | Data |
---|---|---|
27b3f921 JT |
1 | /* |
2 | comedi/drivers/adq12b.c | |
3 | driver for MicroAxial ADQ12-B data acquisition and control card | |
4 | ||
5 | COMEDI - Linux Control and Measurement Device Interface | |
6 | Copyright (C) 2000 David A. Schleef <ds@schleef.org> | |
7 | ||
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. | |
12 | ||
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. | |
27b3f921 JT |
17 | */ |
18 | /* | |
19 | Driver: adq12b | |
20 | Description: driver for MicroAxial ADQ12-B data acquisition and control card | |
21 | Devices: [MicroAxial] ADQ12-B (adq12b) | |
22 | Author: jeremy theler <thelerg@ib.cnea.gov.ar> | |
23 | Updated: Thu, 21 Feb 2008 02:56:27 -0300 | |
24 | Status: works | |
25 | ||
26 | Driver for the acquisition card ADQ12-B (without any add-on). | |
27 | ||
28 | - Analog input is subdevice 0 (16 channels single-ended or 8 differential) | |
29 | - Digital input is subdevice 1 (5 channels) | |
30 | - Digital output is subdevice 1 (8 channels) | |
31 | - The PACER is not supported in this version | |
32 | ||
33 | If you do not specify any options, they will default to | |
34 | ||
35 | # comedi_config /dev/comedi0 adq12b 0x300,0,0 | |
36 | ||
37 | option 1: I/O base address. The following table is provided as a help | |
38 | of the hardware jumpers. | |
39 | ||
d2e01434 BA |
40 | address jumper JADR |
41 | 0x300 1 (factory default) | |
42 | 0x320 2 | |
43 | 0x340 3 | |
44 | 0x360 4 | |
45 | 0x380 5 | |
46 | 0x3A0 6 | |
27b3f921 JT |
47 | |
48 | option 2: unipolar/bipolar ADC selection: 0 -> bipolar, 1 -> unipolar | |
49 | ||
d2e01434 BA |
50 | selection comedi_config option JUB |
51 | bipolar 0 2-3 (factory default) | |
52 | unipolar 1 1-2 | |
27b3f921 JT |
53 | |
54 | option 3: single-ended/differential AI selection: 0 -> SE, 1 -> differential | |
55 | ||
d2e01434 | 56 | selection comedi_config option JCHA JCHB |
27b3f921 JT |
57 | single-ended 0 1-2 1-2 (factory default) |
58 | differential 1 2-3 2-3 | |
59 | ||
27b3f921 JT |
60 | written by jeremy theler <thelerg@ib.cnea.gov.ar> |
61 | ||
62 | instituto balseiro | |
25985edc | 63 | commission nacional de energia atomica |
27b3f921 JT |
64 | universidad nacional de cuyo |
65 | argentina | |
66 | ||
67 | 21-feb-2008 | |
68 | + changed supported devices string (missused the [] and ()) | |
69 | ||
70 | 13-oct-2007 | |
71 | + first try | |
72 | ||
73 | ||
74 | */ | |
75 | ||
ce157f80 HS |
76 | #include <linux/module.h> |
77 | #include <linux/delay.h> | |
78 | ||
27b3f921 JT |
79 | #include "../comedidev.h" |
80 | ||
56e9e166 | 81 | /* address scheme (page 2.17 of the manual) */ |
27b3f921 JT |
82 | #define ADQ12B_SIZE 16 |
83 | ||
84 | #define ADQ12B_CTREG 0x00 | |
85 | #define ADQ12B_STINR 0x00 | |
86 | #define ADQ12B_OUTBR 0x04 | |
87 | #define ADQ12B_ADLOW 0x08 | |
88 | #define ADQ12B_ADHIG 0x09 | |
89 | #define ADQ12B_CONT0 0x0c | |
90 | #define ADQ12B_CONT1 0x0d | |
91 | #define ADQ12B_CONT2 0x0e | |
92 | #define ADQ12B_COWORD 0x0f | |
93 | ||
56e9e166 | 94 | /* mask of the bit at STINR to check end of conversion */ |
27b3f921 JT |
95 | #define ADQ12B_EOC 0x20 |
96 | ||
97 | #define TIMEOUT 20 | |
98 | ||
56e9e166 | 99 | /* available ranges through the PGA gains */ |
9ced1de6 | 100 | static const struct comedi_lrange range_adq12b_ai_bipolar = { 4, { |
0a85b6f0 MT |
101 | BIP_RANGE(5), |
102 | BIP_RANGE(2), | |
103 | BIP_RANGE(1), | |
104 | BIP_RANGE(0.5) | |
105 | } | |
106 | }; | |
27b3f921 | 107 | |
9ced1de6 | 108 | static const struct comedi_lrange range_adq12b_ai_unipolar = { 4, { |
0a85b6f0 MT |
109 | UNI_RANGE(5), |
110 | UNI_RANGE(2), | |
111 | UNI_RANGE(1), | |
112 | UNI_RANGE | |
113 | (0.5) | |
114 | } | |
115 | }; | |
27b3f921 | 116 | |
1ade3157 | 117 | struct adq12b_private { |
0a85b6f0 MT |
118 | int unipolar; /* option 2 of comedi_config (1 is iobase) */ |
119 | int differential; /* option 3 of comedi_config */ | |
120 | int last_channel; | |
121 | int last_range; | |
1ade3157 | 122 | }; |
27b3f921 | 123 | |
27b3f921 | 124 | /* |
632d391b HS |
125 | * "instructions" read/write data in "one-shot" or "software-triggered" |
126 | * mode. | |
27b3f921 | 127 | */ |
27b3f921 | 128 | |
0a85b6f0 MT |
129 | static int adq12b_ai_rinsn(struct comedi_device *dev, |
130 | struct comedi_subdevice *s, struct comedi_insn *insn, | |
632d391b HS |
131 | unsigned int *data) |
132 | { | |
5c1eb385 | 133 | struct adq12b_private *devpriv = dev->private; |
632d391b HS |
134 | int n, i; |
135 | int range, channel; | |
136 | unsigned char hi, lo, status; | |
137 | ||
138 | /* change channel and range only if it is different from the previous */ | |
139 | range = CR_RANGE(insn->chanspec); | |
140 | channel = CR_CHAN(insn->chanspec); | |
141 | if (channel != devpriv->last_channel || range != devpriv->last_range) { | |
142 | outb((range << 4) | channel, dev->iobase + ADQ12B_CTREG); | |
143 | udelay(50); /* wait for the mux to settle */ | |
144 | } | |
145 | ||
146 | /* trigger conversion */ | |
147 | status = inb(dev->iobase + ADQ12B_ADLOW); | |
148 | ||
149 | /* convert n samples */ | |
150 | for (n = 0; n < insn->n; n++) { | |
151 | ||
152 | /* wait for end of conversion */ | |
153 | i = 0; | |
154 | do { | |
155 | /* udelay(1); */ | |
156 | status = inb(dev->iobase + ADQ12B_STINR); | |
157 | status = status & ADQ12B_EOC; | |
158 | } while (status == 0 && ++i < TIMEOUT); | |
159 | /* } while (++i < 10); */ | |
160 | ||
161 | /* read data */ | |
162 | hi = inb(dev->iobase + ADQ12B_ADHIG); | |
163 | lo = inb(dev->iobase + ADQ12B_ADLOW); | |
164 | ||
165 | /* printk("debug: chan=%d range=%d status=%d hi=%d lo=%d\n", | |
166 | channel, range, status, hi, lo); */ | |
167 | data[n] = (hi << 8) | lo; | |
168 | ||
169 | } | |
170 | ||
171 | /* return the number of samples read/written */ | |
172 | return n; | |
173 | } | |
174 | ||
0a85b6f0 MT |
175 | static int adq12b_di_insn_bits(struct comedi_device *dev, |
176 | struct comedi_subdevice *s, | |
632d391b HS |
177 | struct comedi_insn *insn, unsigned int *data) |
178 | { | |
179 | ||
180 | /* only bits 0-4 have information about digital inputs */ | |
181 | data[1] = (inb(dev->iobase + ADQ12B_STINR) & (0x1f)); | |
182 | ||
a2714e3e | 183 | return insn->n; |
632d391b HS |
184 | } |
185 | ||
0a85b6f0 MT |
186 | static int adq12b_do_insn_bits(struct comedi_device *dev, |
187 | struct comedi_subdevice *s, | |
b2c20d15 HS |
188 | struct comedi_insn *insn, |
189 | unsigned int *data) | |
632d391b | 190 | { |
b2c20d15 HS |
191 | unsigned int mask; |
192 | unsigned int chan; | |
193 | unsigned int val; | |
194 | ||
195 | mask = comedi_dio_update_state(s, data); | |
196 | if (mask) { | |
197 | for (chan = 0; chan < 8; chan++) { | |
198 | if ((mask >> chan) & 0x01) { | |
199 | val = (s->state >> chan) & 0x01; | |
200 | outb((val << 3) | chan, | |
201 | dev->iobase + ADQ12B_OUTBR); | |
202 | } | |
203 | } | |
632d391b HS |
204 | } |
205 | ||
b2c20d15 | 206 | data[1] = s->state; |
632d391b | 207 | |
a2714e3e | 208 | return insn->n; |
632d391b | 209 | } |
27b3f921 | 210 | |
f7cbd7aa | 211 | static int adq12b_attach(struct comedi_device *dev, struct comedi_devconfig *it) |
27b3f921 | 212 | { |
5c1eb385 | 213 | struct adq12b_private *devpriv; |
0a85b6f0 | 214 | struct comedi_subdevice *s; |
8b6c5694 | 215 | int ret; |
0a85b6f0 | 216 | |
b8c5d2ca HS |
217 | ret = comedi_request_region(dev, it->options[0], ADQ12B_SIZE); |
218 | if (ret) | |
219 | return ret; | |
27b3f921 | 220 | |
0bdab509 | 221 | devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv)); |
c34fa261 HS |
222 | if (!devpriv) |
223 | return -ENOMEM; | |
27b3f921 | 224 | |
f19df8b0 HS |
225 | devpriv->unipolar = it->options[1]; |
226 | devpriv->differential = it->options[2]; | |
5c1eb385 HS |
227 | /* |
228 | * initialize channel and range to -1 so we make sure we | |
229 | * always write at least once to the CTREG in the instruction | |
230 | */ | |
0a85b6f0 MT |
231 | devpriv->last_channel = -1; |
232 | devpriv->last_range = -1; | |
27b3f921 | 233 | |
8b6c5694 HS |
234 | ret = comedi_alloc_subdevices(dev, 3); |
235 | if (ret) | |
236 | return ret; | |
0a85b6f0 | 237 | |
4fe92e16 | 238 | s = &dev->subdevices[0]; |
0a85b6f0 MT |
239 | /* analog input subdevice */ |
240 | s->type = COMEDI_SUBD_AI; | |
f19df8b0 | 241 | if (devpriv->differential) { |
0a85b6f0 | 242 | s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_DIFF; |
7195ea04 | 243 | s->n_chan = 8; |
0a85b6f0 MT |
244 | } else { |
245 | s->subdev_flags = SDF_READABLE | SDF_GROUND; | |
7195ea04 | 246 | s->n_chan = 16; |
0a85b6f0 MT |
247 | } |
248 | ||
f19df8b0 | 249 | if (devpriv->unipolar) |
0a85b6f0 | 250 | s->range_table = &range_adq12b_ai_unipolar; |
d2e01434 | 251 | else |
0a85b6f0 | 252 | s->range_table = &range_adq12b_ai_bipolar; |
0a85b6f0 | 253 | |
7195ea04 | 254 | s->maxdata = 0xfff; |
0a85b6f0 MT |
255 | |
256 | s->len_chanlist = 4; /* This is the maximum chanlist length that | |
257 | the board can handle */ | |
258 | s->insn_read = adq12b_ai_rinsn; | |
259 | ||
4fe92e16 | 260 | s = &dev->subdevices[1]; |
0a85b6f0 MT |
261 | /* digital input subdevice */ |
262 | s->type = COMEDI_SUBD_DI; | |
263 | s->subdev_flags = SDF_READABLE; | |
7195ea04 | 264 | s->n_chan = 5; |
0a85b6f0 MT |
265 | s->maxdata = 1; |
266 | s->range_table = &range_digital; | |
267 | s->insn_bits = adq12b_di_insn_bits; | |
268 | ||
4fe92e16 | 269 | s = &dev->subdevices[2]; |
0a85b6f0 MT |
270 | /* digital output subdevice */ |
271 | s->type = COMEDI_SUBD_DO; | |
272 | s->subdev_flags = SDF_WRITABLE; | |
7195ea04 | 273 | s->n_chan = 8; |
0a85b6f0 MT |
274 | s->maxdata = 1; |
275 | s->range_table = &range_digital; | |
276 | s->insn_bits = adq12b_do_insn_bits; | |
277 | ||
0a85b6f0 | 278 | return 0; |
27b3f921 JT |
279 | } |
280 | ||
294f930d | 281 | static struct comedi_driver adq12b_driver = { |
632d391b HS |
282 | .driver_name = "adq12b", |
283 | .module = THIS_MODULE, | |
284 | .attach = adq12b_attach, | |
21208519 | 285 | .detach = comedi_legacy_detach, |
632d391b | 286 | }; |
294f930d | 287 | module_comedi_driver(adq12b_driver); |
90f703d3 AT |
288 | |
289 | MODULE_AUTHOR("Comedi http://www.comedi.org"); | |
290 | MODULE_DESCRIPTION("Comedi low-level driver"); | |
291 | MODULE_LICENSE("GPL"); |