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