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