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