From f3ecdd6a895f788d37219206b6d5f782d43d8828 Mon Sep 17 00:00:00 2001 From: Michael Gernoth Date: Tue, 8 Jun 2010 11:22:58 +0200 Subject: [PATCH] use CLEAR instead of usb_reset to recover from errors --- usbtmc.c | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/usbtmc.c b/usbtmc.c index 2ccbf89..ffebcbf 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() { @@ -133,6 +133,43 @@ 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] != 0x01) { + printf("INITIATE_CLEAR failed: %s\n", 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) || status[0] != 0x01) { + printf("CHECK_CLEAR failed: %s\n", usb_strerror()); + return; + } + + if (status[1] & (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 -- 2.39.2