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/>.
23 /* mostly stolen from src/hardware/saleae-logic16/ */
27 #include <libsigrok/libsigrok.h>
30 #include "libsigrok-internal.h"
33 static const uint32_t scanopts[] = {
37 static const uint32_t drvopts[] = {
38 SR_CONF_LOGIC_ANALYZER,
41 static const uint32_t devopts[] = {
42 /* TODO: SR_CONF_CONTINUOUS, */
43 SR_CONF_CONN | SR_CONF_GET,
44 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
45 SR_CONF_LIMIT_SAMPLES | SR_CONF_SET | SR_CONF_GET | SR_CONF_LIST,
46 SR_CONF_VOLTAGE_THRESHOLD | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
47 SR_CONF_LOGIC_THRESHOLD | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
48 SR_CONF_LOGIC_THRESHOLD_CUSTOM | SR_CONF_GET | SR_CONF_SET,
49 SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
50 SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET,
53 static const int32_t trigger_matches[] = {
60 static const char *channel_names[] = {
61 "0", "1", "2", "3", "4", "5", "6", "7",
62 "8", "9", "10", "11", "12", "13", "14", "15",
65 static const uint64_t samplerates_la2016[] = {
83 static const uint64_t samplerates_la1016[] = {
100 static const float logic_threshold_value[] = {
112 static const char *logic_threshold[] = {
125 #define MAX_NUM_LOGIC_THRESHOLD_ENTRIES ARRAY_SIZE(logic_threshold)
127 static GSList *scan(struct sr_dev_driver *di, GSList *options)
129 struct drv_context *drvc;
130 struct dev_context *devc;
131 struct sr_dev_inst *sdi;
132 struct sr_usb_dev_inst *usb;
133 struct sr_config *src;
136 GSList *conn_devices;
137 struct libusb_device_descriptor des;
138 libusb_device **devlist;
141 char connection_id[64];
143 unsigned int dev_addr;
148 for (l = options; l; l = l->next) {
152 conn = g_variant_get_string(src->data, NULL);
157 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
161 /* Find all LA2016 devices and upload firmware to them. */
163 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
164 for (i = 0; devlist[i]; i++) {
167 for (l = conn_devices; l; l = l->next) {
169 if (usb->bus == libusb_get_bus_number(devlist[i]) &&
170 usb->address == libusb_get_device_address(devlist[i]))
174 /* This device matched none of the ones that
175 * matched the conn specification. */
180 libusb_get_device_descriptor(devlist[i], &des);
182 if (usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)) < 0)
185 if (des.idVendor != LA2016_VID || des.idProduct != LA2016_PID)
188 /* Already has the firmware */
189 sr_dbg("Found a LA2016 device.");
190 sdi = g_malloc0(sizeof(struct sr_dev_inst));
191 sdi->status = SR_ST_INITIALIZING;
192 sdi->connection_id = g_strdup(connection_id);
195 dev_addr = libusb_get_device_address(devlist[i]);
196 if (des.iProduct != 2) {
197 sr_info("Device at '%s' has no firmware loaded.", connection_id);
199 if (la2016_upload_firmware(drvc->sr_ctx, devlist[i], des.idProduct) != SR_OK) {
200 sr_err("MCU firmware upload failed.");
201 g_free(sdi->connection_id);
205 fw_updated = g_get_monotonic_time();
206 dev_addr = 0xff; /* to mark that we don't know address yet... ugly */
209 sdi->vendor = g_strdup("Kingst");
210 sdi->model = g_strdup("LA2016");
212 for (j = 0; j < ARRAY_SIZE(channel_names); j++)
213 sr_channel_new(sdi, j, SR_CHANNEL_LOGIC, TRUE, channel_names[j]);
215 devices = g_slist_append(devices, sdi);
217 devc = g_malloc0(sizeof(struct dev_context));
219 devc->fw_updated = fw_updated;
220 devc->threshold_voltage_idx = 0;
221 devc->threshold_voltage = logic_threshold_value[devc->threshold_voltage_idx];
223 sdi->status = SR_ST_INACTIVE;
224 sdi->inst_type = SR_INST_USB;
226 sdi->conn = sr_usb_dev_inst_new(
227 libusb_get_bus_number(devlist[i]),
230 libusb_free_device_list(devlist, 1);
231 g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free);
233 return std_scan_complete(di, devices);
236 static int la2016_dev_open(struct sr_dev_inst *sdi)
238 struct sr_dev_driver *di;
239 libusb_device **devlist;
240 struct sr_usb_dev_inst *usb;
241 struct libusb_device_descriptor des;
242 struct drv_context *drvc;
243 int ret, i, device_count;
244 char connection_id[64];
251 device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
252 if (device_count < 0) {
253 sr_err("Failed to get device list: %s.", libusb_error_name(device_count));
257 for (i = 0; i < device_count; i++) {
258 libusb_get_device_descriptor(devlist[i], &des);
260 if (des.idVendor != LA2016_VID || des.idProduct != LA2016_PID || des.iProduct != 2)
263 if ((sdi->status == SR_ST_INITIALIZING) || (sdi->status == SR_ST_INACTIVE)) {
265 * Check device by its physical USB bus/port address.
267 if (usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)) < 0)
270 if (strcmp(sdi->connection_id, connection_id))
271 /* This is not the one. */
275 if (!(ret = libusb_open(devlist[i], &usb->devhdl))) {
276 if (usb->address == 0xff)
278 * First time we touch this device after FW
279 * upload, so we don't know the address yet.
281 usb->address = libusb_get_device_address(devlist[i]);
283 sr_err("Failed to open device: %s.", libusb_error_name(ret));
288 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
289 if (ret == LIBUSB_ERROR_BUSY) {
290 sr_err("Cannot claim USB interface. Another program or driver using it?");
293 } else if (ret == LIBUSB_ERROR_NO_DEVICE) {
294 sr_err("Device has been disconnected.");
297 } else if (ret != 0) {
298 sr_err("Cannot claim USB interface: %s.", libusb_error_name(ret));
303 if ((ret = la2016_init_device(sdi)) != SR_OK) {
304 sr_err("Cannot initialize device.");
308 sr_info("Opened device on %d.%d (logical) / %s (physical), interface %d.",
309 usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
316 libusb_free_device_list(devlist, 1);
320 libusb_release_interface(usb->devhdl, USB_INTERFACE);
321 libusb_close(usb->devhdl);
330 static int dev_open(struct sr_dev_inst *sdi)
332 struct dev_context *devc;
333 int64_t timediff_us, timediff_ms;
341 * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS
342 * milliseconds for the FX2 to renumerate.
345 if (devc->fw_updated > 0) {
346 sr_info("Waiting for device to reset after firmware upload.");
347 /* Takes >= 2000ms for the uC to be gone from the USB bus. */
348 reset_done = devc->fw_updated + 18 * (uint64_t)1e5; /* 1.8 seconds */
349 now = g_get_monotonic_time();
350 if (reset_done > now)
351 g_usleep(reset_done - now);
353 while (timediff_ms < MAX_RENUM_DELAY_MS) {
354 g_usleep(200 * 1000);
356 timediff_us = g_get_monotonic_time() - devc->fw_updated;
357 timediff_ms = timediff_us / 1000;
359 if ((ret = la2016_dev_open(sdi)) == SR_OK)
361 sr_spew("Waited %" PRIi64 "ms.", timediff_ms);
364 sr_err("Device failed to re-enumerate.");
367 sr_info("Device came back after %" PRIi64 "ms.", timediff_ms);
369 ret = la2016_dev_open(sdi);
373 sr_err("Cannot open device.");
380 static int dev_close(struct sr_dev_inst *sdi)
382 struct sr_usb_dev_inst *usb;
389 la2016_deinit_device(sdi);
391 sr_info("Closing device on %d.%d (logical) / %s (physical) interface %d.",
392 usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
393 libusb_release_interface(usb->devhdl, USB_INTERFACE);
394 libusb_close(usb->devhdl);
400 static int config_get(uint32_t key, GVariant **data,
401 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
403 struct dev_context *devc;
404 struct sr_usb_dev_inst *usb;
418 if (usb->address == 255) {
419 /* Device still needs to re-enumerate after firmware
420 * upload, so we don't know its (future) address. */
423 *data = g_variant_new_printf("%d.%d", usb->bus, usb->address);
425 case SR_CONF_SAMPLERATE:
426 *data = g_variant_new_uint64(devc->cur_samplerate);
428 case SR_CONF_LIMIT_SAMPLES:
429 *data = g_variant_new_uint64(devc->limit_samples);
431 case SR_CONF_CAPTURE_RATIO:
432 *data = g_variant_new_uint64(devc->capture_ratio);
434 case SR_CONF_VOLTAGE_THRESHOLD:
435 rounded = (int)(devc->threshold_voltage / 0.1) * 0.1;
436 *data = std_gvar_tuple_double(rounded, rounded + 0.1);
438 case SR_CONF_LOGIC_THRESHOLD:
439 *data = g_variant_new_string(logic_threshold[devc->threshold_voltage_idx]);
441 case SR_CONF_LOGIC_THRESHOLD_CUSTOM:
442 *data = g_variant_new_double(devc->threshold_voltage);
452 static int config_set(uint32_t key, GVariant *data,
453 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
455 struct dev_context *devc;
464 case SR_CONF_SAMPLERATE:
465 devc->cur_samplerate = g_variant_get_uint64(data);
467 case SR_CONF_LIMIT_SAMPLES:
468 devc->limit_samples = g_variant_get_uint64(data);
470 case SR_CONF_CAPTURE_RATIO:
471 devc->capture_ratio = g_variant_get_uint64(data);
473 case SR_CONF_VOLTAGE_THRESHOLD:
474 g_variant_get(data, "(dd)", &low, &high);
475 devc->threshold_voltage = (low + high) / 2.0;
476 devc->threshold_voltage_idx = MAX_NUM_LOGIC_THRESHOLD_ENTRIES - 1; /* USER */
478 case SR_CONF_LOGIC_THRESHOLD: {
479 if ((idx = std_str_idx(data, logic_threshold, MAX_NUM_LOGIC_THRESHOLD_ENTRIES)) < 0)
481 if (idx == MAX_NUM_LOGIC_THRESHOLD_ENTRIES - 1) {
484 devc->threshold_voltage = logic_threshold_value[idx];
486 devc->threshold_voltage_idx = idx;
489 case SR_CONF_LOGIC_THRESHOLD_CUSTOM:
490 devc->threshold_voltage = g_variant_get_double(data);
499 static int config_list(uint32_t key, GVariant **data,
500 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
502 struct dev_context *devc;
505 case SR_CONF_SCAN_OPTIONS:
506 case SR_CONF_DEVICE_OPTIONS:
507 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
508 case SR_CONF_SAMPLERATE:
512 if (devc->max_samplerate == SR_MHZ(200)) {
513 *data = std_gvar_samplerates(ARRAY_AND_SIZE(samplerates_la2016));
516 *data = std_gvar_samplerates(ARRAY_AND_SIZE(samplerates_la1016));
519 case SR_CONF_LIMIT_SAMPLES:
520 *data = std_gvar_tuple_u64(LA2016_NUM_SAMPLES_MIN, LA2016_NUM_SAMPLES_MAX);
522 case SR_CONF_VOLTAGE_THRESHOLD:
523 *data = std_gvar_min_max_step_thresholds(
524 LA2016_THR_VOLTAGE_MIN,
525 LA2016_THR_VOLTAGE_MAX, 0.1);
527 case SR_CONF_TRIGGER_MATCH:
528 *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
530 case SR_CONF_LOGIC_THRESHOLD:
531 *data = g_variant_new_strv(logic_threshold, MAX_NUM_LOGIC_THRESHOLD_ENTRIES);
540 static int configure_channels(const struct sr_dev_inst *sdi)
542 struct dev_context *devc;
545 devc->cur_channels = 0;
546 devc->num_channels = 0;
548 for (GSList *l = sdi->channels; l; l = l->next) {
549 struct sr_channel *ch = (struct sr_channel*)l->data;
550 if (ch->enabled == FALSE)
552 devc->cur_channels |= 1 << ch->index;
553 devc->num_channels++;
559 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
561 struct sr_dev_driver *di;
562 struct drv_context *drvc;
563 struct dev_context *devc;
570 if (configure_channels(sdi) != SR_OK) {
571 sr_err("Cannot configure channels.");
575 devc->convbuffer_size = 4 * 1024 * 1024;
576 if (!(devc->convbuffer = g_try_malloc(devc->convbuffer_size))) {
577 sr_err("Cannot allocate conversion buffer.");
578 return SR_ERR_MALLOC;
581 if ((ret = la2016_setup_acquisition(sdi)) != SR_OK) {
582 g_free(devc->convbuffer);
583 devc->convbuffer = NULL;
587 devc->ctx = drvc->sr_ctx;
589 if ((ret = la2016_start_acquisition(sdi)) != SR_OK) {
590 la2016_abort_acquisition(sdi);
594 devc->have_trigger = 0;
595 usb_source_add(sdi->session, drvc->sr_ctx, 50,
596 la2016_receive_data, (void *)sdi);
598 std_session_send_df_header(sdi);
603 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
607 ret = la2016_abort_acquisition(sdi);
612 static struct sr_dev_driver kingst_la2016_driver_info = {
613 .name = "kingst-la2016",
614 .longname = "Kingst LA2016",
617 .cleanup = std_cleanup,
619 .dev_list = std_dev_list,
620 .dev_clear = std_dev_clear,
621 .config_get = config_get,
622 .config_set = config_set,
623 .config_list = config_list,
624 .dev_open = dev_open,
625 .dev_close = dev_close,
626 .dev_acquisition_start = dev_acquisition_start,
627 .dev_acquisition_stop = dev_acquisition_stop,
630 SR_REGISTER_DEV_DRIVER(kingst_la2016_driver_info);