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
23 SR_PRIV const struct cv_profile cv_profiles[] = {
24 { CHRONOVU_LA8, "LA8", "ChronoVu LA8", 8, SR_MHZ(100), "01",
26 { CHRONOVU_LA16, "LA16", "ChronoVu LA16", 16, SR_MHZ(200), "01rf",
28 { 0, NULL, NULL, 0, 0, NULL, 0.0 },
31 /* LA8: channels are numbered 0-7. LA16: channels are numbered 0-15. */
32 SR_PRIV const char *cv_channel_names[] = {
33 "0", "1", "2", "3", "4", "5", "6", "7",
34 "8", "9", "10", "11", "12", "13", "14", "15",
37 static int close_usb_reset_sequencer(struct dev_context *devc);
39 SR_PRIV void cv_fill_samplerates_if_needed(const struct sr_dev_inst *sdi)
42 struct dev_context *devc;
46 if (devc->samplerates[0] != 0)
49 for (i = 0; i < 255; i++)
50 devc->samplerates[254 - i] = devc->prof->max_samplerate / (i + 1);
54 * Check if the given samplerate is supported by the hardware.
56 * @param sdi Device instance.
57 * @param samplerate The samplerate (in Hz) to check.
59 * @return 1 if the samplerate is supported/valid, 0 otherwise.
61 static int is_valid_samplerate(const struct sr_dev_inst *sdi,
65 struct dev_context *devc;
69 cv_fill_samplerates_if_needed(sdi);
71 for (i = 0; i < 255; i++) {
72 if (devc->samplerates[i] == samplerate)
76 sr_err("Invalid samplerate (%" PRIu64 "Hz).", samplerate);
82 * Convert a samplerate (in Hz) to the 'divcount' value the device wants.
84 * The divcount value can be 0x00 - 0xfe (0xff is not valid).
87 * sample period = (divcount + 1) * 10ns.
88 * divcount = 0x00: 10ns period, 100MHz samplerate.
89 * divcount = 0xfe: 2550ns period, 392.15kHz samplerate.
92 * sample period = (divcount + 1) * 5ns.
93 * divcount = 0x00: 5ns period, 200MHz samplerate.
94 * divcount = 0xfe: 1275ns period, ~784.31kHz samplerate.
96 * @param sdi Device instance.
97 * @param samplerate The samplerate in Hz.
99 * @return The divcount value as needed by the hardware, or 0xff upon errors.
101 SR_PRIV uint8_t cv_samplerate_to_divcount(const struct sr_dev_inst *sdi,
104 struct dev_context *devc;
108 if (samplerate == 0) {
109 sr_err("Can't convert invalid samplerate of 0 Hz.");
113 if (!is_valid_samplerate(sdi, samplerate)) {
114 sr_err("Can't get divcount, samplerate invalid.");
118 return (devc->prof->max_samplerate / samplerate) - 1;
122 * Write data of a certain length to the FTDI device.
124 * @param devc The struct containing private per-device-instance data. Must not
125 * be NULL. devc->ftdic must not be NULL either.
126 * @param buf The buffer containing the data to write. Must not be NULL.
127 * @param size The number of bytes to write. Must be > 0.
129 * @return The number of bytes written, or a negative value upon errors.
131 SR_PRIV int cv_write(struct dev_context *devc, uint8_t *buf, int size)
135 /* Note: Caller ensures devc/devc->ftdic/buf != NULL and size > 0. */
137 bytes_written = ftdi_write_data(devc->ftdic, buf, size);
139 if (bytes_written < 0) {
140 sr_err("Failed to write data (%d): %s.",
141 bytes_written, ftdi_get_error_string(devc->ftdic));
142 (void) close_usb_reset_sequencer(devc); /* Ignore errors. */
143 } else if (bytes_written != size) {
144 sr_err("Failed to write data, only %d/%d bytes written.",
145 size, bytes_written);
146 (void) close_usb_reset_sequencer(devc); /* Ignore errors. */
149 return bytes_written;
153 * Read a certain amount of bytes from the FTDI device.
155 * @param devc The struct containing private per-device-instance data. Must not
156 * be NULL. devc->ftdic must not be NULL either.
157 * @param buf The buffer where the received data will be stored. Must not
159 * @param size The number of bytes to read. Must be >= 1.
161 * @return The number of bytes read, or a negative value upon errors.
163 static int cv_read(struct dev_context *devc, uint8_t *buf, int size)
167 /* Note: Caller ensures devc/devc->ftdic/buf != NULL and size > 0. */
169 bytes_read = ftdi_read_data(devc->ftdic, buf, size);
171 if (bytes_read < 0) {
172 sr_err("Failed to read data (%d): %s.",
173 bytes_read, ftdi_get_error_string(devc->ftdic));
174 } else if (bytes_read != size) {
175 // sr_err("Failed to read data, only %d/%d bytes read.",
176 // bytes_read, size);
183 * Close the USB port and reset the sequencer logic.
185 * @param devc The struct containing private per-device-instance data.
187 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
189 static int close_usb_reset_sequencer(struct dev_context *devc)
191 /* Magic sequence of bytes for resetting the sequencer logic. */
192 uint8_t buf[8] = {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};
195 /* Note: Caller checked that devc and devc->ftdic != NULL. */
197 if (devc->ftdic->usb_dev) {
198 /* Reset the sequencer logic, then wait 100ms. */
199 sr_dbg("Resetting sequencer logic.");
200 (void) cv_write(devc, buf, 8); /* Ignore errors. */
201 g_usleep(100 * 1000);
203 /* Purge FTDI buffers, then reset and close the FTDI device. */
204 sr_dbg("Purging buffers, resetting+closing FTDI device.");
206 /* Log errors, but ignore them (i.e., don't abort). */
207 if ((ret = ftdi_usb_purge_buffers(devc->ftdic)) < 0)
208 sr_err("Failed to purge FTDI buffers (%d): %s.",
209 ret, ftdi_get_error_string(devc->ftdic));
210 if ((ret = ftdi_usb_reset(devc->ftdic)) < 0)
211 sr_err("Failed to reset FTDI device (%d): %s.",
212 ret, ftdi_get_error_string(devc->ftdic));
213 if ((ret = ftdi_usb_close(devc->ftdic)) < 0)
214 sr_err("Failed to close FTDI device (%d): %s.",
215 ret, ftdi_get_error_string(devc->ftdic));
218 /* Close USB device, deinitialize and free the FTDI context. */
219 ftdi_free(devc->ftdic);
226 * Reset the ChronoVu device.
228 * A reset is required after a failed read/write operation or upon timeouts.
230 * @param devc The struct containing private per-device-instance data.
232 * @return SR_OK upon success, SR_ERR upon failure.
234 static int reset_device(struct dev_context *devc)
240 /* Note: Caller checked that devc and devc->ftdic != NULL. */
242 sr_dbg("Resetting the device.");
245 * Purge pending read data from the FTDI hardware FIFO until
246 * no more data is left, or a timeout occurs (after 20s).
248 done = (20 * G_TIME_SPAN_SECOND) + g_get_monotonic_time();
250 /* Try to read bytes until none are left (or errors occur). */
251 bytes_read = cv_read(devc, (uint8_t *)&buf, BS);
252 now = g_get_monotonic_time();
253 } while ((done > now) && (bytes_read > 0));
255 /* Reset the sequencer logic and close the USB port. */
256 (void) close_usb_reset_sequencer(devc); /* Ignore errors. */
258 sr_dbg("Device reset finished.");
263 SR_PRIV int cv_configure_channels(const struct sr_dev_inst *sdi)
265 struct dev_context *devc;
266 const struct sr_channel *ch;
268 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 for (l = sdi->channels; l; l = l->next) {
277 ch = (struct sr_channel *)l->data;
280 sr_err("%s: channel was NULL.", __func__);
284 /* Skip disabled channels. */
288 /* Skip (enabled) channels with no configured trigger. */
292 /* Note: Must only be run if ch->trigger != NULL. */
293 if (ch->index < 0 || ch->index > (int)devc->prof->num_channels - 1) {
294 sr_err("Invalid channel index %d, must be "
295 "between 0 and %d.", ch->index,
296 devc->prof->num_channels - 1);
300 channel_bit = (1 << (ch->index));
302 /* Configure the channel's trigger pattern/mask/edgemask. */
303 for (tc = ch->trigger; tc && *tc; tc++) {
304 devc->trigger_mask |= channel_bit;
306 /* Sanity check, LA8 only supports low/high trigger. */
307 if ((devc->prof->model == CHRONOVU_LA8) &&
308 (*tc != '0' && *tc != '1')) {
309 sr_err("Invalid trigger '%c', only "
310 "'0'/'1' supported.", *tc);
314 /* state: 1 == high, edge: 1 == rising edge. */
315 if (*tc == '1' || *tc == 'r')
316 devc->trigger_pattern |= channel_bit;
318 /* LA16 (but not LA8) supports edge triggering. */
319 if ((devc->prof->model == CHRONOVU_LA16)) {
320 if (*tc == 'r' || *tc == 'f')
321 devc->trigger_edgemask |= channel_bit;
327 sr_dbg("Trigger pattern/mask/edgemask = 0x%04x / 0x%04x / 0x%04x.",
328 devc->trigger_pattern, devc->trigger_mask,
329 devc->trigger_edgemask);
334 SR_PRIV int cv_set_samplerate(const struct sr_dev_inst *sdi, uint64_t samplerate)
336 struct dev_context *devc;
338 /* Note: Caller checked that sdi and sdi->priv != NULL. */
342 sr_spew("Trying to set samplerate to %" PRIu64 "Hz.", samplerate);
344 cv_fill_samplerates_if_needed(sdi);
346 /* Check if this is a samplerate supported by the hardware. */
347 if (!is_valid_samplerate(sdi, samplerate)) {
348 sr_dbg("Failed to set invalid samplerate (%" PRIu64 "Hz).",
353 devc->cur_samplerate = samplerate;
355 sr_dbg("Samplerate set to %" PRIu64 "Hz.", devc->cur_samplerate);
361 * Get a block of data from the device.
363 * @param devc The struct containing private per-device-instance data. Must not
364 * be NULL. devc->ftdic must not be NULL either.
366 * @return SR_OK upon success, or SR_ERR upon errors.
368 SR_PRIV int cv_read_block(struct dev_context *devc)
370 int i, byte_offset, m, mi, p, q, index, bytes_read;
373 /* Note: Caller checked that devc and devc->ftdic != NULL. */
375 sr_spew("Reading block %d.", devc->block_counter);
377 bytes_read = cv_read(devc, devc->mangled_buf, BS);
379 /* If first block read got 0 bytes, retry until success or timeout. */
380 if ((bytes_read == 0) && (devc->block_counter == 0)) {
382 sr_spew("Reading block 0 (again).");
383 /* Note: If bytes_read < 0 cv_read() will log errors. */
384 bytes_read = cv_read(devc, devc->mangled_buf, BS);
385 now = g_get_monotonic_time();
386 } while ((devc->done > now) && (bytes_read == 0));
389 /* Check if block read was successful or a timeout occured. */
390 if (bytes_read != BS) {
391 sr_err("Trigger timed out. Bytes read: %d.", bytes_read);
392 (void) reset_device(devc); /* Ignore errors. */
396 /* De-mangle the data. */
397 sr_spew("Demangling block %d.", devc->block_counter);
398 byte_offset = devc->block_counter * BS;
399 m = byte_offset / (1024 * 1024);
400 mi = m * (1024 * 1024);
401 for (i = 0; i < BS; i++) {
402 if (devc->prof->model == CHRONOVU_LA8) {
404 index = m * 2 + (((byte_offset + i) - mi) / 2) * 16;
405 index += (devc->divcount == 0) ? p : (1 - p);
409 index = m * 4 + (((byte_offset + i) - mi) / 4) * 32;
410 index += q + (1 - p);
412 devc->final_buf[index] = devc->mangled_buf[i];
418 SR_PRIV void cv_send_block_to_session_bus(struct dev_context *devc, int block)
421 uint8_t sample, expected_sample, tmp8;
422 struct sr_datafeed_packet packet;
423 struct sr_datafeed_logic logic;
424 int trigger_point; /* Relative trigger point (in this block). */
426 /* Note: Caller ensures devc/devc->ftdic != NULL and block > 0. */
428 /* TODO: Implement/test proper trigger support for the LA16. */
430 /* Check if we can find the trigger condition in this block. */
432 expected_sample = devc->trigger_pattern & devc->trigger_mask;
433 for (i = 0; i < BS; i++) {
434 /* Don't continue if the trigger was found previously. */
435 if (devc->trigger_found)
439 * Also, don't continue if triggers are "don't care", i.e. if
440 * no trigger conditions were specified by the user. In that
441 * case we don't want to send an SR_DF_TRIGGER packet at all.
443 if (devc->trigger_mask == 0x0000)
446 sample = *(devc->final_buf + (block * BS) + i);
448 if ((sample & devc->trigger_mask) == expected_sample) {
450 devc->trigger_found = 1;
455 /* Swap low and high bytes of the 16-bit LA16 samples. */
456 if (devc->prof->model == CHRONOVU_LA16) {
457 for (i = 0; i < BS; i += 2) {
458 idx = (block * BS) + i;
459 tmp8 = devc->final_buf[idx];
460 devc->final_buf[idx] = devc->final_buf[idx + 1];
461 devc->final_buf[idx + 1] = tmp8;
465 /* If no trigger was found, send one SR_DF_LOGIC packet. */
466 if (trigger_point == -1) {
467 /* Send an SR_DF_LOGIC packet to the session bus. */
468 sr_spew("Sending SR_DF_LOGIC packet (%d bytes) for "
469 "block %d.", BS, block);
470 packet.type = SR_DF_LOGIC;
471 packet.payload = &logic;
473 logic.unitsize = devc->prof->num_channels / 8;
474 logic.data = devc->final_buf + (block * BS);
475 sr_session_send(devc->cb_data, &packet);
480 * We found the trigger, so some special handling is needed. We have
481 * to send an SR_DF_LOGIC packet with the samples before the trigger
482 * (if any), then the SD_DF_TRIGGER packet itself, then another
483 * SR_DF_LOGIC packet with the samples after the trigger (if any).
486 /* TODO: Send SR_DF_TRIGGER packet before or after the actual sample? */
488 /* If at least one sample is located before the trigger... */
489 if (trigger_point > 0) {
490 /* Send pre-trigger SR_DF_LOGIC packet to the session bus. */
491 sr_spew("Sending pre-trigger SR_DF_LOGIC packet, "
492 "start = %d, length = %d.", block * BS, trigger_point);
493 packet.type = SR_DF_LOGIC;
494 packet.payload = &logic;
495 logic.length = trigger_point;
496 logic.unitsize = devc->prof->num_channels / 8;
497 logic.data = devc->final_buf + (block * BS);
498 sr_session_send(devc->cb_data, &packet);
501 /* Send the SR_DF_TRIGGER packet to the session bus. */
502 sr_spew("Sending SR_DF_TRIGGER packet, sample = %d.",
503 (block * BS) + trigger_point);
504 packet.type = SR_DF_TRIGGER;
505 packet.payload = NULL;
506 sr_session_send(devc->cb_data, &packet);
508 /* If at least one sample is located after the trigger... */
509 if (trigger_point < (BS - 1)) {
510 /* Send post-trigger SR_DF_LOGIC packet to the session bus. */
511 sr_spew("Sending post-trigger SR_DF_LOGIC packet, "
512 "start = %d, length = %d.",
513 (block * BS) + trigger_point, BS - trigger_point);
514 packet.type = SR_DF_LOGIC;
515 packet.payload = &logic;
516 logic.length = BS - trigger_point;
517 logic.unitsize = devc->prof->num_channels / 8;
518 logic.data = devc->final_buf + (block * BS) + trigger_point;
519 sr_session_send(devc->cb_data, &packet);