]>
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 | /* | |
32 | * The ChronoVu LA8 can have multiple PIDs. Older versions shipped with | |
33 | * a standard FTDI USB VID/PID of 0403:6001, newer ones have 0403:8867. | |
34 | */ | |
35 | static const uint16_t usb_pids[] = { | |
36 | 0x6001, | |
37 | 0x8867, | |
38 | }; | |
39 | ||
40 | /* Function prototypes. */ | |
41 | static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data); | |
42 | ||
43 | static int clear_instances(void) | |
44 | { | |
45 | GSList *l; | |
46 | struct sr_dev_inst *sdi; | |
47 | struct drv_context *drvc; | |
48 | struct dev_context *devc; | |
49 | ||
50 | drvc = di->priv; | |
51 | ||
52 | /* Properly close all devices. */ | |
53 | for (l = drvc->instances; l; l = l->next) { | |
54 | if (!(sdi = l->data)) { | |
55 | /* Log error, but continue cleaning up the rest. */ | |
56 | sr_err("%s: sdi was NULL, continuing.", __func__); | |
57 | continue; | |
58 | } | |
59 | if (sdi->priv) { | |
60 | devc = sdi->priv; | |
61 | ftdi_free(devc->ftdic); | |
62 | } | |
63 | sr_dev_inst_free(sdi); | |
64 | } | |
65 | g_slist_free(drvc->instances); | |
66 | drvc->instances = NULL; | |
67 | ||
68 | return SR_OK; | |
69 | } | |
70 | ||
71 | static int hw_init(struct sr_context *sr_ctx) | |
72 | { | |
73 | return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN); | |
74 | } | |
75 | ||
76 | static GSList *hw_scan(GSList *options) | |
77 | { | |
78 | struct sr_dev_inst *sdi; | |
79 | struct sr_probe *probe; | |
80 | struct drv_context *drvc; | |
81 | struct dev_context *devc; | |
82 | GSList *devices; | |
83 | unsigned int i; | |
84 | int ret; | |
85 | ||
86 | (void)options; | |
87 | ||
88 | drvc = di->priv; | |
89 | ||
90 | devices = NULL; | |
91 | ||
92 | /* Allocate memory for our private device context. */ | |
93 | if (!(devc = g_try_malloc(sizeof(struct dev_context)))) { | |
94 | sr_err("Device context malloc failed."); | |
95 | goto err_free_nothing; | |
96 | } | |
97 | ||
98 | /* Set some sane defaults. */ | |
99 | devc->ftdic = NULL; | |
100 | devc->cur_samplerate = SR_MHZ(100); /* 100MHz == max. samplerate */ | |
101 | devc->limit_msec = 0; | |
102 | devc->limit_samples = 0; | |
103 | devc->session_dev_id = NULL; | |
104 | memset(devc->mangled_buf, 0, BS); | |
105 | devc->final_buf = NULL; | |
106 | devc->trigger_pattern = 0x00; /* Value irrelevant, see trigger_mask. */ | |
107 | devc->trigger_mask = 0x00; /* All probes are "don't care". */ | |
108 | devc->trigger_timeout = 10; /* Default to 10s trigger timeout. */ | |
109 | devc->trigger_found = 0; | |
110 | devc->done = 0; | |
111 | devc->block_counter = 0; | |
112 | devc->divcount = 0; /* 10ns sample period == 100MHz samplerate */ | |
113 | devc->usb_pid = 0; | |
114 | ||
115 | /* Allocate memory where we'll store the de-mangled data. */ | |
116 | if (!(devc->final_buf = g_try_malloc(SDRAM_SIZE))) { | |
117 | sr_err("final_buf malloc failed."); | |
118 | goto err_free_devc; | |
119 | } | |
120 | ||
121 | /* Allocate memory for the FTDI context (ftdic) and initialize it. */ | |
122 | if (!(devc->ftdic = ftdi_new())) { | |
123 | sr_err("%s: ftdi_new failed.", __func__); | |
124 | goto err_free_final_buf; | |
125 | } | |
126 | ||
127 | /* Check for the device and temporarily open it. */ | |
128 | for (i = 0; i < ARRAY_SIZE(usb_pids); i++) { | |
129 | sr_dbg("Probing for VID/PID %04x:%04x.", USB_VENDOR_ID, | |
130 | usb_pids[i]); | |
131 | ret = ftdi_usb_open_desc(devc->ftdic, USB_VENDOR_ID, | |
132 | usb_pids[i], USB_DESCRIPTION, NULL); | |
133 | if (ret == 0) { | |
134 | sr_dbg("Found LA8 device (%04x:%04x).", | |
135 | USB_VENDOR_ID, usb_pids[i]); | |
136 | devc->usb_pid = usb_pids[i]; | |
137 | } | |
138 | } | |
139 | ||
140 | if (devc->usb_pid == 0) | |
141 | goto err_free_ftdic; | |
142 | ||
143 | /* Register the device with libsigrok. */ | |
144 | sdi = sr_dev_inst_new(0, SR_ST_INITIALIZING, | |
145 | USB_VENDOR_NAME, USB_MODEL_NAME, USB_MODEL_VERSION); | |
146 | if (!sdi) { | |
147 | sr_err("%s: sr_dev_inst_new failed.", __func__); | |
148 | goto err_close_ftdic; | |
149 | } | |
150 | sdi->driver = di; | |
151 | sdi->priv = devc; | |
152 | ||
153 | for (i = 0; probe_names[i]; i++) { | |
154 | if (!(probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE, | |
155 | probe_names[i]))) | |
156 | return NULL; | |
157 | sdi->probes = g_slist_append(sdi->probes, probe); | |
158 | } | |
159 | ||
160 | devices = g_slist_append(devices, sdi); | |
161 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
162 | ||
163 | /* Close device. We'll reopen it again when we need it. */ | |
164 | (void) la8_close(devc); /* Log, but ignore errors. */ | |
165 | ||
166 | return devices; | |
167 | ||
168 | err_close_ftdic: | |
169 | (void) la8_close(devc); /* Log, but ignore errors. */ | |
170 | err_free_ftdic: | |
171 | ftdi_free(devc->ftdic); /* NOT free() or g_free()! */ | |
172 | err_free_final_buf: | |
173 | g_free(devc->final_buf); | |
174 | err_free_devc: | |
175 | g_free(devc); | |
176 | err_free_nothing: | |
177 | ||
178 | return NULL; | |
179 | } | |
180 | ||
181 | static GSList *hw_dev_list(void) | |
182 | { | |
183 | return ((struct drv_context *)(di->priv))->instances; | |
184 | } | |
185 | ||
186 | static int hw_dev_open(struct sr_dev_inst *sdi) | |
187 | { | |
188 | struct dev_context *devc; | |
189 | int ret; | |
190 | ||
191 | if (!(devc = sdi->priv)) { | |
192 | sr_err("%s: sdi->priv was NULL.", __func__); | |
193 | return SR_ERR_BUG; | |
194 | } | |
195 | ||
196 | sr_dbg("Opening LA8 device (%04x:%04x).", USB_VENDOR_ID, | |
197 | devc->usb_pid); | |
198 | ||
199 | /* Open the device. */ | |
200 | if ((ret = ftdi_usb_open_desc(devc->ftdic, USB_VENDOR_ID, | |
201 | devc->usb_pid, USB_DESCRIPTION, NULL)) < 0) { | |
202 | sr_err("%s: ftdi_usb_open_desc: (%d) %s", | |
203 | __func__, ret, ftdi_get_error_string(devc->ftdic)); | |
204 | (void) la8_close_usb_reset_sequencer(devc); /* Ignore errors. */ | |
205 | return SR_ERR; | |
206 | } | |
207 | sr_dbg("Device opened successfully."); | |
208 | ||
209 | /* Purge RX/TX buffers in the FTDI chip. */ | |
210 | if ((ret = ftdi_usb_purge_buffers(devc->ftdic)) < 0) { | |
211 | sr_err("%s: ftdi_usb_purge_buffers: (%d) %s", | |
212 | __func__, ret, ftdi_get_error_string(devc->ftdic)); | |
213 | (void) la8_close_usb_reset_sequencer(devc); /* Ignore errors. */ | |
214 | goto err_dev_open_close_ftdic; | |
215 | } | |
216 | sr_dbg("FTDI buffers purged successfully."); | |
217 | ||
218 | /* Enable flow control in the FTDI chip. */ | |
219 | if ((ret = ftdi_setflowctrl(devc->ftdic, SIO_RTS_CTS_HS)) < 0) { | |
220 | sr_err("%s: ftdi_setflowcontrol: (%d) %s", | |
221 | __func__, ret, ftdi_get_error_string(devc->ftdic)); | |
222 | (void) la8_close_usb_reset_sequencer(devc); /* Ignore errors. */ | |
223 | goto err_dev_open_close_ftdic; | |
224 | } | |
225 | sr_dbg("FTDI flow control enabled successfully."); | |
226 | ||
227 | /* Wait 100ms. */ | |
228 | g_usleep(100 * 1000); | |
229 | ||
230 | sdi->status = SR_ST_ACTIVE; | |
231 | ||
232 | return SR_OK; | |
233 | ||
234 | err_dev_open_close_ftdic: | |
235 | (void) la8_close(devc); /* Log, but ignore errors. */ | |
236 | return SR_ERR; | |
237 | } | |
238 | ||
239 | static int hw_dev_close(struct sr_dev_inst *sdi) | |
240 | { | |
241 | struct dev_context *devc; | |
242 | ||
243 | if (!(devc = sdi->priv)) { | |
244 | sr_err("%s: sdi->priv was NULL.", __func__); | |
245 | return SR_ERR_BUG; | |
246 | } | |
247 | ||
248 | sr_dbg("Closing device."); | |
249 | ||
250 | if (sdi->status == SR_ST_ACTIVE) { | |
251 | sr_dbg("Status ACTIVE, closing device."); | |
252 | (void) la8_close_usb_reset_sequencer(devc); /* Ignore errors. */ | |
253 | } else { | |
254 | sr_spew("Status not ACTIVE, nothing to do."); | |
255 | } | |
256 | ||
257 | sdi->status = SR_ST_INACTIVE; | |
258 | ||
259 | sr_dbg("Freeing sample buffer."); | |
260 | g_free(devc->final_buf); | |
261 | ||
262 | return SR_OK; | |
263 | } | |
264 | ||
265 | static int hw_cleanup(void) | |
266 | { | |
267 | if (!di->priv) | |
268 | /* Can get called on an unused driver, doesn't matter. */ | |
269 | return SR_OK; | |
270 | ||
271 | clear_instances(); | |
272 | ||
273 | return SR_OK; | |
274 | } | |
275 | ||
276 | static int config_get(int id, const void **data, const struct sr_dev_inst *sdi) | |
277 | { | |
278 | struct dev_context *devc; | |
279 | ||
280 | switch (id) { | |
281 | case SR_CONF_SAMPLERATE: | |
282 | if (sdi) { | |
283 | devc = sdi->priv; | |
284 | *data = &devc->cur_samplerate; | |
285 | sr_spew("%s: Returning samplerate: %" PRIu64 "Hz.", | |
286 | __func__, devc->cur_samplerate); | |
287 | } else | |
288 | return SR_ERR; | |
289 | break; | |
290 | default: | |
291 | return SR_ERR_ARG; | |
292 | } | |
293 | ||
294 | return SR_OK; | |
295 | } | |
296 | ||
297 | static int config_set(int id, const void *value, const struct sr_dev_inst *sdi) | |
298 | { | |
299 | struct dev_context *devc; | |
300 | ||
301 | if (!(devc = sdi->priv)) { | |
302 | sr_err("%s: sdi->priv was NULL.", __func__); | |
303 | return SR_ERR_BUG; | |
304 | } | |
305 | ||
306 | switch (id) { | |
307 | case SR_CONF_SAMPLERATE: | |
308 | if (set_samplerate(sdi, *(const uint64_t *)value) == SR_ERR) { | |
309 | sr_err("%s: setting samplerate failed.", __func__); | |
310 | return SR_ERR; | |
311 | } | |
312 | sr_dbg("SAMPLERATE = %" PRIu64, devc->cur_samplerate); | |
313 | break; | |
314 | case SR_CONF_LIMIT_MSEC: | |
315 | if (*(const uint64_t *)value == 0) { | |
316 | sr_err("%s: LIMIT_MSEC can't be 0.", __func__); | |
317 | return SR_ERR; | |
318 | } | |
319 | devc->limit_msec = *(const uint64_t *)value; | |
320 | sr_dbg("LIMIT_MSEC = %" PRIu64, devc->limit_msec); | |
321 | break; | |
322 | case SR_CONF_LIMIT_SAMPLES: | |
323 | if (*(const uint64_t *)value < MIN_NUM_SAMPLES) { | |
324 | sr_err("%s: LIMIT_SAMPLES too small.", __func__); | |
325 | return SR_ERR; | |
326 | } | |
327 | devc->limit_samples = *(const uint64_t *)value; | |
328 | sr_dbg("LIMIT_SAMPLES = %" PRIu64, devc->limit_samples); | |
329 | break; | |
330 | default: | |
331 | /* Unknown capability, return SR_ERR. */ | |
332 | sr_err("%s: Unknown capability: %d.", __func__, id); | |
333 | return SR_ERR; | |
334 | break; | |
335 | } | |
336 | ||
337 | return SR_OK; | |
338 | } | |
339 | ||
340 | static int config_list(int key, const void **data, const struct sr_dev_inst *sdi) | |
341 | { | |
342 | ||
343 | (void)sdi; | |
344 | ||
345 | switch (key) { | |
346 | case SR_CONF_DEVICE_OPTIONS: | |
347 | *data = hwcaps; | |
348 | break; | |
349 | case SR_CONF_SAMPLERATE: | |
350 | fill_supported_samplerates_if_needed(); | |
351 | *data = &samplerates; | |
352 | break; | |
353 | case SR_CONF_TRIGGER_TYPE: | |
354 | *data = (char *)TRIGGER_TYPE; | |
355 | break; | |
356 | default: | |
357 | return SR_ERR_ARG; | |
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 | hw_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 | hw_dev_acquisition_stop(sdi, sdi); | |
407 | ||
408 | return TRUE; | |
409 | } | |
410 | ||
411 | static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, | |
412 | void *cb_data) | |
413 | { | |
414 | struct dev_context *devc; | |
415 | struct sr_datafeed_packet packet; | |
416 | struct sr_datafeed_header header; | |
417 | uint8_t buf[4]; | |
418 | int bytes_written; | |
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 | sr_dbg("Starting acquisition."); | |
442 | ||
443 | /* Fill acquisition parameters into buf[]. */ | |
444 | buf[0] = devc->divcount; | |
445 | buf[1] = 0xff; /* This byte must always be 0xff. */ | |
446 | buf[2] = devc->trigger_pattern; | |
447 | buf[3] = devc->trigger_mask; | |
448 | ||
449 | /* Start acquisition. */ | |
450 | bytes_written = la8_write(devc, buf, 4); | |
451 | ||
452 | if (bytes_written < 0) { | |
453 | sr_err("Acquisition failed to start: %d.", bytes_written); | |
454 | return SR_ERR; | |
455 | } else if (bytes_written != 4) { | |
456 | sr_err("Acquisition failed to start: %d.", bytes_written); | |
457 | return SR_ERR; | |
458 | } | |
459 | ||
460 | sr_dbg("Acquisition started successfully."); | |
461 | ||
462 | devc->session_dev_id = cb_data; | |
463 | ||
464 | /* Send header packet to the session bus. */ | |
465 | sr_dbg("Sending SR_DF_HEADER."); | |
466 | packet.type = SR_DF_HEADER; | |
467 | packet.payload = &header; | |
468 | header.feed_version = 1; | |
469 | gettimeofday(&header.starttime, NULL); | |
470 | sr_session_send(devc->session_dev_id, &packet); | |
471 | ||
472 | /* Time when we should be done (for detecting trigger timeouts). */ | |
473 | devc->done = (devc->divcount + 1) * 0.08388608 + time(NULL) | |
474 | + devc->trigger_timeout; | |
475 | devc->block_counter = 0; | |
476 | devc->trigger_found = 0; | |
477 | ||
478 | /* Hook up a dummy handler to receive data from the LA8. */ | |
479 | sr_source_add(-1, G_IO_IN, 0, receive_data, (void *)sdi); | |
480 | ||
481 | return SR_OK; | |
482 | } | |
483 | ||
484 | static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) | |
485 | { | |
486 | struct sr_datafeed_packet packet; | |
487 | ||
488 | (void)sdi; | |
489 | ||
490 | sr_dbg("Stopping acquisition."); | |
491 | sr_source_remove(-1); | |
492 | ||
493 | /* Send end packet to the session bus. */ | |
494 | sr_dbg("Sending SR_DF_END."); | |
495 | packet.type = SR_DF_END; | |
496 | sr_session_send(cb_data, &packet); | |
497 | ||
498 | return SR_OK; | |
499 | } | |
500 | ||
501 | SR_PRIV struct sr_dev_driver chronovu_la8_driver_info = { | |
502 | .name = "chronovu-la8", | |
503 | .longname = "ChronoVu LA8", | |
504 | .api_version = 1, | |
505 | .init = hw_init, | |
506 | .cleanup = hw_cleanup, | |
507 | .scan = hw_scan, | |
508 | .dev_list = hw_dev_list, | |
509 | .dev_clear = clear_instances, | |
510 | .config_get = config_get, | |
511 | .config_set = config_set, | |
512 | .config_list = config_list, | |
513 | .dev_open = hw_dev_open, | |
514 | .dev_close = hw_dev_close, | |
515 | .dev_acquisition_start = hw_dev_acquisition_start, | |
516 | .dev_acquisition_stop = hw_dev_acquisition_stop, | |
517 | .priv = NULL, | |
518 | }; |