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;
88 /** Counter/index for the data block (0..2047) to be read. */
91 /** The divcount value (determines the sample period) for the LA8. */
95 /* This will be initialized via hw_get_device_info()/SR_DI_SAMPLERATES. */
96 static uint64_t supported_samplerates[255 + 1] = { 0 };
99 * Min: 1 sample per 0.01us -> sample time is 0.084s, samplerate 100MHz
100 * Max: 1 sample per 2.55us -> sample time is 21.391s, samplerate 392.15kHz
102 static struct sr_samplerates samplerates = {
106 .list = supported_samplerates,
109 /* Note: Continuous sampling is not supported by the hardware. */
110 static int capabilities[] = {
111 SR_HWCAP_LOGIC_ANALYZER,
113 SR_HWCAP_LIMIT_MSEC, /* TODO: Not yet implemented. */
114 SR_HWCAP_LIMIT_SAMPLES, /* TODO: Not yet implemented. */
118 /* Function prototypes. */
119 static int la8_close_usb_reset_sequencer(struct la8 *la8);
120 static void hw_stop_acquisition(int device_index, gpointer session_device_id);
121 static int la8_reset(struct la8 *la8);
123 static void fill_supported_samplerates_if_needed(void)
127 /* Do nothing if supported_samplerates[] is already filled. */
128 if (supported_samplerates[0] != 0)
131 /* Fill supported_samplerates[] with the proper values. */
132 for (i = 0; i < 255; i++)
133 supported_samplerates[254 - i] = SR_MHZ(100) / (i + 1);
134 supported_samplerates[255] = 0;
138 * Check if the given samplerate is supported by the LA8 hardware.
140 * @param samplerate The samplerate (in Hz) to check.
141 * @return 1 if the samplerate is supported/valid, 0 otherwise.
143 static int is_valid_samplerate(uint64_t samplerate)
147 fill_supported_samplerates_if_needed();
149 for (i = 0; i < 255; i++) {
150 if (supported_samplerates[i] == samplerate)
154 sr_warn("la8: %s: invalid samplerate (%" PRIu64 "Hz)",
155 __func__, samplerate);
161 * Convert a samplerate (in Hz) to the 'divcount' value the LA8 wants.
163 * LA8 hardware: sample period = (divcount + 1) * 10ns.
164 * Min. value for divcount: 0x00 (10ns sample period, 100MHz samplerate).
165 * Max. value for divcount: 0xfe (2550ns sample period, 392.15kHz samplerate).
167 * @param samplerate The samplerate in Hz.
168 * @return The divcount value as needed by the hardware, or 0xff upon errors.
170 static uint8_t samplerate_to_divcount(uint64_t samplerate)
172 if (samplerate == 0) {
173 sr_err("la8: %s: samplerate was 0", __func__);
177 if (!is_valid_samplerate(samplerate)) {
178 sr_err("la8: %s: can't get divcount, samplerate invalid",
183 return (SR_MHZ(100) / samplerate) - 1;
187 * Write data of a certain length to the LA8's FTDI device.
189 * @param la8 The LA8 struct containing private per-device-instance data.
190 * @param buf The buffer containing the data to write.
191 * @param size The number of bytes to write.
192 * @return The number of bytes written, or a negative value upon errors.
194 static int la8_write(struct la8 *la8, uint8_t *buf, int size)
199 sr_err("la8: %s: la8 was NULL", __func__);
204 sr_err("la8: %s: la8->ftdic was NULL", __func__);
209 sr_err("la8: %s: buf was NULL", __func__);
214 sr_err("la8: %s: size was < 0", __func__);
218 bytes_written = ftdi_write_data(la8->ftdic, buf, size);
220 if (bytes_written < 0) {
221 sr_warn("la8: %s: ftdi_write_data: (%d) %s", __func__,
222 bytes_written, ftdi_get_error_string(la8->ftdic));
223 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
224 } else if (bytes_written != size) {
225 sr_warn("la8: %s: bytes to write: %d, bytes written: %d",
226 __func__, size, bytes_written);
227 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
230 return bytes_written;
234 * Read a certain amount of bytes from the LA8's FTDI device.
236 * @param la8 The LA8 struct containing private per-device-instance data.
237 * @param buf The buffer where the received data will be stored.
238 * @param size The number of bytes to read.
239 * @return The number of bytes read, or a negative value upon errors.
241 static int la8_read(struct la8 *la8, uint8_t *buf, int size)
246 sr_err("la8: %s: la8 was NULL", __func__);
251 sr_err("la8: %s: la8->ftdic was NULL", __func__);
256 sr_err("la8: %s: buf was NULL", __func__);
261 sr_err("la8: %s: size was <= 0", __func__);
265 bytes_read = ftdi_read_data(la8->ftdic, buf, size);
267 if (bytes_read < 0) {
268 sr_warn("la8: %s: ftdi_read_data: (%d) %s", __func__,
269 bytes_read, ftdi_get_error_string(la8->ftdic));
270 } else if (bytes_read != size) {
271 // sr_warn("la8: %s: bytes to read: %d, bytes read: %d",
272 // __func__, size, bytes_read);
278 static int la8_close(struct la8 *la8)
283 sr_err("la8: %s: la8 was NULL", __func__);
288 sr_err("la8: %s: la8->ftdic was NULL", __func__);
292 if ((ret = ftdi_usb_close(la8->ftdic)) < 0) {
293 sr_warn("la8: %s: ftdi_usb_close: (%d) %s",
294 __func__, ret, ftdi_get_error_string(la8->ftdic));
301 * Close the ChronoVu LA8 USB port and reset the LA8 sequencer logic.
303 * @param la8 The LA8 struct containing private per-device-instance data.
304 * @return SR_OK upon success, SR_ERR upon failure.
306 static int la8_close_usb_reset_sequencer(struct la8 *la8)
308 /* Magic sequence of bytes for resetting the LA8 sequencer logic. */
309 uint8_t buf[8] = {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};
312 sr_dbg("la8: entering %s", __func__);
315 sr_err("la8: %s: la8 was NULL", __func__);
320 sr_err("la8: %s: la8->ftdic was NULL", __func__);
324 if (la8->ftdic->usb_dev) {
325 /* Reset the LA8 sequencer logic, then wait 100ms. */
326 sr_dbg("la8: resetting sequencer logic");
327 (void) la8_write(la8, buf, 8); /* Ignore errors. */
328 g_usleep(100 * 1000);
330 /* Purge FTDI buffers, then reset and close the FTDI device. */
331 sr_dbg("la8: purging buffers, resetting+closing FTDI device");
333 /* Log errors, but ignore them (i.e., don't abort). */
334 if ((ret = ftdi_usb_purge_buffers(la8->ftdic)) < 0)
335 sr_warn("la8: %s: ftdi_usb_purge_buffers: (%d) %s",
336 __func__, ret, ftdi_get_error_string(la8->ftdic));
337 if ((ret = ftdi_usb_reset(la8->ftdic)) < 0)
338 sr_warn("la8: %s: ftdi_usb_reset: (%d) %s", __func__,
339 ret, ftdi_get_error_string(la8->ftdic));
340 if ((ret = ftdi_usb_close(la8->ftdic)) < 0)
341 sr_warn("la8: %s: ftdi_usb_close: (%d) %s", __func__,
342 ret, ftdi_get_error_string(la8->ftdic));
344 sr_dbg("la8: %s: usb_dev was NULL, nothing to do", __func__);
347 ftdi_free(la8->ftdic); /* Returns void. */
354 * Reset the ChronoVu LA8.
356 * The LA8 must be reset after a failed read/write operation or upon timeouts.
358 * @param la8 The LA8 struct containing private per-device-instance data.
359 * @return SR_OK upon success, SR_ERR upon failure.
361 static int la8_reset(struct la8 *la8)
368 sr_err("la8: %s: la8 was NULL", __func__);
373 sr_err("la8: %s: la8->ftdic was NULL", __func__);
377 sr_dbg("la8: resetting the device");
380 * Purge pending read data from the FTDI hardware FIFO until
381 * no more data is left, or a timeout occurs (after 20s).
383 done = 20 + time(NULL);
385 /* TODO: Ignore errors? Check for < 0 at least! */
386 bytes_read = la8_read(la8, (uint8_t *)&buf, 4096);
388 } while ((done > now) && (bytes_read > 0));
390 /* Reset the LA8 sequencer logic and close the USB port. */
391 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
393 sr_dbg("la8: device reset finished");
398 static int configure_probes(struct la8 *la8, GSList *probes)
400 struct sr_probe *probe;
405 la8->trigger_pattern = 0;
406 la8->trigger_mask = 0; /* Default to "don't care" for all probes. */
408 for (l = probes; l; l = l->next) {
409 probe = (struct sr_probe *)l->data;
412 sr_err("la8: %s: probe was NULL", __func__);
416 /* Skip disabled probes. */
420 /* Skip (enabled) probes with no configured trigger. */
424 /* Note: Must only be run if probe->trigger != NULL. */
425 if (probe->index < 0 || probe->index > 7) {
426 sr_err("la8: %s: invalid probe index %d, must be "
427 "between 0 and 7", __func__, probe->index);
431 probe_bit = (1 << (probe->index - 1));
433 /* Configure the probe's trigger mask and trigger pattern. */
434 for (tc = probe->trigger; tc && *tc; tc++) {
435 la8->trigger_mask |= probe_bit;
437 /* Sanity check, LA8 only supports low/high trigger. */
438 if (*tc != '0' && *tc != '1') {
439 sr_err("la8: %s: invalid trigger '%c', only "
440 "'0'/'1' supported", __func__, *tc);
445 la8->trigger_pattern |= probe_bit;
449 sr_dbg("la8: %s: trigger_mask = 0x%x, trigger_pattern = 0x%x",
450 __func__, la8->trigger_mask, la8->trigger_pattern);
455 static int hw_init(const char *deviceinfo)
458 struct sr_device_instance *sdi;
461 sr_dbg("la8: entering %s", __func__);
463 /* Avoid compiler errors. */
464 deviceinfo = deviceinfo;
466 /* Allocate memory for our private driver context. */
467 if (!(la8 = g_try_malloc(sizeof(struct la8)))) {
468 sr_err("la8: %s: struct la8 malloc failed", __func__);
470 goto err_free_nothing;
473 /* Set some sane defaults. */
475 la8->cur_samplerate = SR_MHZ(100); /* 100MHz == max. samplerate */
477 la8->limit_samples = 0;
478 la8->session_id = NULL;
479 memset(la8->mangled_buf, 0, 4096);
480 la8->final_buf = NULL;
481 la8->trigger_pattern = 0x00; /* Value irrelevant, see trigger_mask. */
482 la8->trigger_mask = 0x00; /* All probes are "don't care". */
483 la8->trigger_timeout = 10; /* Default to 10s trigger timeout. */
485 la8->block_counter = 0;
486 la8->divcount = 0; /* 10ns sample period == 100MHz samplerate */
488 /* Allocate memory where we'll store the de-mangled data. */
489 if (!(la8->final_buf = g_try_malloc(SDRAM_SIZE))) {
490 sr_err("la8: %s: final_buf malloc failed", __func__);
495 /* Allocate memory for the FTDI context (ftdic) and initialize it. */
496 if (!(la8->ftdic = ftdi_new())) {
497 sr_err("la8: %s: ftdi_new failed", __func__);
498 ret = SR_ERR; /* TODO: More specific error? */
499 goto err_free_final_buf;
502 /* Check for the device and temporarily open it. */
503 if ((ret = ftdi_usb_open_desc(la8->ftdic, USB_VENDOR_ID,
504 USB_PRODUCT_ID, USB_DESCRIPTION, NULL)) < 0) {
505 sr_err("la8: %s: ftdi_usb_open_desc: (%d) %s",
506 __func__, ret, ftdi_get_error_string(la8->ftdic));
507 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
508 ret = SR_ERR; /* TODO: More specific error? */
511 sr_dbg("la8: found device");
513 /* Register the device with libsigrok. */
514 sdi = sr_device_instance_new(0, SR_ST_INITIALIZING,
515 USB_VENDOR_NAME, USB_MODEL_NAME, USB_MODEL_VERSION);
517 sr_err("la8: %s: sr_device_instance_new failed", __func__);
518 ret = SR_ERR; /* TODO: More specific error? */
519 goto err_close_ftdic;
524 device_instances = g_slist_append(device_instances, sdi);
526 sr_dbg("la8: %s finished successfully", __func__);
528 /* Close device. We'll reopen it again when we need it. */
529 (void) la8_close(la8); /* Log, but ignore errors. */
531 // return SR_OK; /* TODO */
535 (void) la8_close(la8); /* Log, but ignore errors. */
537 free(la8->ftdic); /* NOT g_free()! */
539 g_free(la8->final_buf);
543 // return ret; /* TODO */
547 static int hw_opendev(int device_index)
550 struct sr_device_instance *sdi;
553 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
554 sr_err("la8: %s: sdi was NULL", __func__);
555 return SR_ERR; /* TODO: SR_ERR_ARG? */
558 if (!(la8 = sdi->priv)) {
559 sr_err("la8: %s: sdi->priv was NULL", __func__);
560 return SR_ERR; /* TODO: SR_ERR_ARG? */
563 sr_dbg("la8: opening device");
565 /* Open the device. */
566 if ((ret = ftdi_usb_open_desc(la8->ftdic, USB_VENDOR_ID,
567 USB_PRODUCT_ID, USB_DESCRIPTION, NULL)) < 0) {
568 sr_err("la8: %s: ftdi_usb_open_desc: (%d) %s",
569 __func__, ret, ftdi_get_error_string(la8->ftdic));
570 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
573 sr_dbg("la8: device opened successfully");
575 /* Purge RX/TX buffers in the FTDI chip. */
576 if ((ret = ftdi_usb_purge_buffers(la8->ftdic)) < 0) {
577 sr_err("la8: %s: ftdi_usb_purge_buffers: (%d) %s",
578 __func__, ret, ftdi_get_error_string(la8->ftdic));
579 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
580 goto err_opendev_close_ftdic;
582 sr_dbg("la8: FTDI buffers purged successfully");
584 /* Enable flow control in the FTDI chip. */
585 if ((ret = ftdi_setflowctrl(la8->ftdic, SIO_RTS_CTS_HS)) < 0) {
586 sr_err("la8: %s: ftdi_setflowcontrol: (%d) %s",
587 __func__, ret, ftdi_get_error_string(la8->ftdic));
588 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
589 goto err_opendev_close_ftdic;
591 sr_dbg("la8: FTDI flow control enabled successfully");
594 g_usleep(100 * 1000);
596 sdi->status = SR_ST_ACTIVE;
600 err_opendev_close_ftdic:
601 (void) la8_close(la8); /* Log, but ignore errors. */
605 static int set_samplerate(struct sr_device_instance *sdi, uint64_t samplerate)
610 sr_err("la8: %s: sdi was NULL", __func__);
614 if (!(la8 = sdi->priv)) {
615 sr_err("la8: %s: sdi->priv was NULL", __func__);
619 sr_dbg("la8: setting samplerate");
621 fill_supported_samplerates_if_needed();
623 /* Check if this is a samplerate supported by the hardware. */
624 if (!is_valid_samplerate(samplerate))
627 /* Set the new samplerate. */
628 la8->cur_samplerate = samplerate;
630 sr_dbg("la8: samplerate set to %" PRIu64 "Hz", la8->cur_samplerate);
635 static int hw_closedev(int device_index)
637 struct sr_device_instance *sdi;
640 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
641 sr_err("la8: %s: sdi was NULL", __func__);
642 return SR_ERR; /* TODO: SR_ERR_ARG? */
645 if (!(la8 = sdi->priv)) {
646 sr_err("la8: %s: sdi->priv was NULL", __func__);
647 return SR_ERR; /* TODO: SR_ERR_ARG? */
650 sr_dbg("la8: closing device");
652 if (sdi->status == SR_ST_ACTIVE) {
653 sr_dbg("la8: %s: status ACTIVE, closing device", __func__);
654 /* TODO: Really ignore errors here, or return SR_ERR? */
655 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
657 sr_dbg("la8: %s: status not ACTIVE, nothing to do", __func__);
660 sdi->status = SR_ST_INACTIVE;
662 sr_dbg("la8: %s: freeing sample buffers", __func__);
663 free(la8->final_buf);
668 static void hw_cleanup(void)
671 struct sr_device_instance *sdi;
673 sr_dbg("la8: entering %s", __func__);
675 /* Properly close all devices. */
676 for (l = device_instances; l; l = l->next) {
677 if ((sdi = l->data) == NULL) {
678 sr_warn("la8: %s: sdi was NULL, continuing", __func__);
681 if (sdi->priv != NULL)
684 sr_warn("la8: %s: sdi->priv was NULL, nothing "
686 sr_device_instance_free(sdi); /* Returns void. */
688 g_slist_free(device_instances); /* Returns void. */
689 device_instances = NULL;
692 static void *hw_get_device_info(int device_index, int device_info_id)
694 struct sr_device_instance *sdi;
698 sr_dbg("la8: entering %s", __func__);
700 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
701 sr_err("la8: %s: sdi was NULL", __func__);
705 if (!(la8 = sdi->priv)) {
706 sr_err("la8: %s: sdi->priv was NULL", __func__);
710 switch (device_info_id) {
714 case SR_DI_NUM_PROBES:
715 info = GINT_TO_POINTER(NUM_PROBES);
717 case SR_DI_SAMPLERATES:
718 fill_supported_samplerates_if_needed();
721 case SR_DI_TRIGGER_TYPES:
722 info = (char *)TRIGGER_TYPES;
724 case SR_DI_CUR_SAMPLERATE:
725 info = &la8->cur_samplerate;
728 /* Unknown device info ID, return NULL. */
729 sr_err("la8: %s: Unknown device info ID", __func__);
737 static int hw_get_status(int device_index)
739 struct sr_device_instance *sdi;
741 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
742 sr_warn("la8: %s: sdi was NULL, device not found", __func__);
743 return SR_ST_NOT_FOUND;
746 sr_dbg("la8: %s: returning status %d", __func__, sdi->status);
751 static int *hw_get_capabilities(void)
753 sr_dbg("la8: entering %s", __func__);
758 static int hw_set_configuration(int device_index, int capability, void *value)
760 struct sr_device_instance *sdi;
763 sr_dbg("la8: entering %s", __func__);
765 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
766 sr_err("la8: %s: sdi was NULL", __func__);
767 return SR_ERR; /* TODO: SR_ERR_ARG? */
770 if (!(la8 = sdi->priv)) {
771 sr_err("la8: %s: sdi->priv was NULL", __func__);
772 return SR_ERR; /* TODO: SR_ERR_ARG? */
775 switch (capability) {
776 case SR_HWCAP_SAMPLERATE:
777 if (set_samplerate(sdi, *(uint64_t *)value) == SR_ERR)
779 sr_dbg("la8: SAMPLERATE = %" PRIu64, la8->cur_samplerate);
781 case SR_HWCAP_PROBECONFIG:
782 if (configure_probes(la8, (GSList *)value) != SR_OK) {
783 sr_err("la8: %s: probe config failed", __func__);
787 case SR_HWCAP_LIMIT_MSEC:
788 if (*(uint64_t *)value == 0) {
789 sr_err("la8: %s: LIMIT_MSEC can't be 0", __func__);
792 la8->limit_msec = *(uint64_t *)value;
793 sr_dbg("la8: LIMIT_MSEC = %" PRIu64, la8->limit_msec);
795 case SR_HWCAP_LIMIT_SAMPLES:
796 if (*(uint64_t *)value < MIN_NUM_SAMPLES) {
797 sr_err("la8: %s: LIMIT_SAMPLES too small", __func__);
800 la8->limit_samples = *(uint64_t *)value;
801 sr_dbg("la8: LIMIT_SAMPLES = %" PRIu64, la8->limit_samples);
804 /* Unknown capability, return SR_ERR. */
805 sr_err("la8: %s: Unknown capability", __func__);
814 * Get a block of 4096 bytes of data from the LA8.
816 * @param la8 The LA8 struct containing private per-device-instance data.
817 * @return SR_OK upon success, or SR_ERR upon errors.
819 static int la8_read_block(struct la8 *la8)
821 int i, byte_offset, m, mi, p, index, bytes_read;
825 sr_err("la8: %s: la8 was NULL", __func__);
830 sr_err("la8: %s: la8->ftdic was NULL", __func__);
834 // sr_dbg("la8: %s: reading block %d", __func__, la8->block_counter);
836 bytes_read = la8_read(la8, la8->mangled_buf, 4096);
838 /* If first block read got 0 bytes, retry until success or timeout. */
839 if ((bytes_read == 0) && (la8->block_counter == 0)) {
841 // sr_dbg("la8: %s: reading block 0 again", __func__);
842 bytes_read = la8_read(la8, la8->mangled_buf, 4096);
843 /* TODO: How to handle read errors here? */
845 } while ((la8->done > now) && (bytes_read == 0));
848 /* Check if block read was successful or a timeout occured. */
849 if (bytes_read != 4096) {
850 sr_warn("la8: %s: trigger timed out", __func__);
851 (void) la8_reset(la8); /* Ignore errors. */
855 /* De-mangle the data. */
856 // sr_dbg("la8: de-mangling samples of block %d", la8->block_counter);
857 byte_offset = la8->block_counter * 4096;
858 m = byte_offset / (1024 * 1024);
859 mi = m * (1024 * 1024);
860 for (i = 0; i < 4096; i++) {
862 index = m * 2 + (((byte_offset + i) - mi) / 2) * 16;
863 index += (la8->divcount == 0) ? p : (1 - p);
864 la8->final_buf[index] = la8->mangled_buf[i];
870 static int receive_data(int fd, int revents, void *user_data)
873 struct sr_device_instance *sdi;
874 struct sr_datafeed_packet packet;
877 /* Avoid compiler errors. */
881 if (!(sdi = user_data)) {
882 sr_err("la8: %s: user_data was NULL", __func__);
886 if (!(la8 = sdi->priv)) {
887 sr_err("la8: %s: sdi->priv was NULL", __func__);
891 /* Get one block of data (4096 bytes). */
892 if ((ret = la8_read_block(la8)) < 0) {
893 sr_err("la8: %s: la8_read_block error: %d", __func__, ret);
894 hw_stop_acquisition(sdi->index, user_data);
898 /* We need to get exactly 2048 blocks (i.e. 8MB) of data. */
899 if (la8->block_counter != 2047) {
900 la8->block_counter++;
904 sr_dbg("la8: sampling finished, sending data to session bus now");
906 /* All data was received and demangled, send it to the session bus. */
907 for (i = 0; i < 2048; i++) {
908 /* Send a 4096 byte SR_DF_LOGIC packet to the session bus. */
909 // sr_dbg("la8: %s: sending SR_DF_LOGIC packet", __func__);
910 packet.type = SR_DF_LOGIC;
911 packet.length = 4096;
913 packet.payload = la8->final_buf + (i * 4096);
914 sr_session_bus(la8->session_id, &packet);
917 hw_stop_acquisition(sdi->index, user_data);
919 // return FALSE; /* FIXME? */
923 static int hw_start_acquisition(int device_index, gpointer session_device_id)
925 struct sr_device_instance *sdi;
927 struct sr_datafeed_packet packet;
928 struct sr_datafeed_header header;
932 sr_dbg("la8: entering %s", __func__);
934 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
935 sr_err("la8: %s: sdi was NULL", __func__);
936 return SR_ERR; /* TODO: SR_ERR_ARG? */
939 if (!(la8 = sdi->priv)) {
940 sr_err("la8: %s: sdi->priv was NULL", __func__);
941 return SR_ERR; /* TODO: SR_ERR_ARG? */
945 sr_err("la8: %s: la8->ftdic was NULL", __func__);
949 la8->divcount = samplerate_to_divcount(la8->cur_samplerate);
950 if (la8->divcount == 0xff) {
951 sr_err("la8: %s: invalid divcount/samplerate", __func__);
955 /* Fill acquisition parameters into buf[]. */
956 buf[0] = la8->divcount;
957 buf[1] = 0xff; /* This byte must always be 0xff. */
958 buf[2] = la8->trigger_pattern;
959 buf[3] = la8->trigger_mask;
961 /* Start acquisition. */
962 bytes_written = la8_write(la8, buf, 4);
964 if (bytes_written < 0) {
965 sr_err("la8: acquisition failed to start");
967 } else if (bytes_written != 4) {
968 sr_err("la8: acquisition failed to start");
969 return SR_ERR; /* TODO: Other error and return code? */
972 sr_dbg("la8: acquisition started successfully");
974 la8->session_id = session_device_id;
976 /* Send header packet to the session bus. */
977 sr_dbg("la8: %s: sending SR_DF_HEADER", __func__);
978 packet.type = SR_DF_HEADER;
979 packet.length = sizeof(struct sr_datafeed_header);
981 packet.payload = &header;
982 header.feed_version = 1;
983 gettimeofday(&header.starttime, NULL);
984 header.samplerate = la8->cur_samplerate;
985 header.protocol_id = SR_PROTO_RAW;
986 header.num_logic_probes = NUM_PROBES;
987 header.num_analog_probes = 0;
988 sr_session_bus(session_device_id, &packet);
990 /* Time when we should be done (for detecting trigger timeouts). */
991 la8->done = (la8->divcount + 1) * 0.08388608 + time(NULL)
992 + la8->trigger_timeout;
993 la8->block_counter = 0;
995 /* Hook up a dummy handler to receive data from the LA8. */
996 sr_source_add(-1, G_IO_IN, 0, receive_data, sdi);
1001 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
1003 struct sr_device_instance *sdi;
1005 struct sr_datafeed_packet packet;
1007 sr_dbg("la8: stopping acquisition");
1009 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
1010 sr_err("la8: %s: sdi was NULL", __func__);
1014 if (!(la8 = sdi->priv)) {
1015 sr_err("la8: %s: sdi->priv was NULL", __func__);
1019 /* Send end packet to the session bus. */
1020 sr_dbg("la8: %s: sending SR_DF_END", __func__);
1021 packet.type = SR_DF_END;
1023 packet.unitsize = 0;
1024 packet.payload = NULL;
1025 sr_session_bus(session_device_id, &packet);
1028 struct sr_device_plugin chronovu_la8_plugin_info = {
1029 .name = "chronovu-la8",
1030 .longname = "ChronoVu LA8",
1033 .cleanup = hw_cleanup,
1034 .opendev = hw_opendev,
1035 .closedev = hw_closedev,
1036 .get_device_info = hw_get_device_info,
1037 .get_status = hw_get_status,
1038 .get_capabilities = hw_get_capabilities,
1039 .set_configuration = hw_set_configuration,
1040 .start_acquisition = hw_start_acquisition,
1041 .stop_acquisition = hw_stop_acquisition,