]> sigrok.org Git - libsigrok.git/blame - src/hardware/kingst-la2016/api.c
kingst-la2016: style nits, remove not needed include directives
[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
23/* mostly stolen from src/hardware/saleae-logic16/ */
24
25#include <config.h>
a7740b06 26
a7740b06 27#include <libsigrok/libsigrok.h>
f2cd2deb 28#include <string.h>
a7740b06 29
f2cd2deb
FS
30#include "libsigrok-internal.h"
31#include "protocol.h"
32
33static const uint32_t scanopts[] = {
34 SR_CONF_CONN,
35};
36
37static const uint32_t drvopts[] = {
38 SR_CONF_LOGIC_ANALYZER,
39};
40
41static 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
53static const int32_t trigger_matches[] = {
54 SR_TRIGGER_ZERO,
55 SR_TRIGGER_ONE,
56 SR_TRIGGER_RISING,
57 SR_TRIGGER_FALLING,
58};
59
60static const char *channel_names[] = {
955ab604
GS
61 "0", "1", "2", "3", "4", "5", "6", "7",
62 "8", "9", "10", "11", "12", "13", "14", "15",
f2cd2deb
FS
63};
64
8b172e78 65static const uint64_t samplerates_la2016[] = {
f2cd2deb
FS
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
8b172e78
KG
83static 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
f2cd2deb
FS
100static 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
112static 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
f2cd2deb
FS
127static 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;
955ab604
GS
169 if (usb->bus == libusb_get_bus_number(devlist[i]) &&
170 usb->address == libusb_get_device_address(devlist[i]))
f2cd2deb
FS
171 break;
172 }
955ab604 173 if (!l) {
f2cd2deb
FS
174 /* This device matched none of the ones that
175 * matched the conn specification. */
176 continue;
955ab604 177 }
f2cd2deb
FS
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("uC 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
236static 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("Unable to claim USB interface. Another "
291 "program or driver has already claimed it.");
292 ret = SR_ERR;
293 break;
294 } else if (ret == LIBUSB_ERROR_NO_DEVICE) {
295 sr_err("Device has been disconnected.");
296 ret = SR_ERR;
297 break;
298 } else if (ret != 0) {
299 sr_err("Unable to claim interface: %s.", libusb_error_name(ret));
300 ret = SR_ERR;
301 break;
302 }
303
304 if ((ret = la2016_init_device(sdi)) != SR_OK) {
305 sr_err("Failed to init device.");
306 break;
307 }
308
309 sr_info("Opened device on %d.%d (logical) / %s (physical), interface %d.",
310 usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
311
312 ret = SR_OK;
313
314 break;
315 }
316
317 libusb_free_device_list(devlist, 1);
318
319 if (ret != SR_OK) {
320 if (usb->devhdl) {
321 libusb_release_interface(usb->devhdl, USB_INTERFACE);
322 libusb_close(usb->devhdl);
323 usb->devhdl = NULL;
324 }
325 return SR_ERR;
326 }
327
328 return SR_OK;
329}
330
331static int dev_open(struct sr_dev_inst *sdi)
332{
333 struct dev_context *devc;
334 int64_t timediff_us, timediff_ms;
335 uint64_t reset_done;
336 uint64_t now;
337 int ret;
338
339 devc = sdi->priv;
340
341 /*
342 * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS
343 * milliseconds for the FX2 to renumerate.
344 */
345 ret = SR_ERR;
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);
353 timediff_ms = 0;
354 while (timediff_ms < MAX_RENUM_DELAY_MS) {
355 g_usleep(200 * 1000);
356
357 timediff_us = g_get_monotonic_time() - devc->fw_updated;
358 timediff_ms = timediff_us / 1000;
955ab604 359
f2cd2deb
FS
360 if ((ret = la2016_dev_open(sdi)) == SR_OK)
361 break;
362 sr_spew("Waited %" PRIi64 "ms.", timediff_ms);
363 }
364 if (ret != SR_OK) {
365 sr_err("Device failed to re-enumerate.");
366 return SR_ERR;
367 }
368 sr_info("Device came back after %" PRIi64 "ms.", timediff_ms);
955ab604 369 } else {
f2cd2deb 370 ret = la2016_dev_open(sdi);
955ab604 371 }
f2cd2deb
FS
372
373 if (ret != SR_OK) {
374 sr_err("Unable to open device.");
375 return SR_ERR;
376 }
377
378 return SR_OK;
379}
380
381static int dev_close(struct sr_dev_inst *sdi)
382{
383 struct sr_usb_dev_inst *usb;
384
385 usb = sdi->conn;
386
387 if (!usb->devhdl)
388 return SR_ERR_BUG;
389
390 la2016_deinit_device(sdi);
391
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);
396 usb->devhdl = NULL;
397
398 return SR_OK;
399}
400
955ab604
GS
401static int config_get(uint32_t key, GVariant **data,
402 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
f2cd2deb
FS
403{
404 struct dev_context *devc;
405 struct sr_usb_dev_inst *usb;
406 double rounded;
407
408 (void)cg;
409
410 if (!sdi)
411 return SR_ERR_ARG;
412 devc = sdi->priv;
413
414 switch (key) {
415 case SR_CONF_CONN:
416 if (!sdi->conn)
417 return SR_ERR_ARG;
418 usb = sdi->conn;
955ab604 419 if (usb->address == 255) {
f2cd2deb
FS
420 /* Device still needs to re-enumerate after firmware
421 * upload, so we don't know its (future) address. */
422 return SR_ERR;
955ab604 423 }
f2cd2deb
FS
424 *data = g_variant_new_printf("%d.%d", usb->bus, usb->address);
425 break;
426 case SR_CONF_SAMPLERATE:
427 *data = g_variant_new_uint64(devc->cur_samplerate);
428 break;
429 case SR_CONF_LIMIT_SAMPLES:
430 *data = g_variant_new_uint64(devc->limit_samples);
431 break;
432 case SR_CONF_CAPTURE_RATIO:
433 *data = g_variant_new_uint64(devc->capture_ratio);
434 break;
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);
438 return SR_OK;
439 case SR_CONF_LOGIC_THRESHOLD:
440 *data = g_variant_new_string(logic_threshold[devc->threshold_voltage_idx]);
441 break;
442 case SR_CONF_LOGIC_THRESHOLD_CUSTOM:
443 *data = g_variant_new_double(devc->threshold_voltage);
444 break;
955ab604 445
f2cd2deb
FS
446 default:
447 return SR_ERR_NA;
448 }
449
450 return SR_OK;
451}
452
955ab604
GS
453static int config_set(uint32_t key, GVariant *data,
454 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
f2cd2deb
FS
455{
456 struct dev_context *devc;
457 double low, high;
458 int idx;
459
460 (void)cg;
461
462 devc = sdi->priv;
463
464 switch (key) {
465 case SR_CONF_SAMPLERATE:
466 devc->cur_samplerate = g_variant_get_uint64(data);
467 break;
468 case SR_CONF_LIMIT_SAMPLES:
469 devc->limit_samples = g_variant_get_uint64(data);
470 break;
471 case SR_CONF_CAPTURE_RATIO:
472 devc->capture_ratio = g_variant_get_uint64(data);
473 break;
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 */
478 break;
479 case SR_CONF_LOGIC_THRESHOLD: {
480 if ((idx = std_str_idx(data, logic_threshold, MAX_NUM_LOGIC_THRESHOLD_ENTRIES)) < 0)
481 return SR_ERR_ARG;
482 if (idx == MAX_NUM_LOGIC_THRESHOLD_ENTRIES - 1) {
483 /* user threshold */
484 } else {
485 devc->threshold_voltage = logic_threshold_value[idx];
486 }
487 devc->threshold_voltage_idx = idx;
488 break;
489 }
490 case SR_CONF_LOGIC_THRESHOLD_CUSTOM:
491 devc->threshold_voltage = g_variant_get_double(data);
492 break;
493 default:
494 return SR_ERR_NA;
495 }
496
497 return SR_OK;
498}
499
955ab604
GS
500static int config_list(uint32_t key, GVariant **data,
501 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
f2cd2deb 502{
8b172e78
KG
503 struct dev_context *devc;
504
f2cd2deb
FS
505 switch (key) {
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:
fb28e72d
MW
510 if (!sdi)
511 return SR_ERR_ARG;
512 devc = sdi->priv;
8b172e78
KG
513 if (devc->max_samplerate == SR_MHZ(200)) {
514 *data = std_gvar_samplerates(ARRAY_AND_SIZE(samplerates_la2016));
515 }
516 else {
517 *data = std_gvar_samplerates(ARRAY_AND_SIZE(samplerates_la1016));
518 }
f2cd2deb
FS
519 break;
520 case SR_CONF_LIMIT_SAMPLES:
521 *data = std_gvar_tuple_u64(LA2016_NUM_SAMPLES_MIN, LA2016_NUM_SAMPLES_MAX);
522 break;
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);
527 break;
528 case SR_CONF_TRIGGER_MATCH:
529 *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
530 break;
531 case SR_CONF_LOGIC_THRESHOLD:
532 *data = g_variant_new_strv(logic_threshold, MAX_NUM_LOGIC_THRESHOLD_ENTRIES);
533 break;
534 default:
535 return SR_ERR_NA;
536 }
537
538 return SR_OK;
539}
540
f2cd2deb
FS
541static int configure_channels(const struct sr_dev_inst *sdi)
542{
543 struct dev_context *devc;
544
545 devc = sdi->priv;
546 devc->cur_channels = 0;
547 devc->num_channels = 0;
548
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)
552 continue;
955ab604 553 devc->cur_channels |= 1 << ch->index;
f2cd2deb
FS
554 devc->num_channels++;
555 }
556
557 return SR_OK;
558}
559
560static int dev_acquisition_start(const struct sr_dev_inst *sdi)
561{
562 struct sr_dev_driver *di;
563 struct drv_context *drvc;
564 struct dev_context *devc;
565 int ret;
566
567 di = sdi->driver;
568 drvc = di->context;
569 devc = sdi->priv;
570
571 if (configure_channels(sdi) != SR_OK) {
572 sr_err("Failed to configure channels.");
573 return SR_ERR;
574 }
575
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;
580 }
581
582 if ((ret = la2016_setup_acquisition(sdi)) != SR_OK) {
583 g_free(devc->convbuffer);
584 devc->convbuffer = NULL;
585 return ret;
586 }
587
588 devc->ctx = drvc->sr_ctx;
589
590 if ((ret = la2016_start_acquisition(sdi)) != SR_OK) {
3ebc1cb2 591 la2016_abort_acquisition(sdi);
f2cd2deb
FS
592 return ret;
593 }
594
595 devc->have_trigger = 0;
388438e4
GS
596 usb_source_add(sdi->session, drvc->sr_ctx, 50,
597 la2016_receive_data, (void *)sdi);
f2cd2deb
FS
598
599 std_session_send_df_header(sdi);
600
601 return SR_OK;
602}
603
604static int dev_acquisition_stop(struct sr_dev_inst *sdi)
605{
606 int ret;
607
608 ret = la2016_abort_acquisition(sdi);
f2cd2deb
FS
609
610 return ret;
611}
612
613static struct sr_dev_driver kingst_la2016_driver_info = {
614 .name = "kingst-la2016",
615 .longname = "Kingst LA2016",
616 .api_version = 1,
617 .init = std_init,
618 .cleanup = std_cleanup,
619 .scan = scan,
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,
629 .context = NULL,
630};
631SR_REGISTER_DEV_DRIVER(kingst_la2016_driver_info);