]>
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 | struct drv_context *drvc; | |
74 | ||
75 | if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) { | |
76 | sr_err("Driver context malloc failed."); | |
77 | return SR_ERR_MALLOC; | |
78 | } | |
79 | ||
80 | di->priv = drvc; | |
81 | ||
82 | return SR_OK; | |
83 | } | |
84 | ||
85 | static GSList *hw_scan(GSList *options) | |
86 | { | |
87 | struct sr_dev_inst *sdi; | |
88 | struct sr_probe *probe; | |
89 | struct drv_context *drvc; | |
90 | struct dev_context *devc; | |
91 | GSList *devices; | |
92 | unsigned int i; | |
93 | int ret; | |
94 | ||
95 | (void)options; | |
96 | ||
97 | drvc = di->priv; | |
98 | devices = NULL; | |
99 | ||
100 | /* Allocate memory for our private device context. */ | |
101 | if (!(devc = g_try_malloc(sizeof(struct dev_context)))) { | |
102 | sr_err("Device context malloc failed."); | |
103 | goto err_free_nothing; | |
104 | } | |
105 | ||
106 | /* Set some sane defaults. */ | |
107 | devc->ftdic = NULL; | |
108 | devc->cur_samplerate = SR_MHZ(100); /* 100MHz == max. samplerate */ | |
109 | devc->limit_msec = 0; | |
110 | devc->limit_samples = 0; | |
111 | devc->session_dev_id = NULL; | |
112 | memset(devc->mangled_buf, 0, BS); | |
113 | devc->final_buf = NULL; | |
114 | devc->trigger_pattern = 0x00; /* Value irrelevant, see trigger_mask. */ | |
115 | devc->trigger_mask = 0x00; /* All probes are "don't care". */ | |
116 | devc->trigger_timeout = 10; /* Default to 10s trigger timeout. */ | |
117 | devc->trigger_found = 0; | |
118 | devc->done = 0; | |
119 | devc->block_counter = 0; | |
120 | devc->divcount = 0; /* 10ns sample period == 100MHz samplerate */ | |
121 | devc->usb_pid = 0; | |
122 | ||
123 | /* Allocate memory where we'll store the de-mangled data. */ | |
124 | if (!(devc->final_buf = g_try_malloc(SDRAM_SIZE))) { | |
125 | sr_err("final_buf malloc failed."); | |
126 | goto err_free_devc; | |
127 | } | |
128 | ||
129 | /* Allocate memory for the FTDI context (ftdic) and initialize it. */ | |
130 | if (!(devc->ftdic = ftdi_new())) { | |
131 | sr_err("%s: ftdi_new failed.", __func__); | |
132 | goto err_free_final_buf; | |
133 | } | |
134 | ||
135 | /* Check for the device and temporarily open it. */ | |
136 | for (i = 0; i < ARRAY_SIZE(usb_pids); i++) { | |
137 | sr_dbg("Probing for VID/PID %04x:%04x.", USB_VENDOR_ID, | |
138 | usb_pids[i]); | |
139 | ret = ftdi_usb_open_desc(devc->ftdic, USB_VENDOR_ID, | |
140 | usb_pids[i], USB_DESCRIPTION, NULL); | |
141 | if (ret == 0) { | |
142 | sr_dbg("Found LA8 device (%04x:%04x).", | |
143 | USB_VENDOR_ID, usb_pids[i]); | |
144 | devc->usb_pid = usb_pids[i]; | |
145 | } | |
146 | } | |
147 | ||
148 | if (devc->usb_pid == 0) | |
149 | goto err_free_ftdic; | |
150 | ||
151 | /* Register the device with libsigrok. */ | |
152 | sdi = sr_dev_inst_new(0, SR_ST_INITIALIZING, | |
153 | USB_VENDOR_NAME, USB_MODEL_NAME, USB_MODEL_VERSION); | |
154 | if (!sdi) { | |
155 | sr_err("%s: sr_dev_inst_new failed.", __func__); | |
156 | goto err_close_ftdic; | |
157 | } | |
158 | sdi->driver = di; | |
159 | sdi->priv = devc; | |
160 | ||
161 | for (i = 0; probe_names[i]; i++) { | |
162 | if (!(probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE, | |
163 | probe_names[i]))) | |
164 | return NULL; | |
165 | sdi->probes = g_slist_append(sdi->probes, probe); | |
166 | } | |
167 | ||
168 | devices = g_slist_append(devices, sdi); | |
169 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
170 | ||
171 | sr_spew("Device init successful."); | |
172 | ||
173 | /* Close device. We'll reopen it again when we need it. */ | |
174 | (void) la8_close(devc); /* Log, but ignore errors. */ | |
175 | ||
176 | return devices; | |
177 | ||
178 | err_close_ftdic: | |
179 | (void) la8_close(devc); /* Log, but ignore errors. */ | |
180 | err_free_ftdic: | |
181 | free(devc->ftdic); /* NOT g_free()! */ | |
182 | err_free_final_buf: | |
183 | g_free(devc->final_buf); | |
184 | err_free_devc: | |
185 | g_free(devc); | |
186 | err_free_nothing: | |
187 | ||
188 | return NULL; | |
189 | } | |
190 | ||
191 | static GSList *hw_dev_list(void) | |
192 | { | |
193 | struct drv_context *drvc; | |
194 | ||
195 | drvc = di->priv; | |
196 | ||
197 | return drvc->instances; | |
198 | } | |
199 | ||
200 | static int hw_dev_open(struct sr_dev_inst *sdi) | |
201 | { | |
202 | struct dev_context *devc; | |
203 | int ret; | |
204 | ||
205 | if (!(devc = sdi->priv)) { | |
206 | sr_err("%s: sdi->priv was NULL.", __func__); | |
207 | return SR_ERR_BUG; | |
208 | } | |
209 | ||
210 | sr_dbg("Opening LA8 device (%04x:%04x).", USB_VENDOR_ID, | |
211 | devc->usb_pid); | |
212 | ||
213 | /* Open the device. */ | |
214 | if ((ret = ftdi_usb_open_desc(devc->ftdic, USB_VENDOR_ID, | |
215 | devc->usb_pid, USB_DESCRIPTION, NULL)) < 0) { | |
216 | sr_err("%s: ftdi_usb_open_desc: (%d) %s", | |
217 | __func__, ret, ftdi_get_error_string(devc->ftdic)); | |
218 | (void) la8_close_usb_reset_sequencer(devc); /* Ignore errors. */ | |
219 | return SR_ERR; | |
220 | } | |
221 | sr_dbg("Device opened successfully."); | |
222 | ||
223 | /* Purge RX/TX buffers in the FTDI chip. */ | |
224 | if ((ret = ftdi_usb_purge_buffers(devc->ftdic)) < 0) { | |
225 | sr_err("%s: ftdi_usb_purge_buffers: (%d) %s", | |
226 | __func__, ret, ftdi_get_error_string(devc->ftdic)); | |
227 | (void) la8_close_usb_reset_sequencer(devc); /* Ignore errors. */ | |
228 | goto err_dev_open_close_ftdic; | |
229 | } | |
230 | sr_dbg("FTDI buffers purged successfully."); | |
231 | ||
232 | /* Enable flow control in the FTDI chip. */ | |
233 | if ((ret = ftdi_setflowctrl(devc->ftdic, SIO_RTS_CTS_HS)) < 0) { | |
234 | sr_err("%s: ftdi_setflowcontrol: (%d) %s", | |
235 | __func__, ret, ftdi_get_error_string(devc->ftdic)); | |
236 | (void) la8_close_usb_reset_sequencer(devc); /* Ignore errors. */ | |
237 | goto err_dev_open_close_ftdic; | |
238 | } | |
239 | sr_dbg("FTDI flow control enabled successfully."); | |
240 | ||
241 | /* Wait 100ms. */ | |
242 | g_usleep(100 * 1000); | |
243 | ||
244 | sdi->status = SR_ST_ACTIVE; | |
245 | ||
246 | return SR_OK; | |
247 | ||
248 | err_dev_open_close_ftdic: | |
249 | (void) la8_close(devc); /* Log, but ignore errors. */ | |
250 | return SR_ERR; | |
251 | } | |
252 | ||
253 | static int hw_dev_close(struct sr_dev_inst *sdi) | |
254 | { | |
255 | struct dev_context *devc; | |
256 | ||
257 | if (!(devc = sdi->priv)) { | |
258 | sr_err("%s: sdi->priv was NULL.", __func__); | |
259 | return SR_ERR_BUG; | |
260 | } | |
261 | ||
262 | sr_dbg("Closing device."); | |
263 | ||
264 | if (sdi->status == SR_ST_ACTIVE) { | |
265 | sr_dbg("Status ACTIVE, closing device."); | |
266 | (void) la8_close_usb_reset_sequencer(devc); /* Ignore errors. */ | |
267 | } else { | |
268 | sr_spew("Status not ACTIVE, nothing to do."); | |
269 | } | |
270 | ||
271 | sdi->status = SR_ST_INACTIVE; | |
272 | ||
273 | sr_dbg("Freeing sample buffer."); | |
274 | g_free(devc->final_buf); | |
275 | ||
276 | return SR_OK; | |
277 | } | |
278 | ||
279 | static int hw_cleanup(void) | |
280 | { | |
281 | if (!di->priv) | |
282 | /* Can get called on an unused driver, doesn't matter. */ | |
283 | return SR_OK; | |
284 | ||
285 | clear_instances(); | |
286 | ||
287 | return SR_OK; | |
288 | } | |
289 | ||
290 | static int hw_info_get(int info_id, const void **data, | |
291 | const struct sr_dev_inst *sdi) | |
292 | { | |
293 | struct dev_context *devc; | |
294 | ||
295 | switch (info_id) { | |
296 | case SR_DI_HWCAPS: | |
297 | *data = hwcaps; | |
298 | break; | |
299 | case SR_DI_NUM_PROBES: | |
300 | *data = GINT_TO_POINTER(NUM_PROBES); | |
301 | sr_spew("%s: Returning number of probes: %d.", __func__, | |
302 | NUM_PROBES); | |
303 | break; | |
304 | case SR_DI_PROBE_NAMES: | |
305 | *data = probe_names; | |
306 | sr_spew("%s: Returning probenames.", __func__); | |
307 | break; | |
308 | case SR_DI_SAMPLERATES: | |
309 | fill_supported_samplerates_if_needed(); | |
310 | *data = &samplerates; | |
311 | sr_spew("%s: Returning samplerates.", __func__); | |
312 | break; | |
313 | case SR_DI_TRIGGER_TYPES: | |
314 | *data = (char *)TRIGGER_TYPES; | |
315 | sr_spew("%s: Returning trigger types: %s.", __func__, | |
316 | TRIGGER_TYPES); | |
317 | break; | |
318 | case SR_DI_CUR_SAMPLERATE: | |
319 | if (sdi) { | |
320 | devc = sdi->priv; | |
321 | *data = &devc->cur_samplerate; | |
322 | sr_spew("%s: Returning samplerate: %" PRIu64 "Hz.", | |
323 | __func__, devc->cur_samplerate); | |
324 | } else | |
325 | return SR_ERR; | |
326 | break; | |
327 | default: | |
328 | return SR_ERR_ARG; | |
329 | } | |
330 | ||
331 | return SR_OK; | |
332 | } | |
333 | ||
334 | static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap, | |
335 | const void *value) | |
336 | { | |
337 | struct dev_context *devc; | |
338 | ||
339 | if (!(devc = sdi->priv)) { | |
340 | sr_err("%s: sdi->priv was NULL.", __func__); | |
341 | return SR_ERR_BUG; | |
342 | } | |
343 | ||
344 | switch (hwcap) { | |
345 | case SR_HWCAP_SAMPLERATE: | |
346 | if (set_samplerate(sdi, *(const uint64_t *)value) == SR_ERR) { | |
347 | sr_err("%s: setting samplerate failed.", __func__); | |
348 | return SR_ERR; | |
349 | } | |
350 | sr_dbg("SAMPLERATE = %" PRIu64, devc->cur_samplerate); | |
351 | break; | |
352 | case SR_HWCAP_LIMIT_MSEC: | |
353 | if (*(const uint64_t *)value == 0) { | |
354 | sr_err("%s: LIMIT_MSEC can't be 0.", __func__); | |
355 | return SR_ERR; | |
356 | } | |
357 | devc->limit_msec = *(const uint64_t *)value; | |
358 | sr_dbg("LIMIT_MSEC = %" PRIu64, devc->limit_msec); | |
359 | break; | |
360 | case SR_HWCAP_LIMIT_SAMPLES: | |
361 | if (*(const uint64_t *)value < MIN_NUM_SAMPLES) { | |
362 | sr_err("%s: LIMIT_SAMPLES too small.", __func__); | |
363 | return SR_ERR; | |
364 | } | |
365 | devc->limit_samples = *(const uint64_t *)value; | |
366 | sr_dbg("LIMIT_SAMPLES = %" PRIu64, devc->limit_samples); | |
367 | break; | |
368 | default: | |
369 | /* Unknown capability, return SR_ERR. */ | |
370 | sr_err("%s: Unknown capability: %d.", __func__, hwcap); | |
371 | return SR_ERR; | |
372 | break; | |
373 | } | |
374 | ||
375 | return SR_OK; | |
376 | } | |
377 | ||
378 | static int receive_data(int fd, int revents, void *cb_data) | |
379 | { | |
380 | int i, ret; | |
381 | struct sr_dev_inst *sdi; | |
382 | struct dev_context *devc; | |
383 | ||
384 | (void)fd; | |
385 | (void)revents; | |
386 | ||
387 | if (!(sdi = cb_data)) { | |
388 | sr_err("%s: cb_data was NULL.", __func__); | |
389 | return FALSE; | |
390 | } | |
391 | ||
392 | if (!(devc = sdi->priv)) { | |
393 | sr_err("%s: sdi->priv was NULL.", __func__); | |
394 | return FALSE; | |
395 | } | |
396 | ||
397 | if (!devc->ftdic) { | |
398 | sr_err("%s: devc->ftdic was NULL.", __func__); | |
399 | return FALSE; | |
400 | } | |
401 | ||
402 | /* Get one block of data. */ | |
403 | if ((ret = la8_read_block(devc)) < 0) { | |
404 | sr_err("%s: la8_read_block error: %d.", __func__, ret); | |
405 | hw_dev_acquisition_stop(sdi, sdi); | |
406 | return FALSE; | |
407 | } | |
408 | ||
409 | /* We need to get exactly NUM_BLOCKS blocks (i.e. 8MB) of data. */ | |
410 | if (devc->block_counter != (NUM_BLOCKS - 1)) { | |
411 | devc->block_counter++; | |
412 | return TRUE; | |
413 | } | |
414 | ||
415 | sr_dbg("Sampling finished, sending data to session bus now."); | |
416 | ||
417 | /* All data was received and demangled, send it to the session bus. */ | |
418 | for (i = 0; i < NUM_BLOCKS; i++) | |
419 | send_block_to_session_bus(devc, i); | |
420 | ||
421 | hw_dev_acquisition_stop(sdi, sdi); | |
422 | ||
423 | return TRUE; | |
424 | } | |
425 | ||
426 | static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, | |
427 | void *cb_data) | |
428 | { | |
429 | struct dev_context *devc; | |
430 | struct sr_datafeed_packet packet; | |
431 | struct sr_datafeed_header header; | |
432 | struct sr_datafeed_meta_logic meta; | |
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 | sr_dbg("Starting acquisition."); | |
458 | ||
459 | /* Fill acquisition parameters into buf[]. */ | |
460 | buf[0] = devc->divcount; | |
461 | buf[1] = 0xff; /* This byte must always be 0xff. */ | |
462 | buf[2] = devc->trigger_pattern; | |
463 | buf[3] = devc->trigger_mask; | |
464 | ||
465 | /* Start acquisition. */ | |
466 | bytes_written = la8_write(devc, buf, 4); | |
467 | ||
468 | if (bytes_written < 0) { | |
469 | sr_err("Acquisition failed to start: %d.", bytes_written); | |
470 | return SR_ERR; | |
471 | } else if (bytes_written != 4) { | |
472 | sr_err("Acquisition failed to start: %d.", bytes_written); | |
473 | return SR_ERR; | |
474 | } | |
475 | ||
476 | sr_dbg("Acquisition started successfully."); | |
477 | ||
478 | devc->session_dev_id = cb_data; | |
479 | ||
480 | /* Send header packet to the session bus. */ | |
481 | sr_dbg("Sending SR_DF_HEADER."); | |
482 | packet.type = SR_DF_HEADER; | |
483 | packet.payload = &header; | |
484 | header.feed_version = 1; | |
485 | gettimeofday(&header.starttime, NULL); | |
486 | sr_session_send(devc->session_dev_id, &packet); | |
487 | ||
488 | /* Send metadata about the SR_DF_LOGIC packets to come. */ | |
489 | packet.type = SR_DF_META_LOGIC; | |
490 | packet.payload = &meta; | |
491 | meta.samplerate = devc->cur_samplerate; | |
492 | meta.num_probes = NUM_PROBES; | |
493 | sr_session_send(devc->session_dev_id, &packet); | |
494 | ||
495 | /* Time when we should be done (for detecting trigger timeouts). */ | |
496 | devc->done = (devc->divcount + 1) * 0.08388608 + time(NULL) | |
497 | + devc->trigger_timeout; | |
498 | devc->block_counter = 0; | |
499 | devc->trigger_found = 0; | |
500 | ||
501 | /* Hook up a dummy handler to receive data from the LA8. */ | |
502 | sr_source_add(-1, G_IO_IN, 0, receive_data, (void *)sdi); | |
503 | ||
504 | return SR_OK; | |
505 | } | |
506 | ||
507 | static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) | |
508 | { | |
509 | struct sr_datafeed_packet packet; | |
510 | ||
511 | (void)sdi; | |
512 | ||
513 | sr_dbg("Stopping acquisition."); | |
514 | sr_source_remove(-1); | |
515 | ||
516 | /* Send end packet to the session bus. */ | |
517 | sr_dbg("Sending SR_DF_END."); | |
518 | packet.type = SR_DF_END; | |
519 | sr_session_send(cb_data, &packet); | |
520 | ||
521 | return SR_OK; | |
522 | } | |
523 | ||
524 | SR_PRIV struct sr_dev_driver chronovu_la8_driver_info = { | |
525 | .name = "chronovu-la8", | |
526 | .longname = "ChronoVu LA8", | |
527 | .api_version = 1, | |
528 | .init = hw_init, | |
529 | .cleanup = hw_cleanup, | |
530 | .scan = hw_scan, | |
531 | .dev_list = hw_dev_list, | |
532 | .dev_clear = clear_instances, | |
533 | .dev_open = hw_dev_open, | |
534 | .dev_close = hw_dev_close, | |
535 | .info_get = hw_info_get, | |
536 | .dev_config_set = hw_dev_config_set, | |
537 | .dev_acquisition_start = hw_dev_acquisition_start, | |
538 | .dev_acquisition_stop = hw_dev_acquisition_stop, | |
539 | .priv = NULL, | |
540 | }; |