]> sigrok.org Git - libsigrok.git/blame - src/hardware/hantek-dso/protocol.c
hantek-dso: dso2250: Fix capture runaway, only do the requested number of frames.
[libsigrok.git] / src / hardware / hantek-dso / protocol.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
6ec6c43b 22#include <config.h>
3b533202
BV
23#include <string.h>
24#include <glib.h>
25#include <libusb.h>
c1aae900 26#include <libsigrok/libsigrok.h>
515ab088 27#include "libsigrok-internal.h"
caeb8d7a 28#include "protocol.h"
3b533202 29
c118080b 30static int send_begin(const struct sr_dev_inst *sdi)
3b533202 31{
c118080b 32 struct sr_usb_dev_inst *usb;
3b533202
BV
33 int ret;
34 unsigned char buffer[] = {0x0f, 0x03, 0x03, 0x03, 0x68, 0xac, 0xfe,
35 0x00, 0x01, 0x00};
36
e98b7f1b 37 sr_dbg("Sending CTRL_BEGINCOMMAND.");
3b533202 38
c118080b
BV
39 usb = sdi->conn;
40 if ((ret = libusb_control_transfer(usb->devhdl,
3b533202
BV
41 LIBUSB_REQUEST_TYPE_VENDOR, CTRL_BEGINCOMMAND,
42 0, 0, buffer, sizeof(buffer), 200)) != sizeof(buffer)) {
d4928d71
PS
43 sr_err("Failed to send begincommand: %s.",
44 libusb_error_name(ret));
3b533202
BV
45 return SR_ERR;
46 }
47
48 return SR_OK;
49}
50
c118080b 51static int send_bulkcmd(const struct sr_dev_inst *sdi, uint8_t *cmdstring, int cmdlen)
3b533202 52{
c118080b 53 struct sr_usb_dev_inst *usb;
3b533202
BV
54 int ret, tmp;
55
c118080b
BV
56 usb = sdi->conn;
57
58 if (send_begin(sdi) != SR_OK)
3b533202
BV
59 return SR_ERR;
60
c118080b
BV
61 if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT, cmdstring,
62 cmdlen, &tmp, 200)) != 0)
3b533202
BV
63 return SR_ERR;
64
65 return SR_OK;
66}
67
d87c1766 68static int dso_getmps(libusb_device *dev)
3b533202
BV
69{
70 struct libusb_device_descriptor des;
71 struct libusb_config_descriptor *conf_dsc;
72 const struct libusb_interface_descriptor *intf_dsc;
73 int mps;
74
2a8f2d41 75 libusb_get_device_descriptor(dev, &des);
3b533202
BV
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 108{
269971dd 109 struct dev_context *devc;
e32862eb 110 struct drv_context *drvc = sdi->driver->context;
c118080b
BV
111 struct sr_usb_dev_inst *usb;
112 struct libusb_device_descriptor des;
113 libusb_device **devlist;
395206f4
SA
114 int err, i;
115 char connection_id[64];
3b533202 116
269971dd 117 devc = sdi->priv;
c118080b 118 usb = sdi->conn;
3b533202 119
d4abb463 120 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
3b533202 121 for (i = 0; devlist[i]; i++) {
2a8f2d41 122 libusb_get_device_descriptor(devlist[i], &des);
3b533202 123
269971dd
BV
124 if (des.idVendor != devc->profile->fw_vid
125 || des.idProduct != devc->profile->fw_pid)
3b533202
BV
126 continue;
127
395206f4
SA
128 if ((sdi->status == SR_ST_INITIALIZING) ||
129 (sdi->status == SR_ST_INACTIVE)) {
3b533202 130 /*
395206f4 131 * Check device by its physical USB bus/port address.
3b533202 132 */
395206f4
SA
133 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
134 if (strcmp(sdi->connection_id, connection_id))
135 /* This is not the one. */
3b533202
BV
136 continue;
137 }
138
c118080b
BV
139 if (!(err = libusb_open(devlist[i], &usb->devhdl))) {
140 if (usb->address == 0xff)
3b533202
BV
141 /*
142 * first time we touch this device after firmware upload,
143 * so we don't know the address yet.
144 */
c118080b 145 usb->address = libusb_get_device_address(devlist[i]);
3b533202 146
e98b7f1b
UH
147 if (!(devc->epin_maxpacketsize = dso_getmps(devlist[i])))
148 sr_err("Wrong endpoint profile.");
3b533202
BV
149 else {
150 sdi->status = SR_ST_ACTIVE;
395206f4
SA
151 sr_info("Opened device on %d.%d (logical) / "
152 "%s (physical) interface %d.",
153 usb->bus, usb->address,
154 sdi->connection_id, USB_INTERFACE);
3b533202
BV
155 }
156 } else {
d4928d71
PS
157 sr_err("Failed to open device: %s.",
158 libusb_error_name(err));
3b533202
BV
159 }
160
e98b7f1b 161 /* If we made it here, we handled the device (somehow). */
3b533202
BV
162 break;
163 }
164 libusb_free_device_list(devlist, 1);
165
166 if (sdi->status != SR_ST_ACTIVE)
167 return SR_ERR;
168
169 return SR_OK;
170}
171
172SR_PRIV void dso_close(struct sr_dev_inst *sdi)
173{
c118080b 174 struct sr_usb_dev_inst *usb;
3b533202 175
c118080b 176 usb = sdi->conn;
3b533202 177
98fec29e 178 if (!usb->devhdl)
3b533202
BV
179 return;
180
395206f4
SA
181 sr_info("Closing device on %d.%d (logical) / %s (physical) interface %d.",
182 usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
c118080b
BV
183 libusb_release_interface(usb->devhdl, USB_INTERFACE);
184 libusb_close(usb->devhdl);
185 usb->devhdl = NULL;
3b533202
BV
186 sdi->status = SR_ST_INACTIVE;
187
188}
189
c118080b 190static int get_channel_offsets(const struct sr_dev_inst *sdi)
3b533202 191{
c118080b
BV
192 struct dev_context *devc;
193 struct sr_usb_dev_inst *usb;
2715c0b8
BV
194 GString *gs;
195 int chan, v, ret;
3b533202 196
e98b7f1b 197 sr_dbg("Getting channel offsets.");
3b533202 198
c118080b
BV
199 devc = sdi->priv;
200 usb = sdi->conn;
201
202 ret = libusb_control_transfer(usb->devhdl,
3b533202
BV
203 LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR,
204 CTRL_READ_EEPROM, EEPROM_CHANNEL_OFFSETS, 0,
269971dd
BV
205 (unsigned char *)&devc->channel_levels,
206 sizeof(devc->channel_levels), 200);
207 if (ret != sizeof(devc->channel_levels)) {
d4928d71
PS
208 sr_err("Failed to get channel offsets: %s.",
209 libusb_error_name(ret));
3b533202
BV
210 return SR_ERR;
211 }
212
2715c0b8
BV
213 /* Comes in as 16-bit numbers with the second byte always 0 on
214 * the DSO-2090. Guessing this is supposed to be big-endian,
215 * since that's how voltage offsets are submitted back to the DSO.
216 * Convert to host order now, so we can use them natively.
217 */
07ffa5b3 218 for (chan = 0; chan < NUM_CHANNELS; chan++) {
2715c0b8 219 for (v = 0; v < 9; v++) {
e98b7f1b
UH
220 devc->channel_levels[chan][v][0] =
221 g_ntohs(devc->channel_levels[chan][v][0]);
222 devc->channel_levels[chan][v][1] =
223 g_ntohs(devc->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);
07ffa5b3 229 for (chan = 0; chan < NUM_CHANNELS; chan++) {
e98b7f1b 230 g_string_printf(gs, "CH%d:", chan + 1);
2715c0b8
BV
231 for (v = 0; v < 9; v++) {
232 g_string_append_printf(gs, " %.4x-%.4x",
e98b7f1b
UH
233 devc->channel_levels[chan][v][0],
234 devc->channel_levels[chan][v][1]);
2715c0b8 235 }
cbc6f3b2 236 sr_dbg("%s", gs->str);
2715c0b8
BV
237 }
238 g_string_free(gs, TRUE);
239 }
240
3b533202
BV
241 return SR_OK;
242}
243
95983cc3
PM
244
245static void dso2250_set_triggerpos(int value, int long_buffer, uint8_t dest[], int offset)
246{
247 uint32_t min, max;
248 uint32_t tmp, diff;
249
250 min = long_buffer ? 0 : 0x7d7ff;
251 max = 0x7ffff;
252
253 diff = max - min;
254 tmp = min + diff * value / 100;
255 sr_dbg("2250 trigger pos: %3d%% * [0x%x,0x%x] == 0x%x", value, min, max, tmp);
256
257 dest[offset + 0] = tmp & 0xff;
258 dest[offset + 1] = (tmp >> 8) & 0xff;
259 dest[offset + 2] = (tmp >> 16) & 0x7;
260}
261
262
87f56d01
PM
263/* See http://openhantek.sourceforge.net/doc/namespaceHantek.html#ac1cd181814cf3da74771c29800b39028 */
264static int dso2250_set_trigger_samplerate(const struct sr_dev_inst *sdi)
265{
266 struct dev_context *devc;
267 struct sr_usb_dev_inst *usb;
268 int ret, tmp;
269 int base;
270 uint8_t cmdstring[12];
95983cc3 271 int trig;
87f56d01
PM
272
273
274 devc = sdi->priv;
275 usb = sdi->conn;
276
277 memset(cmdstring, 0, sizeof(cmdstring));
278 /* Command */
279 cmdstring[0] = CMD_2250_SET_TRIGGERSOURCE;
280 sr_dbg("Trigger source %s.", devc->triggersource);
281 if (!strcmp("CH2", devc->triggersource))
282 tmp = 3;
283 else if (!strcmp("CH1", devc->triggersource))
284 tmp = 2;
285 else if (!strcmp("EXT", devc->triggersource))
286 tmp = 0;
287 else {
288 sr_err("Invalid trigger source: '%s'.", devc->triggersource);
289 return SR_ERR_ARG;
290 }
291 cmdstring[2] = tmp;
292
293
294 sr_dbg("Trigger slope: %d.", devc->triggerslope);
295 cmdstring[2] |= (devc->triggerslope == SLOPE_NEGATIVE ? 1 : 0) << 3;
296
297 if (send_begin(sdi) != SR_OK)
298 return SR_ERR;
299
300 if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT,
301 cmdstring, 8, &tmp, 100)) != 0) {
302 sr_err("Failed to set trigger/samplerate: %s.",
303 libusb_error_name(ret));
304 return SR_ERR;
305 }
306
307
308 /* Frame size */
309 sr_dbg("Frame size: %d.", devc->framesize);
310 cmdstring[0] = CMD_2250_SET_RECORD_LENGTH;
311 cmdstring[2] = devc->framesize == FRAMESIZE_SMALL ? 0x01 : 0x02;
312
313 if (send_begin(sdi) != SR_OK)
314 return SR_ERR;
315
316 if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT,
317 cmdstring, 4, &tmp, 100)) != 0) {
318 sr_err("Failed to set record length: %s.",
319 libusb_error_name(ret));
320 return SR_ERR;
321 }
322
323
324 memset(cmdstring, 0, sizeof(cmdstring));
325 cmdstring[0] = CMD_2250_SET_SAMPLERATE;
11e33196 326 sr_dbg("Sample rate: %u", devc->samplerate);
87f56d01 327 base = 100e6;
11e33196
PM
328 if (devc->samplerate > base) {
329 /* Timebase fast */
330 sr_err("Sample rate > 100MHz not yet supported.");
331 return SR_ERR_ARG;
87f56d01
PM
332 }
333
11e33196
PM
334 tmp = base / devc->samplerate;
335 sr_dbg("sample rate value: %d.", devc->samplerate);
336 if (tmp) {
337 /* Downsampling on */
338 cmdstring[2] |= 2;
339 /* Downsampler = 1comp((Base / Samplerate) - 2)
340 * Base == 100Msa resp. 200MSa
341 *
342 * Example for 500kSa/s:
343 * 100e6 / 500e3 => 200
344 * 200 - 2 => 198
345 * 1comp(198) => ff39 */
346 tmp -= 2;
347 tmp = ~tmp;
348 sr_dbg("down sampler value: 0x%x.", tmp & 0xffff);
349 cmdstring[4] = (tmp >> 0) & 0xff;
350 cmdstring[5] = (tmp >> 8) & 0xff;
351 }
87f56d01
PM
352
353 if (send_begin(sdi) != SR_OK)
354 return SR_ERR;
355
356 if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT,
357 cmdstring, 8, &tmp, 100)) != 0) {
358 sr_err("Failed to set sample rate: %s.",
359 libusb_error_name(ret));
360 return SR_ERR;
361 }
362 sr_dbg("Sent CMD_2250_SET_SAMPLERATE.");
363
364
365 /* Enabled channels: 00=CH1 01=CH2 10=both */
366 memset(cmdstring, 0, sizeof(cmdstring));
367 cmdstring[0] = CMD_2250_SET_CHANNELS;
368 sr_dbg("Channels CH1=%d CH2=%d", devc->ch_enabled[0], devc->ch_enabled[1]);
369 cmdstring[2] = (devc->ch_enabled[0] ? 0 : 1) + (devc->ch_enabled[1] ? 2 : 0);
370
371 if (send_begin(sdi) != SR_OK)
372 return SR_ERR;
373
374 if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT,
375 cmdstring, 4, &tmp, 100)) != 0) {
376 sr_err("Failed to set channels: %s.",
377 libusb_error_name(ret));
378 return SR_ERR;
379 }
380 sr_dbg("Sent CMD_2250_SET_CHANNELS.");
381
382
383
384 /* Trigger slope: 0=positive 1=negative */
385 memset(cmdstring, 0, sizeof(cmdstring));
386 cmdstring[0] = CMD_2250_SET_TRIGGERPOS_AND_BUFFER;
387
95983cc3
PM
388 sr_dbg("Capture ratio: %d.", devc->capture_ratio);
389 trig = devc->capture_ratio;
390 dso2250_set_triggerpos(trig,
391 devc->framesize != FRAMESIZE_SMALL, cmdstring, 2);
392 dso2250_set_triggerpos(100 - trig,
393 devc->framesize != FRAMESIZE_SMALL, cmdstring, 6);
87f56d01
PM
394
395 if (send_begin(sdi) != SR_OK)
396 return SR_ERR;
397
398 /* TODO: 12 bytes according to documentation? */
399 if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT,
400 cmdstring, 10, &tmp, 100)) != 0) {
401 sr_err("Failed to set trigger position: %s.",
402 libusb_error_name(ret));
403 return SR_ERR;
404 }
405
406 return SR_OK;
407}
408
11e33196 409int dso_set_trigger_samplerate(const struct sr_dev_inst *sdi)
3b533202 410{
c118080b
BV
411 struct dev_context *devc;
412 struct sr_usb_dev_inst *usb;
3b533202
BV
413 int ret, tmp;
414 uint8_t cmdstring[12];
3b533202 415 uint16_t timebase_small[] = { 0xffff, 0xfffc, 0xfff7, 0xffe8, 0xffce,
e98b7f1b 416 0xff9c, 0xff07, 0xfe0d, 0xfc19, 0xf63d, 0xec79, 0xd8f1 };
3b533202 417 uint16_t timebase_large[] = { 0xffff, 0x0000, 0xfffc, 0xfff7, 0xffe8,
e98b7f1b 418 0xffce, 0xff9d, 0xff07, 0xfe0d, 0xfc19, 0xf63d, 0xec79 };
3b533202 419
87f56d01
PM
420 devc = sdi->priv;
421 if (devc->profile->fw_pid == 0x2250)
422 return dso2250_set_trigger_samplerate(sdi);
423
e98b7f1b 424 sr_dbg("Preparing CMD_SET_TRIGGER_SAMPLERATE.");
3b533202 425
c118080b
BV
426 usb = sdi->conn;
427
3b533202
BV
428 memset(cmdstring, 0, sizeof(cmdstring));
429 /* Command */
430 cmdstring[0] = CMD_SET_TRIGGER_SAMPLERATE;
431
432 /* Trigger source */
e98b7f1b 433 sr_dbg("Trigger source %s.", devc->triggersource);
269971dd 434 if (!strcmp("CH2", devc->triggersource))
a370ef19 435 tmp = 0;
269971dd 436 else if (!strcmp("CH1", devc->triggersource))
a370ef19 437 tmp = 1;
269971dd 438 else if (!strcmp("EXT", devc->triggersource))
a370ef19
BV
439 tmp = 2;
440 else {
e98b7f1b 441 sr_err("Invalid trigger source: '%s'.", devc->triggersource);
a370ef19
BV
442 return SR_ERR_ARG;
443 }
444 cmdstring[2] = tmp;
3b533202
BV
445
446 /* Frame size */
e98b7f1b 447 sr_dbg("Frame size: %d.", devc->framesize);
269971dd 448 cmdstring[2] |= (devc->framesize == FRAMESIZE_SMALL ? 0x01 : 0x02) << 2;
bc79e906
BV
449
450 /* Timebase fast */
e98b7f1b 451 sr_dbg("Time base index: %d.", devc->timebase);
034accb5 452 if (devc->framesize == FRAMESIZE_SMALL) {
269971dd 453 if (devc->timebase < TIME_20us)
bc79e906 454 tmp = 0;
269971dd 455 else if (devc->timebase == TIME_20us)
bc79e906 456 tmp = 1;
269971dd 457 else if (devc->timebase == TIME_40us)
bc79e906 458 tmp = 2;
269971dd 459 else if (devc->timebase == TIME_100us)
bc79e906 460 tmp = 3;
269971dd 461 else if (devc->timebase >= TIME_200us)
bc79e906 462 tmp = 4;
034accb5 463 } else {
269971dd 464 if (devc->timebase < TIME_40us) {
e98b7f1b 465 sr_err("Timebase < 40us only supported with 10K buffer.");
bc79e906
BV
466 return SR_ERR_ARG;
467 }
269971dd 468 else if (devc->timebase == TIME_40us)
bc79e906 469 tmp = 0;
269971dd 470 else if (devc->timebase == TIME_100us)
bc79e906 471 tmp = 2;
269971dd 472 else if (devc->timebase == TIME_200us)
bc79e906 473 tmp = 3;
269971dd 474 else if (devc->timebase >= TIME_400us)
bc79e906 475 tmp = 4;
3b533202 476 }
bc79e906
BV
477 cmdstring[2] |= (tmp & 0x07) << 5;
478
479 /* Enabled channels: 00=CH1 01=CH2 10=both */
417412c8
AJ
480 sr_dbg("Channels CH1=%d CH2=%d", devc->ch_enabled[0], devc->ch_enabled[1]);
481 tmp = (((devc->ch_enabled[1] ? 1 : 0) << 1) + (devc->ch_enabled[0] ? 1 : 0)) - 1;
6e71ef3b 482 cmdstring[3] = tmp;
3b533202 483
bc79e906 484 /* Fast rates channel */
e98b7f1b 485 /* TODO: Is this right? */
269971dd 486 tmp = devc->timebase < TIME_10us ? 1 : 0;
bc79e906 487 cmdstring[3] |= tmp << 2;
3b533202 488
bc79e906 489 /* Trigger slope: 0=positive 1=negative */
e98b7f1b
UH
490 /* TODO: Does this work? */
491 sr_dbg("Trigger slope: %d.", devc->triggerslope);
269971dd 492 cmdstring[3] |= (devc->triggerslope == SLOPE_NEGATIVE ? 1 : 0) << 3;
3b533202 493
a370ef19 494 /* Timebase slow */
269971dd 495 if (devc->timebase < TIME_100us)
3b533202 496 tmp = 0;
269971dd 497 else if (devc->timebase > TIME_400ms)
3b533202
BV
498 tmp = 0xffed;
499 else {
269971dd
BV
500 if (devc->framesize == FRAMESIZE_SMALL)
501 tmp = timebase_small[devc->timebase - 3];
3b533202 502 else
269971dd 503 tmp = timebase_large[devc->timebase - 3];
3b533202 504 }
bc79e906
BV
505 cmdstring[4] = tmp & 0xff;
506 cmdstring[5] = (tmp >> 8) & 0xff;
507
508 /* Horizontal trigger position */
95983cc3
PM
509 sr_dbg("Capture ratio: %d.", devc->capture_ratio);
510 tmp = 0x77fff + 0x8000 * devc->capture_ratio / 100;
3b533202
BV
511 cmdstring[6] = tmp & 0xff;
512 cmdstring[7] = (tmp >> 8) & 0xff;
3b533202
BV
513 cmdstring[10] = (tmp >> 16) & 0xff;
514
c118080b 515 if (send_begin(sdi) != SR_OK)
3b533202
BV
516 return SR_ERR;
517
c118080b
BV
518 if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT,
519 cmdstring, sizeof(cmdstring), &tmp, 100)) != 0) {
d4928d71
PS
520 sr_err("Failed to set trigger/samplerate: %s.",
521 libusb_error_name(ret));
3b533202
BV
522 return SR_ERR;
523 }
e98b7f1b 524 sr_dbg("Sent CMD_SET_TRIGGER_SAMPLERATE.");
3b533202
BV
525
526 return SR_OK;
527}
528
87f56d01 529
d87c1766 530static int dso_set_filters(const struct sr_dev_inst *sdi)
3b533202 531{
c118080b
BV
532 struct dev_context *devc;
533 struct sr_usb_dev_inst *usb;
3b533202
BV
534 int ret, tmp;
535 uint8_t cmdstring[8];
536
e98b7f1b 537 sr_dbg("Preparing CMD_SET_FILTERS.");
3b533202 538
c118080b
BV
539 devc = sdi->priv;
540 usb = sdi->conn;
541
3b533202
BV
542 memset(cmdstring, 0, sizeof(cmdstring));
543 cmdstring[0] = CMD_SET_FILTERS;
544 cmdstring[1] = 0x0f;
933defaa 545 if (devc->filter[0]) {
e98b7f1b 546 sr_dbg("Turning on CH1 filter.");
3b533202 547 cmdstring[2] |= 0x80;
ebb781a6 548 }
933defaa 549 if (devc->filter[1]) {
e98b7f1b 550 sr_dbg("Turning on CH2 filter.");
3b533202 551 cmdstring[2] |= 0x40;
ebb781a6 552 }
933defaa
BV
553 /*
554 * Not supported: filtering on the trigger
555 * cmdstring[2] |= 0x20;
556 */
3b533202 557
c118080b 558 if (send_begin(sdi) != SR_OK)
3b533202
BV
559 return SR_ERR;
560
c118080b
BV
561 if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT,
562 cmdstring, sizeof(cmdstring), &tmp, 100)) != 0) {
1740429d 563 sr_err("Failed to set filters: %s.", libusb_error_name(ret));
3b533202
BV
564 return SR_ERR;
565 }
e98b7f1b 566 sr_dbg("Sent CMD_SET_FILTERS.");
3b533202
BV
567
568 return SR_OK;
569}
570
d87c1766 571static int dso_set_voltage(const struct sr_dev_inst *sdi)
3b533202 572{
c118080b
BV
573 struct dev_context *devc;
574 struct sr_usb_dev_inst *usb;
3b533202
BV
575 int ret, tmp;
576 uint8_t cmdstring[8];
577
e98b7f1b 578 sr_dbg("Preparing CMD_SET_VOLTAGE.");
3b533202 579
c118080b
BV
580 devc = sdi->priv;
581 usb = sdi->conn;
582
3b533202
BV
583 memset(cmdstring, 0, sizeof(cmdstring));
584 cmdstring[0] = CMD_SET_VOLTAGE;
87f56d01
PM
585
586 if (devc->profile->fw_pid == 0x2250) {
587 cmdstring[2] = 0x08;
588 } else {
589 cmdstring[1] = 0x0f;
590 cmdstring[2] = 0x30;
591 }
313deed2
BV
592
593 /* CH1 volts/div is encoded in bits 0-1 */
933defaa
BV
594 sr_dbg("CH1 vdiv index: %d.", devc->voltage[0]);
595 switch (devc->voltage[0]) {
313deed2
BV
596 case VDIV_1V:
597 case VDIV_100MV:
598 case VDIV_10MV:
599 cmdstring[2] |= 0x00;
600 break;
601 case VDIV_2V:
602 case VDIV_200MV:
603 case VDIV_20MV:
604 cmdstring[2] |= 0x01;
605 break;
606 case VDIV_5V:
607 case VDIV_500MV:
608 case VDIV_50MV:
609 cmdstring[2] |= 0x02;
610 break;
611 }
612
613 /* CH2 volts/div is encoded in bits 2-3 */
933defaa
BV
614 sr_dbg("CH2 vdiv index: %d.", devc->voltage[1]);
615 switch (devc->voltage[1]) {
313deed2
BV
616 case VDIV_1V:
617 case VDIV_100MV:
618 case VDIV_10MV:
619 cmdstring[2] |= 0x00;
620 break;
621 case VDIV_2V:
622 case VDIV_200MV:
623 case VDIV_20MV:
1d97091e 624 cmdstring[2] |= 0x04;
313deed2
BV
625 break;
626 case VDIV_5V:
627 case VDIV_500MV:
628 case VDIV_50MV:
1d97091e 629 cmdstring[2] |= 0x08;
313deed2
BV
630 break;
631 }
3b533202 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 set voltage: %s.", libusb_error_name(ret));
3b533202
BV
639 return SR_ERR;
640 }
e98b7f1b 641 sr_dbg("Sent CMD_SET_VOLTAGE.");
3b533202
BV
642
643 return SR_OK;
644}
645
d87c1766 646static int dso_set_relays(const struct sr_dev_inst *sdi)
3b533202 647{
c118080b
BV
648 struct dev_context *devc;
649 struct sr_usb_dev_inst *usb;
a217bcdf
BV
650 GString *gs;
651 int ret, i;
652 uint8_t relays[17] = { 0x00, 0x04, 0x08, 0x02, 0x20, 0x40, 0x10, 0x01,
3b533202
BV
653 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
654
e98b7f1b 655 sr_dbg("Preparing CTRL_SETRELAYS.");
3b533202 656
c118080b
BV
657 devc = sdi->priv;
658 usb = sdi->conn;
659
933defaa 660 if (devc->voltage[0] < VDIV_1V)
3b533202
BV
661 relays[1] = ~relays[1];
662
933defaa 663 if (devc->voltage[0] < VDIV_100MV)
3b533202
BV
664 relays[2] = ~relays[2];
665
933defaa
BV
666 sr_dbg("CH1 coupling: %d.", devc->coupling[0]);
667 if (devc->coupling[0] != COUPLING_AC)
3b533202
BV
668 relays[3] = ~relays[3];
669
933defaa 670 if (devc->voltage[1] < VDIV_1V)
3b533202
BV
671 relays[4] = ~relays[4];
672
933defaa 673 if (devc->voltage[1] < VDIV_100MV)
3b533202
BV
674 relays[5] = ~relays[5];
675
933defaa
BV
676 sr_dbg("CH2 coupling: %d.", devc->coupling[1]);
677 if (devc->coupling[1] != COUPLING_AC)
3b533202
BV
678 relays[6] = ~relays[6];
679
269971dd 680 if (!strcmp(devc->triggersource, "EXT"))
3b533202
BV
681 relays[7] = ~relays[7];
682
a217bcdf
BV
683 if (sr_log_loglevel_get() >= SR_LOG_DBG) {
684 gs = g_string_sized_new(128);
e98b7f1b 685 g_string_printf(gs, "Relays:");
a217bcdf
BV
686 for (i = 0; i < 17; i++)
687 g_string_append_printf(gs, " %.2x", relays[i]);
cbc6f3b2 688 sr_dbg("%s", gs->str);
a217bcdf
BV
689 g_string_free(gs, TRUE);
690 }
691
c118080b 692 if ((ret = libusb_control_transfer(usb->devhdl,
3b533202 693 LIBUSB_REQUEST_TYPE_VENDOR, CTRL_SETRELAYS,
a217bcdf 694 0, 0, relays, 17, 100)) != sizeof(relays)) {
1740429d 695 sr_err("Failed to set relays: %s.", libusb_error_name(ret));
3b533202
BV
696 return SR_ERR;
697 }
e98b7f1b 698 sr_dbg("Sent CTRL_SETRELAYS.");
3b533202
BV
699
700 return SR_OK;
701}
702
12f62ce6 703int dso_set_voffsets(const struct sr_dev_inst *sdi)
3b533202 704{
c118080b
BV
705 struct dev_context *devc;
706 struct sr_usb_dev_inst *usb;
3b533202
BV
707 int offset, ret;
708 uint16_t *ch_levels;
2715c0b8
BV
709 uint8_t offsets[17];
710
e98b7f1b 711 sr_dbg("Preparing CTRL_SETOFFSET.");
2715c0b8 712
c118080b
BV
713 devc = sdi->priv;
714 usb = sdi->conn;
715
2715c0b8
BV
716 memset(offsets, 0, sizeof(offsets));
717 /* Channel 1 */
933defaa 718 ch_levels = devc->channel_levels[0][devc->voltage[0]];
269971dd 719 offset = (ch_levels[1] - ch_levels[0]) * devc->voffset_ch1 + ch_levels[0];
2715c0b8
BV
720 offsets[0] = (offset >> 8) | 0x20;
721 offsets[1] = offset & 0xff;
e98b7f1b
UH
722 sr_dbg("CH1 offset: %3.2f (%.2x%.2x).", devc->voffset_ch1,
723 offsets[0], offsets[1]);
2715c0b8
BV
724
725 /* Channel 2 */
933defaa 726 ch_levels = devc->channel_levels[1][devc->voltage[1]];
269971dd 727 offset = (ch_levels[1] - ch_levels[0]) * devc->voffset_ch2 + ch_levels[0];
2715c0b8
BV
728 offsets[2] = (offset >> 8) | 0x20;
729 offsets[3] = offset & 0xff;
e98b7f1b
UH
730 sr_dbg("CH2 offset: %3.2f (%.2x%.2x).", devc->voffset_ch2,
731 offsets[2], offsets[3]);
2715c0b8
BV
732
733 /* Trigger */
269971dd 734 offset = MAX_VERT_TRIGGER * devc->voffset_trigger;
2715c0b8
BV
735 offsets[4] = (offset >> 8) | 0x20;
736 offsets[5] = offset & 0xff;
e98b7f1b 737 sr_dbg("Trigger offset: %3.2f (%.2x%.2x).", devc->voffset_trigger,
2715c0b8 738 offsets[4], offsets[5]);
3b533202 739
c118080b 740 if ((ret = libusb_control_transfer(usb->devhdl,
3b533202
BV
741 LIBUSB_REQUEST_TYPE_VENDOR, CTRL_SETOFFSET,
742 0, 0, offsets, sizeof(offsets), 100)) != sizeof(offsets)) {
1740429d 743 sr_err("Failed to set offsets: %s.", libusb_error_name(ret));
3b533202
BV
744 return SR_ERR;
745 }
e98b7f1b 746 sr_dbg("Sent CTRL_SETOFFSET.");
3b533202
BV
747
748 return SR_OK;
749}
750
c118080b 751SR_PRIV int dso_enable_trigger(const struct sr_dev_inst *sdi)
3b533202 752{
c118080b 753 struct sr_usb_dev_inst *usb;
3b533202
BV
754 int ret, tmp;
755 uint8_t cmdstring[2];
756
e98b7f1b 757 sr_dbg("Sending CMD_ENABLE_TRIGGER.");
3b533202 758
c118080b
BV
759 usb = sdi->conn;
760
3b533202
BV
761 memset(cmdstring, 0, sizeof(cmdstring));
762 cmdstring[0] = CMD_ENABLE_TRIGGER;
763 cmdstring[1] = 0x00;
764
c118080b 765 if (send_begin(sdi) != SR_OK)
3b533202
BV
766 return SR_ERR;
767
c118080b
BV
768 if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT,
769 cmdstring, sizeof(cmdstring), &tmp, 100)) != 0) {
1740429d 770 sr_err("Failed to enable trigger: %s.", libusb_error_name(ret));
3b533202
BV
771 return SR_ERR;
772 }
773
774 return SR_OK;
775}
776
c118080b 777SR_PRIV int dso_force_trigger(const struct sr_dev_inst *sdi)
3b533202 778{
c118080b 779 struct sr_usb_dev_inst *usb;
3b533202
BV
780 int ret, tmp;
781 uint8_t cmdstring[2];
782
e98b7f1b 783 sr_dbg("Sending CMD_FORCE_TRIGGER.");
3b533202 784
c118080b
BV
785 usb = sdi->conn;
786
3b533202
BV
787 memset(cmdstring, 0, sizeof(cmdstring));
788 cmdstring[0] = CMD_FORCE_TRIGGER;
789 cmdstring[1] = 0x00;
790
c118080b 791 if (send_begin(sdi) != SR_OK)
3b533202
BV
792 return SR_ERR;
793
c118080b
BV
794 if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT,
795 cmdstring, sizeof(cmdstring), &tmp, 100)) != 0) {
1740429d 796 sr_err("Failed to force trigger: %s.", libusb_error_name(ret));
3b533202
BV
797 return SR_ERR;
798 }
799
800 return SR_OK;
801}
802
c118080b 803SR_PRIV int dso_init(const struct sr_dev_inst *sdi)
3b533202
BV
804{
805
e98b7f1b 806 sr_dbg("Initializing DSO.");
3b533202 807
c118080b 808 if (get_channel_offsets(sdi) != SR_OK)
3b533202
BV
809 return SR_ERR;
810
c118080b 811 if (dso_set_trigger_samplerate(sdi) != SR_OK)
3b533202
BV
812 return SR_ERR;
813
c118080b 814 if (dso_set_filters(sdi) != SR_OK)
3b533202
BV
815 return SR_ERR;
816
c118080b 817 if (dso_set_voltage(sdi) != SR_OK)
3b533202
BV
818 return SR_ERR;
819
c118080b 820 if (dso_set_relays(sdi) != SR_OK)
3b533202
BV
821 return SR_ERR;
822
c118080b 823 if (dso_set_voffsets(sdi) != SR_OK)
3b533202
BV
824 return SR_ERR;
825
c118080b 826 if (dso_enable_trigger(sdi) != SR_OK)
3b533202
BV
827 return SR_ERR;
828
3b533202
BV
829 return SR_OK;
830}
831
c118080b
BV
832SR_PRIV int dso_get_capturestate(const struct sr_dev_inst *sdi,
833 uint8_t *capturestate, uint32_t *trigger_offset)
3b533202 834{
c118080b 835 struct sr_usb_dev_inst *usb;
e05a174b
BV
836 int ret, tmp, i;
837 unsigned int bitvalue, toff;
3b533202
BV
838 uint8_t cmdstring[2], inbuf[512];
839
e98b7f1b 840 sr_dbg("Sending CMD_GET_CAPTURESTATE.");
3b533202 841
c118080b
BV
842 usb = sdi->conn;
843
3b533202
BV
844 cmdstring[0] = CMD_GET_CAPTURESTATE;
845 cmdstring[1] = 0;
846
c118080b 847 if ((ret = send_bulkcmd(sdi, cmdstring, sizeof(cmdstring))) != SR_OK) {
d4928d71
PS
848 sr_dbg("Failed to send get_capturestate command: %s.",
849 libusb_error_name(ret));
6e6eeff4 850 return SR_ERR;
3b533202
BV
851 }
852
c118080b 853 if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_IN,
3b533202 854 inbuf, 512, &tmp, 100)) != 0) {
d4928d71
PS
855 sr_dbg("Failed to get capturestate: %s.",
856 libusb_error_name(ret));
6e6eeff4 857 return SR_ERR;
3b533202 858 }
6e6eeff4 859 *capturestate = inbuf[0];
e05a174b
BV
860 toff = (inbuf[1] << 16) | (inbuf[3] << 8) | inbuf[2];
861
e98b7f1b
UH
862 /*
863 * This conversion comes from the openhantek project.
e05a174b
BV
864 * Each set bit in the 24-bit value inverts all bits with a lower
865 * value. No idea why the device reports the trigger point this way.
866 */
867 bitvalue = 1;
868 for (i = 0; i < 24; i++) {
e749a8cb 869 /* Each set bit inverts all bits with a lower value. */
0c5f2abc 870 if (toff & bitvalue)
e05a174b
BV
871 toff ^= bitvalue - 1;
872 bitvalue <<= 1;
873 }
874 *trigger_offset = toff;
3b533202 875
6e6eeff4 876 return SR_OK;
3b533202
BV
877}
878
c118080b 879SR_PRIV int dso_capture_start(const struct sr_dev_inst *sdi)
3b533202
BV
880{
881 int ret;
882 uint8_t cmdstring[2];
883
e98b7f1b 884 sr_dbg("Sending CMD_CAPTURE_START.");
3b533202
BV
885
886 cmdstring[0] = CMD_CAPTURE_START;
887 cmdstring[1] = 0;
888
c118080b 889 if ((ret = send_bulkcmd(sdi, cmdstring, sizeof(cmdstring))) != SR_OK) {
d4928d71
PS
890 sr_err("Failed to send capture_start command: %s.",
891 libusb_error_name(ret));
3b533202
BV
892 return SR_ERR;
893 }
894
895 return SR_OK;
896}
897
69e19dd7
BV
898SR_PRIV int dso_get_channeldata(const struct sr_dev_inst *sdi,
899 libusb_transfer_cb_fn cb)
3b533202 900{
69e19dd7 901 struct dev_context *devc;
c118080b 902 struct sr_usb_dev_inst *usb;
3b533202
BV
903 struct libusb_transfer *transfer;
904 int num_transfers, ret, i;
905 uint8_t cmdstring[2];
906 unsigned char *buf;
907
e98b7f1b 908 sr_dbg("Sending CMD_GET_CHANNELDATA.");
3b533202 909
c118080b
BV
910 devc = sdi->priv;
911 usb = sdi->conn;
912
3b533202
BV
913 cmdstring[0] = CMD_GET_CHANNELDATA;
914 cmdstring[1] = 0;
915
c118080b 916 if ((ret = send_bulkcmd(sdi, cmdstring, sizeof(cmdstring))) != SR_OK) {
d4928d71
PS
917 sr_err("Failed to get channel data: %s.",
918 libusb_error_name(ret));
3b533202
BV
919 return SR_ERR;
920 }
921
e98b7f1b
UH
922 /* TODO: DSO-2xxx only. */
923 num_transfers = devc->framesize *
924 sizeof(unsigned short) / devc->epin_maxpacketsize;
925 sr_dbg("Queueing up %d transfers.", num_transfers);
3b533202 926 for (i = 0; i < num_transfers; i++) {
269971dd 927 if (!(buf = g_try_malloc(devc->epin_maxpacketsize))) {
e98b7f1b 928 sr_err("Failed to malloc USB endpoint buffer.");
3b533202
BV
929 return SR_ERR_MALLOC;
930 }
931 transfer = libusb_alloc_transfer(0);
c118080b 932 libusb_fill_bulk_transfer(transfer, usb->devhdl, DSO_EP_IN, buf,
69e19dd7 933 devc->epin_maxpacketsize, cb, (void *)sdi, 40);
3b533202 934 if ((ret = libusb_submit_transfer(transfer)) != 0) {
d4928d71
PS
935 sr_err("Failed to submit transfer: %s.",
936 libusb_error_name(ret));
3b533202
BV
937 /* TODO: Free them all. */
938 libusb_free_transfer(transfer);
939 g_free(buf);
940 return SR_ERR;
941 }
942 }
943
944 return SR_OK;
945}