]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2011-2014 Uwe Hermann <uwe@hermann-uwe.de> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | #include <config.h> | |
21 | #include "protocol.h" | |
22 | ||
23 | SR_PRIV const struct cv_profile cv_profiles[] = { | |
24 | { CHRONOVU_LA8, "LA8", "ChronoVu LA8", 8, SR_MHZ(100), 2, 0.8388608 }, | |
25 | { CHRONOVU_LA16, "LA16", "ChronoVu LA16", 16, SR_MHZ(200), 4, 0.042 }, | |
26 | ALL_ZERO | |
27 | }; | |
28 | ||
29 | /* LA8: channels are numbered 0-7. LA16: channels are numbered 0-15. */ | |
30 | SR_PRIV const char *cv_channel_names[] = { | |
31 | "0", "1", "2", "3", "4", "5", "6", "7", | |
32 | "8", "9", "10", "11", "12", "13", "14", "15", | |
33 | }; | |
34 | ||
35 | static int close_usb_reset_sequencer(struct dev_context *devc); | |
36 | ||
37 | SR_PRIV void cv_fill_samplerates_if_needed(const struct sr_dev_inst *sdi) | |
38 | { | |
39 | int i; | |
40 | struct dev_context *devc; | |
41 | ||
42 | devc = sdi->priv; | |
43 | ||
44 | if (devc->samplerates[0] != 0) | |
45 | return; | |
46 | ||
47 | for (i = 0; i < 255; i++) | |
48 | devc->samplerates[254 - i] = devc->prof->max_samplerate / (i + 1); | |
49 | } | |
50 | ||
51 | /** | |
52 | * Check if the given samplerate is supported by the hardware. | |
53 | * | |
54 | * @param sdi Device instance. | |
55 | * @param samplerate The samplerate (in Hz) to check. | |
56 | * | |
57 | * @return 1 if the samplerate is supported/valid, 0 otherwise. | |
58 | */ | |
59 | static int is_valid_samplerate(const struct sr_dev_inst *sdi, | |
60 | uint64_t samplerate) | |
61 | { | |
62 | int i; | |
63 | struct dev_context *devc; | |
64 | ||
65 | devc = sdi->priv; | |
66 | ||
67 | cv_fill_samplerates_if_needed(sdi); | |
68 | ||
69 | for (i = 0; i < 255; i++) { | |
70 | if (devc->samplerates[i] == samplerate) | |
71 | return 1; | |
72 | } | |
73 | ||
74 | sr_err("Invalid samplerate (%" PRIu64 "Hz).", samplerate); | |
75 | ||
76 | return 0; | |
77 | } | |
78 | ||
79 | /** | |
80 | * Convert a samplerate (in Hz) to the 'divcount' value the device wants. | |
81 | * | |
82 | * The divcount value can be 0x00 - 0xfe (0xff is not valid). | |
83 | * | |
84 | * LA8: | |
85 | * sample period = (divcount + 1) * 10ns. | |
86 | * divcount = 0x00: 10ns period, 100MHz samplerate. | |
87 | * divcount = 0xfe: 2550ns period, 392.15kHz samplerate. | |
88 | * | |
89 | * LA16: | |
90 | * sample period = (divcount + 1) * 5ns. | |
91 | * divcount = 0x00: 5ns period, 200MHz samplerate. | |
92 | * divcount = 0xfe: 1275ns period, ~784.31kHz samplerate. | |
93 | * | |
94 | * @param sdi Device instance. | |
95 | * @param samplerate The samplerate in Hz. | |
96 | * | |
97 | * @return The divcount value as needed by the hardware, or 0xff upon errors. | |
98 | */ | |
99 | SR_PRIV uint8_t cv_samplerate_to_divcount(const struct sr_dev_inst *sdi, | |
100 | uint64_t samplerate) | |
101 | { | |
102 | struct dev_context *devc; | |
103 | ||
104 | devc = sdi->priv; | |
105 | ||
106 | if (samplerate == 0) { | |
107 | sr_err("Can't convert invalid samplerate of 0 Hz."); | |
108 | return 0xff; | |
109 | } | |
110 | ||
111 | if (!is_valid_samplerate(sdi, samplerate)) { | |
112 | sr_err("Can't get divcount, samplerate invalid."); | |
113 | return 0xff; | |
114 | } | |
115 | ||
116 | return (devc->prof->max_samplerate / samplerate) - 1; | |
117 | } | |
118 | ||
119 | /** | |
120 | * Write data of a certain length to the FTDI device. | |
121 | * | |
122 | * @param devc The struct containing private per-device-instance data. Must not | |
123 | * be NULL. devc->ftdic must not be NULL either. | |
124 | * @param buf The buffer containing the data to write. Must not be NULL. | |
125 | * @param size The number of bytes to write. Must be > 0. | |
126 | * | |
127 | * @return The number of bytes written, or a negative value upon errors. | |
128 | */ | |
129 | SR_PRIV int cv_write(struct dev_context *devc, uint8_t *buf, int size) | |
130 | { | |
131 | int bytes_written; | |
132 | ||
133 | /* Note: Caller ensures devc/devc->ftdic/buf != NULL and size > 0. */ | |
134 | ||
135 | bytes_written = ftdi_write_data(devc->ftdic, buf, size); | |
136 | ||
137 | if (bytes_written < 0) { | |
138 | sr_err("Failed to write data (%d): %s.", | |
139 | bytes_written, ftdi_get_error_string(devc->ftdic)); | |
140 | (void) close_usb_reset_sequencer(devc); /* Ignore errors. */ | |
141 | } else if (bytes_written != size) { | |
142 | sr_err("Failed to write data, only %d/%d bytes written.", | |
143 | size, bytes_written); | |
144 | (void) close_usb_reset_sequencer(devc); /* Ignore errors. */ | |
145 | } | |
146 | ||
147 | return bytes_written; | |
148 | } | |
149 | ||
150 | /** | |
151 | * Read a certain amount of bytes from the FTDI device. | |
152 | * | |
153 | * @param devc The struct containing private per-device-instance data. Must not | |
154 | * be NULL. devc->ftdic must not be NULL either. | |
155 | * @param buf The buffer where the received data will be stored. Must not | |
156 | * be NULL. | |
157 | * @param size The number of bytes to read. Must be >= 1. | |
158 | * | |
159 | * @return The number of bytes read, or a negative value upon errors. | |
160 | */ | |
161 | static int cv_read(struct dev_context *devc, uint8_t *buf, int size) | |
162 | { | |
163 | int bytes_read; | |
164 | ||
165 | /* Note: Caller ensures devc/devc->ftdic/buf != NULL and size > 0. */ | |
166 | ||
167 | bytes_read = ftdi_read_data(devc->ftdic, buf, size); | |
168 | ||
169 | if (bytes_read < 0) { | |
170 | sr_err("Failed to read data (%d): %s.", | |
171 | bytes_read, ftdi_get_error_string(devc->ftdic)); | |
172 | } else if (bytes_read != size) { | |
173 | // sr_err("Failed to read data, only %d/%d bytes read.", | |
174 | // bytes_read, size); | |
175 | } | |
176 | ||
177 | return bytes_read; | |
178 | } | |
179 | ||
180 | /** | |
181 | * Close the USB port and reset the sequencer logic. | |
182 | * | |
183 | * @param devc The struct containing private per-device-instance data. | |
184 | * | |
185 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments. | |
186 | */ | |
187 | static int close_usb_reset_sequencer(struct dev_context *devc) | |
188 | { | |
189 | /* Magic sequence of bytes for resetting the sequencer logic. */ | |
190 | uint8_t buf[8] = {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}; | |
191 | int ret; | |
192 | ||
193 | /* Note: Caller checked that devc and devc->ftdic != NULL. */ | |
194 | ||
195 | if (devc->ftdic->usb_dev) { | |
196 | /* Reset the sequencer logic, then wait 100ms. */ | |
197 | sr_dbg("Resetting sequencer logic."); | |
198 | (void) cv_write(devc, buf, 8); /* Ignore errors. */ | |
199 | g_usleep(100 * 1000); | |
200 | ||
201 | /* Purge FTDI buffers, then reset and close the FTDI device. */ | |
202 | sr_dbg("Purging buffers, resetting+closing FTDI device."); | |
203 | ||
204 | /* Log errors, but ignore them (i.e., don't abort). */ | |
205 | if ((ret = ftdi_usb_purge_buffers(devc->ftdic)) < 0) | |
206 | sr_err("Failed to purge FTDI buffers (%d): %s.", | |
207 | ret, ftdi_get_error_string(devc->ftdic)); | |
208 | if ((ret = ftdi_usb_reset(devc->ftdic)) < 0) | |
209 | sr_err("Failed to reset FTDI device (%d): %s.", | |
210 | ret, ftdi_get_error_string(devc->ftdic)); | |
211 | if ((ret = ftdi_usb_close(devc->ftdic)) < 0) | |
212 | sr_err("Failed to close FTDI device (%d): %s.", | |
213 | ret, ftdi_get_error_string(devc->ftdic)); | |
214 | } | |
215 | ||
216 | /* Close USB device, deinitialize and free the FTDI context. */ | |
217 | ftdi_free(devc->ftdic); | |
218 | devc->ftdic = NULL; | |
219 | ||
220 | return SR_OK; | |
221 | } | |
222 | ||
223 | /** | |
224 | * Reset the ChronoVu device. | |
225 | * | |
226 | * A reset is required after a failed read/write operation or upon timeouts. | |
227 | * | |
228 | * @param devc The struct containing private per-device-instance data. | |
229 | * | |
230 | * @return SR_OK upon success, SR_ERR upon failure. | |
231 | */ | |
232 | static int reset_device(struct dev_context *devc) | |
233 | { | |
234 | uint8_t buf[BS]; | |
235 | gint64 done, now; | |
236 | int bytes_read; | |
237 | ||
238 | /* Note: Caller checked that devc and devc->ftdic != NULL. */ | |
239 | ||
240 | sr_dbg("Resetting the device."); | |
241 | ||
242 | /* | |
243 | * Purge pending read data from the FTDI hardware FIFO until | |
244 | * no more data is left, or a timeout occurs (after 20s). | |
245 | */ | |
246 | done = (20 * G_TIME_SPAN_SECOND) + g_get_monotonic_time(); | |
247 | do { | |
248 | /* Try to read bytes until none are left (or errors occur). */ | |
249 | bytes_read = cv_read(devc, (uint8_t *)&buf, BS); | |
250 | now = g_get_monotonic_time(); | |
251 | } while ((done > now) && (bytes_read > 0)); | |
252 | ||
253 | /* Reset the sequencer logic and close the USB port. */ | |
254 | (void) close_usb_reset_sequencer(devc); /* Ignore errors. */ | |
255 | ||
256 | sr_dbg("Device reset finished."); | |
257 | ||
258 | return SR_OK; | |
259 | } | |
260 | ||
261 | SR_PRIV int cv_convert_trigger(const struct sr_dev_inst *sdi) | |
262 | { | |
263 | struct dev_context *devc; | |
264 | struct sr_trigger *trigger; | |
265 | struct sr_trigger_stage *stage; | |
266 | struct sr_trigger_match *match; | |
267 | const GSList *l, *m; | |
268 | uint16_t channel_bit; | |
269 | ||
270 | devc = sdi->priv; | |
271 | devc->trigger_pattern = 0x0000; /* Default to "low" trigger. */ | |
272 | devc->trigger_mask = 0x0000; /* Default to "don't care". */ | |
273 | devc->trigger_edgemask = 0x0000; /* Default to "state triggered". */ | |
274 | ||
275 | if (!(trigger = sr_session_trigger_get(sdi->session))) | |
276 | return SR_OK; | |
277 | ||
278 | if (g_slist_length(trigger->stages) > 1) { | |
279 | sr_err("This device only supports 1 trigger stage."); | |
280 | return SR_ERR; | |
281 | } | |
282 | ||
283 | for (l = trigger->stages; l; l = l->next) { | |
284 | stage = l->data; | |
285 | for (m = stage->matches; m; m = m->next) { | |
286 | match = m->data; | |
287 | if (!match->channel->enabled) | |
288 | /* Ignore disabled channels with a trigger. */ | |
289 | continue; | |
290 | if (devc->prof->model == CHRONOVU_LA8 && | |
291 | (match->match == SR_TRIGGER_RISING | |
292 | || match->match == SR_TRIGGER_FALLING)) { | |
293 | sr_err("This model supports only simple triggers."); | |
294 | return SR_ERR; | |
295 | } | |
296 | channel_bit = (1 << (match->channel->index)); | |
297 | ||
298 | /* state: 1 == high, edge: 1 == rising edge. */ | |
299 | if (match->match == SR_TRIGGER_ONE | |
300 | || match->match == SR_TRIGGER_RISING) | |
301 | devc->trigger_pattern |= channel_bit; | |
302 | ||
303 | /* LA8 and LA16 support state triggering. */ | |
304 | if (match->match == SR_TRIGGER_ONE | |
305 | || match->match == SR_TRIGGER_ZERO) | |
306 | devc->trigger_mask |= channel_bit; | |
307 | ||
308 | /* LA16 (but not LA8) supports edge triggering. */ | |
309 | if ((devc->prof->model == CHRONOVU_LA16)) { | |
310 | if (match->match == SR_TRIGGER_RISING | |
311 | || match->match == SR_TRIGGER_FALLING) | |
312 | devc->trigger_edgemask |= channel_bit; | |
313 | } | |
314 | } | |
315 | } | |
316 | ||
317 | sr_dbg("Trigger pattern/mask/edgemask = 0x%04x / 0x%04x / 0x%04x.", | |
318 | devc->trigger_pattern, devc->trigger_mask, | |
319 | devc->trigger_edgemask); | |
320 | ||
321 | return SR_OK; | |
322 | } | |
323 | ||
324 | SR_PRIV int cv_set_samplerate(const struct sr_dev_inst *sdi, uint64_t samplerate) | |
325 | { | |
326 | struct dev_context *devc; | |
327 | ||
328 | /* Note: Caller checked that sdi and sdi->priv != NULL. */ | |
329 | ||
330 | devc = sdi->priv; | |
331 | ||
332 | sr_spew("Trying to set samplerate to %" PRIu64 "Hz.", samplerate); | |
333 | ||
334 | cv_fill_samplerates_if_needed(sdi); | |
335 | ||
336 | /* Check if this is a samplerate supported by the hardware. */ | |
337 | if (!is_valid_samplerate(sdi, samplerate)) { | |
338 | sr_dbg("Failed to set invalid samplerate (%" PRIu64 "Hz).", | |
339 | samplerate); | |
340 | return SR_ERR; | |
341 | } | |
342 | ||
343 | devc->cur_samplerate = samplerate; | |
344 | ||
345 | sr_dbg("Samplerate set to %" PRIu64 "Hz.", devc->cur_samplerate); | |
346 | ||
347 | return SR_OK; | |
348 | } | |
349 | ||
350 | /** | |
351 | * Get a block of data from the device. | |
352 | * | |
353 | * @param devc The struct containing private per-device-instance data. Must not | |
354 | * be NULL. devc->ftdic must not be NULL either. | |
355 | * | |
356 | * @return SR_OK upon success, or SR_ERR upon errors. | |
357 | */ | |
358 | SR_PRIV int cv_read_block(struct dev_context *devc) | |
359 | { | |
360 | int i, byte_offset, m, mi, p, q, index, bytes_read; | |
361 | gint64 now; | |
362 | ||
363 | /* Note: Caller checked that devc and devc->ftdic != NULL. */ | |
364 | ||
365 | sr_spew("Reading block %d.", devc->block_counter); | |
366 | ||
367 | bytes_read = cv_read(devc, devc->mangled_buf, BS); | |
368 | ||
369 | /* If first block read got 0 bytes, retry until success or timeout. */ | |
370 | if ((bytes_read == 0) && (devc->block_counter == 0)) { | |
371 | do { | |
372 | sr_spew("Reading block 0 (again)."); | |
373 | /* Note: If bytes_read < 0 cv_read() will log errors. */ | |
374 | bytes_read = cv_read(devc, devc->mangled_buf, BS); | |
375 | now = g_get_monotonic_time(); | |
376 | } while ((devc->done > now) && (bytes_read == 0)); | |
377 | } | |
378 | ||
379 | /* Check if block read was successful or a timeout occurred. */ | |
380 | if (bytes_read != BS) { | |
381 | sr_err("Trigger timed out. Bytes read: %d.", bytes_read); | |
382 | (void) reset_device(devc); /* Ignore errors. */ | |
383 | return SR_ERR; | |
384 | } | |
385 | ||
386 | /* De-mangle the data. */ | |
387 | sr_spew("Demangling block %d.", devc->block_counter); | |
388 | byte_offset = devc->block_counter * BS; | |
389 | m = byte_offset / (1024 * 1024); | |
390 | mi = m * (1024 * 1024); | |
391 | for (i = 0; i < BS; i++) { | |
392 | if (devc->prof->model == CHRONOVU_LA8) { | |
393 | p = i & (1 << 0); | |
394 | index = m * 2 + (((byte_offset + i) - mi) / 2) * 16; | |
395 | index += (devc->divcount == 0) ? p : (1 - p); | |
396 | } else { | |
397 | p = i & (1 << 0); | |
398 | q = i & (1 << 1); | |
399 | index = m * 4 + (((byte_offset + i) - mi) / 4) * 32; | |
400 | index += q + (1 - p); | |
401 | } | |
402 | devc->final_buf[index] = devc->mangled_buf[i]; | |
403 | } | |
404 | ||
405 | return SR_OK; | |
406 | } | |
407 | ||
408 | SR_PRIV void cv_send_block_to_session_bus(const struct sr_dev_inst *sdi, int block) | |
409 | { | |
410 | int i, idx; | |
411 | uint8_t sample, expected_sample, tmp8; | |
412 | struct sr_datafeed_packet packet; | |
413 | struct sr_datafeed_logic logic; | |
414 | int trigger_point; /* Relative trigger point (in this block). */ | |
415 | struct dev_context *devc; | |
416 | ||
417 | /* Note: Caller ensures devc/devc->ftdic != NULL and block > 0. */ | |
418 | ||
419 | devc = sdi->priv; | |
420 | ||
421 | /* TODO: Implement/test proper trigger support for the LA16. */ | |
422 | ||
423 | /* Check if we can find the trigger condition in this block. */ | |
424 | trigger_point = -1; | |
425 | expected_sample = devc->trigger_pattern & devc->trigger_mask; | |
426 | for (i = 0; i < BS; i++) { | |
427 | /* Don't continue if the trigger was found previously. */ | |
428 | if (devc->trigger_found) | |
429 | break; | |
430 | ||
431 | /* | |
432 | * Also, don't continue if triggers are "don't care", i.e. if | |
433 | * no trigger conditions were specified by the user. In that | |
434 | * case we don't want to send an SR_DF_TRIGGER packet at all. | |
435 | */ | |
436 | if (devc->trigger_mask == 0x0000) | |
437 | break; | |
438 | ||
439 | sample = *(devc->final_buf + (block * BS) + i); | |
440 | ||
441 | if ((sample & devc->trigger_mask) == expected_sample) { | |
442 | trigger_point = i; | |
443 | devc->trigger_found = 1; | |
444 | break; | |
445 | } | |
446 | } | |
447 | ||
448 | /* Swap low and high bytes of the 16-bit LA16 samples. */ | |
449 | if (devc->prof->model == CHRONOVU_LA16) { | |
450 | for (i = 0; i < BS; i += 2) { | |
451 | idx = (block * BS) + i; | |
452 | tmp8 = devc->final_buf[idx]; | |
453 | devc->final_buf[idx] = devc->final_buf[idx + 1]; | |
454 | devc->final_buf[idx + 1] = tmp8; | |
455 | } | |
456 | } | |
457 | ||
458 | /* If no trigger was found, send one SR_DF_LOGIC packet. */ | |
459 | if (trigger_point == -1) { | |
460 | /* Send an SR_DF_LOGIC packet to the session bus. */ | |
461 | sr_spew("Sending SR_DF_LOGIC packet (%d bytes) for " | |
462 | "block %d.", BS, block); | |
463 | packet.type = SR_DF_LOGIC; | |
464 | packet.payload = &logic; | |
465 | logic.length = BS; | |
466 | logic.unitsize = devc->prof->num_channels / 8; | |
467 | logic.data = devc->final_buf + (block * BS); | |
468 | sr_session_send(sdi, &packet); | |
469 | return; | |
470 | } | |
471 | ||
472 | /* | |
473 | * We found the trigger, so some special handling is needed. We have | |
474 | * to send an SR_DF_LOGIC packet with the samples before the trigger | |
475 | * (if any), then the SD_DF_TRIGGER packet itself, then another | |
476 | * SR_DF_LOGIC packet with the samples after the trigger (if any). | |
477 | */ | |
478 | ||
479 | /* TODO: Send SR_DF_TRIGGER packet before or after the actual sample? */ | |
480 | ||
481 | /* If at least one sample is located before the trigger... */ | |
482 | if (trigger_point > 0) { | |
483 | /* Send pre-trigger SR_DF_LOGIC packet to the session bus. */ | |
484 | sr_spew("Sending pre-trigger SR_DF_LOGIC packet, " | |
485 | "start = %d, length = %d.", block * BS, trigger_point); | |
486 | packet.type = SR_DF_LOGIC; | |
487 | packet.payload = &logic; | |
488 | logic.length = trigger_point; | |
489 | logic.unitsize = devc->prof->num_channels / 8; | |
490 | logic.data = devc->final_buf + (block * BS); | |
491 | sr_session_send(sdi, &packet); | |
492 | } | |
493 | ||
494 | /* Send the SR_DF_TRIGGER packet to the session bus. */ | |
495 | sr_spew("Sending SR_DF_TRIGGER packet, sample = %d.", | |
496 | (block * BS) + trigger_point); | |
497 | packet.type = SR_DF_TRIGGER; | |
498 | packet.payload = NULL; | |
499 | sr_session_send(sdi, &packet); | |
500 | ||
501 | /* If at least one sample is located after the trigger... */ | |
502 | if (trigger_point < (BS - 1)) { | |
503 | /* Send post-trigger SR_DF_LOGIC packet to the session bus. */ | |
504 | sr_spew("Sending post-trigger SR_DF_LOGIC packet, " | |
505 | "start = %d, length = %d.", | |
506 | (block * BS) + trigger_point, BS - trigger_point); | |
507 | packet.type = SR_DF_LOGIC; | |
508 | packet.payload = &logic; | |
509 | logic.length = BS - trigger_point; | |
510 | logic.unitsize = devc->prof->num_channels / 8; | |
511 | logic.data = devc->final_buf + (block * BS) + trigger_point; | |
512 | sr_session_send(sdi, &packet); | |
513 | } | |
514 | } |