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