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