2 * This file is part of the sigrok project.
4 * Copyright (C) 2011-2012 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 #include "libsigrok.h"
24 #include "libsigrok-internal.h"
27 /* Probes are numbered 0-7. */
28 SR_PRIV const char *probe_names[NUM_PROBES + 1] = {
29 "0", "1", "2", "3", "4", "5", "6", "7",
33 /* This will be initialized via config_list()/SR_CONF_SAMPLERATE. */
34 SR_PRIV uint64_t supported_samplerates[255 + 1] = { 0 };
37 * Min: 1 sample per 0.01us -> sample time is 0.084s, samplerate 100MHz
38 * Max: 1 sample per 2.55us -> sample time is 21.391s, samplerate 392.15kHz
40 const struct sr_samplerates samplerates = {
44 .list = supported_samplerates,
47 /* Note: Continuous sampling is not supported by the hardware. */
48 SR_PRIV const int hwcaps[] = {
49 SR_CONF_LOGIC_ANALYZER,
51 SR_CONF_LIMIT_MSEC, /* TODO: Not yet implemented. */
52 SR_CONF_LIMIT_SAMPLES, /* TODO: Not yet implemented. */
56 SR_PRIV void fill_supported_samplerates_if_needed(void)
60 /* Do nothing if supported_samplerates[] is already filled. */
61 if (supported_samplerates[0] != 0)
64 /* Fill supported_samplerates[] with the proper values. */
65 for (i = 0; i < 255; i++)
66 supported_samplerates[254 - i] = SR_MHZ(100) / (i + 1);
67 supported_samplerates[255] = 0;
71 * Check if the given samplerate is supported by the LA8 hardware.
73 * @param samplerate The samplerate (in Hz) to check.
74 * @return 1 if the samplerate is supported/valid, 0 otherwise.
76 SR_PRIV int is_valid_samplerate(uint64_t samplerate)
80 fill_supported_samplerates_if_needed();
82 for (i = 0; i < 255; i++) {
83 if (supported_samplerates[i] == samplerate)
87 sr_err("Invalid samplerate (%" PRIu64 "Hz).", samplerate);
93 * Convert a samplerate (in Hz) to the 'divcount' value the LA8 wants.
95 * LA8 hardware: sample period = (divcount + 1) * 10ns.
96 * Min. value for divcount: 0x00 (10ns sample period, 100MHz samplerate).
97 * Max. value for divcount: 0xfe (2550ns sample period, 392.15kHz samplerate).
99 * @param samplerate The samplerate in Hz.
100 * @return The divcount value as needed by the hardware, or 0xff upon errors.
102 SR_PRIV uint8_t samplerate_to_divcount(uint64_t samplerate)
104 if (samplerate == 0) {
105 sr_err("%s: samplerate was 0.", __func__);
109 if (!is_valid_samplerate(samplerate)) {
110 sr_err("%s: Can't get divcount, samplerate invalid.", __func__);
114 return (SR_MHZ(100) / samplerate) - 1;
118 * Write data of a certain length to the LA8's FTDI device.
120 * @param devc The struct containing private per-device-instance data. Must not
121 * be NULL. devc->ftdic must not be NULL either.
122 * @param buf The buffer containing the data to write. Must not be NULL.
123 * @param size The number of bytes to write. Must be >= 0.
124 * @return The number of bytes written, or a negative value upon errors.
126 SR_PRIV int la8_write(struct dev_context *devc, uint8_t *buf, int size)
130 /* Note: Caller checked that devc and devc->ftdic != NULL. */
133 sr_err("%s: buf was NULL.", __func__);
138 sr_err("%s: size was < 0.", __func__);
142 bytes_written = ftdi_write_data(devc->ftdic, buf, size);
144 if (bytes_written < 0) {
145 sr_err("%s: ftdi_write_data: (%d) %s.", __func__,
146 bytes_written, ftdi_get_error_string(devc->ftdic));
147 (void) la8_close_usb_reset_sequencer(devc); /* Ignore errors. */
148 } else if (bytes_written != size) {
149 sr_err("%s: bytes to write: %d, bytes written: %d.",
150 __func__, size, bytes_written);
151 (void) la8_close_usb_reset_sequencer(devc); /* Ignore errors. */
154 return bytes_written;
158 * Read a certain amount of bytes from the LA8's FTDI device.
160 * @param devc The struct containing private per-device-instance data. Must not
161 * be NULL. devc->ftdic must not be NULL either.
162 * @param buf The buffer where the received data will be stored. Must not
164 * @param size The number of bytes to read. Must be >= 1.
165 * @return The number of bytes read, or a negative value upon errors.
167 SR_PRIV int la8_read(struct dev_context *devc, uint8_t *buf, int size)
171 /* Note: Caller checked that devc and devc->ftdic != NULL. */
174 sr_err("%s: buf was NULL.", __func__);
179 sr_err("%s: size was <= 0.", __func__);
183 bytes_read = ftdi_read_data(devc->ftdic, buf, size);
185 if (bytes_read < 0) {
186 sr_err("%s: ftdi_read_data: (%d) %s.", __func__,
187 bytes_read, ftdi_get_error_string(devc->ftdic));
188 } else if (bytes_read != size) {
189 // sr_err("%s: Bytes to read: %d, bytes read: %d.",
190 // __func__, size, bytes_read);
196 SR_PRIV int la8_close(struct dev_context *devc)
201 sr_err("%s: devc was NULL.", __func__);
206 sr_err("%s: devc->ftdic was NULL.", __func__);
210 if ((ret = ftdi_usb_close(devc->ftdic)) < 0) {
211 sr_err("%s: ftdi_usb_close: (%d) %s.",
212 __func__, ret, ftdi_get_error_string(devc->ftdic));
219 * Close the ChronoVu LA8 USB port and reset the LA8 sequencer logic.
221 * @param devc The struct containing private per-device-instance data.
222 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
224 SR_PRIV int la8_close_usb_reset_sequencer(struct dev_context *devc)
226 /* Magic sequence of bytes for resetting the LA8 sequencer logic. */
227 uint8_t buf[8] = {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};
231 sr_err("%s: devc was NULL.", __func__);
236 sr_err("%s: devc->ftdic was NULL.", __func__);
240 if (devc->ftdic->usb_dev) {
241 /* Reset the LA8 sequencer logic, then wait 100ms. */
242 sr_dbg("Resetting sequencer logic.");
243 (void) la8_write(devc, buf, 8); /* Ignore errors. */
244 g_usleep(100 * 1000);
246 /* Purge FTDI buffers, then reset and close the FTDI device. */
247 sr_dbg("Purging buffers, resetting+closing FTDI device.");
249 /* Log errors, but ignore them (i.e., don't abort). */
250 if ((ret = ftdi_usb_purge_buffers(devc->ftdic)) < 0)
251 sr_err("%s: ftdi_usb_purge_buffers: (%d) %s.",
252 __func__, ret, ftdi_get_error_string(devc->ftdic));
253 if ((ret = ftdi_usb_reset(devc->ftdic)) < 0)
254 sr_err("%s: ftdi_usb_reset: (%d) %s.", __func__,
255 ret, ftdi_get_error_string(devc->ftdic));
256 if ((ret = ftdi_usb_close(devc->ftdic)) < 0)
257 sr_err("%s: ftdi_usb_close: (%d) %s.", __func__,
258 ret, ftdi_get_error_string(devc->ftdic));
261 /* Close USB device, deinitialize and free the FTDI context. */
262 ftdi_free(devc->ftdic); /* Returns void. */
269 * Reset the ChronoVu LA8.
271 * The LA8 must be reset after a failed read/write operation or upon timeouts.
273 * @param devc The struct containing private per-device-instance data.
274 * @return SR_OK upon success, SR_ERR upon failure.
276 SR_PRIV int la8_reset(struct dev_context *devc)
283 sr_err("%s: devc was NULL.", __func__);
288 sr_err("%s: devc->ftdic was NULL.", __func__);
292 sr_dbg("Resetting the device.");
295 * Purge pending read data from the FTDI hardware FIFO until
296 * no more data is left, or a timeout occurs (after 20s).
298 done = 20 + time(NULL);
300 /* TODO: Ignore errors? Check for < 0 at least! */
301 bytes_read = la8_read(devc, (uint8_t *)&buf, BS);
303 } while ((done > now) && (bytes_read > 0));
305 /* Reset the LA8 sequencer logic and close the USB port. */
306 (void) la8_close_usb_reset_sequencer(devc); /* Ignore errors. */
308 sr_dbg("Device reset finished.");
313 SR_PRIV int configure_probes(const struct sr_dev_inst *sdi)
315 struct dev_context *devc;
316 const struct sr_probe *probe;
322 devc->trigger_pattern = 0;
323 devc->trigger_mask = 0; /* Default to "don't care" for all probes. */
325 for (l = sdi->probes; l; l = l->next) {
326 probe = (struct sr_probe *)l->data;
329 sr_err("%s: probe was NULL.", __func__);
333 /* Skip disabled probes. */
337 /* Skip (enabled) probes with no configured trigger. */
341 /* Note: Must only be run if probe->trigger != NULL. */
342 if (probe->index < 0 || probe->index > 7) {
343 sr_err("%s: Invalid probe index %d, must be "
344 "between 0 and 7.", __func__, probe->index);
348 probe_bit = (1 << (probe->index));
350 /* Configure the probe's trigger mask and trigger pattern. */
351 for (tc = probe->trigger; tc && *tc; tc++) {
352 devc->trigger_mask |= probe_bit;
354 /* Sanity check, LA8 only supports low/high trigger. */
355 if (*tc != '0' && *tc != '1') {
356 sr_err("%s: Invalid trigger '%c', only "
357 "'0'/'1' supported.", __func__, *tc);
362 devc->trigger_pattern |= probe_bit;
366 sr_dbg("Trigger mask = 0x%x, trigger pattern = 0x%x.",
367 devc->trigger_mask, devc->trigger_pattern);
372 SR_PRIV int set_samplerate(const struct sr_dev_inst *sdi, uint64_t samplerate)
374 struct dev_context *devc;
376 /* Note: Caller checked that sdi and sdi->priv != NULL. */
380 sr_spew("Trying to set samplerate to %" PRIu64 "Hz.", samplerate);
382 fill_supported_samplerates_if_needed();
384 /* Check if this is a samplerate supported by the hardware. */
385 if (!is_valid_samplerate(samplerate))
388 /* Set the new samplerate. */
389 devc->cur_samplerate = samplerate;
391 sr_dbg("Samplerate set to %" PRIu64 "Hz.", devc->cur_samplerate);
397 * Get a block of data from the LA8.
399 * @param devc The struct containing private per-device-instance data. Must not
400 * be NULL. devc->ftdic must not be NULL either.
401 * @return SR_OK upon success, or SR_ERR upon errors.
403 SR_PRIV int la8_read_block(struct dev_context *devc)
405 int i, byte_offset, m, mi, p, index, bytes_read;
408 /* Note: Caller checked that devc and devc->ftdic != NULL. */
410 sr_spew("Reading block %d.", devc->block_counter);
412 bytes_read = la8_read(devc, devc->mangled_buf, BS);
414 /* If first block read got 0 bytes, retry until success or timeout. */
415 if ((bytes_read == 0) && (devc->block_counter == 0)) {
417 sr_spew("Reading block 0 (again).");
418 bytes_read = la8_read(devc, devc->mangled_buf, BS);
419 /* TODO: How to handle read errors here? */
421 } while ((devc->done > now) && (bytes_read == 0));
424 /* Check if block read was successful or a timeout occured. */
425 if (bytes_read != BS) {
426 sr_err("Trigger timed out. Bytes read: %d.", bytes_read);
427 (void) la8_reset(devc); /* Ignore errors. */
431 /* De-mangle the data. */
432 sr_spew("Demangling block %d.", devc->block_counter);
433 byte_offset = devc->block_counter * BS;
434 m = byte_offset / (1024 * 1024);
435 mi = m * (1024 * 1024);
436 for (i = 0; i < BS; i++) {
438 index = m * 2 + (((byte_offset + i) - mi) / 2) * 16;
439 index += (devc->divcount == 0) ? p : (1 - p);
440 devc->final_buf[index] = devc->mangled_buf[i];
446 SR_PRIV void send_block_to_session_bus(struct dev_context *devc, int block)
449 uint8_t sample, expected_sample;
450 struct sr_datafeed_packet packet;
451 struct sr_datafeed_logic logic;
452 int trigger_point; /* Relative trigger point (in this block). */
454 /* Note: No sanity checks on devc/block, caller is responsible. */
456 /* Check if we can find the trigger condition in this block. */
458 expected_sample = devc->trigger_pattern & devc->trigger_mask;
459 for (i = 0; i < BS; i++) {
460 /* Don't continue if the trigger was found previously. */
461 if (devc->trigger_found)
465 * Also, don't continue if triggers are "don't care", i.e. if
466 * no trigger conditions were specified by the user. In that
467 * case we don't want to send an SR_DF_TRIGGER packet at all.
469 if (devc->trigger_mask == 0x00)
472 sample = *(devc->final_buf + (block * BS) + i);
474 if ((sample & devc->trigger_mask) == expected_sample) {
476 devc->trigger_found = 1;
481 /* If no trigger was found, send one SR_DF_LOGIC packet. */
482 if (trigger_point == -1) {
483 /* Send an SR_DF_LOGIC packet to the session bus. */
484 sr_spew("Sending SR_DF_LOGIC packet (%d bytes) for "
485 "block %d.", BS, block);
486 packet.type = SR_DF_LOGIC;
487 packet.payload = &logic;
490 logic.data = devc->final_buf + (block * BS);
491 sr_session_send(devc->cb_data, &packet);
496 * We found the trigger, so some special handling is needed. We have
497 * to send an SR_DF_LOGIC packet with the samples before the trigger
498 * (if any), then the SD_DF_TRIGGER packet itself, then another
499 * SR_DF_LOGIC packet with the samples after the trigger (if any).
502 /* TODO: Send SR_DF_TRIGGER packet before or after the actual sample? */
504 /* If at least one sample is located before the trigger... */
505 if (trigger_point > 0) {
506 /* Send pre-trigger SR_DF_LOGIC packet to the session bus. */
507 sr_spew("Sending pre-trigger SR_DF_LOGIC packet, "
508 "start = %d, length = %d.", block * BS, trigger_point);
509 packet.type = SR_DF_LOGIC;
510 packet.payload = &logic;
511 logic.length = trigger_point;
513 logic.data = devc->final_buf + (block * BS);
514 sr_session_send(devc->cb_data, &packet);
517 /* Send the SR_DF_TRIGGER packet to the session bus. */
518 sr_spew("Sending SR_DF_TRIGGER packet, sample = %d.",
519 (block * BS) + trigger_point);
520 packet.type = SR_DF_TRIGGER;
521 packet.payload = NULL;
522 sr_session_send(devc->cb_data, &packet);
524 /* If at least one sample is located after the trigger... */
525 if (trigger_point < (BS - 1)) {
526 /* Send post-trigger SR_DF_LOGIC packet to the session bus. */
527 sr_spew("Sending post-trigger SR_DF_LOGIC packet, "
528 "start = %d, length = %d.",
529 (block * BS) + trigger_point, BS - trigger_point);
530 packet.type = SR_DF_LOGIC;
531 packet.payload = &logic;
532 logic.length = BS - trigger_point;
534 logic.data = devc->final_buf + (block * BS) + trigger_point;
535 sr_session_send(devc->cb_data, &packet);