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("uC 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("Unable to claim USB interface. Another "
291 "program or driver has already claimed it.");
294 } else if (ret == LIBUSB_ERROR_NO_DEVICE) {
295 sr_err("Device has been disconnected.");
298 } else if (ret != 0) {
299 sr_err("Unable to claim interface: %s.", libusb_error_name(ret));
304 if ((ret = la2016_init_device(sdi)) != SR_OK) {
305 sr_err("Failed to init device.");
309 sr_info("Opened device on %d.%d (logical) / %s (physical), interface %d.",
310 usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
317 libusb_free_device_list(devlist, 1);
321 libusb_release_interface(usb->devhdl, USB_INTERFACE);
322 libusb_close(usb->devhdl);
331 static int dev_open(struct sr_dev_inst *sdi)
333 struct dev_context *devc;
334 int64_t timediff_us, timediff_ms;
342 * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS
343 * milliseconds for the FX2 to renumerate.
346 if (devc->fw_updated > 0) {
347 sr_info("Waiting for device to reset after firmware upload.");
348 /* Takes >= 2000ms for the uC to be gone from the USB bus. */
349 reset_done = devc->fw_updated + 18 * (uint64_t)1e5; /* 1.8 seconds */
350 now = g_get_monotonic_time();
351 if (reset_done > now)
352 g_usleep(reset_done - now);
354 while (timediff_ms < MAX_RENUM_DELAY_MS) {
355 g_usleep(200 * 1000);
357 timediff_us = g_get_monotonic_time() - devc->fw_updated;
358 timediff_ms = timediff_us / 1000;
360 if ((ret = la2016_dev_open(sdi)) == SR_OK)
362 sr_spew("Waited %" PRIi64 "ms.", timediff_ms);
365 sr_err("Device failed to re-enumerate.");
368 sr_info("Device came back after %" PRIi64 "ms.", timediff_ms);
370 ret = la2016_dev_open(sdi);
374 sr_err("Unable to open device.");
381 static int dev_close(struct sr_dev_inst *sdi)
383 struct sr_usb_dev_inst *usb;
390 la2016_deinit_device(sdi);
392 sr_info("Closing device on %d.%d (logical) / %s (physical) interface %d.",
393 usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
394 libusb_release_interface(usb->devhdl, USB_INTERFACE);
395 libusb_close(usb->devhdl);
401 static int config_get(uint32_t key, GVariant **data,
402 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
404 struct dev_context *devc;
405 struct sr_usb_dev_inst *usb;
419 if (usb->address == 255) {
420 /* Device still needs to re-enumerate after firmware
421 * upload, so we don't know its (future) address. */
424 *data = g_variant_new_printf("%d.%d", usb->bus, usb->address);
426 case SR_CONF_SAMPLERATE:
427 *data = g_variant_new_uint64(devc->cur_samplerate);
429 case SR_CONF_LIMIT_SAMPLES:
430 *data = g_variant_new_uint64(devc->limit_samples);
432 case SR_CONF_CAPTURE_RATIO:
433 *data = g_variant_new_uint64(devc->capture_ratio);
435 case SR_CONF_VOLTAGE_THRESHOLD:
436 rounded = (int)(devc->threshold_voltage / 0.1) * 0.1;
437 *data = std_gvar_tuple_double(rounded, rounded + 0.1);
439 case SR_CONF_LOGIC_THRESHOLD:
440 *data = g_variant_new_string(logic_threshold[devc->threshold_voltage_idx]);
442 case SR_CONF_LOGIC_THRESHOLD_CUSTOM:
443 *data = g_variant_new_double(devc->threshold_voltage);
453 static int config_set(uint32_t key, GVariant *data,
454 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
456 struct dev_context *devc;
465 case SR_CONF_SAMPLERATE:
466 devc->cur_samplerate = g_variant_get_uint64(data);
468 case SR_CONF_LIMIT_SAMPLES:
469 devc->limit_samples = g_variant_get_uint64(data);
471 case SR_CONF_CAPTURE_RATIO:
472 devc->capture_ratio = g_variant_get_uint64(data);
474 case SR_CONF_VOLTAGE_THRESHOLD:
475 g_variant_get(data, "(dd)", &low, &high);
476 devc->threshold_voltage = (low + high) / 2.0;
477 devc->threshold_voltage_idx = MAX_NUM_LOGIC_THRESHOLD_ENTRIES - 1; /* USER */
479 case SR_CONF_LOGIC_THRESHOLD: {
480 if ((idx = std_str_idx(data, logic_threshold, MAX_NUM_LOGIC_THRESHOLD_ENTRIES)) < 0)
482 if (idx == MAX_NUM_LOGIC_THRESHOLD_ENTRIES - 1) {
485 devc->threshold_voltage = logic_threshold_value[idx];
487 devc->threshold_voltage_idx = idx;
490 case SR_CONF_LOGIC_THRESHOLD_CUSTOM:
491 devc->threshold_voltage = g_variant_get_double(data);
500 static int config_list(uint32_t key, GVariant **data,
501 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
503 struct dev_context *devc;
506 case SR_CONF_SCAN_OPTIONS:
507 case SR_CONF_DEVICE_OPTIONS:
508 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
509 case SR_CONF_SAMPLERATE:
513 if (devc->max_samplerate == SR_MHZ(200)) {
514 *data = std_gvar_samplerates(ARRAY_AND_SIZE(samplerates_la2016));
517 *data = std_gvar_samplerates(ARRAY_AND_SIZE(samplerates_la1016));
520 case SR_CONF_LIMIT_SAMPLES:
521 *data = std_gvar_tuple_u64(LA2016_NUM_SAMPLES_MIN, LA2016_NUM_SAMPLES_MAX);
523 case SR_CONF_VOLTAGE_THRESHOLD:
524 *data = std_gvar_min_max_step_thresholds(
525 LA2016_THR_VOLTAGE_MIN,
526 LA2016_THR_VOLTAGE_MAX, 0.1);
528 case SR_CONF_TRIGGER_MATCH:
529 *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
531 case SR_CONF_LOGIC_THRESHOLD:
532 *data = g_variant_new_strv(logic_threshold, MAX_NUM_LOGIC_THRESHOLD_ENTRIES);
541 static int configure_channels(const struct sr_dev_inst *sdi)
543 struct dev_context *devc;
546 devc->cur_channels = 0;
547 devc->num_channels = 0;
549 for (GSList *l = sdi->channels; l; l = l->next) {
550 struct sr_channel *ch = (struct sr_channel*)l->data;
551 if (ch->enabled == FALSE)
553 devc->cur_channels |= 1 << ch->index;
554 devc->num_channels++;
560 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
562 struct sr_dev_driver *di;
563 struct drv_context *drvc;
564 struct dev_context *devc;
571 if (configure_channels(sdi) != SR_OK) {
572 sr_err("Failed to configure channels.");
576 devc->convbuffer_size = 4 * 1024 * 1024;
577 if (!(devc->convbuffer = g_try_malloc(devc->convbuffer_size))) {
578 sr_err("Conversion buffer malloc failed.");
579 return SR_ERR_MALLOC;
582 if ((ret = la2016_setup_acquisition(sdi)) != SR_OK) {
583 g_free(devc->convbuffer);
584 devc->convbuffer = NULL;
588 devc->ctx = drvc->sr_ctx;
590 if ((ret = la2016_start_acquisition(sdi)) != SR_OK) {
591 la2016_abort_acquisition(sdi);
595 devc->have_trigger = 0;
596 usb_source_add(sdi->session, drvc->sr_ctx, 50,
597 la2016_receive_data, (void *)sdi);
599 std_session_send_df_header(sdi);
604 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
608 ret = la2016_abort_acquisition(sdi);
613 static struct sr_dev_driver kingst_la2016_driver_info = {
614 .name = "kingst-la2016",
615 .longname = "Kingst LA2016",
618 .cleanup = std_cleanup,
620 .dev_list = std_dev_list,
621 .dev_clear = std_dev_clear,
622 .config_get = config_get,
623 .config_set = config_set,
624 .config_list = config_list,
625 .dev_open = dev_open,
626 .dev_close = dev_close,
627 .dev_acquisition_start = dev_acquisition_start,
628 .dev_acquisition_stop = dev_acquisition_stop,
631 SR_REGISTER_DEV_DRIVER(kingst_la2016_driver_info);