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>
30 /* Number of logic channels. */
31 #define NUM_CHANNELS 32
33 static const uint32_t scanopts[] = {
37 static const uint32_t drvopts[] = {
38 SR_CONF_LOGIC_ANALYZER,
41 static const int32_t trigger_matches[] = {
48 static const uint64_t capture_ratios[] = {
49 0, 10, 20, 30, 50, 70, 90, 100,
52 static const uint32_t devopts[] = {
53 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
54 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
55 SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
56 SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
57 SR_CONF_RLE | SR_CONF_GET,
60 static const uint64_t samplerates[] = {
61 SR_MHZ(500), SR_MHZ(400), SR_MHZ(250), SR_MHZ(200), SR_MHZ(100),
62 SR_MHZ(50), SR_MHZ(25), SR_MHZ(20), SR_MHZ(10), SR_MHZ(5), SR_MHZ(2),
63 SR_MHZ(1), SR_KHZ(500), SR_KHZ(200), SR_KHZ(100), SR_KHZ(50),
64 SR_KHZ(20), SR_KHZ(10), SR_KHZ(5), SR_KHZ(2),
67 static struct sr_dev_inst *dev_inst_new(void)
69 struct sr_dev_inst *sdi;
70 struct dev_context *devc;
74 devc = g_malloc0(sizeof(struct dev_context));
75 devc->active_fpga_config = FPGA_NOCONF;
76 devc->samplerate = samplerates[0];
77 devc->limit_samples = MAX_LIMIT_SAMPLES;
78 devc->capture_ratio = capture_ratios[4];
79 devc->channel_mask = (UINT64_C(1) << NUM_CHANNELS) - 1;
81 sdi = g_malloc0(sizeof(struct sr_dev_inst));
82 sdi->status = SR_ST_INACTIVE;
83 sdi->vendor = g_strdup("Sysclk");
84 sdi->model = g_strdup("SLA5032");
87 for (i = 0; i < NUM_CHANNELS; i++) {
88 g_snprintf(name, sizeof(name), "CH%d", i);
89 sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, name);
95 /* Create a new device instance for a libusb device if it is a Sysclk SLA5032
96 * device and also matches the connection specification.
98 static struct sr_dev_inst *dev_inst_new_matching(GSList *conn_matches,
102 struct sr_usb_dev_inst *usb;
103 struct sr_dev_inst *sdi;
104 struct libusb_device_descriptor des;
105 int bus, address, ret;
106 unsigned int vid, pid;
108 bus = libusb_get_bus_number(dev);
109 address = libusb_get_device_address(dev);
111 for (node = conn_matches; node != NULL; node = node->next) {
113 if (usb && usb->bus == bus && usb->address == address)
116 if (conn_matches && !node)
117 return NULL; /* no match */
119 ret = libusb_get_device_descriptor(dev, &des);
121 sr_err("Failed to get USB device descriptor: %s.",
122 libusb_error_name(ret));
128 /* Create sigrok device instance. */
129 if (vid == USB_VID_SYSCLK && pid == USB_PID_SLA5032) {
132 sr_warn("USB device %d.%d (%04x:%04x) is not a"
133 " Sysclk SLA5032.", bus, address, vid, pid);
136 sdi = dev_inst_new();
138 sdi->inst_type = SR_INST_USB;
139 sdi->conn = sr_usb_dev_inst_new(bus, address, NULL);
144 static GSList *scan(struct sr_dev_driver *di, GSList *options)
146 GSList *conn_devices, *devices, *node;
147 struct drv_context *drvc;
148 struct sr_dev_inst *sdi;
149 struct sr_config *src;
151 libusb_device **devlist;
159 for (node = options; node != NULL; node = node->next) {
161 if (src->key == SR_CONF_CONN) {
162 conn = g_variant_get_string(src->data, NULL);
167 /* Find devices matching the connection specification. */
168 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
171 /* List all libusb devices. */
172 num_devs = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
174 sr_err("Failed to list USB devices: %s.",
175 libusb_error_name(num_devs));
176 g_slist_free_full(conn_devices,
177 (GDestroyNotify)&sr_usb_dev_inst_free);
181 /* Scan the USB device list for matching devices. */
182 for (i = 0; i < num_devs; i++) {
183 sdi = dev_inst_new_matching(conn_devices, devlist[i]);
185 continue; /* no match */
187 /* Register device instance with driver. */
188 devices = g_slist_append(devices, sdi);
191 libusb_free_device_list(devlist, 1);
192 g_slist_free_full(conn_devices, (GDestroyNotify)&sr_usb_dev_inst_free);
194 return std_scan_complete(di, devices);
197 static int dev_open(struct sr_dev_inst *sdi)
199 struct drv_context *drvc;
200 struct dev_context *devc;
201 struct sr_usb_dev_inst *usb;
204 drvc = sdi->driver->context;
208 ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb);
212 ret = libusb_set_configuration(usb->devhdl, USB_CONFIG);
213 if (ret != LIBUSB_SUCCESS) {
214 sr_err("Failed to set USB configuration: %s.",
215 libusb_error_name(ret));
220 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
221 if (ret != LIBUSB_SUCCESS) {
222 sr_err("Failed to claim interface: %s.",
223 libusb_error_name(ret));
228 sdi->status = SR_ST_ACTIVE;
230 devc->active_fpga_config = FPGA_NOCONF;
231 devc->state = STATE_IDLE;
233 return sla5032_apply_fpga_config(sdi);
236 static int dev_close(struct sr_dev_inst *sdi)
238 struct sr_usb_dev_inst *usb;
243 libusb_release_interface(usb->devhdl, USB_INTERFACE);
250 /* Check whether the device options contain a specific key.
251 * Also match against get/set/list bits if specified.
253 static int has_devopt(uint32_t key)
257 for (i = 0; i < ARRAY_SIZE(devopts); i++) {
258 if ((devopts[i] & (SR_CONF_MASK | key)) == key)
265 static int config_get(uint32_t key, GVariant **data,
266 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
268 struct dev_context *devc;
277 if (!has_devopt(key | SR_CONF_GET))
281 case SR_CONF_SAMPLERATE:
282 *data = g_variant_new_uint64(devc->samplerate);
284 case SR_CONF_LIMIT_SAMPLES:
285 *data = g_variant_new_uint64(devc->limit_samples);
287 case SR_CONF_CAPTURE_RATIO:
288 *data = g_variant_new_uint64(devc->capture_ratio);
291 *data = g_variant_new_boolean(TRUE);
294 /* Must not happen for a key listed in devopts. */
301 static int config_set(uint32_t key, GVariant *data,
302 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
305 struct dev_context *devc;
315 if (!has_devopt(key | SR_CONF_SET))
319 case SR_CONF_SAMPLERATE:
320 value = g_variant_get_uint64(data);
321 if ((idx = std_u64_idx(data, ARRAY_AND_SIZE(samplerates))) < 0)
323 devc->samplerate = samplerates[idx];
325 case SR_CONF_LIMIT_SAMPLES:
326 value = g_variant_get_uint64(data);
327 if (value > MAX_LIMIT_SAMPLES || value < MIN_LIMIT_SAMPLES)
329 devc->limit_samples = value;
331 case SR_CONF_CAPTURE_RATIO:
332 devc->capture_ratio = g_variant_get_uint64(data);
335 /* Must not happen for a key listed in devopts. */
342 static int config_channel_set(const struct sr_dev_inst *sdi,
343 struct sr_channel *ch, unsigned int changes)
345 uint64_t channel_bit;
346 struct dev_context *devc;
353 if (ch->index < 0 || ch->index >= NUM_CHANNELS) {
354 sr_err("Channel index %d out of range.", ch->index);
358 if ((changes & SR_CHANNEL_SET_ENABLED) != 0) {
359 channel_bit = UINT64_C(1) << ch->index;
361 /* Enable or disable logic input for this channel. */
363 devc->channel_mask |= channel_bit;
365 devc->channel_mask &= ~channel_bit;
371 /* Derive trigger masks from the session's trigger configuration. */
372 static int prepare_trigger_masks(const struct sr_dev_inst *sdi)
374 uint32_t trigger_mask, trigger_values, trigger_edge_mask;
375 uint32_t level_bit, type_bit;
376 struct dev_context *devc;
377 struct sr_trigger *trigger;
378 struct sr_trigger_stage *stage;
379 struct sr_trigger_match *match;
382 enum sr_trigger_matches trg;
388 trigger_edge_mask = 0;
390 trigger = sr_session_trigger_get(sdi->session);
391 if (!trigger || !trigger->stages) {
395 if (trigger->stages->next) {
396 sr_err("This device only supports 1 trigger stage.");
399 stage = trigger->stages->data;
401 for (node = stage->matches; node; node = node->next) {
404 if (!match->channel->enabled)
405 continue; /* Ignore disabled channel. */
407 idx = match->channel->index;
410 if (idx < 0 || idx >= NUM_CHANNELS) {
411 sr_err("Channel index %d out of range.", idx);
412 return SR_ERR_BUG; /* Should not happen. */
414 if (trg != SR_TRIGGER_ZERO
415 && trg != SR_TRIGGER_ONE
416 && trg != SR_TRIGGER_RISING
417 && trg != SR_TRIGGER_FALLING) {
418 sr_err("Unsupported trigger match for CH%d.", idx);
421 level_bit = (trg == SR_TRIGGER_ONE
422 || trg == SR_TRIGGER_RISING) ? 1 : 0;
423 type_bit = (trg == SR_TRIGGER_RISING
424 || trg == SR_TRIGGER_FALLING) ? 1 : 0; /* 1 if edge triggered, 0 if level triggered */
426 trigger_mask |= UINT32_C(1) << idx;
427 trigger_values |= level_bit << idx;
428 trigger_edge_mask |= type_bit << idx;
432 devc->trigger_mask = trigger_mask;
433 devc->trigger_values = trigger_values;
434 devc->trigger_edge_mask = trigger_edge_mask;
439 static int config_commit(const struct sr_dev_inst *sdi)
443 ret = prepare_trigger_masks(sdi);
447 ret = sla5032_apply_fpga_config(sdi);
449 sr_err("Failed to apply FPGA configuration.");
456 static int config_list(uint32_t key, GVariant **data,
457 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
459 struct dev_context *devc;
461 devc = (sdi) ? sdi->priv : NULL;
464 case SR_CONF_SCAN_OPTIONS:
465 case SR_CONF_DEVICE_OPTIONS:
466 return std_opts_config_list(key, data, sdi, cg,
467 ARRAY_AND_SIZE(scanopts), ARRAY_AND_SIZE(drvopts),
468 (devc) ? devopts : NULL,
469 (devc) ? ARRAY_SIZE(devopts) : 0);
474 if (!has_devopt(key | SR_CONF_LIST))
478 case SR_CONF_SAMPLERATE:
479 *data = std_gvar_samplerates(ARRAY_AND_SIZE(samplerates));
481 case SR_CONF_LIMIT_SAMPLES:
482 *data = std_gvar_tuple_u64(MIN_LIMIT_SAMPLES, MAX_LIMIT_SAMPLES);
484 case SR_CONF_CAPTURE_RATIO:
485 *data = std_gvar_array_u64(ARRAY_AND_SIZE(capture_ratios));
487 case SR_CONF_TRIGGER_MATCH:
488 *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
491 /* Must not happen for a key listed in devopts. */
498 /* Set up the device hardware to begin capturing samples as soon as the
499 * configured trigger conditions are met, or immediately if no triggers
502 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
504 return sla5032_start_acquisition(sdi);
507 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
509 struct dev_context *devc;
513 sr_session_source_remove(sdi->session, -1);
515 std_session_send_df_end(sdi);
517 devc->state = STATE_IDLE;
522 static struct sr_dev_driver sysclk_sla5032_driver_info = {
523 .name = "sysclk-sla5032",
524 .longname = "Sysclk SLA5032",
527 .cleanup = std_cleanup,
529 .dev_list = std_dev_list,
530 .dev_clear = std_dev_clear,
531 .config_get = config_get,
532 .config_set = config_set,
533 .config_channel_set = config_channel_set,
534 .config_commit = config_commit,
535 .config_list = config_list,
536 .dev_open = dev_open,
537 .dev_close = dev_close,
538 .dev_acquisition_start = dev_acquisition_start,
539 .dev_acquisition_stop = dev_acquisition_stop,
542 SR_REGISTER_DEV_DRIVER(sysclk_sla5032_driver_info);