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
56 2006-02-07: S-Series PCI-6143: Support has been added but is not
57 fully tested as yet. Terry Barnaby, BEAM Ltd.
60 //#define DEBUG_INTERRUPT
61 //#define DEBUG_STATUS_A
62 //#define DEBUG_STATUS_B
66 #include "comedi_fc.h"
69 #define MDPRINTK(format,args...)
74 #define NI_TIMEOUT 1000
76 /* Note: this table must match the ai_gain_* definitions */
77 static short ni_gainlkup[][16]={
79 { 0, 1, 2, 3, 4, 5, 6, 7, 0x100, 0x101, 0x102, 0x103, 0x104, 0x105,
82 { 1, 2, 4, 7, 0x101, 0x102, 0x104, 0x107 },
84 { 1, 2, 3, 4, 5, 6, 7, 0x101, 0x102, 0x103, 0x104, 0x105, 0x106,
89 { 0x00a, 0x00b, 0x001, 0x002, 0x003, 0x004, 0x005, 0x006 },
93 { 1, 2, 3, 4, 5, 6, 7},
95 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
98 static comedi_lrange range_ni_E_ai={ 16, {
104 RANGE( -0.25, 0.25 ),
106 RANGE( -0.05, 0.05 ),
116 static comedi_lrange range_ni_E_ai_limited={ 8, {
126 static comedi_lrange range_ni_E_ai_limited14={ 14, {
142 static comedi_lrange range_ni_E_ai_bipolar4={ 4, {
146 RANGE( -0.05, 0.05 ),
148 static comedi_lrange range_ni_E_ai_611x={ 8, {
158 static comedi_lrange range_ni_M_ai_622x={ 4, {
164 static comedi_lrange range_ni_M_ai_628x={ 7, {
173 static comedi_lrange range_ni_S_ai_6143 = { 1, {
176 static comedi_lrange range_ni_E_ao_ext = { 4, {
183 static comedi_lrange *ni_range_lkup[]={
185 &range_ni_E_ai_limited,
186 &range_ni_E_ai_limited14,
187 &range_ni_E_ai_bipolar4,
196 static int ni_dio_insn_config(comedi_device *dev,comedi_subdevice *s,
197 comedi_insn *insn,lsampl_t *data);
198 static int ni_dio_insn_bits(comedi_device *dev,comedi_subdevice *s,
199 comedi_insn *insn,lsampl_t *data);
201 static int ni_serial_insn_config(comedi_device *dev,comedi_subdevice *s,
202 comedi_insn *insn,lsampl_t *data);
203 static int ni_serial_hw_readwrite8(comedi_device *dev,comedi_subdevice *s,
204 unsigned char data_out, unsigned char *data_in);
205 static int ni_serial_sw_readwrite8(comedi_device *dev,comedi_subdevice *s,
206 unsigned char data_out, unsigned char *data_in);
208 static int ni_calib_insn_read(comedi_device *dev,comedi_subdevice *s,
209 comedi_insn *insn,lsampl_t *data);
210 static int ni_calib_insn_write(comedi_device *dev,comedi_subdevice *s,
211 comedi_insn *insn,lsampl_t *data);
213 static int ni_eeprom_insn_read(comedi_device *dev,comedi_subdevice *s,
214 comedi_insn *insn,lsampl_t *data);
215 static int ni_m_series_eeprom_insn_read(comedi_device *dev,comedi_subdevice *s,
216 comedi_insn *insn,lsampl_t *data);
218 static int ni_pfi_insn_bits(comedi_device *dev,comedi_subdevice *s,
219 comedi_insn *insn,lsampl_t *data);
220 static int ni_pfi_insn_config(comedi_device *dev,comedi_subdevice *s,
221 comedi_insn *insn,lsampl_t *data);
223 static void ni_rtsi_init(comedi_device *dev);
224 static int ni_rtsi_insn_bits(comedi_device *dev,comedi_subdevice *s,
225 comedi_insn *insn,lsampl_t *data);
226 static int ni_rtsi_insn_config(comedi_device *dev,comedi_subdevice *s,
227 comedi_insn *insn,lsampl_t *data);
229 static void caldac_setup(comedi_device *dev,comedi_subdevice *s);
230 static int ni_read_eeprom(comedi_device *dev,int addr);
232 #ifdef DEBUG_STATUS_A
233 static void ni_mio_print_status_a(int status);
235 #define ni_mio_print_status_a(a)
237 #ifdef DEBUG_STATUS_B
238 static void ni_mio_print_status_b(int status);
240 #define ni_mio_print_status_b(a)
243 static int ni_ai_reset(comedi_device *dev,comedi_subdevice *s);
245 static void ni_handle_fifo_half_full(comedi_device *dev);
246 static int ni_ao_fifo_half_empty(comedi_device *dev,comedi_subdevice *s);
248 static void ni_handle_fifo_dregs(comedi_device *dev);
249 static int ni_ai_inttrig(comedi_device *dev,comedi_subdevice *s,
250 unsigned int trignum);
251 static void ni_load_channelgain_list(comedi_device *dev,unsigned int n_chan,
253 static void shutdown_ai_command( comedi_device *dev );
255 static int ni_ao_inttrig(comedi_device *dev,comedi_subdevice *s,
256 unsigned int trignum);
258 static int ni_ao_reset(comedi_device *dev,comedi_subdevice *s);
260 static int ni_8255_callback(int dir,int port,int data,unsigned long arg);
262 static int ni_ns_to_timer(int *nanosec,int round_mode);
265 /*GPCT function def's*/
266 static int GPCT_G_Watch(comedi_device *dev, int chan);
268 static void GPCT_Reset(comedi_device *dev, int chan);
269 static void GPCT_Gen_Cont_Pulse(comedi_device *dev, int chan, unsigned int length);
270 static void GPCT_Gen_Single_Pulse(comedi_device *dev, int chan, unsigned int length);
271 static void GPCT_Period_Meas(comedi_device *dev, int chan);
272 static void GPCT_Pulse_Width_Meas(comedi_device *dev, int chan);
273 static void GPCT_Event_Counting(comedi_device *dev,int chan);
274 static int GPCT_Set_Direction(comedi_device *dev,int chan,int direction);
275 static int GPCT_Set_Gate(comedi_device *dev,int chan ,int gate);
276 static int GPCT_Set_Source(comedi_device *dev,int chan ,int source);
278 static int ni_gpct_insn_write(comedi_device *dev,comedi_subdevice *s,
279 comedi_insn *insn,lsampl_t *data);
280 static int ni_gpct_insn_read(comedi_device *dev,comedi_subdevice *s,
281 comedi_insn *insn,lsampl_t *data);
282 static int ni_gpct_insn_config(comedi_device *dev,comedi_subdevice *s,
283 comedi_insn *insn,lsampl_t *data);
285 static int init_cs5529(comedi_device *dev);
286 static int cs5529_do_conversion(comedi_device *dev, unsigned short *data);
287 static int cs5529_ai_insn_read(comedi_device *dev,comedi_subdevice *s,comedi_insn *insn,lsampl_t *data);
288 static unsigned int cs5529_config_read(comedi_device *dev, unsigned int reg_select_bits);
289 static void cs5529_config_write(comedi_device *dev, unsigned int value, unsigned int reg_select_bits);
291 static int ni_m_series_pwm_config(comedi_device *dev, comedi_subdevice *s,
292 comedi_insn *insn,lsampl_t *data);
293 static int ni_6143_pwm_config(comedi_device *dev, comedi_subdevice *s,
294 comedi_insn *insn, lsampl_t *data);
299 AIMODE_HALF_FULL = 1,
304 #define SERIAL_DISABLED 0
305 #define SERIAL_600NS 600
306 #define SERIAL_1_2US 1200
307 #define SERIAL_10US 10000
309 static const int num_adc_stages_611x = 3;
311 static void handle_a_interrupt(comedi_device *dev,unsigned short status,
312 unsigned int m_status);
313 static void handle_b_interrupt(comedi_device *dev,unsigned short status,
314 unsigned int m_status);
315 static void get_last_sample_611x( comedi_device *dev );
316 static void get_last_sample_6143( comedi_device *dev );
318 //static void mite_handle_interrupt(comedi_device *dev,unsigned int status);
319 static int ni_ai_drain_dma(comedi_device *dev );
322 static void ni_flush_ai_fifo(comedi_device *dev){
323 if(boardtype.reg_type == ni_reg_6143){
324 // Flush the 6143 data FIFO
325 ni_writel(0x10, AIFIFO_Control_6143); // Flush fifo
326 ni_writel(0x00, AIFIFO_Control_6143); // Flush fifo
327 while(ni_readl(AIFIFO_Status_6143) & 0x10); // Wait for complete
330 devpriv->stc_writew(dev, 1,ADC_FIFO_Clear);
334 static void win_out2(comedi_device *dev, uint32_t data, int reg)
336 devpriv->stc_writew(dev, data >> 16, reg);
337 devpriv->stc_writew(dev, data & 0xffff, reg + 1);
340 #define ao_win_out(data,addr) ni_ao_win_outw(dev,data,addr)
341 static inline void ni_ao_win_outw( comedi_device *dev, uint16_t data, int addr )
345 comedi_spin_lock_irqsave(&devpriv->window_lock,flags);
346 ni_writew(addr,AO_Window_Address_611x);
347 ni_writew(data,AO_Window_Data_611x);
348 comedi_spin_unlock_irqrestore(&devpriv->window_lock,flags);
351 static inline void ni_ao_win_outl(comedi_device *dev, uint32_t data, int addr)
355 comedi_spin_lock_irqsave(&devpriv->window_lock,flags);
356 ni_writew(addr,AO_Window_Address_611x);
357 ni_writel(data,AO_Window_Data_611x);
358 comedi_spin_unlock_irqrestore(&devpriv->window_lock,flags);
361 static inline unsigned short ni_ao_win_inw( comedi_device *dev, int addr )
366 comedi_spin_lock_irqsave(&devpriv->window_lock,flags);
367 ni_writew(addr, AO_Window_Address_611x);
368 data = ni_readw(AO_Window_Data_611x);
369 comedi_spin_unlock_irqrestore(&devpriv->window_lock,flags);
373 /* ni_set_bits( ) allows different parts of the ni_mio_common driver to
374 * share registers (such as Interrupt_A_Register) without interfering with
377 * NOTE: the switch/case statements are optimized out for a constant argument
378 * so this is actually quite fast--- If you must wrap another function around this
379 * make it inline to avoid a large speed penalty.
381 * value should only be 1 or 0.
383 static inline void ni_set_bits(comedi_device *dev, int reg, int bits, int value)
387 comedi_spin_lock_irqsave( &devpriv->window_lock, flags );
389 case Interrupt_A_Enable_Register:
391 devpriv->int_a_enable_reg |= bits;
393 devpriv->int_a_enable_reg &= ~bits;
394 comedi_spin_unlock_irqrestore( &devpriv->window_lock, flags );
395 devpriv->stc_writew(dev, devpriv->int_a_enable_reg,Interrupt_A_Enable_Register);
397 case Interrupt_B_Enable_Register:
399 devpriv->int_b_enable_reg |= bits;
401 devpriv->int_b_enable_reg &= ~bits;
402 comedi_spin_unlock_irqrestore( &devpriv->window_lock, flags );
403 devpriv->stc_writew(dev, devpriv->int_b_enable_reg,Interrupt_B_Enable_Register);
405 case IO_Bidirection_Pin_Register:
407 devpriv->io_bidirection_pin_reg |= bits;
409 devpriv->io_bidirection_pin_reg &= ~bits;
410 comedi_spin_unlock_irqrestore( &devpriv->window_lock, flags );
411 devpriv->stc_writew(dev, devpriv->io_bidirection_pin_reg,IO_Bidirection_Pin_Register);
414 rt_printk("Warning ni_set_bits() called with invalid arguments\n");
415 rt_printk("reg is %d\n",reg);
416 comedi_spin_unlock_irqrestore( &devpriv->window_lock, flags );
422 static irqreturn_t ni_E_interrupt(int irq,void *d,struct pt_regs * regs)
424 comedi_device *dev=d;
425 unsigned short a_status;
426 unsigned short b_status;
427 unsigned int m0_status;
428 unsigned int m1_status;
431 struct mite_struct *mite = devpriv->mite;
434 if(dev->attached == 0) return IRQ_NONE;
435 // lock to avoid race with comedi_poll
436 comedi_spin_lock_irqsave(&dev->spinlock, flags);
437 a_status=devpriv->stc_readw(dev, AI_Status_1_Register);
438 b_status=devpriv->stc_readw(dev, AO_Status_1_Register);
440 m0_status=readl(mite->mite_io_addr + MITE_CHSR(AI_DMA_CHAN));
441 m1_status=readl(mite->mite_io_addr + MITE_CHSR(AO_DMA_CHAN));
447 if(a_status&Interrupt_A_St || m0_status & CHSR_INT )
448 handle_a_interrupt(dev, a_status, m0_status);
449 if(b_status&Interrupt_B_St || m1_status & CHSR_INT )
450 handle_b_interrupt(dev, b_status, m1_status);
451 comedi_spin_unlock_irqrestore(&dev->spinlock, flags);
456 static void ni_sync_ai_dma(struct mite_struct *mite, comedi_device *dev)
459 comedi_subdevice *s = dev->subdevices + 0;
460 comedi_async *async = s->async;
461 unsigned int nbytes, old_alloc_count;
462 unsigned int bytes_per_scan = bytes_per_sample(s) * async->cmd.chanlist_len;
464 old_alloc_count = async->buf_write_alloc_count;
465 // write alloc as much as we can
466 comedi_buf_write_alloc(s->async, s->async->prealloc_bufsz);
468 nbytes = mite_bytes_written_to_memory_lb(mite, AI_DMA_CHAN);
470 if( (int)(mite_bytes_written_to_memory_ub(mite, AI_DMA_CHAN) - old_alloc_count) > 0 ){
471 rt_printk("ni_mio_common: DMA overwrite of free area\n");
473 async->events |= COMEDI_CB_OVERFLOW;
477 count = nbytes - async->buf_write_count;
479 /* it's possible count will be negative due to
480 * conservative value returned by mite_bytes_transferred */
483 comedi_buf_write_free(async, count);
485 async->scan_progress += count;
486 if( async->scan_progress >= bytes_per_scan )
488 async->scan_progress %= bytes_per_scan;
489 async->events |= COMEDI_CB_EOS;
491 async->events |= COMEDI_CB_BLOCK;
494 static void mite_handle_b_linkc(struct mite_struct *mite, comedi_device *dev)
497 comedi_subdevice *s = dev->subdevices + 1;
498 comedi_async *async = s->async;
499 u32 nbytes_ub, nbytes_lb;
500 unsigned int new_write_count;
501 u32 stop_count = async->cmd.stop_arg * sizeof(sampl_t);
503 writel(CHOR_CLRLC, mite->mite_io_addr + MITE_CHOR(AO_DMA_CHAN));
505 new_write_count = async->buf_write_count;
507 nbytes_lb = mite_bytes_read_from_memory_lb(mite, AO_DMA_CHAN);
508 if(async->cmd.stop_src == TRIG_COUNT &&
509 (int) (nbytes_lb - stop_count) > 0)
510 nbytes_lb = stop_count;
512 nbytes_ub = mite_bytes_read_from_memory_ub(mite, AO_DMA_CHAN);
513 if(async->cmd.stop_src == TRIG_COUNT &&
514 (int) (nbytes_ub - stop_count) > 0)
515 nbytes_ub = stop_count;
516 if((int)(nbytes_ub - devpriv->last_buf_write_count) > 0){
517 rt_printk("ni_mio_common: DMA underrun\n");
519 async->events |= COMEDI_CB_OVERFLOW;
523 devpriv->last_buf_write_count = new_write_count;
525 count = nbytes_lb - async->buf_read_count;
529 comedi_buf_read_free(async, count);
531 async->events |= COMEDI_CB_BLOCK;
534 static int ni_ao_wait_for_dma_load( comedi_device *dev )
536 static const int timeout = 10000;
539 for(i = 0; i < timeout; i++)
541 unsigned short b_status;
543 b_status = devpriv->stc_readw(dev, AO_Status_1_Register );
544 if( b_status & AO_FIFO_Half_Full_St )
546 /* if we poll too often, the pci bus activity seems
547 to slow the dma transfer down */
552 comedi_error(dev, "timed out waiting for dma load");
559 static void ni_handle_eos(comedi_device *dev, comedi_subdevice *s)
561 if(devpriv->aimode == AIMODE_SCAN)
564 static const int timeout = 10;
567 for(i = 0; i < timeout; i++)
569 ni_sync_ai_dma(devpriv->mite, dev);
570 if((s->async->events & COMEDI_CB_EOS)) break;
574 ni_handle_fifo_dregs(dev);
575 s->async->events |= COMEDI_CB_EOS;
578 /* handle special case of single scan using AI_End_On_End_Of_Scan */
579 if((devpriv->ai_cmd2 & AI_End_On_End_Of_Scan)){
580 shutdown_ai_command( dev );
585 static void shutdown_ai_command( comedi_device *dev )
587 comedi_subdevice *s = dev->subdevices + 0;
590 ni_ai_drain_dma( dev );
591 mite_dma_disarm(devpriv->mite, AI_DMA_CHAN);
593 ni_handle_fifo_dregs(dev);
594 get_last_sample_611x(dev);
595 get_last_sample_6143(dev);
597 ni_set_bits(dev, Interrupt_A_Enable_Register,
598 AI_SC_TC_Interrupt_Enable | AI_START1_Interrupt_Enable|
599 AI_START2_Interrupt_Enable| AI_START_Interrupt_Enable|
600 AI_STOP_Interrupt_Enable| AI_Error_Interrupt_Enable|
601 AI_FIFO_Interrupt_Enable,0);
603 s->async->events |= COMEDI_CB_EOA;
606 static void handle_a_interrupt(comedi_device *dev,unsigned short status,
607 unsigned int m_status)
609 comedi_subdevice *s=dev->subdevices+0;
610 unsigned short ack=0;
612 s->async->events = 0;
614 #ifdef DEBUG_INTERRUPT
615 rt_printk("ni_mio_common: interrupt: a_status=%04x m0_status=%08x\n",
617 ni_mio_print_status_a(status);
622 /* Currently, mite.c requires us to handle LINKC and DONE */
623 if(m_status & CHSR_LINKC){
624 writel(CHOR_CLRLC, devpriv->mite->mite_io_addr + MITE_CHOR(AI_DMA_CHAN));
625 ni_sync_ai_dma(devpriv->mite, dev);
628 if(m_status & CHSR_DONE){
629 writel(CHOR_CLRDONE, devpriv->mite->mite_io_addr + MITE_CHOR(AI_DMA_CHAN));
632 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)){
633 rt_printk("unknown mite interrupt, ack! (m_status=%08x)\n", m_status);
634 //mite_print_chsr(m_status);
635 mite_dma_disarm(devpriv->mite, AI_DMA_CHAN );
636 writel(CHOR_DMARESET, devpriv->mite->mite_io_addr + MITE_CHOR(AI_DMA_CHAN));
637 //disable_irq(dev->irq);
641 /* test for all uncommon interrupt events at the same time */
642 if(status&(AI_Overrun_St|AI_Overflow_St|AI_SC_TC_Error_St|AI_SC_TC_St|AI_START1_St)){
644 rt_printk("ni_mio_common: a_status=0xffff. Card removed?\n");
645 /* we probably aren't even running a command now,
646 * so it's a good idea to be careful. */
647 if(s->subdev_flags&SDF_RUNNING){
648 s->async->events |= COMEDI_CB_ERROR | COMEDI_CB_EOA;
649 //comedi_event(dev,s,s->async->events);
653 if(status&(AI_Overrun_St|AI_Overflow_St|AI_SC_TC_Error_St)){
654 rt_printk("ni_mio_common: ai error a_status=%04x\n",
656 ni_mio_print_status_a(status);
658 ni_ai_reset(dev,dev->subdevices);
661 shutdown_ai_command( dev );
663 s->async->events |= COMEDI_CB_ERROR;
664 if(status & (AI_Overrun_St | AI_Overflow_St))
665 s->async->events |= COMEDI_CB_OVERFLOW;
667 comedi_event(dev,s,s->async->events);
671 if(status&AI_SC_TC_St){
672 #ifdef DEBUG_INTERRUPT
673 rt_printk("ni_mio_common: SC_TC interrupt\n");
675 if(!devpriv->ai_continuous){
676 shutdown_ai_command( dev );
678 ack|=AI_SC_TC_Interrupt_Ack;
680 if(status&AI_START1_St){
681 ack|=AI_START1_Interrupt_Ack;
685 if(status&AI_FIFO_Half_Full_St){
687 static const int timeout = 10;
688 /* pcmcia cards (at least 6036) seem to stop producing interrupts if we
689 *fail to get the fifo less than half full, so loop to be sure.*/
690 for(i = 0; i < timeout; ++i)
692 ni_handle_fifo_half_full(dev);
693 if((devpriv->stc_readw(dev, AI_Status_1_Register) & AI_FIFO_Half_Full_St) == 0)
699 if( (status & AI_STOP_St) ){
700 ni_handle_eos(dev, s);
701 /* we need to ack the START, also */
702 ack |= AI_STOP_Interrupt_Ack|AI_START_Interrupt_Ack;
705 if(devpriv->aimode==AIMODE_SAMPLE){
706 ni_handle_fifo_dregs(dev);
708 //s->async->events |= COMEDI_CB_SAMPLE;
711 if(ack) devpriv->stc_writew(dev, ack,Interrupt_A_Ack_Register);
713 comedi_event(dev,s,s->async->events);
715 #ifdef DEBUG_INTERRUPT
716 status=devpriv->stc_readw(dev, AI_Status_1_Register);
717 if(status&Interrupt_A_St){
718 rt_printk("handle_a_interrupt: didn't clear interrupt? status=0x%x\n", status);
723 static void handle_b_interrupt(comedi_device *dev,unsigned short b_status, unsigned int m_status)
725 comedi_subdevice *s=dev->subdevices+1;
726 //unsigned short ack=0;
727 #ifdef DEBUG_INTERRUPT
728 rt_printk("ni_mio_common: interrupt: b_status=%04x m1_status=%08x\n",
730 ni_mio_print_status_b(b_status);
735 /* Currently, mite.c requires us to handle LINKC and DONE */
736 if(m_status & CHSR_LINKC){
737 mite_handle_b_linkc(devpriv->mite, dev);
740 if(m_status & CHSR_DONE){
741 writel(CHOR_CLRDONE, devpriv->mite->mite_io_addr + MITE_CHOR(AO_DMA_CHAN));
744 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)){
745 rt_printk("unknown mite interrupt, ack! (m_status=%08x)\n", m_status);
746 //mite_print_chsr(m_status);
747 mite_dma_disarm(devpriv->mite, AO_DMA_CHAN );
748 writel(CHOR_DMARESET, devpriv->mite->mite_io_addr + MITE_CHOR(AO_DMA_CHAN));
752 if(b_status==0xffff)return;
753 if(b_status&AO_Overrun_St){
754 rt_printk("ni_mio_common: AO FIFO underrun status=0x%04x status2=0x%04x\n",b_status,devpriv->stc_readw(dev, AO_Status_2_Register));
756 s->async->events |= COMEDI_CB_OVERFLOW;
759 if(b_status&AO_BC_TC_St){
760 MDPRINTK("ni_mio_common: AO BC_TC status=0x%04x status2=0x%04x\n",b_status,devpriv->stc_readw(dev, AO_Status_2_Register));
762 s->async->events |= COMEDI_CB_EOA;
766 if(b_status&AO_FIFO_Request_St){
769 ret = ni_ao_fifo_half_empty(dev,s);
771 rt_printk("ni_mio_common: AO buffer underrun\n");
772 ni_set_bits(dev, Interrupt_B_Enable_Register,
773 AO_FIFO_Interrupt_Enable|AO_Error_Interrupt_Enable, 0);
774 s->async->events |= COMEDI_CB_OVERFLOW;
779 b_status=devpriv->stc_readw(dev, AO_Status_1_Register);
780 if(b_status&Interrupt_B_St){
781 if(b_status&AO_FIFO_Request_St){
782 rt_printk("ni_mio_common: AO buffer underrun\n");
784 rt_printk("Ack! didn't clear AO interrupt. b_status=0x%04x\n",b_status);
785 ni_set_bits(dev,Interrupt_B_Enable_Register,~0,0);
787 s->async->events |= COMEDI_CB_OVERFLOW;
790 comedi_event(dev,s,s->async->events);
793 #ifdef DEBUG_STATUS_A
794 static char *status_a_strings[]={
795 "passthru0","fifo","G0_gate","G0_TC",
796 "stop","start","sc_tc","start1",
797 "start2","sc_tc_error","overflow","overrun",
798 "fifo_empty","fifo_half_full","fifo_full","interrupt_a"
801 static void ni_mio_print_status_a(int status)
805 rt_printk("A status:");
808 rt_printk(" %s",status_a_strings[i]);
815 #ifdef DEBUG_STATUS_B
816 static char *status_b_strings[]={
817 "passthru1","fifo","G1_gate","G1_TC",
818 "UI2_TC","UPDATE","UC_TC","BC_TC",
819 "start1","overrun","start","bc_tc_error",
820 "fifo_empty","fifo_half_full","fifo_full","interrupt_b"
823 static void ni_mio_print_status_b(int status)
827 rt_printk("B status:");
830 rt_printk(" %s",status_b_strings[i]);
839 static void ni_ao_fifo_load(comedi_device *dev,comedi_subdevice *s, int n)
841 comedi_async *async = s->async;
842 comedi_cmd *cmd = &async->cmd;
850 chan = async->cur_chan;
852 err &= comedi_buf_get(async, &d);
855 range = CR_RANGE(cmd->chanlist[chan]);
857 if(boardtype.reg_type & ni_reg_6xxx_mask)
859 packed_data = d & 0xffff;
860 /* 6711 only has 16 bit wide ao fifo */
861 if(boardtype.reg_type != ni_reg_6711)
863 err &= comedi_buf_get(async, &d);
867 packed_data |= ( d << 16 ) & 0xffff0000;
869 ni_writel( packed_data, DAC_FIFO_Data_611x );
871 ni_writew(d, DAC_FIFO_Data);
874 chan %= cmd->chanlist_len;
876 async->cur_chan = chan;
878 async->events |= COMEDI_CB_OVERFLOW;
883 * There's a small problem if the FIFO gets really low and we
884 * don't have the data to fill it. Basically, if after we fill
885 * the FIFO with all the data available, the FIFO is _still_
886 * less than half full, we never clear the interrupt. If the
887 * IRQ is in edge mode, we never get another interrupt, because
888 * this one wasn't cleared. If in level mode, we get flooded
889 * with interrupts that we can't fulfill, because nothing ever
890 * gets put into the buffer.
892 * This kind of situation is recoverable, but it is easier to
893 * just pretend we had a FIFO underrun, since there is a good
894 * chance it will happen anyway. This is _not_ the case for
895 * RT code, as RT code might purposely be running close to the
896 * metal. Needs to be fixed eventually.
898 static int ni_ao_fifo_half_empty(comedi_device *dev,comedi_subdevice *s)
902 n = comedi_buf_read_n_available(s);
904 s->async->events |= COMEDI_CB_OVERFLOW;
908 n /= sizeof(sampl_t);
909 if(n > boardtype.ao_fifo_depth / 2)
910 n = boardtype.ao_fifo_depth / 2;
912 ni_ao_fifo_load(dev,s,n);
914 s->async->events |= COMEDI_CB_BLOCK;
919 static int ni_ao_prep_fifo(comedi_device *dev,comedi_subdevice *s)
924 devpriv->stc_writew(dev, 1,DAC_FIFO_Clear);
925 if(boardtype.reg_type & ni_reg_6xxx_mask)
926 ni_ao_win_outl(dev, 0x6, AO_FIFO_Offset_Load_611x);
929 n = comedi_buf_read_n_available(s);
932 n /= sizeof(sampl_t);
933 if(n > boardtype.ao_fifo_depth)
934 n = boardtype.ao_fifo_depth;
936 ni_ao_fifo_load(dev,s,n);
941 static void ni_ai_fifo_read(comedi_device *dev,comedi_subdevice *s,
944 comedi_async *async = s->async;
947 if(boardtype.reg_type == ni_reg_611x){
951 for( i = 0; i < n / 2; i++ ){
952 dl=ni_readl(ADC_FIFO_Data_611x);
953 /* This may get the hi/lo data in the wrong order */
954 data[0] = (dl>>16) & 0xffff;
955 data[1] = dl & 0xffff;
956 cfc_write_array_to_buffer(s, data, sizeof(data));
958 /* Check if there's a single sample stuck in the FIFO */
960 dl=ni_readl(ADC_FIFO_Data_611x);
961 data[0] = dl & 0xffff;
962 cfc_write_to_buffer(s, data[0]);
964 } else if(boardtype.reg_type == ni_reg_6143){
968 // This just reads the FIFO assuming the data is present, no checks on the FIFO status are performed
969 for(i = 0; i < n / 2; i++){
970 dl = ni_readl(AIFIFO_Data_6143);
972 data[0] = (dl >> 16) & 0xffff;
973 data[1] = dl & 0xffff;
974 cfc_write_array_to_buffer(s, data, sizeof(data));
977 /* Assume there is a single sample stuck in the FIFO */
978 ni_writel(0x01, AIFIFO_Control_6143); // Get stranded sample into FIFO
979 dl = ni_readl(AIFIFO_Data_6143);
980 data[0] = (dl >> 16) & 0xffff;
981 cfc_write_to_buffer(s, data[0]);
984 if( n > sizeof(devpriv->ai_fifo_buffer) / sizeof(devpriv->ai_fifo_buffer[0]))
986 comedi_error( dev, "bug! ai_fifo_buffer too small" );
987 async->events |= COMEDI_CB_ERROR;
990 for(i = 0; i < n; i++){
991 devpriv->ai_fifo_buffer[i] = ni_readw(ADC_FIFO_Data_Register);
993 cfc_write_array_to_buffer( s, devpriv->ai_fifo_buffer,
994 n * sizeof(devpriv->ai_fifo_buffer[0]) );
998 static void ni_handle_fifo_half_full(comedi_device *dev)
1001 comedi_subdevice *s=dev->subdevices+0;
1003 n=boardtype.ai_fifo_depth/2;
1005 ni_ai_fifo_read(dev,s,n);
1010 static int ni_ai_drain_dma(comedi_device *dev )
1012 struct mite_struct *mite = devpriv->mite;
1014 static const int timeout = 10000;
1016 for( i = 0; i < timeout; i++ )
1018 if((devpriv->stc_readw(dev, AI_Status_1_Register) & AI_FIFO_Empty_St) &&
1019 mite_bytes_in_transit(mite, AI_DMA_CHAN) == 0)
1025 rt_printk("ni_mio_common: wait for dma drain timed out\n");
1026 rt_printk("mite_bytes_in_transit=%i, AI_Status1_Register=0x%x\n",
1027 mite_bytes_in_transit(mite, AI_DMA_CHAN), devpriv->stc_readw(dev, AI_Status_1_Register));
1031 ni_sync_ai_dma( mite, dev );
1039 static void ni_handle_fifo_dregs(comedi_device *dev)
1041 comedi_subdevice *s=dev->subdevices+0;
1047 if(boardtype.reg_type == ni_reg_611x){
1048 while((devpriv->stc_readw(dev, AI_Status_1_Register)&AI_FIFO_Empty_St) == 0){
1049 dl=ni_readl(ADC_FIFO_Data_611x);
1051 /* This may get the hi/lo data in the wrong order */
1053 data[1] = (dl&0xffff);
1054 cfc_write_array_to_buffer(s, data, sizeof(data));
1056 }else if(boardtype.reg_type == ni_reg_6143){
1058 while(ni_readl(AIFIFO_Status_6143) & 0x04){
1059 dl = ni_readl(AIFIFO_Data_6143);
1061 /* This may get the hi/lo data in the wrong order */
1062 data[0] = (dl >> 16);
1063 data[1] = (dl & 0xffff);
1064 cfc_write_array_to_buffer(s, data, sizeof(data));
1067 // Check if stranded sample is present
1068 if(ni_readl(AIFIFO_Status_6143) & 0x01){
1069 ni_writel(0x01, AIFIFO_Control_6143); // Get stranded sample into FIFO
1070 dl = ni_readl(AIFIFO_Data_6143);
1071 data[0] = (dl >> 16) & 0xffff;
1072 cfc_write_to_buffer(s, data[0]);
1076 fifo_empty = devpriv->stc_readw(dev, AI_Status_1_Register) & AI_FIFO_Empty_St;
1077 while(fifo_empty == 0)
1079 for(i = 0; i < sizeof(devpriv->ai_fifo_buffer) / sizeof(devpriv->ai_fifo_buffer[0]); i++)
1081 fifo_empty = devpriv->stc_readw(dev, AI_Status_1_Register) & AI_FIFO_Empty_St;
1082 if(fifo_empty) break;
1083 devpriv->ai_fifo_buffer[i] = ni_readw(ADC_FIFO_Data_Register);
1085 cfc_write_array_to_buffer( s, devpriv->ai_fifo_buffer,
1086 i * sizeof(devpriv->ai_fifo_buffer[0]) );
1091 static void get_last_sample_611x( comedi_device *dev )
1093 comedi_subdevice *s=dev->subdevices+0;
1097 if(boardtype.reg_type != ni_reg_611x) return;
1099 /* Check if there's a single sample stuck in the FIFO */
1100 if(ni_readb(XXX_Status)&0x80){
1101 dl=ni_readl(ADC_FIFO_Data_611x);
1103 cfc_write_to_buffer(s, data);
1107 static void get_last_sample_6143(comedi_device* dev)
1109 comedi_subdevice* s = dev->subdevices + 0;
1113 if(boardtype.reg_type != ni_reg_6143) return;
1115 /* Check if there's a single sample stuck in the FIFO */
1116 if(ni_readl(AIFIFO_Status_6143) & 0x01){
1117 ni_writel(0x01, AIFIFO_Control_6143); // Get stranded sample into FIFO
1118 dl = ni_readl(AIFIFO_Data_6143);
1120 /* This may get the hi/lo data in the wrong order */
1121 data = (dl >> 16) & 0xffff;
1122 cfc_write_to_buffer(s, data);
1126 static void ni_ai_munge(comedi_device *dev, comedi_subdevice *s,
1127 void *data, unsigned int num_bytes, unsigned int chan_index )
1129 comedi_async *async = s->async;
1131 unsigned int length = num_bytes / bytes_per_sample(s);
1132 sampl_t *array = data;
1133 lsampl_t *larray = data;
1134 for(i = 0; i < length; i++)
1137 if(s->subdev_flags & SDF_LSAMPL)
1138 larray[i] = le32_to_cpu(larray[i]);
1140 array[i] = le16_to_cpu(array[i]);
1142 if(s->subdev_flags & SDF_LSAMPL)
1143 larray[i] += devpriv->ai_offset[chan_index];
1145 array[i] += devpriv->ai_offset[chan_index];
1147 chan_index %= async->cmd.chanlist_len;
1153 static void ni_ai_setup_MITE_dma(comedi_device *dev,comedi_cmd *cmd)
1155 struct mite_struct *mite = devpriv->mite;
1156 struct mite_channel *mite_chan = &mite->channels[ AI_DMA_CHAN ];
1157 comedi_subdevice *s = dev->subdevices + 0;
1159 /* write alloc the entire buffer */
1160 comedi_buf_write_alloc(s->async, s->async->prealloc_bufsz);
1162 mite_chan->current_link = 0;
1163 mite_chan->dir = COMEDI_INPUT;
1164 switch(boardtype.reg_type)
1168 mite_prep_dma(mite, AI_DMA_CHAN, 32, 16);
1170 case ni_reg_m_series:
1171 if(boardtype.adbits > 16)
1172 mite_prep_dma(mite, AI_DMA_CHAN, 32, 32);
1174 mite_prep_dma(mite, AI_DMA_CHAN, 16, 16); //guess
1177 mite_prep_dma(mite, AI_DMA_CHAN, 16, 16);
1181 mite_dma_arm(mite, AI_DMA_CHAN);
1184 static void ni_ao_setup_MITE_dma(comedi_device *dev,comedi_cmd *cmd)
1186 struct mite_struct *mite = devpriv->mite;
1187 struct mite_channel *mite_chan = &mite->channels[ AO_DMA_CHAN ];
1188 comedi_subdevice *s = dev->subdevices + 1;
1190 devpriv->last_buf_write_count = s->async->buf_write_count;
1192 mite_chan->current_link = 0;
1193 mite_chan->dir = COMEDI_OUTPUT;
1194 if(boardtype.reg_type & (ni_reg_611x | ni_reg_6713))
1195 mite_prep_dma(mite, AO_DMA_CHAN, 32, 32);
1197 mite_prep_dma(mite, AO_DMA_CHAN, 16, 16);
1200 mite_dma_arm(mite, AO_DMA_CHAN);
1206 used for both cancel ioctl and board initialization
1208 this is pretty harsh for a cancel, but it works...
1211 static int ni_ai_reset(comedi_device *dev,comedi_subdevice *s)
1214 mite_dma_disarm(devpriv->mite, AI_DMA_CHAN);
1216 /* ai configuration */
1217 devpriv->stc_writew(dev, AI_Configuration_Start | AI_Reset, Joint_Reset_Register);
1219 ni_set_bits(dev, Interrupt_A_Enable_Register,
1220 AI_SC_TC_Interrupt_Enable | AI_START1_Interrupt_Enable|
1221 AI_START2_Interrupt_Enable| AI_START_Interrupt_Enable|
1222 AI_STOP_Interrupt_Enable| AI_Error_Interrupt_Enable|
1223 AI_FIFO_Interrupt_Enable,0);
1225 ni_flush_ai_fifo(dev);
1227 if(boardtype.reg_type != ni_reg_6143)
1228 ni_writeb(0, Misc_Command);
1230 devpriv->stc_writew(dev, AI_Disarm, AI_Command_1_Register); /* reset pulses */
1231 devpriv->stc_writew(dev, AI_Start_Stop | AI_Mode_1_Reserved /*| AI_Trigger_Once */,
1232 AI_Mode_1_Register);
1233 devpriv->stc_writew(dev, 0x0000,AI_Mode_2_Register);
1234 /* generate FIFO interrupts on non-empty */
1235 devpriv->stc_writew(dev, (0<<6)|0x0000,AI_Mode_3_Register);
1236 if(boardtype.reg_type == ni_reg_611x){
1237 devpriv->stc_writew(dev, AI_SHIFTIN_Pulse_Width |
1239 AI_LOCALMUX_CLK_Pulse_Width, AI_Personal_Register);
1240 devpriv->stc_writew(dev, AI_SCAN_IN_PROG_Output_Select(3) |
1241 AI_EXTMUX_CLK_Output_Select(0) |
1242 AI_LOCALMUX_CLK_Output_Select(2) |
1243 AI_SC_TC_Output_Select(3) |
1244 AI_CONVERT_Output_Select(3),AI_Output_Control_Register);
1245 }else if(boardtype.reg_type == ni_reg_6143){
1246 devpriv->stc_writew(dev, AI_SHIFTIN_Pulse_Width |
1248 AI_LOCALMUX_CLK_Pulse_Width, AI_Personal_Register);
1249 devpriv->stc_writew(dev, AI_SCAN_IN_PROG_Output_Select(3) |
1250 AI_EXTMUX_CLK_Output_Select(0) |
1251 AI_LOCALMUX_CLK_Output_Select(2) |
1252 AI_SC_TC_Output_Select(3) |
1253 AI_CONVERT_Output_Select(2),AI_Output_Control_Register);
1255 devpriv->stc_writew(dev, AI_SHIFTIN_Pulse_Width |
1257 AI_CONVERT_Pulse_Width |
1258 AI_LOCALMUX_CLK_Pulse_Width, AI_Personal_Register);
1259 devpriv->stc_writew(dev, AI_SCAN_IN_PROG_Output_Select(3) |
1260 AI_EXTMUX_CLK_Output_Select(0) |
1261 AI_LOCALMUX_CLK_Output_Select(2) |
1262 AI_SC_TC_Output_Select(3) |
1263 AI_CONVERT_Output_Select(2),AI_Output_Control_Register);
1265 /* the following registers should not be changed, because there
1266 * are no backup registers in devpriv. If you want to change
1267 * any of these, add a backup register and other appropriate code:
1268 * AI_Mode_1_Register
1269 * AI_Mode_3_Register
1270 * AI_Personal_Register
1271 * AI_Output_Control_Register
1273 devpriv->stc_writew(dev, AI_SC_TC_Error_Confirm | AI_START_Interrupt_Ack |
1274 AI_START2_Interrupt_Ack | AI_START1_Interrupt_Ack |
1275 AI_SC_TC_Interrupt_Ack | AI_Error_Interrupt_Ack |
1276 AI_STOP_Interrupt_Ack, Interrupt_A_Ack_Register); /* clear interrupts */
1278 devpriv->stc_writew(dev, AI_Configuration_End,Joint_Reset_Register);
1283 static int ni_ai_poll(comedi_device *dev,comedi_subdevice *s)
1285 unsigned long flags = 0;
1288 // lock to avoid race with interrupt handler
1289 if(in_interrupt() == 0)
1290 comedi_spin_lock_irqsave(&dev->spinlock, flags);
1292 ni_handle_fifo_dregs(dev);
1294 ni_sync_ai_dma(devpriv->mite, dev);
1296 count = s->async->buf_write_count - s->async->buf_read_count;
1297 if(in_interrupt() == 0)
1298 comedi_spin_unlock_irqrestore(&dev->spinlock, flags);
1304 static int ni_ai_insn_read(comedi_device *dev,comedi_subdevice *s,comedi_insn *insn,lsampl_t *data)
1307 const unsigned int mask = (1 << boardtype.adbits) - 1;
1312 ni_load_channelgain_list(dev,1,&insn->chanspec);
1314 ni_flush_ai_fifo(dev);
1316 signbits=devpriv->ai_offset[0];
1317 if(boardtype.reg_type == ni_reg_611x){
1318 for(n=0; n < num_adc_stages_611x; n++){
1319 devpriv->stc_writew(dev, AI_CONVERT_Pulse, AI_Command_1_Register);
1322 for(n=0; n<insn->n; n++){
1323 devpriv->stc_writew(dev, AI_CONVERT_Pulse, AI_Command_1_Register);
1324 /* The 611x has screwy 32-bit FIFOs. */
1326 for(i=0; i<NI_TIMEOUT; i++){
1327 if(ni_readb(XXX_Status)&0x80)
1329 d = ( ni_readl(ADC_FIFO_Data_611x) >> 16 ) & 0xffff;
1332 if(!(devpriv->stc_readw(dev, AI_Status_1_Register)&AI_FIFO_Empty_St))
1334 d = ni_readl(ADC_FIFO_Data_611x) & 0xffff;
1339 rt_printk("ni_mio_common: timeout in 611x ni_ai_insn_read\n");
1345 }else if(boardtype.reg_type == ni_reg_6143){
1346 for(n = 0; n < insn->n; n++){
1347 devpriv->stc_writew(dev, AI_CONVERT_Pulse, AI_Command_1_Register);
1349 /* The 6143 has 32-bit FIFOs. You need to strobe a bit to move a single 16bit stranded sample into the FIFO */
1351 for(i = 0; i < NI_TIMEOUT; i++){
1352 if(ni_readl(AIFIFO_Status_6143) & 0x01)
1354 ni_writel(0x01, AIFIFO_Control_6143); // Get stranded sample into FIFO
1355 dl = ni_readl(AIFIFO_Data_6143);
1359 if(i == NI_TIMEOUT){
1360 rt_printk("ni_mio_common: timeout in 6143 ni_ai_insn_read\n");
1363 data[n] = (((dl >> 16) & 0xFFFF) + signbits) & 0xFFFF;
1366 for(n=0;n<insn->n;n++){
1367 devpriv->stc_writew(dev, AI_CONVERT_Pulse, AI_Command_1_Register);
1368 for(i=0;i<NI_TIMEOUT;i++){
1369 if(!(devpriv->stc_readw(dev, AI_Status_1_Register)&AI_FIFO_Empty_St))
1373 rt_printk("ni_mio_common: timeout in ni_ai_insn_read\n");
1376 if(boardtype.reg_type == ni_reg_m_series)
1378 data[n] = ni_readl(M_Offset_AI_FIFO_Data) & mask;
1381 d = ni_readw(ADC_FIFO_Data_Register);
1382 d += signbits; /* subtle: needs to be short addition */
1390 void ni_prime_channelgain_list(comedi_device *dev)
1393 devpriv->stc_writew(dev, AI_CONVERT_Pulse, AI_Command_1_Register);
1394 for(i = 0; i < NI_TIMEOUT; ++i)
1396 if(!(devpriv->stc_readw(dev, AI_Status_1_Register) & AI_FIFO_Empty_St))
1398 devpriv->stc_writew(dev, 1, ADC_FIFO_Clear);
1403 rt_printk("ni_mio_common: timeout loading channel/gain list\n");
1406 static void ni_m_series_load_channelgain_list(comedi_device *dev,unsigned int n_chan,
1409 unsigned int chan, range, aref;
1411 unsigned config_bits = 0;
1413 unsigned int dither;
1414 unsigned range_code;
1416 devpriv->stc_writew(dev, 1, Configuration_Memory_Clear);
1418 // offset = 1 << (boardtype.adbits - 1);
1419 if((list[0] & CR_ALT_SOURCE))
1421 chan = CR_CHAN(list[0]);
1422 range = CR_RANGE(list[0]);
1423 range_code = ni_gainlkup[boardtype.gainlkup][range];
1424 dither = ((list[0] & CR_ALT_FILTER) != 0);
1425 unsigned bypass_bits = MSeries_AI_Bypass_Config_FIFO_Bit;
1426 bypass_bits |= chan;
1427 bypass_bits |= (devpriv->ai_calib_source) & (MSeries_AI_Bypass_Cal_Sel_Pos_Mask |
1428 MSeries_AI_Bypass_Cal_Sel_Neg_Mask | MSeries_AI_Bypass_Mode_Mux_Mask |
1429 MSeries_AO_Bypass_AO_Cal_Sel_Mask);
1430 bypass_bits |= MSeries_AI_Bypass_Gain_Bits(range_code);
1432 bypass_bits |= MSeries_AI_Bypass_Dither_Bit;
1433 // don't use 2's complement encoding
1434 bypass_bits |= MSeries_AI_Bypass_Polarity_Bit;
1435 ni_writel(bypass_bits, M_Offset_AI_Config_FIFO_Bypass);
1438 ni_writel(0, M_Offset_AI_Config_FIFO_Bypass);
1441 for(i = 0; i < n_chan; i++)
1443 chan = CR_CHAN(list[i]);
1444 aref = CR_AREF(list[i]);
1445 range = CR_RANGE(list[i]);
1446 dither = ((list[i] & CR_ALT_FILTER) != 0);
1448 range_code = ni_gainlkup[boardtype.gainlkup][range];
1449 devpriv->ai_offset[i] = offset;
1454 config_bits |= MSeries_AI_Config_Channel_Type_Differential_Bits;
1457 config_bits |= MSeries_AI_Config_Channel_Type_Common_Ref_Bits;
1460 config_bits |= MSeries_AI_Config_Channel_Type_Ground_Ref_Bits;
1465 config_bits |= MSeries_AI_Config_Channel_Bits(chan);
1466 config_bits |= MSeries_AI_Config_Bank_Bits(chan);
1467 config_bits |= MSeries_AI_Config_Gain_Bits(range_code);
1468 if(i == n_chan - 1) config_bits |= MSeries_AI_Config_Last_Channel_Bit;
1469 if(dither) config_bits |= MSeries_AI_Config_Dither_Bit;
1470 // don't use 2's complement encoding
1471 config_bits |= MSeries_AI_Config_Polarity_Bit;
1472 ni_writew(config_bits, M_Offset_AI_Config_FIFO_Data);
1474 ni_prime_channelgain_list(dev);
1478 * Notes on the 6110 and 6111:
1479 * These boards a slightly different than the rest of the series, since
1480 * they have multiple A/D converters.
1481 * From the driver side, the configuration memory is a
1483 * Configuration Memory Low:
1485 * bit 8: unipolar/bipolar (should be 0 for bipolar)
1486 * bits 0-3: gain. This is 4 bits instead of 3 for the other boards
1487 * 1001 gain=0.1 (+/- 50)
1496 * Configuration Memory High:
1497 * bits 12-14: Channel Type
1498 * 001 for differential
1499 * 000 for calibration
1500 * bit 11: coupling (this is not currently handled)
1504 * valid channels are 0-3
1506 static void ni_load_channelgain_list(comedi_device *dev,unsigned int n_chan,
1509 unsigned int chan,range,aref;
1513 unsigned int dither;
1515 if(boardtype.reg_type == ni_reg_m_series)
1517 ni_m_series_load_channelgain_list(dev, n_chan, list);
1520 if(n_chan == 1 && (boardtype.reg_type != ni_reg_611x) && (boardtype.reg_type != ni_reg_6143)){
1521 if(devpriv->changain_state && devpriv->changain_spec==list[0]){
1525 devpriv->changain_state=1;
1526 devpriv->changain_spec=list[0];
1528 devpriv->changain_state=0;
1531 devpriv->stc_writew(dev, 1,Configuration_Memory_Clear);
1533 // Set up Calibration mode if required
1534 if(boardtype.reg_type == ni_reg_6143){
1535 if((list[0] & CR_ALT_SOURCE) && !devpriv->ai_calib_source_enabled){
1536 // Strobe Relay enable bit
1537 ni_writew(devpriv->ai_calib_source | Calibration_Channel_6143_RelayOn, Calibration_Channel_6143);
1538 ni_writew(devpriv->ai_calib_source, Calibration_Channel_6143);
1539 devpriv->ai_calib_source_enabled = 1;
1540 msleep_interruptible(100); // Allow relays to change
1542 else if(!(list[0] & CR_ALT_SOURCE) && devpriv->ai_calib_source_enabled){
1543 // Strobe Relay disable bit
1544 ni_writew(devpriv->ai_calib_source | Calibration_Channel_6143_RelayOff, Calibration_Channel_6143);
1545 ni_writew(devpriv->ai_calib_source, Calibration_Channel_6143);
1546 devpriv->ai_calib_source_enabled = 0;
1547 msleep_interruptible(100); // Allow relays to change
1551 offset=1<<(boardtype.adbits-1);
1552 for(i=0;i<n_chan;i++){
1553 if((boardtype.reg_type != ni_reg_6143) && (list[i] & CR_ALT_SOURCE)){
1554 chan=devpriv->ai_calib_source;
1556 chan=CR_CHAN(list[i]);
1558 aref=CR_AREF(list[i]);
1559 range=CR_RANGE(list[i]);
1560 dither=((list[i]&CR_ALT_FILTER)!=0);
1562 /* fix the external/internal range differences */
1563 range = ni_gainlkup[boardtype.gainlkup][range];
1564 if(boardtype.reg_type == ni_reg_611x)
1565 devpriv->ai_offset[i] = offset;
1567 devpriv->ai_offset[i] = (range&0x100)?0:offset;
1570 if( ( list[i] & CR_ALT_SOURCE ) )
1572 if(boardtype.reg_type == ni_reg_611x)
1573 ni_writew(CR_CHAN(list[i])&0x0003, Calibration_Channel_Select_611x);
1576 if(boardtype.reg_type == ni_reg_611x)
1578 else if(boardtype.reg_type == ni_reg_6143)
1583 hi |= AI_DIFFERENTIAL;
1595 hi |= AI_CONFIG_CHANNEL( chan );
1597 ni_writew(hi,Configuration_Memory_High);
1599 if(boardtype.reg_type != ni_reg_6143){
1601 if(i == n_chan - 1) lo |= AI_LAST_CHANNEL;
1602 if( dither ) lo |= AI_DITHER;
1604 ni_writew(lo,Configuration_Memory_Low);
1608 /* prime the channel/gain list */
1609 if((boardtype.reg_type != ni_reg_611x) && (boardtype.reg_type != ni_reg_6143)){
1610 ni_prime_channelgain_list(dev);
1614 #define TIMER_BASE 50 /* 20 Mhz base */
1616 static int ni_ns_to_timer(int *nanosec,int round_mode)
1623 case TRIG_ROUND_NEAREST:
1625 divider=(*nanosec+base/2)/base;
1627 case TRIG_ROUND_DOWN:
1628 divider=(*nanosec)/base;
1631 divider=(*nanosec+base-1)/base;
1635 *nanosec=base*divider;
1639 static int ni_ai_cmdtest(comedi_device *dev,comedi_subdevice *s,comedi_cmd *cmd)
1645 /* step 1: make sure trigger sources are trivially valid */
1648 cmd->start_src &= TRIG_NOW|TRIG_INT|TRIG_EXT;
1649 if(!cmd->start_src || tmp!=cmd->start_src)err++;
1651 tmp=cmd->scan_begin_src;
1652 cmd->scan_begin_src &= TRIG_TIMER|TRIG_EXT;
1653 if(!cmd->scan_begin_src || tmp!=cmd->scan_begin_src)err++;
1655 tmp=cmd->convert_src;
1656 sources = TRIG_TIMER | TRIG_EXT;
1657 if((boardtype.reg_type == ni_reg_611x) || (boardtype.reg_type == ni_reg_6143)) sources |= TRIG_NOW;
1658 cmd->convert_src &= sources;
1659 if(!cmd->convert_src || tmp!=cmd->convert_src)err++;
1661 tmp=cmd->scan_end_src;
1662 cmd->scan_end_src &= TRIG_COUNT;
1663 if(!cmd->scan_end_src || tmp!=cmd->scan_end_src)err++;
1666 cmd->stop_src &= TRIG_COUNT|TRIG_NONE;
1667 if(!cmd->stop_src || tmp!=cmd->stop_src)err++;
1671 /* step 2: make sure trigger sources are unique and mutually compatible */
1673 /* note that mutual compatiblity is not an issue here */
1674 if(cmd->start_src!=TRIG_NOW &&
1675 cmd->start_src!=TRIG_INT &&
1676 cmd->start_src!=TRIG_EXT)err++;
1677 if(cmd->scan_begin_src!=TRIG_TIMER &&
1678 cmd->scan_begin_src!=TRIG_EXT &&
1679 cmd->scan_begin_src!=TRIG_OTHER)err++;
1680 if(cmd->convert_src!=TRIG_TIMER &&
1681 cmd->convert_src!=TRIG_EXT &&
1682 cmd->convert_src!=TRIG_NOW)err++;
1683 if(cmd->stop_src!=TRIG_COUNT &&
1684 cmd->stop_src!=TRIG_NONE)err++;
1688 /* step 3: make sure arguments are trivially compatible */
1690 if(cmd->start_src==TRIG_EXT){
1691 /* external trigger */
1692 unsigned int tmp = CR_CHAN(cmd->start_arg);
1694 if(tmp > 16) tmp = 16;
1695 tmp |= (cmd->start_arg & (CR_INVERT | CR_EDGE));
1696 if(cmd->start_arg != tmp){
1697 cmd->start_arg = tmp;
1701 if(cmd->start_arg!=0){
1702 /* true for both TRIG_NOW and TRIG_INT */
1707 if(cmd->scan_begin_src==TRIG_TIMER){
1708 if(cmd->scan_begin_arg<boardtype.ai_speed){
1709 cmd->scan_begin_arg=boardtype.ai_speed;
1712 if(cmd->scan_begin_arg>TIMER_BASE*0xffffff){
1713 cmd->scan_begin_arg=TIMER_BASE*0xffffff;
1716 }else if(cmd->scan_begin_src==TRIG_EXT){
1717 /* external trigger */
1718 unsigned int tmp = CR_CHAN(cmd->scan_begin_arg);
1721 tmp |= (cmd->scan_begin_arg & (CR_INVERT | CR_EDGE));
1722 if(cmd->scan_begin_arg!=tmp){
1723 cmd->scan_begin_arg = tmp;
1726 }else{ /* TRIG_OTHER */
1727 if(cmd->scan_begin_arg){
1728 cmd->scan_begin_arg=0;
1732 if(cmd->convert_src==TRIG_TIMER){
1733 if((boardtype.reg_type == ni_reg_611x) || (boardtype.reg_type == ni_reg_6143)){
1734 if(cmd->convert_arg != 0){
1735 cmd->convert_arg = 0;
1739 if(cmd->convert_arg<boardtype.ai_speed){
1740 cmd->convert_arg=boardtype.ai_speed;
1743 if(cmd->convert_arg>TIMER_BASE*0xffff){
1744 cmd->convert_arg=TIMER_BASE*0xffff;
1748 }else if(cmd->convert_src == TRIG_EXT){
1749 /* external trigger */
1750 unsigned int tmp = CR_CHAN(cmd->convert_arg);
1753 tmp |= (cmd->convert_arg&(CR_ALT_FILTER|CR_INVERT));
1754 if(cmd->convert_arg!=tmp){
1755 cmd->convert_arg = tmp;
1758 }else if(cmd->convert_src == TRIG_NOW){
1759 if(cmd->convert_arg != 0){
1760 cmd->convert_arg = 0;
1765 if(cmd->scan_end_arg!=cmd->chanlist_len){
1766 cmd->scan_end_arg=cmd->chanlist_len;
1769 if(cmd->stop_src==TRIG_COUNT){
1770 unsigned int max_count = 0x01000000;
1772 if(boardtype.reg_type == ni_reg_611x )
1773 max_count -= num_adc_stages_611x;
1774 if(cmd->stop_arg > max_count){
1775 cmd->stop_arg = max_count;
1778 if(cmd->stop_arg < 1){
1784 if(cmd->stop_arg!=0){
1792 /* step 4: fix up any arguments */
1794 if(cmd->scan_begin_src==TRIG_TIMER){
1795 tmp=cmd->scan_begin_arg;
1796 ni_ns_to_timer(&cmd->scan_begin_arg,cmd->flags&TRIG_ROUND_MASK);
1797 if(tmp!=cmd->scan_begin_arg)err++;
1799 if(cmd->convert_src==TRIG_TIMER){
1800 if((boardtype.reg_type != ni_reg_611x) && (boardtype.reg_type != ni_reg_6143)){
1801 tmp=cmd->convert_arg;
1802 ni_ns_to_timer(&cmd->convert_arg,cmd->flags&TRIG_ROUND_MASK);
1803 if(tmp!=cmd->convert_arg)err++;
1804 if(cmd->scan_begin_src==TRIG_TIMER &&
1805 cmd->scan_begin_arg<cmd->convert_arg*cmd->scan_end_arg){
1806 cmd->scan_begin_arg=cmd->convert_arg*cmd->scan_end_arg;
1817 static int ni_ai_cmd(comedi_device *dev,comedi_subdevice *s)
1819 comedi_cmd *cmd=&s->async->cmd;
1821 int mode1=0; /* mode1 is needed for both stop and convert */
1823 int start_stop_select=0;
1824 unsigned int stop_count;
1825 int interrupt_a_enable=0;
1827 MDPRINTK("ni_ai_cmd\n");
1830 comedi_error(dev, "cannot run command without an irq");
1833 ni_flush_ai_fifo(dev);
1835 ni_load_channelgain_list(dev,cmd->chanlist_len,cmd->chanlist);
1837 /* start configuration */
1838 devpriv->stc_writew(dev, AI_Configuration_Start,Joint_Reset_Register);
1840 /* disable analog triggering for now, since it
1841 * interferes with the use of pfi0 */
1842 devpriv->an_trig_etc_reg &= ~Analog_Trigger_Enable;
1843 devpriv->stc_writew(dev, devpriv->an_trig_etc_reg, Analog_Trigger_Etc_Register);
1845 switch(cmd->start_src){
1848 devpriv->stc_writew(dev, AI_START2_Select(0)|
1849 AI_START1_Sync|AI_START1_Edge|AI_START1_Select(0),
1850 AI_Trigger_Select_Register);
1854 int chan = CR_CHAN(cmd->start_arg);
1855 unsigned int bits = AI_START2_Select(0)|
1857 AI_START1_Select(chan + 1);
1859 if(cmd->start_arg & CR_INVERT)
1860 bits |= AI_START1_Polarity;
1861 if(cmd->start_arg & CR_EDGE)
1862 bits |= AI_START1_Edge;
1863 devpriv->stc_writew(dev, bits, AI_Trigger_Select_Register);
1868 mode2 &= ~AI_Pre_Trigger;
1869 mode2 &= ~AI_SC_Initial_Load_Source;
1870 mode2 &= ~AI_SC_Reload_Mode;
1871 devpriv->stc_writew(dev, mode2, AI_Mode_2_Register);
1873 if(cmd->chanlist_len == 1 || (boardtype.reg_type == ni_reg_611x) || (boardtype.reg_type == ni_reg_6143)){
1874 start_stop_select |= AI_STOP_Polarity;
1875 start_stop_select |= AI_STOP_Select( 31 ); // logic low
1876 start_stop_select |= AI_STOP_Sync;
1879 start_stop_select |= AI_STOP_Select(19); // ai configuration memory
1881 devpriv->stc_writew(dev, start_stop_select, AI_START_STOP_Select_Register);
1883 devpriv->ai_cmd2 = 0;
1884 switch(cmd->stop_src){
1886 stop_count = cmd->stop_arg - 1;
1888 if(boardtype.reg_type == ni_reg_611x){
1889 // have to take 3 stage adc pipeline into account
1890 stop_count += num_adc_stages_611x;
1892 /* stage number of scans */
1893 devpriv->stc_writel(dev, stop_count, AI_SC_Load_A_Registers);
1895 mode1 |= AI_Start_Stop | AI_Mode_1_Reserved | AI_Trigger_Once;
1896 devpriv->stc_writew(dev, mode1,AI_Mode_1_Register);
1897 /* load SC (Scan Count) */
1898 devpriv->stc_writew(dev, AI_SC_Load,AI_Command_1_Register);
1900 devpriv->ai_continuous = 0;
1901 if( stop_count == 0 ){
1902 devpriv->ai_cmd2 |= AI_End_On_End_Of_Scan;
1903 interrupt_a_enable |= AI_STOP_Interrupt_Enable;
1904 // this is required to get the last sample for chanlist_len > 1, not sure why
1905 if(cmd->chanlist_len > 1)
1906 start_stop_select |= AI_STOP_Polarity | AI_STOP_Edge;
1910 /* stage number of scans */
1911 devpriv->stc_writel(dev, 0,AI_SC_Load_A_Registers);
1913 mode1 |= AI_Start_Stop | AI_Mode_1_Reserved | AI_Continuous;
1914 devpriv->stc_writew(dev, mode1,AI_Mode_1_Register);
1916 /* load SC (Scan Count) */
1917 devpriv->stc_writew(dev, AI_SC_Load,AI_Command_1_Register);
1919 devpriv->ai_continuous = 1;
1924 switch(cmd->scan_begin_src){
1927 stop bits for non 611x boards
1928 AI_SI_Special_Trigger_Delay=0
1930 AI_START_STOP_Select_Register:
1931 AI_START_Polarity=0 (?) rising edge
1932 AI_START_Edge=1 edge triggered
1934 AI_START_Select=0 SI_TC
1935 AI_STOP_Polarity=0 rising edge
1936 AI_STOP_Edge=0 level
1938 AI_STOP_Select=19 external pin (configuration mem)
1940 start_stop_select |= AI_START_Edge | AI_START_Sync;
1941 devpriv->stc_writew(dev, start_stop_select, AI_START_STOP_Select_Register);
1943 mode2 |= AI_SI_Reload_Mode(0);
1944 /* AI_SI_Initial_Load_Source=A */
1945 mode2 &= ~AI_SI_Initial_Load_Source;
1946 //mode2 |= AI_SC_Reload_Mode;
1947 devpriv->stc_writew(dev, mode2, AI_Mode_2_Register);
1950 timer=ni_ns_to_timer(&cmd->scan_begin_arg,TRIG_ROUND_NEAREST);
1951 devpriv->stc_writel(dev, timer,AI_SI_Load_A_Registers);
1952 devpriv->stc_writew(dev, AI_SI_Load,AI_Command_1_Register);
1955 if( cmd->scan_begin_arg & CR_EDGE )
1956 start_stop_select |= AI_START_Edge;
1957 /* AI_START_Polarity==1 is falling edge */
1958 if( cmd->scan_begin_arg & CR_INVERT )
1959 start_stop_select |= AI_START_Polarity;
1960 if( cmd->scan_begin_src != cmd->convert_src ||
1961 ( cmd->scan_begin_arg & ~CR_EDGE ) != ( cmd->convert_arg & ~CR_EDGE ) )
1962 start_stop_select |= AI_START_Sync;
1963 start_stop_select |= AI_START_Select(1 + CR_CHAN(cmd->scan_begin_arg));
1964 devpriv->stc_writew(dev, start_stop_select, AI_START_STOP_Select_Register);
1968 switch(cmd->convert_src){
1971 if( cmd->convert_arg == 0 || cmd->convert_src == TRIG_NOW )
1974 timer=ni_ns_to_timer(&cmd->convert_arg, TRIG_ROUND_NEAREST);
1975 devpriv->stc_writew(dev, 1,AI_SI2_Load_A_Register); /* 0,0 does not work. */
1976 devpriv->stc_writew(dev, timer,AI_SI2_Load_B_Register);
1978 /* AI_SI2_Reload_Mode = alternate */
1979 /* AI_SI2_Initial_Load_Source = A */
1980 mode2 &= ~AI_SI2_Initial_Load_Source;
1981 mode2 |= AI_SI2_Reload_Mode;
1982 devpriv->stc_writew(dev, mode2, AI_Mode_2_Register);
1985 devpriv->stc_writew(dev, AI_SI2_Load,AI_Command_1_Register);
1987 mode2 |= AI_SI2_Reload_Mode; // alternate
1988 mode2 |= AI_SI2_Initial_Load_Source; // B
1990 devpriv->stc_writew(dev, mode2,AI_Mode_2_Register);
1993 mode1 |= AI_CONVERT_Source_Select(1+cmd->convert_arg);
1994 if( ( cmd->convert_arg & CR_INVERT ) == 0 )
1995 mode1 |= AI_CONVERT_Source_Polarity;
1996 devpriv->stc_writew(dev, mode1,AI_Mode_1_Register);
1998 mode2 |= AI_Start_Stop_Gate_Enable | AI_SC_Gate_Enable;
1999 devpriv->stc_writew(dev, mode2, AI_Mode_2_Register);
2006 /* interrupt on FIFO, errors, SC_TC */
2007 interrupt_a_enable |= AI_Error_Interrupt_Enable|
2008 AI_SC_TC_Interrupt_Enable;
2011 interrupt_a_enable|=AI_FIFO_Interrupt_Enable;
2014 if(cmd->flags & TRIG_WAKE_EOS || (devpriv->ai_cmd2 & AI_End_On_End_Of_Scan)){
2015 /* wake on end-of-scan */
2016 devpriv->aimode=AIMODE_SCAN;
2018 devpriv->aimode=AIMODE_HALF_FULL;
2021 switch(devpriv->aimode){
2022 case AIMODE_HALF_FULL:
2023 /*generate FIFO interrupts and DMA requests on half-full */
2025 devpriv->stc_writew(dev, AI_FIFO_Mode_HF_to_E, AI_Mode_3_Register);
2027 devpriv->stc_writew(dev, AI_FIFO_Mode_HF, AI_Mode_3_Register);
2031 /*generate FIFO interrupts on non-empty */
2032 devpriv->stc_writew(dev, AI_FIFO_Mode_NE, AI_Mode_3_Register);
2036 devpriv->stc_writew(dev, AI_FIFO_Mode_NE, AI_Mode_3_Register);
2038 devpriv->stc_writew(dev, AI_FIFO_Mode_HF, AI_Mode_3_Register);
2040 interrupt_a_enable |= AI_STOP_Interrupt_Enable;
2046 devpriv->stc_writew(dev, 0x3f80,Interrupt_A_Ack_Register); /* clear interrupts */
2048 ni_set_bits(dev, Interrupt_A_Enable_Register, interrupt_a_enable, 1);
2050 MDPRINTK("Interrupt_A_Enable_Register = 0x%04x\n",devpriv->int_a_enable_reg);
2052 /* interrupt on nothing */
2053 ni_set_bits(dev, Interrupt_A_Enable_Register, ~0, 0);
2055 /* XXX start polling if necessary */
2056 MDPRINTK("interrupting on nothing\n");
2059 /* end configuration */
2060 devpriv->stc_writew(dev, AI_Configuration_End,Joint_Reset_Register);
2062 switch(cmd->scan_begin_src){
2064 devpriv->stc_writew(dev, AI_SI2_Arm | AI_SI_Arm | AI_DIV_Arm | AI_SC_Arm,
2065 AI_Command_1_Register);
2068 /* XXX AI_SI_Arm? */
2069 devpriv->stc_writew(dev, AI_SI2_Arm | AI_SI_Arm | AI_DIV_Arm | AI_SC_Arm,
2070 AI_Command_1_Register);
2075 ni_ai_setup_MITE_dma(dev,cmd);
2076 //mite_dump_regs(devpriv->mite);
2079 switch(cmd->start_src){
2081 /* AI_START1_Pulse */
2082 devpriv->stc_writew(dev, AI_START1_Pulse | devpriv->ai_cmd2, AI_Command_2_Register );
2083 s->async->inttrig=NULL;
2086 s->async->inttrig=NULL;
2089 s->async->inttrig=ni_ai_inttrig;
2093 MDPRINTK("exit ni_ai_cmd\n");
2098 static int ni_ai_inttrig(comedi_device *dev,comedi_subdevice *s,
2099 unsigned int trignum)
2101 if(trignum!=0)return -EINVAL;
2103 devpriv->stc_writew(dev, AI_START1_Pulse | devpriv->ai_cmd2, AI_Command_2_Register );
2104 s->async->inttrig=NULL;
2109 static int ni_ai_config_analog_trig(comedi_device *dev,comedi_subdevice *s,
2110 comedi_insn *insn, lsampl_t *data);
2112 static int ni_ai_insn_config(comedi_device *dev,comedi_subdevice *s,
2113 comedi_insn *insn, lsampl_t *data)
2115 if(insn->n<1)return -EINVAL;
2118 case INSN_CONFIG_ANALOG_TRIG:
2119 return ni_ai_config_analog_trig(dev,s,insn,data);
2120 case INSN_CONFIG_ALT_SOURCE:
2121 if(boardtype.reg_type == ni_reg_m_series)
2123 if(data[1] & ~(MSeries_AI_Bypass_Cal_Sel_Pos_Mask |
2124 MSeries_AI_Bypass_Cal_Sel_Neg_Mask | MSeries_AI_Bypass_Mode_Mux_Mask |
2125 MSeries_AO_Bypass_AO_Cal_Sel_Mask))
2129 devpriv->ai_calib_source = data[1];
2130 } else if(boardtype.reg_type == ni_reg_6143)
2132 unsigned int calib_source;
2134 calib_source = data[1] & 0xf;
2137 if(calib_source > 0xF)
2140 devpriv->ai_calib_source = calib_source;
2141 ni_writew(calib_source, Calibration_Channel_6143);
2144 unsigned int calib_source;
2145 unsigned int calib_source_adjust;
2147 calib_source = data[1] & 0xf;
2148 calib_source_adjust = ( data[1] >> 4 ) & 0xff;
2150 if(calib_source >= 8)
2152 devpriv->ai_calib_source = calib_source;
2153 if(boardtype.reg_type == ni_reg_611x){
2154 ni_writeb( calib_source_adjust, Cal_Gain_Select_611x );
2165 static int ni_ai_config_analog_trig(comedi_device *dev,comedi_subdevice *s,
2166 comedi_insn *insn, lsampl_t *data)
2168 unsigned int a,b,modebits;
2172 * data[2] is analog line
2173 * data[3] is set level
2174 * data[4] is reset level */
2175 if(!boardtype.has_analog_trig)return -EINVAL;
2176 if(insn->n!=5)return -EINVAL;
2177 if((data[1]&0xffff0000) != COMEDI_EV_SCAN_BEGIN){
2178 data[1]&=~(COMEDI_EV_SCAN_BEGIN&0xffff);
2181 if(data[2]>=boardtype.n_adchan){
2182 data[2]=boardtype.n_adchan-1;
2185 if(data[3]>255){ /* a */
2189 if(data[4]>255){ /* b */
2200 * high mode 00 00 01 10
2201 * low mode 00 00 10 01
2203 * hysteresis low mode 10 00 00 01
2204 * hysteresis high mode 01 00 00 10
2205 * middle mode 10 01 01 10
2210 modebits=data[1]&0xff;
2212 /* two level mode */
2217 modebits=((data[1]&0xf)<<4)|((data[1]&0xf0)>>4);
2219 devpriv->atrig_low = a;
2220 devpriv->atrig_high = b;
2222 case 0x81: /* low hysteresis mode */
2223 devpriv->atrig_mode = 6;
2225 case 0x42: /* high hysteresis mode */
2226 devpriv->atrig_mode = 3;
2228 case 0x96: /* middle window mode */
2229 devpriv->atrig_mode = 2;
2236 /* one level mode */
2242 case 0x06: /* high window mode */
2243 devpriv->atrig_high = a;
2244 devpriv->atrig_mode = 0;
2246 case 0x09: /* low window mode */
2247 devpriv->atrig_low = a;
2248 devpriv->atrig_mode = 1;
2255 if(err)return -EAGAIN;
2259 /* munge data from unsigned to 2's complement for analog output bipolar modes */
2260 static void ni_ao_munge(comedi_device *dev, comedi_subdevice *s,
2261 void *data, unsigned int num_bytes, unsigned int chan_index )
2263 comedi_async *async = s->async;
2266 unsigned int offset;
2267 unsigned int length = num_bytes / sizeof( sampl_t );
2268 sampl_t *array = data;
2270 offset = 1 << (boardtype.aobits - 1);
2271 for(i = 0; i < length; i++)
2273 range = CR_RANGE( async->cmd.chanlist[ chan_index ] );
2274 if(boardtype.ao_unipolar == 0 || (range & 1) == 0 )
2277 array[i] = cpu_to_le16( array[i] );
2280 chan_index %= async->cmd.chanlist_len;
2284 static int ni_ao_config_chanlist(comedi_device *dev, comedi_subdevice *s,
2285 unsigned int chanspec[], unsigned int n_chans)
2293 for(i=0;i<n_chans;i++){
2294 chan = CR_CHAN(chanspec[i]);
2295 range = CR_RANGE(chanspec[i]);
2296 if(boardtype.reg_type == ni_reg_m_series)
2298 comedi_krange *krange = s->range_table->range + range;
2301 switch(krange->max - krange->min)
2304 conf |= MSeries_AO_DAC_Reference_10V_Internal_Bits;
2305 ni_writeb(0, M_Offset_AO_Reference_Attenuation(chan));
2308 conf |= MSeries_AO_DAC_Reference_5V_Internal_Bits;
2309 ni_writeb(0, M_Offset_AO_Reference_Attenuation(chan));
2312 conf |= MSeries_AO_DAC_Reference_10V_Internal_Bits;
2313 ni_writeb(MSeries_Attenuate_x5_Bit, M_Offset_AO_Reference_Attenuation(chan));
2316 conf |= MSeries_AO_DAC_Reference_5V_Internal_Bits;
2317 ni_writeb(MSeries_Attenuate_x5_Bit, M_Offset_AO_Reference_Attenuation(chan));
2320 rt_printk("%s: bug! unhandled ao reference voltage\n", __FUNCTION__);
2323 switch(krange->max + krange->min)
2326 conf |= MSeries_AO_DAC_Offset_0V_Bits;
2329 conf |= MSeries_AO_DAC_Offset_5V_Bits;
2332 rt_printk("%s: bug! unhandled ao offset voltage\n", __FUNCTION__);
2335 ni_writeb(conf, M_Offset_AO_Config_Bank(chan));
2338 conf = AO_Channel(chan);
2340 if(boardtype.ao_unipolar){
2343 invert = (1<<(boardtype.aobits-1));
2351 invert = (1<<(boardtype.aobits-1));
2354 /* not all boards can deglitch, but this shouldn't hurt */
2355 if(chanspec[i] & CR_DEGLITCH)
2356 conf |= AO_Deglitch;
2358 /* analog reference */
2359 /* AREF_OTHER connects AO ground to AI ground, i think */
2360 conf |= (CR_AREF(chanspec[i])==AREF_OTHER)? AO_Ground_Ref : 0;
2362 ni_writew(conf,AO_Configuration);
2364 devpriv->ao_conf[chan] = conf;
2369 static int ni_ao_insn_read(comedi_device *dev,comedi_subdevice *s,
2370 comedi_insn *insn,lsampl_t *data)
2372 data[0] = devpriv->ao[CR_CHAN(insn->chanspec)];
2377 static int ni_ao_insn_write(comedi_device *dev,comedi_subdevice *s,
2378 comedi_insn *insn,lsampl_t *data)
2380 unsigned int chan = CR_CHAN(insn->chanspec);
2381 unsigned int invert;
2383 invert = ni_ao_config_chanlist(dev,s,&insn->chanspec,1);
2385 devpriv->ao[chan] = data[0];
2387 if(boardtype.reg_type == ni_reg_m_series)
2389 ni_writew(data[0], M_Offset_DAC_Direct_Data(chan));
2392 ni_writew(data[0] ^ invert,(chan)? DAC1_Direct_Data : DAC0_Direct_Data);
2397 static int ni_ao_insn_write_671x(comedi_device *dev,comedi_subdevice *s,
2398 comedi_insn *insn,lsampl_t *data)
2400 unsigned int chan = CR_CHAN(insn->chanspec);
2401 unsigned int invert;
2403 ao_win_out(1 << chan, AO_Immediate_671x);
2404 invert = 1 << (boardtype.aobits - 1);
2406 ni_ao_config_chanlist(dev,s,&insn->chanspec,1);
2408 devpriv->ao[chan] = data[0];
2409 ao_win_out(data[0] ^ invert, DACx_Direct_Data_671x(chan));
2414 static int ni_ao_inttrig(comedi_device *dev,comedi_subdevice *s,
2415 unsigned int trignum)
2418 int interrupt_b_bits;
2420 static const int timeout = 1000;
2422 if(trignum!=0)return -EINVAL;
2424 ni_set_bits(dev, Interrupt_B_Enable_Register, AO_FIFO_Interrupt_Enable | AO_Error_Interrupt_Enable, 0);
2425 interrupt_b_bits = AO_Error_Interrupt_Enable;
2427 devpriv->stc_writew(dev, 1, DAC_FIFO_Clear);
2428 if(boardtype.reg_type & ni_reg_6xxx_mask)
2429 ni_ao_win_outl(dev, 0x6, AO_FIFO_Offset_Load_611x);
2430 ni_ao_setup_MITE_dma(dev, &s->async->cmd);
2431 ret = ni_ao_wait_for_dma_load(dev);
2432 if(ret < 0) return ret;
2435 ret = ni_ao_prep_fifo(dev,s);
2436 if(ret==0)return -EPIPE;
2438 interrupt_b_bits |= AO_FIFO_Interrupt_Enable;
2441 devpriv->stc_writew(dev, devpriv->ao_mode3|AO_Not_An_UPDATE,AO_Mode_3_Register);
2442 devpriv->stc_writew(dev, devpriv->ao_mode3,AO_Mode_3_Register);
2443 /* wait for DACs to be loaded */
2444 for(i = 0; i < timeout; i++)
2447 if((devpriv->stc_readw(dev, Joint_Status_2_Register) & AO_TMRDACWRs_In_Progress_St) == 0)
2452 comedi_error(dev, "timed out waiting for AO_TMRDACWRs_In_Progress_St to clear");
2455 // stc manual says we are need to clear error interrupt after AO_TMRDACWRs_In_Progress_St clears
2456 devpriv->stc_writew(dev, AO_Error_Interrupt_Ack, Interrupt_B_Ack_Register);
2458 ni_set_bits(dev, Interrupt_B_Enable_Register, interrupt_b_bits, 1);
2460 devpriv->stc_writew(dev, devpriv->ao_cmd1|AO_UI_Arm|AO_UC_Arm|AO_BC_Arm|AO_DAC1_Update_Mode|AO_DAC0_Update_Mode,
2461 AO_Command_1_Register);
2463 devpriv->stc_writew(dev, devpriv->ao_cmd2|AO_START1_Pulse,AO_Command_2_Register);
2465 s->async->inttrig=NULL;
2470 static int ni_ao_cmd(comedi_device *dev,comedi_subdevice *s)
2472 comedi_cmd *cmd = &s->async->cmd;
2479 comedi_error(dev, "cannot run command without an irq");
2482 trigvar = ni_ns_to_timer(&cmd->scan_begin_arg,TRIG_ROUND_NEAREST);
2484 devpriv->stc_writew(dev, AO_Configuration_Start,Joint_Reset_Register);
2486 devpriv->stc_writew(dev, AO_Disarm,AO_Command_1_Register);
2488 if(boardtype.reg_type & ni_reg_6xxx_mask)
2490 ao_win_out(CLEAR_WG, AO_Misc_611x);
2493 for(i = 0; i < cmd->chanlist_len; i++)
2497 chan = CR_CHAN(cmd->chanlist[i]);
2499 ao_win_out(chan, AO_Waveform_Generation_611x);
2501 ao_win_out(bits, AO_Timed_611x);
2504 ni_ao_config_chanlist(dev,s,cmd->chanlist,cmd->chanlist_len);
2506 if(cmd->stop_src==TRIG_NONE){
2507 devpriv->ao_mode1|=AO_Continuous;
2508 devpriv->ao_mode1&=~AO_Trigger_Once;
2510 devpriv->ao_mode1&=~AO_Continuous;
2511 devpriv->ao_mode1|=AO_Trigger_Once;
2513 devpriv->stc_writew(dev, devpriv->ao_mode1,AO_Mode_1_Register);
2514 devpriv->ao_trigger_select&=~(AO_START1_Polarity|AO_START1_Select(-1));
2515 devpriv->ao_trigger_select|=AO_START1_Edge|AO_START1_Sync;
2516 devpriv->stc_writew(dev, devpriv->ao_trigger_select,AO_Trigger_Select_Register);
2517 devpriv->ao_mode3&=~AO_Trigger_Length;
2518 devpriv->stc_writew(dev, devpriv->ao_mode3,AO_Mode_3_Register);
2520 devpriv->stc_writew(dev, devpriv->ao_mode1,AO_Mode_1_Register);
2521 devpriv->ao_mode2&=~AO_BC_Initial_Load_Source;
2522 devpriv->stc_writew(dev, devpriv->ao_mode2,AO_Mode_2_Register);
2523 if(cmd->stop_src==TRIG_NONE){
2524 devpriv->stc_writel(dev, 0xffffff,AO_BC_Load_A_Register);
2526 devpriv->stc_writel(dev, 0,AO_BC_Load_A_Register);
2528 devpriv->stc_writew(dev, AO_BC_Load,AO_Command_1_Register);
2529 devpriv->ao_mode2&=~AO_UC_Initial_Load_Source;
2530 devpriv->stc_writew(dev, devpriv->ao_mode2,AO_Mode_2_Register);
2531 switch(cmd->stop_src){
2533 devpriv->stc_writel(dev, cmd->stop_arg,AO_UC_Load_A_Register);
2534 devpriv->stc_writew(dev, AO_UC_Load,AO_Command_1_Register);
2535 devpriv->stc_writel(dev, cmd->stop_arg - 1,AO_UC_Load_A_Register);
2538 devpriv->stc_writel(dev, 0xffffff,AO_UC_Load_A_Register);
2539 devpriv->stc_writew(dev, AO_UC_Load,AO_Command_1_Register);
2540 devpriv->stc_writel(dev, 0xffffff,AO_UC_Load_A_Register);
2543 devpriv->stc_writel(dev, 0,AO_UC_Load_A_Register);
2544 devpriv->stc_writew(dev, AO_UC_Load,AO_Command_1_Register);
2545 devpriv->stc_writel(dev, cmd->stop_arg,AO_UC_Load_A_Register);
2548 devpriv->ao_cmd2&=~AO_BC_Gate_Enable;
2549 devpriv->stc_writew(dev, devpriv->ao_cmd2,AO_Command_2_Register);
2550 devpriv->ao_mode1&=~(AO_UI_Source_Select(0x1f)|AO_UI_Source_Polarity);
2551 devpriv->stc_writew(dev, devpriv->ao_mode1,AO_Mode_1_Register);
2552 devpriv->ao_mode2&=~(AO_UI_Reload_Mode(3)|AO_UI_Initial_Load_Source);
2553 devpriv->stc_writew(dev, devpriv->ao_mode2,AO_Mode_2_Register);
2554 devpriv->stc_writel(dev, 1,AO_UI_Load_A_Register);
2555 devpriv->stc_writew(dev, AO_UI_Load,AO_Command_1_Register);
2556 devpriv->stc_writel(dev, trigvar,AO_UI_Load_A_Register);
2558 if((boardtype.reg_type & ni_reg_6xxx_mask) == 0){
2559 if(cmd->scan_end_arg>1){
2560 devpriv->ao_mode1|=AO_Multiple_Channels;
2561 devpriv->stc_writew(dev, AO_Number_Of_Channels(cmd->scan_end_arg-1)|
2562 AO_UPDATE_Output_Select(1),
2563 AO_Output_Control_Register);
2565 devpriv->ao_mode1&=~AO_Multiple_Channels;
2566 devpriv->stc_writew(dev, AO_Number_Of_Channels(CR_CHAN(cmd->chanlist[0]))|
2567 AO_UPDATE_Output_Select(1),
2568 AO_Output_Control_Register);
2570 devpriv->stc_writew(dev, devpriv->ao_mode1,AO_Mode_1_Register);
2573 devpriv->stc_writew(dev, AO_DAC0_Update_Mode|AO_DAC1_Update_Mode,AO_Command_1_Register);
2575 devpriv->ao_mode3|=AO_Stop_On_Overrun_Error;
2576 devpriv->stc_writew(dev, devpriv->ao_mode3,AO_Mode_3_Register);
2578 devpriv->ao_mode2 &= AO_FIFO_Mode_Mask;
2580 devpriv->ao_mode2 |= AO_FIFO_Mode_HF_to_F;
2582 devpriv->ao_mode2 |= AO_FIFO_Mode_HF;
2584 devpriv->ao_mode2 &= ~AO_FIFO_Retransmit_Enable;
2585 devpriv->stc_writew(dev, devpriv->ao_mode2,AO_Mode_2_Register);
2587 bits = AO_BC_Source_Select | AO_UPDATE_Pulse_Width |
2588 AO_TMRDACWR_Pulse_Width;
2589 if( boardtype.ao_fifo_depth )
2590 bits |= AO_FIFO_Enable;
2592 bits |= AO_DMA_PIO_Control;
2593 devpriv->stc_writew(dev, bits, AO_Personal_Register);
2594 // enable sending of ao dma requests
2595 devpriv->stc_writew(dev, AO_AOFREQ_Enable, AO_Start_Select_Register);
2597 devpriv->stc_writew(dev, AO_Configuration_End,Joint_Reset_Register);
2599 if(cmd->stop_src==TRIG_COUNT) {
2600 devpriv->stc_writew(dev, AO_BC_TC_Interrupt_Ack,Interrupt_B_Ack_Register);
2601 ni_set_bits(dev, Interrupt_B_Enable_Register,
2602 AO_BC_TC_Interrupt_Enable, 1);
2605 s->async->inttrig=ni_ao_inttrig;
2610 static int ni_ao_cmdtest(comedi_device *dev,comedi_subdevice *s,comedi_cmd *cmd)
2615 /* step 1: make sure trigger sources are trivially valid */
2618 cmd->start_src &= TRIG_INT;
2619 if(!cmd->start_src || tmp!=cmd->start_src)err++;
2621 tmp=cmd->scan_begin_src;
2622 cmd->scan_begin_src &= TRIG_TIMER;
2623 if(!cmd->scan_begin_src || tmp!=cmd->scan_begin_src)err++;
2625 tmp=cmd->convert_src;
2626 cmd->convert_src &= TRIG_NOW;
2627 if(!cmd->convert_src || tmp!=cmd->convert_src)err++;
2629 tmp=cmd->scan_end_src;
2630 cmd->scan_end_src &= TRIG_COUNT;
2631 if(!cmd->scan_end_src || tmp!=cmd->scan_end_src)err++;
2634 cmd->stop_src &= TRIG_COUNT|TRIG_NONE;
2635 if(!cmd->stop_src || tmp!=cmd->stop_src)err++;
2639 /* step 2: make sure trigger sources are unique and mutually compatible */
2641 if(cmd->stop_src!=TRIG_COUNT &&
2642 cmd->stop_src!=TRIG_NONE)err++;
2646 /* step 3: make sure arguments are trivially compatible */
2648 if(cmd->start_arg!=0){
2653 /* XXX need ao_speed */
2654 if(cmd->scan_begin_arg<boardtype.ao_speed){
2655 cmd->scan_begin_arg=boardtype.ao_speed;
2659 if(cmd->scan_begin_arg>TIMER_BASE*0xffffff){ /* XXX check */
2660 cmd->scan_begin_arg=TIMER_BASE*0xffffff;
2663 if(cmd->convert_arg!=0){
2667 if(cmd->scan_end_arg!=cmd->chanlist_len){
2668 cmd->scan_end_arg=cmd->chanlist_len;
2671 if(cmd->stop_src==TRIG_COUNT){ /* XXX check */
2672 if(cmd->stop_arg>0x00ffffff){
2673 cmd->stop_arg=0x00ffffff;
2678 if(cmd->stop_arg!=0){
2686 /* step 4: fix up any arguments */
2688 tmp = cmd->scan_begin_arg;
2689 ni_ns_to_timer(&cmd->scan_begin_arg,cmd->flags&TRIG_ROUND_MASK);
2690 if(tmp!=cmd->scan_begin_arg)err++;
2694 /* step 5: fix up chanlist */
2702 static int ni_ao_reset(comedi_device *dev,comedi_subdevice *s)
2704 //devpriv->ao0p=0x0000;
2705 //ni_writew(devpriv->ao0p,AO_Configuration);
2707 //devpriv->ao1p=AO_Channel(1);
2708 //ni_writew(devpriv->ao1p,AO_Configuration);
2711 mite_dma_disarm(devpriv->mite, AO_DMA_CHAN);
2712 writel(CHOR_DMARESET | CHOR_FRESET, devpriv->mite->mite_io_addr + MITE_CHOR(AO_DMA_CHAN));
2715 devpriv->stc_writew(dev, AO_Configuration_Start,Joint_Reset_Register);
2716 devpriv->stc_writew(dev, AO_Disarm,AO_Command_1_Register);
2717 ni_set_bits(dev,Interrupt_B_Enable_Register,~0,0);
2718 devpriv->stc_writew(dev, AO_BC_Source_Select, AO_Personal_Register);
2719 devpriv->stc_writew(dev, 0x3f98,Interrupt_B_Ack_Register);
2720 devpriv->stc_writew(dev, AO_BC_Source_Select | AO_UPDATE_Pulse_Width |
2721 AO_TMRDACWR_Pulse_Width, AO_Personal_Register);
2722 devpriv->stc_writew(dev, 0,AO_Output_Control_Register);
2723 devpriv->stc_writew(dev, 0,AO_Start_Select_Register);
2725 devpriv->stc_writew(dev, devpriv->ao_cmd1,AO_Command_1_Register);
2727 devpriv->ao_mode1=0;
2728 devpriv->ao_mode2=0;
2729 if(boardtype.reg_type == ni_reg_m_series)
2730 devpriv->ao_mode3 = AO_Last_Gate_Disable;
2732 devpriv->ao_mode3 = 0;
2733 devpriv->stc_writew(dev, devpriv->ao_mode3, AO_Mode_3_Register);
2734 devpriv->ao_trigger_select=0;
2735 if(boardtype.reg_type & ni_reg_6xxx_mask){
2736 ao_win_out(0x3, AO_Immediate_671x);
2737 ao_win_out(CLEAR_WG, AO_Misc_611x);
2739 devpriv->stc_writew(dev, AO_Configuration_End, Joint_Reset_Register);
2744 static int ni_dio_insn_config(comedi_device *dev,comedi_subdevice *s,
2745 comedi_insn *insn,lsampl_t *data)
2748 rt_printk("ni_dio_insn_config() chan=%d io=%d\n",
2749 CR_CHAN(insn->chanspec),data[0]);
2752 case INSN_CONFIG_DIO_OUTPUT:
2753 s->io_bits |= 1<<CR_CHAN(insn->chanspec);
2755 case INSN_CONFIG_DIO_INPUT:
2756 s->io_bits &= ~(1<<CR_CHAN(insn->chanspec));
2758 case INSN_CONFIG_DIO_QUERY:
2759 data[1] = (s->io_bits & (1<<CR_CHAN(insn->chanspec))) ? COMEDI_OUTPUT : COMEDI_INPUT;
2766 devpriv->dio_control &= ~DIO_Pins_Dir_Mask;
2767 devpriv->dio_control |= DIO_Pins_Dir(s->io_bits);
2768 devpriv->stc_writew(dev, devpriv->dio_control,DIO_Control_Register);
2773 static int ni_dio_insn_bits(comedi_device *dev,comedi_subdevice *s,
2774 comedi_insn *insn,lsampl_t *data)
2777 rt_printk("ni_dio_insn_bits() mask=0x%x bits=0x%x\n",data[0],data[1]);
2779 if(insn->n!=2)return -EINVAL;
2781 /* Perform check to make sure we're not using the
2782 serial part of the dio */
2783 if((data[0] & (DIO_SDIN | DIO_SDOUT)) && devpriv->serial_interval_ns)
2786 s->state &= ~data[0];
2787 s->state |= (data[0]&data[1]);
2788 devpriv->dio_output &= ~DIO_Parallel_Data_Mask;
2789 devpriv->dio_output |= DIO_Parallel_Data_Out(s->state);
2790 devpriv->stc_writew(dev, devpriv->dio_output,DIO_Output_Register);
2792 data[1] = devpriv->stc_readw(dev, DIO_Parallel_Input_Register);
2797 static int ni_m_series_dio_insn_config(comedi_device *dev,comedi_subdevice *s,
2798 comedi_insn *insn, lsampl_t *data)
2801 rt_printk("ni_m_series_dio_insn_config() chan=%d io=%d\n",
2802 CR_CHAN(insn->chanspec), data[0]);
2806 case INSN_CONFIG_DIO_OUTPUT:
2807 s->io_bits |= 1 << CR_CHAN(insn->chanspec);
2809 case INSN_CONFIG_DIO_INPUT:
2810 s->io_bits &= ~(1 << CR_CHAN(insn->chanspec));
2812 case INSN_CONFIG_DIO_QUERY:
2813 data[1] = (s->io_bits & (1<<CR_CHAN(insn->chanspec))) ? COMEDI_OUTPUT : COMEDI_INPUT;
2820 ni_writel(s->io_bits, M_Offset_DIO_Direction);
2825 static int ni_m_series_dio_insn_bits(comedi_device *dev,comedi_subdevice *s,
2826 comedi_insn *insn, lsampl_t *data)
2829 rt_printk("ni_m_series_dio_insn_bits() mask=0x%x bits=0x%x\n",data[0],data[1]);
2831 if(insn->n!=2)return -EINVAL;
2833 s->state &= ~data[0];
2834 s->state |= (data[0] & data[1]);
2835 ni_writel(s->state, M_Offset_Static_Digital_Output);
2837 data[1] = ni_readl(M_Offset_Static_Digital_Input);
2842 static int ni_serial_insn_config(comedi_device *dev,comedi_subdevice *s,
2843 comedi_insn *insn,lsampl_t *data)
2846 unsigned char byte_out, byte_in;
2848 if(insn->n!=2)return -EINVAL;
2851 case INSN_CONFIG_SERIAL_CLOCK:
2854 rt_printk("SPI serial clock Config cd\n", data[1]);
2856 devpriv->serial_hw_mode = 1;
2857 devpriv->dio_control |= DIO_HW_Serial_Enable;
2859 if(data[1] == SERIAL_DISABLED) {
2860 devpriv->serial_hw_mode = 0;
2861 devpriv->dio_control &= ~(DIO_HW_Serial_Enable |
2862 DIO_Software_Serial_Control);
2863 data[1] = SERIAL_DISABLED;
2864 devpriv->serial_interval_ns = data[1];
2866 else if(data[1] <= SERIAL_600NS) {
2867 /* Warning: this clock speed is too fast to reliably
2869 devpriv->dio_control &= ~DIO_HW_Serial_Timebase;
2870 devpriv->clock_and_fout |= Slow_Internal_Timebase;
2871 devpriv->clock_and_fout &= ~DIO_Serial_Out_Divide_By_2;
2872 data[1] = SERIAL_600NS;
2873 devpriv->serial_interval_ns = data[1];
2875 else if(data[1] <= SERIAL_1_2US) {
2876 devpriv->dio_control &= ~DIO_HW_Serial_Timebase;
2877 devpriv->clock_and_fout |= Slow_Internal_Timebase |
2878 DIO_Serial_Out_Divide_By_2;
2879 data[1] = SERIAL_1_2US;
2880 devpriv->serial_interval_ns = data[1];
2882 else if(data[1] <= SERIAL_10US) {
2883 devpriv->dio_control |= DIO_HW_Serial_Timebase;
2884 devpriv->clock_and_fout |= Slow_Internal_Timebase |
2885 DIO_Serial_Out_Divide_By_2;
2886 /* Note: DIO_Serial_Out_Divide_By_2 only affects
2887 600ns/1.2us. If you turn divide_by_2 off with the
2888 slow clock, you will still get 10us, except then
2889 all your delays are wrong. */
2890 data[1] = SERIAL_10US;
2891 devpriv->serial_interval_ns = data[1];
2894 devpriv->dio_control &= ~(DIO_HW_Serial_Enable |
2895 DIO_Software_Serial_Control);
2896 devpriv->serial_hw_mode = 0;
2897 data[1] = (data[1] / 1000) * 1000;
2898 devpriv->serial_interval_ns = data[1];
2901 devpriv->stc_writew(dev, devpriv->dio_control,DIO_Control_Register);
2902 devpriv->stc_writew(dev, devpriv->clock_and_fout,Clock_and_FOUT_Register);
2907 case INSN_CONFIG_BIDIRECTIONAL_DATA:
2909 if(devpriv->serial_interval_ns == 0) {
2913 byte_out = data[1] & 0xFF;
2915 if(devpriv->serial_hw_mode) {
2916 err = ni_serial_hw_readwrite8(dev,s,byte_out,&byte_in);
2917 } else if(devpriv->serial_interval_ns > 0) {
2918 err = ni_serial_sw_readwrite8(dev,s,byte_out,&byte_in);
2920 rt_printk("ni_serial_insn_config: serial disabled!\n");
2923 if(err < 0) return err;
2924 data[1] = byte_in & 0xFF;
2934 static int ni_serial_hw_readwrite8(comedi_device *dev,comedi_subdevice *s,
2935 unsigned char data_out,
2936 unsigned char *data_in)
2938 unsigned int status1;
2939 int err = 0, count = 20;
2942 rt_printk("ni_serial_hw_readwrite8: outputting 0x%x\n", data_out);
2945 devpriv->dio_output &= ~DIO_Serial_Data_Mask;
2946 devpriv->dio_output |= DIO_Serial_Data_Out(data_out);
2947 devpriv->stc_writew(dev, devpriv->dio_output,DIO_Output_Register);
2949 status1 = devpriv->stc_readw(dev, Joint_Status_1_Register);
2950 if(status1 & DIO_Serial_IO_In_Progress_St) {
2955 devpriv->dio_control |= DIO_HW_Serial_Start;
2956 devpriv->stc_writew(dev, devpriv->dio_control,DIO_Control_Register);
2957 devpriv->dio_control &= ~DIO_HW_Serial_Start;
2959 /* Wait until STC says we're done, but don't loop infinitely. */
2960 while((status1 = devpriv->stc_readw(dev, Joint_Status_1_Register)) & DIO_Serial_IO_In_Progress_St) {
2961 /* Delay one bit per loop */
2962 comedi_udelay((devpriv->serial_interval_ns + 999) / 1000);
2964 rt_printk("ni_serial_hw_readwrite8: SPI serial I/O didn't finish in time!\n");
2970 /* Delay for last bit. This delay is absolutely necessary, because
2971 DIO_Serial_IO_In_Progress_St goes high one bit too early. */
2972 comedi_udelay((devpriv->serial_interval_ns + 999) / 1000);
2974 if(data_in != NULL) {
2975 *data_in = devpriv->stc_readw(dev, DIO_Serial_Input_Register);
2977 rt_printk("ni_serial_hw_readwrite8: inputted 0x%x\n", *data_in);
2982 devpriv->stc_writew(dev, devpriv->dio_control,DIO_Control_Register);
2987 static int ni_serial_sw_readwrite8(comedi_device *dev,comedi_subdevice *s,
2988 unsigned char data_out,
2989 unsigned char *data_in)
2991 unsigned char mask, input = 0;
2994 rt_printk("ni_serial_sw_readwrite8: outputting 0x%x\n", data_out);
2997 /* Wait for one bit before transfer */
2998 comedi_udelay((devpriv->serial_interval_ns + 999) / 1000);
3000 for(mask = 0x80; mask; mask >>= 1) {
3001 /* Output current bit; note that we cannot touch s->state
3002 because it is a per-subdevice field, and serial is
3003 a separate subdevice from DIO. */
3004 devpriv->dio_output &= ~DIO_SDOUT;
3005 if(data_out & mask) {
3006 devpriv->dio_output |= DIO_SDOUT;
3008 devpriv->stc_writew(dev, devpriv->dio_output,DIO_Output_Register);
3010 /* Assert SDCLK (active low, inverted), wait for half of
3011 the delay, deassert SDCLK, and wait for the other half. */
3012 devpriv->dio_control |= DIO_Software_Serial_Control;
3013 devpriv->stc_writew(dev, devpriv->dio_control,DIO_Control_Register);
3015 comedi_udelay((devpriv->serial_interval_ns + 999) / 2000);
3017 devpriv->dio_control &= ~DIO_Software_Serial_Control;
3018 devpriv->stc_writew(dev, devpriv->dio_control,DIO_Control_Register);
3020 comedi_udelay((devpriv->serial_interval_ns + 999) / 2000);
3022 /* Input current bit */
3023 if(devpriv->stc_readw(dev, DIO_Parallel_Input_Register) & DIO_SDIN) {
3024 /* rt_printk("DIO_P_I_R: 0x%x\n", devpriv->stc_readw(dev, DIO_Parallel_Input_Register)); */
3029 rt_printk("ni_serial_sw_readwrite8: inputted 0x%x\n", input);
3031 if(data_in) *data_in = input;
3036 static void mio_common_detach(comedi_device *dev)
3038 if(dev->subdevices && boardtype.has_8255)
3039 subdev_8255_cleanup(dev,dev->subdevices+3);
3042 static void init_ao_67xx(comedi_device *dev, comedi_subdevice *s)
3046 for(i = 0; i < s->n_chan; i++)
3047 ni_ao_win_outw(dev, AO_Channel(i) | 0x0, AO_Configuration_2_67xx);
3050 static int ni_alloc_private(comedi_device *dev)
3054 ret = alloc_private(dev, sizeof(ni_private));
3055 if(ret < 0) return ret;
3057 spin_lock_init(&devpriv->window_lock);
3062 static int ni_E_init(comedi_device *dev,comedi_devconfig *it)
3064 comedi_subdevice *s;
3067 if(boardtype.n_aochan > MAX_N_AO_CHAN)
3069 printk("bug! boardtype.n_aochan > MAX_N_AO_CHAN\n");
3073 if(alloc_subdevices(dev, 11) < 0)
3076 /* analog input subdevice */
3078 s=dev->subdevices+0;
3080 if(boardtype.n_adchan){
3081 s->type=COMEDI_SUBD_AI;
3082 s->subdev_flags=SDF_READABLE | SDF_DIFF | SDF_DITHER;
3083 if(boardtype.reg_type != ni_reg_611x)
3084 s->subdev_flags |= SDF_GROUND | SDF_COMMON | SDF_OTHER;
3085 if(boardtype.adbits > 16)
3086 s->subdev_flags |= SDF_LSAMPL;
3087 if(boardtype.reg_type == ni_reg_m_series)
3088 s->subdev_flags |= SDF_SOFT_CALIBRATED;
3089 s->n_chan=boardtype.n_adchan;
3090 s->len_chanlist=512;
3091 s->maxdata=(1<<boardtype.adbits)-1;
3092 s->range_table=ni_range_lkup[boardtype.gainlkup];
3093 s->insn_read=ni_ai_insn_read;
3094 s->insn_config=ni_ai_insn_config;
3095 s->do_cmdtest=ni_ai_cmdtest;
3096 s->do_cmd=ni_ai_cmd;
3097 s->cancel=ni_ai_reset;
3099 s->munge=ni_ai_munge;
3101 s->type=COMEDI_SUBD_UNUSED;
3104 /* analog output subdevice */
3106 s=dev->subdevices+1;
3107 if(boardtype.n_aochan){
3108 dev->write_subdev=s;
3109 s->type=COMEDI_SUBD_AO;
3110 s->subdev_flags=SDF_WRITABLE|SDF_DEGLITCH|SDF_GROUND;
3111 if(boardtype.reg_type == ni_reg_m_series)
3112 s->subdev_flags |= SDF_SOFT_CALIBRATED;
3113 s->n_chan=boardtype.n_aochan;
3114 s->maxdata=(1<<boardtype.aobits)-1;
3115 s->range_table = boardtype.ao_range_table;
3116 s->insn_read=ni_ao_insn_read;
3117 if(boardtype.reg_type & ni_reg_6xxx_mask){
3118 s->insn_write=ni_ao_insn_write_671x;
3120 s->insn_write=ni_ao_insn_write;
3123 if(boardtype.n_aochan){
3125 if(boardtype.ao_fifo_depth){
3127 s->do_cmd=ni_ao_cmd;
3128 s->do_cmdtest=ni_ao_cmdtest;
3129 s->len_chanlist = boardtype.n_aochan;
3130 if(boardtype.reg_type != ni_reg_m_series)
3131 s->munge=ni_ao_munge;
3133 s->cancel=ni_ao_reset;
3135 s->type=COMEDI_SUBD_UNUSED;
3137 if((boardtype.reg_type & ni_reg_67xx_mask))
3138 init_ao_67xx(dev, s);
3140 /* digital i/o subdevice */
3142 s=dev->subdevices+2;
3143 s->type=COMEDI_SUBD_DIO;
3144 s->subdev_flags=SDF_WRITABLE|SDF_READABLE;
3146 s->io_bits=0; /* all bits input */
3147 s->range_table=&range_digital;
3148 if(boardtype.reg_type == ni_reg_m_series)
3151 s->insn_bits = ni_m_series_dio_insn_bits;
3152 s->insn_config=ni_m_series_dio_insn_config;
3153 ni_writel(s->io_bits, M_Offset_DIO_Direction);
3157 s->insn_bits=ni_dio_insn_bits;
3158 s->insn_config=ni_dio_insn_config;
3159 devpriv->dio_control = DIO_Pins_Dir(s->io_bits);
3160 ni_writew(devpriv->dio_control, DIO_Control_Register);
3164 s=dev->subdevices+3;
3165 if(boardtype.has_8255){
3166 subdev_8255_init(dev,s,ni_8255_callback,(unsigned long)dev);
3168 s->type=COMEDI_SUBD_UNUSED;
3171 /* general purpose counter/timer device */
3172 s=dev->subdevices+4;
3173 s->type=COMEDI_SUBD_COUNTER;
3174 s->subdev_flags=SDF_READABLE|SDF_WRITABLE;
3175 s->insn_read= ni_gpct_insn_read;
3176 s->insn_write= ni_gpct_insn_write;
3177 s->insn_config=ni_gpct_insn_config;
3180 devpriv->an_trig_etc_reg = 0;
3184 /* calibration subdevice -- ai and ao */
3185 s=dev->subdevices+5;
3186 s->type=COMEDI_SUBD_CALIB;
3187 if(boardtype.reg_type == ni_reg_m_series)
3189 // internal PWM analog output used for AI nonlinearity calibration
3190 s->subdev_flags = SDF_INTERNAL;
3191 s->insn_config = &ni_m_series_pwm_config;
3194 ni_writel(0x0, M_Offset_Cal_PWM);
3195 } else if(boardtype.reg_type == ni_reg_6143)
3197 // internal PWM analog output used for AI nonlinearity calibration
3198 s->subdev_flags = SDF_INTERNAL;
3199 s->insn_config = &ni_6143_pwm_config;
3204 s->subdev_flags = SDF_WRITABLE | SDF_INTERNAL;
3205 s->insn_read = &ni_calib_insn_read;
3206 s->insn_write = &ni_calib_insn_write;
3207 caldac_setup(dev, s);
3211 s=dev->subdevices+6;
3212 s->type=COMEDI_SUBD_MEMORY;
3213 s->subdev_flags=SDF_READABLE|SDF_INTERNAL;
3215 if(boardtype.reg_type == ni_reg_m_series)
3217 s->n_chan = M_SERIES_EEPROM_SIZE;
3218 s->insn_read = &ni_m_series_eeprom_insn_read;
3222 s->insn_read = &ni_eeprom_insn_read;
3225 s=dev->subdevices+7;
3226 s->type=COMEDI_SUBD_DIO;
3227 s->subdev_flags=SDF_READABLE|SDF_WRITABLE|SDF_INTERNAL;
3230 s->insn_bits = ni_pfi_insn_bits;
3231 s->insn_config = ni_pfi_insn_config;
3232 ni_set_bits(dev, IO_Bidirection_Pin_Register, ~0, 0);
3234 /* cs5529 calibration adc */
3235 s = dev->subdevices + 8;
3236 if(boardtype.reg_type & ni_reg_67xx_mask)
3238 s->type = COMEDI_SUBD_AI;
3239 s->subdev_flags = SDF_READABLE | SDF_DIFF | SDF_INTERNAL;
3240 // one channel for each analog output channel
3241 s->n_chan = boardtype.n_aochan;
3242 s->maxdata = (1 << 16) - 1;
3243 s->range_table = &range_unknown; /* XXX */
3244 s->insn_read=cs5529_ai_insn_read;
3245 s->insn_config=NULL;
3249 s->type=COMEDI_SUBD_UNUSED;
3253 s=dev->subdevices+9;
3254 s->type=COMEDI_SUBD_SERIAL;
3255 s->subdev_flags=SDF_READABLE|SDF_WRITABLE|SDF_INTERNAL;
3258 s->insn_config = ni_serial_insn_config;
3259 devpriv->serial_interval_ns = 0;
3260 devpriv->serial_hw_mode = 0;
3263 s=dev->subdevices+10;
3264 s->type=COMEDI_SUBD_DIO;
3265 s->subdev_flags=SDF_READABLE|SDF_WRITABLE|SDF_INTERNAL;
3268 s->insn_bits = ni_rtsi_insn_bits;
3269 s->insn_config = ni_rtsi_insn_config;
3272 /* ai configuration */
3273 ni_ai_reset(dev,dev->subdevices+0);
3274 if((boardtype.reg_type & ni_reg_6xxx_mask) == 0){
3275 // BEAM is this needed for PCI-6143 ??
3276 devpriv->clock_and_fout =
3277 Slow_Internal_Time_Divide_By_2 |
3278 Slow_Internal_Timebase |
3279 Clock_To_Board_Divide_By_2 |
3281 AI_Output_Divide_By_2 |
3282 AO_Output_Divide_By_2;
3284 devpriv->clock_and_fout =
3285 Slow_Internal_Time_Divide_By_2 |
3286 Slow_Internal_Timebase |
3287 Clock_To_Board_Divide_By_2 |
3290 devpriv->stc_writew(dev, devpriv->clock_and_fout, Clock_and_FOUT_Register);
3292 /* analog output configuration */
3293 ni_ao_reset(dev,dev->subdevices + 1);
3296 devpriv->stc_writew(dev, (IRQ_POLARITY?Interrupt_Output_Polarity:0) |
3297 (Interrupt_Output_On_3_Pins&0) |
3298 Interrupt_A_Enable |
3299 Interrupt_B_Enable |
3300 Interrupt_A_Output_Select(interrupt_pin(dev->irq)) |
3301 Interrupt_B_Output_Select(interrupt_pin(dev->irq)),
3302 Interrupt_Control_Register
3307 /* tell the STC which dma channels to use for AI and AO */
3308 bits = 1 << ( AI_DMA_CHAN );
3309 bits |= 1 << ( AO_DMA_CHAN + 4 );
3310 ni_writeb( bits, AI_AO_Select);
3311 /* tell the STC which dma channels to use for
3312 * General purpose counters 0 and 1 */
3313 bits = 1 << ( GPC0_DMA_CHAN );
3314 bits |= 1 << ( GPC1_DMA_CHAN + 4 );
3315 ni_writeb( bits, G0_G1_Select);
3317 if(boardtype.reg_type & ni_reg_6xxx_mask)
3319 ni_writeb( 0, Magic_611x );
3320 }else if(boardtype.reg_type == ni_reg_m_series)
3323 for(channel = 0; channel < boardtype.n_aochan; ++channel)
3325 ni_writeb(0xf, M_Offset_AO_Waveform_Order(channel));
3326 ni_writeb(0x0, M_Offset_AO_Reference_Attenuation(channel));
3328 ni_writeb(0x0, M_Offset_AO_Calibration);
3338 static int ni_8255_callback(int dir,int port,int data,unsigned long arg)
3340 comedi_device *dev=(comedi_device *)arg;
3343 ni_writeb(data,Port_A+2*port);
3346 return ni_readb(Port_A+2*port);
3351 presents the EEPROM as a subdevice
3354 static int ni_eeprom_insn_read(comedi_device *dev,comedi_subdevice *s,
3355 comedi_insn *insn,lsampl_t *data)
3357 data[0]=ni_read_eeprom(dev,CR_CHAN(insn->chanspec));
3363 reads bytes out of eeprom
3366 static int ni_read_eeprom(comedi_device *dev,int addr)
3371 bitstring=0x0300|((addr&0x100)<<3)|(addr&0xff);
3372 ni_writeb(0x04,Serial_Command);
3373 for(bit=0x8000;bit;bit>>=1){
3374 ni_writeb(0x04|((bit&bitstring)?0x02:0),Serial_Command);
3375 ni_writeb(0x05|((bit&bitstring)?0x02:0),Serial_Command);
3378 for(bit=0x80;bit;bit>>=1){
3379 ni_writeb(0x04,Serial_Command);
3380 ni_writeb(0x05,Serial_Command);
3381 bitstring|=((ni_readb(XXX_Status)&PROMOUT)?bit:0);
3383 ni_writeb(0x00,Serial_Command);
3388 static int ni_m_series_eeprom_insn_read(comedi_device *dev,comedi_subdevice *s,
3389 comedi_insn *insn,lsampl_t *data)
3391 data[0] = devpriv->eeprom_buffer[CR_CHAN(insn->chanspec)];
3396 static int ni_get_pwm_config(comedi_device *dev, lsampl_t *data)
3398 data[1] = devpriv->pwm_up_count * TIMER_BASE;
3399 data[2] = devpriv->pwm_down_count * TIMER_BASE;
3403 static int ni_m_series_pwm_config(comedi_device *dev, comedi_subdevice *s,
3404 comedi_insn *insn, lsampl_t *data)
3406 unsigned up_count, down_count;
3409 case INSN_CONFIG_PWM_OUTPUT:
3412 case TRIG_ROUND_NEAREST:
3413 up_count = (data[2] + TIMER_BASE / 2) / TIMER_BASE;
3415 case TRIG_ROUND_DOWN:
3416 up_count = data[2] / TIMER_BASE;
3419 up_count = (data[2] + TIMER_BASE - 1) / TIMER_BASE;
3427 case TRIG_ROUND_NEAREST:
3428 down_count = (data[4] + TIMER_BASE / 2) / TIMER_BASE;
3430 case TRIG_ROUND_DOWN:
3431 down_count = data[4] / TIMER_BASE;
3434 down_count = (data[4] + TIMER_BASE - 1) / TIMER_BASE;
3440 if(up_count * TIMER_BASE != data[2] ||
3441 down_count * TIMER_BASE != data[4])
3443 data[2] = up_count * TIMER_BASE;
3444 data[4] = down_count * TIMER_BASE;
3447 ni_writel(MSeries_Cal_PWM_High_Time_Bits(up_count) | MSeries_Cal_PWM_Low_Time_Bits(down_count), M_Offset_Cal_PWM);
3448 devpriv->pwm_up_count = up_count;
3449 devpriv->pwm_down_count = down_count;
3452 case INSN_CONFIG_GET_PWM_OUTPUT:
3453 return ni_get_pwm_config(dev, data);
3462 static int ni_6143_pwm_config(comedi_device *dev, comedi_subdevice *s,
3463 comedi_insn *insn, lsampl_t *data)
3465 unsigned up_count, down_count;
3468 case INSN_CONFIG_PWM_OUTPUT:
3471 case TRIG_ROUND_NEAREST:
3472 up_count = (data[2] + TIMER_BASE / 2) / TIMER_BASE;
3474 case TRIG_ROUND_DOWN:
3475 up_count = data[2] / TIMER_BASE;
3478 up_count = (data[2] + TIMER_BASE - 1) / TIMER_BASE;
3486 case TRIG_ROUND_NEAREST:
3487 down_count = (data[4] + TIMER_BASE / 2) / TIMER_BASE;
3489 case TRIG_ROUND_DOWN:
3490 down_count = data[4] / TIMER_BASE;
3493 down_count = (data[4] + TIMER_BASE - 1) / TIMER_BASE;
3499 if(up_count * TIMER_BASE != data[2] ||
3500 down_count * TIMER_BASE != data[4])
3502 data[2] = up_count * TIMER_BASE;
3503 data[4] = down_count * TIMER_BASE;
3506 ni_writel(up_count, Calibration_HighTime_6143);
3507 devpriv->pwm_up_count = up_count;
3508 ni_writel(down_count, Calibration_LowTime_6143);
3509 devpriv->pwm_down_count = down_count;
3512 case INSN_CONFIG_GET_PWM_OUTPUT:
3513 return ni_get_pwm_config(dev, data);
3521 static void ni_write_caldac(comedi_device *dev,int addr,int val);
3523 calibration subdevice
3525 static int ni_calib_insn_write(comedi_device *dev,comedi_subdevice *s,
3526 comedi_insn *insn,lsampl_t *data)
3528 ni_write_caldac(dev,CR_CHAN(insn->chanspec),data[0]);
3533 static int ni_calib_insn_read(comedi_device *dev,comedi_subdevice *s,
3534 comedi_insn *insn,lsampl_t *data)
3536 data[0] = devpriv->caldacs[CR_CHAN(insn->chanspec)];
3541 static int pack_mb88341(int addr,int val,int *bitstring);
3542 static int pack_dac8800(int addr,int val,int *bitstring);
3543 static int pack_dac8043(int addr,int val,int *bitstring);
3544 static int pack_ad8522(int addr,int val,int *bitstring);
3545 static int pack_ad8804(int addr,int val,int *bitstring);
3546 static int pack_ad8842(int addr,int val,int *bitstring);
3548 struct caldac_struct{
3551 int (*packbits)(int,int,int *);
3554 static struct caldac_struct caldacs[] = {
3555 [mb88341] = { 12, 8, pack_mb88341 },
3556 [dac8800] = { 8, 8, pack_dac8800 },
3557 [dac8043] = { 1, 12, pack_dac8043 },
3558 [ad8522] = { 2, 12, pack_ad8522 },
3559 [ad8804] = { 12, 8, pack_ad8804 },
3560 [ad8842] = { 8, 8, pack_ad8842 },
3561 [ad8804_debug] = { 16, 8, pack_ad8804 },
3564 static void caldac_setup(comedi_device *dev,comedi_subdevice *s)
3574 type = boardtype.caldac[0];
3575 if(type==caldac_none)return;
3576 n_bits=caldacs[type].n_bits;
3578 type = boardtype.caldac[i];
3579 if(type==caldac_none)break;
3580 if(caldacs[type].n_bits!=n_bits)diffbits=1;
3581 n_chans+=caldacs[type].n_chans;
3588 if(n_chans>MAX_N_CALDACS){
3589 printk("BUG! MAX_N_CALDACS too small\n");
3591 s->maxdata_list=devpriv->caldac_maxdata_list;
3593 for(i=0;i<n_dacs;i++){
3594 type = boardtype.caldac[i];
3595 for(j=0;j<caldacs[type].n_chans;j++){
3596 s->maxdata_list[chan]=
3597 (1<<caldacs[type].n_bits)-1;
3602 for( chan = 0; chan < s->n_chan; chan++ )
3603 ni_write_caldac( dev, i, s->maxdata_list[ i ] / 2 );
3605 type = boardtype.caldac[0];
3606 s->maxdata=(1<<caldacs[type].n_bits)-1;
3608 for( chan = 0; chan < s->n_chan; chan++ )
3609 ni_write_caldac( dev, i, s->maxdata / 2 );
3613 static void ni_write_caldac(comedi_device *dev,int addr,int val)
3615 unsigned int loadbit=0,bits=0,bit,bitstring=0;
3619 //printk("ni_write_caldac: chan=%d val=%d\n",addr,val);
3620 if( devpriv->caldacs[ addr ] == val ) return;
3621 devpriv->caldacs[ addr ] = val;
3624 type = boardtype.caldac[i];
3625 if(type==caldac_none)break;
3626 if(addr<caldacs[type].n_chans){
3627 bits=caldacs[type].packbits(addr,val,&bitstring);
3628 loadbit=SerDacLd(i);
3629 //printk("caldac: using i=%d addr=%d %x\n",i,addr,bitstring);
3632 addr-=caldacs[type].n_chans;
3635 for(bit=1<<(bits-1);bit;bit>>=1){
3636 ni_writeb(((bit&bitstring)?0x02:0),Serial_Command);
3638 ni_writeb(1|((bit&bitstring)?0x02:0),Serial_Command);
3641 ni_writeb(loadbit,Serial_Command);
3643 ni_writeb(0,Serial_Command);
3648 static int pack_mb88341(int addr,int val,int *bitstring)
3652 Note that address bits are reversed. Thanks to
3653 Ingo Keen for noticing this.
3655 Note also that the 88341 expects address values from
3656 1-12, whereas we use channel numbers 0-11. The NI
3657 docs use 1-12, also, so be careful here.
3660 *bitstring=((addr&0x1)<<11) |
3668 static int pack_dac8800(int addr,int val,int *bitstring)
3670 *bitstring=((addr&0x7)<<8)|(val&0xff);
3674 static int pack_dac8043(int addr,int val,int *bitstring)
3676 *bitstring=val&0xfff;
3680 static int pack_ad8522(int addr,int val,int *bitstring)
3682 *bitstring=(val&0xfff)|(addr ? 0xc000:0xa000);
3686 static int pack_ad8804(int addr,int val,int *bitstring)
3688 *bitstring=((addr&0xf)<<8) | (val&0xff);
3692 static int pack_ad8842(int addr,int val,int *bitstring)
3694 *bitstring=((addr+1)<<8) | (val&0xff);
3704 * General Purpose Counter/Timer section
3709 * Low level stuff...Each STC counter has two 24 bit load registers
3710 * (A&B). Just make it easier to access them.
3712 * These are inlined _only_ because they are used once in subsequent
3713 * code. Otherwise they should not be inlined.
3715 static inline void GPCT_Load_A(comedi_device *dev, int chan, unsigned int value)
3717 devpriv->stc_writel(dev, value & 0x00ffffff, G_Load_A_Register(chan));
3720 static inline void GPCT_Load_B(comedi_device *dev, int chan, unsigned int value)
3722 devpriv->stc_writel(dev, value & 0x00ffffff, G_Load_B_Register(chan));
3725 /* Load a value into the counter, using register A as the intermediate step.
3726 * You might use GPCT_Load_Using_A to load a 0x000000 into a counter
3729 static void GPCT_Load_Using_A(comedi_device *dev, int chan, unsigned int value)
3731 devpriv->gpct_mode[chan] &= (~G_Load_Source_Select);
3732 devpriv->stc_writew(dev, devpriv->gpct_mode[chan],G_Mode_Register(chan));
3733 GPCT_Load_A(dev,chan,value);
3734 devpriv->stc_writew(dev, devpriv->gpct_command[chan]|G_Load,G_Command_Register(chan));
3738 * Read the GPCTs current value.
3740 static int GPCT_G_Watch(comedi_device *dev, int chan)
3742 unsigned int hi1,hi2,lo;
3744 devpriv->gpct_command[chan] &= ~G_Save_Trace;
3745 devpriv->stc_writew(dev, devpriv->gpct_command[chan],G_Command_Register(chan));
3747 devpriv->gpct_command[chan] |= G_Save_Trace;
3748 devpriv->stc_writew(dev, devpriv->gpct_command[chan], G_Command_Register(chan));
3750 /* This procedure is used because the two registers cannot
3751 * be read atomically. */
3753 hi1 = devpriv->stc_readw(dev, G_Save_Register_High(chan));
3754 lo = devpriv->stc_readw(dev, G_Save_Register_Low(chan));
3755 hi2 = devpriv->stc_readw(dev, G_Save_Register_High(chan));
3758 return (hi1<<16)|lo;
3762 static int GPCT_Disarm(comedi_device *dev, int chan)
3764 devpriv->stc_writew(dev, devpriv->gpct_command[chan] | G_Disarm,G_Command_Register(chan));
3769 static int GPCT_Arm(comedi_device *dev, int chan)
3771 devpriv->stc_writew(dev, devpriv->gpct_command[chan] | G_Arm,G_Command_Register(chan));
3772 /* If the counter is doing pulse width measurement, then make
3773 sure that the counter did not start counting right away. This would
3774 indicate that we started acquiring the pulse after it had already
3775 started and our measurement would be inaccurate */
3776 if(devpriv->gpct_cur_operation[chan] == GPCT_SINGLE_PW){
3779 g_status=devpriv->stc_readw(dev, G_Status_Register);
3782 //TIM 5/2/01 possible error with very short pulses
3783 if((G0_Counting_St & g_status)|| !(G0_Armed_St&g_status)) {
3784 //error: we missed the beginning of the pulse
3785 return -EINVAL; //there is probably a more accurate error code...
3788 if((G1_Counting_St & g_status)|| !(G1_Armed_St&g_status)) {
3789 //error: we missed the beginning of the pulse
3797 static int GPCT_Set_Source(comedi_device *dev,int chan ,int source)
3799 //printk("GPCT_Set_Source...");
3800 devpriv->gpct_input_select[chan] &= ~G_Source_Select(0x1f);//reset gate to 0
3802 case GPCT_INT_CLOCK:
3803 devpriv->gpct_input_select[chan] |= G_Source_Select(0);//INT_TIMEBASE
3807 devpriv->gpct_input_select[chan] |= G_Source_Select(9);//PFI8
3809 devpriv->gpct_input_select[chan] |= G_Source_Select(4);//PFI3
3814 devpriv->stc_writew(dev, devpriv->gpct_input_select[chan], G_Input_Select_Register(chan));
3815 //printk("exit GPCT_Set_Source\n");
3819 static int GPCT_Set_Gate(comedi_device *dev,int chan ,int gate)
3821 //printk("GPCT_Set_Gate...");
3822 devpriv->gpct_input_select[chan] &= ~G_Gate_Select(0x1f);//reset gate to 0
3825 devpriv->gpct_input_select[chan] |= G_Gate_Select(31);//Low
3826 devpriv->gpct_mode[chan] |= G_Gate_Polarity;
3829 devpriv->gpct_mode[chan] &= ~G_Gate_Polarity;
3831 devpriv->gpct_input_select[chan] |= G_Gate_Select(10);//PFI9
3833 devpriv->gpct_input_select[chan] |= G_Gate_Select(5);//PFI4
3839 devpriv->stc_writew(dev, devpriv->gpct_input_select[chan], G_Input_Select_Register(chan));
3840 devpriv->stc_writew(dev, devpriv->gpct_mode[chan], G_Mode_Register(chan));
3841 //printk("exit GPCT_Set_Gate\n");
3845 static int GPCT_Set_Direction(comedi_device *dev,int chan,int direction)
3847 //printk("GPCT_Set_Direction...");
3849 devpriv->gpct_command[chan] &= ~G_Up_Down(0x3);
3850 switch (direction) {
3852 devpriv->gpct_command[chan] |= G_Up_Down(1);
3855 devpriv->gpct_command[chan] |= G_Up_Down(0);
3858 devpriv->gpct_command[chan] |= G_Up_Down(2);
3861 rt_printk("Error direction=0x%08x..",direction);
3864 devpriv->stc_writew(dev, devpriv->gpct_command[chan], G_Command_Register(chan));
3865 //TIM 4/23/01 devpriv->stc_writew(dev, devpriv->gpct_mode[chan], G_Mode_Register(chan));
3866 //printk("exit GPCT_Set_Direction\n");
3870 static void GPCT_Event_Counting(comedi_device *dev,int chan)
3873 //NOTE: possible residual bits from multibit masks can corrupt
3874 //If you config for several measurements between Resets, watch out!
3876 //printk("GPCT_Event_Counting...");
3878 devpriv->gpct_cur_operation[chan] = GPCT_SIMPLE_EVENT;
3881 devpriv->gpct_mode[chan] &= ~(G_Gating_Mode(0x3));
3882 devpriv->gpct_mode[chan] |= G_Gating_Mode(1);
3884 // Trigger_Mode_For_Edge_Gate = 1
3885 devpriv->gpct_mode[chan] &= ~(G_Trigger_Mode_For_Edge_Gate(0x3));
3886 devpriv->gpct_mode[chan] |= G_Trigger_Mode_For_Edge_Gate(2);
3888 devpriv->stc_writew(dev, devpriv->gpct_mode[chan],G_Mode_Register(chan));
3889 //printk("exit GPCT_Event_Counting\n");
3892 static void GPCT_Period_Meas(comedi_device *dev, int chan)
3894 //printk("GPCT_Period_Meas...");
3896 devpriv->gpct_cur_operation[chan] = GPCT_SINGLE_PERIOD;
3899 //NOTE: possible residual bits from multibit masks can corrupt
3900 //If you config for several measurements between Resets, watch out!
3901 devpriv->gpct_mode[chan] &= ~G_OR_Gate;
3902 devpriv->gpct_mode[chan] &= ~G_Gate_Select_Load_Source;
3905 devpriv->gpct_mode[chan] &= ~(G_Output_Mode(0x3));
3906 devpriv->gpct_mode[chan] |= G_Output_Mode(3);
3910 devpriv->gpct_mode[chan] &= ~(G_Gating_Mode(0x3));
3911 devpriv->gpct_mode[chan] |= G_Gating_Mode(2);
3913 // Trigger_Mode_For_Edge_Gate=0
3914 devpriv->gpct_mode[chan] &= ~(G_Trigger_Mode_For_Edge_Gate(0x3));
3915 devpriv->gpct_mode[chan] |= G_Trigger_Mode_For_Edge_Gate(0);
3917 devpriv->gpct_mode[chan] |= G_Reload_Source_Switching;
3918 devpriv->gpct_mode[chan] &= ~G_Loading_On_Gate;
3919 devpriv->gpct_mode[chan] &= ~G_Loading_On_TC;
3920 devpriv->gpct_mode[chan] &= ~G_Gate_On_Both_Edges;
3923 devpriv->gpct_mode[chan] &= ~(G_Stop_Mode(0x3));
3924 devpriv->gpct_mode[chan] |= G_Stop_Mode(0);
3926 // Counting_Once = 2
3927 devpriv->gpct_mode[chan] &= ~(G_Counting_Once(0x3));
3928 devpriv->gpct_mode[chan] |= G_Counting_Once(2);
3931 devpriv->gpct_command[chan] &= ~(G_Up_Down(0x3));
3932 devpriv->gpct_command[chan] |= G_Up_Down(1);
3934 devpriv->stc_writew(dev, devpriv->gpct_mode[chan],G_Mode_Register(chan));
3935 devpriv->stc_writew(dev, devpriv->gpct_command[chan],G_Command_Register(chan));
3936 //printk("exit GPCT_Period_Meas\n");
3939 static void GPCT_Pulse_Width_Meas(comedi_device *dev, int chan)
3941 //printk("GPCT_Pulse_Width_Meas...");
3943 devpriv->gpct_cur_operation[chan] = GPCT_SINGLE_PW;
3945 devpriv->gpct_mode[chan] &= ~G_OR_Gate;
3946 devpriv->gpct_mode[chan] &= ~G_Gate_Select_Load_Source;
3949 devpriv->gpct_mode[chan] &= ~(G_Output_Mode(0x3));
3950 devpriv->gpct_mode[chan] |= G_Output_Mode(3);
3953 devpriv->gpct_mode[chan] &= ~(G_Gating_Mode(0x3));
3954 devpriv->gpct_mode[chan] |= G_Gating_Mode(1);//TIM 4/24/01 was 2
3956 // Trigger_Mode_For_Edge_Gate=2
3957 devpriv->gpct_mode[chan] &= ~(G_Trigger_Mode_For_Edge_Gate(0x3));
3958 devpriv->gpct_mode[chan] |= G_Trigger_Mode_For_Edge_Gate(2);//TIM 4/24/01 was 0
3961 devpriv->gpct_mode[chan] |= G_Reload_Source_Switching;//TIM 4/24/01 was 1
3962 devpriv->gpct_mode[chan] &= ~G_Loading_On_Gate;//TIM 4/24/01 was 0
3964 devpriv->gpct_mode[chan] &= ~G_Loading_On_TC;
3965 devpriv->gpct_mode[chan] &= ~G_Gate_On_Both_Edges;
3968 devpriv->gpct_mode[chan] &= ~(G_Stop_Mode(0x3));
3969 devpriv->gpct_mode[chan] |= G_Stop_Mode(0);
3971 // Counting_Once = 2
3972 devpriv->gpct_mode[chan] &= ~(G_Counting_Once(0x3));
3973 devpriv->gpct_mode[chan] |= G_Counting_Once(2);
3976 devpriv->gpct_command[chan] &= ~(G_Up_Down(0x3));
3977 devpriv->gpct_command[chan] |= G_Up_Down(1);
3979 devpriv->stc_writew(dev, devpriv->gpct_mode[chan],G_Mode_Register(chan));
3980 devpriv->stc_writew(dev, devpriv->gpct_command[chan],G_Command_Register(chan));
3982 //printk("exit GPCT_Pulse_Width_Meas\n");
3985 /* GPCT_Gen_Single_Pulse() creates pulse of length pulsewidth which starts after the Arm
3986 signal is sent. The pulse is delayed by the value already in the counter. This function could
3987 be modified to send a pulse in response to a trigger event at its gate.*/
3988 static void GPCT_Gen_Single_Pulse(comedi_device *dev, int chan, unsigned int length)
3990 //printk("GPCT_Gen_Cont...");
3992 devpriv->gpct_cur_operation[chan] = GPCT_SINGLE_PULSE_OUT;
3994 // Set length of the pulse
3995 GPCT_Load_B(dev,chan, length-1);
3997 //Load next time using B, This is reset by GPCT_Load_Using_A()
3998 devpriv->gpct_mode[chan] |= G_Load_Source_Select;
4000 devpriv->gpct_mode[chan] &= ~G_OR_Gate;
4001 devpriv->gpct_mode[chan] &= ~G_Gate_Select_Load_Source;
4004 devpriv->gpct_mode[chan] &= ~(G_Output_Mode(0x3));
4005 devpriv->gpct_mode[chan] |= G_Output_Mode(2); //TIM 4/26/01 was 3
4007 //Gating Mode=0 for untriggered single pulse
4008 devpriv->gpct_mode[chan] &= ~(G_Gating_Mode(0x3));
4009 devpriv->gpct_mode[chan] |= G_Gating_Mode(0); //TIM 4/25/01 was 1
4011 // Trigger_Mode_For_Edge_Gate=0
4012 devpriv->gpct_mode[chan] &= ~(G_Trigger_Mode_For_Edge_Gate(0x3));
4013 devpriv->gpct_mode[chan] |= G_Trigger_Mode_For_Edge_Gate(2);
4016 devpriv->gpct_mode[chan] |= G_Reload_Source_Switching;
4017 devpriv->gpct_mode[chan] &= ~G_Loading_On_Gate;
4018 devpriv->gpct_mode[chan] |= G_Loading_On_TC; //TIM 4/25/01
4019 devpriv->gpct_mode[chan] &= ~G_Gate_On_Both_Edges;
4022 devpriv->gpct_mode[chan] &= ~(G_Stop_Mode(0x3));
4023 devpriv->gpct_mode[chan] |= G_Stop_Mode(2); //TIM 4/25/01
4025 // Counting_Once = 2
4026 devpriv->gpct_mode[chan] &= ~(G_Counting_Once(0x3));
4027 devpriv->gpct_mode[chan] |= G_Counting_Once(1); //TIM 4/25/01
4030 devpriv->gpct_command[chan] &= ~(G_Up_Down(0x3));
4031 devpriv->gpct_command[chan] |= G_Up_Down(0); //TIM 4/25/01 was 1
4033 devpriv->stc_writew(dev, devpriv->gpct_mode[chan],G_Mode_Register(chan));
4034 devpriv->stc_writew(dev, devpriv->gpct_command[chan],G_Command_Register(chan));
4036 //printk("exit GPCT_Gen_Cont\n");
4039 static void GPCT_Gen_Cont_Pulse(comedi_device *dev, int chan, unsigned int length)
4041 //printk("GPCT_Gen_Cont...");
4043 devpriv->gpct_cur_operation[chan] = GPCT_CONT_PULSE_OUT;
4045 // Set length of the pulse
4046 GPCT_Load_B(dev,chan, length-1);
4048 //Load next time using B, This is reset by GPCT_Load_Using_A()
4049 devpriv->gpct_mode[chan] |= G_Load_Source_Select;
4051 devpriv->gpct_mode[chan] &= ~G_OR_Gate;
4052 devpriv->gpct_mode[chan] &= ~G_Gate_Select_Load_Source;
4055 devpriv->gpct_mode[chan] &= ~(G_Output_Mode(0x3));
4056 devpriv->gpct_mode[chan] |= G_Output_Mode(2); //TIM 4/26/01 was 3
4058 //Gating Mode=0 for untriggered single pulse
4059 devpriv->gpct_mode[chan] &= ~(G_Gating_Mode(0x3));
4060 devpriv->gpct_mode[chan] |= G_Gating_Mode(0); //TIM 4/26/01 was 0
4062 // Trigger_Mode_For_Edge_Gate=0
4063 devpriv->gpct_mode[chan] &= ~(G_Trigger_Mode_For_Edge_Gate(0x3));
4064 devpriv->gpct_mode[chan] |= G_Trigger_Mode_For_Edge_Gate(2);
4067 devpriv->gpct_mode[chan] |= G_Reload_Source_Switching;
4068 devpriv->gpct_mode[chan] &= ~G_Loading_On_Gate;
4069 devpriv->gpct_mode[chan] |= G_Loading_On_TC;
4070 devpriv->gpct_mode[chan] &= ~G_Gate_On_Both_Edges;
4073 devpriv->gpct_mode[chan] &= ~(G_Stop_Mode(0x3));
4074 devpriv->gpct_mode[chan] |= G_Stop_Mode(0); //TIM 4/26/01
4076 // Counting_Once = 2
4077 devpriv->gpct_mode[chan] &= ~(G_Counting_Once(0x3));
4078 devpriv->gpct_mode[chan] |= G_Counting_Once(0); //TIM 4/26/01
4081 devpriv->gpct_command[chan] &= ~(G_Up_Down(0x3));
4082 devpriv->gpct_command[chan] |= G_Up_Down(0);
4085 //This seems pretty unsafe since I don't think it is cleared anywhere.
4086 //I don't think this is working
4087 //devpriv->gpct_command[chan] &= ~G_Bank_Switch_Enable;
4088 //devpriv->gpct_command[chan] &= ~G_Bank_Switch_Mode;
4091 devpriv->stc_writew(dev, devpriv->gpct_mode[chan],G_Mode_Register(chan));
4092 devpriv->stc_writew(dev, devpriv->gpct_command[chan],G_Command_Register(chan));
4094 //printk("exit GPCT_Gen_Cont\n");
4097 static void GPCT_Reset(comedi_device *dev, int chan)
4101 //printk("GPCT_Reset...");
4102 devpriv->gpct_cur_operation[chan] = GPCT_RESET;
4106 devpriv->stc_writew(dev, G0_Reset,Joint_Reset_Register);
4107 ni_set_bits(dev,Interrupt_A_Enable_Register,G0_TC_Interrupt_Enable, 0);
4108 ni_set_bits(dev,Interrupt_A_Enable_Register,G0_Gate_Interrupt_Enable,0);
4109 temp_ack_reg |= G0_Gate_Error_Confirm;
4110 temp_ack_reg |= G0_TC_Error_Confirm;
4111 temp_ack_reg |= G0_TC_Interrupt_Ack;
4112 temp_ack_reg |= G0_Gate_Interrupt_Ack;
4113 devpriv->stc_writew(dev, temp_ack_reg,Interrupt_A_Ack_Register);
4115 //problem...this interferes with the other ctr...
4116 devpriv->an_trig_etc_reg |= GPFO_0_Output_Enable;
4117 devpriv->stc_writew(dev, devpriv->an_trig_etc_reg, Analog_Trigger_Etc_Register);
4120 devpriv->stc_writew(dev, G1_Reset,Joint_Reset_Register);
4121 ni_set_bits(dev,Interrupt_B_Enable_Register,G1_TC_Interrupt_Enable, 0);
4122 ni_set_bits(dev,Interrupt_B_Enable_Register,G0_Gate_Interrupt_Enable,0);
4123 temp_ack_reg |= G1_Gate_Error_Confirm;
4124 temp_ack_reg |= G1_TC_Error_Confirm;
4125 temp_ack_reg |= G1_TC_Interrupt_Ack;
4126 temp_ack_reg |= G1_Gate_Interrupt_Ack;
4127 devpriv->stc_writew(dev, temp_ack_reg,Interrupt_B_Ack_Register);
4129 devpriv->an_trig_etc_reg |= GPFO_1_Output_Enable;
4130 devpriv->stc_writew(dev, devpriv->an_trig_etc_reg, Analog_Trigger_Etc_Register);
4134 devpriv->gpct_mode[chan] = 0;
4135 devpriv->gpct_input_select[chan] = 0;
4136 devpriv->gpct_command[chan] = 0;
4138 devpriv->gpct_command[chan] |= G_Synchronized_Gate;
4140 devpriv->stc_writew(dev, devpriv->gpct_mode[chan],G_Mode_Register(chan));
4141 devpriv->stc_writew(dev, devpriv->gpct_input_select[chan],G_Input_Select_Register(chan));
4142 devpriv->stc_writew(dev, 0,G_Autoincrement_Register(chan));
4144 //printk("exit GPCT_Reset\n");
4147 static int ni_gpct_insn_config(comedi_device *dev,comedi_subdevice *s,
4148 comedi_insn *insn,lsampl_t *data)
4151 //printk("data[0] is 0x%08x, data[1] is 0x%08x\n",data[0],data[1]);
4154 if(insn->n!=1)return -EINVAL;
4155 GPCT_Reset(dev,insn->chanspec);
4157 case GPCT_SET_SOURCE:
4158 if(insn->n!=2)return -EINVAL;
4159 retval=GPCT_Set_Source(dev,insn->chanspec,data[1]);
4162 if(insn->n!=2)return -EINVAL;
4163 retval=GPCT_Set_Gate(dev,insn->chanspec,data[1]);
4165 case GPCT_SET_DIRECTION:
4166 if(insn->n!=2) return -EINVAL;
4167 retval=GPCT_Set_Direction(dev,insn->chanspec,data[1]);
4169 case GPCT_GET_INT_CLK_FRQ:
4170 if(insn->n!=2) return -EINVAL;
4171 //There are actually 2 internal clocks on the STC, we always
4172 //use the fast 20MHz one at this time. Tim Ousley 5/1/01
4173 //NOTE: This is not the final interface, ideally the user
4174 //will never need to know the int. clk. freq.
4175 data[1]=50;//50ns = 20MHz = internal timebase of STC
4177 case GPCT_SET_OPERATION:
4178 //TIM 5/1/01 if((insn->n<2)||(insn->n>3))return -EINVAL;
4180 case GPCT_SIMPLE_EVENT:
4181 GPCT_Event_Counting(dev,insn->chanspec);
4183 case GPCT_SINGLE_PERIOD:
4184 GPCT_Period_Meas(dev,insn->chanspec);
4186 case GPCT_SINGLE_PW:
4187 GPCT_Pulse_Width_Meas(dev,insn->chanspec);
4189 case GPCT_SINGLE_PULSE_OUT:
4190 GPCT_Gen_Single_Pulse(dev,insn->chanspec,data[2]);
4192 case GPCT_CONT_PULSE_OUT:
4193 GPCT_Gen_Cont_Pulse(dev,insn->chanspec,data[2]);
4196 rt_printk("unsupported GPCT operation!\n");
4201 if(insn->n!=1)return -EINVAL;
4202 retval=GPCT_Arm(dev,insn->chanspec);
4205 if(insn->n!=1)return -EINVAL;
4206 retval=GPCT_Disarm(dev,insn->chanspec);
4212 //catch any errors from return values
4216 if(data[0]!=GPCT_ARM){
4217 rt_printk("error: retval was %d\n",retval);
4218 rt_printk("data[0] is 0x%08x, data[1] is 0x%08x\n",data[0],data[1]);
4225 static int ni_gpct_insn_read(comedi_device *dev,comedi_subdevice *s,
4226 comedi_insn *insn,lsampl_t *data) {
4228 int chan=insn->chanspec;
4229 int cur_op = devpriv->gpct_cur_operation[chan];
4231 //printk("in ni_gpct_insn_read, n=%d, data[0]=%d\n",insn->chanspec,data[0]);
4232 if(insn->n!=1)return -EINVAL;
4234 data[0] = GPCT_G_Watch(dev,insn->chanspec);
4236 /* for certain modes (period and pulse width measurment), the value
4237 in the counter is not valid until the counter stops. If the value is
4238 invalid, return a 0 */
4239 if((cur_op == GPCT_SINGLE_PERIOD) || (cur_op == GPCT_SINGLE_PW)){
4240 /* is the counter still running? */
4241 if(devpriv->stc_readw(dev, G_Status_Register) & (chan?G1_Counting_St:G0_Counting_St))
4247 static int ni_gpct_insn_write(comedi_device *dev,comedi_subdevice *s,
4248 comedi_insn *insn,lsampl_t *data) {
4250 //printk("in ni_gpct_insn_write");
4251 if(insn->n!=1)return -EINVAL;
4252 GPCT_Load_Using_A(dev,insn->chanspec,data[0]);
4259 * Programmable Function Inputs
4263 static int ni_pfi_insn_bits(comedi_device *dev,comedi_subdevice *s,
4264 comedi_insn *insn,lsampl_t *data)
4266 if(insn->n!=2)return -EINVAL;
4273 static int ni_pfi_insn_config(comedi_device *dev,comedi_subdevice *s,
4274 comedi_insn *insn,lsampl_t *data)
4278 if(insn->n < 1)return -EINVAL;
4280 chan = CR_CHAN(insn->chanspec);
4281 if(chan>10)return -EINVAL;
4285 ni_set_bits(dev, IO_Bidirection_Pin_Register, 1<<chan, 1);
4288 ni_set_bits(dev, IO_Bidirection_Pin_Register, 1<<chan, 0);
4290 case INSN_CONFIG_DIO_QUERY:
4291 data[1] = (devpriv->io_bidirection_pin_reg & (1<<chan)) ? COMEDI_OUTPUT : COMEDI_INPUT;
4303 * NI RTSI Bus Functions
4306 static void ni_rtsi_init(comedi_device *dev)
4308 // Initialises the RTSI bus signal switch to a default state
4310 // Set clock mode to internal
4311 devpriv->stc_writew(dev, COMEDI_RTSI_CLOCK_MODE_INTERNAL, RTSI_Trig_Direction_Register);
4313 // Standard internal lines are routed to standard RTSI bus lines
4314 devpriv->stc_writew(dev, 0x3210, RTSI_Trig_A_Output_Register);
4315 devpriv->stc_writew(dev, 0x0654, RTSI_Trig_B_Output_Register);
4317 // Sets the source and direction of the 4 on board lines
4318 // devpriv->stc_writew(dev, 0x0000, RTSI_Board_Register);
4321 static int ni_rtsi_insn_bits(comedi_device *dev,comedi_subdevice *s,
4322 comedi_insn *insn,lsampl_t *data)
4324 if(insn->n != 2) return -EINVAL;
4331 static int ni_rtsi_insn_config(comedi_device *dev,comedi_subdevice *s,
4332 comedi_insn *insn,lsampl_t *data)
4337 if(insn->n < 1) return -EINVAL;
4339 if(data[0] == INSN_CONFIG_SET_RTSI_CLOCK_MODE){
4343 devpriv->rtsi_trig_direction_reg &= ~0x03;
4344 devpriv->rtsi_trig_direction_reg |= data[1];
4345 devpriv->stc_writew(dev, devpriv->rtsi_trig_direction_reg, RTSI_Trig_Direction_Register);
4348 chan = CR_CHAN(insn->chanspec);
4349 if(chan > 6) return -EINVAL;
4354 case INSN_CONFIG_DIO_OUTPUT:
4355 devpriv->rtsi_trig_direction_reg |= (1 << bit);
4356 devpriv->stc_writew(dev, devpriv->rtsi_trig_direction_reg, RTSI_Trig_Direction_Register);
4358 case INSN_CONFIG_DIO_INPUT:
4359 devpriv->rtsi_trig_direction_reg &= ~(1 << bit);
4360 devpriv->stc_writew(dev, devpriv->rtsi_trig_direction_reg, RTSI_Trig_Direction_Register);
4362 case INSN_CONFIG_DIO_QUERY:
4363 data[1] = (devpriv->rtsi_trig_direction_reg & (1<<bit)) ? INSN_CONFIG_DIO_OUTPUT : INSN_CONFIG_DIO_INPUT;
4374 static int cs5529_wait_for_idle(comedi_device *dev)
4376 unsigned short status;
4377 const int timeout = HZ;
4380 for(i = 0; i < timeout; i++)
4382 status = ni_ao_win_inw(dev, CAL_ADC_Status_67xx);
4383 if((status & CSS_ADC_BUSY) == 0)
4387 set_current_state(TASK_INTERRUPTIBLE);
4388 if(schedule_timeout(1))
4393 //printk("looped %i times waiting for idle\n", i);
4396 rt_printk("%s: %s: timeout\n", __FILE__, __FUNCTION__);
4402 static void cs5529_command(comedi_device *dev, unsigned short value)
4404 static const int timeout = 100;
4407 ni_ao_win_outw(dev, value, CAL_ADC_Command_67xx);
4408 /* give time for command to start being serially clocked into cs5529.
4409 * this insures that the CSS_ADC_BUSY bit will get properly
4410 * set before we exit this function.
4412 for(i = 0; i < timeout; i++)
4414 if((ni_ao_win_inw(dev, CAL_ADC_Status_67xx) & CSS_ADC_BUSY))
4418 //printk("looped %i times writing command to cs5529\n", i);
4421 comedi_error(dev, "possible problem - never saw adc go busy?");
4425 /* write to cs5529 register */
4426 static void cs5529_config_write(comedi_device *dev, unsigned int value, unsigned int reg_select_bits)
4428 ni_ao_win_outw(dev, ((value >> 16) & 0xff), CAL_ADC_Config_Data_High_Word_67xx);
4429 ni_ao_win_outw(dev, (value & 0xffff), CAL_ADC_Config_Data_Low_Word_67xx);
4430 reg_select_bits &= CSCMD_REGISTER_SELECT_MASK;
4431 cs5529_command(dev, CSCMD_COMMAND | reg_select_bits);
4432 if(cs5529_wait_for_idle(dev))
4433 comedi_error(dev, "time or signal in cs5529_config_write()");
4436 /* read from cs5529 register */
4437 static unsigned int cs5529_config_read(comedi_device *dev, unsigned int reg_select_bits)
4441 reg_select_bits &= CSCMD_REGISTER_SELECT_MASK;
4442 cs5529_command(dev, CSCMD_COMMAND | CSCMD_READ | reg_select_bits);
4443 if(cs5529_wait_for_idle(dev))
4444 comedi_error(dev, "timeout or signal in cs5529_config_read()");
4445 value = (ni_ao_win_inw(dev, CAL_ADC_Config_Data_High_Word_67xx) << 16) & 0xff0000;
4446 value |= ni_ao_win_inw(dev, CAL_ADC_Config_Data_Low_Word_67xx) & 0xffff;
4450 static int cs5529_do_conversion(comedi_device *dev, unsigned short *data)
4453 unsigned short status;
4455 cs5529_command(dev, CSCMD_COMMAND | CSCMD_SINGLE_CONVERSION);
4456 retval = cs5529_wait_for_idle(dev);
4459 comedi_error(dev, "timeout or signal in cs5529_do_conversion()");
4462 status = ni_ao_win_inw(dev, CAL_ADC_Status_67xx);
4463 if(status & CSS_OSC_DETECT)
4465 rt_printk("ni_mio_common: cs5529 conversion error, status CSS_OSC_DETECT\n");
4468 if(status & CSS_OVERRANGE)
4470 rt_printk("ni_mio_common: cs5529 conversion error, overrange (ignoring)\n");
4474 *data = ni_ao_win_inw(dev, CAL_ADC_Data_67xx);
4475 /* cs5529 returns 16 bit signed data in bipolar mode */
4481 static int cs5529_ai_insn_read(comedi_device *dev,comedi_subdevice *s,comedi_insn *insn,lsampl_t *data)
4484 unsigned short sample;
4485 unsigned int channel_select;
4486 const unsigned int INTERNAL_REF = 0x1000;
4488 /* Set calibration adc source. Docs lie, reference select bits 8 to 11
4489 * do nothing. bit 12 seems to chooses internal reference voltage, bit
4490 * 13 causes the adc input to go overrange (maybe reads external reference?) */
4491 if(insn->chanspec & CR_ALT_SOURCE)
4492 channel_select = INTERNAL_REF;
4494 channel_select = CR_CHAN(insn->chanspec);
4495 ni_ao_win_outw(dev, channel_select, AO_Calibration_Channel_Select_67xx);
4497 for(n = 0; n < insn->n; n++)
4499 retval = cs5529_do_conversion(dev, &sample);
4500 if(retval < 0) return retval;
4506 static int init_cs5529(comedi_device *dev)
4508 unsigned int config_bits = CSCFG_PORT_MODE | CSCFG_WORD_RATE_2180_CYCLES;
4511 /* do self-calibration */
4512 cs5529_config_write(dev, config_bits | CSCFG_SELF_CAL_OFFSET_GAIN, CSCMD_CONFIG_REGISTER);
4513 /* need to force a conversion for calibration to run */
4514 cs5529_do_conversion(dev, NULL);
4516 /* force gain calibration to 1 */
4517 cs5529_config_write(dev, 0x400000, CSCMD_GAIN_REGISTER);
4518 cs5529_config_write(dev, config_bits | CSCFG_SELF_CAL_OFFSET, CSCMD_CONFIG_REGISTER);
4519 if(cs5529_wait_for_idle(dev))
4520 comedi_error(dev, "timeout or signal in init_cs5529()\n");
4524 rt_printk("config: 0x%x\n", cs5529_config_read(dev, CSCMD_CONFIG_REGISTER));
4525 rt_printk("gain: 0x%x\n", cs5529_config_read(dev, CSCMD_GAIN_REGISTER));
4526 rt_printk("offset: 0x%x\n", cs5529_config_read(dev, CSCMD_OFFSET_REGISTER));