2 comedi/drivers/ni_mio_common.c
3 Hardware driver for DAQ-STC based boards
5 COMEDI - Linux Control and Measurement Device Interface
6 Copyright (C) 1997-2001 David A. Schleef <ds@schleef.org>
7 Copyright (C) 2002, 2003 Frank Mori Hess <fmhess@users.sourceforge.net
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 This file is meant to be included by another file, e.g.,
27 ni_atmio.c or ni_pcimio.c.
29 Interrupt support originally added by Truxton Fulton
32 References (from ftp://ftp.natinst.com/support/manuals):
34 340747b.pdf AT-MIO E series Register Level Programmer Manual
35 341079b.pdf PCI E Series RLPM
36 340934b.pdf DAQ-STC reference manual
37 67xx and 611x registers (from http://www.ni.com/pdf/daq/us)
40 Other possibly relevant info:
42 320517c.pdf User manual (obsolete)
43 320517f.pdf User manual (new)
45 320906c.pdf maximum signal ratings
47 321791a.pdf discontinuation of at-mio-16e-10 rev. c
48 321808a.pdf about at-mio-16e-10 rev P
49 321837a.pdf discontinuation of at-mio-16de-10 rev d
50 321838a.pdf about at-mio-16de-10 rev N
54 - the interrupt routine needs to be cleaned up
55 - many printk's need to be changed to rt_printk()
57 2006-02-07: S-Series PCI-6143: Support has been added but is not
58 fully tested as yet. Terry Barnaby, BEAM Ltd.
61 //#define DEBUG_INTERRUPT
62 //#define DEBUG_STATUS_A
63 //#define DEBUG_STATUS_B
67 #include "comedi_fc.h"
70 #define MDPRINTK(format,args...)
75 #define NI_TIMEOUT 1000
77 /* Note: this table must match the ai_gain_* definitions */
78 static short ni_gainlkup[][16]={
80 { 0, 1, 2, 3, 4, 5, 6, 7, 0x100, 0x101, 0x102, 0x103, 0x104, 0x105,
83 { 1, 2, 4, 7, 0x101, 0x102, 0x104, 0x107 },
85 { 1, 2, 3, 4, 5, 6, 7, 0x101, 0x102, 0x103, 0x104, 0x105, 0x106,
90 { 0x00a, 0x00b, 0x001, 0x002, 0x003, 0x004, 0x005, 0x006 },
94 { 1, 2, 3, 4, 5, 6, 7},
96 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
99 static comedi_lrange range_ni_E_ai={ 16, {
105 RANGE( -0.25, 0.25 ),
107 RANGE( -0.05, 0.05 ),
117 static comedi_lrange range_ni_E_ai_limited={ 8, {
127 static comedi_lrange range_ni_E_ai_limited14={ 14, {
143 static comedi_lrange range_ni_E_ai_bipolar4={ 4, {
147 RANGE( -0.05, 0.05 ),
149 static comedi_lrange range_ni_E_ai_611x={ 8, {
159 static comedi_lrange range_ni_M_ai_622x={ 4, {
165 static comedi_lrange range_ni_M_ai_628x={ 7, {
174 static comedi_lrange range_ni_S_ai_6143 = { 1, {
177 static comedi_lrange range_ni_E_ao_ext = { 4, {
184 static comedi_lrange *ni_range_lkup[]={
186 &range_ni_E_ai_limited,
187 &range_ni_E_ai_limited14,
188 &range_ni_E_ai_bipolar4,
197 static int ni_dio_insn_config(comedi_device *dev,comedi_subdevice *s,
198 comedi_insn *insn,lsampl_t *data);
199 static int ni_dio_insn_bits(comedi_device *dev,comedi_subdevice *s,
200 comedi_insn *insn,lsampl_t *data);
202 static int ni_serial_insn_config(comedi_device *dev,comedi_subdevice *s,
203 comedi_insn *insn,lsampl_t *data);
204 static int ni_serial_hw_readwrite8(comedi_device *dev,comedi_subdevice *s,
205 unsigned char data_out, unsigned char *data_in);
206 static int ni_serial_sw_readwrite8(comedi_device *dev,comedi_subdevice *s,
207 unsigned char data_out, unsigned char *data_in);
209 static int ni_calib_insn_read(comedi_device *dev,comedi_subdevice *s,
210 comedi_insn *insn,lsampl_t *data);
211 static int ni_calib_insn_write(comedi_device *dev,comedi_subdevice *s,
212 comedi_insn *insn,lsampl_t *data);
214 static int ni_eeprom_insn_read(comedi_device *dev,comedi_subdevice *s,
215 comedi_insn *insn,lsampl_t *data);
216 static int ni_m_series_eeprom_insn_read(comedi_device *dev,comedi_subdevice *s,
217 comedi_insn *insn,lsampl_t *data);
219 static int ni_pfi_insn_bits(comedi_device *dev,comedi_subdevice *s,
220 comedi_insn *insn,lsampl_t *data);
221 static int ni_pfi_insn_config(comedi_device *dev,comedi_subdevice *s,
222 comedi_insn *insn,lsampl_t *data);
224 static void ni_rtsi_init(comedi_device *dev);
225 static int ni_rtsi_insn_bits(comedi_device *dev,comedi_subdevice *s,
226 comedi_insn *insn,lsampl_t *data);
227 static int ni_rtsi_insn_config(comedi_device *dev,comedi_subdevice *s,
228 comedi_insn *insn,lsampl_t *data);
230 static void caldac_setup(comedi_device *dev,comedi_subdevice *s);
231 static int ni_read_eeprom(comedi_device *dev,int addr);
233 #ifdef DEBUG_STATUS_A
234 static void ni_mio_print_status_a(int status);
236 #define ni_mio_print_status_a(a)
238 #ifdef DEBUG_STATUS_B
239 static void ni_mio_print_status_b(int status);
241 #define ni_mio_print_status_b(a)
244 static int ni_ai_reset(comedi_device *dev,comedi_subdevice *s);
246 static void ni_handle_fifo_half_full(comedi_device *dev);
247 static int ni_ao_fifo_half_empty(comedi_device *dev,comedi_subdevice *s);
249 static void ni_handle_fifo_dregs(comedi_device *dev);
250 static int ni_ai_inttrig(comedi_device *dev,comedi_subdevice *s,
251 unsigned int trignum);
252 static void ni_load_channelgain_list(comedi_device *dev,unsigned int n_chan,
254 static void shutdown_ai_command( comedi_device *dev );
256 static int ni_ao_inttrig(comedi_device *dev,comedi_subdevice *s,
257 unsigned int trignum);
259 static int ni_ao_reset(comedi_device *dev,comedi_subdevice *s);
261 static int ni_8255_callback(int dir,int port,int data,unsigned long arg);
263 static int ni_ns_to_timer(int *nanosec,int round_mode);
266 /*GPCT function def's*/
267 static int GPCT_G_Watch(comedi_device *dev, int chan);
269 static void GPCT_Reset(comedi_device *dev, int chan);
270 static void GPCT_Gen_Cont_Pulse(comedi_device *dev, int chan, unsigned int length);
271 static void GPCT_Gen_Single_Pulse(comedi_device *dev, int chan, unsigned int length);
272 static void GPCT_Period_Meas(comedi_device *dev, int chan);
273 static void GPCT_Pulse_Width_Meas(comedi_device *dev, int chan);
274 static void GPCT_Event_Counting(comedi_device *dev,int chan);
275 static int GPCT_Set_Direction(comedi_device *dev,int chan,int direction);
276 static int GPCT_Set_Gate(comedi_device *dev,int chan ,int gate);
277 static int GPCT_Set_Source(comedi_device *dev,int chan ,int source);
279 static int ni_gpct_insn_write(comedi_device *dev,comedi_subdevice *s,
280 comedi_insn *insn,lsampl_t *data);
281 static int ni_gpct_insn_read(comedi_device *dev,comedi_subdevice *s,
282 comedi_insn *insn,lsampl_t *data);
283 static int ni_gpct_insn_config(comedi_device *dev,comedi_subdevice *s,
284 comedi_insn *insn,lsampl_t *data);
286 static int init_cs5529(comedi_device *dev);
287 static int cs5529_do_conversion(comedi_device *dev, unsigned short *data);
288 static int cs5529_ai_insn_read(comedi_device *dev,comedi_subdevice *s,comedi_insn *insn,lsampl_t *data);
289 static unsigned int cs5529_config_read(comedi_device *dev, unsigned int reg_select_bits);
290 static void cs5529_config_write(comedi_device *dev, unsigned int value, unsigned int reg_select_bits);
292 static int ni_m_series_pwm_config(comedi_device *dev, comedi_subdevice *s,
293 comedi_insn *insn,lsampl_t *data);
294 static int ni_6143_pwm_config(comedi_device *dev, comedi_subdevice *s,
295 comedi_insn *insn, lsampl_t *data);
300 AIMODE_HALF_FULL = 1,
305 #define SERIAL_DISABLED 0
306 #define SERIAL_600NS 600
307 #define SERIAL_1_2US 1200
308 #define SERIAL_10US 10000
310 static const int num_adc_stages_611x = 3;
312 static void handle_a_interrupt(comedi_device *dev,unsigned short status,
313 unsigned int m_status);
314 static void handle_b_interrupt(comedi_device *dev,unsigned short status,
315 unsigned int m_status);
316 static void get_last_sample_611x( comedi_device *dev );
317 static void get_last_sample_6143( comedi_device *dev );
319 //static void mite_handle_interrupt(comedi_device *dev,unsigned int status);
320 static int ni_ai_drain_dma(comedi_device *dev );
323 static void ni_flush_ai_fifo(comedi_device *dev){
324 if(boardtype.reg_type == ni_reg_6143){
325 // Flush the 6143 data FIFO
326 ni_writel(0x10, AIFIFO_Control_6143); // Flush fifo
327 ni_writel(0x00, AIFIFO_Control_6143); // Flush fifo
328 while(ni_readl(AIFIFO_Status_6143) & 0x10); // Wait for complete
331 devpriv->stc_writew(dev, 1,ADC_FIFO_Clear);
335 static void win_out2(comedi_device *dev, uint32_t data, int reg)
337 devpriv->stc_writew(dev, data >> 16, reg);
338 devpriv->stc_writew(dev, data & 0xffff, reg + 1);
341 #define ao_win_out(data,addr) ni_ao_win_outw(dev,data,addr)
342 static inline void ni_ao_win_outw( comedi_device *dev, uint16_t data, int addr )
346 comedi_spin_lock_irqsave(&devpriv->window_lock,flags);
347 ni_writew(addr,AO_Window_Address_611x);
348 ni_writew(data,AO_Window_Data_611x);
349 comedi_spin_unlock_irqrestore(&devpriv->window_lock,flags);
352 static inline void ni_ao_win_outl(comedi_device *dev, uint32_t data, int addr)
356 comedi_spin_lock_irqsave(&devpriv->window_lock,flags);
357 ni_writew(addr,AO_Window_Address_611x);
358 ni_writel(data,AO_Window_Data_611x);
359 comedi_spin_unlock_irqrestore(&devpriv->window_lock,flags);
362 static inline unsigned short ni_ao_win_inw( comedi_device *dev, int addr )
367 comedi_spin_lock_irqsave(&devpriv->window_lock,flags);
368 ni_writew(addr, AO_Window_Address_611x);
369 data = ni_readw(AO_Window_Data_611x);
370 comedi_spin_unlock_irqrestore(&devpriv->window_lock,flags);
374 /* ni_set_bits( ) allows different parts of the ni_mio_common driver to
375 * share registers (such as Interrupt_A_Register) without interfering with
378 * NOTE: the switch/case statements are optimized out for a constant argument
379 * so this is actually quite fast--- If you must wrap another function around this
380 * make it inline to avoid a large speed penalty.
382 * value should only be 1 or 0.
384 static inline void ni_set_bits(comedi_device *dev, int reg, int bits, int value)
388 comedi_spin_lock_irqsave( &devpriv->window_lock, flags );
390 case Interrupt_A_Enable_Register:
392 devpriv->int_a_enable_reg |= bits;
394 devpriv->int_a_enable_reg &= ~bits;
395 comedi_spin_unlock_irqrestore( &devpriv->window_lock, flags );
396 devpriv->stc_writew(dev, devpriv->int_a_enable_reg,Interrupt_A_Enable_Register);
398 case Interrupt_B_Enable_Register:
400 devpriv->int_b_enable_reg |= bits;
402 devpriv->int_b_enable_reg &= ~bits;
403 comedi_spin_unlock_irqrestore( &devpriv->window_lock, flags );
404 devpriv->stc_writew(dev, devpriv->int_b_enable_reg,Interrupt_B_Enable_Register);
406 case IO_Bidirection_Pin_Register:
408 devpriv->io_bidirection_pin_reg |= bits;
410 devpriv->io_bidirection_pin_reg &= ~bits;
411 comedi_spin_unlock_irqrestore( &devpriv->window_lock, flags );
412 devpriv->stc_writew(dev, devpriv->io_bidirection_pin_reg,IO_Bidirection_Pin_Register);
415 printk("Warning ni_set_bits() called with invalid arguments\n");
416 printk("reg is %d\n",reg);
417 comedi_spin_unlock_irqrestore( &devpriv->window_lock, flags );
423 static irqreturn_t ni_E_interrupt(int irq,void *d,struct pt_regs * regs)
425 comedi_device *dev=d;
426 unsigned short a_status;
427 unsigned short b_status;
428 unsigned int m0_status;
429 unsigned int m1_status;
432 struct mite_struct *mite = devpriv->mite;
435 if(dev->attached == 0) return IRQ_NONE;
436 // lock to avoid race with comedi_poll
437 comedi_spin_lock_irqsave(&dev->spinlock, flags);
438 a_status=devpriv->stc_readw(dev, AI_Status_1_Register);
439 b_status=devpriv->stc_readw(dev, AO_Status_1_Register);
441 m0_status=readl(mite->mite_io_addr + MITE_CHSR(AI_DMA_CHAN));
442 m1_status=readl(mite->mite_io_addr + MITE_CHSR(AO_DMA_CHAN));
448 if(a_status&Interrupt_A_St || m0_status & CHSR_INT )
449 handle_a_interrupt(dev, a_status, m0_status);
450 if(b_status&Interrupt_B_St || m1_status & CHSR_INT )
451 handle_b_interrupt(dev, b_status, m1_status);
452 comedi_spin_unlock_irqrestore(&dev->spinlock, flags);
457 static void ni_sync_ai_dma(struct mite_struct *mite, comedi_device *dev)
460 comedi_subdevice *s = dev->subdevices + 0;
461 comedi_async *async = s->async;
462 unsigned int nbytes, old_alloc_count;
463 unsigned int bytes_per_scan = bytes_per_sample(s) * async->cmd.chanlist_len;
465 old_alloc_count = async->buf_write_alloc_count;
466 // write alloc as much as we can
467 comedi_buf_write_alloc(s->async, s->async->prealloc_bufsz);
469 nbytes = mite_bytes_written_to_memory_lb(mite, AI_DMA_CHAN);
471 if( (int)(mite_bytes_written_to_memory_ub(mite, AI_DMA_CHAN) - old_alloc_count) > 0 ){
472 printk("ni_mio_common: DMA overwrite of free area\n");
474 async->events |= COMEDI_CB_OVERFLOW;
478 count = nbytes - async->buf_write_count;
480 /* it's possible count will be negative due to
481 * conservative value returned by mite_bytes_transferred */
484 comedi_buf_write_free(async, count);
486 async->scan_progress += count;
487 if( async->scan_progress >= bytes_per_scan )
489 async->scan_progress %= bytes_per_scan;
490 async->events |= COMEDI_CB_EOS;
492 async->events |= COMEDI_CB_BLOCK;
495 static void mite_handle_b_linkc(struct mite_struct *mite, comedi_device *dev)
498 comedi_subdevice *s = dev->subdevices + 1;
499 comedi_async *async = s->async;
500 u32 nbytes_ub, nbytes_lb;
501 unsigned int new_write_count;
502 u32 stop_count = async->cmd.stop_arg * sizeof(sampl_t);
504 writel(CHOR_CLRLC, mite->mite_io_addr + MITE_CHOR(AO_DMA_CHAN));
506 new_write_count = async->buf_write_count;
508 nbytes_lb = mite_bytes_read_from_memory_lb(mite, AO_DMA_CHAN);
509 if(async->cmd.stop_src == TRIG_COUNT &&
510 (int) (nbytes_lb - stop_count) > 0)
511 nbytes_lb = stop_count;
513 nbytes_ub = mite_bytes_read_from_memory_ub(mite, AO_DMA_CHAN);
514 if(async->cmd.stop_src == TRIG_COUNT &&
515 (int) (nbytes_ub - stop_count) > 0)
516 nbytes_ub = stop_count;
517 if((int)(nbytes_ub - devpriv->last_buf_write_count) > 0){
518 rt_printk("ni_mio_common: DMA underrun\n");
520 async->events |= COMEDI_CB_OVERFLOW;
524 devpriv->last_buf_write_count = new_write_count;
526 count = nbytes_lb - async->buf_read_count;
530 comedi_buf_read_free(async, count);
532 async->events |= COMEDI_CB_BLOCK;
535 static int ni_ao_wait_for_dma_load( comedi_device *dev )
537 static const int timeout = 10000;
540 for(i = 0; i < timeout; i++)
542 unsigned short b_status;
544 b_status = devpriv->stc_readw(dev, AO_Status_1_Register );
545 if( b_status & AO_FIFO_Half_Full_St )
547 /* if we poll too often, the pci bus activity seems
548 to slow the dma transfer down */
553 comedi_error(dev, "timed out waiting for dma load");
560 static void ni_handle_eos(comedi_device *dev, comedi_subdevice *s)
562 if(devpriv->aimode == AIMODE_SCAN)
565 static const int timeout = 10;
568 for(i = 0; i < timeout; i++)
570 ni_sync_ai_dma(devpriv->mite, dev);
571 if((s->async->events & COMEDI_CB_EOS)) break;
575 ni_handle_fifo_dregs(dev);
576 s->async->events |= COMEDI_CB_EOS;
579 /* handle special case of single scan using AI_End_On_End_Of_Scan */
580 if((devpriv->ai_cmd2 & AI_End_On_End_Of_Scan)){
581 shutdown_ai_command( dev );
586 static void shutdown_ai_command( comedi_device *dev )
588 comedi_subdevice *s = dev->subdevices + 0;
591 ni_ai_drain_dma( dev );
592 mite_dma_disarm(devpriv->mite, AI_DMA_CHAN);
594 ni_handle_fifo_dregs(dev);
595 get_last_sample_611x(dev);
596 get_last_sample_6143(dev);
598 ni_set_bits(dev, Interrupt_A_Enable_Register,
599 AI_SC_TC_Interrupt_Enable | AI_START1_Interrupt_Enable|
600 AI_START2_Interrupt_Enable| AI_START_Interrupt_Enable|
601 AI_STOP_Interrupt_Enable| AI_Error_Interrupt_Enable|
602 AI_FIFO_Interrupt_Enable,0);
604 s->async->events |= COMEDI_CB_EOA;
607 static void handle_a_interrupt(comedi_device *dev,unsigned short status,
608 unsigned int m_status)
610 comedi_subdevice *s=dev->subdevices+0;
611 unsigned short ack=0;
613 s->async->events = 0;
615 #ifdef DEBUG_INTERRUPT
616 rt_printk("ni_mio_common: interrupt: a_status=%04x m0_status=%08x\n",
618 ni_mio_print_status_a(status);
623 /* Currently, mite.c requires us to handle LINKC and DONE */
624 if(m_status & CHSR_LINKC){
625 writel(CHOR_CLRLC, devpriv->mite->mite_io_addr + MITE_CHOR(AI_DMA_CHAN));
626 ni_sync_ai_dma(devpriv->mite, dev);
629 if(m_status & CHSR_DONE){
630 writel(CHOR_CLRDONE, devpriv->mite->mite_io_addr + MITE_CHOR(AI_DMA_CHAN));
633 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)){
634 printk("unknown mite interrupt, ack! (m_status=%08x)\n", m_status);
635 //mite_print_chsr(m_status);
636 mite_dma_disarm(devpriv->mite, AI_DMA_CHAN );
637 writel(CHOR_DMARESET, devpriv->mite->mite_io_addr + MITE_CHOR(AI_DMA_CHAN));
638 //disable_irq(dev->irq);
642 /* test for all uncommon interrupt events at the same time */
643 if(status&(AI_Overrun_St|AI_Overflow_St|AI_SC_TC_Error_St|AI_SC_TC_St|AI_START1_St)){
645 rt_printk("ni_mio_common: a_status=0xffff. Card removed?\n");
646 /* we probably aren't even running a command now,
647 * so it's a good idea to be careful. */
648 if(s->subdev_flags&SDF_RUNNING){
649 s->async->events |= COMEDI_CB_ERROR | COMEDI_CB_EOA;
650 //comedi_event(dev,s,s->async->events);
654 if(status&(AI_Overrun_St|AI_Overflow_St|AI_SC_TC_Error_St)){
655 rt_printk("ni_mio_common: ai error a_status=%04x\n",
657 ni_mio_print_status_a(status);
659 ni_ai_reset(dev,dev->subdevices);
662 shutdown_ai_command( dev );
664 s->async->events |= COMEDI_CB_ERROR;
665 if(status & (AI_Overrun_St | AI_Overflow_St))
666 s->async->events |= COMEDI_CB_OVERFLOW;
668 comedi_event(dev,s,s->async->events);
672 if(status&AI_SC_TC_St){
673 #ifdef DEBUG_INTERRUPT
674 rt_printk("ni_mio_common: SC_TC interrupt\n");
676 if(!devpriv->ai_continuous){
677 shutdown_ai_command( dev );
679 ack|=AI_SC_TC_Interrupt_Ack;
681 if(status&AI_START1_St){
682 ack|=AI_START1_Interrupt_Ack;
686 if(status&AI_FIFO_Half_Full_St){
688 static const int timeout = 10;
689 /* pcmcia cards (at least 6036) seem to stop producing interrupts if we
690 *fail to get the fifo less than half full, so loop to be sure.*/
691 for(i = 0; i < timeout; ++i)
693 ni_handle_fifo_half_full(dev);
694 if((devpriv->stc_readw(dev, AI_Status_1_Register) & AI_FIFO_Half_Full_St) == 0)
700 if( (status & AI_STOP_St) ){
701 ni_handle_eos(dev, s);
702 /* we need to ack the START, also */
703 ack |= AI_STOP_Interrupt_Ack|AI_START_Interrupt_Ack;
706 if(devpriv->aimode==AIMODE_SAMPLE){
707 ni_handle_fifo_dregs(dev);
709 //s->async->events |= COMEDI_CB_SAMPLE;
712 if(ack) devpriv->stc_writew(dev, ack,Interrupt_A_Ack_Register);
714 comedi_event(dev,s,s->async->events);
716 #ifdef DEBUG_INTERRUPT
717 status=devpriv->stc_readw(dev, AI_Status_1_Register);
718 if(status&Interrupt_A_St){
719 printk("handle_a_interrupt: didn't clear interrupt? status=0x%x\n", status);
724 static void handle_b_interrupt(comedi_device *dev,unsigned short b_status, unsigned int m_status)
726 comedi_subdevice *s=dev->subdevices+1;
727 //unsigned short ack=0;
728 #ifdef DEBUG_INTERRUPT
729 rt_printk("ni_mio_common: interrupt: b_status=%04x m1_status=%08x\n",
731 ni_mio_print_status_b(b_status);
736 /* Currently, mite.c requires us to handle LINKC and DONE */
737 if(m_status & CHSR_LINKC){
738 mite_handle_b_linkc(devpriv->mite, dev);
741 if(m_status & CHSR_DONE){
742 writel(CHOR_CLRDONE, devpriv->mite->mite_io_addr + MITE_CHOR(AO_DMA_CHAN));
745 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)){
746 printk("unknown mite interrupt, ack! (m_status=%08x)\n", m_status);
747 //mite_print_chsr(m_status);
748 mite_dma_disarm(devpriv->mite, AO_DMA_CHAN );
749 writel(CHOR_DMARESET, devpriv->mite->mite_io_addr + MITE_CHOR(AO_DMA_CHAN));
753 if(b_status==0xffff)return;
754 if(b_status&AO_Overrun_St){
755 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));
757 s->async->events |= COMEDI_CB_OVERFLOW;
760 if(b_status&AO_BC_TC_St){
761 MDPRINTK("ni_mio_common: AO BC_TC status=0x%04x status2=0x%04x\n",b_status,devpriv->stc_readw(dev, AO_Status_2_Register));
763 s->async->events |= COMEDI_CB_EOA;
767 if(b_status&AO_FIFO_Request_St){
770 ret = ni_ao_fifo_half_empty(dev,s);
772 rt_printk("ni_mio_common: AO buffer underrun\n");
773 ni_set_bits(dev, Interrupt_B_Enable_Register,
774 AO_FIFO_Interrupt_Enable|AO_Error_Interrupt_Enable, 0);
775 s->async->events |= COMEDI_CB_OVERFLOW;
780 b_status=devpriv->stc_readw(dev, AO_Status_1_Register);
781 if(b_status&Interrupt_B_St){
782 if(b_status&AO_FIFO_Request_St){
783 rt_printk("ni_mio_common: AO buffer underrun\n");
785 rt_printk("Ack! didn't clear AO interrupt. b_status=0x%04x\n",b_status);
786 ni_set_bits(dev,Interrupt_B_Enable_Register,~0,0);
788 s->async->events |= COMEDI_CB_OVERFLOW;
791 comedi_event(dev,s,s->async->events);
794 #ifdef DEBUG_STATUS_A
795 static char *status_a_strings[]={
796 "passthru0","fifo","G0_gate","G0_TC",
797 "stop","start","sc_tc","start1",
798 "start2","sc_tc_error","overflow","overrun",
799 "fifo_empty","fifo_half_full","fifo_full","interrupt_a"
802 static void ni_mio_print_status_a(int status)
806 rt_printk("A status:");
809 rt_printk(" %s",status_a_strings[i]);
816 #ifdef DEBUG_STATUS_B
817 static char *status_b_strings[]={
818 "passthru1","fifo","G1_gate","G1_TC",
819 "UI2_TC","UPDATE","UC_TC","BC_TC",
820 "start1","overrun","start","bc_tc_error",
821 "fifo_empty","fifo_half_full","fifo_full","interrupt_b"
824 static void ni_mio_print_status_b(int status)
828 rt_printk("B status:");
831 rt_printk(" %s",status_b_strings[i]);
840 static void ni_ao_fifo_load(comedi_device *dev,comedi_subdevice *s, int n)
842 comedi_async *async = s->async;
843 comedi_cmd *cmd = &async->cmd;
851 chan = async->cur_chan;
853 err &= comedi_buf_get(async, &d);
856 range = CR_RANGE(cmd->chanlist[chan]);
858 if(boardtype.reg_type & ni_reg_6xxx_mask)
860 packed_data = d & 0xffff;
861 /* 6711 only has 16 bit wide ao fifo */
862 if(boardtype.reg_type != ni_reg_6711)
864 err &= comedi_buf_get(async, &d);
868 packed_data |= ( d << 16 ) & 0xffff0000;
870 ni_writel( packed_data, DAC_FIFO_Data_611x );
872 ni_writew(d, DAC_FIFO_Data);
875 chan %= cmd->chanlist_len;
877 async->cur_chan = chan;
879 async->events |= COMEDI_CB_OVERFLOW;
884 * There's a small problem if the FIFO gets really low and we
885 * don't have the data to fill it. Basically, if after we fill
886 * the FIFO with all the data available, the FIFO is _still_
887 * less than half full, we never clear the interrupt. If the
888 * IRQ is in edge mode, we never get another interrupt, because
889 * this one wasn't cleared. If in level mode, we get flooded
890 * with interrupts that we can't fulfill, because nothing ever
891 * gets put into the buffer.
893 * This kind of situation is recoverable, but it is easier to
894 * just pretend we had a FIFO underrun, since there is a good
895 * chance it will happen anyway. This is _not_ the case for
896 * RT code, as RT code might purposely be running close to the
897 * metal. Needs to be fixed eventually.
899 static int ni_ao_fifo_half_empty(comedi_device *dev,comedi_subdevice *s)
903 n = comedi_buf_read_n_available(s);
905 s->async->events |= COMEDI_CB_OVERFLOW;
909 n /= sizeof(sampl_t);
910 if(n > boardtype.ao_fifo_depth / 2)
911 n = boardtype.ao_fifo_depth / 2;
913 ni_ao_fifo_load(dev,s,n);
915 s->async->events |= COMEDI_CB_BLOCK;
920 static int ni_ao_prep_fifo(comedi_device *dev,comedi_subdevice *s)
925 devpriv->stc_writew(dev, 1,DAC_FIFO_Clear);
926 if(boardtype.reg_type & ni_reg_6xxx_mask)
927 ni_ao_win_outl(dev, 0x6, AO_FIFO_Offset_Load_611x);
930 n = comedi_buf_read_n_available(s);
933 n /= sizeof(sampl_t);
934 if(n > boardtype.ao_fifo_depth)
935 n = boardtype.ao_fifo_depth;
937 ni_ao_fifo_load(dev,s,n);
942 static void ni_ai_fifo_read(comedi_device *dev,comedi_subdevice *s,
945 comedi_async *async = s->async;
948 if(boardtype.reg_type == ni_reg_611x){
952 for( i = 0; i < n / 2; i++ ){
953 dl=ni_readl(ADC_FIFO_Data_611x);
954 /* This may get the hi/lo data in the wrong order */
955 data[0] = (dl>>16) & 0xffff;
956 data[1] = dl & 0xffff;
957 cfc_write_array_to_buffer(s, data, sizeof(data));
959 /* Check if there's a single sample stuck in the FIFO */
961 dl=ni_readl(ADC_FIFO_Data_611x);
962 data[0] = dl & 0xffff;
963 cfc_write_to_buffer(s, data[0]);
965 } else if(boardtype.reg_type == ni_reg_6143){
969 // This just reads the FIFO assuming the data is present, no checks on the FIFO status are performed
970 for(i = 0; i < n / 2; i++){
971 dl = ni_readl(AIFIFO_Data_6143);
973 data[0] = (dl >> 16) & 0xffff;
974 data[1] = dl & 0xffff;
975 cfc_write_array_to_buffer(s, data, sizeof(data));
978 /* Assume there is a single sample stuck in the FIFO */
979 ni_writel(0x01, AIFIFO_Control_6143); // Get stranded sample into FIFO
980 dl = ni_readl(AIFIFO_Data_6143);
981 data[0] = (dl >> 16) & 0xffff;
982 cfc_write_to_buffer(s, data[0]);
985 if( n > sizeof(devpriv->ai_fifo_buffer) / sizeof(devpriv->ai_fifo_buffer[0]))
987 comedi_error( dev, "bug! ai_fifo_buffer too small" );
988 async->events |= COMEDI_CB_ERROR;
991 for(i = 0; i < n; i++){
992 devpriv->ai_fifo_buffer[i] = ni_readw(ADC_FIFO_Data_Register);
994 cfc_write_array_to_buffer( s, devpriv->ai_fifo_buffer,
995 n * sizeof(devpriv->ai_fifo_buffer[0]) );
999 static void ni_handle_fifo_half_full(comedi_device *dev)
1002 comedi_subdevice *s=dev->subdevices+0;
1004 n=boardtype.ai_fifo_depth/2;
1006 ni_ai_fifo_read(dev,s,n);
1011 static int ni_ai_drain_dma(comedi_device *dev )
1013 struct mite_struct *mite = devpriv->mite;
1015 static const int timeout = 10000;
1017 for( i = 0; i < timeout; i++ )
1019 if((devpriv->stc_readw(dev, AI_Status_1_Register) & AI_FIFO_Empty_St) &&
1020 mite_bytes_in_transit(mite, AI_DMA_CHAN) == 0)
1026 rt_printk("ni_mio_common: wait for dma drain timed out\n");
1027 rt_printk("mite_bytes_in_transit=%i, AI_Status1_Register=0x%x\n",
1028 mite_bytes_in_transit(mite, AI_DMA_CHAN), devpriv->stc_readw(dev, AI_Status_1_Register));
1032 ni_sync_ai_dma( mite, dev );
1040 static void ni_handle_fifo_dregs(comedi_device *dev)
1042 comedi_subdevice *s=dev->subdevices+0;
1048 if(boardtype.reg_type == ni_reg_611x){
1049 while((devpriv->stc_readw(dev, AI_Status_1_Register)&AI_FIFO_Empty_St) == 0){
1050 dl=ni_readl(ADC_FIFO_Data_611x);
1052 /* This may get the hi/lo data in the wrong order */
1054 data[1] = (dl&0xffff);
1055 cfc_write_array_to_buffer(s, data, sizeof(data));
1057 }else if(boardtype.reg_type == ni_reg_6143){
1059 while(ni_readl(AIFIFO_Status_6143) & 0x04){
1060 dl = ni_readl(AIFIFO_Data_6143);
1062 /* This may get the hi/lo data in the wrong order */
1063 data[0] = (dl >> 16);
1064 data[1] = (dl & 0xffff);
1065 cfc_write_array_to_buffer(s, data, sizeof(data));
1068 // Check if stranded sample is present
1069 if(ni_readl(AIFIFO_Status_6143) & 0x01){
1070 ni_writel(0x01, AIFIFO_Control_6143); // Get stranded sample into FIFO
1071 dl = ni_readl(AIFIFO_Data_6143);
1072 data[0] = (dl >> 16) & 0xffff;
1073 cfc_write_to_buffer(s, data[0]);
1077 fifo_empty = devpriv->stc_readw(dev, AI_Status_1_Register) & AI_FIFO_Empty_St;
1078 while(fifo_empty == 0)
1080 for(i = 0; i < sizeof(devpriv->ai_fifo_buffer) / sizeof(devpriv->ai_fifo_buffer[0]); i++)
1082 fifo_empty = devpriv->stc_readw(dev, AI_Status_1_Register) & AI_FIFO_Empty_St;
1083 if(fifo_empty) break;
1084 devpriv->ai_fifo_buffer[i] = ni_readw(ADC_FIFO_Data_Register);
1086 cfc_write_array_to_buffer( s, devpriv->ai_fifo_buffer,
1087 i * sizeof(devpriv->ai_fifo_buffer[0]) );
1092 static void get_last_sample_611x( comedi_device *dev )
1094 comedi_subdevice *s=dev->subdevices+0;
1098 if(boardtype.reg_type != ni_reg_611x) return;
1100 /* Check if there's a single sample stuck in the FIFO */
1101 if(ni_readb(XXX_Status)&0x80){
1102 dl=ni_readl(ADC_FIFO_Data_611x);
1104 cfc_write_to_buffer(s, data);
1108 static void get_last_sample_6143(comedi_device* dev)
1110 comedi_subdevice* s = dev->subdevices + 0;
1114 if(boardtype.reg_type != ni_reg_6143) return;
1116 /* Check if there's a single sample stuck in the FIFO */
1117 if(ni_readl(AIFIFO_Status_6143) & 0x01){
1118 ni_writel(0x01, AIFIFO_Control_6143); // Get stranded sample into FIFO
1119 dl = ni_readl(AIFIFO_Data_6143);
1121 /* This may get the hi/lo data in the wrong order */
1122 data = (dl >> 16) & 0xffff;
1123 cfc_write_to_buffer(s, data);
1127 static void ni_ai_munge(comedi_device *dev, comedi_subdevice *s,
1128 void *data, unsigned int num_bytes, unsigned int chan_index )
1130 comedi_async *async = s->async;
1132 unsigned int length = num_bytes / bytes_per_sample(s);
1133 sampl_t *array = data;
1134 lsampl_t *larray = data;
1135 for(i = 0; i < length; i++)
1138 if(s->subdev_flags & SDF_LSAMPL)
1139 larray[i] = le32_to_cpu(larray[i]);
1141 array[i] = le16_to_cpu(array[i]);
1143 if(s->subdev_flags & SDF_LSAMPL)
1144 larray[i] += devpriv->ai_offset[chan_index];
1146 array[i] += devpriv->ai_offset[chan_index];
1148 chan_index %= async->cmd.chanlist_len;
1154 static void ni_ai_setup_MITE_dma(comedi_device *dev,comedi_cmd *cmd)
1156 struct mite_struct *mite = devpriv->mite;
1157 struct mite_channel *mite_chan = &mite->channels[ AI_DMA_CHAN ];
1158 comedi_subdevice *s = dev->subdevices + 0;
1160 /* write alloc the entire buffer */
1161 comedi_buf_write_alloc(s->async, s->async->prealloc_bufsz);
1163 mite_chan->current_link = 0;
1164 mite_chan->dir = COMEDI_INPUT;
1165 switch(boardtype.reg_type)
1169 mite_prep_dma(mite, AI_DMA_CHAN, 32, 16);
1171 case ni_reg_m_series:
1172 if(boardtype.adbits > 16)
1173 mite_prep_dma(mite, AI_DMA_CHAN, 32, 32);
1175 mite_prep_dma(mite, AI_DMA_CHAN, 16, 16); //guess
1178 mite_prep_dma(mite, AI_DMA_CHAN, 16, 16);
1182 mite_dma_arm(mite, AI_DMA_CHAN);
1185 static void ni_ao_setup_MITE_dma(comedi_device *dev,comedi_cmd *cmd)
1187 struct mite_struct *mite = devpriv->mite;
1188 struct mite_channel *mite_chan = &mite->channels[ AO_DMA_CHAN ];
1189 comedi_subdevice *s = dev->subdevices + 1;
1191 devpriv->last_buf_write_count = s->async->buf_write_count;
1193 mite_chan->current_link = 0;
1194 mite_chan->dir = COMEDI_OUTPUT;
1195 if(boardtype.reg_type & (ni_reg_611x | ni_reg_6713))
1196 mite_prep_dma(mite, AO_DMA_CHAN, 32, 32);
1198 mite_prep_dma(mite, AO_DMA_CHAN, 16, 16);
1201 mite_dma_arm(mite, AO_DMA_CHAN);
1207 used for both cancel ioctl and board initialization
1209 this is pretty harsh for a cancel, but it works...
1212 static int ni_ai_reset(comedi_device *dev,comedi_subdevice *s)
1215 mite_dma_disarm(devpriv->mite, AI_DMA_CHAN);
1217 /* ai configuration */
1218 devpriv->stc_writew(dev, AI_Configuration_Start | AI_Reset, Joint_Reset_Register);
1220 ni_set_bits(dev, Interrupt_A_Enable_Register,
1221 AI_SC_TC_Interrupt_Enable | AI_START1_Interrupt_Enable|
1222 AI_START2_Interrupt_Enable| AI_START_Interrupt_Enable|
1223 AI_STOP_Interrupt_Enable| AI_Error_Interrupt_Enable|
1224 AI_FIFO_Interrupt_Enable,0);
1226 ni_flush_ai_fifo(dev);
1228 if(boardtype.reg_type != ni_reg_6143)
1229 ni_writeb(0, Misc_Command);
1231 devpriv->stc_writew(dev, AI_Disarm, AI_Command_1_Register); /* reset pulses */
1232 devpriv->stc_writew(dev, AI_Start_Stop | AI_Mode_1_Reserved /*| AI_Trigger_Once */,
1233 AI_Mode_1_Register);
1234 devpriv->stc_writew(dev, 0x0000,AI_Mode_2_Register);
1235 /* generate FIFO interrupts on non-empty */
1236 devpriv->stc_writew(dev, (0<<6)|0x0000,AI_Mode_3_Register);
1237 if(boardtype.reg_type == ni_reg_611x){
1238 devpriv->stc_writew(dev, AI_SHIFTIN_Pulse_Width |
1240 AI_LOCALMUX_CLK_Pulse_Width, AI_Personal_Register);
1241 devpriv->stc_writew(dev, AI_SCAN_IN_PROG_Output_Select(3) |
1242 AI_EXTMUX_CLK_Output_Select(0) |
1243 AI_LOCALMUX_CLK_Output_Select(2) |
1244 AI_SC_TC_Output_Select(3) |
1245 AI_CONVERT_Output_Select(3),AI_Output_Control_Register);
1246 }else if(boardtype.reg_type == ni_reg_6143){
1247 devpriv->stc_writew(dev, AI_SHIFTIN_Pulse_Width |
1249 AI_LOCALMUX_CLK_Pulse_Width, AI_Personal_Register);
1250 devpriv->stc_writew(dev, AI_SCAN_IN_PROG_Output_Select(3) |
1251 AI_EXTMUX_CLK_Output_Select(0) |
1252 AI_LOCALMUX_CLK_Output_Select(2) |
1253 AI_SC_TC_Output_Select(3) |
1254 AI_CONVERT_Output_Select(2),AI_Output_Control_Register);
1256 devpriv->stc_writew(dev, AI_SHIFTIN_Pulse_Width |
1258 AI_CONVERT_Pulse_Width |
1259 AI_LOCALMUX_CLK_Pulse_Width, AI_Personal_Register);
1260 devpriv->stc_writew(dev, AI_SCAN_IN_PROG_Output_Select(3) |
1261 AI_EXTMUX_CLK_Output_Select(0) |
1262 AI_LOCALMUX_CLK_Output_Select(2) |
1263 AI_SC_TC_Output_Select(3) |
1264 AI_CONVERT_Output_Select(2),AI_Output_Control_Register);
1266 /* the following registers should not be changed, because there
1267 * are no backup registers in devpriv. If you want to change
1268 * any of these, add a backup register and other appropriate code:
1269 * AI_Mode_1_Register
1270 * AI_Mode_3_Register
1271 * AI_Personal_Register
1272 * AI_Output_Control_Register
1274 devpriv->stc_writew(dev, AI_SC_TC_Error_Confirm | AI_START_Interrupt_Ack |
1275 AI_START2_Interrupt_Ack | AI_START1_Interrupt_Ack |
1276 AI_SC_TC_Interrupt_Ack | AI_Error_Interrupt_Ack |
1277 AI_STOP_Interrupt_Ack, Interrupt_A_Ack_Register); /* clear interrupts */
1279 devpriv->stc_writew(dev, AI_Configuration_End,Joint_Reset_Register);
1284 static int ni_ai_poll(comedi_device *dev,comedi_subdevice *s)
1286 unsigned long flags = 0;
1289 // lock to avoid race with interrupt handler
1290 if(in_interrupt() == 0)
1291 comedi_spin_lock_irqsave(&dev->spinlock, flags);
1293 ni_handle_fifo_dregs(dev);
1295 ni_sync_ai_dma(devpriv->mite, dev);
1297 count = s->async->buf_write_count - s->async->buf_read_count;
1298 if(in_interrupt() == 0)
1299 comedi_spin_unlock_irqrestore(&dev->spinlock, flags);
1305 static int ni_ai_insn_read(comedi_device *dev,comedi_subdevice *s,comedi_insn *insn,lsampl_t *data)
1313 ni_load_channelgain_list(dev,1,&insn->chanspec);
1315 ni_flush_ai_fifo(dev);
1317 mask=(1<<boardtype.adbits)-1;
1318 signbits=devpriv->ai_offset[0];
1319 if(boardtype.reg_type == ni_reg_611x){
1320 for(n=0; n < num_adc_stages_611x; n++){
1321 devpriv->stc_writew(dev, AI_CONVERT_Pulse, AI_Command_1_Register);
1324 for(n=0; n<insn->n; n++){
1325 devpriv->stc_writew(dev, AI_CONVERT_Pulse, AI_Command_1_Register);
1326 /* The 611x has screwy 32-bit FIFOs. */
1328 for(i=0; i<NI_TIMEOUT; i++){
1329 if(ni_readb(XXX_Status)&0x80)
1331 d = ( ni_readl(ADC_FIFO_Data_611x) >> 16 ) & 0xffff;
1334 if(!(devpriv->stc_readw(dev, AI_Status_1_Register)&AI_FIFO_Empty_St))
1336 d = ni_readl(ADC_FIFO_Data_611x) & 0xffff;
1341 rt_printk("ni_mio_common: timeout in 611x ni_ai_insn_read\n");
1347 }else if(boardtype.reg_type == ni_reg_6143){
1348 for(n = 0; n < insn->n; n++){
1349 devpriv->stc_writew(dev, AI_CONVERT_Pulse, AI_Command_1_Register);
1351 /* The 6143 has 32-bit FIFOs. You need to strobe a bit to move a single 16bit stranded sample into the FIFO */
1353 for(i = 0; i < NI_TIMEOUT; i++){
1354 if(ni_readl(AIFIFO_Status_6143) & 0x01)
1356 ni_writel(0x01, AIFIFO_Control_6143); // Get stranded sample into FIFO
1357 dl = ni_readl(AIFIFO_Data_6143);
1361 if(i == NI_TIMEOUT){
1362 rt_printk("ni_mio_common: timeout in 6143 ni_ai_insn_read\n");
1365 data[n] = (((dl >> 16) & 0xFFFF) + signbits) & 0xFFFF;
1368 for(n=0;n<insn->n;n++){
1369 devpriv->stc_writew(dev, AI_CONVERT_Pulse, AI_Command_1_Register);
1370 for(i=0;i<NI_TIMEOUT;i++){
1371 if(!(devpriv->stc_readw(dev, AI_Status_1_Register)&AI_FIFO_Empty_St))
1375 rt_printk("ni_mio_common: timeout in ni_ai_insn_read\n");
1378 if(boardtype.reg_type == ni_reg_m_series)
1380 data[n] = ni_readl(M_Offset_AI_FIFO_Data);
1381 data[n] += signbits;
1384 d = ni_readw(ADC_FIFO_Data_Register);
1385 d += signbits; /* subtle: needs to be short addition */
1393 void ni_prime_channelgain_list(comedi_device *dev)
1396 devpriv->stc_writew(dev, AI_CONVERT_Pulse, AI_Command_1_Register);
1397 for(i = 0; i < NI_TIMEOUT; ++i)
1399 if(!(devpriv->stc_readw(dev, AI_Status_1_Register) & AI_FIFO_Empty_St))
1401 devpriv->stc_writew(dev, 1, ADC_FIFO_Clear);
1406 rt_printk("ni_mio_common: timeout loading channel/gain list\n");
1409 static void ni_m_series_load_channelgain_list(comedi_device *dev,unsigned int n_chan,
1412 unsigned int chan, range, aref;
1414 unsigned config_bits = 0;
1416 unsigned int dither;
1417 unsigned int use_alt_src;
1418 unsigned range_code;
1420 devpriv->stc_writew(dev, 1, Configuration_Memory_Clear);
1422 // offset = 1 << (boardtype.adbits - 1);
1424 for(i = 0; i < n_chan; i++)
1426 chan = CR_CHAN(list[i]);
1427 aref = CR_AREF(list[i]);
1428 range = CR_RANGE(list[i]);
1429 dither = ((list[i] & CR_ALT_FILTER) != 0);
1430 use_alt_src = ((list[i] & CR_ALT_SOURCE) != 0);
1432 range_code = ni_gainlkup[boardtype.gainlkup][range];
1433 devpriv->ai_offset[i] = offset;
1437 unsigned bypass_bits = MSeries_AI_Bypass_Config_FIFO_Bit;
1438 bypass_bits |= chan;
1439 bypass_bits |= (devpriv->ai_calib_source) & (MSeries_AI_Bypass_Cal_Sel_Pos_Mask |
1440 MSeries_AI_Bypass_Cal_Sel_Neg_Mask | MSeries_AI_Bypass_Mode_Mux_Mask |
1441 MSeries_AO_Bypass_AO_Cal_Sel_Mask);
1442 bypass_bits |= MSeries_AI_Bypass_Gain_Bits(range_code);
1444 bypass_bits |= MSeries_AI_Bypass_Dither_Bit;
1445 // don't use 2's complement encoding
1446 bypass_bits |= MSeries_AI_Bypass_Polarity_Bit;
1447 ni_writel(bypass_bits, M_Offset_AI_Config_FIFO_Bypass);
1450 ni_writel(0, M_Offset_AI_Config_FIFO_Bypass);
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);
1475 ni_prime_channelgain_list(dev);
1479 * Notes on the 6110 and 6111:
1480 * These boards a slightly different than the rest of the series, since
1481 * they have multiple A/D converters.
1482 * From the driver side, the configuration memory is a
1484 * Configuration Memory Low:
1486 * bit 8: unipolar/bipolar (should be 0 for bipolar)
1487 * bits 0-3: gain. This is 4 bits instead of 3 for the other boards
1488 * 1001 gain=0.1 (+/- 50)
1497 * Configuration Memory High:
1498 * bits 12-14: Channel Type
1499 * 001 for differential
1500 * 000 for calibration
1501 * bit 11: coupling (this is not currently handled)
1505 * valid channels are 0-3
1507 static void ni_load_channelgain_list(comedi_device *dev,unsigned int n_chan,
1510 unsigned int chan,range,aref;
1514 unsigned int dither;
1516 if(boardtype.reg_type == ni_reg_m_series)
1518 ni_m_series_load_channelgain_list(dev, n_chan, list);
1521 if(n_chan == 1 && (boardtype.reg_type != ni_reg_611x) && (boardtype.reg_type != ni_reg_6143)){
1522 if(devpriv->changain_state && devpriv->changain_spec==list[0]){
1526 devpriv->changain_state=1;
1527 devpriv->changain_spec=list[0];
1529 devpriv->changain_state=0;
1532 devpriv->stc_writew(dev, 1,Configuration_Memory_Clear);
1534 // Set up Calibration mode if required
1535 if(boardtype.reg_type == ni_reg_6143){
1536 if((list[0] & CR_ALT_SOURCE) && !devpriv->ai_calib_source_enabled){
1537 // Strobe Relay enable bit
1538 ni_writew(devpriv->ai_calib_source | Calibration_Channel_6143_RelayOn, Calibration_Channel_6143);
1539 ni_writew(devpriv->ai_calib_source, Calibration_Channel_6143);
1540 devpriv->ai_calib_source_enabled = 1;
1541 msleep_interruptible(100); // Allow relays to change
1543 else if(!(list[0] & CR_ALT_SOURCE) && devpriv->ai_calib_source_enabled){
1544 // Strobe Relay disable bit
1545 ni_writew(devpriv->ai_calib_source | Calibration_Channel_6143_RelayOff, Calibration_Channel_6143);
1546 ni_writew(devpriv->ai_calib_source, Calibration_Channel_6143);
1547 devpriv->ai_calib_source_enabled = 0;
1548 msleep_interruptible(100); // Allow relays to change
1552 offset=1<<(boardtype.adbits-1);
1553 for(i=0;i<n_chan;i++){
1554 if((boardtype.reg_type != ni_reg_6143) && (list[i] & CR_ALT_SOURCE)){
1555 chan=devpriv->ai_calib_source;
1557 chan=CR_CHAN(list[i]);
1559 aref=CR_AREF(list[i]);
1560 range=CR_RANGE(list[i]);
1561 dither=((list[i]&CR_ALT_FILTER)!=0);
1563 /* fix the external/internal range differences */
1564 range = ni_gainlkup[boardtype.gainlkup][range];
1565 if(boardtype.reg_type == ni_reg_611x)
1566 devpriv->ai_offset[i] = offset;
1568 devpriv->ai_offset[i] = (range&0x100)?0:offset;
1571 if( ( list[i] & CR_ALT_SOURCE ) )
1573 if(boardtype.reg_type == ni_reg_611x)
1574 ni_writew(CR_CHAN(list[i])&0x0003, Calibration_Channel_Select_611x);
1577 if(boardtype.reg_type == ni_reg_611x)
1579 else if(boardtype.reg_type == ni_reg_6143)
1584 hi |= AI_DIFFERENTIAL;
1596 hi |= AI_CONFIG_CHANNEL( chan );
1598 ni_writew(hi,Configuration_Memory_High);
1600 if(boardtype.reg_type != ni_reg_6143){
1602 if(i == n_chan - 1) lo |= AI_LAST_CHANNEL;
1603 if( dither ) lo |= AI_DITHER;
1605 ni_writew(lo,Configuration_Memory_Low);
1609 /* prime the channel/gain list */
1610 if((boardtype.reg_type != ni_reg_611x) && (boardtype.reg_type != ni_reg_6143)){
1611 ni_prime_channelgain_list(dev);
1615 #define TIMER_BASE 50 /* 20 Mhz base */
1617 static int ni_ns_to_timer(int *nanosec,int round_mode)
1624 case TRIG_ROUND_NEAREST:
1626 divider=(*nanosec+base/2)/base;
1628 case TRIG_ROUND_DOWN:
1629 divider=(*nanosec)/base;
1632 divider=(*nanosec+base-1)/base;
1636 *nanosec=base*divider;
1640 static int ni_ai_cmdtest(comedi_device *dev,comedi_subdevice *s,comedi_cmd *cmd)
1646 /* step 1: make sure trigger sources are trivially valid */
1649 cmd->start_src &= TRIG_NOW|TRIG_INT|TRIG_EXT;
1650 if(!cmd->start_src || tmp!=cmd->start_src)err++;
1652 tmp=cmd->scan_begin_src;
1653 cmd->scan_begin_src &= TRIG_TIMER|TRIG_EXT;
1654 if(!cmd->scan_begin_src || tmp!=cmd->scan_begin_src)err++;
1656 tmp=cmd->convert_src;
1657 sources = TRIG_TIMER | TRIG_EXT;
1658 if((boardtype.reg_type == ni_reg_611x) || (boardtype.reg_type == ni_reg_6143)) sources |= TRIG_NOW;
1659 cmd->convert_src &= sources;
1660 if(!cmd->convert_src || tmp!=cmd->convert_src)err++;
1662 tmp=cmd->scan_end_src;
1663 cmd->scan_end_src &= TRIG_COUNT;
1664 if(!cmd->scan_end_src || tmp!=cmd->scan_end_src)err++;
1667 cmd->stop_src &= TRIG_COUNT|TRIG_NONE;
1668 if(!cmd->stop_src || tmp!=cmd->stop_src)err++;
1672 /* step 2: make sure trigger sources are unique and mutually compatible */
1674 /* note that mutual compatiblity is not an issue here */
1675 if(cmd->start_src!=TRIG_NOW &&
1676 cmd->start_src!=TRIG_INT &&
1677 cmd->start_src!=TRIG_EXT)err++;
1678 if(cmd->scan_begin_src!=TRIG_TIMER &&
1679 cmd->scan_begin_src!=TRIG_EXT &&
1680 cmd->scan_begin_src!=TRIG_OTHER)err++;
1681 if(cmd->convert_src!=TRIG_TIMER &&
1682 cmd->convert_src!=TRIG_EXT &&
1683 cmd->convert_src!=TRIG_NOW)err++;
1684 if(cmd->stop_src!=TRIG_COUNT &&
1685 cmd->stop_src!=TRIG_NONE)err++;
1689 /* step 3: make sure arguments are trivially compatible */
1691 if(cmd->start_src==TRIG_EXT){
1692 /* external trigger */
1693 unsigned int tmp = CR_CHAN(cmd->start_arg);
1695 if(tmp > 16) tmp = 16;
1696 tmp |= (cmd->start_arg & (CR_INVERT | CR_EDGE));
1697 if(cmd->start_arg != tmp){
1698 cmd->start_arg = tmp;
1702 if(cmd->start_arg!=0){
1703 /* true for both TRIG_NOW and TRIG_INT */
1708 if(cmd->scan_begin_src==TRIG_TIMER){
1709 if(cmd->scan_begin_arg<boardtype.ai_speed){
1710 cmd->scan_begin_arg=boardtype.ai_speed;
1713 if(cmd->scan_begin_arg>TIMER_BASE*0xffffff){
1714 cmd->scan_begin_arg=TIMER_BASE*0xffffff;
1717 }else if(cmd->scan_begin_src==TRIG_EXT){
1718 /* external trigger */
1719 unsigned int tmp = CR_CHAN(cmd->scan_begin_arg);
1722 tmp |= (cmd->scan_begin_arg & (CR_INVERT | CR_EDGE));
1723 if(cmd->scan_begin_arg!=tmp){
1724 cmd->scan_begin_arg = tmp;
1727 }else{ /* TRIG_OTHER */
1728 if(cmd->scan_begin_arg){
1729 cmd->scan_begin_arg=0;
1733 if(cmd->convert_src==TRIG_TIMER){
1734 if((boardtype.reg_type == ni_reg_611x) || (boardtype.reg_type == ni_reg_6143)){
1735 if(cmd->convert_arg != 0){
1736 cmd->convert_arg = 0;
1740 if(cmd->convert_arg<boardtype.ai_speed){
1741 cmd->convert_arg=boardtype.ai_speed;
1744 if(cmd->convert_arg>TIMER_BASE*0xffff){
1745 cmd->convert_arg=TIMER_BASE*0xffff;
1749 }else if(cmd->convert_src == TRIG_EXT){
1750 /* external trigger */
1751 unsigned int tmp = CR_CHAN(cmd->convert_arg);
1754 tmp |= (cmd->convert_arg&(CR_ALT_FILTER|CR_INVERT));
1755 if(cmd->convert_arg!=tmp){
1756 cmd->convert_arg = tmp;
1759 }else if(cmd->convert_src == TRIG_NOW){
1760 if(cmd->convert_arg != 0){
1761 cmd->convert_arg = 0;
1766 if(cmd->scan_end_arg!=cmd->chanlist_len){
1767 cmd->scan_end_arg=cmd->chanlist_len;
1770 if(cmd->stop_src==TRIG_COUNT){
1771 unsigned int max_count = 0x01000000;
1773 if(boardtype.reg_type == ni_reg_611x )
1774 max_count -= num_adc_stages_611x;
1775 if(cmd->stop_arg > max_count){
1776 cmd->stop_arg = max_count;
1779 if(cmd->stop_arg < 1){
1785 if(cmd->stop_arg!=0){
1793 /* step 4: fix up any arguments */
1795 if(cmd->scan_begin_src==TRIG_TIMER){
1796 tmp=cmd->scan_begin_arg;
1797 ni_ns_to_timer(&cmd->scan_begin_arg,cmd->flags&TRIG_ROUND_MASK);
1798 if(tmp!=cmd->scan_begin_arg)err++;
1800 if(cmd->convert_src==TRIG_TIMER){
1801 if((boardtype.reg_type != ni_reg_611x) && (boardtype.reg_type != ni_reg_6143)){
1802 tmp=cmd->convert_arg;
1803 ni_ns_to_timer(&cmd->convert_arg,cmd->flags&TRIG_ROUND_MASK);
1804 if(tmp!=cmd->convert_arg)err++;
1805 if(cmd->scan_begin_src==TRIG_TIMER &&
1806 cmd->scan_begin_arg<cmd->convert_arg*cmd->scan_end_arg){
1807 cmd->scan_begin_arg=cmd->convert_arg*cmd->scan_end_arg;
1818 static int ni_ai_cmd(comedi_device *dev,comedi_subdevice *s)
1820 comedi_cmd *cmd=&s->async->cmd;
1822 int mode1=0; /* mode1 is needed for both stop and convert */
1824 int start_stop_select=0;
1825 unsigned int stop_count;
1826 int interrupt_a_enable=0;
1828 MDPRINTK("ni_ai_cmd\n");
1831 comedi_error(dev, "cannot run command without an irq");
1834 ni_flush_ai_fifo(dev);
1836 ni_load_channelgain_list(dev,cmd->chanlist_len,cmd->chanlist);
1838 /* start configuration */
1839 devpriv->stc_writew(dev, AI_Configuration_Start,Joint_Reset_Register);
1841 /* disable analog triggering for now, since it
1842 * interferes with the use of pfi0 */
1843 devpriv->an_trig_etc_reg &= ~Analog_Trigger_Enable;
1844 devpriv->stc_writew(dev, devpriv->an_trig_etc_reg, Analog_Trigger_Etc_Register);
1846 switch(cmd->start_src){
1849 devpriv->stc_writew(dev, AI_START2_Select(0)|
1850 AI_START1_Sync|AI_START1_Edge|AI_START1_Select(0),
1851 AI_Trigger_Select_Register);
1855 int chan = CR_CHAN(cmd->start_arg);
1856 unsigned int bits = AI_START2_Select(0)|
1858 AI_START1_Select(chan + 1);
1860 if(cmd->start_arg & CR_INVERT)
1861 bits |= AI_START1_Polarity;
1862 if(cmd->start_arg & CR_EDGE)
1863 bits |= AI_START1_Edge;
1864 devpriv->stc_writew(dev, bits, AI_Trigger_Select_Register);
1869 mode2 &= ~AI_Pre_Trigger;
1870 mode2 &= ~AI_SC_Initial_Load_Source;
1871 mode2 &= ~AI_SC_Reload_Mode;
1872 devpriv->stc_writew(dev, mode2, AI_Mode_2_Register);
1874 if(cmd->chanlist_len == 1 || (boardtype.reg_type == ni_reg_611x) || (boardtype.reg_type == ni_reg_6143)){
1875 start_stop_select |= AI_STOP_Polarity;
1876 start_stop_select |= AI_STOP_Select( 31 ); // logic low
1877 start_stop_select |= AI_STOP_Sync;
1880 start_stop_select |= AI_STOP_Select(19); // ai configuration memory
1882 devpriv->stc_writew(dev, start_stop_select, AI_START_STOP_Select_Register);
1884 devpriv->ai_cmd2 = 0;
1885 switch(cmd->stop_src){
1887 stop_count = cmd->stop_arg - 1;
1889 if(boardtype.reg_type == ni_reg_611x){
1890 // have to take 3 stage adc pipeline into account
1891 stop_count += num_adc_stages_611x;
1893 /* stage number of scans */
1894 devpriv->stc_writel(dev, stop_count, AI_SC_Load_A_Registers);
1896 mode1 |= AI_Start_Stop | AI_Mode_1_Reserved | AI_Trigger_Once;
1897 devpriv->stc_writew(dev, mode1,AI_Mode_1_Register);
1898 /* load SC (Scan Count) */
1899 devpriv->stc_writew(dev, AI_SC_Load,AI_Command_1_Register);
1901 devpriv->ai_continuous = 0;
1902 if( stop_count == 0 ){
1903 devpriv->ai_cmd2 |= AI_End_On_End_Of_Scan;
1904 interrupt_a_enable |= AI_STOP_Interrupt_Enable;
1905 // this is required to get the last sample for chanlist_len > 1, not sure why
1906 if(cmd->chanlist_len > 1)
1907 start_stop_select |= AI_STOP_Polarity | AI_STOP_Edge;
1911 /* stage number of scans */
1912 devpriv->stc_writel(dev, 0,AI_SC_Load_A_Registers);
1914 mode1 |= AI_Start_Stop | AI_Mode_1_Reserved | AI_Continuous;
1915 devpriv->stc_writew(dev, mode1,AI_Mode_1_Register);
1917 /* load SC (Scan Count) */
1918 devpriv->stc_writew(dev, AI_SC_Load,AI_Command_1_Register);
1920 devpriv->ai_continuous = 1;
1925 switch(cmd->scan_begin_src){
1928 stop bits for non 611x boards
1929 AI_SI_Special_Trigger_Delay=0
1931 AI_START_STOP_Select_Register:
1932 AI_START_Polarity=0 (?) rising edge
1933 AI_START_Edge=1 edge triggered
1935 AI_START_Select=0 SI_TC
1936 AI_STOP_Polarity=0 rising edge
1937 AI_STOP_Edge=0 level
1939 AI_STOP_Select=19 external pin (configuration mem)
1941 start_stop_select |= AI_START_Edge | AI_START_Sync;
1942 devpriv->stc_writew(dev, start_stop_select, AI_START_STOP_Select_Register);
1944 mode2 |= AI_SI_Reload_Mode(0);
1945 /* AI_SI_Initial_Load_Source=A */
1946 mode2 &= ~AI_SI_Initial_Load_Source;
1947 //mode2 |= AI_SC_Reload_Mode;
1948 devpriv->stc_writew(dev, mode2, AI_Mode_2_Register);
1951 timer=ni_ns_to_timer(&cmd->scan_begin_arg,TRIG_ROUND_NEAREST);
1952 devpriv->stc_writel(dev, timer,AI_SI_Load_A_Registers);
1953 devpriv->stc_writew(dev, AI_SI_Load,AI_Command_1_Register);
1956 if( cmd->scan_begin_arg & CR_EDGE )
1957 start_stop_select |= AI_START_Edge;
1958 /* AI_START_Polarity==1 is falling edge */
1959 if( cmd->scan_begin_arg & CR_INVERT )
1960 start_stop_select |= AI_START_Polarity;
1961 if( cmd->scan_begin_src != cmd->convert_src ||
1962 ( cmd->scan_begin_arg & ~CR_EDGE ) != ( cmd->convert_arg & ~CR_EDGE ) )
1963 start_stop_select |= AI_START_Sync;
1964 start_stop_select |= AI_START_Select(1 + CR_CHAN(cmd->scan_begin_arg));
1965 devpriv->stc_writew(dev, start_stop_select, AI_START_STOP_Select_Register);
1969 switch(cmd->convert_src){
1972 if( cmd->convert_arg == 0 || cmd->convert_src == TRIG_NOW )
1975 timer=ni_ns_to_timer(&cmd->convert_arg, TRIG_ROUND_NEAREST);
1976 devpriv->stc_writew(dev, 1,AI_SI2_Load_A_Register); /* 0,0 does not work. */
1977 devpriv->stc_writew(dev, timer,AI_SI2_Load_B_Register);
1979 /* AI_SI2_Reload_Mode = alternate */
1980 /* AI_SI2_Initial_Load_Source = A */
1981 mode2 &= ~AI_SI2_Initial_Load_Source;
1982 mode2 |= AI_SI2_Reload_Mode;
1983 devpriv->stc_writew(dev, mode2, AI_Mode_2_Register);
1986 devpriv->stc_writew(dev, AI_SI2_Load,AI_Command_1_Register);
1988 mode2 |= AI_SI2_Reload_Mode; // alternate
1989 mode2 |= AI_SI2_Initial_Load_Source; // B
1991 devpriv->stc_writew(dev, mode2,AI_Mode_2_Register);
1994 mode1 |= AI_CONVERT_Source_Select(1+cmd->convert_arg);
1995 if( ( cmd->convert_arg & CR_INVERT ) == 0 )
1996 mode1 |= AI_CONVERT_Source_Polarity;
1997 devpriv->stc_writew(dev, mode1,AI_Mode_1_Register);
1999 mode2 |= AI_Start_Stop_Gate_Enable | AI_SC_Gate_Enable;
2000 devpriv->stc_writew(dev, mode2, AI_Mode_2_Register);
2007 /* interrupt on FIFO, errors, SC_TC */
2008 interrupt_a_enable |= AI_Error_Interrupt_Enable|
2009 AI_SC_TC_Interrupt_Enable;
2012 interrupt_a_enable|=AI_FIFO_Interrupt_Enable;
2015 if(cmd->flags & TRIG_WAKE_EOS || (devpriv->ai_cmd2 & AI_End_On_End_Of_Scan)){
2016 /* wake on end-of-scan */
2017 devpriv->aimode=AIMODE_SCAN;
2019 devpriv->aimode=AIMODE_HALF_FULL;
2022 switch(devpriv->aimode){
2023 case AIMODE_HALF_FULL:
2024 /*generate FIFO interrupts and DMA requests on half-full */
2026 devpriv->stc_writew(dev, AI_FIFO_Mode_HF_to_E, AI_Mode_3_Register);
2028 devpriv->stc_writew(dev, AI_FIFO_Mode_HF, AI_Mode_3_Register);
2032 /*generate FIFO interrupts on non-empty */
2033 devpriv->stc_writew(dev, AI_FIFO_Mode_NE, AI_Mode_3_Register);
2037 devpriv->stc_writew(dev, AI_FIFO_Mode_NE, AI_Mode_3_Register);
2039 devpriv->stc_writew(dev, AI_FIFO_Mode_HF, AI_Mode_3_Register);
2041 interrupt_a_enable |= AI_STOP_Interrupt_Enable;
2047 devpriv->stc_writew(dev, 0x3f80,Interrupt_A_Ack_Register); /* clear interrupts */
2049 ni_set_bits(dev, Interrupt_A_Enable_Register, interrupt_a_enable, 1);
2051 MDPRINTK("Interrupt_A_Enable_Register = 0x%04x\n",devpriv->int_a_enable_reg);
2053 /* interrupt on nothing */
2054 ni_set_bits(dev, Interrupt_A_Enable_Register, ~0, 0);
2056 /* XXX start polling if necessary */
2057 MDPRINTK("interrupting on nothing\n");
2060 /* end configuration */
2061 devpriv->stc_writew(dev, AI_Configuration_End,Joint_Reset_Register);
2063 switch(cmd->scan_begin_src){
2065 devpriv->stc_writew(dev, AI_SI2_Arm | AI_SI_Arm | AI_DIV_Arm | AI_SC_Arm,
2066 AI_Command_1_Register);
2069 /* XXX AI_SI_Arm? */
2070 devpriv->stc_writew(dev, AI_SI2_Arm | AI_SI_Arm | AI_DIV_Arm | AI_SC_Arm,
2071 AI_Command_1_Register);
2076 ni_ai_setup_MITE_dma(dev,cmd);
2077 //mite_dump_regs(devpriv->mite);
2080 switch(cmd->start_src){
2082 /* AI_START1_Pulse */
2083 devpriv->stc_writew(dev, AI_START1_Pulse | devpriv->ai_cmd2, AI_Command_2_Register );
2084 s->async->inttrig=NULL;
2087 s->async->inttrig=NULL;
2090 s->async->inttrig=ni_ai_inttrig;
2094 MDPRINTK("exit ni_ai_cmd\n");
2099 static int ni_ai_inttrig(comedi_device *dev,comedi_subdevice *s,
2100 unsigned int trignum)
2102 if(trignum!=0)return -EINVAL;
2104 devpriv->stc_writew(dev, AI_START1_Pulse | devpriv->ai_cmd2, AI_Command_2_Register );
2105 s->async->inttrig=NULL;
2110 static int ni_ai_config_analog_trig(comedi_device *dev,comedi_subdevice *s,
2111 comedi_insn *insn, lsampl_t *data);
2113 static int ni_ai_insn_config(comedi_device *dev,comedi_subdevice *s,
2114 comedi_insn *insn, lsampl_t *data)
2116 if(insn->n<1)return -EINVAL;
2119 case INSN_CONFIG_ANALOG_TRIG:
2120 return ni_ai_config_analog_trig(dev,s,insn,data);
2121 case INSN_CONFIG_ALT_SOURCE:
2122 if(boardtype.reg_type == ni_reg_m_series)
2124 if(data[1] & ~(MSeries_AI_Bypass_Cal_Sel_Pos_Mask |
2125 MSeries_AI_Bypass_Cal_Sel_Neg_Mask | MSeries_AI_Bypass_Mode_Mux_Mask |
2126 MSeries_AO_Bypass_AO_Cal_Sel_Mask))
2130 devpriv->ai_calib_source = data[1];
2131 } else if(boardtype.reg_type == ni_reg_6143)
2133 unsigned int calib_source;
2135 calib_source = data[1] & 0xf;
2138 if(calib_source > 0xF)
2141 devpriv->ai_calib_source = calib_source;
2142 ni_writew(calib_source, Calibration_Channel_6143);
2145 unsigned int calib_source;
2146 unsigned int calib_source_adjust;
2148 calib_source = data[1] & 0xf;
2149 calib_source_adjust = ( data[1] >> 4 ) & 0xff;
2151 if(calib_source >= 8)
2153 devpriv->ai_calib_source = calib_source;
2154 if(boardtype.reg_type == ni_reg_611x){
2155 ni_writeb( calib_source_adjust, Cal_Gain_Select_611x );
2166 static int ni_ai_config_analog_trig(comedi_device *dev,comedi_subdevice *s,
2167 comedi_insn *insn, lsampl_t *data)
2169 unsigned int a,b,modebits;
2173 * data[2] is analog line
2174 * data[3] is set level
2175 * data[4] is reset level */
2176 if(!boardtype.has_analog_trig)return -EINVAL;
2177 if(insn->n!=5)return -EINVAL;
2178 if((data[1]&0xffff0000) != COMEDI_EV_SCAN_BEGIN){
2179 data[1]&=~(COMEDI_EV_SCAN_BEGIN&0xffff);
2182 if(data[2]>=boardtype.n_adchan){
2183 data[2]=boardtype.n_adchan-1;
2186 if(data[3]>255){ /* a */
2190 if(data[4]>255){ /* b */
2201 * high mode 00 00 01 10
2202 * low mode 00 00 10 01
2204 * hysteresis low mode 10 00 00 01
2205 * hysteresis high mode 01 00 00 10
2206 * middle mode 10 01 01 10
2211 modebits=data[1]&0xff;
2213 /* two level mode */
2218 modebits=((data[1]&0xf)<<4)|((data[1]&0xf0)>>4);
2220 devpriv->atrig_low = a;
2221 devpriv->atrig_high = b;
2223 case 0x81: /* low hysteresis mode */
2224 devpriv->atrig_mode = 6;
2226 case 0x42: /* high hysteresis mode */
2227 devpriv->atrig_mode = 3;
2229 case 0x96: /* middle window mode */
2230 devpriv->atrig_mode = 2;
2237 /* one level mode */
2243 case 0x06: /* high window mode */
2244 devpriv->atrig_high = a;
2245 devpriv->atrig_mode = 0;
2247 case 0x09: /* low window mode */
2248 devpriv->atrig_low = a;
2249 devpriv->atrig_mode = 1;
2256 if(err)return -EAGAIN;
2260 /* munge data from unsigned to 2's complement for analog output bipolar modes */
2261 static void ni_ao_munge(comedi_device *dev, comedi_subdevice *s,
2262 void *data, unsigned int num_bytes, unsigned int chan_index )
2264 comedi_async *async = s->async;
2267 unsigned int offset;
2268 unsigned int length = num_bytes / sizeof( sampl_t );
2269 sampl_t *array = data;
2271 offset = 1 << (boardtype.aobits - 1);
2272 for(i = 0; i < length; i++)
2274 range = CR_RANGE( async->cmd.chanlist[ chan_index ] );
2275 if(boardtype.ao_unipolar == 0 || (range & 1) == 0 )
2278 array[i] = cpu_to_le16( array[i] );
2281 chan_index %= async->cmd.chanlist_len;
2285 static int ni_ao_config_chanlist(comedi_device *dev, comedi_subdevice *s,
2286 unsigned int chanspec[], unsigned int n_chans)
2294 for(i=0;i<n_chans;i++){
2295 chan = CR_CHAN(chanspec[i]);
2296 range = CR_RANGE(chanspec[i]);
2297 if(boardtype.reg_type == ni_reg_m_series)
2299 comedi_krange *krange = s->range_table->range + range;
2302 switch(krange->max - krange->min)
2305 conf |= MSeries_AO_DAC_Reference_10V_Internal_Bits;
2306 ni_writeb(0, M_Offset_AO_Reference_Attenuation(chan));
2309 conf |= MSeries_AO_DAC_Reference_5V_Internal_Bits;
2310 ni_writeb(0, M_Offset_AO_Reference_Attenuation(chan));
2313 conf |= MSeries_AO_DAC_Reference_10V_Internal_Bits;
2314 ni_writeb(MSeries_Attenuate_x5_Bit, M_Offset_AO_Reference_Attenuation(chan));
2317 conf |= MSeries_AO_DAC_Reference_5V_Internal_Bits;
2318 ni_writeb(MSeries_Attenuate_x5_Bit, M_Offset_AO_Reference_Attenuation(chan));
2321 rt_printk("%s: bug! unhandled ao reference voltage\n", __FUNCTION__);
2324 switch(krange->max + krange->min)
2327 conf |= MSeries_AO_DAC_Offset_0V_Bits;
2330 conf |= MSeries_AO_DAC_Offset_5V_Bits;
2333 rt_printk("%s: bug! unhandled ao offset voltage\n", __FUNCTION__);
2336 ni_writeb(conf, M_Offset_AO_Config_Bank(chan));
2339 conf = AO_Channel(chan);
2341 if(boardtype.ao_unipolar){
2344 invert = (1<<(boardtype.aobits-1));
2352 invert = (1<<(boardtype.aobits-1));
2355 /* not all boards can deglitch, but this shouldn't hurt */
2356 if(chanspec[i] & CR_DEGLITCH)
2357 conf |= AO_Deglitch;
2359 /* analog reference */
2360 /* AREF_OTHER connects AO ground to AI ground, i think */
2361 conf |= (CR_AREF(chanspec[i])==AREF_OTHER)? AO_Ground_Ref : 0;
2363 ni_writew(conf,AO_Configuration);
2365 devpriv->ao_conf[chan] = conf;
2370 static int ni_ao_insn_read(comedi_device *dev,comedi_subdevice *s,
2371 comedi_insn *insn,lsampl_t *data)
2373 data[0] = devpriv->ao[CR_CHAN(insn->chanspec)];
2378 static int ni_ao_insn_write(comedi_device *dev,comedi_subdevice *s,
2379 comedi_insn *insn,lsampl_t *data)
2381 unsigned int chan = CR_CHAN(insn->chanspec);
2382 unsigned int invert;
2384 invert = ni_ao_config_chanlist(dev,s,&insn->chanspec,1);
2386 devpriv->ao[chan] = data[0];
2388 if(boardtype.reg_type == ni_reg_m_series)
2390 ni_writew(data[0], M_Offset_DAC_Direct_Data(chan));
2393 ni_writew(data[0] ^ invert,(chan)? DAC1_Direct_Data : DAC0_Direct_Data);
2398 static int ni_ao_insn_write_671x(comedi_device *dev,comedi_subdevice *s,
2399 comedi_insn *insn,lsampl_t *data)
2401 unsigned int chan = CR_CHAN(insn->chanspec);
2402 unsigned int invert;
2404 ao_win_out(1 << chan, AO_Immediate_671x);
2405 invert = 1 << (boardtype.aobits - 1);
2407 ni_ao_config_chanlist(dev,s,&insn->chanspec,1);
2409 devpriv->ao[chan] = data[0];
2410 ao_win_out(data[0] ^ invert, DACx_Direct_Data_671x(chan));
2415 static int ni_ao_inttrig(comedi_device *dev,comedi_subdevice *s,
2416 unsigned int trignum)
2419 int interrupt_b_bits;
2421 static const int timeout = 1000;
2423 if(trignum!=0)return -EINVAL;
2425 ni_set_bits(dev, Interrupt_B_Enable_Register, AO_FIFO_Interrupt_Enable | AO_Error_Interrupt_Enable, 0);
2426 interrupt_b_bits = AO_Error_Interrupt_Enable;
2428 devpriv->stc_writew(dev, 1, DAC_FIFO_Clear);
2429 if(boardtype.reg_type & ni_reg_6xxx_mask)
2430 ni_ao_win_outl(dev, 0x6, AO_FIFO_Offset_Load_611x);
2431 ni_ao_setup_MITE_dma(dev, &s->async->cmd);
2432 ret = ni_ao_wait_for_dma_load(dev);
2433 if(ret < 0) return ret;
2436 ret = ni_ao_prep_fifo(dev,s);
2437 if(ret==0)return -EPIPE;
2439 interrupt_b_bits |= AO_FIFO_Interrupt_Enable;
2442 devpriv->stc_writew(dev, devpriv->ao_mode3|AO_Not_An_UPDATE,AO_Mode_3_Register);
2443 devpriv->stc_writew(dev, devpriv->ao_mode3,AO_Mode_3_Register);
2444 /* wait for DACs to be loaded */
2445 for(i = 0; i < timeout; i++)
2448 if((devpriv->stc_readw(dev, Joint_Status_2_Register) & AO_TMRDACWRs_In_Progress_St) == 0)
2453 comedi_error(dev, "timed out waiting for AO_TMRDACWRs_In_Progress_St to clear");
2456 // stc manual says we are need to clear error interrupt after AO_TMRDACWRs_In_Progress_St clears
2457 devpriv->stc_writew(dev, AO_Error_Interrupt_Ack, Interrupt_B_Ack_Register);
2459 ni_set_bits(dev, Interrupt_B_Enable_Register, interrupt_b_bits, 1);
2461 devpriv->stc_writew(dev, devpriv->ao_cmd1|AO_UI_Arm|AO_UC_Arm|AO_BC_Arm|AO_DAC1_Update_Mode|AO_DAC0_Update_Mode,
2462 AO_Command_1_Register);
2464 devpriv->stc_writew(dev, devpriv->ao_cmd2|AO_START1_Pulse,AO_Command_2_Register);
2466 s->async->inttrig=NULL;
2471 static int ni_ao_cmd(comedi_device *dev,comedi_subdevice *s)
2473 comedi_cmd *cmd = &s->async->cmd;
2480 comedi_error(dev, "cannot run command without an irq");
2483 trigvar = ni_ns_to_timer(&cmd->scan_begin_arg,TRIG_ROUND_NEAREST);
2485 devpriv->stc_writew(dev, AO_Configuration_Start,Joint_Reset_Register);
2487 devpriv->stc_writew(dev, AO_Disarm,AO_Command_1_Register);
2489 if(boardtype.reg_type & ni_reg_6xxx_mask)
2491 ao_win_out(CLEAR_WG, AO_Misc_611x);
2494 for(i = 0; i < cmd->chanlist_len; i++)
2498 chan = CR_CHAN(cmd->chanlist[i]);
2500 ao_win_out(chan, AO_Waveform_Generation_611x);
2502 ao_win_out(bits, AO_Timed_611x);
2505 ni_ao_config_chanlist(dev,s,cmd->chanlist,cmd->chanlist_len);
2507 if(cmd->stop_src==TRIG_NONE){
2508 devpriv->ao_mode1|=AO_Continuous;
2509 devpriv->ao_mode1&=~AO_Trigger_Once;
2511 devpriv->ao_mode1&=~AO_Continuous;
2512 devpriv->ao_mode1|=AO_Trigger_Once;
2514 devpriv->stc_writew(dev, devpriv->ao_mode1,AO_Mode_1_Register);
2515 devpriv->ao_trigger_select&=~(AO_START1_Polarity|AO_START1_Select(-1));
2516 devpriv->ao_trigger_select|=AO_START1_Edge|AO_START1_Sync;
2517 devpriv->stc_writew(dev, devpriv->ao_trigger_select,AO_Trigger_Select_Register);
2518 devpriv->ao_mode3&=~AO_Trigger_Length;
2519 devpriv->stc_writew(dev, devpriv->ao_mode3,AO_Mode_3_Register);
2521 devpriv->stc_writew(dev, devpriv->ao_mode1,AO_Mode_1_Register);
2522 devpriv->ao_mode2&=~AO_BC_Initial_Load_Source;
2523 devpriv->stc_writew(dev, devpriv->ao_mode2,AO_Mode_2_Register);
2524 if(cmd->stop_src==TRIG_NONE){
2525 devpriv->stc_writel(dev, 0xffffff,AO_BC_Load_A_Register);
2527 devpriv->stc_writel(dev, 0,AO_BC_Load_A_Register);
2529 devpriv->stc_writew(dev, AO_BC_Load,AO_Command_1_Register);
2530 devpriv->ao_mode2&=~AO_UC_Initial_Load_Source;
2531 devpriv->stc_writew(dev, devpriv->ao_mode2,AO_Mode_2_Register);
2532 switch(cmd->stop_src){
2534 devpriv->stc_writel(dev, cmd->stop_arg,AO_UC_Load_A_Register);
2535 devpriv->stc_writew(dev, AO_UC_Load,AO_Command_1_Register);
2536 devpriv->stc_writel(dev, cmd->stop_arg - 1,AO_UC_Load_A_Register);
2539 devpriv->stc_writel(dev, 0xffffff,AO_UC_Load_A_Register);
2540 devpriv->stc_writew(dev, AO_UC_Load,AO_Command_1_Register);
2541 devpriv->stc_writel(dev, 0xffffff,AO_UC_Load_A_Register);
2544 devpriv->stc_writel(dev, 0,AO_UC_Load_A_Register);
2545 devpriv->stc_writew(dev, AO_UC_Load,AO_Command_1_Register);
2546 devpriv->stc_writel(dev, cmd->stop_arg,AO_UC_Load_A_Register);
2549 devpriv->ao_cmd2&=~AO_BC_Gate_Enable;
2550 devpriv->stc_writew(dev, devpriv->ao_cmd2,AO_Command_2_Register);
2551 devpriv->ao_mode1&=~(AO_UI_Source_Select(0x1f)|AO_UI_Source_Polarity);
2552 devpriv->stc_writew(dev, devpriv->ao_mode1,AO_Mode_1_Register);
2553 devpriv->ao_mode2&=~(AO_UI_Reload_Mode(3)|AO_UI_Initial_Load_Source);
2554 devpriv->stc_writew(dev, devpriv->ao_mode2,AO_Mode_2_Register);
2555 devpriv->stc_writel(dev, 1,AO_UI_Load_A_Register);
2556 devpriv->stc_writew(dev, AO_UI_Load,AO_Command_1_Register);
2557 devpriv->stc_writel(dev, trigvar,AO_UI_Load_A_Register);
2559 if((boardtype.reg_type & ni_reg_6xxx_mask) == 0){
2560 if(cmd->scan_end_arg>1){
2561 devpriv->ao_mode1|=AO_Multiple_Channels;
2562 devpriv->stc_writew(dev, AO_Number_Of_Channels(cmd->scan_end_arg-1)|
2563 AO_UPDATE_Output_Select(1),
2564 AO_Output_Control_Register);
2566 devpriv->ao_mode1&=~AO_Multiple_Channels;
2567 devpriv->stc_writew(dev, AO_Number_Of_Channels(CR_CHAN(cmd->chanlist[0]))|
2568 AO_UPDATE_Output_Select(1),
2569 AO_Output_Control_Register);
2571 devpriv->stc_writew(dev, devpriv->ao_mode1,AO_Mode_1_Register);
2574 devpriv->stc_writew(dev, AO_DAC0_Update_Mode|AO_DAC1_Update_Mode,AO_Command_1_Register);
2576 devpriv->ao_mode3|=AO_Stop_On_Overrun_Error;
2577 devpriv->stc_writew(dev, devpriv->ao_mode3,AO_Mode_3_Register);
2579 devpriv->ao_mode2 &= AO_FIFO_Mode_Mask;
2581 devpriv->ao_mode2 |= AO_FIFO_Mode_HF_to_F;
2583 devpriv->ao_mode2 |= AO_FIFO_Mode_HF;
2585 devpriv->ao_mode2 &= ~AO_FIFO_Retransmit_Enable;
2586 devpriv->stc_writew(dev, devpriv->ao_mode2,AO_Mode_2_Register);
2588 bits = AO_BC_Source_Select | AO_UPDATE_Pulse_Width |
2589 AO_TMRDACWR_Pulse_Width;
2590 if( boardtype.ao_fifo_depth )
2591 bits |= AO_FIFO_Enable;
2593 bits |= AO_DMA_PIO_Control;
2594 devpriv->stc_writew(dev, bits, AO_Personal_Register);
2595 // enable sending of ao dma requests
2596 devpriv->stc_writew(dev, AO_AOFREQ_Enable, AO_Start_Select_Register);
2598 devpriv->stc_writew(dev, AO_Configuration_End,Joint_Reset_Register);
2600 if(cmd->stop_src==TRIG_COUNT) {
2601 devpriv->stc_writew(dev, AO_BC_TC_Interrupt_Ack,Interrupt_B_Ack_Register);
2602 ni_set_bits(dev, Interrupt_B_Enable_Register,
2603 AO_BC_TC_Interrupt_Enable, 1);
2606 s->async->inttrig=ni_ao_inttrig;
2611 static int ni_ao_cmdtest(comedi_device *dev,comedi_subdevice *s,comedi_cmd *cmd)
2616 /* step 1: make sure trigger sources are trivially valid */
2619 cmd->start_src &= TRIG_INT;
2620 if(!cmd->start_src || tmp!=cmd->start_src)err++;
2622 tmp=cmd->scan_begin_src;
2623 cmd->scan_begin_src &= TRIG_TIMER;
2624 if(!cmd->scan_begin_src || tmp!=cmd->scan_begin_src)err++;
2626 tmp=cmd->convert_src;
2627 cmd->convert_src &= TRIG_NOW;
2628 if(!cmd->convert_src || tmp!=cmd->convert_src)err++;
2630 tmp=cmd->scan_end_src;
2631 cmd->scan_end_src &= TRIG_COUNT;
2632 if(!cmd->scan_end_src || tmp!=cmd->scan_end_src)err++;
2635 cmd->stop_src &= TRIG_COUNT|TRIG_NONE;
2636 if(!cmd->stop_src || tmp!=cmd->stop_src)err++;
2640 /* step 2: make sure trigger sources are unique and mutually compatible */
2642 if(cmd->stop_src!=TRIG_COUNT &&
2643 cmd->stop_src!=TRIG_NONE)err++;
2647 /* step 3: make sure arguments are trivially compatible */
2649 if(cmd->start_arg!=0){
2654 /* XXX need ao_speed */
2655 if(cmd->scan_begin_arg<boardtype.ao_speed){
2656 cmd->scan_begin_arg=boardtype.ao_speed;
2660 if(cmd->scan_begin_arg>TIMER_BASE*0xffffff){ /* XXX check */
2661 cmd->scan_begin_arg=TIMER_BASE*0xffffff;
2664 if(cmd->convert_arg!=0){
2668 if(cmd->scan_end_arg!=cmd->chanlist_len){
2669 cmd->scan_end_arg=cmd->chanlist_len;
2672 if(cmd->stop_src==TRIG_COUNT){ /* XXX check */
2673 if(cmd->stop_arg>0x00ffffff){
2674 cmd->stop_arg=0x00ffffff;
2679 if(cmd->stop_arg!=0){
2687 /* step 4: fix up any arguments */
2689 tmp = cmd->scan_begin_arg;
2690 ni_ns_to_timer(&cmd->scan_begin_arg,cmd->flags&TRIG_ROUND_MASK);
2691 if(tmp!=cmd->scan_begin_arg)err++;
2695 /* step 5: fix up chanlist */
2703 static int ni_ao_reset(comedi_device *dev,comedi_subdevice *s)
2705 //devpriv->ao0p=0x0000;
2706 //ni_writew(devpriv->ao0p,AO_Configuration);
2708 //devpriv->ao1p=AO_Channel(1);
2709 //ni_writew(devpriv->ao1p,AO_Configuration);
2712 mite_dma_disarm(devpriv->mite, AO_DMA_CHAN);
2713 writel(CHOR_DMARESET | CHOR_FRESET, devpriv->mite->mite_io_addr + MITE_CHOR(AO_DMA_CHAN));
2716 devpriv->stc_writew(dev, AO_Configuration_Start,Joint_Reset_Register);
2717 devpriv->stc_writew(dev, AO_Disarm,AO_Command_1_Register);
2718 ni_set_bits(dev,Interrupt_B_Enable_Register,~0,0);
2719 devpriv->stc_writew(dev, AO_BC_Source_Select, AO_Personal_Register);
2720 devpriv->stc_writew(dev, 0x3f98,Interrupt_B_Ack_Register);
2721 devpriv->stc_writew(dev, AO_BC_Source_Select | AO_UPDATE_Pulse_Width |
2722 AO_TMRDACWR_Pulse_Width, AO_Personal_Register);
2723 devpriv->stc_writew(dev, 0,AO_Output_Control_Register);
2724 devpriv->stc_writew(dev, 0,AO_Start_Select_Register);
2726 devpriv->stc_writew(dev, devpriv->ao_cmd1,AO_Command_1_Register);
2728 devpriv->ao_mode1=0;
2729 devpriv->ao_mode2=0;
2730 if(boardtype.reg_type == ni_reg_m_series)
2731 devpriv->ao_mode3 = AO_Last_Gate_Disable;
2733 devpriv->ao_mode3 = 0;
2734 devpriv->stc_writew(dev, devpriv->ao_mode3, AO_Mode_3_Register);
2735 devpriv->ao_trigger_select=0;
2736 if(boardtype.reg_type & ni_reg_6xxx_mask){
2737 ao_win_out(0x3, AO_Immediate_671x);
2738 ao_win_out(CLEAR_WG, AO_Misc_611x);
2740 devpriv->stc_writew(dev, AO_Configuration_End, Joint_Reset_Register);
2745 static int ni_dio_insn_config(comedi_device *dev,comedi_subdevice *s,
2746 comedi_insn *insn,lsampl_t *data)
2749 printk("ni_dio_insn_config() chan=%d io=%d\n",
2750 CR_CHAN(insn->chanspec),data[0]);
2753 case INSN_CONFIG_DIO_OUTPUT:
2754 s->io_bits |= 1<<CR_CHAN(insn->chanspec);
2756 case INSN_CONFIG_DIO_INPUT:
2757 s->io_bits &= ~(1<<CR_CHAN(insn->chanspec));
2759 case INSN_CONFIG_DIO_QUERY:
2760 data[1] = (s->io_bits & (1<<CR_CHAN(insn->chanspec))) ? COMEDI_OUTPUT : COMEDI_INPUT;
2767 devpriv->dio_control &= ~DIO_Pins_Dir_Mask;
2768 devpriv->dio_control |= DIO_Pins_Dir(s->io_bits);
2769 devpriv->stc_writew(dev, devpriv->dio_control,DIO_Control_Register);
2774 static int ni_dio_insn_bits(comedi_device *dev,comedi_subdevice *s,
2775 comedi_insn *insn,lsampl_t *data)
2778 printk("ni_dio_insn_bits() mask=0x%x bits=0x%x\n",data[0],data[1]);
2780 if(insn->n!=2)return -EINVAL;
2782 /* Perform check to make sure we're not using the
2783 serial part of the dio */
2784 if((data[0] & (DIO_SDIN | DIO_SDOUT)) && devpriv->serial_interval_ns)
2787 s->state &= ~data[0];
2788 s->state |= (data[0]&data[1]);
2789 devpriv->dio_output &= ~DIO_Parallel_Data_Mask;
2790 devpriv->dio_output |= DIO_Parallel_Data_Out(s->state);
2791 devpriv->stc_writew(dev, devpriv->dio_output,DIO_Output_Register);
2793 data[1] = devpriv->stc_readw(dev, DIO_Parallel_Input_Register);
2798 static int ni_serial_insn_config(comedi_device *dev,comedi_subdevice *s,
2799 comedi_insn *insn,lsampl_t *data)
2802 unsigned char byte_out, byte_in;
2804 if(insn->n!=2)return -EINVAL;
2807 case INSN_CONFIG_SERIAL_CLOCK:
2810 printk("SPI serial clock Config cd\n", data[1]);
2812 devpriv->serial_hw_mode = 1;
2813 devpriv->dio_control |= DIO_HW_Serial_Enable;
2815 if(data[1] == SERIAL_DISABLED) {
2816 devpriv->serial_hw_mode = 0;
2817 devpriv->dio_control &= ~(DIO_HW_Serial_Enable |
2818 DIO_Software_Serial_Control);
2819 data[1] = SERIAL_DISABLED;
2820 devpriv->serial_interval_ns = data[1];
2822 else if(data[1] <= SERIAL_600NS) {
2823 /* Warning: this clock speed is too fast to reliably
2825 devpriv->dio_control &= ~DIO_HW_Serial_Timebase;
2826 devpriv->clock_and_fout |= Slow_Internal_Timebase;
2827 devpriv->clock_and_fout &= ~DIO_Serial_Out_Divide_By_2;
2828 data[1] = SERIAL_600NS;
2829 devpriv->serial_interval_ns = data[1];
2831 else if(data[1] <= SERIAL_1_2US) {
2832 devpriv->dio_control &= ~DIO_HW_Serial_Timebase;
2833 devpriv->clock_and_fout |= Slow_Internal_Timebase |
2834 DIO_Serial_Out_Divide_By_2;
2835 data[1] = SERIAL_1_2US;
2836 devpriv->serial_interval_ns = data[1];
2838 else if(data[1] <= SERIAL_10US) {
2839 devpriv->dio_control |= DIO_HW_Serial_Timebase;
2840 devpriv->clock_and_fout |= Slow_Internal_Timebase |
2841 DIO_Serial_Out_Divide_By_2;
2842 /* Note: DIO_Serial_Out_Divide_By_2 only affects
2843 600ns/1.2us. If you turn divide_by_2 off with the
2844 slow clock, you will still get 10us, except then
2845 all your delays are wrong. */
2846 data[1] = SERIAL_10US;
2847 devpriv->serial_interval_ns = data[1];
2850 devpriv->dio_control &= ~(DIO_HW_Serial_Enable |
2851 DIO_Software_Serial_Control);
2852 devpriv->serial_hw_mode = 0;
2853 data[1] = (data[1] / 1000) * 1000;
2854 devpriv->serial_interval_ns = data[1];
2857 devpriv->stc_writew(dev, devpriv->dio_control,DIO_Control_Register);
2858 devpriv->stc_writew(dev, devpriv->clock_and_fout,Clock_and_FOUT_Register);
2863 case INSN_CONFIG_BIDIRECTIONAL_DATA:
2865 if(devpriv->serial_interval_ns == 0) {
2869 byte_out = data[1] & 0xFF;
2871 if(devpriv->serial_hw_mode) {
2872 err = ni_serial_hw_readwrite8(dev,s,byte_out,&byte_in);
2873 } else if(devpriv->serial_interval_ns > 0) {
2874 err = ni_serial_sw_readwrite8(dev,s,byte_out,&byte_in);
2876 printk("ni_serial_insn_config: serial disabled!\n");
2879 if(err < 0) return err;
2880 data[1] = byte_in & 0xFF;
2890 static int ni_serial_hw_readwrite8(comedi_device *dev,comedi_subdevice *s,
2891 unsigned char data_out,
2892 unsigned char *data_in)
2894 unsigned int status1;
2895 int err = 0, count = 20;
2898 printk("ni_serial_hw_readwrite8: outputting 0x%x\n", data_out);
2901 devpriv->dio_output &= ~DIO_Serial_Data_Mask;
2902 devpriv->dio_output |= DIO_Serial_Data_Out(data_out);
2903 devpriv->stc_writew(dev, devpriv->dio_output,DIO_Output_Register);
2905 status1 = devpriv->stc_readw(dev, Joint_Status_1_Register);
2906 if(status1 & DIO_Serial_IO_In_Progress_St) {
2911 devpriv->dio_control |= DIO_HW_Serial_Start;
2912 devpriv->stc_writew(dev, devpriv->dio_control,DIO_Control_Register);
2913 devpriv->dio_control &= ~DIO_HW_Serial_Start;
2915 /* Wait until STC says we're done, but don't loop infinitely. */
2916 while((status1 = devpriv->stc_readw(dev, Joint_Status_1_Register)) & DIO_Serial_IO_In_Progress_St) {
2917 /* Delay one bit per loop */
2918 comedi_udelay((devpriv->serial_interval_ns + 999) / 1000);
2920 printk("ni_serial_hw_readwrite8: SPI serial I/O didn't finish in time!\n");
2926 /* Delay for last bit. This delay is absolutely necessary, because
2927 DIO_Serial_IO_In_Progress_St goes high one bit too early. */
2928 comedi_udelay((devpriv->serial_interval_ns + 999) / 1000);
2930 if(data_in != NULL) {
2931 *data_in = devpriv->stc_readw(dev, DIO_Serial_Input_Register);
2933 printk("ni_serial_hw_readwrite8: inputted 0x%x\n", *data_in);
2938 devpriv->stc_writew(dev, devpriv->dio_control,DIO_Control_Register);
2943 static int ni_serial_sw_readwrite8(comedi_device *dev,comedi_subdevice *s,
2944 unsigned char data_out,
2945 unsigned char *data_in)
2947 unsigned char mask, input = 0;
2950 printk("ni_serial_sw_readwrite8: outputting 0x%x\n", data_out);
2953 /* Wait for one bit before transfer */
2954 comedi_udelay((devpriv->serial_interval_ns + 999) / 1000);
2956 for(mask = 0x80; mask; mask >>= 1) {
2957 /* Output current bit; note that we cannot touch s->state
2958 because it is a per-subdevice field, and serial is
2959 a separate subdevice from DIO. */
2960 devpriv->dio_output &= ~DIO_SDOUT;
2961 if(data_out & mask) {
2962 devpriv->dio_output |= DIO_SDOUT;
2964 devpriv->stc_writew(dev, devpriv->dio_output,DIO_Output_Register);
2966 /* Assert SDCLK (active low, inverted), wait for half of
2967 the delay, deassert SDCLK, and wait for the other half. */
2968 devpriv->dio_control |= DIO_Software_Serial_Control;
2969 devpriv->stc_writew(dev, devpriv->dio_control,DIO_Control_Register);
2971 comedi_udelay((devpriv->serial_interval_ns + 999) / 2000);
2973 devpriv->dio_control &= ~DIO_Software_Serial_Control;
2974 devpriv->stc_writew(dev, devpriv->dio_control,DIO_Control_Register);
2976 comedi_udelay((devpriv->serial_interval_ns + 999) / 2000);
2978 /* Input current bit */
2979 if(devpriv->stc_readw(dev, DIO_Parallel_Input_Register) & DIO_SDIN) {
2980 /* printk("DIO_P_I_R: 0x%x\n", devpriv->stc_readw(dev, DIO_Parallel_Input_Register)); */
2985 printk("ni_serial_sw_readwrite8: inputted 0x%x\n", input);
2987 if(data_in) *data_in = input;
2992 static void mio_common_detach(comedi_device *dev)
2994 if(dev->subdevices && boardtype.has_8255)
2995 subdev_8255_cleanup(dev,dev->subdevices+3);
2998 static void init_ao_67xx(comedi_device *dev, comedi_subdevice *s)
3002 for(i = 0; i < s->n_chan; i++)
3003 ni_ao_win_outw(dev, AO_Channel(i) | 0x0, AO_Configuration_2_67xx);
3006 static int ni_alloc_private(comedi_device *dev)
3010 ret = alloc_private(dev, sizeof(ni_private));
3011 if(ret < 0) return ret;
3013 spin_lock_init(&devpriv->window_lock);
3018 static int ni_E_init(comedi_device *dev,comedi_devconfig *it)
3020 comedi_subdevice *s;
3023 if(boardtype.n_aochan > MAX_N_AO_CHAN)
3025 printk("bug! boardtype.n_aochan > MAX_N_AO_CHAN\n");
3029 if(alloc_subdevices(dev, 11) < 0)
3032 /* analog input subdevice */
3034 s=dev->subdevices+0;
3036 if(boardtype.n_adchan){
3037 s->type=COMEDI_SUBD_AI;
3038 s->subdev_flags=SDF_READABLE | SDF_DIFF | SDF_DITHER;
3039 if(boardtype.reg_type != ni_reg_611x)
3040 s->subdev_flags |= SDF_GROUND | SDF_COMMON | SDF_OTHER;
3041 if(boardtype.adbits > 16)
3042 s->subdev_flags |= SDF_LSAMPL;
3043 s->n_chan=boardtype.n_adchan;
3044 s->len_chanlist=512;
3045 s->maxdata=(1<<boardtype.adbits)-1;
3046 s->range_table=ni_range_lkup[boardtype.gainlkup];
3047 s->insn_read=ni_ai_insn_read;
3048 s->insn_config=ni_ai_insn_config;
3049 s->do_cmdtest=ni_ai_cmdtest;
3050 s->do_cmd=ni_ai_cmd;
3051 s->cancel=ni_ai_reset;
3053 s->munge=ni_ai_munge;
3055 s->type=COMEDI_SUBD_UNUSED;
3058 /* analog output subdevice */
3060 s=dev->subdevices+1;
3061 if(boardtype.n_aochan){
3062 dev->write_subdev=s;
3063 s->type=COMEDI_SUBD_AO;
3064 s->subdev_flags=SDF_WRITABLE|SDF_DEGLITCH|SDF_GROUND;
3065 s->n_chan=boardtype.n_aochan;
3066 s->maxdata=(1<<boardtype.aobits)-1;
3067 s->range_table = boardtype.ao_range_table;
3068 s->insn_read=ni_ao_insn_read;
3069 if(boardtype.reg_type & ni_reg_6xxx_mask){
3070 s->insn_write=ni_ao_insn_write_671x;
3072 s->insn_write=ni_ao_insn_write;
3075 if(boardtype.n_aochan){
3077 if(boardtype.ao_fifo_depth){
3079 s->do_cmd=ni_ao_cmd;
3080 s->do_cmdtest=ni_ao_cmdtest;
3081 s->len_chanlist = boardtype.n_aochan;
3082 if(boardtype.reg_type != ni_reg_m_series)
3083 s->munge=ni_ao_munge;
3085 s->cancel=ni_ao_reset;
3087 s->type=COMEDI_SUBD_UNUSED;
3089 if((boardtype.reg_type & ni_reg_67xx_mask))
3090 init_ao_67xx(dev, s);
3092 /* digital i/o subdevice */
3094 s=dev->subdevices+2;
3095 s->type=COMEDI_SUBD_DIO;
3096 s->subdev_flags=SDF_WRITABLE|SDF_READABLE;
3099 s->range_table=&range_digital;
3100 s->io_bits=0; /* all bits input */
3101 s->insn_bits=ni_dio_insn_bits;
3102 s->insn_config=ni_dio_insn_config;
3105 devpriv->dio_control = DIO_Pins_Dir(s->io_bits);
3106 devpriv->stc_writew(dev, devpriv->dio_control,DIO_Control_Register);
3109 s=dev->subdevices+3;
3110 if(boardtype.has_8255){
3111 subdev_8255_init(dev,s,ni_8255_callback,(unsigned long)dev);
3113 s->type=COMEDI_SUBD_UNUSED;
3116 /* general purpose counter/timer device */
3117 s=dev->subdevices+4;
3118 s->type=COMEDI_SUBD_COUNTER;
3119 s->subdev_flags=SDF_READABLE|SDF_WRITABLE;
3120 s->insn_read= ni_gpct_insn_read;
3121 s->insn_write= ni_gpct_insn_write;
3122 s->insn_config=ni_gpct_insn_config;
3125 devpriv->an_trig_etc_reg = 0;
3129 /* calibration subdevice -- ai and ao */
3130 s=dev->subdevices+5;
3131 s->type=COMEDI_SUBD_CALIB;
3132 if(boardtype.reg_type == ni_reg_m_series)
3134 // internal PWM analog output used for AI nonlinearity calibration
3135 s->subdev_flags = SDF_INTERNAL;
3136 s->insn_config = &ni_m_series_pwm_config;
3139 ni_writel(0x0, M_Offset_Cal_PWM);
3140 } else if(boardtype.reg_type == ni_reg_6143)
3142 // internal PWM analog output used for AI nonlinearity calibration
3143 s->subdev_flags = SDF_INTERNAL;
3144 s->insn_config = &ni_6143_pwm_config;
3149 s->subdev_flags = SDF_WRITABLE | SDF_INTERNAL;
3150 s->insn_read = &ni_calib_insn_read;
3151 s->insn_write = &ni_calib_insn_write;
3152 caldac_setup(dev, s);
3156 s=dev->subdevices+6;
3157 s->type=COMEDI_SUBD_MEMORY;
3158 s->subdev_flags=SDF_READABLE|SDF_INTERNAL;
3160 if(boardtype.reg_type == ni_reg_m_series)
3162 s->n_chan = M_SERIES_EEPROM_SIZE;
3163 s->insn_read = &ni_m_series_eeprom_insn_read;
3167 s->insn_read = &ni_eeprom_insn_read;
3170 s=dev->subdevices+7;
3171 s->type=COMEDI_SUBD_DIO;
3172 s->subdev_flags=SDF_READABLE|SDF_WRITABLE|SDF_INTERNAL;
3175 s->insn_bits = ni_pfi_insn_bits;
3176 s->insn_config = ni_pfi_insn_config;
3177 ni_set_bits(dev, IO_Bidirection_Pin_Register, ~0, 0);
3179 /* cs5529 calibration adc */
3180 s = dev->subdevices + 8;
3181 if(boardtype.reg_type & ni_reg_67xx_mask)
3183 s->type = COMEDI_SUBD_AI;
3184 s->subdev_flags = SDF_READABLE | SDF_DIFF | SDF_INTERNAL;
3185 // one channel for each analog output channel
3186 s->n_chan = boardtype.n_aochan;
3187 s->maxdata = (1 << 16) - 1;
3188 s->range_table = &range_unknown; /* XXX */
3189 s->insn_read=cs5529_ai_insn_read;
3190 s->insn_config=NULL;
3194 s->type=COMEDI_SUBD_UNUSED;
3198 s=dev->subdevices+9;
3199 s->type=COMEDI_SUBD_SERIAL;
3200 s->subdev_flags=SDF_READABLE|SDF_WRITABLE|SDF_INTERNAL;
3203 s->insn_config = ni_serial_insn_config;
3204 devpriv->serial_interval_ns = 0;
3205 devpriv->serial_hw_mode = 0;
3208 s=dev->subdevices+10;
3209 s->type=COMEDI_SUBD_DIO;
3210 s->subdev_flags=SDF_READABLE|SDF_WRITABLE|SDF_INTERNAL;
3213 s->insn_bits = ni_rtsi_insn_bits;
3214 s->insn_config = ni_rtsi_insn_config;
3217 /* ai configuration */
3218 ni_ai_reset(dev,dev->subdevices+0);
3219 if((boardtype.reg_type & ni_reg_6xxx_mask) == 0){
3220 // BEAM is this needed for PCI-6143 ??
3221 devpriv->clock_and_fout =
3222 Slow_Internal_Time_Divide_By_2 |
3223 Slow_Internal_Timebase |
3224 Clock_To_Board_Divide_By_2 |
3226 AI_Output_Divide_By_2 |
3227 AO_Output_Divide_By_2;
3229 devpriv->clock_and_fout =
3230 Slow_Internal_Time_Divide_By_2 |
3231 Slow_Internal_Timebase |
3232 Clock_To_Board_Divide_By_2 |
3235 devpriv->stc_writew(dev, devpriv->clock_and_fout, Clock_and_FOUT_Register);
3237 /* analog output configuration */
3238 ni_ao_reset(dev,dev->subdevices + 1);
3241 devpriv->stc_writew(dev, (IRQ_POLARITY?Interrupt_Output_Polarity:0) |
3242 (Interrupt_Output_On_3_Pins&0) |
3243 Interrupt_A_Enable |
3244 Interrupt_B_Enable |
3245 Interrupt_A_Output_Select(interrupt_pin(dev->irq)) |
3246 Interrupt_B_Output_Select(interrupt_pin(dev->irq)),
3247 Interrupt_Control_Register
3252 /* tell the STC which dma channels to use for AI and AO */
3253 bits = 1 << ( AI_DMA_CHAN );
3254 bits |= 1 << ( AO_DMA_CHAN + 4 );
3255 ni_writeb( bits, AI_AO_Select);
3256 /* tell the STC which dma channels to use for
3257 * General purpose counters 0 and 1 */
3258 bits = 1 << ( GPC0_DMA_CHAN );
3259 bits |= 1 << ( GPC1_DMA_CHAN + 4 );
3260 ni_writeb( bits, G0_G1_Select);
3262 if(boardtype.reg_type & ni_reg_6xxx_mask)
3264 ni_writeb( 0, Magic_611x );
3265 }else if(boardtype.reg_type == ni_reg_m_series)
3268 for(channel = 0; channel < boardtype.n_aochan; ++channel)
3270 ni_writeb(0xf, M_Offset_AO_Waveform_Order(channel));
3271 ni_writeb(0x0, M_Offset_AO_Reference_Attenuation(channel));
3273 ni_writeb(0x0, M_Offset_AO_Calibration);
3283 static int ni_8255_callback(int dir,int port,int data,unsigned long arg)
3285 comedi_device *dev=(comedi_device *)arg;
3288 ni_writeb(data,Port_A+2*port);
3291 return ni_readb(Port_A+2*port);
3296 presents the EEPROM as a subdevice
3299 static int ni_eeprom_insn_read(comedi_device *dev,comedi_subdevice *s,
3300 comedi_insn *insn,lsampl_t *data)
3302 data[0]=ni_read_eeprom(dev,CR_CHAN(insn->chanspec));
3308 reads bytes out of eeprom
3311 static int ni_read_eeprom(comedi_device *dev,int addr)
3316 bitstring=0x0300|((addr&0x100)<<3)|(addr&0xff);
3317 ni_writeb(0x04,Serial_Command);
3318 for(bit=0x8000;bit;bit>>=1){
3319 ni_writeb(0x04|((bit&bitstring)?0x02:0),Serial_Command);
3320 ni_writeb(0x05|((bit&bitstring)?0x02:0),Serial_Command);
3323 for(bit=0x80;bit;bit>>=1){
3324 ni_writeb(0x04,Serial_Command);
3325 ni_writeb(0x05,Serial_Command);
3326 bitstring|=((ni_readb(XXX_Status)&PROMOUT)?bit:0);
3328 ni_writeb(0x00,Serial_Command);
3333 static int ni_m_series_eeprom_insn_read(comedi_device *dev,comedi_subdevice *s,
3334 comedi_insn *insn,lsampl_t *data)
3336 data[0] = devpriv->eeprom_buffer[CR_CHAN(insn->chanspec)];
3341 static int ni_get_pwm_config(comedi_device *dev, lsampl_t *data)
3343 data[1] = devpriv->pwm_up_count * TIMER_BASE;
3344 data[2] = devpriv->pwm_down_count * TIMER_BASE;
3348 static int ni_m_series_pwm_config(comedi_device *dev, comedi_subdevice *s,
3349 comedi_insn *insn, lsampl_t *data)
3351 unsigned up_count, down_count;
3354 case INSN_CONFIG_PWM_OUTPUT:
3357 case TRIG_ROUND_NEAREST:
3358 up_count = (data[2] + TIMER_BASE / 2) / TIMER_BASE;
3360 case TRIG_ROUND_DOWN:
3361 up_count = data[2] / TIMER_BASE;
3364 up_count = (data[2] + TIMER_BASE - 1) / TIMER_BASE;
3372 case TRIG_ROUND_NEAREST:
3373 down_count = (data[4] + TIMER_BASE / 2) / TIMER_BASE;
3375 case TRIG_ROUND_DOWN:
3376 down_count = data[4] / TIMER_BASE;
3379 down_count = (data[4] + TIMER_BASE - 1) / TIMER_BASE;
3385 if(up_count * TIMER_BASE != data[2] ||
3386 down_count * TIMER_BASE != data[4])
3388 data[2] = up_count * TIMER_BASE;
3389 data[4] = down_count * TIMER_BASE;
3392 ni_writel(MSeries_Cal_PWM_High_Time_Bits(up_count) | MSeries_Cal_PWM_Low_Time_Bits(down_count), M_Offset_Cal_PWM);
3393 devpriv->pwm_up_count = up_count;
3394 devpriv->pwm_down_count = down_count;
3397 case INSN_CONFIG_GET_PWM_OUTPUT:
3398 return ni_get_pwm_config(dev, data);
3407 static int ni_6143_pwm_config(comedi_device *dev, comedi_subdevice *s,
3408 comedi_insn *insn, lsampl_t *data)
3410 unsigned up_count, down_count;
3413 case INSN_CONFIG_PWM_OUTPUT:
3416 case TRIG_ROUND_NEAREST:
3417 up_count = (data[2] + TIMER_BASE / 2) / TIMER_BASE;
3419 case TRIG_ROUND_DOWN:
3420 up_count = data[2] / TIMER_BASE;
3423 up_count = (data[2] + TIMER_BASE - 1) / TIMER_BASE;
3431 case TRIG_ROUND_NEAREST:
3432 down_count = (data[4] + TIMER_BASE / 2) / TIMER_BASE;
3434 case TRIG_ROUND_DOWN:
3435 down_count = data[4] / TIMER_BASE;
3438 down_count = (data[4] + TIMER_BASE - 1) / TIMER_BASE;
3444 if(up_count * TIMER_BASE != data[2] ||
3445 down_count * TIMER_BASE != data[4])
3447 data[2] = up_count * TIMER_BASE;
3448 data[4] = down_count * TIMER_BASE;
3451 ni_writel(up_count, Calibration_HighTime_6143);
3452 devpriv->pwm_up_count = up_count;
3453 ni_writel(down_count, Calibration_LowTime_6143);
3454 devpriv->pwm_down_count = down_count;
3457 case INSN_CONFIG_GET_PWM_OUTPUT:
3458 return ni_get_pwm_config(dev, data);
3466 static void ni_write_caldac(comedi_device *dev,int addr,int val);
3468 calibration subdevice
3470 static int ni_calib_insn_write(comedi_device *dev,comedi_subdevice *s,
3471 comedi_insn *insn,lsampl_t *data)
3473 ni_write_caldac(dev,CR_CHAN(insn->chanspec),data[0]);
3478 static int ni_calib_insn_read(comedi_device *dev,comedi_subdevice *s,
3479 comedi_insn *insn,lsampl_t *data)
3481 data[0] = devpriv->caldacs[CR_CHAN(insn->chanspec)];
3486 static int pack_mb88341(int addr,int val,int *bitstring);
3487 static int pack_dac8800(int addr,int val,int *bitstring);
3488 static int pack_dac8043(int addr,int val,int *bitstring);
3489 static int pack_ad8522(int addr,int val,int *bitstring);
3490 static int pack_ad8804(int addr,int val,int *bitstring);
3491 static int pack_ad8842(int addr,int val,int *bitstring);
3493 struct caldac_struct{
3496 int (*packbits)(int,int,int *);
3499 static struct caldac_struct caldacs[] = {
3500 [mb88341] = { 12, 8, pack_mb88341 },
3501 [dac8800] = { 8, 8, pack_dac8800 },
3502 [dac8043] = { 1, 12, pack_dac8043 },
3503 [ad8522] = { 2, 12, pack_ad8522 },
3504 [ad8804] = { 12, 8, pack_ad8804 },
3505 [ad8842] = { 8, 8, pack_ad8842 },
3506 [ad8804_debug] = { 16, 8, pack_ad8804 },
3509 static void caldac_setup(comedi_device *dev,comedi_subdevice *s)
3519 type = boardtype.caldac[0];
3520 if(type==caldac_none)return;
3521 n_bits=caldacs[type].n_bits;
3523 type = boardtype.caldac[i];
3524 if(type==caldac_none)break;
3525 if(caldacs[type].n_bits!=n_bits)diffbits=1;
3526 n_chans+=caldacs[type].n_chans;
3533 if(n_chans>MAX_N_CALDACS){
3534 printk("BUG! MAX_N_CALDACS too small\n");
3536 s->maxdata_list=devpriv->caldac_maxdata_list;
3538 for(i=0;i<n_dacs;i++){
3539 type = boardtype.caldac[i];
3540 for(j=0;j<caldacs[type].n_chans;j++){
3541 s->maxdata_list[chan]=
3542 (1<<caldacs[type].n_bits)-1;
3547 for( chan = 0; chan < s->n_chan; chan++ )
3548 ni_write_caldac( dev, i, s->maxdata_list[ i ] / 2 );
3550 type = boardtype.caldac[0];
3551 s->maxdata=(1<<caldacs[type].n_bits)-1;
3553 for( chan = 0; chan < s->n_chan; chan++ )
3554 ni_write_caldac( dev, i, s->maxdata / 2 );
3558 static void ni_write_caldac(comedi_device *dev,int addr,int val)
3560 unsigned int loadbit=0,bits=0,bit,bitstring=0;
3564 //printk("ni_write_caldac: chan=%d val=%d\n",addr,val);
3565 if( devpriv->caldacs[ addr ] == val ) return;
3566 devpriv->caldacs[ addr ] = val;
3569 type = boardtype.caldac[i];
3570 if(type==caldac_none)break;
3571 if(addr<caldacs[type].n_chans){
3572 bits=caldacs[type].packbits(addr,val,&bitstring);
3573 loadbit=SerDacLd(i);
3574 //printk("caldac: using i=%d addr=%d %x\n",i,addr,bitstring);
3577 addr-=caldacs[type].n_chans;
3580 for(bit=1<<(bits-1);bit;bit>>=1){
3581 ni_writeb(((bit&bitstring)?0x02:0),Serial_Command);
3583 ni_writeb(1|((bit&bitstring)?0x02:0),Serial_Command);
3586 ni_writeb(loadbit,Serial_Command);
3588 ni_writeb(0,Serial_Command);
3593 static int pack_mb88341(int addr,int val,int *bitstring)
3597 Note that address bits are reversed. Thanks to
3598 Ingo Keen for noticing this.
3600 Note also that the 88341 expects address values from
3601 1-12, whereas we use channel numbers 0-11. The NI
3602 docs use 1-12, also, so be careful here.
3605 *bitstring=((addr&0x1)<<11) |
3613 static int pack_dac8800(int addr,int val,int *bitstring)
3615 *bitstring=((addr&0x7)<<8)|(val&0xff);
3619 static int pack_dac8043(int addr,int val,int *bitstring)
3621 *bitstring=val&0xfff;
3625 static int pack_ad8522(int addr,int val,int *bitstring)
3627 *bitstring=(val&0xfff)|(addr ? 0xc000:0xa000);
3631 static int pack_ad8804(int addr,int val,int *bitstring)
3633 *bitstring=((addr&0xf)<<8) | (val&0xff);
3637 static int pack_ad8842(int addr,int val,int *bitstring)
3639 *bitstring=((addr+1)<<8) | (val&0xff);
3649 * General Purpose Counter/Timer section
3654 * Low level stuff...Each STC counter has two 24 bit load registers
3655 * (A&B). Just make it easier to access them.
3657 * These are inlined _only_ because they are used once in subsequent
3658 * code. Otherwise they should not be inlined.
3660 static inline void GPCT_Load_A(comedi_device *dev, int chan, unsigned int value)
3662 devpriv->stc_writel(dev, value & 0x00ffffff, G_Load_A_Register(chan));
3665 static inline void GPCT_Load_B(comedi_device *dev, int chan, unsigned int value)
3667 devpriv->stc_writel(dev, value & 0x00ffffff, G_Load_B_Register(chan));
3670 /* Load a value into the counter, using register A as the intermediate step.
3671 * You might use GPCT_Load_Using_A to load a 0x000000 into a counter
3674 static void GPCT_Load_Using_A(comedi_device *dev, int chan, unsigned int value)
3676 devpriv->gpct_mode[chan] &= (~G_Load_Source_Select);
3677 devpriv->stc_writew(dev, devpriv->gpct_mode[chan],G_Mode_Register(chan));
3678 GPCT_Load_A(dev,chan,value);
3679 devpriv->stc_writew(dev, devpriv->gpct_command[chan]|G_Load,G_Command_Register(chan));
3683 * Read the GPCTs current value.
3685 static int GPCT_G_Watch(comedi_device *dev, int chan)
3687 unsigned int hi1,hi2,lo;
3689 devpriv->gpct_command[chan] &= ~G_Save_Trace;
3690 devpriv->stc_writew(dev, devpriv->gpct_command[chan],G_Command_Register(chan));
3692 devpriv->gpct_command[chan] |= G_Save_Trace;
3693 devpriv->stc_writew(dev, devpriv->gpct_command[chan], G_Command_Register(chan));
3695 /* This procedure is used because the two registers cannot
3696 * be read atomically. */
3698 hi1 = devpriv->stc_readw(dev, G_Save_Register_High(chan));
3699 lo = devpriv->stc_readw(dev, G_Save_Register_Low(chan));
3700 hi2 = devpriv->stc_readw(dev, G_Save_Register_High(chan));
3703 return (hi1<<16)|lo;
3707 static int GPCT_Disarm(comedi_device *dev, int chan)
3709 devpriv->stc_writew(dev, devpriv->gpct_command[chan] | G_Disarm,G_Command_Register(chan));
3714 static int GPCT_Arm(comedi_device *dev, int chan)
3716 devpriv->stc_writew(dev, devpriv->gpct_command[chan] | G_Arm,G_Command_Register(chan));
3717 /* If the counter is doing pulse width measurement, then make
3718 sure that the counter did not start counting right away. This would
3719 indicate that we started acquiring the pulse after it had already
3720 started and our measurement would be inaccurate */
3721 if(devpriv->gpct_cur_operation[chan] == GPCT_SINGLE_PW){
3724 g_status=devpriv->stc_readw(dev, G_Status_Register);
3727 //TIM 5/2/01 possible error with very short pulses
3728 if((G0_Counting_St & g_status)|| !(G0_Armed_St&g_status)) {
3729 //error: we missed the beginning of the pulse
3730 return -EINVAL; //there is probably a more accurate error code...
3733 if((G1_Counting_St & g_status)|| !(G1_Armed_St&g_status)) {
3734 //error: we missed the beginning of the pulse
3742 static int GPCT_Set_Source(comedi_device *dev,int chan ,int source)
3744 //printk("GPCT_Set_Source...");
3745 devpriv->gpct_input_select[chan] &= ~G_Source_Select(0x1f);//reset gate to 0
3747 case GPCT_INT_CLOCK:
3748 devpriv->gpct_input_select[chan] |= G_Source_Select(0);//INT_TIMEBASE
3752 devpriv->gpct_input_select[chan] |= G_Source_Select(9);//PFI8
3754 devpriv->gpct_input_select[chan] |= G_Source_Select(4);//PFI3
3759 devpriv->stc_writew(dev, devpriv->gpct_input_select[chan], G_Input_Select_Register(chan));
3760 //printk("exit GPCT_Set_Source\n");
3764 static int GPCT_Set_Gate(comedi_device *dev,int chan ,int gate)
3766 //printk("GPCT_Set_Gate...");
3767 devpriv->gpct_input_select[chan] &= ~G_Gate_Select(0x1f);//reset gate to 0
3770 devpriv->gpct_input_select[chan] |= G_Gate_Select(31);//Low
3771 devpriv->gpct_mode[chan] |= G_Gate_Polarity;
3774 devpriv->gpct_mode[chan] &= ~G_Gate_Polarity;
3776 devpriv->gpct_input_select[chan] |= G_Gate_Select(10);//PFI9
3778 devpriv->gpct_input_select[chan] |= G_Gate_Select(5);//PFI4
3784 devpriv->stc_writew(dev, devpriv->gpct_input_select[chan], G_Input_Select_Register(chan));
3785 devpriv->stc_writew(dev, devpriv->gpct_mode[chan], G_Mode_Register(chan));
3786 //printk("exit GPCT_Set_Gate\n");
3790 static int GPCT_Set_Direction(comedi_device *dev,int chan,int direction)
3792 //printk("GPCT_Set_Direction...");
3794 devpriv->gpct_command[chan] &= ~G_Up_Down(0x3);
3795 switch (direction) {
3797 devpriv->gpct_command[chan] |= G_Up_Down(1);
3800 devpriv->gpct_command[chan] |= G_Up_Down(0);
3803 devpriv->gpct_command[chan] |= G_Up_Down(2);
3806 printk("Error direction=0x%08x..",direction);
3809 devpriv->stc_writew(dev, devpriv->gpct_command[chan], G_Command_Register(chan));
3810 //TIM 4/23/01 devpriv->stc_writew(dev, devpriv->gpct_mode[chan], G_Mode_Register(chan));
3811 //printk("exit GPCT_Set_Direction\n");
3815 static void GPCT_Event_Counting(comedi_device *dev,int chan)
3818 //NOTE: possible residual bits from multibit masks can corrupt
3819 //If you config for several measurements between Resets, watch out!
3821 //printk("GPCT_Event_Counting...");
3823 devpriv->gpct_cur_operation[chan] = GPCT_SIMPLE_EVENT;
3826 devpriv->gpct_mode[chan] &= ~(G_Gating_Mode(0x3));
3827 devpriv->gpct_mode[chan] |= G_Gating_Mode(1);
3829 // Trigger_Mode_For_Edge_Gate = 1
3830 devpriv->gpct_mode[chan] &= ~(G_Trigger_Mode_For_Edge_Gate(0x3));
3831 devpriv->gpct_mode[chan] |= G_Trigger_Mode_For_Edge_Gate(2);
3833 devpriv->stc_writew(dev, devpriv->gpct_mode[chan],G_Mode_Register(chan));
3834 //printk("exit GPCT_Event_Counting\n");
3837 static void GPCT_Period_Meas(comedi_device *dev, int chan)
3839 //printk("GPCT_Period_Meas...");
3841 devpriv->gpct_cur_operation[chan] = GPCT_SINGLE_PERIOD;
3844 //NOTE: possible residual bits from multibit masks can corrupt
3845 //If you config for several measurements between Resets, watch out!
3846 devpriv->gpct_mode[chan] &= ~G_OR_Gate;
3847 devpriv->gpct_mode[chan] &= ~G_Gate_Select_Load_Source;
3850 devpriv->gpct_mode[chan] &= ~(G_Output_Mode(0x3));
3851 devpriv->gpct_mode[chan] |= G_Output_Mode(3);
3855 devpriv->gpct_mode[chan] &= ~(G_Gating_Mode(0x3));
3856 devpriv->gpct_mode[chan] |= G_Gating_Mode(2);
3858 // Trigger_Mode_For_Edge_Gate=0
3859 devpriv->gpct_mode[chan] &= ~(G_Trigger_Mode_For_Edge_Gate(0x3));
3860 devpriv->gpct_mode[chan] |= G_Trigger_Mode_For_Edge_Gate(0);
3862 devpriv->gpct_mode[chan] |= G_Reload_Source_Switching;
3863 devpriv->gpct_mode[chan] &= ~G_Loading_On_Gate;
3864 devpriv->gpct_mode[chan] &= ~G_Loading_On_TC;
3865 devpriv->gpct_mode[chan] &= ~G_Gate_On_Both_Edges;
3868 devpriv->gpct_mode[chan] &= ~(G_Stop_Mode(0x3));
3869 devpriv->gpct_mode[chan] |= G_Stop_Mode(0);
3871 // Counting_Once = 2
3872 devpriv->gpct_mode[chan] &= ~(G_Counting_Once(0x3));
3873 devpriv->gpct_mode[chan] |= G_Counting_Once(2);
3876 devpriv->gpct_command[chan] &= ~(G_Up_Down(0x3));
3877 devpriv->gpct_command[chan] |= G_Up_Down(1);
3879 devpriv->stc_writew(dev, devpriv->gpct_mode[chan],G_Mode_Register(chan));
3880 devpriv->stc_writew(dev, devpriv->gpct_command[chan],G_Command_Register(chan));
3881 //printk("exit GPCT_Period_Meas\n");
3884 static void GPCT_Pulse_Width_Meas(comedi_device *dev, int chan)
3886 //printk("GPCT_Pulse_Width_Meas...");
3888 devpriv->gpct_cur_operation[chan] = GPCT_SINGLE_PW;
3890 devpriv->gpct_mode[chan] &= ~G_OR_Gate;
3891 devpriv->gpct_mode[chan] &= ~G_Gate_Select_Load_Source;
3894 devpriv->gpct_mode[chan] &= ~(G_Output_Mode(0x3));
3895 devpriv->gpct_mode[chan] |= G_Output_Mode(3);
3898 devpriv->gpct_mode[chan] &= ~(G_Gating_Mode(0x3));
3899 devpriv->gpct_mode[chan] |= G_Gating_Mode(1);//TIM 4/24/01 was 2
3901 // Trigger_Mode_For_Edge_Gate=2
3902 devpriv->gpct_mode[chan] &= ~(G_Trigger_Mode_For_Edge_Gate(0x3));
3903 devpriv->gpct_mode[chan] |= G_Trigger_Mode_For_Edge_Gate(2);//TIM 4/24/01 was 0
3906 devpriv->gpct_mode[chan] |= G_Reload_Source_Switching;//TIM 4/24/01 was 1
3907 devpriv->gpct_mode[chan] &= ~G_Loading_On_Gate;//TIM 4/24/01 was 0
3909 devpriv->gpct_mode[chan] &= ~G_Loading_On_TC;
3910 devpriv->gpct_mode[chan] &= ~G_Gate_On_Both_Edges;
3913 devpriv->gpct_mode[chan] &= ~(G_Stop_Mode(0x3));
3914 devpriv->gpct_mode[chan] |= G_Stop_Mode(0);
3916 // Counting_Once = 2
3917 devpriv->gpct_mode[chan] &= ~(G_Counting_Once(0x3));
3918 devpriv->gpct_mode[chan] |= G_Counting_Once(2);
3921 devpriv->gpct_command[chan] &= ~(G_Up_Down(0x3));
3922 devpriv->gpct_command[chan] |= G_Up_Down(1);
3924 devpriv->stc_writew(dev, devpriv->gpct_mode[chan],G_Mode_Register(chan));
3925 devpriv->stc_writew(dev, devpriv->gpct_command[chan],G_Command_Register(chan));
3927 //printk("exit GPCT_Pulse_Width_Meas\n");
3930 /* GPCT_Gen_Single_Pulse() creates pulse of length pulsewidth which starts after the Arm
3931 signal is sent. The pulse is delayed by the value already in the counter. This function could
3932 be modified to send a pulse in response to a trigger event at its gate.*/
3933 static void GPCT_Gen_Single_Pulse(comedi_device *dev, int chan, unsigned int length)
3935 //printk("GPCT_Gen_Cont...");
3937 devpriv->gpct_cur_operation[chan] = GPCT_SINGLE_PULSE_OUT;
3939 // Set length of the pulse
3940 GPCT_Load_B(dev,chan, length-1);
3942 //Load next time using B, This is reset by GPCT_Load_Using_A()
3943 devpriv->gpct_mode[chan] |= G_Load_Source_Select;
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(2); //TIM 4/26/01 was 3
3952 //Gating Mode=0 for untriggered single pulse
3953 devpriv->gpct_mode[chan] &= ~(G_Gating_Mode(0x3));
3954 devpriv->gpct_mode[chan] |= G_Gating_Mode(0); //TIM 4/25/01 was 1
3956 // Trigger_Mode_For_Edge_Gate=0
3957 devpriv->gpct_mode[chan] &= ~(G_Trigger_Mode_For_Edge_Gate(0x3));
3958 devpriv->gpct_mode[chan] |= G_Trigger_Mode_For_Edge_Gate(2);
3961 devpriv->gpct_mode[chan] |= G_Reload_Source_Switching;
3962 devpriv->gpct_mode[chan] &= ~G_Loading_On_Gate;
3963 devpriv->gpct_mode[chan] |= G_Loading_On_TC; //TIM 4/25/01
3964 devpriv->gpct_mode[chan] &= ~G_Gate_On_Both_Edges;
3967 devpriv->gpct_mode[chan] &= ~(G_Stop_Mode(0x3));
3968 devpriv->gpct_mode[chan] |= G_Stop_Mode(2); //TIM 4/25/01
3970 // Counting_Once = 2
3971 devpriv->gpct_mode[chan] &= ~(G_Counting_Once(0x3));
3972 devpriv->gpct_mode[chan] |= G_Counting_Once(1); //TIM 4/25/01
3975 devpriv->gpct_command[chan] &= ~(G_Up_Down(0x3));
3976 devpriv->gpct_command[chan] |= G_Up_Down(0); //TIM 4/25/01 was 1
3978 devpriv->stc_writew(dev, devpriv->gpct_mode[chan],G_Mode_Register(chan));
3979 devpriv->stc_writew(dev, devpriv->gpct_command[chan],G_Command_Register(chan));
3981 //printk("exit GPCT_Gen_Cont\n");
3984 static void GPCT_Gen_Cont_Pulse(comedi_device *dev, int chan, unsigned int length)
3986 //printk("GPCT_Gen_Cont...");
3988 devpriv->gpct_cur_operation[chan] = GPCT_CONT_PULSE_OUT;
3990 // Set length of the pulse
3991 GPCT_Load_B(dev,chan, length-1);
3993 //Load next time using B, This is reset by GPCT_Load_Using_A()
3994 devpriv->gpct_mode[chan] |= G_Load_Source_Select;
3996 devpriv->gpct_mode[chan] &= ~G_OR_Gate;
3997 devpriv->gpct_mode[chan] &= ~G_Gate_Select_Load_Source;
4000 devpriv->gpct_mode[chan] &= ~(G_Output_Mode(0x3));
4001 devpriv->gpct_mode[chan] |= G_Output_Mode(2); //TIM 4/26/01 was 3
4003 //Gating Mode=0 for untriggered single pulse
4004 devpriv->gpct_mode[chan] &= ~(G_Gating_Mode(0x3));
4005 devpriv->gpct_mode[chan] |= G_Gating_Mode(0); //TIM 4/26/01 was 0
4007 // Trigger_Mode_For_Edge_Gate=0
4008 devpriv->gpct_mode[chan] &= ~(G_Trigger_Mode_For_Edge_Gate(0x3));
4009 devpriv->gpct_mode[chan] |= G_Trigger_Mode_For_Edge_Gate(2);
4012 devpriv->gpct_mode[chan] |= G_Reload_Source_Switching;
4013 devpriv->gpct_mode[chan] &= ~G_Loading_On_Gate;
4014 devpriv->gpct_mode[chan] |= G_Loading_On_TC;
4015 devpriv->gpct_mode[chan] &= ~G_Gate_On_Both_Edges;
4018 devpriv->gpct_mode[chan] &= ~(G_Stop_Mode(0x3));
4019 devpriv->gpct_mode[chan] |= G_Stop_Mode(0); //TIM 4/26/01
4021 // Counting_Once = 2
4022 devpriv->gpct_mode[chan] &= ~(G_Counting_Once(0x3));
4023 devpriv->gpct_mode[chan] |= G_Counting_Once(0); //TIM 4/26/01
4026 devpriv->gpct_command[chan] &= ~(G_Up_Down(0x3));
4027 devpriv->gpct_command[chan] |= G_Up_Down(0);
4030 //This seems pretty unsafe since I don't think it is cleared anywhere.
4031 //I don't think this is working
4032 //devpriv->gpct_command[chan] &= ~G_Bank_Switch_Enable;
4033 //devpriv->gpct_command[chan] &= ~G_Bank_Switch_Mode;
4036 devpriv->stc_writew(dev, devpriv->gpct_mode[chan],G_Mode_Register(chan));
4037 devpriv->stc_writew(dev, devpriv->gpct_command[chan],G_Command_Register(chan));
4039 //printk("exit GPCT_Gen_Cont\n");
4042 static void GPCT_Reset(comedi_device *dev, int chan)
4046 //printk("GPCT_Reset...");
4047 devpriv->gpct_cur_operation[chan] = GPCT_RESET;
4051 devpriv->stc_writew(dev, G0_Reset,Joint_Reset_Register);
4052 ni_set_bits(dev,Interrupt_A_Enable_Register,G0_TC_Interrupt_Enable, 0);
4053 ni_set_bits(dev,Interrupt_A_Enable_Register,G0_Gate_Interrupt_Enable,0);
4054 temp_ack_reg |= G0_Gate_Error_Confirm;
4055 temp_ack_reg |= G0_TC_Error_Confirm;
4056 temp_ack_reg |= G0_TC_Interrupt_Ack;
4057 temp_ack_reg |= G0_Gate_Interrupt_Ack;
4058 devpriv->stc_writew(dev, temp_ack_reg,Interrupt_A_Ack_Register);
4060 //problem...this interferes with the other ctr...
4061 devpriv->an_trig_etc_reg |= GPFO_0_Output_Enable;
4062 devpriv->stc_writew(dev, devpriv->an_trig_etc_reg, Analog_Trigger_Etc_Register);
4065 devpriv->stc_writew(dev, G1_Reset,Joint_Reset_Register);
4066 ni_set_bits(dev,Interrupt_B_Enable_Register,G1_TC_Interrupt_Enable, 0);
4067 ni_set_bits(dev,Interrupt_B_Enable_Register,G0_Gate_Interrupt_Enable,0);
4068 temp_ack_reg |= G1_Gate_Error_Confirm;
4069 temp_ack_reg |= G1_TC_Error_Confirm;
4070 temp_ack_reg |= G1_TC_Interrupt_Ack;
4071 temp_ack_reg |= G1_Gate_Interrupt_Ack;
4072 devpriv->stc_writew(dev, temp_ack_reg,Interrupt_B_Ack_Register);
4074 devpriv->an_trig_etc_reg |= GPFO_1_Output_Enable;
4075 devpriv->stc_writew(dev, devpriv->an_trig_etc_reg, Analog_Trigger_Etc_Register);
4079 devpriv->gpct_mode[chan] = 0;
4080 devpriv->gpct_input_select[chan] = 0;
4081 devpriv->gpct_command[chan] = 0;
4083 devpriv->gpct_command[chan] |= G_Synchronized_Gate;
4085 devpriv->stc_writew(dev, devpriv->gpct_mode[chan],G_Mode_Register(chan));
4086 devpriv->stc_writew(dev, devpriv->gpct_input_select[chan],G_Input_Select_Register(chan));
4087 devpriv->stc_writew(dev, 0,G_Autoincrement_Register(chan));
4089 //printk("exit GPCT_Reset\n");
4092 static int ni_gpct_insn_config(comedi_device *dev,comedi_subdevice *s,
4093 comedi_insn *insn,lsampl_t *data)
4096 //printk("data[0] is 0x%08x, data[1] is 0x%08x\n",data[0],data[1]);
4099 if(insn->n!=1)return -EINVAL;
4100 GPCT_Reset(dev,insn->chanspec);
4102 case GPCT_SET_SOURCE:
4103 if(insn->n!=2)return -EINVAL;
4104 retval=GPCT_Set_Source(dev,insn->chanspec,data[1]);
4107 if(insn->n!=2)return -EINVAL;
4108 retval=GPCT_Set_Gate(dev,insn->chanspec,data[1]);
4110 case GPCT_SET_DIRECTION:
4111 if(insn->n!=2) return -EINVAL;
4112 retval=GPCT_Set_Direction(dev,insn->chanspec,data[1]);
4114 case GPCT_GET_INT_CLK_FRQ:
4115 if(insn->n!=2) return -EINVAL;
4116 //There are actually 2 internal clocks on the STC, we always
4117 //use the fast 20MHz one at this time. Tim Ousley 5/1/01
4118 //NOTE: This is not the final interface, ideally the user
4119 //will never need to know the int. clk. freq.
4120 data[1]=50;//50ns = 20MHz = internal timebase of STC
4122 case GPCT_SET_OPERATION:
4123 //TIM 5/1/01 if((insn->n<2)||(insn->n>3))return -EINVAL;
4125 case GPCT_SIMPLE_EVENT:
4126 GPCT_Event_Counting(dev,insn->chanspec);
4128 case GPCT_SINGLE_PERIOD:
4129 GPCT_Period_Meas(dev,insn->chanspec);
4131 case GPCT_SINGLE_PW:
4132 GPCT_Pulse_Width_Meas(dev,insn->chanspec);
4134 case GPCT_SINGLE_PULSE_OUT:
4135 GPCT_Gen_Single_Pulse(dev,insn->chanspec,data[2]);
4137 case GPCT_CONT_PULSE_OUT:
4138 GPCT_Gen_Cont_Pulse(dev,insn->chanspec,data[2]);
4141 printk("unsupported GPCT operation!\n");
4146 if(insn->n!=1)return -EINVAL;
4147 retval=GPCT_Arm(dev,insn->chanspec);
4150 if(insn->n!=1)return -EINVAL;
4151 retval=GPCT_Disarm(dev,insn->chanspec);
4157 //catch any errors from return values
4161 if(data[0]!=GPCT_ARM){
4162 printk("error: retval was %d\n",retval);
4163 printk("data[0] is 0x%08x, data[1] is 0x%08x\n",data[0],data[1]);
4170 static int ni_gpct_insn_read(comedi_device *dev,comedi_subdevice *s,
4171 comedi_insn *insn,lsampl_t *data) {
4173 int chan=insn->chanspec;
4174 int cur_op = devpriv->gpct_cur_operation[chan];
4176 //printk("in ni_gpct_insn_read, n=%d, data[0]=%d\n",insn->chanspec,data[0]);
4177 if(insn->n!=1)return -EINVAL;
4179 data[0] = GPCT_G_Watch(dev,insn->chanspec);
4181 /* for certain modes (period and pulse width measurment), the value
4182 in the counter is not valid until the counter stops. If the value is
4183 invalid, return a 0 */
4184 if((cur_op == GPCT_SINGLE_PERIOD) || (cur_op == GPCT_SINGLE_PW)){
4185 /* is the counter still running? */
4186 if(devpriv->stc_readw(dev, G_Status_Register) & (chan?G1_Counting_St:G0_Counting_St))
4192 static int ni_gpct_insn_write(comedi_device *dev,comedi_subdevice *s,
4193 comedi_insn *insn,lsampl_t *data) {
4195 //printk("in ni_gpct_insn_write");
4196 if(insn->n!=1)return -EINVAL;
4197 GPCT_Load_Using_A(dev,insn->chanspec,data[0]);
4204 * Programmable Function Inputs
4208 static int ni_pfi_insn_bits(comedi_device *dev,comedi_subdevice *s,
4209 comedi_insn *insn,lsampl_t *data)
4211 if(insn->n!=2)return -EINVAL;
4218 static int ni_pfi_insn_config(comedi_device *dev,comedi_subdevice *s,
4219 comedi_insn *insn,lsampl_t *data)
4223 if(insn->n < 1)return -EINVAL;
4225 chan = CR_CHAN(insn->chanspec);
4226 if(chan>10)return -EINVAL;
4230 ni_set_bits(dev, IO_Bidirection_Pin_Register, 1<<chan, 1);
4233 ni_set_bits(dev, IO_Bidirection_Pin_Register, 1<<chan, 0);
4235 case INSN_CONFIG_DIO_QUERY:
4236 data[1] = (devpriv->io_bidirection_pin_reg & (1<<chan)) ? COMEDI_OUTPUT : COMEDI_INPUT;
4248 * NI RTSI Bus Functions
4251 static void ni_rtsi_init(comedi_device *dev)
4253 // Initialises the RTSI bus signal switch to a default state
4255 // Set clock mode to internal
4256 devpriv->stc_writew(dev, COMEDI_RTSI_CLOCK_MODE_INTERNAL, RTSI_Trig_Direction_Register);
4258 // Standard internal lines are routed to standard RTSI bus lines
4259 devpriv->stc_writew(dev, 0x3210, RTSI_Trig_A_Output_Register);
4260 devpriv->stc_writew(dev, 0x0654, RTSI_Trig_B_Output_Register);
4262 // Sets the source and direction of the 4 on board lines
4263 // devpriv->stc_writew(dev, 0x0000, RTSI_Board_Register);
4266 static int ni_rtsi_insn_bits(comedi_device *dev,comedi_subdevice *s,
4267 comedi_insn *insn,lsampl_t *data)
4269 if(insn->n != 2) return -EINVAL;
4276 static int ni_rtsi_insn_config(comedi_device *dev,comedi_subdevice *s,
4277 comedi_insn *insn,lsampl_t *data)
4282 if(insn->n < 1) return -EINVAL;
4284 if(data[0] == INSN_CONFIG_SET_RTSI_CLOCK_MODE){
4288 devpriv->rtsi_trig_direction_reg &= ~0x03;
4289 devpriv->rtsi_trig_direction_reg |= data[1];
4290 devpriv->stc_writew(dev, devpriv->rtsi_trig_direction_reg, RTSI_Trig_Direction_Register);
4293 chan = CR_CHAN(insn->chanspec);
4294 if(chan > 6) return -EINVAL;
4299 case INSN_CONFIG_DIO_OUTPUT:
4300 devpriv->rtsi_trig_direction_reg |= (1 << bit);
4301 devpriv->stc_writew(dev, devpriv->rtsi_trig_direction_reg, RTSI_Trig_Direction_Register);
4303 case INSN_CONFIG_DIO_INPUT:
4304 devpriv->rtsi_trig_direction_reg &= ~(1 << bit);
4305 devpriv->stc_writew(dev, devpriv->rtsi_trig_direction_reg, RTSI_Trig_Direction_Register);
4307 case INSN_CONFIG_DIO_QUERY:
4308 data[1] = (devpriv->rtsi_trig_direction_reg & (1<<bit)) ? INSN_CONFIG_DIO_OUTPUT : INSN_CONFIG_DIO_INPUT;
4319 static int cs5529_wait_for_idle(comedi_device *dev)
4321 unsigned short status;
4322 const int timeout = HZ;
4325 for(i = 0; i < timeout; i++)
4327 status = ni_ao_win_inw(dev, CAL_ADC_Status_67xx);
4328 if((status & CSS_ADC_BUSY) == 0)
4332 set_current_state(TASK_INTERRUPTIBLE);
4333 if(schedule_timeout(1))
4338 //printk("looped %i times waiting for idle\n", i);
4341 rt_printk("%s: %s: timeout\n", __FILE__, __FUNCTION__);
4347 static void cs5529_command(comedi_device *dev, unsigned short value)
4349 static const int timeout = 100;
4352 ni_ao_win_outw(dev, value, CAL_ADC_Command_67xx);
4353 /* give time for command to start being serially clocked into cs5529.
4354 * this insures that the CSS_ADC_BUSY bit will get properly
4355 * set before we exit this function.
4357 for(i = 0; i < timeout; i++)
4359 if((ni_ao_win_inw(dev, CAL_ADC_Status_67xx) & CSS_ADC_BUSY))
4363 //printk("looped %i times writing command to cs5529\n", i);
4366 comedi_error(dev, "possible problem - never saw adc go busy?");
4370 /* write to cs5529 register */
4371 static void cs5529_config_write(comedi_device *dev, unsigned int value, unsigned int reg_select_bits)
4373 ni_ao_win_outw(dev, ((value >> 16) & 0xff), CAL_ADC_Config_Data_High_Word_67xx);
4374 ni_ao_win_outw(dev, (value & 0xffff), CAL_ADC_Config_Data_Low_Word_67xx);
4375 reg_select_bits &= CSCMD_REGISTER_SELECT_MASK;
4376 cs5529_command(dev, CSCMD_COMMAND | reg_select_bits);
4377 if(cs5529_wait_for_idle(dev))
4378 comedi_error(dev, "time or signal in cs5529_config_write()");
4381 /* read from cs5529 register */
4382 static unsigned int cs5529_config_read(comedi_device *dev, unsigned int reg_select_bits)
4386 reg_select_bits &= CSCMD_REGISTER_SELECT_MASK;
4387 cs5529_command(dev, CSCMD_COMMAND | CSCMD_READ | reg_select_bits);
4388 if(cs5529_wait_for_idle(dev))
4389 comedi_error(dev, "timeout or signal in cs5529_config_read()");
4390 value = (ni_ao_win_inw(dev, CAL_ADC_Config_Data_High_Word_67xx) << 16) & 0xff0000;
4391 value |= ni_ao_win_inw(dev, CAL_ADC_Config_Data_Low_Word_67xx) & 0xffff;
4395 static int cs5529_do_conversion(comedi_device *dev, unsigned short *data)
4398 unsigned short status;
4400 cs5529_command(dev, CSCMD_COMMAND | CSCMD_SINGLE_CONVERSION);
4401 retval = cs5529_wait_for_idle(dev);
4404 comedi_error(dev, "timeout or signal in cs5529_do_conversion()");
4407 status = ni_ao_win_inw(dev, CAL_ADC_Status_67xx);
4408 if(status & CSS_OSC_DETECT)
4410 rt_printk("ni_mio_common: cs5529 conversion error, status CSS_OSC_DETECT\n");
4413 if(status & CSS_OVERRANGE)
4415 rt_printk("ni_mio_common: cs5529 conversion error, overrange (ignoring)\n");
4419 *data = ni_ao_win_inw(dev, CAL_ADC_Data_67xx);
4420 /* cs5529 returns 16 bit signed data in bipolar mode */
4426 static int cs5529_ai_insn_read(comedi_device *dev,comedi_subdevice *s,comedi_insn *insn,lsampl_t *data)
4429 unsigned short sample;
4430 unsigned int channel_select;
4431 const unsigned int INTERNAL_REF = 0x1000;
4433 /* Set calibration adc source. Docs lie, reference select bits 8 to 11
4434 * do nothing. bit 12 seems to chooses internal reference voltage, bit
4435 * 13 causes the adc input to go overrange (maybe reads external reference?) */
4436 if(insn->chanspec & CR_ALT_SOURCE)
4437 channel_select = INTERNAL_REF;
4439 channel_select = CR_CHAN(insn->chanspec);
4440 ni_ao_win_outw(dev, channel_select, AO_Calibration_Channel_Select_67xx);
4442 for(n = 0; n < insn->n; n++)
4444 retval = cs5529_do_conversion(dev, &sample);
4445 if(retval < 0) return retval;
4451 static int init_cs5529(comedi_device *dev)
4453 unsigned int config_bits = CSCFG_PORT_MODE | CSCFG_WORD_RATE_2180_CYCLES;
4456 /* do self-calibration */
4457 cs5529_config_write(dev, config_bits | CSCFG_SELF_CAL_OFFSET_GAIN, CSCMD_CONFIG_REGISTER);
4458 /* need to force a conversion for calibration to run */
4459 cs5529_do_conversion(dev, NULL);
4461 /* force gain calibration to 1 */
4462 cs5529_config_write(dev, 0x400000, CSCMD_GAIN_REGISTER);
4463 cs5529_config_write(dev, config_bits | CSCFG_SELF_CAL_OFFSET, CSCMD_CONFIG_REGISTER);
4464 if(cs5529_wait_for_idle(dev))
4465 comedi_error(dev, "timeout or signal in init_cs5529()\n");
4469 rt_printk("config: 0x%x\n", cs5529_config_read(dev, CSCMD_CONFIG_REGISTER));
4470 rt_printk("gain: 0x%x\n", cs5529_config_read(dev, CSCMD_GAIN_REGISTER));
4471 rt_printk("offset: 0x%x\n", cs5529_config_read(dev, CSCMD_OFFSET_REGISTER));