]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2012 Bert Vermeulen <bert@biot.com> | |
5 | * With protocol information from the hantekdso project, | |
6 | * Copyright (C) 2008 Oleg Khudyakov <prcoder@gmail.com> | |
7 | * | |
8 | * This program is free software: you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation, either version 3 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 | */ | |
21 | ||
22 | #include "libsigrok.h" | |
23 | #include "libsigrok-internal.h" | |
24 | #include "dso.h" | |
25 | #include <string.h> | |
26 | #include <glib.h> | |
27 | #include <libusb.h> | |
28 | ||
29 | extern struct sr_dev_driver hantek_dso_driver_info; | |
30 | ||
31 | static int send_begin(const struct sr_dev_inst *sdi) | |
32 | { | |
33 | struct sr_usb_dev_inst *usb; | |
34 | int ret; | |
35 | unsigned char buffer[] = {0x0f, 0x03, 0x03, 0x03, 0x68, 0xac, 0xfe, | |
36 | 0x00, 0x01, 0x00}; | |
37 | ||
38 | sr_dbg("Sending CTRL_BEGINCOMMAND."); | |
39 | ||
40 | usb = sdi->conn; | |
41 | if ((ret = libusb_control_transfer(usb->devhdl, | |
42 | LIBUSB_REQUEST_TYPE_VENDOR, CTRL_BEGINCOMMAND, | |
43 | 0, 0, buffer, sizeof(buffer), 200)) != sizeof(buffer)) { | |
44 | sr_err("Failed to send begincommand: %s.", | |
45 | libusb_error_name(ret)); | |
46 | return SR_ERR; | |
47 | } | |
48 | ||
49 | return SR_OK; | |
50 | } | |
51 | ||
52 | static int send_bulkcmd(const struct sr_dev_inst *sdi, uint8_t *cmdstring, int cmdlen) | |
53 | { | |
54 | struct sr_usb_dev_inst *usb; | |
55 | int ret, tmp; | |
56 | ||
57 | usb = sdi->conn; | |
58 | ||
59 | if (send_begin(sdi) != SR_OK) | |
60 | return SR_ERR; | |
61 | ||
62 | if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT, cmdstring, | |
63 | cmdlen, &tmp, 200)) != 0) | |
64 | return SR_ERR; | |
65 | ||
66 | return SR_OK; | |
67 | } | |
68 | ||
69 | static int dso_getmps(libusb_device *dev) | |
70 | { | |
71 | struct libusb_device_descriptor des; | |
72 | struct libusb_config_descriptor *conf_dsc; | |
73 | const struct libusb_interface_descriptor *intf_dsc; | |
74 | int mps; | |
75 | ||
76 | if (libusb_get_device_descriptor(dev, &des) != 0) | |
77 | return 0; | |
78 | ||
79 | if (des.bNumConfigurations != 1) | |
80 | return 0; | |
81 | ||
82 | if (libusb_get_config_descriptor(dev, 0, &conf_dsc) != 0) | |
83 | return 0; | |
84 | ||
85 | mps = 0; | |
86 | intf_dsc = &(conf_dsc->interface[0].altsetting[0]); | |
87 | if (intf_dsc->bNumEndpoints != 2) | |
88 | goto err; | |
89 | ||
90 | if ((intf_dsc->endpoint[0].bEndpointAddress & 0x8f) != | |
91 | (2 | LIBUSB_ENDPOINT_OUT)) | |
92 | /* The first endpoint should be 2 (outbound). */ | |
93 | goto err; | |
94 | ||
95 | if ((intf_dsc->endpoint[1].bEndpointAddress & 0x8f) != | |
96 | (6 | LIBUSB_ENDPOINT_IN)) | |
97 | /* The second endpoint should be 6 (inbound). */ | |
98 | goto err; | |
99 | ||
100 | mps = intf_dsc->endpoint[1].wMaxPacketSize; | |
101 | ||
102 | err: | |
103 | if (conf_dsc) | |
104 | libusb_free_config_descriptor(conf_dsc); | |
105 | ||
106 | return mps; | |
107 | } | |
108 | ||
109 | SR_PRIV int dso_open(struct sr_dev_inst *sdi) | |
110 | { | |
111 | struct dev_context *devc; | |
112 | struct drv_context *drvc = hantek_dso_driver_info.priv; | |
113 | struct sr_usb_dev_inst *usb; | |
114 | struct libusb_device_descriptor des; | |
115 | libusb_device **devlist; | |
116 | int err, i; | |
117 | char connection_id[64]; | |
118 | ||
119 | devc = sdi->priv; | |
120 | usb = sdi->conn; | |
121 | ||
122 | if (sdi->status == SR_ST_ACTIVE) | |
123 | /* already in use */ | |
124 | return SR_ERR; | |
125 | ||
126 | libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); | |
127 | for (i = 0; devlist[i]; i++) { | |
128 | if ((err = libusb_get_device_descriptor(devlist[i], &des))) { | |
129 | sr_err("Failed to get device descriptor: %s.", | |
130 | libusb_error_name(err)); | |
131 | continue; | |
132 | } | |
133 | ||
134 | if (des.idVendor != devc->profile->fw_vid | |
135 | || des.idProduct != devc->profile->fw_pid) | |
136 | continue; | |
137 | ||
138 | if ((sdi->status == SR_ST_INITIALIZING) || | |
139 | (sdi->status == SR_ST_INACTIVE)) { | |
140 | /* | |
141 | * Check device by its physical USB bus/port address. | |
142 | */ | |
143 | usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)); | |
144 | if (strcmp(sdi->connection_id, connection_id)) | |
145 | /* This is not the one. */ | |
146 | continue; | |
147 | } | |
148 | ||
149 | if (!(err = libusb_open(devlist[i], &usb->devhdl))) { | |
150 | if (usb->address == 0xff) | |
151 | /* | |
152 | * first time we touch this device after firmware upload, | |
153 | * so we don't know the address yet. | |
154 | */ | |
155 | usb->address = libusb_get_device_address(devlist[i]); | |
156 | ||
157 | if (!(devc->epin_maxpacketsize = dso_getmps(devlist[i]))) | |
158 | sr_err("Wrong endpoint profile."); | |
159 | else { | |
160 | sdi->status = SR_ST_ACTIVE; | |
161 | sr_info("Opened device on %d.%d (logical) / " | |
162 | "%s (physical) interface %d.", | |
163 | usb->bus, usb->address, | |
164 | sdi->connection_id, USB_INTERFACE); | |
165 | } | |
166 | } else { | |
167 | sr_err("Failed to open device: %s.", | |
168 | libusb_error_name(err)); | |
169 | } | |
170 | ||
171 | /* If we made it here, we handled the device (somehow). */ | |
172 | break; | |
173 | } | |
174 | libusb_free_device_list(devlist, 1); | |
175 | ||
176 | if (sdi->status != SR_ST_ACTIVE) | |
177 | return SR_ERR; | |
178 | ||
179 | return SR_OK; | |
180 | } | |
181 | ||
182 | SR_PRIV void dso_close(struct sr_dev_inst *sdi) | |
183 | { | |
184 | struct sr_usb_dev_inst *usb; | |
185 | ||
186 | usb = sdi->conn; | |
187 | ||
188 | if (usb->devhdl == NULL) | |
189 | return; | |
190 | ||
191 | sr_info("Closing device on %d.%d (logical) / %s (physical) interface %d.", | |
192 | usb->bus, usb->address, sdi->connection_id, USB_INTERFACE); | |
193 | libusb_release_interface(usb->devhdl, USB_INTERFACE); | |
194 | libusb_close(usb->devhdl); | |
195 | usb->devhdl = NULL; | |
196 | sdi->status = SR_ST_INACTIVE; | |
197 | ||
198 | } | |
199 | ||
200 | static int get_channel_offsets(const struct sr_dev_inst *sdi) | |
201 | { | |
202 | struct dev_context *devc; | |
203 | struct sr_usb_dev_inst *usb; | |
204 | GString *gs; | |
205 | int chan, v, ret; | |
206 | ||
207 | sr_dbg("Getting channel offsets."); | |
208 | ||
209 | devc = sdi->priv; | |
210 | usb = sdi->conn; | |
211 | ||
212 | ret = libusb_control_transfer(usb->devhdl, | |
213 | LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR, | |
214 | CTRL_READ_EEPROM, EEPROM_CHANNEL_OFFSETS, 0, | |
215 | (unsigned char *)&devc->channel_levels, | |
216 | sizeof(devc->channel_levels), 200); | |
217 | if (ret != sizeof(devc->channel_levels)) { | |
218 | sr_err("Failed to get channel offsets: %s.", | |
219 | libusb_error_name(ret)); | |
220 | return SR_ERR; | |
221 | } | |
222 | ||
223 | /* Comes in as 16-bit numbers with the second byte always 0 on | |
224 | * the DSO-2090. Guessing this is supposed to be big-endian, | |
225 | * since that's how voltage offsets are submitted back to the DSO. | |
226 | * Convert to host order now, so we can use them natively. | |
227 | */ | |
228 | for (chan = 0; chan < 2; chan++) { | |
229 | for (v = 0; v < 9; v++) { | |
230 | devc->channel_levels[chan][v][0] = | |
231 | g_ntohs(devc->channel_levels[chan][v][0]); | |
232 | devc->channel_levels[chan][v][1] = | |
233 | g_ntohs(devc->channel_levels[chan][v][1]); | |
234 | } | |
235 | } | |
236 | ||
237 | if (sr_log_loglevel_get() >= SR_LOG_DBG) { | |
238 | gs = g_string_sized_new(128); | |
239 | for (chan = 0; chan < 2; chan++) { | |
240 | g_string_printf(gs, "CH%d:", chan + 1); | |
241 | for (v = 0; v < 9; v++) { | |
242 | g_string_append_printf(gs, " %.4x-%.4x", | |
243 | devc->channel_levels[chan][v][0], | |
244 | devc->channel_levels[chan][v][1]); | |
245 | } | |
246 | sr_dbg("%s", gs->str); | |
247 | } | |
248 | g_string_free(gs, TRUE); | |
249 | } | |
250 | ||
251 | return SR_OK; | |
252 | } | |
253 | ||
254 | static int dso_set_trigger_samplerate(const struct sr_dev_inst *sdi) | |
255 | { | |
256 | struct dev_context *devc; | |
257 | struct sr_usb_dev_inst *usb; | |
258 | int ret, tmp; | |
259 | uint8_t cmdstring[12]; | |
260 | uint16_t timebase_small[] = { 0xffff, 0xfffc, 0xfff7, 0xffe8, 0xffce, | |
261 | 0xff9c, 0xff07, 0xfe0d, 0xfc19, 0xf63d, 0xec79, 0xd8f1 }; | |
262 | uint16_t timebase_large[] = { 0xffff, 0x0000, 0xfffc, 0xfff7, 0xffe8, | |
263 | 0xffce, 0xff9d, 0xff07, 0xfe0d, 0xfc19, 0xf63d, 0xec79 }; | |
264 | ||
265 | sr_dbg("Preparing CMD_SET_TRIGGER_SAMPLERATE."); | |
266 | ||
267 | devc = sdi->priv; | |
268 | usb = sdi->conn; | |
269 | ||
270 | memset(cmdstring, 0, sizeof(cmdstring)); | |
271 | /* Command */ | |
272 | cmdstring[0] = CMD_SET_TRIGGER_SAMPLERATE; | |
273 | ||
274 | /* Trigger source */ | |
275 | sr_dbg("Trigger source %s.", devc->triggersource); | |
276 | if (!strcmp("CH2", devc->triggersource)) | |
277 | tmp = 0; | |
278 | else if (!strcmp("CH1", devc->triggersource)) | |
279 | tmp = 1; | |
280 | else if (!strcmp("EXT", devc->triggersource)) | |
281 | tmp = 2; | |
282 | else { | |
283 | sr_err("Invalid trigger source: '%s'.", devc->triggersource); | |
284 | return SR_ERR_ARG; | |
285 | } | |
286 | cmdstring[2] = tmp; | |
287 | ||
288 | /* Frame size */ | |
289 | sr_dbg("Frame size: %d.", devc->framesize); | |
290 | cmdstring[2] |= (devc->framesize == FRAMESIZE_SMALL ? 0x01 : 0x02) << 2; | |
291 | ||
292 | /* Timebase fast */ | |
293 | sr_dbg("Time base index: %d.", devc->timebase); | |
294 | if (devc->framesize == FRAMESIZE_SMALL) { | |
295 | if (devc->timebase < TIME_20us) | |
296 | tmp = 0; | |
297 | else if (devc->timebase == TIME_20us) | |
298 | tmp = 1; | |
299 | else if (devc->timebase == TIME_40us) | |
300 | tmp = 2; | |
301 | else if (devc->timebase == TIME_100us) | |
302 | tmp = 3; | |
303 | else if (devc->timebase >= TIME_200us) | |
304 | tmp = 4; | |
305 | } else { | |
306 | if (devc->timebase < TIME_40us) { | |
307 | sr_err("Timebase < 40us only supported with 10K buffer."); | |
308 | return SR_ERR_ARG; | |
309 | } | |
310 | else if (devc->timebase == TIME_40us) | |
311 | tmp = 0; | |
312 | else if (devc->timebase == TIME_100us) | |
313 | tmp = 2; | |
314 | else if (devc->timebase == TIME_200us) | |
315 | tmp = 3; | |
316 | else if (devc->timebase >= TIME_400us) | |
317 | tmp = 4; | |
318 | } | |
319 | cmdstring[2] |= (tmp & 0x07) << 5; | |
320 | ||
321 | /* Enabled channels: 00=CH1 01=CH2 10=both */ | |
322 | sr_dbg("Channels CH1=%d CH2=%d", devc->ch1_enabled, devc->ch2_enabled); | |
323 | tmp = (((devc->ch2_enabled ? 1 : 0) << 1) + (devc->ch1_enabled ? 1 : 0)) - 1; | |
324 | cmdstring[3] = tmp; | |
325 | ||
326 | /* Fast rates channel */ | |
327 | /* TODO: Is this right? */ | |
328 | tmp = devc->timebase < TIME_10us ? 1 : 0; | |
329 | cmdstring[3] |= tmp << 2; | |
330 | ||
331 | /* Trigger slope: 0=positive 1=negative */ | |
332 | /* TODO: Does this work? */ | |
333 | sr_dbg("Trigger slope: %d.", devc->triggerslope); | |
334 | cmdstring[3] |= (devc->triggerslope == SLOPE_NEGATIVE ? 1 : 0) << 3; | |
335 | ||
336 | /* Timebase slow */ | |
337 | if (devc->timebase < TIME_100us) | |
338 | tmp = 0; | |
339 | else if (devc->timebase > TIME_400ms) | |
340 | tmp = 0xffed; | |
341 | else { | |
342 | if (devc->framesize == FRAMESIZE_SMALL) | |
343 | tmp = timebase_small[devc->timebase - 3]; | |
344 | else | |
345 | tmp = timebase_large[devc->timebase - 3]; | |
346 | } | |
347 | cmdstring[4] = tmp & 0xff; | |
348 | cmdstring[5] = (tmp >> 8) & 0xff; | |
349 | ||
350 | /* Horizontal trigger position */ | |
351 | sr_dbg("Trigger position: %3.2f.", devc->triggerposition); | |
352 | tmp = 0x77fff + 0x8000 * devc->triggerposition; | |
353 | cmdstring[6] = tmp & 0xff; | |
354 | cmdstring[7] = (tmp >> 8) & 0xff; | |
355 | cmdstring[10] = (tmp >> 16) & 0xff; | |
356 | ||
357 | if (send_begin(sdi) != SR_OK) | |
358 | return SR_ERR; | |
359 | ||
360 | if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT, | |
361 | cmdstring, sizeof(cmdstring), &tmp, 100)) != 0) { | |
362 | sr_err("Failed to set trigger/samplerate: %s.", | |
363 | libusb_error_name(ret)); | |
364 | return SR_ERR; | |
365 | } | |
366 | sr_dbg("Sent CMD_SET_TRIGGER_SAMPLERATE."); | |
367 | ||
368 | return SR_OK; | |
369 | } | |
370 | ||
371 | static int dso_set_filters(const struct sr_dev_inst *sdi) | |
372 | { | |
373 | struct dev_context *devc; | |
374 | struct sr_usb_dev_inst *usb; | |
375 | int ret, tmp; | |
376 | uint8_t cmdstring[8]; | |
377 | ||
378 | sr_dbg("Preparing CMD_SET_FILTERS."); | |
379 | ||
380 | devc = sdi->priv; | |
381 | usb = sdi->conn; | |
382 | ||
383 | memset(cmdstring, 0, sizeof(cmdstring)); | |
384 | cmdstring[0] = CMD_SET_FILTERS; | |
385 | cmdstring[1] = 0x0f; | |
386 | if (devc->filter[0]) { | |
387 | sr_dbg("Turning on CH1 filter."); | |
388 | cmdstring[2] |= 0x80; | |
389 | } | |
390 | if (devc->filter[1]) { | |
391 | sr_dbg("Turning on CH2 filter."); | |
392 | cmdstring[2] |= 0x40; | |
393 | } | |
394 | /* | |
395 | * Not supported: filtering on the trigger | |
396 | * cmdstring[2] |= 0x20; | |
397 | */ | |
398 | ||
399 | if (send_begin(sdi) != SR_OK) | |
400 | return SR_ERR; | |
401 | ||
402 | if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT, | |
403 | cmdstring, sizeof(cmdstring), &tmp, 100)) != 0) { | |
404 | sr_err("Failed to set filters: %s.", libusb_error_name(ret)); | |
405 | return SR_ERR; | |
406 | } | |
407 | sr_dbg("Sent CMD_SET_FILTERS."); | |
408 | ||
409 | return SR_OK; | |
410 | } | |
411 | ||
412 | static int dso_set_voltage(const struct sr_dev_inst *sdi) | |
413 | { | |
414 | struct dev_context *devc; | |
415 | struct sr_usb_dev_inst *usb; | |
416 | int ret, tmp; | |
417 | uint8_t cmdstring[8]; | |
418 | ||
419 | sr_dbg("Preparing CMD_SET_VOLTAGE."); | |
420 | ||
421 | devc = sdi->priv; | |
422 | usb = sdi->conn; | |
423 | ||
424 | memset(cmdstring, 0, sizeof(cmdstring)); | |
425 | cmdstring[0] = CMD_SET_VOLTAGE; | |
426 | cmdstring[1] = 0x0f; | |
427 | cmdstring[2] = 0x30; | |
428 | ||
429 | /* CH1 volts/div is encoded in bits 0-1 */ | |
430 | sr_dbg("CH1 vdiv index: %d.", devc->voltage[0]); | |
431 | switch (devc->voltage[0]) { | |
432 | case VDIV_1V: | |
433 | case VDIV_100MV: | |
434 | case VDIV_10MV: | |
435 | cmdstring[2] |= 0x00; | |
436 | break; | |
437 | case VDIV_2V: | |
438 | case VDIV_200MV: | |
439 | case VDIV_20MV: | |
440 | cmdstring[2] |= 0x01; | |
441 | break; | |
442 | case VDIV_5V: | |
443 | case VDIV_500MV: | |
444 | case VDIV_50MV: | |
445 | cmdstring[2] |= 0x02; | |
446 | break; | |
447 | } | |
448 | ||
449 | /* CH2 volts/div is encoded in bits 2-3 */ | |
450 | sr_dbg("CH2 vdiv index: %d.", devc->voltage[1]); | |
451 | switch (devc->voltage[1]) { | |
452 | case VDIV_1V: | |
453 | case VDIV_100MV: | |
454 | case VDIV_10MV: | |
455 | cmdstring[2] |= 0x00; | |
456 | break; | |
457 | case VDIV_2V: | |
458 | case VDIV_200MV: | |
459 | case VDIV_20MV: | |
460 | cmdstring[2] |= 0x04; | |
461 | break; | |
462 | case VDIV_5V: | |
463 | case VDIV_500MV: | |
464 | case VDIV_50MV: | |
465 | cmdstring[2] |= 0x08; | |
466 | break; | |
467 | } | |
468 | ||
469 | if (send_begin(sdi) != SR_OK) | |
470 | return SR_ERR; | |
471 | ||
472 | if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT, | |
473 | cmdstring, sizeof(cmdstring), &tmp, 100)) != 0) { | |
474 | sr_err("Failed to set voltage: %s.", libusb_error_name(ret)); | |
475 | return SR_ERR; | |
476 | } | |
477 | sr_dbg("Sent CMD_SET_VOLTAGE."); | |
478 | ||
479 | return SR_OK; | |
480 | } | |
481 | ||
482 | static int dso_set_relays(const struct sr_dev_inst *sdi) | |
483 | { | |
484 | struct dev_context *devc; | |
485 | struct sr_usb_dev_inst *usb; | |
486 | GString *gs; | |
487 | int ret, i; | |
488 | uint8_t relays[17] = { 0x00, 0x04, 0x08, 0x02, 0x20, 0x40, 0x10, 0x01, | |
489 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; | |
490 | ||
491 | sr_dbg("Preparing CTRL_SETRELAYS."); | |
492 | ||
493 | devc = sdi->priv; | |
494 | usb = sdi->conn; | |
495 | ||
496 | if (devc->voltage[0] < VDIV_1V) | |
497 | relays[1] = ~relays[1]; | |
498 | ||
499 | if (devc->voltage[0] < VDIV_100MV) | |
500 | relays[2] = ~relays[2]; | |
501 | ||
502 | sr_dbg("CH1 coupling: %d.", devc->coupling[0]); | |
503 | if (devc->coupling[0] != COUPLING_AC) | |
504 | relays[3] = ~relays[3]; | |
505 | ||
506 | if (devc->voltage[1] < VDIV_1V) | |
507 | relays[4] = ~relays[4]; | |
508 | ||
509 | if (devc->voltage[1] < VDIV_100MV) | |
510 | relays[5] = ~relays[5]; | |
511 | ||
512 | sr_dbg("CH2 coupling: %d.", devc->coupling[1]); | |
513 | if (devc->coupling[1] != COUPLING_AC) | |
514 | relays[6] = ~relays[6]; | |
515 | ||
516 | if (!strcmp(devc->triggersource, "EXT")) | |
517 | relays[7] = ~relays[7]; | |
518 | ||
519 | if (sr_log_loglevel_get() >= SR_LOG_DBG) { | |
520 | gs = g_string_sized_new(128); | |
521 | g_string_printf(gs, "Relays:"); | |
522 | for (i = 0; i < 17; i++) | |
523 | g_string_append_printf(gs, " %.2x", relays[i]); | |
524 | sr_dbg("%s", gs->str); | |
525 | g_string_free(gs, TRUE); | |
526 | } | |
527 | ||
528 | if ((ret = libusb_control_transfer(usb->devhdl, | |
529 | LIBUSB_REQUEST_TYPE_VENDOR, CTRL_SETRELAYS, | |
530 | 0, 0, relays, 17, 100)) != sizeof(relays)) { | |
531 | sr_err("Failed to set relays: %s.", libusb_error_name(ret)); | |
532 | return SR_ERR; | |
533 | } | |
534 | sr_dbg("Sent CTRL_SETRELAYS."); | |
535 | ||
536 | return SR_OK; | |
537 | } | |
538 | ||
539 | static int dso_set_voffsets(const struct sr_dev_inst *sdi) | |
540 | { | |
541 | struct dev_context *devc; | |
542 | struct sr_usb_dev_inst *usb; | |
543 | int offset, ret; | |
544 | uint16_t *ch_levels; | |
545 | uint8_t offsets[17]; | |
546 | ||
547 | sr_dbg("Preparing CTRL_SETOFFSET."); | |
548 | ||
549 | devc = sdi->priv; | |
550 | usb = sdi->conn; | |
551 | ||
552 | memset(offsets, 0, sizeof(offsets)); | |
553 | /* Channel 1 */ | |
554 | ch_levels = devc->channel_levels[0][devc->voltage[0]]; | |
555 | offset = (ch_levels[1] - ch_levels[0]) * devc->voffset_ch1 + ch_levels[0]; | |
556 | offsets[0] = (offset >> 8) | 0x20; | |
557 | offsets[1] = offset & 0xff; | |
558 | sr_dbg("CH1 offset: %3.2f (%.2x%.2x).", devc->voffset_ch1, | |
559 | offsets[0], offsets[1]); | |
560 | ||
561 | /* Channel 2 */ | |
562 | ch_levels = devc->channel_levels[1][devc->voltage[1]]; | |
563 | offset = (ch_levels[1] - ch_levels[0]) * devc->voffset_ch2 + ch_levels[0]; | |
564 | offsets[2] = (offset >> 8) | 0x20; | |
565 | offsets[3] = offset & 0xff; | |
566 | sr_dbg("CH2 offset: %3.2f (%.2x%.2x).", devc->voffset_ch2, | |
567 | offsets[2], offsets[3]); | |
568 | ||
569 | /* Trigger */ | |
570 | offset = MAX_VERT_TRIGGER * devc->voffset_trigger; | |
571 | offsets[4] = (offset >> 8) | 0x20; | |
572 | offsets[5] = offset & 0xff; | |
573 | sr_dbg("Trigger offset: %3.2f (%.2x%.2x).", devc->voffset_trigger, | |
574 | offsets[4], offsets[5]); | |
575 | ||
576 | if ((ret = libusb_control_transfer(usb->devhdl, | |
577 | LIBUSB_REQUEST_TYPE_VENDOR, CTRL_SETOFFSET, | |
578 | 0, 0, offsets, sizeof(offsets), 100)) != sizeof(offsets)) { | |
579 | sr_err("Failed to set offsets: %s.", libusb_error_name(ret)); | |
580 | return SR_ERR; | |
581 | } | |
582 | sr_dbg("Sent CTRL_SETOFFSET."); | |
583 | ||
584 | return SR_OK; | |
585 | } | |
586 | ||
587 | SR_PRIV int dso_enable_trigger(const struct sr_dev_inst *sdi) | |
588 | { | |
589 | struct sr_usb_dev_inst *usb; | |
590 | int ret, tmp; | |
591 | uint8_t cmdstring[2]; | |
592 | ||
593 | sr_dbg("Sending CMD_ENABLE_TRIGGER."); | |
594 | ||
595 | usb = sdi->conn; | |
596 | ||
597 | memset(cmdstring, 0, sizeof(cmdstring)); | |
598 | cmdstring[0] = CMD_ENABLE_TRIGGER; | |
599 | cmdstring[1] = 0x00; | |
600 | ||
601 | if (send_begin(sdi) != SR_OK) | |
602 | return SR_ERR; | |
603 | ||
604 | if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT, | |
605 | cmdstring, sizeof(cmdstring), &tmp, 100)) != 0) { | |
606 | sr_err("Failed to enable trigger: %s.", libusb_error_name(ret)); | |
607 | return SR_ERR; | |
608 | } | |
609 | ||
610 | return SR_OK; | |
611 | } | |
612 | ||
613 | SR_PRIV int dso_force_trigger(const struct sr_dev_inst *sdi) | |
614 | { | |
615 | struct sr_usb_dev_inst *usb; | |
616 | int ret, tmp; | |
617 | uint8_t cmdstring[2]; | |
618 | ||
619 | sr_dbg("Sending CMD_FORCE_TRIGGER."); | |
620 | ||
621 | usb = sdi->conn; | |
622 | ||
623 | memset(cmdstring, 0, sizeof(cmdstring)); | |
624 | cmdstring[0] = CMD_FORCE_TRIGGER; | |
625 | cmdstring[1] = 0x00; | |
626 | ||
627 | if (send_begin(sdi) != SR_OK) | |
628 | return SR_ERR; | |
629 | ||
630 | if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT, | |
631 | cmdstring, sizeof(cmdstring), &tmp, 100)) != 0) { | |
632 | sr_err("Failed to force trigger: %s.", libusb_error_name(ret)); | |
633 | return SR_ERR; | |
634 | } | |
635 | ||
636 | return SR_OK; | |
637 | } | |
638 | ||
639 | SR_PRIV int dso_init(const struct sr_dev_inst *sdi) | |
640 | { | |
641 | ||
642 | sr_dbg("Initializing DSO."); | |
643 | ||
644 | if (get_channel_offsets(sdi) != SR_OK) | |
645 | return SR_ERR; | |
646 | ||
647 | if (dso_set_trigger_samplerate(sdi) != SR_OK) | |
648 | return SR_ERR; | |
649 | ||
650 | if (dso_set_filters(sdi) != SR_OK) | |
651 | return SR_ERR; | |
652 | ||
653 | if (dso_set_voltage(sdi) != SR_OK) | |
654 | return SR_ERR; | |
655 | ||
656 | if (dso_set_relays(sdi) != SR_OK) | |
657 | return SR_ERR; | |
658 | ||
659 | if (dso_set_voffsets(sdi) != SR_OK) | |
660 | return SR_ERR; | |
661 | ||
662 | if (dso_enable_trigger(sdi) != SR_OK) | |
663 | return SR_ERR; | |
664 | ||
665 | return SR_OK; | |
666 | } | |
667 | ||
668 | SR_PRIV int dso_get_capturestate(const struct sr_dev_inst *sdi, | |
669 | uint8_t *capturestate, uint32_t *trigger_offset) | |
670 | { | |
671 | struct sr_usb_dev_inst *usb; | |
672 | int ret, tmp, i; | |
673 | unsigned int bitvalue, toff; | |
674 | uint8_t cmdstring[2], inbuf[512]; | |
675 | ||
676 | sr_dbg("Sending CMD_GET_CAPTURESTATE."); | |
677 | ||
678 | usb = sdi->conn; | |
679 | ||
680 | cmdstring[0] = CMD_GET_CAPTURESTATE; | |
681 | cmdstring[1] = 0; | |
682 | ||
683 | if ((ret = send_bulkcmd(sdi, cmdstring, sizeof(cmdstring))) != SR_OK) { | |
684 | sr_dbg("Failed to send get_capturestate command: %s.", | |
685 | libusb_error_name(ret)); | |
686 | return SR_ERR; | |
687 | } | |
688 | ||
689 | if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_IN, | |
690 | inbuf, 512, &tmp, 100)) != 0) { | |
691 | sr_dbg("Failed to get capturestate: %s.", | |
692 | libusb_error_name(ret)); | |
693 | return SR_ERR; | |
694 | } | |
695 | *capturestate = inbuf[0]; | |
696 | toff = (inbuf[1] << 16) | (inbuf[3] << 8) | inbuf[2]; | |
697 | ||
698 | /* | |
699 | * This conversion comes from the openhantek project. | |
700 | * Each set bit in the 24-bit value inverts all bits with a lower | |
701 | * value. No idea why the device reports the trigger point this way. | |
702 | */ | |
703 | bitvalue = 1; | |
704 | for (i = 0; i < 24; i++) { | |
705 | /* Each set bit inverts all bits with a lower value. */ | |
706 | if(toff & bitvalue) | |
707 | toff ^= bitvalue - 1; | |
708 | bitvalue <<= 1; | |
709 | } | |
710 | *trigger_offset = toff; | |
711 | ||
712 | return SR_OK; | |
713 | } | |
714 | ||
715 | SR_PRIV int dso_capture_start(const struct sr_dev_inst *sdi) | |
716 | { | |
717 | int ret; | |
718 | uint8_t cmdstring[2]; | |
719 | ||
720 | sr_dbg("Sending CMD_CAPTURE_START."); | |
721 | ||
722 | cmdstring[0] = CMD_CAPTURE_START; | |
723 | cmdstring[1] = 0; | |
724 | ||
725 | if ((ret = send_bulkcmd(sdi, cmdstring, sizeof(cmdstring))) != SR_OK) { | |
726 | sr_err("Failed to send capture_start command: %s.", | |
727 | libusb_error_name(ret)); | |
728 | return SR_ERR; | |
729 | } | |
730 | ||
731 | return SR_OK; | |
732 | } | |
733 | ||
734 | SR_PRIV int dso_get_channeldata(const struct sr_dev_inst *sdi, | |
735 | libusb_transfer_cb_fn cb) | |
736 | { | |
737 | struct dev_context *devc; | |
738 | struct sr_usb_dev_inst *usb; | |
739 | struct libusb_transfer *transfer; | |
740 | int num_transfers, ret, i; | |
741 | uint8_t cmdstring[2]; | |
742 | unsigned char *buf; | |
743 | ||
744 | sr_dbg("Sending CMD_GET_CHANNELDATA."); | |
745 | ||
746 | devc = sdi->priv; | |
747 | usb = sdi->conn; | |
748 | ||
749 | cmdstring[0] = CMD_GET_CHANNELDATA; | |
750 | cmdstring[1] = 0; | |
751 | ||
752 | if ((ret = send_bulkcmd(sdi, cmdstring, sizeof(cmdstring))) != SR_OK) { | |
753 | sr_err("Failed to get channel data: %s.", | |
754 | libusb_error_name(ret)); | |
755 | return SR_ERR; | |
756 | } | |
757 | ||
758 | /* TODO: DSO-2xxx only. */ | |
759 | num_transfers = devc->framesize * | |
760 | sizeof(unsigned short) / devc->epin_maxpacketsize; | |
761 | sr_dbg("Queueing up %d transfers.", num_transfers); | |
762 | for (i = 0; i < num_transfers; i++) { | |
763 | if (!(buf = g_try_malloc(devc->epin_maxpacketsize))) { | |
764 | sr_err("Failed to malloc USB endpoint buffer."); | |
765 | return SR_ERR_MALLOC; | |
766 | } | |
767 | transfer = libusb_alloc_transfer(0); | |
768 | libusb_fill_bulk_transfer(transfer, usb->devhdl, DSO_EP_IN, buf, | |
769 | devc->epin_maxpacketsize, cb, (void *)sdi, 40); | |
770 | if ((ret = libusb_submit_transfer(transfer)) != 0) { | |
771 | sr_err("Failed to submit transfer: %s.", | |
772 | libusb_error_name(ret)); | |
773 | /* TODO: Free them all. */ | |
774 | libusb_free_transfer(transfer); | |
775 | g_free(buf); | |
776 | return SR_ERR; | |
777 | } | |
778 | } | |
779 | ||
780 | return SR_OK; | |
781 | } |