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