]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the sigrok project. | |
3 | * | |
4 | * Copyright (C) 2011-2012 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 <glib.h> | |
23 | #include "libsigrok.h" | |
24 | #include "libsigrok-internal.h" | |
25 | #include "protocol.h" | |
26 | ||
27 | /* Probes are numbered 0-7. */ | |
28 | SR_PRIV const char *probe_names[NUM_PROBES + 1] = { | |
29 | "0", | |
30 | "1", | |
31 | "2", | |
32 | "3", | |
33 | "4", | |
34 | "5", | |
35 | "6", | |
36 | "7", | |
37 | NULL, | |
38 | }; | |
39 | ||
40 | /* This will be initialized via hw_info_get()/SR_DI_SAMPLERATES. */ | |
41 | SR_PRIV uint64_t supported_samplerates[255 + 1] = { 0 }; | |
42 | ||
43 | /* | |
44 | * Min: 1 sample per 0.01us -> sample time is 0.084s, samplerate 100MHz | |
45 | * Max: 1 sample per 2.55us -> sample time is 21.391s, samplerate 392.15kHz | |
46 | */ | |
47 | const struct sr_samplerates samplerates = { | |
48 | .low = 0, | |
49 | .high = 0, | |
50 | .step = 0, | |
51 | .list = supported_samplerates, | |
52 | }; | |
53 | ||
54 | /* Note: Continuous sampling is not supported by the hardware. */ | |
55 | SR_PRIV const int hwcaps[] = { | |
56 | SR_HWCAP_LOGIC_ANALYZER, | |
57 | SR_HWCAP_SAMPLERATE, | |
58 | SR_HWCAP_LIMIT_MSEC, /* TODO: Not yet implemented. */ | |
59 | SR_HWCAP_LIMIT_SAMPLES, /* TODO: Not yet implemented. */ | |
60 | 0, | |
61 | }; | |
62 | ||
63 | SR_PRIV void fill_supported_samplerates_if_needed(void) | |
64 | { | |
65 | int i; | |
66 | ||
67 | /* Do nothing if supported_samplerates[] is already filled. */ | |
68 | if (supported_samplerates[0] != 0) | |
69 | return; | |
70 | ||
71 | /* Fill supported_samplerates[] with the proper values. */ | |
72 | for (i = 0; i < 255; i++) | |
73 | supported_samplerates[254 - i] = SR_MHZ(100) / (i + 1); | |
74 | supported_samplerates[255] = 0; | |
75 | } | |
76 | ||
77 | /** | |
78 | * Check if the given samplerate is supported by the LA8 hardware. | |
79 | * | |
80 | * @param samplerate The samplerate (in Hz) to check. | |
81 | * @return 1 if the samplerate is supported/valid, 0 otherwise. | |
82 | */ | |
83 | SR_PRIV int is_valid_samplerate(uint64_t samplerate) | |
84 | { | |
85 | int i; | |
86 | ||
87 | fill_supported_samplerates_if_needed(); | |
88 | ||
89 | for (i = 0; i < 255; i++) { | |
90 | if (supported_samplerates[i] == samplerate) | |
91 | return 1; | |
92 | } | |
93 | ||
94 | sr_err("Invalid samplerate (%" PRIu64 "Hz).", samplerate); | |
95 | ||
96 | return 0; | |
97 | } | |
98 | ||
99 | /** | |
100 | * Convert a samplerate (in Hz) to the 'divcount' value the LA8 wants. | |
101 | * | |
102 | * LA8 hardware: sample period = (divcount + 1) * 10ns. | |
103 | * Min. value for divcount: 0x00 (10ns sample period, 100MHz samplerate). | |
104 | * Max. value for divcount: 0xfe (2550ns sample period, 392.15kHz samplerate). | |
105 | * | |
106 | * @param samplerate The samplerate in Hz. | |
107 | * @return The divcount value as needed by the hardware, or 0xff upon errors. | |
108 | */ | |
109 | SR_PRIV uint8_t samplerate_to_divcount(uint64_t samplerate) | |
110 | { | |
111 | if (samplerate == 0) { | |
112 | sr_err("%s: samplerate was 0.", __func__); | |
113 | return 0xff; | |
114 | } | |
115 | ||
116 | if (!is_valid_samplerate(samplerate)) { | |
117 | sr_err("%s: Can't get divcount, samplerate invalid.", __func__); | |
118 | return 0xff; | |
119 | } | |
120 | ||
121 | return (SR_MHZ(100) / samplerate) - 1; | |
122 | } | |
123 | ||
124 | /** | |
125 | * Write data of a certain length to the LA8's FTDI device. | |
126 | * | |
127 | * @param devc The struct containing private per-device-instance data. Must not | |
128 | * be NULL. devc->ftdic must not be NULL either. | |
129 | * @param buf The buffer containing the data to write. Must not be NULL. | |
130 | * @param size The number of bytes to write. Must be >= 0. | |
131 | * @return The number of bytes written, or a negative value upon errors. | |
132 | */ | |
133 | SR_PRIV int la8_write(struct dev_context *devc, uint8_t *buf, int size) | |
134 | { | |
135 | int bytes_written; | |
136 | ||
137 | /* Note: Caller checked that devc and devc->ftdic != NULL. */ | |
138 | ||
139 | if (!buf) { | |
140 | sr_err("%s: buf was NULL.", __func__); | |
141 | return SR_ERR_ARG; | |
142 | } | |
143 | ||
144 | if (size < 0) { | |
145 | sr_err("%s: size was < 0.", __func__); | |
146 | return SR_ERR_ARG; | |
147 | } | |
148 | ||
149 | bytes_written = ftdi_write_data(devc->ftdic, buf, size); | |
150 | ||
151 | if (bytes_written < 0) { | |
152 | sr_err("%s: ftdi_write_data: (%d) %s.", __func__, | |
153 | bytes_written, ftdi_get_error_string(devc->ftdic)); | |
154 | (void) la8_close_usb_reset_sequencer(devc); /* Ignore errors. */ | |
155 | } else if (bytes_written != size) { | |
156 | sr_err("%s: bytes to write: %d, bytes written: %d.", | |
157 | __func__, size, bytes_written); | |
158 | (void) la8_close_usb_reset_sequencer(devc); /* Ignore errors. */ | |
159 | } | |
160 | ||
161 | return bytes_written; | |
162 | } | |
163 | ||
164 | /** | |
165 | * Read a certain amount of bytes from the LA8's FTDI device. | |
166 | * | |
167 | * @param devc The struct containing private per-device-instance data. Must not | |
168 | * be NULL. devc->ftdic must not be NULL either. | |
169 | * @param buf The buffer where the received data will be stored. Must not | |
170 | * be NULL. | |
171 | * @param size The number of bytes to read. Must be >= 1. | |
172 | * @return The number of bytes read, or a negative value upon errors. | |
173 | */ | |
174 | SR_PRIV int la8_read(struct dev_context *devc, uint8_t *buf, int size) | |
175 | { | |
176 | int bytes_read; | |
177 | ||
178 | /* Note: Caller checked that devc and devc->ftdic != NULL. */ | |
179 | ||
180 | if (!buf) { | |
181 | sr_err("%s: buf was NULL.", __func__); | |
182 | return SR_ERR_ARG; | |
183 | } | |
184 | ||
185 | if (size <= 0) { | |
186 | sr_err("%s: size was <= 0.", __func__); | |
187 | return SR_ERR_ARG; | |
188 | } | |
189 | ||
190 | bytes_read = ftdi_read_data(devc->ftdic, buf, size); | |
191 | ||
192 | if (bytes_read < 0) { | |
193 | sr_err("%s: ftdi_read_data: (%d) %s.", __func__, | |
194 | bytes_read, ftdi_get_error_string(devc->ftdic)); | |
195 | } else if (bytes_read != size) { | |
196 | // sr_err("%s: Bytes to read: %d, bytes read: %d.", | |
197 | // __func__, size, bytes_read); | |
198 | } | |
199 | ||
200 | return bytes_read; | |
201 | } | |
202 | ||
203 | SR_PRIV int la8_close(struct dev_context *devc) | |
204 | { | |
205 | int ret; | |
206 | ||
207 | if (!devc) { | |
208 | sr_err("%s: devc was NULL.", __func__); | |
209 | return SR_ERR_ARG; | |
210 | } | |
211 | ||
212 | if (!devc->ftdic) { | |
213 | sr_err("%s: devc->ftdic was NULL.", __func__); | |
214 | return SR_ERR_ARG; | |
215 | } | |
216 | ||
217 | if ((ret = ftdi_usb_close(devc->ftdic)) < 0) { | |
218 | sr_err("%s: ftdi_usb_close: (%d) %s.", | |
219 | __func__, ret, ftdi_get_error_string(devc->ftdic)); | |
220 | } | |
221 | ||
222 | return ret; | |
223 | } | |
224 | ||
225 | /** | |
226 | * Close the ChronoVu LA8 USB port and reset the LA8 sequencer logic. | |
227 | * | |
228 | * @param devc The struct containing private per-device-instance data. | |
229 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments. | |
230 | */ | |
231 | SR_PRIV int la8_close_usb_reset_sequencer(struct dev_context *devc) | |
232 | { | |
233 | /* Magic sequence of bytes for resetting the LA8 sequencer logic. */ | |
234 | uint8_t buf[8] = {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}; | |
235 | int ret; | |
236 | ||
237 | if (!devc) { | |
238 | sr_err("%s: devc was NULL.", __func__); | |
239 | return SR_ERR_ARG; | |
240 | } | |
241 | ||
242 | if (!devc->ftdic) { | |
243 | sr_err("%s: devc->ftdic was NULL.", __func__); | |
244 | return SR_ERR_ARG; | |
245 | } | |
246 | ||
247 | if (devc->ftdic->usb_dev) { | |
248 | /* Reset the LA8 sequencer logic, then wait 100ms. */ | |
249 | sr_dbg("Resetting sequencer logic."); | |
250 | (void) la8_write(devc, buf, 8); /* Ignore errors. */ | |
251 | g_usleep(100 * 1000); | |
252 | ||
253 | /* Purge FTDI buffers, then reset and close the FTDI device. */ | |
254 | sr_dbg("Purging buffers, resetting+closing FTDI device."); | |
255 | ||
256 | /* Log errors, but ignore them (i.e., don't abort). */ | |
257 | if ((ret = ftdi_usb_purge_buffers(devc->ftdic)) < 0) | |
258 | sr_err("%s: ftdi_usb_purge_buffers: (%d) %s.", | |
259 | __func__, ret, ftdi_get_error_string(devc->ftdic)); | |
260 | if ((ret = ftdi_usb_reset(devc->ftdic)) < 0) | |
261 | sr_err("%s: ftdi_usb_reset: (%d) %s.", __func__, | |
262 | ret, ftdi_get_error_string(devc->ftdic)); | |
263 | if ((ret = ftdi_usb_close(devc->ftdic)) < 0) | |
264 | sr_err("%s: ftdi_usb_close: (%d) %s.", __func__, | |
265 | ret, ftdi_get_error_string(devc->ftdic)); | |
266 | } | |
267 | ||
268 | /* Close USB device, deinitialize and free the FTDI context. */ | |
269 | ftdi_free(devc->ftdic); /* Returns void. */ | |
270 | devc->ftdic = NULL; | |
271 | ||
272 | return SR_OK; | |
273 | } | |
274 | ||
275 | /** | |
276 | * Reset the ChronoVu LA8. | |
277 | * | |
278 | * The LA8 must be reset after a failed read/write operation or upon timeouts. | |
279 | * | |
280 | * @param devc The struct containing private per-device-instance data. | |
281 | * @return SR_OK upon success, SR_ERR upon failure. | |
282 | */ | |
283 | SR_PRIV int la8_reset(struct dev_context *devc) | |
284 | { | |
285 | uint8_t buf[BS]; | |
286 | time_t done, now; | |
287 | int bytes_read; | |
288 | ||
289 | if (!devc) { | |
290 | sr_err("%s: devc was NULL.", __func__); | |
291 | return SR_ERR_ARG; | |
292 | } | |
293 | ||
294 | if (!devc->ftdic) { | |
295 | sr_err("%s: devc->ftdic was NULL.", __func__); | |
296 | return SR_ERR_ARG; | |
297 | } | |
298 | ||
299 | sr_dbg("Resetting the device."); | |
300 | ||
301 | /* | |
302 | * Purge pending read data from the FTDI hardware FIFO until | |
303 | * no more data is left, or a timeout occurs (after 20s). | |
304 | */ | |
305 | done = 20 + time(NULL); | |
306 | do { | |
307 | /* TODO: Ignore errors? Check for < 0 at least! */ | |
308 | bytes_read = la8_read(devc, (uint8_t *)&buf, BS); | |
309 | now = time(NULL); | |
310 | } while ((done > now) && (bytes_read > 0)); | |
311 | ||
312 | /* Reset the LA8 sequencer logic and close the USB port. */ | |
313 | (void) la8_close_usb_reset_sequencer(devc); /* Ignore errors. */ | |
314 | ||
315 | sr_dbg("Device reset finished."); | |
316 | ||
317 | return SR_OK; | |
318 | } | |
319 | ||
320 | SR_PRIV int configure_probes(const struct sr_dev_inst *sdi) | |
321 | { | |
322 | struct dev_context *devc; | |
323 | const struct sr_probe *probe; | |
324 | const GSList *l; | |
325 | uint8_t probe_bit; | |
326 | char *tc; | |
327 | ||
328 | devc = sdi->priv; | |
329 | devc->trigger_pattern = 0; | |
330 | devc->trigger_mask = 0; /* Default to "don't care" for all probes. */ | |
331 | ||
332 | for (l = sdi->probes; l; l = l->next) { | |
333 | probe = (struct sr_probe *)l->data; | |
334 | ||
335 | if (!probe) { | |
336 | sr_err("%s: probe was NULL.", __func__); | |
337 | return SR_ERR; | |
338 | } | |
339 | ||
340 | /* Skip disabled probes. */ | |
341 | if (!probe->enabled) | |
342 | continue; | |
343 | ||
344 | /* Skip (enabled) probes with no configured trigger. */ | |
345 | if (!probe->trigger) | |
346 | continue; | |
347 | ||
348 | /* Note: Must only be run if probe->trigger != NULL. */ | |
349 | if (probe->index < 0 || probe->index > 7) { | |
350 | sr_err("%s: Invalid probe index %d, must be " | |
351 | "between 0 and 7.", __func__, probe->index); | |
352 | return SR_ERR; | |
353 | } | |
354 | ||
355 | probe_bit = (1 << (probe->index)); | |
356 | ||
357 | /* Configure the probe's trigger mask and trigger pattern. */ | |
358 | for (tc = probe->trigger; tc && *tc; tc++) { | |
359 | devc->trigger_mask |= probe_bit; | |
360 | ||
361 | /* Sanity check, LA8 only supports low/high trigger. */ | |
362 | if (*tc != '0' && *tc != '1') { | |
363 | sr_err("%s: Invalid trigger '%c', only " | |
364 | "'0'/'1' supported.", __func__, *tc); | |
365 | return SR_ERR; | |
366 | } | |
367 | ||
368 | if (*tc == '1') | |
369 | devc->trigger_pattern |= probe_bit; | |
370 | } | |
371 | } | |
372 | ||
373 | sr_dbg("Trigger mask = 0x%x, trigger pattern = 0x%x.", | |
374 | devc->trigger_mask, devc->trigger_pattern); | |
375 | ||
376 | return SR_OK; | |
377 | } | |
378 | ||
379 | SR_PRIV int set_samplerate(const struct sr_dev_inst *sdi, uint64_t samplerate) | |
380 | { | |
381 | struct dev_context *devc; | |
382 | ||
383 | /* Note: Caller checked that sdi and sdi->priv != NULL. */ | |
384 | ||
385 | devc = sdi->priv; | |
386 | ||
387 | sr_spew("Trying to set samplerate to %" PRIu64 "Hz.", samplerate); | |
388 | ||
389 | fill_supported_samplerates_if_needed(); | |
390 | ||
391 | /* Check if this is a samplerate supported by the hardware. */ | |
392 | if (!is_valid_samplerate(samplerate)) | |
393 | return SR_ERR; | |
394 | ||
395 | /* Set the new samplerate. */ | |
396 | devc->cur_samplerate = samplerate; | |
397 | ||
398 | sr_dbg("Samplerate set to %" PRIu64 "Hz.", devc->cur_samplerate); | |
399 | ||
400 | return SR_OK; | |
401 | } | |
402 | ||
403 | /** | |
404 | * Get a block of data from the LA8. | |
405 | * | |
406 | * @param devc The struct containing private per-device-instance data. Must not | |
407 | * be NULL. devc->ftdic must not be NULL either. | |
408 | * @return SR_OK upon success, or SR_ERR upon errors. | |
409 | */ | |
410 | SR_PRIV int la8_read_block(struct dev_context *devc) | |
411 | { | |
412 | int i, byte_offset, m, mi, p, index, bytes_read; | |
413 | time_t now; | |
414 | ||
415 | /* Note: Caller checked that devc and devc->ftdic != NULL. */ | |
416 | ||
417 | sr_spew("Reading block %d.", devc->block_counter); | |
418 | ||
419 | bytes_read = la8_read(devc, devc->mangled_buf, BS); | |
420 | ||
421 | /* If first block read got 0 bytes, retry until success or timeout. */ | |
422 | if ((bytes_read == 0) && (devc->block_counter == 0)) { | |
423 | do { | |
424 | sr_spew("Reading block 0 (again)."); | |
425 | bytes_read = la8_read(devc, devc->mangled_buf, BS); | |
426 | /* TODO: How to handle read errors here? */ | |
427 | now = time(NULL); | |
428 | } while ((devc->done > now) && (bytes_read == 0)); | |
429 | } | |
430 | ||
431 | /* Check if block read was successful or a timeout occured. */ | |
432 | if (bytes_read != BS) { | |
433 | sr_err("Trigger timed out. Bytes read: %d.", bytes_read); | |
434 | (void) la8_reset(devc); /* Ignore errors. */ | |
435 | return SR_ERR; | |
436 | } | |
437 | ||
438 | /* De-mangle the data. */ | |
439 | sr_spew("Demangling block %d.", devc->block_counter); | |
440 | byte_offset = devc->block_counter * BS; | |
441 | m = byte_offset / (1024 * 1024); | |
442 | mi = m * (1024 * 1024); | |
443 | for (i = 0; i < BS; i++) { | |
444 | p = i & (1 << 0); | |
445 | index = m * 2 + (((byte_offset + i) - mi) / 2) * 16; | |
446 | index += (devc->divcount == 0) ? p : (1 - p); | |
447 | devc->final_buf[index] = devc->mangled_buf[i]; | |
448 | } | |
449 | ||
450 | return SR_OK; | |
451 | } | |
452 | ||
453 | SR_PRIV void send_block_to_session_bus(struct dev_context *devc, int block) | |
454 | { | |
455 | int i; | |
456 | uint8_t sample, expected_sample; | |
457 | struct sr_datafeed_packet packet; | |
458 | struct sr_datafeed_logic logic; | |
459 | int trigger_point; /* Relative trigger point (in this block). */ | |
460 | ||
461 | /* Note: No sanity checks on devc/block, caller is responsible. */ | |
462 | ||
463 | /* Check if we can find the trigger condition in this block. */ | |
464 | trigger_point = -1; | |
465 | expected_sample = devc->trigger_pattern & devc->trigger_mask; | |
466 | for (i = 0; i < BS; i++) { | |
467 | /* Don't continue if the trigger was found previously. */ | |
468 | if (devc->trigger_found) | |
469 | break; | |
470 | ||
471 | /* | |
472 | * Also, don't continue if triggers are "don't care", i.e. if | |
473 | * no trigger conditions were specified by the user. In that | |
474 | * case we don't want to send an SR_DF_TRIGGER packet at all. | |
475 | */ | |
476 | if (devc->trigger_mask == 0x00) | |
477 | break; | |
478 | ||
479 | sample = *(devc->final_buf + (block * BS) + i); | |
480 | ||
481 | if ((sample & devc->trigger_mask) == expected_sample) { | |
482 | trigger_point = i; | |
483 | devc->trigger_found = 1; | |
484 | break; | |
485 | } | |
486 | } | |
487 | ||
488 | /* If no trigger was found, send one SR_DF_LOGIC packet. */ | |
489 | if (trigger_point == -1) { | |
490 | /* Send an SR_DF_LOGIC packet to the session bus. */ | |
491 | sr_spew("Sending SR_DF_LOGIC packet (%d bytes) for " | |
492 | "block %d.", BS, block); | |
493 | packet.type = SR_DF_LOGIC; | |
494 | packet.payload = &logic; | |
495 | logic.length = BS; | |
496 | logic.unitsize = 1; | |
497 | logic.data = devc->final_buf + (block * BS); | |
498 | sr_session_send(devc->session_dev_id, &packet); | |
499 | return; | |
500 | } | |
501 | ||
502 | /* | |
503 | * We found the trigger, so some special handling is needed. We have | |
504 | * to send an SR_DF_LOGIC packet with the samples before the trigger | |
505 | * (if any), then the SD_DF_TRIGGER packet itself, then another | |
506 | * SR_DF_LOGIC packet with the samples after the trigger (if any). | |
507 | */ | |
508 | ||
509 | /* TODO: Send SR_DF_TRIGGER packet before or after the actual sample? */ | |
510 | ||
511 | /* If at least one sample is located before the trigger... */ | |
512 | if (trigger_point > 0) { | |
513 | /* Send pre-trigger SR_DF_LOGIC packet to the session bus. */ | |
514 | sr_spew("Sending pre-trigger SR_DF_LOGIC packet, " | |
515 | "start = %d, length = %d.", block * BS, trigger_point); | |
516 | packet.type = SR_DF_LOGIC; | |
517 | packet.payload = &logic; | |
518 | logic.length = trigger_point; | |
519 | logic.unitsize = 1; | |
520 | logic.data = devc->final_buf + (block * BS); | |
521 | sr_session_send(devc->session_dev_id, &packet); | |
522 | } | |
523 | ||
524 | /* Send the SR_DF_TRIGGER packet to the session bus. */ | |
525 | sr_spew("Sending SR_DF_TRIGGER packet, sample = %d.", | |
526 | (block * BS) + trigger_point); | |
527 | packet.type = SR_DF_TRIGGER; | |
528 | packet.payload = NULL; | |
529 | sr_session_send(devc->session_dev_id, &packet); | |
530 | ||
531 | /* If at least one sample is located after the trigger... */ | |
532 | if (trigger_point < (BS - 1)) { | |
533 | /* Send post-trigger SR_DF_LOGIC packet to the session bus. */ | |
534 | sr_spew("Sending post-trigger SR_DF_LOGIC packet, " | |
535 | "start = %d, length = %d.", | |
536 | (block * BS) + trigger_point, BS - trigger_point); | |
537 | packet.type = SR_DF_LOGIC; | |
538 | packet.payload = &logic; | |
539 | logic.length = BS - trigger_point; | |
540 | logic.unitsize = 1; | |
541 | logic.data = devc->final_buf + (block * BS) + trigger_point; | |
542 | sr_session_send(devc->session_dev_id, &packet); | |
543 | } | |
544 | } |