]> git.zerfleddert.de Git - rigol/commitdiff
completely rewritten usbtmc_sendscpi
authorMichael Gernoth <michael@gernoth.net>
Mon, 7 Jun 2010 21:51:55 +0000 (23:51 +0200)
committerMichael Gernoth <michael@gernoth.net>
Mon, 7 Jun 2010 21:51:55 +0000 (23:51 +0200)
scope.h
usbtmc.c

diff --git a/scope.h b/scope.h
index 9feef05a012aa2aad0fef31f010ae8a7d99ad0ea..1fdb515fe42a62741b9527472b0b993ae6477a09 100644 (file)
--- a/scope.h
+++ b/scope.h
@@ -2,9 +2,10 @@ struct scope {
        struct {
                struct usb_dev_handle *dev;
                int ep_bulk_out;
-               unsigned int wMaxPacketSize_out;
                int ep_bulk_in;
                unsigned int wMaxPacketSize_in;
+               unsigned char bTag;
+               int brokenRigol;
        } usb;
        char idn[128];
 };
index eb7737ad287efda71b1f49008d1835da374c6595..5b07ade655dc14a32ae28c8686acbcb31599bfd6 100644 (file)
--- a/usbtmc.c
+++ b/usbtmc.c
 #error BYTE_ORDER not defined/known!
 #endif
 
-//Helper-routine: Convert a little-endian 4-byte word to an int
-static void int2chars(unsigned char *buff,unsigned int a) {
-       buff[3]=(a>>24)&0xff;
-       buff[2]=(a>>16)&0xff;
-       buff[1]=(a>>8)&0xff;
-       buff[0]=(a)&0xff;
-}
-
-//Helper-routine: Convert an int to  little-endian 4-byte word
-unsigned int chars2int(unsigned char *buff) {
-       unsigned int a;
-       a=buff[3]<<24;
-       a+=buff[2]<<16;
-       a+=buff[1]<<8;
-       a+=buff[0];
-       return a;
-}
+#define USB_ERROR(x) do { if (x < 0) { fprintf(stderr, "usb: %s\n", usb_strerror()); exit(EXIT_FAILURE); } } while(0)
 
