2 * This file is part of the libsigrok project.
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>
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.
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.
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/>.
24 * This driver implementation initially was derived from the
25 * src/hardware/saleae-logic16/ source code.
30 #include <libsigrok/libsigrok.h>
33 #include "libsigrok-internal.h"
36 static const uint32_t scanopts[] = {
40 static const uint32_t drvopts[] = {
41 SR_CONF_LOGIC_ANALYZER,
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,
56 static const int32_t trigger_matches[] = {
63 static const char *channel_names[] = {
64 "0", "1", "2", "3", "4", "5", "6", "7",
65 "8", "9", "10", "11", "12", "13", "14", "15",
68 static const uint64_t samplerates_la2016[] = {
86 static const uint64_t samplerates_la1016[] = {
103 static const float logic_threshold_value[] = {
115 static const char *logic_threshold[] = {
128 #define MAX_NUM_LOGIC_THRESHOLD_ENTRIES ARRAY_SIZE(logic_threshold)
130 static GSList *scan(struct sr_dev_driver *di, GSList *options)
132 struct drv_context *drvc;
133 struct dev_context *devc;
134 struct sr_dev_inst *sdi;
135 struct sr_usb_dev_inst *usb;
136 struct sr_config *src;
139 GSList *conn_devices;
140 struct libusb_device_descriptor des;
141 libusb_device **devlist;
144 char connection_id[64];
145 uint64_t fw_uploaded;
146 unsigned int dev_addr;
151 for (l = options; l; l = l->next) {
155 conn = g_variant_get_string(src->data, NULL);
160 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
164 /* Find all LA2016 devices, optionally upload firmware to them. */
166 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
167 for (i = 0; devlist[i]; i++) {
170 for (l = conn_devices; l; l = l->next) {
172 if (usb->bus == libusb_get_bus_number(devlist[i]) &&
173 usb->address == libusb_get_device_address(devlist[i]))
178 * A connection parameter was specified and
179 * this device does not match the filter.
185 libusb_get_device_descriptor(devlist[i], &des);
187 if (usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)) < 0)
190 if (des.idVendor != LA2016_VID || des.idProduct != LA2016_PID)
193 /* USB identification matches, a device was found. */
194 sr_dbg("Found a LA2016 device.");
195 sdi = g_malloc0(sizeof(struct sr_dev_inst));
196 sdi->status = SR_ST_INITIALIZING;
197 sdi->connection_id = g_strdup(connection_id);
200 dev_addr = libusb_get_device_address(devlist[i]);
201 if (des.iProduct != 2) {
202 sr_info("Device at '%s' has no firmware loaded.", connection_id);
204 if (la2016_upload_firmware(drvc->sr_ctx, devlist[i], des.idProduct) != SR_OK) {
205 sr_err("MCU firmware upload failed.");
206 g_free(sdi->connection_id);
210 fw_uploaded = g_get_monotonic_time();
211 /* Will re-enumerate. Mark as "unknown address yet". */
215 sdi->vendor = g_strdup("Kingst");
216 sdi->model = g_strdup("LA2016");
218 for (j = 0; j < ARRAY_SIZE(channel_names); j++)
219 sr_channel_new(sdi, j, SR_CHANNEL_LOGIC, TRUE, channel_names[j]);
221 devices = g_slist_append(devices, sdi);
223 devc = g_malloc0(sizeof(struct dev_context));
225 devc->fw_uploaded = fw_uploaded;
226 devc->threshold_voltage_idx = 0;
227 devc->threshold_voltage = logic_threshold_value[devc->threshold_voltage_idx];
229 sdi->status = SR_ST_INACTIVE;
230 sdi->inst_type = SR_INST_USB;
232 sdi->conn = sr_usb_dev_inst_new(
233 libusb_get_bus_number(devlist[i]),
236 libusb_free_device_list(devlist, 1);
237 g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free);
239 return std_scan_complete(di, devices);
242 static int la2016_dev_open(struct sr_dev_inst *sdi)
244 struct sr_dev_driver *di;
245 libusb_device **devlist;
246 struct sr_usb_dev_inst *usb;
247 struct libusb_device_descriptor des;
248 struct drv_context *drvc;
249 int ret, i, device_count;
250 char connection_id[64];
257 device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
258 if (device_count < 0) {
259 sr_err("Failed to get device list: %s.", libusb_error_name(device_count));
263 for (i = 0; i < device_count; i++) {
264 libusb_get_device_descriptor(devlist[i], &des);
266 if (des.idVendor != LA2016_VID || des.idProduct != LA2016_PID || des.iProduct != 2)
269 if ((sdi->status == SR_ST_INITIALIZING) || (sdi->status == SR_ST_INACTIVE)) {
270 /* Check physical USB bus/port address. */
271 if (usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)) < 0)
274 if (strcmp(sdi->connection_id, connection_id)) {
275 /* Not the device we looked up before. */
280 if (!(ret = libusb_open(devlist[i], &usb->devhdl))) {
281 if (usb->address == 0xff) {
283 * First encounter after firmware upload.
284 * Grab current address after enumeration.
286 usb->address = libusb_get_device_address(devlist[i]);
289 sr_err("Failed to open device: %s.", libusb_error_name(ret));
294 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
295 if (ret == LIBUSB_ERROR_BUSY) {
296 sr_err("Cannot claim USB interface. Another program or driver using it?");
299 } else if (ret == LIBUSB_ERROR_NO_DEVICE) {
300 sr_err("Device has been disconnected.");
303 } else if (ret != 0) {
304 sr_err("Cannot claim USB interface: %s.", libusb_error_name(ret));
309 if ((ret = la2016_init_device(sdi)) != SR_OK) {
310 sr_err("Cannot initialize device.");
314 sr_info("Opened device on %d.%d (logical) / %s (physical), interface %d.",
315 usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
322 libusb_free_device_list(devlist, 1);
326 libusb_release_interface(usb->devhdl, USB_INTERFACE);
327 libusb_close(usb->devhdl);
336 static int dev_open(struct sr_dev_inst *sdi)
338 struct dev_context *devc;
339 uint64_t reset_done, now, elapsed_ms;
345 * When the sigrok driver recently has uploaded MCU firmware,
346 * then wait for the FX2 to re-enumerate. Allow the USB device
347 * to vanish before it reappears. Timeouts are rough estimates
348 * after all, the imprecise time of the last check (potentially
349 * executes after the total check period) simplifies code paths
350 * with optional diagnostics. And increases the probability of
351 * successfully detecting "late/slow" devices.
353 if (devc->fw_uploaded) {
354 sr_info("Waiting for device to reset after firmware upload.");
355 now = g_get_monotonic_time();
356 reset_done = devc->fw_uploaded + RENUM_GONE_DELAY_MS * 1000;
357 if (now < reset_done)
358 g_usleep(reset_done - now);
360 now = g_get_monotonic_time();
361 elapsed_ms = (now - devc->fw_uploaded) / 1000;
362 sr_spew("Waited %" PRIu64 "ms.", elapsed_ms);
363 ret = la2016_dev_open(sdi);
365 devc->fw_uploaded = 0;
368 g_usleep(RENUM_POLL_INTERVAL_MS * 1000);
369 } while (elapsed_ms < RENUM_CHECK_PERIOD_MS);
371 sr_err("Device failed to re-enumerate.");
374 sr_info("Device came back after %" PRIi64 "ms.", elapsed_ms);
376 ret = la2016_dev_open(sdi);
380 sr_err("Cannot open device.");
387 static int dev_close(struct sr_dev_inst *sdi)
389 struct sr_usb_dev_inst *usb;
396 la2016_deinit_device(sdi);
398 sr_info("Closing device on %d.%d (logical) / %s (physical) interface %d.",
399 usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
400 libusb_release_interface(usb->devhdl, USB_INTERFACE);
401 libusb_close(usb->devhdl);
407 static int config_get(uint32_t key, GVariant **data,
408 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
410 struct dev_context *devc;
411 struct sr_usb_dev_inst *usb;
425 if (usb->address == 0xff) {
427 * Device still needs to re-enumerate after firmware
428 * upload, so we don't know its (future) address.
432 *data = g_variant_new_printf("%d.%d", usb->bus, usb->address);
434 case SR_CONF_SAMPLERATE:
435 *data = g_variant_new_uint64(devc->cur_samplerate);
437 case SR_CONF_LIMIT_SAMPLES:
438 *data = g_variant_new_uint64(devc->limit_samples);
440 case SR_CONF_CAPTURE_RATIO:
441 *data = g_variant_new_uint64(devc->capture_ratio);
443 case SR_CONF_VOLTAGE_THRESHOLD:
444 rounded = (int)(devc->threshold_voltage / 0.1) * 0.1;
445 *data = std_gvar_tuple_double(rounded, rounded + 0.1);
447 case SR_CONF_LOGIC_THRESHOLD:
448 *data = g_variant_new_string(logic_threshold[devc->threshold_voltage_idx]);
450 case SR_CONF_LOGIC_THRESHOLD_CUSTOM:
451 *data = g_variant_new_double(devc->threshold_voltage);
461 static int config_set(uint32_t key, GVariant *data,
462 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
464 struct dev_context *devc;
473 case SR_CONF_SAMPLERATE:
474 devc->cur_samplerate = g_variant_get_uint64(data);
476 case SR_CONF_LIMIT_SAMPLES:
477 devc->limit_samples = g_variant_get_uint64(data);
479 case SR_CONF_CAPTURE_RATIO:
480 devc->capture_ratio = g_variant_get_uint64(data);
482 case SR_CONF_VOLTAGE_THRESHOLD:
483 g_variant_get(data, "(dd)", &low, &high);
484 devc->threshold_voltage = (low + high) / 2.0;
485 devc->threshold_voltage_idx = MAX_NUM_LOGIC_THRESHOLD_ENTRIES - 1; /* USER */
487 case SR_CONF_LOGIC_THRESHOLD: {
488 if ((idx = std_str_idx(data, logic_threshold, MAX_NUM_LOGIC_THRESHOLD_ENTRIES)) < 0)
490 if (idx == MAX_NUM_LOGIC_THRESHOLD_ENTRIES - 1) {
493 devc->threshold_voltage = logic_threshold_value[idx];
495 devc->threshold_voltage_idx = idx;
498 case SR_CONF_LOGIC_THRESHOLD_CUSTOM:
499 devc->threshold_voltage = g_variant_get_double(data);
508 static int config_list(uint32_t key, GVariant **data,
509 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
511 struct dev_context *devc;
514 case SR_CONF_SCAN_OPTIONS:
515 case SR_CONF_DEVICE_OPTIONS:
516 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
517 case SR_CONF_SAMPLERATE:
521 if (devc->max_samplerate == SR_MHZ(200)) {
522 *data = std_gvar_samplerates(ARRAY_AND_SIZE(samplerates_la2016));
524 *data = std_gvar_samplerates(ARRAY_AND_SIZE(samplerates_la1016));
527 case SR_CONF_LIMIT_SAMPLES:
528 *data = std_gvar_tuple_u64(LA2016_NUM_SAMPLES_MIN, LA2016_NUM_SAMPLES_MAX);
530 case SR_CONF_VOLTAGE_THRESHOLD:
531 *data = std_gvar_min_max_step_thresholds(
532 LA2016_THR_VOLTAGE_MIN,
533 LA2016_THR_VOLTAGE_MAX, 0.1);
535 case SR_CONF_TRIGGER_MATCH:
536 *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
538 case SR_CONF_LOGIC_THRESHOLD:
539 *data = g_variant_new_strv(logic_threshold, MAX_NUM_LOGIC_THRESHOLD_ENTRIES);
548 static int configure_channels(const struct sr_dev_inst *sdi)
550 struct dev_context *devc;
553 devc->cur_channels = 0;
554 devc->num_channels = 0;
556 for (GSList *l = sdi->channels; l; l = l->next) {
557 struct sr_channel *ch = (struct sr_channel*)l->data;
558 if (ch->enabled == FALSE)
560 devc->cur_channels |= 1 << ch->index;
561 devc->num_channels++;
567 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
569 struct sr_dev_driver *di;
570 struct drv_context *drvc;
571 struct dev_context *devc;
578 if (configure_channels(sdi) != SR_OK) {
579 sr_err("Cannot configure channels.");
583 devc->convbuffer_size = 4 * 1024 * 1024;
584 if (!(devc->convbuffer = g_try_malloc(devc->convbuffer_size))) {
585 sr_err("Cannot allocate conversion buffer.");
586 return SR_ERR_MALLOC;
589 if ((ret = la2016_setup_acquisition(sdi)) != SR_OK) {
590 g_free(devc->convbuffer);
591 devc->convbuffer = NULL;
595 devc->ctx = drvc->sr_ctx;
597 if ((ret = la2016_start_acquisition(sdi)) != SR_OK) {
598 la2016_abort_acquisition(sdi);
602 devc->have_trigger = 0;
603 usb_source_add(sdi->session, drvc->sr_ctx, 50,
604 la2016_receive_data, (void *)sdi);
606 std_session_send_df_header(sdi);
611 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
615 ret = la2016_abort_acquisition(sdi);
620 static struct sr_dev_driver kingst_la2016_driver_info = {
621 .name = "kingst-la2016",
622 .longname = "Kingst LA2016",
625 .cleanup = std_cleanup,
627 .dev_list = std_dev_list,
628 .dev_clear = std_dev_clear,
629 .config_get = config_get,
630 .config_set = config_set,
631 .config_list = config_list,
632 .dev_open = dev_open,
633 .dev_close = dev_close,
634 .dev_acquisition_start = dev_acquisition_start,
635 .dev_acquisition_stop = dev_acquisition_stop,
638 SR_REGISTER_DEV_DRIVER(kingst_la2016_driver_info);