]> git.zerfleddert.de Git - rigol/commitdiff
split out USB functions to separate file
authorMichael Gernoth <michael@gernoth.net>
Sun, 6 Jun 2010 11:56:23 +0000 (13:56 +0200)
committerMichael Gernoth <michael@gernoth.net>
Sun, 6 Jun 2010 11:56:23 +0000 (13:56 +0200)
.gitignore
Makefile
rigol.c
usbtmc.c [new file with mode: 0644]
usbtmc.h [new file with mode: 0644]

index 7dae92ce3a6209a81413154d1003be34fe6c5bed..1a30e02b9ea19c2f6cd9ce7200f54a9f5ed0e10a 100644 (file)
@@ -2,4 +2,5 @@ raw2gp
 rigol
 rigol.o
 png.o
+usbtmc.o
 screen_*.png
index 2c14bad9deaa48a579eb253639b4312c853c9467..e5415d4357e1fa242a0d7bd6c881f1719fb932db 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
 
 CFILES=rigol.c
-OFILES=rigol.o png.o
+OFILES=rigol.o png.o usbtmc.o
 CFLAGS=-O2 -Wall
 LDFLAGS=-lusb -lreadline -lz
 CC=gcc
diff --git a/rigol.c b/rigol.c
index 0e9ff9e9e5bbe226322a220eba4f904344dccff7..ebf01fb5986cc80beeb500b4244adfb0cab0b533 100644 (file)
--- a/rigol.c
+++ b/rigol.c
@@ -23,44 +23,10 @@ rmmod uhci_hcd; modprobe uhci_hcd
 #include <readline/readline.h>
 #include <readline/history.h>
 
+#include "usbtmc.h"
 #include "png.h"
 
 
-//This routine locates a scope by VID/PID and returns an opened handle to it.
-usb_dev_handle *find_scope() {
-       struct usb_bus *bus;
-       struct usb_device *dev=NULL;
-       usb_find_busses();
-       usb_find_devices();
-       for (bus=usb_busses; bus; bus=bus->next) {
-               for (dev=bus->devices; dev; dev=dev->next) {
-                       //fprintf(stderr,"Prod/dev: %04X:%04X\n",dev->descriptor.idVendor,dev->descriptor.idProduct);
-                       if (dev->descriptor.idVendor==0x400 && dev->descriptor.idProduct==0x5dc) {
-                               return usb_open(dev);
-                       }
-               }
-       }
-       return NULL;
-}
-
-//Helper-routine: Convert a little-endian 4-byte word to an int
-void int2chars(unsigned char *buff,unsigned int a) {
-       buff[3]=(a>>24)&0xff;
-       buff[2]=(a>>16)&0xff;
-       buff[1]=(a>>8)&0xff;
-       buff[0]=(a)&0xff;
-}
-
-//Helper-routine: Convert an int to  little-endian 4-byte word
-unsigned int chars2int(unsigned char *buff) {
-       unsigned int a;
-       a=buff[3]<<24;
-       a+=buff[2]<<16;
-       a+=buff[1]<<8;
-       a+=buff[0];
-       return a;
-}
-
 #define MIN(a,b) (((a)<(b))?(a):(b))
 
 inline char printable (char ch)
@@ -92,88 +58,6 @@ void printb (unsigned char *pkt, int len)
        }
 }
 
