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/>.
23 #include "sigrok-internal.h"
30 extern libusb_context *usb_context;
31 extern GSList *dev_insts;
34 static int send_begin(struct context *ctx)
37 unsigned char buffer[] = {0x0f, 0x03, 0x03, 0x03, 0x68, 0xac, 0xfe,
40 sr_dbg("hantek-dso: sending CTRL_BEGINCOMMAND");
42 if ((ret = libusb_control_transfer(ctx->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: %d", ret);
52 static int send_bulkcmd(struct context *ctx, uint8_t *cmdstring, int cmdlen)
56 if (send_begin(ctx) != SR_OK)
59 if ((ret = libusb_bulk_transfer(ctx->usb->devhdl,
60 DSO_EP_OUT | LIBUSB_ENDPOINT_OUT,
61 cmdstring, cmdlen, &tmp, 200)) != 0)
67 SR_PRIV int dso_getmps(libusb_device *dev)
69 struct libusb_device_descriptor des;
70 struct libusb_config_descriptor *conf_dsc;
71 const struct libusb_interface_descriptor *intf_dsc;
74 if (libusb_get_device_descriptor(dev, &des) != 0)
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(int dev_index)
109 libusb_device **devlist;
110 struct libusb_device_descriptor des;
111 struct sr_dev_inst *sdi;
115 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
119 if (sdi->status == SR_ST_ACTIVE)
124 libusb_get_device_list(usb_context, &devlist);
125 for (i = 0; devlist[i]; i++) {
126 if ((err = libusb_get_device_descriptor(devlist[i], &des))) {
127 sr_err("hantek-dso: failed to get device descriptor: %d", err);
131 if (des.idVendor != ctx->profile->fw_vid
132 || des.idProduct != ctx->profile->fw_pid)
135 if (sdi->status == SR_ST_INITIALIZING) {
136 if (skip != dev_index) {
137 /* Skip devices of this type that aren't the one we want. */
141 } else if (sdi->status == SR_ST_INACTIVE) {
143 * This device is fully enumerated, so we need to find
144 * this device by vendor, product, bus and address.
146 if (libusb_get_bus_number(devlist[i]) != ctx->usb->bus
147 || libusb_get_device_address(devlist[i]) != ctx->usb->address)
148 /* this is not the one */
152 if (!(err = libusb_open(devlist[i], &ctx->usb->devhdl))) {
153 if (ctx->usb->address == 0xff)
155 * first time we touch this device after firmware upload,
156 * so we don't know the address yet.
158 ctx->usb->address = libusb_get_device_address(devlist[i]);
160 if(!(ctx->epin_maxpacketsize = dso_getmps(devlist[i])))
161 sr_err("hantek-dso: wrong endpoint profile");
163 sdi->status = SR_ST_ACTIVE;
164 sr_info("hantek-dso: opened device %d on %d.%d interface %d",
165 sdi->index, ctx->usb->bus,
166 ctx->usb->address, USB_INTERFACE);
169 sr_err("hantek-dso: failed to open device: %d", err);
172 /* if we made it here, we handled the device one way or another */
175 libusb_free_device_list(devlist, 1);
177 if (sdi->status != SR_ST_ACTIVE)
183 SR_PRIV void dso_close(struct sr_dev_inst *sdi)
189 if (ctx->usb->devhdl == NULL)
192 sr_info("hantek-dso: closing device %d on %d.%d interface %d", sdi->index,
193 ctx->usb->bus, ctx->usb->address, USB_INTERFACE);
194 libusb_release_interface(ctx->usb->devhdl, USB_INTERFACE);
195 libusb_close(ctx->usb->devhdl);
196 ctx->usb->devhdl = NULL;
197 sdi->status = SR_ST_INACTIVE;
201 static int get_channel_offsets(struct context *ctx)
205 sr_dbg("hantek-dso: getting channel offsets");
207 ret = libusb_control_transfer(ctx->usb->devhdl,
208 LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR,
209 CTRL_READ_EEPROM, EEPROM_CHANNEL_OFFSETS, 0,
210 (unsigned char *)&ctx->channel_levels,
211 sizeof(ctx->channel_levels), 200);
212 if (ret != sizeof(ctx->channel_levels)) {
213 sr_err("failed to get channel offsets: %d", ret);
220 SR_PRIV int dso_set_trigger_samplerate(struct context *ctx)
223 uint8_t cmdstring[12];
224 uint16_t timebase_small[] = { 0xffff, 0xfffc, 0xfff7, 0xffe8, 0xffce,
225 0xff9c, 0xff07, 0xfe0d, 0xfc19, 0xf63d, 0xec79, 0xd8f1 };
226 uint16_t timebase_large[] = { 0xffff, 0x0000, 0xfffc, 0xfff7, 0xffe8,
227 0xffce, 0xff9d, 0xff07, 0xfe0d, 0xfc19, 0xf63d, 0xec79 };
229 sr_dbg("hantek-dso: preparing CMD_SET_TRIGGER_SAMPLERATE");
231 memset(cmdstring, 0, sizeof(cmdstring));
233 cmdstring[0] = CMD_SET_TRIGGER_SAMPLERATE;
236 sr_dbg("hantek-dso: trigger source %s", ctx->triggersource);
237 if (!strcmp("CH2", ctx->triggersource))
239 else if (!strcmp("CH1", ctx->triggersource))
241 else if (!strcmp("EXT", ctx->triggersource))
244 sr_err("hantek-dso: invalid trigger source %s", ctx->triggersource);
250 sr_dbg("hantek-dso: frame size %d", ctx->framesize);
251 cmdstring[2] |= (ctx->framesize == FRAMESIZE_SMALL ? 0x01 : 0x02) << 2;
254 sr_dbg("hantek-dso: time base index %d", ctx->timebase);
255 switch (ctx->framesize) {
256 case FRAMESIZE_SMALL:
257 if (ctx->timebase < TIME_20us)
259 else if (ctx->timebase == TIME_20us)
261 else if (ctx->timebase == TIME_40us)
263 else if (ctx->timebase == TIME_100us)
265 else if (ctx->timebase >= TIME_200us)
268 case FRAMESIZE_LARGE:
269 if (ctx->timebase < TIME_40us) {
270 sr_err("hantek-dso: timebase < 40us only supported with 10K buffer");
273 else if (ctx->timebase == TIME_40us)
275 else if (ctx->timebase == TIME_100us)
277 else if (ctx->timebase == TIME_200us)
279 else if (ctx->timebase >= TIME_400us)
283 cmdstring[2] |= (tmp & 0x07) << 5;
285 /* Enabled channels: 00=CH1 01=CH2 10=both */
286 sr_dbg("hantek-dso: channels CH1=%d CH2=%d", ctx->ch1_enabled, ctx->ch2_enabled);
287 tmp = (((ctx->ch2_enabled ? 1 : 0) << 1) + (ctx->ch1_enabled ? 1 : 0)) - 1;
290 /* Fast rates channel */
291 /* TODO: is this right? */
292 tmp = ctx->timebase < TIME_10us ? 1 : 0;
293 cmdstring[3] |= tmp << 2;
295 /* Trigger slope: 0=positive 1=negative */
296 /* TODO: does this work? */
297 sr_dbg("hantek-dso: trigger slope %d", ctx->triggerslope);
298 cmdstring[3] |= (ctx->triggerslope == SLOPE_NEGATIVE ? 1 : 0) << 3;
301 if (ctx->timebase < TIME_100us)
303 else if (ctx->timebase > TIME_400ms)
306 if (ctx->framesize == FRAMESIZE_SMALL)
307 tmp = timebase_small[ctx->timebase - 3];
309 tmp = timebase_large[ctx->timebase - 3];
311 cmdstring[4] = tmp & 0xff;
312 cmdstring[5] = (tmp >> 8) & 0xff;
314 /* Horizontal trigger position */
315 sr_dbg("hantek-dso: trigger position %3.2f", ctx->triggerposition);
316 tmp = 0x77fff + 0x8000 * ctx->triggerposition;
317 cmdstring[6] = tmp & 0xff;
318 cmdstring[7] = (tmp >> 8) & 0xff;
319 cmdstring[10] = (tmp >> 16) & 0xff;
321 if (send_begin(ctx) != SR_OK)
324 if ((ret = libusb_bulk_transfer(ctx->usb->devhdl,
325 DSO_EP_OUT | LIBUSB_ENDPOINT_OUT,
326 cmdstring, sizeof(cmdstring),
328 sr_err("Failed to set trigger/samplerate: %d", ret);
331 sr_dbg("hantek-dso: sent CMD_SET_TRIGGER_SAMPLERATE");
336 SR_PRIV int dso_set_filters(struct context *ctx)
339 uint8_t cmdstring[8];
341 sr_dbg("hantek-dso: sending CMD_SET_FILTERS");
343 memset(cmdstring, 0, sizeof(cmdstring));
344 cmdstring[0] = CMD_SET_FILTERS;
347 cmdstring[2] |= 0x80;
349 cmdstring[2] |= 0x40;
350 if (ctx->filter_trigger)
351 cmdstring[2] |= 0x20;
353 if (send_begin(ctx) != SR_OK)
356 if ((ret = libusb_bulk_transfer(ctx->usb->devhdl,
357 DSO_EP_OUT | LIBUSB_ENDPOINT_OUT,
358 cmdstring, sizeof(cmdstring),
360 sr_err("Failed to set filters: %d", ret);
367 SR_PRIV int dso_set_voltage(struct context *ctx)
370 uint8_t cmdstring[8];
372 sr_dbg("hantek-dso: sending CMD_SET_VOLTAGE");
374 memset(cmdstring, 0, sizeof(cmdstring));
375 cmdstring[0] = CMD_SET_VOLTAGE;
378 cmdstring[2] |= ((2 - ctx->voltage_ch1 % 3) << 6);
379 cmdstring[2] |= ((2 - ctx->voltage_ch2 % 3) << 4);
382 if (send_begin(ctx) != SR_OK)
385 if ((ret = libusb_bulk_transfer(ctx->usb->devhdl,
386 DSO_EP_OUT | LIBUSB_ENDPOINT_OUT,
387 cmdstring, sizeof(cmdstring),
389 sr_err("Failed to set voltage: %d", ret);
396 SR_PRIV int dso_set_relays(struct context *ctx)
399 uint8_t relays[] = { 0x00, 0x04, 0x08, 0x02, 0x20, 0x40, 0x10, 0x01,
400 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
402 sr_dbg("hantek-dso: sending CTRL_SETRELAYS");
404 cv1 = ctx->voltage_ch1 / 3;
405 cv2 = ctx->voltage_ch2 / 3;
408 relays[1] = ~relays[1];
411 relays[2] = ~relays[2];
413 if (ctx->coupling_ch1 != COUPLING_AC)
414 relays[3] = ~relays[3];
417 relays[4] = ~relays[4];
420 relays[5] = ~relays[5];
422 if (ctx->coupling_ch2 != COUPLING_AC)
423 relays[6] = ~relays[6];
425 if (!strcmp(ctx->triggersource, "EXT"))
426 relays[7] = ~relays[7];
428 if ((ret = libusb_control_transfer(ctx->usb->devhdl,
429 LIBUSB_REQUEST_TYPE_VENDOR, CTRL_SETRELAYS,
430 0, 0, relays, sizeof(relays), 100)) != sizeof(relays)) {
431 sr_err("failed to set relays: %d", ret);
438 SR_PRIV int dso_set_voffsets(struct context *ctx)
442 // uint8_t offsets[17];
443 uint8_t offsets[] = {0x20,0x75,0x30,0x3f,0x20,0xbd,0x3f,0x02,0x20,0x00,0x71,0x01,0x2e,0x0b,0x3f,0x02,0x50};
444 //uint8_t offsets[] = {0xff, 0x75, 0x30, 0x3f, 0x20, 0xbd,
445 // 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
448 sr_dbg("hantek-dso: sending CTRL_SETOFFSET");
450 // memset(offsets, 0, sizeof(offsets));
452 // ch_levels = ctx->channel_levels[0][VOLTAGE_10mV - ctx->voltage_ch1];
453 // offset = (ch_levels[1] - ch_levels[0]) * ctx->voffset_ch1 + ch_levels[0];
454 // offsets[0] = (offset >> 8) | 0x20;
455 // offsets[1] = offset & 0xff;
458 // ch_levels = ctx->channel_levels[1][VOLTAGE_10mV - ctx->voltage_ch2];
459 // offset = (ch_levels[1] - ch_levels[0]) * ctx->voffset_ch2 + ch_levels[0];
460 // offsets[2] = (offset >> 8) | 0x20;
461 // offsets[3] = offset & 0xff;
464 // offset = MAX_VERT_TRIGGER * ctx->voffset_trigger;
465 // offsets[4] = (offset >> 8) | 0x20;
466 // offsets[5] = offset & 0xff;
468 if ((ret = libusb_control_transfer(ctx->usb->devhdl,
469 LIBUSB_REQUEST_TYPE_VENDOR, CTRL_SETOFFSET,
470 0, 0, offsets, sizeof(offsets), 100)) != sizeof(offsets)) {
471 sr_err("failed to set offsets: %d", ret);
478 SR_PRIV int dso_enable_trigger(struct context *ctx)
481 uint8_t cmdstring[2];
483 sr_dbg("hantek-dso: sending CMD_ENABLE_TRIGGER");
485 memset(cmdstring, 0, sizeof(cmdstring));
486 cmdstring[0] = CMD_ENABLE_TRIGGER;
489 if (send_begin(ctx) != SR_OK)
492 if ((ret = libusb_bulk_transfer(ctx->usb->devhdl,
493 DSO_EP_OUT | LIBUSB_ENDPOINT_OUT,
494 cmdstring, sizeof(cmdstring),
496 sr_err("Failed to enable trigger: %d", ret);
503 SR_PRIV int dso_force_trigger(struct context *ctx)
506 uint8_t cmdstring[2];
508 sr_dbg("hantek-dso: sending CMD_FORCE_TRIGGER");
510 memset(cmdstring, 0, sizeof(cmdstring));
511 cmdstring[0] = CMD_FORCE_TRIGGER;
514 if (send_begin(ctx) != SR_OK)
517 if ((ret = libusb_bulk_transfer(ctx->usb->devhdl,
518 DSO_EP_OUT | LIBUSB_ENDPOINT_OUT,
519 cmdstring, sizeof(cmdstring),
521 sr_err("Failed to force trigger: %d", ret);
528 SR_PRIV int dso_init(struct context *ctx)
531 sr_dbg("hantek-dso: initializing dso");
533 if (get_channel_offsets(ctx) != SR_OK)
536 if (dso_set_trigger_samplerate(ctx) != SR_OK)
539 if (dso_set_filters(ctx) != SR_OK)
542 if (dso_set_voltage(ctx) != SR_OK)
545 if (dso_set_relays(ctx) != SR_OK)
548 if (dso_set_voffsets(ctx) != SR_OK)
551 if (dso_enable_trigger(ctx) != SR_OK)
554 if (dso_force_trigger(ctx) != SR_OK)
560 SR_PRIV uint8_t dso_get_capturestate(struct context *ctx)
563 uint8_t cmdstring[2], inbuf[512];
565 sr_dbg("hantek-dso: sending CMD_GET_CAPTURESTATE");
567 cmdstring[0] = CMD_GET_CAPTURESTATE;
570 if ((ret = send_bulkcmd(ctx, cmdstring, sizeof(cmdstring))) != SR_OK) {
571 sr_dbg("Failed to send get_capturestate command: %d", ret);
572 return CAPTURE_UNKNOWN;
575 if ((ret = libusb_bulk_transfer(ctx->usb->devhdl,
576 DSO_EP_IN | LIBUSB_ENDPOINT_IN,
577 inbuf, 512, &tmp, 100)) != 0) {
578 sr_dbg("Failed to get capturestate: %d", ret);
579 return CAPTURE_UNKNOWN;
585 SR_PRIV uint8_t dso_capture_start(struct context *ctx)
588 uint8_t cmdstring[2];
590 sr_dbg("hantek-dso: sending CMD_CAPTURE_START");
592 cmdstring[0] = CMD_CAPTURE_START;
595 if ((ret = send_bulkcmd(ctx, cmdstring, sizeof(cmdstring))) != SR_OK) {
596 sr_err("Failed to send capture_start command: %d", ret);
603 SR_PRIV int dso_get_channeldata(struct context *ctx, libusb_transfer_cb_fn cb)
605 struct libusb_transfer *transfer;
606 int num_transfers, ret, i;
607 uint8_t cmdstring[2];
610 sr_dbg("hantek-dso: sending CMD_GET_CHANNELDATA");
612 cmdstring[0] = CMD_GET_CHANNELDATA;
615 if ((ret = send_bulkcmd(ctx, cmdstring, sizeof(cmdstring))) != SR_OK) {
616 sr_err("Failed to get channel data: %d", ret);
620 /* TODO: dso-2xxx only */
621 num_transfers = ctx->framesize * sizeof(unsigned short) / ctx->epin_maxpacketsize;
622 sr_dbg("hantek-dso: queueing up %d transfers", num_transfers);
623 for (i = 0; i < num_transfers; i++) {
624 if (!(buf = g_try_malloc(ctx->epin_maxpacketsize))) {
625 sr_err("hantek-dso: %s: buf malloc failed", __func__);
626 return SR_ERR_MALLOC;
628 transfer = libusb_alloc_transfer(0);
629 libusb_fill_bulk_transfer(transfer, ctx->usb->devhdl,
630 DSO_EP_IN | LIBUSB_ENDPOINT_IN, buf,
631 ctx->epin_maxpacketsize, cb, ctx, 40);
632 if ((ret = libusb_submit_transfer(transfer)) != 0) {
633 sr_err("failed to submit transfer: %d", ret);
634 /* TODO: Free them all. */
635 libusb_free_transfer(transfer);