]>
Commit | Line | Data |
---|---|---|
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 | ||
6ec6c43b | 20 | #include <config.h> |
5874e88d DE |
21 | #include <glib.h> |
22 | #include <libusb.h> | |
23 | #include <stdlib.h> | |
24 | #include <string.h> | |
c1aae900 | 25 | #include <libsigrok/libsigrok.h> |
407b6e2c | 26 | #include <libsigrok-internal.h> |
515ab088 | 27 | #include "protocol.h" |
5874e88d | 28 | |
a0e0bb41 | 29 | static const uint32_t scanopts[] = { |
99c76642 DE |
30 | SR_CONF_CONN, |
31 | }; | |
32 | ||
be64f90b | 33 | static const uint32_t drvopts[] = { |
5874e88d | 34 | SR_CONF_LOGIC_ANALYZER, |
5874e88d DE |
35 | }; |
36 | ||
bbe7e48a BV |
37 | static const int32_t trigger_matches[] = { |
38 | SR_TRIGGER_ZERO, | |
39 | SR_TRIGGER_ONE, | |
40 | SR_TRIGGER_RISING, | |
41 | SR_TRIGGER_FALLING, | |
42 | }; | |
43 | ||
e124cf9b | 44 | static const char *trigger_sources[] = { |
be64f90b DE |
45 | [TRIGGER_CHANNELS] = "CH", |
46 | [TRIGGER_EXT_TRG] = "TRG", | |
5874e88d | 47 | }; |
aeaad0b0 | 48 | |
e124cf9b | 49 | static const char *signal_edges[] = { |
be64f90b DE |
50 | [EDGE_POSITIVE] = "r", |
51 | [EDGE_NEGATIVE] = "f", | |
52 | }; | |
e6e54bd2 | 53 | |
be64f90b | 54 | static struct sr_dev_inst *dev_inst_new(const struct model_info *model) |
43db3436 DE |
55 | { |
56 | struct sr_dev_inst *sdi; | |
57 | struct dev_context *devc; | |
5e23fcab ML |
58 | int i; |
59 | char name[8]; | |
43db3436 | 60 | |
f57d8ffe | 61 | devc = g_malloc0(sizeof(struct dev_context)); |
be64f90b DE |
62 | devc->model = model; |
63 | devc->active_fpga_config = FPGA_NOCONF; | |
64 | devc->cfg_rle = TRUE; | |
65 | devc->samplerate = model->samplerates[0]; | |
66 | devc->channel_mask = (UINT64_C(1) << model->num_channels) - 1; | |
43db3436 | 67 | |
aac29cc1 | 68 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); |
0af636be | 69 | sdi->status = SR_ST_INACTIVE; |
b15ff1c9 | 70 | sdi->vendor = g_strdup("SysClk"); |
be64f90b | 71 | sdi->model = g_strdup(model->name); |
43db3436 | 72 | sdi->priv = devc; |
be64f90b | 73 | |
be64f90b | 74 | for (i = 0; i < model->num_channels; i++) { |
fe473123 DE |
75 | g_snprintf(name, sizeof(name), "CH%d", i + 1); |
76 | sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, name); | |
5e23fcab | 77 | } |
43db3436 DE |
78 | |
79 | return sdi; | |
80 | } | |
81 | ||
be64f90b DE |
82 | /* Create a new device instance for a libusb device if it is a SysClk LWLA |
83 | * device and also matches the connection specification. | |
84 | */ | |
85 | static struct sr_dev_inst *dev_inst_new_matching(GSList *conn_matches, | |
86 | libusb_device *dev) | |
87 | { | |
88 | GSList *node; | |
89 | struct sr_usb_dev_inst *usb; | |
90 | const struct model_info *model; | |
91 | struct sr_dev_inst *sdi; | |
92 | struct libusb_device_descriptor des; | |
d64b5f43 | 93 | int bus, address, ret; |
be64f90b | 94 | unsigned int vid, pid; |
be64f90b DE |
95 | |
96 | bus = libusb_get_bus_number(dev); | |
97 | address = libusb_get_device_address(dev); | |
98 | ||
99 | for (node = conn_matches; node != NULL; node = node->next) { | |
100 | usb = node->data; | |
101 | if (usb && usb->bus == bus && usb->address == address) | |
102 | break; /* found */ | |
103 | } | |
104 | if (conn_matches && !node) | |
105 | return NULL; /* no match */ | |
106 | ||
107 | ret = libusb_get_device_descriptor(dev, &des); | |
108 | if (ret != 0) { | |
109 | sr_err("Failed to get USB device descriptor: %s.", | |
110 | libusb_error_name(ret)); | |
111 | return NULL; | |
112 | } | |
113 | vid = des.idVendor; | |
114 | pid = des.idProduct; | |
115 | ||
116 | /* Create sigrok device instance. */ | |
117 | if (vid == USB_VID_SYSCLK && pid == USB_PID_LWLA1016) { | |
118 | model = &lwla1016_info; | |
119 | } else if (vid == USB_VID_SYSCLK && pid == USB_PID_LWLA1034) { | |
120 | model = &lwla1034_info; | |
121 | } else { | |
122 | if (conn_matches) | |
123 | sr_warn("USB device %d.%d (%04x:%04x) is not a" | |
124 | " SysClk LWLA.", bus, address, vid, pid); | |
125 | return NULL; | |
126 | } | |
127 | sdi = dev_inst_new(model); | |
128 | ||
129 | sdi->inst_type = SR_INST_USB; | |
130 | sdi->conn = sr_usb_dev_inst_new(bus, address, NULL); | |
131 | ||
132 | return sdi; | |
133 | } | |
134 | ||
4f840ce9 | 135 | static GSList *scan(struct sr_dev_driver *di, GSList *options) |
aeaad0b0 | 136 | { |
be64f90b | 137 | GSList *conn_devices, *devices, *node; |
aeaad0b0 | 138 | struct drv_context *drvc; |
5874e88d | 139 | struct sr_dev_inst *sdi; |
7ebe9b9e DE |
140 | struct sr_config *src; |
141 | const char *conn; | |
be64f90b DE |
142 | libusb_device **devlist; |
143 | ssize_t num_devs, i; | |
aeaad0b0 | 144 | |
41812aca | 145 | drvc = di->context; |
be64f90b DE |
146 | conn = NULL; |
147 | conn_devices = NULL; | |
148 | devices = NULL; | |
5874e88d | 149 | |
7ebe9b9e DE |
150 | for (node = options; node != NULL; node = node->next) { |
151 | src = node->data; | |
152 | if (src->key == SR_CONF_CONN) { | |
153 | conn = g_variant_get_string(src->data, NULL); | |
154 | break; | |
155 | } | |
156 | } | |
be64f90b DE |
157 | if (conn) { |
158 | /* Find devices matching the connection specification. */ | |
159 | conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn); | |
160 | } | |
5874e88d | 161 | |
be64f90b DE |
162 | /* List all libusb devices. */ |
163 | num_devs = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); | |
164 | if (num_devs < 0) { | |
165 | sr_err("Failed to list USB devices: %s.", | |
166 | libusb_error_name(num_devs)); | |
167 | g_slist_free_full(conn_devices, | |
168 | (GDestroyNotify)&sr_usb_dev_inst_free); | |
169 | return NULL; | |
170 | } | |
5874e88d | 171 | |
be64f90b DE |
172 | /* Scan the USB device list for matching LWLA devices. */ |
173 | for (i = 0; i < num_devs; i++) { | |
174 | sdi = dev_inst_new_matching(conn_devices, devlist[i]); | |
175 | if (!sdi) | |
176 | continue; /* no match */ | |
5874e88d | 177 | |
43db3436 | 178 | /* Register device instance with driver. */ |
5874e88d DE |
179 | devices = g_slist_append(devices, sdi); |
180 | } | |
aeaad0b0 | 181 | |
be64f90b DE |
182 | libusb_free_device_list(devlist, 1); |
183 | g_slist_free_full(conn_devices, (GDestroyNotify)&sr_usb_dev_inst_free); | |
aeaad0b0 | 184 | |
15a5bfe4 | 185 | return std_scan_complete(di, devices); |
aeaad0b0 DE |
186 | } |
187 | ||
93ed0241 DE |
188 | /* Drain any pending data from the USB transfer buffers on the device. |
189 | * This may be necessary e.g. after a crash or generally to clean up after | |
190 | * an abnormal condition. | |
191 | */ | |
192 | static int drain_usb(struct sr_usb_dev_inst *usb, unsigned int endpoint) | |
193 | { | |
d64b5f43 | 194 | int drained, xfer_len, ret; |
93ed0241 | 195 | unsigned char buf[512]; |
93ed0241 DE |
196 | const unsigned int drain_timeout_ms = 10; |
197 | ||
198 | drained = 0; | |
199 | do { | |
200 | xfer_len = 0; | |
201 | ret = libusb_bulk_transfer(usb->devhdl, endpoint, | |
b3cfc6e9 | 202 | buf, sizeof(buf), &xfer_len, |
93ed0241 DE |
203 | drain_timeout_ms); |
204 | drained += xfer_len; | |
205 | } while (ret == LIBUSB_SUCCESS && xfer_len != 0); | |
206 | ||
207 | if (ret != LIBUSB_SUCCESS && ret != LIBUSB_ERROR_TIMEOUT) { | |
208 | sr_err("Failed to drain USB endpoint %u: %s.", | |
209 | endpoint & (LIBUSB_ENDPOINT_IN - 1), | |
210 | libusb_error_name(ret)); | |
211 | return SR_ERR; | |
212 | } | |
213 | if (drained > 0) { | |
214 | sr_warn("Drained %d bytes from USB endpoint %u.", | |
215 | drained, endpoint & (LIBUSB_ENDPOINT_IN - 1)); | |
216 | } | |
d64b5f43 | 217 | |
93ed0241 DE |
218 | return SR_OK; |
219 | } | |
220 | ||
aeaad0b0 DE |
221 | static int dev_open(struct sr_dev_inst *sdi) |
222 | { | |
5874e88d | 223 | struct drv_context *drvc; |
be64f90b | 224 | struct dev_context *devc; |
5874e88d | 225 | struct sr_usb_dev_inst *usb; |
d64b5f43 | 226 | int i, ret; |
aeaad0b0 | 227 | |
ce19d4c6 | 228 | drvc = sdi->driver->context; |
be64f90b DE |
229 | devc = sdi->priv; |
230 | usb = sdi->conn; | |
aeaad0b0 | 231 | |
e35a4592 DE |
232 | /* Try the whole shebang three times, fingers crossed. */ |
233 | for (i = 0; i < 3; i++) { | |
234 | ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb); | |
235 | if (ret != SR_OK) | |
236 | return ret; | |
237 | ||
238 | ret = libusb_set_configuration(usb->devhdl, USB_CONFIG); | |
239 | if (ret != LIBUSB_SUCCESS) { | |
240 | sr_err("Failed to set USB configuration: %s.", | |
241 | libusb_error_name(ret)); | |
242 | sr_usb_close(usb); | |
243 | return SR_ERR; | |
244 | } | |
ad6181e2 | 245 | |
e35a4592 DE |
246 | ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE); |
247 | if (ret != LIBUSB_SUCCESS) { | |
248 | sr_err("Failed to claim interface: %s.", | |
249 | libusb_error_name(ret)); | |
250 | sr_usb_close(usb); | |
251 | return SR_ERR; | |
252 | } | |
be64f90b | 253 | |
e35a4592 DE |
254 | ret = drain_usb(usb, EP_REPLY); |
255 | if (ret != SR_OK) { | |
256 | sr_usb_close(usb); | |
257 | return ret; | |
258 | } | |
259 | /* This delay appears to be necessary for reliable operation. */ | |
260 | g_usleep(30 * 1000); | |
93ed0241 | 261 | |
42d14d91 UH |
262 | sdi->status = SR_ST_ACTIVE; |
263 | ||
e35a4592 DE |
264 | devc->active_fpga_config = FPGA_NOCONF; |
265 | devc->short_transfer_quirk = FALSE; | |
266 | devc->state = STATE_IDLE; | |
be64f90b | 267 | |
e35a4592 | 268 | ret = (*devc->model->apply_fpga_config)(sdi); |
be64f90b | 269 | |
e35a4592 DE |
270 | if (ret == SR_OK) |
271 | ret = (*devc->model->device_init_check)(sdi); | |
272 | if (ret == SR_OK) | |
273 | break; | |
be64f90b | 274 | |
e35a4592 | 275 | /* Rinse and repeat. */ |
42d14d91 | 276 | sdi->status = SR_ST_INACTIVE; |
be64f90b | 277 | sr_usb_close(usb); |
c81069b3 | 278 | } |
e35a4592 DE |
279 | |
280 | if (ret == SR_OK && devc->short_transfer_quirk) | |
78648577 DE |
281 | sr_warn("Short transfer quirk detected! " |
282 | "Memory reads will be slow."); | |
e35a4592 | 283 | return ret; |
aeaad0b0 DE |
284 | } |
285 | ||
286 | static int dev_close(struct sr_dev_inst *sdi) | |
287 | { | |
c81069b3 | 288 | struct dev_context *devc; |
be64f90b | 289 | struct sr_usb_dev_inst *usb; |
c81069b3 | 290 | int ret; |
5874e88d | 291 | |
be64f90b DE |
292 | devc = sdi->priv; |
293 | usb = sdi->conn; | |
294 | ||
be64f90b DE |
295 | if (devc->acquisition) { |
296 | sr_err("Cannot close device during acquisition!"); | |
297 | /* Request stop, leak handle, and prepare for the worst. */ | |
298 | devc->cancel_requested = TRUE; | |
299 | return SR_ERR_BUG; | |
300 | } | |
301 | ||
be64f90b DE |
302 | /* Download of the shutdown bitstream, if any. */ |
303 | ret = (*devc->model->apply_fpga_config)(sdi); | |
c81069b3 | 304 | if (ret != SR_OK) |
be64f90b | 305 | sr_warn("Unable to shut down device."); |
5874e88d | 306 | |
6c1a4cb4 UH |
307 | if (usb->devhdl) |
308 | libusb_release_interface(usb->devhdl, USB_INTERFACE); | |
f1ba6b4b | 309 | |
c81069b3 | 310 | sr_usb_close(usb); |
5874e88d | 311 | |
42d14d91 | 312 | return ret; |
aeaad0b0 DE |
313 | } |
314 | ||
be64f90b DE |
315 | /* Check whether the device options contain a specific key. |
316 | * Also match against get/set/list bits if specified. | |
317 | */ | |
318 | static int has_devopt(const struct model_info *model, uint32_t key) | |
aeaad0b0 | 319 | { |
be64f90b DE |
320 | unsigned int i; |
321 | ||
322 | for (i = 0; i < model->num_devopts; i++) { | |
323 | if ((model->devopts[i] & (SR_CONF_MASK | key)) == key) | |
324 | return TRUE; | |
325 | } | |
d64b5f43 | 326 | |
be64f90b | 327 | return FALSE; |
aeaad0b0 DE |
328 | } |
329 | ||
dd7a72ea UH |
330 | static int config_get(uint32_t key, GVariant **data, |
331 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
aeaad0b0 | 332 | { |
5874e88d | 333 | struct dev_context *devc; |
7ed80817 | 334 | unsigned int idx; |
aeaad0b0 | 335 | |
53b4680f | 336 | (void)cg; |
aeaad0b0 | 337 | |
5874e88d DE |
338 | if (!sdi) |
339 | return SR_ERR_ARG; | |
340 | ||
341 | devc = sdi->priv; | |
342 | ||
be64f90b DE |
343 | if (!has_devopt(devc->model, key | SR_CONF_GET)) |
344 | return SR_ERR_NA; | |
345 | ||
aeaad0b0 | 346 | switch (key) { |
5874e88d DE |
347 | case SR_CONF_SAMPLERATE: |
348 | *data = g_variant_new_uint64(devc->samplerate); | |
349 | break; | |
29d58767 DE |
350 | case SR_CONF_LIMIT_MSEC: |
351 | *data = g_variant_new_uint64(devc->limit_msec); | |
352 | break; | |
5874e88d DE |
353 | case SR_CONF_LIMIT_SAMPLES: |
354 | *data = g_variant_new_uint64(devc->limit_samples); | |
355 | break; | |
be64f90b DE |
356 | case SR_CONF_RLE: |
357 | *data = g_variant_new_boolean(devc->cfg_rle); | |
358 | break; | |
5874e88d | 359 | case SR_CONF_EXTERNAL_CLOCK: |
6358f0a9 DE |
360 | *data = g_variant_new_boolean(devc->cfg_clock_source |
361 | == CLOCK_EXT_CLK); | |
362 | break; | |
363 | case SR_CONF_CLOCK_EDGE: | |
364 | idx = devc->cfg_clock_edge; | |
e124cf9b | 365 | if (idx >= ARRAY_SIZE(signal_edges)) |
6358f0a9 | 366 | return SR_ERR_BUG; |
e124cf9b | 367 | *data = g_variant_new_string(signal_edges[idx]); |
5874e88d | 368 | break; |
e6e54bd2 DE |
369 | case SR_CONF_TRIGGER_SOURCE: |
370 | idx = devc->cfg_trigger_source; | |
e124cf9b | 371 | if (idx >= ARRAY_SIZE(trigger_sources)) |
e6e54bd2 | 372 | return SR_ERR_BUG; |
e124cf9b | 373 | *data = g_variant_new_string(trigger_sources[idx]); |
e6e54bd2 DE |
374 | break; |
375 | case SR_CONF_TRIGGER_SLOPE: | |
376 | idx = devc->cfg_trigger_slope; | |
e124cf9b | 377 | if (idx >= ARRAY_SIZE(signal_edges)) |
e6e54bd2 | 378 | return SR_ERR_BUG; |
e124cf9b | 379 | *data = g_variant_new_string(signal_edges[idx]); |
e6e54bd2 | 380 | break; |
aeaad0b0 | 381 | default: |
be64f90b DE |
382 | /* Must not happen for a key listed in devopts. */ |
383 | return SR_ERR_BUG; | |
aeaad0b0 DE |
384 | } |
385 | ||
5874e88d | 386 | return SR_OK; |
aeaad0b0 DE |
387 | } |
388 | ||
dd7a72ea UH |
389 | static int config_set(uint32_t key, GVariant *data, |
390 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
aeaad0b0 | 391 | { |
29d58767 | 392 | uint64_t value; |
5874e88d | 393 | struct dev_context *devc; |
e6e54bd2 | 394 | int idx; |
aeaad0b0 | 395 | |
53b4680f | 396 | (void)cg; |
aeaad0b0 | 397 | |
be64f90b DE |
398 | if (!sdi) |
399 | return SR_ERR_ARG; | |
400 | ||
5874e88d | 401 | devc = sdi->priv; |
be64f90b DE |
402 | |
403 | if (!has_devopt(devc->model, key | SR_CONF_SET)) | |
404 | return SR_ERR_NA; | |
aeaad0b0 | 405 | |
aeaad0b0 | 406 | switch (key) { |
5874e88d | 407 | case SR_CONF_SAMPLERATE: |
29d58767 | 408 | value = g_variant_get_uint64(data); |
be64f90b DE |
409 | if (value < devc->model->samplerates[devc->model->num_samplerates - 1] |
410 | || value > devc->model->samplerates[0]) | |
5874e88d | 411 | return SR_ERR_SAMPLERATE; |
29d58767 DE |
412 | devc->samplerate = value; |
413 | break; | |
414 | case SR_CONF_LIMIT_MSEC: | |
415 | value = g_variant_get_uint64(data); | |
416 | if (value > MAX_LIMIT_MSEC) | |
417 | return SR_ERR_ARG; | |
418 | devc->limit_msec = value; | |
5874e88d DE |
419 | break; |
420 | case SR_CONF_LIMIT_SAMPLES: | |
29d58767 DE |
421 | value = g_variant_get_uint64(data); |
422 | if (value > MAX_LIMIT_SAMPLES) | |
423 | return SR_ERR_ARG; | |
424 | devc->limit_samples = value; | |
5874e88d | 425 | break; |
be64f90b DE |
426 | case SR_CONF_RLE: |
427 | devc->cfg_rle = g_variant_get_boolean(data); | |
428 | break; | |
5874e88d | 429 | case SR_CONF_EXTERNAL_CLOCK: |
6358f0a9 DE |
430 | devc->cfg_clock_source = (g_variant_get_boolean(data)) |
431 | ? CLOCK_EXT_CLK : CLOCK_INTERNAL; | |
432 | break; | |
433 | case SR_CONF_CLOCK_EDGE: | |
e124cf9b | 434 | if ((idx = std_str_idx(data, ARRAY_AND_SIZE(signal_edges))) < 0) |
6358f0a9 DE |
435 | return SR_ERR_ARG; |
436 | devc->cfg_clock_edge = idx; | |
5874e88d | 437 | break; |
e6e54bd2 | 438 | case SR_CONF_TRIGGER_SOURCE: |
e124cf9b | 439 | if ((idx = std_str_idx(data, ARRAY_AND_SIZE(trigger_sources))) < 0) |
e6e54bd2 DE |
440 | return SR_ERR_ARG; |
441 | devc->cfg_trigger_source = idx; | |
442 | break; | |
443 | case SR_CONF_TRIGGER_SLOPE: | |
e124cf9b | 444 | if ((idx = std_str_idx(data, ARRAY_AND_SIZE(signal_edges))) < 0) |
e6e54bd2 DE |
445 | return SR_ERR_ARG; |
446 | devc->cfg_trigger_slope = idx; | |
447 | break; | |
aeaad0b0 | 448 | default: |
be64f90b DE |
449 | /* Must not happen for a key listed in devopts. */ |
450 | return SR_ERR_BUG; | |
aeaad0b0 DE |
451 | } |
452 | ||
5874e88d | 453 | return SR_OK; |
aeaad0b0 DE |
454 | } |
455 | ||
f3ca73ed | 456 | static int config_channel_set(const struct sr_dev_inst *sdi, |
dd7a72ea | 457 | struct sr_channel *ch, unsigned int changes) |
43db3436 | 458 | { |
ba7dd8bb | 459 | uint64_t channel_bit; |
43db3436 DE |
460 | struct dev_context *devc; |
461 | ||
be64f90b DE |
462 | if (!sdi) |
463 | return SR_ERR_ARG; | |
464 | ||
43db3436 | 465 | devc = sdi->priv; |
43db3436 | 466 | |
be64f90b | 467 | if (ch->index < 0 || ch->index >= devc->model->num_channels) { |
ba7dd8bb | 468 | sr_err("Channel index %d out of range.", ch->index); |
43db3436 DE |
469 | return SR_ERR_BUG; |
470 | } | |
43db3436 | 471 | |
3f239f08 | 472 | if ((changes & SR_CHANNEL_SET_ENABLED) != 0) { |
be64f90b DE |
473 | channel_bit = UINT64_C(1) << ch->index; |
474 | ||
475 | /* Enable or disable logic input for this channel. */ | |
ba7dd8bb UH |
476 | if (ch->enabled) |
477 | devc->channel_mask |= channel_bit; | |
43db3436 | 478 | else |
ba7dd8bb | 479 | devc->channel_mask &= ~channel_bit; |
43db3436 DE |
480 | } |
481 | ||
43db3436 DE |
482 | return SR_OK; |
483 | } | |
484 | ||
ca314e06 | 485 | /* Derive trigger masks from the session's trigger configuration. */ |
225d3cb0 DE |
486 | static int prepare_trigger_masks(const struct sr_dev_inst *sdi) |
487 | { | |
d64b5f43 | 488 | uint64_t trigger_mask, trigger_values, trigger_edge_mask; |
09ffac33 | 489 | uint64_t level_bit, type_bit; |
225d3cb0 DE |
490 | struct dev_context *devc; |
491 | struct sr_trigger *trigger; | |
492 | struct sr_trigger_stage *stage; | |
493 | struct sr_trigger_match *match; | |
494 | const GSList *node; | |
be64f90b | 495 | int idx; |
09ffac33 | 496 | enum sr_trigger_matches trg; |
225d3cb0 DE |
497 | |
498 | devc = sdi->priv; | |
499 | ||
500 | trigger = sr_session_trigger_get(sdi->session); | |
501 | if (!trigger || !trigger->stages) | |
502 | return SR_OK; | |
503 | ||
504 | if (trigger->stages->next) { | |
505 | sr_err("This device only supports 1 trigger stage."); | |
506 | return SR_ERR_ARG; | |
507 | } | |
508 | stage = trigger->stages->data; | |
509 | ||
510 | trigger_mask = 0; | |
511 | trigger_values = 0; | |
512 | trigger_edge_mask = 0; | |
513 | ||
514 | for (node = stage->matches; node; node = node->next) { | |
515 | match = node->data; | |
516 | ||
517 | if (!match->channel->enabled) | |
d64b5f43 | 518 | continue; /* Ignore disabled channel. */ |
225d3cb0 | 519 | |
be64f90b | 520 | idx = match->channel->index; |
09ffac33 | 521 | trg = match->match; |
be64f90b DE |
522 | |
523 | if (idx < 0 || idx >= devc->model->num_channels) { | |
524 | sr_err("Channel index %d out of range.", idx); | |
d64b5f43 | 525 | return SR_ERR_BUG; /* Should not happen. */ |
be64f90b | 526 | } |
09ffac33 DE |
527 | if (trg != SR_TRIGGER_ZERO |
528 | && trg != SR_TRIGGER_ONE | |
529 | && trg != SR_TRIGGER_RISING | |
530 | && trg != SR_TRIGGER_FALLING) { | |
be64f90b | 531 | sr_err("Unsupported trigger match for CH%d.", idx + 1); |
225d3cb0 DE |
532 | return SR_ERR_ARG; |
533 | } | |
09ffac33 DE |
534 | level_bit = (trg == SR_TRIGGER_ONE |
535 | || trg == SR_TRIGGER_RISING) ? 1 : 0; | |
536 | type_bit = (trg == SR_TRIGGER_RISING | |
537 | || trg == SR_TRIGGER_FALLING) ? 1 : 0; | |
538 | ||
539 | trigger_mask |= UINT64_C(1) << idx; | |
540 | trigger_values |= level_bit << idx; | |
541 | trigger_edge_mask |= type_bit << idx; | |
225d3cb0 DE |
542 | } |
543 | devc->trigger_mask = trigger_mask; | |
544 | devc->trigger_values = trigger_values; | |
545 | devc->trigger_edge_mask = trigger_edge_mask; | |
546 | ||
547 | return SR_OK; | |
548 | } | |
549 | ||
ee38c8ba DE |
550 | static int config_commit(const struct sr_dev_inst *sdi) |
551 | { | |
225d3cb0 | 552 | struct dev_context *devc; |
be64f90b DE |
553 | int ret; |
554 | ||
555 | devc = sdi->priv; | |
225d3cb0 | 556 | |
225d3cb0 DE |
557 | if (devc->acquisition) { |
558 | sr_err("Acquisition still in progress?"); | |
ee38c8ba DE |
559 | return SR_ERR; |
560 | } | |
561 | ||
be64f90b DE |
562 | ret = prepare_trigger_masks(sdi); |
563 | if (ret != SR_OK) | |
564 | return ret; | |
565 | ||
566 | ret = (*devc->model->apply_fpga_config)(sdi); | |
567 | if (ret != SR_OK) { | |
568 | sr_err("Failed to apply FPGA configuration."); | |
569 | return ret; | |
570 | } | |
571 | ||
572 | return SR_OK; | |
ee38c8ba DE |
573 | } |
574 | ||
be64f90b | 575 | static int config_list(uint32_t key, GVariant **data, |
dd7a72ea | 576 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) |
aeaad0b0 | 577 | { |
be64f90b | 578 | struct dev_context *devc; |
aeaad0b0 | 579 | |
e66d1892 | 580 | devc = (sdi) ? sdi->priv : NULL; |
aeaad0b0 | 581 | |
e66d1892 UH |
582 | switch (key) { |
583 | case SR_CONF_SCAN_OPTIONS: | |
584 | case SR_CONF_DEVICE_OPTIONS: | |
585 | return std_opts_config_list(key, data, sdi, cg, | |
53012da6 | 586 | ARRAY_AND_SIZE(scanopts), ARRAY_AND_SIZE(drvopts), |
e66d1892 UH |
587 | (devc) ? devc->model->devopts : NULL, |
588 | (devc) ? devc->model->num_devopts : 0); | |
be64f90b DE |
589 | } |
590 | ||
aedbf89d GS |
591 | if (!devc) |
592 | return SR_ERR_ARG; | |
be64f90b DE |
593 | if (!has_devopt(devc->model, key | SR_CONF_LIST)) |
594 | return SR_ERR_NA; | |
595 | ||
596 | switch (key) { | |
5874e88d | 597 | case SR_CONF_SAMPLERATE: |
463160cb | 598 | *data = std_gvar_samplerates(devc->model->samplerates, devc->model->num_samplerates); |
5874e88d | 599 | break; |
bbe7e48a | 600 | case SR_CONF_TRIGGER_MATCH: |
53012da6 | 601 | *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches)); |
5874e88d | 602 | break; |
e6e54bd2 | 603 | case SR_CONF_TRIGGER_SOURCE: |
e124cf9b | 604 | *data = g_variant_new_strv(ARRAY_AND_SIZE(trigger_sources)); |
e6e54bd2 DE |
605 | break; |
606 | case SR_CONF_TRIGGER_SLOPE: | |
6358f0a9 | 607 | case SR_CONF_CLOCK_EDGE: |
e124cf9b | 608 | *data = g_variant_new_strv(ARRAY_AND_SIZE(signal_edges)); |
e6e54bd2 | 609 | break; |
aeaad0b0 | 610 | default: |
be64f90b DE |
611 | /* Must not happen for a key listed in devopts. */ |
612 | return SR_ERR_BUG; | |
aeaad0b0 DE |
613 | } |
614 | ||
5874e88d | 615 | return SR_OK; |
aeaad0b0 DE |
616 | } |
617 | ||
be64f90b DE |
618 | /* Set up the device hardware to begin capturing samples as soon as the |
619 | * configured trigger conditions are met, or immediately if no triggers | |
620 | * are configured. | |
621 | */ | |
695dc859 | 622 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) |
5874e88d | 623 | { |
be64f90b | 624 | return lwla_start_acquisition(sdi); |
aeaad0b0 DE |
625 | } |
626 | ||
695dc859 | 627 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) |
aeaad0b0 | 628 | { |
c81069b3 DE |
629 | struct dev_context *devc; |
630 | ||
c81069b3 | 631 | devc = sdi->priv; |
aeaad0b0 | 632 | |
be64f90b | 633 | if (devc->state != STATE_IDLE && !devc->cancel_requested) { |
c81069b3 | 634 | devc->cancel_requested = TRUE; |
d2f7c417 | 635 | sr_dbg("Requesting cancel."); |
c81069b3 | 636 | } |
d64b5f43 | 637 | |
aeaad0b0 DE |
638 | return SR_OK; |
639 | } | |
640 | ||
dd5c48a6 | 641 | static struct sr_dev_driver sysclk_lwla_driver_info = { |
aeaad0b0 | 642 | .name = "sysclk-lwla", |
5874e88d | 643 | .longname = "SysClk LWLA series", |
aeaad0b0 | 644 | .api_version = 1, |
c2fdcc25 | 645 | .init = std_init, |
700d6b64 | 646 | .cleanup = std_cleanup, |
aeaad0b0 | 647 | .scan = scan, |
c01bf34c | 648 | .dev_list = std_dev_list, |
8bf18daa | 649 | .dev_clear = std_dev_clear, |
aeaad0b0 DE |
650 | .config_get = config_get, |
651 | .config_set = config_set, | |
f3ca73ed | 652 | .config_channel_set = config_channel_set, |
ee38c8ba | 653 | .config_commit = config_commit, |
aeaad0b0 DE |
654 | .config_list = config_list, |
655 | .dev_open = dev_open, | |
656 | .dev_close = dev_close, | |
657 | .dev_acquisition_start = dev_acquisition_start, | |
658 | .dev_acquisition_stop = dev_acquisition_stop, | |
41812aca | 659 | .context = NULL, |
aeaad0b0 | 660 | }; |
dd5c48a6 | 661 | SR_REGISTER_DEV_DRIVER(sysclk_lwla_driver_info); |