2 comedi/drivers/ni_at_ao.c
3 Driver for NI AT-AO-6/10 boards
5 COMEDI - Linux Control and Measurement Device Interface
6 Copyright (C) 2000,2002 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: National Instruments AT-AO-6/10
26 Devices: [National Instruments] AT-AO-6 (at-ao-6), AT-AO-10 (at-ao-10)
29 Updated: Sun Dec 26 12:26:28 EST 2004
31 Configuration options:
32 [0] - I/O port base address
35 [3] - analog output range, set by jumpers on hardware (0 for -10 to 10V bipolar, 1 for 0V to 10V unipolar)
39 * Register-level programming information can be found in NI
40 * document 320379.pdf.
43 #include "../comedidev.h"
45 #include <linux/ioport.h>
48 /* registers with _2_ are accessed when GRP2WR is set in CFG1 */
50 #define ATAO_SIZE 0x20
52 #define ATAO_2_DMATCCLR 0x00 /* W 16 */
53 #define ATAO_DIN 0x00 /* R 16 */
54 #define ATAO_DOUT 0x00 /* W 16 */
56 #define ATAO_CFG2 0x02 /* W 16 */
74 #define ATAO_2_INT1CLR 0x02 /* W 16 */
76 #define ATAO_CFG3 0x04 /* W 16 */
77 #define DMAMODE 0x0040
80 #define DOUTEN2 0x0008
81 #define DOUTEN1 0x0004
85 #define ATAO_2_INT2CLR 0x04 /* W 16 */
87 #define ATAO_82C53_BASE 0x06 /* RW 8 */
89 #define ATAO_82C53_CNTR1 0x06 /* RW 8 */
90 #define ATAO_82C53_CNTR2 0x07 /* RW 8 */
91 #define ATAO_82C53_CNTR3 0x08 /* RW 8 */
92 #define ATAO_82C53_CNTRCMD 0x09 /* W 8 */
101 /* read-back command */
117 #define ATAO_2_RTSISHFT 0x06 /* W 8 */
120 #define ATAO_2_RTSISTRB 0x07 /* W 8 */
122 #define ATAO_CFG1 0x0a /* W 16 */
123 #define EXTINT2EN 0x8000
124 #define EXTINT1EN 0x4000
125 #define CNTINT2EN 0x2000
126 #define CNTINT1EN 0x1000
127 #define TCINTEN 0x0800
128 #define CNT1SRC 0x0400
129 #define CNT2SRC 0x0200
130 #define FIFOEN 0x0100
131 #define GRP2WR 0x0080
132 #define EXTUPDEN 0x0040
135 #define CH_mask 0x000f
136 #define ATAO_STATUS 0x0a /* R 16 */
143 #define PROMOUT 0x0001
145 #define ATAO_FIFO_WRITE 0x0c /* W 16 */
146 #define ATAO_FIFO_CLEAR 0x0c /* R 16 */
147 #define ATAO_DACn(x) (0x0c + 2*(x)) /* W */
150 * Board descriptions for two imaginary boards. Describing the
151 * boards in this way is optional, and completely driver-dependent.
152 * Some drivers use arrays such as this, other do not.
159 static const struct atao_board atao_boards
[] = {
170 #define thisboard ((struct atao_board *)dev->board_ptr)
172 struct atao_private
{
178 /* Used for AO readback */
179 unsigned int ao_readback
[10];
182 #define devpriv ((struct atao_private *)dev->private)
184 static int atao_attach(struct comedi_device
* dev
, struct comedi_devconfig
* it
);
185 static int atao_detach(struct comedi_device
* dev
);
186 static struct comedi_driver driver_atao
= {
187 driver_name
:"ni_at_ao",
191 board_name
:&atao_boards
[0].name
,
192 offset
:sizeof(struct atao_board
),
193 num_names
:sizeof(atao_boards
) / sizeof(struct atao_board
),
196 COMEDI_INITCLEANUP(driver_atao
);
198 static void atao_reset(struct comedi_device
* dev
);
200 static int atao_ao_winsn(struct comedi_device
* dev
, struct comedi_subdevice
* s
,
201 struct comedi_insn
* insn
, unsigned int * data
);
202 static int atao_ao_rinsn(struct comedi_device
* dev
, struct comedi_subdevice
* s
,
203 struct comedi_insn
* insn
, unsigned int * data
);
204 static int atao_dio_insn_bits(struct comedi_device
* dev
, struct comedi_subdevice
* s
,
205 struct comedi_insn
* insn
, unsigned int * data
);
206 static int atao_dio_insn_config(struct comedi_device
* dev
, struct comedi_subdevice
* s
,
207 struct comedi_insn
* insn
, unsigned int * data
);
208 static int atao_calib_insn_read(struct comedi_device
* dev
, struct comedi_subdevice
* s
,
209 struct comedi_insn
* insn
, unsigned int * data
);
210 static int atao_calib_insn_write(struct comedi_device
* dev
, struct comedi_subdevice
* s
,
211 struct comedi_insn
* insn
, unsigned int * data
);
213 static int atao_attach(struct comedi_device
* dev
, struct comedi_devconfig
* it
)
215 struct comedi_subdevice
*s
;
216 unsigned long iobase
;
219 iobase
= it
->options
[0];
222 ao_unipolar
= it
->options
[3];
224 printk("comedi%d: ni_at_ao: 0x%04lx", dev
->minor
, iobase
);
226 if (!request_region(iobase
, ATAO_SIZE
, "ni_at_ao")) {
227 printk(" I/O port conflict\n");
230 dev
->iobase
= iobase
;
232 //dev->board_ptr = atao_probe(dev);
234 dev
->board_name
= thisboard
->name
;
236 if (alloc_private(dev
, sizeof(struct atao_private
)) < 0)
239 if (alloc_subdevices(dev
, 4) < 0)
242 s
= dev
->subdevices
+ 0;
243 /* analog output subdevice */
244 s
->type
= COMEDI_SUBD_AO
;
245 s
->subdev_flags
= SDF_WRITABLE
;
246 s
->n_chan
= thisboard
->n_ao_chans
;
247 s
->maxdata
= (1 << 12) - 1;
249 s
->range_table
= &range_unipolar10
;
251 s
->range_table
= &range_bipolar10
;
252 s
->insn_write
= &atao_ao_winsn
;
253 s
->insn_read
= &atao_ao_rinsn
;
255 s
= dev
->subdevices
+ 1;
256 /* digital i/o subdevice */
257 s
->type
= COMEDI_SUBD_DIO
;
258 s
->subdev_flags
= SDF_READABLE
| SDF_WRITABLE
;
261 s
->range_table
= &range_digital
;
262 s
->insn_bits
= atao_dio_insn_bits
;
263 s
->insn_config
= atao_dio_insn_config
;
265 s
= dev
->subdevices
+ 2;
266 /* caldac subdevice */
267 s
->type
= COMEDI_SUBD_CALIB
;
268 s
->subdev_flags
= SDF_WRITABLE
| SDF_INTERNAL
;
271 s
->insn_read
= atao_calib_insn_read
;
272 s
->insn_write
= atao_calib_insn_write
;
274 s
= dev
->subdevices
+ 3;
275 /* eeprom subdevice */
276 //s->type=COMEDI_SUBD_EEPROM;
277 s
->type
= COMEDI_SUBD_UNUSED
;
286 static int atao_detach(struct comedi_device
* dev
)
288 printk("comedi%d: atao: remove\n", dev
->minor
);
291 release_region(dev
->iobase
, ATAO_SIZE
);
296 static void atao_reset(struct comedi_device
* dev
)
298 /* This is the reset sequence described in the manual */
301 outw(devpriv
->cfg1
, dev
->iobase
+ ATAO_CFG1
);
303 outb(RWSEL0
| MODESEL2
, dev
->iobase
+ ATAO_82C53_CNTRCMD
);
304 outb(0x03, dev
->iobase
+ ATAO_82C53_CNTR1
);
305 outb(CNTRSEL0
| RWSEL0
| MODESEL2
, dev
->iobase
+ ATAO_82C53_CNTRCMD
);
308 outw(devpriv
->cfg2
, dev
->iobase
+ ATAO_CFG2
);
311 outw(devpriv
->cfg3
, dev
->iobase
+ ATAO_CFG3
);
313 inw(dev
->iobase
+ ATAO_FIFO_CLEAR
);
315 devpriv
->cfg1
|= GRP2WR
;
316 outw(devpriv
->cfg1
, dev
->iobase
+ ATAO_CFG1
);
318 outw(0, dev
->iobase
+ ATAO_2_INT1CLR
);
319 outw(0, dev
->iobase
+ ATAO_2_INT2CLR
);
320 outw(0, dev
->iobase
+ ATAO_2_DMATCCLR
);
322 devpriv
->cfg1
&= ~GRP2WR
;
323 outw(devpriv
->cfg1
, dev
->iobase
+ ATAO_CFG1
);
326 static int atao_ao_winsn(struct comedi_device
* dev
, struct comedi_subdevice
* s
,
327 struct comedi_insn
* insn
, unsigned int * data
)
330 int chan
= CR_CHAN(insn
->chanspec
);
333 for (i
= 0; i
< insn
->n
; i
++) {
334 bits
= data
[i
] - 0x800;
336 devpriv
->cfg1
|= GRP2WR
;
337 outw(devpriv
->cfg1
, dev
->iobase
+ ATAO_CFG1
);
339 outw(bits
, dev
->iobase
+ ATAO_DACn(chan
));
341 devpriv
->cfg1
&= ~GRP2WR
;
342 outw(devpriv
->cfg1
, dev
->iobase
+ ATAO_CFG1
);
344 devpriv
->ao_readback
[chan
] = data
[i
];
350 static int atao_ao_rinsn(struct comedi_device
* dev
, struct comedi_subdevice
* s
,
351 struct comedi_insn
* insn
, unsigned int * data
)
354 int chan
= CR_CHAN(insn
->chanspec
);
356 for (i
= 0; i
< insn
->n
; i
++)
357 data
[i
] = devpriv
->ao_readback
[chan
];
362 static int atao_dio_insn_bits(struct comedi_device
* dev
, struct comedi_subdevice
* s
,
363 struct comedi_insn
* insn
, unsigned int * data
)
369 s
->state
&= ~data
[0];
370 s
->state
|= data
[0] & data
[1];
371 outw(s
->state
, dev
->iobase
+ ATAO_DOUT
);
374 data
[1] = inw(dev
->iobase
+ ATAO_DIN
);
379 static int atao_dio_insn_config(struct comedi_device
* dev
, struct comedi_subdevice
* s
,
380 struct comedi_insn
* insn
, unsigned int * data
)
382 int chan
= CR_CHAN(insn
->chanspec
);
383 unsigned int mask
, bit
;
385 /* The input or output configuration of each digital line is
386 * configured by a special insn_config instruction. chanspec
387 * contains the channel to be changed, and data[0] contains the
388 * value COMEDI_INPUT or COMEDI_OUTPUT. */
390 mask
= (chan
< 4) ? 0x0f : 0xf0;
391 bit
= (chan
< 4) ? DOUTEN1
: DOUTEN2
;
394 case INSN_CONFIG_DIO_OUTPUT
:
396 devpriv
->cfg3
|= bit
;
398 case INSN_CONFIG_DIO_INPUT
:
400 devpriv
->cfg3
&= ~bit
;
402 case INSN_CONFIG_DIO_QUERY
:
405 io_bits
& (1 << chan
)) ? COMEDI_OUTPUT
: COMEDI_INPUT
;
413 outw(devpriv
->cfg3
, dev
->iobase
+ ATAO_CFG3
);
419 * Figure 2-1 in the manual shows 3 chips labeled DAC8800, which
420 * are 8-channel 8-bit DACs. These are most likely the calibration
421 * DACs. It is not explicitly stated in the manual how to access
422 * the caldacs, but we can guess.
424 static int atao_calib_insn_read(struct comedi_device
* dev
, struct comedi_subdevice
* s
,
425 struct comedi_insn
* insn
, unsigned int * data
)
428 for (i
= 0; i
< insn
->n
; i
++) {
429 data
[i
] = 0; /* XXX */
434 static int atao_calib_insn_write(struct comedi_device
* dev
, struct comedi_subdevice
* s
,
435 struct comedi_insn
* insn
, unsigned int * data
)
437 unsigned int bitstring
, bit
;
438 unsigned int chan
= CR_CHAN(insn
->chanspec
);
440 bitstring
= ((chan
& 0x7) << 8) | (data
[insn
->n
- 1] & 0xff);
442 for (bit
= 1 << (11 - 1); bit
; bit
>>= 1) {
443 outw(devpriv
->cfg2
| ((bit
& bitstring
) ? SDATA
: 0),
444 dev
->iobase
+ ATAO_CFG2
);
445 outw(devpriv
->cfg2
| SCLK
| ((bit
& bitstring
) ? SDATA
: 0),
446 dev
->iobase
+ ATAO_CFG2
);
448 /* strobe the appropriate caldac */
449 outw(devpriv
->cfg2
| (((chan
>> 3) + 1) << 14),
450 dev
->iobase
+ ATAO_CFG2
);
451 outw(devpriv
->cfg2
, dev
->iobase
+ ATAO_CFG2
);