2 * This file is part of the sigrok project.
4 * Copyright (C) 2011 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
26 #include "sigrok-internal.h"
28 #define USB_VENDOR_ID 0x0403
29 #define USB_PRODUCT_ID 0x6001
30 #define USB_DESCRIPTION "ChronoVu LA8"
31 #define USB_VENDOR_NAME "ChronoVu"
32 #define USB_MODEL_NAME "LA8"
33 #define USB_MODEL_VERSION ""
36 #define TRIGGER_TYPES "01"
37 #define SDRAM_SIZE (8 * 1024 * 1024)
38 #define MIN_NUM_SAMPLES 1
40 #define BS 4096 /* Block size */
41 #define NUM_BLOCKS 2048 /* Number of blocks */
43 static GSList *device_instances = NULL;
45 static const char *probe_names[NUM_PROBES + 1] = {
58 /** FTDI device context (used by libftdi). */
59 struct ftdi_context *ftdic;
61 /** The currently configured samplerate of the device. */
62 uint64_t cur_samplerate;
64 /** The current sampling limit (in ms). */
67 /** The current sampling limit (in number of samples). */
68 uint64_t limit_samples;
74 * A buffer containing some (mangled) samples from the device.
75 * Format: Pretty mangled-up (due to hardware reasons), see code.
77 uint8_t mangled_buf[BS];
80 * An 8MB buffer where we'll store the de-mangled samples.
81 * Format: Each sample is 1 byte, MSB is channel 7, LSB is channel 0.
86 * Trigger pattern (MSB = channel 7, LSB = channel 0).
87 * A 1 bit matches a high signal, 0 matches a low signal on a probe.
88 * Only low/high triggers (but not e.g. rising/falling) are supported.
90 uint8_t trigger_pattern;
93 * Trigger mask (MSB = channel 7, LSB = channel 0).
94 * A 1 bit means "must match trigger_pattern", 0 means "don't care".
98 /** Time (in seconds) before the trigger times out. */
99 uint64_t trigger_timeout;
101 /** Tells us whether an SR_DF_TRIGGER packet was already sent. */
107 /** Counter/index for the data block to be read. */
110 /** The divcount value (determines the sample period) for the LA8. */
114 /* This will be initialized via hw_get_device_info()/SR_DI_SAMPLERATES. */
115 static uint64_t supported_samplerates[255 + 1] = { 0 };
118 * Min: 1 sample per 0.01us -> sample time is 0.084s, samplerate 100MHz
119 * Max: 1 sample per 2.55us -> sample time is 21.391s, samplerate 392.15kHz
121 static struct sr_samplerates samplerates = {
125 .list = supported_samplerates,
128 /* Note: Continuous sampling is not supported by the hardware. */
129 static int capabilities[] = {
130 SR_HWCAP_LOGIC_ANALYZER,
132 SR_HWCAP_LIMIT_MSEC, /* TODO: Not yet implemented. */
133 SR_HWCAP_LIMIT_SAMPLES, /* TODO: Not yet implemented. */
137 /* Function prototypes. */
138 static int la8_close_usb_reset_sequencer(struct la8 *la8);
139 static int hw_stop_acquisition(int device_index, gpointer session_data);
140 static int la8_reset(struct la8 *la8);
142 static void fill_supported_samplerates_if_needed(void)
146 /* Do nothing if supported_samplerates[] is already filled. */
147 if (supported_samplerates[0] != 0)
150 /* Fill supported_samplerates[] with the proper values. */
151 for (i = 0; i < 255; i++)
152 supported_samplerates[254 - i] = SR_MHZ(100) / (i + 1);
153 supported_samplerates[255] = 0;
157 * Check if the given samplerate is supported by the LA8 hardware.
159 * @param samplerate The samplerate (in Hz) to check.
160 * @return 1 if the samplerate is supported/valid, 0 otherwise.
162 static int is_valid_samplerate(uint64_t samplerate)
166 fill_supported_samplerates_if_needed();
168 for (i = 0; i < 255; i++) {
169 if (supported_samplerates[i] == samplerate)
173 sr_err("la8: %s: invalid samplerate (%" PRIu64 "Hz)",
174 __func__, samplerate);
180 * Convert a samplerate (in Hz) to the 'divcount' value the LA8 wants.
182 * LA8 hardware: sample period = (divcount + 1) * 10ns.
183 * Min. value for divcount: 0x00 (10ns sample period, 100MHz samplerate).
184 * Max. value for divcount: 0xfe (2550ns sample period, 392.15kHz samplerate).
186 * @param samplerate The samplerate in Hz.
187 * @return The divcount value as needed by the hardware, or 0xff upon errors.
189 static uint8_t samplerate_to_divcount(uint64_t samplerate)
191 if (samplerate == 0) {
192 sr_err("la8: %s: samplerate was 0", __func__);
196 if (!is_valid_samplerate(samplerate)) {
197 sr_err("la8: %s: can't get divcount, samplerate invalid",
202 return (SR_MHZ(100) / samplerate) - 1;
206 * Write data of a certain length to the LA8's FTDI device.
208 * @param la8 The LA8 struct containing private per-device-instance data.
209 * @param buf The buffer containing the data to write.
210 * @param size The number of bytes to write.
211 * @return The number of bytes written, or a negative value upon errors.
213 static int la8_write(struct la8 *la8, uint8_t *buf, int size)
218 sr_err("la8: %s: la8 was NULL", __func__);
223 sr_err("la8: %s: la8->ftdic was NULL", __func__);
228 sr_err("la8: %s: buf was NULL", __func__);
233 sr_err("la8: %s: size was < 0", __func__);
237 bytes_written = ftdi_write_data(la8->ftdic, buf, size);
239 if (bytes_written < 0) {
240 sr_err("la8: %s: ftdi_write_data: (%d) %s", __func__,
241 bytes_written, ftdi_get_error_string(la8->ftdic));
242 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
243 } else if (bytes_written != size) {
244 sr_err("la8: %s: bytes to write: %d, bytes written: %d",
245 __func__, size, bytes_written);
246 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
249 return bytes_written;
253 * Read a certain amount of bytes from the LA8's FTDI device.
255 * @param la8 The LA8 struct containing private per-device-instance data.
256 * @param buf The buffer where the received data will be stored.
257 * @param size The number of bytes to read.
258 * @return The number of bytes read, or a negative value upon errors.
260 static int la8_read(struct la8 *la8, uint8_t *buf, int size)
265 sr_err("la8: %s: la8 was NULL", __func__);
270 sr_err("la8: %s: la8->ftdic was NULL", __func__);
275 sr_err("la8: %s: buf was NULL", __func__);
280 sr_err("la8: %s: size was <= 0", __func__);
284 bytes_read = ftdi_read_data(la8->ftdic, buf, size);
286 if (bytes_read < 0) {
287 sr_err("la8: %s: ftdi_read_data: (%d) %s", __func__,
288 bytes_read, ftdi_get_error_string(la8->ftdic));
289 } else if (bytes_read != size) {
290 // sr_err("la8: %s: bytes to read: %d, bytes read: %d",
291 // __func__, size, bytes_read);
297 static int la8_close(struct la8 *la8)
302 sr_err("la8: %s: la8 was NULL", __func__);
307 sr_err("la8: %s: la8->ftdic was NULL", __func__);
311 if ((ret = ftdi_usb_close(la8->ftdic)) < 0) {
312 sr_err("la8: %s: ftdi_usb_close: (%d) %s",
313 __func__, ret, ftdi_get_error_string(la8->ftdic));
320 * Close the ChronoVu LA8 USB port and reset the LA8 sequencer logic.
322 * @param la8 The LA8 struct containing private per-device-instance data.
323 * @return SR_OK upon success, SR_ERR upon failure.
325 static int la8_close_usb_reset_sequencer(struct la8 *la8)
327 /* Magic sequence of bytes for resetting the LA8 sequencer logic. */
328 uint8_t buf[8] = {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};
331 sr_spew("la8: entering %s", __func__);
334 sr_err("la8: %s: la8 was NULL", __func__);
339 sr_err("la8: %s: la8->ftdic was NULL", __func__);
343 if (la8->ftdic->usb_dev) {
344 /* Reset the LA8 sequencer logic, then wait 100ms. */
345 sr_dbg("la8: resetting sequencer logic");
346 (void) la8_write(la8, buf, 8); /* Ignore errors. */
347 g_usleep(100 * 1000);
349 /* Purge FTDI buffers, then reset and close the FTDI device. */
350 sr_dbg("la8: purging buffers, resetting+closing FTDI device");
352 /* Log errors, but ignore them (i.e., don't abort). */
353 if ((ret = ftdi_usb_purge_buffers(la8->ftdic)) < 0)
354 sr_err("la8: %s: ftdi_usb_purge_buffers: (%d) %s",
355 __func__, ret, ftdi_get_error_string(la8->ftdic));
356 if ((ret = ftdi_usb_reset(la8->ftdic)) < 0)
357 sr_err("la8: %s: ftdi_usb_reset: (%d) %s", __func__,
358 ret, ftdi_get_error_string(la8->ftdic));
359 if ((ret = ftdi_usb_close(la8->ftdic)) < 0)
360 sr_err("la8: %s: ftdi_usb_close: (%d) %s", __func__,
361 ret, ftdi_get_error_string(la8->ftdic));
363 sr_spew("la8: %s: usb_dev was NULL, nothing to do", __func__);
366 ftdi_free(la8->ftdic); /* Returns void. */
373 * Reset the ChronoVu LA8.
375 * The LA8 must be reset after a failed read/write operation or upon timeouts.
377 * @param la8 The LA8 struct containing private per-device-instance data.
378 * @return SR_OK upon success, SR_ERR upon failure.
380 static int la8_reset(struct la8 *la8)
387 sr_err("la8: %s: la8 was NULL", __func__);
392 sr_err("la8: %s: la8->ftdic was NULL", __func__);
396 sr_dbg("la8: resetting the device");
399 * Purge pending read data from the FTDI hardware FIFO until
400 * no more data is left, or a timeout occurs (after 20s).
402 done = 20 + time(NULL);
404 /* TODO: Ignore errors? Check for < 0 at least! */
405 bytes_read = la8_read(la8, (uint8_t *)&buf, BS);
407 } while ((done > now) && (bytes_read > 0));
409 /* Reset the LA8 sequencer logic and close the USB port. */
410 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
412 sr_dbg("la8: device reset finished");
417 static int configure_probes(struct la8 *la8, GSList *probes)
419 struct sr_probe *probe;
424 la8->trigger_pattern = 0;
425 la8->trigger_mask = 0; /* Default to "don't care" for all probes. */
427 for (l = probes; l; l = l->next) {
428 probe = (struct sr_probe *)l->data;
431 sr_err("la8: %s: probe was NULL", __func__);
435 /* Skip disabled probes. */
439 /* Skip (enabled) probes with no configured trigger. */
443 /* Note: Must only be run if probe->trigger != NULL. */
444 if (probe->index < 0 || probe->index > 7) {
445 sr_err("la8: %s: invalid probe index %d, must be "
446 "between 0 and 7", __func__, probe->index);
450 probe_bit = (1 << (probe->index - 1));
452 /* Configure the probe's trigger mask and trigger pattern. */
453 for (tc = probe->trigger; tc && *tc; tc++) {
454 la8->trigger_mask |= probe_bit;
456 /* Sanity check, LA8 only supports low/high trigger. */
457 if (*tc != '0' && *tc != '1') {
458 sr_err("la8: %s: invalid trigger '%c', only "
459 "'0'/'1' supported", __func__, *tc);
464 la8->trigger_pattern |= probe_bit;
468 sr_dbg("la8: %s: trigger_mask = 0x%x, trigger_pattern = 0x%x",
469 __func__, la8->trigger_mask, la8->trigger_pattern);
474 static int hw_init(const char *deviceinfo)
477 struct sr_device_instance *sdi;
480 sr_spew("la8: entering %s", __func__);
482 /* Avoid compiler errors. */
485 /* Allocate memory for our private driver context. */
486 if (!(la8 = g_try_malloc(sizeof(struct la8)))) {
487 sr_err("la8: %s: struct la8 malloc failed", __func__);
488 goto err_free_nothing;
491 /* Set some sane defaults. */
493 la8->cur_samplerate = SR_MHZ(100); /* 100MHz == max. samplerate */
495 la8->limit_samples = 0;
496 la8->session_id = NULL;
497 memset(la8->mangled_buf, 0, BS);
498 la8->final_buf = NULL;
499 la8->trigger_pattern = 0x00; /* Value irrelevant, see trigger_mask. */
500 la8->trigger_mask = 0x00; /* All probes are "don't care". */
501 la8->trigger_timeout = 10; /* Default to 10s trigger timeout. */
502 la8->trigger_found = 0;
504 la8->block_counter = 0;
505 la8->divcount = 0; /* 10ns sample period == 100MHz samplerate */
507 /* Allocate memory where we'll store the de-mangled data. */
508 if (!(la8->final_buf = g_try_malloc(SDRAM_SIZE))) {
509 sr_err("la8: %s: final_buf malloc failed", __func__);
513 /* Allocate memory for the FTDI context (ftdic) and initialize it. */
514 if (!(la8->ftdic = ftdi_new())) {
515 sr_err("la8: %s: ftdi_new failed", __func__);
516 goto err_free_final_buf;
519 /* Check for the device and temporarily open it. */
520 if ((ret = ftdi_usb_open_desc(la8->ftdic, USB_VENDOR_ID,
521 USB_PRODUCT_ID, USB_DESCRIPTION, NULL)) < 0) {
522 sr_dbg("la8: %s: ftdi_usb_open_desc: (%d) %s",
523 __func__, ret, ftdi_get_error_string(la8->ftdic));
524 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
527 sr_dbg("la8: found device");
529 /* Register the device with libsigrok. */
530 sdi = sr_dev_inst_new(0, SR_ST_INITIALIZING,
531 USB_VENDOR_NAME, USB_MODEL_NAME, USB_MODEL_VERSION);
533 sr_err("la8: %s: sr_device_instance_new failed", __func__);
534 goto err_close_ftdic;
539 device_instances = g_slist_append(device_instances, sdi);
541 sr_spew("la8: %s finished successfully", __func__);
543 /* Close device. We'll reopen it again when we need it. */
544 (void) la8_close(la8); /* Log, but ignore errors. */
549 (void) la8_close(la8); /* Log, but ignore errors. */
551 free(la8->ftdic); /* NOT g_free()! */
553 g_free(la8->final_buf);
561 static int hw_opendev(int device_index)
564 struct sr_device_instance *sdi;
567 if (!(sdi = sr_dev_inst_get(device_instances, device_index))) {
568 sr_err("la8: %s: sdi was NULL", __func__);
569 return SR_ERR; /* TODO: SR_ERR_ARG? */
572 if (!(la8 = sdi->priv)) {
573 sr_err("la8: %s: sdi->priv was NULL", __func__);
574 return SR_ERR; /* TODO: SR_ERR_ARG? */
577 sr_dbg("la8: opening device");
579 /* Open the device. */
580 if ((ret = ftdi_usb_open_desc(la8->ftdic, USB_VENDOR_ID,
581 USB_PRODUCT_ID, USB_DESCRIPTION, NULL)) < 0) {
582 sr_err("la8: %s: ftdi_usb_open_desc: (%d) %s",
583 __func__, ret, ftdi_get_error_string(la8->ftdic));
584 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
587 sr_dbg("la8: device opened successfully");
589 /* Purge RX/TX buffers in the FTDI chip. */
590 if ((ret = ftdi_usb_purge_buffers(la8->ftdic)) < 0) {
591 sr_err("la8: %s: ftdi_usb_purge_buffers: (%d) %s",
592 __func__, ret, ftdi_get_error_string(la8->ftdic));
593 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
594 goto err_opendev_close_ftdic;
596 sr_dbg("la8: FTDI buffers purged successfully");
598 /* Enable flow control in the FTDI chip. */
599 if ((ret = ftdi_setflowctrl(la8->ftdic, SIO_RTS_CTS_HS)) < 0) {
600 sr_err("la8: %s: ftdi_setflowcontrol: (%d) %s",
601 __func__, ret, ftdi_get_error_string(la8->ftdic));
602 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
603 goto err_opendev_close_ftdic;
605 sr_dbg("la8: FTDI flow control enabled successfully");
608 g_usleep(100 * 1000);
610 sdi->status = SR_ST_ACTIVE;
614 err_opendev_close_ftdic:
615 (void) la8_close(la8); /* Log, but ignore errors. */
619 static int set_samplerate(struct sr_device_instance *sdi, uint64_t samplerate)
624 sr_err("la8: %s: sdi was NULL", __func__);
628 if (!(la8 = sdi->priv)) {
629 sr_err("la8: %s: sdi->priv was NULL", __func__);
633 sr_spew("la8: setting samplerate");
635 fill_supported_samplerates_if_needed();
637 /* Check if this is a samplerate supported by the hardware. */
638 if (!is_valid_samplerate(samplerate))
641 /* Set the new samplerate. */
642 la8->cur_samplerate = samplerate;
644 sr_dbg("la8: samplerate set to %" PRIu64 "Hz", la8->cur_samplerate);
649 static int hw_closedev(int device_index)
651 struct sr_device_instance *sdi;
654 if (!(sdi = sr_dev_inst_get(device_instances, device_index))) {
655 sr_err("la8: %s: sdi was NULL", __func__);
656 return SR_ERR; /* TODO: SR_ERR_ARG? */
659 if (!(la8 = sdi->priv)) {
660 sr_err("la8: %s: sdi->priv was NULL", __func__);
661 return SR_ERR; /* TODO: SR_ERR_ARG? */
664 sr_dbg("la8: closing device");
666 if (sdi->status == SR_ST_ACTIVE) {
667 sr_dbg("la8: %s: status ACTIVE, closing device", __func__);
668 /* TODO: Really ignore errors here, or return SR_ERR? */
669 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
671 sr_spew("la8: %s: status not ACTIVE, nothing to do", __func__);
674 sdi->status = SR_ST_INACTIVE;
676 sr_dbg("la8: %s: freeing sample buffers", __func__);
677 g_free(la8->final_buf);
682 static int hw_cleanup(void)
685 struct sr_device_instance *sdi;
688 sr_spew("la8: entering %s", __func__);
690 /* Properly close all devices. */
691 for (l = device_instances; l; l = l->next) {
692 if (!(sdi = l->data)) {
693 /* Log error, but continue cleaning up the rest. */
694 sr_err("la8: %s: sdi was NULL, continuing", __func__);
700 * Fixes a segfault as it's free()d elsewhere already.
701 * TODO: Document who is supposed to free this, and when.
703 if (sdi->priv != NULL)
706 sr_err("la8: %s: sdi->priv was NULL, nothing "
709 sr_dev_inst_free(sdi); /* Returns void. */
711 g_slist_free(device_instances); /* Returns void. */
712 device_instances = NULL;
717 static void *hw_get_device_info(int device_index, int device_info_id)
719 struct sr_device_instance *sdi;
723 sr_spew("la8: entering %s", __func__);
725 if (!(sdi = sr_dev_inst_get(device_instances, device_index))) {
726 sr_err("la8: %s: sdi was NULL", __func__);
730 if (!(la8 = sdi->priv)) {
731 sr_err("la8: %s: sdi->priv was NULL", __func__);
735 switch (device_info_id) {
739 case SR_DI_NUM_PROBES:
740 info = GINT_TO_POINTER(NUM_PROBES);
742 case SR_DI_PROBE_NAMES:
745 case SR_DI_SAMPLERATES:
746 fill_supported_samplerates_if_needed();
749 case SR_DI_TRIGGER_TYPES:
750 info = (char *)TRIGGER_TYPES;
752 case SR_DI_CUR_SAMPLERATE:
753 info = &la8->cur_samplerate;
756 /* Unknown device info ID, return NULL. */
757 sr_err("la8: %s: Unknown device info ID", __func__);
765 static int hw_get_status(int device_index)
767 struct sr_device_instance *sdi;
769 if (!(sdi = sr_dev_inst_get(device_instances, device_index))) {
770 sr_err("la8: %s: sdi was NULL, device not found", __func__);
771 return SR_ST_NOT_FOUND;
774 sr_dbg("la8: %s: returning status %d", __func__, sdi->status);
779 static int *hw_get_capabilities(void)
781 sr_spew("la8: entering %s", __func__);
786 static int hw_set_configuration(int device_index, int capability, void *value)
788 struct sr_device_instance *sdi;
791 sr_spew("la8: entering %s", __func__);
793 if (!(sdi = sr_dev_inst_get(device_instances, device_index))) {
794 sr_err("la8: %s: sdi was NULL", __func__);
795 return SR_ERR; /* TODO: SR_ERR_ARG? */
798 if (!(la8 = sdi->priv)) {
799 sr_err("la8: %s: sdi->priv was NULL", __func__);
800 return SR_ERR; /* TODO: SR_ERR_ARG? */
803 switch (capability) {
804 case SR_HWCAP_SAMPLERATE:
805 if (set_samplerate(sdi, *(uint64_t *)value) == SR_ERR)
807 sr_dbg("la8: SAMPLERATE = %" PRIu64, la8->cur_samplerate);
809 case SR_HWCAP_PROBECONFIG:
810 if (configure_probes(la8, (GSList *)value) != SR_OK) {
811 sr_err("la8: %s: probe config failed", __func__);
815 case SR_HWCAP_LIMIT_MSEC:
816 if (*(uint64_t *)value == 0) {
817 sr_err("la8: %s: LIMIT_MSEC can't be 0", __func__);
820 la8->limit_msec = *(uint64_t *)value;
821 sr_dbg("la8: LIMIT_MSEC = %" PRIu64, la8->limit_msec);
823 case SR_HWCAP_LIMIT_SAMPLES:
824 if (*(uint64_t *)value < MIN_NUM_SAMPLES) {
825 sr_err("la8: %s: LIMIT_SAMPLES too small", __func__);
828 la8->limit_samples = *(uint64_t *)value;
829 sr_dbg("la8: LIMIT_SAMPLES = %" PRIu64, la8->limit_samples);
832 /* Unknown capability, return SR_ERR. */
833 sr_err("la8: %s: Unknown capability", __func__);
842 * Get a block of data from the LA8.
844 * @param la8 The LA8 struct containing private per-device-instance data.
845 * @return SR_OK upon success, or SR_ERR upon errors.
847 static int la8_read_block(struct la8 *la8)
849 int i, byte_offset, m, mi, p, index, bytes_read;
853 sr_err("la8: %s: la8 was NULL", __func__);
858 sr_err("la8: %s: la8->ftdic was NULL", __func__);
862 sr_spew("la8: %s: reading block %d", __func__, la8->block_counter);
864 bytes_read = la8_read(la8, la8->mangled_buf, BS);
866 /* If first block read got 0 bytes, retry until success or timeout. */
867 if ((bytes_read == 0) && (la8->block_counter == 0)) {
869 sr_spew("la8: %s: reading block 0 again", __func__);
870 bytes_read = la8_read(la8, la8->mangled_buf, BS);
871 /* TODO: How to handle read errors here? */
873 } while ((la8->done > now) && (bytes_read == 0));
876 /* Check if block read was successful or a timeout occured. */
877 if (bytes_read != BS) {
878 sr_err("la8: %s: trigger timed out", __func__);
879 (void) la8_reset(la8); /* Ignore errors. */
883 /* De-mangle the data. */
884 sr_spew("la8: de-mangling samples of block %d", la8->block_counter);
885 byte_offset = la8->block_counter * BS;
886 m = byte_offset / (1024 * 1024);
887 mi = m * (1024 * 1024);
888 for (i = 0; i < BS; i++) {
890 index = m * 2 + (((byte_offset + i) - mi) / 2) * 16;
891 index += (la8->divcount == 0) ? p : (1 - p);
892 la8->final_buf[index] = la8->mangled_buf[i];
898 static void send_block_to_session_bus(struct la8 *la8, int block)
901 uint8_t sample, expected_sample;
902 struct sr_datafeed_packet packet;
903 struct sr_datafeed_logic logic;
904 int trigger_point; /* Relative trigger point (in this block). */
906 /* Note: No sanity checks on la8/block, caller is responsible. */
908 /* Check if we can find the trigger condition in this block. */
910 expected_sample = la8->trigger_pattern & la8->trigger_mask;
911 for (i = 0; i < BS; i++) {
912 /* Don't continue if the trigger was found previously. */
913 if (la8->trigger_found)
917 * Also, don't continue if triggers are "don't care", i.e. if
918 * no trigger conditions were specified by the user. In that
919 * case we don't want to send an SR_DF_TRIGGER packet at all.
921 if (la8->trigger_mask == 0x00)
924 sample = *(la8->final_buf + (block * BS) + i);
926 if ((sample & la8->trigger_mask) == expected_sample) {
928 la8->trigger_found = 1;
933 /* If no trigger was found, send one SR_DF_LOGIC packet. */
934 if (trigger_point == -1) {
935 /* Send an SR_DF_LOGIC packet to the session bus. */
936 sr_spew("la8: sending SR_DF_LOGIC packet (%d bytes) for "
937 "block %d", BS, block);
938 packet.type = SR_DF_LOGIC;
939 packet.payload = &logic;
942 logic.data = la8->final_buf + (block * BS);
943 sr_session_bus(la8->session_id, &packet);
948 * We found the trigger, so some special handling is needed. We have
949 * to send an SR_DF_LOGIC packet with the samples before the trigger
950 * (if any), then the SD_DF_TRIGGER packet itself, then another
951 * SR_DF_LOGIC packet with the samples after the trigger (if any).
954 /* TODO: Send SR_DF_TRIGGER packet before or after the actual sample? */
956 /* If at least one sample is located before the trigger... */
957 if (trigger_point > 0) {
958 /* Send pre-trigger SR_DF_LOGIC packet to the session bus. */
959 sr_spew("la8: sending pre-trigger SR_DF_LOGIC packet, "
960 "start = %d, length = %d", block * BS, trigger_point);
961 packet.type = SR_DF_LOGIC;
962 packet.payload = &logic;
963 logic.length = trigger_point;
965 logic.data = la8->final_buf + (block * BS);
966 sr_session_bus(la8->session_id, &packet);
969 /* Send the SR_DF_TRIGGER packet to the session bus. */
970 sr_spew("la8: sending SR_DF_TRIGGER packet, sample = %d",
971 (block * BS) + trigger_point);
972 packet.type = SR_DF_TRIGGER;
973 packet.payload = NULL;
974 sr_session_bus(la8->session_id, &packet);
976 /* If at least one sample is located after the trigger... */
977 if (trigger_point < (BS - 1)) {
978 /* Send post-trigger SR_DF_LOGIC packet to the session bus. */
979 sr_spew("la8: sending post-trigger SR_DF_LOGIC packet, "
980 "start = %d, length = %d",
981 (block * BS) + trigger_point, BS - trigger_point);
982 packet.type = SR_DF_LOGIC;
983 packet.payload = &logic;
984 logic.length = BS - trigger_point;
986 logic.data = la8->final_buf + (block * BS) + trigger_point;
987 sr_session_bus(la8->session_id, &packet);
991 static int receive_data(int fd, int revents, void *session_data)
994 struct sr_device_instance *sdi;
997 /* Avoid compiler errors. */
1001 if (!(sdi = session_data)) {
1002 sr_err("la8: %s: session_data was NULL", __func__);
1006 if (!(la8 = sdi->priv)) {
1007 sr_err("la8: %s: sdi->priv was NULL", __func__);
1011 /* Get one block of data. */
1012 if ((ret = la8_read_block(la8)) < 0) {
1013 sr_err("la8: %s: la8_read_block error: %d", __func__, ret);
1014 hw_stop_acquisition(sdi->index, session_data);
1018 /* We need to get exactly NUM_BLOCKS blocks (i.e. 8MB) of data. */
1019 if (la8->block_counter != (NUM_BLOCKS - 1)) {
1020 la8->block_counter++;
1024 sr_dbg("la8: sampling finished, sending data to session bus now");
1026 /* All data was received and demangled, send it to the session bus. */
1027 for (i = 0; i < NUM_BLOCKS; i++)
1028 send_block_to_session_bus(la8, i);
1030 hw_stop_acquisition(sdi->index, session_data);
1032 // return FALSE; /* FIXME? */
1036 static int hw_start_acquisition(int device_index, gpointer session_data)
1038 struct sr_device_instance *sdi;
1040 struct sr_datafeed_packet packet;
1041 struct sr_datafeed_header header;
1045 sr_spew("la8: entering %s", __func__);
1047 if (!(sdi = sr_dev_inst_get(device_instances, device_index))) {
1048 sr_err("la8: %s: sdi was NULL", __func__);
1049 return SR_ERR; /* TODO: SR_ERR_ARG? */
1052 if (!(la8 = sdi->priv)) {
1053 sr_err("la8: %s: sdi->priv was NULL", __func__);
1054 return SR_ERR; /* TODO: SR_ERR_ARG? */
1058 sr_err("la8: %s: la8->ftdic was NULL", __func__);
1062 la8->divcount = samplerate_to_divcount(la8->cur_samplerate);
1063 if (la8->divcount == 0xff) {
1064 sr_err("la8: %s: invalid divcount/samplerate", __func__);
1068 /* Fill acquisition parameters into buf[]. */
1069 buf[0] = la8->divcount;
1070 buf[1] = 0xff; /* This byte must always be 0xff. */
1071 buf[2] = la8->trigger_pattern;
1072 buf[3] = la8->trigger_mask;
1074 /* Start acquisition. */
1075 bytes_written = la8_write(la8, buf, 4);
1077 if (bytes_written < 0) {
1078 sr_err("la8: acquisition failed to start");
1080 } else if (bytes_written != 4) {
1081 sr_err("la8: acquisition failed to start");
1082 return SR_ERR; /* TODO: Other error and return code? */
1085 sr_dbg("la8: acquisition started successfully");
1087 la8->session_id = session_data;
1089 /* Send header packet to the session bus. */
1090 sr_dbg("la8: %s: sending SR_DF_HEADER", __func__);
1091 packet.type = SR_DF_HEADER;
1092 packet.payload = &header;
1093 header.feed_version = 1;
1094 gettimeofday(&header.starttime, NULL);
1095 header.samplerate = la8->cur_samplerate;
1096 header.num_logic_probes = NUM_PROBES;
1097 sr_session_bus(session_data, &packet);
1099 /* Time when we should be done (for detecting trigger timeouts). */
1100 la8->done = (la8->divcount + 1) * 0.08388608 + time(NULL)
1101 + la8->trigger_timeout;
1102 la8->block_counter = 0;
1103 la8->trigger_found = 0;
1105 /* Hook up a dummy handler to receive data from the LA8. */
1106 sr_source_add(-1, G_IO_IN, 0, receive_data, sdi);
1111 static int hw_stop_acquisition(int device_index, gpointer session_data)
1113 struct sr_device_instance *sdi;
1115 struct sr_datafeed_packet packet;
1117 sr_dbg("la8: stopping acquisition");
1119 if (!(sdi = sr_dev_inst_get(device_instances, device_index))) {
1120 sr_err("la8: %s: sdi was NULL", __func__);
1124 if (!(la8 = sdi->priv)) {
1125 sr_err("la8: %s: sdi->priv was NULL", __func__);
1129 /* Send end packet to the session bus. */
1130 sr_dbg("la8: %s: sending SR_DF_END", __func__);
1131 packet.type = SR_DF_END;
1132 sr_session_bus(session_data, &packet);
1137 SR_PRIV struct sr_device_plugin chronovu_la8_plugin_info = {
1138 .name = "chronovu-la8",
1139 .longname = "ChronoVu LA8",
1142 .cleanup = hw_cleanup,
1143 .opendev = hw_opendev,
1144 .closedev = hw_closedev,
1145 .get_device_info = hw_get_device_info,
1146 .get_status = hw_get_status,
1147 .get_capabilities = hw_get_capabilities,
1148 .set_configuration = hw_set_configuration,
1149 .start_acquisition = hw_start_acquisition,
1150 .stop_acquisition = hw_stop_acquisition,