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