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