]>
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 | ||
96dc954e GS |
23 | /* |
24 | * This driver implementation initially was derived from the | |
25 | * src/hardware/saleae-logic16/ source code. | |
26 | */ | |
f2cd2deb FS |
27 | |
28 | #include <config.h> | |
a7740b06 | 29 | |
a7740b06 | 30 | #include <libsigrok/libsigrok.h> |
f2cd2deb | 31 | #include <string.h> |
a7740b06 | 32 | |
f2cd2deb FS |
33 | #include "libsigrok-internal.h" |
34 | #include "protocol.h" | |
35 | ||
36 | static const uint32_t scanopts[] = { | |
37 | SR_CONF_CONN, | |
38 | }; | |
39 | ||
40 | static const uint32_t drvopts[] = { | |
41 | SR_CONF_LOGIC_ANALYZER, | |
42 | }; | |
43 | ||
44 | static const uint32_t devopts[] = { | |
45 | /* TODO: SR_CONF_CONTINUOUS, */ | |
46 | SR_CONF_CONN | SR_CONF_GET, | |
47 | SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
48 | SR_CONF_LIMIT_SAMPLES | SR_CONF_SET | SR_CONF_GET | SR_CONF_LIST, | |
49 | SR_CONF_VOLTAGE_THRESHOLD | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
50 | SR_CONF_LOGIC_THRESHOLD | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
51 | SR_CONF_LOGIC_THRESHOLD_CUSTOM | SR_CONF_GET | SR_CONF_SET, | |
52 | SR_CONF_TRIGGER_MATCH | SR_CONF_LIST, | |
53 | SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET, | |
54 | }; | |
55 | ||
56 | static const int32_t trigger_matches[] = { | |
57 | SR_TRIGGER_ZERO, | |
58 | SR_TRIGGER_ONE, | |
59 | SR_TRIGGER_RISING, | |
60 | SR_TRIGGER_FALLING, | |
61 | }; | |
62 | ||
63 | static const char *channel_names[] = { | |
da25c287 GS |
64 | "CH0", "CH1", "CH2", "CH3", "CH4", "CH5", "CH6", "CH7", |
65 | "CH8", "CH9", "CH10", "CH11", "CH12", "CH13", "CH14", "CH15", | |
f2cd2deb FS |
66 | }; |
67 | ||
8b172e78 | 68 | static const uint64_t samplerates_la2016[] = { |
f2cd2deb FS |
69 | SR_KHZ(20), |
70 | SR_KHZ(50), | |
71 | SR_KHZ(100), | |
72 | SR_KHZ(200), | |
73 | SR_KHZ(500), | |
74 | SR_MHZ(1), | |
75 | SR_MHZ(2), | |
76 | SR_MHZ(4), | |
77 | SR_MHZ(5), | |
78 | SR_MHZ(8), | |
79 | SR_MHZ(10), | |
80 | SR_MHZ(20), | |
81 | SR_MHZ(50), | |
82 | SR_MHZ(100), | |
83 | SR_MHZ(200), | |
84 | }; | |
85 | ||
8b172e78 KG |
86 | static const uint64_t samplerates_la1016[] = { |
87 | SR_KHZ(20), | |
88 | SR_KHZ(50), | |
89 | SR_KHZ(100), | |
90 | SR_KHZ(200), | |
91 | SR_KHZ(500), | |
92 | SR_MHZ(1), | |
93 | SR_MHZ(2), | |
94 | SR_MHZ(4), | |
95 | SR_MHZ(5), | |
96 | SR_MHZ(8), | |
97 | SR_MHZ(10), | |
98 | SR_MHZ(20), | |
99 | SR_MHZ(50), | |
100 | SR_MHZ(100), | |
101 | }; | |
102 | ||
f2cd2deb FS |
103 | static const float logic_threshold_value[] = { |
104 | 1.58, | |
105 | 2.5, | |
106 | 1.165, | |
107 | 1.5, | |
108 | 1.25, | |
109 | 0.9, | |
110 | 0.75, | |
111 | 0.60, | |
112 | 0.45, | |
113 | }; | |
114 | ||
115 | static const char *logic_threshold[] = { | |
116 | "TTL 5V", | |
117 | "CMOS 5V", | |
118 | "CMOS 3.3V", | |
119 | "CMOS 3.0V", | |
120 | "CMOS 2.5V", | |
121 | "CMOS 1.8V", | |
122 | "CMOS 1.5V", | |
123 | "CMOS 1.2V", | |
124 | "CMOS 0.9V", | |
125 | "USER", | |
126 | }; | |
127 | ||
da2cb50d | 128 | #define LOGIC_THRESHOLD_IDX_USER (ARRAY_SIZE(logic_threshold) - 1) |
f2cd2deb | 129 | |
f2cd2deb FS |
130 | static GSList *scan(struct sr_dev_driver *di, GSList *options) |
131 | { | |
132 | struct drv_context *drvc; | |
520a20e9 | 133 | struct sr_context *ctx; |
f2cd2deb FS |
134 | struct dev_context *devc; |
135 | struct sr_dev_inst *sdi; | |
136 | struct sr_usb_dev_inst *usb; | |
137 | struct sr_config *src; | |
138 | GSList *l; | |
139 | GSList *devices; | |
140 | GSList *conn_devices; | |
141 | struct libusb_device_descriptor des; | |
520a20e9 GS |
142 | libusb_device **devlist, *dev; |
143 | size_t dev_count, dev_idx, ch_idx; | |
144 | uint8_t bus, addr; | |
f2cd2deb | 145 | const char *conn; |
520a20e9 | 146 | char conn_id[64]; |
22cb067a | 147 | uint64_t fw_uploaded; |
520a20e9 | 148 | int ret; |
f2cd2deb FS |
149 | |
150 | drvc = di->context; | |
520a20e9 | 151 | ctx = drvc->sr_ctx;; |
f2cd2deb FS |
152 | |
153 | conn = NULL; | |
154 | for (l = options; l; l = l->next) { | |
155 | src = l->data; | |
156 | switch (src->key) { | |
157 | case SR_CONF_CONN: | |
158 | conn = g_variant_get_string(src->data, NULL); | |
159 | break; | |
160 | } | |
161 | } | |
162 | if (conn) | |
520a20e9 | 163 | conn_devices = sr_usb_find(ctx->libusb_ctx, conn); |
f2cd2deb FS |
164 | else |
165 | conn_devices = NULL; | |
166 | ||
96dc954e | 167 | /* Find all LA2016 devices, optionally upload firmware to them. */ |
f2cd2deb | 168 | devices = NULL; |
520a20e9 GS |
169 | ret = libusb_get_device_list(ctx->libusb_ctx, &devlist); |
170 | if (ret < 0) { | |
171 | sr_err("Cannot get device list: %s.", libusb_error_name(ret)); | |
172 | return devices; | |
173 | } | |
174 | dev_count = ret; | |
175 | for (dev_idx = 0; dev_idx < dev_count; dev_idx++) { | |
176 | dev = devlist[dev_idx]; | |
177 | bus = libusb_get_bus_number(dev); | |
178 | addr = libusb_get_device_address(dev); | |
f2cd2deb FS |
179 | if (conn) { |
180 | usb = NULL; | |
181 | for (l = conn_devices; l; l = l->next) { | |
182 | usb = l->data; | |
520a20e9 | 183 | if (usb->bus == bus && usb->address == addr) |
f2cd2deb FS |
184 | break; |
185 | } | |
955ab604 | 186 | if (!l) { |
96dc954e GS |
187 | /* |
188 | * A connection parameter was specified and | |
189 | * this device does not match the filter. | |
190 | */ | |
f2cd2deb | 191 | continue; |
955ab604 | 192 | } |
f2cd2deb FS |
193 | } |
194 | ||
520a20e9 GS |
195 | libusb_get_device_descriptor(dev, &des); |
196 | ret = usb_get_port_path(dev, conn_id, sizeof(conn_id)); | |
197 | if (ret < 0) | |
f2cd2deb | 198 | continue; |
f2cd2deb FS |
199 | if (des.idVendor != LA2016_VID || des.idProduct != LA2016_PID) |
200 | continue; | |
201 | ||
96dc954e | 202 | /* USB identification matches, a device was found. */ |
cf057ac4 | 203 | sr_dbg("Found a device (USB identification)."); |
520a20e9 | 204 | sdi = g_malloc0(sizeof(*sdi)); |
f2cd2deb | 205 | sdi->status = SR_ST_INITIALIZING; |
520a20e9 | 206 | sdi->connection_id = g_strdup(conn_id); |
f2cd2deb | 207 | |
22cb067a | 208 | fw_uploaded = 0; |
852c7d14 | 209 | if (des.iProduct != LA2016_IPRODUCT_INDEX) { |
520a20e9 GS |
210 | sr_info("Device at '%s' has no firmware loaded.", |
211 | conn_id); | |
f2cd2deb | 212 | |
520a20e9 GS |
213 | ret = la2016_upload_firmware(ctx, dev, des.idProduct); |
214 | if (ret != SR_OK) { | |
91f73872 | 215 | sr_err("MCU firmware upload failed."); |
f2cd2deb FS |
216 | g_free(sdi->connection_id); |
217 | g_free(sdi); | |
218 | continue; | |
219 | } | |
22cb067a | 220 | fw_uploaded = g_get_monotonic_time(); |
96dc954e | 221 | /* Will re-enumerate. Mark as "unknown address yet". */ |
520a20e9 | 222 | addr = 0xff; |
f2cd2deb FS |
223 | } |
224 | ||
225 | sdi->vendor = g_strdup("Kingst"); | |
226 | sdi->model = g_strdup("LA2016"); | |
227 | ||
520a20e9 GS |
228 | for (ch_idx = 0; ch_idx < ARRAY_SIZE(channel_names); ch_idx++) { |
229 | sr_channel_new(sdi, ch_idx, SR_CHANNEL_LOGIC, | |
230 | TRUE, channel_names[ch_idx]); | |
231 | } | |
f2cd2deb FS |
232 | |
233 | devices = g_slist_append(devices, sdi); | |
234 | ||
520a20e9 | 235 | devc = g_malloc0(sizeof(*devc)); |
f2cd2deb | 236 | sdi->priv = devc; |
22cb067a | 237 | devc->fw_uploaded = fw_uploaded; |
f2cd2deb FS |
238 | devc->threshold_voltage_idx = 0; |
239 | devc->threshold_voltage = logic_threshold_value[devc->threshold_voltage_idx]; | |
240 | ||
241 | sdi->status = SR_ST_INACTIVE; | |
242 | sdi->inst_type = SR_INST_USB; | |
243 | ||
520a20e9 | 244 | sdi->conn = sr_usb_dev_inst_new(bus, addr, NULL); |
f2cd2deb FS |
245 | } |
246 | libusb_free_device_list(devlist, 1); | |
247 | g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free); | |
248 | ||
249 | return std_scan_complete(di, devices); | |
250 | } | |
251 | ||
252 | static int la2016_dev_open(struct sr_dev_inst *sdi) | |
253 | { | |
254 | struct sr_dev_driver *di; | |
520a20e9 GS |
255 | struct drv_context *drvc; |
256 | struct sr_context *ctx; | |
257 | libusb_device **devlist, *dev; | |
f2cd2deb FS |
258 | struct sr_usb_dev_inst *usb; |
259 | struct libusb_device_descriptor des; | |
520a20e9 GS |
260 | int ret; |
261 | size_t device_count, dev_idx; | |
262 | gboolean check_conn; | |
263 | char conn_id[64]; | |
f2cd2deb FS |
264 | |
265 | di = sdi->driver; | |
266 | drvc = di->context; | |
520a20e9 | 267 | ctx = drvc->sr_ctx;; |
f2cd2deb FS |
268 | usb = sdi->conn; |
269 | ret = SR_ERR; | |
270 | ||
520a20e9 GS |
271 | ret = libusb_get_device_list(ctx->libusb_ctx, &devlist); |
272 | if (ret < 0) { | |
273 | sr_err("Cannot get device list: %s.", libusb_error_name(ret)); | |
f2cd2deb FS |
274 | return SR_ERR; |
275 | } | |
520a20e9 GS |
276 | device_count = ret; |
277 | if (!device_count) { | |
278 | sr_warn("Device list is empty. Cannot open."); | |
279 | return SR_ERR; | |
280 | } | |
281 | for (dev_idx = 0; dev_idx < device_count; dev_idx++) { | |
282 | dev = devlist[dev_idx]; | |
283 | libusb_get_device_descriptor(dev, &des); | |
f2cd2deb | 284 | |
852c7d14 GS |
285 | if (des.idVendor != LA2016_VID || des.idProduct != LA2016_PID) |
286 | continue; | |
287 | if (des.iProduct != LA2016_IPRODUCT_INDEX) | |
f2cd2deb FS |
288 | continue; |
289 | ||
520a20e9 GS |
290 | check_conn = sdi->status == SR_ST_INITIALIZING; |
291 | check_conn |= sdi->status == SR_ST_INACTIVE; | |
292 | if (check_conn) { | |
96dc954e | 293 | /* Check physical USB bus/port address. */ |
520a20e9 GS |
294 | ret = usb_get_port_path(dev, conn_id, sizeof(conn_id)); |
295 | if (ret < 0) | |
f2cd2deb | 296 | continue; |
520a20e9 | 297 | if (strcmp(sdi->connection_id, conn_id) != 0) { |
96dc954e | 298 | /* Not the device we looked up before. */ |
f2cd2deb | 299 | continue; |
96dc954e | 300 | } |
f2cd2deb FS |
301 | } |
302 | ||
520a20e9 GS |
303 | ret = libusb_open(dev, &usb->devhdl); |
304 | if (ret != 0) { | |
305 | sr_err("Cannot open device: %s.", | |
306 | libusb_error_name(ret)); | |
307 | ret = SR_ERR_IO; | |
f2cd2deb FS |
308 | break; |
309 | } | |
310 | ||
520a20e9 GS |
311 | if (usb->address == 0xff) { |
312 | /* | |
313 | * First encounter after firmware upload. | |
314 | * Grab current address after enumeration. | |
315 | */ | |
316 | usb->address = libusb_get_device_address(dev); | |
317 | } | |
318 | ||
f2cd2deb FS |
319 | ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE); |
320 | if (ret == LIBUSB_ERROR_BUSY) { | |
91f73872 | 321 | sr_err("Cannot claim USB interface. Another program or driver using it?"); |
f2cd2deb FS |
322 | ret = SR_ERR; |
323 | break; | |
324 | } else if (ret == LIBUSB_ERROR_NO_DEVICE) { | |
325 | sr_err("Device has been disconnected."); | |
326 | ret = SR_ERR; | |
327 | break; | |
328 | } else if (ret != 0) { | |
520a20e9 GS |
329 | sr_err("Cannot claim USB interface: %s.", |
330 | libusb_error_name(ret)); | |
f2cd2deb FS |
331 | ret = SR_ERR; |
332 | break; | |
333 | } | |
334 | ||
335 | if ((ret = la2016_init_device(sdi)) != SR_OK) { | |
91f73872 | 336 | sr_err("Cannot initialize device."); |
f2cd2deb FS |
337 | break; |
338 | } | |
339 | ||
340 | sr_info("Opened device on %d.%d (logical) / %s (physical), interface %d.", | |
341 | usb->bus, usb->address, sdi->connection_id, USB_INTERFACE); | |
f2cd2deb | 342 | ret = SR_OK; |
f2cd2deb FS |
343 | break; |
344 | } | |
f2cd2deb FS |
345 | libusb_free_device_list(devlist, 1); |
346 | ||
347 | if (ret != SR_OK) { | |
348 | if (usb->devhdl) { | |
349 | libusb_release_interface(usb->devhdl, USB_INTERFACE); | |
350 | libusb_close(usb->devhdl); | |
351 | usb->devhdl = NULL; | |
352 | } | |
520a20e9 | 353 | return ret; |
f2cd2deb FS |
354 | } |
355 | ||
356 | return SR_OK; | |
357 | } | |
358 | ||
359 | static int dev_open(struct sr_dev_inst *sdi) | |
360 | { | |
361 | struct dev_context *devc; | |
22cb067a | 362 | uint64_t reset_done, now, elapsed_ms; |
f2cd2deb FS |
363 | int ret; |
364 | ||
365 | devc = sdi->priv; | |
366 | ||
367 | /* | |
22cb067a GS |
368 | * When the sigrok driver recently has uploaded MCU firmware, |
369 | * then wait for the FX2 to re-enumerate. Allow the USB device | |
370 | * to vanish before it reappears. Timeouts are rough estimates | |
371 | * after all, the imprecise time of the last check (potentially | |
372 | * executes after the total check period) simplifies code paths | |
373 | * with optional diagnostics. And increases the probability of | |
374 | * successfully detecting "late/slow" devices. | |
f2cd2deb | 375 | */ |
22cb067a | 376 | if (devc->fw_uploaded) { |
f2cd2deb | 377 | sr_info("Waiting for device to reset after firmware upload."); |
f2cd2deb | 378 | now = g_get_monotonic_time(); |
22cb067a GS |
379 | reset_done = devc->fw_uploaded + RENUM_GONE_DELAY_MS * 1000; |
380 | if (now < reset_done) | |
f2cd2deb | 381 | g_usleep(reset_done - now); |
22cb067a GS |
382 | do { |
383 | now = g_get_monotonic_time(); | |
384 | elapsed_ms = (now - devc->fw_uploaded) / 1000; | |
385 | sr_spew("Waited %" PRIu64 "ms.", elapsed_ms); | |
386 | ret = la2016_dev_open(sdi); | |
387 | if (ret == SR_OK) { | |
388 | devc->fw_uploaded = 0; | |
f2cd2deb | 389 | break; |
22cb067a GS |
390 | } |
391 | g_usleep(RENUM_POLL_INTERVAL_MS * 1000); | |
392 | } while (elapsed_ms < RENUM_CHECK_PERIOD_MS); | |
f2cd2deb FS |
393 | if (ret != SR_OK) { |
394 | sr_err("Device failed to re-enumerate."); | |
520a20e9 | 395 | return ret; |
f2cd2deb | 396 | } |
22cb067a | 397 | sr_info("Device came back after %" PRIi64 "ms.", elapsed_ms); |
955ab604 | 398 | } else { |
f2cd2deb | 399 | ret = la2016_dev_open(sdi); |
955ab604 | 400 | } |
f2cd2deb FS |
401 | |
402 | if (ret != SR_OK) { | |
91f73872 | 403 | sr_err("Cannot open device."); |
520a20e9 | 404 | return ret; |
f2cd2deb FS |
405 | } |
406 | ||
407 | return SR_OK; | |
408 | } | |
409 | ||
410 | static int dev_close(struct sr_dev_inst *sdi) | |
411 | { | |
412 | struct sr_usb_dev_inst *usb; | |
413 | ||
414 | usb = sdi->conn; | |
415 | ||
416 | if (!usb->devhdl) | |
417 | return SR_ERR_BUG; | |
418 | ||
419 | la2016_deinit_device(sdi); | |
420 | ||
421 | sr_info("Closing device on %d.%d (logical) / %s (physical) interface %d.", | |
422 | usb->bus, usb->address, sdi->connection_id, USB_INTERFACE); | |
423 | libusb_release_interface(usb->devhdl, USB_INTERFACE); | |
424 | libusb_close(usb->devhdl); | |
425 | usb->devhdl = NULL; | |
426 | ||
427 | return SR_OK; | |
428 | } | |
429 | ||
955ab604 GS |
430 | static int config_get(uint32_t key, GVariant **data, |
431 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
f2cd2deb FS |
432 | { |
433 | struct dev_context *devc; | |
434 | struct sr_usb_dev_inst *usb; | |
435 | double rounded; | |
da2cb50d | 436 | const char *label; |
f2cd2deb FS |
437 | |
438 | (void)cg; | |
439 | ||
440 | if (!sdi) | |
441 | return SR_ERR_ARG; | |
442 | devc = sdi->priv; | |
443 | ||
444 | switch (key) { | |
445 | case SR_CONF_CONN: | |
446 | if (!sdi->conn) | |
447 | return SR_ERR_ARG; | |
448 | usb = sdi->conn; | |
96dc954e GS |
449 | if (usb->address == 0xff) { |
450 | /* | |
451 | * Device still needs to re-enumerate after firmware | |
452 | * upload, so we don't know its (future) address. | |
453 | */ | |
f2cd2deb | 454 | return SR_ERR; |
955ab604 | 455 | } |
f2cd2deb FS |
456 | *data = g_variant_new_printf("%d.%d", usb->bus, usb->address); |
457 | break; | |
458 | case SR_CONF_SAMPLERATE: | |
459 | *data = g_variant_new_uint64(devc->cur_samplerate); | |
460 | break; | |
461 | case SR_CONF_LIMIT_SAMPLES: | |
462 | *data = g_variant_new_uint64(devc->limit_samples); | |
463 | break; | |
464 | case SR_CONF_CAPTURE_RATIO: | |
465 | *data = g_variant_new_uint64(devc->capture_ratio); | |
466 | break; | |
467 | case SR_CONF_VOLTAGE_THRESHOLD: | |
468 | rounded = (int)(devc->threshold_voltage / 0.1) * 0.1; | |
469 | *data = std_gvar_tuple_double(rounded, rounded + 0.1); | |
470 | return SR_OK; | |
471 | case SR_CONF_LOGIC_THRESHOLD: | |
da2cb50d GS |
472 | label = logic_threshold[devc->threshold_voltage_idx]; |
473 | *data = g_variant_new_string(label); | |
f2cd2deb FS |
474 | break; |
475 | case SR_CONF_LOGIC_THRESHOLD_CUSTOM: | |
476 | *data = g_variant_new_double(devc->threshold_voltage); | |
477 | break; | |
955ab604 | 478 | |
f2cd2deb FS |
479 | default: |
480 | return SR_ERR_NA; | |
481 | } | |
482 | ||
483 | return SR_OK; | |
484 | } | |
485 | ||
955ab604 GS |
486 | static int config_set(uint32_t key, GVariant *data, |
487 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
f2cd2deb FS |
488 | { |
489 | struct dev_context *devc; | |
490 | double low, high; | |
491 | int idx; | |
492 | ||
493 | (void)cg; | |
494 | ||
495 | devc = sdi->priv; | |
496 | ||
497 | switch (key) { | |
498 | case SR_CONF_SAMPLERATE: | |
499 | devc->cur_samplerate = g_variant_get_uint64(data); | |
500 | break; | |
501 | case SR_CONF_LIMIT_SAMPLES: | |
502 | devc->limit_samples = g_variant_get_uint64(data); | |
503 | break; | |
504 | case SR_CONF_CAPTURE_RATIO: | |
505 | devc->capture_ratio = g_variant_get_uint64(data); | |
506 | break; | |
507 | case SR_CONF_VOLTAGE_THRESHOLD: | |
508 | g_variant_get(data, "(dd)", &low, &high); | |
509 | devc->threshold_voltage = (low + high) / 2.0; | |
da2cb50d | 510 | devc->threshold_voltage_idx = LOGIC_THRESHOLD_IDX_USER; |
f2cd2deb FS |
511 | break; |
512 | case SR_CONF_LOGIC_THRESHOLD: { | |
411ad77c GS |
513 | idx = std_str_idx(data, ARRAY_AND_SIZE(logic_threshold)); |
514 | if (idx < 0) | |
f2cd2deb | 515 | return SR_ERR_ARG; |
da2cb50d | 516 | if (idx != LOGIC_THRESHOLD_IDX_USER) { |
f2cd2deb FS |
517 | devc->threshold_voltage = logic_threshold_value[idx]; |
518 | } | |
519 | devc->threshold_voltage_idx = idx; | |
520 | break; | |
521 | } | |
522 | case SR_CONF_LOGIC_THRESHOLD_CUSTOM: | |
523 | devc->threshold_voltage = g_variant_get_double(data); | |
524 | break; | |
525 | default: | |
526 | return SR_ERR_NA; | |
527 | } | |
528 | ||
529 | return SR_OK; | |
530 | } | |
531 | ||
955ab604 GS |
532 | static int config_list(uint32_t key, GVariant **data, |
533 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
f2cd2deb | 534 | { |
8b172e78 KG |
535 | struct dev_context *devc; |
536 | ||
f2cd2deb FS |
537 | switch (key) { |
538 | case SR_CONF_SCAN_OPTIONS: | |
539 | case SR_CONF_DEVICE_OPTIONS: | |
411ad77c GS |
540 | return STD_CONFIG_LIST(key, data, sdi, cg, |
541 | scanopts, drvopts, devopts); | |
f2cd2deb | 542 | case SR_CONF_SAMPLERATE: |
fb28e72d MW |
543 | if (!sdi) |
544 | return SR_ERR_ARG; | |
545 | devc = sdi->priv; | |
8b172e78 KG |
546 | if (devc->max_samplerate == SR_MHZ(200)) { |
547 | *data = std_gvar_samplerates(ARRAY_AND_SIZE(samplerates_la2016)); | |
1ed93110 | 548 | } else { |
8b172e78 KG |
549 | *data = std_gvar_samplerates(ARRAY_AND_SIZE(samplerates_la1016)); |
550 | } | |
f2cd2deb FS |
551 | break; |
552 | case SR_CONF_LIMIT_SAMPLES: | |
411ad77c GS |
553 | *data = std_gvar_tuple_u64(LA2016_NUM_SAMPLES_MIN, |
554 | LA2016_NUM_SAMPLES_MAX); | |
f2cd2deb FS |
555 | break; |
556 | case SR_CONF_VOLTAGE_THRESHOLD: | |
557 | *data = std_gvar_min_max_step_thresholds( | |
558 | LA2016_THR_VOLTAGE_MIN, | |
559 | LA2016_THR_VOLTAGE_MAX, 0.1); | |
560 | break; | |
561 | case SR_CONF_TRIGGER_MATCH: | |
562 | *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches)); | |
563 | break; | |
564 | case SR_CONF_LOGIC_THRESHOLD: | |
da2cb50d | 565 | *data = g_variant_new_strv(ARRAY_AND_SIZE(logic_threshold)); |
f2cd2deb FS |
566 | break; |
567 | default: | |
568 | return SR_ERR_NA; | |
569 | } | |
570 | ||
571 | return SR_OK; | |
572 | } | |
573 | ||
f2cd2deb FS |
574 | static int configure_channels(const struct sr_dev_inst *sdi) |
575 | { | |
576 | struct dev_context *devc; | |
084d6b92 GS |
577 | GSList *l; |
578 | struct sr_channel *ch; | |
f2cd2deb FS |
579 | |
580 | devc = sdi->priv; | |
084d6b92 | 581 | |
f2cd2deb | 582 | devc->cur_channels = 0; |
084d6b92 GS |
583 | for (l = sdi->channels; l; l = l->next) { |
584 | ch = l->data; | |
585 | if (!ch->enabled) | |
f2cd2deb | 586 | continue; |
084d6b92 | 587 | devc->cur_channels |= 1UL << ch->index; |
f2cd2deb FS |
588 | } |
589 | ||
590 | return SR_OK; | |
591 | } | |
592 | ||
593 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) | |
594 | { | |
595 | struct sr_dev_driver *di; | |
596 | struct drv_context *drvc; | |
520a20e9 | 597 | struct sr_context *ctx; |
f2cd2deb FS |
598 | struct dev_context *devc; |
599 | int ret; | |
600 | ||
601 | di = sdi->driver; | |
602 | drvc = di->context; | |
520a20e9 | 603 | ctx = drvc->sr_ctx;; |
f2cd2deb FS |
604 | devc = sdi->priv; |
605 | ||
606 | if (configure_channels(sdi) != SR_OK) { | |
91f73872 | 607 | sr_err("Cannot configure channels."); |
f2cd2deb FS |
608 | return SR_ERR; |
609 | } | |
610 | ||
852c7d14 | 611 | devc->convbuffer_size = LA2016_CONVBUFFER_SIZE; |
411ad77c GS |
612 | devc->convbuffer = g_try_malloc(devc->convbuffer_size); |
613 | if (!devc->convbuffer) { | |
91f73872 | 614 | sr_err("Cannot allocate conversion buffer."); |
f2cd2deb FS |
615 | return SR_ERR_MALLOC; |
616 | } | |
617 | ||
411ad77c GS |
618 | ret = la2016_setup_acquisition(sdi); |
619 | if (ret != SR_OK) { | |
f2cd2deb FS |
620 | g_free(devc->convbuffer); |
621 | devc->convbuffer = NULL; | |
622 | return ret; | |
623 | } | |
624 | ||
411ad77c GS |
625 | ret = la2016_start_acquisition(sdi); |
626 | if (ret != SR_OK) { | |
3ebc1cb2 | 627 | la2016_abort_acquisition(sdi); |
520a20e9 GS |
628 | g_free(devc->convbuffer); |
629 | devc->convbuffer = NULL; | |
f2cd2deb FS |
630 | return ret; |
631 | } | |
632 | ||
cf057ac4 | 633 | devc->completion_seen = FALSE; |
520a20e9 | 634 | usb_source_add(sdi->session, ctx, 50, |
388438e4 | 635 | la2016_receive_data, (void *)sdi); |
f2cd2deb FS |
636 | |
637 | std_session_send_df_header(sdi); | |
638 | ||
639 | return SR_OK; | |
640 | } | |
641 | ||
642 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) | |
643 | { | |
644 | int ret; | |
645 | ||
646 | ret = la2016_abort_acquisition(sdi); | |
f2cd2deb FS |
647 | |
648 | return ret; | |
649 | } | |
650 | ||
651 | static struct sr_dev_driver kingst_la2016_driver_info = { | |
652 | .name = "kingst-la2016", | |
653 | .longname = "Kingst LA2016", | |
654 | .api_version = 1, | |
655 | .init = std_init, | |
656 | .cleanup = std_cleanup, | |
657 | .scan = scan, | |
658 | .dev_list = std_dev_list, | |
659 | .dev_clear = std_dev_clear, | |
660 | .config_get = config_get, | |
661 | .config_set = config_set, | |
662 | .config_list = config_list, | |
663 | .dev_open = dev_open, | |
664 | .dev_close = dev_close, | |
665 | .dev_acquisition_start = dev_acquisition_start, | |
666 | .dev_acquisition_stop = dev_acquisition_stop, | |
667 | .context = NULL, | |
668 | }; | |
669 | SR_REGISTER_DEV_DRIVER(kingst_la2016_driver_info); |