]>
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_la_driver_info; | |
24 | static struct sr_dev_driver *di = &chronovu_la_driver_info; | |
25 | ||
26 | static const uint32_t devopts[] = { | |
27 | SR_CONF_LOGIC_ANALYZER, | |
28 | SR_CONF_LIMIT_MSEC | SR_CONF_SET, | |
29 | SR_CONF_LIMIT_SAMPLES | SR_CONF_SET | SR_CONF_LIST, | |
30 | SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
31 | SR_CONF_TRIGGER_MATCH | SR_CONF_LIST, | |
32 | }; | |
33 | ||
34 | static const int32_t trigger_matches[] = { | |
35 | SR_TRIGGER_ZERO, | |
36 | SR_TRIGGER_ONE, | |
37 | SR_TRIGGER_RISING, | |
38 | SR_TRIGGER_FALLING, | |
39 | }; | |
40 | ||
41 | /* The ChronoVu LA8/LA16 can have multiple VID/PID pairs. */ | |
42 | static struct { | |
43 | uint16_t vid; | |
44 | uint16_t pid; | |
45 | int model; | |
46 | const char *iproduct; | |
47 | } vid_pid[] = { | |
48 | { 0x0403, 0x6001, CHRONOVU_LA8, "ChronoVu LA8" }, | |
49 | { 0x0403, 0x8867, CHRONOVU_LA8, "ChronoVu LA8" }, | |
50 | { 0x0403, 0x6001, CHRONOVU_LA16, "ChronoVu LA16" }, | |
51 | { 0x0403, 0x8867, CHRONOVU_LA16, "ChronoVu LA16" }, | |
52 | }; | |
53 | ||
54 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data); | |
55 | ||
56 | static void clear_helper(void *priv) | |
57 | { | |
58 | struct dev_context *devc; | |
59 | ||
60 | devc = priv; | |
61 | ||
62 | ftdi_free(devc->ftdic); | |
63 | g_free(devc->final_buf); | |
64 | } | |
65 | ||
66 | static int dev_clear(void) | |
67 | { | |
68 | return std_dev_clear(di, clear_helper); | |
69 | } | |
70 | ||
71 | static int init(struct sr_context *sr_ctx) | |
72 | { | |
73 | return std_init(sr_ctx, di, LOG_PREFIX); | |
74 | } | |
75 | ||
76 | static int add_device(int idx, int model, GSList **devices) | |
77 | { | |
78 | int ret; | |
79 | unsigned int i; | |
80 | struct sr_dev_inst *sdi; | |
81 | struct drv_context *drvc; | |
82 | struct dev_context *devc; | |
83 | struct sr_channel *ch; | |
84 | ||
85 | ret = SR_OK; | |
86 | ||
87 | drvc = di->priv; | |
88 | ||
89 | /* Allocate memory for our private device context. */ | |
90 | devc = g_malloc0(sizeof(struct dev_context)); | |
91 | ||
92 | /* Set some sane defaults. */ | |
93 | devc->prof = &cv_profiles[model]; | |
94 | devc->ftdic = NULL; /* Will be set in the open() API call. */ | |
95 | devc->cur_samplerate = 0; /* Set later (different for LA8/LA16). */ | |
96 | devc->limit_msec = 0; | |
97 | devc->limit_samples = 0; | |
98 | devc->cb_data = NULL; | |
99 | memset(devc->mangled_buf, 0, BS); | |
100 | devc->final_buf = NULL; | |
101 | devc->trigger_pattern = 0x0000; /* Irrelevant, see trigger_mask. */ | |
102 | devc->trigger_mask = 0x0000; /* All channels: "don't care". */ | |
103 | devc->trigger_edgemask = 0x0000; /* All channels: "state triggered". */ | |
104 | devc->trigger_found = 0; | |
105 | devc->done = 0; | |
106 | devc->block_counter = 0; | |
107 | devc->divcount = 0; | |
108 | devc->usb_vid = vid_pid[idx].vid; | |
109 | devc->usb_pid = vid_pid[idx].pid; | |
110 | memset(devc->samplerates, 0, sizeof(uint64_t) * 255); | |
111 | ||
112 | /* Allocate memory where we'll store the de-mangled data. */ | |
113 | if (!(devc->final_buf = g_try_malloc(SDRAM_SIZE))) { | |
114 | sr_err("Failed to allocate memory for sample buffer."); | |
115 | ret = SR_ERR_MALLOC; | |
116 | goto err_free_devc; | |
117 | } | |
118 | ||
119 | /* We now know the device, set its max. samplerate as default. */ | |
120 | devc->cur_samplerate = devc->prof->max_samplerate; | |
121 | ||
122 | /* Register the device with libsigrok. */ | |
123 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
124 | sdi->status = SR_ST_INITIALIZING; | |
125 | sdi->vendor = g_strdup("ChronoVu"); | |
126 | sdi->model = g_strdup(devc->prof->modelname); | |
127 | sdi->driver = di; | |
128 | sdi->priv = devc; | |
129 | ||
130 | for (i = 0; i < devc->prof->num_channels; i++) { | |
131 | ch = sr_channel_new(i, SR_CHANNEL_LOGIC, TRUE, | |
132 | cv_channel_names[i]); | |
133 | sdi->channels = g_slist_append(sdi->channels, ch); | |
134 | } | |
135 | ||
136 | *devices = g_slist_append(*devices, sdi); | |
137 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
138 | ||
139 | if (ret == SR_OK) | |
140 | return SR_OK; | |
141 | ||
142 | err_free_devc: | |
143 | g_free(devc); | |
144 | ||
145 | return ret; | |
146 | } | |
147 | ||
148 | static GSList *scan(GSList *options) | |
149 | { | |
150 | int ret; | |
151 | unsigned int i; | |
152 | GSList *devices; | |
153 | struct ftdi_context *ftdic; | |
154 | ||
155 | (void)options; | |
156 | ||
157 | devices = NULL; | |
158 | ||
159 | /* Allocate memory for the FTDI context and initialize it. */ | |
160 | if (!(ftdic = ftdi_new())) { | |
161 | sr_err("Failed to initialize libftdi."); | |
162 | return NULL; | |
163 | } | |
164 | ||
165 | /* Check for LA8 and/or LA16 devices with various VID/PIDs. */ | |
166 | for (i = 0; i < ARRAY_SIZE(vid_pid); i++) { | |
167 | ret = ftdi_usb_open_desc(ftdic, vid_pid[i].vid, | |
168 | vid_pid[i].pid, vid_pid[i].iproduct, NULL); | |
169 | /* Show errors other than "device not found". */ | |
170 | if (ret < 0 && ret != -3) | |
171 | sr_dbg("Error finding/opening device (%d): %s.", | |
172 | ret, ftdi_get_error_string(ftdic)); | |
173 | if (ret < 0) | |
174 | continue; /* No device found, or not usable. */ | |
175 | ||
176 | sr_dbg("Found %s device (%04x:%04x).", | |
177 | vid_pid[i].iproduct, vid_pid[i].vid, vid_pid[i].pid); | |
178 | ||
179 | if ((ret = add_device(i, vid_pid[i].model, &devices)) < 0) | |
180 | sr_dbg("Failed to add device: %d.", ret); | |
181 | ||
182 | if ((ret = ftdi_usb_close(ftdic)) < 0) | |
183 | sr_dbg("Failed to close FTDI device (%d): %s.", | |
184 | ret, ftdi_get_error_string(ftdic)); | |
185 | } | |
186 | ||
187 | /* Close USB device, deinitialize and free the FTDI context. */ | |
188 | ftdi_free(ftdic); | |
189 | ftdic = NULL; | |
190 | ||
191 | return devices; | |
192 | } | |
193 | ||
194 | static GSList *dev_list(void) | |
195 | { | |
196 | return ((struct drv_context *)(di->priv))->instances; | |
197 | } | |
198 | ||
199 | static int dev_open(struct sr_dev_inst *sdi) | |
200 | { | |
201 | struct dev_context *devc; | |
202 | int ret; | |
203 | ||
204 | if (!(devc = sdi->priv)) | |
205 | return SR_ERR_BUG; | |
206 | ||
207 | /* Allocate memory for the FTDI context and initialize it. */ | |
208 | if (!(devc->ftdic = ftdi_new())) { | |
209 | sr_err("Failed to initialize libftdi."); | |
210 | return SR_ERR; | |
211 | } | |
212 | ||
213 | sr_dbg("Opening %s device (%04x:%04x).", devc->prof->modelname, | |
214 | devc->usb_vid, devc->usb_pid); | |
215 | ||
216 | /* Open the device. */ | |
217 | if ((ret = ftdi_usb_open_desc(devc->ftdic, devc->usb_vid, | |
218 | devc->usb_pid, devc->prof->iproduct, NULL)) < 0) { | |
219 | sr_err("Failed to open FTDI device (%d): %s.", | |
220 | ret, ftdi_get_error_string(devc->ftdic)); | |
221 | goto err_ftdi_free; | |
222 | } | |
223 | sr_dbg("Device opened successfully."); | |
224 | ||
225 | /* Purge RX/TX buffers in the FTDI chip. */ | |
226 | if ((ret = ftdi_usb_purge_buffers(devc->ftdic)) < 0) { | |
227 | sr_err("Failed to purge FTDI buffers (%d): %s.", | |
228 | ret, ftdi_get_error_string(devc->ftdic)); | |
229 | goto err_ftdi_free; | |
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("Failed to enable FTDI flow control (%d): %s.", | |
236 | ret, ftdi_get_error_string(devc->ftdic)); | |
237 | goto err_ftdi_free; | |
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 | if (ret == SR_OK) | |
247 | return SR_OK; | |
248 | ||
249 | err_ftdi_free: | |
250 | ftdi_free(devc->ftdic); /* Close device (if open), free FTDI context. */ | |
251 | devc->ftdic = NULL; | |
252 | return ret; | |
253 | } | |
254 | ||
255 | static int dev_close(struct sr_dev_inst *sdi) | |
256 | { | |
257 | int ret; | |
258 | struct dev_context *devc; | |
259 | ||
260 | if (sdi->status != SR_ST_ACTIVE) | |
261 | return SR_OK; | |
262 | ||
263 | devc = sdi->priv; | |
264 | ||
265 | if (devc->ftdic && (ret = ftdi_usb_close(devc->ftdic)) < 0) | |
266 | sr_err("Failed to close FTDI device (%d): %s.", | |
267 | ret, ftdi_get_error_string(devc->ftdic)); | |
268 | sdi->status = SR_ST_INACTIVE; | |
269 | ||
270 | return SR_OK; | |
271 | } | |
272 | ||
273 | static int cleanup(void) | |
274 | { | |
275 | return dev_clear(); | |
276 | } | |
277 | ||
278 | static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, | |
279 | const struct sr_channel_group *cg) | |
280 | { | |
281 | struct dev_context *devc; | |
282 | ||
283 | (void)cg; | |
284 | ||
285 | switch (key) { | |
286 | case SR_CONF_SAMPLERATE: | |
287 | if (!sdi || !(devc = sdi->priv)) | |
288 | return SR_ERR_BUG; | |
289 | *data = g_variant_new_uint64(devc->cur_samplerate); | |
290 | break; | |
291 | default: | |
292 | return SR_ERR_NA; | |
293 | } | |
294 | ||
295 | return SR_OK; | |
296 | } | |
297 | ||
298 | static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, | |
299 | const struct sr_channel_group *cg) | |
300 | { | |
301 | struct dev_context *devc; | |
302 | ||
303 | (void)cg; | |
304 | ||
305 | if (sdi->status != SR_ST_ACTIVE) | |
306 | return SR_ERR_DEV_CLOSED; | |
307 | ||
308 | if (!(devc = sdi->priv)) | |
309 | return SR_ERR_BUG; | |
310 | ||
311 | switch (key) { | |
312 | case SR_CONF_SAMPLERATE: | |
313 | if (cv_set_samplerate(sdi, g_variant_get_uint64(data)) < 0) | |
314 | return SR_ERR; | |
315 | break; | |
316 | case SR_CONF_LIMIT_MSEC: | |
317 | if (g_variant_get_uint64(data) == 0) | |
318 | return SR_ERR_ARG; | |
319 | devc->limit_msec = g_variant_get_uint64(data); | |
320 | break; | |
321 | case SR_CONF_LIMIT_SAMPLES: | |
322 | if (g_variant_get_uint64(data) == 0) | |
323 | return SR_ERR_ARG; | |
324 | devc->limit_samples = g_variant_get_uint64(data); | |
325 | break; | |
326 | default: | |
327 | return SR_ERR_NA; | |
328 | } | |
329 | ||
330 | return SR_OK; | |
331 | } | |
332 | ||
333 | static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, | |
334 | const struct sr_channel_group *cg) | |
335 | { | |
336 | GVariant *gvar, *grange[2]; | |
337 | GVariantBuilder gvb; | |
338 | struct dev_context *devc; | |
339 | ||
340 | (void)cg; | |
341 | ||
342 | switch (key) { | |
343 | case SR_CONF_DEVICE_OPTIONS: | |
344 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
345 | devopts, ARRAY_SIZE(devopts), sizeof(uint32_t)); | |
346 | break; | |
347 | case SR_CONF_SAMPLERATE: | |
348 | if (!sdi || !sdi->priv || !(devc = sdi->priv)) | |
349 | return SR_ERR_BUG; | |
350 | cv_fill_samplerates_if_needed(sdi); | |
351 | g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}")); | |
352 | gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"), | |
353 | devc->samplerates, | |
354 | ARRAY_SIZE(devc->samplerates), | |
355 | sizeof(uint64_t)); | |
356 | g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar); | |
357 | *data = g_variant_builder_end(&gvb); | |
358 | break; | |
359 | case SR_CONF_LIMIT_SAMPLES: | |
360 | if (!sdi || !sdi->priv || !(devc = sdi->priv) || !devc->prof) | |
361 | return SR_ERR_BUG; | |
362 | grange[0] = g_variant_new_uint64(0); | |
363 | if (devc->prof->model == CHRONOVU_LA8) | |
364 | grange[1] = g_variant_new_uint64(MAX_NUM_SAMPLES); | |
365 | else | |
366 | grange[1] = g_variant_new_uint64(MAX_NUM_SAMPLES / 2); | |
367 | *data = g_variant_new_tuple(grange, 2); | |
368 | break; | |
369 | case SR_CONF_TRIGGER_MATCH: | |
370 | if (!sdi || !sdi->priv || !(devc = sdi->priv) || !devc->prof) | |
371 | return SR_ERR_BUG; | |
372 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
373 | trigger_matches, devc->prof->num_trigger_matches, | |
374 | sizeof(int32_t)); | |
375 | break; | |
376 | default: | |
377 | return SR_ERR_NA; | |
378 | } | |
379 | ||
380 | return SR_OK; | |
381 | } | |
382 | ||
383 | static int receive_data(int fd, int revents, void *cb_data) | |
384 | { | |
385 | int i, ret; | |
386 | struct sr_dev_inst *sdi; | |
387 | struct dev_context *devc; | |
388 | ||
389 | (void)fd; | |
390 | (void)revents; | |
391 | ||
392 | if (!(sdi = cb_data)) { | |
393 | sr_err("cb_data was NULL."); | |
394 | return FALSE; | |
395 | } | |
396 | ||
397 | if (!(devc = sdi->priv)) { | |
398 | sr_err("sdi->priv was NULL."); | |
399 | return FALSE; | |
400 | } | |
401 | ||
402 | if (!devc->ftdic) { | |
403 | sr_err("devc->ftdic was NULL."); | |
404 | return FALSE; | |
405 | } | |
406 | ||
407 | /* Get one block of data. */ | |
408 | if ((ret = cv_read_block(devc)) < 0) { | |
409 | sr_err("Failed to read data block: %d.", ret); | |
410 | dev_acquisition_stop(sdi, sdi); | |
411 | return FALSE; | |
412 | } | |
413 | ||
414 | /* We need to get exactly NUM_BLOCKS blocks (i.e. 8MB) of data. */ | |
415 | if (devc->block_counter != (NUM_BLOCKS - 1)) { | |
416 | devc->block_counter++; | |
417 | return TRUE; | |
418 | } | |
419 | ||
420 | sr_dbg("Sampling finished, sending data to session bus now."); | |
421 | ||
422 | /* | |
423 | * All data was received and demangled, send it to the session bus. | |
424 | * | |
425 | * Note: Due to the method how data is spread across the 8MByte of | |
426 | * SDRAM, we can _not_ send it to the session bus in a streaming | |
427 | * manner while we receive it. We have to receive and de-mangle the | |
428 | * full 8MByte first, only then the whole buffer contains valid data. | |
429 | */ | |
430 | for (i = 0; i < NUM_BLOCKS; i++) | |
431 | cv_send_block_to_session_bus(devc, i); | |
432 | ||
433 | dev_acquisition_stop(sdi, sdi); | |
434 | ||
435 | return TRUE; | |
436 | } | |
437 | ||
438 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) | |
439 | { | |
440 | struct dev_context *devc; | |
441 | uint8_t buf[8]; | |
442 | int bytes_to_write, bytes_written; | |
443 | ||
444 | if (sdi->status != SR_ST_ACTIVE) | |
445 | return SR_ERR_DEV_CLOSED; | |
446 | ||
447 | if (!(devc = sdi->priv)) { | |
448 | sr_err("sdi->priv was NULL."); | |
449 | return SR_ERR_BUG; | |
450 | } | |
451 | ||
452 | if (!devc->ftdic) { | |
453 | sr_err("devc->ftdic was NULL."); | |
454 | return SR_ERR_BUG; | |
455 | } | |
456 | ||
457 | devc->divcount = cv_samplerate_to_divcount(sdi, devc->cur_samplerate); | |
458 | if (devc->divcount == 0xff) { | |
459 | sr_err("Invalid divcount/samplerate."); | |
460 | return SR_ERR; | |
461 | } | |
462 | ||
463 | if (cv_convert_trigger(sdi) != SR_OK) { | |
464 | sr_err("Failed to configure trigger."); | |
465 | return SR_ERR; | |
466 | } | |
467 | ||
468 | /* Fill acquisition parameters into buf[]. */ | |
469 | if (devc->prof->model == CHRONOVU_LA8) { | |
470 | buf[0] = devc->divcount; | |
471 | buf[1] = 0xff; /* This byte must always be 0xff. */ | |
472 | buf[2] = devc->trigger_pattern & 0xff; | |
473 | buf[3] = devc->trigger_mask & 0xff; | |
474 | bytes_to_write = 4; | |
475 | } else { | |
476 | buf[0] = devc->divcount; | |
477 | buf[1] = 0xff; /* This byte must always be 0xff. */ | |
478 | buf[2] = (devc->trigger_pattern & 0xff00) >> 8; /* LSB */ | |
479 | buf[3] = (devc->trigger_pattern & 0x00ff) >> 0; /* MSB */ | |
480 | buf[4] = (devc->trigger_mask & 0xff00) >> 8; /* LSB */ | |
481 | buf[5] = (devc->trigger_mask & 0x00ff) >> 0; /* MSB */ | |
482 | buf[6] = (devc->trigger_edgemask & 0xff00) >> 8; /* LSB */ | |
483 | buf[7] = (devc->trigger_edgemask & 0x00ff) >> 0; /* MSB */ | |
484 | bytes_to_write = 8; | |
485 | } | |
486 | ||
487 | /* Start acquisition. */ | |
488 | bytes_written = cv_write(devc, buf, bytes_to_write); | |
489 | ||
490 | if (bytes_written < 0 || bytes_written != bytes_to_write) { | |
491 | sr_err("Acquisition failed to start."); | |
492 | return SR_ERR; | |
493 | } | |
494 | ||
495 | sr_dbg("Hardware acquisition started successfully."); | |
496 | ||
497 | devc->cb_data = cb_data; | |
498 | ||
499 | /* Send header packet to the session bus. */ | |
500 | std_session_send_df_header(sdi, LOG_PREFIX); | |
501 | ||
502 | /* Time when we should be done (for detecting trigger timeouts). */ | |
503 | devc->done = (devc->divcount + 1) * devc->prof->trigger_constant + | |
504 | g_get_monotonic_time() + (10 * G_TIME_SPAN_SECOND); | |
505 | devc->block_counter = 0; | |
506 | devc->trigger_found = 0; | |
507 | ||
508 | /* Hook up a dummy handler to receive data from the device. */ | |
509 | sr_session_source_add(sdi->session, -1, G_IO_IN, 0, receive_data, (void *)sdi); | |
510 | ||
511 | return SR_OK; | |
512 | } | |
513 | ||
514 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) | |
515 | { | |
516 | struct sr_datafeed_packet packet; | |
517 | ||
518 | (void)cb_data; | |
519 | ||
520 | sr_dbg("Stopping acquisition."); | |
521 | sr_session_source_remove(sdi->session, -1); | |
522 | ||
523 | /* Send end packet to the session bus. */ | |
524 | sr_dbg("Sending SR_DF_END."); | |
525 | packet.type = SR_DF_END; | |
526 | sr_session_send(sdi, &packet); | |
527 | ||
528 | return SR_OK; | |
529 | } | |
530 | ||
531 | SR_PRIV struct sr_dev_driver chronovu_la_driver_info = { | |
532 | .name = "chronovu-la", | |
533 | .longname = "ChronoVu LA8/LA16", | |
534 | .api_version = 1, | |
535 | .init = init, | |
536 | .cleanup = cleanup, | |
537 | .scan = scan, | |
538 | .dev_list = dev_list, | |
539 | .dev_clear = dev_clear, | |
540 | .config_get = config_get, | |
541 | .config_set = config_set, | |
542 | .config_list = config_list, | |
543 | .dev_open = dev_open, | |
544 | .dev_close = dev_close, | |
545 | .dev_acquisition_start = dev_acquisition_start, | |
546 | .dev_acquisition_stop = dev_acquisition_stop, | |
547 | .priv = NULL, | |
548 | }; |