2 * This file is part of the libsigrok project.
4 * Copyright (C) 2011-2014 Uwe Hermann <uwe@hermann-uwe.de>
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.
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.
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
24 SR_PRIV const struct cv_profile cv_profiles[] = {
25 { CHRONOVU_LA8, "LA8", "ChronoVu LA8", 8, SR_MHZ(100), 2, 0.8388608 },
26 { CHRONOVU_LA16, "LA16", "ChronoVu LA16", 16, SR_MHZ(200), 4, 0.042 },
27 { 0, NULL, NULL, 0, 0, 0, 0.0 },
30 /* LA8: channels are numbered 0-7. LA16: channels are numbered 0-15. */
31 SR_PRIV const char *cv_channel_names[] = {
32 "0", "1", "2", "3", "4", "5", "6", "7",
33 "8", "9", "10", "11", "12", "13", "14", "15",
36 static int close_usb_reset_sequencer(struct dev_context *devc);
38 SR_PRIV void cv_fill_samplerates_if_needed(const struct sr_dev_inst *sdi)
41 struct dev_context *devc;
45 if (devc->samplerates[0] != 0)
48 for (i = 0; i < 255; i++)
49 devc->samplerates[254 - i] = devc->prof->max_samplerate / (i + 1);
53 * Check if the given samplerate is supported by the hardware.
55 * @param sdi Device instance.
56 * @param samplerate The samplerate (in Hz) to check.
58 * @return 1 if the samplerate is supported/valid, 0 otherwise.
60 static int is_valid_samplerate(const struct sr_dev_inst *sdi,
64 struct dev_context *devc;
68 cv_fill_samplerates_if_needed(sdi);
70 for (i = 0; i < 255; i++) {
71 if (devc->samplerates[i] == samplerate)
75 sr_err("Invalid samplerate (%" PRIu64 "Hz).", samplerate);
81 * Convert a samplerate (in Hz) to the 'divcount' value the device wants.
83 * The divcount value can be 0x00 - 0xfe (0xff is not valid).
86 * sample period = (divcount + 1) * 10ns.
87 * divcount = 0x00: 10ns period, 100MHz samplerate.
88 * divcount = 0xfe: 2550ns period, 392.15kHz samplerate.
91 * sample period = (divcount + 1) * 5ns.
92 * divcount = 0x00: 5ns period, 200MHz samplerate.
93 * divcount = 0xfe: 1275ns period, ~784.31kHz samplerate.
95 * @param sdi Device instance.
96 * @param samplerate The samplerate in Hz.
98 * @return The divcount value as needed by the hardware, or 0xff upon errors.
100 SR_PRIV uint8_t cv_samplerate_to_divcount(const struct sr_dev_inst *sdi,
103 struct dev_context *devc;
107 if (samplerate == 0) {
108 sr_err("Can't convert invalid samplerate of 0 Hz.");
112 if (!is_valid_samplerate(sdi, samplerate)) {
113 sr_err("Can't get divcount, samplerate invalid.");
117 return (devc->prof->max_samplerate / samplerate) - 1;
121 * Write data of a certain length to the FTDI device.
123 * @param devc The struct containing private per-device-instance data. Must not
124 * be NULL. devc->ftdic must not be NULL either.
125 * @param buf The buffer containing the data to write. Must not be NULL.
126 * @param size The number of bytes to write. Must be > 0.
128 * @return The number of bytes written, or a negative value upon errors.
130 SR_PRIV int cv_write(struct dev_context *devc, uint8_t *buf, int size)
134 /* Note: Caller ensures devc/devc->ftdic/buf != NULL and size > 0. */
136 bytes_written = ftdi_write_data(devc->ftdic, buf, size);
138 if (bytes_written < 0) {
139 sr_err("Failed to write data (%d): %s.",
140 bytes_written, ftdi_get_error_string(devc->ftdic));
141 (void) close_usb_reset_sequencer(devc); /* Ignore errors. */
142 } else if (bytes_written != size) {
143 sr_err("Failed to write data, only %d/%d bytes written.",
144 size, bytes_written);
145 (void) close_usb_reset_sequencer(devc); /* Ignore errors. */
148 return bytes_written;
152 * Read a certain amount of bytes from the FTDI device.
154 * @param devc The struct containing private per-device-instance data. Must not
155 * be NULL. devc->ftdic must not be NULL either.
156 * @param buf The buffer where the received data will be stored. Must not
158 * @param size The number of bytes to read. Must be >= 1.
160 * @return The number of bytes read, or a negative value upon errors.
162 static int cv_read(struct dev_context *devc, uint8_t *buf, int size)
166 /* Note: Caller ensures devc/devc->ftdic/buf != NULL and size > 0. */
168 bytes_read = ftdi_read_data(devc->ftdic, buf, size);
170 if (bytes_read < 0) {
171 sr_err("Failed to read data (%d): %s.",
172 bytes_read, ftdi_get_error_string(devc->ftdic));
173 } else if (bytes_read != size) {
174 // sr_err("Failed to read data, only %d/%d bytes read.",
175 // bytes_read, size);
182 * Close the USB port and reset the sequencer logic.
184 * @param devc The struct containing private per-device-instance data.
186 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
188 static int close_usb_reset_sequencer(struct dev_context *devc)
190 /* Magic sequence of bytes for resetting the sequencer logic. */
191 uint8_t buf[8] = {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};
194 /* Note: Caller checked that devc and devc->ftdic != NULL. */
196 if (devc->ftdic->usb_dev) {
197 /* Reset the sequencer logic, then wait 100ms. */
198 sr_dbg("Resetting sequencer logic.");
199 (void) cv_write(devc, buf, 8); /* Ignore errors. */
200 g_usleep(100 * 1000);
202 /* Purge FTDI buffers, then reset and close the FTDI device. */
203 sr_dbg("Purging buffers, resetting+closing FTDI device.");
205 /* Log errors, but ignore them (i.e., don't abort). */
206 if ((ret = ftdi_usb_purge_buffers(devc->ftdic)) < 0)
207 sr_err("Failed to purge FTDI buffers (%d): %s.",
208 ret, ftdi_get_error_string(devc->ftdic));
209 if ((ret = ftdi_usb_reset(devc->ftdic)) < 0)
210 sr_err("Failed to reset FTDI device (%d): %s.",
211 ret, ftdi_get_error_string(devc->ftdic));
212 if ((ret = ftdi_usb_close(devc->ftdic)) < 0)
213 sr_err("Failed to close FTDI device (%d): %s.",
214 ret, ftdi_get_error_string(devc->ftdic));
217 /* Close USB device, deinitialize and free the FTDI context. */
218 ftdi_free(devc->ftdic);
225 * Reset the ChronoVu device.
227 * A reset is required after a failed read/write operation or upon timeouts.
229 * @param devc The struct containing private per-device-instance data.
231 * @return SR_OK upon success, SR_ERR upon failure.
233 static int reset_device(struct dev_context *devc)
239 /* Note: Caller checked that devc and devc->ftdic != NULL. */
241 sr_dbg("Resetting the device.");
244 * Purge pending read data from the FTDI hardware FIFO until
245 * no more data is left, or a timeout occurs (after 20s).
247 done = (20 * G_TIME_SPAN_SECOND) + g_get_monotonic_time();
249 /* Try to read bytes until none are left (or errors occur). */
250 bytes_read = cv_read(devc, (uint8_t *)&buf, BS);
251 now = g_get_monotonic_time();
252 } while ((done > now) && (bytes_read > 0));
254 /* Reset the sequencer logic and close the USB port. */
255 (void) close_usb_reset_sequencer(devc); /* Ignore errors. */
257 sr_dbg("Device reset finished.");
262 SR_PRIV int cv_convert_trigger(const struct sr_dev_inst *sdi)
264 struct dev_context *devc;
265 struct sr_trigger *trigger;
266 struct sr_trigger_stage *stage;
267 struct sr_trigger_match *match;
269 uint16_t channel_bit;
272 devc->trigger_pattern = 0x0000; /* Default to "low" trigger. */
273 devc->trigger_mask = 0x0000; /* Default to "don't care". */
274 devc->trigger_edgemask = 0x0000; /* Default to "state triggered". */
276 if (!(trigger = sr_session_trigger_get(sdi->session)))
279 if (g_slist_length(trigger->stages) > 1) {
280 sr_err("This device only supports 1 trigger stage.");
284 for (l = trigger->stages; l; l = l->next) {
286 for (m = stage->matches; m; m = m->next) {
288 if (!match->channel->enabled)
289 /* Ignore disabled channels with a trigger. */
291 if (devc->prof->model == CHRONOVU_LA8 &&
292 (match->match == SR_TRIGGER_RISING
293 || match->match == SR_TRIGGER_FALLING)) {
294 sr_err("This model supports only simple triggers.");
297 channel_bit = (1 << (match->channel->index));
299 /* state: 1 == high, edge: 1 == rising edge. */
300 if (match->match == SR_TRIGGER_ONE
301 || match->match == SR_TRIGGER_RISING)
302 devc->trigger_pattern |= channel_bit;
304 /* LA16 (but not LA8) supports edge triggering. */
305 if ((devc->prof->model == CHRONOVU_LA16)) {
306 if (match->match == SR_TRIGGER_RISING
307 || match->match == SR_TRIGGER_FALLING)
308 devc->trigger_edgemask |= channel_bit;
313 sr_dbg("Trigger pattern/mask/edgemask = 0x%04x / 0x%04x / 0x%04x.",
314 devc->trigger_pattern, devc->trigger_mask,
315 devc->trigger_edgemask);
320 SR_PRIV int cv_set_samplerate(const struct sr_dev_inst *sdi, uint64_t samplerate)
322 struct dev_context *devc;
324 /* Note: Caller checked that sdi and sdi->priv != NULL. */
328 sr_spew("Trying to set samplerate to %" PRIu64 "Hz.", samplerate);
330 cv_fill_samplerates_if_needed(sdi);
332 /* Check if this is a samplerate supported by the hardware. */
333 if (!is_valid_samplerate(sdi, samplerate)) {
334 sr_dbg("Failed to set invalid samplerate (%" PRIu64 "Hz).",
339 devc->cur_samplerate = samplerate;
341 sr_dbg("Samplerate set to %" PRIu64 "Hz.", devc->cur_samplerate);
347 * Get a block of data from the device.
349 * @param devc The struct containing private per-device-instance data. Must not
350 * be NULL. devc->ftdic must not be NULL either.
352 * @return SR_OK upon success, or SR_ERR upon errors.
354 SR_PRIV int cv_read_block(struct dev_context *devc)
356 int i, byte_offset, m, mi, p, q, index, bytes_read;
359 /* Note: Caller checked that devc and devc->ftdic != NULL. */
361 sr_spew("Reading block %d.", devc->block_counter);
363 bytes_read = cv_read(devc, devc->mangled_buf, BS);
365 /* If first block read got 0 bytes, retry until success or timeout. */
366 if ((bytes_read == 0) && (devc->block_counter == 0)) {
368 sr_spew("Reading block 0 (again).");
369 /* Note: If bytes_read < 0 cv_read() will log errors. */
370 bytes_read = cv_read(devc, devc->mangled_buf, BS);
371 now = g_get_monotonic_time();
372 } while ((devc->done > now) && (bytes_read == 0));
375 /* Check if block read was successful or a timeout occurred. */
376 if (bytes_read != BS) {
377 sr_err("Trigger timed out. Bytes read: %d.", bytes_read);
378 (void) reset_device(devc); /* Ignore errors. */
382 /* De-mangle the data. */
383 sr_spew("Demangling block %d.", devc->block_counter);
384 byte_offset = devc->block_counter * BS;
385 m = byte_offset / (1024 * 1024);
386 mi = m * (1024 * 1024);
387 for (i = 0; i < BS; i++) {
388 if (devc->prof->model == CHRONOVU_LA8) {
390 index = m * 2 + (((byte_offset + i) - mi) / 2) * 16;
391 index += (devc->divcount == 0) ? p : (1 - p);
395 index = m * 4 + (((byte_offset + i) - mi) / 4) * 32;
396 index += q + (1 - p);
398 devc->final_buf[index] = devc->mangled_buf[i];
404 SR_PRIV void cv_send_block_to_session_bus(struct dev_context *devc, int block)
407 uint8_t sample, expected_sample, tmp8;
408 struct sr_datafeed_packet packet;
409 struct sr_datafeed_logic logic;
410 int trigger_point; /* Relative trigger point (in this block). */
412 /* Note: Caller ensures devc/devc->ftdic != NULL and block > 0. */
414 /* TODO: Implement/test proper trigger support for the LA16. */
416 /* Check if we can find the trigger condition in this block. */
418 expected_sample = devc->trigger_pattern & devc->trigger_mask;
419 for (i = 0; i < BS; i++) {
420 /* Don't continue if the trigger was found previously. */
421 if (devc->trigger_found)
425 * Also, don't continue if triggers are "don't care", i.e. if
426 * no trigger conditions were specified by the user. In that
427 * case we don't want to send an SR_DF_TRIGGER packet at all.
429 if (devc->trigger_mask == 0x0000)
432 sample = *(devc->final_buf + (block * BS) + i);
434 if ((sample & devc->trigger_mask) == expected_sample) {
436 devc->trigger_found = 1;
441 /* Swap low and high bytes of the 16-bit LA16 samples. */
442 if (devc->prof->model == CHRONOVU_LA16) {
443 for (i = 0; i < BS; i += 2) {
444 idx = (block * BS) + i;
445 tmp8 = devc->final_buf[idx];
446 devc->final_buf[idx] = devc->final_buf[idx + 1];
447 devc->final_buf[idx + 1] = tmp8;
451 /* If no trigger was found, send one SR_DF_LOGIC packet. */
452 if (trigger_point == -1) {
453 /* Send an SR_DF_LOGIC packet to the session bus. */
454 sr_spew("Sending SR_DF_LOGIC packet (%d bytes) for "
455 "block %d.", BS, block);
456 packet.type = SR_DF_LOGIC;
457 packet.payload = &logic;
459 logic.unitsize = devc->prof->num_channels / 8;
460 logic.data = devc->final_buf + (block * BS);
461 sr_session_send(devc->cb_data, &packet);
466 * We found the trigger, so some special handling is needed. We have
467 * to send an SR_DF_LOGIC packet with the samples before the trigger
468 * (if any), then the SD_DF_TRIGGER packet itself, then another
469 * SR_DF_LOGIC packet with the samples after the trigger (if any).
472 /* TODO: Send SR_DF_TRIGGER packet before or after the actual sample? */
474 /* If at least one sample is located before the trigger... */
475 if (trigger_point > 0) {
476 /* Send pre-trigger SR_DF_LOGIC packet to the session bus. */
477 sr_spew("Sending pre-trigger SR_DF_LOGIC packet, "
478 "start = %d, length = %d.", block * BS, trigger_point);
479 packet.type = SR_DF_LOGIC;
480 packet.payload = &logic;
481 logic.length = trigger_point;
482 logic.unitsize = devc->prof->num_channels / 8;
483 logic.data = devc->final_buf + (block * BS);
484 sr_session_send(devc->cb_data, &packet);
487 /* Send the SR_DF_TRIGGER packet to the session bus. */
488 sr_spew("Sending SR_DF_TRIGGER packet, sample = %d.",
489 (block * BS) + trigger_point);
490 packet.type = SR_DF_TRIGGER;
491 packet.payload = NULL;
492 sr_session_send(devc->cb_data, &packet);
494 /* If at least one sample is located after the trigger... */
495 if (trigger_point < (BS - 1)) {
496 /* Send post-trigger SR_DF_LOGIC packet to the session bus. */
497 sr_spew("Sending post-trigger SR_DF_LOGIC packet, "
498 "start = %d, length = %d.",
499 (block * BS) + trigger_point, BS - trigger_point);
500 packet.type = SR_DF_LOGIC;
501 packet.payload = &logic;
502 logic.length = BS - trigger_point;
503 logic.unitsize = devc->prof->num_channels / 8;
504 logic.data = devc->final_buf + (block * BS) + trigger_point;
505 sr_session_send(devc->cb_data, &packet);