]>
Commit | Line | Data |
---|---|---|
f4314d7e UH |
1 | /* |
2 | * This file is part of the sigrok project. | |
3 | * | |
4 | * Copyright (C) 2011 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 <sigrok.h> | |
23 | ||
24 | #define USB_VENDOR_ID 0x0403 | |
25 | #define USB_PRODUCT_ID 0x6001 | |
26 | #define USB_DESCRIPTION "ChronoVu LA8" | |
27 | #define USB_VENDOR_NAME "ChronoVu" | |
28 | #define USB_MODEL_NAME "LA8" | |
29 | #define USB_MODEL_VERSION "" | |
30 | ||
31 | #define NUM_PROBES 8 | |
32 | #define TRIGGER_TYPES "01" | |
33 | #define SDRAM_SIZE (8 * 1024 * 1024) | |
34 | #define MIN_NUM_SAMPLES 1 | |
35 | ||
36 | static GSList *device_instances = NULL; | |
37 | ||
38 | struct la8 { | |
39 | /** FTDI device context (used by libftdi). */ | |
40 | struct ftdi_context *ftdic; | |
41 | ||
42 | /** The currently configured samplerate of the device. */ | |
43 | uint64_t cur_samplerate; | |
44 | ||
45 | /** The current sampling limit (in ms). */ | |
46 | uint64_t limit_msec; | |
47 | ||
48 | /** The current sampling limit (in number of samples). */ | |
49 | uint64_t limit_samples; | |
50 | ||
51 | /** The number of probes. */ | |
52 | int num_probes; | |
53 | ||
54 | /** TODO */ | |
55 | gpointer session_id; | |
56 | ||
57 | /** | |
58 | * An 8MB buffer containing the (mangled) samples from the device. | |
59 | * Format: Pretty mangled-up (due to hardware reasons), see code. | |
60 | */ | |
61 | uint8_t *mangled_buf; | |
62 | ||
63 | /** | |
64 | * An 8MB buffer where we'll store the de-mangled samples. | |
65 | * Format: Each sample is 1 byte, MSB is channel 7, LSB is channel 0. | |
66 | */ | |
67 | uint8_t *final_buf; | |
68 | ||
69 | /** | |
70 | * Trigger pattern (MSB = channel 7, LSB = channel 0). | |
71 | * A 1 bit matches a high signal, 0 matches a low signal on a probe. | |
72 | * Only low/high triggers (but not e.g. rising/falling) are supported. | |
73 | */ | |
74 | uint8_t trigger_pattern; | |
75 | ||
76 | /** | |
77 | * Trigger mask (MSB = channel 7, LSB = channel 0). | |
78 | * A 1 bit means "must match trigger_pattern", 0 means "don't care". | |
79 | */ | |
80 | uint8_t trigger_mask; | |
81 | ||
82 | /** Time (in seconds) before the trigger times out. */ | |
83 | uint64_t trigger_timeout; | |
84 | ||
85 | /** TODO */ | |
86 | time_t done; | |
87 | ||
88 | /** Counter/index for the data block (0..2047) to be read. */ | |
89 | int block_counter; | |
90 | ||
91 | /** The divcount value (determines the sample period) for the LA8. */ | |
92 | uint8_t divcount; | |
93 | }; | |
94 | ||
95 | /* This will be initialized via hw_get_device_info()/SR_DI_SAMPLERATES. */ | |
96 | static uint64_t supported_samplerates[255 + 1] = { 0 }; | |
97 | ||
98 | /* | |
99 | * Min: 1 sample per 0.01us -> sample time is 0.084s, samplerate 100MHz | |
100 | * Max: 1 sample per 2.55us -> sample time is 21.391s, samplerate 392.15kHz | |
101 | */ | |
102 | static struct sr_samplerates samplerates = { | |
103 | .low = 0, | |
104 | .high = 0, | |
105 | .step = 0, | |
106 | .list = supported_samplerates, | |
107 | }; | |
108 | ||
109 | /* Note: Continuous sampling is not supported by the hardware. */ | |
110 | static int capabilities[] = { | |
111 | SR_HWCAP_LOGIC_ANALYZER, | |
112 | SR_HWCAP_SAMPLERATE, | |
113 | SR_HWCAP_LIMIT_MSEC, /* TODO: Not yet implemented. */ | |
114 | SR_HWCAP_LIMIT_SAMPLES, /* TODO: Not yet implemented. */ | |
115 | 0, | |
116 | }; | |
117 | ||
118 | /* Function prototypes. */ | |
119 | static int la8_close_usb_reset_sequencer(struct la8 *la8); | |
120 | static void hw_stop_acquisition(int device_index, gpointer session_device_id); | |
121 | static int la8_reset(struct la8 *la8); | |
122 | ||
123 | static void fill_supported_samplerates_if_needed(void) | |
124 | { | |
125 | int i; | |
126 | ||
127 | /* Do nothing if supported_samplerates[] is already filled. */ | |
128 | if (supported_samplerates[0] != 0) | |
129 | return; | |
130 | ||
131 | /* Fill supported_samplerates[] with the proper values. */ | |
132 | for (i = 0; i < 255; i++) | |
133 | supported_samplerates[254 - i] = SR_MHZ(100) / (i + 1); | |
134 | supported_samplerates[255] = 0; | |
135 | } | |
136 | ||
137 | /** | |
138 | * Check if the given samplerate is supported by the LA8 hardware. | |
139 | * | |
140 | * @param samplerate The samplerate (in Hz) to check. | |
141 | * @return 1 if the samplerate is supported/valid, 0 otherwise. | |
142 | */ | |
143 | static int is_valid_samplerate(uint64_t samplerate) | |
144 | { | |
145 | int i; | |
146 | ||
147 | fill_supported_samplerates_if_needed(); | |
148 | ||
149 | for (i = 0; i < 255; i++) { | |
150 | if (supported_samplerates[i] == samplerate) | |
151 | return 1; | |
152 | } | |
153 | ||
154 | g_warning("la8: %s: invalid samplerate (%" PRIu64 "Hz)", | |
155 | __func__, samplerate); | |
156 | ||
157 | return 0; | |
158 | } | |
159 | ||
160 | /** | |
161 | * Convert a samplerate (in Hz) to the 'divcount' value the LA8 wants. | |
162 | * | |
163 | * LA8 hardware: sample period = (divcount + 1) * 10ns. | |
164 | * Min. value for divcount: 0x00 (10ns sample period, 100MHz samplerate). | |
165 | * Max. value for divcount: 0xfe (2550ns sample period, 392.15kHz samplerate). | |
166 | * | |
167 | * @param samplerate The samplerate in Hz. | |
168 | * @return The divcount value as needed by the hardware, or 0xff upon errors. | |
169 | */ | |
170 | static uint8_t samplerate_to_divcount(uint64_t samplerate) | |
171 | { | |
172 | if (samplerate == 0) { | |
173 | g_warning("la8: %s: samplerate was 0", __func__); | |
174 | return 0xff; | |
175 | } | |
176 | ||
177 | if (!is_valid_samplerate(samplerate)) { | |
178 | g_warning("la8: %s: can't get divcount, samplerate invalid", | |
179 | __func__); | |
180 | return 0xff; | |
181 | } | |
182 | ||
183 | return (SR_MHZ(100) / samplerate) - 1; | |
184 | } | |
185 | ||
186 | /** | |
187 | * Write data of a certain length to the LA8's FTDI device. | |
188 | * | |
189 | * @param la8 The LA8 struct containing private per-device-instance data. | |
190 | * @param buf The buffer containing the data to write. | |
191 | * @param size The number of bytes to write. | |
192 | * @return The number of bytes written, or a negative value upon errors. | |
193 | */ | |
194 | static int la8_write(struct la8 *la8, uint8_t *buf, int size) | |
195 | { | |
196 | int bytes_written; | |
197 | ||
198 | if (!la8) { | |
199 | g_warning("la8: %s: la8 was NULL", __func__); | |
8703f512 | 200 | return SR_ERR_ARG; |
f4314d7e UH |
201 | } |
202 | ||
203 | if (!la8->ftdic) { | |
204 | g_warning("la8: %s: la8->ftdic was NULL", __func__); | |
8703f512 | 205 | return SR_ERR_ARG; |
f4314d7e UH |
206 | } |
207 | ||
208 | if (!buf) { | |
209 | g_warning("la8: %s: buf was NULL", __func__); | |
8703f512 | 210 | return SR_ERR_ARG; |
f4314d7e UH |
211 | } |
212 | ||
213 | if (size < 0) { | |
214 | g_warning("la8: %s: size was < 0", __func__); | |
8703f512 | 215 | return SR_ERR_ARG; |
f4314d7e UH |
216 | } |
217 | ||
218 | bytes_written = ftdi_write_data(la8->ftdic, buf, size); | |
219 | ||
220 | if (bytes_written < 0) { | |
221 | g_warning("la8: %s: ftdi_write_data: (%d) %s", __func__, | |
222 | bytes_written, ftdi_get_error_string(la8->ftdic)); | |
223 | (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */ | |
224 | } else if (bytes_written != size) { | |
225 | g_warning("la8: %s: bytes to write: %d, bytes written: %d", | |
226 | __func__, size, bytes_written); | |
227 | (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */ | |
228 | } | |
229 | ||
230 | return bytes_written; | |
231 | } | |
232 | ||
233 | /** | |
234 | * Read a certain amount of bytes from the LA8's FTDI device. | |
235 | * | |
236 | * @param la8 The LA8 struct containing private per-device-instance data. | |
237 | * @param buf The buffer where the received data will be stored. | |
238 | * @param size The number of bytes to read. | |
239 | * @return The number of bytes read, or a negative value upon errors. | |
240 | */ | |
241 | static int la8_read(struct la8 *la8, uint8_t *buf, int size) | |
242 | { | |
243 | int bytes_read; | |
244 | ||
245 | if (!la8) { | |
246 | g_warning("la8: %s: la8 was NULL", __func__); | |
8703f512 | 247 | return SR_ERR_ARG; |
f4314d7e UH |
248 | } |
249 | ||
250 | if (!la8->ftdic) { | |
251 | g_warning("la8: %s: la8->ftdic was NULL", __func__); | |
8703f512 | 252 | return SR_ERR_ARG; |
f4314d7e UH |
253 | } |
254 | ||
255 | if (!buf) { | |
256 | g_warning("la8: %s: buf was NULL", __func__); | |
8703f512 | 257 | return SR_ERR_ARG; |
f4314d7e UH |
258 | } |
259 | ||
260 | if (size <= 0) { | |
261 | g_warning("la8: %s: size was <= 0", __func__); | |
8703f512 | 262 | return SR_ERR_ARG; |
f4314d7e UH |
263 | } |
264 | ||
265 | bytes_read = ftdi_read_data(la8->ftdic, buf, size); | |
266 | ||
267 | if (bytes_read < 0) { | |
268 | g_warning("la8: %s: ftdi_read_data: (%d) %s", __func__, | |
269 | bytes_read, ftdi_get_error_string(la8->ftdic)); | |
270 | } else if (bytes_read != size) { | |
271 | // g_warning("la8: %s: bytes to read: %d, bytes read: %d", | |
272 | // __func__, size, bytes_read); | |
273 | } | |
274 | ||
275 | return bytes_read; | |
276 | } | |
277 | ||
278 | static int la8_close(struct la8 *la8) | |
279 | { | |
280 | int ret; | |
281 | ||
282 | if (!la8) { | |
283 | g_warning("la8: %s: la8 was NULL", __func__); | |
8703f512 | 284 | return SR_ERR_ARG; |
f4314d7e UH |
285 | } |
286 | ||
287 | if (!la8->ftdic) { | |
288 | g_warning("la8: %s: la8->ftdic was NULL", __func__); | |
8703f512 | 289 | return SR_ERR_ARG; |
f4314d7e UH |
290 | } |
291 | ||
292 | if ((ret = ftdi_usb_close(la8->ftdic)) < 0) { | |
293 | g_warning("la8: %s: ftdi_usb_close: (%d) %s", | |
294 | __func__, ret, ftdi_get_error_string(la8->ftdic)); | |
295 | } | |
296 | ||
297 | return ret; | |
298 | } | |
299 | ||
300 | /** | |
301 | * Close the ChronoVu LA8 USB port and reset the LA8 sequencer logic. | |
302 | * | |
303 | * @param la8 The LA8 struct containing private per-device-instance data. | |
304 | * @return SR_OK upon success, SR_ERR upon failure. | |
305 | */ | |
306 | static int la8_close_usb_reset_sequencer(struct la8 *la8) | |
307 | { | |
308 | /* Magic sequence of bytes for resetting the LA8 sequencer logic. */ | |
309 | uint8_t buf[8] = {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}; | |
310 | int ret; | |
311 | ||
312 | g_debug("la8: entering %s", __func__); | |
313 | ||
314 | if (!la8) { | |
315 | g_warning("la8: %s: la8 was NULL", __func__); | |
8703f512 | 316 | return SR_ERR_ARG; |
f4314d7e UH |
317 | } |
318 | ||
319 | if (!la8->ftdic) { | |
320 | g_warning("la8: %s: la8->ftdic was NULL", __func__); | |
8703f512 | 321 | return SR_ERR_ARG; |
f4314d7e UH |
322 | } |
323 | ||
324 | if (la8->ftdic->usb_dev) { | |
325 | /* Reset the LA8 sequencer logic, then wait 100ms. */ | |
326 | g_debug("la8: resetting sequencer logic"); | |
327 | (void) la8_write(la8, buf, 8); /* Ignore errors. */ | |
328 | g_usleep(100 * 1000); | |
329 | ||
330 | /* Purge FTDI buffers, then reset and close the FTDI device. */ | |
331 | g_debug("la8: purging buffers, resetting+closing FTDI device"); | |
332 | ||
333 | /* Log errors, but ignore them (i.e., don't abort). */ | |
334 | if ((ret = ftdi_usb_purge_buffers(la8->ftdic)) < 0) | |
335 | g_warning("la8: %s: ftdi_usb_purge_buffers: (%d) %s", | |
336 | __func__, ret, ftdi_get_error_string(la8->ftdic)); | |
337 | if ((ret = ftdi_usb_reset(la8->ftdic)) < 0) | |
338 | g_warning("la8: %s: ftdi_usb_reset: (%d) %s", __func__, | |
339 | ret, ftdi_get_error_string(la8->ftdic)); | |
340 | if ((ret = ftdi_usb_close(la8->ftdic)) < 0) | |
341 | g_warning("la8: %s: ftdi_usb_close: (%d) %s", __func__, | |
342 | ret, ftdi_get_error_string(la8->ftdic)); | |
343 | } else { | |
344 | g_debug("la8: %s: usb_dev was NULL, nothing to do", __func__); | |
345 | } | |
346 | ||
347 | ftdi_free(la8->ftdic); /* Returns void. */ | |
348 | la8->ftdic = NULL; | |
349 | ||
350 | return SR_OK; | |
351 | } | |
352 | ||
353 | /** | |
354 | * Reset the ChronoVu LA8. | |
355 | * | |
356 | * The LA8 must be reset after a failed read/write operation or upon timeouts. | |
357 | * | |
358 | * @param la8 The LA8 struct containing private per-device-instance data. | |
359 | * @return SR_OK upon success, SR_ERR upon failure. | |
360 | */ | |
361 | static int la8_reset(struct la8 *la8) | |
362 | { | |
363 | uint8_t buf[4096]; | |
364 | time_t done, now; | |
365 | int bytes_read; | |
366 | ||
367 | if (!la8) { | |
368 | g_warning("la8: %s: la8 was NULL", __func__); | |
8703f512 | 369 | return SR_ERR_ARG; |
f4314d7e UH |
370 | } |
371 | ||
372 | if (!la8->ftdic) { | |
373 | g_warning("la8: %s: la8->ftdic was NULL", __func__); | |
8703f512 | 374 | return SR_ERR_ARG; |
f4314d7e UH |
375 | } |
376 | ||
377 | g_debug("la8: resetting the device"); | |
378 | ||
379 | /* | |
380 | * Purge pending read data from the FTDI hardware FIFO until | |
381 | * no more data is left, or a timeout occurs (after 20s). | |
382 | */ | |
383 | done = 20 + time(NULL); | |
384 | do { | |
385 | /* TODO: Ignore errors? Check for < 0 at least! */ | |
386 | bytes_read = la8_read(la8, (uint8_t *)&buf, 4096); | |
387 | now = time(NULL); | |
388 | } while ((done > now) && (bytes_read > 0)); | |
389 | ||
390 | /* Reset the LA8 sequencer logic and close the USB port. */ | |
391 | (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */ | |
392 | ||
393 | g_debug("la8: device reset finished"); | |
394 | ||
395 | return SR_OK; | |
396 | } | |
397 | ||
398 | static int hw_init(const char *deviceinfo) | |
399 | { | |
400 | int ret; | |
401 | struct sr_device_instance *sdi; | |
402 | struct la8 *la8; | |
403 | ||
404 | g_debug("la8: entering %s", __func__); | |
405 | ||
406 | /* Avoid compiler errors. */ | |
407 | deviceinfo = deviceinfo; | |
408 | ||
409 | /* Allocate memory for our private driver context. */ | |
410 | if (!(la8 = malloc(sizeof(struct la8)))) { | |
411 | g_warning("la8: %s: struct la8 malloc failed", __func__); | |
412 | ret = SR_ERR_MALLOC; | |
413 | goto err_free_nothing; | |
414 | } | |
415 | ||
416 | /* Set some sane defaults. */ | |
417 | la8->ftdic = NULL; | |
418 | la8->cur_samplerate = SR_MHZ(100); /* 100MHz == max. samplerate */ | |
419 | la8->limit_msec = 0; | |
420 | la8->limit_samples = 0; | |
421 | la8->num_probes = NUM_PROBES; | |
422 | la8->session_id = NULL; | |
423 | la8->mangled_buf = NULL; | |
424 | la8->final_buf = NULL; | |
425 | la8->trigger_pattern = 0x00; /* Value irrelevant, see trigger_mask. */ | |
426 | la8->trigger_mask = 0x00; /* All probes are "don't care". */ | |
427 | la8->trigger_timeout = 10; /* Default to 10s trigger timeout. */ | |
428 | la8->done = 0; | |
429 | la8->block_counter = 0; | |
430 | la8->divcount = 0; /* 10ns sample period == 100MHz samplerate */ | |
431 | ||
432 | /* Allocate memory for the raw (mangled) data from the LA8. */ | |
433 | if (!(la8->mangled_buf = malloc(SDRAM_SIZE))) { | |
434 | g_warning("la8: %s: mangled_buf malloc failed", __func__); | |
435 | ret = SR_ERR_MALLOC; | |
436 | goto err_free_la8; | |
437 | } | |
438 | ||
439 | /* Allocate memory where we'll store the de-mangled data. */ | |
440 | if (!(la8->final_buf = malloc(SDRAM_SIZE))) { | |
441 | g_warning("la8: %s: final_buf malloc failed", __func__); | |
442 | ret = SR_ERR_MALLOC; | |
443 | goto err_free_mangled_buf; | |
444 | } | |
445 | ||
446 | /* Allocate memory for the FTDI context (ftdic) and initialize it. */ | |
447 | if (!(la8->ftdic = ftdi_new())) { | |
448 | g_warning("la8: %s: ftdi_new failed", __func__); | |
449 | ret = SR_ERR; /* TODO: More specific error? */ | |
450 | goto err_free_final_buf; | |
451 | } | |
452 | ||
453 | /* Check for the device and temporarily open it. */ | |
454 | if ((ret = ftdi_usb_open_desc(la8->ftdic, USB_VENDOR_ID, | |
455 | USB_PRODUCT_ID, USB_DESCRIPTION, NULL)) < 0) { | |
456 | g_warning("la8: %s: ftdi_usb_open_desc: (%d) %s", | |
457 | __func__, ret, ftdi_get_error_string(la8->ftdic)); | |
458 | (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */ | |
459 | ret = SR_ERR; /* TODO: More specific error? */ | |
460 | goto err_free_ftdic; | |
461 | } | |
462 | g_debug("la8: found device"); | |
463 | ||
464 | /* Register the device with libsigrok. */ | |
465 | sdi = sr_device_instance_new(0, SR_ST_INITIALIZING, | |
466 | USB_VENDOR_NAME, USB_MODEL_NAME, USB_MODEL_VERSION); | |
467 | if (!sdi) { | |
468 | g_warning("la8: %s: sr_device_instance_new failed", __func__); | |
469 | ret = SR_ERR; /* TODO: More specific error? */ | |
470 | goto err_close_ftdic; | |
471 | } | |
472 | ||
473 | sdi->priv = la8; | |
474 | ||
475 | device_instances = g_slist_append(device_instances, sdi); | |
476 | ||
477 | g_debug("la8: %s finished successfully", __func__); | |
478 | ||
479 | /* Close device. We'll reopen it again when we need it. */ | |
480 | (void) la8_close(la8); /* Log, but ignore errors. */ | |
481 | ||
482 | // return SR_OK; /* TODO */ | |
483 | return 1; | |
484 | ||
485 | err_close_ftdic: | |
486 | (void) la8_close(la8); /* Log, but ignore errors. */ | |
487 | err_free_ftdic: | |
488 | free(la8->ftdic); | |
489 | err_free_final_buf: | |
490 | free(la8->final_buf); | |
491 | err_free_mangled_buf: | |
492 | free(la8->mangled_buf); | |
493 | err_free_la8: | |
494 | free(la8); | |
495 | err_free_nothing: | |
496 | // return ret; /* TODO */ | |
497 | return 0; | |
498 | } | |
499 | ||
500 | static int hw_opendev(int device_index) | |
501 | { | |
502 | int ret; | |
503 | struct sr_device_instance *sdi; | |
504 | struct la8 *la8; | |
505 | ||
506 | if (!(sdi = sr_get_device_instance(device_instances, device_index))) { | |
507 | g_warning("la8: %s: sdi was NULL", __func__); | |
8703f512 | 508 | return SR_ERR; /* TODO: SR_ERR_ARG? */ |
f4314d7e UH |
509 | } |
510 | ||
511 | if (!(la8 = sdi->priv)) { | |
512 | g_warning("la8: %s: sdi->priv was NULL", __func__); | |
8703f512 | 513 | return SR_ERR; /* TODO: SR_ERR_ARG? */ |
f4314d7e UH |
514 | } |
515 | ||
516 | g_debug("la8: opening device"); | |
517 | ||
518 | /* Open the device. */ | |
519 | if ((ret = ftdi_usb_open_desc(la8->ftdic, USB_VENDOR_ID, | |
520 | USB_PRODUCT_ID, USB_DESCRIPTION, NULL)) < 0) { | |
521 | g_warning("la8: %s: ftdi_usb_open_desc: (%d) %s", | |
522 | __func__, ret, ftdi_get_error_string(la8->ftdic)); | |
523 | (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */ | |
524 | return SR_ERR; | |
525 | } | |
526 | g_debug("la8: device opened successfully"); | |
527 | ||
528 | /* Purge RX/TX buffers in the FTDI chip. */ | |
529 | if ((ret = ftdi_usb_purge_buffers(la8->ftdic)) < 0) { | |
530 | g_warning("la8: %s: ftdi_usb_purge_buffers: (%d) %s", | |
531 | __func__, ret, ftdi_get_error_string(la8->ftdic)); | |
532 | (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */ | |
533 | goto err_opendev_close_ftdic; | |
534 | } | |
535 | g_debug("la8: FTDI buffers purged successfully"); | |
536 | ||
537 | /* Enable flow control in the FTDI chip. */ | |
538 | if ((ret = ftdi_setflowctrl(la8->ftdic, SIO_RTS_CTS_HS)) < 0) { | |
539 | g_warning("la8: %s: ftdi_setflowcontrol: (%d) %s", | |
540 | __func__, ret, ftdi_get_error_string(la8->ftdic)); | |
541 | (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */ | |
542 | goto err_opendev_close_ftdic; | |
543 | } | |
544 | g_debug("la8: FTDI flow control enabled successfully"); | |
545 | ||
546 | /* Wait 100ms. */ | |
547 | g_usleep(100 * 1000); | |
548 | ||
549 | sdi->status = SR_ST_ACTIVE; | |
550 | ||
551 | return SR_OK; | |
552 | ||
553 | err_opendev_close_ftdic: | |
554 | (void) la8_close(la8); /* Log, but ignore errors. */ | |
555 | return SR_ERR; | |
556 | } | |
557 | ||
558 | static int set_samplerate(struct sr_device_instance *sdi, uint64_t samplerate) | |
559 | { | |
560 | struct la8 *la8; | |
561 | ||
562 | if (!sdi) { | |
563 | g_warning("la8: %s: sdi was NULL", __func__); | |
8703f512 | 564 | return SR_ERR_ARG; |
f4314d7e UH |
565 | } |
566 | ||
567 | if (!(la8 = sdi->priv)) { | |
568 | g_warning("la8: %s: sdi->priv was NULL", __func__); | |
8703f512 | 569 | return SR_ERR_ARG; |
f4314d7e UH |
570 | } |
571 | ||
572 | g_debug("la8: setting samplerate"); | |
573 | ||
574 | fill_supported_samplerates_if_needed(); | |
575 | ||
576 | /* Check if this is a samplerate supported by the hardware. */ | |
577 | if (!is_valid_samplerate(samplerate)) | |
578 | return SR_ERR; | |
579 | ||
580 | /* Set the new samplerate. */ | |
581 | la8->cur_samplerate = samplerate; | |
582 | ||
583 | g_debug("la8: samplerate set to %" PRIu64 "Hz", la8->cur_samplerate); | |
584 | ||
585 | return SR_OK; | |
586 | } | |
587 | ||
588 | static void hw_closedev(int device_index) | |
589 | { | |
590 | struct sr_device_instance *sdi; | |
591 | struct la8 *la8; | |
592 | ||
593 | if (!(sdi = sr_get_device_instance(device_instances, device_index))) { | |
594 | g_warning("la8: %s: sdi was NULL", __func__); | |
595 | return; | |
596 | } | |
597 | ||
598 | if (!(la8 = sdi->priv)) { | |
599 | g_warning("la8: %s: sdi->priv was NULL", __func__); | |
600 | return; | |
601 | } | |
602 | ||
603 | g_debug("la8: closing device"); | |
604 | ||
605 | if (sdi->status == SR_ST_ACTIVE) { | |
606 | g_debug("la8: %s: status ACTIVE, closing device", __func__); | |
607 | /* TODO: Handle or ignore errors here? */ | |
608 | (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */ | |
609 | } else { | |
610 | g_debug("la8: %s: status not ACTIVE, nothing to do", __func__); | |
611 | } | |
612 | ||
613 | sdi->status = SR_ST_INACTIVE; | |
614 | } | |
615 | ||
616 | static void hw_cleanup(void) | |
617 | { | |
618 | GSList *l; | |
619 | struct sr_device_instance *sdi; | |
620 | ||
621 | g_debug("la8: entering %s", __func__); | |
622 | ||
623 | /* Properly close all devices. */ | |
624 | for (l = device_instances; l; l = l->next) { | |
625 | if ((sdi = l->data) == NULL) { | |
626 | g_warning("la8: %s: sdi was NULL", __func__); | |
627 | continue; | |
628 | } | |
629 | if (sdi->priv != NULL) | |
630 | free(sdi->priv); | |
631 | else | |
632 | g_warning("la8: %s: sdi->priv was NULL", __func__); | |
633 | sr_device_instance_free(sdi); /* Returns void. */ | |
634 | } | |
635 | g_slist_free(device_instances); /* Returns void. */ | |
636 | device_instances = NULL; | |
637 | } | |
638 | ||
639 | static void *hw_get_device_info(int device_index, int device_info_id) | |
640 | { | |
641 | struct sr_device_instance *sdi; | |
642 | struct la8 *la8; | |
643 | void *info; | |
644 | ||
645 | g_debug("la8: entering %s", __func__); | |
646 | ||
647 | if (!(sdi = sr_get_device_instance(device_instances, device_index))) { | |
648 | g_warning("la8: %s: sdi was NULL", __func__); | |
649 | return NULL; | |
650 | } | |
651 | ||
652 | if (!(la8 = sdi->priv)) { | |
653 | g_warning("la8: %s: sdi->priv was NULL", __func__); | |
654 | return NULL; | |
655 | } | |
656 | ||
657 | switch (device_info_id) { | |
658 | case SR_DI_INSTANCE: | |
659 | info = sdi; | |
660 | break; | |
661 | case SR_DI_NUM_PROBES: | |
662 | info = GINT_TO_POINTER(NUM_PROBES); | |
663 | break; | |
664 | case SR_DI_SAMPLERATES: | |
665 | fill_supported_samplerates_if_needed(); | |
666 | info = &samplerates; | |
667 | break; | |
668 | case SR_DI_TRIGGER_TYPES: | |
669 | info = (char *)TRIGGER_TYPES; | |
670 | break; | |
671 | case SR_DI_CUR_SAMPLERATE: | |
672 | info = &la8->cur_samplerate; | |
673 | break; | |
674 | default: | |
675 | /* Unknown device info ID, return NULL. */ | |
676 | g_warning("la8: %s: Unknown device info ID", __func__); | |
677 | info = NULL; | |
678 | break; | |
679 | } | |
680 | ||
681 | return info; | |
682 | } | |
683 | ||
684 | static int hw_get_status(int device_index) | |
685 | { | |
686 | struct sr_device_instance *sdi; | |
687 | ||
688 | if (!(sdi = sr_get_device_instance(device_instances, device_index))) { | |
689 | g_warning("la8: %s: sdi was NULL, device not found", __func__); | |
690 | return SR_ST_NOT_FOUND; | |
691 | } | |
692 | ||
693 | g_debug("la8: %s: returning status %d", __func__, sdi->status); | |
694 | ||
695 | return sdi->status; | |
696 | } | |
697 | ||
698 | static int *hw_get_capabilities(void) | |
699 | { | |
700 | g_debug("la8: entering %s", __func__); | |
701 | ||
702 | return capabilities; | |
703 | } | |
704 | ||
705 | static int hw_set_configuration(int device_index, int capability, void *value) | |
706 | { | |
707 | struct sr_device_instance *sdi; | |
708 | struct la8 *la8; | |
709 | ||
710 | g_debug("la8: entering %s", __func__); | |
711 | ||
712 | if (!(sdi = sr_get_device_instance(device_instances, device_index))) { | |
713 | g_warning("la8: %s: sdi was NULL", __func__); | |
8703f512 | 714 | return SR_ERR; /* TODO: SR_ERR_ARG? */ |
f4314d7e UH |
715 | } |
716 | ||
717 | if (!(la8 = sdi->priv)) { | |
718 | g_warning("la8: %s: sdi->priv was NULL", __func__); | |
8703f512 | 719 | return SR_ERR; /* TODO: SR_ERR_ARG? */ |
f4314d7e UH |
720 | } |
721 | ||
722 | switch (capability) { | |
723 | case SR_HWCAP_SAMPLERATE: | |
724 | if (set_samplerate(sdi, *(uint64_t *)value) == SR_ERR) | |
725 | return SR_ERR; | |
726 | g_debug("la8: SAMPLERATE = %" PRIu64, la8->cur_samplerate); | |
727 | break; | |
728 | case SR_HWCAP_PROBECONFIG: | |
729 | /* Nothing to do, but this entry must exist. Fix this. */ | |
730 | /* TODO? */ | |
731 | g_debug("la8: %s: SR_HWCAP_PROBECONFIG called", __func__); | |
732 | return SR_OK; | |
733 | break; | |
734 | case SR_HWCAP_LIMIT_MSEC: | |
735 | if (*(uint64_t *)value == 0) { | |
736 | g_warning("la8: %s: LIMIT_MSEC can't be 0", __func__); | |
737 | return SR_ERR; | |
738 | } | |
739 | la8->limit_msec = *(uint64_t *)value; | |
740 | g_debug("la8: LIMIT_MSEC = %" PRIu64, la8->limit_msec); | |
741 | break; | |
742 | case SR_HWCAP_LIMIT_SAMPLES: | |
743 | if (*(uint64_t *)value < MIN_NUM_SAMPLES) { | |
744 | g_warning("la8: %s: LIMIT_SAMPLES too small", __func__); | |
745 | return SR_ERR; | |
746 | } | |
747 | la8->limit_samples = *(uint64_t *)value; | |
748 | g_debug("la8: LIMIT_SAMPLES = %" PRIu64, la8->limit_samples); | |
749 | break; | |
750 | default: | |
751 | /* Unknown capability, return SR_ERR. */ | |
752 | g_warning("la8: %s: Unknown capability", __func__); | |
753 | return SR_ERR; | |
754 | break; | |
755 | } | |
756 | ||
757 | return SR_OK; | |
758 | } | |
759 | ||
760 | /** | |
761 | * Get a block of 4096 bytes of data from the LA8. | |
762 | * | |
763 | * @param la8 The LA8 struct containing private per-device-instance data. | |
764 | * @return SR_OK upon success, or SR_ERR upon errors. | |
765 | */ | |
766 | static int la8_read_block(struct la8 *la8) | |
767 | { | |
768 | int i, byte_offset, m, mi, p, index, bytes_read; | |
769 | time_t now; | |
770 | ||
771 | if (!la8) { | |
772 | g_warning("la8: %s: la8 was NULL", __func__); | |
8703f512 | 773 | return SR_ERR_ARG; |
f4314d7e UH |
774 | } |
775 | ||
776 | if (!la8->ftdic) { | |
777 | g_warning("la8: %s: la8->ftdic was NULL", __func__); | |
8703f512 | 778 | return SR_ERR_ARG; |
f4314d7e UH |
779 | } |
780 | ||
781 | // g_debug("la8: %s: reading block %d", __func__, la8->block_counter); | |
782 | ||
783 | bytes_read = la8_read(la8, la8->mangled_buf, 4096); | |
784 | ||
785 | /* If first block read got 0 bytes, retry until success or timeout. */ | |
786 | if ((bytes_read == 0) && (la8->block_counter == 0)) { | |
787 | do { | |
788 | // g_debug("la8: %s: reading block 0 again", __func__); | |
789 | bytes_read = la8_read(la8, la8->mangled_buf, 4096); | |
790 | /* TODO: How to handle read errors here? */ | |
791 | now = time(NULL); | |
792 | } while ((la8->done > now) && (bytes_read == 0)); | |
793 | } | |
794 | ||
795 | /* Check if block read was successful or a timeout occured. */ | |
796 | if (bytes_read != 4096) { | |
797 | g_warning("la8: %s: trigger timed out", __func__); | |
798 | (void) la8_reset(la8); /* Ignore errors. */ | |
799 | return SR_ERR; | |
800 | } | |
801 | ||
802 | /* De-mangle the data. */ | |
803 | // g_debug("la8: de-mangling samples of block %d", la8->block_counter); | |
804 | byte_offset = la8->block_counter * 4096; | |
805 | m = byte_offset / (1024 * 1024); | |
806 | mi = m * (1024 * 1024); | |
807 | for (i = 0; i < 4096; i++) { | |
808 | p = i & (1 << 0); | |
809 | index = m * 2 + (((byte_offset + i) - mi) / 2) * 16; | |
810 | index += (la8->divcount == 0) ? p : (1 - p); | |
811 | la8->final_buf[index] = la8->mangled_buf[i]; | |
812 | } | |
813 | ||
814 | return SR_OK; | |
815 | } | |
816 | ||
817 | static int receive_data(int fd, int revents, void *user_data) | |
818 | { | |
819 | int i, ret; | |
820 | struct sr_device_instance *sdi; | |
821 | struct sr_datafeed_packet packet; | |
822 | struct la8 *la8; | |
823 | ||
824 | /* Avoid compiler errors. */ | |
825 | fd = fd; | |
826 | revents = revents; | |
827 | ||
828 | if (!(sdi = user_data)) { | |
829 | g_warning("la8: %s: user_data was NULL", __func__); | |
830 | return FALSE; | |
831 | } | |
832 | ||
833 | if (!(la8 = sdi->priv)) { | |
834 | g_warning("la8: %s: sdi->priv was NULL", __func__); | |
835 | return FALSE; | |
836 | } | |
837 | ||
838 | /* Get one block of data (4096 bytes). */ | |
839 | if ((ret = la8_read_block(la8)) < 0) { | |
840 | g_warning("la8: %s: la8_read_block error: %d", __func__, ret); | |
841 | return FALSE; | |
842 | } | |
843 | ||
844 | /* We need to get exactly 2048 blocks (i.e. 8MB) of data. */ | |
845 | if (la8->block_counter != 2047) { | |
846 | la8->block_counter++; | |
847 | return TRUE; | |
848 | } | |
849 | ||
850 | g_debug("la8: sampling finished, sending data to session bus now"); | |
851 | ||
852 | /* All data was received and demangled, send it to the session bus. */ | |
853 | for (i = 0; i < 2048; i++) { | |
854 | /* Send a 4096 byte SR_DF_LOGIC packet to the session bus. */ | |
855 | // g_debug("la8: %s: sending SR_DF_LOGIC packet", __func__); | |
856 | packet.type = SR_DF_LOGIC; | |
857 | packet.length = 4096; | |
858 | packet.unitsize = 1; | |
859 | packet.payload = la8->final_buf + (i * 4096); | |
860 | sr_session_bus(la8->session_id, &packet); | |
861 | } | |
862 | ||
863 | hw_stop_acquisition(sdi->index, user_data); | |
864 | ||
865 | // return FALSE; /* FIXME? */ | |
866 | return TRUE; | |
867 | } | |
868 | ||
869 | static int hw_start_acquisition(int device_index, gpointer session_device_id) | |
870 | { | |
871 | struct sr_device_instance *sdi; | |
872 | struct la8 *la8; | |
873 | struct sr_datafeed_packet packet; | |
874 | struct sr_datafeed_header header; | |
875 | uint8_t buf[4]; | |
876 | int bytes_written; | |
877 | ||
878 | g_debug("la8: entering %s", __func__); | |
879 | ||
880 | if (!(sdi = sr_get_device_instance(device_instances, device_index))) { | |
881 | g_warning("la8: %s: sdi was NULL", __func__); | |
8703f512 | 882 | return SR_ERR; /* TODO: SR_ERR_ARG? */ |
f4314d7e UH |
883 | } |
884 | ||
885 | if (!(la8 = sdi->priv)) { | |
886 | g_warning("la8: %s: sdi->priv was NULL", __func__); | |
8703f512 | 887 | return SR_ERR; /* TODO: SR_ERR_ARG? */ |
f4314d7e UH |
888 | } |
889 | ||
890 | if (!la8->ftdic) { | |
891 | g_warning("la8: %s: la8->ftdic was NULL", __func__); | |
8703f512 | 892 | return SR_ERR_ARG; |
f4314d7e UH |
893 | } |
894 | ||
895 | la8->divcount = samplerate_to_divcount(la8->cur_samplerate); | |
896 | if (la8->divcount == 0xff) { | |
897 | g_warning("la8: %s: invalid divcount/samplerate", __func__); | |
898 | return SR_ERR; | |
899 | } | |
900 | ||
901 | /* Fill acquisition parameters into buf[]. */ | |
902 | buf[0] = la8->divcount; | |
903 | buf[1] = 0xff; /* This byte must always be 0xff. */ | |
904 | buf[2] = la8->trigger_pattern; | |
905 | buf[3] = la8->trigger_mask; | |
906 | ||
907 | /* Start acquisition. */ | |
908 | bytes_written = la8_write(la8, buf, 4); | |
909 | ||
910 | if (bytes_written < 0) { | |
911 | g_warning("la8: acquisition failed to start"); | |
912 | return SR_ERR; | |
913 | } else if (bytes_written != 4) { | |
914 | g_warning("la8: acquisition failed to start"); | |
915 | return SR_ERR; /* TODO: Other error and return code? */ | |
916 | } | |
917 | ||
918 | g_debug("la8: acquisition started successfully"); | |
919 | ||
920 | la8->session_id = session_device_id; | |
921 | ||
922 | /* Send header packet to the session bus. */ | |
923 | g_debug("la8: %s: sending SR_DF_HEADER", __func__); | |
924 | packet.type = SR_DF_HEADER; | |
925 | packet.length = sizeof(struct sr_datafeed_header); | |
926 | packet.unitsize = 0; | |
927 | packet.payload = &header; | |
928 | header.feed_version = 1; | |
929 | gettimeofday(&header.starttime, NULL); | |
930 | header.samplerate = la8->cur_samplerate; | |
931 | header.protocol_id = SR_PROTO_RAW; | |
932 | header.num_logic_probes = la8->num_probes; | |
933 | header.num_analog_probes = 0; | |
934 | sr_session_bus(session_device_id, &packet); | |
935 | ||
936 | /* Time when we should be done (for detecting trigger timeouts). */ | |
937 | la8->done = (la8->divcount + 1) * 0.08388608 + time(NULL) | |
938 | + la8->trigger_timeout; | |
939 | la8->block_counter = 0; | |
940 | ||
941 | /* Hook up a dummy handler to receive data from the LA8. */ | |
942 | sr_source_add(-1, G_IO_IN, 0, receive_data, sdi); | |
943 | ||
944 | return SR_OK; | |
945 | } | |
946 | ||
947 | static void hw_stop_acquisition(int device_index, gpointer session_device_id) | |
948 | { | |
949 | struct sr_device_instance *sdi; | |
950 | struct la8 *la8; | |
951 | struct sr_datafeed_packet packet; | |
952 | ||
953 | g_debug("la8: stopping acquisition"); | |
954 | ||
955 | if (!(sdi = sr_get_device_instance(device_instances, device_index))) { | |
956 | g_warning("la8: %s: sdi was NULL", __func__); | |
957 | return; | |
958 | } | |
959 | ||
960 | if (!(la8 = sdi->priv)) { | |
961 | g_warning("la8: %s: sdi->priv was NULL", __func__); | |
962 | return; | |
963 | } | |
964 | ||
965 | /* Reset the LA8 sequencer and close the device. */ | |
966 | (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */ | |
967 | ||
968 | /* Send end packet to the session bus. */ | |
969 | g_debug("la8: %s: sending SR_DF_END", __func__); | |
970 | packet.type = SR_DF_END; | |
971 | packet.length = 0; | |
972 | packet.unitsize = 0; | |
973 | packet.payload = NULL; | |
974 | sr_session_bus(session_device_id, &packet); | |
975 | } | |
976 | ||
977 | struct sr_device_plugin chronovu_la8_plugin_info = { | |
978 | .name = "chronovu-la8", | |
979 | .longname = "ChronoVu LA8", | |
980 | .api_version = 1, | |
981 | .init = hw_init, | |
982 | .cleanup = hw_cleanup, | |
983 | .open = hw_opendev, | |
984 | .close = hw_closedev, | |
985 | .get_device_info = hw_get_device_info, | |
986 | .get_status = hw_get_status, | |
987 | .get_capabilities = hw_get_capabilities, | |
988 | .set_configuration = hw_set_configuration, | |
989 | .start_acquisition = hw_start_acquisition, | |
990 | .stop_acquisition = hw_stop_acquisition, | |
991 | }; |