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/>.
21 #include "libsigrok.h"
22 #include "libsigrok-internal.h"
28 static const int32_t hwopts[] = {
32 static const int32_t hwcaps[] = {
33 SR_CONF_LOGIC_ANALYZER,
35 SR_CONF_EXTERNAL_CLOCK,
38 SR_CONF_TRIGGER_SOURCE,
39 SR_CONF_TRIGGER_SLOPE,
41 SR_CONF_LIMIT_SAMPLES,
44 /* The hardware supports more samplerates than these, but these are the
45 * options hardcoded into the vendor's Windows GUI.
47 static const uint64_t samplerates[] = {
48 SR_MHZ(125), SR_MHZ(100),
49 SR_MHZ(50), SR_MHZ(20), SR_MHZ(10),
50 SR_MHZ(5), SR_MHZ(2), SR_MHZ(1),
51 SR_KHZ(500), SR_KHZ(200), SR_KHZ(100),
52 SR_KHZ(50), SR_KHZ(20), SR_KHZ(10),
53 SR_KHZ(5), SR_KHZ(2), SR_KHZ(1),
54 SR_HZ(500), SR_HZ(200), SR_HZ(100),
57 /* Names assigned to available trigger sources. Indices must match
58 * trigger_source enum values.
60 static const char *const trigger_source_names[] = { "CH", "TRG" };
62 /* Names assigned to available trigger slope choices. Indices must
63 * match the signal_edge enum values.
65 static const char *const signal_edge_names[] = { "r", "f" };
67 SR_PRIV struct sr_dev_driver sysclk_lwla_driver_info;
68 static struct sr_dev_driver *const di = &sysclk_lwla_driver_info;
70 static int init(struct sr_context *sr_ctx)
72 return std_init(sr_ctx, di, LOG_PREFIX);
75 static GSList *gen_channel_list(int num_channels)
78 struct sr_channel *ch;
84 for (i = num_channels; i > 0; --i) {
85 /* The LWLA series simply number channels from CH1 to CHxx. */
86 g_snprintf(name, sizeof(name), "CH%d", i);
88 ch = sr_channel_new(i - 1, SR_CHANNEL_LOGIC, TRUE, name);
89 list = g_slist_prepend(list, ch);
95 static struct sr_dev_inst *dev_inst_new(int device_index)
97 struct sr_dev_inst *sdi;
98 struct dev_context *devc;
100 /* Allocate memory for our private driver context. */
101 devc = g_try_new0(struct dev_context, 1);
103 sr_err("Device context malloc failed.");
107 /* Register the device with libsigrok. */
108 sdi = sr_dev_inst_new(device_index, SR_ST_INACTIVE,
109 VENDOR_NAME, MODEL_NAME, NULL);
111 sr_err("Failed to instantiate device.");
116 /* Enable all channels to match the default channel configuration. */
117 devc->channel_mask = ALL_CHANNELS_MASK;
118 devc->samplerate = DEFAULT_SAMPLERATE;
121 sdi->channels = gen_channel_list(NUM_CHANNELS);
126 static GSList *scan(GSList *options)
128 GSList *usb_devices, *devices, *node;
129 struct drv_context *drvc;
130 struct sr_dev_inst *sdi;
131 struct sr_usb_dev_inst *usb;
132 struct sr_config *src;
139 for (node = options; node != NULL; node = node->next) {
141 if (src->key == SR_CONF_CONN) {
142 conn = g_variant_get_string(src->data, NULL);
146 usb_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
148 device_index = g_slist_length(drvc->instances);
150 for (node = usb_devices; node != NULL; node = node->next) {
153 /* Create sigrok device instance. */
154 sdi = dev_inst_new(device_index);
156 sr_usb_dev_inst_free(usb);
160 sdi->inst_type = SR_INST_USB;
163 /* Register device instance with driver. */
164 drvc->instances = g_slist_append(drvc->instances, sdi);
165 devices = g_slist_append(devices, sdi);
168 g_slist_free(usb_devices);
173 static GSList *dev_list(void)
175 struct drv_context *drvc;
179 return drvc->instances;
182 static void clear_dev_context(void *priv)
184 struct dev_context *devc;
188 sr_dbg("Device context cleared.");
190 lwla_free_acquisition_state(devc->acquisition);
194 static int dev_clear(void)
196 return std_dev_clear(di, &clear_dev_context);
199 static int dev_open(struct sr_dev_inst *sdi)
201 struct drv_context *drvc;
202 struct sr_usb_dev_inst *usb;
208 sr_err("Driver was not initialized.");
214 ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb);
218 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
220 sr_err("Failed to claim interface: %s.",
221 libusb_error_name(ret));
225 sdi->status = SR_ST_INITIALIZING;
227 ret = lwla_init_device(sdi);
230 sdi->status = SR_ST_ACTIVE;
235 static int dev_close(struct sr_dev_inst *sdi)
237 struct sr_usb_dev_inst *usb;
240 sr_err("Driver was not initialized.");
248 sdi->status = SR_ST_INACTIVE;
250 /* Trigger download of the shutdown bitstream. */
251 if (lwla_set_clock_config(sdi) != SR_OK)
252 sr_err("Unable to shut down device.");
254 libusb_release_interface(usb->devhdl, USB_INTERFACE);
255 libusb_close(usb->devhdl);
262 static int cleanup(void)
267 static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi,
268 const struct sr_channel_group *cg)
270 struct dev_context *devc;
281 case SR_CONF_SAMPLERATE:
282 *data = g_variant_new_uint64(devc->samplerate);
284 case SR_CONF_LIMIT_MSEC:
285 *data = g_variant_new_uint64(devc->limit_msec);
287 case SR_CONF_LIMIT_SAMPLES:
288 *data = g_variant_new_uint64(devc->limit_samples);
290 case SR_CONF_EXTERNAL_CLOCK:
291 *data = g_variant_new_boolean(devc->cfg_clock_source
294 case SR_CONF_CLOCK_EDGE:
295 idx = devc->cfg_clock_edge;
296 if (idx >= G_N_ELEMENTS(signal_edge_names))
298 *data = g_variant_new_string(signal_edge_names[idx]);
300 case SR_CONF_TRIGGER_SOURCE:
301 idx = devc->cfg_trigger_source;
302 if (idx >= G_N_ELEMENTS(trigger_source_names))
304 *data = g_variant_new_string(trigger_source_names[idx]);
306 case SR_CONF_TRIGGER_SLOPE:
307 idx = devc->cfg_trigger_slope;
308 if (idx >= G_N_ELEMENTS(signal_edge_names))
310 *data = g_variant_new_string(signal_edge_names[idx]);
319 /* Helper for mapping a string-typed configuration value to an index
320 * within a table of possible values.
322 static int lookup_index(GVariant *value, const char *const *table, int len)
327 entry = g_variant_get_string(value, NULL);
331 /* Linear search is fine for very small tables. */
332 for (i = 0; i < len; ++i) {
333 if (strcmp(entry, table[i]) == 0)
339 static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
340 const struct sr_channel_group *cg)
343 struct dev_context *devc;
350 return SR_ERR_DEV_CLOSED;
353 case SR_CONF_SAMPLERATE:
354 value = g_variant_get_uint64(data);
355 if (value < samplerates[G_N_ELEMENTS(samplerates) - 1]
356 || value > samplerates[0])
357 return SR_ERR_SAMPLERATE;
358 devc->samplerate = value;
360 case SR_CONF_LIMIT_MSEC:
361 value = g_variant_get_uint64(data);
362 if (value > MAX_LIMIT_MSEC)
364 devc->limit_msec = value;
366 case SR_CONF_LIMIT_SAMPLES:
367 value = g_variant_get_uint64(data);
368 if (value > MAX_LIMIT_SAMPLES)
370 devc->limit_samples = value;
372 case SR_CONF_EXTERNAL_CLOCK:
373 devc->cfg_clock_source = (g_variant_get_boolean(data))
374 ? CLOCK_EXT_CLK : CLOCK_INTERNAL;
376 case SR_CONF_CLOCK_EDGE:
377 idx = lookup_index(data, signal_edge_names,
378 G_N_ELEMENTS(signal_edge_names));
381 devc->cfg_clock_edge = idx;
383 case SR_CONF_TRIGGER_SOURCE:
384 idx = lookup_index(data, trigger_source_names,
385 G_N_ELEMENTS(trigger_source_names));
388 devc->cfg_trigger_source = idx;
390 case SR_CONF_TRIGGER_SLOPE:
391 idx = lookup_index(data, signal_edge_names,
392 G_N_ELEMENTS(signal_edge_names));
395 devc->cfg_trigger_slope = idx;
404 static int config_probe_set(const struct sr_dev_inst *sdi,
405 struct sr_channel *ch, unsigned int changes)
407 uint64_t channel_bit;
408 uint64_t trigger_mask;
409 uint64_t trigger_values;
410 uint64_t trigger_edge_mask;
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;
431 if ((changes & SR_CHANNEL_SET_TRIGGER) != 0) {
432 trigger_mask = devc->trigger_mask & ~channel_bit;
433 trigger_values = devc->trigger_values & ~channel_bit;
434 trigger_edge_mask = devc->trigger_edge_mask & ~channel_bit;
436 if (ch->trigger && ch->trigger[0] != '\0') {
437 if (ch->trigger[1] != '\0') {
438 sr_warn("Trigger configuration \"%s\" with "
439 "multiple stages is not supported.",
443 /* Enable trigger for this channel. */
444 trigger_mask |= channel_bit;
446 /* Configure edge mask and trigger value. */
447 switch (ch->trigger[0]) {
448 case '1': trigger_values |= channel_bit;
451 case 'r': trigger_values |= channel_bit;
452 case 'f': trigger_edge_mask |= channel_bit;
455 sr_warn("Trigger type '%c' is not supported.",
460 /* Store validated trigger setup. */
461 devc->trigger_mask = trigger_mask;
462 devc->trigger_values = trigger_values;
463 devc->trigger_edge_mask = trigger_edge_mask;
469 static int config_commit(const struct sr_dev_inst *sdi)
471 if (sdi->status != SR_ST_ACTIVE) {
472 sr_err("Device not ready (status %d).", (int)sdi->status);
476 return lwla_set_clock_config(sdi);
479 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
480 const struct sr_channel_group *cg)
489 case SR_CONF_SCAN_OPTIONS:
490 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
491 hwopts, G_N_ELEMENTS(hwopts), sizeof(int32_t));
493 case SR_CONF_DEVICE_OPTIONS:
494 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
495 hwcaps, G_N_ELEMENTS(hwcaps), sizeof(int32_t));
497 case SR_CONF_SAMPLERATE:
498 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
499 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
500 samplerates, G_N_ELEMENTS(samplerates),
502 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
503 *data = g_variant_builder_end(&gvb);
505 case SR_CONF_TRIGGER_TYPE:
506 *data = g_variant_new_string(TRIGGER_TYPES);
508 case SR_CONF_TRIGGER_SOURCE:
509 *data = g_variant_new_strv(trigger_source_names,
510 G_N_ELEMENTS(trigger_source_names));
512 case SR_CONF_TRIGGER_SLOPE:
513 case SR_CONF_CLOCK_EDGE:
514 *data = g_variant_new_strv(signal_edge_names,
515 G_N_ELEMENTS(signal_edge_names));
524 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
526 struct drv_context *drvc;
527 struct dev_context *devc;
528 struct acquisition_state *acq;
533 if (sdi->status != SR_ST_ACTIVE)
534 return SR_ERR_DEV_CLOSED;
539 if (devc->acquisition) {
540 sr_err("Acquisition still in progress?");
543 acq = lwla_alloc_acquisition_state();
545 return SR_ERR_MALLOC;
547 devc->stopping_in_progress = FALSE;
548 devc->transfer_error = FALSE;
550 sr_info("Starting acquisition.");
552 devc->acquisition = acq;
553 ret = lwla_setup_acquisition(sdi);
555 sr_err("Failed to set up acquisition.");
556 devc->acquisition = NULL;
557 lwla_free_acquisition_state(acq);
561 ret = lwla_start_acquisition(sdi);
563 sr_err("Failed to start acquisition.");
564 devc->acquisition = NULL;
565 lwla_free_acquisition_state(acq);
568 usb_source_add(drvc->sr_ctx, 100, &lwla_receive_data,
569 (struct sr_dev_inst *)sdi);
571 sr_info("Waiting for data.");
573 /* Send header packet to the session bus. */
574 std_session_send_df_header(sdi, LOG_PREFIX);
579 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
583 if (sdi->status != SR_ST_ACTIVE)
584 return SR_ERR_DEV_CLOSED;
586 sr_dbg("Stopping acquisition.");
588 sdi->status = SR_ST_STOPPING;
593 SR_PRIV struct sr_dev_driver sysclk_lwla_driver_info = {
594 .name = "sysclk-lwla",
595 .longname = "SysClk LWLA series",
600 .dev_list = dev_list,
601 .dev_clear = dev_clear,
602 .config_get = config_get,
603 .config_set = config_set,
604 .config_probe_set = config_probe_set,
605 .config_commit = config_commit,
606 .config_list = config_list,
607 .dev_open = dev_open,
608 .dev_close = dev_close,
609 .dev_acquisition_start = dev_acquisition_start,
610 .dev_acquisition_stop = dev_acquisition_stop,