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 #define NUM_CHANNELS 2
32 static int send_begin(const struct sr_dev_inst *sdi)
34 struct sr_usb_dev_inst *usb;
36 unsigned char buffer[] = {0x0f, 0x03, 0x03, 0x03, 0x68, 0xac, 0xfe,
39 sr_dbg("Sending CTRL_BEGINCOMMAND.");
42 if ((ret = libusb_control_transfer(usb->devhdl,
43 LIBUSB_REQUEST_TYPE_VENDOR, CTRL_BEGINCOMMAND,
44 0, 0, buffer, sizeof(buffer), 200)) != sizeof(buffer)) {
45 sr_err("Failed to send begincommand: %s.",
46 libusb_error_name(ret));
53 static int send_bulkcmd(const struct sr_dev_inst *sdi, uint8_t *cmdstring, int cmdlen)
55 struct sr_usb_dev_inst *usb;
60 if (send_begin(sdi) != SR_OK)
63 if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT, cmdstring,
64 cmdlen, &tmp, 200)) != 0)
70 static int dso_getmps(libusb_device *dev)
72 struct libusb_device_descriptor des;
73 struct libusb_config_descriptor *conf_dsc;
74 const struct libusb_interface_descriptor *intf_dsc;
77 libusb_get_device_descriptor(dev, &des);
79 if (des.bNumConfigurations != 1)
82 if (libusb_get_config_descriptor(dev, 0, &conf_dsc) != 0)
86 intf_dsc = &(conf_dsc->interface[0].altsetting[0]);
87 if (intf_dsc->bNumEndpoints != 2)
90 if ((intf_dsc->endpoint[0].bEndpointAddress & 0x8f) !=
91 (2 | LIBUSB_ENDPOINT_OUT))
92 /* The first endpoint should be 2 (outbound). */
95 if ((intf_dsc->endpoint[1].bEndpointAddress & 0x8f) !=
96 (6 | LIBUSB_ENDPOINT_IN))
97 /* The second endpoint should be 6 (inbound). */
100 mps = intf_dsc->endpoint[1].wMaxPacketSize;
104 libusb_free_config_descriptor(conf_dsc);
109 SR_PRIV int dso_open(struct sr_dev_inst *sdi)
111 struct dev_context *devc;
112 struct drv_context *drvc = sdi->driver->context;
113 struct sr_usb_dev_inst *usb;
114 struct libusb_device_descriptor des;
115 libusb_device **devlist;
117 char connection_id[64];
122 if (sdi->status == SR_ST_ACTIVE)
126 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
127 for (i = 0; devlist[i]; i++) {
128 libusb_get_device_descriptor(devlist[i], &des);
130 if (des.idVendor != devc->profile->fw_vid
131 || des.idProduct != devc->profile->fw_pid)
134 if ((sdi->status == SR_ST_INITIALIZING) ||
135 (sdi->status == SR_ST_INACTIVE)) {
137 * Check device by its physical USB bus/port address.
139 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
140 if (strcmp(sdi->connection_id, connection_id))
141 /* This is not the one. */
145 if (!(err = libusb_open(devlist[i], &usb->devhdl))) {
146 if (usb->address == 0xff)
148 * first time we touch this device after firmware upload,
149 * so we don't know the address yet.
151 usb->address = libusb_get_device_address(devlist[i]);
153 if (!(devc->epin_maxpacketsize = dso_getmps(devlist[i])))
154 sr_err("Wrong endpoint profile.");
156 sdi->status = SR_ST_ACTIVE;
157 sr_info("Opened device on %d.%d (logical) / "
158 "%s (physical) interface %d.",
159 usb->bus, usb->address,
160 sdi->connection_id, USB_INTERFACE);
163 sr_err("Failed to open device: %s.",
164 libusb_error_name(err));
167 /* If we made it here, we handled the device (somehow). */
170 libusb_free_device_list(devlist, 1);
172 if (sdi->status != SR_ST_ACTIVE)
178 SR_PRIV void dso_close(struct sr_dev_inst *sdi)
180 struct sr_usb_dev_inst *usb;
187 sr_info("Closing device on %d.%d (logical) / %s (physical) interface %d.",
188 usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
189 libusb_release_interface(usb->devhdl, USB_INTERFACE);
190 libusb_close(usb->devhdl);
192 sdi->status = SR_ST_INACTIVE;
196 static int get_channel_offsets(const struct sr_dev_inst *sdi)
198 struct dev_context *devc;
199 struct sr_usb_dev_inst *usb;
203 sr_dbg("Getting channel offsets.");
208 ret = libusb_control_transfer(usb->devhdl,
209 LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR,
210 CTRL_READ_EEPROM, EEPROM_CHANNEL_OFFSETS, 0,
211 (unsigned char *)&devc->channel_levels,
212 sizeof(devc->channel_levels), 200);
213 if (ret != sizeof(devc->channel_levels)) {
214 sr_err("Failed to get channel offsets: %s.",
215 libusb_error_name(ret));
219 /* Comes in as 16-bit numbers with the second byte always 0 on
220 * the DSO-2090. Guessing this is supposed to be big-endian,
221 * since that's how voltage offsets are submitted back to the DSO.
222 * Convert to host order now, so we can use them natively.
224 for (chan = 0; chan < NUM_CHANNELS; chan++) {
225 for (v = 0; v < 9; v++) {
226 devc->channel_levels[chan][v][0] =
227 g_ntohs(devc->channel_levels[chan][v][0]);
228 devc->channel_levels[chan][v][1] =
229 g_ntohs(devc->channel_levels[chan][v][1]);
233 if (sr_log_loglevel_get() >= SR_LOG_DBG) {
234 gs = g_string_sized_new(128);
235 for (chan = 0; chan < NUM_CHANNELS; chan++) {
236 g_string_printf(gs, "CH%d:", chan + 1);
237 for (v = 0; v < 9; v++) {
238 g_string_append_printf(gs, " %.4x-%.4x",
239 devc->channel_levels[chan][v][0],
240 devc->channel_levels[chan][v][1]);
242 sr_dbg("%s", gs->str);
244 g_string_free(gs, TRUE);
250 static int dso_set_trigger_samplerate(const struct sr_dev_inst *sdi)
252 struct dev_context *devc;
253 struct sr_usb_dev_inst *usb;
255 uint8_t cmdstring[12];
256 uint16_t timebase_small[] = { 0xffff, 0xfffc, 0xfff7, 0xffe8, 0xffce,
257 0xff9c, 0xff07, 0xfe0d, 0xfc19, 0xf63d, 0xec79, 0xd8f1 };
258 uint16_t timebase_large[] = { 0xffff, 0x0000, 0xfffc, 0xfff7, 0xffe8,
259 0xffce, 0xff9d, 0xff07, 0xfe0d, 0xfc19, 0xf63d, 0xec79 };
261 sr_dbg("Preparing CMD_SET_TRIGGER_SAMPLERATE.");
266 memset(cmdstring, 0, sizeof(cmdstring));
268 cmdstring[0] = CMD_SET_TRIGGER_SAMPLERATE;
271 sr_dbg("Trigger source %s.", devc->triggersource);
272 if (!strcmp("CH2", devc->triggersource))
274 else if (!strcmp("CH1", devc->triggersource))
276 else if (!strcmp("EXT", devc->triggersource))
279 sr_err("Invalid trigger source: '%s'.", devc->triggersource);
285 sr_dbg("Frame size: %d.", devc->framesize);
286 cmdstring[2] |= (devc->framesize == FRAMESIZE_SMALL ? 0x01 : 0x02) << 2;
289 sr_dbg("Time base index: %d.", devc->timebase);
290 if (devc->framesize == FRAMESIZE_SMALL) {
291 if (devc->timebase < TIME_20us)
293 else if (devc->timebase == TIME_20us)
295 else if (devc->timebase == TIME_40us)
297 else if (devc->timebase == TIME_100us)
299 else if (devc->timebase >= TIME_200us)
302 if (devc->timebase < TIME_40us) {
303 sr_err("Timebase < 40us only supported with 10K buffer.");
306 else if (devc->timebase == TIME_40us)
308 else if (devc->timebase == TIME_100us)
310 else if (devc->timebase == TIME_200us)
312 else if (devc->timebase >= TIME_400us)
315 cmdstring[2] |= (tmp & 0x07) << 5;
317 /* Enabled channels: 00=CH1 01=CH2 10=both */
318 sr_dbg("Channels CH1=%d CH2=%d", devc->ch1_enabled, devc->ch2_enabled);
319 tmp = (((devc->ch2_enabled ? 1 : 0) << 1) + (devc->ch1_enabled ? 1 : 0)) - 1;
322 /* Fast rates channel */
323 /* TODO: Is this right? */
324 tmp = devc->timebase < TIME_10us ? 1 : 0;
325 cmdstring[3] |= tmp << 2;
327 /* Trigger slope: 0=positive 1=negative */
328 /* TODO: Does this work? */
329 sr_dbg("Trigger slope: %d.", devc->triggerslope);
330 cmdstring[3] |= (devc->triggerslope == SLOPE_NEGATIVE ? 1 : 0) << 3;
333 if (devc->timebase < TIME_100us)
335 else if (devc->timebase > TIME_400ms)
338 if (devc->framesize == FRAMESIZE_SMALL)
339 tmp = timebase_small[devc->timebase - 3];
341 tmp = timebase_large[devc->timebase - 3];
343 cmdstring[4] = tmp & 0xff;
344 cmdstring[5] = (tmp >> 8) & 0xff;
346 /* Horizontal trigger position */
347 sr_dbg("Trigger position: %3.2f.", devc->triggerposition);
348 tmp = 0x77fff + 0x8000 * devc->triggerposition;
349 cmdstring[6] = tmp & 0xff;
350 cmdstring[7] = (tmp >> 8) & 0xff;
351 cmdstring[10] = (tmp >> 16) & 0xff;
353 if (send_begin(sdi) != SR_OK)
356 if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT,
357 cmdstring, sizeof(cmdstring), &tmp, 100)) != 0) {
358 sr_err("Failed to set trigger/samplerate: %s.",
359 libusb_error_name(ret));
362 sr_dbg("Sent CMD_SET_TRIGGER_SAMPLERATE.");
367 static int dso_set_filters(const struct sr_dev_inst *sdi)
369 struct dev_context *devc;
370 struct sr_usb_dev_inst *usb;
372 uint8_t cmdstring[8];
374 sr_dbg("Preparing CMD_SET_FILTERS.");
379 memset(cmdstring, 0, sizeof(cmdstring));
380 cmdstring[0] = CMD_SET_FILTERS;
382 if (devc->filter[0]) {
383 sr_dbg("Turning on CH1 filter.");
384 cmdstring[2] |= 0x80;
386 if (devc->filter[1]) {
387 sr_dbg("Turning on CH2 filter.");
388 cmdstring[2] |= 0x40;
391 * Not supported: filtering on the trigger
392 * cmdstring[2] |= 0x20;
395 if (send_begin(sdi) != SR_OK)
398 if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT,
399 cmdstring, sizeof(cmdstring), &tmp, 100)) != 0) {
400 sr_err("Failed to set filters: %s.", libusb_error_name(ret));
403 sr_dbg("Sent CMD_SET_FILTERS.");
408 static int dso_set_voltage(const struct sr_dev_inst *sdi)
410 struct dev_context *devc;
411 struct sr_usb_dev_inst *usb;
413 uint8_t cmdstring[8];
415 sr_dbg("Preparing CMD_SET_VOLTAGE.");
420 memset(cmdstring, 0, sizeof(cmdstring));
421 cmdstring[0] = CMD_SET_VOLTAGE;
425 /* CH1 volts/div is encoded in bits 0-1 */
426 sr_dbg("CH1 vdiv index: %d.", devc->voltage[0]);
427 switch (devc->voltage[0]) {
431 cmdstring[2] |= 0x00;
436 cmdstring[2] |= 0x01;
441 cmdstring[2] |= 0x02;
445 /* CH2 volts/div is encoded in bits 2-3 */
446 sr_dbg("CH2 vdiv index: %d.", devc->voltage[1]);
447 switch (devc->voltage[1]) {
451 cmdstring[2] |= 0x00;
456 cmdstring[2] |= 0x04;
461 cmdstring[2] |= 0x08;
465 if (send_begin(sdi) != SR_OK)
468 if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT,
469 cmdstring, sizeof(cmdstring), &tmp, 100)) != 0) {
470 sr_err("Failed to set voltage: %s.", libusb_error_name(ret));
473 sr_dbg("Sent CMD_SET_VOLTAGE.");
478 static int dso_set_relays(const struct sr_dev_inst *sdi)
480 struct dev_context *devc;
481 struct sr_usb_dev_inst *usb;
484 uint8_t relays[17] = { 0x00, 0x04, 0x08, 0x02, 0x20, 0x40, 0x10, 0x01,
485 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
487 sr_dbg("Preparing CTRL_SETRELAYS.");
492 if (devc->voltage[0] < VDIV_1V)
493 relays[1] = ~relays[1];
495 if (devc->voltage[0] < VDIV_100MV)
496 relays[2] = ~relays[2];
498 sr_dbg("CH1 coupling: %d.", devc->coupling[0]);
499 if (devc->coupling[0] != COUPLING_AC)
500 relays[3] = ~relays[3];
502 if (devc->voltage[1] < VDIV_1V)
503 relays[4] = ~relays[4];
505 if (devc->voltage[1] < VDIV_100MV)
506 relays[5] = ~relays[5];
508 sr_dbg("CH2 coupling: %d.", devc->coupling[1]);
509 if (devc->coupling[1] != COUPLING_AC)
510 relays[6] = ~relays[6];
512 if (!strcmp(devc->triggersource, "EXT"))
513 relays[7] = ~relays[7];
515 if (sr_log_loglevel_get() >= SR_LOG_DBG) {
516 gs = g_string_sized_new(128);
517 g_string_printf(gs, "Relays:");
518 for (i = 0; i < 17; i++)
519 g_string_append_printf(gs, " %.2x", relays[i]);
520 sr_dbg("%s", gs->str);
521 g_string_free(gs, TRUE);
524 if ((ret = libusb_control_transfer(usb->devhdl,
525 LIBUSB_REQUEST_TYPE_VENDOR, CTRL_SETRELAYS,
526 0, 0, relays, 17, 100)) != sizeof(relays)) {
527 sr_err("Failed to set relays: %s.", libusb_error_name(ret));
530 sr_dbg("Sent CTRL_SETRELAYS.");
535 static int dso_set_voffsets(const struct sr_dev_inst *sdi)
537 struct dev_context *devc;
538 struct sr_usb_dev_inst *usb;
543 sr_dbg("Preparing CTRL_SETOFFSET.");
548 memset(offsets, 0, sizeof(offsets));
550 ch_levels = devc->channel_levels[0][devc->voltage[0]];
551 offset = (ch_levels[1] - ch_levels[0]) * devc->voffset_ch1 + ch_levels[0];
552 offsets[0] = (offset >> 8) | 0x20;
553 offsets[1] = offset & 0xff;
554 sr_dbg("CH1 offset: %3.2f (%.2x%.2x).", devc->voffset_ch1,
555 offsets[0], offsets[1]);
558 ch_levels = devc->channel_levels[1][devc->voltage[1]];
559 offset = (ch_levels[1] - ch_levels[0]) * devc->voffset_ch2 + ch_levels[0];
560 offsets[2] = (offset >> 8) | 0x20;
561 offsets[3] = offset & 0xff;
562 sr_dbg("CH2 offset: %3.2f (%.2x%.2x).", devc->voffset_ch2,
563 offsets[2], offsets[3]);
566 offset = MAX_VERT_TRIGGER * devc->voffset_trigger;
567 offsets[4] = (offset >> 8) | 0x20;
568 offsets[5] = offset & 0xff;
569 sr_dbg("Trigger offset: %3.2f (%.2x%.2x).", devc->voffset_trigger,
570 offsets[4], offsets[5]);
572 if ((ret = libusb_control_transfer(usb->devhdl,
573 LIBUSB_REQUEST_TYPE_VENDOR, CTRL_SETOFFSET,
574 0, 0, offsets, sizeof(offsets), 100)) != sizeof(offsets)) {
575 sr_err("Failed to set offsets: %s.", libusb_error_name(ret));
578 sr_dbg("Sent CTRL_SETOFFSET.");
583 SR_PRIV int dso_enable_trigger(const struct sr_dev_inst *sdi)
585 struct sr_usb_dev_inst *usb;
587 uint8_t cmdstring[2];
589 sr_dbg("Sending CMD_ENABLE_TRIGGER.");
593 memset(cmdstring, 0, sizeof(cmdstring));
594 cmdstring[0] = CMD_ENABLE_TRIGGER;
597 if (send_begin(sdi) != SR_OK)
600 if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT,
601 cmdstring, sizeof(cmdstring), &tmp, 100)) != 0) {
602 sr_err("Failed to enable trigger: %s.", libusb_error_name(ret));
609 SR_PRIV int dso_force_trigger(const struct sr_dev_inst *sdi)
611 struct sr_usb_dev_inst *usb;
613 uint8_t cmdstring[2];
615 sr_dbg("Sending CMD_FORCE_TRIGGER.");
619 memset(cmdstring, 0, sizeof(cmdstring));
620 cmdstring[0] = CMD_FORCE_TRIGGER;
623 if (send_begin(sdi) != SR_OK)
626 if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT,
627 cmdstring, sizeof(cmdstring), &tmp, 100)) != 0) {
628 sr_err("Failed to force trigger: %s.", libusb_error_name(ret));
635 SR_PRIV int dso_init(const struct sr_dev_inst *sdi)
638 sr_dbg("Initializing DSO.");
640 if (get_channel_offsets(sdi) != SR_OK)
643 if (dso_set_trigger_samplerate(sdi) != SR_OK)
646 if (dso_set_filters(sdi) != SR_OK)
649 if (dso_set_voltage(sdi) != SR_OK)
652 if (dso_set_relays(sdi) != SR_OK)
655 if (dso_set_voffsets(sdi) != SR_OK)
658 if (dso_enable_trigger(sdi) != SR_OK)
664 SR_PRIV int dso_get_capturestate(const struct sr_dev_inst *sdi,
665 uint8_t *capturestate, uint32_t *trigger_offset)
667 struct sr_usb_dev_inst *usb;
669 unsigned int bitvalue, toff;
670 uint8_t cmdstring[2], inbuf[512];
672 sr_dbg("Sending CMD_GET_CAPTURESTATE.");
676 cmdstring[0] = CMD_GET_CAPTURESTATE;
679 if ((ret = send_bulkcmd(sdi, cmdstring, sizeof(cmdstring))) != SR_OK) {
680 sr_dbg("Failed to send get_capturestate command: %s.",
681 libusb_error_name(ret));
685 if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_IN,
686 inbuf, 512, &tmp, 100)) != 0) {
687 sr_dbg("Failed to get capturestate: %s.",
688 libusb_error_name(ret));
691 *capturestate = inbuf[0];
692 toff = (inbuf[1] << 16) | (inbuf[3] << 8) | inbuf[2];
695 * This conversion comes from the openhantek project.
696 * Each set bit in the 24-bit value inverts all bits with a lower
697 * value. No idea why the device reports the trigger point this way.
700 for (i = 0; i < 24; i++) {
701 /* Each set bit inverts all bits with a lower value. */
703 toff ^= bitvalue - 1;
706 *trigger_offset = toff;
711 SR_PRIV int dso_capture_start(const struct sr_dev_inst *sdi)
714 uint8_t cmdstring[2];
716 sr_dbg("Sending CMD_CAPTURE_START.");
718 cmdstring[0] = CMD_CAPTURE_START;
721 if ((ret = send_bulkcmd(sdi, cmdstring, sizeof(cmdstring))) != SR_OK) {
722 sr_err("Failed to send capture_start command: %s.",
723 libusb_error_name(ret));
730 SR_PRIV int dso_get_channeldata(const struct sr_dev_inst *sdi,
731 libusb_transfer_cb_fn cb)
733 struct dev_context *devc;
734 struct sr_usb_dev_inst *usb;
735 struct libusb_transfer *transfer;
736 int num_transfers, ret, i;
737 uint8_t cmdstring[2];
740 sr_dbg("Sending CMD_GET_CHANNELDATA.");
745 cmdstring[0] = CMD_GET_CHANNELDATA;
748 if ((ret = send_bulkcmd(sdi, cmdstring, sizeof(cmdstring))) != SR_OK) {
749 sr_err("Failed to get channel data: %s.",
750 libusb_error_name(ret));
754 /* TODO: DSO-2xxx only. */
755 num_transfers = devc->framesize *
756 sizeof(unsigned short) / devc->epin_maxpacketsize;
757 sr_dbg("Queueing up %d transfers.", num_transfers);
758 for (i = 0; i < num_transfers; i++) {
759 if (!(buf = g_try_malloc(devc->epin_maxpacketsize))) {
760 sr_err("Failed to malloc USB endpoint buffer.");
761 return SR_ERR_MALLOC;
763 transfer = libusb_alloc_transfer(0);
764 libusb_fill_bulk_transfer(transfer, usb->devhdl, DSO_EP_IN, buf,
765 devc->epin_maxpacketsize, cb, (void *)sdi, 40);
766 if ((ret = libusb_submit_transfer(transfer)) != 0) {
767 sr_err("Failed to submit transfer: %s.",
768 libusb_error_name(ret));
769 /* TODO: Free them all. */
770 libusb_free_transfer(transfer);