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/ */
31 #include <libsigrok/libsigrok.h>
32 #include "libsigrok-internal.h"
35 static const uint32_t scanopts[] = {
39 static const uint32_t drvopts[] = {
40 SR_CONF_LOGIC_ANALYZER,
43 static const uint32_t devopts[] = {
44 /* TODO: SR_CONF_CONTINUOUS, */
45 SR_CONF_CONN | SR_CONF_GET,
46 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
47 SR_CONF_LIMIT_SAMPLES | SR_CONF_SET | SR_CONF_GET | SR_CONF_LIST,
48 SR_CONF_VOLTAGE_THRESHOLD | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
49 SR_CONF_LOGIC_THRESHOLD | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
50 SR_CONF_LOGIC_THRESHOLD_CUSTOM | SR_CONF_GET | SR_CONF_SET,
51 SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
52 SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET,
55 static const int32_t trigger_matches[] = {
62 static const char *channel_names[] = {
63 "0", "1", "2", "3", "4", "5", "6", "7",
64 "8", "9", "10", "11", "12", "13", "14", "15",
67 static const uint64_t samplerates_la2016[] = {
85 static const uint64_t samplerates_la1016[] = {
102 static const float logic_threshold_value[] = {
114 static const char *logic_threshold[] = {
127 #define MAX_NUM_LOGIC_THRESHOLD_ENTRIES ARRAY_SIZE(logic_threshold)
129 static GSList *scan(struct sr_dev_driver *di, GSList *options)
131 struct drv_context *drvc;
132 struct dev_context *devc;
133 struct sr_dev_inst *sdi;
134 struct sr_usb_dev_inst *usb;
135 struct sr_config *src;
138 GSList *conn_devices;
139 struct libusb_device_descriptor des;
140 libusb_device **devlist;
143 char connection_id[64];
145 unsigned int dev_addr;
150 for (l = options; l; l = l->next) {
154 conn = g_variant_get_string(src->data, NULL);
159 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
163 /* Find all LA2016 devices and upload firmware to them. */
165 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
166 for (i = 0; devlist[i]; i++) {
169 for (l = conn_devices; l; l = l->next) {
171 if (usb->bus == libusb_get_bus_number(devlist[i]) &&
172 usb->address == libusb_get_device_address(devlist[i]))
176 /* This device matched none of the ones that
177 * matched the conn specification. */
182 libusb_get_device_descriptor(devlist[i], &des);
184 if (usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)) < 0)
187 if (des.idVendor != LA2016_VID || des.idProduct != LA2016_PID)
190 /* Already has the firmware */
191 sr_dbg("Found a LA2016 device.");
192 sdi = g_malloc0(sizeof(struct sr_dev_inst));
193 sdi->status = SR_ST_INITIALIZING;
194 sdi->connection_id = g_strdup(connection_id);
197 dev_addr = libusb_get_device_address(devlist[i]);
198 if (des.iProduct != 2) {
199 sr_info("device at '%s' has no firmware loaded!", connection_id);
201 if (la2016_upload_firmware(drvc->sr_ctx, devlist[i], des.idProduct) != SR_OK) {
202 sr_err("uC firmware upload failed!");
203 g_free(sdi->connection_id);
207 fw_updated = g_get_monotonic_time();
208 dev_addr = 0xff; /* to mark that we don't know address yet... ugly */
211 sdi->vendor = g_strdup("Kingst");
212 sdi->model = g_strdup("LA2016");
214 for (j = 0; j < ARRAY_SIZE(channel_names); j++)
215 sr_channel_new(sdi, j, SR_CHANNEL_LOGIC, TRUE, channel_names[j]);
217 devices = g_slist_append(devices, sdi);
219 devc = g_malloc0(sizeof(struct dev_context));
221 devc->fw_updated = fw_updated;
222 devc->threshold_voltage_idx = 0;
223 devc->threshold_voltage = logic_threshold_value[devc->threshold_voltage_idx];
225 sdi->status = SR_ST_INACTIVE;
226 sdi->inst_type = SR_INST_USB;
228 sdi->conn = sr_usb_dev_inst_new(
229 libusb_get_bus_number(devlist[i]),
232 libusb_free_device_list(devlist, 1);
233 g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free);
235 return std_scan_complete(di, devices);
238 static int la2016_dev_open(struct sr_dev_inst *sdi)
240 struct sr_dev_driver *di;
241 libusb_device **devlist;
242 struct sr_usb_dev_inst *usb;
243 struct libusb_device_descriptor des;
244 struct drv_context *drvc;
245 int ret, i, device_count;
246 char connection_id[64];
253 device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
254 if (device_count < 0) {
255 sr_err("Failed to get device list: %s.", libusb_error_name(device_count));
259 for (i = 0; i < device_count; i++) {
260 libusb_get_device_descriptor(devlist[i], &des);
262 if (des.idVendor != LA2016_VID || des.idProduct != LA2016_PID || des.iProduct != 2)
265 if ((sdi->status == SR_ST_INITIALIZING) || (sdi->status == SR_ST_INACTIVE)) {
267 * Check device by its physical USB bus/port address.
269 if (usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)) < 0)
272 if (strcmp(sdi->connection_id, connection_id))
273 /* This is not the one. */
277 if (!(ret = libusb_open(devlist[i], &usb->devhdl))) {
278 if (usb->address == 0xff)
280 * First time we touch this device after FW
281 * upload, so we don't know the address yet.
283 usb->address = libusb_get_device_address(devlist[i]);
285 sr_err("Failed to open device: %s.", libusb_error_name(ret));
290 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
291 if (ret == LIBUSB_ERROR_BUSY) {
292 sr_err("Unable to claim USB interface. Another "
293 "program or driver has already claimed it.");
296 } else if (ret == LIBUSB_ERROR_NO_DEVICE) {
297 sr_err("Device has been disconnected.");
300 } else if (ret != 0) {
301 sr_err("Unable to claim interface: %s.", libusb_error_name(ret));
306 if ((ret = la2016_init_device(sdi)) != SR_OK) {
307 sr_err("Failed to init device.");
311 sr_info("Opened device on %d.%d (logical) / %s (physical), interface %d.",
312 usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
319 libusb_free_device_list(devlist, 1);
323 libusb_release_interface(usb->devhdl, USB_INTERFACE);
324 libusb_close(usb->devhdl);
333 static int dev_open(struct sr_dev_inst *sdi)
335 struct dev_context *devc;
336 int64_t timediff_us, timediff_ms;
344 * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS
345 * milliseconds for the FX2 to renumerate.
348 if (devc->fw_updated > 0) {
349 sr_info("Waiting for device to reset after firmware upload.");
350 /* Takes >= 2000ms for the uC to be gone from the USB bus. */
351 reset_done = devc->fw_updated + 18 * (uint64_t)1e5; /* 1.8 seconds */
352 now = g_get_monotonic_time();
353 if (reset_done > now)
354 g_usleep(reset_done - now);
356 while (timediff_ms < MAX_RENUM_DELAY_MS) {
357 g_usleep(200 * 1000);
359 timediff_us = g_get_monotonic_time() - devc->fw_updated;
360 timediff_ms = timediff_us / 1000;
362 if ((ret = la2016_dev_open(sdi)) == SR_OK)
364 sr_spew("Waited %" PRIi64 "ms.", timediff_ms);
367 sr_err("Device failed to re-enumerate.");
370 sr_info("Device came back after %" PRIi64 "ms.", timediff_ms);
372 ret = la2016_dev_open(sdi);
376 sr_err("Unable to open device.");
383 static int dev_close(struct sr_dev_inst *sdi)
385 struct sr_usb_dev_inst *usb;
392 la2016_deinit_device(sdi);
394 sr_info("Closing device on %d.%d (logical) / %s (physical) interface %d.",
395 usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
396 libusb_release_interface(usb->devhdl, USB_INTERFACE);
397 libusb_close(usb->devhdl);
403 static int config_get(uint32_t key, GVariant **data,
404 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
406 struct dev_context *devc;
407 struct sr_usb_dev_inst *usb;
421 if (usb->address == 255) {
422 /* Device still needs to re-enumerate after firmware
423 * upload, so we don't know its (future) address. */
426 *data = g_variant_new_printf("%d.%d", usb->bus, usb->address);
428 case SR_CONF_SAMPLERATE:
429 *data = g_variant_new_uint64(devc->cur_samplerate);
431 case SR_CONF_LIMIT_SAMPLES:
432 *data = g_variant_new_uint64(devc->limit_samples);
434 case SR_CONF_CAPTURE_RATIO:
435 *data = g_variant_new_uint64(devc->capture_ratio);
437 case SR_CONF_VOLTAGE_THRESHOLD:
438 rounded = (int)(devc->threshold_voltage / 0.1) * 0.1;
439 *data = std_gvar_tuple_double(rounded, rounded + 0.1);
441 case SR_CONF_LOGIC_THRESHOLD:
442 *data = g_variant_new_string(logic_threshold[devc->threshold_voltage_idx]);
444 case SR_CONF_LOGIC_THRESHOLD_CUSTOM:
445 *data = g_variant_new_double(devc->threshold_voltage);
455 static int config_set(uint32_t key, GVariant *data,
456 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
458 struct dev_context *devc;
467 case SR_CONF_SAMPLERATE:
468 devc->cur_samplerate = g_variant_get_uint64(data);
470 case SR_CONF_LIMIT_SAMPLES:
471 devc->limit_samples = g_variant_get_uint64(data);
473 case SR_CONF_CAPTURE_RATIO:
474 devc->capture_ratio = g_variant_get_uint64(data);
476 case SR_CONF_VOLTAGE_THRESHOLD:
477 g_variant_get(data, "(dd)", &low, &high);
478 devc->threshold_voltage = (low + high) / 2.0;
479 devc->threshold_voltage_idx = MAX_NUM_LOGIC_THRESHOLD_ENTRIES - 1; /* USER */
481 case SR_CONF_LOGIC_THRESHOLD: {
482 if ((idx = std_str_idx(data, logic_threshold, MAX_NUM_LOGIC_THRESHOLD_ENTRIES)) < 0)
484 if (idx == MAX_NUM_LOGIC_THRESHOLD_ENTRIES - 1) {
487 devc->threshold_voltage = logic_threshold_value[idx];
489 devc->threshold_voltage_idx = idx;
492 case SR_CONF_LOGIC_THRESHOLD_CUSTOM:
493 devc->threshold_voltage = g_variant_get_double(data);
502 static int config_list(uint32_t key, GVariant **data,
503 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
505 struct dev_context *devc;
508 case SR_CONF_SCAN_OPTIONS:
509 case SR_CONF_DEVICE_OPTIONS:
510 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
511 case SR_CONF_SAMPLERATE:
515 if (devc->max_samplerate == SR_MHZ(200)) {
516 *data = std_gvar_samplerates(ARRAY_AND_SIZE(samplerates_la2016));
519 *data = std_gvar_samplerates(ARRAY_AND_SIZE(samplerates_la1016));
522 case SR_CONF_LIMIT_SAMPLES:
523 *data = std_gvar_tuple_u64(LA2016_NUM_SAMPLES_MIN, LA2016_NUM_SAMPLES_MAX);
525 case SR_CONF_VOLTAGE_THRESHOLD:
526 *data = std_gvar_min_max_step_thresholds(
527 LA2016_THR_VOLTAGE_MIN,
528 LA2016_THR_VOLTAGE_MAX, 0.1);
530 case SR_CONF_TRIGGER_MATCH:
531 *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
533 case SR_CONF_LOGIC_THRESHOLD:
534 *data = g_variant_new_strv(logic_threshold, MAX_NUM_LOGIC_THRESHOLD_ENTRIES);
543 static int configure_channels(const struct sr_dev_inst *sdi)
545 struct dev_context *devc;
548 devc->cur_channels = 0;
549 devc->num_channels = 0;
551 for (GSList *l = sdi->channels; l; l = l->next) {
552 struct sr_channel *ch = (struct sr_channel*)l->data;
553 if (ch->enabled == FALSE)
555 devc->cur_channels |= 1 << ch->index;
556 devc->num_channels++;
562 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
564 struct sr_dev_driver *di;
565 struct drv_context *drvc;
566 struct dev_context *devc;
573 if (configure_channels(sdi) != SR_OK) {
574 sr_err("Failed to configure channels.");
578 devc->convbuffer_size = 4 * 1024 * 1024;
579 if (!(devc->convbuffer = g_try_malloc(devc->convbuffer_size))) {
580 sr_err("Conversion buffer malloc failed.");
581 return SR_ERR_MALLOC;
584 if ((ret = la2016_setup_acquisition(sdi)) != SR_OK) {
585 g_free(devc->convbuffer);
586 devc->convbuffer = NULL;
590 devc->ctx = drvc->sr_ctx;
592 if ((ret = la2016_start_acquisition(sdi)) != SR_OK) {
593 la2016_abort_acquisition(sdi);
597 devc->have_trigger = 0;
598 usb_source_add(sdi->session, drvc->sr_ctx, 50,
599 la2016_receive_data, (void *)sdi);
601 std_session_send_df_header(sdi);
606 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
610 ret = la2016_abort_acquisition(sdi);
615 static struct sr_dev_driver kingst_la2016_driver_info = {
616 .name = "kingst-la2016",
617 .longname = "Kingst LA2016",
620 .cleanup = std_cleanup,
622 .dev_list = std_dev_list,
623 .dev_clear = std_dev_clear,
624 .config_get = config_get,
625 .config_set = config_set,
626 .config_list = config_list,
627 .dev_open = dev_open,
628 .dev_close = dev_close,
629 .dev_acquisition_start = dev_acquisition_start,
630 .dev_acquisition_stop = dev_acquisition_stop,
633 SR_REGISTER_DEV_DRIVER(kingst_la2016_driver_info);