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