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 | ||
7906b395 | 7 | #include "scope.h" |
58a9e276 MG |
8 | #include "usbtmc.h" |
9 | ||
f3ecdd6a | 10 | #define USB_TIMEOUT 10000 |
165abe62 | 11 | |
ddf9f28b MG |
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 | ||
483a564b | 20 | /* TODO: fix memory leak here: */ |
f3ecdd6a | 21 | #define USB_ERROR(s, x) do { if (x < 0) { fprintf(stderr, "usb %s: %s\n", s, usb_strerror()); usbtmc_clear(sc); return 0; } } while(0) |
58a9e276 | 22 | |
7b575069 MG |
23 | /* This routine locates a scope by VID/PID and returns a struct scope* for it */ |
24 | static struct scope* usbtmc_find_scope() { | |
58a9e276 MG |
25 | struct usb_bus *bus; |
26 | struct usb_device *dev=NULL; | |
7b575069 MG |
27 | struct usb_dev_handle *devh; |
28 | ||
29 | struct scope *sc; | |
30 | ||
58a9e276 MG |
31 | usb_find_busses(); |
32 | usb_find_devices(); | |
7b575069 | 33 | |
58a9e276 MG |
34 | for (bus=usb_busses; bus; bus=bus->next) { |
35 | for (dev=bus->devices; dev; dev=dev->next) { | |
7b575069 MG |
36 | if (dev->descriptor.idVendor == 0x400 && dev->descriptor.idProduct == 0x5dc) { |
37 | devh = usb_open(dev); | |
38 | if (devh == NULL) | |
39 | return NULL; | |
40 | ||
41 | sc = calloc(1, sizeof(struct scope)); | |
42 | if (sc == NULL) { | |
43 | perror("calloc"); | |
44 | exit(EXIT_FAILURE); | |
45 | } | |
46 | ||
47 | sc->usb.dev = devh; | |
48 | ||
49 | /* TODO: FIXME */ | |
50 | sc->usb.brokenRigol = 1; | |
c399a457 MG |
51 | sc->usb.ep_bulk_out = 0x01; |
52 | sc->usb.ep_bulk_in = 0x82; | |
7b575069 MG |
53 | sc->usb.wMaxPacketSize_in = 0x40; |
54 | ||
55 | return sc; | |
58a9e276 MG |
56 | } |
57 | } | |
58 | } | |
7b575069 | 59 | |
58a9e276 MG |
60 | return NULL; |
61 | } | |
62 | ||
483a564b | 63 | static unsigned char usbtmc_status(struct scope *sc) |
121b4233 MG |
64 | { |
65 | int r; | |
66 | unsigned char status[3]; | |
67 | ||
68 | sc->usb.bTag++; | |
69 | ||
483a564b MG |
70 | r = usb_control_msg(sc->usb.dev, 0xA1, |
71 | USB488_CTL_READ_STATUS_BYTE, | |
72 | (sc->usb.bTag & 0x7f), 0, (char*)status, 3, | |
73 | USB_TIMEOUT); | |
74 | ||
121b4233 MG |
75 | if ((r != 3) || (status[0] != 0x01) || (status[1] != (sc->usb.bTag & 0x7f))) { |
76 | printf("READ_STATUS_BYTE failed: %d 0x%x 0x%x 0x%x\n", r, status[0], status[1], status[2]); | |
77 | return 0xff; | |
78 | } | |
79 | ||
80 | return status[2]; | |
81 | } | |
82 | ||
483a564b MG |
83 | static struct usbtmc_capabilities* usbtmc_get_capabilities(struct scope *sc) |
84 | { | |
85 | int r; | |
86 | static struct usbtmc_capabilities res; | |
87 | ||
88 | r = usb_control_msg(sc->usb.dev, 0xA1, | |
89 | USBTMC_CTL_GET_CAPABILITIES, | |
90 | 0, 0, (char*)&res, sizeof(struct usbtmc_capabilities), | |
91 | USB_TIMEOUT); | |
92 | if (r != sizeof(struct usbtmc_capabilities)) { | |
93 | printf("GET_CAPABILITIES failed: %s\n", usb_strerror()); | |
94 | return NULL; | |
95 | } | |
96 | ||
97 | printf("USBTMC Version %x.%x Capabilities:\n", res.bcdUSBTMC[0], res.bcdUSBTMC[1]); | |
98 | if (res.USBTMCIFcapabilities & USBTMC_CAP_IF_INDICATOR_PULSE) | |
99 | printf("\tInterface supports indicator pulse\n"); | |
100 | ||
101 | if (res.USBTMCIFcapabilities & USBTMC_CAP_IF_TALKONLY) | |
102 | printf("\tInterface is talk only\n"); | |
103 | ||
104 | if (res.USBTMCIFcapabilities & USBTMC_CAP_IF_LISTENONLY) | |
105 | printf("\tInterface is listen only\n"); | |
106 | ||
107 | if (res.USBTMCDEVcapabilities & USBTMC_CAP_DEV_TERMCHAR_SUPP) | |
108 | printf("\tDevice supports Termchar\n"); | |
109 | ||
110 | printf("USB488 Version %x.%x Capabilities:\n", res.bcdUSB488[0], res.bcdUSB488[1]); | |
111 | ||
112 | if (res.USB488IFcapabilities & USB488_CAP_IF_4882) | |
113 | printf("\tInterface is 488.2 compliant\n"); | |
114 | ||
115 | if (res.USB488IFcapabilities & USB488_CAP_IF_LOCKOUT) | |
116 | printf("\tInterface supports local lockout\n"); | |
117 | ||
118 | if (res.USB488IFcapabilities & USB488_CAP_IF_TRIGGER) | |
119 | printf("\tInterface supports TRIGGER\n"); | |
120 | ||
121 | if (res.USB488DEVcapabilities & USB488_CAP_DEV_SCPI) | |
122 | printf("\tDevice is SCPI compliant\n"); | |
123 | ||
124 | if (res.USB488DEVcapabilities & USB488_CAP_DEV_SR1) | |
125 | printf("\tDevice is SR1 capable\n"); | |
126 | ||
127 | if (res.USB488DEVcapabilities & USB488_CAP_DEV_RL1) | |
128 | printf("\tDevice is RL1 capable\n"); | |
129 | ||
130 | if (res.USB488DEVcapabilities & USB488_CAP_DEV_DT1) | |
131 | printf("\tDevice is DT1 capable\n"); | |
132 | ||
133 | return &res; | |
134 | } | |
135 | ||
f3ecdd6a MG |
136 | static void usbtmc_clear(struct scope *sc) |
137 | { | |
138 | int r; | |
139 | unsigned char status[2]; | |
140 | ||
141 | printf("Initiating clear...\n"); | |
142 | r = usb_control_msg(sc->usb.dev, 0xA1, | |
143 | USBTMC_CTL_INITIATE_CLEAR, | |
144 | 0, 0, (char*)status, 1, | |
145 | USB_TIMEOUT); | |
146 | ||
147 | if ((r != 1) || status[0] != 0x01) { | |
148 | printf("INITIATE_CLEAR failed: %s\n", usb_strerror()); | |
149 | return; | |
150 | } | |
151 | ||
152 | while(1) { | |
153 | usleep(100000); | |
154 | printf("Waiting for clear to complete...\n"); | |
155 | ||
156 | r = usb_control_msg(sc->usb.dev, 0xA1, | |
157 | USBTMC_CTL_CHECK_CLEAR_STAT, | |
158 | 0, 0, (char*)status, 2, | |
159 | USB_TIMEOUT); | |
160 | ||
161 | if ((r != 2) || status[0] != 0x01) { | |
162 | printf("CHECK_CLEAR failed: %s\n", usb_strerror()); | |
163 | return; | |
164 | } | |
165 | ||
166 | if (status[1] & (1<<0)) { | |
167 | printf("Success!\n"); | |
168 | break; | |
169 | } | |
170 | } | |
171 | } | |
172 | ||
7b575069 MG |
173 | /* |
174 | * Send a scpi-command to the scope. The response goes into the buffer | |
175 | * called resp, with a size of resplen. If resp==NULL, no response | |
176 | * is requested. | |
177 | */ | |
7906b395 | 178 | int usbtmc_sendscpi(struct scope *sc, char* cmd, |
58a9e276 | 179 | unsigned char *resp, int resplen) { |
7b575069 | 180 | int len,r; |
58a9e276 | 181 | int cmdlen = strlen(cmd); |
7b575069 MG |
182 | struct usbtmc_header *req; |
183 | ||
184 | sc->usb.bTag++; | |
185 | ||
186 | len = sizeof(struct usbtmc_header) + cmdlen; | |
187 | if (len%4) | |
188 | len += 4 - (len%4); | |
189 | ||
190 | req = calloc(1, len); | |
191 | if (req == NULL) { | |
192 | perror("calloc"); | |
193 | exit(EXIT_FAILURE); | |
194 | } | |
195 | ||
196 | req->MsgID = USBTMC_DEV_DEP_MSG_OUT; | |
197 | req->bTag = sc->usb.bTag; | |
198 | req->bTagInverse = ~sc->usb.bTag; | |
199 | req->TransferSize = LE32(cmdlen); | |
200 | req->bmTransferAttributes = USBTMC_TRANSFERATTRIB_EOM; | |
201 | memcpy(req->msg, cmd, cmdlen); | |
202 | ||
203 | if (sc->usb.brokenRigol) { | |
204 | r=usb_bulk_write(sc->usb.dev, sc->usb.ep_bulk_out, | |
205 | (char*)req, sizeof(struct usbtmc_header), | |
206 | USB_TIMEOUT); | |
0aa0b761 | 207 | USB_ERROR("USBTMC_DEV_DEP_MSG_OUT1", r); |
7b575069 MG |
208 | |
209 | r=usb_bulk_write(sc->usb.dev, sc->usb.ep_bulk_out, | |
210 | (char*)&(req->msg), len - sizeof(struct usbtmc_header), | |
211 | USB_TIMEOUT); | |
0aa0b761 | 212 | USB_ERROR("USBTMC_DEV_DEP_MSG_OUT2", r); |
7b575069 MG |
213 | } else { |
214 | r=usb_bulk_write(sc->usb.dev, sc->usb.ep_bulk_out, | |
215 | (char*)req, len, USB_TIMEOUT); | |
0aa0b761 | 216 | USB_ERROR("USBTMC_DEV_DEP_MSG_OUT", r); |
7b575069 MG |
217 | } |
218 | ||
219 | free(req); | |
220 | ||
58a9e276 | 221 | if (resp != NULL && resplen != 0) { |
7b575069 MG |
222 | unsigned char *buff; |
223 | struct usbtmc_header *res; | |
224 | int bytes_read; | |
225 | ||
226 | sc->usb.bTag++; | |
227 | ||
228 | req = calloc(1, sizeof(struct usbtmc_header)); | |
229 | if (req == NULL) { | |
230 | perror("calloc"); | |
231 | exit(EXIT_FAILURE); | |
232 | } | |
233 | ||
234 | req->MsgID = USBTMC_REQUEST_DEV_DEP_MSG_IN; | |
235 | req->bTag = sc->usb.bTag; | |
236 | req->bTagInverse = ~sc->usb.bTag; | |
237 | req->TransferSize = LE32(sc->usb.wMaxPacketSize_in); | |
a163d2fb | 238 | req->bmTransferAttributes = 0; |
7b575069 MG |
239 | req->TermChar = 0; |
240 | ||
241 | /* send read command */ | |
242 | r=usb_bulk_write(sc->usb.dev, sc->usb.ep_bulk_out, | |
243 | (char*)req, sizeof(struct usbtmc_header), USB_TIMEOUT); | |
0aa0b761 | 244 | USB_ERROR("USBTMC_REQUEST_DEV_DEP_MSG_IN", r); |
7b575069 MG |
245 | |
246 | free(req); | |
247 | ||
248 | buff=malloc(sc->usb.wMaxPacketSize_in); | |
249 | if (buff == NULL) { | |
250 | perror("malloc"); | |
251 | exit(EXIT_FAILURE); | |
58a9e276 | 252 | } |
7b575069 MG |
253 | |
254 | r=usb_bulk_read(sc->usb.dev, sc->usb.ep_bulk_in, | |
255 | (char*)buff, sc->usb.wMaxPacketSize_in, USB_TIMEOUT); | |
0aa0b761 | 256 | USB_ERROR("USBTMC_DEV_DEP_MSG_IN1", r); |
7b575069 MG |
257 | |
258 | if (r < sizeof(struct usbtmc_header)) { | |
259 | fprintf(stderr, "Short read!\n"); | |
260 | return 0; | |
58a9e276 | 261 | } |
7b575069 MG |
262 | |
263 | bytes_read = r - sizeof(struct usbtmc_header); | |
264 | ||
265 | res = (struct usbtmc_header*)buff; | |
266 | len = LE32(res->TransferSize); | |
267 | ||
268 | memmove(buff, buff + sizeof(struct usbtmc_header), bytes_read); | |
269 | ||
270 | buff = realloc(buff, len); | |
271 | if (buff == NULL) { | |
272 | perror("realloc"); | |
273 | exit(EXIT_FAILURE); | |
274 | } | |
275 | ||
276 | while ((len - bytes_read) > 0) { | |
277 | r=usb_bulk_read(sc->usb.dev, sc->usb.ep_bulk_in, | |
278 | (char*)buff + bytes_read, len - bytes_read, | |
279 | USB_TIMEOUT); | |
0aa0b761 | 280 | USB_ERROR("USBTMC_DEV_DEP_MSG_INx", r); |
7b575069 MG |
281 | |
282 | bytes_read += r; | |
283 | } | |
284 | ||
285 | /* TODO: FIXME */ | |
286 | if (bytes_read > resplen) { | |
287 | fprintf(stderr, "Response buffer to small: %d instead of %d bytes!\n", | |
288 | resplen, bytes_read); | |
289 | bytes_read = resplen; | |
290 | } | |
291 | ||
292 | memcpy(resp, buff, bytes_read); | |
293 | free(buff); | |
294 | ||
295 | return bytes_read; | |
58a9e276 MG |
296 | } |
297 | return 0; | |
298 | } | |
299 | ||
7906b395 | 300 | void usbtmc_claim(struct scope *sc) |
ad9fbc05 | 301 | { |
7906b395 | 302 | usb_claim_interface(sc->usb.dev, 0); |
ad9fbc05 MG |
303 | } |
304 | ||
7906b395 | 305 | void usbtmc_release(struct scope *sc) |
ad9fbc05 | 306 | { |
7906b395 | 307 | usb_release_interface(sc->usb.dev, 0); |
ad9fbc05 MG |
308 | } |
309 | ||
483a564b | 310 | /* Initialize the scope. */ |
7906b395 | 311 | struct scope* usbtmc_initscope(void) { |
58a9e276 | 312 | int r; |
80564ddb | 313 | uint32_t vidpid; |
7906b395 | 314 | struct scope *sc; |
58a9e276 | 315 | |
7b575069 | 316 | /* Init libusb */ |
58a9e276 | 317 | usb_init(); |
7b575069 MG |
318 | /* Locate and open the scope */ |
319 | sc = usbtmc_find_scope(); | |
320 | if (!sc) { | |
2999345d | 321 | return NULL; |
58a9e276 | 322 | } |
7906b395 | 323 | |
7906b395 | 324 | usbtmc_claim(sc); |
90e416c4 | 325 | sc->usb.cap = usbtmc_get_capabilities(sc); |
483a564b | 326 | printf("Device status: 0x%x\n", usbtmc_status(sc)); |
7b575069 MG |
327 | /* The following code isn't really necessary, the program works |
328 | OK without it too. */ | |
80564ddb | 329 | r=usb_control_msg(sc->usb.dev, 0xC8, 9, 0, 0, (char*)&vidpid, 4, USB_TIMEOUT); |
7906b395 | 330 | usbtmc_release(sc); |
58a9e276 MG |
331 | if (r < 0) { |
332 | fprintf (stderr, "Error %d sending init message: %s\n", | |
333 | r, strerror (-r)); | |
334 | fprintf (stderr, "Do you have permission on the USB device?\n"); | |
335 | exit (1); | |
336 | } | |
80564ddb MG |
337 | if (LE32(vidpid)!=0x40005dc) { |
338 | fprintf(stderr,"Init: buff[%i]=%x\n",r,LE32(vidpid)); | |
58a9e276 | 339 | } |
7906b395 | 340 | return sc; |
58a9e276 | 341 | } |
b74fea90 | 342 | |
7906b395 | 343 | void usbtmc_close(struct scope *sc) |
b74fea90 | 344 | { |
7b575069 | 345 | /* Free up and exit */ |
7906b395 | 346 | usb_close(sc->usb.dev); |
b74fea90 | 347 | } |