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 if (usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)) < 0)
136 if (strcmp(sdi->connection_id, connection_id))
137 /* This is not the one. */
141 if (!(err = libusb_open(devlist[i], &usb->devhdl))) {
142 if (usb->address == 0xff)
144 * first time we touch this device after firmware upload,
145 * so we don't know the address yet.
147 usb->address = libusb_get_device_address(devlist[i]);
149 if (!(devc->epin_maxpacketsize = dso_getmps(devlist[i])))
150 sr_err("Wrong endpoint profile.");
152 sdi->status = SR_ST_ACTIVE;
153 sr_info("Opened device on %d.%d (logical) / "
154 "%s (physical) interface %d.",
155 usb->bus, usb->address,
156 sdi->connection_id, USB_INTERFACE);
159 sr_err("Failed to open device: %s.",
160 libusb_error_name(err));
163 /* If we made it here, we handled the device (somehow). */
166 libusb_free_device_list(devlist, 1);
168 if (sdi->status != SR_ST_ACTIVE)
174 SR_PRIV void dso_close(struct sr_dev_inst *sdi)
176 struct sr_usb_dev_inst *usb;
183 sr_info("Closing device on %d.%d (logical) / %s (physical) interface %d.",
184 usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
185 libusb_release_interface(usb->devhdl, USB_INTERFACE);
186 libusb_close(usb->devhdl);
188 sdi->status = SR_ST_INACTIVE;
191 static int get_channel_offsets(const struct sr_dev_inst *sdi)
193 struct dev_context *devc;
194 struct sr_usb_dev_inst *usb;
198 sr_dbg("Getting channel offsets.");
203 ret = libusb_control_transfer(usb->devhdl,
204 LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR,
205 CTRL_READ_EEPROM, EEPROM_CHANNEL_OFFSETS, 0,
206 (unsigned char *)&devc->channel_levels,
207 sizeof(devc->channel_levels), 200);
208 if (ret != sizeof(devc->channel_levels)) {
209 sr_err("Failed to get channel offsets: %s.",
210 libusb_error_name(ret));
214 /* Comes in as 16-bit numbers with the second byte always 0 on
215 * the DSO-2090. Guessing this is supposed to be big-endian,
216 * since that's how voltage offsets are submitted back to the DSO.
217 * Convert to host order now, so we can use them natively.
219 for (chan = 0; chan < NUM_CHANNELS; chan++) {
220 for (v = 0; v < 9; v++) {
221 devc->channel_levels[chan][v][0] =
222 g_ntohs(devc->channel_levels[chan][v][0]);
223 devc->channel_levels[chan][v][1] =
224 g_ntohs(devc->channel_levels[chan][v][1]);
228 if (sr_log_loglevel_get() >= SR_LOG_DBG) {
229 gs = g_string_sized_new(128);
230 for (chan = 0; chan < NUM_CHANNELS; chan++) {
231 g_string_printf(gs, "CH%d:", chan + 1);
232 for (v = 0; v < 9; v++) {
233 g_string_append_printf(gs, " %.4x-%.4x",
234 devc->channel_levels[chan][v][0],
235 devc->channel_levels[chan][v][1]);
237 sr_dbg("%s", gs->str);
239 g_string_free(gs, TRUE);
245 static void dso2250_set_triggerpos(int value, int long_buffer, uint8_t dest[], int offset)
250 min = long_buffer ? 0 : 0x7d7ff;
254 tmp = min + diff * value / 100;
255 sr_dbg("2250 trigger pos: %3d%% * [0x%x,0x%x] == 0x%x", value, min, max, tmp);
257 dest[offset + 0] = tmp & 0xff;
258 dest[offset + 1] = (tmp >> 8) & 0xff;
259 dest[offset + 2] = (tmp >> 16) & 0x7;
262 /* See http://openhantek.sourceforge.net/doc/namespaceHantek.html#ac1cd181814cf3da74771c29800b39028 */
263 static int dso2250_set_trigger_samplerate(const struct sr_dev_inst *sdi)
265 struct dev_context *devc;
266 struct sr_usb_dev_inst *usb;
269 uint8_t cmdstring[12];
275 memset(cmdstring, 0, sizeof(cmdstring));
277 cmdstring[0] = CMD_2250_SET_TRIGGERSOURCE;
278 sr_dbg("Trigger source %s.", devc->triggersource);
279 if (!strcmp("CH2", devc->triggersource))
281 else if (!strcmp("CH1", devc->triggersource))
283 else if (!strcmp("EXT", devc->triggersource) || !strcmp("forced", devc->triggersource))
286 sr_err("Invalid trigger source: '%s'.", devc->triggersource);
291 sr_dbg("Trigger slope: %d.", devc->triggerslope);
292 cmdstring[2] |= (devc->triggerslope == SLOPE_NEGATIVE ? 1 : 0) << 3;
294 if (send_begin(sdi) != SR_OK)
297 if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT,
298 cmdstring, 8, &tmp, 100)) != 0) {
299 sr_err("Failed to set trigger/samplerate: %s.",
300 libusb_error_name(ret));
305 sr_dbg("Frame size: %d.", devc->framesize);
306 cmdstring[0] = CMD_2250_SET_RECORD_LENGTH;
307 cmdstring[2] = (devc->framesize == FRAMESIZE_SMALL) ? 0x01 : 0x02;
309 if (send_begin(sdi) != SR_OK)
312 if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT,
313 cmdstring, 4, &tmp, 100)) != 0) {
314 sr_err("Failed to set record length: %s.",
315 libusb_error_name(ret));
319 memset(cmdstring, 0, sizeof(cmdstring));
320 cmdstring[0] = CMD_2250_SET_SAMPLERATE;
322 if (devc->samplerate > base) {
324 sr_err("Sample rate > 100MHz not yet supported.");
328 tmp = base / devc->samplerate;
329 /* Downsample only if really necessary */
331 /* Downsampling on */
333 /* Downsampler = 1comp((Base / Samplerate) - 2)
334 * Base == 100Msa resp. 200MSa
336 * Example for 500kSa/s:
337 * 100e6 / 500e3 => 200
339 * 1comp(198) => ff39 */
342 sr_dbg("Down sampler value: 0x%x.", tmp & 0xffff);
343 cmdstring[4] = (tmp >> 0) & 0xff;
344 cmdstring[5] = (tmp >> 8) & 0xff;
347 if (send_begin(sdi) != SR_OK)
350 if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT,
351 cmdstring, 8, &tmp, 100)) != 0) {
352 sr_err("Failed to set sample rate: %s.",
353 libusb_error_name(ret));
356 sr_dbg("Sent CMD_2250_SET_SAMPLERATE.");
358 /* Enabled channels: 00=CH1, 01=CH2, 10=both. */
359 memset(cmdstring, 0, sizeof(cmdstring));
360 cmdstring[0] = CMD_2250_SET_CHANNELS;
361 sr_dbg("Channels: CH1=%d, CH2=%d.", devc->ch_enabled[0], devc->ch_enabled[1]);
362 cmdstring[2] = (devc->ch_enabled[0] ? 0 : 1) + (devc->ch_enabled[1] ? 2 : 0);
364 if (send_begin(sdi) != SR_OK)
367 if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT,
368 cmdstring, 4, &tmp, 100)) != 0) {
369 sr_err("Failed to set channels: %s.",
370 libusb_error_name(ret));
373 sr_dbg("Sent CMD_2250_SET_CHANNELS.");
375 /* Trigger slope: 0=positive, 1=negative. */
376 memset(cmdstring, 0, sizeof(cmdstring));
377 cmdstring[0] = CMD_2250_SET_TRIGGERPOS_AND_BUFFER;
379 sr_dbg("Capture ratio: %" PRIu64 ".", devc->capture_ratio);
380 trig = devc->capture_ratio;
381 dso2250_set_triggerpos(trig,
382 devc->framesize != FRAMESIZE_SMALL, cmdstring, 2);
383 dso2250_set_triggerpos(100 - trig,
384 devc->framesize != FRAMESIZE_SMALL, cmdstring, 6);
386 if (send_begin(sdi) != SR_OK)
389 /* TODO: 12 bytes according to documentation? */
390 if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT,
391 cmdstring, 10, &tmp, 100)) != 0) {
392 sr_err("Failed to set trigger position: %s.",
393 libusb_error_name(ret));
400 SR_PRIV int dso_set_trigger_samplerate(const struct sr_dev_inst *sdi)
402 struct dev_context *devc;
403 struct sr_usb_dev_inst *usb;
405 uint8_t cmdstring[12];
406 uint16_t timebase_small[] = { 0xffff, 0xfffc, 0xfff7, 0xffe8, 0xffce,
407 0xff9c, 0xff07, 0xfe0d, 0xfc19, 0xf63d, 0xec79, 0xd8f1 };
408 uint16_t timebase_large[] = { 0xffff, 0x0000, 0xfffc, 0xfff7, 0xffe8,
409 0xffce, 0xff9d, 0xff07, 0xfe0d, 0xfc19, 0xf63d, 0xec79 };
412 if (devc->profile->fw_pid == 0x2250)
413 return dso2250_set_trigger_samplerate(sdi);
415 sr_dbg("Preparing CMD_SET_TRIGGER_SAMPLERATE.");
419 memset(cmdstring, 0, sizeof(cmdstring));
421 cmdstring[0] = CMD_SET_TRIGGER_SAMPLERATE;
424 sr_dbg("Trigger source %s.", devc->triggersource);
425 if (!strcmp("CH2", devc->triggersource))
427 else if (!strcmp("CH1", devc->triggersource))
429 else if (!strcmp("EXT", devc->triggersource) || !strcmp("forced", devc->triggersource))
432 sr_err("Invalid trigger source: '%s'.", devc->triggersource);
438 sr_dbg("Frame size: %d.", devc->framesize);
439 cmdstring[2] |= (devc->framesize == FRAMESIZE_SMALL ? 0x01 : 0x02) << 2;
442 sr_dbg("Time base index: %d.", devc->timebase);
443 if (devc->framesize == FRAMESIZE_SMALL) {
444 if (devc->timebase < TIME_20us)
446 else if (devc->timebase == TIME_20us)
448 else if (devc->timebase == TIME_40us)
450 else if (devc->timebase == TIME_100us)
452 else if (devc->timebase >= TIME_200us)
455 if (devc->timebase < TIME_40us) {
456 sr_err("Timebase < 40us only supported with 10K buffer.");
459 else if (devc->timebase == TIME_40us)
461 else if (devc->timebase == TIME_100us)
463 else if (devc->timebase == TIME_200us)
465 else if (devc->timebase >= TIME_400us)
468 cmdstring[2] |= (tmp & 0x07) << 5;
470 /* Enabled channels: 00=CH1 01=CH2 10=both */
471 sr_dbg("Channels CH1=%d CH2=%d", devc->ch_enabled[0], devc->ch_enabled[1]);
472 tmp = (((devc->ch_enabled[1] ? 1 : 0) << 1) + (devc->ch_enabled[0] ? 1 : 0)) - 1;
475 /* Fast rates channel */
476 /* TODO: Is this right? */
477 tmp = devc->timebase < TIME_10us ? 1 : 0;
478 cmdstring[3] |= tmp << 2;
480 /* Trigger slope: 0=positive 1=negative */
481 /* TODO: Does this work? */
482 sr_dbg("Trigger slope: %d.", devc->triggerslope);
483 cmdstring[3] |= (devc->triggerslope == SLOPE_NEGATIVE ? 1 : 0) << 3;
486 if (devc->timebase < TIME_100us)
488 else if (devc->timebase > TIME_400ms)
491 if (devc->framesize == FRAMESIZE_SMALL)
492 tmp = timebase_small[devc->timebase - 3];
494 tmp = timebase_large[devc->timebase - 3];
496 cmdstring[4] = tmp & 0xff;
497 cmdstring[5] = (tmp >> 8) & 0xff;
499 /* Horizontal trigger position */
500 sr_dbg("Capture ratio: %" PRIu64 ".", devc->capture_ratio);
501 tmp = 0x77fff + 0x8000 * devc->capture_ratio / 100;
502 cmdstring[6] = tmp & 0xff;
503 cmdstring[7] = (tmp >> 8) & 0xff;
504 cmdstring[10] = (tmp >> 16) & 0xff;
506 if (send_begin(sdi) != SR_OK)
509 if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT,
510 cmdstring, sizeof(cmdstring), &tmp, 100)) != 0) {
511 sr_err("Failed to set trigger/samplerate: %s.",
512 libusb_error_name(ret));
515 sr_dbg("Sent CMD_SET_TRIGGER_SAMPLERATE.");
520 static int dso_set_filters(const struct sr_dev_inst *sdi)
522 struct dev_context *devc;
523 struct sr_usb_dev_inst *usb;
525 uint8_t cmdstring[8];
527 sr_dbg("Preparing CMD_SET_FILTERS.");
532 memset(cmdstring, 0, sizeof(cmdstring));
533 cmdstring[0] = CMD_SET_FILTERS;
535 if (devc->filter[0]) {
536 sr_dbg("Turning on CH1 filter.");
537 cmdstring[2] |= 0x80;
539 if (devc->filter[1]) {
540 sr_dbg("Turning on CH2 filter.");
541 cmdstring[2] |= 0x40;
544 * Not supported: filtering on the trigger
545 * cmdstring[2] |= 0x20;
548 if (send_begin(sdi) != SR_OK)
551 if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT,
552 cmdstring, sizeof(cmdstring), &tmp, 100)) != 0) {
553 sr_err("Failed to set filters: %s.", libusb_error_name(ret));
556 sr_dbg("Sent CMD_SET_FILTERS.");
561 static int dso_set_voltage(const struct sr_dev_inst *sdi)
563 struct dev_context *devc;
564 struct sr_usb_dev_inst *usb;
566 uint8_t cmdstring[8];
568 sr_dbg("Preparing CMD_SET_VOLTAGE.");
573 memset(cmdstring, 0, sizeof(cmdstring));
574 cmdstring[0] = CMD_SET_VOLTAGE;
576 if (devc->profile->fw_pid == 0x2250) {
584 /* CH1 volts/div is encoded in bits 0-1. */
585 sr_dbg("CH1 vdiv index: %d.", devc->voltage[0]);
586 switch (devc->voltage[0]) {
590 cmdstring[2] |= 0x00;
595 cmdstring[2] |= 0x01;
600 cmdstring[2] |= 0x02;
604 /* CH2 volts/div is encoded in bits 2-3. */
605 sr_dbg("CH2 vdiv index: %d.", devc->voltage[1]);
606 switch (devc->voltage[1]) {
610 cmdstring[2] |= 0x00;
615 cmdstring[2] |= 0x04;
620 cmdstring[2] |= 0x08;
624 if (send_begin(sdi) != SR_OK)
627 if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT,
628 cmdstring, sizeof(cmdstring), &tmp, 100)) != 0) {
629 sr_err("Failed to set voltage: %s.", libusb_error_name(ret));
632 sr_dbg("Sent CMD_SET_VOLTAGE.");
637 static int dso_set_relays(const struct sr_dev_inst *sdi)
639 struct dev_context *devc;
640 struct sr_usb_dev_inst *usb;
643 uint8_t relays[17] = { 0x00, 0x04, 0x08, 0x02, 0x20, 0x40, 0x10, 0x01,
644 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
646 sr_dbg("Preparing CTRL_SETRELAYS.");
651 if (devc->voltage[0] < VDIV_1V)
652 relays[1] = ~relays[1];
654 if (devc->voltage[0] < VDIV_100MV)
655 relays[2] = ~relays[2];
657 sr_dbg("CH1 coupling: %d.", devc->coupling[0]);
658 if (devc->coupling[0] != COUPLING_AC)
659 relays[3] = ~relays[3];
661 if (devc->voltage[1] < VDIV_1V)
662 relays[4] = ~relays[4];
664 if (devc->voltage[1] < VDIV_100MV)
665 relays[5] = ~relays[5];
667 sr_dbg("CH2 coupling: %d.", devc->coupling[1]);
668 if (devc->coupling[1] != COUPLING_AC)
669 relays[6] = ~relays[6];
671 if (!strcmp(devc->triggersource, "EXT"))
672 relays[7] = ~relays[7];
674 if (sr_log_loglevel_get() >= SR_LOG_DBG) {
675 gs = g_string_sized_new(128);
676 g_string_printf(gs, "Relays:");
677 for (i = 0; i < 17; i++)
678 g_string_append_printf(gs, " %.2x", relays[i]);
679 sr_dbg("%s", gs->str);
680 g_string_free(gs, TRUE);
683 if ((ret = libusb_control_transfer(usb->devhdl,
684 LIBUSB_REQUEST_TYPE_VENDOR, CTRL_SETRELAYS,
685 0, 0, relays, 17, 100)) != sizeof(relays)) {
686 sr_err("Failed to set relays: %s.", libusb_error_name(ret));
689 sr_dbg("Sent CTRL_SETRELAYS.");
694 SR_PRIV int dso_set_voffsets(const struct sr_dev_inst *sdi)
696 struct dev_context *devc;
697 struct sr_usb_dev_inst *usb;
702 sr_dbg("Preparing CTRL_SETOFFSET.");
707 memset(offsets, 0, sizeof(offsets));
709 ch_levels = devc->channel_levels[0][devc->voltage[0]];
710 offset = (ch_levels[1] - ch_levels[0]) * devc->voffset_ch1 + ch_levels[0];
711 offsets[0] = (offset >> 8) | 0x20;
712 offsets[1] = offset & 0xff;
713 sr_dbg("CH1 offset: %3.2f (%.2x%.2x).", devc->voffset_ch1,
714 offsets[0], offsets[1]);
717 ch_levels = devc->channel_levels[1][devc->voltage[1]];
718 offset = (ch_levels[1] - ch_levels[0]) * devc->voffset_ch2 + ch_levels[0];
719 offsets[2] = (offset >> 8) | 0x20;
720 offsets[3] = offset & 0xff;
721 sr_dbg("CH2 offset: %3.2f (%.2x%.2x).", devc->voffset_ch2,
722 offsets[2], offsets[3]);
725 offset = MAX_VERT_TRIGGER * devc->voffset_trigger;
726 offsets[4] = (offset >> 8) | 0x20;
727 offsets[5] = offset & 0xff;
728 sr_dbg("Trigger offset: %3.2f (%.2x%.2x).", devc->voffset_trigger,
729 offsets[4], offsets[5]);
731 if ((ret = libusb_control_transfer(usb->devhdl,
732 LIBUSB_REQUEST_TYPE_VENDOR, CTRL_SETOFFSET,
733 0, 0, offsets, sizeof(offsets), 100)) != sizeof(offsets)) {
734 sr_err("Failed to set offsets: %s.", libusb_error_name(ret));
737 sr_dbg("Sent CTRL_SETOFFSET.");
742 SR_PRIV int dso_enable_trigger(const struct sr_dev_inst *sdi)
744 struct sr_usb_dev_inst *usb;
746 uint8_t cmdstring[2];
748 sr_dbg("Sending CMD_ENABLE_TRIGGER.");
752 memset(cmdstring, 0, sizeof(cmdstring));
753 cmdstring[0] = CMD_ENABLE_TRIGGER;
756 if (send_begin(sdi) != SR_OK)
759 if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT,
760 cmdstring, sizeof(cmdstring), &tmp, 100)) != 0) {
761 sr_err("Failed to enable trigger: %s.", libusb_error_name(ret));
768 SR_PRIV int dso_force_trigger(const struct sr_dev_inst *sdi)
770 struct sr_usb_dev_inst *usb;
772 uint8_t cmdstring[2];
774 sr_dbg("Sending CMD_FORCE_TRIGGER.");
778 memset(cmdstring, 0, sizeof(cmdstring));
779 cmdstring[0] = CMD_FORCE_TRIGGER;
782 if (send_begin(sdi) != SR_OK)
785 if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT,
786 cmdstring, sizeof(cmdstring), &tmp, 100)) != 0) {
787 sr_err("Failed to force trigger: %s.", libusb_error_name(ret));
794 SR_PRIV int dso_init(const struct sr_dev_inst *sdi)
796 sr_dbg("Initializing DSO.");
798 if (get_channel_offsets(sdi) != SR_OK)
801 if (dso_set_trigger_samplerate(sdi) != SR_OK)
804 if (dso_set_filters(sdi) != SR_OK)
807 if (dso_set_voltage(sdi) != SR_OK)
810 if (dso_set_relays(sdi) != SR_OK)
813 if (dso_set_voffsets(sdi) != SR_OK)
816 if (dso_enable_trigger(sdi) != SR_OK)
822 SR_PRIV int dso_get_capturestate(const struct sr_dev_inst *sdi,
823 uint8_t *capturestate, uint32_t *trigger_offset)
825 struct sr_usb_dev_inst *usb;
827 unsigned int bitvalue, toff;
828 uint8_t cmdstring[2], inbuf[512];
830 sr_dbg("Sending CMD_GET_CAPTURESTATE.");
834 cmdstring[0] = CMD_GET_CAPTURESTATE;
837 if ((ret = send_bulkcmd(sdi, cmdstring, sizeof(cmdstring))) != SR_OK) {
838 sr_dbg("Failed to send get_capturestate command: %s.",
839 libusb_error_name(ret));
843 if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_IN,
844 inbuf, 512, &tmp, 100)) != 0) {
845 sr_dbg("Failed to get capturestate: %s.",
846 libusb_error_name(ret));
849 *capturestate = inbuf[0];
850 toff = (inbuf[1] << 16) | (inbuf[3] << 8) | inbuf[2];
853 * This conversion comes from the openhantek project.
854 * Each set bit in the 24-bit value inverts all bits with a lower
855 * value. No idea why the device reports the trigger point this way.
858 for (i = 0; i < 24; i++) {
859 /* Each set bit inverts all bits with a lower value. */
861 toff ^= bitvalue - 1;
864 *trigger_offset = toff;
869 SR_PRIV int dso_capture_start(const struct sr_dev_inst *sdi)
872 uint8_t cmdstring[2];
874 sr_dbg("Sending CMD_CAPTURE_START.");
876 cmdstring[0] = CMD_CAPTURE_START;
879 if ((ret = send_bulkcmd(sdi, cmdstring, sizeof(cmdstring))) != SR_OK) {
880 sr_err("Failed to send capture_start command: %s.",
881 libusb_error_name(ret));
888 SR_PRIV int dso_get_channeldata(const struct sr_dev_inst *sdi,
889 libusb_transfer_cb_fn cb)
891 struct dev_context *devc;
892 struct sr_usb_dev_inst *usb;
893 struct libusb_transfer *transfer;
894 int num_transfers, ret, i;
895 uint8_t cmdstring[2];
898 sr_dbg("Sending CMD_GET_CHANNELDATA.");
903 cmdstring[0] = CMD_GET_CHANNELDATA;
906 if ((ret = send_bulkcmd(sdi, cmdstring, sizeof(cmdstring))) != SR_OK) {
907 sr_err("Failed to get channel data: %s.",
908 libusb_error_name(ret));
912 /* TODO: DSO-2xxx only. */
913 num_transfers = devc->framesize *
914 sizeof(unsigned short) / devc->epin_maxpacketsize;
915 sr_dbg("Queueing up %d transfers.", num_transfers);
916 for (i = 0; i < num_transfers; i++) {
917 if (!(buf = g_try_malloc(devc->epin_maxpacketsize))) {
918 sr_err("Failed to malloc USB endpoint buffer.");
919 return SR_ERR_MALLOC;
921 transfer = libusb_alloc_transfer(0);
922 libusb_fill_bulk_transfer(transfer, usb->devhdl, DSO_EP_IN, buf,
923 devc->epin_maxpacketsize, cb, (void *)sdi, 40);
924 if ((ret = libusb_submit_transfer(transfer)) != 0) {
925 sr_err("Failed to submit transfer: %s.",
926 libusb_error_name(ret));
927 /* TODO: Free them all. */
928 libusb_free_transfer(transfer);