2 * This file is part of the sigrok 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/>.
22 #include "libsigrok.h"
23 #include "libsigrok-internal.h"
29 extern struct sr_dev_driver hantek_dso_driver_info;
32 static int send_begin(struct dev_context *devc)
35 unsigned char buffer[] = {0x0f, 0x03, 0x03, 0x03, 0x68, 0xac, 0xfe,
38 sr_dbg("hantek-dso: sending CTRL_BEGINCOMMAND");
40 if ((ret = libusb_control_transfer(devc->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: %d", ret);
50 static int send_bulkcmd(struct dev_context *devc, uint8_t *cmdstring, int cmdlen)
54 if (send_begin(devc) != SR_OK)
57 if ((ret = libusb_bulk_transfer(devc->usb->devhdl,
58 DSO_EP_OUT | LIBUSB_ENDPOINT_OUT,
59 cmdstring, cmdlen, &tmp, 200)) != 0)
65 SR_PRIV int dso_getmps(libusb_device *dev)
67 struct libusb_device_descriptor des;
68 struct libusb_config_descriptor *conf_dsc;
69 const struct libusb_interface_descriptor *intf_dsc;
72 if (libusb_get_device_descriptor(dev, &des) != 0)
75 if (des.bNumConfigurations != 1)
78 if (libusb_get_config_descriptor(dev, 0, &conf_dsc) != 0)
82 intf_dsc = &(conf_dsc->interface[0].altsetting[0]);
83 if (intf_dsc->bNumEndpoints != 2)
86 if ((intf_dsc->endpoint[0].bEndpointAddress & 0x8f) !=
87 (2 | LIBUSB_ENDPOINT_OUT))
88 /* The first endpoint should be 2 (outbound). */
91 if ((intf_dsc->endpoint[1].bEndpointAddress & 0x8f) !=
92 (6 | LIBUSB_ENDPOINT_IN))
93 /* The second endpoint should be 6 (inbound). */
96 mps = intf_dsc->endpoint[1].wMaxPacketSize;
100 libusb_free_config_descriptor(conf_dsc);
105 SR_PRIV int dso_open(struct sr_dev_inst *sdi)
107 libusb_device **devlist;
108 struct libusb_device_descriptor des;
109 struct dev_context *devc;
114 if (sdi->status == SR_ST_ACTIVE)
119 libusb_get_device_list(NULL, &devlist);
120 for (i = 0; devlist[i]; i++) {
121 if ((err = libusb_get_device_descriptor(devlist[i], &des))) {
122 sr_err("hantek-dso: failed to get device descriptor: %d", err);
126 if (des.idVendor != devc->profile->fw_vid
127 || des.idProduct != devc->profile->fw_pid)
130 if (sdi->status == SR_ST_INITIALIZING) {
131 if (skip != sdi->index) {
132 /* Skip devices of this type that aren't the one we want. */
136 } else if (sdi->status == SR_ST_INACTIVE) {
138 * This device is fully enumerated, so we need to find
139 * this device by vendor, product, bus and address.
141 if (libusb_get_bus_number(devlist[i]) != devc->usb->bus
142 || libusb_get_device_address(devlist[i]) != devc->usb->address)
143 /* this is not the one */
147 if (!(err = libusb_open(devlist[i], &devc->usb->devhdl))) {
148 if (devc->usb->address == 0xff)
150 * first time we touch this device after firmware upload,
151 * so we don't know the address yet.
153 devc->usb->address = libusb_get_device_address(devlist[i]);
155 if(!(devc->epin_maxpacketsize = dso_getmps(devlist[i])))
156 sr_err("hantek-dso: wrong endpoint profile");
158 sdi->status = SR_ST_ACTIVE;
159 sr_info("hantek-dso: opened device %d on %d.%d interface %d",
160 sdi->index, devc->usb->bus,
161 devc->usb->address, USB_INTERFACE);
164 sr_err("hantek-dso: failed to open device: %d", err);
167 /* if we made it here, we handled the device one way or another */
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 dev_context *devc;
184 if (devc->usb->devhdl == NULL)
187 sr_info("hantek-dso: closing device %d on %d.%d interface %d", sdi->index,
188 devc->usb->bus, devc->usb->address, USB_INTERFACE);
189 libusb_release_interface(devc->usb->devhdl, USB_INTERFACE);
190 libusb_close(devc->usb->devhdl);
191 devc->usb->devhdl = NULL;
192 sdi->status = SR_ST_INACTIVE;
196 static int get_channel_offsets(struct dev_context *devc)
201 sr_dbg("hantek-dso: getting channel offsets");
203 ret = libusb_control_transfer(devc->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: %d", 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 < 2; chan++) {
219 for (v = 0; v < 9; v++) {
220 devc->channel_levels[chan][v][0] = g_ntohs(devc->channel_levels[chan][v][0]);
221 devc->channel_levels[chan][v][1] = g_ntohs(devc->channel_levels[chan][v][1]);
225 if (sr_log_loglevel_get() >= SR_LOG_DBG) {
226 gs = g_string_sized_new(128);
227 for (chan = 0; chan < 2; chan++) {
228 g_string_printf(gs, "hantek-dso: CH%d:", chan + 1);
229 for (v = 0; v < 9; v++) {
230 g_string_append_printf(gs, " %.4x-%.4x",
231 devc->channel_levels[chan][v][0],
232 devc->channel_levels[chan][v][1]);
236 g_string_free(gs, TRUE);
242 SR_PRIV int dso_set_trigger_samplerate(struct dev_context *devc)
245 uint8_t cmdstring[12];
246 uint16_t timebase_small[] = { 0xffff, 0xfffc, 0xfff7, 0xffe8, 0xffce,
247 0xff9c, 0xff07, 0xfe0d, 0xfc19, 0xf63d, 0xec79, 0xd8f1 };
248 uint16_t timebase_large[] = { 0xffff, 0x0000, 0xfffc, 0xfff7, 0xffe8,
249 0xffce, 0xff9d, 0xff07, 0xfe0d, 0xfc19, 0xf63d, 0xec79 };
251 sr_dbg("hantek-dso: preparing CMD_SET_TRIGGER_SAMPLERATE");
253 memset(cmdstring, 0, sizeof(cmdstring));
255 cmdstring[0] = CMD_SET_TRIGGER_SAMPLERATE;
258 sr_dbg("hantek-dso: trigger source %s", devc->triggersource);
259 if (!strcmp("CH2", devc->triggersource))
261 else if (!strcmp("CH1", devc->triggersource))
263 else if (!strcmp("EXT", devc->triggersource))
266 sr_err("hantek-dso: invalid trigger source %s", devc->triggersource);
272 sr_dbg("hantek-dso: frame size %d", devc->framesize);
273 cmdstring[2] |= (devc->framesize == FRAMESIZE_SMALL ? 0x01 : 0x02) << 2;
276 sr_dbg("hantek-dso: time base index %d", devc->timebase);
277 switch (devc->framesize) {
278 case FRAMESIZE_SMALL:
279 if (devc->timebase < TIME_20us)
281 else if (devc->timebase == TIME_20us)
283 else if (devc->timebase == TIME_40us)
285 else if (devc->timebase == TIME_100us)
287 else if (devc->timebase >= TIME_200us)
290 case FRAMESIZE_LARGE:
291 if (devc->timebase < TIME_40us) {
292 sr_err("hantek-dso: timebase < 40us only supported with 10K buffer");
295 else if (devc->timebase == TIME_40us)
297 else if (devc->timebase == TIME_100us)
299 else if (devc->timebase == TIME_200us)
301 else if (devc->timebase >= TIME_400us)
305 cmdstring[2] |= (tmp & 0x07) << 5;
307 /* Enabled channels: 00=CH1 01=CH2 10=both */
308 sr_dbg("hantek-dso: channels CH1=%d CH2=%d", devc->ch1_enabled, devc->ch2_enabled);
309 tmp = (((devc->ch2_enabled ? 1 : 0) << 1) + (devc->ch1_enabled ? 1 : 0)) - 1;
312 /* Fast rates channel */
313 /* TODO: is this right? */
314 tmp = devc->timebase < TIME_10us ? 1 : 0;
315 cmdstring[3] |= tmp << 2;
317 /* Trigger slope: 0=positive 1=negative */
318 /* TODO: does this work? */
319 sr_dbg("hantek-dso: trigger slope %d", devc->triggerslope);
320 cmdstring[3] |= (devc->triggerslope == SLOPE_NEGATIVE ? 1 : 0) << 3;
323 if (devc->timebase < TIME_100us)
325 else if (devc->timebase > TIME_400ms)
328 if (devc->framesize == FRAMESIZE_SMALL)
329 tmp = timebase_small[devc->timebase - 3];
331 tmp = timebase_large[devc->timebase - 3];
333 cmdstring[4] = tmp & 0xff;
334 cmdstring[5] = (tmp >> 8) & 0xff;
336 /* Horizontal trigger position */
337 sr_dbg("hantek-dso: trigger position %3.2f", devc->triggerposition);
338 tmp = 0x77fff + 0x8000 * devc->triggerposition;
339 cmdstring[6] = tmp & 0xff;
340 cmdstring[7] = (tmp >> 8) & 0xff;
341 cmdstring[10] = (tmp >> 16) & 0xff;
343 if (send_begin(devc) != SR_OK)
346 if ((ret = libusb_bulk_transfer(devc->usb->devhdl,
347 DSO_EP_OUT | LIBUSB_ENDPOINT_OUT,
348 cmdstring, sizeof(cmdstring),
350 sr_err("Failed to set trigger/samplerate: %d", ret);
353 sr_dbg("hantek-dso: sent CMD_SET_TRIGGER_SAMPLERATE");
358 SR_PRIV int dso_set_filters(struct dev_context *devc)
361 uint8_t cmdstring[8];
363 sr_dbg("hantek-dso: preparing CMD_SET_FILTERS");
365 memset(cmdstring, 0, sizeof(cmdstring));
366 cmdstring[0] = CMD_SET_FILTERS;
368 if (devc->filter_ch1) {
369 sr_dbg("hantek-dso: turning on CH1 filter");
370 cmdstring[2] |= 0x80;
372 if (devc->filter_ch2) {
373 sr_dbg("hantek-dso: turning on CH2 filter");
374 cmdstring[2] |= 0x40;
376 if (devc->filter_trigger) {
377 /* TODO: supported on the DSO-2090? */
378 sr_dbg("hantek-dso: turning on trigger filter");
379 cmdstring[2] |= 0x20;
382 if (send_begin(devc) != SR_OK)
385 if ((ret = libusb_bulk_transfer(devc->usb->devhdl,
386 DSO_EP_OUT | LIBUSB_ENDPOINT_OUT,
387 cmdstring, sizeof(cmdstring),
389 sr_err("Failed to set filters: %d", ret);
392 sr_dbg("hantek-dso: sent CMD_SET_FILTERS");
397 SR_PRIV int dso_set_voltage(struct dev_context *devc)
400 uint8_t cmdstring[8];
402 sr_dbg("hantek-dso: preparing CMD_SET_VOLTAGE");
404 memset(cmdstring, 0, sizeof(cmdstring));
405 cmdstring[0] = CMD_SET_VOLTAGE;
409 /* CH1 volts/div is encoded in bits 0-1 */
410 sr_dbg("hantek-dso: CH1 vdiv index %d", devc->voltage_ch1);
411 switch (devc->voltage_ch1) {
415 cmdstring[2] |= 0x00;
420 cmdstring[2] |= 0x01;
425 cmdstring[2] |= 0x02;
429 /* CH2 volts/div is encoded in bits 2-3 */
430 sr_dbg("hantek-dso: CH2 vdiv index %d", devc->voltage_ch2);
431 switch (devc->voltage_ch2) {
435 cmdstring[2] |= 0x00;
440 cmdstring[2] |= 0x04;
445 cmdstring[2] |= 0x08;
449 if (send_begin(devc) != SR_OK)
452 if ((ret = libusb_bulk_transfer(devc->usb->devhdl,
453 DSO_EP_OUT | LIBUSB_ENDPOINT_OUT,
454 cmdstring, sizeof(cmdstring),
456 sr_err("Failed to set voltage: %d", ret);
459 sr_dbg("hantek-dso: sent CMD_SET_VOLTAGE");
464 SR_PRIV int dso_set_relays(struct dev_context *devc)
468 uint8_t relays[17] = { 0x00, 0x04, 0x08, 0x02, 0x20, 0x40, 0x10, 0x01,
469 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
471 sr_dbg("hantek-dso: preparing CTRL_SETRELAYS");
473 if (devc->voltage_ch1 < VDIV_1V)
474 relays[1] = ~relays[1];
476 if (devc->voltage_ch1 < VDIV_100MV)
477 relays[2] = ~relays[2];
479 sr_dbg("hantek-dso: CH1 coupling %d", devc->coupling_ch1);
480 if (devc->coupling_ch1 != COUPLING_AC)
481 relays[3] = ~relays[3];
483 if (devc->voltage_ch2 < VDIV_1V)
484 relays[4] = ~relays[4];
486 if (devc->voltage_ch2 < VDIV_100MV)
487 relays[5] = ~relays[5];
489 sr_dbg("hantek-dso: CH2 coupling %d", devc->coupling_ch1);
490 if (devc->coupling_ch2 != COUPLING_AC)
491 relays[6] = ~relays[6];
493 if (!strcmp(devc->triggersource, "EXT"))
494 relays[7] = ~relays[7];
496 if (sr_log_loglevel_get() >= SR_LOG_DBG) {
497 gs = g_string_sized_new(128);
498 g_string_printf(gs, "hantek-dso: relays:");
499 for (i = 0; i < 17; i++)
500 g_string_append_printf(gs, " %.2x", relays[i]);
502 g_string_free(gs, TRUE);
505 if ((ret = libusb_control_transfer(devc->usb->devhdl,
506 LIBUSB_REQUEST_TYPE_VENDOR, CTRL_SETRELAYS,
507 0, 0, relays, 17, 100)) != sizeof(relays)) {
508 sr_err("failed to set relays: %d", ret);
511 sr_dbg("hantek-dso: sent CTRL_SETRELAYS");
516 SR_PRIV int dso_set_voffsets(struct dev_context *devc)
522 sr_dbg("hantek-dso: preparing CTRL_SETOFFSET");
524 memset(offsets, 0, sizeof(offsets));
526 ch_levels = devc->channel_levels[0][devc->voltage_ch1];
527 offset = (ch_levels[1] - ch_levels[0]) * devc->voffset_ch1 + ch_levels[0];
528 offsets[0] = (offset >> 8) | 0x20;
529 offsets[1] = offset & 0xff;
530 sr_dbg("hantek-dso: CH1 offset %3.2f (%.2x%.2x)", devc->voffset_ch1,
531 offsets[0], offsets[1]);
534 ch_levels = devc->channel_levels[1][devc->voltage_ch2];
535 offset = (ch_levels[1] - ch_levels[0]) * devc->voffset_ch2 + ch_levels[0];
536 offsets[2] = (offset >> 8) | 0x20;
537 offsets[3] = offset & 0xff;
538 sr_dbg("hantek-dso: CH2 offset %3.2f (%.2x%.2x)", devc->voffset_ch2,
539 offsets[2], offsets[3]);
542 offset = MAX_VERT_TRIGGER * devc->voffset_trigger;
543 offsets[4] = (offset >> 8) | 0x20;
544 offsets[5] = offset & 0xff;
545 sr_dbg("hantek-dso: trigger offset %3.2f (%.2x%.2x)", devc->voffset_trigger,
546 offsets[4], offsets[5]);
548 if ((ret = libusb_control_transfer(devc->usb->devhdl,
549 LIBUSB_REQUEST_TYPE_VENDOR, CTRL_SETOFFSET,
550 0, 0, offsets, sizeof(offsets), 100)) != sizeof(offsets)) {
551 sr_err("failed to set offsets: %d", ret);
554 sr_dbg("hantek-dso: sent CTRL_SETOFFSET");
559 SR_PRIV int dso_enable_trigger(struct dev_context *devc)
562 uint8_t cmdstring[2];
564 sr_dbg("hantek-dso: sending CMD_ENABLE_TRIGGER");
566 memset(cmdstring, 0, sizeof(cmdstring));
567 cmdstring[0] = CMD_ENABLE_TRIGGER;
570 if (send_begin(devc) != SR_OK)
573 if ((ret = libusb_bulk_transfer(devc->usb->devhdl,
574 DSO_EP_OUT | LIBUSB_ENDPOINT_OUT,
575 cmdstring, sizeof(cmdstring),
577 sr_err("Failed to enable trigger: %d", ret);
584 SR_PRIV int dso_force_trigger(struct dev_context *devc)
587 uint8_t cmdstring[2];
589 sr_dbg("hantek-dso: sending CMD_FORCE_TRIGGER");
591 memset(cmdstring, 0, sizeof(cmdstring));
592 cmdstring[0] = CMD_FORCE_TRIGGER;
595 if (send_begin(devc) != SR_OK)
598 if ((ret = libusb_bulk_transfer(devc->usb->devhdl,
599 DSO_EP_OUT | LIBUSB_ENDPOINT_OUT,
600 cmdstring, sizeof(cmdstring),
602 sr_err("Failed to force trigger: %d", ret);
609 SR_PRIV int dso_init(struct dev_context *devc)
612 sr_dbg("hantek-dso: initializing dso");
614 if (get_channel_offsets(devc) != SR_OK)
617 if (dso_set_trigger_samplerate(devc) != SR_OK)
620 if (dso_set_filters(devc) != SR_OK)
623 if (dso_set_voltage(devc) != SR_OK)
626 if (dso_set_relays(devc) != SR_OK)
629 if (dso_set_voffsets(devc) != SR_OK)
632 if (dso_enable_trigger(devc) != SR_OK)
638 SR_PRIV int dso_get_capturestate(struct dev_context *devc, uint8_t *capturestate,
639 uint32_t *trigger_offset)
642 unsigned int bitvalue, toff;
643 uint8_t cmdstring[2], inbuf[512];
645 sr_dbg("hantek-dso: sending CMD_GET_CAPTURESTATE");
647 cmdstring[0] = CMD_GET_CAPTURESTATE;
650 if ((ret = send_bulkcmd(devc, cmdstring, sizeof(cmdstring))) != SR_OK) {
651 sr_dbg("Failed to send get_capturestate command: %d", ret);
655 if ((ret = libusb_bulk_transfer(devc->usb->devhdl,
656 DSO_EP_IN | LIBUSB_ENDPOINT_IN,
657 inbuf, 512, &tmp, 100)) != 0) {
658 sr_dbg("Failed to get capturestate: %d", ret);
661 *capturestate = inbuf[0];
662 toff = (inbuf[1] << 16) | (inbuf[3] << 8) | inbuf[2];
664 /* This conversion comes from the openhantek project.
665 * Each set bit in the 24-bit value inverts all bits with a lower
666 * value. No idea why the device reports the trigger point this way.
669 for (i = 0; i < 24; i++) {
670 /* Each set bit inverts all bits with a lower value. */
672 toff ^= bitvalue - 1;
675 *trigger_offset = toff;
680 SR_PRIV int dso_capture_start(struct dev_context *devc)
683 uint8_t cmdstring[2];
685 sr_dbg("hantek-dso: sending CMD_CAPTURE_START");
687 cmdstring[0] = CMD_CAPTURE_START;
690 if ((ret = send_bulkcmd(devc, cmdstring, sizeof(cmdstring))) != SR_OK) {
691 sr_err("Failed to send capture_start command: %d", ret);
698 SR_PRIV int dso_get_channeldata(struct dev_context *devc, libusb_transfer_cb_fn cb)
700 struct libusb_transfer *transfer;
701 int num_transfers, ret, i;
702 uint8_t cmdstring[2];
705 sr_dbg("hantek-dso: sending CMD_GET_CHANNELDATA");
707 cmdstring[0] = CMD_GET_CHANNELDATA;
710 if ((ret = send_bulkcmd(devc, cmdstring, sizeof(cmdstring))) != SR_OK) {
711 sr_err("Failed to get channel data: %d", ret);
715 /* TODO: dso-2xxx only */
716 num_transfers = devc->framesize * sizeof(unsigned short) / devc->epin_maxpacketsize;
717 sr_dbg("hantek-dso: queueing up %d transfers", num_transfers);
718 for (i = 0; i < num_transfers; i++) {
719 if (!(buf = g_try_malloc(devc->epin_maxpacketsize))) {
720 sr_err("hantek-dso: %s: buf malloc failed", __func__);
721 return SR_ERR_MALLOC;
723 transfer = libusb_alloc_transfer(0);
724 libusb_fill_bulk_transfer(transfer, devc->usb->devhdl,
725 DSO_EP_IN | LIBUSB_ENDPOINT_IN, buf,
726 devc->epin_maxpacketsize, cb, devc, 40);
727 if ((ret = libusb_submit_transfer(transfer)) != 0) {
728 sr_err("failed to submit transfer: %d", ret);
729 /* TODO: Free them all. */
730 libusb_free_transfer(transfer);