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