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