2 * This file is part of the libsigrok project.
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>
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.
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.
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/>.
26 #include <libsigrok/libsigrok.h>
27 #include "libsigrok-internal.h"
30 static int send_begin(const struct sr_dev_inst *sdi)
32 struct sr_usb_dev_inst *usb;
34 unsigned char buffer[] = {0x0f, 0x03, 0x03, 0x03, 0x68, 0xac, 0xfe,
37 sr_dbg("Sending CTRL_BEGINCOMMAND.");
40 if ((ret = libusb_control_transfer(usb->devhdl,
41 LIBUSB_REQUEST_TYPE_VENDOR, CTRL_BEGINCOMMAND,
42 0, 0, buffer, sizeof(buffer), 200)) != sizeof(buffer)) {
43 sr_err("Failed to send begincommand: %s.",
44 libusb_error_name(ret));
51 static int send_bulkcmd(const struct sr_dev_inst *sdi, uint8_t *cmdstring, int cmdlen)
53 struct sr_usb_dev_inst *usb;
58 if (send_begin(sdi) != SR_OK)
61 if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT, cmdstring,
62 cmdlen, &tmp, 200)) != 0)
68 static int dso_getmps(libusb_device *dev)
70 struct libusb_device_descriptor des;
71 struct libusb_config_descriptor *conf_dsc;
72 const struct libusb_interface_descriptor *intf_dsc;
75 libusb_get_device_descriptor(dev, &des);
77 if (des.bNumConfigurations != 1)
80 if (libusb_get_config_descriptor(dev, 0, &conf_dsc) != 0)
84 intf_dsc = &(conf_dsc->interface[0].altsetting[0]);
85 if (intf_dsc->bNumEndpoints != 2)
88 if ((intf_dsc->endpoint[0].bEndpointAddress & 0x8f) !=
89 (2 | LIBUSB_ENDPOINT_OUT))
90 /* The first endpoint should be 2 (outbound). */
93 if ((intf_dsc->endpoint[1].bEndpointAddress & 0x8f) !=
94 (6 | LIBUSB_ENDPOINT_IN))
95 /* The second endpoint should be 6 (inbound). */
98 mps = intf_dsc->endpoint[1].wMaxPacketSize;
102 libusb_free_config_descriptor(conf_dsc);
107 SR_PRIV int dso_open(struct sr_dev_inst *sdi)
109 struct dev_context *devc;
110 struct drv_context *drvc = sdi->driver->context;
111 struct sr_usb_dev_inst *usb;
112 struct libusb_device_descriptor des;
113 libusb_device **devlist;
115 char connection_id[64];
120 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
121 for (i = 0; devlist[i]; i++) {
122 libusb_get_device_descriptor(devlist[i], &des);
124 if (des.idVendor != devc->profile->fw_vid
125 || des.idProduct != devc->profile->fw_pid)
128 if ((sdi->status == SR_ST_INITIALIZING) ||
129 (sdi->status == SR_ST_INACTIVE)) {
131 * Check device by its physical USB bus/port address.
133 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
134 if (strcmp(sdi->connection_id, connection_id))
135 /* This is not the one. */
139 if (!(err = libusb_open(devlist[i], &usb->devhdl))) {
140 if (usb->address == 0xff)
142 * first time we touch this device after firmware upload,
143 * so we don't know the address yet.
145 usb->address = libusb_get_device_address(devlist[i]);
147 if (!(devc->epin_maxpacketsize = dso_getmps(devlist[i])))
148 sr_err("Wrong endpoint profile.");
150 sdi->status = SR_ST_ACTIVE;
151 sr_info("Opened device on %d.%d (logical) / "
152 "%s (physical) interface %d.",
153 usb->bus, usb->address,
154 sdi->connection_id, USB_INTERFACE);
157 sr_err("Failed to open device: %s.",
158 libusb_error_name(err));
161 /* If we made it here, we handled the device (somehow). */
164 libusb_free_device_list(devlist, 1);
166 if (sdi->status != SR_ST_ACTIVE)
172 SR_PRIV void dso_close(struct sr_dev_inst *sdi)
174 struct sr_usb_dev_inst *usb;
181 sr_info("Closing device on %d.%d (logical) / %s (physical) interface %d.",
182 usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
183 libusb_release_interface(usb->devhdl, USB_INTERFACE);
184 libusb_close(usb->devhdl);
186 sdi->status = SR_ST_INACTIVE;
190 static int get_channel_offsets(const struct sr_dev_inst *sdi)
192 struct dev_context *devc;
193 struct sr_usb_dev_inst *usb;
197 sr_dbg("Getting channel offsets.");
202 ret = libusb_control_transfer(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: %s.",
209 libusb_error_name(ret));
213 /* Comes in as 16-bit numbers with the second byte always 0 on
214 * the DSO-2090. Guessing this is supposed to be big-endian,
215 * since that's how voltage offsets are submitted back to the DSO.
216 * Convert to host order now, so we can use them natively.
218 for (chan = 0; chan < NUM_CHANNELS; chan++) {
219 for (v = 0; v < 9; v++) {
220 devc->channel_levels[chan][v][0] =
221 g_ntohs(devc->channel_levels[chan][v][0]);
222 devc->channel_levels[chan][v][1] =
223 g_ntohs(devc->channel_levels[chan][v][1]);
227 if (sr_log_loglevel_get() >= SR_LOG_DBG) {
228 gs = g_string_sized_new(128);
229 for (chan = 0; chan < NUM_CHANNELS; chan++) {
230 g_string_printf(gs, "CH%d:", chan + 1);
231 for (v = 0; v < 9; v++) {
232 g_string_append_printf(gs, " %.4x-%.4x",
233 devc->channel_levels[chan][v][0],
234 devc->channel_levels[chan][v][1]);
236 sr_dbg("%s", gs->str);
238 g_string_free(gs, TRUE);
244 /* See http://openhantek.sourceforge.net/doc/namespaceHantek.html#ac1cd181814cf3da74771c29800b39028 */
245 static int dso2250_set_trigger_samplerate(const struct sr_dev_inst *sdi)
247 struct dev_context *devc;
248 struct sr_usb_dev_inst *usb;
251 uint8_t cmdstring[12];
257 memset(cmdstring, 0, sizeof(cmdstring));
259 cmdstring[0] = CMD_2250_SET_TRIGGERSOURCE;
260 sr_dbg("Trigger source %s.", devc->triggersource);
261 if (!strcmp("CH2", devc->triggersource))
263 else if (!strcmp("CH1", devc->triggersource))
265 else if (!strcmp("EXT", devc->triggersource))
268 sr_err("Invalid trigger source: '%s'.", devc->triggersource);
274 sr_dbg("Trigger slope: %d.", devc->triggerslope);
275 cmdstring[2] |= (devc->triggerslope == SLOPE_NEGATIVE ? 1 : 0) << 3;
277 if (send_begin(sdi) != SR_OK)
280 if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT,
281 cmdstring, 8, &tmp, 100)) != 0) {
282 sr_err("Failed to set trigger/samplerate: %s.",
283 libusb_error_name(ret));
289 sr_dbg("Frame size: %d.", devc->framesize);
290 cmdstring[0] = CMD_2250_SET_RECORD_LENGTH;
291 cmdstring[2] = devc->framesize == FRAMESIZE_SMALL ? 0x01 : 0x02;
293 if (send_begin(sdi) != SR_OK)
296 if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT,
297 cmdstring, 4, &tmp, 100)) != 0) {
298 sr_err("Failed to set record length: %s.",
299 libusb_error_name(ret));
304 memset(cmdstring, 0, sizeof(cmdstring));
305 cmdstring[0] = CMD_2250_SET_SAMPLERATE;
306 sr_dbg("Sample rate: %u", devc->samplerate);
308 if (devc->samplerate > base) {
310 sr_err("Sample rate > 100MHz not yet supported.");
314 tmp = base / devc->samplerate;
315 sr_dbg("sample rate value: %d.", devc->samplerate);
317 /* Downsampling on */
319 /* Downsampler = 1comp((Base / Samplerate) - 2)
320 * Base == 100Msa resp. 200MSa
322 * Example for 500kSa/s:
323 * 100e6 / 500e3 => 200
325 * 1comp(198) => ff39 */
328 sr_dbg("down sampler value: 0x%x.", tmp & 0xffff);
329 cmdstring[4] = (tmp >> 0) & 0xff;
330 cmdstring[5] = (tmp >> 8) & 0xff;
333 if (send_begin(sdi) != SR_OK)
336 if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT,
337 cmdstring, 8, &tmp, 100)) != 0) {
338 sr_err("Failed to set sample rate: %s.",
339 libusb_error_name(ret));
342 sr_dbg("Sent CMD_2250_SET_SAMPLERATE.");
345 /* Enabled channels: 00=CH1 01=CH2 10=both */
346 memset(cmdstring, 0, sizeof(cmdstring));
347 cmdstring[0] = CMD_2250_SET_CHANNELS;
348 sr_dbg("Channels CH1=%d CH2=%d", devc->ch_enabled[0], devc->ch_enabled[1]);
349 cmdstring[2] = (devc->ch_enabled[0] ? 0 : 1) + (devc->ch_enabled[1] ? 2 : 0);
351 if (send_begin(sdi) != SR_OK)
354 if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT,
355 cmdstring, 4, &tmp, 100)) != 0) {
356 sr_err("Failed to set channels: %s.",
357 libusb_error_name(ret));
360 sr_dbg("Sent CMD_2250_SET_CHANNELS.");
364 /* Trigger slope: 0=positive 1=negative */
365 memset(cmdstring, 0, sizeof(cmdstring));
366 cmdstring[0] = CMD_2250_SET_TRIGGERPOS_AND_BUFFER;
368 /* TODO for big buffer */
369 sr_dbg("Trigger position: %3.2f.", devc->triggerposition);
370 // tmp = 0x77fff + 0x8000 * devc->triggerposition;
371 // cmdstring[6] = tmp & 0xff;
372 // cmdstring[7] = (tmp >> 8) & 0xff;
373 // cmdstring[10] = (tmp >> 16) & 0xff;
383 if (send_begin(sdi) != SR_OK)
386 /* TODO: 12 bytes according to documentation? */
387 if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT,
388 cmdstring, 10, &tmp, 100)) != 0) {
389 sr_err("Failed to set trigger position: %s.",
390 libusb_error_name(ret));
397 int dso_set_trigger_samplerate(const struct sr_dev_inst *sdi)
399 struct dev_context *devc;
400 struct sr_usb_dev_inst *usb;
402 uint8_t cmdstring[12];
403 uint16_t timebase_small[] = { 0xffff, 0xfffc, 0xfff7, 0xffe8, 0xffce,
404 0xff9c, 0xff07, 0xfe0d, 0xfc19, 0xf63d, 0xec79, 0xd8f1 };
405 uint16_t timebase_large[] = { 0xffff, 0x0000, 0xfffc, 0xfff7, 0xffe8,
406 0xffce, 0xff9d, 0xff07, 0xfe0d, 0xfc19, 0xf63d, 0xec79 };
409 if (devc->profile->fw_pid == 0x2250)
410 return dso2250_set_trigger_samplerate(sdi);
412 sr_dbg("Preparing CMD_SET_TRIGGER_SAMPLERATE.");
416 memset(cmdstring, 0, sizeof(cmdstring));
418 cmdstring[0] = CMD_SET_TRIGGER_SAMPLERATE;
421 sr_dbg("Trigger source %s.", devc->triggersource);
422 if (!strcmp("CH2", devc->triggersource))
424 else if (!strcmp("CH1", devc->triggersource))
426 else if (!strcmp("EXT", devc->triggersource))
429 sr_err("Invalid trigger source: '%s'.", devc->triggersource);
435 sr_dbg("Frame size: %d.", devc->framesize);
436 cmdstring[2] |= (devc->framesize == FRAMESIZE_SMALL ? 0x01 : 0x02) << 2;
439 sr_dbg("Time base index: %d.", devc->timebase);
440 if (devc->framesize == FRAMESIZE_SMALL) {
441 if (devc->timebase < TIME_20us)
443 else if (devc->timebase == TIME_20us)
445 else if (devc->timebase == TIME_40us)
447 else if (devc->timebase == TIME_100us)
449 else if (devc->timebase >= TIME_200us)
452 if (devc->timebase < TIME_40us) {
453 sr_err("Timebase < 40us only supported with 10K buffer.");
456 else if (devc->timebase == TIME_40us)
458 else if (devc->timebase == TIME_100us)
460 else if (devc->timebase == TIME_200us)
462 else if (devc->timebase >= TIME_400us)
465 cmdstring[2] |= (tmp & 0x07) << 5;
467 /* Enabled channels: 00=CH1 01=CH2 10=both */
468 sr_dbg("Channels CH1=%d CH2=%d", devc->ch_enabled[0], devc->ch_enabled[1]);
469 tmp = (((devc->ch_enabled[1] ? 1 : 0) << 1) + (devc->ch_enabled[0] ? 1 : 0)) - 1;
472 /* Fast rates channel */
473 /* TODO: Is this right? */
474 tmp = devc->timebase < TIME_10us ? 1 : 0;
475 cmdstring[3] |= tmp << 2;
477 /* Trigger slope: 0=positive 1=negative */
478 /* TODO: Does this work? */
479 sr_dbg("Trigger slope: %d.", devc->triggerslope);
480 cmdstring[3] |= (devc->triggerslope == SLOPE_NEGATIVE ? 1 : 0) << 3;
483 if (devc->timebase < TIME_100us)
485 else if (devc->timebase > TIME_400ms)
488 if (devc->framesize == FRAMESIZE_SMALL)
489 tmp = timebase_small[devc->timebase - 3];
491 tmp = timebase_large[devc->timebase - 3];
493 cmdstring[4] = tmp & 0xff;
494 cmdstring[5] = (tmp >> 8) & 0xff;
496 /* Horizontal trigger position */
497 sr_dbg("Trigger position: %3.2f.", devc->triggerposition);
498 tmp = 0x77fff + 0x8000 * devc->triggerposition;
499 cmdstring[6] = tmp & 0xff;
500 cmdstring[7] = (tmp >> 8) & 0xff;
501 cmdstring[10] = (tmp >> 16) & 0xff;
503 if (send_begin(sdi) != SR_OK)
506 if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT,
507 cmdstring, sizeof(cmdstring), &tmp, 100)) != 0) {
508 sr_err("Failed to set trigger/samplerate: %s.",
509 libusb_error_name(ret));
512 sr_dbg("Sent CMD_SET_TRIGGER_SAMPLERATE.");
518 static int dso_set_filters(const struct sr_dev_inst *sdi)
520 struct dev_context *devc;
521 struct sr_usb_dev_inst *usb;
523 uint8_t cmdstring[8];
525 sr_dbg("Preparing CMD_SET_FILTERS.");
530 memset(cmdstring, 0, sizeof(cmdstring));
531 cmdstring[0] = CMD_SET_FILTERS;
533 if (devc->filter[0]) {
534 sr_dbg("Turning on CH1 filter.");
535 cmdstring[2] |= 0x80;
537 if (devc->filter[1]) {
538 sr_dbg("Turning on CH2 filter.");
539 cmdstring[2] |= 0x40;
542 * Not supported: filtering on the trigger
543 * cmdstring[2] |= 0x20;
546 if (send_begin(sdi) != SR_OK)
549 if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT,
550 cmdstring, sizeof(cmdstring), &tmp, 100)) != 0) {
551 sr_err("Failed to set filters: %s.", libusb_error_name(ret));
554 sr_dbg("Sent CMD_SET_FILTERS.");
559 static int dso_set_voltage(const struct sr_dev_inst *sdi)
561 struct dev_context *devc;
562 struct sr_usb_dev_inst *usb;
564 uint8_t cmdstring[8];
566 sr_dbg("Preparing CMD_SET_VOLTAGE.");
571 memset(cmdstring, 0, sizeof(cmdstring));
572 cmdstring[0] = CMD_SET_VOLTAGE;
574 if (devc->profile->fw_pid == 0x2250) {
581 /* CH1 volts/div is encoded in bits 0-1 */
582 sr_dbg("CH1 vdiv index: %d.", devc->voltage[0]);
583 switch (devc->voltage[0]) {
587 cmdstring[2] |= 0x00;
592 cmdstring[2] |= 0x01;
597 cmdstring[2] |= 0x02;
601 /* CH2 volts/div is encoded in bits 2-3 */
602 sr_dbg("CH2 vdiv index: %d.", devc->voltage[1]);
603 switch (devc->voltage[1]) {
607 cmdstring[2] |= 0x00;
612 cmdstring[2] |= 0x04;
617 cmdstring[2] |= 0x08;
621 if (send_begin(sdi) != SR_OK)
624 if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT,
625 cmdstring, sizeof(cmdstring), &tmp, 100)) != 0) {
626 sr_err("Failed to set voltage: %s.", libusb_error_name(ret));
629 sr_dbg("Sent CMD_SET_VOLTAGE.");
634 static int dso_set_relays(const struct sr_dev_inst *sdi)
636 struct dev_context *devc;
637 struct sr_usb_dev_inst *usb;
640 uint8_t relays[17] = { 0x00, 0x04, 0x08, 0x02, 0x20, 0x40, 0x10, 0x01,
641 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
643 sr_dbg("Preparing CTRL_SETRELAYS.");
648 if (devc->voltage[0] < VDIV_1V)
649 relays[1] = ~relays[1];
651 if (devc->voltage[0] < VDIV_100MV)
652 relays[2] = ~relays[2];
654 sr_dbg("CH1 coupling: %d.", devc->coupling[0]);
655 if (devc->coupling[0] != COUPLING_AC)
656 relays[3] = ~relays[3];
658 if (devc->voltage[1] < VDIV_1V)
659 relays[4] = ~relays[4];
661 if (devc->voltage[1] < VDIV_100MV)
662 relays[5] = ~relays[5];
664 sr_dbg("CH2 coupling: %d.", devc->coupling[1]);
665 if (devc->coupling[1] != COUPLING_AC)
666 relays[6] = ~relays[6];
668 if (!strcmp(devc->triggersource, "EXT"))
669 relays[7] = ~relays[7];
671 if (sr_log_loglevel_get() >= SR_LOG_DBG) {
672 gs = g_string_sized_new(128);
673 g_string_printf(gs, "Relays:");
674 for (i = 0; i < 17; i++)
675 g_string_append_printf(gs, " %.2x", relays[i]);
676 sr_dbg("%s", gs->str);
677 g_string_free(gs, TRUE);
680 if ((ret = libusb_control_transfer(usb->devhdl,
681 LIBUSB_REQUEST_TYPE_VENDOR, CTRL_SETRELAYS,
682 0, 0, relays, 17, 100)) != sizeof(relays)) {
683 sr_err("Failed to set relays: %s.", libusb_error_name(ret));
686 sr_dbg("Sent CTRL_SETRELAYS.");
691 static int dso_set_voffsets(const struct sr_dev_inst *sdi)
693 struct dev_context *devc;
694 struct sr_usb_dev_inst *usb;
699 sr_dbg("Preparing CTRL_SETOFFSET.");
704 memset(offsets, 0, sizeof(offsets));
706 ch_levels = devc->channel_levels[0][devc->voltage[0]];
707 offset = (ch_levels[1] - ch_levels[0]) * devc->voffset_ch1 + ch_levels[0];
708 offsets[0] = (offset >> 8) | 0x20;
709 offsets[1] = offset & 0xff;
710 sr_dbg("CH1 offset: %3.2f (%.2x%.2x).", devc->voffset_ch1,
711 offsets[0], offsets[1]);
714 ch_levels = devc->channel_levels[1][devc->voltage[1]];
715 offset = (ch_levels[1] - ch_levels[0]) * devc->voffset_ch2 + ch_levels[0];
716 offsets[2] = (offset >> 8) | 0x20;
717 offsets[3] = offset & 0xff;
718 sr_dbg("CH2 offset: %3.2f (%.2x%.2x).", devc->voffset_ch2,
719 offsets[2], offsets[3]);
722 offset = MAX_VERT_TRIGGER * devc->voffset_trigger;
723 offsets[4] = (offset >> 8) | 0x20;
724 offsets[5] = offset & 0xff;
725 sr_dbg("Trigger offset: %3.2f (%.2x%.2x).", devc->voffset_trigger,
726 offsets[4], offsets[5]);
728 if ((ret = libusb_control_transfer(usb->devhdl,
729 LIBUSB_REQUEST_TYPE_VENDOR, CTRL_SETOFFSET,
730 0, 0, offsets, sizeof(offsets), 100)) != sizeof(offsets)) {
731 sr_err("Failed to set offsets: %s.", libusb_error_name(ret));
734 sr_dbg("Sent CTRL_SETOFFSET.");
739 SR_PRIV int dso_enable_trigger(const struct sr_dev_inst *sdi)
741 struct sr_usb_dev_inst *usb;
743 uint8_t cmdstring[2];
745 sr_dbg("Sending CMD_ENABLE_TRIGGER.");
749 memset(cmdstring, 0, sizeof(cmdstring));
750 cmdstring[0] = CMD_ENABLE_TRIGGER;
753 if (send_begin(sdi) != SR_OK)
756 if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT,
757 cmdstring, sizeof(cmdstring), &tmp, 100)) != 0) {
758 sr_err("Failed to enable trigger: %s.", libusb_error_name(ret));
765 SR_PRIV int dso_force_trigger(const struct sr_dev_inst *sdi)
767 struct sr_usb_dev_inst *usb;
769 uint8_t cmdstring[2];
771 sr_dbg("Sending CMD_FORCE_TRIGGER.");
775 memset(cmdstring, 0, sizeof(cmdstring));
776 cmdstring[0] = CMD_FORCE_TRIGGER;
779 if (send_begin(sdi) != SR_OK)
782 if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT,
783 cmdstring, sizeof(cmdstring), &tmp, 100)) != 0) {
784 sr_err("Failed to force trigger: %s.", libusb_error_name(ret));
791 SR_PRIV int dso_init(const struct sr_dev_inst *sdi)
794 sr_dbg("Initializing DSO.");
796 if (get_channel_offsets(sdi) != SR_OK)
799 if (dso_set_trigger_samplerate(sdi) != SR_OK)
802 if (dso_set_filters(sdi) != SR_OK)
805 if (dso_set_voltage(sdi) != SR_OK)
808 if (dso_set_relays(sdi) != SR_OK)
811 if (dso_set_voffsets(sdi) != SR_OK)
814 if (dso_enable_trigger(sdi) != SR_OK)
820 SR_PRIV int dso_get_capturestate(const struct sr_dev_inst *sdi,
821 uint8_t *capturestate, uint32_t *trigger_offset)
823 struct sr_usb_dev_inst *usb;
825 unsigned int bitvalue, toff;
826 uint8_t cmdstring[2], inbuf[512];
828 sr_dbg("Sending CMD_GET_CAPTURESTATE.");
832 cmdstring[0] = CMD_GET_CAPTURESTATE;
835 if ((ret = send_bulkcmd(sdi, cmdstring, sizeof(cmdstring))) != SR_OK) {
836 sr_dbg("Failed to send get_capturestate command: %s.",
837 libusb_error_name(ret));
841 if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_IN,
842 inbuf, 512, &tmp, 100)) != 0) {
843 sr_dbg("Failed to get capturestate: %s.",
844 libusb_error_name(ret));
847 *capturestate = inbuf[0];
848 toff = (inbuf[1] << 16) | (inbuf[3] << 8) | inbuf[2];
851 * This conversion comes from the openhantek project.
852 * Each set bit in the 24-bit value inverts all bits with a lower
853 * value. No idea why the device reports the trigger point this way.
856 for (i = 0; i < 24; i++) {
857 /* Each set bit inverts all bits with a lower value. */
859 toff ^= bitvalue - 1;
862 *trigger_offset = toff;
867 SR_PRIV int dso_capture_start(const struct sr_dev_inst *sdi)
870 uint8_t cmdstring[2];
872 sr_dbg("Sending CMD_CAPTURE_START.");
874 cmdstring[0] = CMD_CAPTURE_START;
877 if ((ret = send_bulkcmd(sdi, cmdstring, sizeof(cmdstring))) != SR_OK) {
878 sr_err("Failed to send capture_start command: %s.",
879 libusb_error_name(ret));
886 SR_PRIV int dso_get_channeldata(const struct sr_dev_inst *sdi,
887 libusb_transfer_cb_fn cb)
889 struct dev_context *devc;
890 struct sr_usb_dev_inst *usb;
891 struct libusb_transfer *transfer;
892 int num_transfers, ret, i;
893 uint8_t cmdstring[2];
896 sr_dbg("Sending CMD_GET_CHANNELDATA.");
901 cmdstring[0] = CMD_GET_CHANNELDATA;
904 if ((ret = send_bulkcmd(sdi, cmdstring, sizeof(cmdstring))) != SR_OK) {
905 sr_err("Failed to get channel data: %s.",
906 libusb_error_name(ret));
910 /* TODO: DSO-2xxx only. */
911 num_transfers = devc->framesize *
912 sizeof(unsigned short) / devc->epin_maxpacketsize;
913 sr_dbg("Queueing up %d transfers.", num_transfers);
914 for (i = 0; i < num_transfers; i++) {
915 if (!(buf = g_try_malloc(devc->epin_maxpacketsize))) {
916 sr_err("Failed to malloc USB endpoint buffer.");
917 return SR_ERR_MALLOC;
919 transfer = libusb_alloc_transfer(0);
920 libusb_fill_bulk_transfer(transfer, usb->devhdl, DSO_EP_IN, buf,
921 devc->epin_maxpacketsize, cb, (void *)sdi, 40);
922 if ((ret = libusb_submit_transfer(transfer)) != 0) {
923 sr_err("Failed to submit transfer: %s.",
924 libusb_error_name(ret));
925 /* TODO: Free them all. */
926 libusb_free_transfer(transfer);