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;
44 static const char *probe_names[NUM_PROBES + 1] = {
57 /** FTDI device context (used by libftdi). */
58 struct ftdi_context *ftdic;
60 /** The currently configured samplerate of the device. */
61 uint64_t cur_samplerate;
63 /** The current sampling limit (in ms). */
66 /** The current sampling limit (in number of samples). */
67 uint64_t limit_samples;
73 * A buffer containing some (mangled) samples from the device.
74 * Format: Pretty mangled-up (due to hardware reasons), see code.
76 uint8_t mangled_buf[BS];
79 * An 8MB buffer where we'll store the de-mangled samples.
80 * Format: Each sample is 1 byte, MSB is channel 7, LSB is channel 0.
85 * Trigger pattern (MSB = channel 7, LSB = channel 0).
86 * A 1 bit matches a high signal, 0 matches a low signal on a probe.
87 * Only low/high triggers (but not e.g. rising/falling) are supported.
89 uint8_t trigger_pattern;
92 * Trigger mask (MSB = channel 7, LSB = channel 0).
93 * A 1 bit means "must match trigger_pattern", 0 means "don't care".
97 /** Time (in seconds) before the trigger times out. */
98 uint64_t trigger_timeout;
100 /** Tells us whether an SR_DF_TRIGGER packet was already sent. */
106 /** Counter/index for the data block to be read. */
109 /** The divcount value (determines the sample period) for the LA8. */
113 /* This will be initialized via hw_get_device_info()/SR_DI_SAMPLERATES. */
114 static uint64_t supported_samplerates[255 + 1] = { 0 };
117 * Min: 1 sample per 0.01us -> sample time is 0.084s, samplerate 100MHz
118 * Max: 1 sample per 2.55us -> sample time is 21.391s, samplerate 392.15kHz
120 static struct sr_samplerates samplerates = {
124 .list = supported_samplerates,
127 /* Note: Continuous sampling is not supported by the hardware. */
128 static int capabilities[] = {
129 SR_HWCAP_LOGIC_ANALYZER,
131 SR_HWCAP_LIMIT_MSEC, /* TODO: Not yet implemented. */
132 SR_HWCAP_LIMIT_SAMPLES, /* TODO: Not yet implemented. */
136 /* Function prototypes. */
137 static int la8_close_usb_reset_sequencer(struct la8 *la8);
138 static void hw_stop_acquisition(int device_index, gpointer session_data);
139 static int la8_reset(struct la8 *la8);
141 static void fill_supported_samplerates_if_needed(void)
145 /* Do nothing if supported_samplerates[] is already filled. */
146 if (supported_samplerates[0] != 0)
149 /* Fill supported_samplerates[] with the proper values. */
150 for (i = 0; i < 255; i++)
151 supported_samplerates[254 - i] = SR_MHZ(100) / (i + 1);
152 supported_samplerates[255] = 0;
156 * Check if the given samplerate is supported by the LA8 hardware.
158 * @param samplerate The samplerate (in Hz) to check.
159 * @return 1 if the samplerate is supported/valid, 0 otherwise.
161 static int is_valid_samplerate(uint64_t samplerate)
165 fill_supported_samplerates_if_needed();
167 for (i = 0; i < 255; i++) {
168 if (supported_samplerates[i] == samplerate)
172 sr_warn("la8: %s: invalid samplerate (%" PRIu64 "Hz)",
173 __func__, samplerate);
179 * Convert a samplerate (in Hz) to the 'divcount' value the LA8 wants.
181 * LA8 hardware: sample period = (divcount + 1) * 10ns.
182 * Min. value for divcount: 0x00 (10ns sample period, 100MHz samplerate).
183 * Max. value for divcount: 0xfe (2550ns sample period, 392.15kHz samplerate).
185 * @param samplerate The samplerate in Hz.
186 * @return The divcount value as needed by the hardware, or 0xff upon errors.
188 static uint8_t samplerate_to_divcount(uint64_t samplerate)
190 if (samplerate == 0) {
191 sr_err("la8: %s: samplerate was 0", __func__);
195 if (!is_valid_samplerate(samplerate)) {
196 sr_err("la8: %s: can't get divcount, samplerate invalid",
201 return (SR_MHZ(100) / samplerate) - 1;
205 * Write data of a certain length to the LA8's FTDI device.
207 * @param la8 The LA8 struct containing private per-device-instance data.
208 * @param buf The buffer containing the data to write.
209 * @param size The number of bytes to write.
210 * @return The number of bytes written, or a negative value upon errors.
212 static int la8_write(struct la8 *la8, uint8_t *buf, int size)
217 sr_err("la8: %s: la8 was NULL", __func__);
222 sr_err("la8: %s: la8->ftdic was NULL", __func__);
227 sr_err("la8: %s: buf was NULL", __func__);
232 sr_err("la8: %s: size was < 0", __func__);
236 bytes_written = ftdi_write_data(la8->ftdic, buf, size);
238 if (bytes_written < 0) {
239 sr_warn("la8: %s: ftdi_write_data: (%d) %s", __func__,
240 bytes_written, ftdi_get_error_string(la8->ftdic));
241 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
242 } else if (bytes_written != size) {
243 sr_warn("la8: %s: bytes to write: %d, bytes written: %d",
244 __func__, size, bytes_written);
245 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
248 return bytes_written;
252 * Read a certain amount of bytes from the LA8's FTDI device.
254 * @param la8 The LA8 struct containing private per-device-instance data.
255 * @param buf The buffer where the received data will be stored.
256 * @param size The number of bytes to read.
257 * @return The number of bytes read, or a negative value upon errors.
259 static int la8_read(struct la8 *la8, uint8_t *buf, int size)
264 sr_err("la8: %s: la8 was NULL", __func__);
269 sr_err("la8: %s: la8->ftdic was NULL", __func__);
274 sr_err("la8: %s: buf was NULL", __func__);
279 sr_err("la8: %s: size was <= 0", __func__);
283 bytes_read = ftdi_read_data(la8->ftdic, buf, size);
285 if (bytes_read < 0) {
286 sr_warn("la8: %s: ftdi_read_data: (%d) %s", __func__,
287 bytes_read, ftdi_get_error_string(la8->ftdic));
288 } else if (bytes_read != size) {
289 // sr_warn("la8: %s: bytes to read: %d, bytes read: %d",
290 // __func__, size, bytes_read);
296 static int la8_close(struct la8 *la8)
301 sr_err("la8: %s: la8 was NULL", __func__);
306 sr_err("la8: %s: la8->ftdic was NULL", __func__);
310 if ((ret = ftdi_usb_close(la8->ftdic)) < 0) {
311 sr_warn("la8: %s: ftdi_usb_close: (%d) %s",
312 __func__, ret, ftdi_get_error_string(la8->ftdic));
319 * Close the ChronoVu LA8 USB port and reset the LA8 sequencer logic.
321 * @param la8 The LA8 struct containing private per-device-instance data.
322 * @return SR_OK upon success, SR_ERR upon failure.
324 static int la8_close_usb_reset_sequencer(struct la8 *la8)
326 /* Magic sequence of bytes for resetting the LA8 sequencer logic. */
327 uint8_t buf[8] = {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};
330 sr_spew("la8: entering %s", __func__);
333 sr_err("la8: %s: la8 was NULL", __func__);
338 sr_err("la8: %s: la8->ftdic was NULL", __func__);
342 if (la8->ftdic->usb_dev) {
343 /* Reset the LA8 sequencer logic, then wait 100ms. */
344 sr_dbg("la8: resetting sequencer logic");
345 (void) la8_write(la8, buf, 8); /* Ignore errors. */
346 g_usleep(100 * 1000);
348 /* Purge FTDI buffers, then reset and close the FTDI device. */
349 sr_dbg("la8: purging buffers, resetting+closing FTDI device");
351 /* Log errors, but ignore them (i.e., don't abort). */
352 if ((ret = ftdi_usb_purge_buffers(la8->ftdic)) < 0)
353 sr_warn("la8: %s: ftdi_usb_purge_buffers: (%d) %s",
354 __func__, ret, ftdi_get_error_string(la8->ftdic));
355 if ((ret = ftdi_usb_reset(la8->ftdic)) < 0)
356 sr_warn("la8: %s: ftdi_usb_reset: (%d) %s", __func__,
357 ret, ftdi_get_error_string(la8->ftdic));
358 if ((ret = ftdi_usb_close(la8->ftdic)) < 0)
359 sr_warn("la8: %s: ftdi_usb_close: (%d) %s", __func__,
360 ret, ftdi_get_error_string(la8->ftdic));
362 sr_spew("la8: %s: usb_dev was NULL, nothing to do", __func__);
365 ftdi_free(la8->ftdic); /* Returns void. */
372 * Reset the ChronoVu LA8.
374 * The LA8 must be reset after a failed read/write operation or upon timeouts.
376 * @param la8 The LA8 struct containing private per-device-instance data.
377 * @return SR_OK upon success, SR_ERR upon failure.
379 static int la8_reset(struct la8 *la8)
386 sr_err("la8: %s: la8 was NULL", __func__);
391 sr_err("la8: %s: la8->ftdic was NULL", __func__);
395 sr_dbg("la8: resetting the device");
398 * Purge pending read data from the FTDI hardware FIFO until
399 * no more data is left, or a timeout occurs (after 20s).
401 done = 20 + time(NULL);
403 /* TODO: Ignore errors? Check for < 0 at least! */
404 bytes_read = la8_read(la8, (uint8_t *)&buf, BS);
406 } while ((done > now) && (bytes_read > 0));
408 /* Reset the LA8 sequencer logic and close the USB port. */
409 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
411 sr_dbg("la8: device reset finished");
416 static int configure_probes(struct la8 *la8, GSList *probes)
418 struct sr_probe *probe;
423 la8->trigger_pattern = 0;
424 la8->trigger_mask = 0; /* Default to "don't care" for all probes. */
426 for (l = probes; l; l = l->next) {
427 probe = (struct sr_probe *)l->data;
430 sr_err("la8: %s: probe was NULL", __func__);
434 /* Skip disabled probes. */
438 /* Skip (enabled) probes with no configured trigger. */
442 /* Note: Must only be run if probe->trigger != NULL. */
443 if (probe->index < 0 || probe->index > 7) {
444 sr_err("la8: %s: invalid probe index %d, must be "
445 "between 0 and 7", __func__, probe->index);
449 probe_bit = (1 << (probe->index - 1));
451 /* Configure the probe's trigger mask and trigger pattern. */
452 for (tc = probe->trigger; tc && *tc; tc++) {
453 la8->trigger_mask |= probe_bit;
455 /* Sanity check, LA8 only supports low/high trigger. */
456 if (*tc != '0' && *tc != '1') {
457 sr_err("la8: %s: invalid trigger '%c', only "
458 "'0'/'1' supported", __func__, *tc);
463 la8->trigger_pattern |= probe_bit;
467 sr_dbg("la8: %s: trigger_mask = 0x%x, trigger_pattern = 0x%x",
468 __func__, la8->trigger_mask, la8->trigger_pattern);
473 static int hw_init(const char *deviceinfo)
476 struct sr_device_instance *sdi;
479 sr_spew("la8: entering %s", __func__);
481 /* Avoid compiler errors. */
484 /* Allocate memory for our private driver context. */
485 if (!(la8 = g_try_malloc(sizeof(struct la8)))) {
486 sr_err("la8: %s: struct la8 malloc failed", __func__);
487 goto err_free_nothing;
490 /* Set some sane defaults. */
492 la8->cur_samplerate = SR_MHZ(100); /* 100MHz == max. samplerate */
494 la8->limit_samples = 0;
495 la8->session_id = NULL;
496 memset(la8->mangled_buf, 0, BS);
497 la8->final_buf = NULL;
498 la8->trigger_pattern = 0x00; /* Value irrelevant, see trigger_mask. */
499 la8->trigger_mask = 0x00; /* All probes are "don't care". */
500 la8->trigger_timeout = 10; /* Default to 10s trigger timeout. */
501 la8->trigger_found = 0;
503 la8->block_counter = 0;
504 la8->divcount = 0; /* 10ns sample period == 100MHz samplerate */
506 /* Allocate memory where we'll store the de-mangled data. */
507 if (!(la8->final_buf = g_try_malloc(SDRAM_SIZE))) {
508 sr_err("la8: %s: final_buf malloc failed", __func__);
512 /* Allocate memory for the FTDI context (ftdic) and initialize it. */
513 if (!(la8->ftdic = ftdi_new())) {
514 sr_err("la8: %s: ftdi_new failed", __func__);
515 goto err_free_final_buf;
518 /* Check for the device and temporarily open it. */
519 if ((ret = ftdi_usb_open_desc(la8->ftdic, USB_VENDOR_ID,
520 USB_PRODUCT_ID, USB_DESCRIPTION, NULL)) < 0) {
521 sr_dbg("la8: %s: ftdi_usb_open_desc: (%d) %s",
522 __func__, ret, ftdi_get_error_string(la8->ftdic));
523 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
526 sr_dbg("la8: found device");
528 /* Register the device with libsigrok. */
529 sdi = sr_device_instance_new(0, SR_ST_INITIALIZING,
530 USB_VENDOR_NAME, USB_MODEL_NAME, USB_MODEL_VERSION);
532 sr_err("la8: %s: sr_device_instance_new failed", __func__);
533 goto err_close_ftdic;
538 device_instances = g_slist_append(device_instances, sdi);
540 sr_spew("la8: %s finished successfully", __func__);
542 /* Close device. We'll reopen it again when we need it. */
543 (void) la8_close(la8); /* Log, but ignore errors. */
548 (void) la8_close(la8); /* Log, but ignore errors. */
550 free(la8->ftdic); /* NOT g_free()! */
552 g_free(la8->final_buf);
560 static int hw_opendev(int device_index)
563 struct sr_device_instance *sdi;
566 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
567 sr_err("la8: %s: sdi was NULL", __func__);
568 return SR_ERR; /* TODO: SR_ERR_ARG? */
571 if (!(la8 = sdi->priv)) {
572 sr_err("la8: %s: sdi->priv was NULL", __func__);
573 return SR_ERR; /* TODO: SR_ERR_ARG? */
576 sr_dbg("la8: opening device");
578 /* Open the device. */
579 if ((ret = ftdi_usb_open_desc(la8->ftdic, USB_VENDOR_ID,
580 USB_PRODUCT_ID, USB_DESCRIPTION, NULL)) < 0) {
581 sr_err("la8: %s: ftdi_usb_open_desc: (%d) %s",
582 __func__, ret, ftdi_get_error_string(la8->ftdic));
583 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
586 sr_dbg("la8: device opened successfully");
588 /* Purge RX/TX buffers in the FTDI chip. */
589 if ((ret = ftdi_usb_purge_buffers(la8->ftdic)) < 0) {
590 sr_err("la8: %s: ftdi_usb_purge_buffers: (%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 buffers purged successfully");
597 /* Enable flow control in the FTDI chip. */
598 if ((ret = ftdi_setflowctrl(la8->ftdic, SIO_RTS_CTS_HS)) < 0) {
599 sr_err("la8: %s: ftdi_setflowcontrol: (%d) %s",
600 __func__, ret, ftdi_get_error_string(la8->ftdic));
601 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
602 goto err_opendev_close_ftdic;
604 sr_dbg("la8: FTDI flow control enabled successfully");
607 g_usleep(100 * 1000);
609 sdi->status = SR_ST_ACTIVE;
613 err_opendev_close_ftdic:
614 (void) la8_close(la8); /* Log, but ignore errors. */
618 static int set_samplerate(struct sr_device_instance *sdi, uint64_t samplerate)
623 sr_err("la8: %s: sdi was NULL", __func__);
627 if (!(la8 = sdi->priv)) {
628 sr_err("la8: %s: sdi->priv was NULL", __func__);
632 sr_spew("la8: setting samplerate");
634 fill_supported_samplerates_if_needed();
636 /* Check if this is a samplerate supported by the hardware. */
637 if (!is_valid_samplerate(samplerate))
640 /* Set the new samplerate. */
641 la8->cur_samplerate = samplerate;
643 sr_dbg("la8: samplerate set to %" PRIu64 "Hz", la8->cur_samplerate);
648 static int hw_closedev(int device_index)
650 struct sr_device_instance *sdi;
653 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
654 sr_err("la8: %s: sdi was NULL", __func__);
655 return SR_ERR; /* TODO: SR_ERR_ARG? */
658 if (!(la8 = sdi->priv)) {
659 sr_err("la8: %s: sdi->priv was NULL", __func__);
660 return SR_ERR; /* TODO: SR_ERR_ARG? */
663 sr_dbg("la8: closing device");
665 if (sdi->status == SR_ST_ACTIVE) {
666 sr_dbg("la8: %s: status ACTIVE, closing device", __func__);
667 /* TODO: Really ignore errors here, or return SR_ERR? */
668 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
670 sr_spew("la8: %s: status not ACTIVE, nothing to do", __func__);
673 sdi->status = SR_ST_INACTIVE;
675 sr_dbg("la8: %s: freeing sample buffers", __func__);
676 g_free(la8->final_buf);
681 static void hw_cleanup(void)
684 struct sr_device_instance *sdi;
686 sr_spew("la8: entering %s", __func__);
688 /* Properly close all devices. */
689 for (l = device_instances; l; l = l->next) {
690 if ((sdi = l->data) == NULL) {
691 sr_warn("la8: %s: sdi was NULL, continuing", __func__);
696 * Fixes a segfault as it's free()d elsewhere already.
697 * TODO: Document who is supposed to free this, and when.
699 if (sdi->priv != NULL)
702 sr_warn("la8: %s: sdi->priv was NULL, nothing "
705 sr_device_instance_free(sdi); /* Returns void. */
707 g_slist_free(device_instances); /* Returns void. */
708 device_instances = NULL;
711 static void *hw_get_device_info(int device_index, int device_info_id)
713 struct sr_device_instance *sdi;
717 sr_spew("la8: entering %s", __func__);
719 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
720 sr_err("la8: %s: sdi was NULL", __func__);
724 if (!(la8 = sdi->priv)) {
725 sr_err("la8: %s: sdi->priv was NULL", __func__);
729 switch (device_info_id) {
733 case SR_DI_NUM_PROBES:
734 info = GINT_TO_POINTER(NUM_PROBES);
736 case SR_DI_PROBE_NAMES:
739 case SR_DI_SAMPLERATES:
740 fill_supported_samplerates_if_needed();
743 case SR_DI_TRIGGER_TYPES:
744 info = (char *)TRIGGER_TYPES;
746 case SR_DI_CUR_SAMPLERATE:
747 info = &la8->cur_samplerate;
750 /* Unknown device info ID, return NULL. */
751 sr_err("la8: %s: Unknown device info ID", __func__);
759 static int hw_get_status(int device_index)
761 struct sr_device_instance *sdi;
763 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
764 sr_warn("la8: %s: sdi was NULL, device not found", __func__);
765 return SR_ST_NOT_FOUND;
768 sr_dbg("la8: %s: returning status %d", __func__, sdi->status);
773 static int *hw_get_capabilities(void)
775 sr_spew("la8: entering %s", __func__);
780 static int hw_set_configuration(int device_index, int capability, void *value)
782 struct sr_device_instance *sdi;
785 sr_spew("la8: entering %s", __func__);
787 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
788 sr_err("la8: %s: sdi was NULL", __func__);
789 return SR_ERR; /* TODO: SR_ERR_ARG? */
792 if (!(la8 = sdi->priv)) {
793 sr_err("la8: %s: sdi->priv was NULL", __func__);
794 return SR_ERR; /* TODO: SR_ERR_ARG? */
797 switch (capability) {
798 case SR_HWCAP_SAMPLERATE:
799 if (set_samplerate(sdi, *(uint64_t *)value) == SR_ERR)
801 sr_dbg("la8: SAMPLERATE = %" PRIu64, la8->cur_samplerate);
803 case SR_HWCAP_PROBECONFIG:
804 if (configure_probes(la8, (GSList *)value) != SR_OK) {
805 sr_err("la8: %s: probe config failed", __func__);
809 case SR_HWCAP_LIMIT_MSEC:
810 if (*(uint64_t *)value == 0) {
811 sr_err("la8: %s: LIMIT_MSEC can't be 0", __func__);
814 la8->limit_msec = *(uint64_t *)value;
815 sr_dbg("la8: LIMIT_MSEC = %" PRIu64, la8->limit_msec);
817 case SR_HWCAP_LIMIT_SAMPLES:
818 if (*(uint64_t *)value < MIN_NUM_SAMPLES) {
819 sr_err("la8: %s: LIMIT_SAMPLES too small", __func__);
822 la8->limit_samples = *(uint64_t *)value;
823 sr_dbg("la8: LIMIT_SAMPLES = %" PRIu64, la8->limit_samples);
826 /* Unknown capability, return SR_ERR. */
827 sr_err("la8: %s: Unknown capability", __func__);
836 * Get a block of data from the LA8.
838 * @param la8 The LA8 struct containing private per-device-instance data.
839 * @return SR_OK upon success, or SR_ERR upon errors.
841 static int la8_read_block(struct la8 *la8)
843 int i, byte_offset, m, mi, p, index, bytes_read;
847 sr_err("la8: %s: la8 was NULL", __func__);
852 sr_err("la8: %s: la8->ftdic was NULL", __func__);
856 sr_spew("la8: %s: reading block %d", __func__, la8->block_counter);
858 bytes_read = la8_read(la8, la8->mangled_buf, BS);
860 /* If first block read got 0 bytes, retry until success or timeout. */
861 if ((bytes_read == 0) && (la8->block_counter == 0)) {
863 sr_spew("la8: %s: reading block 0 again", __func__);
864 bytes_read = la8_read(la8, la8->mangled_buf, BS);
865 /* TODO: How to handle read errors here? */
867 } while ((la8->done > now) && (bytes_read == 0));
870 /* Check if block read was successful or a timeout occured. */
871 if (bytes_read != BS) {
872 sr_warn("la8: %s: trigger timed out", __func__);
873 (void) la8_reset(la8); /* Ignore errors. */
877 /* De-mangle the data. */
878 sr_spew("la8: de-mangling samples of block %d", la8->block_counter);
879 byte_offset = la8->block_counter * BS;
880 m = byte_offset / (1024 * 1024);
881 mi = m * (1024 * 1024);
882 for (i = 0; i < BS; i++) {
884 index = m * 2 + (((byte_offset + i) - mi) / 2) * 16;
885 index += (la8->divcount == 0) ? p : (1 - p);
886 la8->final_buf[index] = la8->mangled_buf[i];
892 static void send_block_to_session_bus(struct la8 *la8, int block)
895 uint8_t sample, expected_sample;
896 struct sr_datafeed_packet packet;
897 struct sr_datafeed_logic logic;
898 int trigger_point; /* Relative trigger point (in this block). */
900 /* Note: No sanity checks on la8/block, caller is responsible. */
902 /* Check if we can find the trigger condition in this block. */
904 expected_sample = la8->trigger_pattern & la8->trigger_mask;
905 for (i = 0; i < BS; i++) {
906 /* Don't continue if the trigger was found previously. */
907 if (la8->trigger_found)
911 * Also, don't continue if triggers are "don't care", i.e. if
912 * no trigger conditions were specified by the user. In that
913 * case we don't want to send an SR_DF_TRIGGER packet at all.
915 if (la8->trigger_mask == 0x00)
918 sample = *(la8->final_buf + (block * BS) + i);
920 if ((sample & la8->trigger_mask) == expected_sample) {
922 la8->trigger_found = 1;
927 /* If no trigger was found, send one SR_DF_LOGIC packet. */
928 if (trigger_point == -1) {
929 /* Send an SR_DF_LOGIC packet to the session bus. */
930 sr_spew("la8: sending SR_DF_LOGIC packet (%d bytes) for "
931 "block %d", BS, block);
932 packet.type = SR_DF_LOGIC;
933 packet.payload = &logic;
936 logic.data = la8->final_buf + (block * BS);
937 sr_session_bus(la8->session_id, &packet);
942 * We found the trigger, so some special handling is needed. We have
943 * to send an SR_DF_LOGIC packet with the samples before the trigger
944 * (if any), then the SD_DF_TRIGGER packet itself, then another
945 * SR_DF_LOGIC packet with the samples after the trigger (if any).
948 /* TODO: Send SR_DF_TRIGGER packet before or after the actual sample? */
950 /* If at least one sample is located before the trigger... */
951 if (trigger_point > 0) {
952 /* Send pre-trigger SR_DF_LOGIC packet to the session bus. */
953 sr_spew("la8: sending pre-trigger SR_DF_LOGIC packet, "
954 "start = %d, length = %d", block * BS, trigger_point);
955 packet.type = SR_DF_LOGIC;
956 packet.payload = &logic;
957 logic.length = trigger_point;
959 logic.data = la8->final_buf + (block * BS);
960 sr_session_bus(la8->session_id, &packet);
963 /* Send the SR_DF_TRIGGER packet to the session bus. */
964 sr_spew("la8: sending SR_DF_TRIGGER packet, sample = %d",
965 (block * BS) + trigger_point);
966 packet.type = SR_DF_TRIGGER;
967 packet.payload = NULL;
968 sr_session_bus(la8->session_id, &packet);
970 /* If at least one sample is located after the trigger... */
971 if (trigger_point < (BS - 1)) {
972 /* Send post-trigger SR_DF_LOGIC packet to the session bus. */
973 sr_spew("la8: sending post-trigger SR_DF_LOGIC packet, "
974 "start = %d, length = %d",
975 (block * BS) + trigger_point, BS - trigger_point);
976 packet.type = SR_DF_LOGIC;
977 packet.payload = &logic;
978 logic.length = BS - trigger_point;
980 logic.data = la8->final_buf + (block * BS) + trigger_point;
981 sr_session_bus(la8->session_id, &packet);
985 static int receive_data(int fd, int revents, void *session_data)
988 struct sr_device_instance *sdi;
991 /* Avoid compiler errors. */
995 if (!(sdi = session_data)) {
996 sr_err("la8: %s: session_data was NULL", __func__);
1000 if (!(la8 = sdi->priv)) {
1001 sr_err("la8: %s: sdi->priv was NULL", __func__);
1005 /* Get one block of data. */
1006 if ((ret = la8_read_block(la8)) < 0) {
1007 sr_err("la8: %s: la8_read_block error: %d", __func__, ret);
1008 hw_stop_acquisition(sdi->index, session_data);
1012 /* We need to get exactly NUM_BLOCKS blocks (i.e. 8MB) of data. */
1013 if (la8->block_counter != (NUM_BLOCKS - 1)) {
1014 la8->block_counter++;
1018 sr_dbg("la8: sampling finished, sending data to session bus now");
1020 /* All data was received and demangled, send it to the session bus. */
1021 for (i = 0; i < NUM_BLOCKS; i++)
1022 send_block_to_session_bus(la8, i);
1024 hw_stop_acquisition(sdi->index, session_data);
1026 // return FALSE; /* FIXME? */
1030 static int hw_start_acquisition(int device_index, gpointer session_data)
1032 struct sr_device_instance *sdi;
1034 struct sr_datafeed_packet packet;
1035 struct sr_datafeed_header header;
1039 sr_spew("la8: entering %s", __func__);
1041 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
1042 sr_err("la8: %s: sdi was NULL", __func__);
1043 return SR_ERR; /* TODO: SR_ERR_ARG? */
1046 if (!(la8 = sdi->priv)) {
1047 sr_err("la8: %s: sdi->priv was NULL", __func__);
1048 return SR_ERR; /* TODO: SR_ERR_ARG? */
1052 sr_err("la8: %s: la8->ftdic was NULL", __func__);
1056 la8->divcount = samplerate_to_divcount(la8->cur_samplerate);
1057 if (la8->divcount == 0xff) {
1058 sr_err("la8: %s: invalid divcount/samplerate", __func__);
1062 /* Fill acquisition parameters into buf[]. */
1063 buf[0] = la8->divcount;
1064 buf[1] = 0xff; /* This byte must always be 0xff. */
1065 buf[2] = la8->trigger_pattern;
1066 buf[3] = la8->trigger_mask;
1068 /* Start acquisition. */
1069 bytes_written = la8_write(la8, buf, 4);
1071 if (bytes_written < 0) {
1072 sr_err("la8: acquisition failed to start");
1074 } else if (bytes_written != 4) {
1075 sr_err("la8: acquisition failed to start");
1076 return SR_ERR; /* TODO: Other error and return code? */
1079 sr_dbg("la8: acquisition started successfully");
1081 la8->session_id = session_data;
1083 /* Send header packet to the session bus. */
1084 sr_dbg("la8: %s: sending SR_DF_HEADER", __func__);
1085 packet.type = SR_DF_HEADER;
1086 packet.payload = &header;
1087 header.feed_version = 1;
1088 gettimeofday(&header.starttime, NULL);
1089 header.samplerate = la8->cur_samplerate;
1090 header.num_logic_probes = NUM_PROBES;
1091 header.num_analog_probes = 0;
1092 sr_session_bus(session_data, &packet);
1094 /* Time when we should be done (for detecting trigger timeouts). */
1095 la8->done = (la8->divcount + 1) * 0.08388608 + time(NULL)
1096 + la8->trigger_timeout;
1097 la8->block_counter = 0;
1098 la8->trigger_found = 0;
1100 /* Hook up a dummy handler to receive data from the LA8. */
1101 sr_source_add(-1, G_IO_IN, 0, receive_data, sdi);
1106 static void hw_stop_acquisition(int device_index, gpointer session_data)
1108 struct sr_device_instance *sdi;
1110 struct sr_datafeed_packet packet;
1112 sr_dbg("la8: stopping acquisition");
1114 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
1115 sr_err("la8: %s: sdi was NULL", __func__);
1119 if (!(la8 = sdi->priv)) {
1120 sr_err("la8: %s: sdi->priv was NULL", __func__);
1124 /* Send end packet to the session bus. */
1125 sr_dbg("la8: %s: sending SR_DF_END", __func__);
1126 packet.type = SR_DF_END;
1127 sr_session_bus(session_data, &packet);
1130 SR_PRIV struct sr_device_plugin chronovu_la8_plugin_info = {
1131 .name = "chronovu-la8",
1132 .longname = "ChronoVu LA8",
1135 .cleanup = hw_cleanup,
1136 .opendev = hw_opendev,
1137 .closedev = hw_closedev,
1138 .get_device_info = hw_get_device_info,
1139 .get_status = hw_get_status,
1140 .get_capabilities = hw_get_capabilities,
1141 .set_configuration = hw_set_configuration,
1142 .start_acquisition = hw_start_acquisition,
1143 .stop_acquisition = hw_stop_acquisition,