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