]>
Commit | Line | Data |
---|---|---|
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 | ||
20 | #include "protocol.h" | |
21 | #include "libsigrok.h" | |
22 | #include "libsigrok-internal.h" | |
23 | #include <glib.h> | |
24 | #include <libusb.h> | |
25 | #include <stdlib.h> | |
26 | #include <string.h> | |
27 | ||
28 | static const uint32_t scanopts[] = { | |
29 | SR_CONF_CONN, | |
30 | }; | |
31 | ||
32 | static const uint32_t devopts[] = { | |
33 | SR_CONF_LOGIC_ANALYZER, | |
34 | SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET, | |
35 | SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET, | |
36 | SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
37 | SR_CONF_EXTERNAL_CLOCK | SR_CONF_GET | SR_CONF_SET, | |
38 | SR_CONF_CLOCK_EDGE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
39 | SR_CONF_TRIGGER_MATCH | SR_CONF_LIST, | |
40 | SR_CONF_TRIGGER_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
41 | SR_CONF_TRIGGER_SLOPE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
42 | }; | |
43 | ||
44 | static const int32_t trigger_matches[] = { | |
45 | SR_TRIGGER_ZERO, | |
46 | SR_TRIGGER_ONE, | |
47 | SR_TRIGGER_RISING, | |
48 | SR_TRIGGER_FALLING, | |
49 | }; | |
50 | ||
51 | /* The hardware supports more samplerates than these, but these are the | |
52 | * options hardcoded into the vendor's Windows GUI. | |
53 | */ | |
54 | static const uint64_t samplerates[] = { | |
55 | SR_MHZ(125), SR_MHZ(100), | |
56 | SR_MHZ(50), SR_MHZ(20), SR_MHZ(10), | |
57 | SR_MHZ(5), SR_MHZ(2), SR_MHZ(1), | |
58 | SR_KHZ(500), SR_KHZ(200), SR_KHZ(100), | |
59 | SR_KHZ(50), SR_KHZ(20), SR_KHZ(10), | |
60 | SR_KHZ(5), SR_KHZ(2), SR_KHZ(1), | |
61 | SR_HZ(500), SR_HZ(200), SR_HZ(100), | |
62 | }; | |
63 | ||
64 | /* Names assigned to available trigger sources. Indices must match | |
65 | * trigger_source enum values. | |
66 | */ | |
67 | static const char *const trigger_source_names[] = { "CH", "TRG" }; | |
68 | ||
69 | /* Names assigned to available trigger slope choices. Indices must | |
70 | * match the signal_edge enum values. | |
71 | */ | |
72 | static const char *const signal_edge_names[] = { "r", "f" }; | |
73 | ||
74 | SR_PRIV struct sr_dev_driver sysclk_lwla_driver_info; | |
75 | static struct sr_dev_driver *const di = &sysclk_lwla_driver_info; | |
76 | ||
77 | static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx) | |
78 | { | |
79 | return std_init(sr_ctx, di, LOG_PREFIX); | |
80 | } | |
81 | ||
82 | static struct sr_dev_inst *dev_inst_new(void) | |
83 | { | |
84 | struct sr_dev_inst *sdi; | |
85 | struct dev_context *devc; | |
86 | int i; | |
87 | char name[8]; | |
88 | ||
89 | /* Allocate memory for our private driver context. */ | |
90 | devc = g_malloc0(sizeof(struct dev_context)); | |
91 | ||
92 | /* Register the device with libsigrok. */ | |
93 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
94 | sdi->status = SR_ST_INACTIVE; | |
95 | sdi->vendor = g_strdup(VENDOR_NAME); | |
96 | sdi->model = g_strdup(MODEL_NAME); | |
97 | ||
98 | /* Enable all channels to match the default channel configuration. */ | |
99 | devc->channel_mask = ALL_CHANNELS_MASK; | |
100 | devc->samplerate = DEFAULT_SAMPLERATE; | |
101 | ||
102 | sdi->priv = devc; | |
103 | for (i = NUM_CHANNELS; i > 0; --i) { | |
104 | /* The LWLA series simply number channels from CH1 to CHxx. */ | |
105 | g_snprintf(name, sizeof(name), "CH%d", i); | |
106 | sr_channel_new(sdi, i - 1, SR_CHANNEL_LOGIC, TRUE, name); | |
107 | } | |
108 | ||
109 | return sdi; | |
110 | } | |
111 | ||
112 | static GSList *scan(struct sr_dev_driver *di, GSList *options) | |
113 | { | |
114 | GSList *usb_devices, *devices, *node; | |
115 | struct drv_context *drvc; | |
116 | struct sr_dev_inst *sdi; | |
117 | struct sr_usb_dev_inst *usb; | |
118 | struct sr_config *src; | |
119 | const char *conn; | |
120 | ||
121 | drvc = di->priv; | |
122 | conn = USB_VID_PID; | |
123 | ||
124 | for (node = options; node != NULL; node = node->next) { | |
125 | src = node->data; | |
126 | if (src->key == SR_CONF_CONN) { | |
127 | conn = g_variant_get_string(src->data, NULL); | |
128 | break; | |
129 | } | |
130 | } | |
131 | usb_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn); | |
132 | devices = NULL; | |
133 | ||
134 | for (node = usb_devices; node != NULL; node = node->next) { | |
135 | usb = node->data; | |
136 | ||
137 | /* Create sigrok device instance. */ | |
138 | sdi = dev_inst_new(); | |
139 | if (!sdi) { | |
140 | sr_usb_dev_inst_free(usb); | |
141 | continue; | |
142 | } | |
143 | sdi->driver = di; | |
144 | sdi->inst_type = SR_INST_USB; | |
145 | sdi->conn = usb; | |
146 | ||
147 | /* Register device instance with driver. */ | |
148 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
149 | devices = g_slist_append(devices, sdi); | |
150 | } | |
151 | ||
152 | g_slist_free(usb_devices); | |
153 | ||
154 | return devices; | |
155 | } | |
156 | ||
157 | static GSList *dev_list(const struct sr_dev_driver *di) | |
158 | { | |
159 | struct drv_context *drvc; | |
160 | ||
161 | drvc = di->priv; | |
162 | ||
163 | return drvc->instances; | |
164 | } | |
165 | ||
166 | static void clear_dev_context(void *priv) | |
167 | { | |
168 | struct dev_context *devc; | |
169 | ||
170 | devc = priv; | |
171 | ||
172 | sr_dbg("Device context cleared."); | |
173 | ||
174 | lwla_free_acquisition_state(devc->acquisition); | |
175 | g_free(devc); | |
176 | } | |
177 | ||
178 | static int dev_clear(const struct sr_dev_driver *di) | |
179 | { | |
180 | return std_dev_clear(di, &clear_dev_context); | |
181 | } | |
182 | ||
183 | static int dev_open(struct sr_dev_inst *sdi) | |
184 | { | |
185 | struct drv_context *drvc; | |
186 | struct sr_usb_dev_inst *usb; | |
187 | int ret; | |
188 | ||
189 | drvc = di->priv; | |
190 | ||
191 | if (!drvc) { | |
192 | sr_err("Driver was not initialized."); | |
193 | return SR_ERR; | |
194 | } | |
195 | ||
196 | usb = sdi->conn; | |
197 | ||
198 | ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb); | |
199 | if (ret != SR_OK) | |
200 | return ret; | |
201 | ||
202 | ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE); | |
203 | if (ret < 0) { | |
204 | sr_err("Failed to claim interface: %s.", | |
205 | libusb_error_name(ret)); | |
206 | return SR_ERR; | |
207 | } | |
208 | ||
209 | sdi->status = SR_ST_INITIALIZING; | |
210 | ||
211 | ret = lwla_init_device(sdi); | |
212 | ||
213 | if (ret == SR_OK) | |
214 | sdi->status = SR_ST_ACTIVE; | |
215 | ||
216 | return ret; | |
217 | } | |
218 | ||
219 | static int dev_close(struct sr_dev_inst *sdi) | |
220 | { | |
221 | struct sr_usb_dev_inst *usb; | |
222 | ||
223 | if (!di->priv) { | |
224 | sr_err("Driver was not initialized."); | |
225 | return SR_ERR; | |
226 | } | |
227 | ||
228 | usb = sdi->conn; | |
229 | if (!usb->devhdl) | |
230 | return SR_OK; | |
231 | ||
232 | sdi->status = SR_ST_INACTIVE; | |
233 | ||
234 | /* Trigger download of the shutdown bitstream. */ | |
235 | if (lwla_set_clock_config(sdi) != SR_OK) | |
236 | sr_err("Unable to shut down device."); | |
237 | ||
238 | libusb_release_interface(usb->devhdl, USB_INTERFACE); | |
239 | libusb_close(usb->devhdl); | |
240 | ||
241 | usb->devhdl = NULL; | |
242 | ||
243 | return SR_OK; | |
244 | } | |
245 | ||
246 | static int cleanup(const struct sr_dev_driver *di) | |
247 | { | |
248 | return dev_clear(di); | |
249 | } | |
250 | ||
251 | static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, | |
252 | const struct sr_channel_group *cg) | |
253 | { | |
254 | struct dev_context *devc; | |
255 | size_t idx; | |
256 | ||
257 | (void)cg; | |
258 | ||
259 | if (!sdi) | |
260 | return SR_ERR_ARG; | |
261 | ||
262 | devc = sdi->priv; | |
263 | ||
264 | switch (key) { | |
265 | case SR_CONF_SAMPLERATE: | |
266 | *data = g_variant_new_uint64(devc->samplerate); | |
267 | break; | |
268 | case SR_CONF_LIMIT_MSEC: | |
269 | *data = g_variant_new_uint64(devc->limit_msec); | |
270 | break; | |
271 | case SR_CONF_LIMIT_SAMPLES: | |
272 | *data = g_variant_new_uint64(devc->limit_samples); | |
273 | break; | |
274 | case SR_CONF_EXTERNAL_CLOCK: | |
275 | *data = g_variant_new_boolean(devc->cfg_clock_source | |
276 | == CLOCK_EXT_CLK); | |
277 | break; | |
278 | case SR_CONF_CLOCK_EDGE: | |
279 | idx = devc->cfg_clock_edge; | |
280 | if (idx >= G_N_ELEMENTS(signal_edge_names)) | |
281 | return SR_ERR_BUG; | |
282 | *data = g_variant_new_string(signal_edge_names[idx]); | |
283 | break; | |
284 | case SR_CONF_TRIGGER_SOURCE: | |
285 | idx = devc->cfg_trigger_source; | |
286 | if (idx >= G_N_ELEMENTS(trigger_source_names)) | |
287 | return SR_ERR_BUG; | |
288 | *data = g_variant_new_string(trigger_source_names[idx]); | |
289 | break; | |
290 | case SR_CONF_TRIGGER_SLOPE: | |
291 | idx = devc->cfg_trigger_slope; | |
292 | if (idx >= G_N_ELEMENTS(signal_edge_names)) | |
293 | return SR_ERR_BUG; | |
294 | *data = g_variant_new_string(signal_edge_names[idx]); | |
295 | break; | |
296 | default: | |
297 | return SR_ERR_NA; | |
298 | } | |
299 | ||
300 | return SR_OK; | |
301 | } | |
302 | ||
303 | /* Helper for mapping a string-typed configuration value to an index | |
304 | * within a table of possible values. | |
305 | */ | |
306 | static int lookup_index(GVariant *value, const char *const *table, int len) | |
307 | { | |
308 | const char *entry; | |
309 | int i; | |
310 | ||
311 | entry = g_variant_get_string(value, NULL); | |
312 | if (!entry) | |
313 | return -1; | |
314 | ||
315 | /* Linear search is fine for very small tables. */ | |
316 | for (i = 0; i < len; ++i) { | |
317 | if (strcmp(entry, table[i]) == 0) | |
318 | return i; | |
319 | } | |
320 | return -1; | |
321 | } | |
322 | ||
323 | static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, | |
324 | const struct sr_channel_group *cg) | |
325 | { | |
326 | uint64_t value; | |
327 | struct dev_context *devc; | |
328 | int idx; | |
329 | ||
330 | (void)cg; | |
331 | ||
332 | devc = sdi->priv; | |
333 | if (!devc) | |
334 | return SR_ERR_DEV_CLOSED; | |
335 | ||
336 | switch (key) { | |
337 | case SR_CONF_SAMPLERATE: | |
338 | value = g_variant_get_uint64(data); | |
339 | if (value < samplerates[G_N_ELEMENTS(samplerates) - 1] | |
340 | || value > samplerates[0]) | |
341 | return SR_ERR_SAMPLERATE; | |
342 | devc->samplerate = value; | |
343 | break; | |
344 | case SR_CONF_LIMIT_MSEC: | |
345 | value = g_variant_get_uint64(data); | |
346 | if (value > MAX_LIMIT_MSEC) | |
347 | return SR_ERR_ARG; | |
348 | devc->limit_msec = value; | |
349 | break; | |
350 | case SR_CONF_LIMIT_SAMPLES: | |
351 | value = g_variant_get_uint64(data); | |
352 | if (value > MAX_LIMIT_SAMPLES) | |
353 | return SR_ERR_ARG; | |
354 | devc->limit_samples = value; | |
355 | break; | |
356 | case SR_CONF_EXTERNAL_CLOCK: | |
357 | devc->cfg_clock_source = (g_variant_get_boolean(data)) | |
358 | ? CLOCK_EXT_CLK : CLOCK_INTERNAL; | |
359 | break; | |
360 | case SR_CONF_CLOCK_EDGE: | |
361 | idx = lookup_index(data, signal_edge_names, | |
362 | G_N_ELEMENTS(signal_edge_names)); | |
363 | if (idx < 0) | |
364 | return SR_ERR_ARG; | |
365 | devc->cfg_clock_edge = idx; | |
366 | break; | |
367 | case SR_CONF_TRIGGER_SOURCE: | |
368 | idx = lookup_index(data, trigger_source_names, | |
369 | G_N_ELEMENTS(trigger_source_names)); | |
370 | if (idx < 0) | |
371 | return SR_ERR_ARG; | |
372 | devc->cfg_trigger_source = idx; | |
373 | break; | |
374 | case SR_CONF_TRIGGER_SLOPE: | |
375 | idx = lookup_index(data, signal_edge_names, | |
376 | G_N_ELEMENTS(signal_edge_names)); | |
377 | if (idx < 0) | |
378 | return SR_ERR_ARG; | |
379 | devc->cfg_trigger_slope = idx; | |
380 | break; | |
381 | default: | |
382 | return SR_ERR_NA; | |
383 | } | |
384 | ||
385 | return SR_OK; | |
386 | } | |
387 | ||
388 | static int config_channel_set(const struct sr_dev_inst *sdi, | |
389 | struct sr_channel *ch, unsigned int changes) | |
390 | { | |
391 | uint64_t channel_bit; | |
392 | struct dev_context *devc; | |
393 | ||
394 | devc = sdi->priv; | |
395 | if (!devc) | |
396 | return SR_ERR_DEV_CLOSED; | |
397 | ||
398 | if (ch->index < 0 || ch->index >= NUM_CHANNELS) { | |
399 | sr_err("Channel index %d out of range.", ch->index); | |
400 | return SR_ERR_BUG; | |
401 | } | |
402 | channel_bit = (uint64_t)1 << ch->index; | |
403 | ||
404 | if ((changes & SR_CHANNEL_SET_ENABLED) != 0) { | |
405 | /* Enable or disable input channel for this channel. */ | |
406 | if (ch->enabled) | |
407 | devc->channel_mask |= channel_bit; | |
408 | else | |
409 | devc->channel_mask &= ~channel_bit; | |
410 | } | |
411 | ||
412 | return SR_OK; | |
413 | } | |
414 | ||
415 | static int config_commit(const struct sr_dev_inst *sdi) | |
416 | { | |
417 | if (sdi->status != SR_ST_ACTIVE) { | |
418 | sr_err("Device not ready (status %d).", (int)sdi->status); | |
419 | return SR_ERR; | |
420 | } | |
421 | ||
422 | return lwla_set_clock_config(sdi); | |
423 | } | |
424 | ||
425 | static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, | |
426 | const struct sr_channel_group *cg) | |
427 | { | |
428 | GVariant *gvar; | |
429 | GVariantBuilder gvb; | |
430 | ||
431 | (void)sdi; | |
432 | (void)cg; | |
433 | ||
434 | switch (key) { | |
435 | case SR_CONF_SCAN_OPTIONS: | |
436 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
437 | scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t)); | |
438 | break; | |
439 | case SR_CONF_DEVICE_OPTIONS: | |
440 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
441 | devopts, G_N_ELEMENTS(devopts), sizeof(uint32_t)); | |
442 | break; | |
443 | case SR_CONF_SAMPLERATE: | |
444 | g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}")); | |
445 | gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"), | |
446 | samplerates, G_N_ELEMENTS(samplerates), | |
447 | sizeof(uint64_t)); | |
448 | g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar); | |
449 | *data = g_variant_builder_end(&gvb); | |
450 | break; | |
451 | case SR_CONF_TRIGGER_MATCH: | |
452 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
453 | trigger_matches, ARRAY_SIZE(trigger_matches), | |
454 | sizeof(int32_t)); | |
455 | break; | |
456 | case SR_CONF_TRIGGER_SOURCE: | |
457 | *data = g_variant_new_strv(trigger_source_names, | |
458 | G_N_ELEMENTS(trigger_source_names)); | |
459 | break; | |
460 | case SR_CONF_TRIGGER_SLOPE: | |
461 | case SR_CONF_CLOCK_EDGE: | |
462 | *data = g_variant_new_strv(signal_edge_names, | |
463 | G_N_ELEMENTS(signal_edge_names)); | |
464 | break; | |
465 | default: | |
466 | return SR_ERR_NA; | |
467 | } | |
468 | ||
469 | return SR_OK; | |
470 | } | |
471 | ||
472 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) | |
473 | { | |
474 | struct drv_context *drvc; | |
475 | struct dev_context *devc; | |
476 | struct acquisition_state *acq; | |
477 | int ret; | |
478 | ||
479 | (void)cb_data; | |
480 | ||
481 | if (sdi->status != SR_ST_ACTIVE) | |
482 | return SR_ERR_DEV_CLOSED; | |
483 | ||
484 | devc = sdi->priv; | |
485 | drvc = di->priv; | |
486 | ||
487 | if (devc->acquisition) { | |
488 | sr_err("Acquisition still in progress?"); | |
489 | return SR_ERR; | |
490 | } | |
491 | acq = lwla_alloc_acquisition_state(); | |
492 | if (!acq) | |
493 | return SR_ERR_MALLOC; | |
494 | ||
495 | devc->stopping_in_progress = FALSE; | |
496 | devc->transfer_error = FALSE; | |
497 | ||
498 | sr_info("Starting acquisition."); | |
499 | ||
500 | devc->acquisition = acq; | |
501 | lwla_convert_trigger(sdi); | |
502 | ret = lwla_setup_acquisition(sdi); | |
503 | if (ret != SR_OK) { | |
504 | sr_err("Failed to set up acquisition."); | |
505 | devc->acquisition = NULL; | |
506 | lwla_free_acquisition_state(acq); | |
507 | return ret; | |
508 | } | |
509 | ||
510 | ret = lwla_start_acquisition(sdi); | |
511 | if (ret != SR_OK) { | |
512 | sr_err("Failed to start acquisition."); | |
513 | devc->acquisition = NULL; | |
514 | lwla_free_acquisition_state(acq); | |
515 | return ret; | |
516 | } | |
517 | usb_source_add(sdi->session, drvc->sr_ctx, 100, &lwla_receive_data, | |
518 | (struct sr_dev_inst *)sdi); | |
519 | ||
520 | sr_info("Waiting for data."); | |
521 | ||
522 | /* Send header packet to the session bus. */ | |
523 | std_session_send_df_header(sdi, LOG_PREFIX); | |
524 | ||
525 | return SR_OK; | |
526 | } | |
527 | ||
528 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) | |
529 | { | |
530 | (void)cb_data; | |
531 | ||
532 | if (sdi->status != SR_ST_ACTIVE) | |
533 | return SR_ERR_DEV_CLOSED; | |
534 | ||
535 | sr_dbg("Stopping acquisition."); | |
536 | ||
537 | sdi->status = SR_ST_STOPPING; | |
538 | ||
539 | return SR_OK; | |
540 | } | |
541 | ||
542 | SR_PRIV struct sr_dev_driver sysclk_lwla_driver_info = { | |
543 | .name = "sysclk-lwla", | |
544 | .longname = "SysClk LWLA series", | |
545 | .api_version = 1, | |
546 | .init = init, | |
547 | .cleanup = cleanup, | |
548 | .scan = scan, | |
549 | .dev_list = dev_list, | |
550 | .dev_clear = dev_clear, | |
551 | .config_get = config_get, | |
552 | .config_set = config_set, | |
553 | .config_channel_set = config_channel_set, | |
554 | .config_commit = config_commit, | |
555 | .config_list = config_list, | |
556 | .dev_open = dev_open, | |
557 | .dev_close = dev_close, | |
558 | .dev_acquisition_start = dev_acquisition_start, | |
559 | .dev_acquisition_stop = dev_acquisition_stop, | |
560 | .priv = NULL, | |
561 | }; |