]> sigrok.org Git - libsigrok.git/blob - src/hardware/kingst-la2016/api.c
kingst-la2016: rename logic channels to match product's labels
[libsigrok.git] / src / hardware / kingst-la2016 / api.c
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
23 /*
24  * This driver implementation initially was derived from the
25  * src/hardware/saleae-logic16/ source code.
26  */
27
28 #include <config.h>
29
30 #include <libsigrok/libsigrok.h>
31 #include <string.h>
32
33 #include "libsigrok-internal.h"
34 #include "protocol.h"
35
36 static const uint32_t scanopts[] = {
37         SR_CONF_CONN,
38 };
39
40 static const uint32_t drvopts[] = {
41         SR_CONF_LOGIC_ANALYZER,
42 };
43
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,
54 };
55
56 static const int32_t trigger_matches[] = {
57         SR_TRIGGER_ZERO,
58         SR_TRIGGER_ONE,
59         SR_TRIGGER_RISING,
60         SR_TRIGGER_FALLING,
61 };
62
63 static const char *channel_names[] = {
64         "CH0", "CH1", "CH2", "CH3", "CH4", "CH5", "CH6", "CH7",
65         "CH8", "CH9", "CH10", "CH11", "CH12", "CH13", "CH14", "CH15",
66 };
67
68 static const uint64_t samplerates_la2016[] = {
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
86 static 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
103 static 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
115 static 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 LOGIC_THRESHOLD_IDX_USER        (ARRAY_SIZE(logic_threshold) - 1)
129
130 static GSList *scan(struct sr_dev_driver *di, GSList *options)
131 {
132         struct drv_context *drvc;
133         struct sr_context *ctx;
134         struct dev_context *devc;
135         struct sr_dev_inst *sdi;
136         struct sr_usb_dev_inst *usb;
137         struct sr_config *src;
138         GSList *l;
139         GSList *devices;
140         GSList *conn_devices;
141         struct libusb_device_descriptor des;
142         libusb_device **devlist, *dev;
143         size_t dev_count, dev_idx, ch_idx;
144         uint8_t bus, addr;
145         const char *conn;
146         char conn_id[64];
147         uint64_t fw_uploaded;
148         int ret;
149
150         drvc = di->context;
151         ctx = drvc->sr_ctx;;
152
153         conn = NULL;
154         for (l = options; l; l = l->next) {
155                 src = l->data;
156                 switch (src->key) {
157                 case SR_CONF_CONN:
158                         conn = g_variant_get_string(src->data, NULL);
159                         break;
160                 }
161         }
162         if (conn)
163                 conn_devices = sr_usb_find(ctx->libusb_ctx, conn);
164         else
165                 conn_devices = NULL;
166
167         /* Find all LA2016 devices, optionally upload firmware to them. */
168         devices = NULL;
169         ret = libusb_get_device_list(ctx->libusb_ctx, &devlist);
170         if (ret < 0) {
171                 sr_err("Cannot get device list: %s.", libusb_error_name(ret));
172                 return devices;
173         }
174         dev_count = ret;
175         for (dev_idx = 0; dev_idx < dev_count; dev_idx++) {
176                 dev = devlist[dev_idx];
177                 bus = libusb_get_bus_number(dev);
178                 addr = libusb_get_device_address(dev);
179                 if (conn) {
180                         usb = NULL;
181                         for (l = conn_devices; l; l = l->next) {
182                                 usb = l->data;
183                                 if (usb->bus == bus && usb->address == addr)
184                                         break;
185                         }
186                         if (!l) {
187                                 /*
188                                  * A connection parameter was specified and
189                                  * this device does not match the filter.
190                                  */
191                                 continue;
192                         }
193                 }
194
195                 libusb_get_device_descriptor(dev, &des);
196                 ret = usb_get_port_path(dev, conn_id, sizeof(conn_id));
197                 if (ret < 0)
198                         continue;
199                 if (des.idVendor != LA2016_VID || des.idProduct != LA2016_PID)
200                         continue;
201
202                 /* USB identification matches, a device was found. */
203                 sr_dbg("Found a device (USB identification).");
204                 sdi = g_malloc0(sizeof(*sdi));
205                 sdi->status = SR_ST_INITIALIZING;
206                 sdi->connection_id = g_strdup(conn_id);
207
208                 fw_uploaded = 0;
209                 if (des.iProduct != LA2016_IPRODUCT_INDEX) {
210                         sr_info("Device at '%s' has no firmware loaded.",
211                                 conn_id);
212
213                         ret = la2016_upload_firmware(ctx, dev, des.idProduct);
214                         if (ret != SR_OK) {
215                                 sr_err("MCU firmware upload failed.");
216                                 g_free(sdi->connection_id);
217                                 g_free(sdi);
218                                 continue;
219                         }
220                         fw_uploaded = g_get_monotonic_time();
221                         /* Will re-enumerate. Mark as "unknown address yet". */
222                         addr = 0xff;
223                 }
224
225                 sdi->vendor = g_strdup("Kingst");
226                 sdi->model = g_strdup("LA2016");
227
228                 for (ch_idx = 0; ch_idx < ARRAY_SIZE(channel_names); ch_idx++) {
229                         sr_channel_new(sdi, ch_idx, SR_CHANNEL_LOGIC,
230                                 TRUE, channel_names[ch_idx]);
231                 }
232
233                 devices = g_slist_append(devices, sdi);
234
235                 devc = g_malloc0(sizeof(*devc));
236                 sdi->priv = devc;
237                 devc->fw_uploaded = fw_uploaded;
238                 devc->threshold_voltage_idx = 0;
239                 devc->threshold_voltage = logic_threshold_value[devc->threshold_voltage_idx];
240
241                 sdi->status = SR_ST_INACTIVE;
242                 sdi->inst_type = SR_INST_USB;
243
244                 sdi->conn = sr_usb_dev_inst_new(bus, addr, NULL);
245         }
246         libusb_free_device_list(devlist, 1);
247         g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free);
248
249         return std_scan_complete(di, devices);
250 }
251
252 static int la2016_dev_open(struct sr_dev_inst *sdi)
253 {
254         struct sr_dev_driver *di;
255         struct drv_context *drvc;
256         struct sr_context *ctx;
257         libusb_device **devlist, *dev;
258         struct sr_usb_dev_inst *usb;
259         struct libusb_device_descriptor des;
260         int ret;
261         size_t device_count, dev_idx;
262         gboolean check_conn;
263         char conn_id[64];
264
265         di = sdi->driver;
266         drvc = di->context;
267         ctx = drvc->sr_ctx;;
268         usb = sdi->conn;
269         ret = SR_ERR;
270
271         ret = libusb_get_device_list(ctx->libusb_ctx, &devlist);
272         if (ret < 0) {
273                 sr_err("Cannot get device list: %s.", libusb_error_name(ret));
274                 return SR_ERR;
275         }
276         device_count = ret;
277         if (!device_count) {
278                 sr_warn("Device list is empty. Cannot open.");
279                 return SR_ERR;
280         }
281         for (dev_idx = 0; dev_idx < device_count; dev_idx++) {
282                 dev = devlist[dev_idx];
283                 libusb_get_device_descriptor(dev, &des);
284
285                 if (des.idVendor != LA2016_VID || des.idProduct != LA2016_PID)
286                         continue;
287                 if (des.iProduct != LA2016_IPRODUCT_INDEX)
288                         continue;
289
290                 check_conn = sdi->status == SR_ST_INITIALIZING;
291                 check_conn |= sdi->status == SR_ST_INACTIVE;
292                 if (check_conn) {
293                         /* Check physical USB bus/port address. */
294                         ret = usb_get_port_path(dev, conn_id, sizeof(conn_id));
295                         if (ret < 0)
296                                 continue;
297                         if (strcmp(sdi->connection_id, conn_id) != 0) {
298                                 /* Not the device we looked up before. */
299                                 continue;
300                         }
301                 }
302
303                 ret = libusb_open(dev, &usb->devhdl);
304                 if (ret != 0) {
305                         sr_err("Cannot open device: %s.",
306                                 libusb_error_name(ret));
307                         ret = SR_ERR_IO;
308                         break;
309                 }
310
311                 if (usb->address == 0xff) {
312                         /*
313                          * First encounter after firmware upload.
314                          * Grab current address after enumeration.
315                          */
316                         usb->address = libusb_get_device_address(dev);
317                 }
318
319                 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
320                 if (ret == LIBUSB_ERROR_BUSY) {
321                         sr_err("Cannot claim USB interface. Another program or driver using it?");
322                         ret = SR_ERR;
323                         break;
324                 } else if (ret == LIBUSB_ERROR_NO_DEVICE) {
325                         sr_err("Device has been disconnected.");
326                         ret = SR_ERR;
327                         break;
328                 } else if (ret != 0) {
329                         sr_err("Cannot claim USB interface: %s.",
330                                 libusb_error_name(ret));
331                         ret = SR_ERR;
332                         break;
333                 }
334
335                 if ((ret = la2016_init_device(sdi)) != SR_OK) {
336                         sr_err("Cannot initialize device.");
337                         break;
338                 }
339
340                 sr_info("Opened device on %d.%d (logical) / %s (physical), interface %d.",
341                         usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
342                 ret = SR_OK;
343                 break;
344         }
345         libusb_free_device_list(devlist, 1);
346
347         if (ret != SR_OK) {
348                 if (usb->devhdl) {
349                         libusb_release_interface(usb->devhdl, USB_INTERFACE);
350                         libusb_close(usb->devhdl);
351                         usb->devhdl = NULL;
352                 }
353                 return ret;
354         }
355
356         return SR_OK;
357 }
358
359 static int dev_open(struct sr_dev_inst *sdi)
360 {
361         struct dev_context *devc;
362         uint64_t reset_done, now, elapsed_ms;
363         int ret;
364
365         devc = sdi->priv;
366
367         /*
368          * When the sigrok driver recently has uploaded MCU firmware,
369          * then wait for the FX2 to re-enumerate. Allow the USB device
370          * to vanish before it reappears. Timeouts are rough estimates
371          * after all, the imprecise time of the last check (potentially
372          * executes after the total check period) simplifies code paths
373          * with optional diagnostics. And increases the probability of
374          * successfully detecting "late/slow" devices.
375          */
376         if (devc->fw_uploaded) {
377                 sr_info("Waiting for device to reset after firmware upload.");
378                 now = g_get_monotonic_time();
379                 reset_done = devc->fw_uploaded + RENUM_GONE_DELAY_MS * 1000;
380                 if (now < reset_done)
381                         g_usleep(reset_done - now);
382                 do {
383                         now = g_get_monotonic_time();
384                         elapsed_ms = (now - devc->fw_uploaded) / 1000;
385                         sr_spew("Waited %" PRIu64 "ms.", elapsed_ms);
386                         ret = la2016_dev_open(sdi);
387                         if (ret == SR_OK) {
388                                 devc->fw_uploaded = 0;
389                                 break;
390                         }
391                         g_usleep(RENUM_POLL_INTERVAL_MS * 1000);
392                 } while (elapsed_ms < RENUM_CHECK_PERIOD_MS);
393                 if (ret != SR_OK) {
394                         sr_err("Device failed to re-enumerate.");
395                         return ret;
396                 }
397                 sr_info("Device came back after %" PRIi64 "ms.", elapsed_ms);
398         } else {
399                 ret = la2016_dev_open(sdi);
400         }
401
402         if (ret != SR_OK) {
403                 sr_err("Cannot open device.");
404                 return ret;
405         }
406
407         return SR_OK;
408 }
409
410 static int dev_close(struct sr_dev_inst *sdi)
411 {
412         struct sr_usb_dev_inst *usb;
413
414         usb = sdi->conn;
415
416         if (!usb->devhdl)
417                 return SR_ERR_BUG;
418
419         la2016_deinit_device(sdi);
420
421         sr_info("Closing device on %d.%d (logical) / %s (physical) interface %d.",
422                 usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
423         libusb_release_interface(usb->devhdl, USB_INTERFACE);
424         libusb_close(usb->devhdl);
425         usb->devhdl = NULL;
426
427         return SR_OK;
428 }
429
430 static int config_get(uint32_t key, GVariant **data,
431         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
432 {
433         struct dev_context *devc;
434         struct sr_usb_dev_inst *usb;
435         double rounded;
436         const char *label;
437
438         (void)cg;
439
440         if (!sdi)
441                 return SR_ERR_ARG;
442         devc = sdi->priv;
443
444         switch (key) {
445         case SR_CONF_CONN:
446                 if (!sdi->conn)
447                         return SR_ERR_ARG;
448                 usb = sdi->conn;
449                 if (usb->address == 0xff) {
450                         /*
451                          * Device still needs to re-enumerate after firmware
452                          * upload, so we don't know its (future) address.
453                          */
454                         return SR_ERR;
455                 }
456                 *data = g_variant_new_printf("%d.%d", usb->bus, usb->address);
457                 break;
458         case SR_CONF_SAMPLERATE:
459                 *data = g_variant_new_uint64(devc->cur_samplerate);
460                 break;
461         case SR_CONF_LIMIT_SAMPLES:
462                 *data = g_variant_new_uint64(devc->limit_samples);
463                 break;
464         case SR_CONF_CAPTURE_RATIO:
465                 *data = g_variant_new_uint64(devc->capture_ratio);
466                 break;
467         case SR_CONF_VOLTAGE_THRESHOLD:
468                 rounded = (int)(devc->threshold_voltage / 0.1) * 0.1;
469                 *data = std_gvar_tuple_double(rounded, rounded + 0.1);
470                 return SR_OK;
471         case SR_CONF_LOGIC_THRESHOLD:
472                 label = logic_threshold[devc->threshold_voltage_idx];
473                 *data = g_variant_new_string(label);
474                 break;
475         case SR_CONF_LOGIC_THRESHOLD_CUSTOM:
476                 *data = g_variant_new_double(devc->threshold_voltage);
477                 break;
478
479         default:
480                 return SR_ERR_NA;
481         }
482
483         return SR_OK;
484 }
485
486 static int config_set(uint32_t key, GVariant *data,
487         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
488 {
489         struct dev_context *devc;
490         double low, high;
491         int idx;
492
493         (void)cg;
494
495         devc = sdi->priv;
496
497         switch (key) {
498         case SR_CONF_SAMPLERATE:
499                 devc->cur_samplerate = g_variant_get_uint64(data);
500                 break;
501         case SR_CONF_LIMIT_SAMPLES:
502                 devc->limit_samples = g_variant_get_uint64(data);
503                 break;
504         case SR_CONF_CAPTURE_RATIO:
505                 devc->capture_ratio = g_variant_get_uint64(data);
506                 break;
507         case SR_CONF_VOLTAGE_THRESHOLD:
508                 g_variant_get(data, "(dd)", &low, &high);
509                 devc->threshold_voltage = (low + high) / 2.0;
510                 devc->threshold_voltage_idx = LOGIC_THRESHOLD_IDX_USER;
511                 break;
512         case SR_CONF_LOGIC_THRESHOLD: {
513                 idx = std_str_idx(data, ARRAY_AND_SIZE(logic_threshold));
514                 if (idx < 0)
515                         return SR_ERR_ARG;
516                 if (idx != LOGIC_THRESHOLD_IDX_USER) {
517                         devc->threshold_voltage = logic_threshold_value[idx];
518                 }
519                 devc->threshold_voltage_idx = idx;
520                 break;
521         }
522         case SR_CONF_LOGIC_THRESHOLD_CUSTOM:
523                 devc->threshold_voltage = g_variant_get_double(data);
524                 break;
525         default:
526                 return SR_ERR_NA;
527         }
528
529         return SR_OK;
530 }
531
532 static int config_list(uint32_t key, GVariant **data,
533         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
534 {
535         struct dev_context *devc;
536
537         switch (key) {
538         case SR_CONF_SCAN_OPTIONS:
539         case SR_CONF_DEVICE_OPTIONS:
540                 return STD_CONFIG_LIST(key, data, sdi, cg,
541                         scanopts, drvopts, devopts);
542         case SR_CONF_SAMPLERATE:
543                 if (!sdi)
544                         return SR_ERR_ARG;
545                 devc = sdi->priv;
546                 if (devc->max_samplerate == SR_MHZ(200)) {
547                         *data = std_gvar_samplerates(ARRAY_AND_SIZE(samplerates_la2016));
548                 } else {
549                         *data = std_gvar_samplerates(ARRAY_AND_SIZE(samplerates_la1016));
550                 }
551                 break;
552         case SR_CONF_LIMIT_SAMPLES:
553                 *data = std_gvar_tuple_u64(LA2016_NUM_SAMPLES_MIN,
554                         LA2016_NUM_SAMPLES_MAX);
555                 break;
556         case SR_CONF_VOLTAGE_THRESHOLD:
557                 *data = std_gvar_min_max_step_thresholds(
558                         LA2016_THR_VOLTAGE_MIN,
559                         LA2016_THR_VOLTAGE_MAX, 0.1);
560                 break;
561         case SR_CONF_TRIGGER_MATCH:
562                 *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
563                 break;
564         case SR_CONF_LOGIC_THRESHOLD:
565                 *data = g_variant_new_strv(ARRAY_AND_SIZE(logic_threshold));
566                 break;
567         default:
568                 return SR_ERR_NA;
569         }
570
571         return SR_OK;
572 }
573
574 static int configure_channels(const struct sr_dev_inst *sdi)
575 {
576         struct dev_context *devc;
577         GSList *l;
578         struct sr_channel *ch;
579
580         devc = sdi->priv;
581
582         devc->cur_channels = 0;
583         for (l = sdi->channels; l; l = l->next) {
584                 ch = l->data;
585                 if (!ch->enabled)
586                         continue;
587                 devc->cur_channels |= 1UL << ch->index;
588         }
589
590         return SR_OK;
591 }
592
593 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
594 {
595         struct sr_dev_driver *di;
596         struct drv_context *drvc;
597         struct sr_context *ctx;
598         struct dev_context *devc;
599         int ret;
600
601         di = sdi->driver;
602         drvc = di->context;
603         ctx = drvc->sr_ctx;;
604         devc = sdi->priv;
605
606         if (configure_channels(sdi) != SR_OK) {
607                 sr_err("Cannot configure channels.");
608                 return SR_ERR;
609         }
610
611         devc->convbuffer_size = LA2016_CONVBUFFER_SIZE;
612         devc->convbuffer = g_try_malloc(devc->convbuffer_size);
613         if (!devc->convbuffer) {
614                 sr_err("Cannot allocate conversion buffer.");
615                 return SR_ERR_MALLOC;
616         }
617
618         ret = la2016_setup_acquisition(sdi);
619         if (ret != SR_OK) {
620                 g_free(devc->convbuffer);
621                 devc->convbuffer = NULL;
622                 return ret;
623         }
624
625         ret = la2016_start_acquisition(sdi);
626         if (ret != SR_OK) {
627                 la2016_abort_acquisition(sdi);
628                 g_free(devc->convbuffer);
629                 devc->convbuffer = NULL;
630                 return ret;
631         }
632
633         devc->completion_seen = FALSE;
634         usb_source_add(sdi->session, ctx, 50,
635                 la2016_receive_data, (void *)sdi);
636
637         std_session_send_df_header(sdi);
638
639         return SR_OK;
640 }
641
642 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
643 {
644         int ret;
645
646         ret = la2016_abort_acquisition(sdi);
647
648         return ret;
649 }
650
651 static struct sr_dev_driver kingst_la2016_driver_info = {
652         .name = "kingst-la2016",
653         .longname = "Kingst LA2016",
654         .api_version = 1,
655         .init = std_init,
656         .cleanup = std_cleanup,
657         .scan = scan,
658         .dev_list = std_dev_list,
659         .dev_clear = std_dev_clear,
660         .config_get = config_get,
661         .config_set = config_set,
662         .config_list = config_list,
663         .dev_open = dev_open,
664         .dev_close = dev_close,
665         .dev_acquisition_start = dev_acquisition_start,
666         .dev_acquisition_stop = dev_acquisition_stop,
667         .context = NULL,
668 };
669 SR_REGISTER_DEV_DRIVER(kingst_la2016_driver_info);