]> sigrok.org Git - libsigrok.git/blob - src/hardware/kingst-la2016/api.c
kingst-la2016: rephrase diagnostics to improve user perception
[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 /* mostly stolen from src/hardware/saleae-logic16/ */
24
25 #include <config.h>
26
27 #include <libsigrok/libsigrok.h>
28 #include <string.h>
29
30 #include "libsigrok-internal.h"
31 #include "protocol.h"
32
33 static const uint32_t scanopts[] = {
34         SR_CONF_CONN,
35 };
36
37 static const uint32_t drvopts[] = {
38         SR_CONF_LOGIC_ANALYZER,
39 };
40
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,
51 };
52
53 static const int32_t trigger_matches[] = {
54         SR_TRIGGER_ZERO,
55         SR_TRIGGER_ONE,
56         SR_TRIGGER_RISING,
57         SR_TRIGGER_FALLING,
58 };
59
60 static const char *channel_names[] = {
61         "0", "1", "2", "3", "4", "5", "6", "7",
62         "8", "9", "10", "11", "12", "13", "14", "15",
63 };
64
65 static const uint64_t samplerates_la2016[] = {
66         SR_KHZ(20),
67         SR_KHZ(50),
68         SR_KHZ(100),
69         SR_KHZ(200),
70         SR_KHZ(500),
71         SR_MHZ(1),
72         SR_MHZ(2),
73         SR_MHZ(4),
74         SR_MHZ(5),
75         SR_MHZ(8),
76         SR_MHZ(10),
77         SR_MHZ(20),
78         SR_MHZ(50),
79         SR_MHZ(100),
80         SR_MHZ(200),
81 };
82
83 static const uint64_t samplerates_la1016[] = {
84         SR_KHZ(20),
85         SR_KHZ(50),
86         SR_KHZ(100),
87         SR_KHZ(200),
88         SR_KHZ(500),
89         SR_MHZ(1),
90         SR_MHZ(2),
91         SR_MHZ(4),
92         SR_MHZ(5),
93         SR_MHZ(8),
94         SR_MHZ(10),
95         SR_MHZ(20),
96         SR_MHZ(50),
97         SR_MHZ(100),
98 };
99
100 static const float logic_threshold_value[] = {
101         1.58,
102         2.5,
103         1.165,
104         1.5,
105         1.25,
106         0.9,
107         0.75,
108         0.60,
109         0.45,
110 };
111
112 static const char *logic_threshold[] = {
113         "TTL 5V",
114         "CMOS 5V",
115         "CMOS 3.3V",
116         "CMOS 3.0V",
117         "CMOS 2.5V",
118         "CMOS 1.8V",
119         "CMOS 1.5V",
120         "CMOS 1.2V",
121         "CMOS 0.9V",
122         "USER",
123 };
124
125 #define MAX_NUM_LOGIC_THRESHOLD_ENTRIES ARRAY_SIZE(logic_threshold)
126
127 static GSList *scan(struct sr_dev_driver *di, GSList *options)
128 {
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;
134         GSList *l;
135         GSList *devices;
136         GSList *conn_devices;
137         struct libusb_device_descriptor des;
138         libusb_device **devlist;
139         unsigned int i, j;
140         const char *conn;
141         char connection_id[64];
142         int64_t fw_updated;
143         unsigned int dev_addr;
144
145         drvc = di->context;
146
147         conn = NULL;
148         for (l = options; l; l = l->next) {
149                 src = l->data;
150                 switch (src->key) {
151                 case SR_CONF_CONN:
152                         conn = g_variant_get_string(src->data, NULL);
153                         break;
154                 }
155         }
156         if (conn)
157                 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
158         else
159                 conn_devices = NULL;
160
161         /* Find all LA2016 devices and upload firmware to them. */
162         devices = NULL;
163         libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
164         for (i = 0; devlist[i]; i++) {
165                 if (conn) {
166                         usb = NULL;
167                         for (l = conn_devices; l; l = l->next) {
168                                 usb = l->data;
169                                 if (usb->bus == libusb_get_bus_number(devlist[i]) &&
170                                     usb->address == libusb_get_device_address(devlist[i]))
171                                         break;
172                         }
173                         if (!l) {
174                                 /* This device matched none of the ones that
175                                  * matched the conn specification. */
176                                 continue;
177                         }
178                 }
179
180                 libusb_get_device_descriptor(devlist[i], &des);
181
182                 if (usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)) < 0)
183                         continue;
184
185                 if (des.idVendor != LA2016_VID || des.idProduct != LA2016_PID)
186                         continue;
187
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);
193
194                 fw_updated = 0;
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);
198
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);
202                                 g_free(sdi);
203                                 continue;
204                         }
205                         fw_updated = g_get_monotonic_time();
206                         dev_addr = 0xff; /* to mark that we don't know address yet... ugly */
207                 }
208
209                 sdi->vendor = g_strdup("Kingst");
210                 sdi->model = g_strdup("LA2016");
211
212                 for (j = 0; j < ARRAY_SIZE(channel_names); j++)
213                         sr_channel_new(sdi, j, SR_CHANNEL_LOGIC, TRUE, channel_names[j]);
214
215                 devices = g_slist_append(devices, sdi);
216
217                 devc = g_malloc0(sizeof(struct dev_context));
218                 sdi->priv = devc;
219                 devc->fw_updated = fw_updated;
220                 devc->threshold_voltage_idx = 0;
221                 devc->threshold_voltage = logic_threshold_value[devc->threshold_voltage_idx];
222
223                 sdi->status = SR_ST_INACTIVE;
224                 sdi->inst_type = SR_INST_USB;
225
226                 sdi->conn = sr_usb_dev_inst_new(
227                         libusb_get_bus_number(devlist[i]),
228                         dev_addr, NULL);
229         }
230         libusb_free_device_list(devlist, 1);
231         g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free);
232
233         return std_scan_complete(di, devices);
234 }
235
236 static int la2016_dev_open(struct sr_dev_inst *sdi)
237 {
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];
245
246         di = sdi->driver;
247         drvc = di->context;
248         usb = sdi->conn;
249         ret = SR_ERR;
250
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));
254                 return SR_ERR;
255         }
256
257         for (i = 0; i < device_count; i++) {
258                 libusb_get_device_descriptor(devlist[i], &des);
259
260                 if (des.idVendor != LA2016_VID || des.idProduct != LA2016_PID || des.iProduct != 2)
261                         continue;
262
263                 if ((sdi->status == SR_ST_INITIALIZING) || (sdi->status == SR_ST_INACTIVE)) {
264                         /*
265                          * Check device by its physical USB bus/port address.
266                          */
267                         if (usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)) < 0)
268                                 continue;
269
270                         if (strcmp(sdi->connection_id, connection_id))
271                                 /* This is not the one. */
272                                 continue;
273                 }
274
275                 if (!(ret = libusb_open(devlist[i], &usb->devhdl))) {
276                         if (usb->address == 0xff)
277                                 /*
278                                  * First time we touch this device after FW
279                                  * upload, so we don't know the address yet.
280                                  */
281                                 usb->address = libusb_get_device_address(devlist[i]);
282                 } else {
283                         sr_err("Failed to open device: %s.", libusb_error_name(ret));
284                         ret = SR_ERR;
285                         break;
286                 }
287
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?");
291                         ret = SR_ERR;
292                         break;
293                 } else if (ret == LIBUSB_ERROR_NO_DEVICE) {
294                         sr_err("Device has been disconnected.");
295                         ret = SR_ERR;
296                         break;
297                 } else if (ret != 0) {
298                         sr_err("Cannot claim USB interface: %s.", libusb_error_name(ret));
299                         ret = SR_ERR;
300                         break;
301                 }
302
303                 if ((ret = la2016_init_device(sdi)) != SR_OK) {
304                         sr_err("Cannot initialize device.");
305                         break;
306                 }
307
308                 sr_info("Opened device on %d.%d (logical) / %s (physical), interface %d.",
309                         usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
310
311                 ret = SR_OK;
312
313                 break;
314         }
315
316         libusb_free_device_list(devlist, 1);
317
318         if (ret != SR_OK) {
319                 if (usb->devhdl) {
320                         libusb_release_interface(usb->devhdl, USB_INTERFACE);
321                         libusb_close(usb->devhdl);
322                         usb->devhdl = NULL;
323                 }
324                 return SR_ERR;
325         }
326
327         return SR_OK;
328 }
329
330 static int dev_open(struct sr_dev_inst *sdi)
331 {
332         struct dev_context *devc;
333         int64_t timediff_us, timediff_ms;
334         uint64_t reset_done;
335         uint64_t now;
336         int ret;
337
338         devc = sdi->priv;
339
340         /*
341          * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS
342          * milliseconds for the FX2 to renumerate.
343          */
344         ret = SR_ERR;
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);
352                 timediff_ms = 0;
353                 while (timediff_ms < MAX_RENUM_DELAY_MS) {
354                         g_usleep(200 * 1000);
355
356                         timediff_us = g_get_monotonic_time() - devc->fw_updated;
357                         timediff_ms = timediff_us / 1000;
358
359                         if ((ret = la2016_dev_open(sdi)) == SR_OK)
360                                 break;
361                         sr_spew("Waited %" PRIi64 "ms.", timediff_ms);
362                 }
363                 if (ret != SR_OK) {
364                         sr_err("Device failed to re-enumerate.");
365                         return SR_ERR;
366                 }
367                 sr_info("Device came back after %" PRIi64 "ms.", timediff_ms);
368         } else {
369                 ret = la2016_dev_open(sdi);
370         }
371
372         if (ret != SR_OK) {
373                 sr_err("Cannot open device.");
374                 return SR_ERR;
375         }
376
377         return SR_OK;
378 }
379
380 static int dev_close(struct sr_dev_inst *sdi)
381 {
382         struct sr_usb_dev_inst *usb;
383
384         usb = sdi->conn;
385
386         if (!usb->devhdl)
387                 return SR_ERR_BUG;
388
389         la2016_deinit_device(sdi);
390
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);
395         usb->devhdl = NULL;
396
397         return SR_OK;
398 }
399
400 static int config_get(uint32_t key, GVariant **data,
401         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
402 {
403         struct dev_context *devc;
404         struct sr_usb_dev_inst *usb;
405         double rounded;
406
407         (void)cg;
408
409         if (!sdi)
410                 return SR_ERR_ARG;
411         devc = sdi->priv;
412
413         switch (key) {
414         case SR_CONF_CONN:
415                 if (!sdi->conn)
416                         return SR_ERR_ARG;
417                 usb = sdi->conn;
418                 if (usb->address == 255) {
419                         /* Device still needs to re-enumerate after firmware
420                          * upload, so we don't know its (future) address. */
421                         return SR_ERR;
422                 }
423                 *data = g_variant_new_printf("%d.%d", usb->bus, usb->address);
424                 break;
425         case SR_CONF_SAMPLERATE:
426                 *data = g_variant_new_uint64(devc->cur_samplerate);
427                 break;
428         case SR_CONF_LIMIT_SAMPLES:
429                 *data = g_variant_new_uint64(devc->limit_samples);
430                 break;
431         case SR_CONF_CAPTURE_RATIO:
432                 *data = g_variant_new_uint64(devc->capture_ratio);
433                 break;
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);
437                 return SR_OK;
438         case SR_CONF_LOGIC_THRESHOLD:
439                 *data = g_variant_new_string(logic_threshold[devc->threshold_voltage_idx]);
440                 break;
441         case SR_CONF_LOGIC_THRESHOLD_CUSTOM:
442                 *data = g_variant_new_double(devc->threshold_voltage);
443                 break;
444
445         default:
446                 return SR_ERR_NA;
447         }
448
449         return SR_OK;
450 }
451
452 static int config_set(uint32_t key, GVariant *data,
453         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
454 {
455         struct dev_context *devc;
456         double low, high;
457         int idx;
458
459         (void)cg;
460
461         devc = sdi->priv;
462
463         switch (key) {
464         case SR_CONF_SAMPLERATE:
465                 devc->cur_samplerate = g_variant_get_uint64(data);
466                 break;
467         case SR_CONF_LIMIT_SAMPLES:
468                 devc->limit_samples = g_variant_get_uint64(data);
469                 break;
470         case SR_CONF_CAPTURE_RATIO:
471                 devc->capture_ratio = g_variant_get_uint64(data);
472                 break;
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 */
477                 break;
478         case SR_CONF_LOGIC_THRESHOLD: {
479                 if ((idx = std_str_idx(data, logic_threshold, MAX_NUM_LOGIC_THRESHOLD_ENTRIES)) < 0)
480                         return SR_ERR_ARG;
481                 if (idx == MAX_NUM_LOGIC_THRESHOLD_ENTRIES - 1) {
482                         /* user threshold */
483                 } else {
484                         devc->threshold_voltage = logic_threshold_value[idx];
485                 }
486                 devc->threshold_voltage_idx = idx;
487                 break;
488         }
489         case SR_CONF_LOGIC_THRESHOLD_CUSTOM:
490                 devc->threshold_voltage = g_variant_get_double(data);
491                 break;
492         default:
493                 return SR_ERR_NA;
494         }
495
496         return SR_OK;
497 }
498
499 static int config_list(uint32_t key, GVariant **data,
500         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
501 {
502         struct dev_context *devc;
503
504         switch (key) {
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:
509                 if (!sdi)
510                         return SR_ERR_ARG;
511                 devc = sdi->priv;
512                 if (devc->max_samplerate == SR_MHZ(200)) {
513                         *data = std_gvar_samplerates(ARRAY_AND_SIZE(samplerates_la2016));
514                 }
515                 else {
516                         *data = std_gvar_samplerates(ARRAY_AND_SIZE(samplerates_la1016));
517                 }
518                 break;
519         case SR_CONF_LIMIT_SAMPLES:
520                 *data = std_gvar_tuple_u64(LA2016_NUM_SAMPLES_MIN, LA2016_NUM_SAMPLES_MAX);
521                 break;
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);
526                 break;
527         case SR_CONF_TRIGGER_MATCH:
528                 *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
529                 break;
530         case SR_CONF_LOGIC_THRESHOLD:
531                 *data = g_variant_new_strv(logic_threshold, MAX_NUM_LOGIC_THRESHOLD_ENTRIES);
532                 break;
533         default:
534                 return SR_ERR_NA;
535         }
536
537         return SR_OK;
538 }
539
540 static int configure_channels(const struct sr_dev_inst *sdi)
541 {
542         struct dev_context *devc;
543
544         devc = sdi->priv;
545         devc->cur_channels = 0;
546         devc->num_channels = 0;
547
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)
551                         continue;
552                 devc->cur_channels |= 1 << ch->index;
553                 devc->num_channels++;
554         }
555
556         return SR_OK;
557 }
558
559 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
560 {
561         struct sr_dev_driver *di;
562         struct drv_context *drvc;
563         struct dev_context *devc;
564         int ret;
565
566         di = sdi->driver;
567         drvc = di->context;
568         devc = sdi->priv;
569
570         if (configure_channels(sdi) != SR_OK) {
571                 sr_err("Cannot configure channels.");
572                 return SR_ERR;
573         }
574
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;
579         }
580
581         if ((ret = la2016_setup_acquisition(sdi)) != SR_OK) {
582                 g_free(devc->convbuffer);
583                 devc->convbuffer = NULL;
584                 return ret;
585         }
586
587         devc->ctx = drvc->sr_ctx;
588
589         if ((ret = la2016_start_acquisition(sdi)) != SR_OK) {
590                 la2016_abort_acquisition(sdi);
591                 return ret;
592         }
593
594         devc->have_trigger = 0;
595         usb_source_add(sdi->session, drvc->sr_ctx, 50,
596                 la2016_receive_data, (void *)sdi);
597
598         std_session_send_df_header(sdi);
599
600         return SR_OK;
601 }
602
603 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
604 {
605         int ret;
606
607         ret = la2016_abort_acquisition(sdi);
608
609         return ret;
610 }
611
612 static struct sr_dev_driver kingst_la2016_driver_info = {
613         .name = "kingst-la2016",
614         .longname = "Kingst LA2016",
615         .api_version = 1,
616         .init = std_init,
617         .cleanup = std_cleanup,
618         .scan = scan,
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,
628         .context = NULL,
629 };
630 SR_REGISTER_DEV_DRIVER(kingst_la2016_driver_info);