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_SOURCE,
38 SR_CONF_TRIGGER_SLOPE,
40 SR_CONF_LIMIT_SAMPLES,
43 /* The hardware supports more samplerates than these, but these are the
44 * options hardcoded into the vendor's Windows GUI.
46 static const uint64_t samplerates[] = {
47 SR_MHZ(125), SR_MHZ(100),
48 SR_MHZ(50), SR_MHZ(20), SR_MHZ(10),
49 SR_MHZ(5), SR_MHZ(2), SR_MHZ(1),
50 SR_KHZ(500), SR_KHZ(200), SR_KHZ(100),
51 SR_KHZ(50), SR_KHZ(20), SR_KHZ(10),
52 SR_KHZ(5), SR_KHZ(2), SR_KHZ(1),
53 SR_HZ(500), SR_HZ(200), SR_HZ(100),
56 /* Names assigned to available trigger sources. Indices must match
57 * trigger_source enum values.
59 static const char *const trigger_source_names[] = { "CH", "TRG" };
61 /* Names assigned to available trigger slope choices. Indices must
62 * match trigger_slope enum values.
64 static const char *const trigger_slope_names[] = { "r", "f" };
66 SR_PRIV struct sr_dev_driver sysclk_lwla_driver_info;
67 static struct sr_dev_driver *const di = &sysclk_lwla_driver_info;
69 static int init(struct sr_context *sr_ctx)
71 return std_init(sr_ctx, di, LOG_PREFIX);
74 static GSList *gen_probe_list(int num_probes)
77 struct sr_probe *probe;
83 for (i = num_probes; i > 0; --i) {
84 /* The LWLA series simply number probes from CH1 to CHxx. */
85 g_snprintf(name, sizeof(name), "CH%d", i);
87 probe = sr_probe_new(i - 1, SR_PROBE_LOGIC, TRUE, name);
88 list = g_slist_prepend(list, probe);
94 static struct sr_dev_inst *dev_inst_new(int device_index)
96 struct sr_dev_inst *sdi;
97 struct dev_context *devc;
99 /* Allocate memory for our private driver context. */
100 devc = g_try_new0(struct dev_context, 1);
102 sr_err("Device context malloc failed.");
106 /* Register the device with libsigrok. */
107 sdi = sr_dev_inst_new(device_index, SR_ST_INACTIVE,
108 VENDOR_NAME, MODEL_NAME, NULL);
110 sr_err("Failed to instantiate device.");
115 /* Enable all channels to match the default probe configuration. */
116 devc->channel_mask = ALL_CHANNELS_MASK;
117 devc->samplerate = DEFAULT_SAMPLERATE;
120 sdi->probes = gen_probe_list(NUM_PROBES);
125 static GSList *scan(GSList *options)
127 GSList *usb_devices, *devices, *node;
128 struct drv_context *drvc;
129 struct sr_dev_inst *sdi;
130 struct sr_usb_dev_inst *usb;
131 struct sr_config *src;
138 for (node = options; node != NULL; node = node->next) {
140 if (src->key == SR_CONF_CONN) {
141 conn = g_variant_get_string(src->data, NULL);
145 usb_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
147 device_index = g_slist_length(drvc->instances);
149 for (node = usb_devices; node != NULL; node = node->next) {
152 /* Create sigrok device instance. */
153 sdi = dev_inst_new(device_index);
155 sr_usb_dev_inst_free(usb);
159 sdi->inst_type = SR_INST_USB;
162 /* Register device instance with driver. */
163 drvc->instances = g_slist_append(drvc->instances, sdi);
164 devices = g_slist_append(devices, sdi);
167 g_slist_free(usb_devices);
172 static GSList *dev_list(void)
174 struct drv_context *drvc;
178 return drvc->instances;
181 static void clear_dev_context(void *priv)
183 struct dev_context *devc;
187 sr_dbg("Device context cleared.");
189 lwla_free_acquisition_state(devc->acquisition);
193 static int dev_clear(void)
195 return std_dev_clear(di, &clear_dev_context);
198 static int dev_open(struct sr_dev_inst *sdi)
200 struct drv_context *drvc;
201 struct sr_usb_dev_inst *usb;
207 sr_err("Driver was not initialized.");
213 ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb);
217 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
219 sr_err("Failed to claim interface: %s.",
220 libusb_error_name(ret));
224 sdi->status = SR_ST_INITIALIZING;
226 ret = lwla_init_device(sdi);
229 sdi->status = SR_ST_ACTIVE;
234 static int dev_close(struct sr_dev_inst *sdi)
236 struct sr_usb_dev_inst *usb;
237 struct dev_context *devc;
240 sr_err("Driver was not initialized.");
250 /* Trigger download of the shutdown bitstream. */
251 devc->selected_clock_source = CLOCK_SOURCE_NONE;
253 if (lwla_set_clock_source(sdi) != SR_OK)
254 sr_err("Unable to shut down device.");
256 libusb_release_interface(usb->devhdl, USB_INTERFACE);
257 libusb_close(usb->devhdl);
260 sdi->status = SR_ST_INACTIVE;
265 static int cleanup(void)
270 static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi,
271 const struct sr_probe_group *probe_group)
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->selected_clock_source
295 >= CLOCK_SOURCE_EXT_RISE);
297 case SR_CONF_TRIGGER_SOURCE:
298 idx = devc->cfg_trigger_source;
299 if (idx >= G_N_ELEMENTS(trigger_source_names))
301 *data = g_variant_new_string(trigger_source_names[idx]);
303 case SR_CONF_TRIGGER_SLOPE:
304 idx = devc->cfg_trigger_slope;
305 if (idx >= G_N_ELEMENTS(trigger_slope_names))
307 *data = g_variant_new_string(trigger_slope_names[idx]);
316 /* Helper for mapping a string-typed configuration value to an index
317 * within a table of possible values.
319 static int lookup_index(GVariant *value, const char *const *table, int len)
324 entry = g_variant_get_string(value, NULL);
328 /* Linear search is fine for very small tables. */
329 for (i = 0; i < len; ++i) {
330 if (strcmp(entry, table[i]) == 0)
336 static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
337 const struct sr_probe_group *probe_group)
340 struct dev_context *devc;
347 return SR_ERR_DEV_CLOSED;
350 case SR_CONF_SAMPLERATE:
351 value = g_variant_get_uint64(data);
352 if (value < samplerates[G_N_ELEMENTS(samplerates) - 1]
353 || value > samplerates[0])
354 return SR_ERR_SAMPLERATE;
355 devc->samplerate = value;
357 case SR_CONF_LIMIT_MSEC:
358 value = g_variant_get_uint64(data);
359 if (value > MAX_LIMIT_MSEC)
361 devc->limit_msec = value;
363 case SR_CONF_LIMIT_SAMPLES:
364 value = g_variant_get_uint64(data);
365 if (value > MAX_LIMIT_SAMPLES)
367 devc->limit_samples = value;
369 case SR_CONF_EXTERNAL_CLOCK:
370 if (g_variant_get_boolean(data)) {
371 sr_info("Enabling external clock.");
372 /* TODO: Allow the external clock to be inverted */
373 devc->selected_clock_source = CLOCK_SOURCE_EXT_RISE;
375 sr_info("Disabling external clock.");
376 devc->selected_clock_source = CLOCK_SOURCE_INT;
379 case SR_CONF_TRIGGER_SOURCE:
380 idx = lookup_index(data, trigger_source_names,
381 G_N_ELEMENTS(trigger_source_names));
384 devc->cfg_trigger_source = idx;
386 case SR_CONF_TRIGGER_SLOPE:
387 idx = lookup_index(data, trigger_slope_names,
388 G_N_ELEMENTS(trigger_slope_names));
391 devc->cfg_trigger_slope = idx;
400 static int config_probe_set(const struct sr_dev_inst *sdi,
401 struct sr_probe *probe, unsigned int changes)
404 uint64_t trigger_mask;
405 uint64_t trigger_values;
406 uint64_t trigger_edge_mask;
407 struct dev_context *devc;
411 return SR_ERR_DEV_CLOSED;
413 if (probe->index < 0 || probe->index >= NUM_PROBES) {
414 sr_err("Probe index %d out of range.", probe->index);
417 probe_bit = (uint64_t)1 << probe->index;
419 if ((changes & SR_PROBE_SET_ENABLED) != 0) {
420 /* Enable or disable input channel for this probe. */
422 devc->channel_mask |= probe_bit;
424 devc->channel_mask &= ~probe_bit;
427 if ((changes & SR_PROBE_SET_TRIGGER) != 0) {
428 trigger_mask = devc->trigger_mask & ~probe_bit;
429 trigger_values = devc->trigger_values & ~probe_bit;
430 trigger_edge_mask = devc->trigger_edge_mask & ~probe_bit;
432 if (probe->trigger && probe->trigger[0] != '\0') {
433 if (probe->trigger[1] != '\0') {
434 sr_warn("Trigger configuration \"%s\" with "
435 "multiple stages is not supported.",
439 /* Enable trigger for this probe. */
440 trigger_mask |= probe_bit;
442 /* Configure edge mask and trigger value. */
443 switch (probe->trigger[0]) {
444 case '1': trigger_values |= probe_bit;
447 case 'r': trigger_values |= probe_bit;
448 case 'f': trigger_edge_mask |= probe_bit;
451 sr_warn("Trigger type '%c' is not supported.",
456 /* Store validated trigger setup. */
457 devc->trigger_mask = trigger_mask;
458 devc->trigger_values = trigger_values;
459 devc->trigger_edge_mask = trigger_edge_mask;
465 static int config_commit(const struct sr_dev_inst *sdi)
467 if (sdi->status != SR_ST_ACTIVE) {
468 sr_err("Device not ready (status %d).", (int)sdi->status);
472 return lwla_set_clock_source(sdi);
475 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
476 const struct sr_probe_group *probe_group)
485 case SR_CONF_SCAN_OPTIONS:
486 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
487 hwopts, G_N_ELEMENTS(hwopts), sizeof(int32_t));
489 case SR_CONF_DEVICE_OPTIONS:
490 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
491 hwcaps, G_N_ELEMENTS(hwcaps), sizeof(int32_t));
493 case SR_CONF_SAMPLERATE:
494 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
495 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
496 samplerates, G_N_ELEMENTS(samplerates),
498 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
499 *data = g_variant_builder_end(&gvb);
501 case SR_CONF_TRIGGER_TYPE:
502 *data = g_variant_new_string(TRIGGER_TYPES);
504 case SR_CONF_TRIGGER_SOURCE:
505 *data = g_variant_new_strv(trigger_source_names,
506 G_N_ELEMENTS(trigger_source_names));
508 case SR_CONF_TRIGGER_SLOPE:
509 *data = g_variant_new_strv(trigger_slope_names,
510 G_N_ELEMENTS(trigger_slope_names));
519 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
521 struct drv_context *drvc;
522 struct dev_context *devc;
523 struct acquisition_state *acq;
528 if (sdi->status != SR_ST_ACTIVE)
529 return SR_ERR_DEV_CLOSED;
534 if (devc->acquisition) {
535 sr_err("Acquisition still in progress?");
538 acq = lwla_alloc_acquisition_state();
540 return SR_ERR_MALLOC;
542 devc->stopping_in_progress = FALSE;
543 devc->transfer_error = FALSE;
545 sr_info("Starting acquisition.");
547 devc->acquisition = acq;
548 ret = lwla_setup_acquisition(sdi);
550 sr_err("Failed to set up aquisition.");
551 devc->acquisition = NULL;
552 lwla_free_acquisition_state(acq);
556 ret = lwla_start_acquisition(sdi);
558 sr_err("Failed to start aquisition.");
559 devc->acquisition = NULL;
560 lwla_free_acquisition_state(acq);
563 usb_source_add(drvc->sr_ctx, 100, &lwla_receive_data,
564 (struct sr_dev_inst *)sdi);
566 sr_info("Waiting for data.");
568 /* Send header packet to the session bus. */
569 std_session_send_df_header(sdi, LOG_PREFIX);
574 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
578 if (sdi->status != SR_ST_ACTIVE)
579 return SR_ERR_DEV_CLOSED;
581 sr_dbg("Stopping acquisition.");
583 sdi->status = SR_ST_STOPPING;
588 SR_PRIV struct sr_dev_driver sysclk_lwla_driver_info = {
589 .name = "sysclk-lwla",
590 .longname = "SysClk LWLA series",
595 .dev_list = dev_list,
596 .dev_clear = dev_clear,
597 .config_get = config_get,
598 .config_set = config_set,
599 .config_probe_set = config_probe_set,
600 .config_commit = config_commit,
601 .config_list = config_list,
602 .dev_open = dev_open,
603 .dev_close = dev_close,
604 .dev_acquisition_start = dev_acquisition_start,
605 .dev_acquisition_stop = dev_acquisition_stop,