]> sigrok.org Git - libsigrok.git/blame - hardware/hantek-dso/dso.c
sr: driver struct gets a more generic *priv instead of GSList *instances
[libsigrok.git] / hardware / hantek-dso / dso.c
CommitLineData
3b533202
BV
1/*
2 * This file is part of the sigrok project.
3 *
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>
7 *
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.
12 *
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.
17 *
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/>.
20 */
21
45c59c8b
BV
22#include "libsigrok.h"
23#include "libsigrok-internal.h"
3b533202
BV
24#include "config.h"
25#include "dso.h"
26#include <string.h>
27#include <glib.h>
28#include <libusb.h>
29
30extern libusb_context *usb_context;
f8c617cf 31extern struct sr_dev_driver hantek_dso_driver_info;
3b533202
BV
32
33
34static int send_begin(struct context *ctx)
35{
36 int ret;
37 unsigned char buffer[] = {0x0f, 0x03, 0x03, 0x03, 0x68, 0xac, 0xfe,
38 0x00, 0x01, 0x00};
39
40 sr_dbg("hantek-dso: sending CTRL_BEGINCOMMAND");
41
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);
46 return SR_ERR;
47 }
48
49 return SR_OK;
50}
51
52static int send_bulkcmd(struct context *ctx, uint8_t *cmdstring, int cmdlen)
53{
54 int ret, tmp;
55
56 if (send_begin(ctx) != SR_OK)
57 return SR_ERR;
58
59 if ((ret = libusb_bulk_transfer(ctx->usb->devhdl,
60 DSO_EP_OUT | LIBUSB_ENDPOINT_OUT,
61 cmdstring, cmdlen, &tmp, 200)) != 0)
62 return SR_ERR;
63
64 return SR_OK;
65}
66
67SR_PRIV int dso_getmps(libusb_device *dev)
68{
69 struct libusb_device_descriptor des;
70 struct libusb_config_descriptor *conf_dsc;
71 const struct libusb_interface_descriptor *intf_dsc;
72 int mps;
73
74 if (libusb_get_device_descriptor(dev, &des) != 0)
75 return 0;
76
77 if (des.bNumConfigurations != 1)
78 return 0;
79
80 if (libusb_get_config_descriptor(dev, 0, &conf_dsc) != 0)
81 return 0;
82
83 mps = 0;
84 intf_dsc = &(conf_dsc->interface[0].altsetting[0]);
85 if (intf_dsc->bNumEndpoints != 2)
86 goto err;
87
88 if ((intf_dsc->endpoint[0].bEndpointAddress & 0x8f) !=
89 (2 | LIBUSB_ENDPOINT_OUT))
90 /* The first endpoint should be 2 (outbound). */
91 goto err;
92
93 if ((intf_dsc->endpoint[1].bEndpointAddress & 0x8f) !=
94 (6 | LIBUSB_ENDPOINT_IN))
95 /* The second endpoint should be 6 (inbound). */
96 goto err;
97
98 mps = intf_dsc->endpoint[1].wMaxPacketSize;
99
100err:
101 if (conf_dsc)
102 libusb_free_config_descriptor(conf_dsc);
103
104 return mps;
105}
106
25a0f108 107SR_PRIV int dso_open(struct sr_dev_inst *sdi)
3b533202
BV
108{
109 libusb_device **devlist;
110 struct libusb_device_descriptor des;
3b533202
BV
111 struct context *ctx;
112 int err, skip, i;
113
3b533202
BV
114 ctx = sdi->priv;
115
116 if (sdi->status == SR_ST_ACTIVE)
117 /* already in use */
118 return SR_ERR;
119
120 skip = 0;
121 libusb_get_device_list(usb_context, &devlist);
122 for (i = 0; devlist[i]; i++) {
123 if ((err = libusb_get_device_descriptor(devlist[i], &des))) {
124 sr_err("hantek-dso: failed to get device descriptor: %d", err);
125 continue;
126 }
127
128 if (des.idVendor != ctx->profile->fw_vid
129 || des.idProduct != ctx->profile->fw_pid)
130 continue;
131
132 if (sdi->status == SR_ST_INITIALIZING) {
25a0f108 133 if (skip != sdi->index) {
3b533202
BV
134 /* Skip devices of this type that aren't the one we want. */
135 skip += 1;
136 continue;
137 }
138 } else if (sdi->status == SR_ST_INACTIVE) {
139 /*
140 * This device is fully enumerated, so we need to find
141 * this device by vendor, product, bus and address.
142 */
143 if (libusb_get_bus_number(devlist[i]) != ctx->usb->bus
144 || libusb_get_device_address(devlist[i]) != ctx->usb->address)
145 /* this is not the one */
146 continue;
147 }
148
149 if (!(err = libusb_open(devlist[i], &ctx->usb->devhdl))) {
150 if (ctx->usb->address == 0xff)
151 /*
152 * first time we touch this device after firmware upload,
153 * so we don't know the address yet.
154 */
155 ctx->usb->address = libusb_get_device_address(devlist[i]);
156
157 if(!(ctx->epin_maxpacketsize = dso_getmps(devlist[i])))
158 sr_err("hantek-dso: wrong endpoint profile");
159 else {
160 sdi->status = SR_ST_ACTIVE;
161 sr_info("hantek-dso: opened device %d on %d.%d interface %d",
162 sdi->index, ctx->usb->bus,
163 ctx->usb->address, USB_INTERFACE);
164 }
165 } else {
166 sr_err("hantek-dso: failed to open device: %d", err);
167 }
168
169 /* if we made it here, we handled the device one way or another */
170 break;
171 }
172 libusb_free_device_list(devlist, 1);
173
174 if (sdi->status != SR_ST_ACTIVE)
175 return SR_ERR;
176
177 return SR_OK;
178}
179
180SR_PRIV void dso_close(struct sr_dev_inst *sdi)
181{
182 struct context *ctx;
183
184 ctx = sdi->priv;
185
186 if (ctx->usb->devhdl == NULL)
187 return;
188
189 sr_info("hantek-dso: closing device %d on %d.%d interface %d", sdi->index,
190 ctx->usb->bus, ctx->usb->address, USB_INTERFACE);
191 libusb_release_interface(ctx->usb->devhdl, USB_INTERFACE);
192 libusb_close(ctx->usb->devhdl);
193 ctx->usb->devhdl = NULL;
194 sdi->status = SR_ST_INACTIVE;
195
196}
197
198static int get_channel_offsets(struct context *ctx)
199{
2715c0b8
BV
200 GString *gs;
201 int chan, v, ret;
3b533202
BV
202
203 sr_dbg("hantek-dso: getting channel offsets");
204
205 ret = libusb_control_transfer(ctx->usb->devhdl,
206 LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR,
207 CTRL_READ_EEPROM, EEPROM_CHANNEL_OFFSETS, 0,
208 (unsigned char *)&ctx->channel_levels,
209 sizeof(ctx->channel_levels), 200);
210 if (ret != sizeof(ctx->channel_levels)) {
211 sr_err("failed to get channel offsets: %d", ret);
212 return SR_ERR;
213 }
214
2715c0b8
BV
215 /* Comes in as 16-bit numbers with the second byte always 0 on
216 * the DSO-2090. Guessing this is supposed to be big-endian,
217 * since that's how voltage offsets are submitted back to the DSO.
218 * Convert to host order now, so we can use them natively.
219 */
220 for (chan = 0; chan < 2; chan++) {
221 for (v = 0; v < 9; v++) {
384c28d9
UH
222 ctx->channel_levels[chan][v][0] = g_ntohs(ctx->channel_levels[chan][v][0]);
223 ctx->channel_levels[chan][v][1] = g_ntohs(ctx->channel_levels[chan][v][1]);
2715c0b8
BV
224 }
225 }
226
227 if (sr_log_loglevel_get() >= SR_LOG_DBG) {
228 gs = g_string_sized_new(128);
229 for (chan = 0; chan < 2; chan++) {
230 g_string_printf(gs, "hantek-dso: CH%d:", chan + 1);
231 for (v = 0; v < 9; v++) {
232 g_string_append_printf(gs, " %.4x-%.4x",
233 ctx->channel_levels[chan][v][0],
234 ctx->channel_levels[chan][v][1]);
235 }
236 sr_dbg(gs->str);
237 }
238 g_string_free(gs, TRUE);
239 }
240
3b533202
BV
241 return SR_OK;
242}
243
244SR_PRIV int dso_set_trigger_samplerate(struct context *ctx)
245{
246 int ret, tmp;
247 uint8_t cmdstring[12];
3b533202
BV
248 uint16_t timebase_small[] = { 0xffff, 0xfffc, 0xfff7, 0xffe8, 0xffce,
249 0xff9c, 0xff07, 0xfe0d, 0xfc19, 0xf63d, 0xec79, 0xd8f1 };
250 uint16_t timebase_large[] = { 0xffff, 0x0000, 0xfffc, 0xfff7, 0xffe8,
251 0xffce, 0xff9d, 0xff07, 0xfe0d, 0xfc19, 0xf63d, 0xec79 };
252
a370ef19 253 sr_dbg("hantek-dso: preparing CMD_SET_TRIGGER_SAMPLERATE");
3b533202
BV
254
255 memset(cmdstring, 0, sizeof(cmdstring));
256 /* Command */
257 cmdstring[0] = CMD_SET_TRIGGER_SAMPLERATE;
258
259 /* Trigger source */
a370ef19
BV
260 sr_dbg("hantek-dso: trigger source %s", ctx->triggersource);
261 if (!strcmp("CH2", ctx->triggersource))
262 tmp = 0;
263 else if (!strcmp("CH1", ctx->triggersource))
264 tmp = 1;
265 else if (!strcmp("EXT", ctx->triggersource))
266 tmp = 2;
267 else {
268 sr_err("hantek-dso: invalid trigger source %s", ctx->triggersource);
269 return SR_ERR_ARG;
270 }
271 cmdstring[2] = tmp;
3b533202
BV
272
273 /* Frame size */
a370ef19 274 sr_dbg("hantek-dso: frame size %d", ctx->framesize);
bc79e906
BV
275 cmdstring[2] |= (ctx->framesize == FRAMESIZE_SMALL ? 0x01 : 0x02) << 2;
276
277 /* Timebase fast */
a370ef19 278 sr_dbg("hantek-dso: time base index %d", ctx->timebase);
bc79e906
BV
279 switch (ctx->framesize) {
280 case FRAMESIZE_SMALL:
281 if (ctx->timebase < TIME_20us)
282 tmp = 0;
283 else if (ctx->timebase == TIME_20us)
284 tmp = 1;
285 else if (ctx->timebase == TIME_40us)
286 tmp = 2;
287 else if (ctx->timebase == TIME_100us)
288 tmp = 3;
289 else if (ctx->timebase >= TIME_200us)
290 tmp = 4;
291 break;
292 case FRAMESIZE_LARGE:
293 if (ctx->timebase < TIME_40us) {
294 sr_err("hantek-dso: timebase < 40us only supported with 10K buffer");
295 return SR_ERR_ARG;
296 }
297 else if (ctx->timebase == TIME_40us)
298 tmp = 0;
299 else if (ctx->timebase == TIME_100us)
300 tmp = 2;
301 else if (ctx->timebase == TIME_200us)
302 tmp = 3;
303 else if (ctx->timebase >= TIME_400us)
304 tmp = 4;
305 break;
3b533202 306 }
bc79e906
BV
307 cmdstring[2] |= (tmp & 0x07) << 5;
308
309 /* Enabled channels: 00=CH1 01=CH2 10=both */
a370ef19 310 sr_dbg("hantek-dso: channels CH1=%d CH2=%d", ctx->ch1_enabled, ctx->ch2_enabled);
6e71ef3b
BV
311 tmp = (((ctx->ch2_enabled ? 1 : 0) << 1) + (ctx->ch1_enabled ? 1 : 0)) - 1;
312 cmdstring[3] = tmp;
3b533202 313
bc79e906
BV
314 /* Fast rates channel */
315 /* TODO: is this right? */
316 tmp = ctx->timebase < TIME_10us ? 1 : 0;
317 cmdstring[3] |= tmp << 2;
3b533202 318
bc79e906 319 /* Trigger slope: 0=positive 1=negative */
a370ef19
BV
320 /* TODO: does this work? */
321 sr_dbg("hantek-dso: trigger slope %d", ctx->triggerslope);
bc79e906 322 cmdstring[3] |= (ctx->triggerslope == SLOPE_NEGATIVE ? 1 : 0) << 3;
3b533202 323
a370ef19 324 /* Timebase slow */
3b533202
BV
325 if (ctx->timebase < TIME_100us)
326 tmp = 0;
327 else if (ctx->timebase > TIME_400ms)
328 tmp = 0xffed;
329 else {
330 if (ctx->framesize == FRAMESIZE_SMALL)
331 tmp = timebase_small[ctx->timebase - 3];
332 else
333 tmp = timebase_large[ctx->timebase - 3];
334 }
bc79e906
BV
335 cmdstring[4] = tmp & 0xff;
336 cmdstring[5] = (tmp >> 8) & 0xff;
337
338 /* Horizontal trigger position */
a370ef19 339 sr_dbg("hantek-dso: trigger position %3.2f", ctx->triggerposition);
bc79e906 340 tmp = 0x77fff + 0x8000 * ctx->triggerposition;
3b533202
BV
341 cmdstring[6] = tmp & 0xff;
342 cmdstring[7] = (tmp >> 8) & 0xff;
3b533202
BV
343 cmdstring[10] = (tmp >> 16) & 0xff;
344
345 if (send_begin(ctx) != SR_OK)
346 return SR_ERR;
347
348 if ((ret = libusb_bulk_transfer(ctx->usb->devhdl,
349 DSO_EP_OUT | LIBUSB_ENDPOINT_OUT,
350 cmdstring, sizeof(cmdstring),
351 &tmp, 100)) != 0) {
352 sr_err("Failed to set trigger/samplerate: %d", ret);
353 return SR_ERR;
354 }
a370ef19 355 sr_dbg("hantek-dso: sent CMD_SET_TRIGGER_SAMPLERATE");
3b533202
BV
356
357 return SR_OK;
358}
359
360SR_PRIV int dso_set_filters(struct context *ctx)
361{
362 int ret, tmp;
363 uint8_t cmdstring[8];
364
ebb781a6 365 sr_dbg("hantek-dso: preparing CMD_SET_FILTERS");
3b533202
BV
366
367 memset(cmdstring, 0, sizeof(cmdstring));
368 cmdstring[0] = CMD_SET_FILTERS;
369 cmdstring[1] = 0x0f;
ebb781a6
BV
370 if (ctx->filter_ch1) {
371 sr_dbg("hantek-dso: turning on CH1 filter");
3b533202 372 cmdstring[2] |= 0x80;
ebb781a6
BV
373 }
374 if (ctx->filter_ch2) {
375 sr_dbg("hantek-dso: turning on CH2 filter");
3b533202 376 cmdstring[2] |= 0x40;
ebb781a6
BV
377 }
378 if (ctx->filter_trigger) {
379 /* TODO: supported on the DSO-2090? */
380 sr_dbg("hantek-dso: turning on trigger filter");
3b533202 381 cmdstring[2] |= 0x20;
ebb781a6 382 }
3b533202
BV
383
384 if (send_begin(ctx) != SR_OK)
385 return SR_ERR;
386
387 if ((ret = libusb_bulk_transfer(ctx->usb->devhdl,
388 DSO_EP_OUT | LIBUSB_ENDPOINT_OUT,
389 cmdstring, sizeof(cmdstring),
390 &tmp, 100)) != 0) {
391 sr_err("Failed to set filters: %d", ret);
392 return SR_ERR;
393 }
ebb781a6 394 sr_dbg("hantek-dso: sent CMD_SET_FILTERS");
3b533202
BV
395
396 return SR_OK;
397}
398
399SR_PRIV int dso_set_voltage(struct context *ctx)
400{
401 int ret, tmp;
402 uint8_t cmdstring[8];
403
313deed2 404 sr_dbg("hantek-dso: preparing CMD_SET_VOLTAGE");
3b533202
BV
405
406 memset(cmdstring, 0, sizeof(cmdstring));
407 cmdstring[0] = CMD_SET_VOLTAGE;
408 cmdstring[1] = 0x0f;
313deed2
BV
409 cmdstring[2] = 0x30;
410
411 /* CH1 volts/div is encoded in bits 0-1 */
412 sr_dbg("hantek-dso: CH1 vdiv index %d", ctx->voltage_ch1);
413 switch (ctx->voltage_ch1) {
414 case VDIV_1V:
415 case VDIV_100MV:
416 case VDIV_10MV:
417 cmdstring[2] |= 0x00;
418 break;
419 case VDIV_2V:
420 case VDIV_200MV:
421 case VDIV_20MV:
422 cmdstring[2] |= 0x01;
423 break;
424 case VDIV_5V:
425 case VDIV_500MV:
426 case VDIV_50MV:
427 cmdstring[2] |= 0x02;
428 break;
429 }
430
431 /* CH2 volts/div is encoded in bits 2-3 */
432 sr_dbg("hantek-dso: CH2 vdiv index %d", ctx->voltage_ch2);
433 switch (ctx->voltage_ch2) {
434 case VDIV_1V:
435 case VDIV_100MV:
436 case VDIV_10MV:
437 cmdstring[2] |= 0x00;
438 break;
439 case VDIV_2V:
440 case VDIV_200MV:
441 case VDIV_20MV:
1d97091e 442 cmdstring[2] |= 0x04;
313deed2
BV
443 break;
444 case VDIV_5V:
445 case VDIV_500MV:
446 case VDIV_50MV:
1d97091e 447 cmdstring[2] |= 0x08;
313deed2
BV
448 break;
449 }
3b533202
BV
450
451 if (send_begin(ctx) != SR_OK)
452 return SR_ERR;
453
454 if ((ret = libusb_bulk_transfer(ctx->usb->devhdl,
455 DSO_EP_OUT | LIBUSB_ENDPOINT_OUT,
456 cmdstring, sizeof(cmdstring),
457 &tmp, 100)) != 0) {
458 sr_err("Failed to set voltage: %d", ret);
459 return SR_ERR;
460 }
313deed2 461 sr_dbg("hantek-dso: sent CMD_SET_VOLTAGE");
3b533202
BV
462
463 return SR_OK;
464}
465
466SR_PRIV int dso_set_relays(struct context *ctx)
467{
a217bcdf
BV
468 GString *gs;
469 int ret, i;
470 uint8_t relays[17] = { 0x00, 0x04, 0x08, 0x02, 0x20, 0x40, 0x10, 0x01,
3b533202
BV
471 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
472
b58fbd99 473 sr_dbg("hantek-dso: preparing CTRL_SETRELAYS");
3b533202 474
a217bcdf 475 if (ctx->voltage_ch1 < VDIV_1V)
3b533202
BV
476 relays[1] = ~relays[1];
477
a217bcdf 478 if (ctx->voltage_ch1 < VDIV_100MV)
3b533202
BV
479 relays[2] = ~relays[2];
480
b58fbd99 481 sr_dbg("hantek-dso: CH1 coupling %d", ctx->coupling_ch1);
3b533202
BV
482 if (ctx->coupling_ch1 != COUPLING_AC)
483 relays[3] = ~relays[3];
484
a217bcdf 485 if (ctx->voltage_ch2 < VDIV_1V)
3b533202
BV
486 relays[4] = ~relays[4];
487
a217bcdf 488 if (ctx->voltage_ch2 < VDIV_100MV)
3b533202
BV
489 relays[5] = ~relays[5];
490
b58fbd99 491 sr_dbg("hantek-dso: CH2 coupling %d", ctx->coupling_ch1);
3b533202
BV
492 if (ctx->coupling_ch2 != COUPLING_AC)
493 relays[6] = ~relays[6];
494
a370ef19 495 if (!strcmp(ctx->triggersource, "EXT"))
3b533202
BV
496 relays[7] = ~relays[7];
497
a217bcdf
BV
498 if (sr_log_loglevel_get() >= SR_LOG_DBG) {
499 gs = g_string_sized_new(128);
500 g_string_printf(gs, "hantek-dso: relays:");
501 for (i = 0; i < 17; i++)
502 g_string_append_printf(gs, " %.2x", relays[i]);
503 sr_dbg(gs->str);
504 g_string_free(gs, TRUE);
505 }
506
3b533202
BV
507 if ((ret = libusb_control_transfer(ctx->usb->devhdl,
508 LIBUSB_REQUEST_TYPE_VENDOR, CTRL_SETRELAYS,
a217bcdf 509 0, 0, relays, 17, 100)) != sizeof(relays)) {
3b533202
BV
510 sr_err("failed to set relays: %d", ret);
511 return SR_ERR;
512 }
b58fbd99 513 sr_dbg("hantek-dso: sent CTRL_SETRELAYS");
3b533202
BV
514
515 return SR_OK;
516}
517
518SR_PRIV int dso_set_voffsets(struct context *ctx)
519{
520 int offset, ret;
521 uint16_t *ch_levels;
2715c0b8
BV
522 uint8_t offsets[17];
523
524 sr_dbg("hantek-dso: preparing CTRL_SETOFFSET");
525
526 memset(offsets, 0, sizeof(offsets));
527 /* Channel 1 */
528 ch_levels = ctx->channel_levels[0][ctx->voltage_ch1];
529 offset = (ch_levels[1] - ch_levels[0]) * ctx->voffset_ch1 + ch_levels[0];
530 offsets[0] = (offset >> 8) | 0x20;
531 offsets[1] = offset & 0xff;
532 sr_dbg("hantek-dso: CH1 offset %3.2f (%.2x%.2x)", ctx->voffset_ch1,
533 offsets[0], offsets[1]);
534
535 /* Channel 2 */
536 ch_levels = ctx->channel_levels[1][ctx->voltage_ch2];
537 offset = (ch_levels[1] - ch_levels[0]) * ctx->voffset_ch2 + ch_levels[0];
538 offsets[2] = (offset >> 8) | 0x20;
539 offsets[3] = offset & 0xff;
540 sr_dbg("hantek-dso: CH2 offset %3.2f (%.2x%.2x)", ctx->voffset_ch2,
541 offsets[2], offsets[3]);
542
543 /* Trigger */
544 offset = MAX_VERT_TRIGGER * ctx->voffset_trigger;
545 offsets[4] = (offset >> 8) | 0x20;
546 offsets[5] = offset & 0xff;
547 sr_dbg("hantek-dso: trigger offset %3.2f (%.2x%.2x)", ctx->voffset_trigger,
548 offsets[4], offsets[5]);
3b533202
BV
549
550 if ((ret = libusb_control_transfer(ctx->usb->devhdl,
551 LIBUSB_REQUEST_TYPE_VENDOR, CTRL_SETOFFSET,
552 0, 0, offsets, sizeof(offsets), 100)) != sizeof(offsets)) {
553 sr_err("failed to set offsets: %d", ret);
554 return SR_ERR;
555 }
2715c0b8 556 sr_dbg("hantek-dso: sent CTRL_SETOFFSET");
3b533202
BV
557
558 return SR_OK;
559}
560
561SR_PRIV int dso_enable_trigger(struct context *ctx)
562{
563 int ret, tmp;
564 uint8_t cmdstring[2];
565
566 sr_dbg("hantek-dso: sending CMD_ENABLE_TRIGGER");
567
568 memset(cmdstring, 0, sizeof(cmdstring));
569 cmdstring[0] = CMD_ENABLE_TRIGGER;
570 cmdstring[1] = 0x00;
571
572 if (send_begin(ctx) != SR_OK)
573 return SR_ERR;
574
575 if ((ret = libusb_bulk_transfer(ctx->usb->devhdl,
576 DSO_EP_OUT | LIBUSB_ENDPOINT_OUT,
577 cmdstring, sizeof(cmdstring),
578 &tmp, 100)) != 0) {
579 sr_err("Failed to enable trigger: %d", ret);
580 return SR_ERR;
581 }
582
583 return SR_OK;
584}
585
586SR_PRIV int dso_force_trigger(struct context *ctx)
587{
588 int ret, tmp;
589 uint8_t cmdstring[2];
590
591 sr_dbg("hantek-dso: sending CMD_FORCE_TRIGGER");
592
593 memset(cmdstring, 0, sizeof(cmdstring));
594 cmdstring[0] = CMD_FORCE_TRIGGER;
595 cmdstring[1] = 0x00;
596
597 if (send_begin(ctx) != SR_OK)
598 return SR_ERR;
599
600 if ((ret = libusb_bulk_transfer(ctx->usb->devhdl,
601 DSO_EP_OUT | LIBUSB_ENDPOINT_OUT,
602 cmdstring, sizeof(cmdstring),
603 &tmp, 100)) != 0) {
604 sr_err("Failed to force trigger: %d", ret);
605 return SR_ERR;
606 }
607
608 return SR_OK;
609}
610
611SR_PRIV int dso_init(struct context *ctx)
612{
613
614 sr_dbg("hantek-dso: initializing dso");
615
616 if (get_channel_offsets(ctx) != SR_OK)
617 return SR_ERR;
618
619 if (dso_set_trigger_samplerate(ctx) != SR_OK)
620 return SR_ERR;
621
622 if (dso_set_filters(ctx) != SR_OK)
623 return SR_ERR;
624
625 if (dso_set_voltage(ctx) != SR_OK)
626 return SR_ERR;
627
628 if (dso_set_relays(ctx) != SR_OK)
629 return SR_ERR;
630
631 if (dso_set_voffsets(ctx) != SR_OK)
632 return SR_ERR;
633
634 if (dso_enable_trigger(ctx) != SR_OK)
635 return SR_ERR;
636
3b533202
BV
637 return SR_OK;
638}
639
6e6eeff4
BV
640SR_PRIV int dso_get_capturestate(struct context *ctx, uint8_t *capturestate,
641 uint32_t *trigger_offset)
3b533202 642{
e05a174b
BV
643 int ret, tmp, i;
644 unsigned int bitvalue, toff;
3b533202
BV
645 uint8_t cmdstring[2], inbuf[512];
646
647 sr_dbg("hantek-dso: sending CMD_GET_CAPTURESTATE");
648
649 cmdstring[0] = CMD_GET_CAPTURESTATE;
650 cmdstring[1] = 0;
651
652 if ((ret = send_bulkcmd(ctx, cmdstring, sizeof(cmdstring))) != SR_OK) {
653 sr_dbg("Failed to send get_capturestate command: %d", ret);
6e6eeff4 654 return SR_ERR;
3b533202
BV
655 }
656
657 if ((ret = libusb_bulk_transfer(ctx->usb->devhdl,
658 DSO_EP_IN | LIBUSB_ENDPOINT_IN,
659 inbuf, 512, &tmp, 100)) != 0) {
660 sr_dbg("Failed to get capturestate: %d", ret);
6e6eeff4 661 return SR_ERR;
3b533202 662 }
6e6eeff4 663 *capturestate = inbuf[0];
e05a174b
BV
664 toff = (inbuf[1] << 16) | (inbuf[3] << 8) | inbuf[2];
665
666 /* This conversion comes from the openhantek project.
667 * Each set bit in the 24-bit value inverts all bits with a lower
668 * value. No idea why the device reports the trigger point this way.
669 */
670 bitvalue = 1;
671 for (i = 0; i < 24; i++) {
e749a8cb 672 /* Each set bit inverts all bits with a lower value. */
e05a174b
BV
673 if(toff & bitvalue)
674 toff ^= bitvalue - 1;
675 bitvalue <<= 1;
676 }
677 *trigger_offset = toff;
3b533202 678
6e6eeff4 679 return SR_OK;
3b533202
BV
680}
681
6e6eeff4 682SR_PRIV int dso_capture_start(struct context *ctx)
3b533202
BV
683{
684 int ret;
685 uint8_t cmdstring[2];
686
687 sr_dbg("hantek-dso: sending CMD_CAPTURE_START");
688
689 cmdstring[0] = CMD_CAPTURE_START;
690 cmdstring[1] = 0;
691
692 if ((ret = send_bulkcmd(ctx, cmdstring, sizeof(cmdstring))) != SR_OK) {
693 sr_err("Failed to send capture_start command: %d", ret);
694 return SR_ERR;
695 }
696
697 return SR_OK;
698}
699
700SR_PRIV int dso_get_channeldata(struct context *ctx, libusb_transfer_cb_fn cb)
701{
702 struct libusb_transfer *transfer;
703 int num_transfers, ret, i;
704 uint8_t cmdstring[2];
705 unsigned char *buf;
706
707 sr_dbg("hantek-dso: sending CMD_GET_CHANNELDATA");
708
709 cmdstring[0] = CMD_GET_CHANNELDATA;
710 cmdstring[1] = 0;
711
712 if ((ret = send_bulkcmd(ctx, cmdstring, sizeof(cmdstring))) != SR_OK) {
713 sr_err("Failed to get channel data: %d", ret);
714 return SR_ERR;
715 }
716
717 /* TODO: dso-2xxx only */
718 num_transfers = ctx->framesize * sizeof(unsigned short) / ctx->epin_maxpacketsize;
719 sr_dbg("hantek-dso: queueing up %d transfers", num_transfers);
720 for (i = 0; i < num_transfers; i++) {
721 if (!(buf = g_try_malloc(ctx->epin_maxpacketsize))) {
722 sr_err("hantek-dso: %s: buf malloc failed", __func__);
723 return SR_ERR_MALLOC;
724 }
725 transfer = libusb_alloc_transfer(0);
726 libusb_fill_bulk_transfer(transfer, ctx->usb->devhdl,
727 DSO_EP_IN | LIBUSB_ENDPOINT_IN, buf,
728 ctx->epin_maxpacketsize, cb, ctx, 40);
729 if ((ret = libusb_submit_transfer(transfer)) != 0) {
730 sr_err("failed to submit transfer: %d", ret);
731 /* TODO: Free them all. */
732 libusb_free_transfer(transfer);
733 g_free(buf);
734 return SR_ERR;
735 }
736 }
737
738 return SR_OK;
739}
740