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,
34 SR_CONF_LIMIT_SAMPLES,
37 /* The hardware supports more samplerates than these, but these are the
38 * options hardcoded into the vendor's Windows GUI.
40 static const uint64_t samplerates[] = {
41 SR_MHZ(125), SR_MHZ(100),
42 SR_MHZ(50), SR_MHZ(20), SR_MHZ(10),
43 SR_MHZ(5), SR_MHZ(2), SR_MHZ(1),
44 SR_KHZ(500), SR_KHZ(200), SR_KHZ(100),
45 SR_KHZ(50), SR_KHZ(20), SR_KHZ(10),
46 SR_KHZ(5), SR_KHZ(2), SR_KHZ(1),
47 SR_HZ(500), SR_HZ(200), SR_HZ(100),
50 SR_PRIV struct sr_dev_driver sysclk_lwla_driver_info;
51 static struct sr_dev_driver *const di = &sysclk_lwla_driver_info;
53 static int init(struct sr_context *sr_ctx)
55 return std_init(sr_ctx, di, LOG_PREFIX);
58 static GSList *gen_probe_list(int num_probes)
61 struct sr_probe *probe;
67 for (i = num_probes; i > 0; --i) {
68 /* The LWLA series simply number probes from CH1 to CHxx. */
69 g_snprintf(name, sizeof(name), "CH%d", i);
71 probe = sr_probe_new(i - 1, SR_PROBE_LOGIC, TRUE, name);
72 list = g_slist_prepend(list, probe);
78 static GSList *scan(GSList *options)
80 GSList *usb_devices, *devices, *node;
81 struct drv_context *drvc;
82 struct sr_dev_inst *sdi;
83 struct dev_context *devc;
84 struct sr_usb_dev_inst *usb;
85 struct sr_config *src;
90 drvc->instances = NULL;
93 for (node = options; node != NULL; node = node->next) {
95 if (src->key == SR_CONF_CONN) {
96 conn = g_variant_get_string(src->data, NULL);
100 usb_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
104 for (node = usb_devices; node != NULL; node = node->next) {
107 /* Allocate memory for our private driver context. */
108 devc = g_try_new0(struct dev_context, 1);
110 sr_err("Device context malloc failed.");
111 sr_usb_dev_inst_free(usb);
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.");
120 sr_usb_dev_inst_free(usb);
125 sdi->inst_type = SR_INST_USB;
127 sdi->probes = gen_probe_list(NUM_PROBES);
129 drvc->instances = g_slist_append(drvc->instances, sdi);
130 devices = g_slist_append(devices, sdi);
133 g_slist_free(usb_devices);
138 static GSList *dev_list(void)
140 struct drv_context *drvc;
144 return drvc->instances;
147 static void clear_dev_context(void *priv)
149 struct dev_context *devc;
153 sr_dbg("Device context cleared.");
155 lwla_free_acquisition_state(devc->acquisition);
159 static int dev_clear(void)
161 return std_dev_clear(di, &clear_dev_context);
164 static int dev_open(struct sr_dev_inst *sdi)
166 struct drv_context *drvc;
167 struct dev_context *devc;
168 struct sr_usb_dev_inst *usb;
174 sr_err("Driver was not initialized.");
181 ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb);
185 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
187 sr_err("Failed to claim interface: %s.",
188 libusb_error_name(ret));
192 sdi->status = SR_ST_INITIALIZING;
194 if (devc->samplerate == 0)
195 /* Apply default if the samplerate hasn't been set yet. */
196 devc->samplerate = DEFAULT_SAMPLERATE;
198 ret = lwla_init_device(sdi);
201 sdi->status = SR_ST_ACTIVE;
206 static int dev_close(struct sr_dev_inst *sdi)
208 struct sr_usb_dev_inst *usb;
209 struct dev_context *devc;
212 sr_err("Driver was not initialized.");
222 /* Trigger download of the shutdown bitstream. */
223 devc->selected_clock_source = CLOCK_SOURCE_NONE;
225 if (lwla_set_clock_source(sdi) != SR_OK)
226 sr_err("Unable to shut down device.");
228 libusb_release_interface(usb->devhdl, USB_INTERFACE);
229 libusb_close(usb->devhdl);
232 sdi->status = SR_ST_INACTIVE;
237 static int cleanup(void)
242 static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi,
243 const struct sr_probe_group *probe_group)
245 struct dev_context *devc;
255 case SR_CONF_SAMPLERATE:
256 *data = g_variant_new_uint64(devc->samplerate);
258 case SR_CONF_LIMIT_MSEC:
259 *data = g_variant_new_uint64(devc->limit_msec);
261 case SR_CONF_LIMIT_SAMPLES:
262 *data = g_variant_new_uint64(devc->limit_samples);
264 case SR_CONF_EXTERNAL_CLOCK:
265 *data = g_variant_new_boolean(devc->selected_clock_source
266 >= CLOCK_SOURCE_EXT_RISE);
275 static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
276 const struct sr_probe_group *probe_group)
279 struct dev_context *devc;
285 return SR_ERR_DEV_CLOSED;
288 case SR_CONF_SAMPLERATE:
289 value = g_variant_get_uint64(data);
290 sr_info("Setting samplerate %" PRIu64, value);
291 if (value < samplerates[G_N_ELEMENTS(samplerates) - 1]
292 || value > samplerates[0])
293 return SR_ERR_SAMPLERATE;
294 devc->samplerate = value;
296 case SR_CONF_LIMIT_MSEC:
297 value = g_variant_get_uint64(data);
298 if (value > MAX_LIMIT_MSEC)
300 devc->limit_msec = value;
302 case SR_CONF_LIMIT_SAMPLES:
303 value = g_variant_get_uint64(data);
304 if (value > MAX_LIMIT_SAMPLES)
306 devc->limit_samples = value;
308 case SR_CONF_EXTERNAL_CLOCK:
309 if (g_variant_get_boolean(data)) {
310 sr_info("Enabling external clock.");
311 /* TODO: Allow the external clock to be inverted */
312 devc->selected_clock_source = CLOCK_SOURCE_EXT_RISE;
314 sr_info("Disabling external clock.");
315 devc->selected_clock_source = CLOCK_SOURCE_INT;
317 if (sdi->status == SR_ST_ACTIVE)
318 return lwla_set_clock_source(sdi);
327 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
328 const struct sr_probe_group *probe_group)
337 case SR_CONF_DEVICE_OPTIONS:
338 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
339 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
341 case SR_CONF_SAMPLERATE:
342 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
343 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
344 samplerates, ARRAY_SIZE(samplerates),
346 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
347 *data = g_variant_builder_end(&gvb);
349 case SR_CONF_TRIGGER_TYPE:
350 *data = g_variant_new_string(TRIGGER_TYPES);
359 static int configure_probes(const struct sr_dev_inst *sdi)
361 struct dev_context *devc;
362 const struct sr_probe *probe;
368 devc->channel_mask = 0;
369 devc->trigger_mask = 0;
370 devc->trigger_edge_mask = 0;
371 devc->trigger_values = 0;
373 for (node = sdi->probes; node != NULL; node = node->next) {
375 if (!probe || !probe->enabled)
378 if (probe->index >= NUM_PROBES) {
379 sr_err("Channel index %d out of range.", probe->index);
382 probe_bit = (uint64_t)1 << probe->index;
384 /* Enable input channel for this probe. */
385 devc->channel_mask |= probe_bit;
387 if (!probe->trigger || probe->trigger[0] == '\0')
390 if (probe->trigger[1] != '\0') {
391 sr_err("Only one trigger stage is supported.");
394 /* Enable trigger for this probe. */
395 devc->trigger_mask |= probe_bit;
397 /* Configure edge mask and trigger value. */
398 switch (probe->trigger[0]) {
399 case '1': devc->trigger_values |= probe_bit;
402 case 'r': devc->trigger_values |= probe_bit;
403 case 'f': devc->trigger_edge_mask |= probe_bit;
406 sr_err("Trigger type '%c' is not supported.",
414 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
416 struct drv_context *drvc;
417 struct dev_context *devc;
418 struct acquisition_state *acq;
423 if (sdi->status != SR_ST_ACTIVE)
424 return SR_ERR_DEV_CLOSED;
429 if (devc->acquisition) {
430 sr_err("Acquisition still in progress?");
433 acq = lwla_alloc_acquisition_state();
435 return SR_ERR_MALLOC;
437 devc->stopping_in_progress = FALSE;
438 devc->transfer_error = FALSE;
440 ret = configure_probes(sdi);
442 sr_err("Failed to configure probes.");
443 lwla_free_acquisition_state(acq);
447 sr_info("Starting acquisition.");
449 devc->acquisition = acq;
450 ret = lwla_setup_acquisition(sdi);
452 sr_err("Failed to set up aquisition.");
453 devc->acquisition = NULL;
454 lwla_free_acquisition_state(acq);
458 ret = lwla_start_acquisition(sdi);
460 sr_err("Failed to start aquisition.");
461 devc->acquisition = NULL;
462 lwla_free_acquisition_state(acq);
465 usb_source_add(drvc->sr_ctx, 100, &lwla_receive_data,
466 (struct sr_dev_inst *)sdi);
468 sr_info("Waiting for data.");
470 /* Send header packet to the session bus. */
471 std_session_send_df_header(sdi, LOG_PREFIX);
476 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
480 if (sdi->status != SR_ST_ACTIVE)
481 return SR_ERR_DEV_CLOSED;
483 sr_dbg("Stopping acquisition.");
485 sdi->status = SR_ST_STOPPING;
490 SR_PRIV struct sr_dev_driver sysclk_lwla_driver_info = {
491 .name = "sysclk-lwla",
492 .longname = "SysClk LWLA series",
497 .dev_list = dev_list,
498 .dev_clear = dev_clear,
499 .config_get = config_get,
500 .config_set = config_set,
501 .config_list = config_list,
502 .dev_open = dev_open,
503 .dev_close = dev_close,
504 .dev_acquisition_start = dev_acquisition_start,
505 .dev_acquisition_stop = dev_acquisition_stop,