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