]> sigrok.org Git - libsigrok.git/blame - hardware/sysclk-lwla/api.c
configure.ac: Don't build sysclk-lwla if libusb-1.0 is not found.
[libsigrok.git] / hardware / sysclk-lwla / api.c
CommitLineData
aeaad0b0
DE
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2014 Daniel Elstner <daniel.kitta@gmail.com>
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
20#include "protocol.h"
5874e88d
DE
21#include "libsigrok.h"
22#include "libsigrok-internal.h"
23#include <glib.h>
24#include <libusb.h>
25#include <stdlib.h>
26#include <string.h>
27
28static const int32_t hwcaps[] = {
29 SR_CONF_LOGIC_ANALYZER,
30 SR_CONF_SAMPLERATE,
31 SR_CONF_EXTERNAL_CLOCK,
32 SR_CONF_TRIGGER_TYPE,
33 SR_CONF_LIMIT_SAMPLES,
34};
35
36/* The hardware supports more samplerates than these, but these are the
37 * options hardcoded into the vendor's Windows GUI.
38 */
39static 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),
47};
aeaad0b0
DE
48
49SR_PRIV struct sr_dev_driver sysclk_lwla_driver_info;
5874e88d 50static struct sr_dev_driver *const di = &sysclk_lwla_driver_info;
aeaad0b0
DE
51
52static int init(struct sr_context *sr_ctx)
53{
54 return std_init(sr_ctx, di, LOG_PREFIX);
55}
56
5874e88d
DE
57static GSList *gen_probe_list(int num_probes)
58{
59 GSList *list;
60 struct sr_probe *probe;
61 int i;
62 char name[8];
63
64 list = NULL;
65
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);
69
70 probe = sr_probe_new(i - 1, SR_PROBE_LOGIC, TRUE, name);
71 list = g_slist_prepend(list, probe);
72 }
73
74 return list;
75}
76
aeaad0b0
DE
77static GSList *scan(GSList *options)
78{
5874e88d 79 GSList *usb_devices, *devices, *node;
aeaad0b0 80 struct drv_context *drvc;
5874e88d
DE
81 struct sr_dev_inst *sdi;
82 struct dev_context *devc;
83 struct sr_usb_dev_inst *usb;
7ebe9b9e
DE
84 struct sr_config *src;
85 const char *conn;
5874e88d 86 int device_index;
aeaad0b0 87
aeaad0b0
DE
88 drvc = di->priv;
89 drvc->instances = NULL;
7ebe9b9e 90 conn = USB_VID_PID;
5874e88d 91
7ebe9b9e
DE
92 for (node = options; node != NULL; node = node->next) {
93 src = node->data;
94 if (src->key == SR_CONF_CONN) {
95 conn = g_variant_get_string(src->data, NULL);
96 break;
97 }
98 }
99 usb_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
100 devices = NULL;
101 device_index = 0;
5874e88d
DE
102
103 for (node = usb_devices; node != NULL; node = node->next) {
104 usb = node->data;
105
106 /* Allocate memory for our private driver context. */
107 devc = g_try_new0(struct dev_context, 1);
108 if (!devc) {
109 sr_err("Device context malloc failed.");
110 sr_usb_dev_inst_free(usb);
111 continue;
112 }
113 /* Register the device with libsigrok. */
114 sdi = sr_dev_inst_new(device_index, SR_ST_INACTIVE,
115 VENDOR_NAME, MODEL_NAME, NULL);
116 if (!sdi) {
117 sr_err("Failed to instantiate device.");
118 g_free(devc);
119 sr_usb_dev_inst_free(usb);
120 continue;
121 }
122 sdi->driver = di;
123 sdi->priv = devc;
124 sdi->inst_type = SR_INST_USB;
125 sdi->conn = usb;
126 sdi->probes = gen_probe_list(NUM_PROBES);
127
128 drvc->instances = g_slist_append(drvc->instances, sdi);
129 devices = g_slist_append(devices, sdi);
130 }
aeaad0b0 131
5874e88d 132 g_slist_free(usb_devices);
aeaad0b0
DE
133
134 return devices;
135}
136
137static GSList *dev_list(void)
138{
5874e88d
DE
139 struct drv_context *drvc;
140
141 drvc = di->priv;
142
143 return drvc->instances;
144}
145
146static void clear_dev_context(void *priv)
147{
148 struct dev_context *devc;
149
150 devc = priv;
151
152 sr_dbg("Device context cleared.");
153
154 lwla_free_acquisition_state(devc->acquisition);
155 g_free(devc);
aeaad0b0
DE
156}
157
158static int dev_clear(void)
159{
5874e88d 160 return std_dev_clear(di, &clear_dev_context);
aeaad0b0
DE
161}
162
163static int dev_open(struct sr_dev_inst *sdi)
164{
5874e88d
DE
165 struct drv_context *drvc;
166 struct dev_context *devc;
167 struct sr_usb_dev_inst *usb;
168 int ret;
aeaad0b0 169
5874e88d 170 drvc = di->priv;
aeaad0b0 171
5874e88d
DE
172 if (!drvc) {
173 sr_err("Driver was not initialized.");
174 return SR_ERR;
175 }
aeaad0b0 176
5874e88d
DE
177 usb = sdi->conn;
178 devc = sdi->priv;
179
180 ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb);
181 if (ret != SR_OK)
182 return ret;
183
184 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
185 if (ret < 0) {
186 sr_err("Failed to claim interface: %s.",
187 libusb_error_name(ret));
188 return SR_ERR;
189 }
190
191 sdi->status = SR_ST_INITIALIZING;
192
193 if (devc->samplerate == 0)
194 /* Apply default if the samplerate hasn't been set yet. */
195 devc->samplerate = DEFAULT_SAMPLERATE;
196
197 ret = lwla_init_device(sdi);
198
199 if (ret == SR_OK)
200 sdi->status = SR_ST_ACTIVE;
201
202 return ret;
aeaad0b0
DE
203}
204
205static int dev_close(struct sr_dev_inst *sdi)
206{
5874e88d
DE
207 struct sr_usb_dev_inst *usb;
208 struct dev_context *devc;
209
210 if (!di->priv) {
211 sr_err("Driver was not initialized.");
212 return SR_ERR;
213 }
214
215 usb = sdi->conn;
216 devc = sdi->priv;
aeaad0b0 217
5874e88d
DE
218 if (!usb->devhdl)
219 return SR_OK;
aeaad0b0 220
5874e88d
DE
221 /* Trigger download of the shutdown bitstream. */
222 devc->selected_clock_source = CLOCK_SOURCE_NONE;
223
224 if (lwla_set_clock_source(sdi) != SR_OK)
225 sr_err("Unable to shut down device.");
226
227 libusb_release_interface(usb->devhdl, USB_INTERFACE);
228 libusb_close(usb->devhdl);
229
230 usb->devhdl = NULL;
aeaad0b0
DE
231 sdi->status = SR_ST_INACTIVE;
232
233 return SR_OK;
234}
235
236static int cleanup(void)
237{
5874e88d 238 return dev_clear();
aeaad0b0
DE
239}
240
241static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi,
5874e88d 242 const struct sr_probe_group *probe_group)
aeaad0b0 243{
5874e88d 244 struct dev_context *devc;
aeaad0b0 245
aeaad0b0
DE
246 (void)probe_group;
247
5874e88d
DE
248 if (!sdi)
249 return SR_ERR_ARG;
250
251 devc = sdi->priv;
252
aeaad0b0 253 switch (key) {
5874e88d
DE
254 case SR_CONF_SAMPLERATE:
255 *data = g_variant_new_uint64(devc->samplerate);
256 break;
257 case SR_CONF_LIMIT_SAMPLES:
258 *data = g_variant_new_uint64(devc->limit_samples);
259 break;
260 case SR_CONF_EXTERNAL_CLOCK:
261 *data = g_variant_new_boolean(devc->selected_clock_source
262 >= CLOCK_SOURCE_EXT_RISE);
263 break;
aeaad0b0
DE
264 default:
265 return SR_ERR_NA;
266 }
267
5874e88d 268 return SR_OK;
aeaad0b0
DE
269}
270
271static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
5874e88d 272 const struct sr_probe_group *probe_group)
aeaad0b0 273{
5874e88d
DE
274 struct dev_context *devc;
275 uint64_t rate;
aeaad0b0 276
aeaad0b0
DE
277 (void)probe_group;
278
5874e88d
DE
279 devc = sdi->priv;
280 if (!devc)
aeaad0b0
DE
281 return SR_ERR_DEV_CLOSED;
282
aeaad0b0 283 switch (key) {
5874e88d
DE
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;
291 break;
292 case SR_CONF_LIMIT_SAMPLES:
293 devc->limit_samples = g_variant_get_uint64(data);
294 break;
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;
300 } else {
301 sr_info("Disabling external clock.");
302 devc->selected_clock_source = CLOCK_SOURCE_INT;
303 }
304 if (sdi->status == SR_ST_ACTIVE)
305 return lwla_set_clock_source(sdi);
306 break;
aeaad0b0 307 default:
5874e88d 308 return SR_ERR_NA;
aeaad0b0
DE
309 }
310
5874e88d 311 return SR_OK;
aeaad0b0
DE
312}
313
314static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
5874e88d 315 const struct sr_probe_group *probe_group)
aeaad0b0 316{
5874e88d
DE
317 GVariant *gvar;
318 GVariantBuilder gvb;
aeaad0b0
DE
319
320 (void)sdi;
aeaad0b0
DE
321 (void)probe_group;
322
aeaad0b0 323 switch (key) {
5874e88d
DE
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));
327 break;
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),
332 sizeof(uint64_t));
333 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
334 *data = g_variant_builder_end(&gvb);
335 break;
336 case SR_CONF_TRIGGER_TYPE:
337 *data = g_variant_new_string(TRIGGER_TYPES);
338 break;
aeaad0b0
DE
339 default:
340 return SR_ERR_NA;
341 }
342
5874e88d 343 return SR_OK;
aeaad0b0
DE
344}
345
5874e88d 346static int configure_probes(const struct sr_dev_inst *sdi)
aeaad0b0 347{
5874e88d
DE
348 struct dev_context *devc;
349 const struct sr_probe *probe;
350 const GSList *node;
351 uint64_t probe_bit;
352
353 devc = sdi->priv;
354
355 devc->channel_mask = 0;
356 devc->trigger_mask = 0;
357 devc->trigger_edge_mask = 0;
358 devc->trigger_values = 0;
359
360 for (node = sdi->probes, probe_bit = 1;
361 node != NULL;
362 node = node->next, probe_bit <<= 1) {
363
364 if (probe_bit >= ((uint64_t)1 << NUM_PROBES)) {
365 sr_err("Channels over the limit of %d.", NUM_PROBES);
366 return SR_ERR;
367 }
368 probe = node->data;
369 if (!probe || !probe->enabled)
370 continue;
371
372 /* Enable input channel for this probe. */
373 devc->channel_mask |= probe_bit;
374
375 if (!probe->trigger || probe->trigger[0] == '\0')
376 continue;
377
378 if (probe->trigger[1] != '\0') {
379 sr_err("Only one trigger stage is supported.");
380 return SR_ERR;
381 }
382 /* Enable trigger for this probe. */
383 devc->trigger_mask |= probe_bit;
384
385 /* Configure edge mask and trigger value. */
386 switch (probe->trigger[0]) {
387 case '1': devc->trigger_values |= probe_bit;
388 case '0': break;
389
390 case 'r': devc->trigger_values |= probe_bit;
391 case 'f': devc->trigger_edge_mask |= probe_bit;
392 break;
393 default:
394 sr_err("Trigger type '%c' is not supported.",
395 probe->trigger[0]);
396 return SR_ERR;
397 }
398 }
399 return SR_OK;
400}
401
402static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
403{
404 struct drv_context *drvc;
405 struct dev_context *devc;
406 struct acquisition_state *acq;
407 int ret;
408
aeaad0b0
DE
409 (void)cb_data;
410
411 if (sdi->status != SR_ST_ACTIVE)
412 return SR_ERR_DEV_CLOSED;
413
5874e88d
DE
414 devc = sdi->priv;
415 drvc = di->priv;
416
417 if (devc->acquisition) {
418 sr_err("Acquisition still in progress?");
419 return SR_ERR;
420 }
421 acq = lwla_alloc_acquisition_state();
422 if (!acq)
423 return SR_ERR_MALLOC;
424
425 devc->stopping_in_progress = FALSE;
426 devc->transfer_error = FALSE;
427
428 ret = configure_probes(sdi);
429 if (ret != SR_OK) {
430 sr_err("Failed to configure probes.");
431 lwla_free_acquisition_state(acq);
432 return ret;
433 }
434
435 sr_info("Starting acquisition.");
436
437 devc->acquisition = acq;
438 ret = lwla_setup_acquisition(sdi);
439 if (ret != SR_OK) {
440 sr_err("Failed to set up aquisition.");
441 devc->acquisition = NULL;
442 lwla_free_acquisition_state(acq);
443 return ret;
444 }
445
446 ret = lwla_start_acquisition(sdi);
447 if (ret != SR_OK) {
448 sr_err("Failed to start aquisition.");
449 devc->acquisition = NULL;
450 lwla_free_acquisition_state(acq);
451 return ret;
452 }
453 usb_source_add(drvc->sr_ctx, 100, &lwla_receive_data,
454 (struct sr_dev_inst *)sdi);
455
456 sr_info("Waiting for data.");
457
458 /* Send header packet to the session bus. */
459 std_session_send_df_header(sdi, LOG_PREFIX);
aeaad0b0
DE
460
461 return SR_OK;
462}
463
464static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
465{
466 (void)cb_data;
467
468 if (sdi->status != SR_ST_ACTIVE)
469 return SR_ERR_DEV_CLOSED;
470
5874e88d
DE
471 sr_dbg("Stopping acquisition.");
472
473 sdi->status = SR_ST_STOPPING;
aeaad0b0
DE
474
475 return SR_OK;
476}
477
478SR_PRIV struct sr_dev_driver sysclk_lwla_driver_info = {
479 .name = "sysclk-lwla",
5874e88d 480 .longname = "SysClk LWLA series",
aeaad0b0
DE
481 .api_version = 1,
482 .init = init,
483 .cleanup = cleanup,
484 .scan = scan,
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,
494 .priv = NULL,
495};