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