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
24 #include <sigrok-internal.h>
26 #define USB_VENDOR_ID 0x0403
27 #define USB_PRODUCT_ID 0x6001
28 #define USB_DESCRIPTION "ChronoVu LA8"
29 #define USB_VENDOR_NAME "ChronoVu"
30 #define USB_MODEL_NAME "LA8"
31 #define USB_MODEL_VERSION ""
34 #define TRIGGER_TYPES "01"
35 #define SDRAM_SIZE (8 * 1024 * 1024)
36 #define MIN_NUM_SAMPLES 1
38 static GSList *device_instances = NULL;
41 /** FTDI device context (used by libftdi). */
42 struct ftdi_context *ftdic;
44 /** The currently configured samplerate of the device. */
45 uint64_t cur_samplerate;
47 /** The current sampling limit (in ms). */
50 /** The current sampling limit (in number of samples). */
51 uint64_t limit_samples;
53 /** The number of probes. */
60 * An 8MB buffer containing the (mangled) samples from the device.
61 * Format: Pretty mangled-up (due to hardware reasons), see code.
66 * An 8MB buffer where we'll store the de-mangled samples.
67 * Format: Each sample is 1 byte, MSB is channel 7, LSB is channel 0.
72 * Trigger pattern (MSB = channel 7, LSB = channel 0).
73 * A 1 bit matches a high signal, 0 matches a low signal on a probe.
74 * Only low/high triggers (but not e.g. rising/falling) are supported.
76 uint8_t trigger_pattern;
79 * Trigger mask (MSB = channel 7, LSB = channel 0).
80 * A 1 bit means "must match trigger_pattern", 0 means "don't care".
84 /** Time (in seconds) before the trigger times out. */
85 uint64_t trigger_timeout;
90 /** Counter/index for the data block (0..2047) to be read. */
93 /** The divcount value (determines the sample period) for the LA8. */
97 /* This will be initialized via hw_get_device_info()/SR_DI_SAMPLERATES. */
98 static uint64_t supported_samplerates[255 + 1] = { 0 };
101 * Min: 1 sample per 0.01us -> sample time is 0.084s, samplerate 100MHz
102 * Max: 1 sample per 2.55us -> sample time is 21.391s, samplerate 392.15kHz
104 static struct sr_samplerates samplerates = {
108 .list = supported_samplerates,
111 /* Note: Continuous sampling is not supported by the hardware. */
112 static int capabilities[] = {
113 SR_HWCAP_LOGIC_ANALYZER,
115 SR_HWCAP_LIMIT_MSEC, /* TODO: Not yet implemented. */
116 SR_HWCAP_LIMIT_SAMPLES, /* TODO: Not yet implemented. */
120 /* Function prototypes. */
121 static int la8_close_usb_reset_sequencer(struct la8 *la8);
122 static void hw_stop_acquisition(int device_index, gpointer session_device_id);
123 static int la8_reset(struct la8 *la8);
125 static void fill_supported_samplerates_if_needed(void)
129 /* Do nothing if supported_samplerates[] is already filled. */
130 if (supported_samplerates[0] != 0)
133 /* Fill supported_samplerates[] with the proper values. */
134 for (i = 0; i < 255; i++)
135 supported_samplerates[254 - i] = SR_MHZ(100) / (i + 1);
136 supported_samplerates[255] = 0;
140 * Check if the given samplerate is supported by the LA8 hardware.
142 * @param samplerate The samplerate (in Hz) to check.
143 * @return 1 if the samplerate is supported/valid, 0 otherwise.
145 static int is_valid_samplerate(uint64_t samplerate)
149 fill_supported_samplerates_if_needed();
151 for (i = 0; i < 255; i++) {
152 if (supported_samplerates[i] == samplerate)
156 sr_warn("la8: %s: invalid samplerate (%" PRIu64 "Hz)",
157 __func__, samplerate);
163 * Convert a samplerate (in Hz) to the 'divcount' value the LA8 wants.
165 * LA8 hardware: sample period = (divcount + 1) * 10ns.
166 * Min. value for divcount: 0x00 (10ns sample period, 100MHz samplerate).
167 * Max. value for divcount: 0xfe (2550ns sample period, 392.15kHz samplerate).
169 * @param samplerate The samplerate in Hz.
170 * @return The divcount value as needed by the hardware, or 0xff upon errors.
172 static uint8_t samplerate_to_divcount(uint64_t samplerate)
174 if (samplerate == 0) {
175 sr_warn("la8: %s: samplerate was 0", __func__);
179 if (!is_valid_samplerate(samplerate)) {
180 sr_warn("la8: %s: can't get divcount, samplerate invalid",
185 return (SR_MHZ(100) / samplerate) - 1;
189 * Write data of a certain length to the LA8's FTDI device.
191 * @param la8 The LA8 struct containing private per-device-instance data.
192 * @param buf The buffer containing the data to write.
193 * @param size The number of bytes to write.
194 * @return The number of bytes written, or a negative value upon errors.
196 static int la8_write(struct la8 *la8, uint8_t *buf, int size)
201 sr_warn("la8: %s: la8 was NULL", __func__);
206 sr_warn("la8: %s: la8->ftdic was NULL", __func__);
211 sr_warn("la8: %s: buf was NULL", __func__);
216 sr_warn("la8: %s: size was < 0", __func__);
220 bytes_written = ftdi_write_data(la8->ftdic, buf, size);
222 if (bytes_written < 0) {
223 sr_warn("la8: %s: ftdi_write_data: (%d) %s", __func__,
224 bytes_written, ftdi_get_error_string(la8->ftdic));
225 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
226 } else if (bytes_written != size) {
227 sr_warn("la8: %s: bytes to write: %d, bytes written: %d",
228 __func__, size, bytes_written);
229 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
232 return bytes_written;
236 * Read a certain amount of bytes from the LA8's FTDI device.
238 * @param la8 The LA8 struct containing private per-device-instance data.
239 * @param buf The buffer where the received data will be stored.
240 * @param size The number of bytes to read.
241 * @return The number of bytes read, or a negative value upon errors.
243 static int la8_read(struct la8 *la8, uint8_t *buf, int size)
248 sr_warn("la8: %s: la8 was NULL", __func__);
253 sr_warn("la8: %s: la8->ftdic was NULL", __func__);
258 sr_warn("la8: %s: buf was NULL", __func__);
263 sr_warn("la8: %s: size was <= 0", __func__);
267 bytes_read = ftdi_read_data(la8->ftdic, buf, size);
269 if (bytes_read < 0) {
270 sr_warn("la8: %s: ftdi_read_data: (%d) %s", __func__,
271 bytes_read, ftdi_get_error_string(la8->ftdic));
272 } else if (bytes_read != size) {
273 // sr_warn("la8: %s: bytes to read: %d, bytes read: %d",
274 // __func__, size, bytes_read);
280 static int la8_close(struct la8 *la8)
285 sr_warn("la8: %s: la8 was NULL", __func__);
290 sr_warn("la8: %s: la8->ftdic was NULL", __func__);
294 if ((ret = ftdi_usb_close(la8->ftdic)) < 0) {
295 sr_warn("la8: %s: ftdi_usb_close: (%d) %s",
296 __func__, ret, ftdi_get_error_string(la8->ftdic));
303 * Close the ChronoVu LA8 USB port and reset the LA8 sequencer logic.
305 * @param la8 The LA8 struct containing private per-device-instance data.
306 * @return SR_OK upon success, SR_ERR upon failure.
308 static int la8_close_usb_reset_sequencer(struct la8 *la8)
310 /* Magic sequence of bytes for resetting the LA8 sequencer logic. */
311 uint8_t buf[8] = {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};
314 sr_dbg("la8: entering %s", __func__);
317 sr_warn("la8: %s: la8 was NULL", __func__);
322 sr_warn("la8: %s: la8->ftdic was NULL", __func__);
326 if (la8->ftdic->usb_dev) {
327 /* Reset the LA8 sequencer logic, then wait 100ms. */
328 sr_dbg("la8: resetting sequencer logic");
329 (void) la8_write(la8, buf, 8); /* Ignore errors. */
330 g_usleep(100 * 1000);
332 /* Purge FTDI buffers, then reset and close the FTDI device. */
333 sr_dbg("la8: purging buffers, resetting+closing FTDI device");
335 /* Log errors, but ignore them (i.e., don't abort). */
336 if ((ret = ftdi_usb_purge_buffers(la8->ftdic)) < 0)
337 sr_warn("la8: %s: ftdi_usb_purge_buffers: (%d) %s",
338 __func__, ret, ftdi_get_error_string(la8->ftdic));
339 if ((ret = ftdi_usb_reset(la8->ftdic)) < 0)
340 sr_warn("la8: %s: ftdi_usb_reset: (%d) %s", __func__,
341 ret, ftdi_get_error_string(la8->ftdic));
342 if ((ret = ftdi_usb_close(la8->ftdic)) < 0)
343 sr_warn("la8: %s: ftdi_usb_close: (%d) %s", __func__,
344 ret, ftdi_get_error_string(la8->ftdic));
346 sr_dbg("la8: %s: usb_dev was NULL, nothing to do", __func__);
349 ftdi_free(la8->ftdic); /* Returns void. */
356 * Reset the ChronoVu LA8.
358 * The LA8 must be reset after a failed read/write operation or upon timeouts.
360 * @param la8 The LA8 struct containing private per-device-instance data.
361 * @return SR_OK upon success, SR_ERR upon failure.
363 static int la8_reset(struct la8 *la8)
370 sr_warn("la8: %s: la8 was NULL", __func__);
375 sr_warn("la8: %s: la8->ftdic was NULL", __func__);
379 sr_dbg("la8: resetting the device");
382 * Purge pending read data from the FTDI hardware FIFO until
383 * no more data is left, or a timeout occurs (after 20s).
385 done = 20 + time(NULL);
387 /* TODO: Ignore errors? Check for < 0 at least! */
388 bytes_read = la8_read(la8, (uint8_t *)&buf, 4096);
390 } while ((done > now) && (bytes_read > 0));
392 /* Reset the LA8 sequencer logic and close the USB port. */
393 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
395 sr_dbg("la8: device reset finished");
400 static int configure_probes(struct la8 *la8, GSList *probes)
402 struct sr_probe *probe;
407 la8->trigger_pattern = 0;
408 la8->trigger_mask = 0; /* Default to "don't care" for all probes. */
410 for (l = probes; l; l = l->next) {
411 probe = (struct sr_probe *)l->data;
414 sr_err("la8: %s: probe was NULL", __func__);
418 /* Skip disabled probes. */
422 /* Skip (enabled) probes with no configured trigger. */
426 /* Note: Must only be run if probe->trigger != NULL. */
427 if (probe->index < 0 || probe->index > 7) {
428 sr_err("la8: %s: invalid probe index %d, must be "
429 "between 0 and 7", __func__, probe->index);
433 probe_bit = (1 << (probe->index - 1));
435 /* Configure the probe's trigger mask and trigger pattern. */
436 for (tc = probe->trigger; tc && *tc; tc++) {
437 la8->trigger_mask |= probe_bit;
439 /* Sanity check, LA8 only supports low/high trigger. */
440 if (*tc != '0' && *tc != '1') {
441 sr_err("la8: %s: invalid trigger '%c', only "
442 "'0'/'1' supported", __func__, *tc);
447 la8->trigger_pattern |= probe_bit;
451 sr_dbg("la8: %s: trigger_mask = 0x%x, trigger_pattern = 0x%x",
452 __func__, la8->trigger_mask, la8->trigger_pattern);
457 static int hw_init(const char *deviceinfo)
460 struct sr_device_instance *sdi;
463 sr_dbg("la8: entering %s", __func__);
465 /* Avoid compiler errors. */
466 deviceinfo = deviceinfo;
468 /* Allocate memory for our private driver context. */
469 if (!(la8 = g_try_malloc(sizeof(struct la8)))) {
470 sr_warn("la8: %s: struct la8 malloc failed", __func__);
472 goto err_free_nothing;
475 /* Set some sane defaults. */
477 la8->cur_samplerate = SR_MHZ(100); /* 100MHz == max. samplerate */
479 la8->limit_samples = 0;
480 la8->num_probes = NUM_PROBES;
481 la8->session_id = NULL;
482 la8->mangled_buf = NULL;
483 la8->final_buf = NULL;
484 la8->trigger_pattern = 0x00; /* Value irrelevant, see trigger_mask. */
485 la8->trigger_mask = 0x00; /* All probes are "don't care". */
486 la8->trigger_timeout = 10; /* Default to 10s trigger timeout. */
488 la8->block_counter = 0;
489 la8->divcount = 0; /* 10ns sample period == 100MHz samplerate */
491 /* Allocate memory for the raw (mangled) data from the LA8. */
492 if (!(la8->mangled_buf = g_try_malloc(SDRAM_SIZE))) {
493 sr_warn("la8: %s: mangled_buf malloc failed", __func__);
498 /* Allocate memory where we'll store the de-mangled data. */
499 if (!(la8->final_buf = g_try_malloc(SDRAM_SIZE))) {
500 sr_warn("la8: %s: final_buf malloc failed", __func__);
502 goto err_free_mangled_buf;
505 /* Allocate memory for the FTDI context (ftdic) and initialize it. */
506 if (!(la8->ftdic = ftdi_new())) {
507 sr_warn("la8: %s: ftdi_new failed", __func__);
508 ret = SR_ERR; /* TODO: More specific error? */
509 goto err_free_final_buf;
512 /* Check for the device and temporarily open it. */
513 if ((ret = ftdi_usb_open_desc(la8->ftdic, USB_VENDOR_ID,
514 USB_PRODUCT_ID, USB_DESCRIPTION, NULL)) < 0) {
515 sr_warn("la8: %s: ftdi_usb_open_desc: (%d) %s",
516 __func__, ret, ftdi_get_error_string(la8->ftdic));
517 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
518 ret = SR_ERR; /* TODO: More specific error? */
521 sr_dbg("la8: found device");
523 /* Register the device with libsigrok. */
524 sdi = sr_device_instance_new(0, SR_ST_INITIALIZING,
525 USB_VENDOR_NAME, USB_MODEL_NAME, USB_MODEL_VERSION);
527 sr_warn("la8: %s: sr_device_instance_new failed", __func__);
528 ret = SR_ERR; /* TODO: More specific error? */
529 goto err_close_ftdic;
534 device_instances = g_slist_append(device_instances, sdi);
536 sr_dbg("la8: %s finished successfully", __func__);
538 /* Close device. We'll reopen it again when we need it. */
539 (void) la8_close(la8); /* Log, but ignore errors. */
541 // return SR_OK; /* TODO */
545 (void) la8_close(la8); /* Log, but ignore errors. */
547 free(la8->ftdic); /* NOT g_free()! */
549 g_free(la8->final_buf);
550 err_free_mangled_buf:
551 g_free(la8->mangled_buf);
555 // return ret; /* TODO */
559 static int hw_opendev(int device_index)
562 struct sr_device_instance *sdi;
565 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
566 sr_warn("la8: %s: sdi was NULL", __func__);
567 return SR_ERR; /* TODO: SR_ERR_ARG? */
570 if (!(la8 = sdi->priv)) {
571 sr_warn("la8: %s: sdi->priv was NULL", __func__);
572 return SR_ERR; /* TODO: SR_ERR_ARG? */
575 sr_dbg("la8: opening device");
577 /* Open the device. */
578 if ((ret = ftdi_usb_open_desc(la8->ftdic, USB_VENDOR_ID,
579 USB_PRODUCT_ID, USB_DESCRIPTION, NULL)) < 0) {
580 sr_warn("la8: %s: ftdi_usb_open_desc: (%d) %s",
581 __func__, ret, ftdi_get_error_string(la8->ftdic));
582 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
585 sr_dbg("la8: device opened successfully");
587 /* Purge RX/TX buffers in the FTDI chip. */
588 if ((ret = ftdi_usb_purge_buffers(la8->ftdic)) < 0) {
589 sr_warn("la8: %s: ftdi_usb_purge_buffers: (%d) %s",
590 __func__, ret, ftdi_get_error_string(la8->ftdic));
591 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
592 goto err_opendev_close_ftdic;
594 sr_dbg("la8: FTDI buffers purged successfully");
596 /* Enable flow control in the FTDI chip. */
597 if ((ret = ftdi_setflowctrl(la8->ftdic, SIO_RTS_CTS_HS)) < 0) {
598 sr_warn("la8: %s: ftdi_setflowcontrol: (%d) %s",
599 __func__, ret, ftdi_get_error_string(la8->ftdic));
600 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
601 goto err_opendev_close_ftdic;
603 sr_dbg("la8: FTDI flow control enabled successfully");
606 g_usleep(100 * 1000);
608 sdi->status = SR_ST_ACTIVE;
612 err_opendev_close_ftdic:
613 (void) la8_close(la8); /* Log, but ignore errors. */
617 static int set_samplerate(struct sr_device_instance *sdi, uint64_t samplerate)
622 sr_warn("la8: %s: sdi was NULL", __func__);
626 if (!(la8 = sdi->priv)) {
627 sr_warn("la8: %s: sdi->priv was NULL", __func__);
631 sr_dbg("la8: setting samplerate");
633 fill_supported_samplerates_if_needed();
635 /* Check if this is a samplerate supported by the hardware. */
636 if (!is_valid_samplerate(samplerate))
639 /* Set the new samplerate. */
640 la8->cur_samplerate = samplerate;
642 sr_dbg("la8: samplerate set to %" PRIu64 "Hz", la8->cur_samplerate);
647 static void hw_closedev(int device_index)
649 struct sr_device_instance *sdi;
652 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
653 sr_warn("la8: %s: sdi was NULL", __func__);
657 if (!(la8 = sdi->priv)) {
658 sr_warn("la8: %s: sdi->priv was NULL", __func__);
662 sr_dbg("la8: closing device");
664 if (sdi->status == SR_ST_ACTIVE) {
665 sr_dbg("la8: %s: status ACTIVE, closing device", __func__);
666 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
668 sr_dbg("la8: %s: status not ACTIVE, nothing to do", __func__);
671 sdi->status = SR_ST_INACTIVE;
673 sr_dbg("la8: %s: freeing sample buffers", __func__);
674 free(la8->mangled_buf);
675 free(la8->final_buf);
678 static void hw_cleanup(void)
681 struct sr_device_instance *sdi;
683 sr_dbg("la8: entering %s", __func__);
685 /* Properly close all devices. */
686 for (l = device_instances; l; l = l->next) {
687 if ((sdi = l->data) == NULL) {
688 sr_warn("la8: %s: sdi was NULL", __func__);
691 if (sdi->priv != NULL)
694 sr_warn("la8: %s: sdi->priv was NULL", __func__);
695 sr_device_instance_free(sdi); /* Returns void. */
697 g_slist_free(device_instances); /* Returns void. */
698 device_instances = NULL;
701 static void *hw_get_device_info(int device_index, int device_info_id)
703 struct sr_device_instance *sdi;
707 sr_dbg("la8: entering %s", __func__);
709 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
710 sr_warn("la8: %s: sdi was NULL", __func__);
714 if (!(la8 = sdi->priv)) {
715 sr_warn("la8: %s: sdi->priv was NULL", __func__);
719 switch (device_info_id) {
723 case SR_DI_NUM_PROBES:
724 info = GINT_TO_POINTER(NUM_PROBES);
726 case SR_DI_SAMPLERATES:
727 fill_supported_samplerates_if_needed();
730 case SR_DI_TRIGGER_TYPES:
731 info = (char *)TRIGGER_TYPES;
733 case SR_DI_CUR_SAMPLERATE:
734 info = &la8->cur_samplerate;
737 /* Unknown device info ID, return NULL. */
738 sr_warn("la8: %s: Unknown device info ID", __func__);
746 static int hw_get_status(int device_index)
748 struct sr_device_instance *sdi;
750 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
751 sr_warn("la8: %s: sdi was NULL, device not found", __func__);
752 return SR_ST_NOT_FOUND;
755 sr_dbg("la8: %s: returning status %d", __func__, sdi->status);
760 static int *hw_get_capabilities(void)
762 sr_dbg("la8: entering %s", __func__);
767 static int hw_set_configuration(int device_index, int capability, void *value)
769 struct sr_device_instance *sdi;
772 sr_dbg("la8: entering %s", __func__);
774 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
775 sr_warn("la8: %s: sdi was NULL", __func__);
776 return SR_ERR; /* TODO: SR_ERR_ARG? */
779 if (!(la8 = sdi->priv)) {
780 sr_warn("la8: %s: sdi->priv was NULL", __func__);
781 return SR_ERR; /* TODO: SR_ERR_ARG? */
784 switch (capability) {
785 case SR_HWCAP_SAMPLERATE:
786 if (set_samplerate(sdi, *(uint64_t *)value) == SR_ERR)
788 sr_dbg("la8: SAMPLERATE = %" PRIu64, la8->cur_samplerate);
790 case SR_HWCAP_PROBECONFIG:
791 if (configure_probes(la8, (GSList *)value) != SR_OK) {
792 sr_err("la8: %s: probe config failed", __func__);
796 case SR_HWCAP_LIMIT_MSEC:
797 if (*(uint64_t *)value == 0) {
798 sr_warn("la8: %s: LIMIT_MSEC can't be 0", __func__);
801 la8->limit_msec = *(uint64_t *)value;
802 sr_dbg("la8: LIMIT_MSEC = %" PRIu64, la8->limit_msec);
804 case SR_HWCAP_LIMIT_SAMPLES:
805 if (*(uint64_t *)value < MIN_NUM_SAMPLES) {
806 sr_warn("la8: %s: LIMIT_SAMPLES too small", __func__);
809 la8->limit_samples = *(uint64_t *)value;
810 sr_dbg("la8: LIMIT_SAMPLES = %" PRIu64, la8->limit_samples);
813 /* Unknown capability, return SR_ERR. */
814 sr_warn("la8: %s: Unknown capability", __func__);
823 * Get a block of 4096 bytes of data from the LA8.
825 * @param la8 The LA8 struct containing private per-device-instance data.
826 * @return SR_OK upon success, or SR_ERR upon errors.
828 static int la8_read_block(struct la8 *la8)
830 int i, byte_offset, m, mi, p, index, bytes_read;
834 sr_warn("la8: %s: la8 was NULL", __func__);
839 sr_warn("la8: %s: la8->ftdic was NULL", __func__);
843 // sr_dbg("la8: %s: reading block %d", __func__, la8->block_counter);
845 bytes_read = la8_read(la8, la8->mangled_buf, 4096);
847 /* If first block read got 0 bytes, retry until success or timeout. */
848 if ((bytes_read == 0) && (la8->block_counter == 0)) {
850 // sr_dbg("la8: %s: reading block 0 again", __func__);
851 bytes_read = la8_read(la8, la8->mangled_buf, 4096);
852 /* TODO: How to handle read errors here? */
854 } while ((la8->done > now) && (bytes_read == 0));
857 /* Check if block read was successful or a timeout occured. */
858 if (bytes_read != 4096) {
859 sr_warn("la8: %s: trigger timed out", __func__);
860 (void) la8_reset(la8); /* Ignore errors. */
864 /* De-mangle the data. */
865 // sr_dbg("la8: de-mangling samples of block %d", la8->block_counter);
866 byte_offset = la8->block_counter * 4096;
867 m = byte_offset / (1024 * 1024);
868 mi = m * (1024 * 1024);
869 for (i = 0; i < 4096; i++) {
871 index = m * 2 + (((byte_offset + i) - mi) / 2) * 16;
872 index += (la8->divcount == 0) ? p : (1 - p);
873 la8->final_buf[index] = la8->mangled_buf[i];
879 static int receive_data(int fd, int revents, void *user_data)
882 struct sr_device_instance *sdi;
883 struct sr_datafeed_packet packet;
886 /* Avoid compiler errors. */
890 if (!(sdi = user_data)) {
891 sr_warn("la8: %s: user_data was NULL", __func__);
895 if (!(la8 = sdi->priv)) {
896 sr_warn("la8: %s: sdi->priv was NULL", __func__);
900 /* Get one block of data (4096 bytes). */
901 if ((ret = la8_read_block(la8)) < 0) {
902 sr_warn("la8: %s: la8_read_block error: %d", __func__, ret);
903 hw_stop_acquisition(sdi->index, user_data);
907 /* We need to get exactly 2048 blocks (i.e. 8MB) of data. */
908 if (la8->block_counter != 2047) {
909 la8->block_counter++;
913 sr_dbg("la8: sampling finished, sending data to session bus now");
915 /* All data was received and demangled, send it to the session bus. */
916 for (i = 0; i < 2048; i++) {
917 /* Send a 4096 byte SR_DF_LOGIC packet to the session bus. */
918 // sr_dbg("la8: %s: sending SR_DF_LOGIC packet", __func__);
919 packet.type = SR_DF_LOGIC;
920 packet.length = 4096;
922 packet.payload = la8->final_buf + (i * 4096);
923 sr_session_bus(la8->session_id, &packet);
926 hw_stop_acquisition(sdi->index, user_data);
928 // return FALSE; /* FIXME? */
932 static int hw_start_acquisition(int device_index, gpointer session_device_id)
934 struct sr_device_instance *sdi;
936 struct sr_datafeed_packet packet;
937 struct sr_datafeed_header header;
941 sr_dbg("la8: entering %s", __func__);
943 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
944 sr_warn("la8: %s: sdi was NULL", __func__);
945 return SR_ERR; /* TODO: SR_ERR_ARG? */
948 if (!(la8 = sdi->priv)) {
949 sr_warn("la8: %s: sdi->priv was NULL", __func__);
950 return SR_ERR; /* TODO: SR_ERR_ARG? */
954 sr_warn("la8: %s: la8->ftdic was NULL", __func__);
958 la8->divcount = samplerate_to_divcount(la8->cur_samplerate);
959 if (la8->divcount == 0xff) {
960 sr_warn("la8: %s: invalid divcount/samplerate", __func__);
964 /* Fill acquisition parameters into buf[]. */
965 buf[0] = la8->divcount;
966 buf[1] = 0xff; /* This byte must always be 0xff. */
967 buf[2] = la8->trigger_pattern;
968 buf[3] = la8->trigger_mask;
970 /* Start acquisition. */
971 bytes_written = la8_write(la8, buf, 4);
973 if (bytes_written < 0) {
974 sr_warn("la8: acquisition failed to start");
976 } else if (bytes_written != 4) {
977 sr_warn("la8: acquisition failed to start");
978 return SR_ERR; /* TODO: Other error and return code? */
981 sr_dbg("la8: acquisition started successfully");
983 la8->session_id = session_device_id;
985 /* Send header packet to the session bus. */
986 sr_dbg("la8: %s: sending SR_DF_HEADER", __func__);
987 packet.type = SR_DF_HEADER;
988 packet.length = sizeof(struct sr_datafeed_header);
990 packet.payload = &header;
991 header.feed_version = 1;
992 gettimeofday(&header.starttime, NULL);
993 header.samplerate = la8->cur_samplerate;
994 header.protocol_id = SR_PROTO_RAW;
995 header.num_logic_probes = la8->num_probes;
996 header.num_analog_probes = 0;
997 sr_session_bus(session_device_id, &packet);
999 /* Time when we should be done (for detecting trigger timeouts). */
1000 la8->done = (la8->divcount + 1) * 0.08388608 + time(NULL)
1001 + la8->trigger_timeout;
1002 la8->block_counter = 0;
1004 /* Hook up a dummy handler to receive data from the LA8. */
1005 sr_source_add(-1, G_IO_IN, 0, receive_data, sdi);
1010 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
1012 struct sr_device_instance *sdi;
1014 struct sr_datafeed_packet packet;
1016 sr_dbg("la8: stopping acquisition");
1018 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
1019 sr_warn("la8: %s: sdi was NULL", __func__);
1023 if (!(la8 = sdi->priv)) {
1024 sr_warn("la8: %s: sdi->priv was NULL", __func__);
1028 /* Send end packet to the session bus. */
1029 sr_dbg("la8: %s: sending SR_DF_END", __func__);
1030 packet.type = SR_DF_END;
1032 packet.unitsize = 0;
1033 packet.payload = NULL;
1034 sr_session_bus(session_device_id, &packet);
1037 struct sr_device_plugin chronovu_la8_plugin_info = {
1038 .name = "chronovu-la8",
1039 .longname = "ChronoVu LA8",
1042 .cleanup = hw_cleanup,
1044 .close = hw_closedev,
1045 .get_device_info = hw_get_device_info,
1046 .get_status = hw_get_status,
1047 .get_capabilities = hw_get_capabilities,
1048 .set_configuration = hw_set_configuration,
1049 .start_acquisition = hw_start_acquisition,
1050 .stop_acquisition = hw_stop_acquisition,