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