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