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"
30 extern libusb_context *usb_context;
31 extern struct sr_dev_driver hantek_dso_driver_info;
32 static struct sr_dev_driver *hdi = &hantek_dso_driver_info;
35 static int send_begin(struct context *ctx)
38 unsigned char buffer[] = {0x0f, 0x03, 0x03, 0x03, 0x68, 0xac, 0xfe,
41 sr_dbg("hantek-dso: sending CTRL_BEGINCOMMAND");
43 if ((ret = libusb_control_transfer(ctx->usb->devhdl,
44 LIBUSB_REQUEST_TYPE_VENDOR, CTRL_BEGINCOMMAND,
45 0, 0, buffer, sizeof(buffer), 200)) != sizeof(buffer)) {
46 sr_err("failed to send begincommand: %d", ret);
53 static int send_bulkcmd(struct context *ctx, uint8_t *cmdstring, int cmdlen)
57 if (send_begin(ctx) != SR_OK)
60 if ((ret = libusb_bulk_transfer(ctx->usb->devhdl,
61 DSO_EP_OUT | LIBUSB_ENDPOINT_OUT,
62 cmdstring, cmdlen, &tmp, 200)) != 0)
68 SR_PRIV 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 if (libusb_get_device_descriptor(dev, &des) != 0)
78 if (des.bNumConfigurations != 1)
81 if (libusb_get_config_descriptor(dev, 0, &conf_dsc) != 0)
85 intf_dsc = &(conf_dsc->interface[0].altsetting[0]);
86 if (intf_dsc->bNumEndpoints != 2)
89 if ((intf_dsc->endpoint[0].bEndpointAddress & 0x8f) !=
90 (2 | LIBUSB_ENDPOINT_OUT))
91 /* The first endpoint should be 2 (outbound). */
94 if ((intf_dsc->endpoint[1].bEndpointAddress & 0x8f) !=
95 (6 | LIBUSB_ENDPOINT_IN))
96 /* The second endpoint should be 6 (inbound). */
99 mps = intf_dsc->endpoint[1].wMaxPacketSize;
103 libusb_free_config_descriptor(conf_dsc);
108 SR_PRIV int dso_open(int dev_index)
110 libusb_device **devlist;
111 struct libusb_device_descriptor des;
112 struct sr_dev_inst *sdi;
116 if (!(sdi = sr_dev_inst_get(hdi->instances, dev_index)))
120 if (sdi->status == SR_ST_ACTIVE)
125 libusb_get_device_list(usb_context, &devlist);
126 for (i = 0; devlist[i]; i++) {
127 if ((err = libusb_get_device_descriptor(devlist[i], &des))) {
128 sr_err("hantek-dso: failed to get device descriptor: %d", err);
132 if (des.idVendor != ctx->profile->fw_vid
133 || des.idProduct != ctx->profile->fw_pid)
136 if (sdi->status == SR_ST_INITIALIZING) {
137 if (skip != dev_index) {
138 /* Skip devices of this type that aren't the one we want. */
142 } else if (sdi->status == SR_ST_INACTIVE) {
144 * This device is fully enumerated, so we need to find
145 * this device by vendor, product, bus and address.
147 if (libusb_get_bus_number(devlist[i]) != ctx->usb->bus
148 || libusb_get_device_address(devlist[i]) != ctx->usb->address)
149 /* this is not the one */
153 if (!(err = libusb_open(devlist[i], &ctx->usb->devhdl))) {
154 if (ctx->usb->address == 0xff)
156 * first time we touch this device after firmware upload,
157 * so we don't know the address yet.
159 ctx->usb->address = libusb_get_device_address(devlist[i]);
161 if(!(ctx->epin_maxpacketsize = dso_getmps(devlist[i])))
162 sr_err("hantek-dso: wrong endpoint profile");
164 sdi->status = SR_ST_ACTIVE;
165 sr_info("hantek-dso: opened device %d on %d.%d interface %d",
166 sdi->index, ctx->usb->bus,
167 ctx->usb->address, USB_INTERFACE);
170 sr_err("hantek-dso: failed to open device: %d", err);
173 /* if we made it here, we handled the device one way or another */
176 libusb_free_device_list(devlist, 1);
178 if (sdi->status != SR_ST_ACTIVE)
184 SR_PRIV void dso_close(struct sr_dev_inst *sdi)
190 if (ctx->usb->devhdl == NULL)
193 sr_info("hantek-dso: closing device %d on %d.%d interface %d", sdi->index,
194 ctx->usb->bus, ctx->usb->address, USB_INTERFACE);
195 libusb_release_interface(ctx->usb->devhdl, USB_INTERFACE);
196 libusb_close(ctx->usb->devhdl);
197 ctx->usb->devhdl = NULL;
198 sdi->status = SR_ST_INACTIVE;
202 static int get_channel_offsets(struct context *ctx)
207 sr_dbg("hantek-dso: getting channel offsets");
209 ret = libusb_control_transfer(ctx->usb->devhdl,
210 LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR,
211 CTRL_READ_EEPROM, EEPROM_CHANNEL_OFFSETS, 0,
212 (unsigned char *)&ctx->channel_levels,
213 sizeof(ctx->channel_levels), 200);
214 if (ret != sizeof(ctx->channel_levels)) {
215 sr_err("failed to get channel offsets: %d", 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 < 2; chan++) {
225 for (v = 0; v < 9; v++) {
226 ctx->channel_levels[chan][v][0] = g_ntohs(ctx->channel_levels[chan][v][0]);
227 ctx->channel_levels[chan][v][1] = g_ntohs(ctx->channel_levels[chan][v][1]);
231 if (sr_log_loglevel_get() >= SR_LOG_DBG) {
232 gs = g_string_sized_new(128);
233 for (chan = 0; chan < 2; chan++) {
234 g_string_printf(gs, "hantek-dso: CH%d:", chan + 1);
235 for (v = 0; v < 9; v++) {
236 g_string_append_printf(gs, " %.4x-%.4x",
237 ctx->channel_levels[chan][v][0],
238 ctx->channel_levels[chan][v][1]);
242 g_string_free(gs, TRUE);
248 SR_PRIV int dso_set_trigger_samplerate(struct context *ctx)
251 uint8_t cmdstring[12];
252 uint16_t timebase_small[] = { 0xffff, 0xfffc, 0xfff7, 0xffe8, 0xffce,
253 0xff9c, 0xff07, 0xfe0d, 0xfc19, 0xf63d, 0xec79, 0xd8f1 };
254 uint16_t timebase_large[] = { 0xffff, 0x0000, 0xfffc, 0xfff7, 0xffe8,
255 0xffce, 0xff9d, 0xff07, 0xfe0d, 0xfc19, 0xf63d, 0xec79 };
257 sr_dbg("hantek-dso: preparing CMD_SET_TRIGGER_SAMPLERATE");
259 memset(cmdstring, 0, sizeof(cmdstring));
261 cmdstring[0] = CMD_SET_TRIGGER_SAMPLERATE;
264 sr_dbg("hantek-dso: trigger source %s", ctx->triggersource);
265 if (!strcmp("CH2", ctx->triggersource))
267 else if (!strcmp("CH1", ctx->triggersource))
269 else if (!strcmp("EXT", ctx->triggersource))
272 sr_err("hantek-dso: invalid trigger source %s", ctx->triggersource);
278 sr_dbg("hantek-dso: frame size %d", ctx->framesize);
279 cmdstring[2] |= (ctx->framesize == FRAMESIZE_SMALL ? 0x01 : 0x02) << 2;
282 sr_dbg("hantek-dso: time base index %d", ctx->timebase);
283 switch (ctx->framesize) {
284 case FRAMESIZE_SMALL:
285 if (ctx->timebase < TIME_20us)
287 else if (ctx->timebase == TIME_20us)
289 else if (ctx->timebase == TIME_40us)
291 else if (ctx->timebase == TIME_100us)
293 else if (ctx->timebase >= TIME_200us)
296 case FRAMESIZE_LARGE:
297 if (ctx->timebase < TIME_40us) {
298 sr_err("hantek-dso: timebase < 40us only supported with 10K buffer");
301 else if (ctx->timebase == TIME_40us)
303 else if (ctx->timebase == TIME_100us)
305 else if (ctx->timebase == TIME_200us)
307 else if (ctx->timebase >= TIME_400us)
311 cmdstring[2] |= (tmp & 0x07) << 5;
313 /* Enabled channels: 00=CH1 01=CH2 10=both */
314 sr_dbg("hantek-dso: channels CH1=%d CH2=%d", ctx->ch1_enabled, ctx->ch2_enabled);
315 tmp = (((ctx->ch2_enabled ? 1 : 0) << 1) + (ctx->ch1_enabled ? 1 : 0)) - 1;
318 /* Fast rates channel */
319 /* TODO: is this right? */
320 tmp = ctx->timebase < TIME_10us ? 1 : 0;
321 cmdstring[3] |= tmp << 2;
323 /* Trigger slope: 0=positive 1=negative */
324 /* TODO: does this work? */
325 sr_dbg("hantek-dso: trigger slope %d", ctx->triggerslope);
326 cmdstring[3] |= (ctx->triggerslope == SLOPE_NEGATIVE ? 1 : 0) << 3;
329 if (ctx->timebase < TIME_100us)
331 else if (ctx->timebase > TIME_400ms)
334 if (ctx->framesize == FRAMESIZE_SMALL)
335 tmp = timebase_small[ctx->timebase - 3];
337 tmp = timebase_large[ctx->timebase - 3];
339 cmdstring[4] = tmp & 0xff;
340 cmdstring[5] = (tmp >> 8) & 0xff;
342 /* Horizontal trigger position */
343 sr_dbg("hantek-dso: trigger position %3.2f", ctx->triggerposition);
344 tmp = 0x77fff + 0x8000 * ctx->triggerposition;
345 cmdstring[6] = tmp & 0xff;
346 cmdstring[7] = (tmp >> 8) & 0xff;
347 cmdstring[10] = (tmp >> 16) & 0xff;
349 if (send_begin(ctx) != SR_OK)
352 if ((ret = libusb_bulk_transfer(ctx->usb->devhdl,
353 DSO_EP_OUT | LIBUSB_ENDPOINT_OUT,
354 cmdstring, sizeof(cmdstring),
356 sr_err("Failed to set trigger/samplerate: %d", ret);
359 sr_dbg("hantek-dso: sent CMD_SET_TRIGGER_SAMPLERATE");
364 SR_PRIV int dso_set_filters(struct context *ctx)
367 uint8_t cmdstring[8];
369 sr_dbg("hantek-dso: preparing CMD_SET_FILTERS");
371 memset(cmdstring, 0, sizeof(cmdstring));
372 cmdstring[0] = CMD_SET_FILTERS;
374 if (ctx->filter_ch1) {
375 sr_dbg("hantek-dso: turning on CH1 filter");
376 cmdstring[2] |= 0x80;
378 if (ctx->filter_ch2) {
379 sr_dbg("hantek-dso: turning on CH2 filter");
380 cmdstring[2] |= 0x40;
382 if (ctx->filter_trigger) {
383 /* TODO: supported on the DSO-2090? */
384 sr_dbg("hantek-dso: turning on trigger filter");
385 cmdstring[2] |= 0x20;
388 if (send_begin(ctx) != SR_OK)
391 if ((ret = libusb_bulk_transfer(ctx->usb->devhdl,
392 DSO_EP_OUT | LIBUSB_ENDPOINT_OUT,
393 cmdstring, sizeof(cmdstring),
395 sr_err("Failed to set filters: %d", ret);
398 sr_dbg("hantek-dso: sent CMD_SET_FILTERS");
403 SR_PRIV int dso_set_voltage(struct context *ctx)
406 uint8_t cmdstring[8];
408 sr_dbg("hantek-dso: preparing CMD_SET_VOLTAGE");
410 memset(cmdstring, 0, sizeof(cmdstring));
411 cmdstring[0] = CMD_SET_VOLTAGE;
415 /* CH1 volts/div is encoded in bits 0-1 */
416 sr_dbg("hantek-dso: CH1 vdiv index %d", ctx->voltage_ch1);
417 switch (ctx->voltage_ch1) {
421 cmdstring[2] |= 0x00;
426 cmdstring[2] |= 0x01;
431 cmdstring[2] |= 0x02;
435 /* CH2 volts/div is encoded in bits 2-3 */
436 sr_dbg("hantek-dso: CH2 vdiv index %d", ctx->voltage_ch2);
437 switch (ctx->voltage_ch2) {
441 cmdstring[2] |= 0x00;
446 cmdstring[2] |= 0x04;
451 cmdstring[2] |= 0x08;
455 if (send_begin(ctx) != SR_OK)
458 if ((ret = libusb_bulk_transfer(ctx->usb->devhdl,
459 DSO_EP_OUT | LIBUSB_ENDPOINT_OUT,
460 cmdstring, sizeof(cmdstring),
462 sr_err("Failed to set voltage: %d", ret);
465 sr_dbg("hantek-dso: sent CMD_SET_VOLTAGE");
470 SR_PRIV int dso_set_relays(struct context *ctx)
474 uint8_t relays[17] = { 0x00, 0x04, 0x08, 0x02, 0x20, 0x40, 0x10, 0x01,
475 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
477 sr_dbg("hantek-dso: preparing CTRL_SETRELAYS");
479 if (ctx->voltage_ch1 < VDIV_1V)
480 relays[1] = ~relays[1];
482 if (ctx->voltage_ch1 < VDIV_100MV)
483 relays[2] = ~relays[2];
485 sr_dbg("hantek-dso: CH1 coupling %d", ctx->coupling_ch1);
486 if (ctx->coupling_ch1 != COUPLING_AC)
487 relays[3] = ~relays[3];
489 if (ctx->voltage_ch2 < VDIV_1V)
490 relays[4] = ~relays[4];
492 if (ctx->voltage_ch2 < VDIV_100MV)
493 relays[5] = ~relays[5];
495 sr_dbg("hantek-dso: CH2 coupling %d", ctx->coupling_ch1);
496 if (ctx->coupling_ch2 != COUPLING_AC)
497 relays[6] = ~relays[6];
499 if (!strcmp(ctx->triggersource, "EXT"))
500 relays[7] = ~relays[7];
502 if (sr_log_loglevel_get() >= SR_LOG_DBG) {
503 gs = g_string_sized_new(128);
504 g_string_printf(gs, "hantek-dso: relays:");
505 for (i = 0; i < 17; i++)
506 g_string_append_printf(gs, " %.2x", relays[i]);
508 g_string_free(gs, TRUE);
511 if ((ret = libusb_control_transfer(ctx->usb->devhdl,
512 LIBUSB_REQUEST_TYPE_VENDOR, CTRL_SETRELAYS,
513 0, 0, relays, 17, 100)) != sizeof(relays)) {
514 sr_err("failed to set relays: %d", ret);
517 sr_dbg("hantek-dso: sent CTRL_SETRELAYS");
522 SR_PRIV int dso_set_voffsets(struct context *ctx)
528 sr_dbg("hantek-dso: preparing CTRL_SETOFFSET");
530 memset(offsets, 0, sizeof(offsets));
532 ch_levels = ctx->channel_levels[0][ctx->voltage_ch1];
533 offset = (ch_levels[1] - ch_levels[0]) * ctx->voffset_ch1 + ch_levels[0];
534 offsets[0] = (offset >> 8) | 0x20;
535 offsets[1] = offset & 0xff;
536 sr_dbg("hantek-dso: CH1 offset %3.2f (%.2x%.2x)", ctx->voffset_ch1,
537 offsets[0], offsets[1]);
540 ch_levels = ctx->channel_levels[1][ctx->voltage_ch2];
541 offset = (ch_levels[1] - ch_levels[0]) * ctx->voffset_ch2 + ch_levels[0];
542 offsets[2] = (offset >> 8) | 0x20;
543 offsets[3] = offset & 0xff;
544 sr_dbg("hantek-dso: CH2 offset %3.2f (%.2x%.2x)", ctx->voffset_ch2,
545 offsets[2], offsets[3]);
548 offset = MAX_VERT_TRIGGER * ctx->voffset_trigger;
549 offsets[4] = (offset >> 8) | 0x20;
550 offsets[5] = offset & 0xff;
551 sr_dbg("hantek-dso: trigger offset %3.2f (%.2x%.2x)", ctx->voffset_trigger,
552 offsets[4], offsets[5]);
554 if ((ret = libusb_control_transfer(ctx->usb->devhdl,
555 LIBUSB_REQUEST_TYPE_VENDOR, CTRL_SETOFFSET,
556 0, 0, offsets, sizeof(offsets), 100)) != sizeof(offsets)) {
557 sr_err("failed to set offsets: %d", ret);
560 sr_dbg("hantek-dso: sent CTRL_SETOFFSET");
565 SR_PRIV int dso_enable_trigger(struct context *ctx)
568 uint8_t cmdstring[2];
570 sr_dbg("hantek-dso: sending CMD_ENABLE_TRIGGER");
572 memset(cmdstring, 0, sizeof(cmdstring));
573 cmdstring[0] = CMD_ENABLE_TRIGGER;
576 if (send_begin(ctx) != SR_OK)
579 if ((ret = libusb_bulk_transfer(ctx->usb->devhdl,
580 DSO_EP_OUT | LIBUSB_ENDPOINT_OUT,
581 cmdstring, sizeof(cmdstring),
583 sr_err("Failed to enable trigger: %d", ret);
590 SR_PRIV int dso_force_trigger(struct context *ctx)
593 uint8_t cmdstring[2];
595 sr_dbg("hantek-dso: sending CMD_FORCE_TRIGGER");
597 memset(cmdstring, 0, sizeof(cmdstring));
598 cmdstring[0] = CMD_FORCE_TRIGGER;
601 if (send_begin(ctx) != SR_OK)
604 if ((ret = libusb_bulk_transfer(ctx->usb->devhdl,
605 DSO_EP_OUT | LIBUSB_ENDPOINT_OUT,
606 cmdstring, sizeof(cmdstring),
608 sr_err("Failed to force trigger: %d", ret);
615 SR_PRIV int dso_init(struct context *ctx)
618 sr_dbg("hantek-dso: initializing dso");
620 if (get_channel_offsets(ctx) != SR_OK)
623 if (dso_set_trigger_samplerate(ctx) != SR_OK)
626 if (dso_set_filters(ctx) != SR_OK)
629 if (dso_set_voltage(ctx) != SR_OK)
632 if (dso_set_relays(ctx) != SR_OK)
635 if (dso_set_voffsets(ctx) != SR_OK)
638 if (dso_enable_trigger(ctx) != SR_OK)
644 SR_PRIV int dso_get_capturestate(struct context *ctx, uint8_t *capturestate,
645 uint32_t *trigger_offset)
648 unsigned int bitvalue, toff;
649 uint8_t cmdstring[2], inbuf[512];
651 sr_dbg("hantek-dso: sending CMD_GET_CAPTURESTATE");
653 cmdstring[0] = CMD_GET_CAPTURESTATE;
656 if ((ret = send_bulkcmd(ctx, cmdstring, sizeof(cmdstring))) != SR_OK) {
657 sr_dbg("Failed to send get_capturestate command: %d", ret);
661 if ((ret = libusb_bulk_transfer(ctx->usb->devhdl,
662 DSO_EP_IN | LIBUSB_ENDPOINT_IN,
663 inbuf, 512, &tmp, 100)) != 0) {
664 sr_dbg("Failed to get capturestate: %d", ret);
667 *capturestate = inbuf[0];
668 toff = (inbuf[1] << 16) | (inbuf[3] << 8) | inbuf[2];
670 /* This conversion comes from the openhantek project.
671 * Each set bit in the 24-bit value inverts all bits with a lower
672 * value. No idea why the device reports the trigger point this way.
675 for (i = 0; i < 24; i++) {
676 /* Each set bit inverts all bits with a lower value. */
678 toff ^= bitvalue - 1;
681 *trigger_offset = toff;
686 SR_PRIV int dso_capture_start(struct context *ctx)
689 uint8_t cmdstring[2];
691 sr_dbg("hantek-dso: sending CMD_CAPTURE_START");
693 cmdstring[0] = CMD_CAPTURE_START;
696 if ((ret = send_bulkcmd(ctx, cmdstring, sizeof(cmdstring))) != SR_OK) {
697 sr_err("Failed to send capture_start command: %d", ret);
704 SR_PRIV int dso_get_channeldata(struct context *ctx, libusb_transfer_cb_fn cb)
706 struct libusb_transfer *transfer;
707 int num_transfers, ret, i;
708 uint8_t cmdstring[2];
711 sr_dbg("hantek-dso: sending CMD_GET_CHANNELDATA");
713 cmdstring[0] = CMD_GET_CHANNELDATA;
716 if ((ret = send_bulkcmd(ctx, cmdstring, sizeof(cmdstring))) != SR_OK) {
717 sr_err("Failed to get channel data: %d", ret);
721 /* TODO: dso-2xxx only */
722 num_transfers = ctx->framesize * sizeof(unsigned short) / ctx->epin_maxpacketsize;
723 sr_dbg("hantek-dso: queueing up %d transfers", num_transfers);
724 for (i = 0; i < num_transfers; i++) {
725 if (!(buf = g_try_malloc(ctx->epin_maxpacketsize))) {
726 sr_err("hantek-dso: %s: buf malloc failed", __func__);
727 return SR_ERR_MALLOC;
729 transfer = libusb_alloc_transfer(0);
730 libusb_fill_bulk_transfer(transfer, ctx->usb->devhdl,
731 DSO_EP_IN | LIBUSB_ENDPOINT_IN, buf,
732 ctx->epin_maxpacketsize, cb, ctx, 40);
733 if ((ret = libusb_submit_transfer(transfer)) != 0) {
734 sr_err("failed to submit transfer: %d", ret);
735 /* TODO: Free them all. */
736 libusb_free_transfer(transfer);