X-Git-Url: https://git.zerfleddert.de/cgi-bin/gitweb.cgi/rigol/blobdiff_plain/90e416c4df00269ab86d349ff677d0a2c0b0bd40..07a45f0356f55c38f9d2bbbec36e742f43cdd3e1:/usbtmc.c diff --git a/usbtmc.c b/usbtmc.c index e1366ae..732b8f6 100644 --- a/usbtmc.c +++ b/usbtmc.c @@ -7,7 +7,7 @@ #include "scope.h" #include "usbtmc.h" -#define USB_TIMEOUT 50000 +#define USB_TIMEOUT 10000 #if BYTE_ORDER == LITTLE_ENDIAN #define LE32(x) x @@ -18,7 +18,7 @@ #endif /* TODO: fix memory leak here: */ -#define USB_ERROR(s, x) do { if (x < 0) { fprintf(stderr, "usb %s: %s\n", s, usb_strerror()); usb_reset(sc->usb.dev); return 0; } } while(0) +#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 a struct scope* for it */ static struct scope* usbtmc_find_scope() { @@ -60,7 +60,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 +72,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; } @@ -133,6 +133,48 @@ static struct usbtmc_capabilities* usbtmc_get_capabilities(struct scope *sc) return &res; } +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()); + 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 @@ -198,7 +240,7 @@ int usbtmc_sendscpi(struct scope *sc, char* cmd, req->bTag = sc->usb.bTag; req->bTagInverse = ~sc->usb.bTag; req->TransferSize = LE32(sc->usb.wMaxPacketSize_in); - req->bmTransferAttributes = USBTMC_TRANSFERATTRIB_EOM; + req->bmTransferAttributes = 0; req->TermChar = 0; /* send read command */ @@ -286,7 +328,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);