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