]> sigrok.org Git - libsigrok.git/blame - hardware/sysclk-lwla/api.c
Replace 'probe' with 'channel' in most places.
[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
99c76642
DE
28static const int32_t hwopts[] = {
29 SR_CONF_CONN,
30};
31
5874e88d
DE
32static const int32_t hwcaps[] = {
33 SR_CONF_LOGIC_ANALYZER,
34 SR_CONF_SAMPLERATE,
35 SR_CONF_EXTERNAL_CLOCK,
6358f0a9 36 SR_CONF_CLOCK_EDGE,
5874e88d 37 SR_CONF_TRIGGER_TYPE,
e6e54bd2
DE
38 SR_CONF_TRIGGER_SOURCE,
39 SR_CONF_TRIGGER_SLOPE,
29d58767 40 SR_CONF_LIMIT_MSEC,
5874e88d
DE
41 SR_CONF_LIMIT_SAMPLES,
42};
43
44/* The hardware supports more samplerates than these, but these are the
45 * options hardcoded into the vendor's Windows GUI.
46 */
47static const uint64_t samplerates[] = {
48 SR_MHZ(125), SR_MHZ(100),
49 SR_MHZ(50), SR_MHZ(20), SR_MHZ(10),
50 SR_MHZ(5), SR_MHZ(2), SR_MHZ(1),
51 SR_KHZ(500), SR_KHZ(200), SR_KHZ(100),
52 SR_KHZ(50), SR_KHZ(20), SR_KHZ(10),
53 SR_KHZ(5), SR_KHZ(2), SR_KHZ(1),
54 SR_HZ(500), SR_HZ(200), SR_HZ(100),
55};
aeaad0b0 56
e6e54bd2
DE
57/* Names assigned to available trigger sources. Indices must match
58 * trigger_source enum values.
59 */
60static const char *const trigger_source_names[] = { "CH", "TRG" };
61
62/* Names assigned to available trigger slope choices. Indices must
6358f0a9 63 * match the signal_edge enum values.
e6e54bd2 64 */
6358f0a9 65static const char *const signal_edge_names[] = { "r", "f" };
e6e54bd2 66
aeaad0b0 67SR_PRIV struct sr_dev_driver sysclk_lwla_driver_info;
5874e88d 68static struct sr_dev_driver *const di = &sysclk_lwla_driver_info;
aeaad0b0
DE
69
70static int init(struct sr_context *sr_ctx)
71{
72 return std_init(sr_ctx, di, LOG_PREFIX);
73}
74
ba7dd8bb 75static GSList *gen_channel_list(int num_channels)
5874e88d
DE
76{
77 GSList *list;
ba7dd8bb 78 struct sr_channel *ch;
5874e88d
DE
79 int i;
80 char name[8];
81
82 list = NULL;
83
ba7dd8bb
UH
84 for (i = num_channels; i > 0; --i) {
85 /* The LWLA series simply number channels from CH1 to CHxx. */
1f98295d 86 g_snprintf(name, sizeof(name), "CH%d", i);
5874e88d 87
ba7dd8bb
UH
88 ch = sr_probe_new(i - 1, SR_PROBE_LOGIC, TRUE, name);
89 list = g_slist_prepend(list, ch);
5874e88d
DE
90 }
91
92 return list;
93}
94
43db3436
DE
95static struct sr_dev_inst *dev_inst_new(int device_index)
96{
97 struct sr_dev_inst *sdi;
98 struct dev_context *devc;
99
100 /* Allocate memory for our private driver context. */
101 devc = g_try_new0(struct dev_context, 1);
102 if (!devc) {
103 sr_err("Device context malloc failed.");
104 return NULL;
105 }
106
107 /* Register the device with libsigrok. */
108 sdi = sr_dev_inst_new(device_index, SR_ST_INACTIVE,
109 VENDOR_NAME, MODEL_NAME, NULL);
110 if (!sdi) {
111 sr_err("Failed to instantiate device.");
112 g_free(devc);
113 return NULL;
114 }
115
ba7dd8bb 116 /* Enable all channels to match the default channel configuration. */
43db3436
DE
117 devc->channel_mask = ALL_CHANNELS_MASK;
118 devc->samplerate = DEFAULT_SAMPLERATE;
119
120 sdi->priv = devc;
ba7dd8bb 121 sdi->channels = gen_channel_list(NUM_PROBES);
43db3436
DE
122
123 return sdi;
124}
125
aeaad0b0
DE
126static GSList *scan(GSList *options)
127{
5874e88d 128 GSList *usb_devices, *devices, *node;
aeaad0b0 129 struct drv_context *drvc;
5874e88d 130 struct sr_dev_inst *sdi;
5874e88d 131 struct sr_usb_dev_inst *usb;
7ebe9b9e
DE
132 struct sr_config *src;
133 const char *conn;
5874e88d 134 int device_index;
aeaad0b0 135
aeaad0b0 136 drvc = di->priv;
7ebe9b9e 137 conn = USB_VID_PID;
5874e88d 138
7ebe9b9e
DE
139 for (node = options; node != NULL; node = node->next) {
140 src = node->data;
141 if (src->key == SR_CONF_CONN) {
142 conn = g_variant_get_string(src->data, NULL);
143 break;
144 }
145 }
146 usb_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
147 devices = NULL;
50cad98d 148 device_index = g_slist_length(drvc->instances);
5874e88d
DE
149
150 for (node = usb_devices; node != NULL; node = node->next) {
151 usb = node->data;
152
43db3436
DE
153 /* Create sigrok device instance. */
154 sdi = dev_inst_new(device_index);
5874e88d 155 if (!sdi) {
5874e88d
DE
156 sr_usb_dev_inst_free(usb);
157 continue;
158 }
159 sdi->driver = di;
5874e88d
DE
160 sdi->inst_type = SR_INST_USB;
161 sdi->conn = usb;
5874e88d 162
43db3436 163 /* Register device instance with driver. */
5874e88d
DE
164 drvc->instances = g_slist_append(drvc->instances, sdi);
165 devices = g_slist_append(devices, sdi);
166 }
aeaad0b0 167
5874e88d 168 g_slist_free(usb_devices);
aeaad0b0
DE
169
170 return devices;
171}
172
173static GSList *dev_list(void)
174{
5874e88d
DE
175 struct drv_context *drvc;
176
177 drvc = di->priv;
178
179 return drvc->instances;
180}
181
182static void clear_dev_context(void *priv)
183{
184 struct dev_context *devc;
185
186 devc = priv;
187
188 sr_dbg("Device context cleared.");
189
190 lwla_free_acquisition_state(devc->acquisition);
191 g_free(devc);
aeaad0b0
DE
192}
193
194static int dev_clear(void)
195{
5874e88d 196 return std_dev_clear(di, &clear_dev_context);
aeaad0b0
DE
197}
198
199static int dev_open(struct sr_dev_inst *sdi)
200{
5874e88d 201 struct drv_context *drvc;
5874e88d
DE
202 struct sr_usb_dev_inst *usb;
203 int ret;
aeaad0b0 204
5874e88d 205 drvc = di->priv;
aeaad0b0 206
5874e88d
DE
207 if (!drvc) {
208 sr_err("Driver was not initialized.");
209 return SR_ERR;
210 }
aeaad0b0 211
43db3436 212 usb = sdi->conn;
5874e88d
DE
213
214 ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb);
215 if (ret != SR_OK)
216 return ret;
217
218 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
219 if (ret < 0) {
220 sr_err("Failed to claim interface: %s.",
221 libusb_error_name(ret));
222 return SR_ERR;
223 }
224
225 sdi->status = SR_ST_INITIALIZING;
226
5874e88d
DE
227 ret = lwla_init_device(sdi);
228
229 if (ret == SR_OK)
230 sdi->status = SR_ST_ACTIVE;
231
232 return ret;
aeaad0b0
DE
233}
234
235static int dev_close(struct sr_dev_inst *sdi)
236{
5874e88d 237 struct sr_usb_dev_inst *usb;
5874e88d
DE
238
239 if (!di->priv) {
240 sr_err("Driver was not initialized.");
241 return SR_ERR;
242 }
243
6358f0a9 244 usb = sdi->conn;
5874e88d
DE
245 if (!usb->devhdl)
246 return SR_OK;
aeaad0b0 247
6358f0a9 248 sdi->status = SR_ST_INACTIVE;
5874e88d 249
6358f0a9
DE
250 /* Trigger download of the shutdown bitstream. */
251 if (lwla_set_clock_config(sdi) != SR_OK)
5874e88d
DE
252 sr_err("Unable to shut down device.");
253
254 libusb_release_interface(usb->devhdl, USB_INTERFACE);
255 libusb_close(usb->devhdl);
256
257 usb->devhdl = NULL;
aeaad0b0
DE
258
259 return SR_OK;
260}
261
262static int cleanup(void)
263{
5874e88d 264 return dev_clear();
aeaad0b0
DE
265}
266
267static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi,
53b4680f 268 const struct sr_channel_group *cg)
aeaad0b0 269{
5874e88d 270 struct dev_context *devc;
e6e54bd2 271 size_t idx;
aeaad0b0 272
53b4680f 273 (void)cg;
aeaad0b0 274
5874e88d
DE
275 if (!sdi)
276 return SR_ERR_ARG;
277
278 devc = sdi->priv;
279
aeaad0b0 280 switch (key) {
5874e88d
DE
281 case SR_CONF_SAMPLERATE:
282 *data = g_variant_new_uint64(devc->samplerate);
283 break;
29d58767
DE
284 case SR_CONF_LIMIT_MSEC:
285 *data = g_variant_new_uint64(devc->limit_msec);
286 break;
5874e88d
DE
287 case SR_CONF_LIMIT_SAMPLES:
288 *data = g_variant_new_uint64(devc->limit_samples);
289 break;
290 case SR_CONF_EXTERNAL_CLOCK:
6358f0a9
DE
291 *data = g_variant_new_boolean(devc->cfg_clock_source
292 == CLOCK_EXT_CLK);
293 break;
294 case SR_CONF_CLOCK_EDGE:
295 idx = devc->cfg_clock_edge;
296 if (idx >= G_N_ELEMENTS(signal_edge_names))
297 return SR_ERR_BUG;
298 *data = g_variant_new_string(signal_edge_names[idx]);
5874e88d 299 break;
e6e54bd2
DE
300 case SR_CONF_TRIGGER_SOURCE:
301 idx = devc->cfg_trigger_source;
302 if (idx >= G_N_ELEMENTS(trigger_source_names))
303 return SR_ERR_BUG;
304 *data = g_variant_new_string(trigger_source_names[idx]);
305 break;
306 case SR_CONF_TRIGGER_SLOPE:
307 idx = devc->cfg_trigger_slope;
6358f0a9 308 if (idx >= G_N_ELEMENTS(signal_edge_names))
e6e54bd2 309 return SR_ERR_BUG;
6358f0a9 310 *data = g_variant_new_string(signal_edge_names[idx]);
e6e54bd2 311 break;
aeaad0b0
DE
312 default:
313 return SR_ERR_NA;
314 }
315
5874e88d 316 return SR_OK;
aeaad0b0
DE
317}
318
e6e54bd2
DE
319/* Helper for mapping a string-typed configuration value to an index
320 * within a table of possible values.
321 */
322static int lookup_index(GVariant *value, const char *const *table, int len)
323{
324 const char *entry;
325 int i;
326
327 entry = g_variant_get_string(value, NULL);
328 if (!entry)
329 return -1;
330
331 /* Linear search is fine for very small tables. */
332 for (i = 0; i < len; ++i) {
333 if (strcmp(entry, table[i]) == 0)
334 return i;
335 }
336 return -1;
337}
338
aeaad0b0 339static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
53b4680f 340 const struct sr_channel_group *cg)
aeaad0b0 341{
29d58767 342 uint64_t value;
5874e88d 343 struct dev_context *devc;
e6e54bd2 344 int idx;
aeaad0b0 345
53b4680f 346 (void)cg;
aeaad0b0 347
5874e88d
DE
348 devc = sdi->priv;
349 if (!devc)
aeaad0b0
DE
350 return SR_ERR_DEV_CLOSED;
351
aeaad0b0 352 switch (key) {
5874e88d 353 case SR_CONF_SAMPLERATE:
29d58767 354 value = g_variant_get_uint64(data);
29d58767
DE
355 if (value < samplerates[G_N_ELEMENTS(samplerates) - 1]
356 || value > samplerates[0])
5874e88d 357 return SR_ERR_SAMPLERATE;
29d58767
DE
358 devc->samplerate = value;
359 break;
360 case SR_CONF_LIMIT_MSEC:
361 value = g_variant_get_uint64(data);
362 if (value > MAX_LIMIT_MSEC)
363 return SR_ERR_ARG;
364 devc->limit_msec = value;
5874e88d
DE
365 break;
366 case SR_CONF_LIMIT_SAMPLES:
29d58767
DE
367 value = g_variant_get_uint64(data);
368 if (value > MAX_LIMIT_SAMPLES)
369 return SR_ERR_ARG;
370 devc->limit_samples = value;
5874e88d
DE
371 break;
372 case SR_CONF_EXTERNAL_CLOCK:
6358f0a9
DE
373 devc->cfg_clock_source = (g_variant_get_boolean(data))
374 ? CLOCK_EXT_CLK : CLOCK_INTERNAL;
375 break;
376 case SR_CONF_CLOCK_EDGE:
377 idx = lookup_index(data, signal_edge_names,
378 G_N_ELEMENTS(signal_edge_names));
379 if (idx < 0)
380 return SR_ERR_ARG;
381 devc->cfg_clock_edge = idx;
5874e88d 382 break;
e6e54bd2
DE
383 case SR_CONF_TRIGGER_SOURCE:
384 idx = lookup_index(data, trigger_source_names,
385 G_N_ELEMENTS(trigger_source_names));
386 if (idx < 0)
387 return SR_ERR_ARG;
388 devc->cfg_trigger_source = idx;
389 break;
390 case SR_CONF_TRIGGER_SLOPE:
6358f0a9
DE
391 idx = lookup_index(data, signal_edge_names,
392 G_N_ELEMENTS(signal_edge_names));
e6e54bd2
DE
393 if (idx < 0)
394 return SR_ERR_ARG;
395 devc->cfg_trigger_slope = idx;
396 break;
aeaad0b0 397 default:
5874e88d 398 return SR_ERR_NA;
aeaad0b0
DE
399 }
400
5874e88d 401 return SR_OK;
aeaad0b0
DE
402}
403
43db3436 404static int config_probe_set(const struct sr_dev_inst *sdi,
ba7dd8bb 405 struct sr_channel *ch, unsigned int changes)
43db3436 406{
ba7dd8bb 407 uint64_t channel_bit;
43db3436
DE
408 uint64_t trigger_mask;
409 uint64_t trigger_values;
410 uint64_t trigger_edge_mask;
411 struct dev_context *devc;
412
413 devc = sdi->priv;
414 if (!devc)
415 return SR_ERR_DEV_CLOSED;
416
ba7dd8bb
UH
417 if (ch->index < 0 || ch->index >= NUM_PROBES) {
418 sr_err("Channel index %d out of range.", ch->index);
43db3436
DE
419 return SR_ERR_BUG;
420 }
ba7dd8bb 421 channel_bit = (uint64_t)1 << ch->index;
43db3436
DE
422
423 if ((changes & SR_PROBE_SET_ENABLED) != 0) {
ba7dd8bb
UH
424 /* Enable or disable input channel for this channel. */
425 if (ch->enabled)
426 devc->channel_mask |= channel_bit;
43db3436 427 else
ba7dd8bb 428 devc->channel_mask &= ~channel_bit;
43db3436
DE
429 }
430
431 if ((changes & SR_PROBE_SET_TRIGGER) != 0) {
ba7dd8bb
UH
432 trigger_mask = devc->trigger_mask & ~channel_bit;
433 trigger_values = devc->trigger_values & ~channel_bit;
434 trigger_edge_mask = devc->trigger_edge_mask & ~channel_bit;
43db3436 435
ba7dd8bb
UH
436 if (ch->trigger && ch->trigger[0] != '\0') {
437 if (ch->trigger[1] != '\0') {
43db3436
DE
438 sr_warn("Trigger configuration \"%s\" with "
439 "multiple stages is not supported.",
ba7dd8bb 440 ch->trigger);
43db3436
DE
441 return SR_ERR_ARG;
442 }
ba7dd8bb
UH
443 /* Enable trigger for this channel. */
444 trigger_mask |= channel_bit;
43db3436
DE
445
446 /* Configure edge mask and trigger value. */
ba7dd8bb
UH
447 switch (ch->trigger[0]) {
448 case '1': trigger_values |= channel_bit;
43db3436
DE
449 case '0': break;
450
ba7dd8bb
UH
451 case 'r': trigger_values |= channel_bit;
452 case 'f': trigger_edge_mask |= channel_bit;
43db3436
DE
453 break;
454 default:
455 sr_warn("Trigger type '%c' is not supported.",
ba7dd8bb 456 ch->trigger[0]);
43db3436
DE
457 return SR_ERR_ARG;
458 }
459 }
460 /* Store validated trigger setup. */
461 devc->trigger_mask = trigger_mask;
462 devc->trigger_values = trigger_values;
463 devc->trigger_edge_mask = trigger_edge_mask;
464 }
465
466 return SR_OK;
467}
468
ee38c8ba
DE
469static int config_commit(const struct sr_dev_inst *sdi)
470{
471 if (sdi->status != SR_ST_ACTIVE) {
472 sr_err("Device not ready (status %d).", (int)sdi->status);
473 return SR_ERR;
474 }
475
6358f0a9 476 return lwla_set_clock_config(sdi);
ee38c8ba
DE
477}
478
aeaad0b0 479static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
53b4680f 480 const struct sr_channel_group *cg)
aeaad0b0 481{
5874e88d
DE
482 GVariant *gvar;
483 GVariantBuilder gvb;
aeaad0b0
DE
484
485 (void)sdi;
53b4680f 486 (void)cg;
aeaad0b0 487
aeaad0b0 488 switch (key) {
99c76642
DE
489 case SR_CONF_SCAN_OPTIONS:
490 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
491 hwopts, G_N_ELEMENTS(hwopts), sizeof(int32_t));
492 break;
5874e88d
DE
493 case SR_CONF_DEVICE_OPTIONS:
494 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
99c76642 495 hwcaps, G_N_ELEMENTS(hwcaps), sizeof(int32_t));
5874e88d
DE
496 break;
497 case SR_CONF_SAMPLERATE:
498 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
499 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
99c76642 500 samplerates, G_N_ELEMENTS(samplerates),
5874e88d
DE
501 sizeof(uint64_t));
502 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
503 *data = g_variant_builder_end(&gvb);
504 break;
505 case SR_CONF_TRIGGER_TYPE:
506 *data = g_variant_new_string(TRIGGER_TYPES);
507 break;
e6e54bd2
DE
508 case SR_CONF_TRIGGER_SOURCE:
509 *data = g_variant_new_strv(trigger_source_names,
510 G_N_ELEMENTS(trigger_source_names));
511 break;
512 case SR_CONF_TRIGGER_SLOPE:
6358f0a9
DE
513 case SR_CONF_CLOCK_EDGE:
514 *data = g_variant_new_strv(signal_edge_names,
515 G_N_ELEMENTS(signal_edge_names));
e6e54bd2 516 break;
aeaad0b0
DE
517 default:
518 return SR_ERR_NA;
519 }
520
5874e88d 521 return SR_OK;
aeaad0b0
DE
522}
523
5874e88d
DE
524static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
525{
526 struct drv_context *drvc;
527 struct dev_context *devc;
528 struct acquisition_state *acq;
529 int ret;
530
aeaad0b0
DE
531 (void)cb_data;
532
533 if (sdi->status != SR_ST_ACTIVE)
534 return SR_ERR_DEV_CLOSED;
535
5874e88d
DE
536 devc = sdi->priv;
537 drvc = di->priv;
538
539 if (devc->acquisition) {
540 sr_err("Acquisition still in progress?");
541 return SR_ERR;
542 }
543 acq = lwla_alloc_acquisition_state();
544 if (!acq)
545 return SR_ERR_MALLOC;
546
547 devc->stopping_in_progress = FALSE;
548 devc->transfer_error = FALSE;
549
5874e88d
DE
550 sr_info("Starting acquisition.");
551
552 devc->acquisition = acq;
553 ret = lwla_setup_acquisition(sdi);
554 if (ret != SR_OK) {
a84f6ad3 555 sr_err("Failed to set up acquisition.");
5874e88d
DE
556 devc->acquisition = NULL;
557 lwla_free_acquisition_state(acq);
558 return ret;
559 }
560
561 ret = lwla_start_acquisition(sdi);
562 if (ret != SR_OK) {
a84f6ad3 563 sr_err("Failed to start acquisition.");
5874e88d
DE
564 devc->acquisition = NULL;
565 lwla_free_acquisition_state(acq);
566 return ret;
567 }
568 usb_source_add(drvc->sr_ctx, 100, &lwla_receive_data,
569 (struct sr_dev_inst *)sdi);
570
571 sr_info("Waiting for data.");
572
573 /* Send header packet to the session bus. */
574 std_session_send_df_header(sdi, LOG_PREFIX);
aeaad0b0
DE
575
576 return SR_OK;
577}
578
579static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
580{
581 (void)cb_data;
582
583 if (sdi->status != SR_ST_ACTIVE)
584 return SR_ERR_DEV_CLOSED;
585
5874e88d
DE
586 sr_dbg("Stopping acquisition.");
587
588 sdi->status = SR_ST_STOPPING;
aeaad0b0
DE
589
590 return SR_OK;
591}
592
593SR_PRIV struct sr_dev_driver sysclk_lwla_driver_info = {
594 .name = "sysclk-lwla",
5874e88d 595 .longname = "SysClk LWLA series",
aeaad0b0
DE
596 .api_version = 1,
597 .init = init,
598 .cleanup = cleanup,
599 .scan = scan,
600 .dev_list = dev_list,
601 .dev_clear = dev_clear,
602 .config_get = config_get,
603 .config_set = config_set,
43db3436 604 .config_probe_set = config_probe_set,
ee38c8ba 605 .config_commit = config_commit,
aeaad0b0
DE
606 .config_list = config_list,
607 .dev_open = dev_open,
608 .dev_close = dev_close,
609 .dev_acquisition_start = dev_acquisition_start,
610 .dev_acquisition_stop = dev_acquisition_stop,
611 .priv = NULL,
612};