⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dt3000.c

📁 rtlinux-3.2-pre3.tar.bz2 rtlinux3.2-pre3的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/*    module/dt3000.c    Data Translation DT3000 series driver    COMEDI - Linux Control and Measurement Device Interface    Copyright (C) 1999 David A. Schleef <ds@schleef.org>    This program is free software; you can redistribute it and/or modify    it under the terms of the GNU General Public License as published by    the Free Software Foundation; either version 2 of the License, or    (at your option) any later version.    This program is distributed in the hope that it will be useful,    but WITHOUT ANY WARRANTY; without even the implied warranty of    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    GNU General Public License for more details.    You should have received a copy of the GNU General Public License    along with this program; if not, write to the Free Software    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.*//*Driver: dt3000.oDescription: Data Translation DT3000 seriesAuthor: dsDevices: [Data Translation] DT3001 (dt3000), DT3001-PGL, DT3002, DT3003,  DT3003-PGL, DT3004, DT3005, DT3004-200Status: worksThere is code to support AI commands, but it may not work.AO commands are not supported.*//*   The DT3000 series is Data Translation's attempt to make a PCI   data acquisition board.  The design of this series is very nice,   since each board has an on-board DSP (Texas Instruments TMS320C52).   However, a few details are a little annoying.  The boards lack   bus-mastering DMA, which eliminates them from serious work.   They also are not capable of autocalibration, which is a common   feature in modern hardware.  The default firmware is pretty bad,   making it nearly impossible to write an RT compatible driver.   It would make an interesting project to write a decent firmware   for these boards.   Data Translation originally wanted an NDA for the documentation   for the 3k series.  However, if you ask nicely, they might send   you the docs without one, also.*/#define DEBUG 1#include <linux/comedidev.h>#include <linux/delay.h>#include <linux/pci.h>#define PCI_VENDOR_ID_DT	0x1116static comedi_lrange range_dt3000_ai = { 4, {	RANGE( -10,	10 ),	RANGE( -5,	5 ),	RANGE( -2.5,	2.5 ),	RANGE( -1.25,	1.25 )}};static comedi_lrange range_dt3000_ai_pgl = { 4, {	RANGE( -10,	10 ),	RANGE( -1,	1 ),	RANGE( -0.1,	0.1 ),	RANGE( -0.02,	0.02 )}};typedef struct{	char *name;	unsigned int device_id;	int adchan;	int adbits;	int ai_speed;	comedi_lrange *adrange;	int dachan;	int dabits;}dt3k_boardtype;static dt3k_boardtype dt3k_boardtypes[]={	{	name:		"dt3001",		device_id:	0x22,		adchan:		16,		adbits:		12,		adrange:	&range_dt3000_ai,		ai_speed:	3000,		dachan:		2,		dabits:		12,	},	{	name:		"dt3001-pgl",		device_id:	0x27,		adchan:		16,		adbits:		12,		adrange:	&range_dt3000_ai_pgl,		ai_speed:	3000,		dachan:		2,		dabits:		12,	},	{	name:		"dt3002",		device_id:	0x23,		adchan:		32,		adbits:		12,		adrange:	&range_dt3000_ai,		ai_speed:	3000,		dachan:		0,		dabits:		0,	},	{	name:		"dt3003",		device_id:	0x24,		adchan:		64,		adbits:		12,		adrange:	&range_dt3000_ai,		ai_speed:	3000,		dachan:		2,		dabits:		12,	},	{	name:		"dt3003-pgl",		device_id:	0x28,		adchan:		64,		adbits:		12,		adrange:	&range_dt3000_ai_pgl,		ai_speed:	3000,		dachan:		2,		dabits:		12,	},	{	name:		"dt3004",		device_id:	0x25,		adchan:		16,		adbits:		16,		adrange:	&range_dt3000_ai,		ai_speed:	10000,		dachan:		2,		dabits:		12,	},	{	name:		"dt3005",	/* a.k.a. 3004-200 */		device_id:	0x26,		adchan:		16,		adbits:		16,		adrange:	&range_dt3000_ai,		ai_speed:	5000,		dachan:		2,		dabits:		12,	},};#define n_dt3k_boards sizeof(dt3k_boardtypes)/sizeof(dt3k_boardtype)#define this_board ((dt3k_boardtype *)dev->board_ptr)static struct pci_device_id dt3k_pci_table[] __devinitdata = {	{ PCI_VENDOR_ID_DT, 0x0022, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },	{ PCI_VENDOR_ID_DT, 0x0027, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },	{ PCI_VENDOR_ID_DT, 0x0023, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },	{ PCI_VENDOR_ID_DT, 0x0024, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },	{ PCI_VENDOR_ID_DT, 0x0028, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },	{ PCI_VENDOR_ID_DT, 0x0025, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },	{ PCI_VENDOR_ID_DT, 0x0026, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },	{ 0 }};MODULE_DEVICE_TABLE(pci, dt3k_pci_table);#define DT3000_SIZE		(4*0x1000)/* dual-ported RAM location definitions */#define DPR_DAC_buffer		(4*0x000)#define DPR_ADC_buffer		(4*0x800)#define DPR_Command		(4*0xfd3)#define DPR_SubSys		(4*0xfd3)#define DPR_Encode		(4*0xfd4)#define DPR_Params(a)		(4*(0xfd5+(a)))#define DPR_Tick_Reg_Lo		(4*0xff5)#define DPR_Tick_Reg_Hi		(4*0xff6)#define DPR_DA_Buf_Front	(4*0xff7)#define DPR_DA_Buf_Rear		(4*0xff8)#define DPR_AD_Buf_Front	(4*0xff9)#define DPR_AD_Buf_Rear		(4*0xffa)#define DPR_Int_Mask		(4*0xffb)#define DPR_Intr_Flag		(4*0xffc)#define DPR_Response_Mbx	(4*0xffe)#define DPR_Command_Mbx		(4*0xfff)#define AI_FIFO_DEPTH	2003#define AO_FIFO_DEPTH	2048/* command list */#define CMD_GETBRDINFO		0#define CMD_CONFIG		1#define CMD_GETCONFIG		2#define CMD_START		3#define CMD_STOP		4#define CMD_READSINGLE		5#define CMD_WRITESINGLE		6#define CMD_CALCCLOCK		7#define CMD_READEVENTS		8#define CMD_WRITECTCTRL		16#define CMD_READCTCTRL		17#define CMD_WRITECT		18#define CMD_READCT		19#define CMD_WRITEDATA		32#define CMD_READDATA		33#define CMD_WRITEIO		34#define CMD_READIO		35#define CMD_WRITECODE		36#define CMD_READCODE		37#define CMD_EXECUTE		38#define CMD_HALT		48#define SUBS_AI		0#define SUBS_AO		1#define SUBS_DIN	2#define SUBS_DOUT	3#define SUBS_MEM	4#define SUBS_CT		5/* interrupt flags */#define DT3000_CMDONE		0x80#define DT3000_CTDONE		0x40#define DT3000_DAHWERR		0x20#define DT3000_DASWERR		0x10#define DT3000_DAEMPTY		0x08#define DT3000_ADHWERR		0x04#define DT3000_ADSWERR		0x02#define DT3000_ADFULL		0x01#define DT3000_COMPLETION_MASK	0xff00#define DT3000_COMMAND_MASK	0x00ff#define DT3000_NOTPROCESSED	0x0000#define DT3000_NOERROR		0x5500#define DT3000_ERROR		0xaa00#define DT3000_NOTSUPPORTED	0xff00#define DT3000_EXTERNAL_CLOCK	1#define DT3000_RISING_EDGE	2#define TMODE_MASK		0x1c#define DT3000_AD_TRIG_INTERNAL		(0<<2)#define DT3000_AD_TRIG_EXTERNAL		(1<<2)#define DT3000_AD_RETRIG_INTERNAL	(2<<2)#define DT3000_AD_RETRIG_EXTERNAL	(3<<2)#define DT3000_AD_EXTRETRIG		(4<<2)#define DT3000_CHANNEL_MODE_SE		0#define DT3000_CHANNEL_MODE_DI		1typedef struct{	struct pci_dev *pci_dev;	unsigned long phys_addr;	void *io_addr;	unsigned int lock;	lsampl_t ao_readback[2];	unsigned int ai_front;	unsigned int ai_rear;}dt3k_private;#define devpriv ((dt3k_private *)dev->private)static int dt3000_attach(comedi_device *dev,comedi_devconfig *it);static int dt3000_detach(comedi_device *dev);static comedi_driver driver_dt3000={	driver_name:	"dt3000",	module:		THIS_MODULE,	attach:		dt3000_attach,	detach:		dt3000_detach,};COMEDI_INITCLEANUP(driver_dt3000);static void dt3k_ai_empty_fifo(comedi_device *dev,comedi_subdevice *s);static int dt3k_ns_to_timer(unsigned int timer_base, unsigned int *arg,	unsigned int round_mode);static int dt3k_ai_cancel(comedi_device *dev,comedi_subdevice *s);#ifdef DEBUGstatic void debug_intr_flags(unsigned int flags);#endif#define TIMEOUT 100static int dt3k_send_cmd(comedi_device *dev,unsigned int cmd){	int i;	unsigned int status = 0;	writew(cmd,dev->iobase+DPR_Command_Mbx);		for(i=0;i<TIMEOUT;i++){		status=readw(dev->iobase+DPR_Command_Mbx);		if((status&DT3000_COMPLETION_MASK)!=DT3000_NOTPROCESSED)			break;		comedi_udelay(1);	}	if((status&DT3000_COMPLETION_MASK)==DT3000_NOERROR){		return 0;	}	printk("dt3k_send_cmd() timeout/error status=0x%04x\n",status);		return -ETIME;}static unsigned int dt3k_readsingle(comedi_device *dev,unsigned int subsys,	unsigned int chan,unsigned int gain){	writew(subsys,dev->iobase+DPR_SubSys);		writew(chan,dev->iobase+DPR_Params(0));	writew(gain,dev->iobase+DPR_Params(1));		dt3k_send_cmd(dev,CMD_READSINGLE);	return readw(dev->iobase+DPR_Params(2));}static void dt3k_writesingle(comedi_device *dev,unsigned int subsys,	unsigned int chan,unsigned int data){	writew(subsys,dev->iobase+DPR_SubSys);		writew(chan,dev->iobase+DPR_Params(0));	writew(0,dev->iobase+DPR_Params(1));	writew(data,dev->iobase+DPR_Params(2));		dt3k_send_cmd(dev,CMD_WRITESINGLE);}static int debug_n_ints = 0;static void dt3k_interrupt(int irq, void *d, struct pt_regs *regs){	comedi_device *dev = d;	comedi_subdevice *s = dev->subdevices + 0;	unsigned int status;	status = readw(dev->iobase+DPR_Intr_Flag);	debug_intr_flags(status);	if(status & DT3000_ADFULL){		dt3k_ai_empty_fifo(dev,s);		s->async->events |= COMEDI_CB_BLOCK;	}	if(status & (DT3000_ADSWERR | DT3000_ADHWERR)){		s->async->events |= COMEDI_CB_ERROR | COMEDI_CB_EOA;	}	debug_n_ints++;	if(debug_n_ints>=10){		dt3k_ai_cancel(dev,s);		s->async->events |= COMEDI_CB_EOA;	}	comedi_event(dev,s,s->async->events);}#ifdef DEBUGstatic char *intr_flags[] = {	"AdFull", "AdSwError", "AdHwError", "DaEmpty",	"DaSwError", "DaHwError", "CtDone", "CmDone",};static void debug_intr_flags(unsigned int flags){	int i;	printk("dt3k: intr_flags:");	for(i=0;i<8;i++){		if(flags & (1<<i)){			printk(" %s",intr_flags[i]);		}	}	printk("\n");}#endifstatic void dt3k_ai_empty_fifo(comedi_device *dev,comedi_subdevice *s){	int front;	int rear;	int count;	int i;	sampl_t data;	front = readw(dev->iobase + DPR_AD_Buf_Front);	count = front - devpriv->ai_front;	if(count<0)count += AI_FIFO_DEPTH;printk("reading %d samples\n",count);	rear = devpriv->ai_rear;	for(i=0;i<count;i++){		data = readw(dev->iobase + DPR_ADC_buffer + rear);		comedi_buf_put(s->async, data);		rear++;		if(rear>=AI_FIFO_DEPTH)rear = 0;	}		devpriv->ai_rear = rear;	writew(rear,dev->iobase + DPR_AD_Buf_Rear);}static int dt3k_ai_cmdtest(comedi_device *dev,comedi_subdevice *s,	comedi_cmd *cmd){	int err=0;	int tmp;	/* step 1: make sure trigger sources are trivially valid */	tmp=cmd->start_src;	cmd->start_src &= TRIG_NOW;	if(!cmd->start_src || tmp!=cmd->start_src)err++;	tmp=cmd->scan_begin_src;	cmd->scan_begin_src &= TRIG_TIMER;	if(!cmd->scan_begin_src || tmp!=cmd->scan_begin_src)err++;	tmp=cmd->convert_src;	cmd->convert_src &= TRIG_TIMER;	if(!cmd->convert_src || tmp!=cmd->convert_src)err++;	tmp=cmd->scan_end_src;	cmd->scan_end_src &= TRIG_COUNT;	if(!cmd->scan_end_src || tmp!=cmd->scan_end_src)err++;	tmp=cmd->stop_src;	cmd->stop_src &= TRIG_COUNT;	if(!cmd->stop_src || tmp!=cmd->stop_src)err++;	if(err)return 1;	/* step 2: make sure trigger sources are unique and mutually compatible */	if(err)return 2;	/* step 3: make sure arguments are trivially compatible */	if(cmd->start_arg!=0){		cmd->start_arg=0;		err++;	}	if(cmd->scan_begin_src==TRIG_TIMER){		if(cmd->scan_begin_arg < this_board->ai_speed){			cmd->scan_begin_arg = this_board->ai_speed;			err++;		}		if(cmd->scan_begin_arg > 100*16*65535){			cmd->scan_begin_arg = 100*16*65535;			err++;		}	}else{		/* not supported */	}	if(cmd->convert_src==TRIG_TIMER){		if(cmd->convert_arg < this_board->ai_speed){			cmd->convert_arg = this_board->ai_speed;			err++;		}		if(cmd->convert_arg > 50*16*65535){			cmd->convert_arg = 50*16*65535;			err++;		}	}else{		/* not supported */	}	if(cmd->scan_end_arg!=cmd->chanlist_len){

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -