]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the sigrok project. | |
3 | * | |
4 | * Copyright (C) 2011-2012 Uwe Hermann <uwe@hermann-uwe.de> | |
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 2 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, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
20 | ||
21 | #include <ftdi.h> | |
22 | #include <glib.h> | |
23 | #include <string.h> | |
24 | #include "libsigrok.h" | |
25 | #include "libsigrok-internal.h" | |
26 | #include "protocol.h" | |
27 | ||
28 | SR_PRIV struct sr_dev_driver chronovu_la8_driver_info; | |
29 | static struct sr_dev_driver *di = &chronovu_la8_driver_info; | |
30 | ||
31 | /* This will be initialized via config_list()/SR_CONF_SAMPLERATE. | |
32 | * | |
33 | * Min: 1 sample per 0.01us -> sample time is 0.084s, samplerate 100MHz | |
34 | * Max: 1 sample per 2.55us -> sample time is 21.391s, samplerate 392.15kHz | |
35 | */ | |
36 | SR_PRIV uint64_t chronovu_la8_samplerates[255 + 1] = { 0 }; | |
37 | ||
38 | /* Note: Continuous sampling is not supported by the hardware. */ | |
39 | SR_PRIV const int32_t chronovu_la8_hwcaps[] = { | |
40 | SR_CONF_LOGIC_ANALYZER, | |
41 | SR_CONF_SAMPLERATE, | |
42 | SR_CONF_LIMIT_MSEC, /* TODO: Not yet implemented. */ | |
43 | SR_CONF_LIMIT_SAMPLES, /* TODO: Not yet implemented. */ | |
44 | }; | |
45 | ||
46 | /* | |
47 | * The ChronoVu LA8 can have multiple PIDs. Older versions shipped with | |
48 | * a standard FTDI USB VID/PID of 0403:6001, newer ones have 0403:8867. | |
49 | */ | |
50 | static const uint16_t usb_pids[] = { | |
51 | 0x6001, | |
52 | 0x8867, | |
53 | }; | |
54 | ||
55 | /* Function prototypes. */ | |
56 | static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data); | |
57 | ||
58 | static int clear_instances(void) | |
59 | { | |
60 | GSList *l; | |
61 | struct sr_dev_inst *sdi; | |
62 | struct drv_context *drvc; | |
63 | struct dev_context *devc; | |
64 | ||
65 | drvc = di->priv; | |
66 | ||
67 | /* Properly close all devices. */ | |
68 | for (l = drvc->instances; l; l = l->next) { | |
69 | if (!(sdi = l->data)) { | |
70 | /* Log error, but continue cleaning up the rest. */ | |
71 | sr_err("%s: sdi was NULL, continuing.", __func__); | |
72 | continue; | |
73 | } | |
74 | if (sdi->priv) { | |
75 | devc = sdi->priv; | |
76 | ftdi_free(devc->ftdic); | |
77 | } | |
78 | sr_dev_inst_free(sdi); | |
79 | } | |
80 | g_slist_free(drvc->instances); | |
81 | drvc->instances = NULL; | |
82 | ||
83 | return SR_OK; | |
84 | } | |
85 | ||
86 | static int hw_init(struct sr_context *sr_ctx) | |
87 | { | |
88 | return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN); | |
89 | } | |
90 | ||
91 | static GSList *hw_scan(GSList *options) | |
92 | { | |
93 | struct sr_dev_inst *sdi; | |
94 | struct sr_probe *probe; | |
95 | struct drv_context *drvc; | |
96 | struct dev_context *devc; | |
97 | GSList *devices; | |
98 | unsigned int i; | |
99 | int ret; | |
100 | ||
101 | (void)options; | |
102 | ||
103 | drvc = di->priv; | |
104 | ||
105 | devices = NULL; | |
106 | ||
107 | /* Allocate memory for our private device context. */ | |
108 | if (!(devc = g_try_malloc(sizeof(struct dev_context)))) { | |
109 | sr_err("Device context malloc failed."); | |
110 | goto err_free_nothing; | |
111 | } | |
112 | ||
113 | /* Set some sane defaults. */ | |
114 | devc->ftdic = NULL; | |
115 | devc->cur_samplerate = SR_MHZ(100); /* 100MHz == max. samplerate */ | |
116 | devc->limit_msec = 0; | |
117 | devc->limit_samples = 0; | |
118 | devc->cb_data = NULL; | |
119 | memset(devc->mangled_buf, 0, BS); | |
120 | devc->final_buf = NULL; | |
121 | devc->trigger_pattern = 0x00; /* Value irrelevant, see trigger_mask. */ | |
122 | devc->trigger_mask = 0x00; /* All probes are "don't care". */ | |
123 | devc->trigger_timeout = 10; /* Default to 10s trigger timeout. */ | |
124 | devc->trigger_found = 0; | |
125 | devc->done = 0; | |
126 | devc->block_counter = 0; | |
127 | devc->divcount = 0; /* 10ns sample period == 100MHz samplerate */ | |
128 | devc->usb_pid = 0; | |
129 | ||
130 | /* Allocate memory where we'll store the de-mangled data. */ | |
131 | if (!(devc->final_buf = g_try_malloc(SDRAM_SIZE))) { | |
132 | sr_err("final_buf malloc failed."); | |
133 | goto err_free_devc; | |
134 | } | |
135 | ||
136 | /* Allocate memory for the FTDI context (ftdic) and initialize it. */ | |
137 | if (!(devc->ftdic = ftdi_new())) { | |
138 | sr_err("%s: ftdi_new failed.", __func__); | |
139 | goto err_free_final_buf; | |
140 | } | |
141 | ||
142 | /* Check for the device and temporarily open it. */ | |
143 | for (i = 0; i < ARRAY_SIZE(usb_pids); i++) { | |
144 | sr_dbg("Probing for VID/PID %04x:%04x.", USB_VENDOR_ID, | |
145 | usb_pids[i]); | |
146 | ret = ftdi_usb_open_desc(devc->ftdic, USB_VENDOR_ID, | |
147 | usb_pids[i], USB_DESCRIPTION, NULL); | |
148 | if (ret == 0) { | |
149 | sr_dbg("Found LA8 device (%04x:%04x).", | |
150 | USB_VENDOR_ID, usb_pids[i]); | |
151 | devc->usb_pid = usb_pids[i]; | |
152 | } | |
153 | } | |
154 | ||
155 | if (devc->usb_pid == 0) | |
156 | goto err_free_ftdic; | |
157 | ||
158 | /* Register the device with libsigrok. */ | |
159 | sdi = sr_dev_inst_new(0, SR_ST_INITIALIZING, | |
160 | USB_VENDOR_NAME, USB_MODEL_NAME, USB_MODEL_VERSION); | |
161 | if (!sdi) { | |
162 | sr_err("%s: sr_dev_inst_new failed.", __func__); | |
163 | goto err_close_ftdic; | |
164 | } | |
165 | sdi->driver = di; | |
166 | sdi->priv = devc; | |
167 | ||
168 | for (i = 0; chronovu_la8_probe_names[i]; i++) { | |
169 | if (!(probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE, | |
170 | chronovu_la8_probe_names[i]))) | |
171 | return NULL; | |
172 | sdi->probes = g_slist_append(sdi->probes, probe); | |
173 | } | |
174 | ||
175 | devices = g_slist_append(devices, sdi); | |
176 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
177 | ||
178 | /* Close device. We'll reopen it again when we need it. */ | |
179 | (void) la8_close(devc); /* Log, but ignore errors. */ | |
180 | ||
181 | return devices; | |
182 | ||
183 | err_close_ftdic: | |
184 | (void) la8_close(devc); /* Log, but ignore errors. */ | |
185 | err_free_ftdic: | |
186 | ftdi_free(devc->ftdic); /* NOT free() or g_free()! */ | |
187 | err_free_final_buf: | |
188 | g_free(devc->final_buf); | |
189 | err_free_devc: | |
190 | g_free(devc); | |
191 | err_free_nothing: | |
192 | ||
193 | return NULL; | |
194 | } | |
195 | ||
196 | static GSList *hw_dev_list(void) | |
197 | { | |
198 | return ((struct drv_context *)(di->priv))->instances; | |
199 | } | |
200 | ||
201 | static int hw_dev_open(struct sr_dev_inst *sdi) | |
202 | { | |
203 | struct dev_context *devc; | |
204 | int ret; | |
205 | ||
206 | if (!(devc = sdi->priv)) { | |
207 | sr_err("%s: sdi->priv was NULL.", __func__); | |
208 | return SR_ERR_BUG; | |
209 | } | |
210 | ||
211 | sr_dbg("Opening LA8 device (%04x:%04x).", USB_VENDOR_ID, | |
212 | devc->usb_pid); | |
213 | ||
214 | /* Open the device. */ | |
215 | if ((ret = ftdi_usb_open_desc(devc->ftdic, USB_VENDOR_ID, | |
216 | devc->usb_pid, USB_DESCRIPTION, NULL)) < 0) { | |
217 | sr_err("%s: ftdi_usb_open_desc: (%d) %s", | |
218 | __func__, ret, ftdi_get_error_string(devc->ftdic)); | |
219 | (void) la8_close_usb_reset_sequencer(devc); /* Ignore errors. */ | |
220 | return SR_ERR; | |
221 | } | |
222 | sr_dbg("Device opened successfully."); | |
223 | ||
224 | /* Purge RX/TX buffers in the FTDI chip. */ | |
225 | if ((ret = ftdi_usb_purge_buffers(devc->ftdic)) < 0) { | |
226 | sr_err("%s: ftdi_usb_purge_buffers: (%d) %s", | |
227 | __func__, ret, ftdi_get_error_string(devc->ftdic)); | |
228 | (void) la8_close_usb_reset_sequencer(devc); /* Ignore errors. */ | |
229 | goto err_dev_open_close_ftdic; | |
230 | } | |
231 | sr_dbg("FTDI buffers purged successfully."); | |
232 | ||
233 | /* Enable flow control in the FTDI chip. */ | |
234 | if ((ret = ftdi_setflowctrl(devc->ftdic, SIO_RTS_CTS_HS)) < 0) { | |
235 | sr_err("%s: ftdi_setflowcontrol: (%d) %s", | |
236 | __func__, ret, ftdi_get_error_string(devc->ftdic)); | |
237 | (void) la8_close_usb_reset_sequencer(devc); /* Ignore errors. */ | |
238 | goto err_dev_open_close_ftdic; | |
239 | } | |
240 | sr_dbg("FTDI flow control enabled successfully."); | |
241 | ||
242 | /* Wait 100ms. */ | |
243 | g_usleep(100 * 1000); | |
244 | ||
245 | sdi->status = SR_ST_ACTIVE; | |
246 | ||
247 | return SR_OK; | |
248 | ||
249 | err_dev_open_close_ftdic: | |
250 | (void) la8_close(devc); /* Log, but ignore errors. */ | |
251 | return SR_ERR; | |
252 | } | |
253 | ||
254 | static int hw_dev_close(struct sr_dev_inst *sdi) | |
255 | { | |
256 | struct dev_context *devc; | |
257 | ||
258 | devc = sdi->priv; | |
259 | ||
260 | if (sdi->status == SR_ST_ACTIVE) { | |
261 | sr_dbg("Status ACTIVE, closing device."); | |
262 | (void) la8_close_usb_reset_sequencer(devc); /* Ignore errors. */ | |
263 | } else { | |
264 | sr_spew("Status not ACTIVE, nothing to do."); | |
265 | } | |
266 | ||
267 | sdi->status = SR_ST_INACTIVE; | |
268 | ||
269 | g_free(devc->final_buf); | |
270 | ||
271 | return SR_OK; | |
272 | } | |
273 | ||
274 | static int hw_cleanup(void) | |
275 | { | |
276 | if (!di->priv) | |
277 | /* Can get called on an unused driver, doesn't matter. */ | |
278 | return SR_OK; | |
279 | ||
280 | clear_instances(); | |
281 | ||
282 | return SR_OK; | |
283 | } | |
284 | ||
285 | static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi) | |
286 | { | |
287 | struct dev_context *devc; | |
288 | ||
289 | switch (id) { | |
290 | case SR_CONF_SAMPLERATE: | |
291 | if (sdi) { | |
292 | devc = sdi->priv; | |
293 | *data = g_variant_new_uint64(devc->cur_samplerate); | |
294 | sr_spew("%s: Returning samplerate: %" PRIu64 "Hz.", | |
295 | __func__, devc->cur_samplerate); | |
296 | } else | |
297 | return SR_ERR; | |
298 | break; | |
299 | default: | |
300 | return SR_ERR_ARG; | |
301 | } | |
302 | ||
303 | return SR_OK; | |
304 | } | |
305 | ||
306 | static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi) | |
307 | { | |
308 | struct dev_context *devc; | |
309 | ||
310 | if (!(devc = sdi->priv)) { | |
311 | sr_err("%s: sdi->priv was NULL.", __func__); | |
312 | return SR_ERR_BUG; | |
313 | } | |
314 | ||
315 | switch (id) { | |
316 | case SR_CONF_SAMPLERATE: | |
317 | if (set_samplerate(sdi, g_variant_get_uint64(data)) == SR_ERR) { | |
318 | sr_err("%s: setting samplerate failed.", __func__); | |
319 | return SR_ERR; | |
320 | } | |
321 | sr_dbg("SAMPLERATE = %" PRIu64, devc->cur_samplerate); | |
322 | break; | |
323 | case SR_CONF_LIMIT_MSEC: | |
324 | if (g_variant_get_uint64(data) == 0) { | |
325 | sr_err("%s: LIMIT_MSEC can't be 0.", __func__); | |
326 | return SR_ERR; | |
327 | } | |
328 | devc->limit_msec = g_variant_get_uint64(data); | |
329 | sr_dbg("LIMIT_MSEC = %" PRIu64, devc->limit_msec); | |
330 | break; | |
331 | case SR_CONF_LIMIT_SAMPLES: | |
332 | if (g_variant_get_uint64(data) < MIN_NUM_SAMPLES) { | |
333 | sr_err("%s: LIMIT_SAMPLES too small.", __func__); | |
334 | return SR_ERR; | |
335 | } | |
336 | devc->limit_samples = g_variant_get_uint64(data); | |
337 | sr_dbg("LIMIT_SAMPLES = %" PRIu64, devc->limit_samples); | |
338 | break; | |
339 | default: | |
340 | /* Unknown capability, return SR_ERR. */ | |
341 | sr_err("%s: Unknown capability: %d.", __func__, id); | |
342 | return SR_ERR; | |
343 | break; | |
344 | } | |
345 | ||
346 | return SR_OK; | |
347 | } | |
348 | ||
349 | static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi) | |
350 | { | |
351 | GVariant *gvar; | |
352 | GVariantBuilder gvb; | |
353 | ||
354 | (void)sdi; | |
355 | ||
356 | switch (key) { | |
357 | case SR_CONF_DEVICE_OPTIONS: | |
358 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
359 | chronovu_la8_hwcaps, ARRAY_SIZE(chronovu_la8_hwcaps), | |
360 | sizeof(int32_t)); | |
361 | break; | |
362 | case SR_CONF_SAMPLERATE: | |
363 | fill_supported_samplerates_if_needed(); | |
364 | g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}")); | |
365 | gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"), | |
366 | chronovu_la8_samplerates, ARRAY_SIZE(chronovu_la8_samplerates), | |
367 | sizeof(uint64_t)); | |
368 | g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar); | |
369 | *data = g_variant_builder_end(&gvb); | |
370 | break; | |
371 | case SR_CONF_TRIGGER_TYPE: | |
372 | *data = g_variant_new_string(TRIGGER_TYPE); | |
373 | break; | |
374 | default: | |
375 | return SR_ERR_ARG; | |
376 | } | |
377 | ||
378 | return SR_OK; | |
379 | } | |
380 | ||
381 | static int receive_data(int fd, int revents, void *cb_data) | |
382 | { | |
383 | int i, ret; | |
384 | struct sr_dev_inst *sdi; | |
385 | struct dev_context *devc; | |
386 | ||
387 | (void)fd; | |
388 | (void)revents; | |
389 | ||
390 | if (!(sdi = cb_data)) { | |
391 | sr_err("%s: cb_data was NULL.", __func__); | |
392 | return FALSE; | |
393 | } | |
394 | ||
395 | if (!(devc = sdi->priv)) { | |
396 | sr_err("%s: sdi->priv was NULL.", __func__); | |
397 | return FALSE; | |
398 | } | |
399 | ||
400 | if (!devc->ftdic) { | |
401 | sr_err("%s: devc->ftdic was NULL.", __func__); | |
402 | return FALSE; | |
403 | } | |
404 | ||
405 | /* Get one block of data. */ | |
406 | if ((ret = la8_read_block(devc)) < 0) { | |
407 | sr_err("%s: la8_read_block error: %d.", __func__, ret); | |
408 | hw_dev_acquisition_stop(sdi, sdi); | |
409 | return FALSE; | |
410 | } | |
411 | ||
412 | /* We need to get exactly NUM_BLOCKS blocks (i.e. 8MB) of data. */ | |
413 | if (devc->block_counter != (NUM_BLOCKS - 1)) { | |
414 | devc->block_counter++; | |
415 | return TRUE; | |
416 | } | |
417 | ||
418 | sr_dbg("Sampling finished, sending data to session bus now."); | |
419 | ||
420 | /* All data was received and demangled, send it to the session bus. */ | |
421 | for (i = 0; i < NUM_BLOCKS; i++) | |
422 | send_block_to_session_bus(devc, i); | |
423 | ||
424 | hw_dev_acquisition_stop(sdi, sdi); | |
425 | ||
426 | return TRUE; | |
427 | } | |
428 | ||
429 | static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, | |
430 | void *cb_data) | |
431 | { | |
432 | struct dev_context *devc; | |
433 | uint8_t buf[4]; | |
434 | int bytes_written; | |
435 | ||
436 | if (!(devc = sdi->priv)) { | |
437 | sr_err("%s: sdi->priv was NULL.", __func__); | |
438 | return SR_ERR_BUG; | |
439 | } | |
440 | ||
441 | if (!devc->ftdic) { | |
442 | sr_err("%s: devc->ftdic was NULL.", __func__); | |
443 | return SR_ERR_BUG; | |
444 | } | |
445 | ||
446 | devc->divcount = samplerate_to_divcount(devc->cur_samplerate); | |
447 | if (devc->divcount == 0xff) { | |
448 | sr_err("%s: Invalid divcount/samplerate.", __func__); | |
449 | return SR_ERR; | |
450 | } | |
451 | ||
452 | if (configure_probes(sdi) != SR_OK) { | |
453 | sr_err("Failed to configure probes."); | |
454 | return SR_ERR; | |
455 | } | |
456 | ||
457 | /* Fill acquisition parameters into buf[]. */ | |
458 | buf[0] = devc->divcount; | |
459 | buf[1] = 0xff; /* This byte must always be 0xff. */ | |
460 | buf[2] = devc->trigger_pattern; | |
461 | buf[3] = devc->trigger_mask; | |
462 | ||
463 | /* Start acquisition. */ | |
464 | bytes_written = la8_write(devc, buf, 4); | |
465 | ||
466 | if (bytes_written < 0) { | |
467 | sr_err("Acquisition failed to start: %d.", bytes_written); | |
468 | return SR_ERR; | |
469 | } else if (bytes_written != 4) { | |
470 | sr_err("Acquisition failed to start: %d.", bytes_written); | |
471 | return SR_ERR; | |
472 | } | |
473 | ||
474 | sr_dbg("Hardware acquisition started successfully."); | |
475 | ||
476 | devc->cb_data = cb_data; | |
477 | ||
478 | /* Send header packet to the session bus. */ | |
479 | std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN); | |
480 | ||
481 | /* Time when we should be done (for detecting trigger timeouts). */ | |
482 | devc->done = (devc->divcount + 1) * 0.08388608 + time(NULL) | |
483 | + devc->trigger_timeout; | |
484 | devc->block_counter = 0; | |
485 | devc->trigger_found = 0; | |
486 | ||
487 | /* Hook up a dummy handler to receive data from the LA8. */ | |
488 | sr_source_add(-1, G_IO_IN, 0, receive_data, (void *)sdi); | |
489 | ||
490 | return SR_OK; | |
491 | } | |
492 | ||
493 | static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) | |
494 | { | |
495 | struct sr_datafeed_packet packet; | |
496 | ||
497 | (void)sdi; | |
498 | ||
499 | sr_dbg("Stopping acquisition."); | |
500 | sr_source_remove(-1); | |
501 | ||
502 | /* Send end packet to the session bus. */ | |
503 | sr_dbg("Sending SR_DF_END."); | |
504 | packet.type = SR_DF_END; | |
505 | sr_session_send(cb_data, &packet); | |
506 | ||
507 | return SR_OK; | |
508 | } | |
509 | ||
510 | SR_PRIV struct sr_dev_driver chronovu_la8_driver_info = { | |
511 | .name = "chronovu-la8", | |
512 | .longname = "ChronoVu LA8", | |
513 | .api_version = 1, | |
514 | .init = hw_init, | |
515 | .cleanup = hw_cleanup, | |
516 | .scan = hw_scan, | |
517 | .dev_list = hw_dev_list, | |
518 | .dev_clear = clear_instances, | |
519 | .config_get = config_get, | |
520 | .config_set = config_set, | |
521 | .config_list = config_list, | |
522 | .dev_open = hw_dev_open, | |
523 | .dev_close = hw_dev_close, | |
524 | .dev_acquisition_start = hw_dev_acquisition_start, | |
525 | .dev_acquisition_stop = hw_dev_acquisition_stop, | |
526 | .priv = NULL, | |
527 | }; |