-//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 sendscpi(usb_dev_handle *dev, char* cmd, 
-               unsigned char *resp, int resplen) {
-       unsigned char *buff;
-       int len,r,i;
-       int cmdlen = strlen(cmd);
-       static unsigned char seq=0;
-
-
-       buff=malloc(0x40);
-       seq++;
-       buff[0]=1; //func
-       buff[1]=seq; buff[2]=~seq; //nseq
-       buff[3]=0;
-       int2chars(buff+4, cmdlen);
-       buff[8]=1;
-       buff[9]=0x37;
-       buff[10]=0x39;
-       buff[11]=0x39;
-       //fprintf(stderr,"Writing header len=%d\n", cmdlen);
-       //printb(buff,12);
-       r=usb_bulk_write(dev, 1, (char*)buff, 12, 1000);
-       //fprintf(stderr,"%i bytes written. Writing cmd\n",r);
-       //printb(cmd, cmdlen);
-       r=usb_bulk_write(dev, 1, cmd, cmdlen, 1000);
-       //fprintf(stderr,"%i bytes written.\n",r);
-       if (resp != NULL && resplen != 0) {
-               //send read command
-               buff[0]=2; //func
-               seq++;
-               buff[1]=seq; buff[2]=~seq; //nseq
-               int2chars(buff+4,0x40);
-               buff[8]=1;
-               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);
-               //fprintf(stderr,"%i bytes written. Reading response hdr\n",r);
-               r=usb_bulk_read(dev, 2, (char*)buff, 0x40, 1000);
-               //printb(buff,r);
-               len=chars2int(buff+4);
-               //fprintf(stderr,"%i bytes read. Resplen=%i\n",r,len);
-               for (i=0; i<(r-12); i++) {
-                       if (i<resplen) resp[i] = buff[i+12];
-               }
-               //printb(resp,r-12);
-               if (len > 0x40-12) {
-                       //fprintf(stderr," Reading response:\n");
-                       if (resplen<len) len=resplen;
-                       r=usb_bulk_read(dev, 2, (char*)resp+(0x40-12), len-(0x40-12),1000);
-                       //fprintf(stderr,"%i bytes read, wanted %i.\n", r, len-(0x40-12));
-                       return r+(0x40-12);
-               }
-               return len;
-       }
-       return 0;
-}
-
-//Initialize the scope.
-void initscope(usb_dev_handle *dev) {
-       int r;
-       unsigned char buff[10];
-       usb_claim_interface(dev,0);
-       //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);
-       if (r < 0) {
-               fprintf (stderr, "Error %d sending init message: %s\n", 
-                               r, strerror (-r));
-               fprintf (stderr, "Do you have permission on the USB device?\n");
-               exit (1);
-       }
-       if (chars2int(buff)!=0x40004dc) {
-               fprintf(stderr,"Init: buff[%i]=%x\n",r,chars2int(buff));
-       }
-       return;
-}
-
 #define HAVE_READLINE
 #ifndef HAVE_READLINE
 
@@ -210,13 +94,13 @@ void do_plot (struct usb_dev_handle *sc)
        static FILE *gnuplot=NULL;
        FILE *fp;
 
-       l = sendscpi(sc, ":WAV:DATA? CHANEL1", ch1, 1024);
+       l = usbtmc_sendscpi(sc, ":WAV:DATA? CHANEL1", ch1, 1024);
 
        if (l != 1024) {
                printf ("hmm. didnt' get 1024 bytes. \n"); 
        }
 
