]>
Commit | Line | Data |
---|---|---|
f2cd2deb FS |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2020 Florian Schmidt <schmidt_florian@gmx.de> | |
5 | * Copyright (C) 2013 Marcus Comstedt <marcus@mc.pp.se> | |
6 | * Copyright (C) 2013 Bert Vermeulen <bert@biot.com> | |
7 | * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk> | |
8 | * | |
9 | * This program is free software: you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation, either version 3 of the License, or | |
12 | * (at your option) any later version. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU General Public License | |
20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
21 | */ | |
22 | ||
23 | /* mostly stolen from src/hardware/saleae-logic16/ */ | |
24 | ||
25 | #include <config.h> | |
26 | #include <glib.h> | |
27 | #include <libusb.h> | |
28 | #include <stdlib.h> | |
29 | #include <string.h> | |
30 | #include <math.h> | |
31 | #include <libsigrok/libsigrok.h> | |
32 | #include "libsigrok-internal.h" | |
33 | #include "protocol.h" | |
34 | ||
35 | static const uint32_t scanopts[] = { | |
36 | SR_CONF_CONN, | |
37 | }; | |
38 | ||
39 | static const uint32_t drvopts[] = { | |
40 | SR_CONF_LOGIC_ANALYZER, | |
41 | }; | |
42 | ||
43 | static const uint32_t devopts[] = { | |
44 | /* TODO: SR_CONF_CONTINUOUS, */ | |
45 | SR_CONF_CONN | SR_CONF_GET, | |
46 | SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
47 | SR_CONF_LIMIT_SAMPLES | SR_CONF_SET | SR_CONF_GET | SR_CONF_LIST, | |
48 | SR_CONF_VOLTAGE_THRESHOLD | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
49 | SR_CONF_LOGIC_THRESHOLD | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
50 | SR_CONF_LOGIC_THRESHOLD_CUSTOM | SR_CONF_GET | SR_CONF_SET, | |
51 | SR_CONF_TRIGGER_MATCH | SR_CONF_LIST, | |
52 | SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET, | |
53 | }; | |
54 | ||
55 | static const int32_t trigger_matches[] = { | |
56 | SR_TRIGGER_ZERO, | |
57 | SR_TRIGGER_ONE, | |
58 | SR_TRIGGER_RISING, | |
59 | SR_TRIGGER_FALLING, | |
60 | }; | |
61 | ||
62 | static const char *channel_names[] = { | |
63 | "0", "1", "2", "3", "4", "5", "6", "7", "8", | |
64 | "9", "10", "11", "12", "13", "14", "15", | |
65 | }; | |
66 | ||
67 | static const uint64_t samplerates[] = { | |
68 | SR_KHZ(20), | |
69 | SR_KHZ(50), | |
70 | SR_KHZ(100), | |
71 | SR_KHZ(200), | |
72 | SR_KHZ(500), | |
73 | SR_MHZ(1), | |
74 | SR_MHZ(2), | |
75 | SR_MHZ(4), | |
76 | SR_MHZ(5), | |
77 | SR_MHZ(8), | |
78 | SR_MHZ(10), | |
79 | SR_MHZ(20), | |
80 | SR_MHZ(50), | |
81 | SR_MHZ(100), | |
82 | SR_MHZ(200), | |
83 | }; | |
84 | ||
85 | static const float logic_threshold_value[] = { | |
86 | 1.58, | |
87 | 2.5, | |
88 | 1.165, | |
89 | 1.5, | |
90 | 1.25, | |
91 | 0.9, | |
92 | 0.75, | |
93 | 0.60, | |
94 | 0.45, | |
95 | }; | |
96 | ||
97 | static const char *logic_threshold[] = { | |
98 | "TTL 5V", | |
99 | "CMOS 5V", | |
100 | "CMOS 3.3V", | |
101 | "CMOS 3.0V", | |
102 | "CMOS 2.5V", | |
103 | "CMOS 1.8V", | |
104 | "CMOS 1.5V", | |
105 | "CMOS 1.2V", | |
106 | "CMOS 0.9V", | |
107 | "USER", | |
108 | }; | |
109 | ||
110 | #define MAX_NUM_LOGIC_THRESHOLD_ENTRIES ARRAY_SIZE(logic_threshold) | |
111 | ||
f2cd2deb FS |
112 | static GSList *scan(struct sr_dev_driver *di, GSList *options) |
113 | { | |
114 | struct drv_context *drvc; | |
115 | struct dev_context *devc; | |
116 | struct sr_dev_inst *sdi; | |
117 | struct sr_usb_dev_inst *usb; | |
118 | struct sr_config *src; | |
119 | GSList *l; | |
120 | GSList *devices; | |
121 | GSList *conn_devices; | |
122 | struct libusb_device_descriptor des; | |
123 | libusb_device **devlist; | |
124 | unsigned int i, j; | |
125 | const char *conn; | |
126 | char connection_id[64]; | |
127 | int64_t fw_updated; | |
128 | unsigned int dev_addr; | |
129 | ||
130 | drvc = di->context; | |
131 | ||
132 | conn = NULL; | |
133 | for (l = options; l; l = l->next) { | |
134 | src = l->data; | |
135 | switch (src->key) { | |
136 | case SR_CONF_CONN: | |
137 | conn = g_variant_get_string(src->data, NULL); | |
138 | break; | |
139 | } | |
140 | } | |
141 | if (conn) | |
142 | conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn); | |
143 | else | |
144 | conn_devices = NULL; | |
145 | ||
146 | /* Find all LA2016 devices and upload firmware to them. */ | |
147 | devices = NULL; | |
148 | libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); | |
149 | for (i = 0; devlist[i]; i++) { | |
150 | if (conn) { | |
151 | usb = NULL; | |
152 | for (l = conn_devices; l; l = l->next) { | |
153 | usb = l->data; | |
154 | if (usb->bus == libusb_get_bus_number(devlist[i]) | |
155 | && usb->address == libusb_get_device_address(devlist[i])) | |
156 | break; | |
157 | } | |
158 | if (!l) | |
159 | /* This device matched none of the ones that | |
160 | * matched the conn specification. */ | |
161 | continue; | |
162 | } | |
163 | ||
164 | libusb_get_device_descriptor(devlist[i], &des); | |
165 | ||
166 | if (usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)) < 0) | |
167 | continue; | |
168 | ||
169 | if (des.idVendor != LA2016_VID || des.idProduct != LA2016_PID) | |
170 | continue; | |
171 | ||
172 | /* Already has the firmware */ | |
173 | sr_dbg("Found a LA2016 device."); | |
174 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
175 | sdi->status = SR_ST_INITIALIZING; | |
176 | sdi->connection_id = g_strdup(connection_id); | |
177 | ||
178 | fw_updated = 0; | |
179 | dev_addr = libusb_get_device_address(devlist[i]); | |
180 | if (des.iProduct != 2) { | |
181 | sr_info("device at '%s' has no firmware loaded!", connection_id); | |
182 | ||
183 | if (la2016_upload_firmware(drvc->sr_ctx, devlist[i], des.idProduct) != SR_OK) { | |
184 | sr_err("uC firmware upload failed!"); | |
185 | g_free(sdi->connection_id); | |
186 | g_free(sdi); | |
187 | continue; | |
188 | } | |
189 | fw_updated = g_get_monotonic_time(); | |
190 | dev_addr = 0xff; /* to mark that we don't know address yet... ugly */ | |
191 | } | |
192 | ||
193 | sdi->vendor = g_strdup("Kingst"); | |
194 | sdi->model = g_strdup("LA2016"); | |
195 | ||
196 | for (j = 0; j < ARRAY_SIZE(channel_names); j++) | |
197 | sr_channel_new(sdi, j, SR_CHANNEL_LOGIC, TRUE, channel_names[j]); | |
198 | ||
199 | devices = g_slist_append(devices, sdi); | |
200 | ||
201 | devc = g_malloc0(sizeof(struct dev_context)); | |
202 | sdi->priv = devc; | |
203 | devc->fw_updated = fw_updated; | |
204 | devc->threshold_voltage_idx = 0; | |
205 | devc->threshold_voltage = logic_threshold_value[devc->threshold_voltage_idx]; | |
206 | ||
207 | sdi->status = SR_ST_INACTIVE; | |
208 | sdi->inst_type = SR_INST_USB; | |
209 | ||
210 | sdi->conn = sr_usb_dev_inst_new( | |
211 | libusb_get_bus_number(devlist[i]), | |
212 | dev_addr, NULL); | |
213 | } | |
214 | libusb_free_device_list(devlist, 1); | |
215 | g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free); | |
216 | ||
217 | return std_scan_complete(di, devices); | |
218 | } | |
219 | ||
220 | static int la2016_dev_open(struct sr_dev_inst *sdi) | |
221 | { | |
222 | struct sr_dev_driver *di; | |
223 | libusb_device **devlist; | |
224 | struct sr_usb_dev_inst *usb; | |
225 | struct libusb_device_descriptor des; | |
226 | struct drv_context *drvc; | |
227 | int ret, i, device_count; | |
228 | char connection_id[64]; | |
229 | ||
230 | di = sdi->driver; | |
231 | drvc = di->context; | |
232 | usb = sdi->conn; | |
233 | ret = SR_ERR; | |
234 | ||
235 | device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); | |
236 | if (device_count < 0) { | |
237 | sr_err("Failed to get device list: %s.", libusb_error_name(device_count)); | |
238 | return SR_ERR; | |
239 | } | |
240 | ||
241 | for (i = 0; i < device_count; i++) { | |
242 | libusb_get_device_descriptor(devlist[i], &des); | |
243 | ||
244 | if (des.idVendor != LA2016_VID || des.idProduct != LA2016_PID || des.iProduct != 2) | |
245 | continue; | |
246 | ||
247 | if ((sdi->status == SR_ST_INITIALIZING) || (sdi->status == SR_ST_INACTIVE)) { | |
248 | /* | |
249 | * Check device by its physical USB bus/port address. | |
250 | */ | |
251 | if (usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)) < 0) | |
252 | continue; | |
253 | ||
254 | if (strcmp(sdi->connection_id, connection_id)) | |
255 | /* This is not the one. */ | |
256 | continue; | |
257 | } | |
258 | ||
259 | if (!(ret = libusb_open(devlist[i], &usb->devhdl))) { | |
260 | if (usb->address == 0xff) | |
261 | /* | |
262 | * First time we touch this device after FW | |
263 | * upload, so we don't know the address yet. | |
264 | */ | |
265 | usb->address = libusb_get_device_address(devlist[i]); | |
266 | } else { | |
267 | sr_err("Failed to open device: %s.", libusb_error_name(ret)); | |
268 | ret = SR_ERR; | |
269 | break; | |
270 | } | |
271 | ||
272 | ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE); | |
273 | if (ret == LIBUSB_ERROR_BUSY) { | |
274 | sr_err("Unable to claim USB interface. Another " | |
275 | "program or driver has already claimed it."); | |
276 | ret = SR_ERR; | |
277 | break; | |
278 | } else if (ret == LIBUSB_ERROR_NO_DEVICE) { | |
279 | sr_err("Device has been disconnected."); | |
280 | ret = SR_ERR; | |
281 | break; | |
282 | } else if (ret != 0) { | |
283 | sr_err("Unable to claim interface: %s.", libusb_error_name(ret)); | |
284 | ret = SR_ERR; | |
285 | break; | |
286 | } | |
287 | ||
288 | if ((ret = la2016_init_device(sdi)) != SR_OK) { | |
289 | sr_err("Failed to init device."); | |
290 | break; | |
291 | } | |
292 | ||
293 | sr_info("Opened device on %d.%d (logical) / %s (physical), interface %d.", | |
294 | usb->bus, usb->address, sdi->connection_id, USB_INTERFACE); | |
295 | ||
296 | ret = SR_OK; | |
297 | ||
298 | break; | |
299 | } | |
300 | ||
301 | libusb_free_device_list(devlist, 1); | |
302 | ||
303 | if (ret != SR_OK) { | |
304 | if (usb->devhdl) { | |
305 | libusb_release_interface(usb->devhdl, USB_INTERFACE); | |
306 | libusb_close(usb->devhdl); | |
307 | usb->devhdl = NULL; | |
308 | } | |
309 | return SR_ERR; | |
310 | } | |
311 | ||
312 | return SR_OK; | |
313 | } | |
314 | ||
315 | static int dev_open(struct sr_dev_inst *sdi) | |
316 | { | |
317 | struct dev_context *devc; | |
318 | int64_t timediff_us, timediff_ms; | |
319 | uint64_t reset_done; | |
320 | uint64_t now; | |
321 | int ret; | |
322 | ||
323 | devc = sdi->priv; | |
324 | ||
325 | /* | |
326 | * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS | |
327 | * milliseconds for the FX2 to renumerate. | |
328 | */ | |
329 | ret = SR_ERR; | |
330 | if (devc->fw_updated > 0) { | |
331 | sr_info("Waiting for device to reset after firmware upload."); | |
332 | /* Takes >= 2000ms for the uC to be gone from the USB bus. */ | |
333 | reset_done = devc->fw_updated + 18 * (uint64_t)1e5; /* 1.8 seconds */ | |
334 | now = g_get_monotonic_time(); | |
335 | if (reset_done > now) | |
336 | g_usleep(reset_done - now); | |
337 | timediff_ms = 0; | |
338 | while (timediff_ms < MAX_RENUM_DELAY_MS) { | |
339 | g_usleep(200 * 1000); | |
340 | ||
341 | timediff_us = g_get_monotonic_time() - devc->fw_updated; | |
342 | timediff_ms = timediff_us / 1000; | |
343 | ||
344 | if ((ret = la2016_dev_open(sdi)) == SR_OK) | |
345 | break; | |
346 | sr_spew("Waited %" PRIi64 "ms.", timediff_ms); | |
347 | } | |
348 | if (ret != SR_OK) { | |
349 | sr_err("Device failed to re-enumerate."); | |
350 | return SR_ERR; | |
351 | } | |
352 | sr_info("Device came back after %" PRIi64 "ms.", timediff_ms); | |
353 | } else | |
354 | ret = la2016_dev_open(sdi); | |
355 | ||
356 | if (ret != SR_OK) { | |
357 | sr_err("Unable to open device."); | |
358 | return SR_ERR; | |
359 | } | |
360 | ||
361 | return SR_OK; | |
362 | } | |
363 | ||
364 | static int dev_close(struct sr_dev_inst *sdi) | |
365 | { | |
366 | struct sr_usb_dev_inst *usb; | |
367 | ||
368 | usb = sdi->conn; | |
369 | ||
370 | if (!usb->devhdl) | |
371 | return SR_ERR_BUG; | |
372 | ||
373 | la2016_deinit_device(sdi); | |
374 | ||
375 | sr_info("Closing device on %d.%d (logical) / %s (physical) interface %d.", | |
376 | usb->bus, usb->address, sdi->connection_id, USB_INTERFACE); | |
377 | libusb_release_interface(usb->devhdl, USB_INTERFACE); | |
378 | libusb_close(usb->devhdl); | |
379 | usb->devhdl = NULL; | |
380 | ||
381 | return SR_OK; | |
382 | } | |
383 | ||
384 | static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
385 | { | |
386 | struct dev_context *devc; | |
387 | struct sr_usb_dev_inst *usb; | |
388 | double rounded; | |
389 | ||
390 | (void)cg; | |
391 | ||
392 | if (!sdi) | |
393 | return SR_ERR_ARG; | |
394 | devc = sdi->priv; | |
395 | ||
396 | switch (key) { | |
397 | case SR_CONF_CONN: | |
398 | if (!sdi->conn) | |
399 | return SR_ERR_ARG; | |
400 | usb = sdi->conn; | |
401 | if (usb->address == 255) | |
402 | /* Device still needs to re-enumerate after firmware | |
403 | * upload, so we don't know its (future) address. */ | |
404 | return SR_ERR; | |
405 | *data = g_variant_new_printf("%d.%d", usb->bus, usb->address); | |
406 | break; | |
407 | case SR_CONF_SAMPLERATE: | |
408 | *data = g_variant_new_uint64(devc->cur_samplerate); | |
409 | break; | |
410 | case SR_CONF_LIMIT_SAMPLES: | |
411 | *data = g_variant_new_uint64(devc->limit_samples); | |
412 | break; | |
413 | case SR_CONF_CAPTURE_RATIO: | |
414 | *data = g_variant_new_uint64(devc->capture_ratio); | |
415 | break; | |
416 | case SR_CONF_VOLTAGE_THRESHOLD: | |
417 | rounded = (int)(devc->threshold_voltage / 0.1) * 0.1; | |
418 | *data = std_gvar_tuple_double(rounded, rounded + 0.1); | |
419 | return SR_OK; | |
420 | case SR_CONF_LOGIC_THRESHOLD: | |
421 | *data = g_variant_new_string(logic_threshold[devc->threshold_voltage_idx]); | |
422 | break; | |
423 | case SR_CONF_LOGIC_THRESHOLD_CUSTOM: | |
424 | *data = g_variant_new_double(devc->threshold_voltage); | |
425 | break; | |
426 | ||
427 | default: | |
428 | return SR_ERR_NA; | |
429 | } | |
430 | ||
431 | return SR_OK; | |
432 | } | |
433 | ||
434 | static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
435 | { | |
436 | struct dev_context *devc; | |
437 | double low, high; | |
438 | int idx; | |
439 | ||
440 | (void)cg; | |
441 | ||
442 | devc = sdi->priv; | |
443 | ||
444 | switch (key) { | |
445 | case SR_CONF_SAMPLERATE: | |
446 | devc->cur_samplerate = g_variant_get_uint64(data); | |
447 | break; | |
448 | case SR_CONF_LIMIT_SAMPLES: | |
449 | devc->limit_samples = g_variant_get_uint64(data); | |
450 | break; | |
451 | case SR_CONF_CAPTURE_RATIO: | |
452 | devc->capture_ratio = g_variant_get_uint64(data); | |
453 | break; | |
454 | case SR_CONF_VOLTAGE_THRESHOLD: | |
455 | g_variant_get(data, "(dd)", &low, &high); | |
456 | devc->threshold_voltage = (low + high) / 2.0; | |
457 | devc->threshold_voltage_idx = MAX_NUM_LOGIC_THRESHOLD_ENTRIES - 1; /* USER */ | |
458 | break; | |
459 | case SR_CONF_LOGIC_THRESHOLD: { | |
460 | if ((idx = std_str_idx(data, logic_threshold, MAX_NUM_LOGIC_THRESHOLD_ENTRIES)) < 0) | |
461 | return SR_ERR_ARG; | |
462 | if (idx == MAX_NUM_LOGIC_THRESHOLD_ENTRIES - 1) { | |
463 | /* user threshold */ | |
464 | } else { | |
465 | devc->threshold_voltage = logic_threshold_value[idx]; | |
466 | } | |
467 | devc->threshold_voltage_idx = idx; | |
468 | break; | |
469 | } | |
470 | case SR_CONF_LOGIC_THRESHOLD_CUSTOM: | |
471 | devc->threshold_voltage = g_variant_get_double(data); | |
472 | break; | |
473 | default: | |
474 | return SR_ERR_NA; | |
475 | } | |
476 | ||
477 | return SR_OK; | |
478 | } | |
479 | ||
480 | static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
481 | { | |
482 | switch (key) { | |
483 | case SR_CONF_SCAN_OPTIONS: | |
484 | case SR_CONF_DEVICE_OPTIONS: | |
485 | return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts); | |
486 | case SR_CONF_SAMPLERATE: | |
487 | *data = std_gvar_samplerates(ARRAY_AND_SIZE(samplerates)); | |
488 | break; | |
489 | case SR_CONF_LIMIT_SAMPLES: | |
490 | *data = std_gvar_tuple_u64(LA2016_NUM_SAMPLES_MIN, LA2016_NUM_SAMPLES_MAX); | |
491 | break; | |
492 | case SR_CONF_VOLTAGE_THRESHOLD: | |
493 | *data = std_gvar_min_max_step_thresholds( | |
494 | LA2016_THR_VOLTAGE_MIN, | |
495 | LA2016_THR_VOLTAGE_MAX, 0.1); | |
496 | break; | |
497 | case SR_CONF_TRIGGER_MATCH: | |
498 | *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches)); | |
499 | break; | |
500 | case SR_CONF_LOGIC_THRESHOLD: | |
501 | *data = g_variant_new_strv(logic_threshold, MAX_NUM_LOGIC_THRESHOLD_ENTRIES); | |
502 | break; | |
503 | default: | |
504 | return SR_ERR_NA; | |
505 | } | |
506 | ||
507 | return SR_OK; | |
508 | } | |
509 | ||
510 | static void send_chunk(struct sr_dev_inst *sdi, transfer_packet_t *packets, unsigned int num_tfers) | |
511 | { | |
512 | struct dev_context *devc; | |
513 | struct sr_datafeed_logic logic; | |
514 | struct sr_datafeed_packet sr_packet; | |
515 | transfer_packet_t *packet; | |
516 | acq_packet_t *p; | |
517 | unsigned int max_samples, n_samples, total_samples, free_n_samples, ptotal; | |
518 | unsigned int i, j, k; | |
519 | int do_signal_trigger; | |
520 | uint16_t *wp; | |
521 | ||
522 | devc = sdi->priv; | |
523 | ||
524 | logic.unitsize = 2; | |
525 | logic.data = devc->convbuffer; | |
526 | ||
527 | sr_packet.type = SR_DF_LOGIC; | |
528 | sr_packet.payload = &logic; | |
529 | ||
530 | max_samples = devc->convbuffer_size / 2; | |
531 | n_samples = 0; | |
532 | wp = (uint16_t*)devc->convbuffer; | |
533 | total_samples = 0; | |
534 | do_signal_trigger = 0; | |
535 | ||
536 | if (devc->had_triggers_configured && devc->reading_behind_trigger == 0 && devc->info.n_rep_packets_before_trigger == 0) { | |
537 | sr_packet.type = SR_DF_TRIGGER; | |
538 | sr_packet.payload = NULL; | |
539 | sr_session_send(sdi, &sr_packet); | |
540 | devc->reading_behind_trigger = 1; | |
541 | ||
542 | do_signal_trigger = 0; | |
543 | sr_packet.type = SR_DF_LOGIC; | |
544 | sr_packet.payload = &logic; | |
545 | } | |
546 | ||
547 | for (i = 0; i < num_tfers; i++) { | |
548 | transfer_packet_host(packets[i]); | |
549 | packet = packets + i; | |
550 | ptotal = 0; | |
b2b6dd55 | 551 | for (k = 0; k < ARRAY_SIZE(packet->packet); k++) { |
f2cd2deb FS |
552 | free_n_samples = max_samples - n_samples; |
553 | if (free_n_samples < 256 || do_signal_trigger) { | |
554 | logic.length = n_samples * 2; | |
555 | sr_session_send(sdi, &sr_packet); | |
556 | n_samples = 0; | |
557 | wp = (uint16_t*)devc->convbuffer; | |
558 | if (do_signal_trigger) { | |
559 | sr_packet.type = SR_DF_TRIGGER; | |
560 | sr_packet.payload = NULL; | |
561 | sr_session_send(sdi, &sr_packet); | |
562 | ||
563 | do_signal_trigger = 0; | |
564 | sr_packet.type = SR_DF_LOGIC; | |
565 | sr_packet.payload = &logic; | |
566 | } | |
567 | } | |
568 | p = packet->packet + k; | |
569 | for (j = 0; j < p->repetitions; j++) | |
570 | *(wp++) = p->state; | |
571 | n_samples += p->repetitions; | |
572 | total_samples += p->repetitions; | |
573 | ptotal += p->repetitions; | |
574 | devc->total_samples += p->repetitions; | |
575 | if (!devc->reading_behind_trigger) { | |
576 | devc->n_reps_until_trigger --; | |
577 | if (devc->n_reps_until_trigger == 0) { | |
578 | devc->reading_behind_trigger = 1; | |
579 | do_signal_trigger = 1; | |
580 | sr_dbg(" here is trigger position after %" PRIu64 " samples, %.6fms", | |
581 | devc->total_samples, | |
582 | (double)devc->total_samples / devc->cur_samplerate * 1e3); | |
583 | } | |
584 | } | |
585 | } | |
586 | } | |
587 | if (n_samples) { | |
588 | logic.length = n_samples * 2; | |
589 | sr_session_send(sdi, &sr_packet); | |
590 | if (do_signal_trigger) { | |
591 | sr_packet.type = SR_DF_TRIGGER; | |
592 | sr_packet.payload = NULL; | |
593 | sr_session_send(sdi, &sr_packet); | |
594 | } | |
595 | } | |
596 | sr_dbg("send_chunk done after %d samples", total_samples); | |
597 | } | |
598 | ||
599 | static void LIBUSB_CALL receive_transfer(struct libusb_transfer *transfer) | |
600 | { | |
601 | struct sr_dev_inst *sdi; | |
602 | struct dev_context *devc; | |
603 | struct sr_usb_dev_inst *usb; | |
604 | int ret; | |
605 | ||
606 | sdi = transfer->user_data; | |
607 | devc = sdi->priv; | |
608 | usb = sdi->conn; | |
609 | ||
610 | sr_dbg("receive_transfer(): status %s received %d bytes.", | |
611 | libusb_error_name(transfer->status), transfer->actual_length); | |
612 | ||
613 | if (transfer->status == LIBUSB_TRANSFER_TIMED_OUT) { | |
614 | sr_err("bulk transfer timeout!"); | |
615 | devc->transfer_finished = 1; | |
616 | } | |
617 | send_chunk(sdi, (transfer_packet_t*)transfer->buffer, transfer->actual_length / sizeof(transfer_packet_t)); | |
618 | ||
619 | devc->n_bytes_to_read -= transfer->actual_length; | |
620 | if (devc->n_bytes_to_read) { | |
621 | uint32_t to_read = devc->n_bytes_to_read; | |
622 | if (to_read > LA2016_BULK_MAX) | |
623 | to_read = LA2016_BULK_MAX; | |
624 | libusb_fill_bulk_transfer( | |
625 | transfer, usb->devhdl, | |
626 | 0x86, transfer->buffer, to_read, | |
627 | receive_transfer, (void*)sdi, DEFAULT_TIMEOUT_MS); | |
628 | ||
629 | if ((ret = libusb_submit_transfer(transfer)) == 0) | |
630 | return; | |
631 | sr_err("Failed to submit further transfer: %s.", libusb_error_name(ret)); | |
632 | } | |
633 | ||
634 | g_free(transfer->buffer); | |
635 | libusb_free_transfer(transfer); | |
636 | devc->transfer_finished = 1; | |
637 | } | |
638 | ||
639 | static int handle_event(int fd, int revents, void *cb_data) | |
640 | { | |
641 | const struct sr_dev_inst *sdi; | |
642 | struct dev_context *devc; | |
643 | struct drv_context *drvc; | |
644 | struct sr_datafeed_packet packet; | |
645 | struct timeval tv; | |
646 | ||
647 | (void)fd; | |
648 | (void)revents; | |
649 | ||
650 | sdi = cb_data; | |
651 | devc = sdi->priv; | |
652 | drvc = sdi->driver->context; | |
653 | ||
654 | if (devc->have_trigger == 0) { | |
655 | if (la2016_has_triggered(sdi) == 0) { | |
656 | sr_dbg("not yet ready for download..."); | |
657 | return TRUE; | |
658 | } | |
659 | devc->have_trigger = 1; | |
660 | devc->transfer_finished = 0; | |
661 | devc->reading_behind_trigger = 0; | |
662 | devc->total_samples = 0; | |
663 | /* we can start retrieving data! */ | |
664 | if (la2016_start_retrieval(sdi, receive_transfer) != SR_OK) { | |
665 | sr_err("failed to start retrieval!"); | |
666 | return FALSE; | |
667 | } | |
668 | sr_dbg("retrieval is started..."); | |
669 | packet.type = SR_DF_FRAME_BEGIN; | |
670 | sr_session_send(sdi, &packet); | |
671 | ||
672 | return TRUE; | |
673 | } | |
674 | ||
675 | tv.tv_sec = tv.tv_usec = 0; | |
676 | libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv); | |
677 | ||
678 | if (devc->transfer_finished) { | |
679 | sr_dbg("transfer is finished!"); | |
680 | packet.type = SR_DF_FRAME_END; | |
681 | sr_session_send(sdi, &packet); | |
682 | ||
683 | usb_source_remove(sdi->session, drvc->sr_ctx); | |
684 | std_session_send_df_end(sdi); | |
685 | ||
686 | la2016_stop_acquisition(sdi); | |
687 | ||
688 | g_free(devc->convbuffer); | |
689 | devc->convbuffer = NULL; | |
690 | ||
691 | sr_dbg("transfer is now finished"); | |
692 | } | |
693 | ||
694 | return TRUE; | |
695 | } | |
696 | ||
697 | static void abort_acquisition(struct dev_context *devc) | |
698 | { | |
699 | if (devc->transfer) | |
700 | libusb_cancel_transfer(devc->transfer); | |
701 | } | |
702 | ||
703 | static int configure_channels(const struct sr_dev_inst *sdi) | |
704 | { | |
705 | struct dev_context *devc; | |
706 | ||
707 | devc = sdi->priv; | |
708 | devc->cur_channels = 0; | |
709 | devc->num_channels = 0; | |
710 | ||
711 | for (GSList *l = sdi->channels; l; l = l->next) { | |
712 | struct sr_channel *ch = (struct sr_channel*)l->data; | |
713 | if (ch->enabled == FALSE) | |
714 | continue; | |
715 | devc->cur_channels |= 1 << (ch->index); | |
716 | devc->num_channels++; | |
717 | } | |
718 | ||
719 | return SR_OK; | |
720 | } | |
721 | ||
722 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) | |
723 | { | |
724 | struct sr_dev_driver *di; | |
725 | struct drv_context *drvc; | |
726 | struct dev_context *devc; | |
727 | int ret; | |
728 | ||
729 | di = sdi->driver; | |
730 | drvc = di->context; | |
731 | devc = sdi->priv; | |
732 | ||
733 | if (configure_channels(sdi) != SR_OK) { | |
734 | sr_err("Failed to configure channels."); | |
735 | return SR_ERR; | |
736 | } | |
737 | ||
738 | devc->convbuffer_size = 4 * 1024 * 1024; | |
739 | if (!(devc->convbuffer = g_try_malloc(devc->convbuffer_size))) { | |
740 | sr_err("Conversion buffer malloc failed."); | |
741 | return SR_ERR_MALLOC; | |
742 | } | |
743 | ||
744 | if ((ret = la2016_setup_acquisition(sdi)) != SR_OK) { | |
745 | g_free(devc->convbuffer); | |
746 | devc->convbuffer = NULL; | |
747 | return ret; | |
748 | } | |
749 | ||
750 | devc->ctx = drvc->sr_ctx; | |
751 | ||
752 | if ((ret = la2016_start_acquisition(sdi)) != SR_OK) { | |
753 | abort_acquisition(devc); | |
754 | return ret; | |
755 | } | |
756 | ||
757 | devc->have_trigger = 0; | |
758 | usb_source_add(sdi->session, drvc->sr_ctx, 50, handle_event, (void*)sdi); | |
759 | ||
760 | std_session_send_df_header(sdi); | |
761 | ||
762 | return SR_OK; | |
763 | } | |
764 | ||
765 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) | |
766 | { | |
767 | int ret; | |
768 | ||
769 | ret = la2016_abort_acquisition(sdi); | |
770 | abort_acquisition(sdi->priv); | |
771 | ||
772 | return ret; | |
773 | } | |
774 | ||
775 | static struct sr_dev_driver kingst_la2016_driver_info = { | |
776 | .name = "kingst-la2016", | |
777 | .longname = "Kingst LA2016", | |
778 | .api_version = 1, | |
779 | .init = std_init, | |
780 | .cleanup = std_cleanup, | |
781 | .scan = scan, | |
782 | .dev_list = std_dev_list, | |
783 | .dev_clear = std_dev_clear, | |
784 | .config_get = config_get, | |
785 | .config_set = config_set, | |
786 | .config_list = config_list, | |
787 | .dev_open = dev_open, | |
788 | .dev_close = dev_close, | |
789 | .dev_acquisition_start = dev_acquisition_start, | |
790 | .dev_acquisition_stop = dev_acquisition_stop, | |
791 | .context = NULL, | |
792 | }; | |
793 | SR_REGISTER_DEV_DRIVER(kingst_la2016_driver_info); |