X-Git-Url: http://git.zerfleddert.de/cgi-bin/gitweb.cgi/rigol/blobdiff_plain/7b57506902b8a0b9ba2186eea5e99901e5afa952..a163d2fb2ffbcf4babb60d164b8f9dfe7bde61e0:/usbtmc.c?ds=sidebyside diff --git a/usbtmc.c b/usbtmc.c index 5b07ade..2ccbf89 100644 --- a/usbtmc.c +++ b/usbtmc.c @@ -17,7 +17,8 @@ #error BYTE_ORDER not defined/known! #endif -#define USB_ERROR(x) do { if (x < 0) { fprintf(stderr, "usb: %s\n", usb_strerror()); exit(EXIT_FAILURE); } } while(0) +/* 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) /* This routine locates a scope by VID/PID and returns a struct scope* for it */ static struct scope* usbtmc_find_scope() { @@ -47,8 +48,8 @@ static struct scope* usbtmc_find_scope() { /* TODO: FIXME */ sc->usb.brokenRigol = 1; - sc->usb.ep_bulk_out = 1; - sc->usb.ep_bulk_in = 2; + sc->usb.ep_bulk_out = 0x01; + sc->usb.ep_bulk_in = 0x82; sc->usb.wMaxPacketSize_in = 0x40; return sc; @@ -59,6 +60,79 @@ static struct scope* usbtmc_find_scope() { return NULL; } +static unsigned char usbtmc_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] != 0x01) || (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 %x.%x 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 %x.%x 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; +} + /* * 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 @@ -93,16 +167,16 @@ int usbtmc_sendscpi(struct scope *sc, char* cmd, r=usb_bulk_write(sc->usb.dev, sc->usb.ep_bulk_out, (char*)req, sizeof(struct usbtmc_header), USB_TIMEOUT); - USB_ERROR(r); + 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(r); + 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(r); + USB_ERROR("USBTMC_DEV_DEP_MSG_OUT", r); } free(req); @@ -130,7 +204,7 @@ int usbtmc_sendscpi(struct scope *sc, char* cmd, /* 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); + USB_ERROR("USBTMC_REQUEST_DEV_DEP_MSG_IN", r); free(req); @@ -142,7 +216,7 @@ int usbtmc_sendscpi(struct scope *sc, char* cmd, r=usb_bulk_read(sc->usb.dev, sc->usb.ep_bulk_in, (char*)buff, sc->usb.wMaxPacketSize_in, USB_TIMEOUT); - USB_ERROR(r); + USB_ERROR("USBTMC_DEV_DEP_MSG_IN1", r); if (r < sizeof(struct usbtmc_header)) { fprintf(stderr, "Short read!\n"); @@ -166,7 +240,7 @@ int usbtmc_sendscpi(struct scope *sc, char* cmd, r=usb_bulk_read(sc->usb.dev, sc->usb.ep_bulk_in, (char*)buff + bytes_read, len - bytes_read, USB_TIMEOUT); - USB_ERROR(r); + USB_ERROR("USBTMC_DEV_DEP_MSG_INx", r); bytes_read += r; } @@ -196,10 +270,10 @@ void usbtmc_release(struct scope *sc) usb_release_interface(sc->usb.dev, 0); } -//Initialize the scope. +/* Initialize the scope. */ struct scope* usbtmc_initscope(void) { int r; - unsigned char buff[10]; + uint32_t vidpid; struct scope *sc; /* Init libusb */ @@ -211,9 +285,11 @@ struct scope* usbtmc_initscope(void) { } usbtmc_claim(sc); + sc->usb.cap = usbtmc_get_capabilities(sc); + printf("Device status: 0x%x\n", usbtmc_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*)buff, 4, USB_TIMEOUT); + 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", @@ -221,8 +297,8 @@ struct scope* usbtmc_initscope(void) { fprintf (stderr, "Do you have permission on the USB device?\n"); exit (1); } - if (LE32((*(uint32_t*)buff))!=0x40005dc) { - fprintf(stderr,"Init: buff[%i]=%x\n",r,LE32((*(uint32_t*)buff))); + if (LE32(vidpid)!=0x40005dc) { + fprintf(stderr,"Init: buff[%i]=%x\n",r,LE32(vidpid)); } return sc; }