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_err("la8: %s: samplerate was 0", __func__);
179 if (!is_valid_samplerate(samplerate)) {
180 sr_err("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_err("la8: %s: la8 was NULL", __func__);
206 sr_err("la8: %s: la8->ftdic was NULL", __func__);
211 sr_err("la8: %s: buf was NULL", __func__);
216 sr_err("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_err("la8: %s: la8 was NULL", __func__);
253 sr_err("la8: %s: la8->ftdic was NULL", __func__);
258 sr_err("la8: %s: buf was NULL", __func__);
263 sr_err("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_err("la8: %s: la8 was NULL", __func__);
290 sr_err("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_err("la8: %s: la8 was NULL", __func__);
322 sr_err("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_err("la8: %s: la8 was NULL", __func__);
375 sr_err("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_err("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_err("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_err("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_err("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_err("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_err("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_err("la8: %s: sdi was NULL", __func__);
567 return SR_ERR; /* TODO: SR_ERR_ARG? */
570 if (!(la8 = sdi->priv)) {
571 sr_err("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_err("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_err("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_err("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_err("la8: %s: sdi was NULL", __func__);
626 if (!(la8 = sdi->priv)) {
627 sr_err("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_err("la8: %s: sdi was NULL", __func__);
657 if (!(la8 = sdi->priv)) {
658 sr_err("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, continuing", __func__);
691 if (sdi->priv != NULL)
694 sr_warn("la8: %s: sdi->priv was NULL, nothing "
696 sr_device_instance_free(sdi); /* Returns void. */
698 g_slist_free(device_instances); /* Returns void. */
699 device_instances = NULL;
702 static void *hw_get_device_info(int device_index, int device_info_id)
704 struct sr_device_instance *sdi;
708 sr_dbg("la8: entering %s", __func__);
710 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
711 sr_err("la8: %s: sdi was NULL", __func__);
715 if (!(la8 = sdi->priv)) {
716 sr_err("la8: %s: sdi->priv was NULL", __func__);
720 switch (device_info_id) {
724 case SR_DI_NUM_PROBES:
725 info = GINT_TO_POINTER(NUM_PROBES);
727 case SR_DI_SAMPLERATES:
728 fill_supported_samplerates_if_needed();
731 case SR_DI_TRIGGER_TYPES:
732 info = (char *)TRIGGER_TYPES;
734 case SR_DI_CUR_SAMPLERATE:
735 info = &la8->cur_samplerate;
738 /* Unknown device info ID, return NULL. */
739 sr_err("la8: %s: Unknown device info ID", __func__);
747 static int hw_get_status(int device_index)
749 struct sr_device_instance *sdi;
751 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
752 sr_warn("la8: %s: sdi was NULL, device not found", __func__);
753 return SR_ST_NOT_FOUND;
756 sr_dbg("la8: %s: returning status %d", __func__, sdi->status);
761 static int *hw_get_capabilities(void)
763 sr_dbg("la8: entering %s", __func__);
768 static int hw_set_configuration(int device_index, int capability, void *value)
770 struct sr_device_instance *sdi;
773 sr_dbg("la8: entering %s", __func__);
775 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
776 sr_err("la8: %s: sdi was NULL", __func__);
777 return SR_ERR; /* TODO: SR_ERR_ARG? */
780 if (!(la8 = sdi->priv)) {
781 sr_err("la8: %s: sdi->priv was NULL", __func__);
782 return SR_ERR; /* TODO: SR_ERR_ARG? */
785 switch (capability) {
786 case SR_HWCAP_SAMPLERATE:
787 if (set_samplerate(sdi, *(uint64_t *)value) == SR_ERR)
789 sr_dbg("la8: SAMPLERATE = %" PRIu64, la8->cur_samplerate);
791 case SR_HWCAP_PROBECONFIG:
792 if (configure_probes(la8, (GSList *)value) != SR_OK) {
793 sr_err("la8: %s: probe config failed", __func__);
797 case SR_HWCAP_LIMIT_MSEC:
798 if (*(uint64_t *)value == 0) {
799 sr_err("la8: %s: LIMIT_MSEC can't be 0", __func__);
802 la8->limit_msec = *(uint64_t *)value;
803 sr_dbg("la8: LIMIT_MSEC = %" PRIu64, la8->limit_msec);
805 case SR_HWCAP_LIMIT_SAMPLES:
806 if (*(uint64_t *)value < MIN_NUM_SAMPLES) {
807 sr_err("la8: %s: LIMIT_SAMPLES too small", __func__);
810 la8->limit_samples = *(uint64_t *)value;
811 sr_dbg("la8: LIMIT_SAMPLES = %" PRIu64, la8->limit_samples);
814 /* Unknown capability, return SR_ERR. */
815 sr_err("la8: %s: Unknown capability", __func__);
824 * Get a block of 4096 bytes of data from the LA8.
826 * @param la8 The LA8 struct containing private per-device-instance data.
827 * @return SR_OK upon success, or SR_ERR upon errors.
829 static int la8_read_block(struct la8 *la8)
831 int i, byte_offset, m, mi, p, index, bytes_read;
835 sr_err("la8: %s: la8 was NULL", __func__);
840 sr_err("la8: %s: la8->ftdic was NULL", __func__);
844 // sr_dbg("la8: %s: reading block %d", __func__, la8->block_counter);
846 bytes_read = la8_read(la8, la8->mangled_buf, 4096);
848 /* If first block read got 0 bytes, retry until success or timeout. */
849 if ((bytes_read == 0) && (la8->block_counter == 0)) {
851 // sr_dbg("la8: %s: reading block 0 again", __func__);
852 bytes_read = la8_read(la8, la8->mangled_buf, 4096);
853 /* TODO: How to handle read errors here? */
855 } while ((la8->done > now) && (bytes_read == 0));
858 /* Check if block read was successful or a timeout occured. */
859 if (bytes_read != 4096) {
860 sr_warn("la8: %s: trigger timed out", __func__);
861 (void) la8_reset(la8); /* Ignore errors. */
865 /* De-mangle the data. */
866 // sr_dbg("la8: de-mangling samples of block %d", la8->block_counter);
867 byte_offset = la8->block_counter * 4096;
868 m = byte_offset / (1024 * 1024);
869 mi = m * (1024 * 1024);
870 for (i = 0; i < 4096; i++) {
872 index = m * 2 + (((byte_offset + i) - mi) / 2) * 16;
873 index += (la8->divcount == 0) ? p : (1 - p);
874 la8->final_buf[index] = la8->mangled_buf[i];
880 static int receive_data(int fd, int revents, void *user_data)
883 struct sr_device_instance *sdi;
884 struct sr_datafeed_packet packet;
887 /* Avoid compiler errors. */
891 if (!(sdi = user_data)) {
892 sr_err("la8: %s: user_data was NULL", __func__);
896 if (!(la8 = sdi->priv)) {
897 sr_err("la8: %s: sdi->priv was NULL", __func__);
901 /* Get one block of data (4096 bytes). */
902 if ((ret = la8_read_block(la8)) < 0) {
903 sr_err("la8: %s: la8_read_block error: %d", __func__, ret);
904 hw_stop_acquisition(sdi->index, user_data);
908 /* We need to get exactly 2048 blocks (i.e. 8MB) of data. */
909 if (la8->block_counter != 2047) {
910 la8->block_counter++;
914 sr_dbg("la8: sampling finished, sending data to session bus now");
916 /* All data was received and demangled, send it to the session bus. */
917 for (i = 0; i < 2048; i++) {
918 /* Send a 4096 byte SR_DF_LOGIC packet to the session bus. */
919 // sr_dbg("la8: %s: sending SR_DF_LOGIC packet", __func__);
920 packet.type = SR_DF_LOGIC;
921 packet.length = 4096;
923 packet.payload = la8->final_buf + (i * 4096);
924 sr_session_bus(la8->session_id, &packet);
927 hw_stop_acquisition(sdi->index, user_data);
929 // return FALSE; /* FIXME? */
933 static int hw_start_acquisition(int device_index, gpointer session_device_id)
935 struct sr_device_instance *sdi;
937 struct sr_datafeed_packet packet;
938 struct sr_datafeed_header header;
942 sr_dbg("la8: entering %s", __func__);
944 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
945 sr_err("la8: %s: sdi was NULL", __func__);
946 return SR_ERR; /* TODO: SR_ERR_ARG? */
949 if (!(la8 = sdi->priv)) {
950 sr_err("la8: %s: sdi->priv was NULL", __func__);
951 return SR_ERR; /* TODO: SR_ERR_ARG? */
955 sr_err("la8: %s: la8->ftdic was NULL", __func__);
959 la8->divcount = samplerate_to_divcount(la8->cur_samplerate);
960 if (la8->divcount == 0xff) {
961 sr_err("la8: %s: invalid divcount/samplerate", __func__);
965 /* Fill acquisition parameters into buf[]. */
966 buf[0] = la8->divcount;
967 buf[1] = 0xff; /* This byte must always be 0xff. */
968 buf[2] = la8->trigger_pattern;
969 buf[3] = la8->trigger_mask;
971 /* Start acquisition. */
972 bytes_written = la8_write(la8, buf, 4);
974 if (bytes_written < 0) {
975 sr_err("la8: acquisition failed to start");
977 } else if (bytes_written != 4) {
978 sr_err("la8: acquisition failed to start");
979 return SR_ERR; /* TODO: Other error and return code? */
982 sr_dbg("la8: acquisition started successfully");
984 la8->session_id = session_device_id;
986 /* Send header packet to the session bus. */
987 sr_dbg("la8: %s: sending SR_DF_HEADER", __func__);
988 packet.type = SR_DF_HEADER;
989 packet.length = sizeof(struct sr_datafeed_header);
991 packet.payload = &header;
992 header.feed_version = 1;
993 gettimeofday(&header.starttime, NULL);
994 header.samplerate = la8->cur_samplerate;
995 header.protocol_id = SR_PROTO_RAW;
996 header.num_logic_probes = la8->num_probes;
997 header.num_analog_probes = 0;
998 sr_session_bus(session_device_id, &packet);
1000 /* Time when we should be done (for detecting trigger timeouts). */
1001 la8->done = (la8->divcount + 1) * 0.08388608 + time(NULL)
1002 + la8->trigger_timeout;
1003 la8->block_counter = 0;
1005 /* Hook up a dummy handler to receive data from the LA8. */
1006 sr_source_add(-1, G_IO_IN, 0, receive_data, sdi);
1011 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
1013 struct sr_device_instance *sdi;
1015 struct sr_datafeed_packet packet;
1017 sr_dbg("la8: stopping acquisition");
1019 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
1020 sr_err("la8: %s: sdi was NULL", __func__);
1024 if (!(la8 = sdi->priv)) {
1025 sr_err("la8: %s: sdi->priv was NULL", __func__);
1029 /* Send end packet to the session bus. */
1030 sr_dbg("la8: %s: sending SR_DF_END", __func__);
1031 packet.type = SR_DF_END;
1033 packet.unitsize = 0;
1034 packet.payload = NULL;
1035 sr_session_bus(session_device_id, &packet);
1038 struct sr_device_plugin chronovu_la8_plugin_info = {
1039 .name = "chronovu-la8",
1040 .longname = "ChronoVu LA8",
1043 .cleanup = hw_cleanup,
1045 .close = hw_closedev,
1046 .get_device_info = hw_get_device_info,
1047 .get_status = hw_get_status,
1048 .get_capabilities = hw_get_capabilities,
1049 .set_configuration = hw_set_configuration,
1050 .start_acquisition = hw_start_acquisition,
1051 .stop_acquisition = hw_stop_acquisition,