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