]> sigrok.org Git - libsigrok.git/blame - src/hardware/sysclk-sla5032/api.c
sysclk-sla5032: Shorten a few code snippets a bit.
[libsigrok.git] / src / hardware / sysclk-sla5032 / api.c
CommitLineData
8da8c826
VV
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2014 Daniel Elstner <daniel.kitta@gmail.com>
5 * Copyright (C) 2019 Vitaliy Vorobyov
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include <config.h>
22#include <glib.h>
23#include <libusb.h>
24#include <stdlib.h>
25#include <string.h>
26#include <libsigrok/libsigrok.h>
27#include <libsigrok-internal.h>
28#include "protocol.h"
29#include "sla5032.h"
30
31 /* Number of logic channels. */
32#define NUM_CHANNELS 32
33
34static const uint32_t scanopts[] = {
35 SR_CONF_CONN,
36};
37
38static const uint32_t drvopts[] = {
39 SR_CONF_LOGIC_ANALYZER,
40};
41
42static const int32_t trigger_matches[] = {
43 SR_TRIGGER_ZERO,
44 SR_TRIGGER_ONE,
45 SR_TRIGGER_RISING,
46 SR_TRIGGER_FALLING,
47};
48
49static const uint64_t capture_ratios[] = {
50 0, 10, 20, 30, 50, 70, 90, 100,
51};
52
53static const uint32_t devopts[] = {
54 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
55 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
56 SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
57 SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
58 SR_CONF_RLE | SR_CONF_GET,
59};
60
61static const uint64_t samplerates[] = {
62 SR_MHZ(500), SR_MHZ(400), SR_MHZ(250), SR_MHZ(200), SR_MHZ(100),
63 SR_MHZ(50), SR_MHZ(25), SR_MHZ(20), SR_MHZ(10), SR_MHZ(5), SR_MHZ(2),
64 SR_MHZ(1), SR_KHZ(500), SR_KHZ(200), SR_KHZ(100), SR_KHZ(50),
65 SR_KHZ(20), SR_KHZ(10), SR_KHZ(5), SR_KHZ(2),
66};
67
68static struct sr_dev_inst *dev_inst_new(void)
69{
70 struct sr_dev_inst *sdi;
71 struct dev_context *devc;
72 int i;
73 char name[8];
74
75 devc = g_malloc0(sizeof(struct dev_context));
76 devc->active_fpga_config = FPGA_NOCONF;
77 devc->samplerate = samplerates[0];
78 devc->limit_samples = MAX_LIMIT_SAMPLES;
79 devc->capture_ratio = capture_ratios[4];
80 devc->channel_mask = (UINT64_C(1) << NUM_CHANNELS) - 1;
81
82 sdi = g_malloc0(sizeof(struct sr_dev_inst));
83 sdi->status = SR_ST_INACTIVE;
84 sdi->vendor = g_strdup("Sysclk");
85 sdi->model = g_strdup("SLA5032");
86 sdi->priv = devc;
87
88 for (i = 0; i < NUM_CHANNELS; i++) {
89 g_snprintf(name, sizeof(name), "CH%d", i);
90 sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, name);
91 }
92
93 return sdi;
94}
95
96/* Create a new device instance for a libusb device if it is a Sysclk SLA5032
97 * device and also matches the connection specification.
98 */
99static struct sr_dev_inst *dev_inst_new_matching(GSList *conn_matches,
100 libusb_device *dev)
101{
102 GSList *node;
103 struct sr_usb_dev_inst *usb;
104 struct sr_dev_inst *sdi;
105 struct libusb_device_descriptor des;
106 int bus, address, ret;
107 unsigned int vid, pid;
108
109 bus = libusb_get_bus_number(dev);
110 address = libusb_get_device_address(dev);
111
112 for (node = conn_matches; node != NULL; node = node->next) {
113 usb = node->data;
114 if (usb && usb->bus == bus && usb->address == address)
115 break; /* found */
116 }
117 if (conn_matches && !node)
118 return NULL; /* no match */
119
120 ret = libusb_get_device_descriptor(dev, &des);
121 if (ret != 0) {
122 sr_err("Failed to get USB device descriptor: %s.",
123 libusb_error_name(ret));
124 return NULL;
125 }
126 vid = des.idVendor;
127 pid = des.idProduct;
128
129 /* Create sigrok device instance. */
130 if (vid == USB_VID_SYSCLK && pid == USB_PID_SLA5032) {
131 } else {
132 if (conn_matches)
133 sr_warn("USB device %d.%d (%04x:%04x) is not a"
134 " Sysclk SLA5032.", bus, address, vid, pid);
135 return NULL;
136 }
137 sdi = dev_inst_new();
138
139 sdi->inst_type = SR_INST_USB;
140 sdi->conn = sr_usb_dev_inst_new(bus, address, NULL);
141
142 return sdi;
143}
144
145static GSList *scan(struct sr_dev_driver *di, GSList *options)
146{
147 GSList *conn_devices, *devices, *node;
148 struct drv_context *drvc;
149 struct sr_dev_inst *sdi;
150 struct sr_config *src;
151 const char *conn;
152 libusb_device **devlist;
153 ssize_t num_devs, i;
154
155 drvc = di->context;
156 conn = NULL;
157 conn_devices = NULL;
158 devices = NULL;
159
160 for (node = options; node != NULL; node = node->next) {
161 src = node->data;
162 if (src->key == SR_CONF_CONN) {
163 conn = g_variant_get_string(src->data, NULL);
164 break;
165 }
166 }
167 if (conn) {
168 /* Find devices matching the connection specification. */
169 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
170 }
171
172 /* List all libusb devices. */
173 num_devs = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
174 if (num_devs < 0) {
175 sr_err("Failed to list USB devices: %s.",
176 libusb_error_name(num_devs));
177 g_slist_free_full(conn_devices,
178 (GDestroyNotify)&sr_usb_dev_inst_free);
179 return NULL;
180 }
181
182 /* Scan the USB device list for matching devices. */
183 for (i = 0; i < num_devs; i++) {
184 sdi = dev_inst_new_matching(conn_devices, devlist[i]);
185 if (!sdi)
186 continue; /* no match */
187
188 /* Register device instance with driver. */
189 devices = g_slist_append(devices, sdi);
190 }
191
192 libusb_free_device_list(devlist, 1);
193 g_slist_free_full(conn_devices, (GDestroyNotify)&sr_usb_dev_inst_free);
194
195 return std_scan_complete(di, devices);
196}
197
198static int dev_open(struct sr_dev_inst *sdi)
199{
200 struct drv_context *drvc;
201 struct dev_context *devc;
202 struct sr_usb_dev_inst *usb;
203 int ret;
204
205 drvc = sdi->driver->context;
206 devc = sdi->priv;
207 usb = sdi->conn;
208
209 ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb);
210 if (ret != SR_OK)
211 return ret;
212
213 ret = libusb_set_configuration(usb->devhdl, USB_CONFIG);
214 if (ret != LIBUSB_SUCCESS) {
215 sr_err("Failed to set USB configuration: %s.",
216 libusb_error_name(ret));
217 sr_usb_close(usb);
218 return SR_ERR;
219 }
220
221 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
222 if (ret != LIBUSB_SUCCESS) {
223 sr_err("Failed to claim interface: %s.",
224 libusb_error_name(ret));
225 sr_usb_close(usb);
226 return SR_ERR;
227 }
228
229 sdi->status = SR_ST_ACTIVE;
230
231 devc->active_fpga_config = FPGA_NOCONF;
232 devc->state = STATE_IDLE;
233
d57a1143 234 return sla5032_apply_fpga_config(sdi);
8da8c826
VV
235}
236
237static int dev_close(struct sr_dev_inst *sdi)
238{
239 struct sr_usb_dev_inst *usb;
240
241 usb = sdi->conn;
242
243 if (usb->devhdl)
244 libusb_release_interface(usb->devhdl, USB_INTERFACE);
245
246 sr_usb_close(usb);
247
248 return SR_OK;
249}
250
251/* Check whether the device options contain a specific key.
252 * Also match against get/set/list bits if specified.
253 */
254static int has_devopt(uint32_t key)
255{
256 unsigned int i;
257
258 for (i = 0; i < ARRAY_SIZE(devopts); i++) {
259 if ((devopts[i] & (SR_CONF_MASK | key)) == key)
260 return TRUE;
261 }
262
263 return FALSE;
264}
265
266static int config_get(uint32_t key, GVariant **data,
267 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
268{
269 struct dev_context *devc;
270
271 (void)cg;
272
273 if (!sdi)
274 return SR_ERR_ARG;
275
276 devc = sdi->priv;
277
278 if (!has_devopt(key | SR_CONF_GET))
279 return SR_ERR_NA;
280
281 switch (key) {
282 case SR_CONF_SAMPLERATE:
283 *data = g_variant_new_uint64(devc->samplerate);
284 break;
285 case SR_CONF_LIMIT_SAMPLES:
286 *data = g_variant_new_uint64(devc->limit_samples);
287 break;
288 case SR_CONF_CAPTURE_RATIO:
289 *data = g_variant_new_uint64(devc->capture_ratio);
290 break;
291 case SR_CONF_RLE:
292 *data = g_variant_new_boolean(TRUE);
293 break;
294 default:
295 /* Must not happen for a key listed in devopts. */
296 return SR_ERR_BUG;
297 }
298
299 return SR_OK;
300}
301
302static int config_set(uint32_t key, GVariant *data,
303 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
304{
305 uint64_t value;
306 struct dev_context *devc;
307 int idx;
308
309 (void)cg;
310
311 if (!sdi)
312 return SR_ERR_ARG;
313
314 devc = sdi->priv;
315
316 if (!has_devopt(key | SR_CONF_SET))
317 return SR_ERR_NA;
318
319 switch (key) {
320 case SR_CONF_SAMPLERATE:
321 value = g_variant_get_uint64(data);
322 if ((idx = std_u64_idx(data, ARRAY_AND_SIZE(samplerates))) < 0)
323 return SR_ERR_ARG;
324 devc->samplerate = samplerates[idx];
325 break;
326 case SR_CONF_LIMIT_SAMPLES:
327 value = g_variant_get_uint64(data);
328 if (value > MAX_LIMIT_SAMPLES || value < MIN_LIMIT_SAMPLES)
329 return SR_ERR_ARG;
330 devc->limit_samples = value;
331 break;
332 case SR_CONF_CAPTURE_RATIO:
d57a1143 333 devc->capture_ratio = g_variant_get_uint64(data);
8da8c826
VV
334 break;
335 default:
336 /* Must not happen for a key listed in devopts. */
337 return SR_ERR_BUG;
338 }
339
340 return SR_OK;
341}
342
343static int config_channel_set(const struct sr_dev_inst *sdi,
344 struct sr_channel *ch, unsigned int changes)
345{
346 uint64_t channel_bit;
347 struct dev_context *devc;
348
349 if (!sdi)
350 return SR_ERR_ARG;
351
352 devc = sdi->priv;
353
354 if (ch->index < 0 || ch->index >= NUM_CHANNELS) {
355 sr_err("Channel index %d out of range.", ch->index);
356 return SR_ERR_BUG;
357 }
358
359 if ((changes & SR_CHANNEL_SET_ENABLED) != 0) {
360 channel_bit = UINT64_C(1) << ch->index;
361
362 /* Enable or disable logic input for this channel. */
363 if (ch->enabled)
364 devc->channel_mask |= channel_bit;
365 else
366 devc->channel_mask &= ~channel_bit;
367 }
368
369 return SR_OK;
370}
371
372/* Derive trigger masks from the session's trigger configuration. */
373static int prepare_trigger_masks(const struct sr_dev_inst* sdi)
374{
375 uint32_t trigger_mask, trigger_values, trigger_edge_mask;
376 uint32_t level_bit, type_bit;
377 struct dev_context* devc;
378 struct sr_trigger* trigger;
379 struct sr_trigger_stage* stage;
380 struct sr_trigger_match* match;
381 const GSList* node;
382 int idx;
383 enum sr_trigger_matches trg;
384
385 devc = sdi->priv;
386
387 trigger_mask = 0;
388 trigger_values = 0;
389 trigger_edge_mask = 0;
390
391 trigger = sr_session_trigger_get(sdi->session);
392 if (!trigger || !trigger->stages) {
393 goto no_triggers;
394 }
395
396 if (trigger->stages->next) {
397 sr_err("This device only supports 1 trigger stage.");
398 return SR_ERR_ARG;
399 }
400 stage = trigger->stages->data;
401
402 for (node = stage->matches; node; node = node->next) {
403 match = node->data;
404
405 if (!match->channel->enabled)
406 continue; /* Ignore disabled channel. */
407
408 idx = match->channel->index;
409 trg = match->match;
410
411 if (idx < 0 || idx >= NUM_CHANNELS) {
412 sr_err("Channel index %d out of range.", idx);
413 return SR_ERR_BUG; /* Should not happen. */
414 }
415 if (trg != SR_TRIGGER_ZERO
416 && trg != SR_TRIGGER_ONE
417 && trg != SR_TRIGGER_RISING
418 && trg != SR_TRIGGER_FALLING) {
419 sr_err("Unsupported trigger match for CH%d.", idx);
420 return SR_ERR_ARG;
421 }
422 level_bit = (trg == SR_TRIGGER_ONE
423 || trg == SR_TRIGGER_RISING) ? 1 : 0;
424 type_bit = (trg == SR_TRIGGER_RISING
425 || trg == SR_TRIGGER_FALLING) ? 1 : 0; /* 1 if edge triggered, 0 if level triggered */
426
427 trigger_mask |= UINT32_C(1) << idx;
428 trigger_values |= level_bit << idx;
429 trigger_edge_mask |= type_bit << idx;
430 }
431
432no_triggers:
433 devc->trigger_mask = trigger_mask;
434 devc->trigger_values = trigger_values;
435 devc->trigger_edge_mask = trigger_edge_mask;
436
437 return SR_OK;
438}
439
440static int config_commit(const struct sr_dev_inst *sdi)
441{
442 int ret;
443
444 ret = prepare_trigger_masks(sdi);
445 if (ret != SR_OK)
446 return ret;
447
448 ret = sla5032_apply_fpga_config(sdi);
449 if (ret != SR_OK) {
450 sr_err("Failed to apply FPGA configuration.");
451 return ret;
452 }
453
454 return SR_OK;
455}
456
457static int config_list(uint32_t key, GVariant **data,
458 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
459{
460 struct dev_context *devc;
461
462 devc = (sdi) ? sdi->priv : NULL;
463
464 switch (key) {
465 case SR_CONF_SCAN_OPTIONS:
466 case SR_CONF_DEVICE_OPTIONS:
467 return std_opts_config_list(key, data, sdi, cg,
468 ARRAY_AND_SIZE(scanopts), ARRAY_AND_SIZE(drvopts),
469 (devc) ? devopts : NULL,
470 (devc) ? ARRAY_SIZE(devopts) : 0);
471 }
472
473 if (!devc)
474 return SR_ERR_ARG;
475 if (!has_devopt(key | SR_CONF_LIST))
476 return SR_ERR_NA;
477
478 switch (key) {
479 case SR_CONF_SAMPLERATE:
480 *data = std_gvar_samplerates(ARRAY_AND_SIZE(samplerates));
481 break;
482 case SR_CONF_LIMIT_SAMPLES:
483 *data = std_gvar_tuple_u64(MIN_LIMIT_SAMPLES, MAX_LIMIT_SAMPLES);
484 break;
485 case SR_CONF_CAPTURE_RATIO:
486 *data = std_gvar_array_u64(ARRAY_AND_SIZE(capture_ratios));
487 break;
488 case SR_CONF_TRIGGER_MATCH:
489 *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
490 break;
491 default:
492 /* Must not happen for a key listed in devopts. */
493 return SR_ERR_BUG;
494 }
495
496 return SR_OK;
497}
498
499/* Set up the device hardware to begin capturing samples as soon as the
500 * configured trigger conditions are met, or immediately if no triggers
501 * are configured.
502 */
503static int dev_acquisition_start(const struct sr_dev_inst *sdi)
504{
505 return la_start_acquisition(sdi);
506}
507
508static int dev_acquisition_stop(struct sr_dev_inst *sdi)
509{
510 struct dev_context *devc;
511
512 devc = sdi->priv;
513
514 sr_session_source_remove(sdi->session, -1);
515
516 std_session_send_df_end(sdi);
517
518 devc->state = STATE_IDLE;
519
520 return SR_OK;
521}
522
523static struct sr_dev_driver sysclk_sla5032_driver_info = {
524 .name = "sysclk-sla5032",
525 .longname = "Sysclk SLA5032",
526 .api_version = 1,
527 .init = std_init,
528 .cleanup = std_cleanup,
529 .scan = scan,
530 .dev_list = std_dev_list,
531 .dev_clear = std_dev_clear,
532 .config_get = config_get,
533 .config_set = config_set,
534 .config_channel_set = config_channel_set,
535 .config_commit = config_commit,
536 .config_list = config_list,
537 .dev_open = dev_open,
538 .dev_close = dev_close,
539 .dev_acquisition_start = dev_acquisition_start,
540 .dev_acquisition_stop = dev_acquisition_stop,
541 .context = NULL,
542};
543SR_REGISTER_DEV_DRIVER(sysclk_sla5032_driver_info);