]> cvs.zerfleddert.de Git - rigol/blobdiff - usbtmc.c
export data through webinterface
[rigol] / usbtmc.c
index 483014f58e51fa7de953e717323204bd791772cb..d87d9ac7955c643ba261b6d6c5d51190daf50b60 100644 (file)
--- a/usbtmc.c
+++ b/usbtmc.c
 #include <usb.h>
 #include <arpa/inet.h>
 
+#include "scope.h"
 #include "usbtmc.h"
 
-//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;
-}
+#define USB_TIMEOUT            10000
+#define USBTMC_IN_TRANSFERSIZE 0x800
 
-//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;
-}
+#if BYTE_ORDER == LITTLE_ENDIAN
+#define LE32(x) x
+#elif BYTE_ORDER == BIG_ENDIAN
+#define LE32(x) ((uint32_t)((((uint32_t)x)>>24) | ((((uint32_t)x)>>8) & 0xff00) | ((((uint32_t)x)<<8) & 0xff0000) | (((uint32_t)x)<<24)))
+#else
+#error BYTE_ORDER not defined/known!
+#endif
+
+/* TODO: fix memory leak here: */
+#define USB_ERROR(s, x) do { if (x < 0) { fprintf(stderr, "usb %s: %s\n", s, usb_strerror()); usbtmc_clear(sc); return 0; } } 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 = 0x01;
+                               sc->usb.ep_bulk_in = 0x82;
+                               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.
-int usbtmc_sendscpi(usb_dev_handle *dev, char* cmd, 
+static unsigned char usb488_status(struct scope *sc)
+{
+       int r;
+       unsigned char status[3];
+
+       sc->usb.bTag++;
+
+       r = usb_control_msg(sc->usb.dev, 0xA1,
+               USB488_CTL_READ_STATUS_BYTE,
+               (sc->usb.bTag & 0x7f), 0, (char*)status, 3,
+               USB_TIMEOUT);
+
+       if ((r != 3) || (status[0] != USBTMC_STATUS_SUCCESS) || (status[1] != (sc->usb.bTag & 0x7f))) {
+               printf("READ_STATUS_BYTE failed: %d 0x%x 0x%x 0x%x\n", r, status[0], status[1], status[2]);
+               return 0xff;
+       }
+
+       return status[2];
+}
+
+static struct usbtmc_capabilities* usbtmc_get_capabilities(struct scope *sc)
+{
+       int r;
+       static struct usbtmc_capabilities res;
+
+       r = usb_control_msg(sc->usb.dev, 0xA1, 
+               USBTMC_CTL_GET_CAPABILITIES,
+               0, 0, (char*)&res, sizeof(struct usbtmc_capabilities),
+               USB_TIMEOUT);
+       if (r != sizeof(struct usbtmc_capabilities)) {
+               printf("GET_CAPABILITIES failed: %s\n", usb_strerror());
+               return NULL;
+       }
+
+       printf("USBTMC Version %02x.%02x Capabilities:\n", res.bcdUSBTMC[0], res.bcdUSBTMC[1]);
+       if (res.USBTMCIFcapabilities & USBTMC_CAP_IF_INDICATOR_PULSE)
+               printf("\tInterface supports indicator pulse\n");
+
+       if (res.USBTMCIFcapabilities & USBTMC_CAP_IF_TALKONLY)
+               printf("\tInterface is talk only\n");
+
+       if (res.USBTMCIFcapabilities & USBTMC_CAP_IF_LISTENONLY)
+               printf("\tInterface is listen only\n");
+
+       if (res.USBTMCDEVcapabilities & USBTMC_CAP_DEV_TERMCHAR_SUPP)
+               printf("\tDevice supports Termchar\n");
+
+       printf("USB488 Version %02x.%02x Capabilities:\n", res.bcdUSB488[0], res.bcdUSB488[1]);
+
+       if (res.USB488IFcapabilities & USB488_CAP_IF_4882)
+               printf("\tInterface is 488.2 compliant\n");
+
+       if (res.USB488IFcapabilities & USB488_CAP_IF_LOCKOUT)
+               printf("\tInterface supports local lockout\n");
+
+       if (res.USB488IFcapabilities & USB488_CAP_IF_TRIGGER)
+               printf("\tInterface supports TRIGGER\n");
+
+       if (res.USB488DEVcapabilities & USB488_CAP_DEV_SCPI)
+               printf("\tDevice is SCPI compliant\n");
+
+       if (res.USB488DEVcapabilities & USB488_CAP_DEV_SR1)
+               printf("\tDevice is SR1 capable\n");
+
+       if (res.USB488DEVcapabilities & USB488_CAP_DEV_RL1)
+               printf("\tDevice is RL1 capable\n");
+
+       if (res.USB488DEVcapabilities & USB488_CAP_DEV_DT1)
+               printf("\tDevice is DT1 capable\n");
+
+       return &res;
+}
+
+void usbtmc_reset(struct scope *sc)
+{
+       usb_reset(sc->usb.dev);
+       usbtmc_claim(sc);
+}
+
+static void usbtmc_clear(struct scope *sc)
+{
+       int r;
+       unsigned char status[2];
+
+       printf("Initiating clear...\n");
+       r = usb_control_msg(sc->usb.dev, 0xA1,
+               USBTMC_CTL_INITIATE_CLEAR,
+               0, 0, (char*)status, 1,
+               USB_TIMEOUT);
+       
+       if ((r != 1) || status[0] != USBTMC_STATUS_SUCCESS) {
+               printf("INITIATE_CLEAR failed (0x%x): %s\n", status[0], usb_strerror());
+               usbtmc_reset(sc);
+               return;
+       }
+
+       while(1) {
+               usleep(100000);
+               printf("Waiting for clear to complete...\n");
+
+               r = usb_control_msg(sc->usb.dev, 0xA1,
+                       USBTMC_CTL_CHECK_CLEAR_STAT,
+                       0, 0, (char*)status, 2,
+                       USB_TIMEOUT);
+
+               if (r != 2) {
+                       printf("CHECK_CLEAR failed: %s\n", usb_strerror());
+                       return;
+               }
+
+               if (USBTMC_STATUS_FAIL(status[0])) {
+                       printf("CHECK_CLEAR failed: 0x%x\n", status[0]);
+                       return;
+               }
+
+               if ((status[0] == USBTMC_STATUS_SUCCESS) && (status[1] == 0)) {
+                       printf("Success!\n");
+                       break;
+               }
+       }
+}
+
+/*
+ * 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]=0x37;
-       buff[10]=0x39;
-       buff[11]=0x39;
-       //fprintf(stderr,"Writing header len=%d\n", cmdlen);
-       //printb(buff,12);
-       r=usb_bulk_write(dev, 1, (char*)buff, 12, 1000);
-       //fprintf(stderr,"%i bytes written. Writing cmd\n",r);
-       //printb(cmd, cmdlen);
-       r=usb_bulk_write(dev, 1, cmd, cmdlen, 1000);
-       //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("USBTMC_DEV_DEP_MSG_OUT1", 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("USBTMC_DEV_DEP_MSG_OUT2", r);
+       } else {
+               r=usb_bulk_write(sc->usb.dev, sc->usb.ep_bulk_out,
+                       (char*)req, len, USB_TIMEOUT);
+               USB_ERROR("USBTMC_DEV_DEP_MSG_OUT", 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]=1;
-               buff[9]=0xA;
-               buff[10]=0;
-               buff[11]=0;
-               //fprintf(stderr,"Writing resp req header\n");
-               //printb(buff,12);
-               r=usb_bulk_write(dev, 1, (char*)buff, 12, 1000);
-               //fprintf(stderr,"%i bytes written. Reading response hdr\n",r);
-               r=usb_bulk_read(dev, 2, (char*)buff, 0x40, 1000);
-               //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 = NULL;
+               unsigned char rxbuff[USBTMC_IN_TRANSFERSIZE];
+               struct usbtmc_header *res;
+               int bytes_read;
+               unsigned int read_size = USBTMC_IN_TRANSFERSIZE;
+               unsigned int headerlen;
+
+               req = calloc(1, sizeof(struct usbtmc_header));
+               if (req == NULL) {
+                       perror("calloc");
+                       exit(EXIT_FAILURE);
+               }
+
+               if (sc->usb.brokenRigol == 1) {
+                       read_size = sc->usb.wMaxPacketSize_in;
                }
-               //printb(resp,r-12);
-               if (len > 0x40-12) {
-                       //fprintf(stderr," Reading response:\n");
-                       if (resplen<len) len=resplen;
-                       r=usb_bulk_read(dev, 2, (char*)resp+(0x40-12), len-(0x40-12),1000);
-                       //fprintf(stderr,"%i bytes read, wanted %i.\n", r, len-(0x40-12));
-                       return r+(0x40-12);
+
+               bytes_read = 0;
+               do {
+                       headerlen = 0;
+
+                       if ((sc->usb.brokenRigol == 0) || (bytes_read == 0)) {
+                               sc->usb.bTag++;
+
+                               req->MsgID = USBTMC_REQUEST_DEV_DEP_MSG_IN;
+                               req->bTag = sc->usb.bTag;
+                               req->bTagInverse = ~sc->usb.bTag;
+                               req->TransferSize = LE32(USBTMC_IN_TRANSFERSIZE);
+                               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("USBTMC_REQUEST_DEV_DEP_MSG_IN", r);
+
+                               headerlen = sizeof(struct usbtmc_header);
+                       }
+
+                       r=usb_bulk_read(sc->usb.dev, sc->usb.ep_bulk_in,
+                                       (char*)rxbuff, read_size, USB_TIMEOUT);
+                       USB_ERROR("USBTMC_DEV_DEP_MSG_IN", r);
+
+                       if (r < headerlen) {
+                               fprintf(stderr, "Short read!\n");
+                               return 0;
+                       }
+
+                       if (headerlen > 0) {
+                               res = (struct usbtmc_header*)rxbuff;
+
+                               if ((res->bTag != sc->usb.bTag) ||
+                                               (res->bTagInverse != (unsigned char)(~sc->usb.bTag))) {
+                                       fprintf(stderr, "Wrong TAG received! We: 0x%02x, Scope: 0x%02x\n", sc->usb.bTag, res->bTag);
+                                       if (sc->usb.brokenRigol == 1) {
+                                               fprintf(stderr, "Tying to restart transfer...\n");
+                                               bytes_read = 0;
+                                               continue;
+                                       }
+                                       return 0;
+                               }
+
+                               if (buff == NULL) {
+                                       len = LE32(res->TransferSize);
+                                       buff=malloc(len);
+                                       if (buff == NULL) {
+                                               perror("malloc");
+                                               exit(EXIT_FAILURE);
+                                       }
+                               }
+                       }
+
+                       if ((r - headerlen) > 0) {
+                               memcpy(buff + bytes_read, rxbuff + headerlen, r - headerlen);
+                               bytes_read += r - headerlen;
+                       }
+
+                       read_size = USBTMC_IN_TRANSFERSIZE;
+               } while(bytes_read < len);
+
+               free(req);
+
+               /* TODO: FIXME */
+               if (bytes_read > resplen) {
+                       fprintf(stderr, "Response buffer to small: %d instead of %d bytes!\n",
+                               resplen, bytes_read);
+                       bytes_read = resplen;
                }
-               return len;
+
+               memcpy(resp, buff, bytes_read);
+               free(buff);
+
+               return bytes_read;
        }
        return 0;
 }
 
