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