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