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