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