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