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