]> rtime.felk.cvut.cz Git - mf6xx.git/commitdiff
Addded functions for A/D, D/A reading/writing. Not tested.
authorRostislav Lisovy <lisovy@gmail.com>
Fri, 22 Apr 2011 12:54:01 +0000 (14:54 +0200)
committerRostislav Lisovy <lisovy@gmail.com>
Fri, 22 Apr 2011 12:54:01 +0000 (14:54 +0200)
src/comedi/mf614_simple_driver/kernel/mf614.c

index 73568dd23b6e6b05df07e0acf1c0c99d6cd5991c..4af0b97cc7f8c1b0fae3fea8f8c40ec1124e730e 100644 (file)
@@ -4,8 +4,31 @@
 #define PCI_VENDOR_ID_MF614 0x186c
 #define PCI_DEVICE_ID_MF614 0x0614
 
-#define DIN_reg                0x6
-#define DOUT_reg       0x6
+/* BAR0 regs */
+#define ADCTRL_reg             0x0
+#define ADLO_reg               0x0
+#define ADHI_reg               0x1
+
+#define DIN_reg                        0x6
+#define DOUT_reg               0x6
+
+#define DA0LO_reg              0x8
+#define DA0HI_reg              0x9
+#define DA1LO_reg              0xA
+#define DA1HI_reg              0xB
+#define DA2LO_reg              0xC
+#define DA2HI_reg              0xD
+#define DA3LO_reg              0xE
+#define DA3HI_reg              0xF
+
+#define ADCTRL_DEFAULT         (1 << 6)
+#define ADCTRL_RNG             (1 << 4)
+#define ADCTRL_BIP             (1 << 3)
+
+/* BAR2 regs */
+#define STAT_reg               0x10
+
+#define STAT_CC                (1 << 2)
 
 
 static struct pci_device_id mf614_pci_table[] __devinitdata = {
@@ -31,9 +54,9 @@ static const mf614_board mf614_boards[] = {
                name:           "mf614",        /* device name          */
                device_id:      PCI_DEVICE_ID_MF614,    /* PCI dev ID   */
                ai_chans:       8,              /* Num of ADC channels  */
-               ai_bits:        14,             /* Num of ADC bits      */
-               ao_chans:       8,              /* Num of DAC channels  */
-               ao_bits:        14,             /* Num of DAC bits      */
+               ai_bits:        12,             /* Num of ADC bits      */
+               ao_chans:       4,              /* Num of DAC channels  */
+               ao_bits:        12,             /* Num of DAC bits      */
                di_chans:       8,              /* Num of digital in    */
                do_chans:       8,              /* Num of digital out   */
        }
@@ -82,7 +105,8 @@ static int mf614_attach(struct comedi_device *dev, struct comedi_devconfig *it)
 
        printk("comedi%d: mf614: ", dev->minor);
 
-       if(alloc_private(dev, sizeof(mf614_private)) < 0) { // No need to free() later
+       //Comedi function; No need to kfree() later
+       if(alloc_private(dev, sizeof(mf614_private)) < 0) {
                return -ENOMEM;
        }
        
@@ -144,11 +168,12 @@ found:
                goto out_unmap;
        }
        
-       devpriv->in_use = 1;
+       
        dev->board_name = thisboard->name;
        
        /* Allocate subdevices */
-       if(alloc_subdevices(dev, 2) < 0) {
+       //Comedi function; No need to kfree() later
+       if(alloc_subdevices(dev, 4) < 0) {
                return -ENOMEM;
        }
        
@@ -168,9 +193,33 @@ found:
        s->n_chan = thisboard->do_chans;
        s->maxdata = 1;
        s->range_table = &range_digital;
-       s->insn_bits  = mf614_do_insn_bits;
+       s->insn_bits = mf614_do_insn_bits;
+       
+       /* Analog input */
+       s = dev->subdevices + 2;
+       s->type = COMEDI_SUBD_AI;
+       s->subdev_flags = SDF_READABLE|SDF_GROUND;
+       s->n_chan = thisboard->ai_chans;
+       s->maxdata = (1<<thisboard->ai_bits)-1;
+       s->range_table = &range_bipolar10;
+       s->len_chanlist = 8;
+       s->insn_read = mf614_ai_rinsn;
+       s->insn_config = mf614_ai_cfg;
+
+       /* Analog output */
+       s = dev->subdevices + 3;
+       s->type = COMEDI_SUBD_AO;
+       s->subdev_flags = SDF_WRITABLE;
+       s->n_chan = thisboard->ao_chans;
+       s->maxdata = (1<<thisboard->ao_bits)-1;
+       s->range_table = &range_bipolar10;
+       s->insn_write = mf614_ao_winsn;
+       s->insn_read = mf614_ao_rinsn;
+       s->insn_config = mf614_ao_cfg;
+       
 
        printk("comedi%d: mf614: Driver attached\n", dev->minor);
+       devpriv->in_use = 1;
        
        return 0;
 
@@ -184,6 +233,14 @@ out_exit:
        return -ENODEV;
 }
 
+/*
+ * _detach is called to deconfigure a device. It should deallocate
+ * resources.  
+ * This function is also called when _attach() fails, so it should be
+ * careful not to release resources that were not necessarily
+ * allocated by _attach(). dev->private and dev->subdevices are
+ * deallocated automatically by the core.
+ */
 static int mf614_detach(struct comedi_device *dev)
 {
        if(devpriv && devpriv->pci_dev)
@@ -234,6 +291,123 @@ static int mf614_di_insn_bits(struct comedi_device *dev,
        return 2;
 }
 
+/* Read n samples on Analog Input channel */
+static int mf614_ai_rinsn(struct comedi_device *dev,
+                          struct comedi_subdevice *s, 
+                          struct comedi_insn *insn, unsigned int *data)
+{
+       unsigned int dat, n, i, status;
+       unsigned int chan = CR_CHAN(insn->chanspec);
+
+       /* write channel to multiplexer */
+       iowrite8((chan) | ADCTRL_DEFAULT | ADCTRL_BIP | ADCTRL_RNG, devpriv->BAR0_io + ADCTRL_reg);
+
+       /* convert n samples */
+       for(n = 0; n < insn->n; n ++) {
+               #define TIMEOUT 100 
+               /* wait for conversion to end */
+               for(i = 0; i < TIMEOUT; i++){
+                       status = 1;
+                       status = ioread8(devpriv->BAR2_io + STAT_reg);
+                       if(!(status & STAT_CC)) {
+                               break;
+                       }
+               }
+               if (i == TIMEOUT) {
+                       comedi_error(dev, "Conversion timeout!\n");
+                       return -ETIMEDOUT;
+               }
+
+               /* read data */
+               dat = ioread8(devpriv->BAR0_io + ADLO_reg);
+               dat |= (ioread8(devpriv->BAR0_io + ADHI_reg) << 8);
+
+               /* mangle the data as necessary */
+               dat ^= 1 << (thisboard->ai_bits-1);
+
+               data[n] = dat;
+       }
+
+       /* return the number of samples read/written */
+       return n;
+}
+
+/* Analog input configuration */
+static int mf614_ai_cfg(struct comedi_device *dev,
+                        struct comedi_subdevice *s, 
+                        struct comedi_insn *insn, unsigned int *data)
+{
+       return insn->n;
+}
+
+/* Write n samples on Analog Output channel */
+static int mf614_ao_winsn(struct comedi_device *dev,
+                          struct comedi_subdevice *s, 
+                          struct comedi_insn *insn, unsigned int *data)
+{
+       unsigned int i, status;
+       unsigned int chan = CR_CHAN(insn->chanspec);
+       unsigned int regLO, regHI;
+
+       /* Writing a list of values to an AO channel is probably not
+        * very useful, but that's how the interface is defined. */
+       for(i = 0; i < insn->n; i++){
+               switch(chan) {
+                       case 0:
+                               regLO = DA0LO_reg;
+                               regHI = DA0HI_reg;
+                               break;
+                       case 1:
+                               regLO = DA1LO_reg;
+                               regHI = DA1HI_reg;
+                               break;
+                       case 2:
+                               regLO = DA2LO_reg;
+                               regHI = DA2HI_reg;
+                               break;
+                       case 3:
+                               regLO = DA3LO_reg;
+                               regHI = DA3HI_reg;
+                               break;
+                       default:
+                               comedi_error(dev, "Wrong D/A channel number!\n");
+                               return -ENODEV;
+               }
+               
+               iowrite8((u8)(data[i] & 0xFF), devpriv->BAR0_io + regLO);
+               iowrite8((u8)(data[i] >> 8), devpriv->BAR0_io + regHI);
+               devpriv->ao_readback[chan] = data[i];
+       }
+
+       /* return the number of samples read/written */
+       return i;
+}
+
+/* Analog output configuration */
+static int mf614_ao_cfg(struct comedi_device *dev,
+                        struct comedi_subdevice *s, 
+                        struct comedi_insn *insn, unsigned int *data)
+{
+       return insn->n;
+}
+
+
+/* AO subdevices should have a read insn as well as a write insn.
+ * Usually this means copying a value stored in devpriv. */
+static int mf614_ao_rinsn(struct comedi_device *dev,
+                          struct comedi_subdevice *s, 
+                          struct comedi_insn *insn, unsigned int *data)
+{
+       unsigned int i, chan = CR_CHAN(insn->chanspec);
+
+       for(i = 0; i < insn->n; i++) {
+               data[i] = devpriv->ao_readback[chan];
+       }
+
+       return i;
+}
+
+
 /*
 ==============================================================================
 */