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 SR_PRIV struct sr_dev_driver sysclk_lwla_driver_info;
76 static struct sr_dev_driver *const di = &sysclk_lwla_driver_info;
78 static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
80 return std_init(sr_ctx, di, LOG_PREFIX);
83 static struct sr_dev_inst *dev_inst_new(void)
85 struct sr_dev_inst *sdi;
86 struct dev_context *devc;
90 /* Allocate memory for our private driver context. */
91 devc = g_malloc0(sizeof(struct dev_context));
93 /* Register the device with libsigrok. */
94 sdi = g_malloc0(sizeof(struct sr_dev_inst));
95 sdi->status = SR_ST_INACTIVE;
96 sdi->vendor = g_strdup(VENDOR_NAME);
97 sdi->model = g_strdup(MODEL_NAME);
99 /* Enable all channels to match the default channel configuration. */
100 devc->channel_mask = ALL_CHANNELS_MASK;
101 devc->samplerate = DEFAULT_SAMPLERATE;
104 for (i = 0; i < NUM_CHANNELS; ++i) {
105 /* The LWLA series simply number channels from CH1 to CHxx. */
106 g_snprintf(name, sizeof(name), "CH%d", i + 1);
107 sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, name);
113 static GSList *scan(struct sr_dev_driver *di, GSList *options)
115 GSList *usb_devices, *devices, *node;
116 struct drv_context *drvc;
117 struct sr_dev_inst *sdi;
118 struct sr_usb_dev_inst *usb;
119 struct sr_config *src;
125 for (node = options; node != NULL; node = node->next) {
127 if (src->key == SR_CONF_CONN) {
128 conn = g_variant_get_string(src->data, NULL);
132 usb_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
135 for (node = usb_devices; node != NULL; node = node->next) {
138 /* Create sigrok device instance. */
139 sdi = dev_inst_new();
141 sr_usb_dev_inst_free(usb);
145 sdi->inst_type = SR_INST_USB;
148 /* Register device instance with driver. */
149 drvc->instances = g_slist_append(drvc->instances, sdi);
150 devices = g_slist_append(devices, sdi);
153 g_slist_free(usb_devices);
158 static GSList *dev_list(const struct sr_dev_driver *di)
160 struct drv_context *drvc;
164 return drvc->instances;
167 static void clear_dev_context(void *priv)
169 struct dev_context *devc;
173 sr_dbg("Device context cleared.");
175 lwla_free_acquisition_state(devc->acquisition);
179 static int dev_clear(const struct sr_dev_driver *di)
181 return std_dev_clear(di, &clear_dev_context);
184 static int dev_open(struct sr_dev_inst *sdi)
186 struct drv_context *drvc;
187 struct sr_usb_dev_inst *usb;
193 sr_err("Driver was not initialized.");
199 ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb);
203 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
205 sr_err("Failed to claim interface: %s.",
206 libusb_error_name(ret));
210 sdi->status = SR_ST_INITIALIZING;
212 ret = lwla_init_device(sdi);
215 sdi->status = SR_ST_ACTIVE;
220 static int dev_close(struct sr_dev_inst *sdi)
222 struct sr_usb_dev_inst *usb;
225 sr_err("Driver was not initialized.");
233 sdi->status = SR_ST_INACTIVE;
235 /* Trigger download of the shutdown bitstream. */
236 if (lwla_set_clock_config(sdi) != SR_OK)
237 sr_err("Unable to shut down device.");
239 libusb_release_interface(usb->devhdl, USB_INTERFACE);
240 libusb_close(usb->devhdl);
247 static int cleanup(const struct sr_dev_driver *di)
249 return dev_clear(di);
252 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
253 const struct sr_channel_group *cg)
255 struct dev_context *devc;
266 case SR_CONF_SAMPLERATE:
267 *data = g_variant_new_uint64(devc->samplerate);
269 case SR_CONF_LIMIT_MSEC:
270 *data = g_variant_new_uint64(devc->limit_msec);
272 case SR_CONF_LIMIT_SAMPLES:
273 *data = g_variant_new_uint64(devc->limit_samples);
275 case SR_CONF_EXTERNAL_CLOCK:
276 *data = g_variant_new_boolean(devc->cfg_clock_source
279 case SR_CONF_CLOCK_EDGE:
280 idx = devc->cfg_clock_edge;
281 if (idx >= ARRAY_SIZE(signal_edge_names))
283 *data = g_variant_new_string(signal_edge_names[idx]);
285 case SR_CONF_TRIGGER_SOURCE:
286 idx = devc->cfg_trigger_source;
287 if (idx >= ARRAY_SIZE(trigger_source_names))
289 *data = g_variant_new_string(trigger_source_names[idx]);
291 case SR_CONF_TRIGGER_SLOPE:
292 idx = devc->cfg_trigger_slope;
293 if (idx >= ARRAY_SIZE(signal_edge_names))
295 *data = g_variant_new_string(signal_edge_names[idx]);
304 /* Helper for mapping a string-typed configuration value to an index
305 * within a table of possible values.
307 static int lookup_index(GVariant *value, const char *const *table, int len)
312 entry = g_variant_get_string(value, NULL);
316 /* Linear search is fine for very small tables. */
317 for (i = 0; i < len; ++i) {
318 if (strcmp(entry, table[i]) == 0)
324 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
325 const struct sr_channel_group *cg)
328 struct dev_context *devc;
335 return SR_ERR_DEV_CLOSED;
338 case SR_CONF_SAMPLERATE:
339 value = g_variant_get_uint64(data);
340 if (value < samplerates[ARRAY_SIZE(samplerates) - 1]
341 || value > samplerates[0])
342 return SR_ERR_SAMPLERATE;
343 devc->samplerate = value;
345 case SR_CONF_LIMIT_MSEC:
346 value = g_variant_get_uint64(data);
347 if (value > MAX_LIMIT_MSEC)
349 devc->limit_msec = value;
351 case SR_CONF_LIMIT_SAMPLES:
352 value = g_variant_get_uint64(data);
353 if (value > MAX_LIMIT_SAMPLES)
355 devc->limit_samples = value;
357 case SR_CONF_EXTERNAL_CLOCK:
358 devc->cfg_clock_source = (g_variant_get_boolean(data))
359 ? CLOCK_EXT_CLK : CLOCK_INTERNAL;
361 case SR_CONF_CLOCK_EDGE:
362 idx = lookup_index(data, signal_edge_names,
363 ARRAY_SIZE(signal_edge_names));
366 devc->cfg_clock_edge = idx;
368 case SR_CONF_TRIGGER_SOURCE:
369 idx = lookup_index(data, trigger_source_names,
370 ARRAY_SIZE(trigger_source_names));
373 devc->cfg_trigger_source = idx;
375 case SR_CONF_TRIGGER_SLOPE:
376 idx = lookup_index(data, signal_edge_names,
377 ARRAY_SIZE(signal_edge_names));
380 devc->cfg_trigger_slope = idx;
389 static int config_channel_set(const struct sr_dev_inst *sdi,
390 struct sr_channel *ch, unsigned int changes)
392 uint64_t channel_bit;
393 struct dev_context *devc;
397 return SR_ERR_DEV_CLOSED;
399 if (ch->index < 0 || ch->index >= NUM_CHANNELS) {
400 sr_err("Channel index %d out of range.", ch->index);
403 channel_bit = (uint64_t)1 << ch->index;
405 if ((changes & SR_CHANNEL_SET_ENABLED) != 0) {
406 /* Enable or disable input channel for this channel. */
408 devc->channel_mask |= channel_bit;
410 devc->channel_mask &= ~channel_bit;
416 static int config_commit(const struct sr_dev_inst *sdi)
418 if (sdi->status != SR_ST_ACTIVE) {
419 sr_err("Device not ready (status %d).", (int)sdi->status);
423 return lwla_set_clock_config(sdi);
426 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
427 const struct sr_channel_group *cg)
436 case SR_CONF_SCAN_OPTIONS:
437 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
438 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
440 case SR_CONF_DEVICE_OPTIONS:
441 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
442 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
444 case SR_CONF_SAMPLERATE:
445 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
446 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
447 samplerates, ARRAY_SIZE(samplerates),
449 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
450 *data = g_variant_builder_end(&gvb);
452 case SR_CONF_TRIGGER_MATCH:
453 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
454 trigger_matches, ARRAY_SIZE(trigger_matches),
457 case SR_CONF_TRIGGER_SOURCE:
458 *data = g_variant_new_strv(trigger_source_names,
459 ARRAY_SIZE(trigger_source_names));
461 case SR_CONF_TRIGGER_SLOPE:
462 case SR_CONF_CLOCK_EDGE:
463 *data = g_variant_new_strv(signal_edge_names,
464 ARRAY_SIZE(signal_edge_names));
473 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
475 struct drv_context *drvc;
476 struct dev_context *devc;
477 struct acquisition_state *acq;
482 if (sdi->status != SR_ST_ACTIVE)
483 return SR_ERR_DEV_CLOSED;
488 if (devc->acquisition) {
489 sr_err("Acquisition still in progress?");
492 acq = lwla_alloc_acquisition_state();
494 return SR_ERR_MALLOC;
496 devc->stopping_in_progress = FALSE;
497 devc->transfer_error = FALSE;
499 sr_info("Starting acquisition.");
501 devc->acquisition = acq;
502 lwla_convert_trigger(sdi);
503 ret = lwla_setup_acquisition(sdi);
505 sr_err("Failed to set up acquisition.");
506 devc->acquisition = NULL;
507 lwla_free_acquisition_state(acq);
511 ret = lwla_start_acquisition(sdi);
513 sr_err("Failed to start acquisition.");
514 devc->acquisition = NULL;
515 lwla_free_acquisition_state(acq);
518 usb_source_add(sdi->session, drvc->sr_ctx, 100, &lwla_receive_data,
519 (struct sr_dev_inst *)sdi);
521 sr_info("Waiting for data.");
523 /* Send header packet to the session bus. */
524 std_session_send_df_header(sdi, LOG_PREFIX);
529 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
533 if (sdi->status != SR_ST_ACTIVE)
534 return SR_ERR_DEV_CLOSED;
536 sr_dbg("Stopping acquisition.");
538 sdi->status = SR_ST_STOPPING;
543 SR_PRIV struct sr_dev_driver sysclk_lwla_driver_info = {
544 .name = "sysclk-lwla",
545 .longname = "SysClk LWLA series",
550 .dev_list = dev_list,
551 .dev_clear = dev_clear,
552 .config_get = config_get,
553 .config_set = config_set,
554 .config_channel_set = config_channel_set,
555 .config_commit = config_commit,
556 .config_list = config_list,
557 .dev_open = dev_open,
558 .dev_close = dev_close,
559 .dev_acquisition_start = dev_acquisition_start,
560 .dev_acquisition_stop = dev_acquisition_stop,