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 uint32_t scanopts[] = {
32 static const uint32_t devopts[] = {
33 SR_CONF_LOGIC_ANALYZER,
34 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
35 SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
36 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
37 SR_CONF_EXTERNAL_CLOCK | SR_CONF_GET | SR_CONF_SET,
38 SR_CONF_CLOCK_EDGE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
39 SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
40 SR_CONF_TRIGGER_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
41 SR_CONF_TRIGGER_SLOPE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
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_dev_driver *di, struct sr_context *sr_ctx)
79 return std_init(sr_ctx, di, LOG_PREFIX);
82 static struct sr_dev_inst *dev_inst_new(void)
84 struct sr_dev_inst *sdi;
85 struct dev_context *devc;
89 /* Allocate memory for our private driver context. */
90 devc = g_malloc0(sizeof(struct dev_context));
92 /* Register the device with libsigrok. */
93 sdi = g_malloc0(sizeof(struct sr_dev_inst));
94 sdi->status = SR_ST_INACTIVE;
95 sdi->vendor = g_strdup(VENDOR_NAME);
96 sdi->model = g_strdup(MODEL_NAME);
98 /* Enable all channels to match the default channel configuration. */
99 devc->channel_mask = ALL_CHANNELS_MASK;
100 devc->samplerate = DEFAULT_SAMPLERATE;
103 for (i = NUM_CHANNELS; i > 0; --i) {
104 /* The LWLA series simply number channels from CH1 to CHxx. */
105 g_snprintf(name, sizeof(name), "CH%d", i);
106 sr_channel_new(sdi, i - 1, SR_CHANNEL_LOGIC, TRUE, name);
112 static GSList *scan(struct sr_dev_driver *di, GSList *options)
114 GSList *usb_devices, *devices, *node;
115 struct drv_context *drvc;
116 struct sr_dev_inst *sdi;
117 struct sr_usb_dev_inst *usb;
118 struct sr_config *src;
124 for (node = options; node != NULL; node = node->next) {
126 if (src->key == SR_CONF_CONN) {
127 conn = g_variant_get_string(src->data, NULL);
131 usb_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
134 for (node = usb_devices; node != NULL; node = node->next) {
137 /* Create sigrok device instance. */
138 sdi = dev_inst_new();
140 sr_usb_dev_inst_free(usb);
144 sdi->inst_type = SR_INST_USB;
147 /* Register device instance with driver. */
148 drvc->instances = g_slist_append(drvc->instances, sdi);
149 devices = g_slist_append(devices, sdi);
152 g_slist_free(usb_devices);
157 static GSList *dev_list(const struct sr_dev_driver *di)
159 struct drv_context *drvc;
163 return drvc->instances;
166 static void clear_dev_context(void *priv)
168 struct dev_context *devc;
172 sr_dbg("Device context cleared.");
174 lwla_free_acquisition_state(devc->acquisition);
178 static int dev_clear(const struct sr_dev_driver *di)
180 return std_dev_clear(di, &clear_dev_context);
183 static int dev_open(struct sr_dev_inst *sdi)
185 struct drv_context *drvc;
186 struct sr_usb_dev_inst *usb;
192 sr_err("Driver was not initialized.");
198 ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb);
202 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
204 sr_err("Failed to claim interface: %s.",
205 libusb_error_name(ret));
209 sdi->status = SR_ST_INITIALIZING;
211 ret = lwla_init_device(sdi);
214 sdi->status = SR_ST_ACTIVE;
219 static int dev_close(struct sr_dev_inst *sdi)
221 struct sr_usb_dev_inst *usb;
224 sr_err("Driver was not initialized.");
232 sdi->status = SR_ST_INACTIVE;
234 /* Trigger download of the shutdown bitstream. */
235 if (lwla_set_clock_config(sdi) != SR_OK)
236 sr_err("Unable to shut down device.");
238 libusb_release_interface(usb->devhdl, USB_INTERFACE);
239 libusb_close(usb->devhdl);
246 static int cleanup(const struct sr_dev_driver *di)
248 return dev_clear(di);
251 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
252 const struct sr_channel_group *cg)
254 struct dev_context *devc;
265 case SR_CONF_SAMPLERATE:
266 *data = g_variant_new_uint64(devc->samplerate);
268 case SR_CONF_LIMIT_MSEC:
269 *data = g_variant_new_uint64(devc->limit_msec);
271 case SR_CONF_LIMIT_SAMPLES:
272 *data = g_variant_new_uint64(devc->limit_samples);
274 case SR_CONF_EXTERNAL_CLOCK:
275 *data = g_variant_new_boolean(devc->cfg_clock_source
278 case SR_CONF_CLOCK_EDGE:
279 idx = devc->cfg_clock_edge;
280 if (idx >= G_N_ELEMENTS(signal_edge_names))
282 *data = g_variant_new_string(signal_edge_names[idx]);
284 case SR_CONF_TRIGGER_SOURCE:
285 idx = devc->cfg_trigger_source;
286 if (idx >= G_N_ELEMENTS(trigger_source_names))
288 *data = g_variant_new_string(trigger_source_names[idx]);
290 case SR_CONF_TRIGGER_SLOPE:
291 idx = devc->cfg_trigger_slope;
292 if (idx >= G_N_ELEMENTS(signal_edge_names))
294 *data = g_variant_new_string(signal_edge_names[idx]);
303 /* Helper for mapping a string-typed configuration value to an index
304 * within a table of possible values.
306 static int lookup_index(GVariant *value, const char *const *table, int len)
311 entry = g_variant_get_string(value, NULL);
315 /* Linear search is fine for very small tables. */
316 for (i = 0; i < len; ++i) {
317 if (strcmp(entry, table[i]) == 0)
323 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
324 const struct sr_channel_group *cg)
327 struct dev_context *devc;
334 return SR_ERR_DEV_CLOSED;
337 case SR_CONF_SAMPLERATE:
338 value = g_variant_get_uint64(data);
339 if (value < samplerates[G_N_ELEMENTS(samplerates) - 1]
340 || value > samplerates[0])
341 return SR_ERR_SAMPLERATE;
342 devc->samplerate = value;
344 case SR_CONF_LIMIT_MSEC:
345 value = g_variant_get_uint64(data);
346 if (value > MAX_LIMIT_MSEC)
348 devc->limit_msec = value;
350 case SR_CONF_LIMIT_SAMPLES:
351 value = g_variant_get_uint64(data);
352 if (value > MAX_LIMIT_SAMPLES)
354 devc->limit_samples = value;
356 case SR_CONF_EXTERNAL_CLOCK:
357 devc->cfg_clock_source = (g_variant_get_boolean(data))
358 ? CLOCK_EXT_CLK : CLOCK_INTERNAL;
360 case SR_CONF_CLOCK_EDGE:
361 idx = lookup_index(data, signal_edge_names,
362 G_N_ELEMENTS(signal_edge_names));
365 devc->cfg_clock_edge = idx;
367 case SR_CONF_TRIGGER_SOURCE:
368 idx = lookup_index(data, trigger_source_names,
369 G_N_ELEMENTS(trigger_source_names));
372 devc->cfg_trigger_source = idx;
374 case SR_CONF_TRIGGER_SLOPE:
375 idx = lookup_index(data, signal_edge_names,
376 G_N_ELEMENTS(signal_edge_names));
379 devc->cfg_trigger_slope = idx;
388 static int config_channel_set(const struct sr_dev_inst *sdi,
389 struct sr_channel *ch, unsigned int changes)
391 uint64_t channel_bit;
392 struct dev_context *devc;
396 return SR_ERR_DEV_CLOSED;
398 if (ch->index < 0 || ch->index >= NUM_CHANNELS) {
399 sr_err("Channel index %d out of range.", ch->index);
402 channel_bit = (uint64_t)1 << ch->index;
404 if ((changes & SR_CHANNEL_SET_ENABLED) != 0) {
405 /* Enable or disable input channel for this channel. */
407 devc->channel_mask |= channel_bit;
409 devc->channel_mask &= ~channel_bit;
415 static int config_commit(const struct sr_dev_inst *sdi)
417 if (sdi->status != SR_ST_ACTIVE) {
418 sr_err("Device not ready (status %d).", (int)sdi->status);
422 return lwla_set_clock_config(sdi);
425 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
426 const struct sr_channel_group *cg)
435 case SR_CONF_SCAN_OPTIONS:
436 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
437 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
439 case SR_CONF_DEVICE_OPTIONS:
440 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
441 devopts, G_N_ELEMENTS(devopts), sizeof(uint32_t));
443 case SR_CONF_SAMPLERATE:
444 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
445 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
446 samplerates, G_N_ELEMENTS(samplerates),
448 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
449 *data = g_variant_builder_end(&gvb);
451 case SR_CONF_TRIGGER_MATCH:
452 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
453 trigger_matches, ARRAY_SIZE(trigger_matches),
456 case SR_CONF_TRIGGER_SOURCE:
457 *data = g_variant_new_strv(trigger_source_names,
458 G_N_ELEMENTS(trigger_source_names));
460 case SR_CONF_TRIGGER_SLOPE:
461 case SR_CONF_CLOCK_EDGE:
462 *data = g_variant_new_strv(signal_edge_names,
463 G_N_ELEMENTS(signal_edge_names));
472 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
474 struct drv_context *drvc;
475 struct dev_context *devc;
476 struct acquisition_state *acq;
481 if (sdi->status != SR_ST_ACTIVE)
482 return SR_ERR_DEV_CLOSED;
487 if (devc->acquisition) {
488 sr_err("Acquisition still in progress?");
491 acq = lwla_alloc_acquisition_state();
493 return SR_ERR_MALLOC;
495 devc->stopping_in_progress = FALSE;
496 devc->transfer_error = FALSE;
498 sr_info("Starting acquisition.");
500 devc->acquisition = acq;
501 lwla_convert_trigger(sdi);
502 ret = lwla_setup_acquisition(sdi);
504 sr_err("Failed to set up acquisition.");
505 devc->acquisition = NULL;
506 lwla_free_acquisition_state(acq);
510 ret = lwla_start_acquisition(sdi);
512 sr_err("Failed to start acquisition.");
513 devc->acquisition = NULL;
514 lwla_free_acquisition_state(acq);
517 usb_source_add(sdi->session, drvc->sr_ctx, 100, &lwla_receive_data,
518 (struct sr_dev_inst *)sdi);
520 sr_info("Waiting for data.");
522 /* Send header packet to the session bus. */
523 std_session_send_df_header(sdi, LOG_PREFIX);
528 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
532 if (sdi->status != SR_ST_ACTIVE)
533 return SR_ERR_DEV_CLOSED;
535 sr_dbg("Stopping acquisition.");
537 sdi->status = SR_ST_STOPPING;
542 SR_PRIV struct sr_dev_driver sysclk_lwla_driver_info = {
543 .name = "sysclk-lwla",
544 .longname = "SysClk LWLA series",
549 .dev_list = dev_list,
550 .dev_clear = dev_clear,
551 .config_get = config_get,
552 .config_set = config_set,
553 .config_channel_set = config_channel_set,
554 .config_commit = config_commit,
555 .config_list = config_list,
556 .dev_open = dev_open,
557 .dev_close = dev_close,
558 .dev_acquisition_start = dev_acquisition_start,
559 .dev_acquisition_stop = dev_acquisition_stop,