]> git.zerfleddert.de Git - rigol/blob - usbtmc.c
eb7737ad287efda71b1f49008d1835da374c6595
[rigol] / usbtmc.c
1 #include <string.h>
2 #include <stdio.h>
3 #include <stdint.h>
4 #include <usb.h>
5 #include <arpa/inet.h>
6
7 #include "scope.h"
8 #include "usbtmc.h"
9
10 #define USB_TIMEOUT 50000
11
12 #if BYTE_ORDER == LITTLE_ENDIAN
13 #define LE32(x) x
14 #elif BYTE_ORDER == BIG_ENDIAN
15 #define LE32(x) ((uint32_t)((((uint32_t)x)>>24) | ((((uint32_t)x)>>8) & 0xff00) | ((((uint32_t)x)<<8) & 0xff0000) | (((uint32_t)x)<<24)))
16 #else
17 #error BYTE_ORDER not defined/known!
18 #endif
19
20 //Helper-routine: Convert a little-endian 4-byte word to an int
21 static void int2chars(unsigned char *buff,unsigned int a) {
22 buff[3]=(a>>24)&0xff;
23 buff[2]=(a>>16)&0xff;
24 buff[1]=(a>>8)&0xff;
25 buff[0]=(a)&0xff;
26 }
27
28 //Helper-routine: Convert an int to little-endian 4-byte word
29 unsigned int chars2int(unsigned char *buff) {
30 unsigned int a;
31 a=buff[3]<<24;
32 a+=buff[2]<<16;
33 a+=buff[1]<<8;
34 a+=buff[0];
35 return a;
36 }
37
38 //This routine locates a scope by VID/PID and returns an opened handle to it.
39 static usb_dev_handle *usbtmc_find_scope() {
40 struct usb_bus *bus;
41 struct usb_device *dev=NULL;
42 usb_find_busses();
43 usb_find_devices();
44 for (bus=usb_busses; bus; bus=bus->next) {
45 for (dev=bus->devices; dev; dev=dev->next) {
46 //fprintf(stderr,"Prod/dev: %04X:%04X\n",dev->descriptor.idVendor,dev->descriptor.idProduct);
47 if (dev->descriptor.idVendor==0x400 && dev->descriptor.idProduct==0x5dc) {
48 return usb_open(dev);
49 }
50 }
51 }
52 return NULL;
53 }
54
55 //Send a scpi-command to the scope. The response goes into the buffer
56 //called resp, with a size of resplen. If resp==NULL, no response
57 //is requested.
58 int usbtmc_sendscpi(struct scope *sc, char* cmd,
59 unsigned char *resp, int resplen) {
60 unsigned char *buff;
61 int len,r,i;
62 int cmdlen = strlen(cmd);
63 static unsigned char seq=0;
64
65
66 buff=malloc(0x40);
67 seq++;
68 buff[0]=1; //func
69 buff[1]=seq; buff[2]=~seq; //nseq
70 buff[3]=0;
71 int2chars(buff+4, cmdlen);
72 buff[8]=1;
73 buff[9]=0x00;
74 buff[10]=0x00;
75 buff[11]=0x00;
76 //fprintf(stderr,"Writing header len=%d\n", cmdlen);
77 //printb(buff,12);
78 r=usb_bulk_write(sc->usb.dev, 1, (char*)buff, 12, USB_TIMEOUT);
79 //fprintf(stderr,"%i bytes written. Writing cmd\n",r);
80 //printb(cmd, cmdlen);
81 r=usb_bulk_write(sc->usb.dev, 1, cmd, cmdlen, USB_TIMEOUT);
82 //fprintf(stderr,"%i bytes written.\n",r);
83 if (resp != NULL && resplen != 0) {
84 //send read command
85 buff[0]=2; //func
86 seq++;
87 buff[1]=seq; buff[2]=~seq; //nseq
88 int2chars(buff+4,0x40);
89 buff[8]=0;
90 buff[9]=0xA;
91 buff[10]=0;
92 buff[11]=0;
93 //fprintf(stderr,"Writing resp req header\n");
94 //printb(buff,12);
95 r=usb_bulk_write(sc->usb.dev, 1, (char*)buff, 12, USB_TIMEOUT);
96 //fprintf(stderr,"%i bytes written. Reading response hdr\n",r);
97 r=usb_bulk_read(sc->usb.dev, 2, (char*)buff, 0x40, USB_TIMEOUT);
98 //printb(buff,r);
99 len=chars2int(buff+4);
100 //fprintf(stderr,"%i bytes read. Resplen=%i\n",r,len);
101 for (i=0; i<(r-12); i++) {
102 if (i<resplen) resp[i] = buff[i+12];
103 }
104 //printb(resp,r-12);
105 if (len > 0x40-12) {
106 //fprintf(stderr," Reading response:\n");
107 if (resplen<len) len=resplen;
108 r=usb_bulk_read(sc->usb.dev, 2, (char*)resp+(0x40-12), len-(0x40-12), USB_TIMEOUT);
109 //fprintf(stderr,"%i bytes read, wanted %i.\n", r, len-(0x40-12));
110 return r+(0x40-12);
111 }
112 return len;
113 }
114 return 0;
115 }
116
117 void usbtmc_claim(struct scope *sc)
118 {
119 usb_claim_interface(sc->usb.dev, 0);
120 }
121
122 void usbtmc_release(struct scope *sc)
123 {
124 usb_release_interface(sc->usb.dev, 0);
125 }
126
127 //Initialize the scope.
128 struct scope* usbtmc_initscope(void) {
129 int r;
130 unsigned char buff[10];
131 usb_dev_handle *dev;
132 struct scope *sc;
133
134 //Init libusb
135 usb_init();
136 //Locate and open the scope
137 dev = usbtmc_find_scope();
138 if (!dev) {
139 return NULL;
140 }
141
142 sc = calloc(1, sizeof(struct scope));
143 if (sc == NULL) {
144 perror("calloc");
145 exit(EXIT_FAILURE);
146 }
147
148 sc->usb.dev = dev;
149
150 usbtmc_claim(sc);
151 //The following code isn't really necessary, the program works
152 //OK without it too.
153 r=usb_control_msg(sc->usb.dev, 0xC8, 9, 0, 0, (char*)buff, 4, USB_TIMEOUT);
154 usbtmc_release(sc);
155 if (r < 0) {
156 fprintf (stderr, "Error %d sending init message: %s\n",
157 r, strerror (-r));
158 fprintf (stderr, "Do you have permission on the USB device?\n");
159 exit (1);
160 }
161 if (chars2int(buff)!=0x40005dc) {
162 fprintf(stderr,"Init: buff[%i]=%x\n",r,chars2int(buff));
163 }
164 return sc;
165 }
166
167 void usbtmc_close(struct scope *sc)
168 {
169 //Free up and exit
170 usb_close(sc->usb.dev);
171 }
Impressum, Datenschutz