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