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
23 #include <sigrok-internal.h>
25 #define USB_VENDOR_ID 0x0403
26 #define USB_PRODUCT_ID 0x6001
27 #define USB_DESCRIPTION "ChronoVu LA8"
28 #define USB_VENDOR_NAME "ChronoVu"
29 #define USB_MODEL_NAME "LA8"
30 #define USB_MODEL_VERSION ""
33 #define TRIGGER_TYPES "01"
34 #define SDRAM_SIZE (8 * 1024 * 1024)
35 #define MIN_NUM_SAMPLES 1
37 static GSList *device_instances = NULL;
40 /** FTDI device context (used by libftdi). */
41 struct ftdi_context *ftdic;
43 /** The currently configured samplerate of the device. */
44 uint64_t cur_samplerate;
46 /** The current sampling limit (in ms). */
49 /** The current sampling limit (in number of samples). */
50 uint64_t limit_samples;
52 /** The number of probes. */
59 * An 8MB buffer containing the (mangled) samples from the device.
60 * Format: Pretty mangled-up (due to hardware reasons), see code.
65 * An 8MB buffer where we'll store the de-mangled samples.
66 * Format: Each sample is 1 byte, MSB is channel 7, LSB is channel 0.
71 * Trigger pattern (MSB = channel 7, LSB = channel 0).
72 * A 1 bit matches a high signal, 0 matches a low signal on a probe.
73 * Only low/high triggers (but not e.g. rising/falling) are supported.
75 uint8_t trigger_pattern;
78 * Trigger mask (MSB = channel 7, LSB = channel 0).
79 * A 1 bit means "must match trigger_pattern", 0 means "don't care".
83 /** Time (in seconds) before the trigger times out. */
84 uint64_t trigger_timeout;
89 /** Counter/index for the data block (0..2047) to be read. */
92 /** The divcount value (determines the sample period) for the LA8. */
96 /* This will be initialized via hw_get_device_info()/SR_DI_SAMPLERATES. */
97 static uint64_t supported_samplerates[255 + 1] = { 0 };
100 * Min: 1 sample per 0.01us -> sample time is 0.084s, samplerate 100MHz
101 * Max: 1 sample per 2.55us -> sample time is 21.391s, samplerate 392.15kHz
103 static struct sr_samplerates samplerates = {
107 .list = supported_samplerates,
110 /* Note: Continuous sampling is not supported by the hardware. */
111 static int capabilities[] = {
112 SR_HWCAP_LOGIC_ANALYZER,
114 SR_HWCAP_LIMIT_MSEC, /* TODO: Not yet implemented. */
115 SR_HWCAP_LIMIT_SAMPLES, /* TODO: Not yet implemented. */
119 /* Function prototypes. */
120 static int la8_close_usb_reset_sequencer(struct la8 *la8);
121 static void hw_stop_acquisition(int device_index, gpointer session_device_id);
122 static int la8_reset(struct la8 *la8);
124 static void fill_supported_samplerates_if_needed(void)
128 /* Do nothing if supported_samplerates[] is already filled. */
129 if (supported_samplerates[0] != 0)
132 /* Fill supported_samplerates[] with the proper values. */
133 for (i = 0; i < 255; i++)
134 supported_samplerates[254 - i] = SR_MHZ(100) / (i + 1);
135 supported_samplerates[255] = 0;
139 * Check if the given samplerate is supported by the LA8 hardware.
141 * @param samplerate The samplerate (in Hz) to check.
142 * @return 1 if the samplerate is supported/valid, 0 otherwise.
144 static int is_valid_samplerate(uint64_t samplerate)
148 fill_supported_samplerates_if_needed();
150 for (i = 0; i < 255; i++) {
151 if (supported_samplerates[i] == samplerate)
155 sr_warn("la8: %s: invalid samplerate (%" PRIu64 "Hz)",
156 __func__, samplerate);
162 * Convert a samplerate (in Hz) to the 'divcount' value the LA8 wants.
164 * LA8 hardware: sample period = (divcount + 1) * 10ns.
165 * Min. value for divcount: 0x00 (10ns sample period, 100MHz samplerate).
166 * Max. value for divcount: 0xfe (2550ns sample period, 392.15kHz samplerate).
168 * @param samplerate The samplerate in Hz.
169 * @return The divcount value as needed by the hardware, or 0xff upon errors.
171 static uint8_t samplerate_to_divcount(uint64_t samplerate)
173 if (samplerate == 0) {
174 sr_warn("la8: %s: samplerate was 0", __func__);
178 if (!is_valid_samplerate(samplerate)) {
179 sr_warn("la8: %s: can't get divcount, samplerate invalid",
184 return (SR_MHZ(100) / samplerate) - 1;
188 * Write data of a certain length to the LA8's FTDI device.
190 * @param la8 The LA8 struct containing private per-device-instance data.
191 * @param buf The buffer containing the data to write.
192 * @param size The number of bytes to write.
193 * @return The number of bytes written, or a negative value upon errors.
195 static int la8_write(struct la8 *la8, uint8_t *buf, int size)
200 sr_warn("la8: %s: la8 was NULL", __func__);
205 sr_warn("la8: %s: la8->ftdic was NULL", __func__);
210 sr_warn("la8: %s: buf was NULL", __func__);
215 sr_warn("la8: %s: size was < 0", __func__);
219 bytes_written = ftdi_write_data(la8->ftdic, buf, size);
221 if (bytes_written < 0) {
222 sr_warn("la8: %s: ftdi_write_data: (%d) %s", __func__,
223 bytes_written, ftdi_get_error_string(la8->ftdic));
224 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
225 } else if (bytes_written != size) {
226 sr_warn("la8: %s: bytes to write: %d, bytes written: %d",
227 __func__, size, bytes_written);
228 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
231 return bytes_written;
235 * Read a certain amount of bytes from the LA8's FTDI device.
237 * @param la8 The LA8 struct containing private per-device-instance data.
238 * @param buf The buffer where the received data will be stored.
239 * @param size The number of bytes to read.
240 * @return The number of bytes read, or a negative value upon errors.
242 static int la8_read(struct la8 *la8, uint8_t *buf, int size)
247 sr_warn("la8: %s: la8 was NULL", __func__);
252 sr_warn("la8: %s: la8->ftdic was NULL", __func__);
257 sr_warn("la8: %s: buf was NULL", __func__);
262 sr_warn("la8: %s: size was <= 0", __func__);
266 bytes_read = ftdi_read_data(la8->ftdic, buf, size);
268 if (bytes_read < 0) {
269 sr_warn("la8: %s: ftdi_read_data: (%d) %s", __func__,
270 bytes_read, ftdi_get_error_string(la8->ftdic));
271 } else if (bytes_read != size) {
272 // sr_warn("la8: %s: bytes to read: %d, bytes read: %d",
273 // __func__, size, bytes_read);
279 static int la8_close(struct la8 *la8)
284 sr_warn("la8: %s: la8 was NULL", __func__);
289 sr_warn("la8: %s: la8->ftdic was NULL", __func__);
293 if ((ret = ftdi_usb_close(la8->ftdic)) < 0) {
294 sr_warn("la8: %s: ftdi_usb_close: (%d) %s",
295 __func__, ret, ftdi_get_error_string(la8->ftdic));
302 * Close the ChronoVu LA8 USB port and reset the LA8 sequencer logic.
304 * @param la8 The LA8 struct containing private per-device-instance data.
305 * @return SR_OK upon success, SR_ERR upon failure.
307 static int la8_close_usb_reset_sequencer(struct la8 *la8)
309 /* Magic sequence of bytes for resetting the LA8 sequencer logic. */
310 uint8_t buf[8] = {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};
313 sr_dbg("la8: entering %s", __func__);
316 sr_warn("la8: %s: la8 was NULL", __func__);
321 sr_warn("la8: %s: la8->ftdic was NULL", __func__);
325 if (la8->ftdic->usb_dev) {
326 /* Reset the LA8 sequencer logic, then wait 100ms. */
327 sr_dbg("la8: resetting sequencer logic");
328 (void) la8_write(la8, buf, 8); /* Ignore errors. */
329 g_usleep(100 * 1000);
331 /* Purge FTDI buffers, then reset and close the FTDI device. */
332 sr_dbg("la8: purging buffers, resetting+closing FTDI device");
334 /* Log errors, but ignore them (i.e., don't abort). */
335 if ((ret = ftdi_usb_purge_buffers(la8->ftdic)) < 0)
336 sr_warn("la8: %s: ftdi_usb_purge_buffers: (%d) %s",
337 __func__, ret, ftdi_get_error_string(la8->ftdic));
338 if ((ret = ftdi_usb_reset(la8->ftdic)) < 0)
339 sr_warn("la8: %s: ftdi_usb_reset: (%d) %s", __func__,
340 ret, ftdi_get_error_string(la8->ftdic));
341 if ((ret = ftdi_usb_close(la8->ftdic)) < 0)
342 sr_warn("la8: %s: ftdi_usb_close: (%d) %s", __func__,
343 ret, ftdi_get_error_string(la8->ftdic));
345 sr_dbg("la8: %s: usb_dev was NULL, nothing to do", __func__);
348 ftdi_free(la8->ftdic); /* Returns void. */
355 * Reset the ChronoVu LA8.
357 * The LA8 must be reset after a failed read/write operation or upon timeouts.
359 * @param la8 The LA8 struct containing private per-device-instance data.
360 * @return SR_OK upon success, SR_ERR upon failure.
362 static int la8_reset(struct la8 *la8)
369 sr_warn("la8: %s: la8 was NULL", __func__);
374 sr_warn("la8: %s: la8->ftdic was NULL", __func__);
378 sr_dbg("la8: resetting the device");
381 * Purge pending read data from the FTDI hardware FIFO until
382 * no more data is left, or a timeout occurs (after 20s).
384 done = 20 + time(NULL);
386 /* TODO: Ignore errors? Check for < 0 at least! */
387 bytes_read = la8_read(la8, (uint8_t *)&buf, 4096);
389 } while ((done > now) && (bytes_read > 0));
391 /* Reset the LA8 sequencer logic and close the USB port. */
392 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
394 sr_dbg("la8: device reset finished");
399 static int hw_init(const char *deviceinfo)
402 struct sr_device_instance *sdi;
405 sr_dbg("la8: entering %s", __func__);
407 /* Avoid compiler errors. */
408 deviceinfo = deviceinfo;
410 /* Allocate memory for our private driver context. */
411 if (!(la8 = malloc(sizeof(struct la8)))) {
412 sr_warn("la8: %s: struct la8 malloc failed", __func__);
414 goto err_free_nothing;
417 /* Set some sane defaults. */
419 la8->cur_samplerate = SR_MHZ(100); /* 100MHz == max. samplerate */
421 la8->limit_samples = 0;
422 la8->num_probes = NUM_PROBES;
423 la8->session_id = NULL;
424 la8->mangled_buf = NULL;
425 la8->final_buf = NULL;
426 la8->trigger_pattern = 0x00; /* Value irrelevant, see trigger_mask. */
427 la8->trigger_mask = 0x00; /* All probes are "don't care". */
428 la8->trigger_timeout = 10; /* Default to 10s trigger timeout. */
430 la8->block_counter = 0;
431 la8->divcount = 0; /* 10ns sample period == 100MHz samplerate */
433 /* Allocate memory for the raw (mangled) data from the LA8. */
434 if (!(la8->mangled_buf = malloc(SDRAM_SIZE))) {
435 sr_warn("la8: %s: mangled_buf malloc failed", __func__);
440 /* Allocate memory where we'll store the de-mangled data. */
441 if (!(la8->final_buf = malloc(SDRAM_SIZE))) {
442 sr_warn("la8: %s: final_buf malloc failed", __func__);
444 goto err_free_mangled_buf;
447 /* Allocate memory for the FTDI context (ftdic) and initialize it. */
448 if (!(la8->ftdic = ftdi_new())) {
449 sr_warn("la8: %s: ftdi_new failed", __func__);
450 ret = SR_ERR; /* TODO: More specific error? */
451 goto err_free_final_buf;
454 /* Check for the device and temporarily open it. */
455 if ((ret = ftdi_usb_open_desc(la8->ftdic, USB_VENDOR_ID,
456 USB_PRODUCT_ID, USB_DESCRIPTION, NULL)) < 0) {
457 sr_warn("la8: %s: ftdi_usb_open_desc: (%d) %s",
458 __func__, ret, ftdi_get_error_string(la8->ftdic));
459 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
460 ret = SR_ERR; /* TODO: More specific error? */
463 sr_dbg("la8: found device");
465 /* Register the device with libsigrok. */
466 sdi = sr_device_instance_new(0, SR_ST_INITIALIZING,
467 USB_VENDOR_NAME, USB_MODEL_NAME, USB_MODEL_VERSION);
469 sr_warn("la8: %s: sr_device_instance_new failed", __func__);
470 ret = SR_ERR; /* TODO: More specific error? */
471 goto err_close_ftdic;
476 device_instances = g_slist_append(device_instances, sdi);
478 sr_dbg("la8: %s finished successfully", __func__);
480 /* Close device. We'll reopen it again when we need it. */
481 (void) la8_close(la8); /* Log, but ignore errors. */
483 // return SR_OK; /* TODO */
487 (void) la8_close(la8); /* Log, but ignore errors. */
491 free(la8->final_buf);
492 err_free_mangled_buf:
493 free(la8->mangled_buf);
497 // return ret; /* TODO */
501 static int hw_opendev(int device_index)
504 struct sr_device_instance *sdi;
507 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
508 sr_warn("la8: %s: sdi was NULL", __func__);
509 return SR_ERR; /* TODO: SR_ERR_ARG? */
512 if (!(la8 = sdi->priv)) {
513 sr_warn("la8: %s: sdi->priv was NULL", __func__);
514 return SR_ERR; /* TODO: SR_ERR_ARG? */
517 sr_dbg("la8: opening device");
519 /* Open the device. */
520 if ((ret = ftdi_usb_open_desc(la8->ftdic, USB_VENDOR_ID,
521 USB_PRODUCT_ID, USB_DESCRIPTION, NULL)) < 0) {
522 sr_warn("la8: %s: ftdi_usb_open_desc: (%d) %s",
523 __func__, ret, ftdi_get_error_string(la8->ftdic));
524 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
527 sr_dbg("la8: device opened successfully");
529 /* Purge RX/TX buffers in the FTDI chip. */
530 if ((ret = ftdi_usb_purge_buffers(la8->ftdic)) < 0) {
531 sr_warn("la8: %s: ftdi_usb_purge_buffers: (%d) %s",
532 __func__, ret, ftdi_get_error_string(la8->ftdic));
533 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
534 goto err_opendev_close_ftdic;
536 sr_dbg("la8: FTDI buffers purged successfully");
538 /* Enable flow control in the FTDI chip. */
539 if ((ret = ftdi_setflowctrl(la8->ftdic, SIO_RTS_CTS_HS)) < 0) {
540 sr_warn("la8: %s: ftdi_setflowcontrol: (%d) %s",
541 __func__, ret, ftdi_get_error_string(la8->ftdic));
542 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
543 goto err_opendev_close_ftdic;
545 sr_dbg("la8: FTDI flow control enabled successfully");
548 g_usleep(100 * 1000);
550 sdi->status = SR_ST_ACTIVE;
554 err_opendev_close_ftdic:
555 (void) la8_close(la8); /* Log, but ignore errors. */
559 static int set_samplerate(struct sr_device_instance *sdi, uint64_t samplerate)
564 sr_warn("la8: %s: sdi was NULL", __func__);
568 if (!(la8 = sdi->priv)) {
569 sr_warn("la8: %s: sdi->priv was NULL", __func__);
573 sr_dbg("la8: setting samplerate");
575 fill_supported_samplerates_if_needed();
577 /* Check if this is a samplerate supported by the hardware. */
578 if (!is_valid_samplerate(samplerate))
581 /* Set the new samplerate. */
582 la8->cur_samplerate = samplerate;
584 sr_dbg("la8: samplerate set to %" PRIu64 "Hz", la8->cur_samplerate);
589 static void hw_closedev(int device_index)
591 struct sr_device_instance *sdi;
594 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
595 sr_warn("la8: %s: sdi was NULL", __func__);
599 if (!(la8 = sdi->priv)) {
600 sr_warn("la8: %s: sdi->priv was NULL", __func__);
604 sr_dbg("la8: closing device");
606 if (sdi->status == SR_ST_ACTIVE) {
607 sr_dbg("la8: %s: status ACTIVE, closing device", __func__);
608 /* TODO: Handle or ignore errors here? */
609 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
611 sr_dbg("la8: %s: status not ACTIVE, nothing to do", __func__);
614 sdi->status = SR_ST_INACTIVE;
617 static void hw_cleanup(void)
620 struct sr_device_instance *sdi;
622 sr_dbg("la8: entering %s", __func__);
624 /* Properly close all devices. */
625 for (l = device_instances; l; l = l->next) {
626 if ((sdi = l->data) == NULL) {
627 sr_warn("la8: %s: sdi was NULL", __func__);
630 if (sdi->priv != NULL)
633 sr_warn("la8: %s: sdi->priv was NULL", __func__);
634 sr_device_instance_free(sdi); /* Returns void. */
636 g_slist_free(device_instances); /* Returns void. */
637 device_instances = NULL;
640 static void *hw_get_device_info(int device_index, int device_info_id)
642 struct sr_device_instance *sdi;
646 sr_dbg("la8: entering %s", __func__);
648 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
649 sr_warn("la8: %s: sdi was NULL", __func__);
653 if (!(la8 = sdi->priv)) {
654 sr_warn("la8: %s: sdi->priv was NULL", __func__);
658 switch (device_info_id) {
662 case SR_DI_NUM_PROBES:
663 info = GINT_TO_POINTER(NUM_PROBES);
665 case SR_DI_SAMPLERATES:
666 fill_supported_samplerates_if_needed();
669 case SR_DI_TRIGGER_TYPES:
670 info = (char *)TRIGGER_TYPES;
672 case SR_DI_CUR_SAMPLERATE:
673 info = &la8->cur_samplerate;
676 /* Unknown device info ID, return NULL. */
677 sr_warn("la8: %s: Unknown device info ID", __func__);
685 static int hw_get_status(int device_index)
687 struct sr_device_instance *sdi;
689 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
690 sr_warn("la8: %s: sdi was NULL, device not found", __func__);
691 return SR_ST_NOT_FOUND;
694 sr_dbg("la8: %s: returning status %d", __func__, sdi->status);
699 static int *hw_get_capabilities(void)
701 sr_dbg("la8: entering %s", __func__);
706 static int hw_set_configuration(int device_index, int capability, void *value)
708 struct sr_device_instance *sdi;
711 sr_dbg("la8: entering %s", __func__);
713 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
714 sr_warn("la8: %s: sdi was NULL", __func__);
715 return SR_ERR; /* TODO: SR_ERR_ARG? */
718 if (!(la8 = sdi->priv)) {
719 sr_warn("la8: %s: sdi->priv was NULL", __func__);
720 return SR_ERR; /* TODO: SR_ERR_ARG? */
723 switch (capability) {
724 case SR_HWCAP_SAMPLERATE:
725 if (set_samplerate(sdi, *(uint64_t *)value) == SR_ERR)
727 sr_dbg("la8: SAMPLERATE = %" PRIu64, la8->cur_samplerate);
729 case SR_HWCAP_PROBECONFIG:
730 /* Nothing to do, but this entry must exist. Fix this. */
732 sr_dbg("la8: %s: SR_HWCAP_PROBECONFIG called", __func__);
735 case SR_HWCAP_LIMIT_MSEC:
736 if (*(uint64_t *)value == 0) {
737 sr_warn("la8: %s: LIMIT_MSEC can't be 0", __func__);
740 la8->limit_msec = *(uint64_t *)value;
741 sr_dbg("la8: LIMIT_MSEC = %" PRIu64, la8->limit_msec);
743 case SR_HWCAP_LIMIT_SAMPLES:
744 if (*(uint64_t *)value < MIN_NUM_SAMPLES) {
745 sr_warn("la8: %s: LIMIT_SAMPLES too small", __func__);
748 la8->limit_samples = *(uint64_t *)value;
749 sr_dbg("la8: LIMIT_SAMPLES = %" PRIu64, la8->limit_samples);
752 /* Unknown capability, return SR_ERR. */
753 sr_warn("la8: %s: Unknown capability", __func__);
762 * Get a block of 4096 bytes of data from the LA8.
764 * @param la8 The LA8 struct containing private per-device-instance data.
765 * @return SR_OK upon success, or SR_ERR upon errors.
767 static int la8_read_block(struct la8 *la8)
769 int i, byte_offset, m, mi, p, index, bytes_read;
773 sr_warn("la8: %s: la8 was NULL", __func__);
778 sr_warn("la8: %s: la8->ftdic was NULL", __func__);
782 // sr_dbg("la8: %s: reading block %d", __func__, la8->block_counter);
784 bytes_read = la8_read(la8, la8->mangled_buf, 4096);
786 /* If first block read got 0 bytes, retry until success or timeout. */
787 if ((bytes_read == 0) && (la8->block_counter == 0)) {
789 // sr_dbg("la8: %s: reading block 0 again", __func__);
790 bytes_read = la8_read(la8, la8->mangled_buf, 4096);
791 /* TODO: How to handle read errors here? */
793 } while ((la8->done > now) && (bytes_read == 0));
796 /* Check if block read was successful or a timeout occured. */
797 if (bytes_read != 4096) {
798 sr_warn("la8: %s: trigger timed out", __func__);
799 (void) la8_reset(la8); /* Ignore errors. */
803 /* De-mangle the data. */
804 // sr_dbg("la8: de-mangling samples of block %d", la8->block_counter);
805 byte_offset = la8->block_counter * 4096;
806 m = byte_offset / (1024 * 1024);
807 mi = m * (1024 * 1024);
808 for (i = 0; i < 4096; i++) {
810 index = m * 2 + (((byte_offset + i) - mi) / 2) * 16;
811 index += (la8->divcount == 0) ? p : (1 - p);
812 la8->final_buf[index] = la8->mangled_buf[i];
818 static int receive_data(int fd, int revents, void *user_data)
821 struct sr_device_instance *sdi;
822 struct sr_datafeed_packet packet;
825 /* Avoid compiler errors. */
829 if (!(sdi = user_data)) {
830 sr_warn("la8: %s: user_data was NULL", __func__);
834 if (!(la8 = sdi->priv)) {
835 sr_warn("la8: %s: sdi->priv was NULL", __func__);
839 /* Get one block of data (4096 bytes). */
840 if ((ret = la8_read_block(la8)) < 0) {
841 sr_warn("la8: %s: la8_read_block error: %d", __func__, ret);
845 /* We need to get exactly 2048 blocks (i.e. 8MB) of data. */
846 if (la8->block_counter != 2047) {
847 la8->block_counter++;
851 sr_dbg("la8: sampling finished, sending data to session bus now");
853 /* All data was received and demangled, send it to the session bus. */
854 for (i = 0; i < 2048; i++) {
855 /* Send a 4096 byte SR_DF_LOGIC packet to the session bus. */
856 // sr_dbg("la8: %s: sending SR_DF_LOGIC packet", __func__);
857 packet.type = SR_DF_LOGIC;
858 packet.length = 4096;
860 packet.payload = la8->final_buf + (i * 4096);
861 sr_session_bus(la8->session_id, &packet);
864 hw_stop_acquisition(sdi->index, user_data);
866 // return FALSE; /* FIXME? */
870 static int hw_start_acquisition(int device_index, gpointer session_device_id)
872 struct sr_device_instance *sdi;
874 struct sr_datafeed_packet packet;
875 struct sr_datafeed_header header;
879 sr_dbg("la8: entering %s", __func__);
881 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
882 sr_warn("la8: %s: sdi was NULL", __func__);
883 return SR_ERR; /* TODO: SR_ERR_ARG? */
886 if (!(la8 = sdi->priv)) {
887 sr_warn("la8: %s: sdi->priv was NULL", __func__);
888 return SR_ERR; /* TODO: SR_ERR_ARG? */
892 sr_warn("la8: %s: la8->ftdic was NULL", __func__);
896 la8->divcount = samplerate_to_divcount(la8->cur_samplerate);
897 if (la8->divcount == 0xff) {
898 sr_warn("la8: %s: invalid divcount/samplerate", __func__);
902 /* Fill acquisition parameters into buf[]. */
903 buf[0] = la8->divcount;
904 buf[1] = 0xff; /* This byte must always be 0xff. */
905 buf[2] = la8->trigger_pattern;
906 buf[3] = la8->trigger_mask;
908 /* Start acquisition. */
909 bytes_written = la8_write(la8, buf, 4);
911 if (bytes_written < 0) {
912 sr_warn("la8: acquisition failed to start");
914 } else if (bytes_written != 4) {
915 sr_warn("la8: acquisition failed to start");
916 return SR_ERR; /* TODO: Other error and return code? */
919 sr_dbg("la8: acquisition started successfully");
921 la8->session_id = session_device_id;
923 /* Send header packet to the session bus. */
924 sr_dbg("la8: %s: sending SR_DF_HEADER", __func__);
925 packet.type = SR_DF_HEADER;
926 packet.length = sizeof(struct sr_datafeed_header);
928 packet.payload = &header;
929 header.feed_version = 1;
930 gettimeofday(&header.starttime, NULL);
931 header.samplerate = la8->cur_samplerate;
932 header.protocol_id = SR_PROTO_RAW;
933 header.num_logic_probes = la8->num_probes;
934 header.num_analog_probes = 0;
935 sr_session_bus(session_device_id, &packet);
937 /* Time when we should be done (for detecting trigger timeouts). */
938 la8->done = (la8->divcount + 1) * 0.08388608 + time(NULL)
939 + la8->trigger_timeout;
940 la8->block_counter = 0;
942 /* Hook up a dummy handler to receive data from the LA8. */
943 sr_source_add(-1, G_IO_IN, 0, receive_data, sdi);
948 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
950 struct sr_device_instance *sdi;
952 struct sr_datafeed_packet packet;
954 sr_dbg("la8: stopping acquisition");
956 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
957 sr_warn("la8: %s: sdi was NULL", __func__);
961 if (!(la8 = sdi->priv)) {
962 sr_warn("la8: %s: sdi->priv was NULL", __func__);
966 /* Reset the LA8 sequencer and close the device. */
967 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
969 /* Send end packet to the session bus. */
970 sr_dbg("la8: %s: sending SR_DF_END", __func__);
971 packet.type = SR_DF_END;
974 packet.payload = NULL;
975 sr_session_bus(session_device_id, &packet);
978 struct sr_device_plugin chronovu_la8_plugin_info = {
979 .name = "chronovu-la8",
980 .longname = "ChronoVu LA8",
983 .cleanup = hw_cleanup,
985 .close = hw_closedev,
986 .get_device_info = hw_get_device_info,
987 .get_status = hw_get_status,
988 .get_capabilities = hw_get_capabilities,
989 .set_configuration = hw_set_configuration,
990 .start_acquisition = hw_start_acquisition,
991 .stop_acquisition = hw_stop_acquisition,