]> sigrok.org Git - libsigrok.git/blame - src/hardware/sysclk-sla5032/api.c
Add initial Sysclk SLA5032 driver.
[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
234 ret = sla5032_apply_fpga_config(sdi);
235
236 return ret;
237}
238
239static int dev_close(struct sr_dev_inst *sdi)
240{
241 struct sr_usb_dev_inst *usb;
242
243 usb = sdi->conn;
244
245 if (usb->devhdl)
246 libusb_release_interface(usb->devhdl, USB_INTERFACE);
247
248 sr_usb_close(usb);
249
250 return SR_OK;
251}
252
253/* Check whether the device options contain a specific key.
254 * Also match against get/set/list bits if specified.
255 */
256static int has_devopt(uint32_t key)
257{
258 unsigned int i;
259
260 for (i = 0; i < ARRAY_SIZE(devopts); i++) {
261 if ((devopts[i] & (SR_CONF_MASK | key)) == key)
262 return TRUE;
263 }
264
265 return FALSE;
266}
267
268static int config_get(uint32_t key, GVariant **data,
269 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
270{
271 struct dev_context *devc;
272
273 (void)cg;
274
275 if (!sdi)
276 return SR_ERR_ARG;
277
278 devc = sdi->priv;
279
280 if (!has_devopt(key | SR_CONF_GET))
281 return SR_ERR_NA;
282
283 switch (key) {
284 case SR_CONF_SAMPLERATE:
285 *data = g_variant_new_uint64(devc->samplerate);
286 break;
287 case SR_CONF_LIMIT_SAMPLES:
288 *data = g_variant_new_uint64(devc->limit_samples);
289 break;
290 case SR_CONF_CAPTURE_RATIO:
291 *data = g_variant_new_uint64(devc->capture_ratio);
292 break;
293 case SR_CONF_RLE:
294 *data = g_variant_new_boolean(TRUE);
295 break;
296 default:
297 /* Must not happen for a key listed in devopts. */
298 return SR_ERR_BUG;
299 }
300
301 return SR_OK;
302}
303
304static int config_set(uint32_t key, GVariant *data,
305 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
306{
307 uint64_t value;
308 struct dev_context *devc;
309 int idx;
310
311 (void)cg;
312
313 if (!sdi)
314 return SR_ERR_ARG;
315
316 devc = sdi->priv;
317
318 if (!has_devopt(key | SR_CONF_SET))
319 return SR_ERR_NA;
320
321 switch (key) {
322 case SR_CONF_SAMPLERATE:
323 value = g_variant_get_uint64(data);
324 if ((idx = std_u64_idx(data, ARRAY_AND_SIZE(samplerates))) < 0)
325 return SR_ERR_ARG;
326 devc->samplerate = samplerates[idx];
327 break;
328 case SR_CONF_LIMIT_SAMPLES:
329 value = g_variant_get_uint64(data);
330 if (value > MAX_LIMIT_SAMPLES || value < MIN_LIMIT_SAMPLES)
331 return SR_ERR_ARG;
332 devc->limit_samples = value;
333 break;
334 case SR_CONF_CAPTURE_RATIO:
335 value = g_variant_get_uint64(data);
336 devc->capture_ratio = value;
337 break;
338 default:
339 /* Must not happen for a key listed in devopts. */
340 return SR_ERR_BUG;
341 }
342
343 return SR_OK;
344}
345
346static int config_channel_set(const struct sr_dev_inst *sdi,
347 struct sr_channel *ch, unsigned int changes)
348{
349 uint64_t channel_bit;
350 struct dev_context *devc;
351
352 if (!sdi)
353 return SR_ERR_ARG;
354
355 devc = sdi->priv;
356
357 if (ch->index < 0 || ch->index >= NUM_CHANNELS) {
358 sr_err("Channel index %d out of range.", ch->index);
359 return SR_ERR_BUG;
360 }
361
362 if ((changes & SR_CHANNEL_SET_ENABLED) != 0) {
363 channel_bit = UINT64_C(1) << ch->index;
364
365 /* Enable or disable logic input for this channel. */
366 if (ch->enabled)
367 devc->channel_mask |= channel_bit;
368 else
369 devc->channel_mask &= ~channel_bit;
370 }
371
372 return SR_OK;
373}
374
375/* Derive trigger masks from the session's trigger configuration. */
376static int prepare_trigger_masks(const struct sr_dev_inst* sdi)
377{
378 uint32_t trigger_mask, trigger_values, trigger_edge_mask;
379 uint32_t level_bit, type_bit;
380 struct dev_context* devc;
381 struct sr_trigger* trigger;
382 struct sr_trigger_stage* stage;
383 struct sr_trigger_match* match;
384 const GSList* node;
385 int idx;
386 enum sr_trigger_matches trg;
387
388 devc = sdi->priv;
389
390 trigger_mask = 0;
391 trigger_values = 0;
392 trigger_edge_mask = 0;
393
394 trigger = sr_session_trigger_get(sdi->session);
395 if (!trigger || !trigger->stages) {
396 goto no_triggers;
397 }
398
399 if (trigger->stages->next) {
400 sr_err("This device only supports 1 trigger stage.");
401 return SR_ERR_ARG;
402 }
403 stage = trigger->stages->data;
404
405 for (node = stage->matches; node; node = node->next) {
406 match = node->data;
407
408 if (!match->channel->enabled)
409 continue; /* Ignore disabled channel. */
410
411 idx = match->channel->index;
412 trg = match->match;
413
414 if (idx < 0 || idx >= NUM_CHANNELS) {
415 sr_err("Channel index %d out of range.", idx);
416 return SR_ERR_BUG; /* Should not happen. */
417 }
418 if (trg != SR_TRIGGER_ZERO
419 && trg != SR_TRIGGER_ONE
420 && trg != SR_TRIGGER_RISING
421 && trg != SR_TRIGGER_FALLING) {
422 sr_err("Unsupported trigger match for CH%d.", idx);
423 return SR_ERR_ARG;
424 }
425 level_bit = (trg == SR_TRIGGER_ONE
426 || trg == SR_TRIGGER_RISING) ? 1 : 0;
427 type_bit = (trg == SR_TRIGGER_RISING
428 || trg == SR_TRIGGER_FALLING) ? 1 : 0; /* 1 if edge triggered, 0 if level triggered */
429
430 trigger_mask |= UINT32_C(1) << idx;
431 trigger_values |= level_bit << idx;
432 trigger_edge_mask |= type_bit << idx;
433 }
434
435no_triggers:
436 devc->trigger_mask = trigger_mask;
437 devc->trigger_values = trigger_values;
438 devc->trigger_edge_mask = trigger_edge_mask;
439
440 return SR_OK;
441}
442
443static int config_commit(const struct sr_dev_inst *sdi)
444{
445 int ret;
446
447 ret = prepare_trigger_masks(sdi);
448 if (ret != SR_OK)
449 return ret;
450
451 ret = sla5032_apply_fpga_config(sdi);
452 if (ret != SR_OK) {
453 sr_err("Failed to apply FPGA configuration.");
454 return ret;
455 }
456
457 return SR_OK;
458}
459
460static int config_list(uint32_t key, GVariant **data,
461 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
462{
463 struct dev_context *devc;
464
465 devc = (sdi) ? sdi->priv : NULL;
466
467 switch (key) {
468 case SR_CONF_SCAN_OPTIONS:
469 case SR_CONF_DEVICE_OPTIONS:
470 return std_opts_config_list(key, data, sdi, cg,
471 ARRAY_AND_SIZE(scanopts), ARRAY_AND_SIZE(drvopts),
472 (devc) ? devopts : NULL,
473 (devc) ? ARRAY_SIZE(devopts) : 0);
474 }
475
476 if (!devc)
477 return SR_ERR_ARG;
478 if (!has_devopt(key | SR_CONF_LIST))
479 return SR_ERR_NA;
480
481 switch (key) {
482 case SR_CONF_SAMPLERATE:
483 *data = std_gvar_samplerates(ARRAY_AND_SIZE(samplerates));
484 break;
485 case SR_CONF_LIMIT_SAMPLES:
486 *data = std_gvar_tuple_u64(MIN_LIMIT_SAMPLES, MAX_LIMIT_SAMPLES);
487 break;
488 case SR_CONF_CAPTURE_RATIO:
489 *data = std_gvar_array_u64(ARRAY_AND_SIZE(capture_ratios));
490 break;
491 case SR_CONF_TRIGGER_MATCH:
492 *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
493 break;
494 default:
495 /* Must not happen for a key listed in devopts. */
496 return SR_ERR_BUG;
497 }
498
499 return SR_OK;
500}
501
502/* Set up the device hardware to begin capturing samples as soon as the
503 * configured trigger conditions are met, or immediately if no triggers
504 * are configured.
505 */
506static int dev_acquisition_start(const struct sr_dev_inst *sdi)
507{
508 return la_start_acquisition(sdi);
509}
510
511static int dev_acquisition_stop(struct sr_dev_inst *sdi)
512{
513 struct dev_context *devc;
514
515 devc = sdi->priv;
516
517 sr_session_source_remove(sdi->session, -1);
518
519 std_session_send_df_end(sdi);
520
521 devc->state = STATE_IDLE;
522
523 return SR_OK;
524}
525
526static struct sr_dev_driver sysclk_sla5032_driver_info = {
527 .name = "sysclk-sla5032",
528 .longname = "Sysclk SLA5032",
529 .api_version = 1,
530 .init = std_init,
531 .cleanup = std_cleanup,
532 .scan = scan,
533 .dev_list = std_dev_list,
534 .dev_clear = std_dev_clear,
535 .config_get = config_get,
536 .config_set = config_set,
537 .config_channel_set = config_channel_set,
538 .config_commit = config_commit,
539 .config_list = config_list,
540 .dev_open = dev_open,
541 .dev_close = dev_close,
542 .dev_acquisition_start = dev_acquisition_start,
543 .dev_acquisition_stop = dev_acquisition_stop,
544 .context = NULL,
545};
546SR_REGISTER_DEV_DRIVER(sysclk_sla5032_driver_info);