2 * This file is part of the libsigrok project.
4 * Copyright (C) 2014 Daniel Elstner <daniel.kitta@gmail.com>
5 * Copyright (C) 2019 Vitaliy Vorobyov
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.
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.
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/>.
26 #include <libsigrok/libsigrok.h>
27 #include <libsigrok-internal.h>
31 /* Number of logic channels. */
32 #define NUM_CHANNELS 32
34 static const uint32_t scanopts[] = {
38 static const uint32_t drvopts[] = {
39 SR_CONF_LOGIC_ANALYZER,
42 static const int32_t trigger_matches[] = {
49 static const uint64_t capture_ratios[] = {
50 0, 10, 20, 30, 50, 70, 90, 100,
53 static 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,
61 static 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),
68 static struct sr_dev_inst *dev_inst_new(void)
70 struct sr_dev_inst *sdi;
71 struct dev_context *devc;
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;
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");
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);
96 /* Create a new device instance for a libusb device if it is a Sysclk SLA5032
97 * device and also matches the connection specification.
99 static struct sr_dev_inst *dev_inst_new_matching(GSList *conn_matches,
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;
109 bus = libusb_get_bus_number(dev);
110 address = libusb_get_device_address(dev);
112 for (node = conn_matches; node != NULL; node = node->next) {
114 if (usb && usb->bus == bus && usb->address == address)
117 if (conn_matches && !node)
118 return NULL; /* no match */
120 ret = libusb_get_device_descriptor(dev, &des);
122 sr_err("Failed to get USB device descriptor: %s.",
123 libusb_error_name(ret));
129 /* Create sigrok device instance. */
130 if (vid == USB_VID_SYSCLK && pid == USB_PID_SLA5032) {
133 sr_warn("USB device %d.%d (%04x:%04x) is not a"
134 " Sysclk SLA5032.", bus, address, vid, pid);
137 sdi = dev_inst_new();
139 sdi->inst_type = SR_INST_USB;
140 sdi->conn = sr_usb_dev_inst_new(bus, address, NULL);
145 static GSList *scan(struct sr_dev_driver *di, GSList *options)
147 GSList *conn_devices, *devices, *node;
148 struct drv_context *drvc;
149 struct sr_dev_inst *sdi;
150 struct sr_config *src;
152 libusb_device **devlist;
160 for (node = options; node != NULL; node = node->next) {
162 if (src->key == SR_CONF_CONN) {
163 conn = g_variant_get_string(src->data, NULL);
168 /* Find devices matching the connection specification. */
169 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
172 /* List all libusb devices. */
173 num_devs = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
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);
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]);
186 continue; /* no match */
188 /* Register device instance with driver. */
189 devices = g_slist_append(devices, sdi);
192 libusb_free_device_list(devlist, 1);
193 g_slist_free_full(conn_devices, (GDestroyNotify)&sr_usb_dev_inst_free);
195 return std_scan_complete(di, devices);
198 static int dev_open(struct sr_dev_inst *sdi)
200 struct drv_context *drvc;
201 struct dev_context *devc;
202 struct sr_usb_dev_inst *usb;
205 drvc = sdi->driver->context;
209 ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb);
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));
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));
229 sdi->status = SR_ST_ACTIVE;
231 devc->active_fpga_config = FPGA_NOCONF;
232 devc->state = STATE_IDLE;
234 ret = sla5032_apply_fpga_config(sdi);
239 static int dev_close(struct sr_dev_inst *sdi)
241 struct sr_usb_dev_inst *usb;
246 libusb_release_interface(usb->devhdl, USB_INTERFACE);
253 /* Check whether the device options contain a specific key.
254 * Also match against get/set/list bits if specified.
256 static int has_devopt(uint32_t key)
260 for (i = 0; i < ARRAY_SIZE(devopts); i++) {
261 if ((devopts[i] & (SR_CONF_MASK | key)) == key)
268 static int config_get(uint32_t key, GVariant **data,
269 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
271 struct dev_context *devc;
280 if (!has_devopt(key | SR_CONF_GET))
284 case SR_CONF_SAMPLERATE:
285 *data = g_variant_new_uint64(devc->samplerate);
287 case SR_CONF_LIMIT_SAMPLES:
288 *data = g_variant_new_uint64(devc->limit_samples);
290 case SR_CONF_CAPTURE_RATIO:
291 *data = g_variant_new_uint64(devc->capture_ratio);
294 *data = g_variant_new_boolean(TRUE);
297 /* Must not happen for a key listed in devopts. */
304 static int config_set(uint32_t key, GVariant *data,
305 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
308 struct dev_context *devc;
318 if (!has_devopt(key | SR_CONF_SET))
322 case SR_CONF_SAMPLERATE:
323 value = g_variant_get_uint64(data);
324 if ((idx = std_u64_idx(data, ARRAY_AND_SIZE(samplerates))) < 0)
326 devc->samplerate = samplerates[idx];
328 case SR_CONF_LIMIT_SAMPLES:
329 value = g_variant_get_uint64(data);
330 if (value > MAX_LIMIT_SAMPLES || value < MIN_LIMIT_SAMPLES)
332 devc->limit_samples = value;
334 case SR_CONF_CAPTURE_RATIO:
335 value = g_variant_get_uint64(data);
336 devc->capture_ratio = value;
339 /* Must not happen for a key listed in devopts. */
346 static int config_channel_set(const struct sr_dev_inst *sdi,
347 struct sr_channel *ch, unsigned int changes)
349 uint64_t channel_bit;
350 struct dev_context *devc;
357 if (ch->index < 0 || ch->index >= NUM_CHANNELS) {
358 sr_err("Channel index %d out of range.", ch->index);
362 if ((changes & SR_CHANNEL_SET_ENABLED) != 0) {
363 channel_bit = UINT64_C(1) << ch->index;
365 /* Enable or disable logic input for this channel. */
367 devc->channel_mask |= channel_bit;
369 devc->channel_mask &= ~channel_bit;
375 /* Derive trigger masks from the session's trigger configuration. */
376 static int prepare_trigger_masks(const struct sr_dev_inst* sdi)
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;
386 enum sr_trigger_matches trg;
392 trigger_edge_mask = 0;
394 trigger = sr_session_trigger_get(sdi->session);
395 if (!trigger || !trigger->stages) {
399 if (trigger->stages->next) {
400 sr_err("This device only supports 1 trigger stage.");
403 stage = trigger->stages->data;
405 for (node = stage->matches; node; node = node->next) {
408 if (!match->channel->enabled)
409 continue; /* Ignore disabled channel. */
411 idx = match->channel->index;
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. */
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);
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 */
430 trigger_mask |= UINT32_C(1) << idx;
431 trigger_values |= level_bit << idx;
432 trigger_edge_mask |= type_bit << idx;
436 devc->trigger_mask = trigger_mask;
437 devc->trigger_values = trigger_values;
438 devc->trigger_edge_mask = trigger_edge_mask;
443 static int config_commit(const struct sr_dev_inst *sdi)
447 ret = prepare_trigger_masks(sdi);
451 ret = sla5032_apply_fpga_config(sdi);
453 sr_err("Failed to apply FPGA configuration.");
460 static int config_list(uint32_t key, GVariant **data,
461 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
463 struct dev_context *devc;
465 devc = (sdi) ? sdi->priv : NULL;
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);
478 if (!has_devopt(key | SR_CONF_LIST))
482 case SR_CONF_SAMPLERATE:
483 *data = std_gvar_samplerates(ARRAY_AND_SIZE(samplerates));
485 case SR_CONF_LIMIT_SAMPLES:
486 *data = std_gvar_tuple_u64(MIN_LIMIT_SAMPLES, MAX_LIMIT_SAMPLES);
488 case SR_CONF_CAPTURE_RATIO:
489 *data = std_gvar_array_u64(ARRAY_AND_SIZE(capture_ratios));
491 case SR_CONF_TRIGGER_MATCH:
492 *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
495 /* Must not happen for a key listed in devopts. */
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
506 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
508 return la_start_acquisition(sdi);
511 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
513 struct dev_context *devc;
517 sr_session_source_remove(sdi->session, -1);
519 std_session_send_df_end(sdi);
521 devc->state = STATE_IDLE;
526 static struct sr_dev_driver sysclk_sla5032_driver_info = {
527 .name = "sysclk-sla5032",
528 .longname = "Sysclk SLA5032",
531 .cleanup = std_cleanup,
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,
546 SR_REGISTER_DEV_DRIVER(sysclk_sla5032_driver_info);