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 }
90 static comedi_lrange range_ni_E_ai={ 16, {
108 static comedi_lrange range_ni_E_ai_limited={ 8, {
118 static comedi_lrange range_ni_E_ai_limited14={ 14, {
134 static comedi_lrange range_ni_E_ai_bipolar4={ 4, {
138 RANGE( -0.05, 0.05 ),
140 static comedi_lrange range_ni_E_ai_611x={ 8, {
150 static comedi_lrange range_ni_E_ao_ext = { 4, {
157 static comedi_lrange *ni_range_lkup[]={
159 &range_ni_E_ai_limited,
160 &range_ni_E_ai_limited14,
161 &range_ni_E_ai_bipolar4,
167 static int ni_dio_insn_config(comedi_device *dev,comedi_subdevice *s,
168 comedi_insn *insn,lsampl_t *data);
169 static int ni_dio_insn_bits(comedi_device *dev,comedi_subdevice *s,
170 comedi_insn *insn,lsampl_t *data);
172 static int ni_serial_insn_config(comedi_device *dev,comedi_subdevice *s,
173 comedi_insn *insn,lsampl_t *data);
174 static int ni_serial_hw_readwrite8(comedi_device *dev,comedi_subdevice *s,
175 unsigned char data_out, unsigned char *data_in);
176 static int ni_serial_sw_readwrite8(comedi_device *dev,comedi_subdevice *s,
177 unsigned char data_out, unsigned char *data_in);
179 static int ni_calib_insn_read(comedi_device *dev,comedi_subdevice *s,
180 comedi_insn *insn,lsampl_t *data);
181 static int ni_calib_insn_write(comedi_device *dev,comedi_subdevice *s,
182 comedi_insn *insn,lsampl_t *data);
184 static int ni_eeprom_insn_read(comedi_device *dev,comedi_subdevice *s,
185 comedi_insn *insn,lsampl_t *data);
187 static int ni_pfi_insn_bits(comedi_device *dev,comedi_subdevice *s,
188 comedi_insn *insn,lsampl_t *data);
189 static int ni_pfi_insn_config(comedi_device *dev,comedi_subdevice *s,
190 comedi_insn *insn,lsampl_t *data);
192 static void caldac_setup(comedi_device *dev,comedi_subdevice *s);
193 static int ni_read_eeprom(comedi_device *dev,int addr);
195 #ifdef DEBUG_STATUS_A
196 static void ni_mio_print_status_a(int status);
198 #define ni_mio_print_status_a(a)
200 #ifdef DEBUG_STATUS_B
201 static void ni_mio_print_status_b(int status);
203 #define ni_mio_print_status_b(a)
206 static int ni_ai_reset(comedi_device *dev,comedi_subdevice *s);
208 static void ni_handle_fifo_half_full(comedi_device *dev);
209 static int ni_ao_fifo_half_empty(comedi_device *dev,comedi_subdevice *s);
211 static void ni_handle_fifo_dregs(comedi_device *dev);
212 static int ni_ai_inttrig(comedi_device *dev,comedi_subdevice *s,
213 unsigned int trignum);
214 static void ni_load_channelgain_list(comedi_device *dev,unsigned int n_chan,
216 static void shutdown_ai_command( comedi_device *dev );
218 static int ni_ao_inttrig(comedi_device *dev,comedi_subdevice *s,
219 unsigned int trignum);
221 static int ni_ao_reset(comedi_device *dev,comedi_subdevice *s);
223 static int ni_8255_callback(int dir,int port,int data,unsigned long arg);
225 static int ni_ns_to_timer(int *nanosec,int round_mode);
228 /*GPCT function def's*/
229 static int GPCT_G_Watch(comedi_device *dev, int chan);
231 static void GPCT_Reset(comedi_device *dev, int chan);
232 static void GPCT_Gen_Cont_Pulse(comedi_device *dev, int chan, unsigned int length);
233 static void GPCT_Gen_Single_Pulse(comedi_device *dev, int chan, unsigned int length);
234 static void GPCT_Period_Meas(comedi_device *dev, int chan);
235 static void GPCT_Pulse_Width_Meas(comedi_device *dev, int chan);
236 static void GPCT_Event_Counting(comedi_device *dev,int chan);
237 static int GPCT_Set_Direction(comedi_device *dev,int chan,int direction);
238 static int GPCT_Set_Gate(comedi_device *dev,int chan ,int gate);
239 static int GPCT_Set_Source(comedi_device *dev,int chan ,int source);
241 static int ni_gpct_insn_write(comedi_device *dev,comedi_subdevice *s,
242 comedi_insn *insn,lsampl_t *data);
243 static int ni_gpct_insn_read(comedi_device *dev,comedi_subdevice *s,
244 comedi_insn *insn,lsampl_t *data);
245 static int ni_gpct_insn_config(comedi_device *dev,comedi_subdevice *s,
246 comedi_insn *insn,lsampl_t *data);
248 static int init_cs5529(comedi_device *dev);
249 static int cs5529_do_conversion(comedi_device *dev, unsigned short *data);
250 static int cs5529_ai_insn_read(comedi_device *dev,comedi_subdevice *s,comedi_insn *insn,lsampl_t *data);
251 static unsigned int cs5529_config_read(comedi_device *dev, unsigned int reg_select_bits);
252 static void cs5529_config_write(comedi_device *dev, unsigned int value, unsigned int reg_select_bits);
257 AIMODE_HALF_FULL = 1,
262 #define SERIAL_DISABLED 0
263 #define SERIAL_600NS 600
264 #define SERIAL_1_2US 1200
265 #define SERIAL_10US 10000
267 static const int num_adc_stages_611x = 3;
269 static void handle_a_interrupt(comedi_device *dev,unsigned short status,
270 unsigned int m_status);
271 static void handle_b_interrupt(comedi_device *dev,unsigned short status,
272 unsigned int m_status);
273 static void get_last_sample_611x( comedi_device *dev );
275 //static void mite_handle_interrupt(comedi_device *dev,unsigned int status);
276 static int ni_ai_drain_dma(comedi_device *dev );
279 #define win_out2(data,addr) do{ \
280 win_out((data)>>16, (addr)); \
281 win_out((data)&0xffff, (addr)+1); \
285 #define ao_win_out(data,addr) ni_ao_win_outw(dev,data,addr)
286 static inline void ni_ao_win_outw( comedi_device *dev, uint16_t data, int addr )
290 comedi_spin_lock_irqsave(&devpriv->window_lock,flags);
291 ni_writew(addr,AO_Window_Address_611x);
292 ni_writew(data,AO_Window_Data_611x);
293 comedi_spin_unlock_irqrestore(&devpriv->window_lock,flags);
296 static inline void ni_ao_win_outl(comedi_device *dev, uint32_t data, int addr)
300 comedi_spin_lock_irqsave(&devpriv->window_lock,flags);
301 ni_writew(addr,AO_Window_Address_611x);
302 ni_writel(data,AO_Window_Data_611x);
303 comedi_spin_unlock_irqrestore(&devpriv->window_lock,flags);
306 static inline unsigned short ni_ao_win_inw( comedi_device *dev, int addr )
311 comedi_spin_lock_irqsave(&devpriv->window_lock,flags);
312 ni_writew(addr, AO_Window_Address_611x);
313 data = ni_readw(AO_Window_Data_611x);
314 comedi_spin_unlock_irqrestore(&devpriv->window_lock,flags);
318 /* ni_set_bits( ) allows different parts of the ni_mio_common driver to
319 * share registers (such as Interrupt_A_Register) without interfering with
322 * NOTE: the switch/case statements are optimized out for a constant argument
323 * so this is actually quite fast--- If you must wrap another function around this
324 * make it inline to avoid a large speed penalty.
326 * value should only be 1 or 0.
328 static inline void ni_set_bits(comedi_device *dev, int reg, int bits, int value)
332 comedi_spin_lock_irqsave( &devpriv->window_lock, flags );
334 case Interrupt_A_Enable_Register:
336 devpriv->int_a_enable_reg |= bits;
338 devpriv->int_a_enable_reg &= ~bits;
339 comedi_spin_unlock_irqrestore( &devpriv->window_lock, flags );
340 win_out(devpriv->int_a_enable_reg,Interrupt_A_Enable_Register);
342 case Interrupt_B_Enable_Register:
344 devpriv->int_b_enable_reg |= bits;
346 devpriv->int_b_enable_reg &= ~bits;
347 comedi_spin_unlock_irqrestore( &devpriv->window_lock, flags );
348 win_out(devpriv->int_b_enable_reg,Interrupt_B_Enable_Register);
350 case IO_Bidirection_Pin_Register:
352 devpriv->io_bidirection_pin_reg |= bits;
354 devpriv->io_bidirection_pin_reg &= ~bits;
355 comedi_spin_unlock_irqrestore( &devpriv->window_lock, flags );
356 win_out(devpriv->io_bidirection_pin_reg,IO_Bidirection_Pin_Register);
359 printk("Warning ni_set_bits() called with invalid arguments\n");
360 printk("reg is %d\n",reg);
361 comedi_spin_unlock_irqrestore( &devpriv->window_lock, flags );
367 static irqreturn_t ni_E_interrupt(int irq,void *d,struct pt_regs * regs)
369 comedi_device *dev=d;
370 unsigned short a_status;
371 unsigned short b_status;
372 unsigned int m0_status;
373 unsigned int m1_status;
376 struct mite_struct *mite = devpriv->mite;
379 if(dev->attached == 0) return IRQ_NONE;
380 // lock to avoid race with comedi_poll
381 comedi_spin_lock_irqsave(&dev->spinlock, flags);
382 a_status=win_in(AI_Status_1_Register);
383 b_status=win_in(AO_Status_1_Register);
385 m0_status=readl(mite->mite_io_addr + MITE_CHSR(AI_DMA_CHAN));
386 m1_status=readl(mite->mite_io_addr + MITE_CHSR(AO_DMA_CHAN));
392 if(a_status&Interrupt_A_St || m0_status & CHSR_INT )
393 handle_a_interrupt(dev, a_status, m0_status);
394 if(b_status&Interrupt_B_St || m1_status & CHSR_INT )
395 handle_b_interrupt(dev, b_status, m1_status);
396 comedi_spin_unlock_irqrestore(&dev->spinlock, flags);
401 static void ni_sync_ai_dma(struct mite_struct *mite, comedi_device *dev)
404 comedi_subdevice *s = dev->subdevices + 0;
405 comedi_async *async = s->async;
406 unsigned int nbytes, old_alloc_count;
407 unsigned int bytes_per_scan = 2 * async->cmd.chanlist_len;
409 old_alloc_count = async->buf_write_alloc_count;
410 // write alloc as much as we can
411 comedi_buf_write_alloc(s->async, s->async->prealloc_bufsz);
413 nbytes = mite_bytes_written_to_memory_lb(mite, AI_DMA_CHAN);
415 if( (int)(mite_bytes_written_to_memory_ub(mite, AI_DMA_CHAN) - old_alloc_count) > 0 ){
416 printk("ni_mio_common: DMA overwrite of free area\n");
418 async->events |= COMEDI_CB_OVERFLOW;
422 count = nbytes - async->buf_write_count;
424 /* it's possible count will be negative due to
425 * conservative value returned by mite_bytes_transferred */
428 comedi_buf_write_free(async, count);
430 async->scan_progress += count;
431 if( async->scan_progress >= bytes_per_scan )
433 async->scan_progress %= bytes_per_scan;
434 async->events |= COMEDI_CB_EOS;
436 async->events |= COMEDI_CB_BLOCK;
439 static void mite_handle_b_linkc(struct mite_struct *mite, comedi_device *dev)
442 comedi_subdevice *s = dev->subdevices + 1;
443 comedi_async *async = s->async;
444 u32 nbytes_ub, nbytes_lb;
445 unsigned int new_write_count;
446 u32 stop_count = async->cmd.stop_arg * sizeof(sampl_t);
448 writel(CHOR_CLRLC, mite->mite_io_addr + MITE_CHOR(AO_DMA_CHAN));
450 new_write_count = async->buf_write_count;
452 nbytes_lb = mite_bytes_read_from_memory_lb(mite, AO_DMA_CHAN);
453 if(async->cmd.stop_src == TRIG_COUNT &&
454 (int) (nbytes_lb - stop_count) > 0)
455 nbytes_lb = stop_count;
457 nbytes_ub = mite_bytes_read_from_memory_ub(mite, AO_DMA_CHAN);
458 if(async->cmd.stop_src == TRIG_COUNT &&
459 (int) (nbytes_ub - stop_count) > 0)
460 nbytes_ub = stop_count;
461 if((int)(nbytes_ub - devpriv->last_buf_write_count) > 0){
462 rt_printk("ni_mio_common: DMA underrun\n");
464 async->events |= COMEDI_CB_OVERFLOW;
468 devpriv->last_buf_write_count = new_write_count;
470 count = nbytes_lb - async->buf_read_count;
474 comedi_buf_read_free(async, count);
476 async->events |= COMEDI_CB_BLOCK;
479 static int ni_ao_wait_for_dma_load( comedi_device *dev )
481 static const int timeout = 10000;
484 for(i = 0; i < timeout; i++)
486 unsigned short b_status;
488 b_status = win_in( AO_Status_1_Register );
489 if( b_status & AO_FIFO_Half_Full_St )
491 /* if we poll too often, the pci bus activity seems
492 to slow the dma transfer down */
497 comedi_error(dev, "timed out waiting for dma load");
504 static void ni_handle_eos(comedi_device *dev, comedi_subdevice *s)
506 if(devpriv->aimode == AIMODE_SCAN)
509 static const int timeout = 10;
512 for(i = 0; i < timeout; i++)
514 ni_sync_ai_dma(devpriv->mite, dev);
515 if((s->async->events & COMEDI_CB_EOS)) break;
519 ni_handle_fifo_dregs(dev);
520 s->async->events |= COMEDI_CB_EOS;
523 /* handle special case of single scan using AI_End_On_End_Of_Scan */
524 if((devpriv->ai_cmd2 & AI_End_On_End_Of_Scan)){
525 shutdown_ai_command( dev );
530 static void shutdown_ai_command( comedi_device *dev )
532 comedi_subdevice *s = dev->subdevices + 0;
535 ni_ai_drain_dma( dev );
536 mite_dma_disarm(devpriv->mite, AI_DMA_CHAN);
538 ni_handle_fifo_dregs(dev);
539 get_last_sample_611x(dev);
541 ni_set_bits(dev, Interrupt_A_Enable_Register,
542 AI_SC_TC_Interrupt_Enable | AI_START1_Interrupt_Enable|
543 AI_START2_Interrupt_Enable| AI_START_Interrupt_Enable|
544 AI_STOP_Interrupt_Enable| AI_Error_Interrupt_Enable|
545 AI_FIFO_Interrupt_Enable,0);
547 s->async->events |= COMEDI_CB_EOA;
550 static void handle_a_interrupt(comedi_device *dev,unsigned short status,
551 unsigned int m_status)
553 comedi_subdevice *s=dev->subdevices+0;
554 unsigned short ack=0;
556 s->async->events = 0;
558 #ifdef DEBUG_INTERRUPT
559 rt_printk("ni_mio_common: interrupt: a_status=%04x m0_status=%08x\n",
561 ni_mio_print_status_a(status);
566 /* Currently, mite.c requires us to handle LINKC and DONE */
567 if(m_status & CHSR_LINKC){
568 writel(CHOR_CLRLC, devpriv->mite->mite_io_addr + MITE_CHOR(AI_DMA_CHAN));
569 ni_sync_ai_dma(devpriv->mite, dev);
572 if(m_status & CHSR_DONE){
573 writel(CHOR_CLRDONE, devpriv->mite->mite_io_addr + MITE_CHOR(AI_DMA_CHAN));
576 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)){
577 printk("unknown mite interrupt, ack! (m_status=%08x)\n", m_status);
578 //mite_print_chsr(m_status);
579 mite_dma_disarm(devpriv->mite, AI_DMA_CHAN );
580 writel(CHOR_DMARESET, devpriv->mite->mite_io_addr + MITE_CHOR(AI_DMA_CHAN));
581 //disable_irq(dev->irq);
585 /* test for all uncommon interrupt events at the same time */
586 if(status&(AI_Overrun_St|AI_Overflow_St|AI_SC_TC_Error_St|AI_SC_TC_St|AI_START1_St)){
588 rt_printk("ni_mio_common: a_status=0xffff. Card removed?\n");
589 /* we probably aren't even running a command now,
590 * so it's a good idea to be careful. */
591 if(s->subdev_flags&SDF_RUNNING){
592 s->async->events |= COMEDI_CB_ERROR | COMEDI_CB_EOA;
593 //comedi_event(dev,s,s->async->events);
597 if(status&(AI_Overrun_St|AI_Overflow_St|AI_SC_TC_Error_St)){
598 rt_printk("ni_mio_common: ai error a_status=%04x\n",
600 ni_mio_print_status_a(status);
602 ni_ai_reset(dev,dev->subdevices);
605 shutdown_ai_command( dev );
607 s->async->events |= COMEDI_CB_ERROR;
608 if(status & (AI_Overrun_St | AI_Overflow_St))
609 s->async->events |= COMEDI_CB_OVERFLOW;
611 comedi_event(dev,s,s->async->events);
615 if(status&AI_SC_TC_St){
616 #ifdef DEBUG_INTERRUPT
617 rt_printk("ni_mio_common: SC_TC interrupt\n");
619 if(!devpriv->ai_continuous){
620 shutdown_ai_command( dev );
622 ack|=AI_SC_TC_Interrupt_Ack;
624 if(status&AI_START1_St){
625 ack|=AI_START1_Interrupt_Ack;
629 if(status&AI_FIFO_Half_Full_St){
631 static const int timeout = 10;
632 /* pcmcia cards (at least 6036) seem to stop producing interrupts if we
633 *fail to get the fifo less than half full, so loop to be sure.*/
634 for(i = 0; i < timeout; ++i)
636 ni_handle_fifo_half_full(dev);
637 if((win_in(AI_Status_1_Register) & AI_FIFO_Half_Full_St) == 0)
643 if( (status & AI_STOP_St) ){
644 ni_handle_eos(dev, s);
645 /* we need to ack the START, also */
646 ack |= AI_STOP_Interrupt_Ack|AI_START_Interrupt_Ack;
649 if(devpriv->aimode==AIMODE_SAMPLE){
650 ni_handle_fifo_dregs(dev);
652 //s->async->events |= COMEDI_CB_SAMPLE;
655 if(ack) win_out(ack,Interrupt_A_Ack_Register);
657 comedi_event(dev,s,s->async->events);
659 #ifdef DEBUG_INTERRUPT
660 status=win_in(AI_Status_1_Register);
661 if(status&Interrupt_A_St){
662 printk("handle_a_interrupt: didn't clear interrupt? status=0x%x\n", status);
667 static void handle_b_interrupt(comedi_device *dev,unsigned short b_status, unsigned int m_status)
669 comedi_subdevice *s=dev->subdevices+1;
670 //unsigned short ack=0;
671 #ifdef DEBUG_INTERRUPT
672 rt_printk("ni_mio_common: interrupt: b_status=%04x m1_status=%08x\n",
674 ni_mio_print_status_b(b_status);
679 /* Currently, mite.c requires us to handle LINKC and DONE */
680 if(m_status & CHSR_LINKC){
681 mite_handle_b_linkc(devpriv->mite, dev);
684 if(m_status & CHSR_DONE){
685 writel(CHOR_CLRDONE, devpriv->mite->mite_io_addr + MITE_CHOR(AO_DMA_CHAN));
688 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)){
689 printk("unknown mite interrupt, ack! (m_status=%08x)\n", m_status);
690 //mite_print_chsr(m_status);
691 mite_dma_disarm(devpriv->mite, AO_DMA_CHAN );
692 writel(CHOR_DMARESET, devpriv->mite->mite_io_addr + MITE_CHOR(AO_DMA_CHAN));
696 if(b_status==0xffff)return;
697 if(b_status&AO_Overrun_St){
698 rt_printk("ni_mio_common: AO FIFO underrun status=0x%04x status2=0x%04x\n",b_status,win_in(AO_Status_2_Register));
700 s->async->events |= COMEDI_CB_OVERFLOW;
703 if(b_status&AO_BC_TC_St){
704 MDPRINTK("ni_mio_common: AO BC_TC status=0x%04x status2=0x%04x\n",b_status,win_in(AO_Status_2_Register));
706 s->async->events |= COMEDI_CB_EOA;
710 if(b_status&AO_FIFO_Request_St){
713 ret = ni_ao_fifo_half_empty(dev,s);
715 rt_printk("ni_mio_common: AO buffer underrun\n");
716 ni_set_bits(dev, Interrupt_B_Enable_Register,
717 AO_FIFO_Interrupt_Enable|AO_Error_Interrupt_Enable, 0);
718 s->async->events |= COMEDI_CB_OVERFLOW;
723 b_status=win_in(AO_Status_1_Register);
724 if(b_status&Interrupt_B_St){
725 if(b_status&AO_FIFO_Request_St){
726 rt_printk("ni_mio_common: AO buffer underrun\n");
728 rt_printk("Ack! didn't clear AO interrupt. b_status=0x%04x\n",b_status);
729 ni_set_bits(dev,Interrupt_B_Enable_Register,~0,0);
731 s->async->events |= COMEDI_CB_OVERFLOW;
734 comedi_event(dev,s,s->async->events);
737 #ifdef DEBUG_STATUS_A
738 static char *status_a_strings[]={
739 "passthru0","fifo","G0_gate","G0_TC",
740 "stop","start","sc_tc","start1",
741 "start2","sc_tc_error","overflow","overrun",
742 "fifo_empty","fifo_half_full","fifo_full","interrupt_a"
745 static void ni_mio_print_status_a(int status)
749 rt_printk("A status:");
752 rt_printk(" %s",status_a_strings[i]);
759 #ifdef DEBUG_STATUS_B
760 static char *status_b_strings[]={
761 "passthru1","fifo","G1_gate","G1_TC",
762 "UI2_TC","UPDATE","UC_TC","BC_TC",
763 "start1","overrun","start","bc_tc_error",
764 "fifo_empty","fifo_half_full","fifo_full","interrupt_b"
767 static void ni_mio_print_status_b(int status)
771 rt_printk("B status:");
774 rt_printk(" %s",status_b_strings[i]);
783 static void ni_ao_fifo_load(comedi_device *dev,comedi_subdevice *s, int n)
785 comedi_async *async = s->async;
786 comedi_cmd *cmd = &async->cmd;
794 chan = async->cur_chan;
796 err &= comedi_buf_get(async, &d);
799 range = CR_RANGE(cmd->chanlist[chan]);
801 if(boardtype.reg_type & ni_reg_6xxx_mask)
803 packed_data = d & 0xffff;
804 /* 6711 only has 16 bit wide ao fifo */
805 if(boardtype.reg_type != ni_reg_6711)
807 err &= comedi_buf_get(async, &d);
811 packed_data |= ( d << 16 ) & 0xffff0000;
813 ni_writel( packed_data, DAC_FIFO_Data_611x );
815 ni_writew(d, DAC_FIFO_Data);
818 chan %= cmd->chanlist_len;
820 async->cur_chan = chan;
822 async->events |= COMEDI_CB_OVERFLOW;
827 * There's a small problem if the FIFO gets really low and we
828 * don't have the data to fill it. Basically, if after we fill
829 * the FIFO with all the data available, the FIFO is _still_
830 * less than half full, we never clear the interrupt. If the
831 * IRQ is in edge mode, we never get another interrupt, because
832 * this one wasn't cleared. If in level mode, we get flooded
833 * with interrupts that we can't fulfill, because nothing ever
834 * gets put into the buffer.
836 * This kind of situation is recoverable, but it is easier to
837 * just pretend we had a FIFO underrun, since there is a good
838 * chance it will happen anyway. This is _not_ the case for
839 * RT code, as RT code might purposely be running close to the
840 * metal. Needs to be fixed eventually.
842 static int ni_ao_fifo_half_empty(comedi_device *dev,comedi_subdevice *s)
846 n = comedi_buf_read_n_available(s);
848 s->async->events |= COMEDI_CB_OVERFLOW;
852 n /= sizeof(sampl_t);
853 if(n > boardtype.ao_fifo_depth / 2)
854 n = boardtype.ao_fifo_depth / 2;
856 ni_ao_fifo_load(dev,s,n);
858 s->async->events |= COMEDI_CB_BLOCK;
863 static int ni_ao_prep_fifo(comedi_device *dev,comedi_subdevice *s)
868 win_out(0,DAC_FIFO_Clear);
869 if(boardtype.reg_type & ni_reg_6xxx_mask)
870 ni_ao_win_outl(dev, 0x6, AO_FIFO_Offset_Load_611x);
873 n = comedi_buf_read_n_available(s);
876 n /= sizeof(sampl_t);
877 if(n > boardtype.ao_fifo_depth)
878 n = boardtype.ao_fifo_depth;
880 ni_ao_fifo_load(dev,s,n);
885 static void ni_ai_fifo_read(comedi_device *dev,comedi_subdevice *s,
888 comedi_async *async = s->async;
891 if(boardtype.reg_type == ni_reg_611x){
895 for( i = 0; i < n / 2; i++ ){
896 dl=ni_readl(ADC_FIFO_Data_611x);
897 /* This may get the hi/lo data in the wrong order */
898 data[0] = (dl>>16) & 0xffff;
899 data[1] = dl & 0xffff;
900 cfc_write_array_to_buffer(s, data, sizeof(data));
902 /* Check if there's a single sample stuck in the FIFO */
904 dl=ni_readl(ADC_FIFO_Data_611x);
905 data[0] = dl & 0xffff;
906 cfc_write_to_buffer(s, data[0]);
909 if( n > sizeof(devpriv->ai_fifo_buffer) / sizeof(devpriv->ai_fifo_buffer[0]))
911 comedi_error( dev, "bug! ai_fifo_buffer too small" );
912 async->events |= COMEDI_CB_ERROR;
915 for(i = 0; i < n; i++){
916 devpriv->ai_fifo_buffer[i] = ni_readw(ADC_FIFO_Data_Register);
918 cfc_write_array_to_buffer( s, devpriv->ai_fifo_buffer,
919 n * sizeof(devpriv->ai_fifo_buffer[0]) );
923 static void ni_handle_fifo_half_full(comedi_device *dev)
926 comedi_subdevice *s=dev->subdevices+0;
928 n=boardtype.ai_fifo_depth/2;
930 ni_ai_fifo_read(dev,s,n);
935 static int ni_ai_drain_dma(comedi_device *dev )
937 struct mite_struct *mite = devpriv->mite;
939 static const int timeout = 10000;
941 for( i = 0; i < timeout; i++ )
943 if((win_in(AI_Status_1_Register) & AI_FIFO_Empty_St) &&
944 mite_bytes_in_transit(mite, AI_DMA_CHAN) == 0)
950 rt_printk("ni_mio_common: wait for dma drain timed out\n");
951 rt_printk("mite_bytes_in_transit=%i, AI_Status1_Register=0x%x\n",
952 mite_bytes_in_transit(mite, AI_DMA_CHAN), win_in(AI_Status_1_Register));
956 ni_sync_ai_dma( mite, dev );
964 static void ni_handle_fifo_dregs(comedi_device *dev)
966 comedi_subdevice *s=dev->subdevices+0;
972 if(boardtype.reg_type == ni_reg_611x){
973 while((win_in(AI_Status_1_Register)&AI_FIFO_Empty_St) == 0){
974 dl=ni_readl(ADC_FIFO_Data_611x);
976 /* This may get the hi/lo data in the wrong order */
978 data[1] = (dl&0xffff);
979 cfc_write_array_to_buffer(s, data, sizeof(data));
982 fifo_empty = win_in(AI_Status_1_Register) & AI_FIFO_Empty_St;
983 while(fifo_empty == 0)
985 for(i = 0; i < sizeof(devpriv->ai_fifo_buffer) / sizeof(devpriv->ai_fifo_buffer[0]); i++)
987 fifo_empty = win_in(AI_Status_1_Register) & AI_FIFO_Empty_St;
988 if(fifo_empty) break;
989 devpriv->ai_fifo_buffer[i] = ni_readw(ADC_FIFO_Data_Register);
991 cfc_write_array_to_buffer( s, devpriv->ai_fifo_buffer,
992 i * sizeof(devpriv->ai_fifo_buffer[0]) );
997 static void get_last_sample_611x( comedi_device *dev )
999 comedi_subdevice *s=dev->subdevices+0;
1003 if(boardtype.reg_type != ni_reg_611x) return;
1005 /* Check if there's a single sample stuck in the FIFO */
1006 if(ni_readb(XXX_Status)&0x80){
1007 dl=ni_readl(ADC_FIFO_Data_611x);
1009 cfc_write_to_buffer(s, data);
1013 static void ni_ai_munge(comedi_device *dev, comedi_subdevice *s,
1014 void *data, unsigned int num_bytes, unsigned int chan_index )
1016 comedi_async *async = s->async;
1018 unsigned int length = num_bytes / sizeof( sampl_t );
1019 sampl_t *array = data;
1021 for(i = 0; i < length; i++)
1024 array[i] = le16_to_cpu(array[i]);
1026 array[i] += devpriv->ai_offset[ chan_index ];
1028 chan_index %= async->cmd.chanlist_len;
1034 static void ni_ai_setup_MITE_dma(comedi_device *dev,comedi_cmd *cmd)
1036 struct mite_struct *mite = devpriv->mite;
1037 struct mite_channel *mite_chan = &mite->channels[ AI_DMA_CHAN ];
1038 comedi_subdevice *s = dev->subdevices + 0;
1040 /* write alloc the entire buffer */
1041 comedi_buf_write_alloc(s->async, s->async->prealloc_bufsz);
1043 mite_chan->current_link = 0;
1044 mite_chan->dir = COMEDI_INPUT;
1045 if(boardtype.reg_type == ni_reg_611x)
1046 mite_prep_dma(mite, AI_DMA_CHAN, 32, 16);
1048 mite_prep_dma(mite, AI_DMA_CHAN, 16, 16);
1051 mite_dma_arm(mite, AI_DMA_CHAN);
1054 static void ni_ao_setup_MITE_dma(comedi_device *dev,comedi_cmd *cmd)
1056 struct mite_struct *mite = devpriv->mite;
1057 struct mite_channel *mite_chan = &mite->channels[ AO_DMA_CHAN ];
1058 comedi_subdevice *s = dev->subdevices + 1;
1060 devpriv->last_buf_write_count = s->async->buf_write_count;
1062 mite_chan->current_link = 0;
1063 mite_chan->dir = COMEDI_OUTPUT;
1064 if(boardtype.reg_type & (ni_reg_611x | ni_reg_6713))
1065 mite_prep_dma(mite, AO_DMA_CHAN, 32, 32);
1067 mite_prep_dma(mite, AO_DMA_CHAN, 16, 16);
1070 mite_dma_arm(mite, AO_DMA_CHAN);
1076 used for both cancel ioctl and board initialization
1078 this is pretty harsh for a cancel, but it works...
1081 static int ni_ai_reset(comedi_device *dev,comedi_subdevice *s)
1084 mite_dma_disarm(devpriv->mite, AI_DMA_CHAN);
1086 /* ai configuration */
1087 win_out(AI_Configuration_Start | AI_Reset, Joint_Reset_Register);
1089 ni_set_bits(dev, Interrupt_A_Enable_Register,
1090 AI_SC_TC_Interrupt_Enable | AI_START1_Interrupt_Enable|
1091 AI_START2_Interrupt_Enable| AI_START_Interrupt_Enable|
1092 AI_STOP_Interrupt_Enable| AI_Error_Interrupt_Enable|
1093 AI_FIFO_Interrupt_Enable,0);
1095 win_out(1,ADC_FIFO_Clear);
1097 ni_writeb(0, Misc_Command);
1099 win_out(AI_Disarm, AI_Command_1_Register); /* reset pulses */
1100 win_out(AI_Start_Stop | AI_Mode_1_Reserved /*| AI_Trigger_Once */,
1101 AI_Mode_1_Register);
1102 win_out(0x0000,AI_Mode_2_Register);
1103 /* generate FIFO interrupts on non-empty */
1104 win_out((0<<6)|0x0000,AI_Mode_3_Register);
1105 if(boardtype.reg_type == ni_reg_normal){
1106 win_out(AI_SHIFTIN_Pulse_Width |
1108 AI_CONVERT_Pulse_Width |
1109 AI_LOCALMUX_CLK_Pulse_Width, AI_Personal_Register);
1110 win_out(AI_SCAN_IN_PROG_Output_Select(3) |
1111 AI_EXTMUX_CLK_Output_Select(0) |
1112 AI_LOCALMUX_CLK_Output_Select(2) |
1113 AI_SC_TC_Output_Select(3) |
1114 AI_CONVERT_Output_Select(2),AI_Output_Control_Register);
1115 }else{/* 611x boards */
1116 win_out(AI_SHIFTIN_Pulse_Width |
1118 AI_LOCALMUX_CLK_Pulse_Width, AI_Personal_Register);
1119 win_out(AI_SCAN_IN_PROG_Output_Select(3) |
1120 AI_EXTMUX_CLK_Output_Select(0) |
1121 AI_LOCALMUX_CLK_Output_Select(2) |
1122 AI_SC_TC_Output_Select(3) |
1123 AI_CONVERT_Output_Select(3),AI_Output_Control_Register);
1126 /* the following registers should not be changed, because there
1127 * are no backup registers in devpriv. If you want to change
1128 * any of these, add a backup register and other appropriate code:
1129 * AI_Mode_1_Register
1130 * AI_Mode_3_Register
1131 * AI_Personal_Register
1132 * AI_Output_Control_Register
1134 win_out(AI_SC_TC_Error_Confirm | AI_START_Interrupt_Ack |
1135 AI_START2_Interrupt_Ack | AI_START1_Interrupt_Ack |
1136 AI_SC_TC_Interrupt_Ack | AI_Error_Interrupt_Ack |
1137 AI_STOP_Interrupt_Ack, Interrupt_A_Ack_Register); /* clear interrupts */
1139 win_out(AI_Configuration_End,Joint_Reset_Register);
1144 static int ni_ai_poll(comedi_device *dev,comedi_subdevice *s)
1146 unsigned long flags = 0;
1149 // lock to avoid race with interrupt handler
1150 if(in_interrupt() == 0)
1151 comedi_spin_lock_irqsave(&dev->spinlock, flags);
1153 ni_handle_fifo_dregs(dev);
1155 ni_sync_ai_dma(devpriv->mite, dev);
1157 count = s->async->buf_write_count - s->async->buf_read_count;
1158 if(in_interrupt() == 0)
1159 comedi_spin_unlock_irqrestore(&dev->spinlock, flags);
1165 static int ni_ai_insn_read(comedi_device *dev,comedi_subdevice *s,comedi_insn *insn,lsampl_t *data)
1169 unsigned short signbits;
1172 ni_load_channelgain_list(dev,1,&insn->chanspec);
1174 win_out(1,ADC_FIFO_Clear);
1176 mask=(1<<boardtype.adbits)-1;
1177 signbits=devpriv->ai_offset[0];
1178 if(boardtype.reg_type == ni_reg_611x){
1179 for(n=0; n < num_adc_stages_611x; n++){
1180 win_out(AI_CONVERT_Pulse, AI_Command_1_Register);
1183 for(n=0; n<insn->n; n++){
1184 win_out(AI_CONVERT_Pulse, AI_Command_1_Register);
1185 /* The 611x has screwy 32-bit FIFOs. */
1187 for(i=0; i<NI_TIMEOUT; i++){
1188 if(ni_readb(XXX_Status)&0x80)
1190 d = ( ni_readl(ADC_FIFO_Data_611x) >> 16 ) & 0xffff;
1193 if(!(win_in(AI_Status_1_Register)&AI_FIFO_Empty_St))
1195 d = ni_readl(ADC_FIFO_Data_611x) & 0xffff;
1200 rt_printk("ni_mio_common: timeout in 611x ni_ai_insn_read\n");
1203 d += signbits; /* subtle: needs to be short addition */
1207 for(n=0;n<insn->n;n++){
1208 win_out(AI_CONVERT_Pulse, AI_Command_1_Register);
1209 for(i=0;i<NI_TIMEOUT;i++){
1210 if(!(win_in(AI_Status_1_Register)&AI_FIFO_Empty_St))
1214 rt_printk("ni_mio_common: timeout in ni_ai_insn_read\n");
1217 d = ni_readw(ADC_FIFO_Data_Register);
1218 d += signbits; /* subtle: needs to be short addition */
1226 * Notes on the 6110 and 6111:
1227 * These boards a slightly different than the rest of the series, since
1228 * they have multiple A/D converters.
1229 * From the driver side, the configuration memory is a
1231 * Configuration Memory Low:
1233 * bit 8: unipolar/bipolar (should be 0 for bipolar)
1234 * bits 0-3: gain. This is 4 bits instead of 3 for the other boards
1235 * 1001 gain=0.1 (+/- 50)
1244 * Configuration Memory High:
1245 * bits 12-14: Channel Type
1246 * 001 for differential
1247 * 000 for calibration
1248 * bit 11: coupling (this is not currently handled)
1252 * valid channels are 0-3
1254 static void ni_load_channelgain_list(comedi_device *dev,unsigned int n_chan,
1257 unsigned int chan,range,aref;
1260 unsigned short offset;
1261 unsigned int dither;
1263 if(n_chan == 1 && boardtype.reg_type == ni_reg_normal){
1264 if(devpriv->changain_state && devpriv->changain_spec==list[0]){
1268 devpriv->changain_state=1;
1269 devpriv->changain_spec=list[0];
1271 devpriv->changain_state=0;
1274 win_out(1,Configuration_Memory_Clear);
1276 offset=1<<(boardtype.adbits-1);
1277 for(i=0;i<n_chan;i++){
1278 if(list[i]&CR_ALT_SOURCE){
1279 chan=devpriv->ai_calib_source;
1281 chan=CR_CHAN(list[i]);
1283 aref=CR_AREF(list[i]);
1284 range=CR_RANGE(list[i]);
1285 dither=((list[i]&CR_ALT_FILTER)!=0);
1287 /* fix the external/internal range differences */
1288 range = ni_gainlkup[boardtype.gainlkup][range];
1289 if(boardtype.reg_type == ni_reg_611x)
1290 devpriv->ai_offset[i] = offset;
1292 devpriv->ai_offset[i] = (range&0x100)?0:offset;
1295 if( ( list[i] & CR_ALT_SOURCE ) )
1297 if(boardtype.reg_type == ni_reg_611x)
1298 ni_writew(CR_CHAN(list[i])&0x0003, Calibration_Channel_Select_611x);
1301 if(boardtype.reg_type == ni_reg_611x)
1306 hi |= AI_DIFFERENTIAL;
1318 hi |= AI_CONFIG_CHANNEL( chan );
1320 ni_writew(hi,Configuration_Memory_High);
1323 if(i == n_chan - 1) lo |= AI_LAST_CHANNEL;
1324 if( dither ) lo |= AI_DITHER;
1326 ni_writew(lo,Configuration_Memory_Low);
1329 /* prime the channel/gain list */
1330 if(boardtype.reg_type == ni_reg_normal){
1331 win_out(AI_CONVERT_Pulse, AI_Command_1_Register);
1332 for(i=0;i<NI_TIMEOUT;i++){
1333 if(!(win_in(AI_Status_1_Register)&AI_FIFO_Empty_St)){
1334 win_out(1,ADC_FIFO_Clear);
1339 rt_printk("ni_mio_common: timeout loading channel/gain list\n");
1343 #define TIMER_BASE 50 /* 20 Mhz base */
1345 static int ni_ns_to_timer(int *nanosec,int round_mode)
1352 case TRIG_ROUND_NEAREST:
1354 divider=(*nanosec+base/2)/base;
1356 case TRIG_ROUND_DOWN:
1357 divider=(*nanosec)/base;
1360 divider=(*nanosec+base-1)/base;
1364 *nanosec=base*divider;
1368 static int ni_ai_cmdtest(comedi_device *dev,comedi_subdevice *s,comedi_cmd *cmd)
1374 /* step 1: make sure trigger sources are trivially valid */
1377 cmd->start_src &= TRIG_NOW|TRIG_INT|TRIG_EXT;
1378 if(!cmd->start_src || tmp!=cmd->start_src)err++;
1380 tmp=cmd->scan_begin_src;
1381 cmd->scan_begin_src &= TRIG_TIMER|TRIG_EXT;
1382 if(!cmd->scan_begin_src || tmp!=cmd->scan_begin_src)err++;
1384 tmp=cmd->convert_src;
1385 sources = TRIG_TIMER | TRIG_EXT;
1386 if(boardtype.reg_type == ni_reg_611x) sources |= TRIG_NOW;
1387 cmd->convert_src &= sources;
1388 if(!cmd->convert_src || tmp!=cmd->convert_src)err++;
1390 tmp=cmd->scan_end_src;
1391 cmd->scan_end_src &= TRIG_COUNT;
1392 if(!cmd->scan_end_src || tmp!=cmd->scan_end_src)err++;
1395 cmd->stop_src &= TRIG_COUNT|TRIG_NONE;
1396 if(!cmd->stop_src || tmp!=cmd->stop_src)err++;
1400 /* step 2: make sure trigger sources are unique and mutually compatible */
1402 /* note that mutual compatiblity is not an issue here */
1403 if(cmd->start_src!=TRIG_NOW &&
1404 cmd->start_src!=TRIG_INT &&
1405 cmd->start_src!=TRIG_EXT)err++;
1406 if(cmd->scan_begin_src!=TRIG_TIMER &&
1407 cmd->scan_begin_src!=TRIG_EXT &&
1408 cmd->scan_begin_src!=TRIG_OTHER)err++;
1409 if(cmd->convert_src!=TRIG_TIMER &&
1410 cmd->convert_src!=TRIG_EXT &&
1411 cmd->convert_src!=TRIG_NOW)err++;
1412 if(cmd->stop_src!=TRIG_COUNT &&
1413 cmd->stop_src!=TRIG_NONE)err++;
1417 /* step 3: make sure arguments are trivially compatible */
1419 if(cmd->start_src==TRIG_EXT){
1420 /* external trigger */
1421 unsigned int tmp = CR_CHAN(cmd->start_arg);
1423 if(tmp > 9) tmp = 9;
1424 tmp |= (cmd->start_arg & (CR_INVERT | CR_EDGE));
1425 if(cmd->start_arg != tmp){
1426 cmd->start_arg = tmp;
1430 if(cmd->start_arg!=0){
1431 /* true for both TRIG_NOW and TRIG_INT */
1436 if(cmd->scan_begin_src==TRIG_TIMER){
1437 if(cmd->scan_begin_arg<boardtype.ai_speed){
1438 cmd->scan_begin_arg=boardtype.ai_speed;
1441 if(cmd->scan_begin_arg>TIMER_BASE*0xffffff){
1442 cmd->scan_begin_arg=TIMER_BASE*0xffffff;
1445 }else if(cmd->scan_begin_src==TRIG_EXT){
1446 /* external trigger */
1447 unsigned int tmp = CR_CHAN(cmd->scan_begin_arg);
1450 tmp |= (cmd->scan_begin_arg & (CR_INVERT | CR_EDGE));
1451 if(cmd->scan_begin_arg!=tmp){
1452 cmd->scan_begin_arg = tmp;
1455 }else{ /* TRIG_OTHER */
1456 if(cmd->scan_begin_arg){
1457 cmd->scan_begin_arg=0;
1461 if(cmd->convert_src==TRIG_TIMER){
1462 if(boardtype.reg_type == ni_reg_611x){
1463 if(cmd->convert_arg != 0){
1464 cmd->convert_arg = 0;
1468 if(cmd->convert_arg<boardtype.ai_speed){
1469 cmd->convert_arg=boardtype.ai_speed;
1472 if(cmd->convert_arg>TIMER_BASE*0xffff){
1473 cmd->convert_arg=TIMER_BASE*0xffff;
1477 }else if(cmd->convert_src == TRIG_EXT){
1478 /* external trigger */
1479 unsigned int tmp = CR_CHAN(cmd->convert_arg);
1482 tmp |= (cmd->convert_arg&(CR_ALT_FILTER|CR_INVERT));
1483 if(cmd->convert_arg!=tmp){
1484 cmd->convert_arg = tmp;
1487 }else if(cmd->convert_src == TRIG_NOW){
1488 if(cmd->convert_arg != 0){
1489 cmd->convert_arg = 0;
1494 if(cmd->scan_end_arg!=cmd->chanlist_len){
1495 cmd->scan_end_arg=cmd->chanlist_len;
1498 if(cmd->stop_src==TRIG_COUNT){
1499 unsigned int max_count = 0x01000000;
1501 if(boardtype.reg_type == ni_reg_611x )
1502 max_count -= num_adc_stages_611x;
1503 if(cmd->stop_arg > max_count){
1504 cmd->stop_arg = max_count;
1507 if(cmd->stop_arg < 1){
1513 if(cmd->stop_arg!=0){
1521 /* step 4: fix up any arguments */
1523 if(cmd->scan_begin_src==TRIG_TIMER){
1524 tmp=cmd->scan_begin_arg;
1525 ni_ns_to_timer(&cmd->scan_begin_arg,cmd->flags&TRIG_ROUND_MASK);
1526 if(tmp!=cmd->scan_begin_arg)err++;
1528 if(cmd->convert_src==TRIG_TIMER){
1529 if(boardtype.reg_type == ni_reg_normal){
1530 tmp=cmd->convert_arg;
1531 ni_ns_to_timer(&cmd->convert_arg,cmd->flags&TRIG_ROUND_MASK);
1532 if(tmp!=cmd->convert_arg)err++;
1533 if(cmd->scan_begin_src==TRIG_TIMER &&
1534 cmd->scan_begin_arg<cmd->convert_arg*cmd->scan_end_arg){
1535 cmd->scan_begin_arg=cmd->convert_arg*cmd->scan_end_arg;
1546 static int ni_ai_cmd(comedi_device *dev,comedi_subdevice *s)
1548 comedi_cmd *cmd=&s->async->cmd;
1550 int mode1=0; /* mode1 is needed for both stop and convert */
1552 int start_stop_select=0;
1553 unsigned int stop_count;
1554 int interrupt_a_enable=0;
1556 MDPRINTK("ni_ai_cmd\n");
1559 comedi_error(dev, "cannot run command without an irq");
1562 win_out(1,ADC_FIFO_Clear);
1564 ni_load_channelgain_list(dev,cmd->chanlist_len,cmd->chanlist);
1566 /* start configuration */
1567 win_out(AI_Configuration_Start,Joint_Reset_Register);
1569 /* disable analog triggering for now, since it
1570 * interferes with the use of pfi0 */
1571 devpriv->an_trig_etc_reg &= ~Analog_Trigger_Enable;
1572 win_out(devpriv->an_trig_etc_reg, Analog_Trigger_Etc_Register);
1574 switch(cmd->start_src){
1577 win_out(AI_START2_Select(0)|
1578 AI_START1_Sync|AI_START1_Edge|AI_START1_Select(0),
1579 AI_Trigger_Select_Register);
1583 int chan = CR_CHAN(cmd->start_arg);
1584 unsigned int bits = AI_START2_Select(0)|
1586 AI_START1_Select(chan + 1);
1588 if(cmd->start_arg & CR_INVERT)
1589 bits |= AI_START1_Polarity;
1590 if(cmd->start_arg & CR_EDGE)
1591 bits |= AI_START1_Edge;
1592 win_out(bits, AI_Trigger_Select_Register);
1597 mode2 &= ~AI_Pre_Trigger;
1598 mode2 &= ~AI_SC_Initial_Load_Source;
1599 mode2 &= ~AI_SC_Reload_Mode;
1600 win_out(mode2, AI_Mode_2_Register);
1602 if(cmd->chanlist_len == 1 || boardtype.reg_type == ni_reg_611x){
1603 start_stop_select |= AI_STOP_Polarity;
1604 start_stop_select |= AI_STOP_Select( 31 ); // logic low
1605 start_stop_select |= AI_STOP_Sync;
1608 start_stop_select |= AI_STOP_Select(19); // ai configuration memory
1610 win_out(start_stop_select, AI_START_STOP_Select_Register);
1612 devpriv->ai_cmd2 = 0;
1613 switch(cmd->stop_src){
1615 stop_count = cmd->stop_arg - 1;
1617 if(boardtype.reg_type == ni_reg_611x){
1618 // have to take 3 stage adc pipeline into account
1619 stop_count += num_adc_stages_611x;
1621 /* stage number of scans */
1622 win_out2( stop_count, AI_SC_Load_A_Registers);
1624 mode1 |= AI_Start_Stop | AI_Mode_1_Reserved | AI_Trigger_Once;
1625 win_out(mode1,AI_Mode_1_Register);
1626 /* load SC (Scan Count) */
1627 win_out(AI_SC_Load,AI_Command_1_Register);
1629 devpriv->ai_continuous = 0;
1630 if( stop_count == 0 ){
1631 devpriv->ai_cmd2 |= AI_End_On_End_Of_Scan;
1632 interrupt_a_enable |= AI_STOP_Interrupt_Enable;
1633 // this is required to get the last sample for chanlist_len > 1, not sure why
1634 if(cmd->chanlist_len > 1)
1635 start_stop_select |= AI_STOP_Polarity | AI_STOP_Edge;
1639 /* stage number of scans */
1640 win_out2(0,AI_SC_Load_A_Registers);
1642 mode1 |= AI_Start_Stop | AI_Mode_1_Reserved | AI_Continuous;
1643 win_out(mode1,AI_Mode_1_Register);
1645 /* load SC (Scan Count) */
1646 win_out(AI_SC_Load,AI_Command_1_Register);
1648 devpriv->ai_continuous = 1;
1653 switch(cmd->scan_begin_src){
1656 stop bits for non 611x boards
1657 AI_SI_Special_Trigger_Delay=0
1659 AI_START_STOP_Select_Register:
1660 AI_START_Polarity=0 (?) rising edge
1661 AI_START_Edge=1 edge triggered
1663 AI_START_Select=0 SI_TC
1664 AI_STOP_Polarity=0 rising edge
1665 AI_STOP_Edge=0 level
1667 AI_STOP_Select=19 external pin (configuration mem)
1669 start_stop_select |= AI_START_Edge | AI_START_Sync;
1670 win_out(start_stop_select, AI_START_STOP_Select_Register);
1672 mode2 |= AI_SI_Reload_Mode(0);
1673 /* AI_SI_Initial_Load_Source=A */
1674 mode2 &= ~AI_SI_Initial_Load_Source;
1675 //mode2 |= AI_SC_Reload_Mode;
1676 win_out(mode2, AI_Mode_2_Register);
1679 timer=ni_ns_to_timer(&cmd->scan_begin_arg,TRIG_ROUND_NEAREST);
1680 win_out2(timer,AI_SI_Load_A_Registers);
1681 win_out(AI_SI_Load,AI_Command_1_Register);
1684 if( cmd->scan_begin_arg & CR_EDGE )
1685 start_stop_select |= AI_START_Edge;
1686 /* AI_START_Polarity==1 is falling edge */
1687 if( cmd->scan_begin_arg & CR_INVERT )
1688 start_stop_select |= AI_START_Polarity;
1689 if( cmd->scan_begin_src != cmd->convert_src ||
1690 ( cmd->scan_begin_arg & ~CR_EDGE ) != ( cmd->convert_arg & ~CR_EDGE ) )
1691 start_stop_select |= AI_START_Sync;
1692 start_stop_select |= AI_START_Select(1 + CR_CHAN(cmd->scan_begin_arg));
1693 win_out(start_stop_select, AI_START_STOP_Select_Register);
1697 switch(cmd->convert_src){
1700 if( cmd->convert_arg == 0 || cmd->convert_src == TRIG_NOW )
1703 timer=ni_ns_to_timer(&cmd->convert_arg, TRIG_ROUND_NEAREST);
1704 win_out(1,AI_SI2_Load_A_Register); /* 0,0 does not work. */
1705 win_out(timer,AI_SI2_Load_B_Register);
1707 /* AI_SI2_Reload_Mode = alternate */
1708 /* AI_SI2_Initial_Load_Source = A */
1709 mode2 &= ~AI_SI2_Initial_Load_Source;
1710 mode2 |= AI_SI2_Reload_Mode;
1711 win_out( mode2, AI_Mode_2_Register);
1714 win_out(AI_SI2_Load,AI_Command_1_Register);
1716 mode2 |= AI_SI2_Reload_Mode; // alternate
1717 mode2 |= AI_SI2_Initial_Load_Source; // B
1719 win_out(mode2,AI_Mode_2_Register);
1722 mode1 |= AI_CONVERT_Source_Select(1+cmd->convert_arg);
1723 if( ( cmd->convert_arg & CR_INVERT ) == 0 )
1724 mode1 |= AI_CONVERT_Source_Polarity;
1725 win_out(mode1,AI_Mode_1_Register);
1727 mode2 |= AI_Start_Stop_Gate_Enable | AI_SC_Gate_Enable;
1728 win_out(mode2, AI_Mode_2_Register);
1735 /* interrupt on FIFO, errors, SC_TC */
1736 interrupt_a_enable |= AI_Error_Interrupt_Enable|
1737 AI_SC_TC_Interrupt_Enable;
1740 interrupt_a_enable|=AI_FIFO_Interrupt_Enable;
1743 if(cmd->flags & TRIG_WAKE_EOS || (devpriv->ai_cmd2 & AI_End_On_End_Of_Scan)){
1744 /* wake on end-of-scan */
1745 devpriv->aimode=AIMODE_SCAN;
1747 devpriv->aimode=AIMODE_HALF_FULL;
1750 switch(devpriv->aimode){
1751 case AIMODE_HALF_FULL:
1752 /*generate FIFO interrupts and DMA requests on half-full */
1754 win_out(AI_FIFO_Mode_HF_to_E, AI_Mode_3_Register);
1756 win_out(AI_FIFO_Mode_HF, AI_Mode_3_Register);
1760 /*generate FIFO interrupts on non-empty */
1761 win_out(AI_FIFO_Mode_NE, AI_Mode_3_Register);
1765 win_out(AI_FIFO_Mode_NE, AI_Mode_3_Register);
1767 win_out(AI_FIFO_Mode_HF, AI_Mode_3_Register);
1769 interrupt_a_enable |= AI_STOP_Interrupt_Enable;
1775 win_out(0x3f80,Interrupt_A_Ack_Register); /* clear interrupts */
1777 ni_set_bits(dev, Interrupt_A_Enable_Register, interrupt_a_enable, 1);
1779 MDPRINTK("Interrupt_A_Enable_Register = 0x%04x\n",devpriv->int_a_enable_reg);
1781 /* interrupt on nothing */
1782 ni_set_bits(dev, Interrupt_A_Enable_Register, ~0, 0);
1784 /* XXX start polling if necessary */
1785 MDPRINTK("interrupting on nothing\n");
1788 /* end configuration */
1789 win_out(AI_Configuration_End,Joint_Reset_Register);
1791 switch(cmd->scan_begin_src){
1793 win_out(AI_SI2_Arm | AI_SI_Arm | AI_DIV_Arm | AI_SC_Arm,
1794 AI_Command_1_Register);
1797 /* XXX AI_SI_Arm? */
1798 win_out(AI_SI2_Arm | AI_SI_Arm | AI_DIV_Arm | AI_SC_Arm,
1799 AI_Command_1_Register);
1804 ni_ai_setup_MITE_dma(dev,cmd);
1805 //mite_dump_regs(devpriv->mite);
1808 switch(cmd->start_src){
1810 /* AI_START1_Pulse */
1811 win_out( AI_START1_Pulse | devpriv->ai_cmd2, AI_Command_2_Register );
1812 s->async->inttrig=NULL;
1815 s->async->inttrig=NULL;
1818 s->async->inttrig=ni_ai_inttrig;
1822 MDPRINTK("exit ni_ai_cmd\n");
1827 static int ni_ai_inttrig(comedi_device *dev,comedi_subdevice *s,
1828 unsigned int trignum)
1830 if(trignum!=0)return -EINVAL;
1832 win_out( AI_START1_Pulse | devpriv->ai_cmd2, AI_Command_2_Register );
1833 s->async->inttrig=NULL;
1838 static int ni_ai_config_analog_trig(comedi_device *dev,comedi_subdevice *s,
1839 comedi_insn *insn, lsampl_t *data);
1841 static int ni_ai_insn_config(comedi_device *dev,comedi_subdevice *s,
1842 comedi_insn *insn, lsampl_t *data)
1844 if(insn->n<1)return -EINVAL;
1847 case INSN_CONFIG_ANALOG_TRIG:
1848 return ni_ai_config_analog_trig(dev,s,insn,data);
1849 case INSN_CONFIG_ALT_SOURCE:
1851 unsigned int calib_source;
1852 unsigned int calib_source_adjust;
1854 calib_source = data[1] & 0xf;
1855 calib_source_adjust = ( data[1] >> 4 ) & 0xff;
1857 if(calib_source >= 8)
1859 devpriv->ai_calib_source = calib_source;
1860 if(boardtype.reg_type == ni_reg_611x){
1861 ni_writeb( calib_source_adjust, Cal_Gain_Select_611x );
1872 static int ni_ai_config_analog_trig(comedi_device *dev,comedi_subdevice *s,
1873 comedi_insn *insn, lsampl_t *data)
1875 unsigned int a,b,modebits;
1879 * data[2] is analog line
1880 * data[3] is set level
1881 * data[4] is reset level */
1882 if(!boardtype.has_analog_trig)return -EINVAL;
1883 if(insn->n!=5)return -EINVAL;
1884 if((data[1]&0xffff0000) != COMEDI_EV_SCAN_BEGIN){
1885 data[1]&=~(COMEDI_EV_SCAN_BEGIN&0xffff);
1888 if(data[2]>=boardtype.n_adchan){
1889 data[2]=boardtype.n_adchan-1;
1892 if(data[3]>255){ /* a */
1896 if(data[4]>255){ /* b */
1907 * high mode 00 00 01 10
1908 * low mode 00 00 10 01
1910 * hysteresis low mode 10 00 00 01
1911 * hysteresis high mode 01 00 00 10
1912 * middle mode 10 01 01 10
1917 modebits=data[1]&0xff;
1919 /* two level mode */
1924 modebits=((data[1]&0xf)<<4)|((data[1]&0xf0)>>4);
1926 devpriv->atrig_low = a;
1927 devpriv->atrig_high = b;
1929 case 0x81: /* low hysteresis mode */
1930 devpriv->atrig_mode = 6;
1932 case 0x42: /* high hysteresis mode */
1933 devpriv->atrig_mode = 3;
1935 case 0x96: /* middle window mode */
1936 devpriv->atrig_mode = 2;
1943 /* one level mode */
1949 case 0x06: /* high window mode */
1950 devpriv->atrig_high = a;
1951 devpriv->atrig_mode = 0;
1953 case 0x09: /* low window mode */
1954 devpriv->atrig_low = a;
1955 devpriv->atrig_mode = 1;
1962 if(err)return -EAGAIN;
1966 /* munge data from unsigned to 2's complement for analog output bipolar modes */
1967 static void ni_ao_munge(comedi_device *dev, comedi_subdevice *s,
1968 void *data, unsigned int num_bytes, unsigned int chan_index )
1970 comedi_async *async = s->async;
1973 unsigned int offset;
1974 unsigned int length = num_bytes / sizeof( sampl_t );
1975 sampl_t *array = data;
1977 offset = 1 << (boardtype.aobits - 1);
1978 for(i = 0; i < length; i++)
1980 range = CR_RANGE( async->cmd.chanlist[ chan_index ] );
1981 if(boardtype.ao_unipolar == 0 || (range & 1) == 0 )
1984 array[i] = cpu_to_le16( array[i] );
1987 chan_index %= async->cmd.chanlist_len;
1991 static int ni_ao_config_chanlist(comedi_device *dev, comedi_subdevice *s,
1992 unsigned int chanspec[], unsigned int n_chans)
2000 for(i=0;i<n_chans;i++){
2001 chan = CR_CHAN(chanspec[i]);
2002 range = CR_RANGE(chanspec[i]);
2004 conf = AO_Channel(chan);
2006 if(boardtype.ao_unipolar){
2009 invert = (1<<(boardtype.aobits-1));
2017 invert = (1<<(boardtype.aobits-1));
2020 /* not all boards can deglitch, but this shouldn't hurt */
2021 if(chanspec[i] & CR_DEGLITCH)
2022 conf |= AO_Deglitch;
2024 /* analog reference */
2025 /* AREF_OTHER connects AO ground to AI ground, i think */
2026 conf |= (CR_AREF(chanspec[i])==AREF_OTHER)? AO_Ground_Ref : 0;
2028 devpriv->ao_conf[chan] = conf;
2030 ni_writew(conf,AO_Configuration);
2035 static int ni_ao_insn_read(comedi_device *dev,comedi_subdevice *s,
2036 comedi_insn *insn,lsampl_t *data)
2038 data[0] = devpriv->ao[CR_CHAN(insn->chanspec)];
2043 static int ni_ao_insn_write(comedi_device *dev,comedi_subdevice *s,
2044 comedi_insn *insn,lsampl_t *data)
2046 unsigned int chan = CR_CHAN(insn->chanspec);
2047 unsigned int invert;
2049 invert = ni_ao_config_chanlist(dev,s,&insn->chanspec,1);
2051 devpriv->ao[chan] = data[0];
2053 ni_writew(data[0] ^ invert,(chan)? DAC1_Direct_Data : DAC0_Direct_Data);
2058 static int ni_ao_insn_write_671x(comedi_device *dev,comedi_subdevice *s,
2059 comedi_insn *insn,lsampl_t *data)
2061 unsigned int chan = CR_CHAN(insn->chanspec);
2062 unsigned int invert;
2064 ao_win_out(1 << chan, AO_Immediate_671x);
2065 invert = 1 << (boardtype.aobits - 1);
2067 ni_ao_config_chanlist(dev,s,&insn->chanspec,1);
2069 devpriv->ao[chan] = data[0];
2070 ao_win_out(data[0] ^ invert, DACx_Direct_Data_671x(chan));
2075 static int ni_ao_inttrig(comedi_device *dev,comedi_subdevice *s,
2076 unsigned int trignum)
2079 int interrupt_b_bits;
2081 static const int timeout = 1000;
2083 if(trignum!=0)return -EINVAL;
2085 ni_set_bits(dev, Interrupt_B_Enable_Register, AO_FIFO_Interrupt_Enable | AO_Error_Interrupt_Enable, 0);
2086 interrupt_b_bits = AO_Error_Interrupt_Enable;
2088 win_out(0, DAC_FIFO_Clear);
2089 if(boardtype.reg_type & ni_reg_6xxx_mask)
2090 ni_ao_win_outl(dev, 0x6, AO_FIFO_Offset_Load_611x);
2091 ni_ao_setup_MITE_dma(dev, &s->async->cmd);
2092 ret = ni_ao_wait_for_dma_load(dev);
2093 if(ret < 0) return ret;
2096 ret = ni_ao_prep_fifo(dev,s);
2097 if(ret==0)return -EPIPE;
2099 interrupt_b_bits |= AO_FIFO_Interrupt_Enable;
2102 win_out(devpriv->ao_mode3|AO_Not_An_UPDATE,AO_Mode_3_Register);
2103 win_out(devpriv->ao_mode3,AO_Mode_3_Register);
2104 /* wait for DACs to be loaded */
2105 for(i = 0; i < timeout; i++)
2108 if((win_in(Joint_Status_2_Register) & AO_TMRDACWRs_In_Progress_St) == 0)
2113 comedi_error(dev, "timed out waiting for AO_TMRDACWRs_In_Progress_St to clear");
2116 // stc manual says we are need to clear error interrupt after AO_TMRDACWRs_In_Progress_St clears
2117 win_out(AO_Error_Interrupt_Ack, Interrupt_B_Ack_Register);
2119 ni_set_bits(dev, Interrupt_B_Enable_Register, interrupt_b_bits, 1);
2121 win_out(devpriv->ao_cmd1|AO_UI_Arm|AO_UC_Arm|AO_BC_Arm|AO_DAC1_Update_Mode|AO_DAC0_Update_Mode,
2122 AO_Command_1_Register);
2124 win_out(devpriv->ao_cmd2|AO_START1_Pulse,AO_Command_2_Register);
2126 s->async->inttrig=NULL;
2131 static int ni_ao_cmd(comedi_device *dev,comedi_subdevice *s)
2133 comedi_cmd *cmd = &s->async->cmd;
2140 comedi_error(dev, "cannot run command without an irq");
2143 trigvar = ni_ns_to_timer(&cmd->scan_begin_arg,TRIG_ROUND_NEAREST);
2145 win_out(AO_Configuration_Start,Joint_Reset_Register);
2147 win_out(AO_Disarm,AO_Command_1_Register);
2149 if(boardtype.reg_type & ni_reg_6xxx_mask)
2151 ao_win_out(CLEAR_WG, AO_Misc_611x);
2154 for(i = 0; i < cmd->chanlist_len; i++)
2158 chan = CR_CHAN(cmd->chanlist[i]);
2160 ao_win_out(chan, AO_Waveform_Generation_611x);
2162 ao_win_out(bits, AO_Timed_611x);
2165 ni_ao_config_chanlist(dev,s,cmd->chanlist,cmd->chanlist_len);
2167 if(cmd->stop_src==TRIG_NONE){
2168 devpriv->ao_mode1|=AO_Continuous;
2169 devpriv->ao_mode1&=~AO_Trigger_Once;
2171 devpriv->ao_mode1&=~AO_Continuous;
2172 devpriv->ao_mode1|=AO_Trigger_Once;
2174 win_out(devpriv->ao_mode1,AO_Mode_1_Register);
2175 devpriv->ao_trigger_select&=~(AO_START1_Polarity|AO_START1_Select(-1));
2176 devpriv->ao_trigger_select|=AO_START1_Edge|AO_START1_Sync;
2177 win_out(devpriv->ao_trigger_select,AO_Trigger_Select_Register);
2178 devpriv->ao_mode3&=~AO_Trigger_Length;
2179 win_out(devpriv->ao_mode3,AO_Mode_3_Register);
2181 win_out(devpriv->ao_mode1,AO_Mode_1_Register);
2182 devpriv->ao_mode2&=~AO_BC_Initial_Load_Source;
2183 win_out(devpriv->ao_mode2,AO_Mode_2_Register);
2184 if(cmd->stop_src==TRIG_NONE){
2185 win_out2(0xffffff,AO_BC_Load_A_Register);
2187 win_out2(0,AO_BC_Load_A_Register);
2189 win_out(AO_BC_Load,AO_Command_1_Register);
2190 devpriv->ao_mode2&=~AO_UC_Initial_Load_Source;
2191 win_out(devpriv->ao_mode2,AO_Mode_2_Register);
2192 switch(cmd->stop_src){
2194 win_out2(cmd->stop_arg,AO_UC_Load_A_Register);
2195 win_out(AO_UC_Load,AO_Command_1_Register);
2196 win_out2(cmd->stop_arg - 1,AO_UC_Load_A_Register);
2199 win_out2(0xffffff,AO_UC_Load_A_Register);
2200 win_out(AO_UC_Load,AO_Command_1_Register);
2201 win_out2(0xffffff,AO_UC_Load_A_Register);
2204 win_out2(0,AO_UC_Load_A_Register);
2205 win_out(AO_UC_Load,AO_Command_1_Register);
2206 win_out2(cmd->stop_arg,AO_UC_Load_A_Register);
2209 devpriv->ao_cmd2&=~AO_BC_Gate_Enable;
2210 win_out(devpriv->ao_cmd2,AO_Command_2_Register);
2211 devpriv->ao_mode1&=~(AO_UI_Source_Select(0x1f)|AO_UI_Source_Polarity);
2212 win_out(devpriv->ao_mode1,AO_Mode_1_Register);
2213 devpriv->ao_mode2&=~(AO_UI_Reload_Mode(3)|AO_UI_Initial_Load_Source);
2214 win_out(devpriv->ao_mode2,AO_Mode_2_Register);
2215 win_out2(1,AO_UI_Load_A_Register);
2216 win_out(AO_UI_Load,AO_Command_1_Register);
2217 win_out2(trigvar,AO_UI_Load_A_Register);
2219 if(boardtype.reg_type == ni_reg_normal){
2220 if(cmd->scan_end_arg>1){
2221 devpriv->ao_mode1|=AO_Multiple_Channels;
2222 win_out(AO_Number_Of_Channels(cmd->scan_end_arg-1)|
2223 AO_UPDATE_Output_Select(1),
2224 AO_Output_Control_Register);
2226 devpriv->ao_mode1&=~AO_Multiple_Channels;
2227 win_out(AO_Number_Of_Channels(CR_CHAN(cmd->chanlist[0]))|
2228 AO_UPDATE_Output_Select(1),
2229 AO_Output_Control_Register);
2231 win_out(devpriv->ao_mode1,AO_Mode_1_Register);
2234 win_out(AO_DAC0_Update_Mode|AO_DAC1_Update_Mode,AO_Command_1_Register);
2236 devpriv->ao_mode3|=AO_Stop_On_Overrun_Error;
2237 win_out(devpriv->ao_mode3,AO_Mode_3_Register);
2239 devpriv->ao_mode2 &= AO_FIFO_Mode_Mask;
2241 devpriv->ao_mode2 |= AO_FIFO_Mode_HF_to_F;
2243 devpriv->ao_mode2 |= AO_FIFO_Mode_HF;
2245 devpriv->ao_mode2 &= ~AO_FIFO_Retransmit_Enable;
2246 win_out(devpriv->ao_mode2,AO_Mode_2_Register);
2248 bits = AO_BC_Source_Select | AO_UPDATE_Pulse_Width |
2249 AO_TMRDACWR_Pulse_Width;
2250 if( boardtype.ao_fifo_depth )
2251 bits |= AO_FIFO_Enable;
2253 bits |= AO_DMA_PIO_Control;
2254 win_out(bits, AO_Personal_Register);
2255 // enable sending of ao dma requests
2256 win_out(AO_AOFREQ_Enable, AO_Start_Select_Register);
2258 win_out(AO_Configuration_End,Joint_Reset_Register);
2260 if(cmd->stop_src==TRIG_COUNT) {
2261 win_out(AO_BC_TC_Interrupt_Ack,Interrupt_B_Ack_Register);
2262 ni_set_bits(dev, Interrupt_B_Enable_Register,
2263 AO_BC_TC_Interrupt_Enable, 1);
2266 s->async->inttrig=ni_ao_inttrig;
2271 static int ni_ao_cmdtest(comedi_device *dev,comedi_subdevice *s,comedi_cmd *cmd)
2276 /* step 1: make sure trigger sources are trivially valid */
2279 cmd->start_src &= TRIG_INT;
2280 if(!cmd->start_src || tmp!=cmd->start_src)err++;
2282 tmp=cmd->scan_begin_src;
2283 cmd->scan_begin_src &= TRIG_TIMER;
2284 if(!cmd->scan_begin_src || tmp!=cmd->scan_begin_src)err++;
2286 tmp=cmd->convert_src;
2287 cmd->convert_src &= TRIG_NOW;
2288 if(!cmd->convert_src || tmp!=cmd->convert_src)err++;
2290 tmp=cmd->scan_end_src;
2291 cmd->scan_end_src &= TRIG_COUNT;
2292 if(!cmd->scan_end_src || tmp!=cmd->scan_end_src)err++;
2295 cmd->stop_src &= TRIG_COUNT|TRIG_NONE;
2296 if(!cmd->stop_src || tmp!=cmd->stop_src)err++;
2300 /* step 2: make sure trigger sources are unique and mutually compatible */
2302 if(cmd->stop_src!=TRIG_COUNT &&
2303 cmd->stop_src!=TRIG_NONE)err++;
2307 /* step 3: make sure arguments are trivially compatible */
2309 if(cmd->start_arg!=0){
2314 /* XXX need ao_speed */
2315 if(cmd->scan_begin_arg<boardtype.ao_speed){
2316 cmd->scan_begin_arg=boardtype.ao_speed;
2320 if(cmd->scan_begin_arg>TIMER_BASE*0xffffff){ /* XXX check */
2321 cmd->scan_begin_arg=TIMER_BASE*0xffffff;
2324 if(cmd->convert_arg!=0){
2328 if(cmd->scan_end_arg!=cmd->chanlist_len){
2329 cmd->scan_end_arg=cmd->chanlist_len;
2332 if(cmd->stop_src==TRIG_COUNT){ /* XXX check */
2333 if(cmd->stop_arg>0x00ffffff){
2334 cmd->stop_arg=0x00ffffff;
2339 if(cmd->stop_arg!=0){
2347 /* step 4: fix up any arguments */
2349 tmp = cmd->scan_begin_arg;
2350 ni_ns_to_timer(&cmd->scan_begin_arg,cmd->flags&TRIG_ROUND_MASK);
2351 if(tmp!=cmd->scan_begin_arg)err++;
2355 /* step 5: fix up chanlist */
2363 static int ni_ao_reset(comedi_device *dev,comedi_subdevice *s)
2365 //devpriv->ao0p=0x0000;
2366 //ni_writew(devpriv->ao0p,AO_Configuration);
2368 //devpriv->ao1p=AO_Channel(1);
2369 //ni_writew(devpriv->ao1p,AO_Configuration);
2372 mite_dma_disarm(devpriv->mite, AO_DMA_CHAN);
2373 writel(CHOR_DMARESET | CHOR_FRESET, devpriv->mite->mite_io_addr + MITE_CHOR(AO_DMA_CHAN));
2376 win_out(AO_Configuration_Start,Joint_Reset_Register);
2377 win_out(AO_Disarm,AO_Command_1_Register);
2378 ni_set_bits(dev,Interrupt_B_Enable_Register,~0,0);
2379 win_out(AO_BC_Source_Select, AO_Personal_Register);
2380 win_out(0x3f98,Interrupt_B_Ack_Register);
2381 win_out(AO_BC_Source_Select | AO_UPDATE_Pulse_Width |
2382 AO_TMRDACWR_Pulse_Width, AO_Personal_Register);
2383 win_out(0,AO_Output_Control_Register);
2384 win_out(0,AO_Start_Select_Register);
2386 win_out(devpriv->ao_cmd1,AO_Command_1_Register);
2388 devpriv->ao_mode1=0;
2389 devpriv->ao_mode2=0;
2390 devpriv->ao_mode3=0;
2391 devpriv->ao_trigger_select=0;
2392 if(boardtype.reg_type & ni_reg_6xxx_mask){
2393 ao_win_out(0x3, AO_Immediate_671x);
2394 ao_win_out(CLEAR_WG, AO_Misc_611x);
2400 static int ni_dio_insn_config(comedi_device *dev,comedi_subdevice *s,
2401 comedi_insn *insn,lsampl_t *data)
2404 printk("ni_dio_insn_config() chan=%d io=%d\n",
2405 CR_CHAN(insn->chanspec),data[0]);
2408 case INSN_CONFIG_DIO_OUTPUT:
2409 s->io_bits |= 1<<CR_CHAN(insn->chanspec);
2411 case INSN_CONFIG_DIO_INPUT:
2412 s->io_bits &= ~(1<<CR_CHAN(insn->chanspec));
2414 case INSN_CONFIG_DIO_QUERY:
2415 data[1] = (s->io_bits & (1<<CR_CHAN(insn->chanspec))) ? COMEDI_OUTPUT : COMEDI_INPUT;
2422 devpriv->dio_control &= ~DIO_Pins_Dir_Mask;
2423 devpriv->dio_control |= DIO_Pins_Dir(s->io_bits);
2424 win_out(devpriv->dio_control,DIO_Control_Register);
2429 static int ni_dio_insn_bits(comedi_device *dev,comedi_subdevice *s,
2430 comedi_insn *insn,lsampl_t *data)
2433 printk("ni_dio_insn_bits() mask=0x%x bits=0x%x\n",data[0],data[1]);
2435 if(insn->n!=2)return -EINVAL;
2437 /* Perform check to make sure we're not using the
2438 serial part of the dio */
2439 if((data[0] & (DIO_SDIN | DIO_SDOUT)) && devpriv->serial_interval_ns)
2442 s->state &= ~data[0];
2443 s->state |= (data[0]&data[1]);
2444 devpriv->dio_output &= ~DIO_Parallel_Data_Mask;
2445 devpriv->dio_output |= DIO_Parallel_Data_Out(s->state);
2446 win_out(devpriv->dio_output,DIO_Output_Register);
2448 data[1] = win_in(DIO_Parallel_Input_Register);
2453 static int ni_serial_insn_config(comedi_device *dev,comedi_subdevice *s,
2454 comedi_insn *insn,lsampl_t *data)
2457 unsigned char byte_out, byte_in;
2459 if(insn->n!=2)return -EINVAL;
2462 case INSN_CONFIG_SERIAL_CLOCK:
2465 printk("SPI serial clock Config cd\n", data[1]);
2467 devpriv->serial_hw_mode = 1;
2468 devpriv->dio_control |= DIO_HW_Serial_Enable;
2470 if(data[1] == SERIAL_DISABLED) {
2471 devpriv->serial_hw_mode = 0;
2472 devpriv->dio_control &= ~(DIO_HW_Serial_Enable |
2473 DIO_Software_Serial_Control);
2474 data[1] = SERIAL_DISABLED;
2475 devpriv->serial_interval_ns = data[1];
2477 else if(data[1] <= SERIAL_600NS) {
2478 /* Warning: this clock speed is too fast to reliably
2480 devpriv->dio_control &= ~DIO_HW_Serial_Timebase;
2481 devpriv->clock_and_fout |= Slow_Internal_Timebase;
2482 devpriv->clock_and_fout &= ~DIO_Serial_Out_Divide_By_2;
2483 data[1] = SERIAL_600NS;
2484 devpriv->serial_interval_ns = data[1];
2486 else if(data[1] <= SERIAL_1_2US) {
2487 devpriv->dio_control &= ~DIO_HW_Serial_Timebase;
2488 devpriv->clock_and_fout |= Slow_Internal_Timebase |
2489 DIO_Serial_Out_Divide_By_2;
2490 data[1] = SERIAL_1_2US;
2491 devpriv->serial_interval_ns = data[1];
2493 else if(data[1] <= SERIAL_10US) {
2494 devpriv->dio_control |= DIO_HW_Serial_Timebase;
2495 devpriv->clock_and_fout |= Slow_Internal_Timebase |
2496 DIO_Serial_Out_Divide_By_2;
2497 /* Note: DIO_Serial_Out_Divide_By_2 only affects
2498 600ns/1.2us. If you turn divide_by_2 off with the
2499 slow clock, you will still get 10us, except then
2500 all your delays are wrong. */
2501 data[1] = SERIAL_10US;
2502 devpriv->serial_interval_ns = data[1];
2505 devpriv->dio_control &= ~(DIO_HW_Serial_Enable |
2506 DIO_Software_Serial_Control);
2507 devpriv->serial_hw_mode = 0;
2508 data[1] = (data[1] / 1000) * 1000;
2509 devpriv->serial_interval_ns = data[1];
2512 win_out(devpriv->dio_control,DIO_Control_Register);
2513 win_out(devpriv->clock_and_fout,Clock_and_FOUT_Register);
2518 case INSN_CONFIG_BIDIRECTIONAL_DATA:
2520 if(devpriv->serial_interval_ns == 0) {
2524 byte_out = data[1] & 0xFF;
2526 if(devpriv->serial_hw_mode) {
2527 err = ni_serial_hw_readwrite8(dev,s,byte_out,&byte_in);
2528 } else if(devpriv->serial_interval_ns > 0) {
2529 err = ni_serial_sw_readwrite8(dev,s,byte_out,&byte_in);
2531 printk("ni_serial_insn_config: serial disabled!\n");
2534 if(err < 0) return err;
2535 data[1] = byte_in & 0xFF;
2545 static int ni_serial_hw_readwrite8(comedi_device *dev,comedi_subdevice *s,
2546 unsigned char data_out,
2547 unsigned char *data_in)
2549 unsigned int status1;
2550 int err = 0, count = 20;
2553 printk("ni_serial_hw_readwrite8: outputting 0x%x\n", data_out);
2556 devpriv->dio_output &= ~DIO_Serial_Data_Mask;
2557 devpriv->dio_output |= DIO_Serial_Data_Out(data_out);
2558 win_out(devpriv->dio_output,DIO_Output_Register);
2560 status1 = win_in(Joint_Status_1_Register);
2561 if(status1 & DIO_Serial_IO_In_Progress_St) {
2566 devpriv->dio_control |= DIO_HW_Serial_Start;
2567 win_out(devpriv->dio_control,DIO_Control_Register);
2568 devpriv->dio_control &= ~DIO_HW_Serial_Start;
2570 /* Wait until STC says we're done, but don't loop infinitely. */
2571 while((status1 = win_in(Joint_Status_1_Register)) & DIO_Serial_IO_In_Progress_St) {
2572 /* Delay one bit per loop */
2573 comedi_udelay((devpriv->serial_interval_ns + 999) / 1000);
2575 printk("ni_serial_hw_readwrite8: SPI serial I/O didn't finish in time!\n");
2581 /* Delay for last bit. This delay is absolutely necessary, because
2582 DIO_Serial_IO_In_Progress_St goes high one bit too early. */
2583 comedi_udelay((devpriv->serial_interval_ns + 999) / 1000);
2585 if(data_in != NULL) {
2586 *data_in = win_in(DIO_Serial_Input_Register);
2588 printk("ni_serial_hw_readwrite8: inputted 0x%x\n", *data_in);
2593 win_out(devpriv->dio_control,DIO_Control_Register);
2598 static int ni_serial_sw_readwrite8(comedi_device *dev,comedi_subdevice *s,
2599 unsigned char data_out,
2600 unsigned char *data_in)
2602 unsigned char mask, input = 0;
2605 printk("ni_serial_sw_readwrite8: outputting 0x%x\n", data_out);
2608 /* Wait for one bit before transfer */
2609 comedi_udelay((devpriv->serial_interval_ns + 999) / 1000);
2611 for(mask = 0x80; mask; mask >>= 1) {
2612 /* Output current bit; note that we cannot touch s->state
2613 because it is a per-subdevice field, and serial is
2614 a separate subdevice from DIO. */
2615 devpriv->dio_output &= ~DIO_SDOUT;
2616 if(data_out & mask) {
2617 devpriv->dio_output |= DIO_SDOUT;
2619 win_out(devpriv->dio_output,DIO_Output_Register);
2621 /* Assert SDCLK (active low, inverted), wait for half of
2622 the delay, deassert SDCLK, and wait for the other half. */
2623 devpriv->dio_control |= DIO_Software_Serial_Control;
2624 win_out(devpriv->dio_control,DIO_Control_Register);
2626 comedi_udelay((devpriv->serial_interval_ns + 999) / 2000);
2628 devpriv->dio_control &= ~DIO_Software_Serial_Control;
2629 win_out(devpriv->dio_control,DIO_Control_Register);
2631 comedi_udelay((devpriv->serial_interval_ns + 999) / 2000);
2633 /* Input current bit */
2634 if(win_in(DIO_Parallel_Input_Register) & DIO_SDIN) {
2635 /* printk("DIO_P_I_R: 0x%x\n", win_in(DIO_Parallel_Input_Register)); */
2640 printk("ni_serial_sw_readwrite8: inputted 0x%x\n", input);
2642 if(data_in) *data_in = input;
2647 static void mio_common_detach(comedi_device *dev)
2649 if(dev->subdevices && boardtype.has_8255)
2650 subdev_8255_cleanup(dev,dev->subdevices+3);
2653 static void init_ao_67xx(comedi_device *dev, comedi_subdevice *s)
2657 for(i = 0; i < s->n_chan; i++)
2658 ni_ao_win_outw(dev, AO_Channel(i) | 0x0, AO_Configuration_2_67xx);
2661 static int ni_alloc_private(comedi_device *dev)
2665 ret = alloc_private(dev, sizeof(ni_private));
2666 if(ret < 0) return ret;
2668 spin_lock_init(&devpriv->window_lock);
2673 static int ni_E_init(comedi_device *dev,comedi_devconfig *it)
2675 comedi_subdevice *s;
2678 if(boardtype.n_aochan > MAX_N_AO_CHAN)
2680 printk("bug! boardtype.n_aochan > MAX_N_AO_CHAN\n");
2684 if(alloc_subdevices(dev, 10) < 0)
2687 /* analog input subdevice */
2689 s=dev->subdevices+0;
2691 if(boardtype.n_adchan){
2692 s->type=COMEDI_SUBD_AI;
2693 s->subdev_flags=SDF_READABLE|SDF_DIFF;
2694 if(boardtype.reg_type == ni_reg_normal)
2695 s->subdev_flags |= SDF_GROUND | SDF_COMMON | SDF_OTHER;
2696 s->subdev_flags|=SDF_DITHER;
2697 s->n_chan=boardtype.n_adchan;
2698 s->len_chanlist=512;
2699 s->maxdata=(1<<boardtype.adbits)-1;
2700 s->range_table=ni_range_lkup[boardtype.gainlkup];
2701 s->insn_read=ni_ai_insn_read;
2702 s->insn_config=ni_ai_insn_config;
2703 s->do_cmdtest=ni_ai_cmdtest;
2704 s->do_cmd=ni_ai_cmd;
2705 s->cancel=ni_ai_reset;
2707 s->munge=ni_ai_munge;
2709 s->type=COMEDI_SUBD_UNUSED;
2712 /* analog output subdevice */
2714 s=dev->subdevices+1;
2715 if(boardtype.n_aochan){
2716 dev->write_subdev=s;
2717 s->type=COMEDI_SUBD_AO;
2718 s->subdev_flags=SDF_WRITABLE|SDF_DEGLITCH|SDF_GROUND;
2719 s->n_chan=boardtype.n_aochan;
2720 s->maxdata=(1<<boardtype.aobits)-1;
2721 if(boardtype.ao_unipolar){
2722 s->range_table=&range_ni_E_ao_ext; /* XXX wrong for some boards */
2724 s->range_table=&range_bipolar10;
2726 s->insn_read=ni_ao_insn_read;
2727 if(boardtype.reg_type & ni_reg_6xxx_mask){
2728 s->insn_write=ni_ao_insn_write_671x;
2730 s->insn_write=ni_ao_insn_write;
2733 if(boardtype.n_aochan){
2735 if(boardtype.ao_fifo_depth){
2737 s->do_cmd=ni_ao_cmd;
2738 s->do_cmdtest=ni_ao_cmdtest;
2739 s->len_chanlist = boardtype.n_aochan;
2740 s->munge=ni_ao_munge;
2742 s->cancel=ni_ao_reset;
2744 s->type=COMEDI_SUBD_UNUSED;
2746 if((boardtype.reg_type & ni_reg_67xx_mask))
2747 init_ao_67xx(dev, s);
2749 /* digital i/o subdevice */
2751 s=dev->subdevices+2;
2752 s->type=COMEDI_SUBD_DIO;
2753 s->subdev_flags=SDF_WRITABLE|SDF_READABLE;
2756 s->range_table=&range_digital;
2757 s->io_bits=0; /* all bits input */
2758 s->insn_bits=ni_dio_insn_bits;
2759 s->insn_config=ni_dio_insn_config;
2762 devpriv->dio_control = DIO_Pins_Dir(s->io_bits);
2763 win_out(devpriv->dio_control,DIO_Control_Register);
2766 s=dev->subdevices+3;
2767 if(boardtype.has_8255){
2768 subdev_8255_init(dev,s,ni_8255_callback,(unsigned long)dev);
2770 s->type=COMEDI_SUBD_UNUSED;
2773 /* general purpose counter/timer device */
2774 s=dev->subdevices+4;
2775 s->type=COMEDI_SUBD_COUNTER;
2776 s->subdev_flags=SDF_READABLE|SDF_WRITABLE;
2777 s->insn_read= ni_gpct_insn_read;
2778 s->insn_write= ni_gpct_insn_write;
2779 s->insn_config=ni_gpct_insn_config;
2782 devpriv->an_trig_etc_reg = 0;
2786 /* calibration subdevice -- ai and ao */
2787 s=dev->subdevices+5;
2788 s->type=COMEDI_SUBD_CALIB;
2789 s->subdev_flags=SDF_WRITABLE|SDF_INTERNAL;
2790 s->insn_read=ni_calib_insn_read;
2791 s->insn_write=ni_calib_insn_write;
2792 caldac_setup(dev,s);
2795 s=dev->subdevices+6;
2796 s->type=COMEDI_SUBD_MEMORY;
2797 s->subdev_flags=SDF_READABLE|SDF_INTERNAL;
2800 s->insn_read=ni_eeprom_insn_read;
2803 s=dev->subdevices+7;
2804 s->type=COMEDI_SUBD_DIO;
2805 s->subdev_flags=SDF_READABLE|SDF_WRITABLE|SDF_INTERNAL;
2808 s->insn_bits = ni_pfi_insn_bits;
2809 s->insn_config = ni_pfi_insn_config;
2810 ni_set_bits(dev, IO_Bidirection_Pin_Register, ~0, 0);
2812 /* cs5529 calibration adc */
2813 s = dev->subdevices + 8;
2814 if(boardtype.reg_type & ni_reg_67xx_mask)
2816 s->type = COMEDI_SUBD_AI;
2817 s->subdev_flags = SDF_READABLE | SDF_DIFF | SDF_INTERNAL;
2818 // one channel for each analog output channel
2819 s->n_chan = boardtype.n_aochan;
2820 s->maxdata = (1 << 16) - 1;
2821 s->range_table = &range_unknown; /* XXX */
2822 s->insn_read=cs5529_ai_insn_read;
2823 s->insn_config=NULL;
2827 s->type=COMEDI_SUBD_UNUSED;
2831 s=dev->subdevices+9;
2832 s->type=COMEDI_SUBD_SERIAL;
2833 s->subdev_flags=SDF_READABLE|SDF_WRITABLE|SDF_INTERNAL;
2836 s->insn_config = ni_serial_insn_config;
2837 devpriv->serial_interval_ns = 0;
2838 devpriv->serial_hw_mode = 0;
2840 /* ai configuration */
2841 ni_ai_reset(dev,dev->subdevices+0);
2842 if(boardtype.reg_type == ni_reg_normal){
2843 devpriv->clock_and_fout =
2844 Slow_Internal_Time_Divide_By_2 |
2845 Slow_Internal_Timebase |
2846 Clock_To_Board_Divide_By_2 |
2848 AI_Output_Divide_By_2 |
2849 AO_Output_Divide_By_2;
2851 devpriv->clock_and_fout =
2852 Slow_Internal_Time_Divide_By_2 |
2853 Slow_Internal_Timebase |
2854 Clock_To_Board_Divide_By_2 |
2857 win_out(devpriv->clock_and_fout, Clock_and_FOUT_Register);
2859 /* analog output configuration */
2860 ni_ao_reset(dev,dev->subdevices + 1);
2863 win_out((IRQ_POLARITY?Interrupt_Output_Polarity:0) |
2864 (Interrupt_Output_On_3_Pins&0) |
2865 Interrupt_A_Enable |
2866 Interrupt_B_Enable |
2867 Interrupt_A_Output_Select(interrupt_pin(dev->irq)) |
2868 Interrupt_B_Output_Select(interrupt_pin(dev->irq)),
2869 Interrupt_Control_Register
2874 /* tell the STC which dma channels to use for AI and AO */
2875 bits = 1 << ( AI_DMA_CHAN );
2876 bits |= 1 << ( AO_DMA_CHAN + 4 );
2877 ni_writeb( bits, AI_AO_Select);
2878 /* tell the STC which dma channels to use for
2879 * General purpose counters 0 and 1 */
2880 bits = 1 << ( GPC0_DMA_CHAN );
2881 bits |= 1 << ( GPC1_DMA_CHAN + 4 );
2882 ni_writeb( bits, G0_G1_Select);
2885 if(boardtype.reg_type != ni_reg_normal)
2887 ni_writeb( 0, Magic_611x );
2897 static int ni_8255_callback(int dir,int port,int data,unsigned long arg)
2899 comedi_device *dev=(comedi_device *)arg;
2902 ni_writeb(data,Port_A+2*port);
2905 return ni_readb(Port_A+2*port);
2910 presents the EEPROM as a subdevice
2913 static int ni_eeprom_insn_read(comedi_device *dev,comedi_subdevice *s,
2914 comedi_insn *insn,lsampl_t *data)
2916 data[0]=ni_read_eeprom(dev,CR_CHAN(insn->chanspec));
2922 reads bytes out of eeprom
2925 static int ni_read_eeprom(comedi_device *dev,int addr)
2930 bitstring=0x0300|((addr&0x100)<<3)|(addr&0xff);
2931 ni_writeb(0x04,Serial_Command);
2932 for(bit=0x8000;bit;bit>>=1){
2933 ni_writeb(0x04|((bit&bitstring)?0x02:0),Serial_Command);
2934 ni_writeb(0x05|((bit&bitstring)?0x02:0),Serial_Command);
2937 for(bit=0x80;bit;bit>>=1){
2938 ni_writeb(0x04,Serial_Command);
2939 ni_writeb(0x05,Serial_Command);
2940 bitstring|=((ni_readb(XXX_Status)&PROMOUT)?bit:0);
2942 ni_writeb(0x00,Serial_Command);
2947 static void ni_write_caldac(comedi_device *dev,int addr,int val);
2949 calibration subdevice
2951 static int ni_calib_insn_write(comedi_device *dev,comedi_subdevice *s,
2952 comedi_insn *insn,lsampl_t *data)
2954 ni_write_caldac(dev,CR_CHAN(insn->chanspec),data[0]);
2959 static int ni_calib_insn_read(comedi_device *dev,comedi_subdevice *s,
2960 comedi_insn *insn,lsampl_t *data)
2962 data[0] = devpriv->caldacs[CR_CHAN(insn->chanspec)];
2967 static int pack_mb88341(int addr,int val,int *bitstring);
2968 static int pack_dac8800(int addr,int val,int *bitstring);
2969 static int pack_dac8043(int addr,int val,int *bitstring);
2970 static int pack_ad8522(int addr,int val,int *bitstring);
2971 static int pack_ad8804(int addr,int val,int *bitstring);
2972 static int pack_ad8842(int addr,int val,int *bitstring);
2974 struct caldac_struct{
2977 int (*packbits)(int,int,int *);
2980 static struct caldac_struct caldacs[] = {
2981 [mb88341] = { 12, 8, pack_mb88341 },
2982 [dac8800] = { 8, 8, pack_dac8800 },
2983 [dac8043] = { 1, 12, pack_dac8043 },
2984 [ad8522] = { 2, 12, pack_ad8522 },
2985 [ad8804] = { 12, 8, pack_ad8804 },
2986 [ad8842] = { 8, 8, pack_ad8842 },
2987 [ad8804_debug] = { 16, 8, pack_ad8804 },
2990 static void caldac_setup(comedi_device *dev,comedi_subdevice *s)
3000 type = boardtype.caldac[0];
3001 if(type==caldac_none)return;
3002 n_bits=caldacs[type].n_bits;
3004 type = boardtype.caldac[i];
3005 if(type==caldac_none)break;
3006 if(caldacs[type].n_bits!=n_bits)diffbits=1;
3007 n_chans+=caldacs[type].n_chans;
3014 if(n_chans>MAX_N_CALDACS){
3015 printk("BUG! MAX_N_CALDACS too small\n");
3017 s->maxdata_list=devpriv->caldac_maxdata_list;
3019 for(i=0;i<n_dacs;i++){
3020 type = boardtype.caldac[i];
3021 for(j=0;j<caldacs[type].n_chans;j++){
3022 s->maxdata_list[chan]=
3023 (1<<caldacs[type].n_bits)-1;
3028 for( chan = 0; chan < s->n_chan; chan++ )
3029 ni_write_caldac( dev, i, s->maxdata_list[ i ] / 2 );
3031 type = boardtype.caldac[0];
3032 s->maxdata=(1<<caldacs[type].n_bits)-1;
3034 for( chan = 0; chan < s->n_chan; chan++ )
3035 ni_write_caldac( dev, i, s->maxdata / 2 );
3039 static void ni_write_caldac(comedi_device *dev,int addr,int val)
3041 unsigned int loadbit=0,bits=0,bit,bitstring=0;
3045 //printk("ni_write_caldac: chan=%d val=%d\n",addr,val);
3046 if( devpriv->caldacs[ addr ] == val ) return;
3047 devpriv->caldacs[ addr ] = val;
3050 type = boardtype.caldac[i];
3051 if(type==caldac_none)break;
3052 if(addr<caldacs[type].n_chans){
3053 bits=caldacs[type].packbits(addr,val,&bitstring);
3054 loadbit=SerDacLd(i);
3055 //printk("caldac: using i=%d addr=%d %x\n",i,addr,bitstring);
3058 addr-=caldacs[type].n_chans;
3061 for(bit=1<<(bits-1);bit;bit>>=1){
3062 ni_writeb(((bit&bitstring)?0x02:0),Serial_Command);
3064 ni_writeb(1|((bit&bitstring)?0x02:0),Serial_Command);
3067 ni_writeb(loadbit,Serial_Command);
3069 ni_writeb(0,Serial_Command);
3074 static int pack_mb88341(int addr,int val,int *bitstring)
3078 Note that address bits are reversed. Thanks to
3079 Ingo Keen for noticing this.
3081 Note also that the 88341 expects address values from
3082 1-12, whereas we use channel numbers 0-11. The NI
3083 docs use 1-12, also, so be careful here.
3086 *bitstring=((addr&0x1)<<11) |
3094 static int pack_dac8800(int addr,int val,int *bitstring)
3096 *bitstring=((addr&0x7)<<8)|(val&0xff);
3100 static int pack_dac8043(int addr,int val,int *bitstring)
3102 *bitstring=val&0xfff;
3106 static int pack_ad8522(int addr,int val,int *bitstring)
3108 *bitstring=(val&0xfff)|(addr ? 0xc000:0xa000);
3112 static int pack_ad8804(int addr,int val,int *bitstring)
3114 *bitstring=((addr&0xf)<<8) | (val&0xff);
3118 static int pack_ad8842(int addr,int val,int *bitstring)
3120 *bitstring=((addr+1)<<8) | (val&0xff);
3130 * General Purpose Counter/Timer section
3135 * Low level stuff...Each STC counter has two 24 bit load registers
3136 * (A&B). Just make it easier to access them.
3138 * These are inlined _only_ because they are used once in subsequent
3139 * code. Otherwise they should not be inlined.
3141 static inline void GPCT_Load_A(comedi_device *dev, int chan, unsigned int value)
3143 win_out2( value & 0x00ffffff, G_Load_A_Register(chan));
3146 static inline void GPCT_Load_B(comedi_device *dev, int chan, unsigned int value)
3148 win_out2( value & 0x00ffffff, G_Load_B_Register(chan));
3151 /* Load a value into the counter, using register A as the intermediate step.
3152 * You might use GPCT_Load_Using_A to load a 0x000000 into a counter
3155 static void GPCT_Load_Using_A(comedi_device *dev, int chan, unsigned int value)
3157 devpriv->gpct_mode[chan] &= (~G_Load_Source_Select);
3158 win_out( devpriv->gpct_mode[chan],G_Mode_Register(chan));
3159 GPCT_Load_A(dev,chan,value);
3160 win_out( devpriv->gpct_command[chan]|G_Load,G_Command_Register(chan));
3164 * Read the GPCTs current value.
3166 static int GPCT_G_Watch(comedi_device *dev, int chan)
3168 unsigned int hi1,hi2,lo;
3170 devpriv->gpct_command[chan] &= ~G_Save_Trace;
3171 win_out( devpriv->gpct_command[chan],G_Command_Register(chan));
3173 devpriv->gpct_command[chan] |= G_Save_Trace;
3174 win_out( devpriv->gpct_command[chan], G_Command_Register(chan));
3176 /* This procedure is used because the two registers cannot
3177 * be read atomically. */
3179 hi1 = win_in( G_Save_Register_High(chan));
3180 lo = win_in(G_Save_Register_Low(chan));
3181 hi2 = win_in( G_Save_Register_High(chan));
3184 return (hi1<<16)|lo;
3188 static int GPCT_Disarm(comedi_device *dev, int chan)
3190 win_out( devpriv->gpct_command[chan] | G_Disarm,G_Command_Register(chan));
3195 static int GPCT_Arm(comedi_device *dev, int chan)
3197 win_out( devpriv->gpct_command[chan] | G_Arm,G_Command_Register(chan));
3198 /* If the counter is doing pulse width measurement, then make
3199 sure that the counter did not start counting right away. This would
3200 indicate that we started acquiring the pulse after it had already
3201 started and our measurement would be inaccurate */
3202 if(devpriv->gpct_cur_operation[chan] == GPCT_SINGLE_PW){
3205 g_status=win_in(G_Status_Register);
3208 //TIM 5/2/01 possible error with very short pulses
3209 if((G0_Counting_St & g_status)|| !(G0_Armed_St&g_status)) {
3210 //error: we missed the beginning of the pulse
3211 return -EINVAL; //there is probably a more accurate error code...
3214 if((G1_Counting_St & g_status)|| !(G1_Armed_St&g_status)) {
3215 //error: we missed the beginning of the pulse
3223 static int GPCT_Set_Source(comedi_device *dev,int chan ,int source)
3225 //printk("GPCT_Set_Source...");
3226 devpriv->gpct_input_select[chan] &= ~G_Source_Select(0x1f);//reset gate to 0
3228 case GPCT_INT_CLOCK:
3229 devpriv->gpct_input_select[chan] |= G_Source_Select(0);//INT_TIMEBASE
3233 devpriv->gpct_input_select[chan] |= G_Source_Select(9);//PFI8
3235 devpriv->gpct_input_select[chan] |= G_Source_Select(4);//PFI3
3240 win_out(devpriv->gpct_input_select[chan], G_Input_Select_Register(chan));
3241 //printk("exit GPCT_Set_Source\n");
3245 static int GPCT_Set_Gate(comedi_device *dev,int chan ,int gate)
3247 //printk("GPCT_Set_Gate...");
3248 devpriv->gpct_input_select[chan] &= ~G_Gate_Select(0x1f);//reset gate to 0
3251 devpriv->gpct_input_select[chan] |= G_Gate_Select(31);//Low
3252 devpriv->gpct_mode[chan] |= G_Gate_Polarity;
3255 devpriv->gpct_mode[chan] &= ~G_Gate_Polarity;
3257 devpriv->gpct_input_select[chan] |= G_Gate_Select(10);//PFI9
3259 devpriv->gpct_input_select[chan] |= G_Gate_Select(5);//PFI4
3265 win_out(devpriv->gpct_input_select[chan], G_Input_Select_Register(chan));
3266 win_out(devpriv->gpct_mode[chan], G_Mode_Register(chan));
3267 //printk("exit GPCT_Set_Gate\n");
3271 static int GPCT_Set_Direction(comedi_device *dev,int chan,int direction)
3273 //printk("GPCT_Set_Direction...");
3275 devpriv->gpct_command[chan] &= ~G_Up_Down(0x3);
3276 switch (direction) {
3278 devpriv->gpct_command[chan] |= G_Up_Down(1);
3281 devpriv->gpct_command[chan] |= G_Up_Down(0);
3284 devpriv->gpct_command[chan] |= G_Up_Down(2);
3287 printk("Error direction=0x%08x..",direction);
3290 win_out(devpriv->gpct_command[chan], G_Command_Register(chan));
3291 //TIM 4/23/01 win_out(devpriv->gpct_mode[chan], G_Mode_Register(chan));
3292 //printk("exit GPCT_Set_Direction\n");
3296 static void GPCT_Event_Counting(comedi_device *dev,int chan)
3299 //NOTE: possible residual bits from multibit masks can corrupt
3300 //If you config for several measurements between Resets, watch out!
3302 //printk("GPCT_Event_Counting...");
3304 devpriv->gpct_cur_operation[chan] = GPCT_SIMPLE_EVENT;
3307 devpriv->gpct_mode[chan] &= ~(G_Gating_Mode(0x3));
3308 devpriv->gpct_mode[chan] |= G_Gating_Mode(1);
3310 // Trigger_Mode_For_Edge_Gate = 1
3311 devpriv->gpct_mode[chan] &= ~(G_Trigger_Mode_For_Edge_Gate(0x3));
3312 devpriv->gpct_mode[chan] |= G_Trigger_Mode_For_Edge_Gate(2);
3314 win_out( devpriv->gpct_mode[chan],G_Mode_Register(chan));
3315 //printk("exit GPCT_Event_Counting\n");
3318 static void GPCT_Period_Meas(comedi_device *dev, int chan)
3320 //printk("GPCT_Period_Meas...");
3322 devpriv->gpct_cur_operation[chan] = GPCT_SINGLE_PERIOD;
3325 //NOTE: possible residual bits from multibit masks can corrupt
3326 //If you config for several measurements between Resets, watch out!
3327 devpriv->gpct_mode[chan] &= ~G_OR_Gate;
3328 devpriv->gpct_mode[chan] &= ~G_Gate_Select_Load_Source;
3331 devpriv->gpct_mode[chan] &= ~(G_Output_Mode(0x3));
3332 devpriv->gpct_mode[chan] |= G_Output_Mode(3);
3336 devpriv->gpct_mode[chan] &= ~(G_Gating_Mode(0x3));
3337 devpriv->gpct_mode[chan] |= G_Gating_Mode(2);
3339 // Trigger_Mode_For_Edge_Gate=0
3340 devpriv->gpct_mode[chan] &= ~(G_Trigger_Mode_For_Edge_Gate(0x3));
3341 devpriv->gpct_mode[chan] |= G_Trigger_Mode_For_Edge_Gate(0);
3343 devpriv->gpct_mode[chan] |= G_Reload_Source_Switching;
3344 devpriv->gpct_mode[chan] &= ~G_Loading_On_Gate;
3345 devpriv->gpct_mode[chan] &= ~G_Loading_On_TC;
3346 devpriv->gpct_mode[chan] &= ~G_Gate_On_Both_Edges;
3349 devpriv->gpct_mode[chan] &= ~(G_Stop_Mode(0x3));
3350 devpriv->gpct_mode[chan] |= G_Stop_Mode(0);
3352 // Counting_Once = 2
3353 devpriv->gpct_mode[chan] &= ~(G_Counting_Once(0x3));
3354 devpriv->gpct_mode[chan] |= G_Counting_Once(2);
3357 devpriv->gpct_command[chan] &= ~(G_Up_Down(0x3));
3358 devpriv->gpct_command[chan] |= G_Up_Down(1);
3360 win_out( devpriv->gpct_mode[chan],G_Mode_Register(chan));
3361 win_out( devpriv->gpct_command[chan],G_Command_Register(chan));
3362 //printk("exit GPCT_Period_Meas\n");
3365 static void GPCT_Pulse_Width_Meas(comedi_device *dev, int chan)
3367 //printk("GPCT_Pulse_Width_Meas...");
3369 devpriv->gpct_cur_operation[chan] = GPCT_SINGLE_PW;
3371 devpriv->gpct_mode[chan] &= ~G_OR_Gate;
3372 devpriv->gpct_mode[chan] &= ~G_Gate_Select_Load_Source;
3375 devpriv->gpct_mode[chan] &= ~(G_Output_Mode(0x3));
3376 devpriv->gpct_mode[chan] |= G_Output_Mode(3);
3379 devpriv->gpct_mode[chan] &= ~(G_Gating_Mode(0x3));
3380 devpriv->gpct_mode[chan] |= G_Gating_Mode(1);//TIM 4/24/01 was 2
3382 // Trigger_Mode_For_Edge_Gate=2
3383 devpriv->gpct_mode[chan] &= ~(G_Trigger_Mode_For_Edge_Gate(0x3));
3384 devpriv->gpct_mode[chan] |= G_Trigger_Mode_For_Edge_Gate(2);//TIM 4/24/01 was 0
3387 devpriv->gpct_mode[chan] |= G_Reload_Source_Switching;//TIM 4/24/01 was 1
3388 devpriv->gpct_mode[chan] &= ~G_Loading_On_Gate;//TIM 4/24/01 was 0
3390 devpriv->gpct_mode[chan] &= ~G_Loading_On_TC;
3391 devpriv->gpct_mode[chan] &= ~G_Gate_On_Both_Edges;
3394 devpriv->gpct_mode[chan] &= ~(G_Stop_Mode(0x3));
3395 devpriv->gpct_mode[chan] |= G_Stop_Mode(0);
3397 // Counting_Once = 2
3398 devpriv->gpct_mode[chan] &= ~(G_Counting_Once(0x3));
3399 devpriv->gpct_mode[chan] |= G_Counting_Once(2);
3402 devpriv->gpct_command[chan] &= ~(G_Up_Down(0x3));
3403 devpriv->gpct_command[chan] |= G_Up_Down(1);
3405 win_out( devpriv->gpct_mode[chan],G_Mode_Register(chan));
3406 win_out( devpriv->gpct_command[chan],G_Command_Register(chan));
3408 //printk("exit GPCT_Pulse_Width_Meas\n");
3411 /* GPCT_Gen_Single_Pulse() creates pulse of length pulsewidth which starts after the Arm
3412 signal is sent. The pulse is delayed by the value already in the counter. This function could
3413 be modified to send a pulse in response to a trigger event at its gate.*/
3414 static void GPCT_Gen_Single_Pulse(comedi_device *dev, int chan, unsigned int length)
3416 //printk("GPCT_Gen_Cont...");
3418 devpriv->gpct_cur_operation[chan] = GPCT_SINGLE_PULSE_OUT;
3420 // Set length of the pulse
3421 GPCT_Load_B(dev,chan, length-1);
3423 //Load next time using B, This is reset by GPCT_Load_Using_A()
3424 devpriv->gpct_mode[chan] |= G_Load_Source_Select;
3426 devpriv->gpct_mode[chan] &= ~G_OR_Gate;
3427 devpriv->gpct_mode[chan] &= ~G_Gate_Select_Load_Source;
3430 devpriv->gpct_mode[chan] &= ~(G_Output_Mode(0x3));
3431 devpriv->gpct_mode[chan] |= G_Output_Mode(2); //TIM 4/26/01 was 3
3433 //Gating Mode=0 for untriggered single pulse
3434 devpriv->gpct_mode[chan] &= ~(G_Gating_Mode(0x3));
3435 devpriv->gpct_mode[chan] |= G_Gating_Mode(0); //TIM 4/25/01 was 1
3437 // Trigger_Mode_For_Edge_Gate=0
3438 devpriv->gpct_mode[chan] &= ~(G_Trigger_Mode_For_Edge_Gate(0x3));
3439 devpriv->gpct_mode[chan] |= G_Trigger_Mode_For_Edge_Gate(2);
3442 devpriv->gpct_mode[chan] |= G_Reload_Source_Switching;
3443 devpriv->gpct_mode[chan] &= ~G_Loading_On_Gate;
3444 devpriv->gpct_mode[chan] |= G_Loading_On_TC; //TIM 4/25/01
3445 devpriv->gpct_mode[chan] &= ~G_Gate_On_Both_Edges;
3448 devpriv->gpct_mode[chan] &= ~(G_Stop_Mode(0x3));
3449 devpriv->gpct_mode[chan] |= G_Stop_Mode(2); //TIM 4/25/01
3451 // Counting_Once = 2
3452 devpriv->gpct_mode[chan] &= ~(G_Counting_Once(0x3));
3453 devpriv->gpct_mode[chan] |= G_Counting_Once(1); //TIM 4/25/01
3456 devpriv->gpct_command[chan] &= ~(G_Up_Down(0x3));
3457 devpriv->gpct_command[chan] |= G_Up_Down(0); //TIM 4/25/01 was 1
3459 win_out( devpriv->gpct_mode[chan],G_Mode_Register(chan));
3460 win_out( devpriv->gpct_command[chan],G_Command_Register(chan));
3462 //printk("exit GPCT_Gen_Cont\n");
3465 static void GPCT_Gen_Cont_Pulse(comedi_device *dev, int chan, unsigned int length)
3467 //printk("GPCT_Gen_Cont...");
3469 devpriv->gpct_cur_operation[chan] = GPCT_CONT_PULSE_OUT;
3471 // Set length of the pulse
3472 GPCT_Load_B(dev,chan, length-1);
3474 //Load next time using B, This is reset by GPCT_Load_Using_A()
3475 devpriv->gpct_mode[chan] |= G_Load_Source_Select;
3477 devpriv->gpct_mode[chan] &= ~G_OR_Gate;
3478 devpriv->gpct_mode[chan] &= ~G_Gate_Select_Load_Source;
3481 devpriv->gpct_mode[chan] &= ~(G_Output_Mode(0x3));
3482 devpriv->gpct_mode[chan] |= G_Output_Mode(2); //TIM 4/26/01 was 3
3484 //Gating Mode=0 for untriggered single pulse
3485 devpriv->gpct_mode[chan] &= ~(G_Gating_Mode(0x3));
3486 devpriv->gpct_mode[chan] |= G_Gating_Mode(0); //TIM 4/26/01 was 0
3488 // Trigger_Mode_For_Edge_Gate=0
3489 devpriv->gpct_mode[chan] &= ~(G_Trigger_Mode_For_Edge_Gate(0x3));
3490 devpriv->gpct_mode[chan] |= G_Trigger_Mode_For_Edge_Gate(2);
3493 devpriv->gpct_mode[chan] |= G_Reload_Source_Switching;
3494 devpriv->gpct_mode[chan] &= ~G_Loading_On_Gate;
3495 devpriv->gpct_mode[chan] |= G_Loading_On_TC;
3496 devpriv->gpct_mode[chan] &= ~G_Gate_On_Both_Edges;
3499 devpriv->gpct_mode[chan] &= ~(G_Stop_Mode(0x3));
3500 devpriv->gpct_mode[chan] |= G_Stop_Mode(0); //TIM 4/26/01
3502 // Counting_Once = 2
3503 devpriv->gpct_mode[chan] &= ~(G_Counting_Once(0x3));
3504 devpriv->gpct_mode[chan] |= G_Counting_Once(0); //TIM 4/26/01
3507 devpriv->gpct_command[chan] &= ~(G_Up_Down(0x3));
3508 devpriv->gpct_command[chan] |= G_Up_Down(0);
3511 //This seems pretty unsafe since I don't think it is cleared anywhere.
3512 //I don't think this is working
3513 //devpriv->gpct_command[chan] &= ~G_Bank_Switch_Enable;
3514 //devpriv->gpct_command[chan] &= ~G_Bank_Switch_Mode;
3517 win_out( devpriv->gpct_mode[chan],G_Mode_Register(chan));
3518 win_out( devpriv->gpct_command[chan],G_Command_Register(chan));
3520 //printk("exit GPCT_Gen_Cont\n");
3523 static void GPCT_Reset(comedi_device *dev, int chan)
3527 //printk("GPCT_Reset...");
3528 devpriv->gpct_cur_operation[chan] = GPCT_RESET;
3532 win_out(G0_Reset,Joint_Reset_Register);
3533 ni_set_bits(dev,Interrupt_A_Enable_Register,G0_TC_Interrupt_Enable, 0);
3534 ni_set_bits(dev,Interrupt_A_Enable_Register,G0_Gate_Interrupt_Enable,0);
3535 temp_ack_reg |= G0_Gate_Error_Confirm;
3536 temp_ack_reg |= G0_TC_Error_Confirm;
3537 temp_ack_reg |= G0_TC_Interrupt_Ack;
3538 temp_ack_reg |= G0_Gate_Interrupt_Ack;
3539 win_out(temp_ack_reg,Interrupt_A_Ack_Register);
3541 //problem...this interferes with the other ctr...
3542 devpriv->an_trig_etc_reg |= GPFO_0_Output_Enable;
3543 win_out(devpriv->an_trig_etc_reg, Analog_Trigger_Etc_Register);
3546 win_out(G1_Reset,Joint_Reset_Register);
3547 ni_set_bits(dev,Interrupt_B_Enable_Register,G1_TC_Interrupt_Enable, 0);
3548 ni_set_bits(dev,Interrupt_B_Enable_Register,G0_Gate_Interrupt_Enable,0);
3549 temp_ack_reg |= G1_Gate_Error_Confirm;
3550 temp_ack_reg |= G1_TC_Error_Confirm;
3551 temp_ack_reg |= G1_TC_Interrupt_Ack;
3552 temp_ack_reg |= G1_Gate_Interrupt_Ack;
3553 win_out(temp_ack_reg,Interrupt_B_Ack_Register);
3555 devpriv->an_trig_etc_reg |= GPFO_1_Output_Enable;
3556 win_out(devpriv->an_trig_etc_reg, Analog_Trigger_Etc_Register);
3560 devpriv->gpct_mode[chan] = 0;
3561 devpriv->gpct_input_select[chan] = 0;
3562 devpriv->gpct_command[chan] = 0;
3564 devpriv->gpct_command[chan] |= G_Synchronized_Gate;
3566 win_out( devpriv->gpct_mode[chan],G_Mode_Register(chan));
3567 win_out( devpriv->gpct_input_select[chan],G_Input_Select_Register(chan));
3568 win_out( 0,G_Autoincrement_Register(chan));
3570 //printk("exit GPCT_Reset\n");
3573 static int ni_gpct_insn_config(comedi_device *dev,comedi_subdevice *s,
3574 comedi_insn *insn,lsampl_t *data)
3577 //printk("data[0] is 0x%08x, data[1] is 0x%08x\n",data[0],data[1]);
3580 if(insn->n!=1)return -EINVAL;
3581 GPCT_Reset(dev,insn->chanspec);
3583 case GPCT_SET_SOURCE:
3584 if(insn->n!=2)return -EINVAL;
3585 retval=GPCT_Set_Source(dev,insn->chanspec,data[1]);
3588 if(insn->n!=2)return -EINVAL;
3589 retval=GPCT_Set_Gate(dev,insn->chanspec,data[1]);
3591 case GPCT_SET_DIRECTION:
3592 if(insn->n!=2) return -EINVAL;
3593 retval=GPCT_Set_Direction(dev,insn->chanspec,data[1]);
3595 case GPCT_GET_INT_CLK_FRQ:
3596 if(insn->n!=2) return -EINVAL;
3597 //There are actually 2 internal clocks on the STC, we always
3598 //use the fast 20MHz one at this time. Tim Ousley 5/1/01
3599 //NOTE: This is not the final interface, ideally the user
3600 //will never need to know the int. clk. freq.
3601 data[1]=50;//50ns = 20MHz = internal timebase of STC
3603 case GPCT_SET_OPERATION:
3604 //TIM 5/1/01 if((insn->n<2)||(insn->n>3))return -EINVAL;
3606 case GPCT_SIMPLE_EVENT:
3607 GPCT_Event_Counting(dev,insn->chanspec);
3609 case GPCT_SINGLE_PERIOD:
3610 GPCT_Period_Meas(dev,insn->chanspec);
3612 case GPCT_SINGLE_PW:
3613 GPCT_Pulse_Width_Meas(dev,insn->chanspec);
3615 case GPCT_SINGLE_PULSE_OUT:
3616 GPCT_Gen_Single_Pulse(dev,insn->chanspec,data[2]);
3618 case GPCT_CONT_PULSE_OUT:
3619 GPCT_Gen_Cont_Pulse(dev,insn->chanspec,data[2]);
3622 printk("unsupported GPCT operation!\n");
3627 if(insn->n!=1)return -EINVAL;
3628 retval=GPCT_Arm(dev,insn->chanspec);
3631 if(insn->n!=1)return -EINVAL;
3632 retval=GPCT_Disarm(dev,insn->chanspec);
3638 //catch any errors from return values
3642 if(data[0]!=GPCT_ARM){
3643 printk("error: retval was %d\n",retval);
3644 printk("data[0] is 0x%08x, data[1] is 0x%08x\n",data[0],data[1]);
3651 static int ni_gpct_insn_read(comedi_device *dev,comedi_subdevice *s,
3652 comedi_insn *insn,lsampl_t *data) {
3654 int chan=insn->chanspec;
3655 int cur_op = devpriv->gpct_cur_operation[chan];
3657 //printk("in ni_gpct_insn_read, n=%d, data[0]=%d\n",insn->chanspec,data[0]);
3658 if(insn->n!=1)return -EINVAL;
3660 data[0] = GPCT_G_Watch(dev,insn->chanspec);
3662 /* for certain modes (period and pulse width measurment), the value
3663 in the counter is not valid until the counter stops. If the value is
3664 invalid, return a 0 */
3665 if((cur_op == GPCT_SINGLE_PERIOD) || (cur_op == GPCT_SINGLE_PW)){
3666 /* is the counter still running? */
3667 if(win_in(G_Status_Register) & (chan?G1_Counting_St:G0_Counting_St))
3673 static int ni_gpct_insn_write(comedi_device *dev,comedi_subdevice *s,
3674 comedi_insn *insn,lsampl_t *data) {
3676 //printk("in ni_gpct_insn_write");
3677 if(insn->n!=1)return -EINVAL;
3678 GPCT_Load_Using_A(dev,insn->chanspec,data[0]);
3685 * Programmable Function Inputs
3689 static int ni_pfi_insn_bits(comedi_device *dev,comedi_subdevice *s,
3690 comedi_insn *insn,lsampl_t *data)
3692 if(insn->n!=2)return -EINVAL;
3699 static int ni_pfi_insn_config(comedi_device *dev,comedi_subdevice *s,
3700 comedi_insn *insn,lsampl_t *data)
3704 if(insn->n!=1)return -EINVAL;
3706 chan = CR_CHAN(insn->chanspec);
3707 if(chan>10)return -EINVAL;
3711 ni_set_bits(dev, IO_Bidirection_Pin_Register, 1<<chan, 1);
3714 ni_set_bits(dev, IO_Bidirection_Pin_Register, 1<<chan, 0);
3716 case INSN_CONFIG_DIO_QUERY:
3717 data[1] = (devpriv->io_bidirection_pin_reg & (1<<chan)) ? COMEDI_OUTPUT : COMEDI_INPUT;
3727 static int cs5529_wait_for_idle(comedi_device *dev)
3729 unsigned short status;
3730 const int timeout = HZ;
3733 for(i = 0; i < timeout; i++)
3735 status = ni_ao_win_inw(dev, CAL_ADC_Status_67xx);
3736 if((status & CSS_ADC_BUSY) == 0)
3740 set_current_state(TASK_INTERRUPTIBLE);
3741 if(schedule_timeout(1))
3746 //printk("looped %i times waiting for idle\n", i);
3749 rt_printk("%s: %s: timeout\n", __FILE__, __FUNCTION__);
3755 static void cs5529_command(comedi_device *dev, unsigned short value)
3757 static const int timeout = 100;
3760 ni_ao_win_outw(dev, value, CAL_ADC_Command_67xx);
3761 /* give time for command to start being serially clocked into cs5529.
3762 * this insures that the CSS_ADC_BUSY bit will get properly
3763 * set before we exit this function.
3765 for(i = 0; i < timeout; i++)
3767 if((ni_ao_win_inw(dev, CAL_ADC_Status_67xx) & CSS_ADC_BUSY))
3771 //printk("looped %i times writing command to cs5529\n", i);
3774 comedi_error(dev, "possible problem - never saw adc go busy?");
3778 /* write to cs5529 register */
3779 static void cs5529_config_write(comedi_device *dev, unsigned int value, unsigned int reg_select_bits)
3781 ni_ao_win_outw(dev, ((value >> 16) & 0xff), CAL_ADC_Config_Data_High_Word_67xx);
3782 ni_ao_win_outw(dev, (value & 0xffff), CAL_ADC_Config_Data_Low_Word_67xx);
3783 reg_select_bits &= CSCMD_REGISTER_SELECT_MASK;
3784 cs5529_command(dev, CSCMD_COMMAND | reg_select_bits);
3785 if(cs5529_wait_for_idle(dev))
3786 comedi_error(dev, "time or signal in cs5529_config_write()");
3789 /* read from cs5529 register */
3790 static unsigned int cs5529_config_read(comedi_device *dev, unsigned int reg_select_bits)
3794 reg_select_bits &= CSCMD_REGISTER_SELECT_MASK;
3795 cs5529_command(dev, CSCMD_COMMAND | CSCMD_READ | reg_select_bits);
3796 if(cs5529_wait_for_idle(dev))
3797 comedi_error(dev, "timeout or signal in cs5529_config_read()");
3798 value = (ni_ao_win_inw(dev, CAL_ADC_Config_Data_High_Word_67xx) << 16) & 0xff0000;
3799 value |= ni_ao_win_inw(dev, CAL_ADC_Config_Data_Low_Word_67xx) & 0xffff;
3803 static int cs5529_do_conversion(comedi_device *dev, unsigned short *data)
3806 unsigned short status;
3808 cs5529_command(dev, CSCMD_COMMAND | CSCMD_SINGLE_CONVERSION);
3809 retval = cs5529_wait_for_idle(dev);
3812 comedi_error(dev, "timeout or signal in cs5529_do_conversion()");
3815 status = ni_ao_win_inw(dev, CAL_ADC_Status_67xx);
3816 if(status & CSS_OSC_DETECT)
3818 rt_printk("ni_mio_common: cs5529 conversion error, status CSS_OSC_DETECT\n");
3821 if(status & CSS_OVERRANGE)
3823 rt_printk("ni_mio_common: cs5529 conversion error, overrange (ignoring)\n");
3827 *data = ni_ao_win_inw(dev, CAL_ADC_Data_67xx);
3828 /* cs5529 returns 16 bit signed data in bipolar mode */
3834 static int cs5529_ai_insn_read(comedi_device *dev,comedi_subdevice *s,comedi_insn *insn,lsampl_t *data)
3837 unsigned short sample;
3838 unsigned int channel_select;
3839 const unsigned int INTERNAL_REF = 0x1000;
3841 /* Set calibration adc source. Docs lie, reference select bits 8 to 11
3842 * do nothing. bit 12 seems to chooses internal reference voltage, bit
3843 * 13 causes the adc input to go overrange (maybe reads external reference?) */
3844 if(insn->chanspec & CR_ALT_SOURCE)
3845 channel_select = INTERNAL_REF;
3847 channel_select = CR_CHAN(insn->chanspec);
3848 ni_ao_win_outw(dev, channel_select, AO_Calibration_Channel_Select_67xx);
3850 for(n = 0; n < insn->n; n++)
3852 retval = cs5529_do_conversion(dev, &sample);
3853 if(retval < 0) return retval;
3859 static int init_cs5529(comedi_device *dev)
3861 unsigned int config_bits = CSCFG_PORT_MODE | CSCFG_WORD_RATE_2180_CYCLES;
3864 /* do self-calibration */
3865 cs5529_config_write(dev, config_bits | CSCFG_SELF_CAL_OFFSET_GAIN, CSCMD_CONFIG_REGISTER);
3866 /* need to force a conversion for calibration to run */
3867 cs5529_do_conversion(dev, NULL);
3869 /* force gain calibration to 1 */
3870 cs5529_config_write(dev, 0x400000, CSCMD_GAIN_REGISTER);
3871 cs5529_config_write(dev, config_bits | CSCFG_SELF_CAL_OFFSET, CSCMD_CONFIG_REGISTER);
3872 if(cs5529_wait_for_idle(dev))
3873 comedi_error(dev, "timeout or signal in init_cs5529()\n");
3877 rt_printk("config: 0x%x\n", cs5529_config_read(dev, CSCMD_CONFIG_REGISTER));
3878 rt_printk("gain: 0x%x\n", cs5529_config_read(dev, CSCMD_GAIN_REGISTER));
3879 rt_printk("offset: 0x%x\n", cs5529_config_read(dev, CSCMD_OFFSET_REGISTER));