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,
38 SR_CONF_LIMIT_SAMPLES,
41 /* The hardware supports more samplerates than these, but these are the
42 * options hardcoded into the vendor's Windows GUI.
44 static const uint64_t samplerates[] = {
45 SR_MHZ(125), SR_MHZ(100),
46 SR_MHZ(50), SR_MHZ(20), SR_MHZ(10),
47 SR_MHZ(5), SR_MHZ(2), SR_MHZ(1),
48 SR_KHZ(500), SR_KHZ(200), SR_KHZ(100),
49 SR_KHZ(50), SR_KHZ(20), SR_KHZ(10),
50 SR_KHZ(5), SR_KHZ(2), SR_KHZ(1),
51 SR_HZ(500), SR_HZ(200), SR_HZ(100),
54 SR_PRIV struct sr_dev_driver sysclk_lwla_driver_info;
55 static struct sr_dev_driver *const di = &sysclk_lwla_driver_info;
57 static int init(struct sr_context *sr_ctx)
59 return std_init(sr_ctx, di, LOG_PREFIX);
62 static GSList *gen_probe_list(int num_probes)
65 struct sr_probe *probe;
71 for (i = num_probes; i > 0; --i) {
72 /* The LWLA series simply number probes from CH1 to CHxx. */
73 g_snprintf(name, sizeof(name), "CH%d", i);
75 probe = sr_probe_new(i - 1, SR_PROBE_LOGIC, TRUE, name);
76 list = g_slist_prepend(list, probe);
82 static struct sr_dev_inst *dev_inst_new(int device_index)
84 struct sr_dev_inst *sdi;
85 struct dev_context *devc;
87 /* Allocate memory for our private driver context. */
88 devc = g_try_new0(struct dev_context, 1);
90 sr_err("Device context malloc failed.");
94 /* Register the device with libsigrok. */
95 sdi = sr_dev_inst_new(device_index, SR_ST_INACTIVE,
96 VENDOR_NAME, MODEL_NAME, NULL);
98 sr_err("Failed to instantiate device.");
103 /* Enable all channels to match the default probe configuration. */
104 devc->channel_mask = ALL_CHANNELS_MASK;
105 devc->samplerate = DEFAULT_SAMPLERATE;
108 sdi->probes = gen_probe_list(NUM_PROBES);
113 static GSList *scan(GSList *options)
115 GSList *usb_devices, *devices, *node;
116 struct drv_context *drvc;
117 struct sr_dev_inst *sdi;
118 struct sr_usb_dev_inst *usb;
119 struct sr_config *src;
126 for (node = options; node != NULL; node = node->next) {
128 if (src->key == SR_CONF_CONN) {
129 conn = g_variant_get_string(src->data, NULL);
133 usb_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
135 device_index = g_slist_length(drvc->instances);
137 for (node = usb_devices; node != NULL; node = node->next) {
140 /* Create sigrok device instance. */
141 sdi = dev_inst_new(device_index);
143 sr_usb_dev_inst_free(usb);
147 sdi->inst_type = SR_INST_USB;
150 /* Register device instance with driver. */
151 drvc->instances = g_slist_append(drvc->instances, sdi);
152 devices = g_slist_append(devices, sdi);
155 g_slist_free(usb_devices);
160 static GSList *dev_list(void)
162 struct drv_context *drvc;
166 return drvc->instances;
169 static void clear_dev_context(void *priv)
171 struct dev_context *devc;
175 sr_dbg("Device context cleared.");
177 lwla_free_acquisition_state(devc->acquisition);
181 static int dev_clear(void)
183 return std_dev_clear(di, &clear_dev_context);
186 static int dev_open(struct sr_dev_inst *sdi)
188 struct drv_context *drvc;
189 struct sr_usb_dev_inst *usb;
195 sr_err("Driver was not initialized.");
201 ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb);
205 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
207 sr_err("Failed to claim interface: %s.",
208 libusb_error_name(ret));
212 sdi->status = SR_ST_INITIALIZING;
214 ret = lwla_init_device(sdi);
217 sdi->status = SR_ST_ACTIVE;
222 static int dev_close(struct sr_dev_inst *sdi)
224 struct sr_usb_dev_inst *usb;
225 struct dev_context *devc;
228 sr_err("Driver was not initialized.");
238 /* Trigger download of the shutdown bitstream. */
239 devc->selected_clock_source = CLOCK_SOURCE_NONE;
241 if (lwla_set_clock_source(sdi) != SR_OK)
242 sr_err("Unable to shut down device.");
244 libusb_release_interface(usb->devhdl, USB_INTERFACE);
245 libusb_close(usb->devhdl);
248 sdi->status = SR_ST_INACTIVE;
253 static int cleanup(void)
258 static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi,
259 const struct sr_probe_group *probe_group)
261 struct dev_context *devc;
271 case SR_CONF_SAMPLERATE:
272 *data = g_variant_new_uint64(devc->samplerate);
274 case SR_CONF_LIMIT_MSEC:
275 *data = g_variant_new_uint64(devc->limit_msec);
277 case SR_CONF_LIMIT_SAMPLES:
278 *data = g_variant_new_uint64(devc->limit_samples);
280 case SR_CONF_EXTERNAL_CLOCK:
281 *data = g_variant_new_boolean(devc->selected_clock_source
282 >= CLOCK_SOURCE_EXT_RISE);
291 static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
292 const struct sr_probe_group *probe_group)
295 struct dev_context *devc;
301 return SR_ERR_DEV_CLOSED;
304 case SR_CONF_SAMPLERATE:
305 value = g_variant_get_uint64(data);
306 if (value < samplerates[G_N_ELEMENTS(samplerates) - 1]
307 || value > samplerates[0])
308 return SR_ERR_SAMPLERATE;
309 devc->samplerate = value;
311 case SR_CONF_LIMIT_MSEC:
312 value = g_variant_get_uint64(data);
313 if (value > MAX_LIMIT_MSEC)
315 devc->limit_msec = value;
317 case SR_CONF_LIMIT_SAMPLES:
318 value = g_variant_get_uint64(data);
319 if (value > MAX_LIMIT_SAMPLES)
321 devc->limit_samples = value;
323 case SR_CONF_EXTERNAL_CLOCK:
324 if (g_variant_get_boolean(data)) {
325 sr_info("Enabling external clock.");
326 /* TODO: Allow the external clock to be inverted */
327 devc->selected_clock_source = CLOCK_SOURCE_EXT_RISE;
329 sr_info("Disabling external clock.");
330 devc->selected_clock_source = CLOCK_SOURCE_INT;
340 static int config_probe_set(const struct sr_dev_inst *sdi,
341 struct sr_probe *probe, unsigned int changes)
344 uint64_t trigger_mask;
345 uint64_t trigger_values;
346 uint64_t trigger_edge_mask;
347 struct dev_context *devc;
351 return SR_ERR_DEV_CLOSED;
353 if (probe->index < 0 || probe->index >= NUM_PROBES) {
354 sr_err("Probe index %d out of range.", probe->index);
357 probe_bit = (uint64_t)1 << probe->index;
359 if ((changes & SR_PROBE_SET_ENABLED) != 0) {
360 /* Enable or disable input channel for this probe. */
362 devc->channel_mask |= probe_bit;
364 devc->channel_mask &= ~probe_bit;
367 if ((changes & SR_PROBE_SET_TRIGGER) != 0) {
368 trigger_mask = devc->trigger_mask & ~probe_bit;
369 trigger_values = devc->trigger_values & ~probe_bit;
370 trigger_edge_mask = devc->trigger_edge_mask & ~probe_bit;
372 if (probe->trigger && probe->trigger[0] != '\0') {
373 if (probe->trigger[1] != '\0') {
374 sr_warn("Trigger configuration \"%s\" with "
375 "multiple stages is not supported.",
379 /* Enable trigger for this probe. */
380 trigger_mask |= probe_bit;
382 /* Configure edge mask and trigger value. */
383 switch (probe->trigger[0]) {
384 case '1': trigger_values |= probe_bit;
387 case 'r': trigger_values |= probe_bit;
388 case 'f': trigger_edge_mask |= probe_bit;
391 sr_warn("Trigger type '%c' is not supported.",
396 /* Store validated trigger setup. */
397 devc->trigger_mask = trigger_mask;
398 devc->trigger_values = trigger_values;
399 devc->trigger_edge_mask = trigger_edge_mask;
405 static int config_commit(const struct sr_dev_inst *sdi)
407 if (sdi->status != SR_ST_ACTIVE) {
408 sr_err("Device not ready (status %d).", (int)sdi->status);
412 return lwla_set_clock_source(sdi);
415 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
416 const struct sr_probe_group *probe_group)
425 case SR_CONF_SCAN_OPTIONS:
426 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
427 hwopts, G_N_ELEMENTS(hwopts), sizeof(int32_t));
429 case SR_CONF_DEVICE_OPTIONS:
430 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
431 hwcaps, G_N_ELEMENTS(hwcaps), sizeof(int32_t));
433 case SR_CONF_SAMPLERATE:
434 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
435 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
436 samplerates, G_N_ELEMENTS(samplerates),
438 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
439 *data = g_variant_builder_end(&gvb);
441 case SR_CONF_TRIGGER_TYPE:
442 *data = g_variant_new_string(TRIGGER_TYPES);
451 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
453 struct drv_context *drvc;
454 struct dev_context *devc;
455 struct acquisition_state *acq;
460 if (sdi->status != SR_ST_ACTIVE)
461 return SR_ERR_DEV_CLOSED;
466 if (devc->acquisition) {
467 sr_err("Acquisition still in progress?");
470 acq = lwla_alloc_acquisition_state();
472 return SR_ERR_MALLOC;
474 devc->stopping_in_progress = FALSE;
475 devc->transfer_error = FALSE;
477 sr_info("Starting acquisition.");
479 devc->acquisition = acq;
480 ret = lwla_setup_acquisition(sdi);
482 sr_err("Failed to set up aquisition.");
483 devc->acquisition = NULL;
484 lwla_free_acquisition_state(acq);
488 ret = lwla_start_acquisition(sdi);
490 sr_err("Failed to start aquisition.");
491 devc->acquisition = NULL;
492 lwla_free_acquisition_state(acq);
495 usb_source_add(drvc->sr_ctx, 100, &lwla_receive_data,
496 (struct sr_dev_inst *)sdi);
498 sr_info("Waiting for data.");
500 /* Send header packet to the session bus. */
501 std_session_send_df_header(sdi, LOG_PREFIX);
506 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
510 if (sdi->status != SR_ST_ACTIVE)
511 return SR_ERR_DEV_CLOSED;
513 sr_dbg("Stopping acquisition.");
515 sdi->status = SR_ST_STOPPING;
520 SR_PRIV struct sr_dev_driver sysclk_lwla_driver_info = {
521 .name = "sysclk-lwla",
522 .longname = "SysClk LWLA series",
527 .dev_list = dev_list,
528 .dev_clear = dev_clear,
529 .config_get = config_get,
530 .config_set = config_set,
531 .config_probe_set = config_probe_set,
532 .config_commit = config_commit,
533 .config_list = config_list,
534 .dev_open = dev_open,
535 .dev_close = dev_close,
536 .dev_acquisition_start = dev_acquisition_start,
537 .dev_acquisition_stop = dev_acquisition_stop,