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,
37 SR_CONF_TRIGGER_MATCH,
38 SR_CONF_TRIGGER_SOURCE,
39 SR_CONF_TRIGGER_SLOPE,
41 SR_CONF_LIMIT_SAMPLES,
44 static const int32_t trigger_matches[] = {
51 /* The hardware supports more samplerates than these, but these are the
52 * options hardcoded into the vendor's Windows GUI.
54 static const uint64_t samplerates[] = {
55 SR_MHZ(125), SR_MHZ(100),
56 SR_MHZ(50), SR_MHZ(20), SR_MHZ(10),
57 SR_MHZ(5), SR_MHZ(2), SR_MHZ(1),
58 SR_KHZ(500), SR_KHZ(200), SR_KHZ(100),
59 SR_KHZ(50), SR_KHZ(20), SR_KHZ(10),
60 SR_KHZ(5), SR_KHZ(2), SR_KHZ(1),
61 SR_HZ(500), SR_HZ(200), SR_HZ(100),
64 /* Names assigned to available trigger sources. Indices must match
65 * trigger_source enum values.
67 static const char *const trigger_source_names[] = { "CH", "TRG" };
69 /* Names assigned to available trigger slope choices. Indices must
70 * match the signal_edge enum values.
72 static const char *const signal_edge_names[] = { "r", "f" };
74 SR_PRIV struct sr_dev_driver sysclk_lwla_driver_info;
75 static struct sr_dev_driver *const di = &sysclk_lwla_driver_info;
77 static int init(struct sr_context *sr_ctx)
79 return std_init(sr_ctx, di, LOG_PREFIX);
82 static GSList *gen_channel_list(int num_channels)
85 struct sr_channel *ch;
91 for (i = num_channels; i > 0; --i) {
92 /* The LWLA series simply number channels from CH1 to CHxx. */
93 g_snprintf(name, sizeof(name), "CH%d", i);
95 ch = sr_channel_new(i - 1, SR_CHANNEL_LOGIC, TRUE, name);
96 list = g_slist_prepend(list, ch);
102 static struct sr_dev_inst *dev_inst_new(int device_index)
104 struct sr_dev_inst *sdi;
105 struct dev_context *devc;
107 /* Allocate memory for our private driver context. */
108 devc = g_try_new0(struct dev_context, 1);
110 sr_err("Device context malloc failed.");
114 /* Register the device with libsigrok. */
115 sdi = sr_dev_inst_new(device_index, SR_ST_INACTIVE,
116 VENDOR_NAME, MODEL_NAME, NULL);
118 sr_err("Failed to instantiate device.");
123 /* Enable all channels to match the default channel configuration. */
124 devc->channel_mask = ALL_CHANNELS_MASK;
125 devc->samplerate = DEFAULT_SAMPLERATE;
128 sdi->channels = gen_channel_list(NUM_CHANNELS);
133 static GSList *scan(GSList *options)
135 GSList *usb_devices, *devices, *node;
136 struct drv_context *drvc;
137 struct sr_dev_inst *sdi;
138 struct sr_usb_dev_inst *usb;
139 struct sr_config *src;
146 for (node = options; node != NULL; node = node->next) {
148 if (src->key == SR_CONF_CONN) {
149 conn = g_variant_get_string(src->data, NULL);
153 usb_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
155 device_index = g_slist_length(drvc->instances);
157 for (node = usb_devices; node != NULL; node = node->next) {
160 /* Create sigrok device instance. */
161 sdi = dev_inst_new(device_index);
163 sr_usb_dev_inst_free(usb);
167 sdi->inst_type = SR_INST_USB;
170 /* Register device instance with driver. */
171 drvc->instances = g_slist_append(drvc->instances, sdi);
172 devices = g_slist_append(devices, sdi);
175 g_slist_free(usb_devices);
180 static GSList *dev_list(void)
182 struct drv_context *drvc;
186 return drvc->instances;
189 static void clear_dev_context(void *priv)
191 struct dev_context *devc;
195 sr_dbg("Device context cleared.");
197 lwla_free_acquisition_state(devc->acquisition);
201 static int dev_clear(void)
203 return std_dev_clear(di, &clear_dev_context);
206 static int dev_open(struct sr_dev_inst *sdi)
208 struct drv_context *drvc;
209 struct sr_usb_dev_inst *usb;
215 sr_err("Driver was not initialized.");
221 ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb);
225 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
227 sr_err("Failed to claim interface: %s.",
228 libusb_error_name(ret));
232 sdi->status = SR_ST_INITIALIZING;
234 ret = lwla_init_device(sdi);
237 sdi->status = SR_ST_ACTIVE;
242 static int dev_close(struct sr_dev_inst *sdi)
244 struct sr_usb_dev_inst *usb;
247 sr_err("Driver was not initialized.");
255 sdi->status = SR_ST_INACTIVE;
257 /* Trigger download of the shutdown bitstream. */
258 if (lwla_set_clock_config(sdi) != SR_OK)
259 sr_err("Unable to shut down device.");
261 libusb_release_interface(usb->devhdl, USB_INTERFACE);
262 libusb_close(usb->devhdl);
269 static int cleanup(void)
274 static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi,
275 const struct sr_channel_group *cg)
277 struct dev_context *devc;
288 case SR_CONF_SAMPLERATE:
289 *data = g_variant_new_uint64(devc->samplerate);
291 case SR_CONF_LIMIT_MSEC:
292 *data = g_variant_new_uint64(devc->limit_msec);
294 case SR_CONF_LIMIT_SAMPLES:
295 *data = g_variant_new_uint64(devc->limit_samples);
297 case SR_CONF_EXTERNAL_CLOCK:
298 *data = g_variant_new_boolean(devc->cfg_clock_source
301 case SR_CONF_CLOCK_EDGE:
302 idx = devc->cfg_clock_edge;
303 if (idx >= G_N_ELEMENTS(signal_edge_names))
305 *data = g_variant_new_string(signal_edge_names[idx]);
307 case SR_CONF_TRIGGER_SOURCE:
308 idx = devc->cfg_trigger_source;
309 if (idx >= G_N_ELEMENTS(trigger_source_names))
311 *data = g_variant_new_string(trigger_source_names[idx]);
313 case SR_CONF_TRIGGER_SLOPE:
314 idx = devc->cfg_trigger_slope;
315 if (idx >= G_N_ELEMENTS(signal_edge_names))
317 *data = g_variant_new_string(signal_edge_names[idx]);
326 /* Helper for mapping a string-typed configuration value to an index
327 * within a table of possible values.
329 static int lookup_index(GVariant *value, const char *const *table, int len)
334 entry = g_variant_get_string(value, NULL);
338 /* Linear search is fine for very small tables. */
339 for (i = 0; i < len; ++i) {
340 if (strcmp(entry, table[i]) == 0)
346 static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
347 const struct sr_channel_group *cg)
350 struct dev_context *devc;
357 return SR_ERR_DEV_CLOSED;
360 case SR_CONF_SAMPLERATE:
361 value = g_variant_get_uint64(data);
362 if (value < samplerates[G_N_ELEMENTS(samplerates) - 1]
363 || value > samplerates[0])
364 return SR_ERR_SAMPLERATE;
365 devc->samplerate = value;
367 case SR_CONF_LIMIT_MSEC:
368 value = g_variant_get_uint64(data);
369 if (value > MAX_LIMIT_MSEC)
371 devc->limit_msec = value;
373 case SR_CONF_LIMIT_SAMPLES:
374 value = g_variant_get_uint64(data);
375 if (value > MAX_LIMIT_SAMPLES)
377 devc->limit_samples = value;
379 case SR_CONF_EXTERNAL_CLOCK:
380 devc->cfg_clock_source = (g_variant_get_boolean(data))
381 ? CLOCK_EXT_CLK : CLOCK_INTERNAL;
383 case SR_CONF_CLOCK_EDGE:
384 idx = lookup_index(data, signal_edge_names,
385 G_N_ELEMENTS(signal_edge_names));
388 devc->cfg_clock_edge = idx;
390 case SR_CONF_TRIGGER_SOURCE:
391 idx = lookup_index(data, trigger_source_names,
392 G_N_ELEMENTS(trigger_source_names));
395 devc->cfg_trigger_source = idx;
397 case SR_CONF_TRIGGER_SLOPE:
398 idx = lookup_index(data, signal_edge_names,
399 G_N_ELEMENTS(signal_edge_names));
402 devc->cfg_trigger_slope = idx;
411 static int config_channel_set(const struct sr_dev_inst *sdi,
412 struct sr_channel *ch, unsigned int changes)
414 uint64_t channel_bit;
415 struct dev_context *devc;
419 return SR_ERR_DEV_CLOSED;
421 if (ch->index < 0 || ch->index >= NUM_CHANNELS) {
422 sr_err("Channel index %d out of range.", ch->index);
425 channel_bit = (uint64_t)1 << ch->index;
427 if ((changes & SR_CHANNEL_SET_ENABLED) != 0) {
428 /* Enable or disable input channel for this channel. */
430 devc->channel_mask |= channel_bit;
432 devc->channel_mask &= ~channel_bit;
438 static int config_commit(const struct sr_dev_inst *sdi)
440 if (sdi->status != SR_ST_ACTIVE) {
441 sr_err("Device not ready (status %d).", (int)sdi->status);
445 return lwla_set_clock_config(sdi);
448 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
449 const struct sr_channel_group *cg)
458 case SR_CONF_SCAN_OPTIONS:
459 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
460 hwopts, G_N_ELEMENTS(hwopts), sizeof(int32_t));
462 case SR_CONF_DEVICE_OPTIONS:
463 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
464 hwcaps, G_N_ELEMENTS(hwcaps), sizeof(int32_t));
466 case SR_CONF_SAMPLERATE:
467 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
468 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
469 samplerates, G_N_ELEMENTS(samplerates),
471 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
472 *data = g_variant_builder_end(&gvb);
474 case SR_CONF_TRIGGER_MATCH:
475 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
476 trigger_matches, ARRAY_SIZE(trigger_matches),
479 case SR_CONF_TRIGGER_SOURCE:
480 *data = g_variant_new_strv(trigger_source_names,
481 G_N_ELEMENTS(trigger_source_names));
483 case SR_CONF_TRIGGER_SLOPE:
484 case SR_CONF_CLOCK_EDGE:
485 *data = g_variant_new_strv(signal_edge_names,
486 G_N_ELEMENTS(signal_edge_names));
495 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
497 struct drv_context *drvc;
498 struct dev_context *devc;
499 struct acquisition_state *acq;
504 if (sdi->status != SR_ST_ACTIVE)
505 return SR_ERR_DEV_CLOSED;
510 if (devc->acquisition) {
511 sr_err("Acquisition still in progress?");
514 acq = lwla_alloc_acquisition_state();
516 return SR_ERR_MALLOC;
518 devc->stopping_in_progress = FALSE;
519 devc->transfer_error = FALSE;
521 sr_info("Starting acquisition.");
523 devc->acquisition = acq;
524 lwla_convert_trigger(sdi);
525 ret = lwla_setup_acquisition(sdi);
527 sr_err("Failed to set up acquisition.");
528 devc->acquisition = NULL;
529 lwla_free_acquisition_state(acq);
533 ret = lwla_start_acquisition(sdi);
535 sr_err("Failed to start acquisition.");
536 devc->acquisition = NULL;
537 lwla_free_acquisition_state(acq);
540 usb_source_add(drvc->sr_ctx, 100, &lwla_receive_data,
541 (struct sr_dev_inst *)sdi);
543 sr_info("Waiting for data.");
545 /* Send header packet to the session bus. */
546 std_session_send_df_header(sdi, LOG_PREFIX);
551 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
555 if (sdi->status != SR_ST_ACTIVE)
556 return SR_ERR_DEV_CLOSED;
558 sr_dbg("Stopping acquisition.");
560 sdi->status = SR_ST_STOPPING;
565 SR_PRIV struct sr_dev_driver sysclk_lwla_driver_info = {
566 .name = "sysclk-lwla",
567 .longname = "SysClk LWLA series",
572 .dev_list = dev_list,
573 .dev_clear = dev_clear,
574 .config_get = config_get,
575 .config_set = config_set,
576 .config_channel_set = config_channel_set,
577 .config_commit = config_commit,
578 .config_list = config_list,
579 .dev_open = dev_open,
580 .dev_close = dev_close,
581 .dev_acquisition_start = dev_acquisition_start,
582 .dev_acquisition_stop = dev_acquisition_stop,