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