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