-       l = sendscpi(sc, ":WAV:DATA? CHANNEL2", ch2, 1024);
+       l = usbtmc_sendscpi(sc, ":WAV:DATA? CHANNEL2", ch2, 1024);
 
        if (l != 1024) {
                printf ("hmm. didnt' get 1024 bytes. \n"); 
@@ -245,7 +129,7 @@ double get_float_from_scope (struct usb_dev_handle *sc, char *var)
        double temp;
        int l;
 
-       l = sendscpi(sc, var, buf, 1024);
+       l = usbtmc_sendscpi(sc, var, buf, 1024);
        if (l > 0) {
                sscanf ((char*)buf, "%lf", &temp); 
                return temp;
@@ -263,7 +147,7 @@ void do_get_buf (struct usb_dev_handle *sc)
        unsigned char data[512*1024];
        double sampfreq;
 
-       sendscpi (sc, ":STOP", NULL, 0); 
+       usbtmc_sendscpi (sc, ":STOP", NULL, 0); 
 
        sampfreq = get_float_from_scope (sc, ":ACQ:SAMP?");
 
@@ -271,7 +155,7 @@ void do_get_buf (struct usb_dev_handle *sc)
 
        sprintf (buf, ":TIM:SCAL %.15f", 50 / sampfreq);
        printf ("sending scale cmd: %s\n", buf); 
-       sendscpi (sc, buf, NULL, 0);
+       usbtmc_sendscpi (sc, buf, NULL, 0);
 
        sleep (1);
 
@@ -279,9 +163,9 @@ void do_get_buf (struct usb_dev_handle *sc)
        for (i=-254*1024;i< 254*1024;i += 600) {
                sprintf (buf, ":TIM:OFFSET %.15f", i / sampfreq);
                printf ("Sending offset cmd:  %s\n", buf); 
-               sendscpi (sc, buf, NULL, 0);
+               usbtmc_sendscpi (sc, buf, NULL, 0);
 
-               l = sendscpi(sc, ":WAV:DATA? CHANEL1", ch1, 1024);
+               l = usbtmc_sendscpi(sc, ":WAV:DATA? CHANEL1", ch1, 1024);
 
                if (l != 1024) {
                        printf ("hmm. didnt' get 1024 bytes. \n"); 
@@ -296,7 +180,7 @@ void do_get_buf (struct usb_dev_handle *sc)
        fwrite (data, bp, 1, fp);
        fclose (fp); 
 
-       sendscpi (sc, ":TIM:OFFSET 0", NULL, 0);
+       usbtmc_sendscpi (sc, ":TIM:OFFSET 0", NULL, 0);
 }
 
 void do_get_screen(struct usb_dev_handle *sc)
@@ -312,10 +196,10 @@ void do_get_screen(struct usb_dev_handle *sc)
        pid_t display;
 
        /* Hide "RMT" from screen */
-       l = sendscpi(sc, ":KEY:LOCK DISABLE", NULL, 0); 
+       l = usbtmc_sendscpi(sc, ":KEY:LOCK DISABLE", NULL, 0); 
        usleep(20000);
 
-       l = sendscpi(sc, ":LCD:DATA?", screen, sizeof(screen));
+       l = usbtmc_sendscpi(sc, ":LCD:DATA?", screen, sizeof(screen));
 
        if (l != sizeof(screen)) {
                printf ("hmm. didnt' get %d bytes, but %d\n\n", sizeof(screen), l); 
@@ -376,18 +260,8 @@ int main(int argc, char **argv)
        int l;
        struct sigaction act;
 
-       //Init libusb
-       usb_init();
-       //Locate and open the scope
-       sc=find_scope();
-       if (!sc) {
-               printf("No scope found.\n");
-               exit(1);
-       } else {
-               printf("Scope found.\n");
-       }
        //Initialize scope
-       initscope(sc);
+       sc = usbtmc_initscope();
        buff = malloc (1024*1024); 
        if (buff == NULL) {
                perror("malloc");
@@ -432,18 +306,18 @@ int main(int argc, char **argv)
                //printb (scpi, l+2); 
                if (strchr (scpi, '?')) {
                        //printf ("Expecting reply\n");
-                       l = sendscpi(sc, scpi, buff, 1024*1024);
+                       l = usbtmc_sendscpi(sc, scpi, buff, 1024*1024);
                        //printf ("Got replylen = %d.\n", l); 
                        buff[l] = 0; //zero-terminate
                        printb (buff, l);
                } else {
                        //printf ("No reply expected\n");
-                       l=sendscpi(sc,scpi,NULL,0);
+                       l=usbtmc_sendscpi(sc,scpi,NULL,0);
                }
                free (scpi);
        }
        //Disable keylock, so the user doesn't have to press the 'force'-button
-       l=sendscpi(sc, ":KEY:LOCK DISABLE",NULL,0); 
+       l=usbtmc_sendscpi(sc, ":KEY:LOCK DISABLE",NULL,0); 
 
        //Free up and exit
        usb_release_interface(sc,0);
diff --git a/usbtmc.c b/usbtmc.c
new file mode 100644 (file)
index 0000000..483014f
--- /dev/null
+++ b/usbtmc.c
@@ -0,0 +1,136 @@
+#include <string.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <usb.h>
+#include <arpa/inet.h>
+
+#include "usbtmc.h"
+
+//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;
+       buff[2]=(a>>16)&0xff;
+       buff[1]=(a>>8)&0xff;
+       buff[0]=(a)&0xff;
+}
+
+//Helper-routine: Convert an int to  little-endian 4-byte word
+unsigned int chars2int(unsigned char *buff) {
+       unsigned int a;
+       a=buff[3]<<24;
+       a+=buff[2]<<16;
+       a+=buff[1]<<8;
+       a+=buff[0];
+       return a;
+}
+
+//This routine locates a scope by VID/PID and returns an opened handle to it.
+static usb_dev_handle *usbtmc_find_scope() {
+       struct usb_bus *bus;
+       struct usb_device *dev=NULL;
+       usb_find_busses();
+       usb_find_devices();
+       for (bus=usb_busses; bus; bus=bus->next) {
+               for (dev=bus->devices; dev; dev=dev->next) {
+                       //fprintf(stderr,"Prod/dev: %04X:%04X\n",dev->descriptor.idVendor,dev->descriptor.idProduct);
+                       if (dev->descriptor.idVendor==0x400 && dev->descriptor.idProduct==0x5dc) {
+                               return usb_open(dev);
+                       }
+               }
+       }
+       return NULL;
+}
+
+//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, 
+               unsigned char *resp, int resplen) {
+       unsigned char *buff;
+       int len,r,i;
+       int cmdlen = strlen(cmd);
+       static unsigned char seq=0;
+
+
+       buff=malloc(0x40);
+       seq++;
+       buff[0]=1; //func
+       buff[1]=seq; buff[2]=~seq; //nseq
+       buff[3]=0;
+       int2chars(buff+4, cmdlen);
+       buff[8]=1;
+       buff[9]=0x37;
+       buff[10]=0x39;
+       buff[11]=0x39;
+       //fprintf(stderr,"Writing header len=%d\n", cmdlen);
+       //printb(buff,12);
+       r=usb_bulk_write(dev, 1, (char*)buff, 12, 1000);
+       //fprintf(stderr,"%i bytes written. Writing cmd\n",r);
+       //printb(cmd, cmdlen);
+       r=usb_bulk_write(dev, 1, cmd, cmdlen, 1000);
+       //fprintf(stderr,"%i bytes written.\n",r);
+       if (resp != NULL && resplen != 0) {
+               //send read command
+               buff[0]=2; //func
+               seq++;
+               buff[1]=seq; buff[2]=~seq; //nseq
+               int2chars(buff+4,0x40);
+               buff[8]=1;
+               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);
+               //fprintf(stderr,"%i bytes written. Reading response hdr\n",r);
+               r=usb_bulk_read(dev, 2, (char*)buff, 0x40, 1000);
+               //printb(buff,r);
+               len=chars2int(buff+4);
+               //fprintf(stderr,"%i bytes read. Resplen=%i\n",r,len);
+               for (i=0; i<(r-12); i++) {
+                       if (i<resplen) resp[i] = buff[i+12];
+               }
+               //printb(resp,r-12);
+               if (len > 0x40-12) {
+                       //fprintf(stderr," Reading response:\n");
+                       if (resplen<len) len=resplen;
+                       r=usb_bulk_read(dev, 2, (char*)resp+(0x40-12), len-(0x40-12),1000);
+                       //fprintf(stderr,"%i bytes read, wanted %i.\n", r, len-(0x40-12));
+                       return r+(0x40-12);
+               }
+               return len;
+       }
+       return 0;
+}
+
+//Initialize the scope.
+usb_dev_handle* usbtmc_initscope(void) {
+       int r;
+       unsigned char buff[10];
+       usb_dev_handle *dev;
+
+       //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");
+       }
+       usb_claim_interface(dev,0);
+       //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);
+       if (r < 0) {
+               fprintf (stderr, "Error %d sending init message: %s\n", 
+                               r, strerror (-r));
+               fprintf (stderr, "Do you have permission on the USB device?\n");
+               exit (1);
+       }
+       if (chars2int(buff)!=0x40004dc) {
+               fprintf(stderr,"Init: buff[%i]=%x\n",r,chars2int(buff));
+       }
+       return dev;
+}
diff --git a/usbtmc.h b/usbtmc.h
new file mode 100644 (file)
index 0000000..358cad3
--- /dev/null
+++ b/usbtmc.h
@@ -0,0 +1,2 @@
+int usbtmc_sendscpi(usb_dev_handle *dev, char* cmd, unsigned char *resp, int resplen);
+usb_dev_handle* usbtmc_initscope(void);
Impressum, Datenschutz