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 #define BS 4096 /* Block size */
40 #define NUM_BLOCKS 2048 /* Number of blocks */
42 static GSList *device_instances = NULL;
45 /** FTDI device context (used by libftdi). */
46 struct ftdi_context *ftdic;
48 /** The currently configured samplerate of the device. */
49 uint64_t cur_samplerate;
51 /** period in picoseconds corresponding to the samplerate */
54 /** The current sampling limit (in ms). */
57 /** The current sampling limit (in number of samples). */
58 uint64_t limit_samples;
64 * A buffer containing some (mangled) samples from the device.
65 * Format: Pretty mangled-up (due to hardware reasons), see code.
67 uint8_t mangled_buf[BS];
70 * An 8MB buffer where we'll store the de-mangled samples.
71 * Format: Each sample is 1 byte, MSB is channel 7, LSB is channel 0.
76 * Trigger pattern (MSB = channel 7, LSB = channel 0).
77 * A 1 bit matches a high signal, 0 matches a low signal on a probe.
78 * Only low/high triggers (but not e.g. rising/falling) are supported.
80 uint8_t trigger_pattern;
83 * Trigger mask (MSB = channel 7, LSB = channel 0).
84 * A 1 bit means "must match trigger_pattern", 0 means "don't care".
88 /** Time (in seconds) before the trigger times out. */
89 uint64_t trigger_timeout;
91 /** Tells us whether an SR_DF_TRIGGER packet was already sent. */
97 /** Counter/index for the data block to be read. */
100 /** The divcount value (determines the sample period) for the LA8. */
104 /* This will be initialized via hw_get_device_info()/SR_DI_SAMPLERATES. */
105 static uint64_t supported_samplerates[255 + 1] = { 0 };
108 * Min: 1 sample per 0.01us -> sample time is 0.084s, samplerate 100MHz
109 * Max: 1 sample per 2.55us -> sample time is 21.391s, samplerate 392.15kHz
111 static struct sr_samplerates samplerates = {
115 .list = supported_samplerates,
118 /* Note: Continuous sampling is not supported by the hardware. */
119 static int capabilities[] = {
120 SR_HWCAP_LOGIC_ANALYZER,
122 SR_HWCAP_LIMIT_MSEC, /* TODO: Not yet implemented. */
123 SR_HWCAP_LIMIT_SAMPLES, /* TODO: Not yet implemented. */
127 /* Function prototypes. */
128 static int la8_close_usb_reset_sequencer(struct la8 *la8);
129 static void hw_stop_acquisition(int device_index, gpointer session_data);
130 static int la8_reset(struct la8 *la8);
132 static void fill_supported_samplerates_if_needed(void)
136 /* Do nothing if supported_samplerates[] is already filled. */
137 if (supported_samplerates[0] != 0)
140 /* Fill supported_samplerates[] with the proper values. */
141 for (i = 0; i < 255; i++)
142 supported_samplerates[254 - i] = SR_MHZ(100) / (i + 1);
143 supported_samplerates[255] = 0;
147 * Check if the given samplerate is supported by the LA8 hardware.
149 * @param samplerate The samplerate (in Hz) to check.
150 * @return 1 if the samplerate is supported/valid, 0 otherwise.
152 static int is_valid_samplerate(uint64_t samplerate)
156 fill_supported_samplerates_if_needed();
158 for (i = 0; i < 255; i++) {
159 if (supported_samplerates[i] == samplerate)
163 sr_warn("la8: %s: invalid samplerate (%" PRIu64 "Hz)",
164 __func__, samplerate);
170 * Convert a samplerate (in Hz) to the 'divcount' value the LA8 wants.
172 * LA8 hardware: sample period = (divcount + 1) * 10ns.
173 * Min. value for divcount: 0x00 (10ns sample period, 100MHz samplerate).
174 * Max. value for divcount: 0xfe (2550ns sample period, 392.15kHz samplerate).
176 * @param samplerate The samplerate in Hz.
177 * @return The divcount value as needed by the hardware, or 0xff upon errors.
179 static uint8_t samplerate_to_divcount(uint64_t samplerate)
181 if (samplerate == 0) {
182 sr_err("la8: %s: samplerate was 0", __func__);
186 if (!is_valid_samplerate(samplerate)) {
187 sr_err("la8: %s: can't get divcount, samplerate invalid",
192 return (SR_MHZ(100) / samplerate) - 1;
196 * Write data of a certain length to the LA8's FTDI device.
198 * @param la8 The LA8 struct containing private per-device-instance data.
199 * @param buf The buffer containing the data to write.
200 * @param size The number of bytes to write.
201 * @return The number of bytes written, or a negative value upon errors.
203 static int la8_write(struct la8 *la8, uint8_t *buf, int size)
208 sr_err("la8: %s: la8 was NULL", __func__);
213 sr_err("la8: %s: la8->ftdic was NULL", __func__);
218 sr_err("la8: %s: buf was NULL", __func__);
223 sr_err("la8: %s: size was < 0", __func__);
227 bytes_written = ftdi_write_data(la8->ftdic, buf, size);
229 if (bytes_written < 0) {
230 sr_warn("la8: %s: ftdi_write_data: (%d) %s", __func__,
231 bytes_written, ftdi_get_error_string(la8->ftdic));
232 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
233 } else if (bytes_written != size) {
234 sr_warn("la8: %s: bytes to write: %d, bytes written: %d",
235 __func__, size, bytes_written);
236 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
239 return bytes_written;
243 * Read a certain amount of bytes from the LA8's FTDI device.
245 * @param la8 The LA8 struct containing private per-device-instance data.
246 * @param buf The buffer where the received data will be stored.
247 * @param size The number of bytes to read.
248 * @return The number of bytes read, or a negative value upon errors.
250 static int la8_read(struct la8 *la8, uint8_t *buf, int size)
255 sr_err("la8: %s: la8 was NULL", __func__);
260 sr_err("la8: %s: la8->ftdic was NULL", __func__);
265 sr_err("la8: %s: buf was NULL", __func__);
270 sr_err("la8: %s: size was <= 0", __func__);
274 bytes_read = ftdi_read_data(la8->ftdic, buf, size);
276 if (bytes_read < 0) {
277 sr_warn("la8: %s: ftdi_read_data: (%d) %s", __func__,
278 bytes_read, ftdi_get_error_string(la8->ftdic));
279 } else if (bytes_read != size) {
280 // sr_warn("la8: %s: bytes to read: %d, bytes read: %d",
281 // __func__, size, bytes_read);
287 static int la8_close(struct la8 *la8)
292 sr_err("la8: %s: la8 was NULL", __func__);
297 sr_err("la8: %s: la8->ftdic was NULL", __func__);
301 if ((ret = ftdi_usb_close(la8->ftdic)) < 0) {
302 sr_warn("la8: %s: ftdi_usb_close: (%d) %s",
303 __func__, ret, ftdi_get_error_string(la8->ftdic));
310 * Close the ChronoVu LA8 USB port and reset the LA8 sequencer logic.
312 * @param la8 The LA8 struct containing private per-device-instance data.
313 * @return SR_OK upon success, SR_ERR upon failure.
315 static int la8_close_usb_reset_sequencer(struct la8 *la8)
317 /* Magic sequence of bytes for resetting the LA8 sequencer logic. */
318 uint8_t buf[8] = {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};
321 sr_spew("la8: entering %s", __func__);
324 sr_err("la8: %s: la8 was NULL", __func__);
329 sr_err("la8: %s: la8->ftdic was NULL", __func__);
333 if (la8->ftdic->usb_dev) {
334 /* Reset the LA8 sequencer logic, then wait 100ms. */
335 sr_dbg("la8: resetting sequencer logic");
336 (void) la8_write(la8, buf, 8); /* Ignore errors. */
337 g_usleep(100 * 1000);
339 /* Purge FTDI buffers, then reset and close the FTDI device. */
340 sr_dbg("la8: purging buffers, resetting+closing FTDI device");
342 /* Log errors, but ignore them (i.e., don't abort). */
343 if ((ret = ftdi_usb_purge_buffers(la8->ftdic)) < 0)
344 sr_warn("la8: %s: ftdi_usb_purge_buffers: (%d) %s",
345 __func__, ret, ftdi_get_error_string(la8->ftdic));
346 if ((ret = ftdi_usb_reset(la8->ftdic)) < 0)
347 sr_warn("la8: %s: ftdi_usb_reset: (%d) %s", __func__,
348 ret, ftdi_get_error_string(la8->ftdic));
349 if ((ret = ftdi_usb_close(la8->ftdic)) < 0)
350 sr_warn("la8: %s: ftdi_usb_close: (%d) %s", __func__,
351 ret, ftdi_get_error_string(la8->ftdic));
353 sr_spew("la8: %s: usb_dev was NULL, nothing to do", __func__);
356 ftdi_free(la8->ftdic); /* Returns void. */
363 * Reset the ChronoVu LA8.
365 * The LA8 must be reset after a failed read/write operation or upon timeouts.
367 * @param la8 The LA8 struct containing private per-device-instance data.
368 * @return SR_OK upon success, SR_ERR upon failure.
370 static int la8_reset(struct la8 *la8)
377 sr_err("la8: %s: la8 was NULL", __func__);
382 sr_err("la8: %s: la8->ftdic was NULL", __func__);
386 sr_dbg("la8: resetting the device");
389 * Purge pending read data from the FTDI hardware FIFO until
390 * no more data is left, or a timeout occurs (after 20s).
392 done = 20 + time(NULL);
394 /* TODO: Ignore errors? Check for < 0 at least! */
395 bytes_read = la8_read(la8, (uint8_t *)&buf, BS);
397 } while ((done > now) && (bytes_read > 0));
399 /* Reset the LA8 sequencer logic and close the USB port. */
400 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
402 sr_dbg("la8: device reset finished");
407 static int configure_probes(struct la8 *la8, GSList *probes)
409 struct sr_probe *probe;
414 la8->trigger_pattern = 0;
415 la8->trigger_mask = 0; /* Default to "don't care" for all probes. */
417 for (l = probes; l; l = l->next) {
418 probe = (struct sr_probe *)l->data;
421 sr_err("la8: %s: probe was NULL", __func__);
425 /* Skip disabled probes. */
429 /* Skip (enabled) probes with no configured trigger. */
433 /* Note: Must only be run if probe->trigger != NULL. */
434 if (probe->index < 0 || probe->index > 7) {
435 sr_err("la8: %s: invalid probe index %d, must be "
436 "between 0 and 7", __func__, probe->index);
440 probe_bit = (1 << (probe->index - 1));
442 /* Configure the probe's trigger mask and trigger pattern. */
443 for (tc = probe->trigger; tc && *tc; tc++) {
444 la8->trigger_mask |= probe_bit;
446 /* Sanity check, LA8 only supports low/high trigger. */
447 if (*tc != '0' && *tc != '1') {
448 sr_err("la8: %s: invalid trigger '%c', only "
449 "'0'/'1' supported", __func__, *tc);
454 la8->trigger_pattern |= probe_bit;
458 sr_dbg("la8: %s: trigger_mask = 0x%x, trigger_pattern = 0x%x",
459 __func__, la8->trigger_mask, la8->trigger_pattern);
464 static int hw_init(const char *deviceinfo)
467 struct sr_device_instance *sdi;
470 sr_spew("la8: entering %s", __func__);
472 /* Avoid compiler errors. */
475 /* Allocate memory for our private driver context. */
476 if (!(la8 = g_try_malloc(sizeof(struct la8)))) {
477 sr_err("la8: %s: struct la8 malloc failed", __func__);
478 goto err_free_nothing;
481 /* Set some sane defaults. */
483 la8->cur_samplerate = SR_MHZ(100); /* 100MHz == max. samplerate */
484 la8->period_ps = 10000;
486 la8->limit_samples = 0;
487 la8->session_id = NULL;
488 memset(la8->mangled_buf, 0, BS);
489 la8->final_buf = NULL;
490 la8->trigger_pattern = 0x00; /* Value irrelevant, see trigger_mask. */
491 la8->trigger_mask = 0x00; /* All probes are "don't care". */
492 la8->trigger_timeout = 10; /* Default to 10s trigger timeout. */
493 la8->trigger_found = 0;
495 la8->block_counter = 0;
496 la8->divcount = 0; /* 10ns sample period == 100MHz samplerate */
498 /* Allocate memory where we'll store the de-mangled data. */
499 if (!(la8->final_buf = g_try_malloc(SDRAM_SIZE))) {
500 sr_err("la8: %s: final_buf malloc failed", __func__);
504 /* Allocate memory for the FTDI context (ftdic) and initialize it. */
505 if (!(la8->ftdic = ftdi_new())) {
506 sr_err("la8: %s: ftdi_new failed", __func__);
507 goto err_free_final_buf;
510 /* Check for the device and temporarily open it. */
511 if ((ret = ftdi_usb_open_desc(la8->ftdic, USB_VENDOR_ID,
512 USB_PRODUCT_ID, USB_DESCRIPTION, NULL)) < 0) {
513 sr_dbg("la8: %s: ftdi_usb_open_desc: (%d) %s",
514 __func__, ret, ftdi_get_error_string(la8->ftdic));
515 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
518 sr_dbg("la8: found device");
520 /* Register the device with libsigrok. */
521 sdi = sr_device_instance_new(0, SR_ST_INITIALIZING,
522 USB_VENDOR_NAME, USB_MODEL_NAME, USB_MODEL_VERSION);
524 sr_err("la8: %s: sr_device_instance_new failed", __func__);
525 goto err_close_ftdic;
530 device_instances = g_slist_append(device_instances, sdi);
532 sr_spew("la8: %s finished successfully", __func__);
534 /* Close device. We'll reopen it again when we need it. */
535 (void) la8_close(la8); /* Log, but ignore errors. */
540 (void) la8_close(la8); /* Log, but ignore errors. */
542 free(la8->ftdic); /* NOT g_free()! */
544 g_free(la8->final_buf);
552 static int hw_opendev(int device_index)
555 struct sr_device_instance *sdi;
558 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
559 sr_err("la8: %s: sdi was NULL", __func__);
560 return SR_ERR; /* TODO: SR_ERR_ARG? */
563 if (!(la8 = sdi->priv)) {
564 sr_err("la8: %s: sdi->priv was NULL", __func__);
565 return SR_ERR; /* TODO: SR_ERR_ARG? */
568 sr_dbg("la8: opening device");
570 /* Open the device. */
571 if ((ret = ftdi_usb_open_desc(la8->ftdic, USB_VENDOR_ID,
572 USB_PRODUCT_ID, USB_DESCRIPTION, NULL)) < 0) {
573 sr_err("la8: %s: ftdi_usb_open_desc: (%d) %s",
574 __func__, ret, ftdi_get_error_string(la8->ftdic));
575 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
578 sr_dbg("la8: device opened successfully");
580 /* Purge RX/TX buffers in the FTDI chip. */
581 if ((ret = ftdi_usb_purge_buffers(la8->ftdic)) < 0) {
582 sr_err("la8: %s: ftdi_usb_purge_buffers: (%d) %s",
583 __func__, ret, ftdi_get_error_string(la8->ftdic));
584 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
585 goto err_opendev_close_ftdic;
587 sr_dbg("la8: FTDI buffers purged successfully");
589 /* Enable flow control in the FTDI chip. */
590 if ((ret = ftdi_setflowctrl(la8->ftdic, SIO_RTS_CTS_HS)) < 0) {
591 sr_err("la8: %s: ftdi_setflowcontrol: (%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 flow control enabled successfully");
599 g_usleep(100 * 1000);
601 sdi->status = SR_ST_ACTIVE;
605 err_opendev_close_ftdic:
606 (void) la8_close(la8); /* Log, but ignore errors. */
610 static int set_samplerate(struct sr_device_instance *sdi, uint64_t samplerate)
615 sr_err("la8: %s: sdi was NULL", __func__);
619 if (!(la8 = sdi->priv)) {
620 sr_err("la8: %s: sdi->priv was NULL", __func__);
624 sr_spew("la8: setting samplerate");
626 fill_supported_samplerates_if_needed();
628 /* Check if this is a samplerate supported by the hardware. */
629 if (!is_valid_samplerate(samplerate))
632 /* Set the new samplerate. */
633 la8->cur_samplerate = samplerate;
634 la8->period_ps = 1000000000000 / samplerate;
636 sr_dbg("la8: samplerate set to %" PRIu64 "Hz", la8->cur_samplerate);
641 static int hw_closedev(int device_index)
643 struct sr_device_instance *sdi;
646 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
647 sr_err("la8: %s: sdi was NULL", __func__);
648 return SR_ERR; /* TODO: SR_ERR_ARG? */
651 if (!(la8 = sdi->priv)) {
652 sr_err("la8: %s: sdi->priv was NULL", __func__);
653 return SR_ERR; /* TODO: SR_ERR_ARG? */
656 sr_dbg("la8: closing device");
658 if (sdi->status == SR_ST_ACTIVE) {
659 sr_dbg("la8: %s: status ACTIVE, closing device", __func__);
660 /* TODO: Really ignore errors here, or return SR_ERR? */
661 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
663 sr_spew("la8: %s: status not ACTIVE, nothing to do", __func__);
666 sdi->status = SR_ST_INACTIVE;
668 sr_dbg("la8: %s: freeing sample buffers", __func__);
669 g_free(la8->final_buf);
674 static void hw_cleanup(void)
677 struct sr_device_instance *sdi;
679 sr_spew("la8: entering %s", __func__);
681 /* Properly close all devices. */
682 for (l = device_instances; l; l = l->next) {
683 if ((sdi = l->data) == NULL) {
684 sr_warn("la8: %s: sdi was NULL, continuing", __func__);
689 * Fixes a segfault as it's free()d elsewhere already.
690 * TODO: Document who is supposed to free this, and when.
692 if (sdi->priv != NULL)
695 sr_warn("la8: %s: sdi->priv was NULL, nothing "
698 sr_device_instance_free(sdi); /* Returns void. */
700 g_slist_free(device_instances); /* Returns void. */
701 device_instances = NULL;
704 static void *hw_get_device_info(int device_index, int device_info_id)
706 struct sr_device_instance *sdi;
710 sr_spew("la8: entering %s", __func__);
712 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
713 sr_err("la8: %s: sdi was NULL", __func__);
717 if (!(la8 = sdi->priv)) {
718 sr_err("la8: %s: sdi->priv was NULL", __func__);
722 switch (device_info_id) {
726 case SR_DI_NUM_PROBES:
727 info = GINT_TO_POINTER(NUM_PROBES);
729 case SR_DI_SAMPLERATES:
730 fill_supported_samplerates_if_needed();
733 case SR_DI_TRIGGER_TYPES:
734 info = (char *)TRIGGER_TYPES;
736 case SR_DI_CUR_SAMPLERATE:
737 info = &la8->cur_samplerate;
740 /* Unknown device info ID, return NULL. */
741 sr_err("la8: %s: Unknown device info ID", __func__);
749 static int hw_get_status(int device_index)
751 struct sr_device_instance *sdi;
753 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
754 sr_warn("la8: %s: sdi was NULL, device not found", __func__);
755 return SR_ST_NOT_FOUND;
758 sr_dbg("la8: %s: returning status %d", __func__, sdi->status);
763 static int *hw_get_capabilities(void)
765 sr_spew("la8: entering %s", __func__);
770 static int hw_set_configuration(int device_index, int capability, void *value)
772 struct sr_device_instance *sdi;
775 sr_spew("la8: entering %s", __func__);
777 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
778 sr_err("la8: %s: sdi was NULL", __func__);
779 return SR_ERR; /* TODO: SR_ERR_ARG? */
782 if (!(la8 = sdi->priv)) {
783 sr_err("la8: %s: sdi->priv was NULL", __func__);
784 return SR_ERR; /* TODO: SR_ERR_ARG? */
787 switch (capability) {
788 case SR_HWCAP_SAMPLERATE:
789 if (set_samplerate(sdi, *(uint64_t *)value) == SR_ERR)
791 sr_dbg("la8: SAMPLERATE = %" PRIu64, la8->cur_samplerate);
793 case SR_HWCAP_PROBECONFIG:
794 if (configure_probes(la8, (GSList *)value) != SR_OK) {
795 sr_err("la8: %s: probe config failed", __func__);
799 case SR_HWCAP_LIMIT_MSEC:
800 if (*(uint64_t *)value == 0) {
801 sr_err("la8: %s: LIMIT_MSEC can't be 0", __func__);
804 la8->limit_msec = *(uint64_t *)value;
805 sr_dbg("la8: LIMIT_MSEC = %" PRIu64, la8->limit_msec);
807 case SR_HWCAP_LIMIT_SAMPLES:
808 if (*(uint64_t *)value < MIN_NUM_SAMPLES) {
809 sr_err("la8: %s: LIMIT_SAMPLES too small", __func__);
812 la8->limit_samples = *(uint64_t *)value;
813 sr_dbg("la8: LIMIT_SAMPLES = %" PRIu64, la8->limit_samples);
816 /* Unknown capability, return SR_ERR. */
817 sr_err("la8: %s: Unknown capability", __func__);
826 * Get a block of data from the LA8.
828 * @param la8 The LA8 struct containing private per-device-instance data.
829 * @return SR_OK upon success, or SR_ERR upon errors.
831 static int la8_read_block(struct la8 *la8)
833 int i, byte_offset, m, mi, p, index, bytes_read;
837 sr_err("la8: %s: la8 was NULL", __func__);
842 sr_err("la8: %s: la8->ftdic was NULL", __func__);
846 sr_spew("la8: %s: reading block %d", __func__, la8->block_counter);
848 bytes_read = la8_read(la8, la8->mangled_buf, BS);
850 /* If first block read got 0 bytes, retry until success or timeout. */
851 if ((bytes_read == 0) && (la8->block_counter == 0)) {
853 sr_spew("la8: %s: reading block 0 again", __func__);
854 bytes_read = la8_read(la8, la8->mangled_buf, BS);
855 /* TODO: How to handle read errors here? */
857 } while ((la8->done > now) && (bytes_read == 0));
860 /* Check if block read was successful or a timeout occured. */
861 if (bytes_read != BS) {
862 sr_warn("la8: %s: trigger timed out", __func__);
863 (void) la8_reset(la8); /* Ignore errors. */
867 /* De-mangle the data. */
868 sr_spew("la8: de-mangling samples of block %d", la8->block_counter);
869 byte_offset = la8->block_counter * BS;
870 m = byte_offset / (1024 * 1024);
871 mi = m * (1024 * 1024);
872 for (i = 0; i < BS; i++) {
874 index = m * 2 + (((byte_offset + i) - mi) / 2) * 16;
875 index += (la8->divcount == 0) ? p : (1 - p);
876 la8->final_buf[index] = la8->mangled_buf[i];
882 static void send_block_to_session_bus(struct la8 *la8, int block)
885 uint8_t sample, expected_sample;
886 struct sr_datafeed_packet packet;
887 struct sr_datafeed_logic logic;
888 int trigger_point; /* Relative trigger point (in this block). */
890 /* Note: No sanity checks on la8/block, caller is responsible. */
892 /* Check if we can find the trigger condition in this block. */
894 expected_sample = la8->trigger_pattern & la8->trigger_mask;
895 for (i = 0; i < BS; i++) {
896 /* Don't continue if the trigger was found previously. */
897 if (la8->trigger_found)
901 * Also, don't continue if triggers are "don't care", i.e. if
902 * no trigger conditions were specified by the user. In that
903 * case we don't want to send an SR_DF_TRIGGER packet at all.
905 if (la8->trigger_mask == 0x00)
908 sample = *(la8->final_buf + (block * BS) + i);
910 if ((sample & la8->trigger_mask) == expected_sample) {
912 la8->trigger_found = 1;
917 /* If no trigger was found, send one SR_DF_LOGIC packet. */
918 if (trigger_point == -1) {
919 /* Send an SR_DF_LOGIC packet to the session bus. */
920 sr_spew("la8: sending SR_DF_LOGIC packet (%d bytes) for "
921 "block %d", BS, block);
922 packet.type = SR_DF_LOGIC;
923 packet.timeoffset = block * BS * la8->period_ps;
924 packet.duration = BS * la8->period_ps;
925 packet.payload = &logic;
928 logic.data = la8->final_buf + (block * BS);
929 sr_session_bus(la8->session_id, &packet);
934 * We found the trigger, so some special handling is needed. We have
935 * to send an SR_DF_LOGIC packet with the samples before the trigger
936 * (if any), then the SD_DF_TRIGGER packet itself, then another
937 * SR_DF_LOGIC packet with the samples after the trigger (if any).
940 /* TODO: Send SR_DF_TRIGGER packet before or after the actual sample? */
942 /* If at least one sample is located before the trigger... */
943 if (trigger_point > 0) {
944 /* Send pre-trigger SR_DF_LOGIC packet to the session bus. */
945 sr_spew("la8: sending pre-trigger SR_DF_LOGIC packet, "
946 "start = %d, length = %d", block * BS, trigger_point);
947 packet.type = SR_DF_LOGIC;
948 packet.timeoffset = block * BS * la8->period_ps;
949 packet.duration = trigger_point * la8->period_ps;
950 packet.payload = &logic;
951 logic.length = trigger_point;
953 logic.data = la8->final_buf + (block * BS);
954 sr_session_bus(la8->session_id, &packet);
957 /* Send the SR_DF_TRIGGER packet to the session bus. */
958 sr_spew("la8: sending SR_DF_TRIGGER packet, sample = %d",
959 (block * BS) + trigger_point);
960 packet.type = SR_DF_TRIGGER;
961 packet.timeoffset = (block * BS + trigger_point) * la8->period_ps;
963 packet.payload = NULL;
964 sr_session_bus(la8->session_id, &packet);
966 /* If at least one sample is located after the trigger... */
967 if (trigger_point < (BS - 1)) {
968 /* Send post-trigger SR_DF_LOGIC packet to the session bus. */
969 sr_spew("la8: sending post-trigger SR_DF_LOGIC packet, "
970 "start = %d, length = %d",
971 (block * BS) + trigger_point, BS - trigger_point);
972 packet.type = SR_DF_LOGIC;
973 packet.timeoffset = (block * BS + trigger_point) * la8->period_ps;
974 packet.duration = (BS - trigger_point) * la8->period_ps;
975 packet.payload = &logic;
976 logic.length = BS - trigger_point;
978 logic.data = la8->final_buf + (block * BS) + trigger_point;
979 sr_session_bus(la8->session_id, &packet);
983 static int receive_data(int fd, int revents, void *session_data)
986 struct sr_device_instance *sdi;
989 /* Avoid compiler errors. */
993 if (!(sdi = session_data)) {
994 sr_err("la8: %s: user_data was NULL", __func__);
998 if (!(la8 = sdi->priv)) {
999 sr_err("la8: %s: sdi->priv was NULL", __func__);
1003 /* Get one block of data. */
1004 if ((ret = la8_read_block(la8)) < 0) {
1005 sr_err("la8: %s: la8_read_block error: %d", __func__, ret);
1006 hw_stop_acquisition(sdi->index, session_data);
1010 /* We need to get exactly NUM_BLOCKS blocks (i.e. 8MB) of data. */
1011 if (la8->block_counter != (NUM_BLOCKS - 1)) {
1012 la8->block_counter++;
1016 sr_dbg("la8: sampling finished, sending data to session bus now");
1018 /* All data was received and demangled, send it to the session bus. */
1019 for (i = 0; i < NUM_BLOCKS; i++)
1020 send_block_to_session_bus(la8, i);
1022 hw_stop_acquisition(sdi->index, session_data);
1024 // return FALSE; /* FIXME? */
1028 static int hw_start_acquisition(int device_index, gpointer session_data)
1030 struct sr_device_instance *sdi;
1032 struct sr_datafeed_packet packet;
1033 struct sr_datafeed_header header;
1037 sr_spew("la8: entering %s", __func__);
1039 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
1040 sr_err("la8: %s: sdi was NULL", __func__);
1041 return SR_ERR; /* TODO: SR_ERR_ARG? */
1044 if (!(la8 = sdi->priv)) {
1045 sr_err("la8: %s: sdi->priv was NULL", __func__);
1046 return SR_ERR; /* TODO: SR_ERR_ARG? */
1050 sr_err("la8: %s: la8->ftdic was NULL", __func__);
1054 la8->divcount = samplerate_to_divcount(la8->cur_samplerate);
1055 if (la8->divcount == 0xff) {
1056 sr_err("la8: %s: invalid divcount/samplerate", __func__);
1060 /* Fill acquisition parameters into buf[]. */
1061 buf[0] = la8->divcount;
1062 buf[1] = 0xff; /* This byte must always be 0xff. */
1063 buf[2] = la8->trigger_pattern;
1064 buf[3] = la8->trigger_mask;
1066 /* Start acquisition. */
1067 bytes_written = la8_write(la8, buf, 4);
1069 if (bytes_written < 0) {
1070 sr_err("la8: acquisition failed to start");
1072 } else if (bytes_written != 4) {
1073 sr_err("la8: acquisition failed to start");
1074 return SR_ERR; /* TODO: Other error and return code? */
1077 sr_dbg("la8: acquisition started successfully");
1079 la8->session_id = session_data;
1081 /* Send header packet to the session bus. */
1082 sr_dbg("la8: %s: sending SR_DF_HEADER", __func__);
1083 packet.type = SR_DF_HEADER;
1084 packet.payload = &header;
1085 header.feed_version = 1;
1086 gettimeofday(&header.starttime, NULL);
1087 header.samplerate = la8->cur_samplerate;
1088 header.num_logic_probes = NUM_PROBES;
1089 header.num_analog_probes = 0;
1090 sr_session_bus(session_data, &packet);
1092 /* Time when we should be done (for detecting trigger timeouts). */
1093 la8->done = (la8->divcount + 1) * 0.08388608 + time(NULL)
1094 + la8->trigger_timeout;
1095 la8->block_counter = 0;
1096 la8->trigger_found = 0;
1098 /* Hook up a dummy handler to receive data from the LA8. */
1099 sr_source_add(-1, G_IO_IN, 0, receive_data, sdi);
1104 static void hw_stop_acquisition(int device_index, gpointer session_data)
1106 struct sr_device_instance *sdi;
1108 struct sr_datafeed_packet packet;
1110 sr_dbg("la8: stopping acquisition");
1112 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
1113 sr_err("la8: %s: sdi was NULL", __func__);
1117 if (!(la8 = sdi->priv)) {
1118 sr_err("la8: %s: sdi->priv was NULL", __func__);
1122 /* Send end packet to the session bus. */
1123 sr_dbg("la8: %s: sending SR_DF_END", __func__);
1124 packet.type = SR_DF_END;
1125 sr_session_bus(session_data, &packet);
1128 struct sr_device_plugin chronovu_la8_plugin_info = {
1129 .name = "chronovu-la8",
1130 .longname = "ChronoVu LA8",
1133 .cleanup = hw_cleanup,
1134 .opendev = hw_opendev,
1135 .closedev = hw_closedev,
1136 .get_device_info = hw_get_device_info,
1137 .get_status = hw_get_status,
1138 .get_capabilities = hw_get_capabilities,
1139 .set_configuration = hw_set_configuration,
1140 .start_acquisition = hw_start_acquisition,
1141 .stop_acquisition = hw_stop_acquisition,