]>
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> | |
3b533202 BV |
30 | #include <glib.h> |
31 | #include <libusb.h> | |
32 | #include "sigrok.h" | |
33 | #include "sigrok-internal.h" | |
34 | #include "config.h" | |
35 | #include "dso.h" | |
36 | ||
88ff66c2 UH |
37 | /* FIXME: Temporary build fix, this will be removed later. */ |
38 | #define GTV_TO_MSEC(gtv) (gtv.tv_sec * 1000 + gtv.tv_usec / 1000) | |
3b533202 BV |
39 | |
40 | /* Max time in ms before we want to check on events */ | |
41 | #define TICK 1 | |
42 | ||
62bb8840 | 43 | static const int hwcaps[] = { |
3b533202 | 44 | SR_HWCAP_OSCILLOSCOPE, |
ae88b97b | 45 | SR_HWCAP_LIMIT_SAMPLES, |
3b533202 | 46 | SR_HWCAP_CONTINUOUS, |
a370ef19 BV |
47 | SR_HWCAP_TIMEBASE, |
48 | SR_HWCAP_BUFFERSIZE, | |
49 | SR_HWCAP_TRIGGER_SOURCE, | |
50 | SR_HWCAP_TRIGGER_SLOPE, | |
51 | SR_HWCAP_HORIZ_TRIGGERPOS, | |
ebb781a6 | 52 | SR_HWCAP_FILTER, |
313deed2 | 53 | SR_HWCAP_VDIV, |
4a090d72 | 54 | SR_HWCAP_COUPLING, |
3b533202 BV |
55 | 0, |
56 | }; | |
57 | ||
58 | static const char *probe_names[] = { | |
59 | "CH1", | |
60 | "CH2", | |
61 | NULL, | |
62 | }; | |
63 | ||
62bb8840 | 64 | static const struct dso_profile dev_profiles[] = { |
3b533202 BV |
65 | { 0x04b4, 0x2090, |
66 | 0x04b5, 0x2090, | |
67 | "Hantek", "DSO-2090", | |
68 | NULL, 2, | |
69 | FIRMWARE_DIR "/hantek-dso-2090.fw" }, | |
62bb8840 | 70 | { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
3b533202 BV |
71 | }; |
72 | ||
62bb8840 | 73 | static const uint64_t buffersizes[] = { |
a370ef19 BV |
74 | 10240, |
75 | 32768, | |
76 | /* TODO: 65535 */ | |
62bb8840 | 77 | 0, |
a370ef19 BV |
78 | }; |
79 | ||
62bb8840 | 80 | static const struct sr_rational timebases[] = { |
a370ef19 BV |
81 | /* microseconds */ |
82 | { 10, 1000000 }, | |
83 | { 20, 1000000 }, | |
84 | { 40, 1000000 }, | |
85 | { 100, 1000000 }, | |
86 | { 200, 1000000 }, | |
87 | { 400, 1000000 }, | |
88 | /* milliseconds */ | |
89 | { 1, 1000 }, | |
90 | { 2, 1000 }, | |
91 | { 4, 1000 }, | |
92 | { 10, 1000 }, | |
93 | { 20, 1000 }, | |
94 | { 40, 1000 }, | |
95 | { 100, 1000 }, | |
96 | { 200, 1000 }, | |
97 | { 400, 1000 }, | |
62bb8840 | 98 | { 0, 0}, |
a370ef19 BV |
99 | }; |
100 | ||
62bb8840 | 101 | static const struct sr_rational vdivs[] = { |
313deed2 BV |
102 | /* millivolts */ |
103 | { 10, 1000 }, | |
104 | { 20, 1000 }, | |
105 | { 50, 1000 }, | |
106 | { 100, 1000 }, | |
107 | { 200, 1000 }, | |
108 | { 500, 1000 }, | |
109 | /* volts */ | |
110 | { 1, 1 }, | |
111 | { 2, 1 }, | |
112 | { 5, 1 }, | |
62bb8840 | 113 | { 0, 0 }, |
313deed2 BV |
114 | }; |
115 | ||
62bb8840 | 116 | static const char *trigger_sources[] = { |
a370ef19 BV |
117 | "CH1", |
118 | "CH2", | |
119 | "EXT", | |
62bb8840 | 120 | NULL, |
a370ef19 | 121 | }; |
3b533202 | 122 | |
62bb8840 | 123 | static const char *filter_targets[] = { |
ebb781a6 BV |
124 | "CH1", |
125 | "CH2", | |
126 | /* TODO: "TRIGGER", */ | |
62bb8840 | 127 | NULL, |
ebb781a6 BV |
128 | }; |
129 | ||
62bb8840 | 130 | static const char *coupling[] = { |
b58fbd99 BV |
131 | "AC", |
132 | "DC", | |
133 | "GND", | |
62bb8840 | 134 | NULL, |
b58fbd99 BV |
135 | }; |
136 | ||
3b533202 BV |
137 | SR_PRIV libusb_context *usb_context = NULL; |
138 | SR_PRIV GSList *dev_insts = NULL; | |
139 | ||
62bb8840 | 140 | static struct sr_dev_inst *dso_dev_new(int index, const struct dso_profile *prof) |
3b533202 BV |
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 | ||
62bb8840 | 176 | static int configure_probes(struct context *ctx, const GSList *probes) |
3b533202 | 177 | { |
62bb8840 UH |
178 | const struct sr_probe *probe; |
179 | const GSList *l; | |
3b533202 | 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 | ||
193 | static int hw_init(const char *devinfo) | |
194 | { | |
195 | struct sr_dev_inst *sdi; | |
196 | struct libusb_device_descriptor des; | |
62bb8840 | 197 | const struct dso_profile *prof; |
3b533202 BV |
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 | ||
264 | static 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 | ||
311 | static 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 | ||
323 | static 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 | ||
62bb8840 | 358 | static const void *hw_dev_info_get(int dev_index, int dev_info_id) |
3b533202 BV |
359 | { |
360 | struct sr_dev_inst *sdi; | |
361 | struct context *ctx; | |
62bb8840 | 362 | const void *info; |
3b533202 BV |
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 | ||
62bb8840 | 407 | static int hw_dev_status_get(int dev_index) |
3b533202 BV |
408 | { |
409 | struct sr_dev_inst *sdi; | |
410 | ||
62bb8840 | 411 | if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) |
3b533202 BV |
412 | return SR_ST_NOT_FOUND; |
413 | ||
414 | return sdi->status; | |
415 | } | |
416 | ||
62bb8840 | 417 | static const int *hw_hwcap_get_all(void) |
3b533202 | 418 | { |
62bb8840 | 419 | return hwcaps; |
3b533202 BV |
420 | } |
421 | ||
af36b809 | 422 | static int hw_dev_config_set(int dev_index, int hwcap, const void *value) |
3b533202 BV |
423 | { |
424 | struct sr_dev_inst *sdi; | |
425 | struct context *ctx; | |
a370ef19 BV |
426 | struct sr_rational tmp_rat; |
427 | float tmp_float; | |
428 | uint64_t tmp_u64; | |
429 | int ret, i; | |
4a090d72 | 430 | char **targets; |
3b533202 BV |
431 | |
432 | if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) | |
433 | return SR_ERR; | |
434 | ||
435 | if (sdi->status != SR_ST_ACTIVE) | |
436 | return SR_ERR; | |
437 | ||
a370ef19 | 438 | ret = SR_OK; |
3b533202 BV |
439 | ctx = sdi->priv; |
440 | switch (hwcap) { | |
ae88b97b | 441 | case SR_HWCAP_LIMIT_FRAMES: |
62bb8840 | 442 | ctx->limit_frames = *(const uint64_t *)value; |
ae88b97b | 443 | break; |
3b533202 | 444 | case SR_HWCAP_PROBECONFIG: |
62bb8840 | 445 | ret = configure_probes(ctx, (const GSList *)value); |
3b533202 | 446 | break; |
a370ef19 | 447 | case SR_HWCAP_TRIGGER_SLOPE: |
62bb8840 | 448 | tmp_u64 = *(const int *)value; |
a370ef19 BV |
449 | if (tmp_u64 != SLOPE_NEGATIVE && tmp_u64 != SLOPE_POSITIVE) |
450 | ret = SR_ERR_ARG; | |
451 | ctx->triggerslope = tmp_u64; | |
452 | break; | |
453 | case SR_HWCAP_HORIZ_TRIGGERPOS: | |
62bb8840 | 454 | tmp_float = *(const float *)value; |
a370ef19 BV |
455 | if (tmp_float < 0.0 || tmp_float > 1.0) { |
456 | sr_err("hantek-dso: trigger position should be between 0.0 and 1.0"); | |
3b533202 | 457 | ret = SR_ERR_ARG; |
a370ef19 BV |
458 | } else |
459 | ctx->triggerposition = tmp_float; | |
460 | break; | |
461 | case SR_HWCAP_BUFFERSIZE: | |
62bb8840 | 462 | tmp_u64 = *(const int *)value; |
a370ef19 BV |
463 | for (i = 0; buffersizes[i]; i++) { |
464 | if (buffersizes[i] == tmp_u64) { | |
465 | ctx->framesize = tmp_u64; | |
466 | break; | |
467 | } | |
468 | } | |
469 | if (buffersizes[i] == 0) | |
470 | ret = SR_ERR_ARG; | |
471 | break; | |
472 | case SR_HWCAP_TIMEBASE: | |
62bb8840 | 473 | tmp_rat = *(const struct sr_rational *)value; |
a370ef19 BV |
474 | for (i = 0; timebases[i].p && timebases[i].q; i++) { |
475 | if (timebases[i].p == tmp_rat.p | |
476 | && timebases[i].q == tmp_rat.q) { | |
477 | ctx->timebase = i; | |
478 | break; | |
479 | } | |
480 | } | |
481 | if (timebases[i].p == 0 && timebases[i].q == 0) | |
482 | ret = SR_ERR_ARG; | |
483 | break; | |
484 | case SR_HWCAP_TRIGGER_SOURCE: | |
a370ef19 | 485 | for (i = 0; trigger_sources[i]; i++) { |
4a090d72 BV |
486 | if (!strcmp(value, trigger_sources[i])) { |
487 | ctx->triggersource = g_strdup(value); | |
a370ef19 BV |
488 | break; |
489 | } | |
490 | } | |
491 | if (trigger_sources[i] == 0) | |
492 | ret = SR_ERR_ARG; | |
493 | break; | |
ebb781a6 BV |
494 | case SR_HWCAP_FILTER: |
495 | ctx->filter_ch1 = ctx->filter_ch2 = ctx->filter_trigger = 0; | |
496 | targets = g_strsplit(value, ",", 0); | |
497 | for (i = 0; targets[i]; i++) { | |
498 | if (targets[i] == '\0') | |
499 | /* Empty filter string can be used to clear them all. */ | |
500 | ; | |
501 | else if (!strcmp(targets[i], "CH1")) | |
502 | ctx->filter_ch1 = TRUE; | |
503 | else if (!strcmp(targets[i], "CH2")) | |
504 | ctx->filter_ch2 = TRUE; | |
505 | else if (!strcmp(targets[i], "TRIGGER")) | |
506 | ctx->filter_trigger = TRUE; | |
507 | else { | |
508 | sr_err("invalid filter target %s", targets[i]); | |
509 | ret = SR_ERR_ARG; | |
510 | } | |
511 | } | |
512 | g_strfreev(targets); | |
513 | break; | |
313deed2 BV |
514 | case SR_HWCAP_VDIV: |
515 | /* TODO not supporting vdiv per channel yet */ | |
62bb8840 | 516 | tmp_rat = *(const struct sr_rational *)value; |
313deed2 BV |
517 | for (i = 0; vdivs[i].p && vdivs[i].q; i++) { |
518 | if (vdivs[i].p == tmp_rat.p | |
519 | && vdivs[i].q == tmp_rat.q) { | |
520 | ctx->voltage_ch1 = i; | |
521 | ctx->voltage_ch2 = i; | |
522 | break; | |
523 | } | |
524 | } | |
525 | if (vdivs[i].p == 0 && vdivs[i].q == 0) | |
526 | ret = SR_ERR_ARG; | |
527 | break; | |
b58fbd99 BV |
528 | case SR_HWCAP_COUPLING: |
529 | /* TODO not supporting coupling per channel yet */ | |
b58fbd99 | 530 | for (i = 0; coupling[i]; i++) { |
4a090d72 | 531 | if (!strcmp(value, coupling[i])) { |
b58fbd99 BV |
532 | ctx->coupling_ch1 = i; |
533 | ctx->coupling_ch2 = i; | |
534 | break; | |
535 | } | |
536 | } | |
537 | if (coupling[i] == 0) | |
538 | ret = SR_ERR_ARG; | |
539 | break; | |
3b533202 BV |
540 | default: |
541 | ret = SR_ERR_ARG; | |
542 | } | |
543 | ||
544 | return ret; | |
545 | } | |
546 | ||
e749a8cb BV |
547 | static void send_chunk(struct context *ctx, unsigned char *buf, |
548 | int num_samples) | |
3b533202 BV |
549 | { |
550 | struct sr_datafeed_packet packet; | |
551 | struct sr_datafeed_analog analog; | |
c5841b28 | 552 | float ch1, ch2, range; |
6e71ef3b | 553 | int num_probes, data_offset, i; |
3b533202 | 554 | |
6e71ef3b | 555 | num_probes = (ctx->ch1_enabled && ctx->ch2_enabled) ? 2 : 1; |
3b533202 BV |
556 | packet.type = SR_DF_ANALOG; |
557 | packet.payload = &analog; | |
6e71ef3b | 558 | /* TODO: support for 5xxx series 9-bit samples */ |
e749a8cb | 559 | analog.num_samples = num_samples; |
9956f285 UH |
560 | analog.mq = SR_MQ_VOLTAGE; |
561 | analog.unit = SR_UNIT_VOLT; | |
6e71ef3b BV |
562 | analog.data = g_try_malloc(analog.num_samples * sizeof(float) * num_probes); |
563 | data_offset = 0; | |
3b533202 | 564 | for (i = 0; i < analog.num_samples; i++) { |
6e71ef3b BV |
565 | /* The device always sends data for both channels. If a channel |
566 | * is disabled, it contains a copy of the enabled channel's | |
567 | * data. However, we only send the requested channels to the bus. | |
c5841b28 BV |
568 | * |
569 | * Voltage values are encoded as a value 0-255 (0-512 on the 5200*), | |
570 | * where the value is a point in the range represented by the vdiv | |
571 | * setting. There are 8 vertical divs, so e.g. 500mV/div represents | |
572 | * 4V peak-to-peak where 0 = -2V and 255 = +2V. | |
6e71ef3b | 573 | */ |
3b533202 | 574 | /* TODO: support for 5xxx series 9-bit samples */ |
6e71ef3b | 575 | if (ctx->ch1_enabled) { |
c5841b28 | 576 | range = ((float)vdivs[ctx->voltage_ch1].p / vdivs[ctx->voltage_ch1].q) * 8; |
e749a8cb | 577 | ch1 = range / 255 * *(buf + i * 2 + 1); |
c5841b28 BV |
578 | /* Value is centered around 0V. */ |
579 | ch1 -= range / 2; | |
6e71ef3b BV |
580 | analog.data[data_offset++] = ch1; |
581 | } | |
582 | if (ctx->ch2_enabled) { | |
c5841b28 | 583 | range = ((float)vdivs[ctx->voltage_ch2].p / vdivs[ctx->voltage_ch2].q) * 8; |
e749a8cb | 584 | ch2 = range / 255 * *(buf + i * 2); |
c5841b28 | 585 | ch2 -= range / 2; |
6e71ef3b BV |
586 | analog.data[data_offset++] = ch2; |
587 | } | |
3b533202 | 588 | } |
e749a8cb BV |
589 | sr_session_send(ctx->cb_data, &packet); |
590 | ||
591 | } | |
592 | ||
593 | /* Called by libusb (as triggered by handle_event()) when a transfer comes in. | |
594 | * Only channel data comes in asynchronously, and all transfers for this are | |
595 | * queued up beforehand, so this just needs so chuck the incoming data onto | |
596 | * the libsigrok session bus. | |
597 | */ | |
598 | static void receive_transfer(struct libusb_transfer *transfer) | |
599 | { | |
600 | struct sr_datafeed_packet packet; | |
601 | struct context *ctx; | |
602 | int num_samples, pre; | |
603 | ||
604 | ctx = transfer->user_data; | |
605 | sr_dbg("hantek-dso: receive_transfer(): status %d received %d bytes", | |
606 | transfer->status, transfer->actual_length); | |
607 | ||
608 | if (transfer->actual_length == 0) | |
609 | /* Nothing to send to the bus. */ | |
610 | return; | |
611 | ||
612 | num_samples = transfer->actual_length / 2; | |
613 | ||
614 | sr_dbg("hantek-dso: got %d-%d/%d samples in frame", ctx->samp_received + 1, | |
615 | ctx->samp_received + num_samples, ctx->framesize); | |
616 | ||
617 | /* The device always sends a full frame, but the beginning of the frame | |
618 | * doesn't represent the trigger point. The offset at which the trigger | |
619 | * happened came in with the capture state, so we need to start sending | |
620 | * from there up the session bus. The samples in the frame buffer before | |
621 | * that trigger point came after the end of the device's frame buffer was | |
622 | * reached, and it wrapped around to overwrite up until the trigger point. | |
623 | */ | |
624 | if (ctx->samp_received < ctx->trigger_offset) { | |
625 | /* Trigger point not yet reached. */ | |
626 | if (ctx->samp_received + num_samples < ctx->trigger_offset) { | |
627 | /* The entire chunk is before the trigger point. */ | |
628 | memcpy(ctx->framebuf + ctx->samp_buffered * 2, | |
629 | transfer->buffer, num_samples * 2); | |
630 | ctx->samp_buffered += num_samples; | |
631 | } else { | |
632 | /* This chunk hits or overruns the trigger point. | |
633 | * Store the part before the trigger fired, and | |
634 | * send the rest up to the session bus. */ | |
635 | pre = ctx->trigger_offset - ctx->samp_received; | |
636 | memcpy(ctx->framebuf + ctx->samp_buffered * 2, | |
637 | transfer->buffer, pre * 2); | |
638 | ctx->samp_buffered += pre; | |
639 | ||
640 | /* The rest of this chunk starts with the trigger point. */ | |
641 | sr_dbg("hantek-dso: reached trigger point, %d samples buffered", | |
642 | ctx->samp_buffered); | |
643 | ||
644 | /* Avoid the corner case where the chunk ended at | |
645 | * exactly the trigger point. */ | |
646 | if (num_samples > pre) | |
647 | send_chunk(ctx, transfer->buffer + pre * 2, | |
648 | num_samples - pre); | |
649 | } | |
650 | } else { | |
651 | /* Already past the trigger point, just send it all out. */ | |
652 | send_chunk(ctx, transfer->buffer, | |
653 | num_samples); | |
654 | } | |
655 | ||
656 | ctx->samp_received += num_samples; | |
657 | ||
658 | /* Everything in this transfer was either copied to the buffer or | |
659 | * sent to the session bus. */ | |
3b533202 BV |
660 | g_free(transfer->buffer); |
661 | libusb_free_transfer(transfer); | |
3b533202 | 662 | |
e749a8cb BV |
663 | if (ctx->samp_received >= ctx->framesize) { |
664 | /* That was the last chunk in this frame. Send the buffered | |
665 | * pre-trigger samples out now, in one big chunk. */ | |
666 | sr_dbg("hantek-dso: end of frame, sending %d pre-trigger buffered samples", | |
667 | ctx->samp_buffered); | |
668 | send_chunk(ctx, ctx->framebuf, ctx->samp_buffered); | |
669 | ||
670 | /* Mark the end of this frame. */ | |
ae88b97b BV |
671 | packet.type = SR_DF_FRAME_END; |
672 | sr_session_send(ctx->cb_data, &packet); | |
673 | ||
674 | if (ctx->limit_frames && ++ctx->num_frames == ctx->limit_frames) { | |
675 | /* Terminate session */ | |
6e71ef3b | 676 | /* TODO: don't leave pending USB transfers hanging */ |
ae88b97b BV |
677 | packet.type = SR_DF_END; |
678 | sr_session_send(ctx->cb_data, &packet); | |
679 | } else { | |
ae88b97b BV |
680 | ctx->dev_state = NEW_CAPTURE; |
681 | } | |
682 | } | |
683 | ||
3b533202 BV |
684 | } |
685 | ||
686 | static int handle_event(int fd, int revents, void *cb_data) | |
687 | { | |
ae88b97b | 688 | struct sr_datafeed_packet packet; |
3b533202 BV |
689 | struct timeval tv; |
690 | struct context *ctx; | |
e749a8cb | 691 | int num_probes; |
6e6eeff4 BV |
692 | uint32_t trigger_offset; |
693 | uint8_t capturestate; | |
3b533202 BV |
694 | |
695 | /* Avoid compiler warnings. */ | |
696 | (void)fd; | |
697 | (void)revents; | |
698 | ||
699 | /* Always handle pending libusb events. */ | |
700 | tv.tv_sec = tv.tv_usec = 0; | |
701 | libusb_handle_events_timeout(usb_context, &tv); | |
702 | ||
703 | ctx = cb_data; | |
704 | /* TODO: ugh */ | |
705 | if (ctx->dev_state == NEW_CAPTURE) { | |
706 | if (dso_capture_start(ctx) != SR_OK) | |
707 | return TRUE; | |
708 | if (dso_enable_trigger(ctx) != SR_OK) | |
709 | return TRUE; | |
a370ef19 BV |
710 | // if (dso_force_trigger(ctx) != SR_OK) |
711 | // return TRUE; | |
3b533202 BV |
712 | sr_dbg("hantek-dso: successfully requested next chunk"); |
713 | ctx->dev_state = CAPTURE; | |
714 | return TRUE; | |
715 | } | |
716 | if (ctx->dev_state != CAPTURE) | |
717 | return TRUE; | |
718 | ||
6e6eeff4 | 719 | if ((dso_get_capturestate(ctx, &capturestate, &trigger_offset)) != SR_OK) |
3b533202 | 720 | return TRUE; |
3b533202 BV |
721 | |
722 | sr_dbg("hantek-dso: capturestate %d", capturestate); | |
6e6eeff4 | 723 | sr_dbg("hantek-dso: trigger offset 0x%.6x", trigger_offset); |
3b533202 BV |
724 | switch (capturestate) { |
725 | case CAPTURE_EMPTY: | |
726 | if (++ctx->capture_empty_count >= MAX_CAPTURE_EMPTY) { | |
727 | ctx->capture_empty_count = 0; | |
728 | if (dso_capture_start(ctx) != SR_OK) | |
729 | break; | |
730 | if (dso_enable_trigger(ctx) != SR_OK) | |
731 | break; | |
a370ef19 BV |
732 | // if (dso_force_trigger(ctx) != SR_OK) |
733 | // break; | |
3b533202 BV |
734 | sr_dbg("hantek-dso: successfully requested next chunk"); |
735 | } | |
736 | break; | |
737 | case CAPTURE_FILLING: | |
738 | /* no data yet */ | |
739 | break; | |
740 | case CAPTURE_READY_8BIT: | |
e749a8cb BV |
741 | /* Remember where in the captured frame the trigger is. */ |
742 | ctx->trigger_offset = trigger_offset; | |
743 | ||
744 | num_probes = (ctx->ch1_enabled && ctx->ch2_enabled) ? 2 : 1; | |
745 | ctx->framebuf = g_try_malloc(ctx->framesize * num_probes * 2); | |
746 | ctx->samp_buffered = ctx->samp_received = 0; | |
747 | ||
3b533202 BV |
748 | /* Tell the scope to send us the first frame. */ |
749 | if (dso_get_channeldata(ctx, receive_transfer) != SR_OK) | |
750 | break; | |
ae88b97b BV |
751 | |
752 | /* Don't hit the state machine again until we're done fetching | |
753 | * the data we just told the scope to send. | |
754 | */ | |
3b533202 | 755 | ctx->dev_state = FETCH_DATA; |
ae88b97b BV |
756 | |
757 | /* Tell the frontend a new frame is on the way. */ | |
758 | packet.type = SR_DF_FRAME_BEGIN; | |
759 | sr_session_send(cb_data, &packet); | |
3b533202 BV |
760 | break; |
761 | case CAPTURE_READY_9BIT: | |
762 | /* TODO */ | |
763 | sr_err("not yet supported"); | |
764 | break; | |
765 | case CAPTURE_TIMEOUT: | |
766 | /* Doesn't matter, we'll try again next time. */ | |
767 | break; | |
768 | default: | |
769 | sr_dbg("unknown capture state"); | |
770 | } | |
771 | ||
772 | return TRUE; | |
773 | } | |
774 | ||
62bb8840 | 775 | static int hw_dev_acquisition_start(int dev_index, void *cb_data) |
3b533202 BV |
776 | { |
777 | const struct libusb_pollfd **lupfd; | |
778 | struct sr_datafeed_packet packet; | |
779 | struct sr_datafeed_header header; | |
780 | struct sr_datafeed_meta_analog meta; | |
781 | struct sr_dev_inst *sdi; | |
782 | struct context *ctx; | |
783 | int i; | |
784 | ||
62bb8840 | 785 | if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) |
3b533202 BV |
786 | return SR_ERR; |
787 | ||
788 | if (sdi->status != SR_ST_ACTIVE) | |
789 | return SR_ERR; | |
790 | ||
791 | ctx = sdi->priv; | |
792 | ctx->cb_data = cb_data; | |
793 | ||
794 | if (dso_init(ctx) != SR_OK) | |
795 | return SR_ERR; | |
796 | ||
797 | if (dso_capture_start(ctx) != SR_OK) | |
798 | return SR_ERR; | |
799 | ||
800 | ctx->dev_state = CAPTURE; | |
801 | lupfd = libusb_get_pollfds(usb_context); | |
802 | for (i = 0; lupfd[i]; i++) | |
803 | sr_source_add(lupfd[i]->fd, lupfd[i]->events, TICK, handle_event, | |
804 | ctx); | |
805 | free(lupfd); | |
806 | ||
807 | /* Send header packet to the session bus. */ | |
808 | packet.type = SR_DF_HEADER; | |
809 | packet.payload = (unsigned char *)&header; | |
810 | header.feed_version = 1; | |
811 | gettimeofday(&header.starttime, NULL); | |
812 | sr_session_send(cb_data, &packet); | |
813 | ||
814 | /* Send metadata about the SR_DF_ANALOG packets to come. */ | |
815 | packet.type = SR_DF_META_ANALOG; | |
816 | packet.payload = &meta; | |
817 | meta.num_probes = ctx->profile->num_probes; | |
818 | sr_session_send(cb_data, &packet); | |
819 | ||
820 | return SR_OK; | |
821 | } | |
822 | ||
823 | /* TODO: doesn't really cancel pending transfers so they might come in after | |
824 | * SR_DF_END is sent. | |
825 | */ | |
62bb8840 | 826 | static int hw_dev_acquisition_stop(int dev_index, void *cb_data) |
3b533202 BV |
827 | { |
828 | struct sr_datafeed_packet packet; | |
829 | struct sr_dev_inst *sdi; | |
830 | struct context *ctx; | |
831 | ||
62bb8840 | 832 | if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) |
3b533202 BV |
833 | return SR_ERR; |
834 | ||
835 | if (sdi->status != SR_ST_ACTIVE) | |
836 | return SR_ERR; | |
837 | ||
838 | ctx = sdi->priv; | |
839 | ctx->dev_state = IDLE; | |
840 | ||
841 | packet.type = SR_DF_END; | |
62bb8840 | 842 | sr_session_send(cb_data, &packet); |
3b533202 BV |
843 | |
844 | return SR_OK; | |
845 | } | |
846 | ||
62bb8840 | 847 | SR_PRIV struct sr_dev_driver hantek_dso_driver_info = { |
3b533202 BV |
848 | .name = "hantek-dso", |
849 | .longname = "Hantek DSO", | |
850 | .api_version = 1, | |
851 | .init = hw_init, | |
852 | .cleanup = hw_cleanup, | |
853 | .dev_open = hw_dev_open, | |
854 | .dev_close = hw_dev_close, | |
62bb8840 UH |
855 | .dev_info_get = hw_dev_info_get, |
856 | .dev_status_get = hw_dev_status_get, | |
857 | .hwcap_get_all = hw_hwcap_get_all, | |
3b533202 | 858 | .dev_config_set = hw_dev_config_set, |
62bb8840 UH |
859 | .dev_acquisition_start = hw_dev_acquisition_start, |
860 | .dev_acquisition_stop = hw_dev_acquisition_stop, | |
3b533202 | 861 | }; |