X-Git-Url: http://git.zerfleddert.de/cgi-bin/gitweb.cgi/rigol/blobdiff_plain/b131d433f9b984b50266298be6f0cf7f9a13b6fb..fc648ed83d8211054d4d1231260435858e0c7f11:/usbtmc.c diff --git a/usbtmc.c b/usbtmc.c index 054e49c..eb7737a 100644 --- a/usbtmc.c +++ b/usbtmc.c @@ -4,8 +4,19 @@ #include #include +#include "scope.h" #include "usbtmc.h" +#define USB_TIMEOUT 50000 + +#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 + //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; @@ -44,7 +55,7 @@ static usb_dev_handle *usbtmc_find_scope() { //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, +int usbtmc_sendscpi(struct scope *sc, char* cmd, unsigned char *resp, int resplen) { unsigned char *buff; int len,r,i; @@ -59,15 +70,15 @@ int usbtmc_sendscpi(usb_dev_handle *dev, char* cmd, buff[3]=0; int2chars(buff+4, cmdlen); buff[8]=1; - buff[9]=0x37; - buff[10]=0x39; - buff[11]=0x39; + buff[9]=0x00; + buff[10]=0x00; + buff[11]=0x00; //fprintf(stderr,"Writing header len=%d\n", cmdlen); //printb(buff,12); - r=usb_bulk_write(dev, 1, (char*)buff, 12, 1000); + r=usb_bulk_write(sc->usb.dev, 1, (char*)buff, 12, USB_TIMEOUT); //fprintf(stderr,"%i bytes written. Writing cmd\n",r); //printb(cmd, cmdlen); - r=usb_bulk_write(dev, 1, cmd, cmdlen, 1000); + r=usb_bulk_write(sc->usb.dev, 1, cmd, cmdlen, USB_TIMEOUT); //fprintf(stderr,"%i bytes written.\n",r); if (resp != NULL && resplen != 0) { //send read command @@ -75,15 +86,15 @@ int usbtmc_sendscpi(usb_dev_handle *dev, char* cmd, seq++; buff[1]=seq; buff[2]=~seq; //nseq int2chars(buff+4,0x40); - buff[8]=1; + buff[8]=0; 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); + r=usb_bulk_write(sc->usb.dev, 1, (char*)buff, 12, USB_TIMEOUT); //fprintf(stderr,"%i bytes written. Reading response hdr\n",r); - r=usb_bulk_read(dev, 2, (char*)buff, 0x40, 1000); + r=usb_bulk_read(sc->usb.dev, 2, (char*)buff, 0x40, USB_TIMEOUT); //printb(buff,r); len=chars2int(buff+4); //fprintf(stderr,"%i bytes read. Resplen=%i\n",r,len); @@ -94,7 +105,7 @@ int usbtmc_sendscpi(usb_dev_handle *dev, char* cmd, if (len > 0x40-12) { //fprintf(stderr," Reading response:\n"); if (resplenusb.dev, 2, (char*)resp+(0x40-12), len-(0x40-12), USB_TIMEOUT); //fprintf(stderr,"%i bytes read, wanted %i.\n", r, len-(0x40-12)); return r+(0x40-12); } @@ -103,39 +114,44 @@ int usbtmc_sendscpi(usb_dev_handle *dev, char* cmd, return 0; } -void usbtmc_claim(usb_dev_handle *sc) +void usbtmc_claim(struct scope *sc) { - usb_claim_interface(sc, 0); + usb_claim_interface(sc->usb.dev, 0); } -void usbtmc_release(usb_dev_handle *sc) +void usbtmc_release(struct scope *sc) { - //Disable keylock, so the user doesn't have to press the 'force'-button - usbtmc_sendscpi(sc, ":KEY:LOCK DISABLE",NULL,0); - usb_release_interface(sc, 0); + usb_release_interface(sc->usb.dev, 0); } //Initialize the scope. -usb_dev_handle* usbtmc_initscope(void) { +struct scope* usbtmc_initscope(void) { int r; unsigned char buff[10]; usb_dev_handle *dev; + struct scope *sc; //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"); + return NULL; + } + + sc = calloc(1, sizeof(struct scope)); + if (sc == NULL) { + perror("calloc"); + exit(EXIT_FAILURE); } - usbtmc_claim(dev); + + sc->usb.dev = dev; + + usbtmc_claim(sc); //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_release(dev); + r=usb_control_msg(sc->usb.dev, 0xC8, 9, 0, 0, (char*)buff, 4, USB_TIMEOUT); + usbtmc_release(sc); if (r < 0) { fprintf (stderr, "Error %d sending init message: %s\n", r, strerror (-r)); @@ -145,11 +161,11 @@ usb_dev_handle* usbtmc_initscope(void) { if (chars2int(buff)!=0x40005dc) { fprintf(stderr,"Init: buff[%i]=%x\n",r,chars2int(buff)); } - return dev; + return sc; } -void usbtmc_close(usb_dev_handle *sc) +void usbtmc_close(struct scope *sc) { //Free up and exit - usb_close(sc); + usb_close(sc->usb.dev); }