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 range_code;
1419 devpriv->stc_writew(dev, 1, Configuration_Memory_Clear);
1421 // offset = 1 << (boardtype.adbits - 1);
1422 if((list[0] & CR_ALT_SOURCE))
1424 chan = CR_CHAN(list[0]);
1425 range = CR_RANGE(list[0]);
1426 range_code = ni_gainlkup[boardtype.gainlkup][range];
1427 dither = ((list[0] & CR_ALT_FILTER) != 0);
1428 unsigned bypass_bits = MSeries_AI_Bypass_Config_FIFO_Bit;
1429 bypass_bits |= chan;
1430 bypass_bits |= (devpriv->ai_calib_source) & (MSeries_AI_Bypass_Cal_Sel_Pos_Mask |
1431 MSeries_AI_Bypass_Cal_Sel_Neg_Mask | MSeries_AI_Bypass_Mode_Mux_Mask |
1432 MSeries_AO_Bypass_AO_Cal_Sel_Mask);
1433 bypass_bits |= MSeries_AI_Bypass_Gain_Bits(range_code);
1435 bypass_bits |= MSeries_AI_Bypass_Dither_Bit;
1436 // don't use 2's complement encoding
1437 bypass_bits |= MSeries_AI_Bypass_Polarity_Bit;
1438 ni_writel(bypass_bits, M_Offset_AI_Config_FIFO_Bypass);
1441 ni_writel(0, M_Offset_AI_Config_FIFO_Bypass);
1444 for(i = 0; i < n_chan; i++)
1446 chan = CR_CHAN(list[i]);
1447 aref = CR_AREF(list[i]);
1448 range = CR_RANGE(list[i]);
1449 dither = ((list[i] & CR_ALT_FILTER) != 0);
1451 range_code = ni_gainlkup[boardtype.gainlkup][range];
1452 devpriv->ai_offset[i] = offset;
1457 config_bits |= MSeries_AI_Config_Channel_Type_Differential_Bits;
1460 config_bits |= MSeries_AI_Config_Channel_Type_Common_Ref_Bits;
1463 config_bits |= MSeries_AI_Config_Channel_Type_Ground_Ref_Bits;
1468 config_bits |= MSeries_AI_Config_Channel_Bits(chan);
1469 config_bits |= MSeries_AI_Config_Bank_Bits(chan);
1470 config_bits |= MSeries_AI_Config_Gain_Bits(range_code);
1471 if(i == n_chan - 1) config_bits |= MSeries_AI_Config_Last_Channel_Bit;
1472 if(dither) config_bits |= MSeries_AI_Config_Dither_Bit;
1473 // don't use 2's complement encoding
1474 config_bits |= MSeries_AI_Config_Polarity_Bit;
1475 ni_writew(config_bits, M_Offset_AI_Config_FIFO_Data);
1477 ni_prime_channelgain_list(dev);
1481 * Notes on the 6110 and 6111:
1482 * These boards a slightly different than the rest of the series, since
1483 * they have multiple A/D converters.
1484 * From the driver side, the configuration memory is a
1486 * Configuration Memory Low:
1488 * bit 8: unipolar/bipolar (should be 0 for bipolar)
1489 * bits 0-3: gain. This is 4 bits instead of 3 for the other boards
1490 * 1001 gain=0.1 (+/- 50)
1499 * Configuration Memory High:
1500 * bits 12-14: Channel Type
1501 * 001 for differential
1502 * 000 for calibration
1503 * bit 11: coupling (this is not currently handled)
1507 * valid channels are 0-3
1509 static void ni_load_channelgain_list(comedi_device *dev,unsigned int n_chan,
1512 unsigned int chan,range,aref;
1516 unsigned int dither;
1518 if(boardtype.reg_type == ni_reg_m_series)
1520 ni_m_series_load_channelgain_list(dev, n_chan, list);
1523 if(n_chan == 1 && (boardtype.reg_type != ni_reg_611x) && (boardtype.reg_type != ni_reg_6143)){
1524 if(devpriv->changain_state && devpriv->changain_spec==list[0]){
1528 devpriv->changain_state=1;
1529 devpriv->changain_spec=list[0];
1531 devpriv->changain_state=0;
1534 devpriv->stc_writew(dev, 1,Configuration_Memory_Clear);
1536 // Set up Calibration mode if required
1537 if(boardtype.reg_type == ni_reg_6143){
1538 if((list[0] & CR_ALT_SOURCE) && !devpriv->ai_calib_source_enabled){
1539 // Strobe Relay enable bit
1540 ni_writew(devpriv->ai_calib_source | Calibration_Channel_6143_RelayOn, Calibration_Channel_6143);
1541 ni_writew(devpriv->ai_calib_source, Calibration_Channel_6143);
1542 devpriv->ai_calib_source_enabled = 1;
1543 msleep_interruptible(100); // Allow relays to change
1545 else if(!(list[0] & CR_ALT_SOURCE) && devpriv->ai_calib_source_enabled){
1546 // Strobe Relay disable bit
1547 ni_writew(devpriv->ai_calib_source | Calibration_Channel_6143_RelayOff, Calibration_Channel_6143);
1548 ni_writew(devpriv->ai_calib_source, Calibration_Channel_6143);
1549 devpriv->ai_calib_source_enabled = 0;
1550 msleep_interruptible(100); // Allow relays to change
1554 offset=1<<(boardtype.adbits-1);
1555 for(i=0;i<n_chan;i++){
1556 if((boardtype.reg_type != ni_reg_6143) && (list[i] & CR_ALT_SOURCE)){
1557 chan=devpriv->ai_calib_source;
1559 chan=CR_CHAN(list[i]);
1561 aref=CR_AREF(list[i]);
1562 range=CR_RANGE(list[i]);
1563 dither=((list[i]&CR_ALT_FILTER)!=0);
1565 /* fix the external/internal range differences */
1566 range = ni_gainlkup[boardtype.gainlkup][range];
1567 if(boardtype.reg_type == ni_reg_611x)
1568 devpriv->ai_offset[i] = offset;
1570 devpriv->ai_offset[i] = (range&0x100)?0:offset;
1573 if( ( list[i] & CR_ALT_SOURCE ) )
1575 if(boardtype.reg_type == ni_reg_611x)
1576 ni_writew(CR_CHAN(list[i])&0x0003, Calibration_Channel_Select_611x);
1579 if(boardtype.reg_type == ni_reg_611x)
1581 else if(boardtype.reg_type == ni_reg_6143)
1586 hi |= AI_DIFFERENTIAL;
1598 hi |= AI_CONFIG_CHANNEL( chan );
1600 ni_writew(hi,Configuration_Memory_High);
1602 if(boardtype.reg_type != ni_reg_6143){
1604 if(i == n_chan - 1) lo |= AI_LAST_CHANNEL;
1605 if( dither ) lo |= AI_DITHER;
1607 ni_writew(lo,Configuration_Memory_Low);
1611 /* prime the channel/gain list */
1612 if((boardtype.reg_type != ni_reg_611x) && (boardtype.reg_type != ni_reg_6143)){
1613 ni_prime_channelgain_list(dev);
1617 #define TIMER_BASE 50 /* 20 Mhz base */
1619 static int ni_ns_to_timer(int *nanosec,int round_mode)
1626 case TRIG_ROUND_NEAREST:
1628 divider=(*nanosec+base/2)/base;
1630 case TRIG_ROUND_DOWN:
1631 divider=(*nanosec)/base;
1634 divider=(*nanosec+base-1)/base;
1638 *nanosec=base*divider;
1642 static int ni_ai_cmdtest(comedi_device *dev,comedi_subdevice *s,comedi_cmd *cmd)
1648 /* step 1: make sure trigger sources are trivially valid */
1651 cmd->start_src &= TRIG_NOW|TRIG_INT|TRIG_EXT;
1652 if(!cmd->start_src || tmp!=cmd->start_src)err++;
1654 tmp=cmd->scan_begin_src;
1655 cmd->scan_begin_src &= TRIG_TIMER|TRIG_EXT;
1656 if(!cmd->scan_begin_src || tmp!=cmd->scan_begin_src)err++;
1658 tmp=cmd->convert_src;
1659 sources = TRIG_TIMER | TRIG_EXT;
1660 if((boardtype.reg_type == ni_reg_611x) || (boardtype.reg_type == ni_reg_6143)) sources |= TRIG_NOW;
1661 cmd->convert_src &= sources;
1662 if(!cmd->convert_src || tmp!=cmd->convert_src)err++;
1664 tmp=cmd->scan_end_src;
1665 cmd->scan_end_src &= TRIG_COUNT;
1666 if(!cmd->scan_end_src || tmp!=cmd->scan_end_src)err++;
1669 cmd->stop_src &= TRIG_COUNT|TRIG_NONE;
1670 if(!cmd->stop_src || tmp!=cmd->stop_src)err++;
1674 /* step 2: make sure trigger sources are unique and mutually compatible */
1676 /* note that mutual compatiblity is not an issue here */
1677 if(cmd->start_src!=TRIG_NOW &&
1678 cmd->start_src!=TRIG_INT &&
1679 cmd->start_src!=TRIG_EXT)err++;
1680 if(cmd->scan_begin_src!=TRIG_TIMER &&
1681 cmd->scan_begin_src!=TRIG_EXT &&
1682 cmd->scan_begin_src!=TRIG_OTHER)err++;
1683 if(cmd->convert_src!=TRIG_TIMER &&
1684 cmd->convert_src!=TRIG_EXT &&
1685 cmd->convert_src!=TRIG_NOW)err++;
1686 if(cmd->stop_src!=TRIG_COUNT &&
1687 cmd->stop_src!=TRIG_NONE)err++;
1691 /* step 3: make sure arguments are trivially compatible */
1693 if(cmd->start_src==TRIG_EXT){
1694 /* external trigger */
1695 unsigned int tmp = CR_CHAN(cmd->start_arg);
1697 if(tmp > 16) tmp = 16;
1698 tmp |= (cmd->start_arg & (CR_INVERT | CR_EDGE));
1699 if(cmd->start_arg != tmp){
1700 cmd->start_arg = tmp;
1704 if(cmd->start_arg!=0){
1705 /* true for both TRIG_NOW and TRIG_INT */
1710 if(cmd->scan_begin_src==TRIG_TIMER){
1711 if(cmd->scan_begin_arg<boardtype.ai_speed){
1712 cmd->scan_begin_arg=boardtype.ai_speed;
1715 if(cmd->scan_begin_arg>TIMER_BASE*0xffffff){
1716 cmd->scan_begin_arg=TIMER_BASE*0xffffff;
1719 }else if(cmd->scan_begin_src==TRIG_EXT){
1720 /* external trigger */
1721 unsigned int tmp = CR_CHAN(cmd->scan_begin_arg);
1724 tmp |= (cmd->scan_begin_arg & (CR_INVERT | CR_EDGE));
1725 if(cmd->scan_begin_arg!=tmp){
1726 cmd->scan_begin_arg = tmp;
1729 }else{ /* TRIG_OTHER */
1730 if(cmd->scan_begin_arg){
1731 cmd->scan_begin_arg=0;
1735 if(cmd->convert_src==TRIG_TIMER){
1736 if((boardtype.reg_type == ni_reg_611x) || (boardtype.reg_type == ni_reg_6143)){
1737 if(cmd->convert_arg != 0){
1738 cmd->convert_arg = 0;
1742 if(cmd->convert_arg<boardtype.ai_speed){
1743 cmd->convert_arg=boardtype.ai_speed;
1746 if(cmd->convert_arg>TIMER_BASE*0xffff){
1747 cmd->convert_arg=TIMER_BASE*0xffff;
1751 }else if(cmd->convert_src == TRIG_EXT){
1752 /* external trigger */
1753 unsigned int tmp = CR_CHAN(cmd->convert_arg);
1756 tmp |= (cmd->convert_arg&(CR_ALT_FILTER|CR_INVERT));
1757 if(cmd->convert_arg!=tmp){
1758 cmd->convert_arg = tmp;
1761 }else if(cmd->convert_src == TRIG_NOW){
1762 if(cmd->convert_arg != 0){
1763 cmd->convert_arg = 0;
1768 if(cmd->scan_end_arg!=cmd->chanlist_len){
1769 cmd->scan_end_arg=cmd->chanlist_len;
1772 if(cmd->stop_src==TRIG_COUNT){
1773 unsigned int max_count = 0x01000000;
1775 if(boardtype.reg_type == ni_reg_611x )
1776 max_count -= num_adc_stages_611x;
1777 if(cmd->stop_arg > max_count){
1778 cmd->stop_arg = max_count;
1781 if(cmd->stop_arg < 1){
1787 if(cmd->stop_arg!=0){
1795 /* step 4: fix up any arguments */
1797 if(cmd->scan_begin_src==TRIG_TIMER){
1798 tmp=cmd->scan_begin_arg;
1799 ni_ns_to_timer(&cmd->scan_begin_arg,cmd->flags&TRIG_ROUND_MASK);
1800 if(tmp!=cmd->scan_begin_arg)err++;
1802 if(cmd->convert_src==TRIG_TIMER){
1803 if((boardtype.reg_type != ni_reg_611x) && (boardtype.reg_type != ni_reg_6143)){
1804 tmp=cmd->convert_arg;
1805 ni_ns_to_timer(&cmd->convert_arg,cmd->flags&TRIG_ROUND_MASK);
1806 if(tmp!=cmd->convert_arg)err++;
1807 if(cmd->scan_begin_src==TRIG_TIMER &&
1808 cmd->scan_begin_arg<cmd->convert_arg*cmd->scan_end_arg){
1809 cmd->scan_begin_arg=cmd->convert_arg*cmd->scan_end_arg;
1820 static int ni_ai_cmd(comedi_device *dev,comedi_subdevice *s)
1822 comedi_cmd *cmd=&s->async->cmd;
1824 int mode1=0; /* mode1 is needed for both stop and convert */
1826 int start_stop_select=0;
1827 unsigned int stop_count;
1828 int interrupt_a_enable=0;
1830 MDPRINTK("ni_ai_cmd\n");
1833 comedi_error(dev, "cannot run command without an irq");
1836 ni_flush_ai_fifo(dev);
1838 ni_load_channelgain_list(dev,cmd->chanlist_len,cmd->chanlist);
1840 /* start configuration */
1841 devpriv->stc_writew(dev, AI_Configuration_Start,Joint_Reset_Register);
1843 /* disable analog triggering for now, since it
1844 * interferes with the use of pfi0 */
1845 devpriv->an_trig_etc_reg &= ~Analog_Trigger_Enable;
1846 devpriv->stc_writew(dev, devpriv->an_trig_etc_reg, Analog_Trigger_Etc_Register);
1848 switch(cmd->start_src){
1851 devpriv->stc_writew(dev, AI_START2_Select(0)|
1852 AI_START1_Sync|AI_START1_Edge|AI_START1_Select(0),
1853 AI_Trigger_Select_Register);
1857 int chan = CR_CHAN(cmd->start_arg);
1858 unsigned int bits = AI_START2_Select(0)|
1860 AI_START1_Select(chan + 1);
1862 if(cmd->start_arg & CR_INVERT)
1863 bits |= AI_START1_Polarity;
1864 if(cmd->start_arg & CR_EDGE)
1865 bits |= AI_START1_Edge;
1866 devpriv->stc_writew(dev, bits, AI_Trigger_Select_Register);
1871 mode2 &= ~AI_Pre_Trigger;
1872 mode2 &= ~AI_SC_Initial_Load_Source;
1873 mode2 &= ~AI_SC_Reload_Mode;
1874 devpriv->stc_writew(dev, mode2, AI_Mode_2_Register);
1876 if(cmd->chanlist_len == 1 || (boardtype.reg_type == ni_reg_611x) || (boardtype.reg_type == ni_reg_6143)){
1877 start_stop_select |= AI_STOP_Polarity;
1878 start_stop_select |= AI_STOP_Select( 31 ); // logic low
1879 start_stop_select |= AI_STOP_Sync;
1882 start_stop_select |= AI_STOP_Select(19); // ai configuration memory
1884 devpriv->stc_writew(dev, start_stop_select, AI_START_STOP_Select_Register);
1886 devpriv->ai_cmd2 = 0;
1887 switch(cmd->stop_src){
1889 stop_count = cmd->stop_arg - 1;
1891 if(boardtype.reg_type == ni_reg_611x){
1892 // have to take 3 stage adc pipeline into account
1893 stop_count += num_adc_stages_611x;
1895 /* stage number of scans */
1896 devpriv->stc_writel(dev, stop_count, AI_SC_Load_A_Registers);
1898 mode1 |= AI_Start_Stop | AI_Mode_1_Reserved | AI_Trigger_Once;
1899 devpriv->stc_writew(dev, mode1,AI_Mode_1_Register);
1900 /* load SC (Scan Count) */
1901 devpriv->stc_writew(dev, AI_SC_Load,AI_Command_1_Register);
1903 devpriv->ai_continuous = 0;
1904 if( stop_count == 0 ){
1905 devpriv->ai_cmd2 |= AI_End_On_End_Of_Scan;
1906 interrupt_a_enable |= AI_STOP_Interrupt_Enable;
1907 // this is required to get the last sample for chanlist_len > 1, not sure why
1908 if(cmd->chanlist_len > 1)
1909 start_stop_select |= AI_STOP_Polarity | AI_STOP_Edge;
1913 /* stage number of scans */
1914 devpriv->stc_writel(dev, 0,AI_SC_Load_A_Registers);
1916 mode1 |= AI_Start_Stop | AI_Mode_1_Reserved | AI_Continuous;
1917 devpriv->stc_writew(dev, mode1,AI_Mode_1_Register);
1919 /* load SC (Scan Count) */
1920 devpriv->stc_writew(dev, AI_SC_Load,AI_Command_1_Register);
1922 devpriv->ai_continuous = 1;
1927 switch(cmd->scan_begin_src){
1930 stop bits for non 611x boards
1931 AI_SI_Special_Trigger_Delay=0
1933 AI_START_STOP_Select_Register:
1934 AI_START_Polarity=0 (?) rising edge
1935 AI_START_Edge=1 edge triggered
1937 AI_START_Select=0 SI_TC
1938 AI_STOP_Polarity=0 rising edge
1939 AI_STOP_Edge=0 level
1941 AI_STOP_Select=19 external pin (configuration mem)
1943 start_stop_select |= AI_START_Edge | AI_START_Sync;
1944 devpriv->stc_writew(dev, start_stop_select, AI_START_STOP_Select_Register);
1946 mode2 |= AI_SI_Reload_Mode(0);
1947 /* AI_SI_Initial_Load_Source=A */
1948 mode2 &= ~AI_SI_Initial_Load_Source;
1949 //mode2 |= AI_SC_Reload_Mode;
1950 devpriv->stc_writew(dev, mode2, AI_Mode_2_Register);
1953 timer=ni_ns_to_timer(&cmd->scan_begin_arg,TRIG_ROUND_NEAREST);
1954 devpriv->stc_writel(dev, timer,AI_SI_Load_A_Registers);
1955 devpriv->stc_writew(dev, AI_SI_Load,AI_Command_1_Register);
1958 if( cmd->scan_begin_arg & CR_EDGE )
1959 start_stop_select |= AI_START_Edge;
1960 /* AI_START_Polarity==1 is falling edge */
1961 if( cmd->scan_begin_arg & CR_INVERT )
1962 start_stop_select |= AI_START_Polarity;
1963 if( cmd->scan_begin_src != cmd->convert_src ||
1964 ( cmd->scan_begin_arg & ~CR_EDGE ) != ( cmd->convert_arg & ~CR_EDGE ) )
1965 start_stop_select |= AI_START_Sync;
1966 start_stop_select |= AI_START_Select(1 + CR_CHAN(cmd->scan_begin_arg));
1967 devpriv->stc_writew(dev, start_stop_select, AI_START_STOP_Select_Register);
1971 switch(cmd->convert_src){
1974 if( cmd->convert_arg == 0 || cmd->convert_src == TRIG_NOW )
1977 timer=ni_ns_to_timer(&cmd->convert_arg, TRIG_ROUND_NEAREST);
1978 devpriv->stc_writew(dev, 1,AI_SI2_Load_A_Register); /* 0,0 does not work. */
1979 devpriv->stc_writew(dev, timer,AI_SI2_Load_B_Register);
1981 /* AI_SI2_Reload_Mode = alternate */
1982 /* AI_SI2_Initial_Load_Source = A */
1983 mode2 &= ~AI_SI2_Initial_Load_Source;
1984 mode2 |= AI_SI2_Reload_Mode;
1985 devpriv->stc_writew(dev, mode2, AI_Mode_2_Register);
1988 devpriv->stc_writew(dev, AI_SI2_Load,AI_Command_1_Register);
1990 mode2 |= AI_SI2_Reload_Mode; // alternate
1991 mode2 |= AI_SI2_Initial_Load_Source; // B
1993 devpriv->stc_writew(dev, mode2,AI_Mode_2_Register);
1996 mode1 |= AI_CONVERT_Source_Select(1+cmd->convert_arg);
1997 if( ( cmd->convert_arg & CR_INVERT ) == 0 )
1998 mode1 |= AI_CONVERT_Source_Polarity;
1999 devpriv->stc_writew(dev, mode1,AI_Mode_1_Register);
2001 mode2 |= AI_Start_Stop_Gate_Enable | AI_SC_Gate_Enable;
2002 devpriv->stc_writew(dev, mode2, AI_Mode_2_Register);
2009 /* interrupt on FIFO, errors, SC_TC */
2010 interrupt_a_enable |= AI_Error_Interrupt_Enable|
2011 AI_SC_TC_Interrupt_Enable;
2014 interrupt_a_enable|=AI_FIFO_Interrupt_Enable;
2017 if(cmd->flags & TRIG_WAKE_EOS || (devpriv->ai_cmd2 & AI_End_On_End_Of_Scan)){
2018 /* wake on end-of-scan */
2019 devpriv->aimode=AIMODE_SCAN;
2021 devpriv->aimode=AIMODE_HALF_FULL;
2024 switch(devpriv->aimode){
2025 case AIMODE_HALF_FULL:
2026 /*generate FIFO interrupts and DMA requests on half-full */
2028 devpriv->stc_writew(dev, AI_FIFO_Mode_HF_to_E, AI_Mode_3_Register);
2030 devpriv->stc_writew(dev, AI_FIFO_Mode_HF, AI_Mode_3_Register);
2034 /*generate FIFO interrupts on non-empty */
2035 devpriv->stc_writew(dev, AI_FIFO_Mode_NE, AI_Mode_3_Register);
2039 devpriv->stc_writew(dev, AI_FIFO_Mode_NE, AI_Mode_3_Register);
2041 devpriv->stc_writew(dev, AI_FIFO_Mode_HF, AI_Mode_3_Register);
2043 interrupt_a_enable |= AI_STOP_Interrupt_Enable;
2049 devpriv->stc_writew(dev, 0x3f80,Interrupt_A_Ack_Register); /* clear interrupts */
2051 ni_set_bits(dev, Interrupt_A_Enable_Register, interrupt_a_enable, 1);
2053 MDPRINTK("Interrupt_A_Enable_Register = 0x%04x\n",devpriv->int_a_enable_reg);
2055 /* interrupt on nothing */
2056 ni_set_bits(dev, Interrupt_A_Enable_Register, ~0, 0);
2058 /* XXX start polling if necessary */
2059 MDPRINTK("interrupting on nothing\n");
2062 /* end configuration */
2063 devpriv->stc_writew(dev, AI_Configuration_End,Joint_Reset_Register);
2065 switch(cmd->scan_begin_src){
2067 devpriv->stc_writew(dev, AI_SI2_Arm | AI_SI_Arm | AI_DIV_Arm | AI_SC_Arm,
2068 AI_Command_1_Register);
2071 /* XXX AI_SI_Arm? */
2072 devpriv->stc_writew(dev, AI_SI2_Arm | AI_SI_Arm | AI_DIV_Arm | AI_SC_Arm,
2073 AI_Command_1_Register);
2078 ni_ai_setup_MITE_dma(dev,cmd);
2079 //mite_dump_regs(devpriv->mite);
2082 switch(cmd->start_src){
2084 /* AI_START1_Pulse */
2085 devpriv->stc_writew(dev, AI_START1_Pulse | devpriv->ai_cmd2, AI_Command_2_Register );
2086 s->async->inttrig=NULL;
2089 s->async->inttrig=NULL;
2092 s->async->inttrig=ni_ai_inttrig;
2096 MDPRINTK("exit ni_ai_cmd\n");
2101 static int ni_ai_inttrig(comedi_device *dev,comedi_subdevice *s,
2102 unsigned int trignum)
2104 if(trignum!=0)return -EINVAL;
2106 devpriv->stc_writew(dev, AI_START1_Pulse | devpriv->ai_cmd2, AI_Command_2_Register );
2107 s->async->inttrig=NULL;
2112 static int ni_ai_config_analog_trig(comedi_device *dev,comedi_subdevice *s,
2113 comedi_insn *insn, lsampl_t *data);
2115 static int ni_ai_insn_config(comedi_device *dev,comedi_subdevice *s,
2116 comedi_insn *insn, lsampl_t *data)
2118 if(insn->n<1)return -EINVAL;
2121 case INSN_CONFIG_ANALOG_TRIG:
2122 return ni_ai_config_analog_trig(dev,s,insn,data);
2123 case INSN_CONFIG_ALT_SOURCE:
2124 if(boardtype.reg_type == ni_reg_m_series)
2126 if(data[1] & ~(MSeries_AI_Bypass_Cal_Sel_Pos_Mask |
2127 MSeries_AI_Bypass_Cal_Sel_Neg_Mask | MSeries_AI_Bypass_Mode_Mux_Mask |
2128 MSeries_AO_Bypass_AO_Cal_Sel_Mask))
2132 devpriv->ai_calib_source = data[1];
2133 } else if(boardtype.reg_type == ni_reg_6143)
2135 unsigned int calib_source;
2137 calib_source = data[1] & 0xf;
2140 if(calib_source > 0xF)
2143 devpriv->ai_calib_source = calib_source;
2144 ni_writew(calib_source, Calibration_Channel_6143);
2147 unsigned int calib_source;
2148 unsigned int calib_source_adjust;
2150 calib_source = data[1] & 0xf;
2151 calib_source_adjust = ( data[1] >> 4 ) & 0xff;
2153 if(calib_source >= 8)
2155 devpriv->ai_calib_source = calib_source;
2156 if(boardtype.reg_type == ni_reg_611x){
2157 ni_writeb( calib_source_adjust, Cal_Gain_Select_611x );
2168 static int ni_ai_config_analog_trig(comedi_device *dev,comedi_subdevice *s,
2169 comedi_insn *insn, lsampl_t *data)
2171 unsigned int a,b,modebits;
2175 * data[2] is analog line
2176 * data[3] is set level
2177 * data[4] is reset level */
2178 if(!boardtype.has_analog_trig)return -EINVAL;
2179 if(insn->n!=5)return -EINVAL;
2180 if((data[1]&0xffff0000) != COMEDI_EV_SCAN_BEGIN){
2181 data[1]&=~(COMEDI_EV_SCAN_BEGIN&0xffff);
2184 if(data[2]>=boardtype.n_adchan){
2185 data[2]=boardtype.n_adchan-1;
2188 if(data[3]>255){ /* a */
2192 if(data[4]>255){ /* b */
2203 * high mode 00 00 01 10
2204 * low mode 00 00 10 01
2206 * hysteresis low mode 10 00 00 01
2207 * hysteresis high mode 01 00 00 10
2208 * middle mode 10 01 01 10
2213 modebits=data[1]&0xff;
2215 /* two level mode */
2220 modebits=((data[1]&0xf)<<4)|((data[1]&0xf0)>>4);
2222 devpriv->atrig_low = a;
2223 devpriv->atrig_high = b;
2225 case 0x81: /* low hysteresis mode */
2226 devpriv->atrig_mode = 6;
2228 case 0x42: /* high hysteresis mode */
2229 devpriv->atrig_mode = 3;
2231 case 0x96: /* middle window mode */
2232 devpriv->atrig_mode = 2;
2239 /* one level mode */
2245 case 0x06: /* high window mode */
2246 devpriv->atrig_high = a;
2247 devpriv->atrig_mode = 0;
2249 case 0x09: /* low window mode */
2250 devpriv->atrig_low = a;
2251 devpriv->atrig_mode = 1;
2258 if(err)return -EAGAIN;
2262 /* munge data from unsigned to 2's complement for analog output bipolar modes */
2263 static void ni_ao_munge(comedi_device *dev, comedi_subdevice *s,
2264 void *data, unsigned int num_bytes, unsigned int chan_index )
2266 comedi_async *async = s->async;
2269 unsigned int offset;
2270 unsigned int length = num_bytes / sizeof( sampl_t );
2271 sampl_t *array = data;
2273 offset = 1 << (boardtype.aobits - 1);
2274 for(i = 0; i < length; i++)
2276 range = CR_RANGE( async->cmd.chanlist[ chan_index ] );
2277 if(boardtype.ao_unipolar == 0 || (range & 1) == 0 )
2280 array[i] = cpu_to_le16( array[i] );
2283 chan_index %= async->cmd.chanlist_len;
2287 static int ni_ao_config_chanlist(comedi_device *dev, comedi_subdevice *s,
2288 unsigned int chanspec[], unsigned int n_chans)
2296 for(i=0;i<n_chans;i++){
2297 chan = CR_CHAN(chanspec[i]);
2298 range = CR_RANGE(chanspec[i]);
2299 if(boardtype.reg_type == ni_reg_m_series)
2301 comedi_krange *krange = s->range_table->range + range;
2304 switch(krange->max - krange->min)
2307 conf |= MSeries_AO_DAC_Reference_10V_Internal_Bits;
2308 ni_writeb(0, M_Offset_AO_Reference_Attenuation(chan));
2311 conf |= MSeries_AO_DAC_Reference_5V_Internal_Bits;
2312 ni_writeb(0, M_Offset_AO_Reference_Attenuation(chan));
2315 conf |= MSeries_AO_DAC_Reference_10V_Internal_Bits;
2316 ni_writeb(MSeries_Attenuate_x5_Bit, M_Offset_AO_Reference_Attenuation(chan));
2319 conf |= MSeries_AO_DAC_Reference_5V_Internal_Bits;
2320 ni_writeb(MSeries_Attenuate_x5_Bit, M_Offset_AO_Reference_Attenuation(chan));
2323 rt_printk("%s: bug! unhandled ao reference voltage\n", __FUNCTION__);
2326 switch(krange->max + krange->min)
2329 conf |= MSeries_AO_DAC_Offset_0V_Bits;
2332 conf |= MSeries_AO_DAC_Offset_5V_Bits;
2335 rt_printk("%s: bug! unhandled ao offset voltage\n", __FUNCTION__);
2338 ni_writeb(conf, M_Offset_AO_Config_Bank(chan));
2341 conf = AO_Channel(chan);
2343 if(boardtype.ao_unipolar){
2346 invert = (1<<(boardtype.aobits-1));
2354 invert = (1<<(boardtype.aobits-1));
2357 /* not all boards can deglitch, but this shouldn't hurt */
2358 if(chanspec[i] & CR_DEGLITCH)
2359 conf |= AO_Deglitch;
2361 /* analog reference */
2362 /* AREF_OTHER connects AO ground to AI ground, i think */
2363 conf |= (CR_AREF(chanspec[i])==AREF_OTHER)? AO_Ground_Ref : 0;
2365 ni_writew(conf,AO_Configuration);
2367 devpriv->ao_conf[chan] = conf;
2372 static int ni_ao_insn_read(comedi_device *dev,comedi_subdevice *s,
2373 comedi_insn *insn,lsampl_t *data)
2375 data[0] = devpriv->ao[CR_CHAN(insn->chanspec)];
2380 static int ni_ao_insn_write(comedi_device *dev,comedi_subdevice *s,
2381 comedi_insn *insn,lsampl_t *data)
2383 unsigned int chan = CR_CHAN(insn->chanspec);
2384 unsigned int invert;
2386 invert = ni_ao_config_chanlist(dev,s,&insn->chanspec,1);
2388 devpriv->ao[chan] = data[0];
2390 if(boardtype.reg_type == ni_reg_m_series)
2392 ni_writew(data[0], M_Offset_DAC_Direct_Data(chan));
2395 ni_writew(data[0] ^ invert,(chan)? DAC1_Direct_Data : DAC0_Direct_Data);
2400 static int ni_ao_insn_write_671x(comedi_device *dev,comedi_subdevice *s,
2401 comedi_insn *insn,lsampl_t *data)
2403 unsigned int chan = CR_CHAN(insn->chanspec);
2404 unsigned int invert;
2406 ao_win_out(1 << chan, AO_Immediate_671x);
2407 invert = 1 << (boardtype.aobits - 1);
2409 ni_ao_config_chanlist(dev,s,&insn->chanspec,1);
2411 devpriv->ao[chan] = data[0];
2412 ao_win_out(data[0] ^ invert, DACx_Direct_Data_671x(chan));
2417 static int ni_ao_inttrig(comedi_device *dev,comedi_subdevice *s,
2418 unsigned int trignum)
2421 int interrupt_b_bits;
2423 static const int timeout = 1000;
2425 if(trignum!=0)return -EINVAL;
2427 ni_set_bits(dev, Interrupt_B_Enable_Register, AO_FIFO_Interrupt_Enable | AO_Error_Interrupt_Enable, 0);
2428 interrupt_b_bits = AO_Error_Interrupt_Enable;
2430 devpriv->stc_writew(dev, 1, DAC_FIFO_Clear);
2431 if(boardtype.reg_type & ni_reg_6xxx_mask)
2432 ni_ao_win_outl(dev, 0x6, AO_FIFO_Offset_Load_611x);
2433 ni_ao_setup_MITE_dma(dev, &s->async->cmd);
2434 ret = ni_ao_wait_for_dma_load(dev);
2435 if(ret < 0) return ret;
2438 ret = ni_ao_prep_fifo(dev,s);
2439 if(ret==0)return -EPIPE;
2441 interrupt_b_bits |= AO_FIFO_Interrupt_Enable;
2444 devpriv->stc_writew(dev, devpriv->ao_mode3|AO_Not_An_UPDATE,AO_Mode_3_Register);
2445 devpriv->stc_writew(dev, devpriv->ao_mode3,AO_Mode_3_Register);
2446 /* wait for DACs to be loaded */
2447 for(i = 0; i < timeout; i++)
2450 if((devpriv->stc_readw(dev, Joint_Status_2_Register) & AO_TMRDACWRs_In_Progress_St) == 0)
2455 comedi_error(dev, "timed out waiting for AO_TMRDACWRs_In_Progress_St to clear");
2458 // stc manual says we are need to clear error interrupt after AO_TMRDACWRs_In_Progress_St clears
2459 devpriv->stc_writew(dev, AO_Error_Interrupt_Ack, Interrupt_B_Ack_Register);
2461 ni_set_bits(dev, Interrupt_B_Enable_Register, interrupt_b_bits, 1);
2463 devpriv->stc_writew(dev, devpriv->ao_cmd1|AO_UI_Arm|AO_UC_Arm|AO_BC_Arm|AO_DAC1_Update_Mode|AO_DAC0_Update_Mode,
2464 AO_Command_1_Register);
2466 devpriv->stc_writew(dev, devpriv->ao_cmd2|AO_START1_Pulse,AO_Command_2_Register);
2468 s->async->inttrig=NULL;
2473 static int ni_ao_cmd(comedi_device *dev,comedi_subdevice *s)
2475 comedi_cmd *cmd = &s->async->cmd;
2482 comedi_error(dev, "cannot run command without an irq");
2485 trigvar = ni_ns_to_timer(&cmd->scan_begin_arg,TRIG_ROUND_NEAREST);
2487 devpriv->stc_writew(dev, AO_Configuration_Start,Joint_Reset_Register);
2489 devpriv->stc_writew(dev, AO_Disarm,AO_Command_1_Register);
2491 if(boardtype.reg_type & ni_reg_6xxx_mask)
2493 ao_win_out(CLEAR_WG, AO_Misc_611x);
2496 for(i = 0; i < cmd->chanlist_len; i++)
2500 chan = CR_CHAN(cmd->chanlist[i]);
2502 ao_win_out(chan, AO_Waveform_Generation_611x);
2504 ao_win_out(bits, AO_Timed_611x);
2507 ni_ao_config_chanlist(dev,s,cmd->chanlist,cmd->chanlist_len);
2509 if(cmd->stop_src==TRIG_NONE){
2510 devpriv->ao_mode1|=AO_Continuous;
2511 devpriv->ao_mode1&=~AO_Trigger_Once;
2513 devpriv->ao_mode1&=~AO_Continuous;
2514 devpriv->ao_mode1|=AO_Trigger_Once;
2516 devpriv->stc_writew(dev, devpriv->ao_mode1,AO_Mode_1_Register);
2517 devpriv->ao_trigger_select&=~(AO_START1_Polarity|AO_START1_Select(-1));
2518 devpriv->ao_trigger_select|=AO_START1_Edge|AO_START1_Sync;
2519 devpriv->stc_writew(dev, devpriv->ao_trigger_select,AO_Trigger_Select_Register);
2520 devpriv->ao_mode3&=~AO_Trigger_Length;
2521 devpriv->stc_writew(dev, devpriv->ao_mode3,AO_Mode_3_Register);
2523 devpriv->stc_writew(dev, devpriv->ao_mode1,AO_Mode_1_Register);
2524 devpriv->ao_mode2&=~AO_BC_Initial_Load_Source;
2525 devpriv->stc_writew(dev, devpriv->ao_mode2,AO_Mode_2_Register);
2526 if(cmd->stop_src==TRIG_NONE){
2527 devpriv->stc_writel(dev, 0xffffff,AO_BC_Load_A_Register);
2529 devpriv->stc_writel(dev, 0,AO_BC_Load_A_Register);
2531 devpriv->stc_writew(dev, AO_BC_Load,AO_Command_1_Register);
2532 devpriv->ao_mode2&=~AO_UC_Initial_Load_Source;
2533 devpriv->stc_writew(dev, devpriv->ao_mode2,AO_Mode_2_Register);
2534 switch(cmd->stop_src){
2536 devpriv->stc_writel(dev, cmd->stop_arg,AO_UC_Load_A_Register);
2537 devpriv->stc_writew(dev, AO_UC_Load,AO_Command_1_Register);
2538 devpriv->stc_writel(dev, cmd->stop_arg - 1,AO_UC_Load_A_Register);
2541 devpriv->stc_writel(dev, 0xffffff,AO_UC_Load_A_Register);
2542 devpriv->stc_writew(dev, AO_UC_Load,AO_Command_1_Register);
2543 devpriv->stc_writel(dev, 0xffffff,AO_UC_Load_A_Register);
2546 devpriv->stc_writel(dev, 0,AO_UC_Load_A_Register);
2547 devpriv->stc_writew(dev, AO_UC_Load,AO_Command_1_Register);
2548 devpriv->stc_writel(dev, cmd->stop_arg,AO_UC_Load_A_Register);
2551 devpriv->ao_cmd2&=~AO_BC_Gate_Enable;
2552 devpriv->stc_writew(dev, devpriv->ao_cmd2,AO_Command_2_Register);
2553 devpriv->ao_mode1&=~(AO_UI_Source_Select(0x1f)|AO_UI_Source_Polarity);
2554 devpriv->stc_writew(dev, devpriv->ao_mode1,AO_Mode_1_Register);
2555 devpriv->ao_mode2&=~(AO_UI_Reload_Mode(3)|AO_UI_Initial_Load_Source);
2556 devpriv->stc_writew(dev, devpriv->ao_mode2,AO_Mode_2_Register);
2557 devpriv->stc_writel(dev, 1,AO_UI_Load_A_Register);
2558 devpriv->stc_writew(dev, AO_UI_Load,AO_Command_1_Register);
2559 devpriv->stc_writel(dev, trigvar,AO_UI_Load_A_Register);
2561 if((boardtype.reg_type & ni_reg_6xxx_mask) == 0){
2562 if(cmd->scan_end_arg>1){
2563 devpriv->ao_mode1|=AO_Multiple_Channels;
2564 devpriv->stc_writew(dev, AO_Number_Of_Channels(cmd->scan_end_arg-1)|
2565 AO_UPDATE_Output_Select(1),
2566 AO_Output_Control_Register);
2568 devpriv->ao_mode1&=~AO_Multiple_Channels;
2569 devpriv->stc_writew(dev, AO_Number_Of_Channels(CR_CHAN(cmd->chanlist[0]))|
2570 AO_UPDATE_Output_Select(1),
2571 AO_Output_Control_Register);
2573 devpriv->stc_writew(dev, devpriv->ao_mode1,AO_Mode_1_Register);
2576 devpriv->stc_writew(dev, AO_DAC0_Update_Mode|AO_DAC1_Update_Mode,AO_Command_1_Register);
2578 devpriv->ao_mode3|=AO_Stop_On_Overrun_Error;
2579 devpriv->stc_writew(dev, devpriv->ao_mode3,AO_Mode_3_Register);
2581 devpriv->ao_mode2 &= AO_FIFO_Mode_Mask;
2583 devpriv->ao_mode2 |= AO_FIFO_Mode_HF_to_F;
2585 devpriv->ao_mode2 |= AO_FIFO_Mode_HF;
2587 devpriv->ao_mode2 &= ~AO_FIFO_Retransmit_Enable;
2588 devpriv->stc_writew(dev, devpriv->ao_mode2,AO_Mode_2_Register);
2590 bits = AO_BC_Source_Select | AO_UPDATE_Pulse_Width |
2591 AO_TMRDACWR_Pulse_Width;
2592 if( boardtype.ao_fifo_depth )
2593 bits |= AO_FIFO_Enable;
2595 bits |= AO_DMA_PIO_Control;
2596 devpriv->stc_writew(dev, bits, AO_Personal_Register);
2597 // enable sending of ao dma requests
2598 devpriv->stc_writew(dev, AO_AOFREQ_Enable, AO_Start_Select_Register);
2600 devpriv->stc_writew(dev, AO_Configuration_End,Joint_Reset_Register);
2602 if(cmd->stop_src==TRIG_COUNT) {
2603 devpriv->stc_writew(dev, AO_BC_TC_Interrupt_Ack,Interrupt_B_Ack_Register);
2604 ni_set_bits(dev, Interrupt_B_Enable_Register,
2605 AO_BC_TC_Interrupt_Enable, 1);
2608 s->async->inttrig=ni_ao_inttrig;
2613 static int ni_ao_cmdtest(comedi_device *dev,comedi_subdevice *s,comedi_cmd *cmd)
2618 /* step 1: make sure trigger sources are trivially valid */
2621 cmd->start_src &= TRIG_INT;
2622 if(!cmd->start_src || tmp!=cmd->start_src)err++;
2624 tmp=cmd->scan_begin_src;
2625 cmd->scan_begin_src &= TRIG_TIMER;
2626 if(!cmd->scan_begin_src || tmp!=cmd->scan_begin_src)err++;
2628 tmp=cmd->convert_src;
2629 cmd->convert_src &= TRIG_NOW;
2630 if(!cmd->convert_src || tmp!=cmd->convert_src)err++;
2632 tmp=cmd->scan_end_src;
2633 cmd->scan_end_src &= TRIG_COUNT;
2634 if(!cmd->scan_end_src || tmp!=cmd->scan_end_src)err++;
2637 cmd->stop_src &= TRIG_COUNT|TRIG_NONE;
2638 if(!cmd->stop_src || tmp!=cmd->stop_src)err++;
2642 /* step 2: make sure trigger sources are unique and mutually compatible */
2644 if(cmd->stop_src!=TRIG_COUNT &&
2645 cmd->stop_src!=TRIG_NONE)err++;
2649 /* step 3: make sure arguments are trivially compatible */
2651 if(cmd->start_arg!=0){
2656 /* XXX need ao_speed */
2657 if(cmd->scan_begin_arg<boardtype.ao_speed){
2658 cmd->scan_begin_arg=boardtype.ao_speed;
2662 if(cmd->scan_begin_arg>TIMER_BASE*0xffffff){ /* XXX check */
2663 cmd->scan_begin_arg=TIMER_BASE*0xffffff;
2666 if(cmd->convert_arg!=0){
2670 if(cmd->scan_end_arg!=cmd->chanlist_len){
2671 cmd->scan_end_arg=cmd->chanlist_len;
2674 if(cmd->stop_src==TRIG_COUNT){ /* XXX check */
2675 if(cmd->stop_arg>0x00ffffff){
2676 cmd->stop_arg=0x00ffffff;
2681 if(cmd->stop_arg!=0){
2689 /* step 4: fix up any arguments */
2691 tmp = cmd->scan_begin_arg;
2692 ni_ns_to_timer(&cmd->scan_begin_arg,cmd->flags&TRIG_ROUND_MASK);
2693 if(tmp!=cmd->scan_begin_arg)err++;
2697 /* step 5: fix up chanlist */
2705 static int ni_ao_reset(comedi_device *dev,comedi_subdevice *s)
2707 //devpriv->ao0p=0x0000;
2708 //ni_writew(devpriv->ao0p,AO_Configuration);
2710 //devpriv->ao1p=AO_Channel(1);
2711 //ni_writew(devpriv->ao1p,AO_Configuration);
2714 mite_dma_disarm(devpriv->mite, AO_DMA_CHAN);
2715 writel(CHOR_DMARESET | CHOR_FRESET, devpriv->mite->mite_io_addr + MITE_CHOR(AO_DMA_CHAN));
2718 devpriv->stc_writew(dev, AO_Configuration_Start,Joint_Reset_Register);
2719 devpriv->stc_writew(dev, AO_Disarm,AO_Command_1_Register);
2720 ni_set_bits(dev,Interrupt_B_Enable_Register,~0,0);
2721 devpriv->stc_writew(dev, AO_BC_Source_Select, AO_Personal_Register);
2722 devpriv->stc_writew(dev, 0x3f98,Interrupt_B_Ack_Register);
2723 devpriv->stc_writew(dev, AO_BC_Source_Select | AO_UPDATE_Pulse_Width |
2724 AO_TMRDACWR_Pulse_Width, AO_Personal_Register);
2725 devpriv->stc_writew(dev, 0,AO_Output_Control_Register);
2726 devpriv->stc_writew(dev, 0,AO_Start_Select_Register);
2728 devpriv->stc_writew(dev, devpriv->ao_cmd1,AO_Command_1_Register);
2730 devpriv->ao_mode1=0;
2731 devpriv->ao_mode2=0;
2732 if(boardtype.reg_type == ni_reg_m_series)
2733 devpriv->ao_mode3 = AO_Last_Gate_Disable;
2735 devpriv->ao_mode3 = 0;
2736 devpriv->stc_writew(dev, devpriv->ao_mode3, AO_Mode_3_Register);
2737 devpriv->ao_trigger_select=0;
2738 if(boardtype.reg_type & ni_reg_6xxx_mask){
2739 ao_win_out(0x3, AO_Immediate_671x);
2740 ao_win_out(CLEAR_WG, AO_Misc_611x);
2742 devpriv->stc_writew(dev, AO_Configuration_End, Joint_Reset_Register);
2747 static int ni_dio_insn_config(comedi_device *dev,comedi_subdevice *s,
2748 comedi_insn *insn,lsampl_t *data)
2751 printk("ni_dio_insn_config() chan=%d io=%d\n",
2752 CR_CHAN(insn->chanspec),data[0]);
2755 case INSN_CONFIG_DIO_OUTPUT:
2756 s->io_bits |= 1<<CR_CHAN(insn->chanspec);
2758 case INSN_CONFIG_DIO_INPUT:
2759 s->io_bits &= ~(1<<CR_CHAN(insn->chanspec));
2761 case INSN_CONFIG_DIO_QUERY:
2762 data[1] = (s->io_bits & (1<<CR_CHAN(insn->chanspec))) ? COMEDI_OUTPUT : COMEDI_INPUT;
2769 devpriv->dio_control &= ~DIO_Pins_Dir_Mask;
2770 devpriv->dio_control |= DIO_Pins_Dir(s->io_bits);
2771 devpriv->stc_writew(dev, devpriv->dio_control,DIO_Control_Register);
2776 static int ni_dio_insn_bits(comedi_device *dev,comedi_subdevice *s,
2777 comedi_insn *insn,lsampl_t *data)
2780 printk("ni_dio_insn_bits() mask=0x%x bits=0x%x\n",data[0],data[1]);
2782 if(insn->n!=2)return -EINVAL;
2784 /* Perform check to make sure we're not using the
2785 serial part of the dio */
2786 if((data[0] & (DIO_SDIN | DIO_SDOUT)) && devpriv->serial_interval_ns)
2789 s->state &= ~data[0];
2790 s->state |= (data[0]&data[1]);
2791 devpriv->dio_output &= ~DIO_Parallel_Data_Mask;
2792 devpriv->dio_output |= DIO_Parallel_Data_Out(s->state);
2793 devpriv->stc_writew(dev, devpriv->dio_output,DIO_Output_Register);
2795 data[1] = devpriv->stc_readw(dev, DIO_Parallel_Input_Register);
2800 static int ni_serial_insn_config(comedi_device *dev,comedi_subdevice *s,
2801 comedi_insn *insn,lsampl_t *data)
2804 unsigned char byte_out, byte_in;
2806 if(insn->n!=2)return -EINVAL;
2809 case INSN_CONFIG_SERIAL_CLOCK:
2812 printk("SPI serial clock Config cd\n", data[1]);
2814 devpriv->serial_hw_mode = 1;
2815 devpriv->dio_control |= DIO_HW_Serial_Enable;
2817 if(data[1] == SERIAL_DISABLED) {
2818 devpriv->serial_hw_mode = 0;
2819 devpriv->dio_control &= ~(DIO_HW_Serial_Enable |
2820 DIO_Software_Serial_Control);
2821 data[1] = SERIAL_DISABLED;
2822 devpriv->serial_interval_ns = data[1];
2824 else if(data[1] <= SERIAL_600NS) {
2825 /* Warning: this clock speed is too fast to reliably
2827 devpriv->dio_control &= ~DIO_HW_Serial_Timebase;
2828 devpriv->clock_and_fout |= Slow_Internal_Timebase;
2829 devpriv->clock_and_fout &= ~DIO_Serial_Out_Divide_By_2;
2830 data[1] = SERIAL_600NS;
2831 devpriv->serial_interval_ns = data[1];
2833 else if(data[1] <= SERIAL_1_2US) {
2834 devpriv->dio_control &= ~DIO_HW_Serial_Timebase;
2835 devpriv->clock_and_fout |= Slow_Internal_Timebase |
2836 DIO_Serial_Out_Divide_By_2;
2837 data[1] = SERIAL_1_2US;
2838 devpriv->serial_interval_ns = data[1];
2840 else if(data[1] <= SERIAL_10US) {
2841 devpriv->dio_control |= DIO_HW_Serial_Timebase;
2842 devpriv->clock_and_fout |= Slow_Internal_Timebase |
2843 DIO_Serial_Out_Divide_By_2;
2844 /* Note: DIO_Serial_Out_Divide_By_2 only affects
2845 600ns/1.2us. If you turn divide_by_2 off with the
2846 slow clock, you will still get 10us, except then
2847 all your delays are wrong. */
2848 data[1] = SERIAL_10US;
2849 devpriv->serial_interval_ns = data[1];
2852 devpriv->dio_control &= ~(DIO_HW_Serial_Enable |
2853 DIO_Software_Serial_Control);
2854 devpriv->serial_hw_mode = 0;
2855 data[1] = (data[1] / 1000) * 1000;
2856 devpriv->serial_interval_ns = data[1];
2859 devpriv->stc_writew(dev, devpriv->dio_control,DIO_Control_Register);
2860 devpriv->stc_writew(dev, devpriv->clock_and_fout,Clock_and_FOUT_Register);
2865 case INSN_CONFIG_BIDIRECTIONAL_DATA:
2867 if(devpriv->serial_interval_ns == 0) {
2871 byte_out = data[1] & 0xFF;
2873 if(devpriv->serial_hw_mode) {
2874 err = ni_serial_hw_readwrite8(dev,s,byte_out,&byte_in);
2875 } else if(devpriv->serial_interval_ns > 0) {
2876 err = ni_serial_sw_readwrite8(dev,s,byte_out,&byte_in);
2878 printk("ni_serial_insn_config: serial disabled!\n");
2881 if(err < 0) return err;
2882 data[1] = byte_in & 0xFF;
2892 static int ni_serial_hw_readwrite8(comedi_device *dev,comedi_subdevice *s,
2893 unsigned char data_out,
2894 unsigned char *data_in)
2896 unsigned int status1;
2897 int err = 0, count = 20;
2900 printk("ni_serial_hw_readwrite8: outputting 0x%x\n", data_out);
2903 devpriv->dio_output &= ~DIO_Serial_Data_Mask;
2904 devpriv->dio_output |= DIO_Serial_Data_Out(data_out);
2905 devpriv->stc_writew(dev, devpriv->dio_output,DIO_Output_Register);
2907 status1 = devpriv->stc_readw(dev, Joint_Status_1_Register);
2908 if(status1 & DIO_Serial_IO_In_Progress_St) {
2913 devpriv->dio_control |= DIO_HW_Serial_Start;
2914 devpriv->stc_writew(dev, devpriv->dio_control,DIO_Control_Register);
2915 devpriv->dio_control &= ~DIO_HW_Serial_Start;
2917 /* Wait until STC says we're done, but don't loop infinitely. */
2918 while((status1 = devpriv->stc_readw(dev, Joint_Status_1_Register)) & DIO_Serial_IO_In_Progress_St) {
2919 /* Delay one bit per loop */
2920 comedi_udelay((devpriv->serial_interval_ns + 999) / 1000);
2922 printk("ni_serial_hw_readwrite8: SPI serial I/O didn't finish in time!\n");
2928 /* Delay for last bit. This delay is absolutely necessary, because
2929 DIO_Serial_IO_In_Progress_St goes high one bit too early. */
2930 comedi_udelay((devpriv->serial_interval_ns + 999) / 1000);
2932 if(data_in != NULL) {
2933 *data_in = devpriv->stc_readw(dev, DIO_Serial_Input_Register);
2935 printk("ni_serial_hw_readwrite8: inputted 0x%x\n", *data_in);
2940 devpriv->stc_writew(dev, devpriv->dio_control,DIO_Control_Register);
2945 static int ni_serial_sw_readwrite8(comedi_device *dev,comedi_subdevice *s,
2946 unsigned char data_out,
2947 unsigned char *data_in)
2949 unsigned char mask, input = 0;
2952 printk("ni_serial_sw_readwrite8: outputting 0x%x\n", data_out);
2955 /* Wait for one bit before transfer */
2956 comedi_udelay((devpriv->serial_interval_ns + 999) / 1000);
2958 for(mask = 0x80; mask; mask >>= 1) {
2959 /* Output current bit; note that we cannot touch s->state
2960 because it is a per-subdevice field, and serial is
2961 a separate subdevice from DIO. */
2962 devpriv->dio_output &= ~DIO_SDOUT;
2963 if(data_out & mask) {
2964 devpriv->dio_output |= DIO_SDOUT;
2966 devpriv->stc_writew(dev, devpriv->dio_output,DIO_Output_Register);
2968 /* Assert SDCLK (active low, inverted), wait for half of
2969 the delay, deassert SDCLK, and wait for the other half. */
2970 devpriv->dio_control |= DIO_Software_Serial_Control;
2971 devpriv->stc_writew(dev, devpriv->dio_control,DIO_Control_Register);
2973 comedi_udelay((devpriv->serial_interval_ns + 999) / 2000);
2975 devpriv->dio_control &= ~DIO_Software_Serial_Control;
2976 devpriv->stc_writew(dev, devpriv->dio_control,DIO_Control_Register);
2978 comedi_udelay((devpriv->serial_interval_ns + 999) / 2000);
2980 /* Input current bit */
2981 if(devpriv->stc_readw(dev, DIO_Parallel_Input_Register) & DIO_SDIN) {
2982 /* printk("DIO_P_I_R: 0x%x\n", devpriv->stc_readw(dev, DIO_Parallel_Input_Register)); */
2987 printk("ni_serial_sw_readwrite8: inputted 0x%x\n", input);
2989 if(data_in) *data_in = input;
2994 static void mio_common_detach(comedi_device *dev)
2996 if(dev->subdevices && boardtype.has_8255)
2997 subdev_8255_cleanup(dev,dev->subdevices+3);
3000 static void init_ao_67xx(comedi_device *dev, comedi_subdevice *s)
3004 for(i = 0; i < s->n_chan; i++)
3005 ni_ao_win_outw(dev, AO_Channel(i) | 0x0, AO_Configuration_2_67xx);
3008 static int ni_alloc_private(comedi_device *dev)
3012 ret = alloc_private(dev, sizeof(ni_private));
3013 if(ret < 0) return ret;
3015 spin_lock_init(&devpriv->window_lock);
3020 static int ni_E_init(comedi_device *dev,comedi_devconfig *it)
3022 comedi_subdevice *s;
3025 if(boardtype.n_aochan > MAX_N_AO_CHAN)
3027 printk("bug! boardtype.n_aochan > MAX_N_AO_CHAN\n");
3031 if(alloc_subdevices(dev, 11) < 0)
3034 /* analog input subdevice */
3036 s=dev->subdevices+0;
3038 if(boardtype.n_adchan){
3039 s->type=COMEDI_SUBD_AI;
3040 s->subdev_flags=SDF_READABLE | SDF_DIFF | SDF_DITHER;
3041 if(boardtype.reg_type != ni_reg_611x)
3042 s->subdev_flags |= SDF_GROUND | SDF_COMMON | SDF_OTHER;
3043 if(boardtype.adbits > 16)
3044 s->subdev_flags |= SDF_LSAMPL;
3045 if(boardtype.reg_type == ni_reg_m_series)
3046 s->subdev_flags |= SDF_SOFT_CALIBRATED;
3047 s->n_chan=boardtype.n_adchan;
3048 s->len_chanlist=512;
3049 s->maxdata=(1<<boardtype.adbits)-1;
3050 s->range_table=ni_range_lkup[boardtype.gainlkup];
3051 s->insn_read=ni_ai_insn_read;
3052 s->insn_config=ni_ai_insn_config;
3053 s->do_cmdtest=ni_ai_cmdtest;
3054 s->do_cmd=ni_ai_cmd;
3055 s->cancel=ni_ai_reset;
3057 s->munge=ni_ai_munge;
3059 s->type=COMEDI_SUBD_UNUSED;
3062 /* analog output subdevice */
3064 s=dev->subdevices+1;
3065 if(boardtype.n_aochan){
3066 dev->write_subdev=s;
3067 s->type=COMEDI_SUBD_AO;
3068 s->subdev_flags=SDF_WRITABLE|SDF_DEGLITCH|SDF_GROUND;
3069 if(boardtype.reg_type == ni_reg_m_series)
3070 s->subdev_flags |= SDF_SOFT_CALIBRATED;
3071 s->n_chan=boardtype.n_aochan;
3072 s->maxdata=(1<<boardtype.aobits)-1;
3073 s->range_table = boardtype.ao_range_table;
3074 s->insn_read=ni_ao_insn_read;
3075 if(boardtype.reg_type & ni_reg_6xxx_mask){
3076 s->insn_write=ni_ao_insn_write_671x;
3078 s->insn_write=ni_ao_insn_write;
3081 if(boardtype.n_aochan){
3083 if(boardtype.ao_fifo_depth){
3085 s->do_cmd=ni_ao_cmd;
3086 s->do_cmdtest=ni_ao_cmdtest;
3087 s->len_chanlist = boardtype.n_aochan;
3088 if(boardtype.reg_type != ni_reg_m_series)
3089 s->munge=ni_ao_munge;
3091 s->cancel=ni_ao_reset;
3093 s->type=COMEDI_SUBD_UNUSED;
3095 if((boardtype.reg_type & ni_reg_67xx_mask))
3096 init_ao_67xx(dev, s);
3098 /* digital i/o subdevice */
3100 s=dev->subdevices+2;
3101 s->type=COMEDI_SUBD_DIO;
3102 s->subdev_flags=SDF_WRITABLE|SDF_READABLE;
3105 s->range_table=&range_digital;
3106 s->io_bits=0; /* all bits input */
3107 s->insn_bits=ni_dio_insn_bits;
3108 s->insn_config=ni_dio_insn_config;
3111 devpriv->dio_control = DIO_Pins_Dir(s->io_bits);
3112 devpriv->stc_writew(dev, devpriv->dio_control,DIO_Control_Register);
3115 s=dev->subdevices+3;
3116 if(boardtype.has_8255){
3117 subdev_8255_init(dev,s,ni_8255_callback,(unsigned long)dev);
3119 s->type=COMEDI_SUBD_UNUSED;
3122 /* general purpose counter/timer device */
3123 s=dev->subdevices+4;
3124 s->type=COMEDI_SUBD_COUNTER;
3125 s->subdev_flags=SDF_READABLE|SDF_WRITABLE;
3126 s->insn_read= ni_gpct_insn_read;
3127 s->insn_write= ni_gpct_insn_write;
3128 s->insn_config=ni_gpct_insn_config;
3131 devpriv->an_trig_etc_reg = 0;
3135 /* calibration subdevice -- ai and ao */
3136 s=dev->subdevices+5;
3137 s->type=COMEDI_SUBD_CALIB;
3138 if(boardtype.reg_type == ni_reg_m_series)
3140 // internal PWM analog output used for AI nonlinearity calibration
3141 s->subdev_flags = SDF_INTERNAL;
3142 s->insn_config = &ni_m_series_pwm_config;
3145 ni_writel(0x0, M_Offset_Cal_PWM);
3146 } else if(boardtype.reg_type == ni_reg_6143)
3148 // internal PWM analog output used for AI nonlinearity calibration
3149 s->subdev_flags = SDF_INTERNAL;
3150 s->insn_config = &ni_6143_pwm_config;
3155 s->subdev_flags = SDF_WRITABLE | SDF_INTERNAL;
3156 s->insn_read = &ni_calib_insn_read;
3157 s->insn_write = &ni_calib_insn_write;
3158 caldac_setup(dev, s);
3162 s=dev->subdevices+6;
3163 s->type=COMEDI_SUBD_MEMORY;
3164 s->subdev_flags=SDF_READABLE|SDF_INTERNAL;
3166 if(boardtype.reg_type == ni_reg_m_series)
3168 s->n_chan = M_SERIES_EEPROM_SIZE;
3169 s->insn_read = &ni_m_series_eeprom_insn_read;
3173 s->insn_read = &ni_eeprom_insn_read;
3176 s=dev->subdevices+7;
3177 s->type=COMEDI_SUBD_DIO;
3178 s->subdev_flags=SDF_READABLE|SDF_WRITABLE|SDF_INTERNAL;
3181 s->insn_bits = ni_pfi_insn_bits;
3182 s->insn_config = ni_pfi_insn_config;
3183 ni_set_bits(dev, IO_Bidirection_Pin_Register, ~0, 0);
3185 /* cs5529 calibration adc */
3186 s = dev->subdevices + 8;
3187 if(boardtype.reg_type & ni_reg_67xx_mask)
3189 s->type = COMEDI_SUBD_AI;
3190 s->subdev_flags = SDF_READABLE | SDF_DIFF | SDF_INTERNAL;
3191 // one channel for each analog output channel
3192 s->n_chan = boardtype.n_aochan;
3193 s->maxdata = (1 << 16) - 1;
3194 s->range_table = &range_unknown; /* XXX */
3195 s->insn_read=cs5529_ai_insn_read;
3196 s->insn_config=NULL;
3200 s->type=COMEDI_SUBD_UNUSED;
3204 s=dev->subdevices+9;
3205 s->type=COMEDI_SUBD_SERIAL;
3206 s->subdev_flags=SDF_READABLE|SDF_WRITABLE|SDF_INTERNAL;
3209 s->insn_config = ni_serial_insn_config;
3210 devpriv->serial_interval_ns = 0;
3211 devpriv->serial_hw_mode = 0;
3214 s=dev->subdevices+10;
3215 s->type=COMEDI_SUBD_DIO;
3216 s->subdev_flags=SDF_READABLE|SDF_WRITABLE|SDF_INTERNAL;
3219 s->insn_bits = ni_rtsi_insn_bits;
3220 s->insn_config = ni_rtsi_insn_config;
3223 /* ai configuration */
3224 ni_ai_reset(dev,dev->subdevices+0);
3225 if((boardtype.reg_type & ni_reg_6xxx_mask) == 0){
3226 // BEAM is this needed for PCI-6143 ??
3227 devpriv->clock_and_fout =
3228 Slow_Internal_Time_Divide_By_2 |
3229 Slow_Internal_Timebase |
3230 Clock_To_Board_Divide_By_2 |
3232 AI_Output_Divide_By_2 |
3233 AO_Output_Divide_By_2;
3235 devpriv->clock_and_fout =
3236 Slow_Internal_Time_Divide_By_2 |
3237 Slow_Internal_Timebase |
3238 Clock_To_Board_Divide_By_2 |
3241 devpriv->stc_writew(dev, devpriv->clock_and_fout, Clock_and_FOUT_Register);
3243 /* analog output configuration */
3244 ni_ao_reset(dev,dev->subdevices + 1);
3247 devpriv->stc_writew(dev, (IRQ_POLARITY?Interrupt_Output_Polarity:0) |
3248 (Interrupt_Output_On_3_Pins&0) |
3249 Interrupt_A_Enable |
3250 Interrupt_B_Enable |
3251 Interrupt_A_Output_Select(interrupt_pin(dev->irq)) |
3252 Interrupt_B_Output_Select(interrupt_pin(dev->irq)),
3253 Interrupt_Control_Register
3258 /* tell the STC which dma channels to use for AI and AO */
3259 bits = 1 << ( AI_DMA_CHAN );
3260 bits |= 1 << ( AO_DMA_CHAN + 4 );
3261 ni_writeb( bits, AI_AO_Select);
3262 /* tell the STC which dma channels to use for
3263 * General purpose counters 0 and 1 */
3264 bits = 1 << ( GPC0_DMA_CHAN );
3265 bits |= 1 << ( GPC1_DMA_CHAN + 4 );
3266 ni_writeb( bits, G0_G1_Select);
3268 if(boardtype.reg_type & ni_reg_6xxx_mask)
3270 ni_writeb( 0, Magic_611x );
3271 }else if(boardtype.reg_type == ni_reg_m_series)
3274 for(channel = 0; channel < boardtype.n_aochan; ++channel)
3276 ni_writeb(0xf, M_Offset_AO_Waveform_Order(channel));
3277 ni_writeb(0x0, M_Offset_AO_Reference_Attenuation(channel));
3279 ni_writeb(0x0, M_Offset_AO_Calibration);
3289 static int ni_8255_callback(int dir,int port,int data,unsigned long arg)
3291 comedi_device *dev=(comedi_device *)arg;
3294 ni_writeb(data,Port_A+2*port);
3297 return ni_readb(Port_A+2*port);
3302 presents the EEPROM as a subdevice
3305 static int ni_eeprom_insn_read(comedi_device *dev,comedi_subdevice *s,
3306 comedi_insn *insn,lsampl_t *data)
3308 data[0]=ni_read_eeprom(dev,CR_CHAN(insn->chanspec));
3314 reads bytes out of eeprom
3317 static int ni_read_eeprom(comedi_device *dev,int addr)
3322 bitstring=0x0300|((addr&0x100)<<3)|(addr&0xff);
3323 ni_writeb(0x04,Serial_Command);
3324 for(bit=0x8000;bit;bit>>=1){
3325 ni_writeb(0x04|((bit&bitstring)?0x02:0),Serial_Command);
3326 ni_writeb(0x05|((bit&bitstring)?0x02:0),Serial_Command);
3329 for(bit=0x80;bit;bit>>=1){
3330 ni_writeb(0x04,Serial_Command);
3331 ni_writeb(0x05,Serial_Command);
3332 bitstring|=((ni_readb(XXX_Status)&PROMOUT)?bit:0);
3334 ni_writeb(0x00,Serial_Command);
3339 static int ni_m_series_eeprom_insn_read(comedi_device *dev,comedi_subdevice *s,
3340 comedi_insn *insn,lsampl_t *data)
3342 data[0] = devpriv->eeprom_buffer[CR_CHAN(insn->chanspec)];
3347 static int ni_get_pwm_config(comedi_device *dev, lsampl_t *data)
3349 data[1] = devpriv->pwm_up_count * TIMER_BASE;
3350 data[2] = devpriv->pwm_down_count * TIMER_BASE;
3354 static int ni_m_series_pwm_config(comedi_device *dev, comedi_subdevice *s,
3355 comedi_insn *insn, lsampl_t *data)
3357 unsigned up_count, down_count;
3360 case INSN_CONFIG_PWM_OUTPUT:
3363 case TRIG_ROUND_NEAREST:
3364 up_count = (data[2] + TIMER_BASE / 2) / TIMER_BASE;
3366 case TRIG_ROUND_DOWN:
3367 up_count = data[2] / TIMER_BASE;
3370 up_count = (data[2] + TIMER_BASE - 1) / TIMER_BASE;
3378 case TRIG_ROUND_NEAREST:
3379 down_count = (data[4] + TIMER_BASE / 2) / TIMER_BASE;
3381 case TRIG_ROUND_DOWN:
3382 down_count = data[4] / TIMER_BASE;
3385 down_count = (data[4] + TIMER_BASE - 1) / TIMER_BASE;
3391 if(up_count * TIMER_BASE != data[2] ||
3392 down_count * TIMER_BASE != data[4])
3394 data[2] = up_count * TIMER_BASE;
3395 data[4] = down_count * TIMER_BASE;
3398 ni_writel(MSeries_Cal_PWM_High_Time_Bits(up_count) | MSeries_Cal_PWM_Low_Time_Bits(down_count), M_Offset_Cal_PWM);
3399 devpriv->pwm_up_count = up_count;
3400 devpriv->pwm_down_count = down_count;
3403 case INSN_CONFIG_GET_PWM_OUTPUT:
3404 return ni_get_pwm_config(dev, data);
3413 static int ni_6143_pwm_config(comedi_device *dev, comedi_subdevice *s,
3414 comedi_insn *insn, lsampl_t *data)
3416 unsigned up_count, down_count;
3419 case INSN_CONFIG_PWM_OUTPUT:
3422 case TRIG_ROUND_NEAREST:
3423 up_count = (data[2] + TIMER_BASE / 2) / TIMER_BASE;
3425 case TRIG_ROUND_DOWN:
3426 up_count = data[2] / TIMER_BASE;
3429 up_count = (data[2] + TIMER_BASE - 1) / TIMER_BASE;
3437 case TRIG_ROUND_NEAREST:
3438 down_count = (data[4] + TIMER_BASE / 2) / TIMER_BASE;
3440 case TRIG_ROUND_DOWN:
3441 down_count = data[4] / TIMER_BASE;
3444 down_count = (data[4] + TIMER_BASE - 1) / TIMER_BASE;
3450 if(up_count * TIMER_BASE != data[2] ||
3451 down_count * TIMER_BASE != data[4])
3453 data[2] = up_count * TIMER_BASE;
3454 data[4] = down_count * TIMER_BASE;
3457 ni_writel(up_count, Calibration_HighTime_6143);
3458 devpriv->pwm_up_count = up_count;
3459 ni_writel(down_count, Calibration_LowTime_6143);
3460 devpriv->pwm_down_count = down_count;
3463 case INSN_CONFIG_GET_PWM_OUTPUT:
3464 return ni_get_pwm_config(dev, data);
3472 static void ni_write_caldac(comedi_device *dev,int addr,int val);
3474 calibration subdevice
3476 static int ni_calib_insn_write(comedi_device *dev,comedi_subdevice *s,
3477 comedi_insn *insn,lsampl_t *data)
3479 ni_write_caldac(dev,CR_CHAN(insn->chanspec),data[0]);
3484 static int ni_calib_insn_read(comedi_device *dev,comedi_subdevice *s,
3485 comedi_insn *insn,lsampl_t *data)
3487 data[0] = devpriv->caldacs[CR_CHAN(insn->chanspec)];
3492 static int pack_mb88341(int addr,int val,int *bitstring);
3493 static int pack_dac8800(int addr,int val,int *bitstring);
3494 static int pack_dac8043(int addr,int val,int *bitstring);
3495 static int pack_ad8522(int addr,int val,int *bitstring);
3496 static int pack_ad8804(int addr,int val,int *bitstring);
3497 static int pack_ad8842(int addr,int val,int *bitstring);
3499 struct caldac_struct{
3502 int (*packbits)(int,int,int *);
3505 static struct caldac_struct caldacs[] = {
3506 [mb88341] = { 12, 8, pack_mb88341 },
3507 [dac8800] = { 8, 8, pack_dac8800 },
3508 [dac8043] = { 1, 12, pack_dac8043 },
3509 [ad8522] = { 2, 12, pack_ad8522 },
3510 [ad8804] = { 12, 8, pack_ad8804 },
3511 [ad8842] = { 8, 8, pack_ad8842 },
3512 [ad8804_debug] = { 16, 8, pack_ad8804 },
3515 static void caldac_setup(comedi_device *dev,comedi_subdevice *s)
3525 type = boardtype.caldac[0];
3526 if(type==caldac_none)return;
3527 n_bits=caldacs[type].n_bits;
3529 type = boardtype.caldac[i];
3530 if(type==caldac_none)break;
3531 if(caldacs[type].n_bits!=n_bits)diffbits=1;
3532 n_chans+=caldacs[type].n_chans;
3539 if(n_chans>MAX_N_CALDACS){
3540 printk("BUG! MAX_N_CALDACS too small\n");
3542 s->maxdata_list=devpriv->caldac_maxdata_list;
3544 for(i=0;i<n_dacs;i++){
3545 type = boardtype.caldac[i];
3546 for(j=0;j<caldacs[type].n_chans;j++){
3547 s->maxdata_list[chan]=
3548 (1<<caldacs[type].n_bits)-1;
3553 for( chan = 0; chan < s->n_chan; chan++ )
3554 ni_write_caldac( dev, i, s->maxdata_list[ i ] / 2 );
3556 type = boardtype.caldac[0];
3557 s->maxdata=(1<<caldacs[type].n_bits)-1;
3559 for( chan = 0; chan < s->n_chan; chan++ )
3560 ni_write_caldac( dev, i, s->maxdata / 2 );
3564 static void ni_write_caldac(comedi_device *dev,int addr,int val)
3566 unsigned int loadbit=0,bits=0,bit,bitstring=0;
3570 //printk("ni_write_caldac: chan=%d val=%d\n",addr,val);
3571 if( devpriv->caldacs[ addr ] == val ) return;
3572 devpriv->caldacs[ addr ] = val;
3575 type = boardtype.caldac[i];
3576 if(type==caldac_none)break;
3577 if(addr<caldacs[type].n_chans){
3578 bits=caldacs[type].packbits(addr,val,&bitstring);
3579 loadbit=SerDacLd(i);
3580 //printk("caldac: using i=%d addr=%d %x\n",i,addr,bitstring);
3583 addr-=caldacs[type].n_chans;
3586 for(bit=1<<(bits-1);bit;bit>>=1){
3587 ni_writeb(((bit&bitstring)?0x02:0),Serial_Command);
3589 ni_writeb(1|((bit&bitstring)?0x02:0),Serial_Command);
3592 ni_writeb(loadbit,Serial_Command);
3594 ni_writeb(0,Serial_Command);
3599 static int pack_mb88341(int addr,int val,int *bitstring)
3603 Note that address bits are reversed. Thanks to
3604 Ingo Keen for noticing this.
3606 Note also that the 88341 expects address values from
3607 1-12, whereas we use channel numbers 0-11. The NI
3608 docs use 1-12, also, so be careful here.
3611 *bitstring=((addr&0x1)<<11) |
3619 static int pack_dac8800(int addr,int val,int *bitstring)
3621 *bitstring=((addr&0x7)<<8)|(val&0xff);
3625 static int pack_dac8043(int addr,int val,int *bitstring)
3627 *bitstring=val&0xfff;
3631 static int pack_ad8522(int addr,int val,int *bitstring)
3633 *bitstring=(val&0xfff)|(addr ? 0xc000:0xa000);
3637 static int pack_ad8804(int addr,int val,int *bitstring)
3639 *bitstring=((addr&0xf)<<8) | (val&0xff);
3643 static int pack_ad8842(int addr,int val,int *bitstring)
3645 *bitstring=((addr+1)<<8) | (val&0xff);
3655 * General Purpose Counter/Timer section
3660 * Low level stuff...Each STC counter has two 24 bit load registers
3661 * (A&B). Just make it easier to access them.
3663 * These are inlined _only_ because they are used once in subsequent
3664 * code. Otherwise they should not be inlined.
3666 static inline void GPCT_Load_A(comedi_device *dev, int chan, unsigned int value)
3668 devpriv->stc_writel(dev, value & 0x00ffffff, G_Load_A_Register(chan));
3671 static inline void GPCT_Load_B(comedi_device *dev, int chan, unsigned int value)
3673 devpriv->stc_writel(dev, value & 0x00ffffff, G_Load_B_Register(chan));
3676 /* Load a value into the counter, using register A as the intermediate step.
3677 * You might use GPCT_Load_Using_A to load a 0x000000 into a counter
3680 static void GPCT_Load_Using_A(comedi_device *dev, int chan, unsigned int value)
3682 devpriv->gpct_mode[chan] &= (~G_Load_Source_Select);
3683 devpriv->stc_writew(dev, devpriv->gpct_mode[chan],G_Mode_Register(chan));
3684 GPCT_Load_A(dev,chan,value);
3685 devpriv->stc_writew(dev, devpriv->gpct_command[chan]|G_Load,G_Command_Register(chan));
3689 * Read the GPCTs current value.
3691 static int GPCT_G_Watch(comedi_device *dev, int chan)
3693 unsigned int hi1,hi2,lo;
3695 devpriv->gpct_command[chan] &= ~G_Save_Trace;
3696 devpriv->stc_writew(dev, devpriv->gpct_command[chan],G_Command_Register(chan));
3698 devpriv->gpct_command[chan] |= G_Save_Trace;
3699 devpriv->stc_writew(dev, devpriv->gpct_command[chan], G_Command_Register(chan));
3701 /* This procedure is used because the two registers cannot
3702 * be read atomically. */
3704 hi1 = devpriv->stc_readw(dev, G_Save_Register_High(chan));
3705 lo = devpriv->stc_readw(dev, G_Save_Register_Low(chan));
3706 hi2 = devpriv->stc_readw(dev, G_Save_Register_High(chan));
3709 return (hi1<<16)|lo;
3713 static int GPCT_Disarm(comedi_device *dev, int chan)
3715 devpriv->stc_writew(dev, devpriv->gpct_command[chan] | G_Disarm,G_Command_Register(chan));
3720 static int GPCT_Arm(comedi_device *dev, int chan)
3722 devpriv->stc_writew(dev, devpriv->gpct_command[chan] | G_Arm,G_Command_Register(chan));
3723 /* If the counter is doing pulse width measurement, then make
3724 sure that the counter did not start counting right away. This would
3725 indicate that we started acquiring the pulse after it had already
3726 started and our measurement would be inaccurate */
3727 if(devpriv->gpct_cur_operation[chan] == GPCT_SINGLE_PW){
3730 g_status=devpriv->stc_readw(dev, G_Status_Register);
3733 //TIM 5/2/01 possible error with very short pulses
3734 if((G0_Counting_St & g_status)|| !(G0_Armed_St&g_status)) {
3735 //error: we missed the beginning of the pulse
3736 return -EINVAL; //there is probably a more accurate error code...
3739 if((G1_Counting_St & g_status)|| !(G1_Armed_St&g_status)) {
3740 //error: we missed the beginning of the pulse
3748 static int GPCT_Set_Source(comedi_device *dev,int chan ,int source)
3750 //printk("GPCT_Set_Source...");
3751 devpriv->gpct_input_select[chan] &= ~G_Source_Select(0x1f);//reset gate to 0
3753 case GPCT_INT_CLOCK:
3754 devpriv->gpct_input_select[chan] |= G_Source_Select(0);//INT_TIMEBASE
3758 devpriv->gpct_input_select[chan] |= G_Source_Select(9);//PFI8
3760 devpriv->gpct_input_select[chan] |= G_Source_Select(4);//PFI3
3765 devpriv->stc_writew(dev, devpriv->gpct_input_select[chan], G_Input_Select_Register(chan));
3766 //printk("exit GPCT_Set_Source\n");
3770 static int GPCT_Set_Gate(comedi_device *dev,int chan ,int gate)
3772 //printk("GPCT_Set_Gate...");
3773 devpriv->gpct_input_select[chan] &= ~G_Gate_Select(0x1f);//reset gate to 0
3776 devpriv->gpct_input_select[chan] |= G_Gate_Select(31);//Low
3777 devpriv->gpct_mode[chan] |= G_Gate_Polarity;
3780 devpriv->gpct_mode[chan] &= ~G_Gate_Polarity;
3782 devpriv->gpct_input_select[chan] |= G_Gate_Select(10);//PFI9
3784 devpriv->gpct_input_select[chan] |= G_Gate_Select(5);//PFI4
3790 devpriv->stc_writew(dev, devpriv->gpct_input_select[chan], G_Input_Select_Register(chan));
3791 devpriv->stc_writew(dev, devpriv->gpct_mode[chan], G_Mode_Register(chan));
3792 //printk("exit GPCT_Set_Gate\n");
3796 static int GPCT_Set_Direction(comedi_device *dev,int chan,int direction)
3798 //printk("GPCT_Set_Direction...");
3800 devpriv->gpct_command[chan] &= ~G_Up_Down(0x3);
3801 switch (direction) {
3803 devpriv->gpct_command[chan] |= G_Up_Down(1);
3806 devpriv->gpct_command[chan] |= G_Up_Down(0);
3809 devpriv->gpct_command[chan] |= G_Up_Down(2);
3812 printk("Error direction=0x%08x..",direction);
3815 devpriv->stc_writew(dev, devpriv->gpct_command[chan], G_Command_Register(chan));
3816 //TIM 4/23/01 devpriv->stc_writew(dev, devpriv->gpct_mode[chan], G_Mode_Register(chan));
3817 //printk("exit GPCT_Set_Direction\n");
3821 static void GPCT_Event_Counting(comedi_device *dev,int chan)
3824 //NOTE: possible residual bits from multibit masks can corrupt
3825 //If you config for several measurements between Resets, watch out!
3827 //printk("GPCT_Event_Counting...");
3829 devpriv->gpct_cur_operation[chan] = GPCT_SIMPLE_EVENT;
3832 devpriv->gpct_mode[chan] &= ~(G_Gating_Mode(0x3));
3833 devpriv->gpct_mode[chan] |= G_Gating_Mode(1);
3835 // Trigger_Mode_For_Edge_Gate = 1
3836 devpriv->gpct_mode[chan] &= ~(G_Trigger_Mode_For_Edge_Gate(0x3));
3837 devpriv->gpct_mode[chan] |= G_Trigger_Mode_For_Edge_Gate(2);
3839 devpriv->stc_writew(dev, devpriv->gpct_mode[chan],G_Mode_Register(chan));
3840 //printk("exit GPCT_Event_Counting\n");
3843 static void GPCT_Period_Meas(comedi_device *dev, int chan)
3845 //printk("GPCT_Period_Meas...");
3847 devpriv->gpct_cur_operation[chan] = GPCT_SINGLE_PERIOD;
3850 //NOTE: possible residual bits from multibit masks can corrupt
3851 //If you config for several measurements between Resets, watch out!
3852 devpriv->gpct_mode[chan] &= ~G_OR_Gate;
3853 devpriv->gpct_mode[chan] &= ~G_Gate_Select_Load_Source;
3856 devpriv->gpct_mode[chan] &= ~(G_Output_Mode(0x3));
3857 devpriv->gpct_mode[chan] |= G_Output_Mode(3);
3861 devpriv->gpct_mode[chan] &= ~(G_Gating_Mode(0x3));
3862 devpriv->gpct_mode[chan] |= G_Gating_Mode(2);
3864 // Trigger_Mode_For_Edge_Gate=0
3865 devpriv->gpct_mode[chan] &= ~(G_Trigger_Mode_For_Edge_Gate(0x3));
3866 devpriv->gpct_mode[chan] |= G_Trigger_Mode_For_Edge_Gate(0);
3868 devpriv->gpct_mode[chan] |= G_Reload_Source_Switching;
3869 devpriv->gpct_mode[chan] &= ~G_Loading_On_Gate;
3870 devpriv->gpct_mode[chan] &= ~G_Loading_On_TC;
3871 devpriv->gpct_mode[chan] &= ~G_Gate_On_Both_Edges;
3874 devpriv->gpct_mode[chan] &= ~(G_Stop_Mode(0x3));
3875 devpriv->gpct_mode[chan] |= G_Stop_Mode(0);
3877 // Counting_Once = 2
3878 devpriv->gpct_mode[chan] &= ~(G_Counting_Once(0x3));
3879 devpriv->gpct_mode[chan] |= G_Counting_Once(2);
3882 devpriv->gpct_command[chan] &= ~(G_Up_Down(0x3));
3883 devpriv->gpct_command[chan] |= G_Up_Down(1);
3885 devpriv->stc_writew(dev, devpriv->gpct_mode[chan],G_Mode_Register(chan));
3886 devpriv->stc_writew(dev, devpriv->gpct_command[chan],G_Command_Register(chan));
3887 //printk("exit GPCT_Period_Meas\n");
3890 static void GPCT_Pulse_Width_Meas(comedi_device *dev, int chan)
3892 //printk("GPCT_Pulse_Width_Meas...");
3894 devpriv->gpct_cur_operation[chan] = GPCT_SINGLE_PW;
3896 devpriv->gpct_mode[chan] &= ~G_OR_Gate;
3897 devpriv->gpct_mode[chan] &= ~G_Gate_Select_Load_Source;
3900 devpriv->gpct_mode[chan] &= ~(G_Output_Mode(0x3));
3901 devpriv->gpct_mode[chan] |= G_Output_Mode(3);
3904 devpriv->gpct_mode[chan] &= ~(G_Gating_Mode(0x3));
3905 devpriv->gpct_mode[chan] |= G_Gating_Mode(1);//TIM 4/24/01 was 2
3907 // Trigger_Mode_For_Edge_Gate=2
3908 devpriv->gpct_mode[chan] &= ~(G_Trigger_Mode_For_Edge_Gate(0x3));
3909 devpriv->gpct_mode[chan] |= G_Trigger_Mode_For_Edge_Gate(2);//TIM 4/24/01 was 0
3912 devpriv->gpct_mode[chan] |= G_Reload_Source_Switching;//TIM 4/24/01 was 1
3913 devpriv->gpct_mode[chan] &= ~G_Loading_On_Gate;//TIM 4/24/01 was 0
3915 devpriv->gpct_mode[chan] &= ~G_Loading_On_TC;
3916 devpriv->gpct_mode[chan] &= ~G_Gate_On_Both_Edges;
3919 devpriv->gpct_mode[chan] &= ~(G_Stop_Mode(0x3));
3920 devpriv->gpct_mode[chan] |= G_Stop_Mode(0);
3922 // Counting_Once = 2
3923 devpriv->gpct_mode[chan] &= ~(G_Counting_Once(0x3));
3924 devpriv->gpct_mode[chan] |= G_Counting_Once(2);
3927 devpriv->gpct_command[chan] &= ~(G_Up_Down(0x3));
3928 devpriv->gpct_command[chan] |= G_Up_Down(1);
3930 devpriv->stc_writew(dev, devpriv->gpct_mode[chan],G_Mode_Register(chan));
3931 devpriv->stc_writew(dev, devpriv->gpct_command[chan],G_Command_Register(chan));
3933 //printk("exit GPCT_Pulse_Width_Meas\n");
3936 /* GPCT_Gen_Single_Pulse() creates pulse of length pulsewidth which starts after the Arm
3937 signal is sent. The pulse is delayed by the value already in the counter. This function could
3938 be modified to send a pulse in response to a trigger event at its gate.*/
3939 static void GPCT_Gen_Single_Pulse(comedi_device *dev, int chan, unsigned int length)
3941 //printk("GPCT_Gen_Cont...");
3943 devpriv->gpct_cur_operation[chan] = GPCT_SINGLE_PULSE_OUT;
3945 // Set length of the pulse
3946 GPCT_Load_B(dev,chan, length-1);
3948 //Load next time using B, This is reset by GPCT_Load_Using_A()
3949 devpriv->gpct_mode[chan] |= G_Load_Source_Select;
3951 devpriv->gpct_mode[chan] &= ~G_OR_Gate;
3952 devpriv->gpct_mode[chan] &= ~G_Gate_Select_Load_Source;
3955 devpriv->gpct_mode[chan] &= ~(G_Output_Mode(0x3));
3956 devpriv->gpct_mode[chan] |= G_Output_Mode(2); //TIM 4/26/01 was 3
3958 //Gating Mode=0 for untriggered single pulse
3959 devpriv->gpct_mode[chan] &= ~(G_Gating_Mode(0x3));
3960 devpriv->gpct_mode[chan] |= G_Gating_Mode(0); //TIM 4/25/01 was 1
3962 // Trigger_Mode_For_Edge_Gate=0
3963 devpriv->gpct_mode[chan] &= ~(G_Trigger_Mode_For_Edge_Gate(0x3));
3964 devpriv->gpct_mode[chan] |= G_Trigger_Mode_For_Edge_Gate(2);
3967 devpriv->gpct_mode[chan] |= G_Reload_Source_Switching;
3968 devpriv->gpct_mode[chan] &= ~G_Loading_On_Gate;
3969 devpriv->gpct_mode[chan] |= G_Loading_On_TC; //TIM 4/25/01
3970 devpriv->gpct_mode[chan] &= ~G_Gate_On_Both_Edges;
3973 devpriv->gpct_mode[chan] &= ~(G_Stop_Mode(0x3));
3974 devpriv->gpct_mode[chan] |= G_Stop_Mode(2); //TIM 4/25/01
3976 // Counting_Once = 2
3977 devpriv->gpct_mode[chan] &= ~(G_Counting_Once(0x3));
3978 devpriv->gpct_mode[chan] |= G_Counting_Once(1); //TIM 4/25/01
3981 devpriv->gpct_command[chan] &= ~(G_Up_Down(0x3));
3982 devpriv->gpct_command[chan] |= G_Up_Down(0); //TIM 4/25/01 was 1
3984 devpriv->stc_writew(dev, devpriv->gpct_mode[chan],G_Mode_Register(chan));
3985 devpriv->stc_writew(dev, devpriv->gpct_command[chan],G_Command_Register(chan));
3987 //printk("exit GPCT_Gen_Cont\n");
3990 static void GPCT_Gen_Cont_Pulse(comedi_device *dev, int chan, unsigned int length)
3992 //printk("GPCT_Gen_Cont...");
3994 devpriv->gpct_cur_operation[chan] = GPCT_CONT_PULSE_OUT;
3996 // Set length of the pulse
3997 GPCT_Load_B(dev,chan, length-1);
3999 //Load next time using B, This is reset by GPCT_Load_Using_A()
4000 devpriv->gpct_mode[chan] |= G_Load_Source_Select;
4002 devpriv->gpct_mode[chan] &= ~G_OR_Gate;
4003 devpriv->gpct_mode[chan] &= ~G_Gate_Select_Load_Source;
4006 devpriv->gpct_mode[chan] &= ~(G_Output_Mode(0x3));
4007 devpriv->gpct_mode[chan] |= G_Output_Mode(2); //TIM 4/26/01 was 3
4009 //Gating Mode=0 for untriggered single pulse
4010 devpriv->gpct_mode[chan] &= ~(G_Gating_Mode(0x3));
4011 devpriv->gpct_mode[chan] |= G_Gating_Mode(0); //TIM 4/26/01 was 0
4013 // Trigger_Mode_For_Edge_Gate=0
4014 devpriv->gpct_mode[chan] &= ~(G_Trigger_Mode_For_Edge_Gate(0x3));
4015 devpriv->gpct_mode[chan] |= G_Trigger_Mode_For_Edge_Gate(2);
4018 devpriv->gpct_mode[chan] |= G_Reload_Source_Switching;
4019 devpriv->gpct_mode[chan] &= ~G_Loading_On_Gate;
4020 devpriv->gpct_mode[chan] |= G_Loading_On_TC;
4021 devpriv->gpct_mode[chan] &= ~G_Gate_On_Both_Edges;
4024 devpriv->gpct_mode[chan] &= ~(G_Stop_Mode(0x3));
4025 devpriv->gpct_mode[chan] |= G_Stop_Mode(0); //TIM 4/26/01
4027 // Counting_Once = 2
4028 devpriv->gpct_mode[chan] &= ~(G_Counting_Once(0x3));
4029 devpriv->gpct_mode[chan] |= G_Counting_Once(0); //TIM 4/26/01
4032 devpriv->gpct_command[chan] &= ~(G_Up_Down(0x3));
4033 devpriv->gpct_command[chan] |= G_Up_Down(0);
4036 //This seems pretty unsafe since I don't think it is cleared anywhere.
4037 //I don't think this is working
4038 //devpriv->gpct_command[chan] &= ~G_Bank_Switch_Enable;
4039 //devpriv->gpct_command[chan] &= ~G_Bank_Switch_Mode;
4042 devpriv->stc_writew(dev, devpriv->gpct_mode[chan],G_Mode_Register(chan));
4043 devpriv->stc_writew(dev, devpriv->gpct_command[chan],G_Command_Register(chan));
4045 //printk("exit GPCT_Gen_Cont\n");
4048 static void GPCT_Reset(comedi_device *dev, int chan)
4052 //printk("GPCT_Reset...");
4053 devpriv->gpct_cur_operation[chan] = GPCT_RESET;
4057 devpriv->stc_writew(dev, G0_Reset,Joint_Reset_Register);
4058 ni_set_bits(dev,Interrupt_A_Enable_Register,G0_TC_Interrupt_Enable, 0);
4059 ni_set_bits(dev,Interrupt_A_Enable_Register,G0_Gate_Interrupt_Enable,0);
4060 temp_ack_reg |= G0_Gate_Error_Confirm;
4061 temp_ack_reg |= G0_TC_Error_Confirm;
4062 temp_ack_reg |= G0_TC_Interrupt_Ack;
4063 temp_ack_reg |= G0_Gate_Interrupt_Ack;
4064 devpriv->stc_writew(dev, temp_ack_reg,Interrupt_A_Ack_Register);
4066 //problem...this interferes with the other ctr...
4067 devpriv->an_trig_etc_reg |= GPFO_0_Output_Enable;
4068 devpriv->stc_writew(dev, devpriv->an_trig_etc_reg, Analog_Trigger_Etc_Register);
4071 devpriv->stc_writew(dev, G1_Reset,Joint_Reset_Register);
4072 ni_set_bits(dev,Interrupt_B_Enable_Register,G1_TC_Interrupt_Enable, 0);
4073 ni_set_bits(dev,Interrupt_B_Enable_Register,G0_Gate_Interrupt_Enable,0);
4074 temp_ack_reg |= G1_Gate_Error_Confirm;
4075 temp_ack_reg |= G1_TC_Error_Confirm;
4076 temp_ack_reg |= G1_TC_Interrupt_Ack;
4077 temp_ack_reg |= G1_Gate_Interrupt_Ack;
4078 devpriv->stc_writew(dev, temp_ack_reg,Interrupt_B_Ack_Register);
4080 devpriv->an_trig_etc_reg |= GPFO_1_Output_Enable;
4081 devpriv->stc_writew(dev, devpriv->an_trig_etc_reg, Analog_Trigger_Etc_Register);
4085 devpriv->gpct_mode[chan] = 0;
4086 devpriv->gpct_input_select[chan] = 0;
4087 devpriv->gpct_command[chan] = 0;
4089 devpriv->gpct_command[chan] |= G_Synchronized_Gate;
4091 devpriv->stc_writew(dev, devpriv->gpct_mode[chan],G_Mode_Register(chan));
4092 devpriv->stc_writew(dev, devpriv->gpct_input_select[chan],G_Input_Select_Register(chan));
4093 devpriv->stc_writew(dev, 0,G_Autoincrement_Register(chan));
4095 //printk("exit GPCT_Reset\n");
4098 static int ni_gpct_insn_config(comedi_device *dev,comedi_subdevice *s,
4099 comedi_insn *insn,lsampl_t *data)
4102 //printk("data[0] is 0x%08x, data[1] is 0x%08x\n",data[0],data[1]);
4105 if(insn->n!=1)return -EINVAL;
4106 GPCT_Reset(dev,insn->chanspec);
4108 case GPCT_SET_SOURCE:
4109 if(insn->n!=2)return -EINVAL;
4110 retval=GPCT_Set_Source(dev,insn->chanspec,data[1]);
4113 if(insn->n!=2)return -EINVAL;
4114 retval=GPCT_Set_Gate(dev,insn->chanspec,data[1]);
4116 case GPCT_SET_DIRECTION:
4117 if(insn->n!=2) return -EINVAL;
4118 retval=GPCT_Set_Direction(dev,insn->chanspec,data[1]);
4120 case GPCT_GET_INT_CLK_FRQ:
4121 if(insn->n!=2) return -EINVAL;
4122 //There are actually 2 internal clocks on the STC, we always
4123 //use the fast 20MHz one at this time. Tim Ousley 5/1/01
4124 //NOTE: This is not the final interface, ideally the user
4125 //will never need to know the int. clk. freq.
4126 data[1]=50;//50ns = 20MHz = internal timebase of STC
4128 case GPCT_SET_OPERATION:
4129 //TIM 5/1/01 if((insn->n<2)||(insn->n>3))return -EINVAL;
4131 case GPCT_SIMPLE_EVENT:
4132 GPCT_Event_Counting(dev,insn->chanspec);
4134 case GPCT_SINGLE_PERIOD:
4135 GPCT_Period_Meas(dev,insn->chanspec);
4137 case GPCT_SINGLE_PW:
4138 GPCT_Pulse_Width_Meas(dev,insn->chanspec);
4140 case GPCT_SINGLE_PULSE_OUT:
4141 GPCT_Gen_Single_Pulse(dev,insn->chanspec,data[2]);
4143 case GPCT_CONT_PULSE_OUT:
4144 GPCT_Gen_Cont_Pulse(dev,insn->chanspec,data[2]);
4147 printk("unsupported GPCT operation!\n");
4152 if(insn->n!=1)return -EINVAL;
4153 retval=GPCT_Arm(dev,insn->chanspec);
4156 if(insn->n!=1)return -EINVAL;
4157 retval=GPCT_Disarm(dev,insn->chanspec);
4163 //catch any errors from return values
4167 if(data[0]!=GPCT_ARM){
4168 printk("error: retval was %d\n",retval);
4169 printk("data[0] is 0x%08x, data[1] is 0x%08x\n",data[0],data[1]);
4176 static int ni_gpct_insn_read(comedi_device *dev,comedi_subdevice *s,
4177 comedi_insn *insn,lsampl_t *data) {
4179 int chan=insn->chanspec;
4180 int cur_op = devpriv->gpct_cur_operation[chan];
4182 //printk("in ni_gpct_insn_read, n=%d, data[0]=%d\n",insn->chanspec,data[0]);
4183 if(insn->n!=1)return -EINVAL;
4185 data[0] = GPCT_G_Watch(dev,insn->chanspec);
4187 /* for certain modes (period and pulse width measurment), the value
4188 in the counter is not valid until the counter stops. If the value is
4189 invalid, return a 0 */
4190 if((cur_op == GPCT_SINGLE_PERIOD) || (cur_op == GPCT_SINGLE_PW)){
4191 /* is the counter still running? */
4192 if(devpriv->stc_readw(dev, G_Status_Register) & (chan?G1_Counting_St:G0_Counting_St))
4198 static int ni_gpct_insn_write(comedi_device *dev,comedi_subdevice *s,
4199 comedi_insn *insn,lsampl_t *data) {
4201 //printk("in ni_gpct_insn_write");
4202 if(insn->n!=1)return -EINVAL;
4203 GPCT_Load_Using_A(dev,insn->chanspec,data[0]);
4210 * Programmable Function Inputs
4214 static int ni_pfi_insn_bits(comedi_device *dev,comedi_subdevice *s,
4215 comedi_insn *insn,lsampl_t *data)
4217 if(insn->n!=2)return -EINVAL;
4224 static int ni_pfi_insn_config(comedi_device *dev,comedi_subdevice *s,
4225 comedi_insn *insn,lsampl_t *data)
4229 if(insn->n < 1)return -EINVAL;
4231 chan = CR_CHAN(insn->chanspec);
4232 if(chan>10)return -EINVAL;
4236 ni_set_bits(dev, IO_Bidirection_Pin_Register, 1<<chan, 1);
4239 ni_set_bits(dev, IO_Bidirection_Pin_Register, 1<<chan, 0);
4241 case INSN_CONFIG_DIO_QUERY:
4242 data[1] = (devpriv->io_bidirection_pin_reg & (1<<chan)) ? COMEDI_OUTPUT : COMEDI_INPUT;
4254 * NI RTSI Bus Functions
4257 static void ni_rtsi_init(comedi_device *dev)
4259 // Initialises the RTSI bus signal switch to a default state
4261 // Set clock mode to internal
4262 devpriv->stc_writew(dev, COMEDI_RTSI_CLOCK_MODE_INTERNAL, RTSI_Trig_Direction_Register);
4264 // Standard internal lines are routed to standard RTSI bus lines
4265 devpriv->stc_writew(dev, 0x3210, RTSI_Trig_A_Output_Register);
4266 devpriv->stc_writew(dev, 0x0654, RTSI_Trig_B_Output_Register);
4268 // Sets the source and direction of the 4 on board lines
4269 // devpriv->stc_writew(dev, 0x0000, RTSI_Board_Register);
4272 static int ni_rtsi_insn_bits(comedi_device *dev,comedi_subdevice *s,
4273 comedi_insn *insn,lsampl_t *data)
4275 if(insn->n != 2) return -EINVAL;
4282 static int ni_rtsi_insn_config(comedi_device *dev,comedi_subdevice *s,
4283 comedi_insn *insn,lsampl_t *data)
4288 if(insn->n < 1) return -EINVAL;
4290 if(data[0] == INSN_CONFIG_SET_RTSI_CLOCK_MODE){
4294 devpriv->rtsi_trig_direction_reg &= ~0x03;
4295 devpriv->rtsi_trig_direction_reg |= data[1];
4296 devpriv->stc_writew(dev, devpriv->rtsi_trig_direction_reg, RTSI_Trig_Direction_Register);
4299 chan = CR_CHAN(insn->chanspec);
4300 if(chan > 6) return -EINVAL;
4305 case INSN_CONFIG_DIO_OUTPUT:
4306 devpriv->rtsi_trig_direction_reg |= (1 << bit);
4307 devpriv->stc_writew(dev, devpriv->rtsi_trig_direction_reg, RTSI_Trig_Direction_Register);
4309 case INSN_CONFIG_DIO_INPUT:
4310 devpriv->rtsi_trig_direction_reg &= ~(1 << bit);
4311 devpriv->stc_writew(dev, devpriv->rtsi_trig_direction_reg, RTSI_Trig_Direction_Register);
4313 case INSN_CONFIG_DIO_QUERY:
4314 data[1] = (devpriv->rtsi_trig_direction_reg & (1<<bit)) ? INSN_CONFIG_DIO_OUTPUT : INSN_CONFIG_DIO_INPUT;
4325 static int cs5529_wait_for_idle(comedi_device *dev)
4327 unsigned short status;
4328 const int timeout = HZ;
4331 for(i = 0; i < timeout; i++)
4333 status = ni_ao_win_inw(dev, CAL_ADC_Status_67xx);
4334 if((status & CSS_ADC_BUSY) == 0)
4338 set_current_state(TASK_INTERRUPTIBLE);
4339 if(schedule_timeout(1))
4344 //printk("looped %i times waiting for idle\n", i);
4347 rt_printk("%s: %s: timeout\n", __FILE__, __FUNCTION__);
4353 static void cs5529_command(comedi_device *dev, unsigned short value)
4355 static const int timeout = 100;
4358 ni_ao_win_outw(dev, value, CAL_ADC_Command_67xx);
4359 /* give time for command to start being serially clocked into cs5529.
4360 * this insures that the CSS_ADC_BUSY bit will get properly
4361 * set before we exit this function.
4363 for(i = 0; i < timeout; i++)
4365 if((ni_ao_win_inw(dev, CAL_ADC_Status_67xx) & CSS_ADC_BUSY))
4369 //printk("looped %i times writing command to cs5529\n", i);
4372 comedi_error(dev, "possible problem - never saw adc go busy?");
4376 /* write to cs5529 register */
4377 static void cs5529_config_write(comedi_device *dev, unsigned int value, unsigned int reg_select_bits)
4379 ni_ao_win_outw(dev, ((value >> 16) & 0xff), CAL_ADC_Config_Data_High_Word_67xx);
4380 ni_ao_win_outw(dev, (value & 0xffff), CAL_ADC_Config_Data_Low_Word_67xx);
4381 reg_select_bits &= CSCMD_REGISTER_SELECT_MASK;
4382 cs5529_command(dev, CSCMD_COMMAND | reg_select_bits);
4383 if(cs5529_wait_for_idle(dev))
4384 comedi_error(dev, "time or signal in cs5529_config_write()");
4387 /* read from cs5529 register */
4388 static unsigned int cs5529_config_read(comedi_device *dev, unsigned int reg_select_bits)
4392 reg_select_bits &= CSCMD_REGISTER_SELECT_MASK;
4393 cs5529_command(dev, CSCMD_COMMAND | CSCMD_READ | reg_select_bits);
4394 if(cs5529_wait_for_idle(dev))
4395 comedi_error(dev, "timeout or signal in cs5529_config_read()");
4396 value = (ni_ao_win_inw(dev, CAL_ADC_Config_Data_High_Word_67xx) << 16) & 0xff0000;
4397 value |= ni_ao_win_inw(dev, CAL_ADC_Config_Data_Low_Word_67xx) & 0xffff;
4401 static int cs5529_do_conversion(comedi_device *dev, unsigned short *data)
4404 unsigned short status;
4406 cs5529_command(dev, CSCMD_COMMAND | CSCMD_SINGLE_CONVERSION);
4407 retval = cs5529_wait_for_idle(dev);
4410 comedi_error(dev, "timeout or signal in cs5529_do_conversion()");
4413 status = ni_ao_win_inw(dev, CAL_ADC_Status_67xx);
4414 if(status & CSS_OSC_DETECT)
4416 rt_printk("ni_mio_common: cs5529 conversion error, status CSS_OSC_DETECT\n");
4419 if(status & CSS_OVERRANGE)
4421 rt_printk("ni_mio_common: cs5529 conversion error, overrange (ignoring)\n");
4425 *data = ni_ao_win_inw(dev, CAL_ADC_Data_67xx);
4426 /* cs5529 returns 16 bit signed data in bipolar mode */
4432 static int cs5529_ai_insn_read(comedi_device *dev,comedi_subdevice *s,comedi_insn *insn,lsampl_t *data)
4435 unsigned short sample;
4436 unsigned int channel_select;
4437 const unsigned int INTERNAL_REF = 0x1000;
4439 /* Set calibration adc source. Docs lie, reference select bits 8 to 11
4440 * do nothing. bit 12 seems to chooses internal reference voltage, bit
4441 * 13 causes the adc input to go overrange (maybe reads external reference?) */
4442 if(insn->chanspec & CR_ALT_SOURCE)
4443 channel_select = INTERNAL_REF;
4445 channel_select = CR_CHAN(insn->chanspec);
4446 ni_ao_win_outw(dev, channel_select, AO_Calibration_Channel_Select_67xx);
4448 for(n = 0; n < insn->n; n++)
4450 retval = cs5529_do_conversion(dev, &sample);
4451 if(retval < 0) return retval;
4457 static int init_cs5529(comedi_device *dev)
4459 unsigned int config_bits = CSCFG_PORT_MODE | CSCFG_WORD_RATE_2180_CYCLES;
4462 /* do self-calibration */
4463 cs5529_config_write(dev, config_bits | CSCFG_SELF_CAL_OFFSET_GAIN, CSCMD_CONFIG_REGISTER);
4464 /* need to force a conversion for calibration to run */
4465 cs5529_do_conversion(dev, NULL);
4467 /* force gain calibration to 1 */
4468 cs5529_config_write(dev, 0x400000, CSCMD_GAIN_REGISTER);
4469 cs5529_config_write(dev, config_bits | CSCFG_SELF_CAL_OFFSET, CSCMD_CONFIG_REGISTER);
4470 if(cs5529_wait_for_idle(dev))
4471 comedi_error(dev, "timeout or signal in init_cs5529()\n");
4475 rt_printk("config: 0x%x\n", cs5529_config_read(dev, CSCMD_CONFIG_REGISTER));
4476 rt_printk("gain: 0x%x\n", cs5529_config_read(dev, CSCMD_GAIN_REGISTER));
4477 rt_printk("offset: 0x%x\n", cs5529_config_read(dev, CSCMD_OFFSET_REGISTER));