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