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 hwcaps[] = {
29 SR_CONF_LOGIC_ANALYZER,
31 SR_CONF_EXTERNAL_CLOCK,
33 SR_CONF_LIMIT_SAMPLES,
36 /* The hardware supports more samplerates than these, but these are the
37 * options hardcoded into the vendor's Windows GUI.
39 static const uint64_t samplerates[] = {
40 SR_MHZ(125), SR_MHZ(100),
41 SR_MHZ(50), SR_MHZ(20), SR_MHZ(10),
42 SR_MHZ(5), SR_MHZ(2), SR_MHZ(1),
43 SR_KHZ(500), SR_KHZ(200), SR_KHZ(100),
44 SR_KHZ(50), SR_KHZ(20), SR_KHZ(10),
45 SR_KHZ(5), SR_KHZ(2), SR_KHZ(1),
46 SR_HZ(500), SR_HZ(200), SR_HZ(100),
49 SR_PRIV struct sr_dev_driver sysclk_lwla_driver_info;
50 static struct sr_dev_driver *const di = &sysclk_lwla_driver_info;
52 static int init(struct sr_context *sr_ctx)
54 return std_init(sr_ctx, di, LOG_PREFIX);
57 static GSList *gen_probe_list(int num_probes)
60 struct sr_probe *probe;
66 for (i = num_probes; i > 0; --i) {
67 /* The LWLA series simply number probes from CH1 to CHxx. */
68 g_ascii_formatd(name, sizeof name, "CH%.0f", i);
70 probe = sr_probe_new(i - 1, SR_PROBE_LOGIC, TRUE, name);
71 list = g_slist_prepend(list, probe);
77 static GSList *scan(GSList *options)
79 GSList *usb_devices, *devices, *node;
80 struct drv_context *drvc;
81 struct sr_dev_inst *sdi;
82 struct dev_context *devc;
83 struct sr_usb_dev_inst *usb;
84 struct sr_config *src;
89 drvc->instances = NULL;
92 for (node = options; node != NULL; node = node->next) {
94 if (src->key == SR_CONF_CONN) {
95 conn = g_variant_get_string(src->data, NULL);
99 usb_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
103 for (node = usb_devices; node != NULL; node = node->next) {
106 /* Allocate memory for our private driver context. */
107 devc = g_try_new0(struct dev_context, 1);
109 sr_err("Device context malloc failed.");
110 sr_usb_dev_inst_free(usb);
113 /* Register the device with libsigrok. */
114 sdi = sr_dev_inst_new(device_index, SR_ST_INACTIVE,
115 VENDOR_NAME, MODEL_NAME, NULL);
117 sr_err("Failed to instantiate device.");
119 sr_usb_dev_inst_free(usb);
124 sdi->inst_type = SR_INST_USB;
126 sdi->probes = gen_probe_list(NUM_PROBES);
128 drvc->instances = g_slist_append(drvc->instances, sdi);
129 devices = g_slist_append(devices, sdi);
132 g_slist_free(usb_devices);
137 static GSList *dev_list(void)
139 struct drv_context *drvc;
143 return drvc->instances;
146 static void clear_dev_context(void *priv)
148 struct dev_context *devc;
152 sr_dbg("Device context cleared.");
154 lwla_free_acquisition_state(devc->acquisition);
158 static int dev_clear(void)
160 return std_dev_clear(di, &clear_dev_context);
163 static int dev_open(struct sr_dev_inst *sdi)
165 struct drv_context *drvc;
166 struct dev_context *devc;
167 struct sr_usb_dev_inst *usb;
173 sr_err("Driver was not initialized.");
180 ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb);
184 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
186 sr_err("Failed to claim interface: %s.",
187 libusb_error_name(ret));
191 sdi->status = SR_ST_INITIALIZING;
193 if (devc->samplerate == 0)
194 /* Apply default if the samplerate hasn't been set yet. */
195 devc->samplerate = DEFAULT_SAMPLERATE;
197 ret = lwla_init_device(sdi);
200 sdi->status = SR_ST_ACTIVE;
205 static int dev_close(struct sr_dev_inst *sdi)
207 struct sr_usb_dev_inst *usb;
208 struct dev_context *devc;
211 sr_err("Driver was not initialized.");
221 /* Trigger download of the shutdown bitstream. */
222 devc->selected_clock_source = CLOCK_SOURCE_NONE;
224 if (lwla_set_clock_source(sdi) != SR_OK)
225 sr_err("Unable to shut down device.");
227 libusb_release_interface(usb->devhdl, USB_INTERFACE);
228 libusb_close(usb->devhdl);
231 sdi->status = SR_ST_INACTIVE;
236 static int cleanup(void)
241 static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi,
242 const struct sr_probe_group *probe_group)
244 struct dev_context *devc;
254 case SR_CONF_SAMPLERATE:
255 *data = g_variant_new_uint64(devc->samplerate);
257 case SR_CONF_LIMIT_SAMPLES:
258 *data = g_variant_new_uint64(devc->limit_samples);
260 case SR_CONF_EXTERNAL_CLOCK:
261 *data = g_variant_new_boolean(devc->selected_clock_source
262 >= CLOCK_SOURCE_EXT_RISE);
271 static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
272 const struct sr_probe_group *probe_group)
274 struct dev_context *devc;
281 return SR_ERR_DEV_CLOSED;
284 case SR_CONF_SAMPLERATE:
285 rate = g_variant_get_uint64(data);
286 sr_info("Setting samplerate %" G_GUINT64_FORMAT, rate);
287 if (rate > samplerates[0]
288 || rate < samplerates[G_N_ELEMENTS(samplerates) - 1])
289 return SR_ERR_SAMPLERATE;
290 devc->samplerate = rate;
292 case SR_CONF_LIMIT_SAMPLES:
293 devc->limit_samples = g_variant_get_uint64(data);
295 case SR_CONF_EXTERNAL_CLOCK:
296 if (g_variant_get_boolean(data)) {
297 sr_info("Enabling external clock.");
298 /* TODO: Allow the external clock to be inverted */
299 devc->selected_clock_source = CLOCK_SOURCE_EXT_RISE;
301 sr_info("Disabling external clock.");
302 devc->selected_clock_source = CLOCK_SOURCE_INT;
304 if (sdi->status == SR_ST_ACTIVE)
305 return lwla_set_clock_source(sdi);
314 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
315 const struct sr_probe_group *probe_group)
324 case SR_CONF_DEVICE_OPTIONS:
325 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
326 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
328 case SR_CONF_SAMPLERATE:
329 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
330 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
331 samplerates, ARRAY_SIZE(samplerates),
333 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
334 *data = g_variant_builder_end(&gvb);
336 case SR_CONF_TRIGGER_TYPE:
337 *data = g_variant_new_string(TRIGGER_TYPES);
346 static int configure_probes(const struct sr_dev_inst *sdi)
348 struct dev_context *devc;
349 const struct sr_probe *probe;
355 devc->channel_mask = 0;
356 devc->trigger_mask = 0;
357 devc->trigger_edge_mask = 0;
358 devc->trigger_values = 0;
360 for (node = sdi->probes, probe_bit = 1;
362 node = node->next, probe_bit <<= 1) {
364 if (probe_bit >= ((uint64_t)1 << NUM_PROBES)) {
365 sr_err("Channels over the limit of %d.", NUM_PROBES);
369 if (!probe || !probe->enabled)
372 /* Enable input channel for this probe. */
373 devc->channel_mask |= probe_bit;
375 if (!probe->trigger || probe->trigger[0] == '\0')
378 if (probe->trigger[1] != '\0') {
379 sr_err("Only one trigger stage is supported.");
382 /* Enable trigger for this probe. */
383 devc->trigger_mask |= probe_bit;
385 /* Configure edge mask and trigger value. */
386 switch (probe->trigger[0]) {
387 case '1': devc->trigger_values |= probe_bit;
390 case 'r': devc->trigger_values |= probe_bit;
391 case 'f': devc->trigger_edge_mask |= probe_bit;
394 sr_err("Trigger type '%c' is not supported.",
402 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
404 struct drv_context *drvc;
405 struct dev_context *devc;
406 struct acquisition_state *acq;
411 if (sdi->status != SR_ST_ACTIVE)
412 return SR_ERR_DEV_CLOSED;
417 if (devc->acquisition) {
418 sr_err("Acquisition still in progress?");
421 acq = lwla_alloc_acquisition_state();
423 return SR_ERR_MALLOC;
425 devc->stopping_in_progress = FALSE;
426 devc->transfer_error = FALSE;
428 ret = configure_probes(sdi);
430 sr_err("Failed to configure probes.");
431 lwla_free_acquisition_state(acq);
435 sr_info("Starting acquisition.");
437 devc->acquisition = acq;
438 ret = lwla_setup_acquisition(sdi);
440 sr_err("Failed to set up aquisition.");
441 devc->acquisition = NULL;
442 lwla_free_acquisition_state(acq);
446 ret = lwla_start_acquisition(sdi);
448 sr_err("Failed to start aquisition.");
449 devc->acquisition = NULL;
450 lwla_free_acquisition_state(acq);
453 usb_source_add(drvc->sr_ctx, 100, &lwla_receive_data,
454 (struct sr_dev_inst *)sdi);
456 sr_info("Waiting for data.");
458 /* Send header packet to the session bus. */
459 std_session_send_df_header(sdi, LOG_PREFIX);
464 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
468 if (sdi->status != SR_ST_ACTIVE)
469 return SR_ERR_DEV_CLOSED;
471 sr_dbg("Stopping acquisition.");
473 sdi->status = SR_ST_STOPPING;
478 SR_PRIV struct sr_dev_driver sysclk_lwla_driver_info = {
479 .name = "sysclk-lwla",
480 .longname = "SysClk LWLA series",
485 .dev_list = dev_list,
486 .dev_clear = dev_clear,
487 .config_get = config_get,
488 .config_set = config_set,
489 .config_list = config_list,
490 .dev_open = dev_open,
491 .dev_close = dev_close,
492 .dev_acquisition_start = dev_acquisition_start,
493 .dev_acquisition_stop = dev_acquisition_stop,