2 * This file is part of the libsigrok project.
4 * Copyright (C) 2014 Daniel Elstner <daniel.kitta@gmail.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include <libsigrok/libsigrok.h>
26 #include "libsigrok-internal.h"
29 static const uint32_t scanopts[] = {
33 static const uint32_t devopts[] = {
34 SR_CONF_LOGIC_ANALYZER,
35 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
36 SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
37 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
38 SR_CONF_EXTERNAL_CLOCK | SR_CONF_GET | SR_CONF_SET,
39 SR_CONF_CLOCK_EDGE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
40 SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
41 SR_CONF_TRIGGER_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
42 SR_CONF_TRIGGER_SLOPE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
45 static const int32_t trigger_matches[] = {
52 /* The hardware supports more samplerates than these, but these are the
53 * options hardcoded into the vendor's Windows GUI.
55 static const uint64_t samplerates[] = {
56 SR_MHZ(125), SR_MHZ(100),
57 SR_MHZ(50), SR_MHZ(20), SR_MHZ(10),
58 SR_MHZ(5), SR_MHZ(2), SR_MHZ(1),
59 SR_KHZ(500), SR_KHZ(200), SR_KHZ(100),
60 SR_KHZ(50), SR_KHZ(20), SR_KHZ(10),
61 SR_KHZ(5), SR_KHZ(2), SR_KHZ(1),
62 SR_HZ(500), SR_HZ(200), SR_HZ(100),
65 /* Names assigned to available trigger sources. Indices must match
66 * trigger_source enum values.
68 static const char *const trigger_source_names[] = { "CH", "TRG" };
70 /* Names assigned to available trigger slope choices. Indices must
71 * match the signal_edge enum values.
73 static const char *const signal_edge_names[] = { "r", "f" };
75 static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
77 return std_init(sr_ctx, di, LOG_PREFIX);
80 static struct sr_dev_inst *dev_inst_new(void)
82 struct sr_dev_inst *sdi;
83 struct dev_context *devc;
87 /* Allocate memory for our private driver context. */
88 devc = g_malloc0(sizeof(struct dev_context));
90 /* Register the device with libsigrok. */
91 sdi = g_malloc0(sizeof(struct sr_dev_inst));
92 sdi->status = SR_ST_INACTIVE;
93 sdi->vendor = g_strdup(VENDOR_NAME);
94 sdi->model = g_strdup(MODEL_NAME);
96 /* Enable all channels to match the default channel configuration. */
97 devc->channel_mask = ALL_CHANNELS_MASK;
98 devc->samplerate = DEFAULT_SAMPLERATE;
101 for (i = 0; i < NUM_CHANNELS; ++i) {
102 /* The LWLA series simply number channels from CH1 to CHxx. */
103 g_snprintf(name, sizeof(name), "CH%d", i + 1);
104 sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, name);
110 static GSList *scan(struct sr_dev_driver *di, GSList *options)
112 GSList *usb_devices, *devices, *node;
113 struct drv_context *drvc;
114 struct sr_dev_inst *sdi;
115 struct sr_usb_dev_inst *usb;
116 struct sr_config *src;
122 for (node = options; node != NULL; node = node->next) {
124 if (src->key == SR_CONF_CONN) {
125 conn = g_variant_get_string(src->data, NULL);
129 usb_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
132 for (node = usb_devices; node != NULL; node = node->next) {
135 /* Create sigrok device instance. */
136 sdi = dev_inst_new();
138 sr_usb_dev_inst_free(usb);
142 sdi->inst_type = SR_INST_USB;
145 /* Register device instance with driver. */
146 drvc->instances = g_slist_append(drvc->instances, sdi);
147 devices = g_slist_append(devices, sdi);
150 g_slist_free(usb_devices);
155 static GSList *dev_list(const struct sr_dev_driver *di)
157 struct drv_context *drvc;
161 return drvc->instances;
164 static void clear_dev_context(void *priv)
166 struct dev_context *devc;
170 sr_dbg("Device context cleared.");
172 lwla_free_acquisition_state(devc->acquisition);
176 static int dev_clear(const struct sr_dev_driver *di)
178 return std_dev_clear(di, &clear_dev_context);
181 static int dev_open(struct sr_dev_inst *sdi)
183 struct drv_context *drvc;
184 struct sr_usb_dev_inst *usb;
187 drvc = sdi->driver->context;
190 sr_err("Driver was not initialized.");
193 if (sdi->status != SR_ST_INACTIVE) {
194 sr_err("Device already open.");
199 ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb);
203 /* Set the configuration twice to trigger a lightweight reset.
205 ret = libusb_set_configuration(usb->devhdl, USB_CONFIG);
207 ret = libusb_set_configuration(usb->devhdl, USB_CONFIG);
209 sr_err("Failed to set USB configuration: %s.",
210 libusb_error_name(ret));
215 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
217 sr_err("Failed to claim interface: %s.",
218 libusb_error_name(ret));
222 sdi->status = SR_ST_ACTIVE;
224 ret = lwla_init_device(sdi);
227 sdi->status = SR_ST_INACTIVE;
232 static int dev_close(struct sr_dev_inst *sdi)
234 struct sr_usb_dev_inst *usb;
235 struct dev_context *devc;
238 if (!sdi->driver->context) {
239 sr_err("Driver was not initialized.");
245 if (sdi->status == SR_ST_INACTIVE)
248 if (devc && devc->acquisition) {
249 sr_err("Attempt to close device during acquisition.");
252 sdi->status = SR_ST_INACTIVE;
254 /* Trigger download of the shutdown bitstream. */
255 ret = lwla_set_clock_config(sdi);
257 sr_err("Unable to shut down device.");
259 libusb_release_interface(usb->devhdl, USB_INTERFACE);
265 static int cleanup(const struct sr_dev_driver *di)
267 return dev_clear(di);
270 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
271 const struct sr_channel_group *cg)
273 struct dev_context *devc;
284 case SR_CONF_SAMPLERATE:
285 *data = g_variant_new_uint64(devc->samplerate);
287 case SR_CONF_LIMIT_MSEC:
288 *data = g_variant_new_uint64(devc->limit_msec);
290 case SR_CONF_LIMIT_SAMPLES:
291 *data = g_variant_new_uint64(devc->limit_samples);
293 case SR_CONF_EXTERNAL_CLOCK:
294 *data = g_variant_new_boolean(devc->cfg_clock_source
297 case SR_CONF_CLOCK_EDGE:
298 idx = devc->cfg_clock_edge;
299 if (idx >= ARRAY_SIZE(signal_edge_names))
301 *data = g_variant_new_string(signal_edge_names[idx]);
303 case SR_CONF_TRIGGER_SOURCE:
304 idx = devc->cfg_trigger_source;
305 if (idx >= ARRAY_SIZE(trigger_source_names))
307 *data = g_variant_new_string(trigger_source_names[idx]);
309 case SR_CONF_TRIGGER_SLOPE:
310 idx = devc->cfg_trigger_slope;
311 if (idx >= ARRAY_SIZE(signal_edge_names))
313 *data = g_variant_new_string(signal_edge_names[idx]);
322 /* Helper for mapping a string-typed configuration value to an index
323 * within a table of possible values.
325 static int lookup_index(GVariant *value, const char *const *table, int len)
330 entry = g_variant_get_string(value, NULL);
334 /* Linear search is fine for very small tables. */
335 for (i = 0; i < len; ++i) {
336 if (strcmp(entry, table[i]) == 0)
342 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
343 const struct sr_channel_group *cg)
346 struct dev_context *devc;
353 return SR_ERR_DEV_CLOSED;
356 case SR_CONF_SAMPLERATE:
357 value = g_variant_get_uint64(data);
358 if (value < samplerates[ARRAY_SIZE(samplerates) - 1]
359 || value > samplerates[0])
360 return SR_ERR_SAMPLERATE;
361 devc->samplerate = value;
363 case SR_CONF_LIMIT_MSEC:
364 value = g_variant_get_uint64(data);
365 if (value > MAX_LIMIT_MSEC)
367 devc->limit_msec = value;
369 case SR_CONF_LIMIT_SAMPLES:
370 value = g_variant_get_uint64(data);
371 if (value > MAX_LIMIT_SAMPLES)
373 devc->limit_samples = value;
375 case SR_CONF_EXTERNAL_CLOCK:
376 devc->cfg_clock_source = (g_variant_get_boolean(data))
377 ? CLOCK_EXT_CLK : CLOCK_INTERNAL;
379 case SR_CONF_CLOCK_EDGE:
380 idx = lookup_index(data, signal_edge_names,
381 ARRAY_SIZE(signal_edge_names));
384 devc->cfg_clock_edge = idx;
386 case SR_CONF_TRIGGER_SOURCE:
387 idx = lookup_index(data, trigger_source_names,
388 ARRAY_SIZE(trigger_source_names));
391 devc->cfg_trigger_source = idx;
393 case SR_CONF_TRIGGER_SLOPE:
394 idx = lookup_index(data, signal_edge_names,
395 ARRAY_SIZE(signal_edge_names));
398 devc->cfg_trigger_slope = idx;
407 static int config_channel_set(const struct sr_dev_inst *sdi,
408 struct sr_channel *ch, unsigned int changes)
410 uint64_t channel_bit;
411 struct dev_context *devc;
415 return SR_ERR_DEV_CLOSED;
417 if (ch->index < 0 || ch->index >= NUM_CHANNELS) {
418 sr_err("Channel index %d out of range.", ch->index);
421 channel_bit = (uint64_t)1 << ch->index;
423 if ((changes & SR_CHANNEL_SET_ENABLED) != 0) {
424 /* Enable or disable input channel for this channel. */
426 devc->channel_mask |= channel_bit;
428 devc->channel_mask &= ~channel_bit;
434 static int prepare_trigger_masks(const struct sr_dev_inst *sdi)
436 uint64_t trigger_mask;
437 uint64_t trigger_values;
438 uint64_t trigger_edge_mask;
439 uint64_t channel_bit;
440 struct dev_context *devc;
441 struct sr_trigger *trigger;
442 struct sr_trigger_stage *stage;
443 struct sr_trigger_match *match;
448 trigger = sr_session_trigger_get(sdi->session);
449 if (!trigger || !trigger->stages)
452 if (trigger->stages->next) {
453 sr_err("This device only supports 1 trigger stage.");
456 stage = trigger->stages->data;
460 trigger_edge_mask = 0;
462 for (node = stage->matches; node; node = node->next) {
465 if (!match->channel->enabled)
466 continue; /* ignore disabled channel */
468 channel_bit = (uint64_t)1 << match->channel->index;
469 trigger_mask |= channel_bit;
471 switch (match->match) {
472 case SR_TRIGGER_ZERO:
475 trigger_values |= channel_bit;
477 case SR_TRIGGER_RISING:
478 trigger_values |= channel_bit;
479 /* Fall through for edge mask. */
480 case SR_TRIGGER_FALLING:
481 trigger_edge_mask |= channel_bit;
484 sr_err("Unsupported trigger match for CH%d.",
485 match->channel->index + 1);
489 devc->trigger_mask = trigger_mask;
490 devc->trigger_values = trigger_values;
491 devc->trigger_edge_mask = trigger_edge_mask;
496 static int config_commit(const struct sr_dev_inst *sdi)
498 struct dev_context *devc;
501 if (sdi->status != SR_ST_ACTIVE)
502 return SR_ERR_DEV_CLOSED;
505 if (devc->acquisition) {
506 sr_err("Acquisition still in progress?");
509 rc = prepare_trigger_masks(sdi);
513 return lwla_set_clock_config(sdi);
516 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
517 const struct sr_channel_group *cg)
526 case SR_CONF_SCAN_OPTIONS:
527 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
528 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
530 case SR_CONF_DEVICE_OPTIONS:
531 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
532 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
534 case SR_CONF_SAMPLERATE:
535 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
536 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
537 samplerates, ARRAY_SIZE(samplerates),
539 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
540 *data = g_variant_builder_end(&gvb);
542 case SR_CONF_TRIGGER_MATCH:
543 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
544 trigger_matches, ARRAY_SIZE(trigger_matches),
547 case SR_CONF_TRIGGER_SOURCE:
548 *data = g_variant_new_strv(trigger_source_names,
549 ARRAY_SIZE(trigger_source_names));
551 case SR_CONF_TRIGGER_SLOPE:
552 case SR_CONF_CLOCK_EDGE:
553 *data = g_variant_new_strv(signal_edge_names,
554 ARRAY_SIZE(signal_edge_names));
563 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
565 struct drv_context *drvc;
566 struct dev_context *devc;
567 struct acquisition_state *acq;
572 if (sdi->status != SR_ST_ACTIVE)
573 return SR_ERR_DEV_CLOSED;
576 drvc = sdi->driver->context;
578 if (devc->acquisition) {
579 sr_err("Acquisition still in progress?");
582 acq = lwla_alloc_acquisition_state();
584 return SR_ERR_MALLOC;
586 devc->cancel_requested = FALSE;
587 devc->stopping_in_progress = FALSE;
588 devc->transfer_error = FALSE;
590 sr_info("Starting acquisition.");
592 devc->acquisition = acq;
593 ret = lwla_setup_acquisition(sdi);
595 sr_err("Failed to set up acquisition.");
596 devc->acquisition = NULL;
597 lwla_free_acquisition_state(acq);
601 ret = lwla_start_acquisition(sdi);
603 sr_err("Failed to start acquisition.");
604 devc->acquisition = NULL;
605 lwla_free_acquisition_state(acq);
608 usb_source_add(sdi->session, drvc->sr_ctx, 100, &lwla_receive_data,
609 (struct sr_dev_inst *)sdi);
611 sr_info("Waiting for data.");
613 /* Send header packet to the session bus. */
614 std_session_send_df_header(sdi, LOG_PREFIX);
619 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
621 struct dev_context *devc;
626 if (sdi->status != SR_ST_ACTIVE)
627 return SR_ERR_DEV_CLOSED;
629 if (devc->acquisition && !devc->cancel_requested) {
630 devc->cancel_requested = TRUE;
631 sr_dbg("Stopping acquisition.");
636 SR_PRIV struct sr_dev_driver sysclk_lwla_driver_info = {
637 .name = "sysclk-lwla",
638 .longname = "SysClk LWLA series",
643 .dev_list = dev_list,
644 .dev_clear = dev_clear,
645 .config_get = config_get,
646 .config_set = config_set,
647 .config_channel_set = config_channel_set,
648 .config_commit = config_commit,
649 .config_list = config_list,
650 .dev_open = dev_open,
651 .dev_close = dev_close,
652 .dev_acquisition_start = dev_acquisition_start,
653 .dev_acquisition_stop = dev_acquisition_stop,