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