-//This routine locates a scope by VID/PID and returns an opened handle to it.
-static usb_dev_handle *usbtmc_find_scope() {
+/* This routine locates a scope by VID/PID and returns a struct scope* for it */
+static struct scope* usbtmc_find_scope() {
        struct usb_bus *bus;
        struct usb_device *dev=NULL;
+       struct usb_dev_handle *devh;
+
+       struct scope *sc;
+
        usb_find_busses();
        usb_find_devices();
+
        for (bus=usb_busses; bus; bus=bus->next) {
                for (dev=bus->devices; dev; dev=dev->next) {
-                       //fprintf(stderr,"Prod/dev: %04X:%04X\n",dev->descriptor.idVendor,dev->descriptor.idProduct);
-                       if (dev->descriptor.idVendor==0x400 && dev->descriptor.idProduct==0x5dc) {
-                               return usb_open(dev);
+                       if (dev->descriptor.idVendor == 0x400 && dev->descriptor.idProduct == 0x5dc) {
+                               devh = usb_open(dev);
+                               if (devh == NULL)
+                                       return NULL;
+
+                               sc = calloc(1, sizeof(struct scope));
+                               if (sc == NULL) {
+                                       perror("calloc");
+                                       exit(EXIT_FAILURE);
+                               }
+
+                               sc->usb.dev = devh;
+
+                               /* TODO: FIXME */
+                               sc->usb.brokenRigol = 1;
+                               sc->usb.ep_bulk_out = 1;
+                               sc->usb.ep_bulk_in = 2;
+                               sc->usb.wMaxPacketSize_in = 0x40;
+                               
+                               return sc;
                        }
                }
        }
+
        return NULL;
 }
 
-//Send a scpi-command to the scope. The response goes into the buffer
-//called resp, with a size of resplen. If resp==NULL, no response
-//is requested.
+/*
+ * Send a scpi-command to the scope. The response goes into the buffer
+ * called resp, with a size of resplen. If resp==NULL, no response
+ * is requested.
+ */
 int usbtmc_sendscpi(struct scope *sc, char* cmd, 
                unsigned char *resp, int resplen) {
-       unsigned char *buff;
-       int len,r,i;
+       int len,r;
        int cmdlen = strlen(cmd);
-       static unsigned char seq=0;
-
-
-       buff=malloc(0x40);
-       seq++;
-       buff[0]=1; //func
-       buff[1]=seq; buff[2]=~seq; //nseq
-       buff[3]=0;
-       int2chars(buff+4, cmdlen);
-       buff[8]=1;
-       buff[9]=0x00;
-       buff[10]=0x00;
-       buff[11]=0x00;
-       //fprintf(stderr,"Writing header len=%d\n", cmdlen);
-       //printb(buff,12);
-       r=usb_bulk_write(sc->usb.dev, 1, (char*)buff, 12, USB_TIMEOUT);
-       //fprintf(stderr,"%i bytes written. Writing cmd\n",r);
-       //printb(cmd, cmdlen);
-       r=usb_bulk_write(sc->usb.dev, 1, cmd, cmdlen, USB_TIMEOUT);
-       //fprintf(stderr,"%i bytes written.\n",r);
+       struct usbtmc_header *req;
+
+       sc->usb.bTag++;
+
+       len = sizeof(struct usbtmc_header) + cmdlen;
+       if (len%4)
+               len += 4 - (len%4);
+       
+       req = calloc(1, len);
+       if (req == NULL) {
+               perror("calloc");
+               exit(EXIT_FAILURE);
+       }
+
+       req->MsgID = USBTMC_DEV_DEP_MSG_OUT;
+       req->bTag = sc->usb.bTag;
+       req->bTagInverse = ~sc->usb.bTag;
+       req->TransferSize = LE32(cmdlen);
+       req->bmTransferAttributes = USBTMC_TRANSFERATTRIB_EOM;
+       memcpy(req->msg, cmd, cmdlen);
+
+       if (sc->usb.brokenRigol) {
+               r=usb_bulk_write(sc->usb.dev, sc->usb.ep_bulk_out,
+                       (char*)req, sizeof(struct usbtmc_header),
+                       USB_TIMEOUT);
+               USB_ERROR(r);
+
+               r=usb_bulk_write(sc->usb.dev, sc->usb.ep_bulk_out,
+                       (char*)&(req->msg), len - sizeof(struct usbtmc_header),
+                       USB_TIMEOUT);
+               USB_ERROR(r);
+       } else {
+               r=usb_bulk_write(sc->usb.dev, sc->usb.ep_bulk_out,
+                       (char*)req, len, USB_TIMEOUT);
+               USB_ERROR(r);
+       }
+
+       free(req);
+
        if (resp != NULL && resplen != 0) {
-               //send read command
-               buff[0]=2; //func
-               seq++;
-               buff[1]=seq; buff[2]=~seq; //nseq
-               int2chars(buff+4,0x40);
-               buff[8]=0;
-               buff[9]=0xA;
-               buff[10]=0;
-               buff[11]=0;
-               //fprintf(stderr,"Writing resp req header\n");
-               //printb(buff,12);
-               r=usb_bulk_write(sc->usb.dev, 1, (char*)buff, 12, USB_TIMEOUT);
-               //fprintf(stderr,"%i bytes written. Reading response hdr\n",r);
-               r=usb_bulk_read(sc->usb.dev, 2, (char*)buff, 0x40, USB_TIMEOUT);
-               //printb(buff,r);
-               len=chars2int(buff+4);
-               //fprintf(stderr,"%i bytes read. Resplen=%i\n",r,len);
-               for (i=0; i<(r-12); i++) {
-                       if (i<resplen) resp[i] = buff[i+12];
+               unsigned char *buff;
+               struct usbtmc_header *res;
+               int bytes_read;
+
+               sc->usb.bTag++;
+
+               req = calloc(1, sizeof(struct usbtmc_header));
+               if (req == NULL) {
+                       perror("calloc");
+                       exit(EXIT_FAILURE);
+               }
+
+               req->MsgID = USBTMC_REQUEST_DEV_DEP_MSG_IN;
+               req->bTag = sc->usb.bTag;
+               req->bTagInverse = ~sc->usb.bTag;
+               req->TransferSize = LE32(sc->usb.wMaxPacketSize_in);
+               req->bmTransferAttributes = 0;
+               req->TermChar = 0;
+
+               /* send read command */
+               r=usb_bulk_write(sc->usb.dev, sc->usb.ep_bulk_out,
+                       (char*)req, sizeof(struct usbtmc_header), USB_TIMEOUT);
+               USB_ERROR(r);
+
+               free(req);
+
+               buff=malloc(sc->usb.wMaxPacketSize_in);
+               if (buff == NULL) {
+                       perror("malloc");
+                       exit(EXIT_FAILURE);
                }
-               //printb(resp,r-12);
-               if (len > 0x40-12) {
-                       //fprintf(stderr," Reading response:\n");
-                       if (resplen<len) len=resplen;
-                       r=usb_bulk_read(sc->usb.dev, 2, (char*)resp+(0x40-12), len-(0x40-12), USB_TIMEOUT);
-                       //fprintf(stderr,"%i bytes read, wanted %i.\n", r, len-(0x40-12));
-                       return r+(0x40-12);
+
+               r=usb_bulk_read(sc->usb.dev, sc->usb.ep_bulk_in,
+                       (char*)buff, sc->usb.wMaxPacketSize_in, USB_TIMEOUT);
+               USB_ERROR(r);
+
+               if (r < sizeof(struct usbtmc_header)) {
+                       fprintf(stderr, "Short read!\n");
+                       return 0;
                }
-               return len;
+
+               bytes_read = r - sizeof(struct usbtmc_header);
+               
+               res = (struct usbtmc_header*)buff;
+               len = LE32(res->TransferSize);
+
+               memmove(buff, buff + sizeof(struct usbtmc_header), bytes_read);
+
+               buff = realloc(buff, len);
+               if (buff == NULL) {
+                       perror("realloc");
+                       exit(EXIT_FAILURE);
+               }
+
+               while ((len - bytes_read) > 0) {
+                       r=usb_bulk_read(sc->usb.dev, sc->usb.ep_bulk_in,
+                               (char*)buff + bytes_read, len - bytes_read,
+                               USB_TIMEOUT);
+                       USB_ERROR(r);
+
+                       bytes_read += r;
+               }
+
+               /* TODO: FIXME */
+               if (bytes_read > resplen) {
+                       fprintf(stderr, "Response buffer to small: %d instead of %d bytes!\n",
+                               resplen, bytes_read);
+                       bytes_read = resplen;
+               }
+
+               memcpy(resp, buff, bytes_read);
+               free(buff);
+
+               return bytes_read;
        }
        return 0;
 }
@@ -128,28 +200,19 @@ void usbtmc_release(struct scope *sc)
 struct scope* usbtmc_initscope(void) {
        int r;
        unsigned char buff[10];
-       usb_dev_handle *dev;
        struct scope *sc;
 
-       //Init libusb
+       /* Init libusb */
        usb_init();
-       //Locate and open the scope
-       dev = usbtmc_find_scope();
-       if (!dev) {
+       /* Locate and open the scope */
+       sc = usbtmc_find_scope();
+       if (!sc) {
                return NULL;
        }
 
-       sc = calloc(1, sizeof(struct scope));
-       if (sc == NULL) {
-               perror("calloc");
-               exit(EXIT_FAILURE);
-       }
-
-       sc->usb.dev = dev;
-
        usbtmc_claim(sc);
-       //The following code isn't really necessary, the program works
-       //OK without it too.
+       /The following code isn't really necessary, the program works
+          OK without it too. */
        r=usb_control_msg(sc->usb.dev, 0xC8, 9, 0, 0, (char*)buff, 4, USB_TIMEOUT);
        usbtmc_release(sc);
        if (r < 0) {
@@ -158,14 +221,14 @@ struct scope* usbtmc_initscope(void) {
                fprintf (stderr, "Do you have permission on the USB device?\n");
                exit (1);
        }
-       if (chars2int(buff)!=0x40005dc) {
-               fprintf(stderr,"Init: buff[%i]=%x\n",r,chars2int(buff));
+       if (LE32((*(uint32_t*)buff))!=0x40005dc) {
+               fprintf(stderr,"Init: buff[%i]=%x\n",r,LE32((*(uint32_t*)buff)));
        }
        return sc;
 }
 
 void usbtmc_close(struct scope *sc)
 {
-       //Free up and exit
+       /* Free up and exit */
        usb_close(sc->usb.dev);
 }
Impressum, Datenschutz