]> sigrok.org Git - libsigrok.git/blame - hardware/hantek-dso/api.c
fx2lafw: Added a check to limit the sample rate during 16-bit sampling
[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>
30#include <arpa/inet.h>
31#include <glib.h>
32#include <libusb.h>
33#include "sigrok.h"
34#include "sigrok-internal.h"
35#include "config.h"
36#include "dso.h"
37
38
39/* Max time in ms before we want to check on events */
40#define TICK 1
41
42static int capabilities[] = {
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
63static struct dso_profile dev_profiles[] = {
64 { 0x04b4, 0x2090,
65 0x04b5, 0x2090,
66 "Hantek", "DSO-2090",
67 NULL, 2,
68 FIRMWARE_DIR "/hantek-dso-2090.fw" },
69 { 0, 0, 0, 0, 0, 0, 0, 0, 0 }
70};
71
a370ef19
BV
72static uint64_t buffersizes[] = {
73 10240,
74 32768,
75 /* TODO: 65535 */
76 0
77};
78
79static struct sr_rational timebases[] = {
80 /* microseconds */
81 { 10, 1000000 },
82 { 20, 1000000 },
83 { 40, 1000000 },
84 { 100, 1000000 },
85 { 200, 1000000 },
86 { 400, 1000000 },
87 /* milliseconds */
88 { 1, 1000 },
89 { 2, 1000 },
90 { 4, 1000 },
91 { 10, 1000 },
92 { 20, 1000 },
93 { 40, 1000 },
94 { 100, 1000 },
95 { 200, 1000 },
96 { 400, 1000 },
97 {0,0}
98};
99
313deed2
BV
100static struct sr_rational vdivs[] = {
101 /* millivolts */
102 { 10, 1000 },
103 { 20, 1000 },
104 { 50, 1000 },
105 { 100, 1000 },
106 { 200, 1000 },
107 { 500, 1000 },
108 /* volts */
109 { 1, 1 },
110 { 2, 1 },
111 { 5, 1 },
112 {0,0}
113};
114
a370ef19
BV
115static char *trigger_sources[] = {
116 "CH1",
117 "CH2",
118 "EXT",
119 NULL
120};
3b533202 121
ebb781a6
BV
122static char *filter_targets[] = {
123 "CH1",
124 "CH2",
125 /* TODO: "TRIGGER", */
126 NULL
127};
128
b58fbd99
BV
129static char *coupling[] = {
130 "AC",
131 "DC",
132 "GND",
133 NULL
134};
135
3b533202
BV
136SR_PRIV libusb_context *usb_context = NULL;
137SR_PRIV GSList *dev_insts = NULL;
138
139
140static struct sr_dev_inst *dso_dev_new(int index, struct dso_profile *prof)
141{
142 struct sr_dev_inst *sdi;
143 struct context *ctx;
144
145 sdi = sr_dev_inst_new(index, SR_ST_INITIALIZING,
146 prof->vendor, prof->model, prof->model_version);
147 if (!sdi)
148 return NULL;
149
150 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
151 sr_err("hantek-dso: ctx malloc failed");
152 return NULL;
153 }
154 ctx->profile = prof;
155 ctx->dev_state = IDLE;
156 ctx->timebase = DEFAULT_TIMEBASE;
157 ctx->ch1_enabled = TRUE;
158 ctx->ch2_enabled = TRUE;
159 ctx->voltage_ch1 = DEFAULT_VOLTAGE;
160 ctx->voltage_ch2 = DEFAULT_VOLTAGE;
161 ctx->coupling_ch1 = DEFAULT_COUPLING;
162 ctx->coupling_ch2 = DEFAULT_COUPLING;
163 ctx->voffset_ch1 = DEFAULT_VERT_OFFSET;
164 ctx->voffset_ch2 = DEFAULT_VERT_OFFSET;
165 ctx->voffset_trigger = DEFAULT_VERT_TRIGGERPOS;
3b533202
BV
166 ctx->framesize = DEFAULT_FRAMESIZE;
167 ctx->triggerslope = SLOPE_POSITIVE;
a370ef19 168 ctx->triggersource = g_strdup(DEFAULT_TRIGGER_SOURCE);
3b533202
BV
169 ctx->triggerposition = DEFAULT_HORIZ_TRIGGERPOS;
170 sdi->priv = ctx;
171 dev_insts = g_slist_append(dev_insts, sdi);
172
173 return sdi;
174}
175
176static int configure_probes(struct context *ctx, GSList *probes)
177{
178 struct sr_probe *probe;
179 GSList *l;
180
6e71ef3b 181 ctx->ch1_enabled = ctx->ch2_enabled = FALSE;
3b533202
BV
182 for (l = probes; l; l = l->next) {
183 probe = (struct sr_probe *)l->data;
6e71ef3b 184 if (probe->index == 1)
3b533202 185 ctx->ch1_enabled = probe->enabled;
6e71ef3b 186 else if (probe->index == 2)
3b533202
BV
187 ctx->ch2_enabled = probe->enabled;
188 }
189
190 return SR_OK;
191}
192
193static int hw_init(const char *devinfo)
194{
195 struct sr_dev_inst *sdi;
196 struct libusb_device_descriptor des;
197 struct dso_profile *prof;
198 struct context *ctx;
199 libusb_device **devlist;
200 int err, devcnt, i, j;
201
202 /* Avoid compiler warnings. */
203 (void)devinfo;
204
205 if (libusb_init(&usb_context) != 0) {
206 sr_err("hantek-dso: Failed to initialize USB.");
207 return 0;
208 }
209
210 /* Find all Hantek DSO devices and upload firmware to all of them. */
211 devcnt = 0;
212 libusb_get_device_list(usb_context, &devlist);
213 for (i = 0; devlist[i]; i++) {
214 if ((err = libusb_get_device_descriptor(devlist[i], &des))) {
215 sr_err("hantek-dso: failed to get device descriptor: %d", err);
216 continue;
217 }
218
219 prof = NULL;
220 for (j = 0; dev_profiles[j].orig_vid; j++) {
221 if (des.idVendor == dev_profiles[j].orig_vid
222 && des.idProduct == dev_profiles[j].orig_pid) {
223 /* Device matches the pre-firmware profile. */
224 prof = &dev_profiles[j];
225 sr_dbg("hantek-dso: Found a %s %s.", prof->vendor, prof->model);
226 sdi = dso_dev_new(devcnt, prof);
227 ctx = sdi->priv;
228 if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION,
229 prof->firmware) == SR_OK)
230 /* Remember when the firmware on this device was updated */
231 g_get_current_time(&ctx->fw_updated);
232 else
233 sr_err("hantek-dso: firmware upload failed for "
234 "device %d", devcnt);
235 /* Dummy USB address of 0xff will get overwritten later. */
236 ctx->usb = sr_usb_dev_inst_new(
237 libusb_get_bus_number(devlist[i]), 0xff, NULL);
238 devcnt++;
239 break;
240 } else if (des.idVendor == dev_profiles[j].fw_vid
241 && des.idProduct == dev_profiles[j].fw_pid) {
242 /* Device matches the post-firmware profile. */
243 prof = &dev_profiles[j];
244 sr_dbg("hantek-dso: Found a %s %s.", prof->vendor, prof->model);
245 sdi = dso_dev_new(devcnt, prof);
246 sdi->status = SR_ST_INACTIVE;
247 ctx = sdi->priv;
248 ctx->usb = sr_usb_dev_inst_new(
249 libusb_get_bus_number(devlist[i]),
250 libusb_get_device_address(devlist[i]), NULL);
251 devcnt++;
252 break;
253 }
254 }
255 if (!prof)
256 /* not a supported VID/PID */
257 continue;
258 }
259 libusb_free_device_list(devlist, 1);
260
261 return devcnt;
262}
263
264static int hw_dev_open(int dev_index)
265{
266 GTimeVal cur_time;
267 struct sr_dev_inst *sdi;
268 struct context *ctx;
269 int timediff, err;
270
271 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
272 return SR_ERR_ARG;
273 ctx = sdi->priv;
274
275 /*
276 * if the firmware was recently uploaded, wait up to MAX_RENUM_DELAY ms
277 * for the FX2 to renumerate
278 */
279 err = 0;
280 if (GTV_TO_MSEC(ctx->fw_updated) > 0) {
281 sr_info("hantek-dso: waiting for device to reset");
282 /* takes at least 300ms for the FX2 to be gone from the USB bus */
283 g_usleep(300 * 1000);
284 timediff = 0;
285 while (timediff < MAX_RENUM_DELAY) {
286 if ((err = dso_open(dev_index)) == SR_OK)
287 break;
288 g_usleep(100 * 1000);
289 g_get_current_time(&cur_time);
290 timediff = GTV_TO_MSEC(cur_time) - GTV_TO_MSEC(ctx->fw_updated);
291 }
292 sr_info("hantek-dso: device came back after %d ms", timediff);
293 } else {
294 err = dso_open(dev_index);
295 }
296
297 if (err != SR_OK) {
298 sr_err("hantek-dso: unable to open device");
299 return SR_ERR;
300 }
301
302 err = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE);
303 if (err != 0) {
304 sr_err("hantek-dso: Unable to claim interface: %d", err);
305 return SR_ERR;
306 }
307
308 return SR_OK;
309}
310
311static int hw_dev_close(int dev_index)
312{
313 struct sr_dev_inst *sdi;
314
315 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
316 return SR_ERR_ARG;
317
318 dso_close(sdi);
319
320 return SR_OK;
321}
322
323static int hw_cleanup(void)
324{
325 GSList *l;
326 struct sr_dev_inst *sdi;
327 struct context *ctx;
328
329 /* Properly close and free all devices. */
330 for (l = dev_insts; l; l = l->next) {
331 if (!(sdi = l->data)) {
332 /* Log error, but continue cleaning up the rest. */
333 sr_err("hantek-dso: %s: sdi was NULL, continuing", __func__);
334 continue;
335 }
336 if (!(ctx = sdi->priv)) {
337 /* Log error, but continue cleaning up the rest. */
338 sr_err("hantek-dso: %s: sdi->priv was NULL, continuing", __func__);
339 continue;
340 }
341 dso_close(sdi);
342 sr_usb_dev_inst_free(ctx->usb);
a370ef19
BV
343 g_free(ctx->triggersource);
344
3b533202
BV
345 sr_dev_inst_free(sdi);
346 }
347
348 g_slist_free(dev_insts);
349 dev_insts = NULL;
350
351 if (usb_context)
352 libusb_exit(usb_context);
353 usb_context = NULL;
354
355 return SR_OK;
356}
357
358static void *hw_get_device_info(int dev_index, int dev_info_id)
359{
360 struct sr_dev_inst *sdi;
361 struct context *ctx;
362 void *info;
363 uint64_t tmp;
364
365 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
366 return NULL;
367 ctx = sdi->priv;
368
369 info = NULL;
370 switch (dev_info_id) {
371 case SR_DI_INST:
372 info = sdi;
373 break;
374 case SR_DI_NUM_PROBES:
375 info = GINT_TO_POINTER(ctx->profile->num_probes);
376 break;
377 case SR_DI_PROBE_NAMES:
378 info = probe_names;
379 break;
a370ef19
BV
380 case SR_DI_BUFFERSIZES:
381 info = buffersizes;
382 break;
383 case SR_DI_TIMEBASES:
384 info = timebases;
385 break;
386 case SR_DI_TRIGGER_SOURCES:
387 info = trigger_sources;
388 break;
ebb781a6
BV
389 case SR_DI_FILTERS:
390 info = filter_targets;
391 break;
313deed2
BV
392 case SR_DI_VDIVS:
393 info = vdivs;
394 break;
4a090d72
BV
395 case SR_DI_COUPLING:
396 info = coupling;
397 break;
3b533202
BV
398 /* TODO remove this */
399 case SR_DI_CUR_SAMPLERATE:
400 info = &tmp;
401 break;
402 }
403
404 return info;
405}
406
407static int hw_get_status(int device_index)
408{
409 struct sr_dev_inst *sdi;
410
411 if (!(sdi = sr_dev_inst_get(dev_insts, device_index)))
412 return SR_ST_NOT_FOUND;
413
414 return sdi->status;
415}
416
417static int *hwcap_get_all(void)
418{
419
420 return capabilities;
421}
422
423static int hw_dev_config_set(int dev_index, int hwcap, void *value)
424{
425 struct sr_dev_inst *sdi;
426 struct context *ctx;
a370ef19
BV
427 struct sr_rational tmp_rat;
428 float tmp_float;
429 uint64_t tmp_u64;
430 int ret, i;
4a090d72 431 char **targets;
3b533202
BV
432
433 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
434 return SR_ERR;
435
436 if (sdi->status != SR_ST_ACTIVE)
437 return SR_ERR;
438
a370ef19 439 ret = SR_OK;
3b533202
BV
440 ctx = sdi->priv;
441 switch (hwcap) {
ae88b97b
BV
442 case SR_HWCAP_LIMIT_FRAMES:
443 ctx->limit_frames = *(uint64_t *)value;
444 break;
3b533202
BV
445 case SR_HWCAP_PROBECONFIG:
446 ret = configure_probes(ctx, (GSList *) value);
447 break;
a370ef19
BV
448 case SR_HWCAP_TRIGGER_SLOPE:
449 tmp_u64 = *(int *)value;
450 if (tmp_u64 != SLOPE_NEGATIVE && tmp_u64 != SLOPE_POSITIVE)
451 ret = SR_ERR_ARG;
452 ctx->triggerslope = tmp_u64;
453 break;
454 case SR_HWCAP_HORIZ_TRIGGERPOS:
455 tmp_float = *(float *)value;
456 if (tmp_float < 0.0 || tmp_float > 1.0) {
457 sr_err("hantek-dso: trigger position should be between 0.0 and 1.0");
3b533202 458 ret = SR_ERR_ARG;
a370ef19
BV
459 } else
460 ctx->triggerposition = tmp_float;
461 break;
462 case SR_HWCAP_BUFFERSIZE:
463 tmp_u64 = *(int *)value;
464 for (i = 0; buffersizes[i]; i++) {
465 if (buffersizes[i] == tmp_u64) {
466 ctx->framesize = tmp_u64;
467 break;
468 }
469 }
470 if (buffersizes[i] == 0)
471 ret = SR_ERR_ARG;
472 break;
473 case SR_HWCAP_TIMEBASE:
474 tmp_rat = *(struct sr_rational *)value;
475 for (i = 0; timebases[i].p && timebases[i].q; i++) {
476 if (timebases[i].p == tmp_rat.p
477 && timebases[i].q == tmp_rat.q) {
478 ctx->timebase = i;
479 break;
480 }
481 }
482 if (timebases[i].p == 0 && timebases[i].q == 0)
483 ret = SR_ERR_ARG;
484 break;
485 case SR_HWCAP_TRIGGER_SOURCE:
a370ef19 486 for (i = 0; trigger_sources[i]; i++) {
4a090d72
BV
487 if (!strcmp(value, trigger_sources[i])) {
488 ctx->triggersource = g_strdup(value);
a370ef19
BV
489 break;
490 }
491 }
492 if (trigger_sources[i] == 0)
493 ret = SR_ERR_ARG;
494 break;
ebb781a6
BV
495 case SR_HWCAP_FILTER:
496 ctx->filter_ch1 = ctx->filter_ch2 = ctx->filter_trigger = 0;
497 targets = g_strsplit(value, ",", 0);
498 for (i = 0; targets[i]; i++) {
499 if (targets[i] == '\0')
500 /* Empty filter string can be used to clear them all. */
501 ;
502 else if (!strcmp(targets[i], "CH1"))
503 ctx->filter_ch1 = TRUE;
504 else if (!strcmp(targets[i], "CH2"))
505 ctx->filter_ch2 = TRUE;
506 else if (!strcmp(targets[i], "TRIGGER"))
507 ctx->filter_trigger = TRUE;
508 else {
509 sr_err("invalid filter target %s", targets[i]);
510 ret = SR_ERR_ARG;
511 }
512 }
513 g_strfreev(targets);
514 break;
313deed2
BV
515 case SR_HWCAP_VDIV:
516 /* TODO not supporting vdiv per channel yet */
517 tmp_rat = *(struct sr_rational *)value;
518 for (i = 0; vdivs[i].p && vdivs[i].q; i++) {
519 if (vdivs[i].p == tmp_rat.p
520 && vdivs[i].q == tmp_rat.q) {
521 ctx->voltage_ch1 = i;
522 ctx->voltage_ch2 = i;
523 break;
524 }
525 }
526 if (vdivs[i].p == 0 && vdivs[i].q == 0)
527 ret = SR_ERR_ARG;
528 break;
b58fbd99
BV
529 case SR_HWCAP_COUPLING:
530 /* TODO not supporting coupling per channel yet */
b58fbd99 531 for (i = 0; coupling[i]; i++) {
4a090d72 532 if (!strcmp(value, coupling[i])) {
b58fbd99
BV
533 ctx->coupling_ch1 = i;
534 ctx->coupling_ch2 = i;
535 break;
536 }
537 }
538 if (coupling[i] == 0)
539 ret = SR_ERR_ARG;
540 break;
3b533202
BV
541 default:
542 ret = SR_ERR_ARG;
543 }
544
545 return ret;
546}
547
548/* Called by libusb (as triggered by handle_event()) when a transfer comes in.
549 * Only channel data comes in asynchronously, and all transfers for this are
550 * queued up beforehand, so this just needs so chuck the incoming data onto
551 * the libsigrok session bus.
552 */
553static void receive_transfer(struct libusb_transfer *transfer)
554{
555 struct sr_datafeed_packet packet;
556 struct sr_datafeed_analog analog;
557 struct context *ctx;
558 float ch1, ch2;
6e71ef3b 559 int num_probes, data_offset, i;
3b533202
BV
560
561 ctx = transfer->user_data;
562 sr_dbg("hantek-dso: receive_transfer(): status %d received %d bytes",
563 transfer->status, transfer->actual_length);
564
565 if (transfer->actual_length == 0)
566 /* Nothing to send to the bus. */
567 return;
568
569 ctx->current_transfer += transfer->actual_length;
570 sr_dbg("hantek-dso: got %d of %d in frame", ctx->current_transfer, ctx->framesize * 2);
3b533202 571
6e71ef3b 572 num_probes = (ctx->ch1_enabled && ctx->ch2_enabled) ? 2 : 1;
3b533202
BV
573 packet.type = SR_DF_ANALOG;
574 packet.payload = &analog;
6e71ef3b 575 /* TODO: support for 5xxx series 9-bit samples */
3b533202 576 analog.num_samples = transfer->actual_length / 2;
6e71ef3b
BV
577 analog.data = g_try_malloc(analog.num_samples * sizeof(float) * num_probes);
578 data_offset = 0;
3b533202 579 for (i = 0; i < analog.num_samples; i++) {
6e71ef3b
BV
580 /* The device always sends data for both channels. If a channel
581 * is disabled, it contains a copy of the enabled channel's
582 * data. However, we only send the requested channels to the bus.
583 */
3b533202 584 /* TODO: support for 5xxx series 9-bit samples */
6e71ef3b
BV
585 if (ctx->ch1_enabled) {
586 ch1 = (*(transfer->buffer + i * 2 + 1) / 255.0);
587 analog.data[data_offset++] = ch1;
588 }
589 if (ctx->ch2_enabled) {
590 ch2 = (*(transfer->buffer + i * 2) / 255.0);
591 analog.data[data_offset++] = ch2;
592 }
3b533202
BV
593 }
594 g_free(transfer->buffer);
595 libusb_free_transfer(transfer);
3b533202
BV
596 sr_session_send(ctx->cb_data, &packet);
597
ae88b97b
BV
598 if (ctx->current_transfer >= ctx->framesize * 2) {
599 /* That's the last chunk in this frame. */
600 packet.type = SR_DF_FRAME_END;
601 sr_session_send(ctx->cb_data, &packet);
602
603 if (ctx->limit_frames && ++ctx->num_frames == ctx->limit_frames) {
604 /* Terminate session */
6e71ef3b 605 /* TODO: don't leave pending USB transfers hanging */
ae88b97b
BV
606 packet.type = SR_DF_END;
607 sr_session_send(ctx->cb_data, &packet);
608 } else {
609 ctx->current_transfer = 0;
610 ctx->dev_state = NEW_CAPTURE;
611 }
612 }
613
3b533202
BV
614}
615
616static int handle_event(int fd, int revents, void *cb_data)
617{
ae88b97b 618 struct sr_datafeed_packet packet;
3b533202
BV
619 struct timeval tv;
620 struct context *ctx;
621 int capturestate;
622
623 /* Avoid compiler warnings. */
624 (void)fd;
625 (void)revents;
626
627 /* Always handle pending libusb events. */
628 tv.tv_sec = tv.tv_usec = 0;
629 libusb_handle_events_timeout(usb_context, &tv);
630
631 ctx = cb_data;
632 /* TODO: ugh */
633 if (ctx->dev_state == NEW_CAPTURE) {
634 if (dso_capture_start(ctx) != SR_OK)
635 return TRUE;
636 if (dso_enable_trigger(ctx) != SR_OK)
637 return TRUE;
a370ef19
BV
638// if (dso_force_trigger(ctx) != SR_OK)
639// return TRUE;
3b533202
BV
640 sr_dbg("hantek-dso: successfully requested next chunk");
641 ctx->dev_state = CAPTURE;
642 return TRUE;
643 }
644 if (ctx->dev_state != CAPTURE)
645 return TRUE;
646
647 if ((capturestate = dso_get_capturestate(ctx)) == CAPTURE_UNKNOWN) {
648 /* Generated by the function, not the hardware. */
649 return TRUE;
650 }
651
652 sr_dbg("hantek-dso: capturestate %d", capturestate);
653 switch (capturestate) {
654 case CAPTURE_EMPTY:
655 if (++ctx->capture_empty_count >= MAX_CAPTURE_EMPTY) {
656 ctx->capture_empty_count = 0;
657 if (dso_capture_start(ctx) != SR_OK)
658 break;
659 if (dso_enable_trigger(ctx) != SR_OK)
660 break;
a370ef19
BV
661// if (dso_force_trigger(ctx) != SR_OK)
662// break;
3b533202
BV
663 sr_dbg("hantek-dso: successfully requested next chunk");
664 }
665 break;
666 case CAPTURE_FILLING:
667 /* no data yet */
668 break;
669 case CAPTURE_READY_8BIT:
670 /* Tell the scope to send us the first frame. */
671 if (dso_get_channeldata(ctx, receive_transfer) != SR_OK)
672 break;
ae88b97b
BV
673
674 /* Don't hit the state machine again until we're done fetching
675 * the data we just told the scope to send.
676 */
3b533202 677 ctx->dev_state = FETCH_DATA;
ae88b97b
BV
678
679 /* Tell the frontend a new frame is on the way. */
680 packet.type = SR_DF_FRAME_BEGIN;
681 sr_session_send(cb_data, &packet);
3b533202
BV
682 break;
683 case CAPTURE_READY_9BIT:
684 /* TODO */
685 sr_err("not yet supported");
686 break;
687 case CAPTURE_TIMEOUT:
688 /* Doesn't matter, we'll try again next time. */
689 break;
690 default:
691 sr_dbg("unknown capture state");
692 }
693
694 return TRUE;
695}
696
697static int hw_start_acquisition(int device_index, void *cb_data)
698{
699 const struct libusb_pollfd **lupfd;
700 struct sr_datafeed_packet packet;
701 struct sr_datafeed_header header;
702 struct sr_datafeed_meta_analog meta;
703 struct sr_dev_inst *sdi;
704 struct context *ctx;
705 int i;
706
707 if (!(sdi = sr_dev_inst_get(dev_insts, device_index)))
708 return SR_ERR;
709
710 if (sdi->status != SR_ST_ACTIVE)
711 return SR_ERR;
712
713 ctx = sdi->priv;
714 ctx->cb_data = cb_data;
715
716 if (dso_init(ctx) != SR_OK)
717 return SR_ERR;
718
719 if (dso_capture_start(ctx) != SR_OK)
720 return SR_ERR;
721
722 ctx->dev_state = CAPTURE;
723 lupfd = libusb_get_pollfds(usb_context);
724 for (i = 0; lupfd[i]; i++)
725 sr_source_add(lupfd[i]->fd, lupfd[i]->events, TICK, handle_event,
726 ctx);
727 free(lupfd);
728
729 /* Send header packet to the session bus. */
730 packet.type = SR_DF_HEADER;
731 packet.payload = (unsigned char *)&header;
732 header.feed_version = 1;
733 gettimeofday(&header.starttime, NULL);
734 sr_session_send(cb_data, &packet);
735
736 /* Send metadata about the SR_DF_ANALOG packets to come. */
737 packet.type = SR_DF_META_ANALOG;
738 packet.payload = &meta;
739 meta.num_probes = ctx->profile->num_probes;
740 sr_session_send(cb_data, &packet);
741
742 return SR_OK;
743}
744
745/* TODO: doesn't really cancel pending transfers so they might come in after
746 * SR_DF_END is sent.
747 */
748static int hw_stop_acquisition(int device_index, gpointer session_device_id)
749{
750 struct sr_datafeed_packet packet;
751 struct sr_dev_inst *sdi;
752 struct context *ctx;
753
754 if (!(sdi = sr_dev_inst_get(dev_insts, device_index)))
755 return SR_ERR;
756
757 if (sdi->status != SR_ST_ACTIVE)
758 return SR_ERR;
759
760 ctx = sdi->priv;
761 ctx->dev_state = IDLE;
762
763 packet.type = SR_DF_END;
764 sr_session_send(session_device_id, &packet);
765
766 return SR_OK;
767}
768
769SR_PRIV struct sr_dev_driver hantek_dso_plugin_info = {
770 .name = "hantek-dso",
771 .longname = "Hantek DSO",
772 .api_version = 1,
773 .init = hw_init,
774 .cleanup = hw_cleanup,
775 .dev_open = hw_dev_open,
776 .dev_close = hw_dev_close,
777 .dev_info_get = hw_get_device_info,
778 .dev_status_get = hw_get_status,
779 .hwcap_get_all = hwcap_get_all,
780 .dev_config_set = hw_dev_config_set,
781 .dev_acquisition_start = hw_start_acquisition,
782 .dev_acquisition_stop = hw_stop_acquisition,
783};