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