2 comedi/drivers/ni_mio_common.c
3 Hardware driver for DAQ-STC based boards
5 COMEDI - Linux Control and Measurement Device Interface
6 Copyright (C) 1997-2001 David A. Schleef <ds@schleef.org>
7 Copyright (C) 2002, 2003 Frank Mori Hess <fmhess@users.sourceforge.net
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 This file is meant to be included by another file, e.g.,
27 ni_atmio.c or ni_pcimio.c.
29 Interrupt support originally added by Truxton Fulton
32 References (from ftp://ftp.natinst.com/support/manuals):
34 340747b.pdf AT-MIO E series Register Level Programmer Manual
35 341079b.pdf PCI E Series RLPM
36 340934b.pdf DAQ-STC reference manual
37 67xx and 611x registers (from http://www.ni.com/pdf/daq/us)
40 Other possibly relevant info:
42 320517c.pdf User manual (obsolete)
43 320517f.pdf User manual (new)
45 320906c.pdf maximum signal ratings
47 321791a.pdf discontinuation of at-mio-16e-10 rev. c
48 321808a.pdf about at-mio-16e-10 rev P
49 321837a.pdf discontinuation of at-mio-16de-10 rev d
50 321838a.pdf about at-mio-16de-10 rev N
54 - the interrupt routine needs to be cleaned up
55 - many printk's need to be changed to rt_printk()
58 //#define DEBUG_INTERRUPT
59 //#define DEBUG_STATUS_A
60 //#define DEBUG_STATUS_B
64 #include "comedi_fc.h"
67 #define MDPRINTK(format,args...)
72 #define NI_TIMEOUT 1000
74 /* Note: this table must match the ai_gain_* definitions */
75 static short ni_gainlkup[][16]={
77 { 0, 1, 2, 3, 4, 5, 6, 7, 0x100, 0x101, 0x102, 0x103, 0x104, 0x105,
80 { 1, 2, 4, 7, 0x101, 0x102, 0x104, 0x107 },
82 { 1, 2, 3, 4, 5, 6, 7, 0x101, 0x102, 0x103, 0x104, 0x105, 0x106,
87 { 0x00a, 0x00b, 0x001, 0x002, 0x003, 0x004, 0x005, 0x006 },
88 /* ai_gain_622x FIXME: guesses */
91 { 1, 2, 3, 4, 5, 6, 7}
94 static comedi_lrange range_ni_E_ai={ 16, {
100 RANGE( -0.25, 0.25 ),
102 RANGE( -0.05, 0.05 ),
112 static comedi_lrange range_ni_E_ai_limited={ 8, {
122 static comedi_lrange range_ni_E_ai_limited14={ 14, {
138 static comedi_lrange range_ni_E_ai_bipolar4={ 4, {
142 RANGE( -0.05, 0.05 ),
144 static comedi_lrange range_ni_E_ai_611x={ 8, {
154 static comedi_lrange range_ni_M_ai_622x={ 4, {
160 static comedi_lrange range_ni_M_ai_628x={ 7, {
169 static comedi_lrange range_ni_E_ao_ext = { 4, {
176 static comedi_lrange *ni_range_lkup[]={
178 &range_ni_E_ai_limited,
179 &range_ni_E_ai_limited14,
180 &range_ni_E_ai_bipolar4,
188 static int ni_dio_insn_config(comedi_device *dev,comedi_subdevice *s,
189 comedi_insn *insn,lsampl_t *data);
190 static int ni_dio_insn_bits(comedi_device *dev,comedi_subdevice *s,
191 comedi_insn *insn,lsampl_t *data);
193 static int ni_serial_insn_config(comedi_device *dev,comedi_subdevice *s,
194 comedi_insn *insn,lsampl_t *data);
195 static int ni_serial_hw_readwrite8(comedi_device *dev,comedi_subdevice *s,
196 unsigned char data_out, unsigned char *data_in);
197 static int ni_serial_sw_readwrite8(comedi_device *dev,comedi_subdevice *s,
198 unsigned char data_out, unsigned char *data_in);
200 static int ni_calib_insn_read(comedi_device *dev,comedi_subdevice *s,
201 comedi_insn *insn,lsampl_t *data);
202 static int ni_calib_insn_write(comedi_device *dev,comedi_subdevice *s,
203 comedi_insn *insn,lsampl_t *data);
205 static int ni_eeprom_insn_read(comedi_device *dev,comedi_subdevice *s,
206 comedi_insn *insn,lsampl_t *data);
208 static int ni_pfi_insn_bits(comedi_device *dev,comedi_subdevice *s,
209 comedi_insn *insn,lsampl_t *data);
210 static int ni_pfi_insn_config(comedi_device *dev,comedi_subdevice *s,
211 comedi_insn *insn,lsampl_t *data);
213 static void caldac_setup(comedi_device *dev,comedi_subdevice *s);
214 static int ni_read_eeprom(comedi_device *dev,int addr);
216 #ifdef DEBUG_STATUS_A
217 static void ni_mio_print_status_a(int status);
219 #define ni_mio_print_status_a(a)
221 #ifdef DEBUG_STATUS_B
222 static void ni_mio_print_status_b(int status);
224 #define ni_mio_print_status_b(a)
227 static int ni_ai_reset(comedi_device *dev,comedi_subdevice *s);
229 static void ni_handle_fifo_half_full(comedi_device *dev);
230 static int ni_ao_fifo_half_empty(comedi_device *dev,comedi_subdevice *s);
232 static void ni_handle_fifo_dregs(comedi_device *dev);
233 static int ni_ai_inttrig(comedi_device *dev,comedi_subdevice *s,
234 unsigned int trignum);
235 static void ni_load_channelgain_list(comedi_device *dev,unsigned int n_chan,
237 static void shutdown_ai_command( comedi_device *dev );
239 static int ni_ao_inttrig(comedi_device *dev,comedi_subdevice *s,
240 unsigned int trignum);
242 static int ni_ao_reset(comedi_device *dev,comedi_subdevice *s);
244 static int ni_8255_callback(int dir,int port,int data,unsigned long arg);
246 static int ni_ns_to_timer(int *nanosec,int round_mode);
249 /*GPCT function def's*/
250 static int GPCT_G_Watch(comedi_device *dev, int chan);
252 static void GPCT_Reset(comedi_device *dev, int chan);
253 static void GPCT_Gen_Cont_Pulse(comedi_device *dev, int chan, unsigned int length);
254 static void GPCT_Gen_Single_Pulse(comedi_device *dev, int chan, unsigned int length);
255 static void GPCT_Period_Meas(comedi_device *dev, int chan);
256 static void GPCT_Pulse_Width_Meas(comedi_device *dev, int chan);
257 static void GPCT_Event_Counting(comedi_device *dev,int chan);
258 static int GPCT_Set_Direction(comedi_device *dev,int chan,int direction);
259 static int GPCT_Set_Gate(comedi_device *dev,int chan ,int gate);
260 static int GPCT_Set_Source(comedi_device *dev,int chan ,int source);
262 static int ni_gpct_insn_write(comedi_device *dev,comedi_subdevice *s,
263 comedi_insn *insn,lsampl_t *data);
264 static int ni_gpct_insn_read(comedi_device *dev,comedi_subdevice *s,
265 comedi_insn *insn,lsampl_t *data);
266 static int ni_gpct_insn_config(comedi_device *dev,comedi_subdevice *s,
267 comedi_insn *insn,lsampl_t *data);
269 static int init_cs5529(comedi_device *dev);
270 static int cs5529_do_conversion(comedi_device *dev, unsigned short *data);
271 static int cs5529_ai_insn_read(comedi_device *dev,comedi_subdevice *s,comedi_insn *insn,lsampl_t *data);
272 static unsigned int cs5529_config_read(comedi_device *dev, unsigned int reg_select_bits);
273 static void cs5529_config_write(comedi_device *dev, unsigned int value, unsigned int reg_select_bits);
278 AIMODE_HALF_FULL = 1,
283 #define SERIAL_DISABLED 0
284 #define SERIAL_600NS 600
285 #define SERIAL_1_2US 1200
286 #define SERIAL_10US 10000
288 static const int num_adc_stages_611x = 3;
290 static void handle_a_interrupt(comedi_device *dev,unsigned short status,
291 unsigned int m_status);
292 static void handle_b_interrupt(comedi_device *dev,unsigned short status,
293 unsigned int m_status);
294 static void get_last_sample_611x( comedi_device *dev );
296 //static void mite_handle_interrupt(comedi_device *dev,unsigned int status);
297 static int ni_ai_drain_dma(comedi_device *dev );
300 static void win_out2(comedi_device *dev, uint32_t data, int reg)
302 devpriv->stc_writew(dev, data >> 16, reg);
303 devpriv->stc_writew(dev, data & 0xffff, reg + 1);
306 #define ao_win_out(data,addr) ni_ao_win_outw(dev,data,addr)
307 static inline void ni_ao_win_outw( comedi_device *dev, uint16_t data, int addr )
311 comedi_spin_lock_irqsave(&devpriv->window_lock,flags);
312 ni_writew(addr,AO_Window_Address_611x);
313 ni_writew(data,AO_Window_Data_611x);
314 comedi_spin_unlock_irqrestore(&devpriv->window_lock,flags);
317 static inline void ni_ao_win_outl(comedi_device *dev, uint32_t data, int addr)
321 comedi_spin_lock_irqsave(&devpriv->window_lock,flags);
322 ni_writew(addr,AO_Window_Address_611x);
323 ni_writel(data,AO_Window_Data_611x);
324 comedi_spin_unlock_irqrestore(&devpriv->window_lock,flags);
327 static inline unsigned short ni_ao_win_inw( comedi_device *dev, int addr )
332 comedi_spin_lock_irqsave(&devpriv->window_lock,flags);
333 ni_writew(addr, AO_Window_Address_611x);
334 data = ni_readw(AO_Window_Data_611x);
335 comedi_spin_unlock_irqrestore(&devpriv->window_lock,flags);
339 /* ni_set_bits( ) allows different parts of the ni_mio_common driver to
340 * share registers (such as Interrupt_A_Register) without interfering with
343 * NOTE: the switch/case statements are optimized out for a constant argument
344 * so this is actually quite fast--- If you must wrap another function around this
345 * make it inline to avoid a large speed penalty.
347 * value should only be 1 or 0.
349 static inline void ni_set_bits(comedi_device *dev, int reg, int bits, int value)
353 comedi_spin_lock_irqsave( &devpriv->window_lock, flags );
355 case Interrupt_A_Enable_Register:
357 devpriv->int_a_enable_reg |= bits;
359 devpriv->int_a_enable_reg &= ~bits;
360 comedi_spin_unlock_irqrestore( &devpriv->window_lock, flags );
361 devpriv->stc_writew(dev, devpriv->int_a_enable_reg,Interrupt_A_Enable_Register);
363 case Interrupt_B_Enable_Register:
365 devpriv->int_b_enable_reg |= bits;
367 devpriv->int_b_enable_reg &= ~bits;
368 comedi_spin_unlock_irqrestore( &devpriv->window_lock, flags );
369 devpriv->stc_writew(dev, devpriv->int_b_enable_reg,Interrupt_B_Enable_Register);
371 case IO_Bidirection_Pin_Register:
373 devpriv->io_bidirection_pin_reg |= bits;
375 devpriv->io_bidirection_pin_reg &= ~bits;
376 comedi_spin_unlock_irqrestore( &devpriv->window_lock, flags );
377 devpriv->stc_writew(dev, devpriv->io_bidirection_pin_reg,IO_Bidirection_Pin_Register);
380 printk("Warning ni_set_bits() called with invalid arguments\n");
381 printk("reg is %d\n",reg);
382 comedi_spin_unlock_irqrestore( &devpriv->window_lock, flags );
388 static irqreturn_t ni_E_interrupt(int irq,void *d,struct pt_regs * regs)
390 comedi_device *dev=d;
391 unsigned short a_status;
392 unsigned short b_status;
393 unsigned int m0_status;
394 unsigned int m1_status;
397 struct mite_struct *mite = devpriv->mite;
400 if(dev->attached == 0) return IRQ_NONE;
401 // lock to avoid race with comedi_poll
402 comedi_spin_lock_irqsave(&dev->spinlock, flags);
403 a_status=devpriv->stc_readw(dev, AI_Status_1_Register);
404 b_status=devpriv->stc_readw(dev, AO_Status_1_Register);
406 m0_status=readl(mite->mite_io_addr + MITE_CHSR(AI_DMA_CHAN));
407 m1_status=readl(mite->mite_io_addr + MITE_CHSR(AO_DMA_CHAN));
413 if(a_status&Interrupt_A_St || m0_status & CHSR_INT )
414 handle_a_interrupt(dev, a_status, m0_status);
415 if(b_status&Interrupt_B_St || m1_status & CHSR_INT )
416 handle_b_interrupt(dev, b_status, m1_status);
417 comedi_spin_unlock_irqrestore(&dev->spinlock, flags);
422 static void ni_sync_ai_dma(struct mite_struct *mite, comedi_device *dev)
425 comedi_subdevice *s = dev->subdevices + 0;
426 comedi_async *async = s->async;
427 unsigned int nbytes, old_alloc_count;
428 unsigned int bytes_per_scan = 2 * async->cmd.chanlist_len;
430 old_alloc_count = async->buf_write_alloc_count;
431 // write alloc as much as we can
432 comedi_buf_write_alloc(s->async, s->async->prealloc_bufsz);
434 nbytes = mite_bytes_written_to_memory_lb(mite, AI_DMA_CHAN);
436 if( (int)(mite_bytes_written_to_memory_ub(mite, AI_DMA_CHAN) - old_alloc_count) > 0 ){
437 printk("ni_mio_common: DMA overwrite of free area\n");
439 async->events |= COMEDI_CB_OVERFLOW;
443 count = nbytes - async->buf_write_count;
445 /* it's possible count will be negative due to
446 * conservative value returned by mite_bytes_transferred */
449 comedi_buf_write_free(async, count);
451 async->scan_progress += count;
452 if( async->scan_progress >= bytes_per_scan )
454 async->scan_progress %= bytes_per_scan;
455 async->events |= COMEDI_CB_EOS;
457 async->events |= COMEDI_CB_BLOCK;
460 static void mite_handle_b_linkc(struct mite_struct *mite, comedi_device *dev)
463 comedi_subdevice *s = dev->subdevices + 1;
464 comedi_async *async = s->async;
465 u32 nbytes_ub, nbytes_lb;
466 unsigned int new_write_count;
467 u32 stop_count = async->cmd.stop_arg * sizeof(sampl_t);
469 writel(CHOR_CLRLC, mite->mite_io_addr + MITE_CHOR(AO_DMA_CHAN));
471 new_write_count = async->buf_write_count;
473 nbytes_lb = mite_bytes_read_from_memory_lb(mite, AO_DMA_CHAN);
474 if(async->cmd.stop_src == TRIG_COUNT &&
475 (int) (nbytes_lb - stop_count) > 0)
476 nbytes_lb = stop_count;
478 nbytes_ub = mite_bytes_read_from_memory_ub(mite, AO_DMA_CHAN);
479 if(async->cmd.stop_src == TRIG_COUNT &&
480 (int) (nbytes_ub - stop_count) > 0)
481 nbytes_ub = stop_count;
482 if((int)(nbytes_ub - devpriv->last_buf_write_count) > 0){
483 rt_printk("ni_mio_common: DMA underrun\n");
485 async->events |= COMEDI_CB_OVERFLOW;
489 devpriv->last_buf_write_count = new_write_count;
491 count = nbytes_lb - async->buf_read_count;
495 comedi_buf_read_free(async, count);
497 async->events |= COMEDI_CB_BLOCK;
500 static int ni_ao_wait_for_dma_load( comedi_device *dev )
502 static const int timeout = 10000;
505 for(i = 0; i < timeout; i++)
507 unsigned short b_status;
509 b_status = devpriv->stc_readw(dev, AO_Status_1_Register );
510 if( b_status & AO_FIFO_Half_Full_St )
512 /* if we poll too often, the pci bus activity seems
513 to slow the dma transfer down */
518 comedi_error(dev, "timed out waiting for dma load");
525 static void ni_handle_eos(comedi_device *dev, comedi_subdevice *s)
527 if(devpriv->aimode == AIMODE_SCAN)
530 static const int timeout = 10;
533 for(i = 0; i < timeout; i++)
535 ni_sync_ai_dma(devpriv->mite, dev);
536 if((s->async->events & COMEDI_CB_EOS)) break;
540 ni_handle_fifo_dregs(dev);
541 s->async->events |= COMEDI_CB_EOS;
544 /* handle special case of single scan using AI_End_On_End_Of_Scan */
545 if((devpriv->ai_cmd2 & AI_End_On_End_Of_Scan)){
546 shutdown_ai_command( dev );
551 static void shutdown_ai_command( comedi_device *dev )
553 comedi_subdevice *s = dev->subdevices + 0;
556 ni_ai_drain_dma( dev );
557 mite_dma_disarm(devpriv->mite, AI_DMA_CHAN);
559 ni_handle_fifo_dregs(dev);
560 get_last_sample_611x(dev);
562 ni_set_bits(dev, Interrupt_A_Enable_Register,
563 AI_SC_TC_Interrupt_Enable | AI_START1_Interrupt_Enable|
564 AI_START2_Interrupt_Enable| AI_START_Interrupt_Enable|
565 AI_STOP_Interrupt_Enable| AI_Error_Interrupt_Enable|
566 AI_FIFO_Interrupt_Enable,0);
568 s->async->events |= COMEDI_CB_EOA;
571 static void handle_a_interrupt(comedi_device *dev,unsigned short status,
572 unsigned int m_status)
574 comedi_subdevice *s=dev->subdevices+0;
575 unsigned short ack=0;
577 s->async->events = 0;
579 #ifdef DEBUG_INTERRUPT
580 rt_printk("ni_mio_common: interrupt: a_status=%04x m0_status=%08x\n",
582 ni_mio_print_status_a(status);
587 /* Currently, mite.c requires us to handle LINKC and DONE */
588 if(m_status & CHSR_LINKC){
589 writel(CHOR_CLRLC, devpriv->mite->mite_io_addr + MITE_CHOR(AI_DMA_CHAN));
590 ni_sync_ai_dma(devpriv->mite, dev);
593 if(m_status & CHSR_DONE){
594 writel(CHOR_CLRDONE, devpriv->mite->mite_io_addr + MITE_CHOR(AI_DMA_CHAN));
597 if(m_status & ~(CHSR_INT | CHSR_LINKC | CHSR_DONE | CHSR_MRDY | CHSR_DRDY | CHSR_DRQ1 | CHSR_DRQ0 | CHSR_ERROR | CHSR_SABORT | CHSR_XFERR | CHSR_LxERR_mask)){
598 printk("unknown mite interrupt, ack! (m_status=%08x)\n", m_status);
599 //mite_print_chsr(m_status);
600 mite_dma_disarm(devpriv->mite, AI_DMA_CHAN );
601 writel(CHOR_DMARESET, devpriv->mite->mite_io_addr + MITE_CHOR(AI_DMA_CHAN));
602 //disable_irq(dev->irq);
606 /* test for all uncommon interrupt events at the same time */
607 if(status&(AI_Overrun_St|AI_Overflow_St|AI_SC_TC_Error_St|AI_SC_TC_St|AI_START1_St)){
609 rt_printk("ni_mio_common: a_status=0xffff. Card removed?\n");
610 /* we probably aren't even running a command now,
611 * so it's a good idea to be careful. */
612 if(s->subdev_flags&SDF_RUNNING){
613 s->async->events |= COMEDI_CB_ERROR | COMEDI_CB_EOA;
614 //comedi_event(dev,s,s->async->events);
618 if(status&(AI_Overrun_St|AI_Overflow_St|AI_SC_TC_Error_St)){
619 rt_printk("ni_mio_common: ai error a_status=%04x\n",
621 ni_mio_print_status_a(status);
623 ni_ai_reset(dev,dev->subdevices);
626 shutdown_ai_command( dev );
628 s->async->events |= COMEDI_CB_ERROR;
629 if(status & (AI_Overrun_St | AI_Overflow_St))
630 s->async->events |= COMEDI_CB_OVERFLOW;
632 comedi_event(dev,s,s->async->events);
636 if(status&AI_SC_TC_St){
637 #ifdef DEBUG_INTERRUPT
638 rt_printk("ni_mio_common: SC_TC interrupt\n");
640 if(!devpriv->ai_continuous){
641 shutdown_ai_command( dev );
643 ack|=AI_SC_TC_Interrupt_Ack;
645 if(status&AI_START1_St){
646 ack|=AI_START1_Interrupt_Ack;
650 if(status&AI_FIFO_Half_Full_St){
652 static const int timeout = 10;
653 /* pcmcia cards (at least 6036) seem to stop producing interrupts if we
654 *fail to get the fifo less than half full, so loop to be sure.*/
655 for(i = 0; i < timeout; ++i)
657 ni_handle_fifo_half_full(dev);
658 if((devpriv->stc_readw(dev, AI_Status_1_Register) & AI_FIFO_Half_Full_St) == 0)
664 if( (status & AI_STOP_St) ){
665 ni_handle_eos(dev, s);
666 /* we need to ack the START, also */
667 ack |= AI_STOP_Interrupt_Ack|AI_START_Interrupt_Ack;
670 if(devpriv->aimode==AIMODE_SAMPLE){
671 ni_handle_fifo_dregs(dev);
673 //s->async->events |= COMEDI_CB_SAMPLE;
676 if(ack) devpriv->stc_writew(dev, ack,Interrupt_A_Ack_Register);
678 comedi_event(dev,s,s->async->events);
680 #ifdef DEBUG_INTERRUPT
681 status=devpriv->stc_readw(dev, AI_Status_1_Register);
682 if(status&Interrupt_A_St){
683 printk("handle_a_interrupt: didn't clear interrupt? status=0x%x\n", status);
688 static void handle_b_interrupt(comedi_device *dev,unsigned short b_status, unsigned int m_status)
690 comedi_subdevice *s=dev->subdevices+1;
691 //unsigned short ack=0;
692 #ifdef DEBUG_INTERRUPT
693 rt_printk("ni_mio_common: interrupt: b_status=%04x m1_status=%08x\n",
695 ni_mio_print_status_b(b_status);
700 /* Currently, mite.c requires us to handle LINKC and DONE */
701 if(m_status & CHSR_LINKC){
702 mite_handle_b_linkc(devpriv->mite, dev);
705 if(m_status & CHSR_DONE){
706 writel(CHOR_CLRDONE, devpriv->mite->mite_io_addr + MITE_CHOR(AO_DMA_CHAN));
709 if(m_status & ~(CHSR_INT | CHSR_LINKC | CHSR_DONE | CHSR_MRDY | CHSR_DRDY | CHSR_DRQ1 | CHSR_DRQ0 | CHSR_ERROR | CHSR_SABORT | CHSR_XFERR | CHSR_LxERR_mask)){
710 printk("unknown mite interrupt, ack! (m_status=%08x)\n", m_status);
711 //mite_print_chsr(m_status);
712 mite_dma_disarm(devpriv->mite, AO_DMA_CHAN );
713 writel(CHOR_DMARESET, devpriv->mite->mite_io_addr + MITE_CHOR(AO_DMA_CHAN));
717 if(b_status==0xffff)return;
718 if(b_status&AO_Overrun_St){
719 rt_printk("ni_mio_common: AO FIFO underrun status=0x%04x status2=0x%04x\n",b_status,devpriv->stc_readw(dev, AO_Status_2_Register));
721 s->async->events |= COMEDI_CB_OVERFLOW;
724 if(b_status&AO_BC_TC_St){
725 MDPRINTK("ni_mio_common: AO BC_TC status=0x%04x status2=0x%04x\n",b_status,devpriv->stc_readw(dev, AO_Status_2_Register));
727 s->async->events |= COMEDI_CB_EOA;
731 if(b_status&AO_FIFO_Request_St){
734 ret = ni_ao_fifo_half_empty(dev,s);
736 rt_printk("ni_mio_common: AO buffer underrun\n");
737 ni_set_bits(dev, Interrupt_B_Enable_Register,
738 AO_FIFO_Interrupt_Enable|AO_Error_Interrupt_Enable, 0);
739 s->async->events |= COMEDI_CB_OVERFLOW;
744 b_status=devpriv->stc_readw(dev, AO_Status_1_Register);
745 if(b_status&Interrupt_B_St){
746 if(b_status&AO_FIFO_Request_St){
747 rt_printk("ni_mio_common: AO buffer underrun\n");
749 rt_printk("Ack! didn't clear AO interrupt. b_status=0x%04x\n",b_status);
750 ni_set_bits(dev,Interrupt_B_Enable_Register,~0,0);
752 s->async->events |= COMEDI_CB_OVERFLOW;
755 comedi_event(dev,s,s->async->events);
758 #ifdef DEBUG_STATUS_A
759 static char *status_a_strings[]={
760 "passthru0","fifo","G0_gate","G0_TC",
761 "stop","start","sc_tc","start1",
762 "start2","sc_tc_error","overflow","overrun",
763 "fifo_empty","fifo_half_full","fifo_full","interrupt_a"
766 static void ni_mio_print_status_a(int status)
770 rt_printk("A status:");
773 rt_printk(" %s",status_a_strings[i]);
780 #ifdef DEBUG_STATUS_B
781 static char *status_b_strings[]={
782 "passthru1","fifo","G1_gate","G1_TC",
783 "UI2_TC","UPDATE","UC_TC","BC_TC",
784 "start1","overrun","start","bc_tc_error",
785 "fifo_empty","fifo_half_full","fifo_full","interrupt_b"
788 static void ni_mio_print_status_b(int status)
792 rt_printk("B status:");
795 rt_printk(" %s",status_b_strings[i]);
804 static void ni_ao_fifo_load(comedi_device *dev,comedi_subdevice *s, int n)
806 comedi_async *async = s->async;
807 comedi_cmd *cmd = &async->cmd;
815 chan = async->cur_chan;
817 err &= comedi_buf_get(async, &d);
820 range = CR_RANGE(cmd->chanlist[chan]);
822 if(boardtype.reg_type & ni_reg_6xxx_mask)
824 packed_data = d & 0xffff;
825 /* 6711 only has 16 bit wide ao fifo */
826 if(boardtype.reg_type != ni_reg_6711)
828 err &= comedi_buf_get(async, &d);
832 packed_data |= ( d << 16 ) & 0xffff0000;
834 ni_writel( packed_data, DAC_FIFO_Data_611x );
836 ni_writew(d, DAC_FIFO_Data);
839 chan %= cmd->chanlist_len;
841 async->cur_chan = chan;
843 async->events |= COMEDI_CB_OVERFLOW;
848 * There's a small problem if the FIFO gets really low and we
849 * don't have the data to fill it. Basically, if after we fill
850 * the FIFO with all the data available, the FIFO is _still_
851 * less than half full, we never clear the interrupt. If the
852 * IRQ is in edge mode, we never get another interrupt, because
853 * this one wasn't cleared. If in level mode, we get flooded
854 * with interrupts that we can't fulfill, because nothing ever
855 * gets put into the buffer.
857 * This kind of situation is recoverable, but it is easier to
858 * just pretend we had a FIFO underrun, since there is a good
859 * chance it will happen anyway. This is _not_ the case for
860 * RT code, as RT code might purposely be running close to the
861 * metal. Needs to be fixed eventually.
863 static int ni_ao_fifo_half_empty(comedi_device *dev,comedi_subdevice *s)
867 n = comedi_buf_read_n_available(s);
869 s->async->events |= COMEDI_CB_OVERFLOW;
873 n /= sizeof(sampl_t);
874 if(n > boardtype.ao_fifo_depth / 2)
875 n = boardtype.ao_fifo_depth / 2;
877 ni_ao_fifo_load(dev,s,n);
879 s->async->events |= COMEDI_CB_BLOCK;
884 static int ni_ao_prep_fifo(comedi_device *dev,comedi_subdevice *s)
889 devpriv->stc_writew(dev, 1,DAC_FIFO_Clear);
890 if(boardtype.reg_type & ni_reg_6xxx_mask)
891 ni_ao_win_outl(dev, 0x6, AO_FIFO_Offset_Load_611x);
894 n = comedi_buf_read_n_available(s);
897 n /= sizeof(sampl_t);
898 if(n > boardtype.ao_fifo_depth)
899 n = boardtype.ao_fifo_depth;
901 ni_ao_fifo_load(dev,s,n);
906 static void ni_ai_fifo_read(comedi_device *dev,comedi_subdevice *s,
909 comedi_async *async = s->async;
912 if(boardtype.reg_type == ni_reg_611x){
916 for( i = 0; i < n / 2; i++ ){
917 dl=ni_readl(ADC_FIFO_Data_611x);
918 /* This may get the hi/lo data in the wrong order */
919 data[0] = (dl>>16) & 0xffff;
920 data[1] = dl & 0xffff;
921 cfc_write_array_to_buffer(s, data, sizeof(data));
923 /* Check if there's a single sample stuck in the FIFO */
925 dl=ni_readl(ADC_FIFO_Data_611x);
926 data[0] = dl & 0xffff;
927 cfc_write_to_buffer(s, data[0]);
930 if( n > sizeof(devpriv->ai_fifo_buffer) / sizeof(devpriv->ai_fifo_buffer[0]))
932 comedi_error( dev, "bug! ai_fifo_buffer too small" );
933 async->events |= COMEDI_CB_ERROR;
936 for(i = 0; i < n; i++){
937 devpriv->ai_fifo_buffer[i] = ni_readw(ADC_FIFO_Data_Register);
939 cfc_write_array_to_buffer( s, devpriv->ai_fifo_buffer,
940 n * sizeof(devpriv->ai_fifo_buffer[0]) );
944 static void ni_handle_fifo_half_full(comedi_device *dev)
947 comedi_subdevice *s=dev->subdevices+0;
949 n=boardtype.ai_fifo_depth/2;
951 ni_ai_fifo_read(dev,s,n);
956 static int ni_ai_drain_dma(comedi_device *dev )
958 struct mite_struct *mite = devpriv->mite;
960 static const int timeout = 10000;
962 for( i = 0; i < timeout; i++ )
964 if((devpriv->stc_readw(dev, AI_Status_1_Register) & AI_FIFO_Empty_St) &&
965 mite_bytes_in_transit(mite, AI_DMA_CHAN) == 0)
971 rt_printk("ni_mio_common: wait for dma drain timed out\n");
972 rt_printk("mite_bytes_in_transit=%i, AI_Status1_Register=0x%x\n",
973 mite_bytes_in_transit(mite, AI_DMA_CHAN), devpriv->stc_readw(dev, AI_Status_1_Register));
977 ni_sync_ai_dma( mite, dev );
985 static void ni_handle_fifo_dregs(comedi_device *dev)
987 comedi_subdevice *s=dev->subdevices+0;
993 if(boardtype.reg_type == ni_reg_611x){
994 while((devpriv->stc_readw(dev, AI_Status_1_Register)&AI_FIFO_Empty_St) == 0){
995 dl=ni_readl(ADC_FIFO_Data_611x);
997 /* This may get the hi/lo data in the wrong order */
999 data[1] = (dl&0xffff);
1000 cfc_write_array_to_buffer(s, data, sizeof(data));
1003 fifo_empty = devpriv->stc_readw(dev, AI_Status_1_Register) & AI_FIFO_Empty_St;
1004 while(fifo_empty == 0)
1006 for(i = 0; i < sizeof(devpriv->ai_fifo_buffer) / sizeof(devpriv->ai_fifo_buffer[0]); i++)
1008 fifo_empty = devpriv->stc_readw(dev, AI_Status_1_Register) & AI_FIFO_Empty_St;
1009 if(fifo_empty) break;
1010 devpriv->ai_fifo_buffer[i] = ni_readw(ADC_FIFO_Data_Register);
1012 cfc_write_array_to_buffer( s, devpriv->ai_fifo_buffer,
1013 i * sizeof(devpriv->ai_fifo_buffer[0]) );
1018 static void get_last_sample_611x( comedi_device *dev )
1020 comedi_subdevice *s=dev->subdevices+0;
1024 if(boardtype.reg_type != ni_reg_611x) return;
1026 /* Check if there's a single sample stuck in the FIFO */
1027 if(ni_readb(XXX_Status)&0x80){
1028 dl=ni_readl(ADC_FIFO_Data_611x);
1030 cfc_write_to_buffer(s, data);
1034 static void ni_ai_munge(comedi_device *dev, comedi_subdevice *s,
1035 void *data, unsigned int num_bytes, unsigned int chan_index )
1037 comedi_async *async = s->async;
1039 unsigned int length = num_bytes / bytes_per_sample(s);
1040 sampl_t *array = data;
1041 lsampl_t *larray = data;
1043 for(i = 0; i < length; i++)
1046 if(s->subdev_flags & SDF_LSAMPL)
1047 larray[i] = le32_to_cpu(larray[i]);
1049 array[i] = le16_to_cpu(array[i]);
1051 if(s->subdev_flags & SDF_LSAMPL)
1052 larray[i] += devpriv->ai_offset[chan_index];
1054 array[i] += devpriv->ai_offset[chan_index];
1056 chan_index %= async->cmd.chanlist_len;
1062 static void ni_ai_setup_MITE_dma(comedi_device *dev,comedi_cmd *cmd)
1064 struct mite_struct *mite = devpriv->mite;
1065 struct mite_channel *mite_chan = &mite->channels[ AI_DMA_CHAN ];
1066 comedi_subdevice *s = dev->subdevices + 0;
1068 /* write alloc the entire buffer */
1069 comedi_buf_write_alloc(s->async, s->async->prealloc_bufsz);
1071 mite_chan->current_link = 0;
1072 mite_chan->dir = COMEDI_INPUT;
1073 switch(boardtype.reg_type)
1076 mite_prep_dma(mite, AI_DMA_CHAN, 32, 16);
1078 case ni_reg_m_series:
1079 if(boardtype.adbits > 16)
1080 mite_prep_dma(mite, AI_DMA_CHAN, 32, 32);
1082 mite_prep_dma(mite, AI_DMA_CHAN, 16, 16); //guess
1085 mite_prep_dma(mite, AI_DMA_CHAN, 16, 16);
1089 mite_dma_arm(mite, AI_DMA_CHAN);
1092 static void ni_ao_setup_MITE_dma(comedi_device *dev,comedi_cmd *cmd)
1094 struct mite_struct *mite = devpriv->mite;
1095 struct mite_channel *mite_chan = &mite->channels[ AO_DMA_CHAN ];
1096 comedi_subdevice *s = dev->subdevices + 1;
1098 devpriv->last_buf_write_count = s->async->buf_write_count;
1100 mite_chan->current_link = 0;
1101 mite_chan->dir = COMEDI_OUTPUT;
1102 if(boardtype.reg_type & (ni_reg_611x | ni_reg_6713))
1103 mite_prep_dma(mite, AO_DMA_CHAN, 32, 32);
1105 mite_prep_dma(mite, AO_DMA_CHAN, 16, 16);
1108 mite_dma_arm(mite, AO_DMA_CHAN);
1114 used for both cancel ioctl and board initialization
1116 this is pretty harsh for a cancel, but it works...
1119 static int ni_ai_reset(comedi_device *dev,comedi_subdevice *s)
1122 mite_dma_disarm(devpriv->mite, AI_DMA_CHAN);
1124 /* ai configuration */
1125 devpriv->stc_writew(dev, AI_Configuration_Start | AI_Reset, Joint_Reset_Register);
1127 ni_set_bits(dev, Interrupt_A_Enable_Register,
1128 AI_SC_TC_Interrupt_Enable | AI_START1_Interrupt_Enable|
1129 AI_START2_Interrupt_Enable| AI_START_Interrupt_Enable|
1130 AI_STOP_Interrupt_Enable| AI_Error_Interrupt_Enable|
1131 AI_FIFO_Interrupt_Enable,0);
1133 devpriv->stc_writew(dev, 1,ADC_FIFO_Clear);
1135 ni_writeb(0, Misc_Command);
1137 devpriv->stc_writew(dev, AI_Disarm, AI_Command_1_Register); /* reset pulses */
1138 devpriv->stc_writew(dev, AI_Start_Stop | AI_Mode_1_Reserved /*| AI_Trigger_Once */,
1139 AI_Mode_1_Register);
1140 devpriv->stc_writew(dev, 0x0000,AI_Mode_2_Register);
1141 /* generate FIFO interrupts on non-empty */
1142 devpriv->stc_writew(dev, (0<<6)|0x0000,AI_Mode_3_Register);
1143 if(boardtype.reg_type == ni_reg_611x){
1144 devpriv->stc_writew(dev, AI_SHIFTIN_Pulse_Width |
1146 AI_LOCALMUX_CLK_Pulse_Width, AI_Personal_Register);
1147 devpriv->stc_writew(dev, AI_SCAN_IN_PROG_Output_Select(3) |
1148 AI_EXTMUX_CLK_Output_Select(0) |
1149 AI_LOCALMUX_CLK_Output_Select(2) |
1150 AI_SC_TC_Output_Select(3) |
1151 AI_CONVERT_Output_Select(3),AI_Output_Control_Register);
1153 devpriv->stc_writew(dev, AI_SHIFTIN_Pulse_Width |
1155 AI_CONVERT_Pulse_Width |
1156 AI_LOCALMUX_CLK_Pulse_Width, AI_Personal_Register);
1157 devpriv->stc_writew(dev, AI_SCAN_IN_PROG_Output_Select(3) |
1158 AI_EXTMUX_CLK_Output_Select(0) |
1159 AI_LOCALMUX_CLK_Output_Select(2) |
1160 AI_SC_TC_Output_Select(3) |
1161 AI_CONVERT_Output_Select(2),AI_Output_Control_Register);
1163 /* the following registers should not be changed, because there
1164 * are no backup registers in devpriv. If you want to change
1165 * any of these, add a backup register and other appropriate code:
1166 * AI_Mode_1_Register
1167 * AI_Mode_3_Register
1168 * AI_Personal_Register
1169 * AI_Output_Control_Register
1171 devpriv->stc_writew(dev, AI_SC_TC_Error_Confirm | AI_START_Interrupt_Ack |
1172 AI_START2_Interrupt_Ack | AI_START1_Interrupt_Ack |
1173 AI_SC_TC_Interrupt_Ack | AI_Error_Interrupt_Ack |
1174 AI_STOP_Interrupt_Ack, Interrupt_A_Ack_Register); /* clear interrupts */
1176 devpriv->stc_writew(dev, AI_Configuration_End,Joint_Reset_Register);
1181 static int ni_ai_poll(comedi_device *dev,comedi_subdevice *s)
1183 unsigned long flags = 0;
1186 // lock to avoid race with interrupt handler
1187 if(in_interrupt() == 0)
1188 comedi_spin_lock_irqsave(&dev->spinlock, flags);
1190 ni_handle_fifo_dregs(dev);
1192 ni_sync_ai_dma(devpriv->mite, dev);
1194 count = s->async->buf_write_count - s->async->buf_read_count;
1195 if(in_interrupt() == 0)
1196 comedi_spin_unlock_irqrestore(&dev->spinlock, flags);
1202 static int ni_ai_insn_read(comedi_device *dev,comedi_subdevice *s,comedi_insn *insn,lsampl_t *data)
1209 ni_load_channelgain_list(dev,1,&insn->chanspec);
1211 devpriv->stc_writew(dev, 1,ADC_FIFO_Clear);
1213 mask=(1<<boardtype.adbits)-1;
1214 signbits=devpriv->ai_offset[0];
1215 if(boardtype.reg_type == ni_reg_611x){
1216 for(n=0; n < num_adc_stages_611x; n++){
1217 devpriv->stc_writew(dev, AI_CONVERT_Pulse, AI_Command_1_Register);
1220 for(n=0; n<insn->n; n++){
1221 devpriv->stc_writew(dev, AI_CONVERT_Pulse, AI_Command_1_Register);
1222 /* The 611x has screwy 32-bit FIFOs. */
1224 for(i=0; i<NI_TIMEOUT; i++){
1225 if(ni_readb(XXX_Status)&0x80)
1227 d = ( ni_readl(ADC_FIFO_Data_611x) >> 16 ) & 0xffff;
1230 if(!(devpriv->stc_readw(dev, AI_Status_1_Register)&AI_FIFO_Empty_St))
1232 d = ni_readl(ADC_FIFO_Data_611x) & 0xffff;
1237 rt_printk("ni_mio_common: timeout in 611x ni_ai_insn_read\n");
1244 for(n=0;n<insn->n;n++){
1245 devpriv->stc_writew(dev, AI_CONVERT_Pulse, AI_Command_1_Register);
1246 for(i=0;i<NI_TIMEOUT;i++){
1247 if(!(devpriv->stc_readw(dev, AI_Status_1_Register)&AI_FIFO_Empty_St))
1251 rt_printk("ni_mio_common: timeout in ni_ai_insn_read\n");
1254 if(boardtype.reg_type == ni_reg_m_series)
1256 data[n] = ni_readl(M_Offset_AI_FIFO_Data);
1257 data[n] += signbits;
1260 d = ni_readw(ADC_FIFO_Data_Register);
1261 d += signbits; /* subtle: needs to be short addition */
1270 * Notes on the 6110 and 6111:
1271 * These boards a slightly different than the rest of the series, since
1272 * they have multiple A/D converters.
1273 * From the driver side, the configuration memory is a
1275 * Configuration Memory Low:
1277 * bit 8: unipolar/bipolar (should be 0 for bipolar)
1278 * bits 0-3: gain. This is 4 bits instead of 3 for the other boards
1279 * 1001 gain=0.1 (+/- 50)
1288 * Configuration Memory High:
1289 * bits 12-14: Channel Type
1290 * 001 for differential
1291 * 000 for calibration
1292 * bit 11: coupling (this is not currently handled)
1296 * valid channels are 0-3
1298 static void ni_load_channelgain_list(comedi_device *dev,unsigned int n_chan,
1301 unsigned int chan,range,aref;
1305 unsigned int dither;
1307 if(n_chan == 1 && boardtype.reg_type != ni_reg_611x){
1308 if(devpriv->changain_state && devpriv->changain_spec==list[0]){
1312 devpriv->changain_state=1;
1313 devpriv->changain_spec=list[0];
1315 devpriv->changain_state=0;
1318 devpriv->stc_writew(dev, 1,Configuration_Memory_Clear);
1320 offset=1<<(boardtype.adbits-1);
1321 for(i=0;i<n_chan;i++){
1322 if(list[i]&CR_ALT_SOURCE){
1323 chan=devpriv->ai_calib_source;
1325 chan=CR_CHAN(list[i]);
1327 aref=CR_AREF(list[i]);
1328 range=CR_RANGE(list[i]);
1329 dither=((list[i]&CR_ALT_FILTER)!=0);
1331 /* fix the external/internal range differences */
1332 range = ni_gainlkup[boardtype.gainlkup][range];
1333 if(boardtype.reg_type == ni_reg_611x)
1334 devpriv->ai_offset[i] = offset;
1336 devpriv->ai_offset[i] = (range&0x100)?0:offset;
1339 if( ( list[i] & CR_ALT_SOURCE ) )
1341 if(boardtype.reg_type == ni_reg_611x)
1342 ni_writew(CR_CHAN(list[i])&0x0003, Calibration_Channel_Select_611x);
1345 if(boardtype.reg_type == ni_reg_611x)
1350 hi |= AI_DIFFERENTIAL;
1362 hi |= AI_CONFIG_CHANNEL( chan );
1364 ni_writew(hi,Configuration_Memory_High);
1367 if(i == n_chan - 1) lo |= AI_LAST_CHANNEL;
1368 if( dither ) lo |= AI_DITHER;
1370 ni_writew(lo,Configuration_Memory_Low);
1373 /* prime the channel/gain list */
1374 if(boardtype.reg_type != ni_reg_611x){
1375 devpriv->stc_writew(dev, AI_CONVERT_Pulse, AI_Command_1_Register);
1376 for(i=0;i<NI_TIMEOUT;i++){
1377 if(!(devpriv->stc_readw(dev, AI_Status_1_Register)&AI_FIFO_Empty_St)){
1378 devpriv->stc_writew(dev, 1,ADC_FIFO_Clear);
1383 rt_printk("ni_mio_common: timeout loading channel/gain list\n");
1387 #define TIMER_BASE 50 /* 20 Mhz base */
1389 static int ni_ns_to_timer(int *nanosec,int round_mode)
1396 case TRIG_ROUND_NEAREST:
1398 divider=(*nanosec+base/2)/base;
1400 case TRIG_ROUND_DOWN:
1401 divider=(*nanosec)/base;
1404 divider=(*nanosec+base-1)/base;
1408 *nanosec=base*divider;
1412 static int ni_ai_cmdtest(comedi_device *dev,comedi_subdevice *s,comedi_cmd *cmd)
1418 /* step 1: make sure trigger sources are trivially valid */
1421 cmd->start_src &= TRIG_NOW|TRIG_INT|TRIG_EXT;
1422 if(!cmd->start_src || tmp!=cmd->start_src)err++;
1424 tmp=cmd->scan_begin_src;
1425 cmd->scan_begin_src &= TRIG_TIMER|TRIG_EXT;
1426 if(!cmd->scan_begin_src || tmp!=cmd->scan_begin_src)err++;
1428 tmp=cmd->convert_src;
1429 sources = TRIG_TIMER | TRIG_EXT;
1430 if(boardtype.reg_type == ni_reg_611x) sources |= TRIG_NOW;
1431 cmd->convert_src &= sources;
1432 if(!cmd->convert_src || tmp!=cmd->convert_src)err++;
1434 tmp=cmd->scan_end_src;
1435 cmd->scan_end_src &= TRIG_COUNT;
1436 if(!cmd->scan_end_src || tmp!=cmd->scan_end_src)err++;
1439 cmd->stop_src &= TRIG_COUNT|TRIG_NONE;
1440 if(!cmd->stop_src || tmp!=cmd->stop_src)err++;
1444 /* step 2: make sure trigger sources are unique and mutually compatible */
1446 /* note that mutual compatiblity is not an issue here */
1447 if(cmd->start_src!=TRIG_NOW &&
1448 cmd->start_src!=TRIG_INT &&
1449 cmd->start_src!=TRIG_EXT)err++;
1450 if(cmd->scan_begin_src!=TRIG_TIMER &&
1451 cmd->scan_begin_src!=TRIG_EXT &&
1452 cmd->scan_begin_src!=TRIG_OTHER)err++;
1453 if(cmd->convert_src!=TRIG_TIMER &&
1454 cmd->convert_src!=TRIG_EXT &&
1455 cmd->convert_src!=TRIG_NOW)err++;
1456 if(cmd->stop_src!=TRIG_COUNT &&
1457 cmd->stop_src!=TRIG_NONE)err++;
1461 /* step 3: make sure arguments are trivially compatible */
1463 if(cmd->start_src==TRIG_EXT){
1464 /* external trigger */
1465 unsigned int tmp = CR_CHAN(cmd->start_arg);
1467 if(tmp > 9) tmp = 9;
1468 tmp |= (cmd->start_arg & (CR_INVERT | CR_EDGE));
1469 if(cmd->start_arg != tmp){
1470 cmd->start_arg = tmp;
1474 if(cmd->start_arg!=0){
1475 /* true for both TRIG_NOW and TRIG_INT */
1480 if(cmd->scan_begin_src==TRIG_TIMER){
1481 if(cmd->scan_begin_arg<boardtype.ai_speed){
1482 cmd->scan_begin_arg=boardtype.ai_speed;
1485 if(cmd->scan_begin_arg>TIMER_BASE*0xffffff){
1486 cmd->scan_begin_arg=TIMER_BASE*0xffffff;
1489 }else if(cmd->scan_begin_src==TRIG_EXT){
1490 /* external trigger */
1491 unsigned int tmp = CR_CHAN(cmd->scan_begin_arg);
1494 tmp |= (cmd->scan_begin_arg & (CR_INVERT | CR_EDGE));
1495 if(cmd->scan_begin_arg!=tmp){
1496 cmd->scan_begin_arg = tmp;
1499 }else{ /* TRIG_OTHER */
1500 if(cmd->scan_begin_arg){
1501 cmd->scan_begin_arg=0;
1505 if(cmd->convert_src==TRIG_TIMER){
1506 if(boardtype.reg_type == ni_reg_611x){
1507 if(cmd->convert_arg != 0){
1508 cmd->convert_arg = 0;
1512 if(cmd->convert_arg<boardtype.ai_speed){
1513 cmd->convert_arg=boardtype.ai_speed;
1516 if(cmd->convert_arg>TIMER_BASE*0xffff){
1517 cmd->convert_arg=TIMER_BASE*0xffff;
1521 }else if(cmd->convert_src == TRIG_EXT){
1522 /* external trigger */
1523 unsigned int tmp = CR_CHAN(cmd->convert_arg);
1526 tmp |= (cmd->convert_arg&(CR_ALT_FILTER|CR_INVERT));
1527 if(cmd->convert_arg!=tmp){
1528 cmd->convert_arg = tmp;
1531 }else if(cmd->convert_src == TRIG_NOW){
1532 if(cmd->convert_arg != 0){
1533 cmd->convert_arg = 0;
1538 if(cmd->scan_end_arg!=cmd->chanlist_len){
1539 cmd->scan_end_arg=cmd->chanlist_len;
1542 if(cmd->stop_src==TRIG_COUNT){
1543 unsigned int max_count = 0x01000000;
1545 if(boardtype.reg_type == ni_reg_611x )
1546 max_count -= num_adc_stages_611x;
1547 if(cmd->stop_arg > max_count){
1548 cmd->stop_arg = max_count;
1551 if(cmd->stop_arg < 1){
1557 if(cmd->stop_arg!=0){
1565 /* step 4: fix up any arguments */
1567 if(cmd->scan_begin_src==TRIG_TIMER){
1568 tmp=cmd->scan_begin_arg;
1569 ni_ns_to_timer(&cmd->scan_begin_arg,cmd->flags&TRIG_ROUND_MASK);
1570 if(tmp!=cmd->scan_begin_arg)err++;
1572 if(cmd->convert_src==TRIG_TIMER){
1573 if(boardtype.reg_type != ni_reg_611x){
1574 tmp=cmd->convert_arg;
1575 ni_ns_to_timer(&cmd->convert_arg,cmd->flags&TRIG_ROUND_MASK);
1576 if(tmp!=cmd->convert_arg)err++;
1577 if(cmd->scan_begin_src==TRIG_TIMER &&
1578 cmd->scan_begin_arg<cmd->convert_arg*cmd->scan_end_arg){
1579 cmd->scan_begin_arg=cmd->convert_arg*cmd->scan_end_arg;
1590 static int ni_ai_cmd(comedi_device *dev,comedi_subdevice *s)
1592 comedi_cmd *cmd=&s->async->cmd;
1594 int mode1=0; /* mode1 is needed for both stop and convert */
1596 int start_stop_select=0;
1597 unsigned int stop_count;
1598 int interrupt_a_enable=0;
1600 MDPRINTK("ni_ai_cmd\n");
1603 comedi_error(dev, "cannot run command without an irq");
1606 devpriv->stc_writew(dev, 1,ADC_FIFO_Clear);
1608 ni_load_channelgain_list(dev,cmd->chanlist_len,cmd->chanlist);
1610 /* start configuration */
1611 devpriv->stc_writew(dev, AI_Configuration_Start,Joint_Reset_Register);
1613 /* disable analog triggering for now, since it
1614 * interferes with the use of pfi0 */
1615 devpriv->an_trig_etc_reg &= ~Analog_Trigger_Enable;
1616 devpriv->stc_writew(dev, devpriv->an_trig_etc_reg, Analog_Trigger_Etc_Register);
1618 switch(cmd->start_src){
1621 devpriv->stc_writew(dev, AI_START2_Select(0)|
1622 AI_START1_Sync|AI_START1_Edge|AI_START1_Select(0),
1623 AI_Trigger_Select_Register);
1627 int chan = CR_CHAN(cmd->start_arg);
1628 unsigned int bits = AI_START2_Select(0)|
1630 AI_START1_Select(chan + 1);
1632 if(cmd->start_arg & CR_INVERT)
1633 bits |= AI_START1_Polarity;
1634 if(cmd->start_arg & CR_EDGE)
1635 bits |= AI_START1_Edge;
1636 devpriv->stc_writew(dev, bits, AI_Trigger_Select_Register);
1641 mode2 &= ~AI_Pre_Trigger;
1642 mode2 &= ~AI_SC_Initial_Load_Source;
1643 mode2 &= ~AI_SC_Reload_Mode;
1644 devpriv->stc_writew(dev, mode2, AI_Mode_2_Register);
1646 if(cmd->chanlist_len == 1 || boardtype.reg_type == ni_reg_611x){
1647 start_stop_select |= AI_STOP_Polarity;
1648 start_stop_select |= AI_STOP_Select( 31 ); // logic low
1649 start_stop_select |= AI_STOP_Sync;
1652 start_stop_select |= AI_STOP_Select(19); // ai configuration memory
1654 devpriv->stc_writew(dev, start_stop_select, AI_START_STOP_Select_Register);
1656 devpriv->ai_cmd2 = 0;
1657 switch(cmd->stop_src){
1659 stop_count = cmd->stop_arg - 1;
1661 if(boardtype.reg_type == ni_reg_611x){
1662 // have to take 3 stage adc pipeline into account
1663 stop_count += num_adc_stages_611x;
1665 /* stage number of scans */
1666 devpriv->stc_writel(dev, stop_count, AI_SC_Load_A_Registers);
1668 mode1 |= AI_Start_Stop | AI_Mode_1_Reserved | AI_Trigger_Once;
1669 devpriv->stc_writew(dev, mode1,AI_Mode_1_Register);
1670 /* load SC (Scan Count) */
1671 devpriv->stc_writew(dev, AI_SC_Load,AI_Command_1_Register);
1673 devpriv->ai_continuous = 0;
1674 if( stop_count == 0 ){
1675 devpriv->ai_cmd2 |= AI_End_On_End_Of_Scan;
1676 interrupt_a_enable |= AI_STOP_Interrupt_Enable;
1677 // this is required to get the last sample for chanlist_len > 1, not sure why
1678 if(cmd->chanlist_len > 1)
1679 start_stop_select |= AI_STOP_Polarity | AI_STOP_Edge;
1683 /* stage number of scans */
1684 devpriv->stc_writel(dev, 0,AI_SC_Load_A_Registers);
1686 mode1 |= AI_Start_Stop | AI_Mode_1_Reserved | AI_Continuous;
1687 devpriv->stc_writew(dev, mode1,AI_Mode_1_Register);
1689 /* load SC (Scan Count) */
1690 devpriv->stc_writew(dev, AI_SC_Load,AI_Command_1_Register);
1692 devpriv->ai_continuous = 1;
1697 switch(cmd->scan_begin_src){
1700 stop bits for non 611x boards
1701 AI_SI_Special_Trigger_Delay=0
1703 AI_START_STOP_Select_Register:
1704 AI_START_Polarity=0 (?) rising edge
1705 AI_START_Edge=1 edge triggered
1707 AI_START_Select=0 SI_TC
1708 AI_STOP_Polarity=0 rising edge
1709 AI_STOP_Edge=0 level
1711 AI_STOP_Select=19 external pin (configuration mem)
1713 start_stop_select |= AI_START_Edge | AI_START_Sync;
1714 devpriv->stc_writew(dev, start_stop_select, AI_START_STOP_Select_Register);
1716 mode2 |= AI_SI_Reload_Mode(0);
1717 /* AI_SI_Initial_Load_Source=A */
1718 mode2 &= ~AI_SI_Initial_Load_Source;
1719 //mode2 |= AI_SC_Reload_Mode;
1720 devpriv->stc_writew(dev, mode2, AI_Mode_2_Register);
1723 timer=ni_ns_to_timer(&cmd->scan_begin_arg,TRIG_ROUND_NEAREST);
1724 devpriv->stc_writel(dev, timer,AI_SI_Load_A_Registers);
1725 devpriv->stc_writew(dev, AI_SI_Load,AI_Command_1_Register);
1728 if( cmd->scan_begin_arg & CR_EDGE )
1729 start_stop_select |= AI_START_Edge;
1730 /* AI_START_Polarity==1 is falling edge */
1731 if( cmd->scan_begin_arg & CR_INVERT )
1732 start_stop_select |= AI_START_Polarity;
1733 if( cmd->scan_begin_src != cmd->convert_src ||
1734 ( cmd->scan_begin_arg & ~CR_EDGE ) != ( cmd->convert_arg & ~CR_EDGE ) )
1735 start_stop_select |= AI_START_Sync;
1736 start_stop_select |= AI_START_Select(1 + CR_CHAN(cmd->scan_begin_arg));
1737 devpriv->stc_writew(dev, start_stop_select, AI_START_STOP_Select_Register);
1741 switch(cmd->convert_src){
1744 if( cmd->convert_arg == 0 || cmd->convert_src == TRIG_NOW )
1747 timer=ni_ns_to_timer(&cmd->convert_arg, TRIG_ROUND_NEAREST);
1748 devpriv->stc_writew(dev, 1,AI_SI2_Load_A_Register); /* 0,0 does not work. */
1749 devpriv->stc_writew(dev, timer,AI_SI2_Load_B_Register);
1751 /* AI_SI2_Reload_Mode = alternate */
1752 /* AI_SI2_Initial_Load_Source = A */
1753 mode2 &= ~AI_SI2_Initial_Load_Source;
1754 mode2 |= AI_SI2_Reload_Mode;
1755 devpriv->stc_writew(dev, mode2, AI_Mode_2_Register);
1758 devpriv->stc_writew(dev, AI_SI2_Load,AI_Command_1_Register);
1760 mode2 |= AI_SI2_Reload_Mode; // alternate
1761 mode2 |= AI_SI2_Initial_Load_Source; // B
1763 devpriv->stc_writew(dev, mode2,AI_Mode_2_Register);
1766 mode1 |= AI_CONVERT_Source_Select(1+cmd->convert_arg);
1767 if( ( cmd->convert_arg & CR_INVERT ) == 0 )
1768 mode1 |= AI_CONVERT_Source_Polarity;
1769 devpriv->stc_writew(dev, mode1,AI_Mode_1_Register);
1771 mode2 |= AI_Start_Stop_Gate_Enable | AI_SC_Gate_Enable;
1772 devpriv->stc_writew(dev, mode2, AI_Mode_2_Register);
1779 /* interrupt on FIFO, errors, SC_TC */
1780 interrupt_a_enable |= AI_Error_Interrupt_Enable|
1781 AI_SC_TC_Interrupt_Enable;
1784 interrupt_a_enable|=AI_FIFO_Interrupt_Enable;
1787 if(cmd->flags & TRIG_WAKE_EOS || (devpriv->ai_cmd2 & AI_End_On_End_Of_Scan)){
1788 /* wake on end-of-scan */
1789 devpriv->aimode=AIMODE_SCAN;
1791 devpriv->aimode=AIMODE_HALF_FULL;
1794 switch(devpriv->aimode){
1795 case AIMODE_HALF_FULL:
1796 /*generate FIFO interrupts and DMA requests on half-full */
1798 devpriv->stc_writew(dev, AI_FIFO_Mode_HF_to_E, AI_Mode_3_Register);
1800 devpriv->stc_writew(dev, AI_FIFO_Mode_HF, AI_Mode_3_Register);
1804 /*generate FIFO interrupts on non-empty */
1805 devpriv->stc_writew(dev, AI_FIFO_Mode_NE, AI_Mode_3_Register);
1809 devpriv->stc_writew(dev, AI_FIFO_Mode_NE, AI_Mode_3_Register);
1811 devpriv->stc_writew(dev, AI_FIFO_Mode_HF, AI_Mode_3_Register);
1813 interrupt_a_enable |= AI_STOP_Interrupt_Enable;
1819 devpriv->stc_writew(dev, 0x3f80,Interrupt_A_Ack_Register); /* clear interrupts */
1821 ni_set_bits(dev, Interrupt_A_Enable_Register, interrupt_a_enable, 1);
1823 MDPRINTK("Interrupt_A_Enable_Register = 0x%04x\n",devpriv->int_a_enable_reg);
1825 /* interrupt on nothing */
1826 ni_set_bits(dev, Interrupt_A_Enable_Register, ~0, 0);
1828 /* XXX start polling if necessary */
1829 MDPRINTK("interrupting on nothing\n");
1832 /* end configuration */
1833 devpriv->stc_writew(dev, AI_Configuration_End,Joint_Reset_Register);
1835 switch(cmd->scan_begin_src){
1837 devpriv->stc_writew(dev, AI_SI2_Arm | AI_SI_Arm | AI_DIV_Arm | AI_SC_Arm,
1838 AI_Command_1_Register);
1841 /* XXX AI_SI_Arm? */
1842 devpriv->stc_writew(dev, AI_SI2_Arm | AI_SI_Arm | AI_DIV_Arm | AI_SC_Arm,
1843 AI_Command_1_Register);
1848 ni_ai_setup_MITE_dma(dev,cmd);
1849 //mite_dump_regs(devpriv->mite);
1852 switch(cmd->start_src){
1854 /* AI_START1_Pulse */
1855 devpriv->stc_writew(dev, AI_START1_Pulse | devpriv->ai_cmd2, AI_Command_2_Register );
1856 s->async->inttrig=NULL;
1859 s->async->inttrig=NULL;
1862 s->async->inttrig=ni_ai_inttrig;
1866 MDPRINTK("exit ni_ai_cmd\n");
1871 static int ni_ai_inttrig(comedi_device *dev,comedi_subdevice *s,
1872 unsigned int trignum)
1874 if(trignum!=0)return -EINVAL;
1876 devpriv->stc_writew(dev, AI_START1_Pulse | devpriv->ai_cmd2, AI_Command_2_Register );
1877 s->async->inttrig=NULL;
1882 static int ni_ai_config_analog_trig(comedi_device *dev,comedi_subdevice *s,
1883 comedi_insn *insn, lsampl_t *data);
1885 static int ni_ai_insn_config(comedi_device *dev,comedi_subdevice *s,
1886 comedi_insn *insn, lsampl_t *data)
1888 if(insn->n<1)return -EINVAL;
1891 case INSN_CONFIG_ANALOG_TRIG:
1892 return ni_ai_config_analog_trig(dev,s,insn,data);
1893 case INSN_CONFIG_ALT_SOURCE:
1895 unsigned int calib_source;
1896 unsigned int calib_source_adjust;
1898 calib_source = data[1] & 0xf;
1899 calib_source_adjust = ( data[1] >> 4 ) & 0xff;
1901 if(calib_source >= 8)
1903 devpriv->ai_calib_source = calib_source;
1904 if(boardtype.reg_type == ni_reg_611x){
1905 ni_writeb( calib_source_adjust, Cal_Gain_Select_611x );
1916 static int ni_ai_config_analog_trig(comedi_device *dev,comedi_subdevice *s,
1917 comedi_insn *insn, lsampl_t *data)
1919 unsigned int a,b,modebits;
1923 * data[2] is analog line
1924 * data[3] is set level
1925 * data[4] is reset level */
1926 if(!boardtype.has_analog_trig)return -EINVAL;
1927 if(insn->n!=5)return -EINVAL;
1928 if((data[1]&0xffff0000) != COMEDI_EV_SCAN_BEGIN){
1929 data[1]&=~(COMEDI_EV_SCAN_BEGIN&0xffff);
1932 if(data[2]>=boardtype.n_adchan){
1933 data[2]=boardtype.n_adchan-1;
1936 if(data[3]>255){ /* a */
1940 if(data[4]>255){ /* b */
1951 * high mode 00 00 01 10
1952 * low mode 00 00 10 01
1954 * hysteresis low mode 10 00 00 01
1955 * hysteresis high mode 01 00 00 10
1956 * middle mode 10 01 01 10
1961 modebits=data[1]&0xff;
1963 /* two level mode */
1968 modebits=((data[1]&0xf)<<4)|((data[1]&0xf0)>>4);
1970 devpriv->atrig_low = a;
1971 devpriv->atrig_high = b;
1973 case 0x81: /* low hysteresis mode */
1974 devpriv->atrig_mode = 6;
1976 case 0x42: /* high hysteresis mode */
1977 devpriv->atrig_mode = 3;
1979 case 0x96: /* middle window mode */
1980 devpriv->atrig_mode = 2;
1987 /* one level mode */
1993 case 0x06: /* high window mode */
1994 devpriv->atrig_high = a;
1995 devpriv->atrig_mode = 0;
1997 case 0x09: /* low window mode */
1998 devpriv->atrig_low = a;
1999 devpriv->atrig_mode = 1;
2006 if(err)return -EAGAIN;
2010 /* munge data from unsigned to 2's complement for analog output bipolar modes */
2011 static void ni_ao_munge(comedi_device *dev, comedi_subdevice *s,
2012 void *data, unsigned int num_bytes, unsigned int chan_index )
2014 comedi_async *async = s->async;
2017 unsigned int offset;
2018 unsigned int length = num_bytes / sizeof( sampl_t );
2019 sampl_t *array = data;
2021 offset = 1 << (boardtype.aobits - 1);
2022 for(i = 0; i < length; i++)
2024 range = CR_RANGE( async->cmd.chanlist[ chan_index ] );
2025 if(boardtype.ao_unipolar == 0 || (range & 1) == 0 )
2028 array[i] = cpu_to_le16( array[i] );
2031 chan_index %= async->cmd.chanlist_len;
2035 static int ni_ao_config_chanlist(comedi_device *dev, comedi_subdevice *s,
2036 unsigned int chanspec[], unsigned int n_chans)
2044 for(i=0;i<n_chans;i++){
2045 chan = CR_CHAN(chanspec[i]);
2046 range = CR_RANGE(chanspec[i]);
2047 if(boardtype.reg_type == ni_reg_m_series)
2049 comedi_krange *krange = s->range_table->range + range;
2052 switch(krange->max - krange->min)
2055 conf |= MSeries_AO_DAC_Reference_10V_Internal_Bits;
2056 ni_writeb(0, M_Offset_AO_Reference_Attenuation(chan));
2059 conf |= MSeries_AO_DAC_Reference_5V_Internal_Bits;
2060 ni_writeb(0, M_Offset_AO_Reference_Attenuation(chan));
2063 conf |= MSeries_AO_DAC_Reference_10V_Internal_Bits;
2064 ni_writeb(MSeries_Attenuate_x5_Bit, M_Offset_AO_Reference_Attenuation(chan));
2067 conf |= MSeries_AO_DAC_Reference_5V_Internal_Bits;
2068 ni_writeb(MSeries_Attenuate_x5_Bit, M_Offset_AO_Reference_Attenuation(chan));
2071 rt_printk("%s: bug! unhandled ao reference voltage\n", __FUNCTION__);
2074 switch(krange->max + krange->min)
2077 conf |= MSeries_AO_DAC_Offset_0V_Bits;
2080 conf |= MSeries_AO_DAC_Offset_5V_Bits;
2083 rt_printk("%s: bug! unhandled ao offset voltage\n", __FUNCTION__);
2086 ni_writeb(conf, M_Offset_AO_Config_Bank(chan));
2089 conf = AO_Channel(chan);
2091 if(boardtype.ao_unipolar){
2094 invert = (1<<(boardtype.aobits-1));
2102 invert = (1<<(boardtype.aobits-1));
2105 /* not all boards can deglitch, but this shouldn't hurt */
2106 if(chanspec[i] & CR_DEGLITCH)
2107 conf |= AO_Deglitch;
2109 /* analog reference */
2110 /* AREF_OTHER connects AO ground to AI ground, i think */
2111 conf |= (CR_AREF(chanspec[i])==AREF_OTHER)? AO_Ground_Ref : 0;
2113 ni_writew(conf,AO_Configuration);
2115 devpriv->ao_conf[chan] = conf;
2120 static int ni_ao_insn_read(comedi_device *dev,comedi_subdevice *s,
2121 comedi_insn *insn,lsampl_t *data)
2123 data[0] = devpriv->ao[CR_CHAN(insn->chanspec)];
2128 static int ni_ao_insn_write(comedi_device *dev,comedi_subdevice *s,
2129 comedi_insn *insn,lsampl_t *data)
2131 unsigned int chan = CR_CHAN(insn->chanspec);
2132 unsigned int invert;
2134 invert = ni_ao_config_chanlist(dev,s,&insn->chanspec,1);
2136 devpriv->ao[chan] = data[0];
2138 if(boardtype.reg_type == ni_reg_m_series)
2140 ni_writew(data[0], M_Offset_DAC_Direct_Data(chan));
2143 ni_writew(data[0] ^ invert,(chan)? DAC1_Direct_Data : DAC0_Direct_Data);
2148 static int ni_ao_insn_write_671x(comedi_device *dev,comedi_subdevice *s,
2149 comedi_insn *insn,lsampl_t *data)
2151 unsigned int chan = CR_CHAN(insn->chanspec);
2152 unsigned int invert;
2154 ao_win_out(1 << chan, AO_Immediate_671x);
2155 invert = 1 << (boardtype.aobits - 1);
2157 ni_ao_config_chanlist(dev,s,&insn->chanspec,1);
2159 devpriv->ao[chan] = data[0];
2160 ao_win_out(data[0] ^ invert, DACx_Direct_Data_671x(chan));
2165 static int ni_ao_inttrig(comedi_device *dev,comedi_subdevice *s,
2166 unsigned int trignum)
2169 int interrupt_b_bits;
2171 static const int timeout = 1000;
2173 if(trignum!=0)return -EINVAL;
2175 ni_set_bits(dev, Interrupt_B_Enable_Register, AO_FIFO_Interrupt_Enable | AO_Error_Interrupt_Enable, 0);
2176 interrupt_b_bits = AO_Error_Interrupt_Enable;
2178 devpriv->stc_writew(dev, 1, DAC_FIFO_Clear);
2179 if(boardtype.reg_type & ni_reg_6xxx_mask)
2180 ni_ao_win_outl(dev, 0x6, AO_FIFO_Offset_Load_611x);
2181 ni_ao_setup_MITE_dma(dev, &s->async->cmd);
2182 ret = ni_ao_wait_for_dma_load(dev);
2183 if(ret < 0) return ret;
2186 ret = ni_ao_prep_fifo(dev,s);
2187 if(ret==0)return -EPIPE;
2189 interrupt_b_bits |= AO_FIFO_Interrupt_Enable;
2192 devpriv->stc_writew(dev, devpriv->ao_mode3|AO_Not_An_UPDATE,AO_Mode_3_Register);
2193 devpriv->stc_writew(dev, devpriv->ao_mode3,AO_Mode_3_Register);
2194 /* wait for DACs to be loaded */
2195 for(i = 0; i < timeout; i++)
2198 if((devpriv->stc_readw(dev, Joint_Status_2_Register) & AO_TMRDACWRs_In_Progress_St) == 0)
2203 comedi_error(dev, "timed out waiting for AO_TMRDACWRs_In_Progress_St to clear");
2206 // stc manual says we are need to clear error interrupt after AO_TMRDACWRs_In_Progress_St clears
2207 devpriv->stc_writew(dev, AO_Error_Interrupt_Ack, Interrupt_B_Ack_Register);
2209 ni_set_bits(dev, Interrupt_B_Enable_Register, interrupt_b_bits, 1);
2211 devpriv->stc_writew(dev, devpriv->ao_cmd1|AO_UI_Arm|AO_UC_Arm|AO_BC_Arm|AO_DAC1_Update_Mode|AO_DAC0_Update_Mode,
2212 AO_Command_1_Register);
2214 devpriv->stc_writew(dev, devpriv->ao_cmd2|AO_START1_Pulse,AO_Command_2_Register);
2216 s->async->inttrig=NULL;
2221 static int ni_ao_cmd(comedi_device *dev,comedi_subdevice *s)
2223 comedi_cmd *cmd = &s->async->cmd;
2230 comedi_error(dev, "cannot run command without an irq");
2233 trigvar = ni_ns_to_timer(&cmd->scan_begin_arg,TRIG_ROUND_NEAREST);
2235 devpriv->stc_writew(dev, AO_Configuration_Start,Joint_Reset_Register);
2237 devpriv->stc_writew(dev, AO_Disarm,AO_Command_1_Register);
2239 if(boardtype.reg_type & ni_reg_6xxx_mask)
2241 ao_win_out(CLEAR_WG, AO_Misc_611x);
2244 for(i = 0; i < cmd->chanlist_len; i++)
2248 chan = CR_CHAN(cmd->chanlist[i]);
2250 ao_win_out(chan, AO_Waveform_Generation_611x);
2252 ao_win_out(bits, AO_Timed_611x);
2255 ni_ao_config_chanlist(dev,s,cmd->chanlist,cmd->chanlist_len);
2257 if(cmd->stop_src==TRIG_NONE){
2258 devpriv->ao_mode1|=AO_Continuous;
2259 devpriv->ao_mode1&=~AO_Trigger_Once;
2261 devpriv->ao_mode1&=~AO_Continuous;
2262 devpriv->ao_mode1|=AO_Trigger_Once;
2264 devpriv->stc_writew(dev, devpriv->ao_mode1,AO_Mode_1_Register);
2265 devpriv->ao_trigger_select&=~(AO_START1_Polarity|AO_START1_Select(-1));
2266 devpriv->ao_trigger_select|=AO_START1_Edge|AO_START1_Sync;
2267 devpriv->stc_writew(dev, devpriv->ao_trigger_select,AO_Trigger_Select_Register);
2268 devpriv->ao_mode3&=~AO_Trigger_Length;
2269 devpriv->stc_writew(dev, devpriv->ao_mode3,AO_Mode_3_Register);
2271 devpriv->stc_writew(dev, devpriv->ao_mode1,AO_Mode_1_Register);
2272 devpriv->ao_mode2&=~AO_BC_Initial_Load_Source;
2273 devpriv->stc_writew(dev, devpriv->ao_mode2,AO_Mode_2_Register);
2274 if(cmd->stop_src==TRIG_NONE){
2275 devpriv->stc_writel(dev, 0xffffff,AO_BC_Load_A_Register);
2277 devpriv->stc_writel(dev, 0,AO_BC_Load_A_Register);
2279 devpriv->stc_writew(dev, AO_BC_Load,AO_Command_1_Register);
2280 devpriv->ao_mode2&=~AO_UC_Initial_Load_Source;
2281 devpriv->stc_writew(dev, devpriv->ao_mode2,AO_Mode_2_Register);
2282 switch(cmd->stop_src){
2284 devpriv->stc_writel(dev, cmd->stop_arg,AO_UC_Load_A_Register);
2285 devpriv->stc_writew(dev, AO_UC_Load,AO_Command_1_Register);
2286 devpriv->stc_writel(dev, cmd->stop_arg - 1,AO_UC_Load_A_Register);
2289 devpriv->stc_writel(dev, 0xffffff,AO_UC_Load_A_Register);
2290 devpriv->stc_writew(dev, AO_UC_Load,AO_Command_1_Register);
2291 devpriv->stc_writel(dev, 0xffffff,AO_UC_Load_A_Register);
2294 devpriv->stc_writel(dev, 0,AO_UC_Load_A_Register);
2295 devpriv->stc_writew(dev, AO_UC_Load,AO_Command_1_Register);
2296 devpriv->stc_writel(dev, cmd->stop_arg,AO_UC_Load_A_Register);
2299 devpriv->ao_cmd2&=~AO_BC_Gate_Enable;
2300 devpriv->stc_writew(dev, devpriv->ao_cmd2,AO_Command_2_Register);
2301 devpriv->ao_mode1&=~(AO_UI_Source_Select(0x1f)|AO_UI_Source_Polarity);
2302 devpriv->stc_writew(dev, devpriv->ao_mode1,AO_Mode_1_Register);
2303 devpriv->ao_mode2&=~(AO_UI_Reload_Mode(3)|AO_UI_Initial_Load_Source);
2304 devpriv->stc_writew(dev, devpriv->ao_mode2,AO_Mode_2_Register);
2305 devpriv->stc_writel(dev, 1,AO_UI_Load_A_Register);
2306 devpriv->stc_writew(dev, AO_UI_Load,AO_Command_1_Register);
2307 devpriv->stc_writel(dev, trigvar,AO_UI_Load_A_Register);
2309 if((boardtype.reg_type & ni_reg_6xxx_mask) == 0){
2310 if(cmd->scan_end_arg>1){
2311 devpriv->ao_mode1|=AO_Multiple_Channels;
2312 devpriv->stc_writew(dev, AO_Number_Of_Channels(cmd->scan_end_arg-1)|
2313 AO_UPDATE_Output_Select(1),
2314 AO_Output_Control_Register);
2316 devpriv->ao_mode1&=~AO_Multiple_Channels;
2317 devpriv->stc_writew(dev, AO_Number_Of_Channels(CR_CHAN(cmd->chanlist[0]))|
2318 AO_UPDATE_Output_Select(1),
2319 AO_Output_Control_Register);
2321 devpriv->stc_writew(dev, devpriv->ao_mode1,AO_Mode_1_Register);
2324 devpriv->stc_writew(dev, AO_DAC0_Update_Mode|AO_DAC1_Update_Mode,AO_Command_1_Register);
2326 devpriv->ao_mode3|=AO_Stop_On_Overrun_Error;
2327 devpriv->stc_writew(dev, devpriv->ao_mode3,AO_Mode_3_Register);
2329 devpriv->ao_mode2 &= AO_FIFO_Mode_Mask;
2331 devpriv->ao_mode2 |= AO_FIFO_Mode_HF_to_F;
2333 devpriv->ao_mode2 |= AO_FIFO_Mode_HF;
2335 devpriv->ao_mode2 &= ~AO_FIFO_Retransmit_Enable;
2336 devpriv->stc_writew(dev, devpriv->ao_mode2,AO_Mode_2_Register);
2338 bits = AO_BC_Source_Select | AO_UPDATE_Pulse_Width |
2339 AO_TMRDACWR_Pulse_Width;
2340 if( boardtype.ao_fifo_depth )
2341 bits |= AO_FIFO_Enable;
2343 bits |= AO_DMA_PIO_Control;
2344 devpriv->stc_writew(dev, bits, AO_Personal_Register);
2345 // enable sending of ao dma requests
2346 devpriv->stc_writew(dev, AO_AOFREQ_Enable, AO_Start_Select_Register);
2348 devpriv->stc_writew(dev, AO_Configuration_End,Joint_Reset_Register);
2350 if(cmd->stop_src==TRIG_COUNT) {
2351 devpriv->stc_writew(dev, AO_BC_TC_Interrupt_Ack,Interrupt_B_Ack_Register);
2352 ni_set_bits(dev, Interrupt_B_Enable_Register,
2353 AO_BC_TC_Interrupt_Enable, 1);
2356 s->async->inttrig=ni_ao_inttrig;
2361 static int ni_ao_cmdtest(comedi_device *dev,comedi_subdevice *s,comedi_cmd *cmd)
2366 /* step 1: make sure trigger sources are trivially valid */
2369 cmd->start_src &= TRIG_INT;
2370 if(!cmd->start_src || tmp!=cmd->start_src)err++;
2372 tmp=cmd->scan_begin_src;
2373 cmd->scan_begin_src &= TRIG_TIMER;
2374 if(!cmd->scan_begin_src || tmp!=cmd->scan_begin_src)err++;
2376 tmp=cmd->convert_src;
2377 cmd->convert_src &= TRIG_NOW;
2378 if(!cmd->convert_src || tmp!=cmd->convert_src)err++;
2380 tmp=cmd->scan_end_src;
2381 cmd->scan_end_src &= TRIG_COUNT;
2382 if(!cmd->scan_end_src || tmp!=cmd->scan_end_src)err++;
2385 cmd->stop_src &= TRIG_COUNT|TRIG_NONE;
2386 if(!cmd->stop_src || tmp!=cmd->stop_src)err++;
2390 /* step 2: make sure trigger sources are unique and mutually compatible */
2392 if(cmd->stop_src!=TRIG_COUNT &&
2393 cmd->stop_src!=TRIG_NONE)err++;
2397 /* step 3: make sure arguments are trivially compatible */
2399 if(cmd->start_arg!=0){
2404 /* XXX need ao_speed */
2405 if(cmd->scan_begin_arg<boardtype.ao_speed){
2406 cmd->scan_begin_arg=boardtype.ao_speed;
2410 if(cmd->scan_begin_arg>TIMER_BASE*0xffffff){ /* XXX check */
2411 cmd->scan_begin_arg=TIMER_BASE*0xffffff;
2414 if(cmd->convert_arg!=0){
2418 if(cmd->scan_end_arg!=cmd->chanlist_len){
2419 cmd->scan_end_arg=cmd->chanlist_len;
2422 if(cmd->stop_src==TRIG_COUNT){ /* XXX check */
2423 if(cmd->stop_arg>0x00ffffff){
2424 cmd->stop_arg=0x00ffffff;
2429 if(cmd->stop_arg!=0){
2437 /* step 4: fix up any arguments */
2439 tmp = cmd->scan_begin_arg;
2440 ni_ns_to_timer(&cmd->scan_begin_arg,cmd->flags&TRIG_ROUND_MASK);
2441 if(tmp!=cmd->scan_begin_arg)err++;
2445 /* step 5: fix up chanlist */
2453 static int ni_ao_reset(comedi_device *dev,comedi_subdevice *s)
2455 //devpriv->ao0p=0x0000;
2456 //ni_writew(devpriv->ao0p,AO_Configuration);
2458 //devpriv->ao1p=AO_Channel(1);
2459 //ni_writew(devpriv->ao1p,AO_Configuration);
2462 mite_dma_disarm(devpriv->mite, AO_DMA_CHAN);
2463 writel(CHOR_DMARESET | CHOR_FRESET, devpriv->mite->mite_io_addr + MITE_CHOR(AO_DMA_CHAN));
2466 devpriv->stc_writew(dev, AO_Configuration_Start,Joint_Reset_Register);
2467 devpriv->stc_writew(dev, AO_Disarm,AO_Command_1_Register);
2468 ni_set_bits(dev,Interrupt_B_Enable_Register,~0,0);
2469 devpriv->stc_writew(dev, AO_BC_Source_Select, AO_Personal_Register);
2470 devpriv->stc_writew(dev, 0x3f98,Interrupt_B_Ack_Register);
2471 devpriv->stc_writew(dev, AO_BC_Source_Select | AO_UPDATE_Pulse_Width |
2472 AO_TMRDACWR_Pulse_Width, AO_Personal_Register);
2473 devpriv->stc_writew(dev, 0,AO_Output_Control_Register);
2474 devpriv->stc_writew(dev, 0,AO_Start_Select_Register);
2476 devpriv->stc_writew(dev, devpriv->ao_cmd1,AO_Command_1_Register);
2478 devpriv->ao_mode1=0;
2479 devpriv->ao_mode2=0;
2480 if(boardtype.reg_type == ni_reg_m_series)
2481 devpriv->ao_mode3 = AO_Last_Gate_Disable;
2483 devpriv->ao_mode3 = 0;
2484 devpriv->stc_writew(dev, devpriv->ao_mode3, AO_Mode_3_Register);
2485 devpriv->ao_trigger_select=0;
2486 if(boardtype.reg_type & ni_reg_6xxx_mask){
2487 ao_win_out(0x3, AO_Immediate_671x);
2488 ao_win_out(CLEAR_WG, AO_Misc_611x);
2490 devpriv->stc_writew(dev, AO_Configuration_End, Joint_Reset_Register);
2495 static int ni_dio_insn_config(comedi_device *dev,comedi_subdevice *s,
2496 comedi_insn *insn,lsampl_t *data)
2499 printk("ni_dio_insn_config() chan=%d io=%d\n",
2500 CR_CHAN(insn->chanspec),data[0]);
2503 case INSN_CONFIG_DIO_OUTPUT:
2504 s->io_bits |= 1<<CR_CHAN(insn->chanspec);
2506 case INSN_CONFIG_DIO_INPUT:
2507 s->io_bits &= ~(1<<CR_CHAN(insn->chanspec));
2509 case INSN_CONFIG_DIO_QUERY:
2510 data[1] = (s->io_bits & (1<<CR_CHAN(insn->chanspec))) ? COMEDI_OUTPUT : COMEDI_INPUT;
2517 devpriv->dio_control &= ~DIO_Pins_Dir_Mask;
2518 devpriv->dio_control |= DIO_Pins_Dir(s->io_bits);
2519 devpriv->stc_writew(dev, devpriv->dio_control,DIO_Control_Register);
2524 static int ni_dio_insn_bits(comedi_device *dev,comedi_subdevice *s,
2525 comedi_insn *insn,lsampl_t *data)
2528 printk("ni_dio_insn_bits() mask=0x%x bits=0x%x\n",data[0],data[1]);
2530 if(insn->n!=2)return -EINVAL;
2532 /* Perform check to make sure we're not using the
2533 serial part of the dio */
2534 if((data[0] & (DIO_SDIN | DIO_SDOUT)) && devpriv->serial_interval_ns)
2537 s->state &= ~data[0];
2538 s->state |= (data[0]&data[1]);
2539 devpriv->dio_output &= ~DIO_Parallel_Data_Mask;
2540 devpriv->dio_output |= DIO_Parallel_Data_Out(s->state);
2541 devpriv->stc_writew(dev, devpriv->dio_output,DIO_Output_Register);
2543 data[1] = devpriv->stc_readw(dev, DIO_Parallel_Input_Register);
2548 static int ni_serial_insn_config(comedi_device *dev,comedi_subdevice *s,
2549 comedi_insn *insn,lsampl_t *data)
2552 unsigned char byte_out, byte_in;
2554 if(insn->n!=2)return -EINVAL;
2557 case INSN_CONFIG_SERIAL_CLOCK:
2560 printk("SPI serial clock Config cd\n", data[1]);
2562 devpriv->serial_hw_mode = 1;
2563 devpriv->dio_control |= DIO_HW_Serial_Enable;
2565 if(data[1] == SERIAL_DISABLED) {
2566 devpriv->serial_hw_mode = 0;
2567 devpriv->dio_control &= ~(DIO_HW_Serial_Enable |
2568 DIO_Software_Serial_Control);
2569 data[1] = SERIAL_DISABLED;
2570 devpriv->serial_interval_ns = data[1];
2572 else if(data[1] <= SERIAL_600NS) {
2573 /* Warning: this clock speed is too fast to reliably
2575 devpriv->dio_control &= ~DIO_HW_Serial_Timebase;
2576 devpriv->clock_and_fout |= Slow_Internal_Timebase;
2577 devpriv->clock_and_fout &= ~DIO_Serial_Out_Divide_By_2;
2578 data[1] = SERIAL_600NS;
2579 devpriv->serial_interval_ns = data[1];
2581 else if(data[1] <= SERIAL_1_2US) {
2582 devpriv->dio_control &= ~DIO_HW_Serial_Timebase;
2583 devpriv->clock_and_fout |= Slow_Internal_Timebase |
2584 DIO_Serial_Out_Divide_By_2;
2585 data[1] = SERIAL_1_2US;
2586 devpriv->serial_interval_ns = data[1];
2588 else if(data[1] <= SERIAL_10US) {
2589 devpriv->dio_control |= DIO_HW_Serial_Timebase;
2590 devpriv->clock_and_fout |= Slow_Internal_Timebase |
2591 DIO_Serial_Out_Divide_By_2;
2592 /* Note: DIO_Serial_Out_Divide_By_2 only affects
2593 600ns/1.2us. If you turn divide_by_2 off with the
2594 slow clock, you will still get 10us, except then
2595 all your delays are wrong. */
2596 data[1] = SERIAL_10US;
2597 devpriv->serial_interval_ns = data[1];
2600 devpriv->dio_control &= ~(DIO_HW_Serial_Enable |
2601 DIO_Software_Serial_Control);
2602 devpriv->serial_hw_mode = 0;
2603 data[1] = (data[1] / 1000) * 1000;
2604 devpriv->serial_interval_ns = data[1];
2607 devpriv->stc_writew(dev, devpriv->dio_control,DIO_Control_Register);
2608 devpriv->stc_writew(dev, devpriv->clock_and_fout,Clock_and_FOUT_Register);
2613 case INSN_CONFIG_BIDIRECTIONAL_DATA:
2615 if(devpriv->serial_interval_ns == 0) {
2619 byte_out = data[1] & 0xFF;
2621 if(devpriv->serial_hw_mode) {
2622 err = ni_serial_hw_readwrite8(dev,s,byte_out,&byte_in);
2623 } else if(devpriv->serial_interval_ns > 0) {
2624 err = ni_serial_sw_readwrite8(dev,s,byte_out,&byte_in);
2626 printk("ni_serial_insn_config: serial disabled!\n");
2629 if(err < 0) return err;
2630 data[1] = byte_in & 0xFF;
2640 static int ni_serial_hw_readwrite8(comedi_device *dev,comedi_subdevice *s,
2641 unsigned char data_out,
2642 unsigned char *data_in)
2644 unsigned int status1;
2645 int err = 0, count = 20;
2648 printk("ni_serial_hw_readwrite8: outputting 0x%x\n", data_out);
2651 devpriv->dio_output &= ~DIO_Serial_Data_Mask;
2652 devpriv->dio_output |= DIO_Serial_Data_Out(data_out);
2653 devpriv->stc_writew(dev, devpriv->dio_output,DIO_Output_Register);
2655 status1 = devpriv->stc_readw(dev, Joint_Status_1_Register);
2656 if(status1 & DIO_Serial_IO_In_Progress_St) {
2661 devpriv->dio_control |= DIO_HW_Serial_Start;
2662 devpriv->stc_writew(dev, devpriv->dio_control,DIO_Control_Register);
2663 devpriv->dio_control &= ~DIO_HW_Serial_Start;
2665 /* Wait until STC says we're done, but don't loop infinitely. */
2666 while((status1 = devpriv->stc_readw(dev, Joint_Status_1_Register)) & DIO_Serial_IO_In_Progress_St) {
2667 /* Delay one bit per loop */
2668 comedi_udelay((devpriv->serial_interval_ns + 999) / 1000);
2670 printk("ni_serial_hw_readwrite8: SPI serial I/O didn't finish in time!\n");
2676 /* Delay for last bit. This delay is absolutely necessary, because
2677 DIO_Serial_IO_In_Progress_St goes high one bit too early. */
2678 comedi_udelay((devpriv->serial_interval_ns + 999) / 1000);
2680 if(data_in != NULL) {
2681 *data_in = devpriv->stc_readw(dev, DIO_Serial_Input_Register);
2683 printk("ni_serial_hw_readwrite8: inputted 0x%x\n", *data_in);
2688 devpriv->stc_writew(dev, devpriv->dio_control,DIO_Control_Register);
2693 static int ni_serial_sw_readwrite8(comedi_device *dev,comedi_subdevice *s,
2694 unsigned char data_out,
2695 unsigned char *data_in)
2697 unsigned char mask, input = 0;
2700 printk("ni_serial_sw_readwrite8: outputting 0x%x\n", data_out);
2703 /* Wait for one bit before transfer */
2704 comedi_udelay((devpriv->serial_interval_ns + 999) / 1000);
2706 for(mask = 0x80; mask; mask >>= 1) {
2707 /* Output current bit; note that we cannot touch s->state
2708 because it is a per-subdevice field, and serial is
2709 a separate subdevice from DIO. */
2710 devpriv->dio_output &= ~DIO_SDOUT;
2711 if(data_out & mask) {
2712 devpriv->dio_output |= DIO_SDOUT;
2714 devpriv->stc_writew(dev, devpriv->dio_output,DIO_Output_Register);
2716 /* Assert SDCLK (active low, inverted), wait for half of
2717 the delay, deassert SDCLK, and wait for the other half. */
2718 devpriv->dio_control |= DIO_Software_Serial_Control;
2719 devpriv->stc_writew(dev, devpriv->dio_control,DIO_Control_Register);
2721 comedi_udelay((devpriv->serial_interval_ns + 999) / 2000);
2723 devpriv->dio_control &= ~DIO_Software_Serial_Control;
2724 devpriv->stc_writew(dev, devpriv->dio_control,DIO_Control_Register);
2726 comedi_udelay((devpriv->serial_interval_ns + 999) / 2000);
2728 /* Input current bit */
2729 if(devpriv->stc_readw(dev, DIO_Parallel_Input_Register) & DIO_SDIN) {
2730 /* printk("DIO_P_I_R: 0x%x\n", devpriv->stc_readw(dev, DIO_Parallel_Input_Register)); */
2735 printk("ni_serial_sw_readwrite8: inputted 0x%x\n", input);
2737 if(data_in) *data_in = input;
2742 static void mio_common_detach(comedi_device *dev)
2744 if(dev->subdevices && boardtype.has_8255)
2745 subdev_8255_cleanup(dev,dev->subdevices+3);
2748 static void init_ao_67xx(comedi_device *dev, comedi_subdevice *s)
2752 for(i = 0; i < s->n_chan; i++)
2753 ni_ao_win_outw(dev, AO_Channel(i) | 0x0, AO_Configuration_2_67xx);
2756 static int ni_alloc_private(comedi_device *dev)
2760 ret = alloc_private(dev, sizeof(ni_private));
2761 if(ret < 0) return ret;
2763 spin_lock_init(&devpriv->window_lock);
2768 static int ni_E_init(comedi_device *dev,comedi_devconfig *it)
2770 comedi_subdevice *s;
2773 if(boardtype.n_aochan > MAX_N_AO_CHAN)
2775 printk("bug! boardtype.n_aochan > MAX_N_AO_CHAN\n");
2779 if(alloc_subdevices(dev, 10) < 0)
2782 /* analog input subdevice */
2784 s=dev->subdevices+0;
2786 if(boardtype.n_adchan){
2787 s->type=COMEDI_SUBD_AI;
2788 s->subdev_flags=SDF_READABLE | SDF_DIFF | SDF_DITHER;
2789 if(boardtype.reg_type != ni_reg_611x)
2790 s->subdev_flags |= SDF_GROUND | SDF_COMMON | SDF_OTHER;
2791 if(boardtype.adbits > 16)
2792 s->subdev_flags |= SDF_LSAMPL;
2793 s->n_chan=boardtype.n_adchan;
2794 s->len_chanlist=512;
2795 s->maxdata=(1<<boardtype.adbits)-1;
2796 s->range_table=ni_range_lkup[boardtype.gainlkup];
2797 s->insn_read=ni_ai_insn_read;
2798 s->insn_config=ni_ai_insn_config;
2799 s->do_cmdtest=ni_ai_cmdtest;
2800 s->do_cmd=ni_ai_cmd;
2801 s->cancel=ni_ai_reset;
2803 s->munge=ni_ai_munge;
2805 s->type=COMEDI_SUBD_UNUSED;
2808 /* analog output subdevice */
2810 s=dev->subdevices+1;
2811 if(boardtype.n_aochan){
2812 dev->write_subdev=s;
2813 s->type=COMEDI_SUBD_AO;
2814 s->subdev_flags=SDF_WRITABLE|SDF_DEGLITCH|SDF_GROUND;
2815 s->n_chan=boardtype.n_aochan;
2816 s->maxdata=(1<<boardtype.aobits)-1;
2817 s->range_table = boardtype.ao_range_table;
2818 s->insn_read=ni_ao_insn_read;
2819 if(boardtype.reg_type & ni_reg_6xxx_mask){
2820 s->insn_write=ni_ao_insn_write_671x;
2822 s->insn_write=ni_ao_insn_write;
2825 if(boardtype.n_aochan){
2827 if(boardtype.ao_fifo_depth){
2829 s->do_cmd=ni_ao_cmd;
2830 s->do_cmdtest=ni_ao_cmdtest;
2831 s->len_chanlist = boardtype.n_aochan;
2832 if(boardtype.reg_type != ni_reg_m_series)
2833 s->munge=ni_ao_munge;
2835 s->cancel=ni_ao_reset;
2837 s->type=COMEDI_SUBD_UNUSED;
2839 if((boardtype.reg_type & ni_reg_67xx_mask))
2840 init_ao_67xx(dev, s);
2842 /* digital i/o subdevice */
2844 s=dev->subdevices+2;
2845 s->type=COMEDI_SUBD_DIO;
2846 s->subdev_flags=SDF_WRITABLE|SDF_READABLE;
2849 s->range_table=&range_digital;
2850 s->io_bits=0; /* all bits input */
2851 s->insn_bits=ni_dio_insn_bits;
2852 s->insn_config=ni_dio_insn_config;
2855 devpriv->dio_control = DIO_Pins_Dir(s->io_bits);
2856 devpriv->stc_writew(dev, devpriv->dio_control,DIO_Control_Register);
2859 s=dev->subdevices+3;
2860 if(boardtype.has_8255){
2861 subdev_8255_init(dev,s,ni_8255_callback,(unsigned long)dev);
2863 s->type=COMEDI_SUBD_UNUSED;
2866 /* general purpose counter/timer device */
2867 s=dev->subdevices+4;
2868 s->type=COMEDI_SUBD_COUNTER;
2869 s->subdev_flags=SDF_READABLE|SDF_WRITABLE;
2870 s->insn_read= ni_gpct_insn_read;
2871 s->insn_write= ni_gpct_insn_write;
2872 s->insn_config=ni_gpct_insn_config;
2875 devpriv->an_trig_etc_reg = 0;
2879 /* calibration subdevice -- ai and ao */
2880 s=dev->subdevices+5;
2881 s->type=COMEDI_SUBD_CALIB;
2882 s->subdev_flags=SDF_WRITABLE|SDF_INTERNAL;
2883 s->insn_read=ni_calib_insn_read;
2884 s->insn_write=ni_calib_insn_write;
2885 caldac_setup(dev,s);
2888 s=dev->subdevices+6;
2889 s->type=COMEDI_SUBD_MEMORY;
2890 s->subdev_flags=SDF_READABLE|SDF_INTERNAL;
2893 s->insn_read=ni_eeprom_insn_read;
2896 s=dev->subdevices+7;
2897 s->type=COMEDI_SUBD_DIO;
2898 s->subdev_flags=SDF_READABLE|SDF_WRITABLE|SDF_INTERNAL;
2901 s->insn_bits = ni_pfi_insn_bits;
2902 s->insn_config = ni_pfi_insn_config;
2903 ni_set_bits(dev, IO_Bidirection_Pin_Register, ~0, 0);
2905 /* cs5529 calibration adc */
2906 s = dev->subdevices + 8;
2907 if(boardtype.reg_type & ni_reg_67xx_mask)
2909 s->type = COMEDI_SUBD_AI;
2910 s->subdev_flags = SDF_READABLE | SDF_DIFF | SDF_INTERNAL;
2911 // one channel for each analog output channel
2912 s->n_chan = boardtype.n_aochan;
2913 s->maxdata = (1 << 16) - 1;
2914 s->range_table = &range_unknown; /* XXX */
2915 s->insn_read=cs5529_ai_insn_read;
2916 s->insn_config=NULL;
2920 s->type=COMEDI_SUBD_UNUSED;
2924 s=dev->subdevices+9;
2925 s->type=COMEDI_SUBD_SERIAL;
2926 s->subdev_flags=SDF_READABLE|SDF_WRITABLE|SDF_INTERNAL;
2929 s->insn_config = ni_serial_insn_config;
2930 devpriv->serial_interval_ns = 0;
2931 devpriv->serial_hw_mode = 0;
2933 /* ai configuration */
2934 ni_ai_reset(dev,dev->subdevices+0);
2935 if((boardtype.reg_type & ni_reg_6xxx_mask) == 0){
2936 devpriv->clock_and_fout =
2937 Slow_Internal_Time_Divide_By_2 |
2938 Slow_Internal_Timebase |
2939 Clock_To_Board_Divide_By_2 |
2941 AI_Output_Divide_By_2 |
2942 AO_Output_Divide_By_2;
2944 devpriv->clock_and_fout =
2945 Slow_Internal_Time_Divide_By_2 |
2946 Slow_Internal_Timebase |
2947 Clock_To_Board_Divide_By_2 |
2950 devpriv->stc_writew(dev, devpriv->clock_and_fout, Clock_and_FOUT_Register);
2952 /* analog output configuration */
2953 ni_ao_reset(dev,dev->subdevices + 1);
2956 devpriv->stc_writew(dev, (IRQ_POLARITY?Interrupt_Output_Polarity:0) |
2957 (Interrupt_Output_On_3_Pins&0) |
2958 Interrupt_A_Enable |
2959 Interrupt_B_Enable |
2960 Interrupt_A_Output_Select(interrupt_pin(dev->irq)) |
2961 Interrupt_B_Output_Select(interrupt_pin(dev->irq)),
2962 Interrupt_Control_Register
2967 /* tell the STC which dma channels to use for AI and AO */
2968 bits = 1 << ( AI_DMA_CHAN );
2969 bits |= 1 << ( AO_DMA_CHAN + 4 );
2970 ni_writeb( bits, AI_AO_Select);
2971 /* tell the STC which dma channels to use for
2972 * General purpose counters 0 and 1 */
2973 bits = 1 << ( GPC0_DMA_CHAN );
2974 bits |= 1 << ( GPC1_DMA_CHAN + 4 );
2975 ni_writeb( bits, G0_G1_Select);
2977 if(boardtype.reg_type & ni_reg_6xxx_mask)
2979 ni_writeb( 0, Magic_611x );
2980 }else if(boardtype.reg_type == ni_reg_m_series)
2983 for(channel = 0; channel < boardtype.n_aochan; ++channel)
2985 ni_writeb(0xf, M_Offset_AO_Waveform_Order(channel));
2986 ni_writeb(0x0, M_Offset_AO_Reference_Attenuation(channel));
2988 ni_writeb(0x0, M_Offset_AO_Calibration);
2998 static int ni_8255_callback(int dir,int port,int data,unsigned long arg)
3000 comedi_device *dev=(comedi_device *)arg;
3003 ni_writeb(data,Port_A+2*port);
3006 return ni_readb(Port_A+2*port);
3011 presents the EEPROM as a subdevice
3014 static int ni_eeprom_insn_read(comedi_device *dev,comedi_subdevice *s,
3015 comedi_insn *insn,lsampl_t *data)
3017 data[0]=ni_read_eeprom(dev,CR_CHAN(insn->chanspec));
3023 reads bytes out of eeprom
3026 static int ni_read_eeprom(comedi_device *dev,int addr)
3031 bitstring=0x0300|((addr&0x100)<<3)|(addr&0xff);
3032 ni_writeb(0x04,Serial_Command);
3033 for(bit=0x8000;bit;bit>>=1){
3034 ni_writeb(0x04|((bit&bitstring)?0x02:0),Serial_Command);
3035 ni_writeb(0x05|((bit&bitstring)?0x02:0),Serial_Command);
3038 for(bit=0x80;bit;bit>>=1){
3039 ni_writeb(0x04,Serial_Command);
3040 ni_writeb(0x05,Serial_Command);
3041 bitstring|=((ni_readb(XXX_Status)&PROMOUT)?bit:0);
3043 ni_writeb(0x00,Serial_Command);
3048 static void ni_write_caldac(comedi_device *dev,int addr,int val);
3050 calibration subdevice
3052 static int ni_calib_insn_write(comedi_device *dev,comedi_subdevice *s,
3053 comedi_insn *insn,lsampl_t *data)
3055 ni_write_caldac(dev,CR_CHAN(insn->chanspec),data[0]);
3060 static int ni_calib_insn_read(comedi_device *dev,comedi_subdevice *s,
3061 comedi_insn *insn,lsampl_t *data)
3063 data[0] = devpriv->caldacs[CR_CHAN(insn->chanspec)];
3068 static int pack_mb88341(int addr,int val,int *bitstring);
3069 static int pack_dac8800(int addr,int val,int *bitstring);
3070 static int pack_dac8043(int addr,int val,int *bitstring);
3071 static int pack_ad8522(int addr,int val,int *bitstring);
3072 static int pack_ad8804(int addr,int val,int *bitstring);
3073 static int pack_ad8842(int addr,int val,int *bitstring);
3075 struct caldac_struct{
3078 int (*packbits)(int,int,int *);
3081 static struct caldac_struct caldacs[] = {
3082 [mb88341] = { 12, 8, pack_mb88341 },
3083 [dac8800] = { 8, 8, pack_dac8800 },
3084 [dac8043] = { 1, 12, pack_dac8043 },
3085 [ad8522] = { 2, 12, pack_ad8522 },
3086 [ad8804] = { 12, 8, pack_ad8804 },
3087 [ad8842] = { 8, 8, pack_ad8842 },
3088 [ad8804_debug] = { 16, 8, pack_ad8804 },
3091 static void caldac_setup(comedi_device *dev,comedi_subdevice *s)
3101 type = boardtype.caldac[0];
3102 if(type==caldac_none)return;
3103 n_bits=caldacs[type].n_bits;
3105 type = boardtype.caldac[i];
3106 if(type==caldac_none)break;
3107 if(caldacs[type].n_bits!=n_bits)diffbits=1;
3108 n_chans+=caldacs[type].n_chans;
3115 if(n_chans>MAX_N_CALDACS){
3116 printk("BUG! MAX_N_CALDACS too small\n");
3118 s->maxdata_list=devpriv->caldac_maxdata_list;
3120 for(i=0;i<n_dacs;i++){
3121 type = boardtype.caldac[i];
3122 for(j=0;j<caldacs[type].n_chans;j++){
3123 s->maxdata_list[chan]=
3124 (1<<caldacs[type].n_bits)-1;
3129 for( chan = 0; chan < s->n_chan; chan++ )
3130 ni_write_caldac( dev, i, s->maxdata_list[ i ] / 2 );
3132 type = boardtype.caldac[0];
3133 s->maxdata=(1<<caldacs[type].n_bits)-1;
3135 for( chan = 0; chan < s->n_chan; chan++ )
3136 ni_write_caldac( dev, i, s->maxdata / 2 );
3140 static void ni_write_caldac(comedi_device *dev,int addr,int val)
3142 unsigned int loadbit=0,bits=0,bit,bitstring=0;
3146 //printk("ni_write_caldac: chan=%d val=%d\n",addr,val);
3147 if( devpriv->caldacs[ addr ] == val ) return;
3148 devpriv->caldacs[ addr ] = val;
3151 type = boardtype.caldac[i];
3152 if(type==caldac_none)break;
3153 if(addr<caldacs[type].n_chans){
3154 bits=caldacs[type].packbits(addr,val,&bitstring);
3155 loadbit=SerDacLd(i);
3156 //printk("caldac: using i=%d addr=%d %x\n",i,addr,bitstring);
3159 addr-=caldacs[type].n_chans;
3162 for(bit=1<<(bits-1);bit;bit>>=1){
3163 ni_writeb(((bit&bitstring)?0x02:0),Serial_Command);
3165 ni_writeb(1|((bit&bitstring)?0x02:0),Serial_Command);
3168 ni_writeb(loadbit,Serial_Command);
3170 ni_writeb(0,Serial_Command);
3175 static int pack_mb88341(int addr,int val,int *bitstring)
3179 Note that address bits are reversed. Thanks to
3180 Ingo Keen for noticing this.
3182 Note also that the 88341 expects address values from
3183 1-12, whereas we use channel numbers 0-11. The NI
3184 docs use 1-12, also, so be careful here.
3187 *bitstring=((addr&0x1)<<11) |
3195 static int pack_dac8800(int addr,int val,int *bitstring)
3197 *bitstring=((addr&0x7)<<8)|(val&0xff);
3201 static int pack_dac8043(int addr,int val,int *bitstring)
3203 *bitstring=val&0xfff;
3207 static int pack_ad8522(int addr,int val,int *bitstring)
3209 *bitstring=(val&0xfff)|(addr ? 0xc000:0xa000);
3213 static int pack_ad8804(int addr,int val,int *bitstring)
3215 *bitstring=((addr&0xf)<<8) | (val&0xff);
3219 static int pack_ad8842(int addr,int val,int *bitstring)
3221 *bitstring=((addr+1)<<8) | (val&0xff);
3231 * General Purpose Counter/Timer section
3236 * Low level stuff...Each STC counter has two 24 bit load registers
3237 * (A&B). Just make it easier to access them.
3239 * These are inlined _only_ because they are used once in subsequent
3240 * code. Otherwise they should not be inlined.
3242 static inline void GPCT_Load_A(comedi_device *dev, int chan, unsigned int value)
3244 devpriv->stc_writel(dev, value & 0x00ffffff, G_Load_A_Register(chan));
3247 static inline void GPCT_Load_B(comedi_device *dev, int chan, unsigned int value)
3249 devpriv->stc_writel(dev, value & 0x00ffffff, G_Load_B_Register(chan));
3252 /* Load a value into the counter, using register A as the intermediate step.
3253 * You might use GPCT_Load_Using_A to load a 0x000000 into a counter
3256 static void GPCT_Load_Using_A(comedi_device *dev, int chan, unsigned int value)
3258 devpriv->gpct_mode[chan] &= (~G_Load_Source_Select);
3259 devpriv->stc_writew(dev, devpriv->gpct_mode[chan],G_Mode_Register(chan));
3260 GPCT_Load_A(dev,chan,value);
3261 devpriv->stc_writew(dev, devpriv->gpct_command[chan]|G_Load,G_Command_Register(chan));
3265 * Read the GPCTs current value.
3267 static int GPCT_G_Watch(comedi_device *dev, int chan)
3269 unsigned int hi1,hi2,lo;
3271 devpriv->gpct_command[chan] &= ~G_Save_Trace;
3272 devpriv->stc_writew(dev, devpriv->gpct_command[chan],G_Command_Register(chan));
3274 devpriv->gpct_command[chan] |= G_Save_Trace;
3275 devpriv->stc_writew(dev, devpriv->gpct_command[chan], G_Command_Register(chan));
3277 /* This procedure is used because the two registers cannot
3278 * be read atomically. */
3280 hi1 = devpriv->stc_readw(dev, G_Save_Register_High(chan));
3281 lo = devpriv->stc_readw(dev, G_Save_Register_Low(chan));
3282 hi2 = devpriv->stc_readw(dev, G_Save_Register_High(chan));
3285 return (hi1<<16)|lo;
3289 static int GPCT_Disarm(comedi_device *dev, int chan)
3291 devpriv->stc_writew(dev, devpriv->gpct_command[chan] | G_Disarm,G_Command_Register(chan));
3296 static int GPCT_Arm(comedi_device *dev, int chan)
3298 devpriv->stc_writew(dev, devpriv->gpct_command[chan] | G_Arm,G_Command_Register(chan));
3299 /* If the counter is doing pulse width measurement, then make
3300 sure that the counter did not start counting right away. This would
3301 indicate that we started acquiring the pulse after it had already
3302 started and our measurement would be inaccurate */
3303 if(devpriv->gpct_cur_operation[chan] == GPCT_SINGLE_PW){
3306 g_status=devpriv->stc_readw(dev, G_Status_Register);
3309 //TIM 5/2/01 possible error with very short pulses
3310 if((G0_Counting_St & g_status)|| !(G0_Armed_St&g_status)) {
3311 //error: we missed the beginning of the pulse
3312 return -EINVAL; //there is probably a more accurate error code...
3315 if((G1_Counting_St & g_status)|| !(G1_Armed_St&g_status)) {
3316 //error: we missed the beginning of the pulse
3324 static int GPCT_Set_Source(comedi_device *dev,int chan ,int source)
3326 //printk("GPCT_Set_Source...");
3327 devpriv->gpct_input_select[chan] &= ~G_Source_Select(0x1f);//reset gate to 0
3329 case GPCT_INT_CLOCK:
3330 devpriv->gpct_input_select[chan] |= G_Source_Select(0);//INT_TIMEBASE
3334 devpriv->gpct_input_select[chan] |= G_Source_Select(9);//PFI8
3336 devpriv->gpct_input_select[chan] |= G_Source_Select(4);//PFI3
3341 devpriv->stc_writew(dev, devpriv->gpct_input_select[chan], G_Input_Select_Register(chan));
3342 //printk("exit GPCT_Set_Source\n");
3346 static int GPCT_Set_Gate(comedi_device *dev,int chan ,int gate)
3348 //printk("GPCT_Set_Gate...");
3349 devpriv->gpct_input_select[chan] &= ~G_Gate_Select(0x1f);//reset gate to 0
3352 devpriv->gpct_input_select[chan] |= G_Gate_Select(31);//Low
3353 devpriv->gpct_mode[chan] |= G_Gate_Polarity;
3356 devpriv->gpct_mode[chan] &= ~G_Gate_Polarity;
3358 devpriv->gpct_input_select[chan] |= G_Gate_Select(10);//PFI9
3360 devpriv->gpct_input_select[chan] |= G_Gate_Select(5);//PFI4
3366 devpriv->stc_writew(dev, devpriv->gpct_input_select[chan], G_Input_Select_Register(chan));
3367 devpriv->stc_writew(dev, devpriv->gpct_mode[chan], G_Mode_Register(chan));
3368 //printk("exit GPCT_Set_Gate\n");
3372 static int GPCT_Set_Direction(comedi_device *dev,int chan,int direction)
3374 //printk("GPCT_Set_Direction...");
3376 devpriv->gpct_command[chan] &= ~G_Up_Down(0x3);
3377 switch (direction) {
3379 devpriv->gpct_command[chan] |= G_Up_Down(1);
3382 devpriv->gpct_command[chan] |= G_Up_Down(0);
3385 devpriv->gpct_command[chan] |= G_Up_Down(2);
3388 printk("Error direction=0x%08x..",direction);
3391 devpriv->stc_writew(dev, devpriv->gpct_command[chan], G_Command_Register(chan));
3392 //TIM 4/23/01 devpriv->stc_writew(dev, devpriv->gpct_mode[chan], G_Mode_Register(chan));
3393 //printk("exit GPCT_Set_Direction\n");
3397 static void GPCT_Event_Counting(comedi_device *dev,int chan)
3400 //NOTE: possible residual bits from multibit masks can corrupt
3401 //If you config for several measurements between Resets, watch out!
3403 //printk("GPCT_Event_Counting...");
3405 devpriv->gpct_cur_operation[chan] = GPCT_SIMPLE_EVENT;
3408 devpriv->gpct_mode[chan] &= ~(G_Gating_Mode(0x3));
3409 devpriv->gpct_mode[chan] |= G_Gating_Mode(1);
3411 // Trigger_Mode_For_Edge_Gate = 1
3412 devpriv->gpct_mode[chan] &= ~(G_Trigger_Mode_For_Edge_Gate(0x3));
3413 devpriv->gpct_mode[chan] |= G_Trigger_Mode_For_Edge_Gate(2);
3415 devpriv->stc_writew(dev, devpriv->gpct_mode[chan],G_Mode_Register(chan));
3416 //printk("exit GPCT_Event_Counting\n");
3419 static void GPCT_Period_Meas(comedi_device *dev, int chan)
3421 //printk("GPCT_Period_Meas...");
3423 devpriv->gpct_cur_operation[chan] = GPCT_SINGLE_PERIOD;
3426 //NOTE: possible residual bits from multibit masks can corrupt
3427 //If you config for several measurements between Resets, watch out!
3428 devpriv->gpct_mode[chan] &= ~G_OR_Gate;
3429 devpriv->gpct_mode[chan] &= ~G_Gate_Select_Load_Source;
3432 devpriv->gpct_mode[chan] &= ~(G_Output_Mode(0x3));
3433 devpriv->gpct_mode[chan] |= G_Output_Mode(3);
3437 devpriv->gpct_mode[chan] &= ~(G_Gating_Mode(0x3));
3438 devpriv->gpct_mode[chan] |= G_Gating_Mode(2);
3440 // Trigger_Mode_For_Edge_Gate=0
3441 devpriv->gpct_mode[chan] &= ~(G_Trigger_Mode_For_Edge_Gate(0x3));
3442 devpriv->gpct_mode[chan] |= G_Trigger_Mode_For_Edge_Gate(0);
3444 devpriv->gpct_mode[chan] |= G_Reload_Source_Switching;
3445 devpriv->gpct_mode[chan] &= ~G_Loading_On_Gate;
3446 devpriv->gpct_mode[chan] &= ~G_Loading_On_TC;
3447 devpriv->gpct_mode[chan] &= ~G_Gate_On_Both_Edges;
3450 devpriv->gpct_mode[chan] &= ~(G_Stop_Mode(0x3));
3451 devpriv->gpct_mode[chan] |= G_Stop_Mode(0);
3453 // Counting_Once = 2
3454 devpriv->gpct_mode[chan] &= ~(G_Counting_Once(0x3));
3455 devpriv->gpct_mode[chan] |= G_Counting_Once(2);
3458 devpriv->gpct_command[chan] &= ~(G_Up_Down(0x3));
3459 devpriv->gpct_command[chan] |= G_Up_Down(1);
3461 devpriv->stc_writew(dev, devpriv->gpct_mode[chan],G_Mode_Register(chan));
3462 devpriv->stc_writew(dev, devpriv->gpct_command[chan],G_Command_Register(chan));
3463 //printk("exit GPCT_Period_Meas\n");
3466 static void GPCT_Pulse_Width_Meas(comedi_device *dev, int chan)
3468 //printk("GPCT_Pulse_Width_Meas...");
3470 devpriv->gpct_cur_operation[chan] = GPCT_SINGLE_PW;
3472 devpriv->gpct_mode[chan] &= ~G_OR_Gate;
3473 devpriv->gpct_mode[chan] &= ~G_Gate_Select_Load_Source;
3476 devpriv->gpct_mode[chan] &= ~(G_Output_Mode(0x3));
3477 devpriv->gpct_mode[chan] |= G_Output_Mode(3);
3480 devpriv->gpct_mode[chan] &= ~(G_Gating_Mode(0x3));
3481 devpriv->gpct_mode[chan] |= G_Gating_Mode(1);//TIM 4/24/01 was 2
3483 // Trigger_Mode_For_Edge_Gate=2
3484 devpriv->gpct_mode[chan] &= ~(G_Trigger_Mode_For_Edge_Gate(0x3));
3485 devpriv->gpct_mode[chan] |= G_Trigger_Mode_For_Edge_Gate(2);//TIM 4/24/01 was 0
3488 devpriv->gpct_mode[chan] |= G_Reload_Source_Switching;//TIM 4/24/01 was 1
3489 devpriv->gpct_mode[chan] &= ~G_Loading_On_Gate;//TIM 4/24/01 was 0
3491 devpriv->gpct_mode[chan] &= ~G_Loading_On_TC;
3492 devpriv->gpct_mode[chan] &= ~G_Gate_On_Both_Edges;
3495 devpriv->gpct_mode[chan] &= ~(G_Stop_Mode(0x3));
3496 devpriv->gpct_mode[chan] |= G_Stop_Mode(0);
3498 // Counting_Once = 2
3499 devpriv->gpct_mode[chan] &= ~(G_Counting_Once(0x3));
3500 devpriv->gpct_mode[chan] |= G_Counting_Once(2);
3503 devpriv->gpct_command[chan] &= ~(G_Up_Down(0x3));
3504 devpriv->gpct_command[chan] |= G_Up_Down(1);
3506 devpriv->stc_writew(dev, devpriv->gpct_mode[chan],G_Mode_Register(chan));
3507 devpriv->stc_writew(dev, devpriv->gpct_command[chan],G_Command_Register(chan));
3509 //printk("exit GPCT_Pulse_Width_Meas\n");
3512 /* GPCT_Gen_Single_Pulse() creates pulse of length pulsewidth which starts after the Arm
3513 signal is sent. The pulse is delayed by the value already in the counter. This function could
3514 be modified to send a pulse in response to a trigger event at its gate.*/
3515 static void GPCT_Gen_Single_Pulse(comedi_device *dev, int chan, unsigned int length)
3517 //printk("GPCT_Gen_Cont...");
3519 devpriv->gpct_cur_operation[chan] = GPCT_SINGLE_PULSE_OUT;
3521 // Set length of the pulse
3522 GPCT_Load_B(dev,chan, length-1);
3524 //Load next time using B, This is reset by GPCT_Load_Using_A()
3525 devpriv->gpct_mode[chan] |= G_Load_Source_Select;
3527 devpriv->gpct_mode[chan] &= ~G_OR_Gate;
3528 devpriv->gpct_mode[chan] &= ~G_Gate_Select_Load_Source;
3531 devpriv->gpct_mode[chan] &= ~(G_Output_Mode(0x3));
3532 devpriv->gpct_mode[chan] |= G_Output_Mode(2); //TIM 4/26/01 was 3
3534 //Gating Mode=0 for untriggered single pulse
3535 devpriv->gpct_mode[chan] &= ~(G_Gating_Mode(0x3));
3536 devpriv->gpct_mode[chan] |= G_Gating_Mode(0); //TIM 4/25/01 was 1
3538 // Trigger_Mode_For_Edge_Gate=0
3539 devpriv->gpct_mode[chan] &= ~(G_Trigger_Mode_For_Edge_Gate(0x3));
3540 devpriv->gpct_mode[chan] |= G_Trigger_Mode_For_Edge_Gate(2);
3543 devpriv->gpct_mode[chan] |= G_Reload_Source_Switching;
3544 devpriv->gpct_mode[chan] &= ~G_Loading_On_Gate;
3545 devpriv->gpct_mode[chan] |= G_Loading_On_TC; //TIM 4/25/01
3546 devpriv->gpct_mode[chan] &= ~G_Gate_On_Both_Edges;
3549 devpriv->gpct_mode[chan] &= ~(G_Stop_Mode(0x3));
3550 devpriv->gpct_mode[chan] |= G_Stop_Mode(2); //TIM 4/25/01
3552 // Counting_Once = 2
3553 devpriv->gpct_mode[chan] &= ~(G_Counting_Once(0x3));
3554 devpriv->gpct_mode[chan] |= G_Counting_Once(1); //TIM 4/25/01
3557 devpriv->gpct_command[chan] &= ~(G_Up_Down(0x3));
3558 devpriv->gpct_command[chan] |= G_Up_Down(0); //TIM 4/25/01 was 1
3560 devpriv->stc_writew(dev, devpriv->gpct_mode[chan],G_Mode_Register(chan));
3561 devpriv->stc_writew(dev, devpriv->gpct_command[chan],G_Command_Register(chan));
3563 //printk("exit GPCT_Gen_Cont\n");
3566 static void GPCT_Gen_Cont_Pulse(comedi_device *dev, int chan, unsigned int length)
3568 //printk("GPCT_Gen_Cont...");
3570 devpriv->gpct_cur_operation[chan] = GPCT_CONT_PULSE_OUT;
3572 // Set length of the pulse
3573 GPCT_Load_B(dev,chan, length-1);
3575 //Load next time using B, This is reset by GPCT_Load_Using_A()
3576 devpriv->gpct_mode[chan] |= G_Load_Source_Select;
3578 devpriv->gpct_mode[chan] &= ~G_OR_Gate;
3579 devpriv->gpct_mode[chan] &= ~G_Gate_Select_Load_Source;
3582 devpriv->gpct_mode[chan] &= ~(G_Output_Mode(0x3));
3583 devpriv->gpct_mode[chan] |= G_Output_Mode(2); //TIM 4/26/01 was 3
3585 //Gating Mode=0 for untriggered single pulse
3586 devpriv->gpct_mode[chan] &= ~(G_Gating_Mode(0x3));
3587 devpriv->gpct_mode[chan] |= G_Gating_Mode(0); //TIM 4/26/01 was 0
3589 // Trigger_Mode_For_Edge_Gate=0
3590 devpriv->gpct_mode[chan] &= ~(G_Trigger_Mode_For_Edge_Gate(0x3));
3591 devpriv->gpct_mode[chan] |= G_Trigger_Mode_For_Edge_Gate(2);
3594 devpriv->gpct_mode[chan] |= G_Reload_Source_Switching;
3595 devpriv->gpct_mode[chan] &= ~G_Loading_On_Gate;
3596 devpriv->gpct_mode[chan] |= G_Loading_On_TC;
3597 devpriv->gpct_mode[chan] &= ~G_Gate_On_Both_Edges;
3600 devpriv->gpct_mode[chan] &= ~(G_Stop_Mode(0x3));
3601 devpriv->gpct_mode[chan] |= G_Stop_Mode(0); //TIM 4/26/01
3603 // Counting_Once = 2
3604 devpriv->gpct_mode[chan] &= ~(G_Counting_Once(0x3));
3605 devpriv->gpct_mode[chan] |= G_Counting_Once(0); //TIM 4/26/01
3608 devpriv->gpct_command[chan] &= ~(G_Up_Down(0x3));
3609 devpriv->gpct_command[chan] |= G_Up_Down(0);
3612 //This seems pretty unsafe since I don't think it is cleared anywhere.
3613 //I don't think this is working
3614 //devpriv->gpct_command[chan] &= ~G_Bank_Switch_Enable;
3615 //devpriv->gpct_command[chan] &= ~G_Bank_Switch_Mode;
3618 devpriv->stc_writew(dev, devpriv->gpct_mode[chan],G_Mode_Register(chan));
3619 devpriv->stc_writew(dev, devpriv->gpct_command[chan],G_Command_Register(chan));
3621 //printk("exit GPCT_Gen_Cont\n");
3624 static void GPCT_Reset(comedi_device *dev, int chan)
3628 //printk("GPCT_Reset...");
3629 devpriv->gpct_cur_operation[chan] = GPCT_RESET;
3633 devpriv->stc_writew(dev, G0_Reset,Joint_Reset_Register);
3634 ni_set_bits(dev,Interrupt_A_Enable_Register,G0_TC_Interrupt_Enable, 0);
3635 ni_set_bits(dev,Interrupt_A_Enable_Register,G0_Gate_Interrupt_Enable,0);
3636 temp_ack_reg |= G0_Gate_Error_Confirm;
3637 temp_ack_reg |= G0_TC_Error_Confirm;
3638 temp_ack_reg |= G0_TC_Interrupt_Ack;
3639 temp_ack_reg |= G0_Gate_Interrupt_Ack;
3640 devpriv->stc_writew(dev, temp_ack_reg,Interrupt_A_Ack_Register);
3642 //problem...this interferes with the other ctr...
3643 devpriv->an_trig_etc_reg |= GPFO_0_Output_Enable;
3644 devpriv->stc_writew(dev, devpriv->an_trig_etc_reg, Analog_Trigger_Etc_Register);
3647 devpriv->stc_writew(dev, G1_Reset,Joint_Reset_Register);
3648 ni_set_bits(dev,Interrupt_B_Enable_Register,G1_TC_Interrupt_Enable, 0);
3649 ni_set_bits(dev,Interrupt_B_Enable_Register,G0_Gate_Interrupt_Enable,0);
3650 temp_ack_reg |= G1_Gate_Error_Confirm;
3651 temp_ack_reg |= G1_TC_Error_Confirm;
3652 temp_ack_reg |= G1_TC_Interrupt_Ack;
3653 temp_ack_reg |= G1_Gate_Interrupt_Ack;
3654 devpriv->stc_writew(dev, temp_ack_reg,Interrupt_B_Ack_Register);
3656 devpriv->an_trig_etc_reg |= GPFO_1_Output_Enable;
3657 devpriv->stc_writew(dev, devpriv->an_trig_etc_reg, Analog_Trigger_Etc_Register);
3661 devpriv->gpct_mode[chan] = 0;
3662 devpriv->gpct_input_select[chan] = 0;
3663 devpriv->gpct_command[chan] = 0;
3665 devpriv->gpct_command[chan] |= G_Synchronized_Gate;
3667 devpriv->stc_writew(dev, devpriv->gpct_mode[chan],G_Mode_Register(chan));
3668 devpriv->stc_writew(dev, devpriv->gpct_input_select[chan],G_Input_Select_Register(chan));
3669 devpriv->stc_writew(dev, 0,G_Autoincrement_Register(chan));
3671 //printk("exit GPCT_Reset\n");
3674 static int ni_gpct_insn_config(comedi_device *dev,comedi_subdevice *s,
3675 comedi_insn *insn,lsampl_t *data)
3678 //printk("data[0] is 0x%08x, data[1] is 0x%08x\n",data[0],data[1]);
3681 if(insn->n!=1)return -EINVAL;
3682 GPCT_Reset(dev,insn->chanspec);
3684 case GPCT_SET_SOURCE:
3685 if(insn->n!=2)return -EINVAL;
3686 retval=GPCT_Set_Source(dev,insn->chanspec,data[1]);
3689 if(insn->n!=2)return -EINVAL;
3690 retval=GPCT_Set_Gate(dev,insn->chanspec,data[1]);
3692 case GPCT_SET_DIRECTION:
3693 if(insn->n!=2) return -EINVAL;
3694 retval=GPCT_Set_Direction(dev,insn->chanspec,data[1]);
3696 case GPCT_GET_INT_CLK_FRQ:
3697 if(insn->n!=2) return -EINVAL;
3698 //There are actually 2 internal clocks on the STC, we always
3699 //use the fast 20MHz one at this time. Tim Ousley 5/1/01
3700 //NOTE: This is not the final interface, ideally the user
3701 //will never need to know the int. clk. freq.
3702 data[1]=50;//50ns = 20MHz = internal timebase of STC
3704 case GPCT_SET_OPERATION:
3705 //TIM 5/1/01 if((insn->n<2)||(insn->n>3))return -EINVAL;
3707 case GPCT_SIMPLE_EVENT:
3708 GPCT_Event_Counting(dev,insn->chanspec);
3710 case GPCT_SINGLE_PERIOD:
3711 GPCT_Period_Meas(dev,insn->chanspec);
3713 case GPCT_SINGLE_PW:
3714 GPCT_Pulse_Width_Meas(dev,insn->chanspec);
3716 case GPCT_SINGLE_PULSE_OUT:
3717 GPCT_Gen_Single_Pulse(dev,insn->chanspec,data[2]);
3719 case GPCT_CONT_PULSE_OUT:
3720 GPCT_Gen_Cont_Pulse(dev,insn->chanspec,data[2]);
3723 printk("unsupported GPCT operation!\n");
3728 if(insn->n!=1)return -EINVAL;
3729 retval=GPCT_Arm(dev,insn->chanspec);
3732 if(insn->n!=1)return -EINVAL;
3733 retval=GPCT_Disarm(dev,insn->chanspec);
3739 //catch any errors from return values
3743 if(data[0]!=GPCT_ARM){
3744 printk("error: retval was %d\n",retval);
3745 printk("data[0] is 0x%08x, data[1] is 0x%08x\n",data[0],data[1]);
3752 static int ni_gpct_insn_read(comedi_device *dev,comedi_subdevice *s,
3753 comedi_insn *insn,lsampl_t *data) {
3755 int chan=insn->chanspec;
3756 int cur_op = devpriv->gpct_cur_operation[chan];
3758 //printk("in ni_gpct_insn_read, n=%d, data[0]=%d\n",insn->chanspec,data[0]);
3759 if(insn->n!=1)return -EINVAL;
3761 data[0] = GPCT_G_Watch(dev,insn->chanspec);
3763 /* for certain modes (period and pulse width measurment), the value
3764 in the counter is not valid until the counter stops. If the value is
3765 invalid, return a 0 */
3766 if((cur_op == GPCT_SINGLE_PERIOD) || (cur_op == GPCT_SINGLE_PW)){
3767 /* is the counter still running? */
3768 if(devpriv->stc_readw(dev, G_Status_Register) & (chan?G1_Counting_St:G0_Counting_St))
3774 static int ni_gpct_insn_write(comedi_device *dev,comedi_subdevice *s,
3775 comedi_insn *insn,lsampl_t *data) {
3777 //printk("in ni_gpct_insn_write");
3778 if(insn->n!=1)return -EINVAL;
3779 GPCT_Load_Using_A(dev,insn->chanspec,data[0]);
3786 * Programmable Function Inputs
3790 static int ni_pfi_insn_bits(comedi_device *dev,comedi_subdevice *s,
3791 comedi_insn *insn,lsampl_t *data)
3793 if(insn->n!=2)return -EINVAL;
3800 static int ni_pfi_insn_config(comedi_device *dev,comedi_subdevice *s,
3801 comedi_insn *insn,lsampl_t *data)
3805 if(insn->n!=1)return -EINVAL;
3807 chan = CR_CHAN(insn->chanspec);
3808 if(chan>10)return -EINVAL;
3812 ni_set_bits(dev, IO_Bidirection_Pin_Register, 1<<chan, 1);
3815 ni_set_bits(dev, IO_Bidirection_Pin_Register, 1<<chan, 0);
3817 case INSN_CONFIG_DIO_QUERY:
3818 data[1] = (devpriv->io_bidirection_pin_reg & (1<<chan)) ? COMEDI_OUTPUT : COMEDI_INPUT;
3828 static int cs5529_wait_for_idle(comedi_device *dev)
3830 unsigned short status;
3831 const int timeout = HZ;
3834 for(i = 0; i < timeout; i++)
3836 status = ni_ao_win_inw(dev, CAL_ADC_Status_67xx);
3837 if((status & CSS_ADC_BUSY) == 0)
3841 set_current_state(TASK_INTERRUPTIBLE);
3842 if(schedule_timeout(1))
3847 //printk("looped %i times waiting for idle\n", i);
3850 rt_printk("%s: %s: timeout\n", __FILE__, __FUNCTION__);
3856 static void cs5529_command(comedi_device *dev, unsigned short value)
3858 static const int timeout = 100;
3861 ni_ao_win_outw(dev, value, CAL_ADC_Command_67xx);
3862 /* give time for command to start being serially clocked into cs5529.
3863 * this insures that the CSS_ADC_BUSY bit will get properly
3864 * set before we exit this function.
3866 for(i = 0; i < timeout; i++)
3868 if((ni_ao_win_inw(dev, CAL_ADC_Status_67xx) & CSS_ADC_BUSY))
3872 //printk("looped %i times writing command to cs5529\n", i);
3875 comedi_error(dev, "possible problem - never saw adc go busy?");
3879 /* write to cs5529 register */
3880 static void cs5529_config_write(comedi_device *dev, unsigned int value, unsigned int reg_select_bits)
3882 ni_ao_win_outw(dev, ((value >> 16) & 0xff), CAL_ADC_Config_Data_High_Word_67xx);
3883 ni_ao_win_outw(dev, (value & 0xffff), CAL_ADC_Config_Data_Low_Word_67xx);
3884 reg_select_bits &= CSCMD_REGISTER_SELECT_MASK;
3885 cs5529_command(dev, CSCMD_COMMAND | reg_select_bits);
3886 if(cs5529_wait_for_idle(dev))
3887 comedi_error(dev, "time or signal in cs5529_config_write()");
3890 /* read from cs5529 register */
3891 static unsigned int cs5529_config_read(comedi_device *dev, unsigned int reg_select_bits)
3895 reg_select_bits &= CSCMD_REGISTER_SELECT_MASK;
3896 cs5529_command(dev, CSCMD_COMMAND | CSCMD_READ | reg_select_bits);
3897 if(cs5529_wait_for_idle(dev))
3898 comedi_error(dev, "timeout or signal in cs5529_config_read()");
3899 value = (ni_ao_win_inw(dev, CAL_ADC_Config_Data_High_Word_67xx) << 16) & 0xff0000;
3900 value |= ni_ao_win_inw(dev, CAL_ADC_Config_Data_Low_Word_67xx) & 0xffff;
3904 static int cs5529_do_conversion(comedi_device *dev, unsigned short *data)
3907 unsigned short status;
3909 cs5529_command(dev, CSCMD_COMMAND | CSCMD_SINGLE_CONVERSION);
3910 retval = cs5529_wait_for_idle(dev);
3913 comedi_error(dev, "timeout or signal in cs5529_do_conversion()");
3916 status = ni_ao_win_inw(dev, CAL_ADC_Status_67xx);
3917 if(status & CSS_OSC_DETECT)
3919 rt_printk("ni_mio_common: cs5529 conversion error, status CSS_OSC_DETECT\n");
3922 if(status & CSS_OVERRANGE)
3924 rt_printk("ni_mio_common: cs5529 conversion error, overrange (ignoring)\n");
3928 *data = ni_ao_win_inw(dev, CAL_ADC_Data_67xx);
3929 /* cs5529 returns 16 bit signed data in bipolar mode */
3935 static int cs5529_ai_insn_read(comedi_device *dev,comedi_subdevice *s,comedi_insn *insn,lsampl_t *data)
3938 unsigned short sample;
3939 unsigned int channel_select;
3940 const unsigned int INTERNAL_REF = 0x1000;
3942 /* Set calibration adc source. Docs lie, reference select bits 8 to 11
3943 * do nothing. bit 12 seems to chooses internal reference voltage, bit
3944 * 13 causes the adc input to go overrange (maybe reads external reference?) */
3945 if(insn->chanspec & CR_ALT_SOURCE)
3946 channel_select = INTERNAL_REF;
3948 channel_select = CR_CHAN(insn->chanspec);
3949 ni_ao_win_outw(dev, channel_select, AO_Calibration_Channel_Select_67xx);
3951 for(n = 0; n < insn->n; n++)
3953 retval = cs5529_do_conversion(dev, &sample);
3954 if(retval < 0) return retval;
3960 static int init_cs5529(comedi_device *dev)
3962 unsigned int config_bits = CSCFG_PORT_MODE | CSCFG_WORD_RATE_2180_CYCLES;
3965 /* do self-calibration */
3966 cs5529_config_write(dev, config_bits | CSCFG_SELF_CAL_OFFSET_GAIN, CSCMD_CONFIG_REGISTER);
3967 /* need to force a conversion for calibration to run */
3968 cs5529_do_conversion(dev, NULL);
3970 /* force gain calibration to 1 */
3971 cs5529_config_write(dev, 0x400000, CSCMD_GAIN_REGISTER);
3972 cs5529_config_write(dev, config_bits | CSCFG_SELF_CAL_OFFSET, CSCMD_CONFIG_REGISTER);
3973 if(cs5529_wait_for_idle(dev))
3974 comedi_error(dev, "timeout or signal in init_cs5529()\n");
3978 rt_printk("config: 0x%x\n", cs5529_config_read(dev, CSCMD_CONFIG_REGISTER));
3979 rt_printk("gain: 0x%x\n", cs5529_config_read(dev, CSCMD_GAIN_REGISTER));
3980 rt_printk("offset: 0x%x\n", cs5529_config_read(dev, CSCMD_OFFSET_REGISTER));