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
25 #include <sigrok-internal.h>
27 #define USB_VENDOR_ID 0x0403
28 #define USB_PRODUCT_ID 0x6001
29 #define USB_DESCRIPTION "ChronoVu LA8"
30 #define USB_VENDOR_NAME "ChronoVu"
31 #define USB_MODEL_NAME "LA8"
32 #define USB_MODEL_VERSION ""
35 #define TRIGGER_TYPES "01"
36 #define SDRAM_SIZE (8 * 1024 * 1024)
37 #define MIN_NUM_SAMPLES 1
39 static GSList *device_instances = NULL;
42 /** FTDI device context (used by libftdi). */
43 struct ftdi_context *ftdic;
45 /** The currently configured samplerate of the device. */
46 uint64_t cur_samplerate;
48 /** The current sampling limit (in ms). */
51 /** The current sampling limit (in number of samples). */
52 uint64_t limit_samples;
58 * An 4KB buffer containing some (mangled) samples from the device.
59 * Format: Pretty mangled-up (due to hardware reasons), see code.
61 uint8_t mangled_buf[4096];
64 * An 8MB buffer where we'll store the de-mangled samples.
65 * Format: Each sample is 1 byte, MSB is channel 7, LSB is channel 0.
70 * Trigger pattern (MSB = channel 7, LSB = channel 0).
71 * A 1 bit matches a high signal, 0 matches a low signal on a probe.
72 * Only low/high triggers (but not e.g. rising/falling) are supported.
74 uint8_t trigger_pattern;
77 * Trigger mask (MSB = channel 7, LSB = channel 0).
78 * A 1 bit means "must match trigger_pattern", 0 means "don't care".
82 /** Time (in seconds) before the trigger times out. */
83 uint64_t trigger_timeout;
85 /** Tells us whether an SR_DF_TRIGGER packet was already sent. */
91 /** Counter/index for the data block (0..2047) to be read. */
94 /** The divcount value (determines the sample period) for the LA8. */
98 /* This will be initialized via hw_get_device_info()/SR_DI_SAMPLERATES. */
99 static uint64_t supported_samplerates[255 + 1] = { 0 };
102 * Min: 1 sample per 0.01us -> sample time is 0.084s, samplerate 100MHz
103 * Max: 1 sample per 2.55us -> sample time is 21.391s, samplerate 392.15kHz
105 static struct sr_samplerates samplerates = {
109 .list = supported_samplerates,
112 /* Note: Continuous sampling is not supported by the hardware. */
113 static int capabilities[] = {
114 SR_HWCAP_LOGIC_ANALYZER,
116 SR_HWCAP_LIMIT_MSEC, /* TODO: Not yet implemented. */
117 SR_HWCAP_LIMIT_SAMPLES, /* TODO: Not yet implemented. */
121 /* Function prototypes. */
122 static int la8_close_usb_reset_sequencer(struct la8 *la8);
123 static void hw_stop_acquisition(int device_index, gpointer session_device_id);
124 static int la8_reset(struct la8 *la8);
126 static void fill_supported_samplerates_if_needed(void)
130 /* Do nothing if supported_samplerates[] is already filled. */
131 if (supported_samplerates[0] != 0)
134 /* Fill supported_samplerates[] with the proper values. */
135 for (i = 0; i < 255; i++)
136 supported_samplerates[254 - i] = SR_MHZ(100) / (i + 1);
137 supported_samplerates[255] = 0;
141 * Check if the given samplerate is supported by the LA8 hardware.
143 * @param samplerate The samplerate (in Hz) to check.
144 * @return 1 if the samplerate is supported/valid, 0 otherwise.
146 static int is_valid_samplerate(uint64_t samplerate)
150 fill_supported_samplerates_if_needed();
152 for (i = 0; i < 255; i++) {
153 if (supported_samplerates[i] == samplerate)
157 sr_warn("la8: %s: invalid samplerate (%" PRIu64 "Hz)",
158 __func__, samplerate);
164 * Convert a samplerate (in Hz) to the 'divcount' value the LA8 wants.
166 * LA8 hardware: sample period = (divcount + 1) * 10ns.
167 * Min. value for divcount: 0x00 (10ns sample period, 100MHz samplerate).
168 * Max. value for divcount: 0xfe (2550ns sample period, 392.15kHz samplerate).
170 * @param samplerate The samplerate in Hz.
171 * @return The divcount value as needed by the hardware, or 0xff upon errors.
173 static uint8_t samplerate_to_divcount(uint64_t samplerate)
175 if (samplerate == 0) {
176 sr_err("la8: %s: samplerate was 0", __func__);
180 if (!is_valid_samplerate(samplerate)) {
181 sr_err("la8: %s: can't get divcount, samplerate invalid",
186 return (SR_MHZ(100) / samplerate) - 1;
190 * Write data of a certain length to the LA8's FTDI device.
192 * @param la8 The LA8 struct containing private per-device-instance data.
193 * @param buf The buffer containing the data to write.
194 * @param size The number of bytes to write.
195 * @return The number of bytes written, or a negative value upon errors.
197 static int la8_write(struct la8 *la8, uint8_t *buf, int size)
202 sr_err("la8: %s: la8 was NULL", __func__);
207 sr_err("la8: %s: la8->ftdic was NULL", __func__);
212 sr_err("la8: %s: buf was NULL", __func__);
217 sr_err("la8: %s: size was < 0", __func__);
221 bytes_written = ftdi_write_data(la8->ftdic, buf, size);
223 if (bytes_written < 0) {
224 sr_warn("la8: %s: ftdi_write_data: (%d) %s", __func__,
225 bytes_written, ftdi_get_error_string(la8->ftdic));
226 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
227 } else if (bytes_written != size) {
228 sr_warn("la8: %s: bytes to write: %d, bytes written: %d",
229 __func__, size, bytes_written);
230 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
233 return bytes_written;
237 * Read a certain amount of bytes from the LA8's FTDI device.
239 * @param la8 The LA8 struct containing private per-device-instance data.
240 * @param buf The buffer where the received data will be stored.
241 * @param size The number of bytes to read.
242 * @return The number of bytes read, or a negative value upon errors.
244 static int la8_read(struct la8 *la8, uint8_t *buf, int size)
249 sr_err("la8: %s: la8 was NULL", __func__);
254 sr_err("la8: %s: la8->ftdic was NULL", __func__);
259 sr_err("la8: %s: buf was NULL", __func__);
264 sr_err("la8: %s: size was <= 0", __func__);
268 bytes_read = ftdi_read_data(la8->ftdic, buf, size);
270 if (bytes_read < 0) {
271 sr_warn("la8: %s: ftdi_read_data: (%d) %s", __func__,
272 bytes_read, ftdi_get_error_string(la8->ftdic));
273 } else if (bytes_read != size) {
274 // sr_warn("la8: %s: bytes to read: %d, bytes read: %d",
275 // __func__, size, bytes_read);
281 static int la8_close(struct la8 *la8)
286 sr_err("la8: %s: la8 was NULL", __func__);
291 sr_err("la8: %s: la8->ftdic was NULL", __func__);
295 if ((ret = ftdi_usb_close(la8->ftdic)) < 0) {
296 sr_warn("la8: %s: ftdi_usb_close: (%d) %s",
297 __func__, ret, ftdi_get_error_string(la8->ftdic));
304 * Close the ChronoVu LA8 USB port and reset the LA8 sequencer logic.
306 * @param la8 The LA8 struct containing private per-device-instance data.
307 * @return SR_OK upon success, SR_ERR upon failure.
309 static int la8_close_usb_reset_sequencer(struct la8 *la8)
311 /* Magic sequence of bytes for resetting the LA8 sequencer logic. */
312 uint8_t buf[8] = {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};
315 sr_dbg("la8: entering %s", __func__);
318 sr_err("la8: %s: la8 was NULL", __func__);
323 sr_err("la8: %s: la8->ftdic was NULL", __func__);
327 if (la8->ftdic->usb_dev) {
328 /* Reset the LA8 sequencer logic, then wait 100ms. */
329 sr_dbg("la8: resetting sequencer logic");
330 (void) la8_write(la8, buf, 8); /* Ignore errors. */
331 g_usleep(100 * 1000);
333 /* Purge FTDI buffers, then reset and close the FTDI device. */
334 sr_dbg("la8: purging buffers, resetting+closing FTDI device");
336 /* Log errors, but ignore them (i.e., don't abort). */
337 if ((ret = ftdi_usb_purge_buffers(la8->ftdic)) < 0)
338 sr_warn("la8: %s: ftdi_usb_purge_buffers: (%d) %s",
339 __func__, ret, ftdi_get_error_string(la8->ftdic));
340 if ((ret = ftdi_usb_reset(la8->ftdic)) < 0)
341 sr_warn("la8: %s: ftdi_usb_reset: (%d) %s", __func__,
342 ret, ftdi_get_error_string(la8->ftdic));
343 if ((ret = ftdi_usb_close(la8->ftdic)) < 0)
344 sr_warn("la8: %s: ftdi_usb_close: (%d) %s", __func__,
345 ret, ftdi_get_error_string(la8->ftdic));
347 sr_dbg("la8: %s: usb_dev was NULL, nothing to do", __func__);
350 ftdi_free(la8->ftdic); /* Returns void. */
357 * Reset the ChronoVu LA8.
359 * The LA8 must be reset after a failed read/write operation or upon timeouts.
361 * @param la8 The LA8 struct containing private per-device-instance data.
362 * @return SR_OK upon success, SR_ERR upon failure.
364 static int la8_reset(struct la8 *la8)
371 sr_err("la8: %s: la8 was NULL", __func__);
376 sr_err("la8: %s: la8->ftdic was NULL", __func__);
380 sr_dbg("la8: resetting the device");
383 * Purge pending read data from the FTDI hardware FIFO until
384 * no more data is left, or a timeout occurs (after 20s).
386 done = 20 + time(NULL);
388 /* TODO: Ignore errors? Check for < 0 at least! */
389 bytes_read = la8_read(la8, (uint8_t *)&buf, 4096);
391 } while ((done > now) && (bytes_read > 0));
393 /* Reset the LA8 sequencer logic and close the USB port. */
394 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
396 sr_dbg("la8: device reset finished");
401 static int configure_probes(struct la8 *la8, GSList *probes)
403 struct sr_probe *probe;
408 la8->trigger_pattern = 0;
409 la8->trigger_mask = 0; /* Default to "don't care" for all probes. */
411 for (l = probes; l; l = l->next) {
412 probe = (struct sr_probe *)l->data;
415 sr_err("la8: %s: probe was NULL", __func__);
419 /* Skip disabled probes. */
423 /* Skip (enabled) probes with no configured trigger. */
427 /* Note: Must only be run if probe->trigger != NULL. */
428 if (probe->index < 0 || probe->index > 7) {
429 sr_err("la8: %s: invalid probe index %d, must be "
430 "between 0 and 7", __func__, probe->index);
434 probe_bit = (1 << (probe->index - 1));
436 /* Configure the probe's trigger mask and trigger pattern. */
437 for (tc = probe->trigger; tc && *tc; tc++) {
438 la8->trigger_mask |= probe_bit;
440 /* Sanity check, LA8 only supports low/high trigger. */
441 if (*tc != '0' && *tc != '1') {
442 sr_err("la8: %s: invalid trigger '%c', only "
443 "'0'/'1' supported", __func__, *tc);
448 la8->trigger_pattern |= probe_bit;
452 sr_dbg("la8: %s: trigger_mask = 0x%x, trigger_pattern = 0x%x",
453 __func__, la8->trigger_mask, la8->trigger_pattern);
458 static int hw_init(const char *deviceinfo)
461 struct sr_device_instance *sdi;
464 sr_dbg("la8: entering %s", __func__);
466 /* Avoid compiler errors. */
467 deviceinfo = deviceinfo;
469 /* Allocate memory for our private driver context. */
470 if (!(la8 = g_try_malloc(sizeof(struct la8)))) {
471 sr_err("la8: %s: struct la8 malloc failed", __func__);
473 goto err_free_nothing;
476 /* Set some sane defaults. */
478 la8->cur_samplerate = SR_MHZ(100); /* 100MHz == max. samplerate */
480 la8->limit_samples = 0;
481 la8->session_id = NULL;
482 memset(la8->mangled_buf, 0, 4096);
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. */
487 la8->trigger_found = 0;
489 la8->block_counter = 0;
490 la8->divcount = 0; /* 10ns sample period == 100MHz samplerate */
492 /* Allocate memory where we'll store the de-mangled data. */
493 if (!(la8->final_buf = g_try_malloc(SDRAM_SIZE))) {
494 sr_err("la8: %s: final_buf malloc failed", __func__);
499 /* Allocate memory for the FTDI context (ftdic) and initialize it. */
500 if (!(la8->ftdic = ftdi_new())) {
501 sr_err("la8: %s: ftdi_new failed", __func__);
502 ret = SR_ERR; /* TODO: More specific error? */
503 goto err_free_final_buf;
506 /* Check for the device and temporarily open it. */
507 if ((ret = ftdi_usb_open_desc(la8->ftdic, USB_VENDOR_ID,
508 USB_PRODUCT_ID, USB_DESCRIPTION, NULL)) < 0) {
509 sr_err("la8: %s: ftdi_usb_open_desc: (%d) %s",
510 __func__, ret, ftdi_get_error_string(la8->ftdic));
511 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
512 ret = SR_ERR; /* TODO: More specific error? */
515 sr_dbg("la8: found device");
517 /* Register the device with libsigrok. */
518 sdi = sr_device_instance_new(0, SR_ST_INITIALIZING,
519 USB_VENDOR_NAME, USB_MODEL_NAME, USB_MODEL_VERSION);
521 sr_err("la8: %s: sr_device_instance_new failed", __func__);
522 ret = SR_ERR; /* TODO: More specific error? */
523 goto err_close_ftdic;
528 device_instances = g_slist_append(device_instances, sdi);
530 sr_dbg("la8: %s finished successfully", __func__);
532 /* Close device. We'll reopen it again when we need it. */
533 (void) la8_close(la8); /* Log, but ignore errors. */
535 // return SR_OK; /* TODO */
539 (void) la8_close(la8); /* Log, but ignore errors. */
541 free(la8->ftdic); /* NOT g_free()! */
543 g_free(la8->final_buf);
547 // return ret; /* TODO */
551 static int hw_opendev(int device_index)
554 struct sr_device_instance *sdi;
557 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
558 sr_err("la8: %s: sdi was NULL", __func__);
559 return SR_ERR; /* TODO: SR_ERR_ARG? */
562 if (!(la8 = sdi->priv)) {
563 sr_err("la8: %s: sdi->priv was NULL", __func__);
564 return SR_ERR; /* TODO: SR_ERR_ARG? */
567 sr_dbg("la8: opening device");
569 /* Open the device. */
570 if ((ret = ftdi_usb_open_desc(la8->ftdic, USB_VENDOR_ID,
571 USB_PRODUCT_ID, USB_DESCRIPTION, NULL)) < 0) {
572 sr_err("la8: %s: ftdi_usb_open_desc: (%d) %s",
573 __func__, ret, ftdi_get_error_string(la8->ftdic));
574 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
577 sr_dbg("la8: device opened successfully");
579 /* Purge RX/TX buffers in the FTDI chip. */
580 if ((ret = ftdi_usb_purge_buffers(la8->ftdic)) < 0) {
581 sr_err("la8: %s: ftdi_usb_purge_buffers: (%d) %s",
582 __func__, ret, ftdi_get_error_string(la8->ftdic));
583 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
584 goto err_opendev_close_ftdic;
586 sr_dbg("la8: FTDI buffers purged successfully");
588 /* Enable flow control in the FTDI chip. */
589 if ((ret = ftdi_setflowctrl(la8->ftdic, SIO_RTS_CTS_HS)) < 0) {
590 sr_err("la8: %s: ftdi_setflowcontrol: (%d) %s",
591 __func__, ret, ftdi_get_error_string(la8->ftdic));
592 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
593 goto err_opendev_close_ftdic;
595 sr_dbg("la8: FTDI flow control enabled successfully");
598 g_usleep(100 * 1000);
600 sdi->status = SR_ST_ACTIVE;
604 err_opendev_close_ftdic:
605 (void) la8_close(la8); /* Log, but ignore errors. */
609 static int set_samplerate(struct sr_device_instance *sdi, uint64_t samplerate)
614 sr_err("la8: %s: sdi was NULL", __func__);
618 if (!(la8 = sdi->priv)) {
619 sr_err("la8: %s: sdi->priv was NULL", __func__);
623 sr_dbg("la8: setting samplerate");
625 fill_supported_samplerates_if_needed();
627 /* Check if this is a samplerate supported by the hardware. */
628 if (!is_valid_samplerate(samplerate))
631 /* Set the new samplerate. */
632 la8->cur_samplerate = samplerate;
634 sr_dbg("la8: samplerate set to %" PRIu64 "Hz", la8->cur_samplerate);
639 static int hw_closedev(int device_index)
641 struct sr_device_instance *sdi;
644 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
645 sr_err("la8: %s: sdi was NULL", __func__);
646 return SR_ERR; /* TODO: SR_ERR_ARG? */
649 if (!(la8 = sdi->priv)) {
650 sr_err("la8: %s: sdi->priv was NULL", __func__);
651 return SR_ERR; /* TODO: SR_ERR_ARG? */
654 sr_dbg("la8: closing device");
656 if (sdi->status == SR_ST_ACTIVE) {
657 sr_dbg("la8: %s: status ACTIVE, closing device", __func__);
658 /* TODO: Really ignore errors here, or return SR_ERR? */
659 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
661 sr_dbg("la8: %s: status not ACTIVE, nothing to do", __func__);
664 sdi->status = SR_ST_INACTIVE;
666 sr_dbg("la8: %s: freeing sample buffers", __func__);
667 g_free(la8->final_buf);
672 static void hw_cleanup(void)
675 struct sr_device_instance *sdi;
677 sr_dbg("la8: entering %s", __func__);
679 /* Properly close all devices. */
680 for (l = device_instances; l; l = l->next) {
681 if ((sdi = l->data) == NULL) {
682 sr_warn("la8: %s: sdi was NULL, continuing", __func__);
685 if (sdi->priv != NULL)
688 sr_warn("la8: %s: sdi->priv was NULL, nothing "
690 sr_device_instance_free(sdi); /* Returns void. */
692 g_slist_free(device_instances); /* Returns void. */
693 device_instances = NULL;
696 static void *hw_get_device_info(int device_index, int device_info_id)
698 struct sr_device_instance *sdi;
702 sr_dbg("la8: entering %s", __func__);
704 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
705 sr_err("la8: %s: sdi was NULL", __func__);
709 if (!(la8 = sdi->priv)) {
710 sr_err("la8: %s: sdi->priv was NULL", __func__);
714 switch (device_info_id) {
718 case SR_DI_NUM_PROBES:
719 info = GINT_TO_POINTER(NUM_PROBES);
721 case SR_DI_SAMPLERATES:
722 fill_supported_samplerates_if_needed();
725 case SR_DI_TRIGGER_TYPES:
726 info = (char *)TRIGGER_TYPES;
728 case SR_DI_CUR_SAMPLERATE:
729 info = &la8->cur_samplerate;
732 /* Unknown device info ID, return NULL. */
733 sr_err("la8: %s: Unknown device info ID", __func__);
741 static int hw_get_status(int device_index)
743 struct sr_device_instance *sdi;
745 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
746 sr_warn("la8: %s: sdi was NULL, device not found", __func__);
747 return SR_ST_NOT_FOUND;
750 sr_dbg("la8: %s: returning status %d", __func__, sdi->status);
755 static int *hw_get_capabilities(void)
757 sr_dbg("la8: entering %s", __func__);
762 static int hw_set_configuration(int device_index, int capability, void *value)
764 struct sr_device_instance *sdi;
767 sr_dbg("la8: entering %s", __func__);
769 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
770 sr_err("la8: %s: sdi was NULL", __func__);
771 return SR_ERR; /* TODO: SR_ERR_ARG? */
774 if (!(la8 = sdi->priv)) {
775 sr_err("la8: %s: sdi->priv was NULL", __func__);
776 return SR_ERR; /* TODO: SR_ERR_ARG? */
779 switch (capability) {
780 case SR_HWCAP_SAMPLERATE:
781 if (set_samplerate(sdi, *(uint64_t *)value) == SR_ERR)
783 sr_dbg("la8: SAMPLERATE = %" PRIu64, la8->cur_samplerate);
785 case SR_HWCAP_PROBECONFIG:
786 if (configure_probes(la8, (GSList *)value) != SR_OK) {
787 sr_err("la8: %s: probe config failed", __func__);
791 case SR_HWCAP_LIMIT_MSEC:
792 if (*(uint64_t *)value == 0) {
793 sr_err("la8: %s: LIMIT_MSEC can't be 0", __func__);
796 la8->limit_msec = *(uint64_t *)value;
797 sr_dbg("la8: LIMIT_MSEC = %" PRIu64, la8->limit_msec);
799 case SR_HWCAP_LIMIT_SAMPLES:
800 if (*(uint64_t *)value < MIN_NUM_SAMPLES) {
801 sr_err("la8: %s: LIMIT_SAMPLES too small", __func__);
804 la8->limit_samples = *(uint64_t *)value;
805 sr_dbg("la8: LIMIT_SAMPLES = %" PRIu64, la8->limit_samples);
808 /* Unknown capability, return SR_ERR. */
809 sr_err("la8: %s: Unknown capability", __func__);
818 * Get a block of 4096 bytes of data from the LA8.
820 * @param la8 The LA8 struct containing private per-device-instance data.
821 * @return SR_OK upon success, or SR_ERR upon errors.
823 static int la8_read_block(struct la8 *la8)
825 int i, byte_offset, m, mi, p, index, bytes_read;
829 sr_err("la8: %s: la8 was NULL", __func__);
834 sr_err("la8: %s: la8->ftdic was NULL", __func__);
838 // sr_dbg("la8: %s: reading block %d", __func__, la8->block_counter);
840 bytes_read = la8_read(la8, la8->mangled_buf, 4096);
842 /* If first block read got 0 bytes, retry until success or timeout. */
843 if ((bytes_read == 0) && (la8->block_counter == 0)) {
845 // sr_dbg("la8: %s: reading block 0 again", __func__);
846 bytes_read = la8_read(la8, la8->mangled_buf, 4096);
847 /* TODO: How to handle read errors here? */
849 } while ((la8->done > now) && (bytes_read == 0));
852 /* Check if block read was successful or a timeout occured. */
853 if (bytes_read != 4096) {
854 sr_warn("la8: %s: trigger timed out", __func__);
855 (void) la8_reset(la8); /* Ignore errors. */
859 /* De-mangle the data. */
860 // sr_dbg("la8: de-mangling samples of block %d", la8->block_counter);
861 byte_offset = la8->block_counter * 4096;
862 m = byte_offset / (1024 * 1024);
863 mi = m * (1024 * 1024);
864 for (i = 0; i < 4096; i++) {
866 index = m * 2 + (((byte_offset + i) - mi) / 2) * 16;
867 index += (la8->divcount == 0) ? p : (1 - p);
868 la8->final_buf[index] = la8->mangled_buf[i];
874 static void send_block_to_session_bus(struct la8 *la8, int block)
877 uint8_t sample, expected_sample;
878 struct sr_datafeed_packet packet;
879 int trigger_point; /* Relative trigger point (in this block). */
881 /* Note: No sanity checks on la8/block, caller is responsible. */
883 /* Check if we can find the trigger condition in this block. */
885 expected_sample = la8->trigger_pattern & la8->trigger_mask;
886 for (i = 0; i < 4096; i++) {
887 /* Don't continue if the trigger was found previously. */
888 if (la8->trigger_found)
891 sample = *(la8->final_buf + (block * 4096) + i);
893 if ((sample & la8->trigger_mask) == expected_sample) {
895 la8->trigger_found = 1;
900 /* If no trigger was found, send one SR_DF_LOGIC packet. */
901 if (trigger_point == -1) {
902 /* Send a 4096 byte SR_DF_LOGIC packet to the session bus. */
903 // sr_dbg("la8: %s: sending SR_DF_LOGIC packet", __func__);
904 packet.type = SR_DF_LOGIC;
905 packet.length = 4096;
907 packet.payload = la8->final_buf + (block * 4096);
908 sr_session_bus(la8->session_id, &packet);
913 * We found the trigger, so some special handling is needed. We have
914 * to send an SR_DF_LOGIC packet with the samples before the trigger
915 * (if any), then the SD_DF_TRIGGER packet itself, then another
916 * SR_DF_LOGIC packet with the samples after the trigger (if any).
919 /* TODO: Send SR_DF_TRIGGER packet before or after the actual sample? */
921 /* If at least one sample is located before the trigger... */
922 if (trigger_point > 0) {
923 /* Send pre-trigger SR_DF_LOGIC packet to the session bus. */
924 sr_dbg("la8: %s: sending pre-trigger SR_DF_LOGIC packet, ",
925 "start = %" PRIu64 ", length = %d", __func__,
926 block * 4096, trigger_point);
927 packet.type = SR_DF_LOGIC;
928 packet.length = trigger_point;
930 packet.payload = la8->final_buf + (block * 4096);
931 sr_session_bus(la8->session_id, &packet);
934 /* Send the SR_DF_TRIGGER packet to the session bus. */
935 sr_dbg("la8: %s: sending SR_DF_TRIGGER packet, sample = %" PRIu64,
936 __func__, (block * 4096) + trigger_point);
937 packet.type = SR_DF_TRIGGER;
940 packet.payload = NULL;
941 sr_session_bus(la8->session_id, &packet);
943 /* If at least one sample is located after the trigger... */
944 if (trigger_point < (4096 - 1)) {
945 /* Send post-trigger SR_DF_LOGIC packet to the session bus. */
946 sr_dbg("la8: %s: sending post-trigger SR_DF_LOGIC packet, ",
947 "start = %" PRIu64 ", length = %d", __func__,
948 (block * 4096) + trigger_point,
949 (4096 - 1) - trigger_point);
950 packet.type = SR_DF_LOGIC;
951 packet.length = (4096 - 1) - trigger_point;
953 packet.payload = la8->final_buf + (block * 4096)
955 sr_session_bus(la8->session_id, &packet);
959 static int receive_data(int fd, int revents, void *user_data)
962 struct sr_device_instance *sdi;
965 /* Avoid compiler errors. */
969 if (!(sdi = user_data)) {
970 sr_err("la8: %s: user_data was NULL", __func__);
974 if (!(la8 = sdi->priv)) {
975 sr_err("la8: %s: sdi->priv was NULL", __func__);
979 /* Get one block of data (4096 bytes). */
980 if ((ret = la8_read_block(la8)) < 0) {
981 sr_err("la8: %s: la8_read_block error: %d", __func__, ret);
982 hw_stop_acquisition(sdi->index, user_data);
986 /* We need to get exactly 2048 blocks (i.e. 8MB) of data. */
987 if (la8->block_counter != 2047) {
988 la8->block_counter++;
992 sr_dbg("la8: sampling finished, sending data to session bus now");
994 /* All data was received and demangled, send it to the session bus. */
995 for (i = 0; i < 2048; i++)
996 send_block_to_session_bus(la8, i);
998 hw_stop_acquisition(sdi->index, user_data);
1000 // return FALSE; /* FIXME? */
1004 static int hw_start_acquisition(int device_index, gpointer session_device_id)
1006 struct sr_device_instance *sdi;
1008 struct sr_datafeed_packet packet;
1009 struct sr_datafeed_header header;
1013 sr_dbg("la8: entering %s", __func__);
1015 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
1016 sr_err("la8: %s: sdi was NULL", __func__);
1017 return SR_ERR; /* TODO: SR_ERR_ARG? */
1020 if (!(la8 = sdi->priv)) {
1021 sr_err("la8: %s: sdi->priv was NULL", __func__);
1022 return SR_ERR; /* TODO: SR_ERR_ARG? */
1026 sr_err("la8: %s: la8->ftdic was NULL", __func__);
1030 la8->divcount = samplerate_to_divcount(la8->cur_samplerate);
1031 if (la8->divcount == 0xff) {
1032 sr_err("la8: %s: invalid divcount/samplerate", __func__);
1036 /* Fill acquisition parameters into buf[]. */
1037 buf[0] = la8->divcount;
1038 buf[1] = 0xff; /* This byte must always be 0xff. */
1039 buf[2] = la8->trigger_pattern;
1040 buf[3] = la8->trigger_mask;
1042 /* Start acquisition. */
1043 bytes_written = la8_write(la8, buf, 4);
1045 if (bytes_written < 0) {
1046 sr_err("la8: acquisition failed to start");
1048 } else if (bytes_written != 4) {
1049 sr_err("la8: acquisition failed to start");
1050 return SR_ERR; /* TODO: Other error and return code? */
1053 sr_dbg("la8: acquisition started successfully");
1055 la8->session_id = session_device_id;
1057 /* Send header packet to the session bus. */
1058 sr_dbg("la8: %s: sending SR_DF_HEADER", __func__);
1059 packet.type = SR_DF_HEADER;
1060 packet.length = sizeof(struct sr_datafeed_header);
1061 packet.unitsize = 0;
1062 packet.payload = &header;
1063 header.feed_version = 1;
1064 gettimeofday(&header.starttime, NULL);
1065 header.samplerate = la8->cur_samplerate;
1066 header.protocol_id = SR_PROTO_RAW;
1067 header.num_logic_probes = NUM_PROBES;
1068 header.num_analog_probes = 0;
1069 sr_session_bus(session_device_id, &packet);
1071 /* Time when we should be done (for detecting trigger timeouts). */
1072 la8->done = (la8->divcount + 1) * 0.08388608 + time(NULL)
1073 + la8->trigger_timeout;
1074 la8->block_counter = 0;
1075 la8->trigger_found = 0;
1077 /* Hook up a dummy handler to receive data from the LA8. */
1078 sr_source_add(-1, G_IO_IN, 0, receive_data, sdi);
1083 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
1085 struct sr_device_instance *sdi;
1087 struct sr_datafeed_packet packet;
1089 sr_dbg("la8: stopping acquisition");
1091 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
1092 sr_err("la8: %s: sdi was NULL", __func__);
1096 if (!(la8 = sdi->priv)) {
1097 sr_err("la8: %s: sdi->priv was NULL", __func__);
1101 /* Send end packet to the session bus. */
1102 sr_dbg("la8: %s: sending SR_DF_END", __func__);
1103 packet.type = SR_DF_END;
1105 packet.unitsize = 0;
1106 packet.payload = NULL;
1107 sr_session_bus(session_device_id, &packet);
1110 struct sr_device_plugin chronovu_la8_plugin_info = {
1111 .name = "chronovu-la8",
1112 .longname = "ChronoVu LA8",
1115 .cleanup = hw_cleanup,
1116 .opendev = hw_opendev,
1117 .closedev = hw_closedev,
1118 .get_device_info = hw_get_device_info,
1119 .get_status = hw_get_status,
1120 .get_capabilities = hw_get_capabilities,
1121 .set_configuration = hw_set_configuration,
1122 .start_acquisition = hw_start_acquisition,
1123 .stop_acquisition = hw_stop_acquisition,