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/>.
24 * This driver implementation initially was derived from the
25 * src/hardware/saleae-logic16/ source code.
30 #include <libsigrok/libsigrok.h>
33 #include "libsigrok-internal.h"
36 static const uint32_t scanopts[] = {
40 static const uint32_t drvopts[] = {
41 SR_CONF_LOGIC_ANALYZER,
42 SR_CONF_SIGNAL_GENERATOR,
45 static const uint32_t devopts[] = {
46 /* TODO: SR_CONF_CONTINUOUS, */
47 SR_CONF_CONN | SR_CONF_GET,
48 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
49 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
50 SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
51 #if WITH_THRESHOLD_DEVCFG
52 SR_CONF_VOLTAGE_THRESHOLD | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
53 #if !WITH_THRESHOLD_SIMPLE
54 SR_CONF_LOGIC_THRESHOLD | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
55 SR_CONF_LOGIC_THRESHOLD_CUSTOM | SR_CONF_GET | SR_CONF_SET,
58 SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
59 SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET,
62 static const uint32_t devopts_cg_logic[] = {
63 #if !WITH_THRESHOLD_DEVCFG
64 SR_CONF_VOLTAGE_THRESHOLD | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
68 static const uint32_t devopts_cg_pwm[] = {
69 SR_CONF_ENABLED | SR_CONF_GET | SR_CONF_SET,
70 SR_CONF_OUTPUT_FREQUENCY | SR_CONF_GET | SR_CONF_SET,
71 SR_CONF_DUTY_CYCLE | SR_CONF_GET | SR_CONF_SET,
74 static const int32_t trigger_matches[] = {
81 static const char *channel_names_logic[] = {
82 "CH0", "CH1", "CH2", "CH3", "CH4", "CH5", "CH6", "CH7",
83 "CH8", "CH9", "CH10", "CH11", "CH12", "CH13", "CH14", "CH15",
84 "CH16", "CH17", "CH18", "CH19", "CH20", "CH21", "CH22", "CH23",
85 "CH24", "CH25", "CH26", "CH27", "CH28", "CH29", "CH30", "CH31",
88 static const char *channel_names_pwm[] = {
93 * The hardware uses a 100/200/500MHz base clock (model dependent) and
94 * a 16bit divider (common across all models). The range from 10kHz to
95 * 100/200/500MHz should be applicable to all devices. High rates may
96 * suffer from coarse resolution (e.g. in the "500MHz div 2" case) and
97 * may not provide the desired 1/2/5 steps. Fortunately this exclusively
98 * affects the 500MHz model where 250MHz is used instead of 200MHz and
99 * the 166MHz and 125MHz rates are not presented to users. Deep memory
100 * of these models and hardware compression reduce the necessity to let
101 * users pick from a huge list of possible rates.
105 static const uint64_t rates_500mhz[] = {
123 static const uint64_t rates_200mhz[] = {
140 static const uint64_t rates_100mhz[] = {
156 #if WITH_THRESHOLD_SIMPLE
159 * Only list a few discrete voltages, to form a useful set which covers
160 * most logic families. Too many choices can make some applications use
161 * a slider again. Which may lack a scale for the current value, and
162 * leave users without feedback what the currently used value might be.
164 static const double threshold_ranges[][2] = {
169 { 1.4, 1.4, }, /* Default, 1.4V, index 4. */
174 #define LOGIC_THRESHOLD_IDX_DFLT 4
176 static double threshold_voltage(const struct sr_dev_inst *sdi, double *high)
178 struct dev_context *devc;
183 idx = devc->threshold_voltage_idx;
184 voltage = threshold_ranges[idx][0];
186 *high = threshold_ranges[idx][1];
191 #else /* WITH_THRESHOLD_SIMPLE */
193 static const float logic_threshold_value[] = {
205 static const char *logic_threshold[] = {
218 #define LOGIC_THRESHOLD_IDX_USER (ARRAY_SIZE(logic_threshold) - 1)
220 static double threshold_voltage(const struct sr_dev_inst *sdi, double *high)
222 struct dev_context *devc;
227 idx = devc->threshold_voltage_idx;
228 if (idx == LOGIC_THRESHOLD_IDX_USER)
229 voltage = devc->threshold_voltage;
231 voltage = logic_threshold_value[idx];
238 #endif /* WITH_THRESHOLD_SIMPLE */
240 /* Convenience. Release an allocated devc from error paths. */
241 static void kingst_la2016_free_devc(struct dev_context *devc)
245 g_free(devc->mcu_firmware);
246 g_free(devc->fpga_bitstream);
250 /* Convenience. Release an allocated sdi from error paths. */
251 static void kingst_la2016_free_sdi(struct sr_dev_inst *sdi)
257 g_free(sdi->version);
258 g_free(sdi->serial_num);
259 g_free(sdi->connection_id);
260 sr_usb_dev_inst_free(sdi->conn);
261 kingst_la2016_free_devc(sdi->priv);
264 /* Convenience. Open a USB device (including claiming an interface). */
265 static int la2016_open_usb(struct sr_usb_dev_inst *usb,
266 libusb_device *dev, gboolean show_message)
270 ret = libusb_open(dev, &usb->devhdl);
273 sr_err("Cannot open device: %s.",
274 libusb_error_name(ret));
279 if (usb->address == 0xff) {
281 * First encounter after firmware upload.
282 * Grab current address after enumeration.
284 usb->address = libusb_get_device_address(dev);
287 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
288 if (ret == LIBUSB_ERROR_BUSY) {
289 sr_err("Cannot claim USB interface. Another program or driver using it?");
291 } else if (ret == LIBUSB_ERROR_NO_DEVICE) {
292 sr_err("Device has been disconnected.");
294 } else if (ret != 0) {
295 sr_err("Cannot claim USB interface: %s.",
296 libusb_error_name(ret));
303 /* Convenience. Close an opened USB device (and release the interface). */
304 static void la2016_close_usb(struct sr_usb_dev_inst *usb)
311 libusb_release_interface(usb->devhdl, USB_INTERFACE);
312 libusb_close(usb->devhdl);
317 /* Communicate to an USB device to identify the Kingst LA model. */
318 static int la2016_identify_read(struct sr_dev_inst *sdi,
319 struct sr_usb_dev_inst *usb, libusb_device *dev,
320 gboolean show_message)
324 ret = la2016_open_usb(usb, dev, show_message);
327 sr_err("Cannot communicate to MCU firmware.");
332 * Also complete the hardware configuration (FPGA bitstream)
333 * when MCU firmware communication became operational. Either
334 * failure is considered fatal when probing for the device.
336 ret = la2016_identify_device(sdi, show_message);
338 ret = la2016_init_hardware(sdi);
341 la2016_close_usb(usb);
346 /* Find given conn_id in another USB enum. Identify Kingst LA model. */
347 static int la2016_identify_enum(struct sr_dev_inst *sdi)
349 struct sr_dev_driver *di;
350 struct drv_context *drvc;
351 struct sr_context *ctx;
352 libusb_device **devlist, *dev;
353 struct libusb_device_descriptor des;
355 size_t device_count, dev_idx;
362 ret = libusb_get_device_list(ctx->libusb_ctx, &devlist);
369 for (dev_idx = 0; dev_idx < device_count; dev_idx++) {
370 dev = devlist[dev_idx];
371 libusb_get_device_descriptor(dev, &des);
372 if (des.idVendor != LA2016_VID || des.idProduct != LA2016_PID)
374 if (des.iProduct != LA2016_IPRODUCT_INDEX)
376 ret = usb_get_port_path(dev, conn_id, sizeof(conn_id));
379 if (strcmp(sdi->connection_id, conn_id) != 0)
381 id_ret = la2016_identify_read(sdi, sdi->conn, dev, FALSE);
384 libusb_free_device_list(devlist, 1);
389 /* Wait for a device to re-appear after firmware upload. */
390 static int la2016_identify_wait(struct sr_dev_inst *sdi)
392 struct dev_context *devc;
393 uint64_t reset_done, now, elapsed_ms;
398 sr_info("Waiting for device to reset after firmware upload.");
399 now = g_get_monotonic_time();
400 reset_done = devc->fw_uploaded + RENUM_GONE_DELAY_MS * 1000;
401 if (now < reset_done)
402 g_usleep(reset_done - now);
404 now = g_get_monotonic_time();
405 elapsed_ms = (now - devc->fw_uploaded) / 1000;
406 sr_spew("Waited %" PRIu64 "ms.", elapsed_ms);
407 ret = la2016_identify_enum(sdi);
409 devc->fw_uploaded = 0;
412 g_usleep(RENUM_POLL_INTERVAL_MS * 1000);
413 } while (elapsed_ms < RENUM_CHECK_PERIOD_MS);
415 sr_err("Device failed to re-enumerate.");
418 sr_info("Device came back after %" PRIi64 "ms.", elapsed_ms);
424 * Open given conn_id from another USB enum. Used by dev_open(). Similar
425 * to, and should be kept in sync with la2016_identify_enum().
427 static int la2016_open_enum(struct sr_dev_inst *sdi)
429 struct sr_dev_driver *di;
430 struct drv_context *drvc;
431 struct sr_context *ctx;
432 libusb_device **devlist, *dev;
433 struct libusb_device_descriptor des;
435 size_t device_count, dev_idx;
442 ret = libusb_get_device_list(ctx->libusb_ctx, &devlist);
448 open_ret = SR_ERR_IO;
449 for (dev_idx = 0; dev_idx < device_count; dev_idx++) {
450 dev = devlist[dev_idx];
451 libusb_get_device_descriptor(dev, &des);
452 if (des.idVendor != LA2016_VID || des.idProduct != LA2016_PID)
454 if (des.iProduct != LA2016_IPRODUCT_INDEX)
456 ret = usb_get_port_path(dev, conn_id, sizeof(conn_id));
459 if (strcmp(sdi->connection_id, conn_id) != 0)
461 open_ret = la2016_open_usb(sdi->conn, dev, TRUE);
464 libusb_free_device_list(devlist, 1);
469 static GSList *scan(struct sr_dev_driver *di, GSList *options)
471 struct drv_context *drvc;
472 struct sr_context *ctx;
473 struct dev_context *devc;
474 struct sr_dev_inst *sdi;
475 struct sr_usb_dev_inst *usb;
476 struct sr_config *src;
478 GSList *devices, *found_devices, *renum_devices;
479 GSList *conn_devices;
480 struct libusb_device_descriptor des;
481 libusb_device **devlist, *dev;
482 size_t dev_count, dev_idx, ch_idx;
488 size_t ch_off, ch_max;
489 struct sr_channel *ch;
490 struct sr_channel_group *cg;
497 for (l = options; l; l = l->next) {
501 conn = g_variant_get_string(src->data, NULL);
506 conn_devices = sr_usb_find(ctx->libusb_ctx, conn);
507 if (conn && !conn_devices) {
508 sr_err("Cannot find the specified connection '%s'.", conn);
513 * Find all LA2016 devices, optionally upload firmware to them.
514 * Defer completion of sdi/devc creation until all (selected)
515 * devices were found in a usable state, and their models got
516 * identified which affect their feature set. It appears that
517 * we cannot communicate to the device within the same USB enum
518 * cycle, needs another USB enumeration after firmware upload.
521 found_devices = NULL;
522 renum_devices = NULL;
523 ret = libusb_get_device_list(ctx->libusb_ctx, &devlist);
525 sr_err("Cannot get device list: %s.", libusb_error_name(ret));
529 for (dev_idx = 0; dev_idx < dev_count; dev_idx++) {
530 dev = devlist[dev_idx];
531 bus = libusb_get_bus_number(dev);
532 addr = libusb_get_device_address(dev);
534 /* Filter by connection when externally specified. */
535 for (l = conn_devices; l; l = l->next) {
537 if (usb->bus == bus && usb->address == addr)
540 if (conn_devices && !l) {
541 sr_spew("Bus %hhu, addr %hhu do not match specified filter.",
546 /* Check USB VID:PID. Get the connection string. */
547 libusb_get_device_descriptor(dev, &des);
548 if (des.idVendor != LA2016_VID || des.idProduct != LA2016_PID)
551 ret = usb_get_port_path(dev, conn_id, sizeof(conn_id));
554 sr_dbg("USB enum found %04x:%04x at path %s, %d.%d.",
555 des.idVendor, des.idProduct, conn_id, bus, addr);
556 usb = sr_usb_dev_inst_new(bus, addr, NULL);
558 sdi = g_malloc0(sizeof(*sdi));
560 sdi->status = SR_ST_INITIALIZING;
561 sdi->inst_type = SR_INST_USB;
562 sdi->connection_id = g_strdup(conn_id);
565 devc = g_malloc0(sizeof(*devc));
569 * Load MCU firmware if it is currently missing. Which
570 * makes the device disappear and renumerate in USB.
571 * We need to come back another time to communicate to
574 devc->fw_uploaded = 0;
575 if (des.iProduct != LA2016_IPRODUCT_INDEX) {
576 sr_info("Uploading MCU firmware to '%s'.", conn_id);
577 ret = la2016_upload_firmware(sdi, ctx, dev, pid);
579 sr_err("MCU firmware upload failed.");
580 kingst_la2016_free_sdi(sdi);
583 devc->fw_uploaded = g_get_monotonic_time();
585 renum_devices = g_slist_append(renum_devices, sdi);
590 * Communicate to the MCU firmware to access EEPROM data
591 * which lets us identify the device type. Then stop, to
592 * share remaining sdi/devc creation with those devices
593 * which had their MCU firmware uploaded above and which
594 * get revisited later.
596 ret = la2016_identify_read(sdi, usb, dev, TRUE);
597 if (ret != SR_OK || !devc->model) {
598 sr_err("Unknown or unsupported device type.");
599 kingst_la2016_free_sdi(sdi);
602 found_devices = g_slist_append(found_devices, sdi);
604 libusb_free_device_list(devlist, 1);
605 g_slist_free_full(conn_devices, sr_usb_dev_inst_free_cb);
608 * Wait for devices to re-appear after firmware upload. Append
609 * the yet unidentified device to the list of found devices, or
610 * release the previously allocated sdi/devc.
612 for (l = renum_devices; l; l = l->next) {
615 ret = la2016_identify_wait(sdi);
616 if (ret != SR_OK || !devc->model) {
617 sr_dbg("Skipping unusable '%s'.", sdi->connection_id);
618 kingst_la2016_free_sdi(sdi);
621 found_devices = g_slist_append(found_devices, sdi);
623 g_slist_free(renum_devices);
626 * All found devices got identified, their type is known here.
627 * Complete the sdi/devc creation. Assign default settings
628 * because the vendor firmware would not let us read back the
629 * previously written configuration.
631 for (l = found_devices; l; l = l->next) {
635 sdi->vendor = g_strdup("Kingst");
636 sdi->model = g_strdup(devc->model->name);
639 /* Create the "Logic" channel group. */
640 ch_max = ARRAY_SIZE(channel_names_logic);
641 if (ch_max > devc->model->channel_count)
642 ch_max = devc->model->channel_count;
643 cg = sr_channel_group_new(sdi, "Logic", NULL);
645 for (ch_idx = 0; ch_idx < ch_max; ch_idx++) {
646 ch = sr_channel_new(sdi, ch_off,
647 SR_CHANNEL_LOGIC, TRUE,
648 channel_names_logic[ch_idx]);
650 cg->channels = g_slist_append(cg->channels, ch);
653 /* Create the "PWMx" channel groups. */
654 ch_max = ARRAY_SIZE(channel_names_pwm);
655 for (ch_idx = 0; ch_idx < ch_max; ch_idx++) {
657 name = channel_names_pwm[ch_idx];
658 cg = sr_channel_group_new(sdi, name, NULL);
661 ch = sr_channel_new(sdi, ch_off,
662 SR_CHANNEL_ANALOG, FALSE, name);
664 cg->channels = g_slist_append(cg->channels, ch);
668 * Ideally we'd get the previous configuration from the
669 * hardware, but this device is write-only. So we have
670 * to assign a fixed set of initial configuration values.
672 sr_sw_limits_init(&devc->sw_limits);
673 devc->sw_limits.limit_samples = 0;
674 devc->capture_ratio = 50;
675 devc->cur_samplerate = devc->model->samplerate;
676 #if WITH_THRESHOLD_SIMPLE
677 devc->threshold_voltage_idx = LOGIC_THRESHOLD_IDX_DFLT;
678 #else /* WITH_THRESHOLD_SIMPLE */
679 devc->threshold_voltage_idx = 0;
680 devc->threshold_voltage = logic_threshold_value[devc->threshold_voltage_idx];
681 #endif /* WITH_THRESHOLD_SIMPLE */
682 if (ARRAY_SIZE(devc->pwm_setting) >= 1) {
683 devc->pwm_setting[0].enabled = FALSE;
684 devc->pwm_setting[0].freq = SR_KHZ(1);
685 devc->pwm_setting[0].duty = 50;
687 if (ARRAY_SIZE(devc->pwm_setting) >= 2) {
688 devc->pwm_setting[1].enabled = FALSE;
689 devc->pwm_setting[1].freq = SR_KHZ(100);
690 devc->pwm_setting[1].duty = 50;
693 sdi->status = SR_ST_INACTIVE;
694 devices = g_slist_append(devices, sdi);
696 g_slist_free(found_devices);
698 return std_scan_complete(di, devices);
701 static int dev_open(struct sr_dev_inst *sdi)
703 struct dev_context *devc;
709 ret = la2016_open_enum(sdi);
711 sr_err("Cannot open device.");
715 /* Send most recent PWM configuration to the device. */
716 for (ch = 0; ch < ARRAY_SIZE(devc->pwm_setting); ch++) {
717 ret = la2016_write_pwm_config(sdi, ch);
725 static int dev_close(struct sr_dev_inst *sdi)
727 struct sr_usb_dev_inst *usb;
734 la2016_deinit_hardware(sdi);
736 sr_info("Closing device on %d.%d (logical) / %s (physical) interface %d.",
737 usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
738 la2016_close_usb(sdi->conn);
743 /* Config API helper. Get type and index of a channel group. */
744 static int get_cg_index(const struct sr_dev_inst *sdi,
745 const struct sr_channel_group *cg,
746 int *type, size_t *logic, size_t *analog)
748 struct dev_context *devc;
752 /* Preset return values. */
760 /* Start categorizing the received cg. */
766 l = sdi->channel_groups;
768 /* First sdi->channelgroups item is "Logic". */
773 *type = SR_CHANNEL_LOGIC;
780 /* Next sdi->channelgroups items are "PWMx". */
782 while (l && l->data != cg) {
786 if (l && idx < ARRAY_SIZE(devc->pwm_setting)) {
788 *type = SR_CHANNEL_ANALOG;
797 static int config_get(uint32_t key, GVariant **data,
798 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
800 struct dev_context *devc;
802 size_t logic_idx, analog_idx;
803 struct pwm_setting *pwm;
804 struct sr_usb_dev_inst *usb;
805 double voltage, rounded;
815 /* Check for types (and index) of channel groups. */
816 ret = get_cg_index(sdi, cg, &cg_type, &logic_idx, &analog_idx);
817 if (cg && ret != SR_OK)
820 /* Handle requests for the "Logic" channel group. */
821 if (cg && cg_type == SR_CHANNEL_LOGIC) {
823 #if !WITH_THRESHOLD_DEVCFG
824 #if WITH_THRESHOLD_SIMPLE
825 case SR_CONF_VOLTAGE_THRESHOLD:
826 voltage = threshold_voltage(sdi, NULL);
827 *data = std_gvar_tuple_double(voltage, voltage);
829 #endif /* WITH_THRESHOLD_SIMPLE */
830 #endif /* WITH_THRESHOLD_DEVCFG */
837 /* Handle requests for the "PWMx" channel groups. */
838 if (cg && cg_type == SR_CHANNEL_ANALOG) {
839 pwm = &devc->pwm_setting[analog_idx];
841 case SR_CONF_ENABLED:
842 *data = g_variant_new_boolean(pwm->enabled);
844 case SR_CONF_OUTPUT_FREQUENCY:
845 *data = g_variant_new_double(pwm->freq);
847 case SR_CONF_DUTY_CYCLE:
848 *data = g_variant_new_double(pwm->duty);
859 *data = g_variant_new_printf("%d.%d", usb->bus, usb->address);
861 case SR_CONF_SAMPLERATE:
862 *data = g_variant_new_uint64(devc->cur_samplerate);
864 case SR_CONF_LIMIT_SAMPLES:
865 case SR_CONF_LIMIT_MSEC:
866 return sr_sw_limits_config_get(&devc->sw_limits, key, data);
867 case SR_CONF_CAPTURE_RATIO:
868 *data = g_variant_new_uint64(devc->capture_ratio);
870 #if WITH_THRESHOLD_DEVCFG
871 #if WITH_THRESHOLD_SIMPLE
872 case SR_CONF_VOLTAGE_THRESHOLD:
873 voltage = threshold_voltage(sdi, NULL);
874 *data = std_gvar_tuple_double(voltage, voltage);
876 #else /* WITH_THRESHOLD_SIMPLE */
877 case SR_CONF_VOLTAGE_THRESHOLD:
878 rounded = (int)(devc->threshold_voltage / 0.1) * 0.1;
879 *data = std_gvar_tuple_double(rounded, rounded + 0.1);
881 case SR_CONF_LOGIC_THRESHOLD:
882 label = logic_threshold[devc->threshold_voltage_idx];
883 *data = g_variant_new_string(label);
885 case SR_CONF_LOGIC_THRESHOLD_CUSTOM:
886 *data = g_variant_new_double(devc->threshold_voltage);
888 #endif /* WITH_THRESHOLD_SIMPLE */
889 #endif /* WITH_THRESHOLD_DEVCFG */
897 static int config_set(uint32_t key, GVariant *data,
898 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
900 struct dev_context *devc;
902 size_t logic_idx, analog_idx;
903 struct pwm_setting *pwm;
905 double low, high, voltage;
910 /* Check for types (and index) of channel groups. */
911 ret = get_cg_index(sdi, cg, &cg_type, &logic_idx, &analog_idx);
912 if (cg && ret != SR_OK)
915 /* Handle requests for the "Logic" channel group. */
916 if (cg && cg_type == SR_CHANNEL_LOGIC) {
918 #if !WITH_THRESHOLD_DEVCFG
919 #if WITH_THRESHOLD_SIMPLE
920 case SR_CONF_LOGIC_THRESHOLD:
921 idx = std_double_tuple_idx(data,
922 ARRAY_AND_SIZE(threshold_ranges));
925 devc->threshold_voltage_idx = idx;
927 #endif /* WITH_THRESHOLD_SIMPLE */
928 #endif /* WITH_THRESHOLD_DEVCFG */
935 /* Handle requests for the "PWMx" channel groups. */
936 if (cg && cg_type == SR_CHANNEL_ANALOG) {
937 pwm = &devc->pwm_setting[analog_idx];
939 case SR_CONF_ENABLED:
940 pwm->enabled = g_variant_get_boolean(data);
941 ret = la2016_write_pwm_config(sdi, analog_idx);
945 case SR_CONF_OUTPUT_FREQUENCY:
946 value_f = g_variant_get_double(data);
947 if (value_f <= 0.0 || value_f > MAX_PWM_FREQ)
950 ret = la2016_write_pwm_config(sdi, analog_idx);
954 case SR_CONF_DUTY_CYCLE:
955 value_f = g_variant_get_double(data);
956 if (value_f <= 0.0 || value_f > 100.0)
959 ret = la2016_write_pwm_config(sdi, analog_idx);
970 case SR_CONF_SAMPLERATE:
971 devc->cur_samplerate = g_variant_get_uint64(data);
973 case SR_CONF_LIMIT_SAMPLES:
974 case SR_CONF_LIMIT_MSEC:
975 return sr_sw_limits_config_set(&devc->sw_limits, key, data);
976 case SR_CONF_CAPTURE_RATIO:
977 devc->capture_ratio = g_variant_get_uint64(data);
979 #if WITH_THRESHOLD_DEVCFG
980 #if WITH_THRESHOLD_SIMPLE
981 case SR_CONF_VOLTAGE_THRESHOLD:
982 idx = std_double_tuple_idx(data,
983 ARRAY_AND_SIZE(threshold_ranges));
986 devc->threshold_voltage_idx = idx;
988 #else /* WITH_THRESHOLD_SIMPLE */
989 case SR_CONF_VOLTAGE_THRESHOLD:
990 g_variant_get(data, "(dd)", &low, &high);
991 devc->threshold_voltage = (low + high) / 2.0;
992 devc->threshold_voltage_idx = LOGIC_THRESHOLD_IDX_USER;
994 case SR_CONF_LOGIC_THRESHOLD: {
995 idx = std_str_idx(data, ARRAY_AND_SIZE(logic_threshold));
998 if (idx != LOGIC_THRESHOLD_IDX_USER) {
999 devc->threshold_voltage = logic_threshold_value[idx];
1001 devc->threshold_voltage_idx = idx;
1004 case SR_CONF_LOGIC_THRESHOLD_CUSTOM:
1005 devc->threshold_voltage = g_variant_get_double(data);
1007 #endif /* WITH_THRESHOLD_SIMPLE */
1008 #endif /* WITH_THRESHOLD_DEVCFG */
1016 static int config_list(uint32_t key, GVariant **data,
1017 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
1019 struct dev_context *devc;
1021 size_t logic_idx, analog_idx;
1023 devc = sdi ? sdi->priv : NULL;
1025 /* Check for types (and index) of channel groups. */
1026 ret = get_cg_index(sdi, cg, &cg_type, &logic_idx, &analog_idx);
1027 if (cg && ret != SR_OK)
1030 /* Handle requests for the "Logic" channel group. */
1031 if (cg && cg_type == SR_CHANNEL_LOGIC) {
1033 case SR_CONF_DEVICE_OPTIONS:
1034 if (ARRAY_SIZE(devopts_cg_logic) == 0)
1036 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
1037 devopts_cg_logic, ARRAY_SIZE(devopts_cg_logic),
1038 sizeof(devopts_cg_logic[0]));
1040 #if !WITH_THRESHOLD_DEVCFG
1041 #if WITH_THRESHOLD_SIMPLE
1042 case SR_CONF_VOLTAGE_THRESHOLD:
1043 *data = std_gvar_thresholds(ARRAY_AND_SIZE(threshold_ranges));
1045 #endif /* WITH_THRESHOLD_SIMPLE */
1046 #endif /* WITH_THRESHOLD_DEVCFG */
1053 /* Handle requests for the "PWMx" channel groups. */
1054 if (cg && cg_type == SR_CHANNEL_ANALOG) {
1056 case SR_CONF_DEVICE_OPTIONS:
1057 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
1058 devopts_cg_pwm, ARRAY_SIZE(devopts_cg_pwm),
1059 sizeof(devopts_cg_pwm[0]));
1068 case SR_CONF_SCAN_OPTIONS:
1069 case SR_CONF_DEVICE_OPTIONS:
1070 return STD_CONFIG_LIST(key, data, sdi, cg,
1071 scanopts, drvopts, devopts);
1072 case SR_CONF_SAMPLERATE:
1075 if (devc->model->samplerate == SR_MHZ(500))
1076 *data = std_gvar_samplerates(ARRAY_AND_SIZE(rates_500mhz));
1077 else if (devc->model->samplerate == SR_MHZ(200))
1078 *data = std_gvar_samplerates(ARRAY_AND_SIZE(rates_200mhz));
1080 *data = std_gvar_samplerates(ARRAY_AND_SIZE(rates_100mhz));
1082 case SR_CONF_LIMIT_SAMPLES:
1083 *data = std_gvar_tuple_u64(0, LA2016_NUM_SAMPLES_MAX);
1085 #if WITH_THRESHOLD_DEVCFG
1086 #if WITH_THRESHOLD_SIMPLE
1087 case SR_CONF_VOLTAGE_THRESHOLD:
1088 *data = std_gvar_thresholds(ARRAY_AND_SIZE(threshold_ranges));
1090 #else /* WITH_THRESHOLD_SIMPLE */
1091 case SR_CONF_VOLTAGE_THRESHOLD:
1092 *data = std_gvar_min_max_step_thresholds(
1093 LA2016_THR_VOLTAGE_MIN,
1094 LA2016_THR_VOLTAGE_MAX, 0.1);
1096 #endif /* WITH_THRESHOLD_SIMPLE */
1097 #endif /* WITH_THRESHOLD_DEVCFG */
1098 case SR_CONF_TRIGGER_MATCH:
1099 *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
1101 #if WITH_THRESHOLD_DEVCFG && !WITH_THRESHOLD_SIMPLE
1102 case SR_CONF_LOGIC_THRESHOLD:
1103 *data = g_variant_new_strv(ARRAY_AND_SIZE(logic_threshold));
1113 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
1115 struct sr_dev_driver *di;
1116 struct drv_context *drvc;
1117 struct sr_context *ctx;
1118 struct dev_context *devc;
1124 ctx = drvc->sr_ctx;;
1127 if (!devc->feed_queue) {
1128 devc->feed_queue = feed_queue_logic_alloc(sdi,
1129 LA2016_CONVBUFFER_SIZE, sizeof(uint16_t));
1130 if (!devc->feed_queue) {
1131 sr_err("Cannot allocate buffer for session feed.");
1132 return SR_ERR_MALLOC;
1136 sr_sw_limits_acquisition_start(&devc->sw_limits);
1138 voltage = threshold_voltage(sdi, NULL);
1139 ret = la2016_setup_acquisition(sdi, voltage);
1141 feed_queue_logic_free(devc->feed_queue);
1142 devc->feed_queue = NULL;
1146 ret = la2016_start_acquisition(sdi);
1148 la2016_abort_acquisition(sdi);
1149 feed_queue_logic_free(devc->feed_queue);
1150 devc->feed_queue = NULL;
1154 devc->completion_seen = FALSE;
1155 usb_source_add(sdi->session, ctx, 50,
1156 la2016_receive_data, (void *)sdi);
1158 std_session_send_df_header(sdi);
1163 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
1167 ret = la2016_abort_acquisition(sdi);
1172 static struct sr_dev_driver kingst_la2016_driver_info = {
1173 .name = "kingst-la2016",
1174 .longname = "Kingst LA2016",
1177 .cleanup = std_cleanup,
1179 .dev_list = std_dev_list,
1180 .dev_clear = std_dev_clear,
1181 .config_get = config_get,
1182 .config_set = config_set,
1183 .config_list = config_list,
1184 .dev_open = dev_open,
1185 .dev_close = dev_close,
1186 .dev_acquisition_start = dev_acquisition_start,
1187 .dev_acquisition_stop = dev_acquisition_stop,
1190 SR_REGISTER_DEV_DRIVER(kingst_la2016_driver_info);