]> sigrok.org Git - libsigrok.git/blame - hardware/hantek-dso/api.c
hantek-dso: use new scan API
[libsigrok.git] / hardware / hantek-dso / api.c
CommitLineData
3b533202
BV
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <stdio.h>
21#include <stdint.h>
22#include <stdlib.h>
23#include <sys/types.h>
24#include <sys/stat.h>
25#include <fcntl.h>
26#include <unistd.h>
27#include <string.h>
28#include <sys/time.h>
29#include <inttypes.h>
3b533202
BV
30#include <glib.h>
31#include <libusb.h>
45c59c8b
BV
32#include "libsigrok.h"
33#include "libsigrok-internal.h"
3b533202
BV
34#include "config.h"
35#include "dso.h"
36
37
fc8fe3e3
BV
38/* Max time in ms before we want to check on USB events */
39/* TODO tune this properly */
3b533202
BV
40#define TICK 1
41
62bb8840 42static const int hwcaps[] = {
3b533202 43 SR_HWCAP_OSCILLOSCOPE,
ae88b97b 44 SR_HWCAP_LIMIT_SAMPLES,
3b533202 45 SR_HWCAP_CONTINUOUS,
a370ef19
BV
46 SR_HWCAP_TIMEBASE,
47 SR_HWCAP_BUFFERSIZE,
48 SR_HWCAP_TRIGGER_SOURCE,
49 SR_HWCAP_TRIGGER_SLOPE,
50 SR_HWCAP_HORIZ_TRIGGERPOS,
ebb781a6 51 SR_HWCAP_FILTER,
313deed2 52 SR_HWCAP_VDIV,
4a090d72 53 SR_HWCAP_COUPLING,
3b533202
BV
54 0,
55};
56
57static const char *probe_names[] = {
58 "CH1",
59 "CH2",
60 NULL,
61};
62
62bb8840 63static const struct dso_profile dev_profiles[] = {
88a13f30 64 { 0x04b4, 0x2090, 0x04b5, 0x2090,
3b533202 65 "Hantek", "DSO-2090",
88a13f30
BV
66 FIRMWARE_DIR "/hantek-dso-2xxx.fw" },
67 { 0x04b4, 0x2150, 0x04b5, 0x2150,
68 "Hantek", "DSO-2150",
69 FIRMWARE_DIR "/hantek-dso-2xxx.fw" },
70 { 0x04b4, 0x2250, 0x04b5, 0x2250,
71 "Hantek", "DSO-2250",
72 FIRMWARE_DIR "/hantek-dso-2xxx.fw" },
73 { 0x04b4, 0x5200, 0x04b5, 0x5200,
74 "Hantek", "DSO-5200",
75 FIRMWARE_DIR "/hantek-dso-5xxx.fw" },
76 { 0x04b4, 0x520a, 0x04b5, 0x520a,
77 "Hantek", "DSO-5200A",
78 FIRMWARE_DIR "/hantek-dso-5xxx.fw" },
79 { 0, 0, 0, 0, 0, 0, 0 },
3b533202
BV
80};
81
62bb8840 82static const uint64_t buffersizes[] = {
a370ef19
BV
83 10240,
84 32768,
85 /* TODO: 65535 */
62bb8840 86 0,
a370ef19
BV
87};
88
62bb8840 89static const struct sr_rational timebases[] = {
a370ef19
BV
90 /* microseconds */
91 { 10, 1000000 },
92 { 20, 1000000 },
93 { 40, 1000000 },
94 { 100, 1000000 },
95 { 200, 1000000 },
96 { 400, 1000000 },
97 /* milliseconds */
98 { 1, 1000 },
99 { 2, 1000 },
100 { 4, 1000 },
101 { 10, 1000 },
102 { 20, 1000 },
103 { 40, 1000 },
104 { 100, 1000 },
105 { 200, 1000 },
106 { 400, 1000 },
62bb8840 107 { 0, 0},
a370ef19
BV
108};
109
62bb8840 110static const struct sr_rational vdivs[] = {
313deed2
BV
111 /* millivolts */
112 { 10, 1000 },
113 { 20, 1000 },
114 { 50, 1000 },
115 { 100, 1000 },
116 { 200, 1000 },
117 { 500, 1000 },
118 /* volts */
119 { 1, 1 },
120 { 2, 1 },
121 { 5, 1 },
62bb8840 122 { 0, 0 },
313deed2
BV
123};
124
62bb8840 125static const char *trigger_sources[] = {
a370ef19
BV
126 "CH1",
127 "CH2",
128 "EXT",
88a13f30 129 /* TODO: forced */
62bb8840 130 NULL,
a370ef19 131};
3b533202 132
62bb8840 133static const char *filter_targets[] = {
ebb781a6
BV
134 "CH1",
135 "CH2",
136 /* TODO: "TRIGGER", */
62bb8840 137 NULL,
ebb781a6
BV
138};
139
62bb8840 140static const char *coupling[] = {
b58fbd99
BV
141 "AC",
142 "DC",
143 "GND",
62bb8840 144 NULL,
b58fbd99
BV
145};
146
3b533202 147SR_PRIV libusb_context *usb_context = NULL;
982947f7
BV
148SR_PRIV struct sr_dev_driver hantek_dso_driver_info;
149static struct sr_dev_driver *hdi = &hantek_dso_driver_info;
3b533202 150
62bb8840 151static struct sr_dev_inst *dso_dev_new(int index, const struct dso_profile *prof)
3b533202
BV
152{
153 struct sr_dev_inst *sdi;
154 struct context *ctx;
155
156 sdi = sr_dev_inst_new(index, SR_ST_INITIALIZING,
88a13f30 157 prof->vendor, prof->model, NULL);
3b533202
BV
158 if (!sdi)
159 return NULL;
160
161 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
162 sr_err("hantek-dso: ctx malloc failed");
163 return NULL;
164 }
165 ctx->profile = prof;
166 ctx->dev_state = IDLE;
167 ctx->timebase = DEFAULT_TIMEBASE;
168 ctx->ch1_enabled = TRUE;
169 ctx->ch2_enabled = TRUE;
170 ctx->voltage_ch1 = DEFAULT_VOLTAGE;
171 ctx->voltage_ch2 = DEFAULT_VOLTAGE;
172 ctx->coupling_ch1 = DEFAULT_COUPLING;
173 ctx->coupling_ch2 = DEFAULT_COUPLING;
174 ctx->voffset_ch1 = DEFAULT_VERT_OFFSET;
175 ctx->voffset_ch2 = DEFAULT_VERT_OFFSET;
176 ctx->voffset_trigger = DEFAULT_VERT_TRIGGERPOS;
3b533202
BV
177 ctx->framesize = DEFAULT_FRAMESIZE;
178 ctx->triggerslope = SLOPE_POSITIVE;
a370ef19 179 ctx->triggersource = g_strdup(DEFAULT_TRIGGER_SOURCE);
3b533202
BV
180 ctx->triggerposition = DEFAULT_HORIZ_TRIGGERPOS;
181 sdi->priv = ctx;
982947f7 182 hdi->instances = g_slist_append(hdi->instances, sdi);
3b533202
BV
183
184 return sdi;
185}
186
62bb8840 187static int configure_probes(struct context *ctx, const GSList *probes)
3b533202 188{
62bb8840
UH
189 const struct sr_probe *probe;
190 const GSList *l;
3b533202 191
6e71ef3b 192 ctx->ch1_enabled = ctx->ch2_enabled = FALSE;
3b533202
BV
193 for (l = probes; l; l = l->next) {
194 probe = (struct sr_probe *)l->data;
6e71ef3b 195 if (probe->index == 1)
3b533202 196 ctx->ch1_enabled = probe->enabled;
6e71ef3b 197 else if (probe->index == 2)
3b533202
BV
198 ctx->ch2_enabled = probe->enabled;
199 }
200
201 return SR_OK;
202}
203
39cfdd75
BV
204/* Properly close and free all devices. */
205static void clear_instances(void)
206{
207 struct sr_dev_inst *sdi;
208 struct context *ctx;
209 GSList *l;
210
211 for (l = hdi->instances; l; l = l->next) {
212 if (!(sdi = l->data)) {
213 /* Log error, but continue cleaning up the rest. */
214 sr_err("hantek-dso: %s: sdi was NULL, continuing", __func__);
215 continue;
216 }
217 if (!(ctx = sdi->priv)) {
218 /* Log error, but continue cleaning up the rest. */
219 sr_err("hantek-dso: %s: sdi->priv was NULL, continuing", __func__);
220 continue;
221 }
222 dso_close(sdi);
223 sr_usb_dev_inst_free(ctx->usb);
224 g_free(ctx->triggersource);
225
226 sr_dev_inst_free(sdi);
227 }
228
229 g_slist_free(hdi->instances);
230 hdi->instances = NULL;
231
232}
233
40dda2c3 234static int hw_init(void)
61136ea6
BV
235{
236
237 if (libusb_init(&usb_context) != 0) {
238 sr_err("hantek-dso: Failed to initialize USB.");
239 return SR_ERR;
240 }
241
242 return SR_OK;
243}
244
39cfdd75 245static GSList *hw_scan(GSList *options)
3b533202
BV
246{
247 struct sr_dev_inst *sdi;
62bb8840 248 const struct dso_profile *prof;
3b533202 249 struct context *ctx;
39cfdd75
BV
250 GSList *devices;
251 struct libusb_device_descriptor des;
3b533202 252 libusb_device **devlist;
61136ea6 253 int devcnt, ret, i, j;
3b533202 254
39cfdd75 255 (void)options;
3b533202 256 devcnt = 0;
39cfdd75
BV
257 devices = 0;
258 hdi->instances = NULL;
259
260 clear_instances();
261
262 /* Find all Hantek DSO devices and upload firmware to all of them. */
3b533202
BV
263 libusb_get_device_list(usb_context, &devlist);
264 for (i = 0; devlist[i]; i++) {
61136ea6
BV
265 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
266 sr_err("hantek-dso: failed to get device descriptor: %d", ret);
3b533202
BV
267 continue;
268 }
269
270 prof = NULL;
271 for (j = 0; dev_profiles[j].orig_vid; j++) {
272 if (des.idVendor == dev_profiles[j].orig_vid
273 && des.idProduct == dev_profiles[j].orig_pid) {
274 /* Device matches the pre-firmware profile. */
275 prof = &dev_profiles[j];
276 sr_dbg("hantek-dso: Found a %s %s.", prof->vendor, prof->model);
277 sdi = dso_dev_new(devcnt, prof);
39cfdd75 278 devices = g_slist_append(devices, sdi);
3b533202
BV
279 ctx = sdi->priv;
280 if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION,
281 prof->firmware) == SR_OK)
282 /* Remember when the firmware on this device was updated */
fc8fe3e3 283 ctx->fw_updated = g_get_monotonic_time();
3b533202
BV
284 else
285 sr_err("hantek-dso: firmware upload failed for "
286 "device %d", devcnt);
287 /* Dummy USB address of 0xff will get overwritten later. */
288 ctx->usb = sr_usb_dev_inst_new(
289 libusb_get_bus_number(devlist[i]), 0xff, NULL);
290 devcnt++;
291 break;
292 } else if (des.idVendor == dev_profiles[j].fw_vid
293 && des.idProduct == dev_profiles[j].fw_pid) {
294 /* Device matches the post-firmware profile. */
295 prof = &dev_profiles[j];
296 sr_dbg("hantek-dso: Found a %s %s.", prof->vendor, prof->model);
297 sdi = dso_dev_new(devcnt, prof);
298 sdi->status = SR_ST_INACTIVE;
39cfdd75 299 devices = g_slist_append(devices, sdi);
3b533202
BV
300 ctx = sdi->priv;
301 ctx->usb = sr_usb_dev_inst_new(
302 libusb_get_bus_number(devlist[i]),
303 libusb_get_device_address(devlist[i]), NULL);
304 devcnt++;
305 break;
306 }
307 }
308 if (!prof)
309 /* not a supported VID/PID */
310 continue;
311 }
312 libusb_free_device_list(devlist, 1);
313
39cfdd75 314 return devices;
3b533202
BV
315}
316
317static int hw_dev_open(int dev_index)
318{
3b533202
BV
319 struct sr_dev_inst *sdi;
320 struct context *ctx;
fc8fe3e3
BV
321 int64_t timediff_us, timediff_ms;
322 int err;
3b533202 323
982947f7 324 if (!(sdi = sr_dev_inst_get(hdi->instances, dev_index)))
3b533202
BV
325 return SR_ERR_ARG;
326 ctx = sdi->priv;
327
328 /*
fc8fe3e3 329 * if the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS
3b533202
BV
330 * for the FX2 to renumerate
331 */
fc8fe3e3
BV
332 err = SR_ERR;
333 if (ctx->fw_updated > 0) {
3b533202
BV
334 sr_info("hantek-dso: waiting for device to reset");
335 /* takes at least 300ms for the FX2 to be gone from the USB bus */
336 g_usleep(300 * 1000);
fc8fe3e3
BV
337 timediff_ms = 0;
338 while (timediff_ms < MAX_RENUM_DELAY_MS) {
3b533202
BV
339 if ((err = dso_open(dev_index)) == SR_OK)
340 break;
341 g_usleep(100 * 1000);
fc8fe3e3
BV
342 timediff_us = g_get_monotonic_time() - ctx->fw_updated;
343 timediff_ms = timediff_us / 1000;
99f5d45e 344 sr_spew("hantek-dso: waited %" PRIi64 " ms", timediff_ms);
3b533202 345 }
fc8fe3e3 346 sr_info("hantek-dso: device came back after %d ms", timediff_ms);
3b533202
BV
347 } else {
348 err = dso_open(dev_index);
349 }
350
351 if (err != SR_OK) {
352 sr_err("hantek-dso: unable to open device");
353 return SR_ERR;
354 }
355
356 err = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE);
357 if (err != 0) {
358 sr_err("hantek-dso: Unable to claim interface: %d", err);
359 return SR_ERR;
360 }
361
362 return SR_OK;
363}
364
365static int hw_dev_close(int dev_index)
366{
367 struct sr_dev_inst *sdi;
368
982947f7 369 if (!(sdi = sr_dev_inst_get(hdi->instances, dev_index)))
3b533202
BV
370 return SR_ERR_ARG;
371
372 dso_close(sdi);
373
374 return SR_OK;
375}
376
377static int hw_cleanup(void)
378{
3b533202 379
39cfdd75 380 clear_instances();
3b533202
BV
381
382 if (usb_context)
383 libusb_exit(usb_context);
384 usb_context = NULL;
385
386 return SR_OK;
387}
388
62bb8840 389static const void *hw_dev_info_get(int dev_index, int dev_info_id)
3b533202
BV
390{
391 struct sr_dev_inst *sdi;
62bb8840 392 const void *info;
3b533202
BV
393 uint64_t tmp;
394
982947f7 395 if (!(sdi = sr_dev_inst_get(hdi->instances, dev_index)))
3b533202 396 return NULL;
3b533202
BV
397
398 info = NULL;
399 switch (dev_info_id) {
400 case SR_DI_INST:
401 info = sdi;
402 break;
403 case SR_DI_NUM_PROBES:
88a13f30 404 info = GINT_TO_POINTER(NUM_PROBES);
3b533202
BV
405 break;
406 case SR_DI_PROBE_NAMES:
407 info = probe_names;
408 break;
a370ef19
BV
409 case SR_DI_BUFFERSIZES:
410 info = buffersizes;
411 break;
412 case SR_DI_TIMEBASES:
413 info = timebases;
414 break;
415 case SR_DI_TRIGGER_SOURCES:
416 info = trigger_sources;
417 break;
ebb781a6
BV
418 case SR_DI_FILTERS:
419 info = filter_targets;
420 break;
313deed2
BV
421 case SR_DI_VDIVS:
422 info = vdivs;
423 break;
4a090d72
BV
424 case SR_DI_COUPLING:
425 info = coupling;
426 break;
3b533202
BV
427 /* TODO remove this */
428 case SR_DI_CUR_SAMPLERATE:
429 info = &tmp;
430 break;
431 }
432
433 return info;
434}
435
62bb8840 436static int hw_dev_status_get(int dev_index)
3b533202
BV
437{
438 struct sr_dev_inst *sdi;
439
982947f7 440 if (!(sdi = sr_dev_inst_get(hdi->instances, dev_index)))
3b533202
BV
441 return SR_ST_NOT_FOUND;
442
443 return sdi->status;
444}
445
62bb8840 446static const int *hw_hwcap_get_all(void)
3b533202 447{
62bb8840 448 return hwcaps;
3b533202
BV
449}
450
af36b809 451static int hw_dev_config_set(int dev_index, int hwcap, const void *value)
3b533202
BV
452{
453 struct sr_dev_inst *sdi;
454 struct context *ctx;
a370ef19
BV
455 struct sr_rational tmp_rat;
456 float tmp_float;
457 uint64_t tmp_u64;
458 int ret, i;
4a090d72 459 char **targets;
3b533202 460
982947f7 461 if (!(sdi = sr_dev_inst_get(hdi->instances, dev_index)))
3b533202
BV
462 return SR_ERR;
463
464 if (sdi->status != SR_ST_ACTIVE)
465 return SR_ERR;
466
a370ef19 467 ret = SR_OK;
3b533202
BV
468 ctx = sdi->priv;
469 switch (hwcap) {
ae88b97b 470 case SR_HWCAP_LIMIT_FRAMES:
62bb8840 471 ctx->limit_frames = *(const uint64_t *)value;
ae88b97b 472 break;
3b533202 473 case SR_HWCAP_PROBECONFIG:
62bb8840 474 ret = configure_probes(ctx, (const GSList *)value);
3b533202 475 break;
a370ef19 476 case SR_HWCAP_TRIGGER_SLOPE:
62bb8840 477 tmp_u64 = *(const int *)value;
a370ef19
BV
478 if (tmp_u64 != SLOPE_NEGATIVE && tmp_u64 != SLOPE_POSITIVE)
479 ret = SR_ERR_ARG;
480 ctx->triggerslope = tmp_u64;
481 break;
482 case SR_HWCAP_HORIZ_TRIGGERPOS:
62bb8840 483 tmp_float = *(const float *)value;
a370ef19
BV
484 if (tmp_float < 0.0 || tmp_float > 1.0) {
485 sr_err("hantek-dso: trigger position should be between 0.0 and 1.0");
3b533202 486 ret = SR_ERR_ARG;
a370ef19
BV
487 } else
488 ctx->triggerposition = tmp_float;
489 break;
490 case SR_HWCAP_BUFFERSIZE:
62bb8840 491 tmp_u64 = *(const int *)value;
a370ef19
BV
492 for (i = 0; buffersizes[i]; i++) {
493 if (buffersizes[i] == tmp_u64) {
494 ctx->framesize = tmp_u64;
495 break;
496 }
497 }
498 if (buffersizes[i] == 0)
499 ret = SR_ERR_ARG;
500 break;
501 case SR_HWCAP_TIMEBASE:
62bb8840 502 tmp_rat = *(const struct sr_rational *)value;
a370ef19
BV
503 for (i = 0; timebases[i].p && timebases[i].q; i++) {
504 if (timebases[i].p == tmp_rat.p
505 && timebases[i].q == tmp_rat.q) {
506 ctx->timebase = i;
507 break;
508 }
509 }
510 if (timebases[i].p == 0 && timebases[i].q == 0)
511 ret = SR_ERR_ARG;
512 break;
513 case SR_HWCAP_TRIGGER_SOURCE:
a370ef19 514 for (i = 0; trigger_sources[i]; i++) {
4a090d72
BV
515 if (!strcmp(value, trigger_sources[i])) {
516 ctx->triggersource = g_strdup(value);
a370ef19
BV
517 break;
518 }
519 }
520 if (trigger_sources[i] == 0)
521 ret = SR_ERR_ARG;
522 break;
ebb781a6
BV
523 case SR_HWCAP_FILTER:
524 ctx->filter_ch1 = ctx->filter_ch2 = ctx->filter_trigger = 0;
525 targets = g_strsplit(value, ",", 0);
526 for (i = 0; targets[i]; i++) {
527 if (targets[i] == '\0')
528 /* Empty filter string can be used to clear them all. */
529 ;
530 else if (!strcmp(targets[i], "CH1"))
531 ctx->filter_ch1 = TRUE;
532 else if (!strcmp(targets[i], "CH2"))
533 ctx->filter_ch2 = TRUE;
534 else if (!strcmp(targets[i], "TRIGGER"))
535 ctx->filter_trigger = TRUE;
536 else {
537 sr_err("invalid filter target %s", targets[i]);
538 ret = SR_ERR_ARG;
539 }
540 }
541 g_strfreev(targets);
542 break;
313deed2
BV
543 case SR_HWCAP_VDIV:
544 /* TODO not supporting vdiv per channel yet */
62bb8840 545 tmp_rat = *(const struct sr_rational *)value;
313deed2
BV
546 for (i = 0; vdivs[i].p && vdivs[i].q; i++) {
547 if (vdivs[i].p == tmp_rat.p
548 && vdivs[i].q == tmp_rat.q) {
549 ctx->voltage_ch1 = i;
550 ctx->voltage_ch2 = i;
551 break;
552 }
553 }
554 if (vdivs[i].p == 0 && vdivs[i].q == 0)
555 ret = SR_ERR_ARG;
556 break;
b58fbd99
BV
557 case SR_HWCAP_COUPLING:
558 /* TODO not supporting coupling per channel yet */
b58fbd99 559 for (i = 0; coupling[i]; i++) {
4a090d72 560 if (!strcmp(value, coupling[i])) {
b58fbd99
BV
561 ctx->coupling_ch1 = i;
562 ctx->coupling_ch2 = i;
563 break;
564 }
565 }
566 if (coupling[i] == 0)
567 ret = SR_ERR_ARG;
568 break;
3b533202
BV
569 default:
570 ret = SR_ERR_ARG;
571 }
572
573 return ret;
574}
575
e749a8cb
BV
576static void send_chunk(struct context *ctx, unsigned char *buf,
577 int num_samples)
3b533202
BV
578{
579 struct sr_datafeed_packet packet;
580 struct sr_datafeed_analog analog;
c5841b28 581 float ch1, ch2, range;
6e71ef3b 582 int num_probes, data_offset, i;
3b533202 583
6e71ef3b 584 num_probes = (ctx->ch1_enabled && ctx->ch2_enabled) ? 2 : 1;
3b533202
BV
585 packet.type = SR_DF_ANALOG;
586 packet.payload = &analog;
6e71ef3b 587 /* TODO: support for 5xxx series 9-bit samples */
e749a8cb 588 analog.num_samples = num_samples;
9956f285
UH
589 analog.mq = SR_MQ_VOLTAGE;
590 analog.unit = SR_UNIT_VOLT;
6e71ef3b
BV
591 analog.data = g_try_malloc(analog.num_samples * sizeof(float) * num_probes);
592 data_offset = 0;
3b533202 593 for (i = 0; i < analog.num_samples; i++) {
6e71ef3b
BV
594 /* The device always sends data for both channels. If a channel
595 * is disabled, it contains a copy of the enabled channel's
596 * data. However, we only send the requested channels to the bus.
c5841b28
BV
597 *
598 * Voltage values are encoded as a value 0-255 (0-512 on the 5200*),
599 * where the value is a point in the range represented by the vdiv
600 * setting. There are 8 vertical divs, so e.g. 500mV/div represents
601 * 4V peak-to-peak where 0 = -2V and 255 = +2V.
6e71ef3b 602 */
3b533202 603 /* TODO: support for 5xxx series 9-bit samples */
6e71ef3b 604 if (ctx->ch1_enabled) {
c5841b28 605 range = ((float)vdivs[ctx->voltage_ch1].p / vdivs[ctx->voltage_ch1].q) * 8;
e749a8cb 606 ch1 = range / 255 * *(buf + i * 2 + 1);
c5841b28
BV
607 /* Value is centered around 0V. */
608 ch1 -= range / 2;
6e71ef3b
BV
609 analog.data[data_offset++] = ch1;
610 }
611 if (ctx->ch2_enabled) {
c5841b28 612 range = ((float)vdivs[ctx->voltage_ch2].p / vdivs[ctx->voltage_ch2].q) * 8;
e749a8cb 613 ch2 = range / 255 * *(buf + i * 2);
c5841b28 614 ch2 -= range / 2;
6e71ef3b
BV
615 analog.data[data_offset++] = ch2;
616 }
3b533202 617 }
e749a8cb
BV
618 sr_session_send(ctx->cb_data, &packet);
619
620}
621
622/* Called by libusb (as triggered by handle_event()) when a transfer comes in.
623 * Only channel data comes in asynchronously, and all transfers for this are
624 * queued up beforehand, so this just needs so chuck the incoming data onto
625 * the libsigrok session bus.
626 */
627static void receive_transfer(struct libusb_transfer *transfer)
628{
629 struct sr_datafeed_packet packet;
630 struct context *ctx;
631 int num_samples, pre;
632
633 ctx = transfer->user_data;
634 sr_dbg("hantek-dso: receive_transfer(): status %d received %d bytes",
635 transfer->status, transfer->actual_length);
636
637 if (transfer->actual_length == 0)
638 /* Nothing to send to the bus. */
639 return;
640
641 num_samples = transfer->actual_length / 2;
642
643 sr_dbg("hantek-dso: got %d-%d/%d samples in frame", ctx->samp_received + 1,
644 ctx->samp_received + num_samples, ctx->framesize);
645
646 /* The device always sends a full frame, but the beginning of the frame
647 * doesn't represent the trigger point. The offset at which the trigger
648 * happened came in with the capture state, so we need to start sending
649 * from there up the session bus. The samples in the frame buffer before
650 * that trigger point came after the end of the device's frame buffer was
651 * reached, and it wrapped around to overwrite up until the trigger point.
652 */
653 if (ctx->samp_received < ctx->trigger_offset) {
654 /* Trigger point not yet reached. */
655 if (ctx->samp_received + num_samples < ctx->trigger_offset) {
656 /* The entire chunk is before the trigger point. */
657 memcpy(ctx->framebuf + ctx->samp_buffered * 2,
658 transfer->buffer, num_samples * 2);
659 ctx->samp_buffered += num_samples;
660 } else {
661 /* This chunk hits or overruns the trigger point.
662 * Store the part before the trigger fired, and
663 * send the rest up to the session bus. */
664 pre = ctx->trigger_offset - ctx->samp_received;
665 memcpy(ctx->framebuf + ctx->samp_buffered * 2,
666 transfer->buffer, pre * 2);
667 ctx->samp_buffered += pre;
668
669 /* The rest of this chunk starts with the trigger point. */
670 sr_dbg("hantek-dso: reached trigger point, %d samples buffered",
671 ctx->samp_buffered);
672
673 /* Avoid the corner case where the chunk ended at
674 * exactly the trigger point. */
675 if (num_samples > pre)
676 send_chunk(ctx, transfer->buffer + pre * 2,
677 num_samples - pre);
678 }
679 } else {
680 /* Already past the trigger point, just send it all out. */
681 send_chunk(ctx, transfer->buffer,
682 num_samples);
683 }
684
685 ctx->samp_received += num_samples;
686
687 /* Everything in this transfer was either copied to the buffer or
688 * sent to the session bus. */
3b533202
BV
689 g_free(transfer->buffer);
690 libusb_free_transfer(transfer);
3b533202 691
e749a8cb
BV
692 if (ctx->samp_received >= ctx->framesize) {
693 /* That was the last chunk in this frame. Send the buffered
694 * pre-trigger samples out now, in one big chunk. */
695 sr_dbg("hantek-dso: end of frame, sending %d pre-trigger buffered samples",
696 ctx->samp_buffered);
697 send_chunk(ctx, ctx->framebuf, ctx->samp_buffered);
698
699 /* Mark the end of this frame. */
ae88b97b
BV
700 packet.type = SR_DF_FRAME_END;
701 sr_session_send(ctx->cb_data, &packet);
702
703 if (ctx->limit_frames && ++ctx->num_frames == ctx->limit_frames) {
704 /* Terminate session */
6e71ef3b 705 /* TODO: don't leave pending USB transfers hanging */
ae88b97b
BV
706 packet.type = SR_DF_END;
707 sr_session_send(ctx->cb_data, &packet);
708 } else {
ae88b97b
BV
709 ctx->dev_state = NEW_CAPTURE;
710 }
711 }
712
3b533202
BV
713}
714
715static int handle_event(int fd, int revents, void *cb_data)
716{
ae88b97b 717 struct sr_datafeed_packet packet;
3b533202
BV
718 struct timeval tv;
719 struct context *ctx;
e749a8cb 720 int num_probes;
6e6eeff4
BV
721 uint32_t trigger_offset;
722 uint8_t capturestate;
3b533202
BV
723
724 /* Avoid compiler warnings. */
725 (void)fd;
726 (void)revents;
727
728 /* Always handle pending libusb events. */
729 tv.tv_sec = tv.tv_usec = 0;
730 libusb_handle_events_timeout(usb_context, &tv);
731
732 ctx = cb_data;
733 /* TODO: ugh */
734 if (ctx->dev_state == NEW_CAPTURE) {
735 if (dso_capture_start(ctx) != SR_OK)
736 return TRUE;
737 if (dso_enable_trigger(ctx) != SR_OK)
738 return TRUE;
a370ef19
BV
739// if (dso_force_trigger(ctx) != SR_OK)
740// return TRUE;
3b533202
BV
741 sr_dbg("hantek-dso: successfully requested next chunk");
742 ctx->dev_state = CAPTURE;
743 return TRUE;
744 }
745 if (ctx->dev_state != CAPTURE)
746 return TRUE;
747
6e6eeff4 748 if ((dso_get_capturestate(ctx, &capturestate, &trigger_offset)) != SR_OK)
3b533202 749 return TRUE;
3b533202
BV
750
751 sr_dbg("hantek-dso: capturestate %d", capturestate);
6e6eeff4 752 sr_dbg("hantek-dso: trigger offset 0x%.6x", trigger_offset);
3b533202
BV
753 switch (capturestate) {
754 case CAPTURE_EMPTY:
755 if (++ctx->capture_empty_count >= MAX_CAPTURE_EMPTY) {
756 ctx->capture_empty_count = 0;
757 if (dso_capture_start(ctx) != SR_OK)
758 break;
759 if (dso_enable_trigger(ctx) != SR_OK)
760 break;
a370ef19
BV
761// if (dso_force_trigger(ctx) != SR_OK)
762// break;
3b533202
BV
763 sr_dbg("hantek-dso: successfully requested next chunk");
764 }
765 break;
766 case CAPTURE_FILLING:
767 /* no data yet */
768 break;
769 case CAPTURE_READY_8BIT:
e749a8cb
BV
770 /* Remember where in the captured frame the trigger is. */
771 ctx->trigger_offset = trigger_offset;
772
773 num_probes = (ctx->ch1_enabled && ctx->ch2_enabled) ? 2 : 1;
774 ctx->framebuf = g_try_malloc(ctx->framesize * num_probes * 2);
775 ctx->samp_buffered = ctx->samp_received = 0;
776
3b533202
BV
777 /* Tell the scope to send us the first frame. */
778 if (dso_get_channeldata(ctx, receive_transfer) != SR_OK)
779 break;
ae88b97b
BV
780
781 /* Don't hit the state machine again until we're done fetching
782 * the data we just told the scope to send.
783 */
3b533202 784 ctx->dev_state = FETCH_DATA;
ae88b97b
BV
785
786 /* Tell the frontend a new frame is on the way. */
787 packet.type = SR_DF_FRAME_BEGIN;
788 sr_session_send(cb_data, &packet);
3b533202
BV
789 break;
790 case CAPTURE_READY_9BIT:
791 /* TODO */
792 sr_err("not yet supported");
793 break;
794 case CAPTURE_TIMEOUT:
795 /* Doesn't matter, we'll try again next time. */
796 break;
797 default:
798 sr_dbg("unknown capture state");
799 }
800
801 return TRUE;
802}
803
62bb8840 804static int hw_dev_acquisition_start(int dev_index, void *cb_data)
3b533202
BV
805{
806 const struct libusb_pollfd **lupfd;
807 struct sr_datafeed_packet packet;
808 struct sr_datafeed_header header;
809 struct sr_datafeed_meta_analog meta;
810 struct sr_dev_inst *sdi;
811 struct context *ctx;
812 int i;
813
982947f7 814 if (!(sdi = sr_dev_inst_get(hdi->instances, dev_index)))
3b533202
BV
815 return SR_ERR;
816
817 if (sdi->status != SR_ST_ACTIVE)
818 return SR_ERR;
819
820 ctx = sdi->priv;
821 ctx->cb_data = cb_data;
822
823 if (dso_init(ctx) != SR_OK)
824 return SR_ERR;
825
826 if (dso_capture_start(ctx) != SR_OK)
827 return SR_ERR;
828
829 ctx->dev_state = CAPTURE;
830 lupfd = libusb_get_pollfds(usb_context);
831 for (i = 0; lupfd[i]; i++)
832 sr_source_add(lupfd[i]->fd, lupfd[i]->events, TICK, handle_event,
833 ctx);
834 free(lupfd);
835
836 /* Send header packet to the session bus. */
837 packet.type = SR_DF_HEADER;
838 packet.payload = (unsigned char *)&header;
839 header.feed_version = 1;
840 gettimeofday(&header.starttime, NULL);
841 sr_session_send(cb_data, &packet);
842
843 /* Send metadata about the SR_DF_ANALOG packets to come. */
844 packet.type = SR_DF_META_ANALOG;
845 packet.payload = &meta;
88a13f30 846 meta.num_probes = NUM_PROBES;
3b533202
BV
847 sr_session_send(cb_data, &packet);
848
849 return SR_OK;
850}
851
852/* TODO: doesn't really cancel pending transfers so they might come in after
853 * SR_DF_END is sent.
854 */
62bb8840 855static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
3b533202
BV
856{
857 struct sr_datafeed_packet packet;
858 struct sr_dev_inst *sdi;
859 struct context *ctx;
860
982947f7 861 if (!(sdi = sr_dev_inst_get(hdi->instances, dev_index)))
3b533202
BV
862 return SR_ERR;
863
864 if (sdi->status != SR_ST_ACTIVE)
865 return SR_ERR;
866
867 ctx = sdi->priv;
868 ctx->dev_state = IDLE;
869
870 packet.type = SR_DF_END;
62bb8840 871 sr_session_send(cb_data, &packet);
3b533202
BV
872
873 return SR_OK;
874}
875
62bb8840 876SR_PRIV struct sr_dev_driver hantek_dso_driver_info = {
3b533202
BV
877 .name = "hantek-dso",
878 .longname = "Hantek DSO",
879 .api_version = 1,
880 .init = hw_init,
881 .cleanup = hw_cleanup,
61136ea6 882 .scan = hw_scan,
3b533202
BV
883 .dev_open = hw_dev_open,
884 .dev_close = hw_dev_close,
62bb8840
UH
885 .dev_info_get = hw_dev_info_get,
886 .dev_status_get = hw_dev_status_get,
887 .hwcap_get_all = hw_hwcap_get_all,
3b533202 888 .dev_config_set = hw_dev_config_set,
62bb8840
UH
889 .dev_acquisition_start = hw_dev_acquisition_start,
890 .dev_acquisition_stop = hw_dev_acquisition_stop,
982947f7 891 .instances = NULL,
3b533202 892};