]> git.zerfleddert.de Git - rigol/blobdiff - usbtmc.c
export data through webinterface
[rigol] / usbtmc.c
index ffebcbff952b82dc2a4b358f6d8cf1e6166b9fee..d87d9ac7955c643ba261b6d6c5d51190daf50b60 100644 (file)
--- a/usbtmc.c
+++ b/usbtmc.c
@@ -7,7 +7,8 @@
 #include "scope.h"
 #include "usbtmc.h"
 
-#define USB_TIMEOUT 10000
+#define USB_TIMEOUT            10000
+#define USBTMC_IN_TRANSFERSIZE 0x800
 
 #if BYTE_ORDER == LITTLE_ENDIAN
 #define LE32(x) x
@@ -60,7 +61,7 @@ static struct scope* usbtmc_find_scope() {
        return NULL;
 }
 
-static unsigned char usbtmc_status(struct scope *sc)
+static unsigned char usb488_status(struct scope *sc)
 {
        int r;
        unsigned char status[3];
@@ -72,7 +73,7 @@ static unsigned char usbtmc_status(struct scope *sc)
                (sc->usb.bTag & 0x7f), 0, (char*)status, 3,
                USB_TIMEOUT);
 
-       if ((r != 3) || (status[0] != 0x01) || (status[1] != (sc->usb.bTag & 0x7f))) {
+       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;
        }
@@ -94,7 +95,7 @@ static struct usbtmc_capabilities* usbtmc_get_capabilities(struct scope *sc)
                return NULL;
        }
 
-       printf("USBTMC Version %x.%x Capabilities:\n", res.bcdUSBTMC[0], res.bcdUSBTMC[1]);
+       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");
 
@@ -107,7 +108,7 @@ static struct usbtmc_capabilities* usbtmc_get_capabilities(struct scope *sc)
        if (res.USBTMCDEVcapabilities & USBTMC_CAP_DEV_TERMCHAR_SUPP)
                printf("\tDevice supports Termchar\n");
 
-       printf("USB488 Version %x.%x Capabilities:\n", res.bcdUSB488[0], res.bcdUSB488[1]);
+       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");
@@ -133,6 +134,12 @@ static struct usbtmc_capabilities* usbtmc_get_capabilities(struct scope *sc)
        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;
@@ -144,8 +151,9 @@ static void usbtmc_clear(struct scope *sc)
                0, 0, (char*)status, 1,
                USB_TIMEOUT);
        
-       if ((r != 1) || status[0] != 0x01) {
-               printf("INITIATE_CLEAR failed: %s\n", usb_strerror());
+       if ((r != 1) || status[0] != USBTMC_STATUS_SUCCESS) {
+               printf("INITIATE_CLEAR failed (0x%x): %s\n", status[0], usb_strerror());
+               usbtmc_reset(sc);
                return;
        }
 
@@ -158,12 +166,17 @@ static void usbtmc_clear(struct scope *sc)
                        0, 0, (char*)status, 2,
                        USB_TIMEOUT);
 
-               if ((r != 2) || status[0] != 0x01) {
+               if (r != 2) {
                        printf("CHECK_CLEAR failed: %s\n", usb_strerror());
                        return;
                }
 
-               if (status[1] & (1<<0)) {
+               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;
                }
@@ -219,11 +232,12 @@ int usbtmc_sendscpi(struct scope *sc, char* cmd,
        free(req);
 
        if (resp != NULL && resplen != 0) {
-               unsigned char *buff;
+               unsigned char *buff = NULL;
+               unsigned char rxbuff[USBTMC_IN_TRANSFERSIZE];
                struct usbtmc_header *res;
                int bytes_read;
-
-               sc->usb.bTag++;
+               unsigned int read_size = USBTMC_IN_TRANSFERSIZE;
+               unsigned int headerlen;
 
                req = calloc(1, sizeof(struct usbtmc_header));
                if (req == NULL) {
@@ -231,56 +245,74 @@ int usbtmc_sendscpi(struct scope *sc, char* cmd,
                        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;
+               if (sc->usb.brokenRigol == 1) {
+                       read_size = sc->usb.wMaxPacketSize_in;
+               }
 
-               /* 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);
+               bytes_read = 0;
+               do {
+                       headerlen = 0;
 
-               free(req);
+                       if ((sc->usb.brokenRigol == 0) || (bytes_read == 0)) {
+                               sc->usb.bTag++;
 
-               buff=malloc(sc->usb.wMaxPacketSize_in);
-               if (buff == NULL) {
-                       perror("malloc");
-                       exit(EXIT_FAILURE);
-               }
+                               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;
 
-               r=usb_bulk_read(sc->usb.dev, sc->usb.ep_bulk_in,
-                       (char*)buff, sc->usb.wMaxPacketSize_in, USB_TIMEOUT);
-               USB_ERROR("USBTMC_DEV_DEP_MSG_IN1", r);
+                               /* 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);
 
-               if (r < sizeof(struct usbtmc_header)) {
-                       fprintf(stderr, "Short read!\n");
-                       return 0;
-               }
+                               headerlen = sizeof(struct usbtmc_header);
+                       }
 
-               bytes_read = r - sizeof(struct usbtmc_header);
-               
-               res = (struct usbtmc_header*)buff;
-               len = LE32(res->TransferSize);
+                       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);
 
-               memmove(buff, buff + sizeof(struct usbtmc_header), bytes_read);
+                       if (r < headerlen) {
+                               fprintf(stderr, "Short read!\n");
+                               return 0;
+                       }
 
-               buff = realloc(buff, len);
-               if (buff == NULL) {
-                       perror("realloc");
-                       exit(EXIT_FAILURE);
-               }
+                       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;
+                               }
 
-               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("USBTMC_DEV_DEP_MSG_INx", r);
+                               if (buff == NULL) {
+                                       len = LE32(res->TransferSize);
+                                       buff=malloc(len);
+                                       if (buff == NULL) {
+                                               perror("malloc");
+                                               exit(EXIT_FAILURE);
+                                       }
+                               }
+                       }
 
-                       bytes_read += r;
-               }
+                       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) {
@@ -323,7 +355,7 @@ struct scope* usbtmc_initscope(void) {
 
        usbtmc_claim(sc);
        sc->usb.cap = usbtmc_get_capabilities(sc);
-       printf("Device status: 0x%x\n", usbtmc_status(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);
Impressum, Datenschutz