-//Initialize the scope.
-usb_dev_handle* usbtmc_initscope(void) {
+void usbtmc_claim(struct scope *sc)
+{
+       usb_claim_interface(sc->usb.dev, 0);
+}
+
+void usbtmc_release(struct scope *sc)
+{
+       usb_release_interface(sc->usb.dev, 0);
+}
+
+/* Initialize the scope. */
+struct scope* usbtmc_initscope(void) {
        int r;
-       unsigned char buff[10];
-       usb_dev_handle *dev;
+       uint32_t vidpid;
+       struct scope *sc;
 
-       //Init libusb
+       /* Init libusb */
        usb_init();
-       //Locate and open the scope
-       dev = usbtmc_find_scope();
-       if (!dev) {
-               printf("No scope found.\n");
-               exit(1);
-       } else {
-               printf("Scope found.\n");
+       /* Locate and open the scope */
+       sc = usbtmc_find_scope();
+       if (!sc) {
+               return NULL;
        }
-       usb_claim_interface(dev,0);
-       //The following code isn't really necessary, the program works
-       //OK without it too.
-       r=usb_control_msg(dev, 0xC8, 9, 0, 0, (char*)buff, 4, 1000);
+
+       usbtmc_claim(sc);
+       sc->usb.cap = usbtmc_get_capabilities(sc);
+       printf("Device status: 0x%x\n", usb488_status(sc));
+       /* 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*)&vidpid, 4, USB_TIMEOUT);
+       usbtmc_release(sc);
        if (r < 0) {
                fprintf (stderr, "Error %d sending init message: %s\n", 
                                r, strerror (-r));
                fprintf (stderr, "Do you have permission on the USB device?\n");
                exit (1);
        }
-       if (chars2int(buff)!=0x40004dc) {
-               fprintf(stderr,"Init: buff[%i]=%x\n",r,chars2int(buff));
+       if (LE32(vidpid)!=0x40005dc) {
+               fprintf(stderr,"Init: buff[%i]=%x\n",r,LE32(vidpid));
        }
-       return dev;
+       return sc;
+}
+
+void usbtmc_close(struct scope *sc)
+{
+       /* Free up and exit */
+       usb_close(sc->usb.dev);
 }
Impressum, Datenschutz