2 * This file is part of the sigrok project.
4 * Copyright (C) 2011 Uwe Hermann <uwe@hermann-uwe.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <sigrok-internal.h>
27 #define USB_VENDOR_ID 0x0403
28 #define USB_PRODUCT_ID 0x6001
29 #define USB_DESCRIPTION "ChronoVu LA8"
30 #define USB_VENDOR_NAME "ChronoVu"
31 #define USB_MODEL_NAME "LA8"
32 #define USB_MODEL_VERSION ""
35 #define TRIGGER_TYPES "01"
36 #define SDRAM_SIZE (8 * 1024 * 1024)
37 #define MIN_NUM_SAMPLES 1
39 #define BS 4096 /* Block size */
40 #define NUM_BLOCKS 2048 /* Number of blocks */
42 static GSList *device_instances = NULL;
45 /** FTDI device context (used by libftdi). */
46 struct ftdi_context *ftdic;
48 /** The currently configured samplerate of the device. */
49 uint64_t cur_samplerate;
51 /** The current sampling limit (in ms). */
54 /** The current sampling limit (in number of samples). */
55 uint64_t limit_samples;
61 * A buffer containing some (mangled) samples from the device.
62 * Format: Pretty mangled-up (due to hardware reasons), see code.
64 uint8_t mangled_buf[BS];
67 * An 8MB buffer where we'll store the de-mangled samples.
68 * Format: Each sample is 1 byte, MSB is channel 7, LSB is channel 0.
73 * Trigger pattern (MSB = channel 7, LSB = channel 0).
74 * A 1 bit matches a high signal, 0 matches a low signal on a probe.
75 * Only low/high triggers (but not e.g. rising/falling) are supported.
77 uint8_t trigger_pattern;
80 * Trigger mask (MSB = channel 7, LSB = channel 0).
81 * A 1 bit means "must match trigger_pattern", 0 means "don't care".
85 /** Time (in seconds) before the trigger times out. */
86 uint64_t trigger_timeout;
88 /** Tells us whether an SR_DF_TRIGGER packet was already sent. */
94 /** Counter/index for the data block to be read. */
97 /** The divcount value (determines the sample period) for the LA8. */
101 /* This will be initialized via hw_get_device_info()/SR_DI_SAMPLERATES. */
102 static uint64_t supported_samplerates[255 + 1] = { 0 };
105 * Min: 1 sample per 0.01us -> sample time is 0.084s, samplerate 100MHz
106 * Max: 1 sample per 2.55us -> sample time is 21.391s, samplerate 392.15kHz
108 static struct sr_samplerates samplerates = {
112 .list = supported_samplerates,
115 /* Note: Continuous sampling is not supported by the hardware. */
116 static int capabilities[] = {
117 SR_HWCAP_LOGIC_ANALYZER,
119 SR_HWCAP_LIMIT_MSEC, /* TODO: Not yet implemented. */
120 SR_HWCAP_LIMIT_SAMPLES, /* TODO: Not yet implemented. */
124 /* Function prototypes. */
125 static int la8_close_usb_reset_sequencer(struct la8 *la8);
126 static void hw_stop_acquisition(int device_index, gpointer session_device_id);
127 static int la8_reset(struct la8 *la8);
129 static void fill_supported_samplerates_if_needed(void)
133 /* Do nothing if supported_samplerates[] is already filled. */
134 if (supported_samplerates[0] != 0)
137 /* Fill supported_samplerates[] with the proper values. */
138 for (i = 0; i < 255; i++)
139 supported_samplerates[254 - i] = SR_MHZ(100) / (i + 1);
140 supported_samplerates[255] = 0;
144 * Check if the given samplerate is supported by the LA8 hardware.
146 * @param samplerate The samplerate (in Hz) to check.
147 * @return 1 if the samplerate is supported/valid, 0 otherwise.
149 static int is_valid_samplerate(uint64_t samplerate)
153 fill_supported_samplerates_if_needed();
155 for (i = 0; i < 255; i++) {
156 if (supported_samplerates[i] == samplerate)
160 sr_warn("la8: %s: invalid samplerate (%" PRIu64 "Hz)",
161 __func__, samplerate);
167 * Convert a samplerate (in Hz) to the 'divcount' value the LA8 wants.
169 * LA8 hardware: sample period = (divcount + 1) * 10ns.
170 * Min. value for divcount: 0x00 (10ns sample period, 100MHz samplerate).
171 * Max. value for divcount: 0xfe (2550ns sample period, 392.15kHz samplerate).
173 * @param samplerate The samplerate in Hz.
174 * @return The divcount value as needed by the hardware, or 0xff upon errors.
176 static uint8_t samplerate_to_divcount(uint64_t samplerate)
178 if (samplerate == 0) {
179 sr_err("la8: %s: samplerate was 0", __func__);
183 if (!is_valid_samplerate(samplerate)) {
184 sr_err("la8: %s: can't get divcount, samplerate invalid",
189 return (SR_MHZ(100) / samplerate) - 1;
193 * Write data of a certain length to the LA8's FTDI device.
195 * @param la8 The LA8 struct containing private per-device-instance data.
196 * @param buf The buffer containing the data to write.
197 * @param size The number of bytes to write.
198 * @return The number of bytes written, or a negative value upon errors.
200 static int la8_write(struct la8 *la8, uint8_t *buf, int size)
205 sr_err("la8: %s: la8 was NULL", __func__);
210 sr_err("la8: %s: la8->ftdic was NULL", __func__);
215 sr_err("la8: %s: buf was NULL", __func__);
220 sr_err("la8: %s: size was < 0", __func__);
224 bytes_written = ftdi_write_data(la8->ftdic, buf, size);
226 if (bytes_written < 0) {
227 sr_warn("la8: %s: ftdi_write_data: (%d) %s", __func__,
228 bytes_written, ftdi_get_error_string(la8->ftdic));
229 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
230 } else if (bytes_written != size) {
231 sr_warn("la8: %s: bytes to write: %d, bytes written: %d",
232 __func__, size, bytes_written);
233 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
236 return bytes_written;
240 * Read a certain amount of bytes from the LA8's FTDI device.
242 * @param la8 The LA8 struct containing private per-device-instance data.
243 * @param buf The buffer where the received data will be stored.
244 * @param size The number of bytes to read.
245 * @return The number of bytes read, or a negative value upon errors.
247 static int la8_read(struct la8 *la8, uint8_t *buf, int size)
252 sr_err("la8: %s: la8 was NULL", __func__);
257 sr_err("la8: %s: la8->ftdic was NULL", __func__);
262 sr_err("la8: %s: buf was NULL", __func__);
267 sr_err("la8: %s: size was <= 0", __func__);
271 bytes_read = ftdi_read_data(la8->ftdic, buf, size);
273 if (bytes_read < 0) {
274 sr_warn("la8: %s: ftdi_read_data: (%d) %s", __func__,
275 bytes_read, ftdi_get_error_string(la8->ftdic));
276 } else if (bytes_read != size) {
277 // sr_warn("la8: %s: bytes to read: %d, bytes read: %d",
278 // __func__, size, bytes_read);
284 static int la8_close(struct la8 *la8)
289 sr_err("la8: %s: la8 was NULL", __func__);
294 sr_err("la8: %s: la8->ftdic was NULL", __func__);
298 if ((ret = ftdi_usb_close(la8->ftdic)) < 0) {
299 sr_warn("la8: %s: ftdi_usb_close: (%d) %s",
300 __func__, ret, ftdi_get_error_string(la8->ftdic));
307 * Close the ChronoVu LA8 USB port and reset the LA8 sequencer logic.
309 * @param la8 The LA8 struct containing private per-device-instance data.
310 * @return SR_OK upon success, SR_ERR upon failure.
312 static int la8_close_usb_reset_sequencer(struct la8 *la8)
314 /* Magic sequence of bytes for resetting the LA8 sequencer logic. */
315 uint8_t buf[8] = {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};
318 sr_spew("la8: entering %s", __func__);
321 sr_err("la8: %s: la8 was NULL", __func__);
326 sr_err("la8: %s: la8->ftdic was NULL", __func__);
330 if (la8->ftdic->usb_dev) {
331 /* Reset the LA8 sequencer logic, then wait 100ms. */
332 sr_dbg("la8: resetting sequencer logic");
333 (void) la8_write(la8, buf, 8); /* Ignore errors. */
334 g_usleep(100 * 1000);
336 /* Purge FTDI buffers, then reset and close the FTDI device. */
337 sr_dbg("la8: purging buffers, resetting+closing FTDI device");
339 /* Log errors, but ignore them (i.e., don't abort). */
340 if ((ret = ftdi_usb_purge_buffers(la8->ftdic)) < 0)
341 sr_warn("la8: %s: ftdi_usb_purge_buffers: (%d) %s",
342 __func__, ret, ftdi_get_error_string(la8->ftdic));
343 if ((ret = ftdi_usb_reset(la8->ftdic)) < 0)
344 sr_warn("la8: %s: ftdi_usb_reset: (%d) %s", __func__,
345 ret, ftdi_get_error_string(la8->ftdic));
346 if ((ret = ftdi_usb_close(la8->ftdic)) < 0)
347 sr_warn("la8: %s: ftdi_usb_close: (%d) %s", __func__,
348 ret, ftdi_get_error_string(la8->ftdic));
350 sr_spew("la8: %s: usb_dev was NULL, nothing to do", __func__);
353 ftdi_free(la8->ftdic); /* Returns void. */
360 * Reset the ChronoVu LA8.
362 * The LA8 must be reset after a failed read/write operation or upon timeouts.
364 * @param la8 The LA8 struct containing private per-device-instance data.
365 * @return SR_OK upon success, SR_ERR upon failure.
367 static int la8_reset(struct la8 *la8)
374 sr_err("la8: %s: la8 was NULL", __func__);
379 sr_err("la8: %s: la8->ftdic was NULL", __func__);
383 sr_dbg("la8: resetting the device");
386 * Purge pending read data from the FTDI hardware FIFO until
387 * no more data is left, or a timeout occurs (after 20s).
389 done = 20 + time(NULL);
391 /* TODO: Ignore errors? Check for < 0 at least! */
392 bytes_read = la8_read(la8, (uint8_t *)&buf, BS);
394 } while ((done > now) && (bytes_read > 0));
396 /* Reset the LA8 sequencer logic and close the USB port. */
397 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
399 sr_dbg("la8: device reset finished");
404 static int configure_probes(struct la8 *la8, GSList *probes)
406 struct sr_probe *probe;
411 la8->trigger_pattern = 0;
412 la8->trigger_mask = 0; /* Default to "don't care" for all probes. */
414 for (l = probes; l; l = l->next) {
415 probe = (struct sr_probe *)l->data;
418 sr_err("la8: %s: probe was NULL", __func__);
422 /* Skip disabled probes. */
426 /* Skip (enabled) probes with no configured trigger. */
430 /* Note: Must only be run if probe->trigger != NULL. */
431 if (probe->index < 0 || probe->index > 7) {
432 sr_err("la8: %s: invalid probe index %d, must be "
433 "between 0 and 7", __func__, probe->index);
437 probe_bit = (1 << (probe->index - 1));
439 /* Configure the probe's trigger mask and trigger pattern. */
440 for (tc = probe->trigger; tc && *tc; tc++) {
441 la8->trigger_mask |= probe_bit;
443 /* Sanity check, LA8 only supports low/high trigger. */
444 if (*tc != '0' && *tc != '1') {
445 sr_err("la8: %s: invalid trigger '%c', only "
446 "'0'/'1' supported", __func__, *tc);
451 la8->trigger_pattern |= probe_bit;
455 sr_dbg("la8: %s: trigger_mask = 0x%x, trigger_pattern = 0x%x",
456 __func__, la8->trigger_mask, la8->trigger_pattern);
461 static int hw_init(const char *deviceinfo)
464 struct sr_device_instance *sdi;
467 sr_spew("la8: entering %s", __func__);
469 /* Avoid compiler errors. */
470 deviceinfo = deviceinfo;
472 /* Allocate memory for our private driver context. */
473 if (!(la8 = g_try_malloc(sizeof(struct la8)))) {
474 sr_err("la8: %s: struct la8 malloc failed", __func__);
476 goto err_free_nothing;
479 /* Set some sane defaults. */
481 la8->cur_samplerate = SR_MHZ(100); /* 100MHz == max. samplerate */
483 la8->limit_samples = 0;
484 la8->session_id = NULL;
485 memset(la8->mangled_buf, 0, BS);
486 la8->final_buf = NULL;
487 la8->trigger_pattern = 0x00; /* Value irrelevant, see trigger_mask. */
488 la8->trigger_mask = 0x00; /* All probes are "don't care". */
489 la8->trigger_timeout = 10; /* Default to 10s trigger timeout. */
490 la8->trigger_found = 0;
492 la8->block_counter = 0;
493 la8->divcount = 0; /* 10ns sample period == 100MHz samplerate */
495 /* Allocate memory where we'll store the de-mangled data. */
496 if (!(la8->final_buf = g_try_malloc(SDRAM_SIZE))) {
497 sr_err("la8: %s: final_buf malloc failed", __func__);
502 /* Allocate memory for the FTDI context (ftdic) and initialize it. */
503 if (!(la8->ftdic = ftdi_new())) {
504 sr_err("la8: %s: ftdi_new failed", __func__);
505 ret = SR_ERR; /* TODO: More specific error? */
506 goto err_free_final_buf;
509 /* Check for the device and temporarily open it. */
510 if ((ret = ftdi_usb_open_desc(la8->ftdic, USB_VENDOR_ID,
511 USB_PRODUCT_ID, USB_DESCRIPTION, NULL)) < 0) {
512 sr_err("la8: %s: ftdi_usb_open_desc: (%d) %s",
513 __func__, ret, ftdi_get_error_string(la8->ftdic));
514 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
515 ret = SR_ERR; /* TODO: More specific error? */
518 sr_dbg("la8: found device");
520 /* Register the device with libsigrok. */
521 sdi = sr_device_instance_new(0, SR_ST_INITIALIZING,
522 USB_VENDOR_NAME, USB_MODEL_NAME, USB_MODEL_VERSION);
524 sr_err("la8: %s: sr_device_instance_new failed", __func__);
525 ret = SR_ERR; /* TODO: More specific error? */
526 goto err_close_ftdic;
531 device_instances = g_slist_append(device_instances, sdi);
533 sr_spew("la8: %s finished successfully", __func__);
535 /* Close device. We'll reopen it again when we need it. */
536 (void) la8_close(la8); /* Log, but ignore errors. */
538 // return SR_OK; /* TODO */
542 (void) la8_close(la8); /* Log, but ignore errors. */
544 free(la8->ftdic); /* NOT g_free()! */
546 g_free(la8->final_buf);
550 // return ret; /* TODO */
554 static int hw_opendev(int device_index)
557 struct sr_device_instance *sdi;
560 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
561 sr_err("la8: %s: sdi was NULL", __func__);
562 return SR_ERR; /* TODO: SR_ERR_ARG? */
565 if (!(la8 = sdi->priv)) {
566 sr_err("la8: %s: sdi->priv was NULL", __func__);
567 return SR_ERR; /* TODO: SR_ERR_ARG? */
570 sr_dbg("la8: opening device");
572 /* Open the device. */
573 if ((ret = ftdi_usb_open_desc(la8->ftdic, USB_VENDOR_ID,
574 USB_PRODUCT_ID, USB_DESCRIPTION, NULL)) < 0) {
575 sr_err("la8: %s: ftdi_usb_open_desc: (%d) %s",
576 __func__, ret, ftdi_get_error_string(la8->ftdic));
577 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
580 sr_dbg("la8: device opened successfully");
582 /* Purge RX/TX buffers in the FTDI chip. */
583 if ((ret = ftdi_usb_purge_buffers(la8->ftdic)) < 0) {
584 sr_err("la8: %s: ftdi_usb_purge_buffers: (%d) %s",
585 __func__, ret, ftdi_get_error_string(la8->ftdic));
586 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
587 goto err_opendev_close_ftdic;
589 sr_dbg("la8: FTDI buffers purged successfully");
591 /* Enable flow control in the FTDI chip. */
592 if ((ret = ftdi_setflowctrl(la8->ftdic, SIO_RTS_CTS_HS)) < 0) {
593 sr_err("la8: %s: ftdi_setflowcontrol: (%d) %s",
594 __func__, ret, ftdi_get_error_string(la8->ftdic));
595 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
596 goto err_opendev_close_ftdic;
598 sr_dbg("la8: FTDI flow control enabled successfully");
601 g_usleep(100 * 1000);
603 sdi->status = SR_ST_ACTIVE;
607 err_opendev_close_ftdic:
608 (void) la8_close(la8); /* Log, but ignore errors. */
612 static int set_samplerate(struct sr_device_instance *sdi, uint64_t samplerate)
617 sr_err("la8: %s: sdi was NULL", __func__);
621 if (!(la8 = sdi->priv)) {
622 sr_err("la8: %s: sdi->priv was NULL", __func__);
626 sr_spew("la8: setting samplerate");
628 fill_supported_samplerates_if_needed();
630 /* Check if this is a samplerate supported by the hardware. */
631 if (!is_valid_samplerate(samplerate))
634 /* Set the new samplerate. */
635 la8->cur_samplerate = samplerate;
637 sr_dbg("la8: samplerate set to %" PRIu64 "Hz", la8->cur_samplerate);
642 static int hw_closedev(int device_index)
644 struct sr_device_instance *sdi;
647 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
648 sr_err("la8: %s: sdi was NULL", __func__);
649 return SR_ERR; /* TODO: SR_ERR_ARG? */
652 if (!(la8 = sdi->priv)) {
653 sr_err("la8: %s: sdi->priv was NULL", __func__);
654 return SR_ERR; /* TODO: SR_ERR_ARG? */
657 sr_dbg("la8: closing device");
659 if (sdi->status == SR_ST_ACTIVE) {
660 sr_dbg("la8: %s: status ACTIVE, closing device", __func__);
661 /* TODO: Really ignore errors here, or return SR_ERR? */
662 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
664 sr_spew("la8: %s: status not ACTIVE, nothing to do", __func__);
667 sdi->status = SR_ST_INACTIVE;
669 sr_dbg("la8: %s: freeing sample buffers", __func__);
670 g_free(la8->final_buf);
675 static void hw_cleanup(void)
678 struct sr_device_instance *sdi;
680 sr_spew("la8: entering %s", __func__);
682 /* Properly close all devices. */
683 for (l = device_instances; l; l = l->next) {
684 if ((sdi = l->data) == NULL) {
685 sr_warn("la8: %s: sdi was NULL, continuing", __func__);
688 if (sdi->priv != NULL)
691 sr_warn("la8: %s: sdi->priv was NULL, nothing "
693 sr_device_instance_free(sdi); /* Returns void. */
695 g_slist_free(device_instances); /* Returns void. */
696 device_instances = NULL;
699 static void *hw_get_device_info(int device_index, int device_info_id)
701 struct sr_device_instance *sdi;
705 sr_spew("la8: entering %s", __func__);
707 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
708 sr_err("la8: %s: sdi was NULL", __func__);
712 if (!(la8 = sdi->priv)) {
713 sr_err("la8: %s: sdi->priv was NULL", __func__);
717 switch (device_info_id) {
721 case SR_DI_NUM_PROBES:
722 info = GINT_TO_POINTER(NUM_PROBES);
724 case SR_DI_SAMPLERATES:
725 fill_supported_samplerates_if_needed();
728 case SR_DI_TRIGGER_TYPES:
729 info = (char *)TRIGGER_TYPES;
731 case SR_DI_CUR_SAMPLERATE:
732 info = &la8->cur_samplerate;
735 /* Unknown device info ID, return NULL. */
736 sr_err("la8: %s: Unknown device info ID", __func__);
744 static int hw_get_status(int device_index)
746 struct sr_device_instance *sdi;
748 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
749 sr_warn("la8: %s: sdi was NULL, device not found", __func__);
750 return SR_ST_NOT_FOUND;
753 sr_dbg("la8: %s: returning status %d", __func__, sdi->status);
758 static int *hw_get_capabilities(void)
760 sr_spew("la8: entering %s", __func__);
765 static int hw_set_configuration(int device_index, int capability, void *value)
767 struct sr_device_instance *sdi;
770 sr_spew("la8: entering %s", __func__);
772 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
773 sr_err("la8: %s: sdi was NULL", __func__);
774 return SR_ERR; /* TODO: SR_ERR_ARG? */
777 if (!(la8 = sdi->priv)) {
778 sr_err("la8: %s: sdi->priv was NULL", __func__);
779 return SR_ERR; /* TODO: SR_ERR_ARG? */
782 switch (capability) {
783 case SR_HWCAP_SAMPLERATE:
784 if (set_samplerate(sdi, *(uint64_t *)value) == SR_ERR)
786 sr_dbg("la8: SAMPLERATE = %" PRIu64, la8->cur_samplerate);
788 case SR_HWCAP_PROBECONFIG:
789 if (configure_probes(la8, (GSList *)value) != SR_OK) {
790 sr_err("la8: %s: probe config failed", __func__);
794 case SR_HWCAP_LIMIT_MSEC:
795 if (*(uint64_t *)value == 0) {
796 sr_err("la8: %s: LIMIT_MSEC can't be 0", __func__);
799 la8->limit_msec = *(uint64_t *)value;
800 sr_dbg("la8: LIMIT_MSEC = %" PRIu64, la8->limit_msec);
802 case SR_HWCAP_LIMIT_SAMPLES:
803 if (*(uint64_t *)value < MIN_NUM_SAMPLES) {
804 sr_err("la8: %s: LIMIT_SAMPLES too small", __func__);
807 la8->limit_samples = *(uint64_t *)value;
808 sr_dbg("la8: LIMIT_SAMPLES = %" PRIu64, la8->limit_samples);
811 /* Unknown capability, return SR_ERR. */
812 sr_err("la8: %s: Unknown capability", __func__);
821 * Get a block of data from the LA8.
823 * @param la8 The LA8 struct containing private per-device-instance data.
824 * @return SR_OK upon success, or SR_ERR upon errors.
826 static int la8_read_block(struct la8 *la8)
828 int i, byte_offset, m, mi, p, index, bytes_read;
832 sr_err("la8: %s: la8 was NULL", __func__);
837 sr_err("la8: %s: la8->ftdic was NULL", __func__);
841 sr_spew("la8: %s: reading block %d", __func__, la8->block_counter);
843 bytes_read = la8_read(la8, la8->mangled_buf, BS);
845 /* If first block read got 0 bytes, retry until success or timeout. */
846 if ((bytes_read == 0) && (la8->block_counter == 0)) {
848 sr_spew("la8: %s: reading block 0 again", __func__);
849 bytes_read = la8_read(la8, la8->mangled_buf, BS);
850 /* TODO: How to handle read errors here? */
852 } while ((la8->done > now) && (bytes_read == 0));
855 /* Check if block read was successful or a timeout occured. */
856 if (bytes_read != BS) {
857 sr_warn("la8: %s: trigger timed out", __func__);
858 (void) la8_reset(la8); /* Ignore errors. */
862 /* De-mangle the data. */
863 sr_spew("la8: de-mangling samples of block %d", la8->block_counter);
864 byte_offset = la8->block_counter * BS;
865 m = byte_offset / (1024 * 1024);
866 mi = m * (1024 * 1024);
867 for (i = 0; i < BS; i++) {
869 index = m * 2 + (((byte_offset + i) - mi) / 2) * 16;
870 index += (la8->divcount == 0) ? p : (1 - p);
871 la8->final_buf[index] = la8->mangled_buf[i];
877 static void send_block_to_session_bus(struct la8 *la8, int block)
880 uint8_t sample, expected_sample;
881 struct sr_datafeed_packet packet;
882 int trigger_point; /* Relative trigger point (in this block). */
884 /* Note: No sanity checks on la8/block, caller is responsible. */
886 /* Check if we can find the trigger condition in this block. */
888 expected_sample = la8->trigger_pattern & la8->trigger_mask;
889 for (i = 0; i < BS; i++) {
890 /* Don't continue if the trigger was found previously. */
891 if (la8->trigger_found)
895 * Also, don't continue if triggers are "don't care", i.e. if
896 * no trigger conditions were specified by the user. In that
897 * case we don't want to send an SR_DF_TRIGGER packet at all.
899 if (la8->trigger_mask == 0x00)
902 sample = *(la8->final_buf + (block * BS) + i);
904 if ((sample & la8->trigger_mask) == expected_sample) {
906 la8->trigger_found = 1;
911 /* If no trigger was found, send one SR_DF_LOGIC packet. */
912 if (trigger_point == -1) {
913 /* Send an SR_DF_LOGIC packet to the session bus. */
914 sr_spew("la8: sending SR_DF_LOGIC packet (%d bytes) for "
915 "block %d", BS, block);
916 packet.type = SR_DF_LOGIC;
919 packet.payload = la8->final_buf + (block * BS);
920 sr_session_bus(la8->session_id, &packet);
925 * We found the trigger, so some special handling is needed. We have
926 * to send an SR_DF_LOGIC packet with the samples before the trigger
927 * (if any), then the SD_DF_TRIGGER packet itself, then another
928 * SR_DF_LOGIC packet with the samples after the trigger (if any).
931 /* TODO: Send SR_DF_TRIGGER packet before or after the actual sample? */
933 /* If at least one sample is located before the trigger... */
934 if (trigger_point > 0) {
935 /* Send pre-trigger SR_DF_LOGIC packet to the session bus. */
936 sr_spew("la8: sending pre-trigger SR_DF_LOGIC packet, "
937 "start = %d, length = %d", block * BS, trigger_point);
938 packet.type = SR_DF_LOGIC;
939 packet.length = trigger_point;
941 packet.payload = la8->final_buf + (block * BS);
942 sr_session_bus(la8->session_id, &packet);
945 /* Send the SR_DF_TRIGGER packet to the session bus. */
946 sr_spew("la8: sending SR_DF_TRIGGER packet, sample = %d",
947 (block * BS) + trigger_point);
948 packet.type = SR_DF_TRIGGER;
951 packet.payload = NULL;
952 sr_session_bus(la8->session_id, &packet);
954 /* If at least one sample is located after the trigger... */
955 if (trigger_point < (BS - 1)) {
956 /* Send post-trigger SR_DF_LOGIC packet to the session bus. */
957 sr_spew("la8: sending post-trigger SR_DF_LOGIC packet, "
958 "start = %d, length = %d",
959 (block * BS) + trigger_point, BS - trigger_point);
960 packet.type = SR_DF_LOGIC;
961 packet.length = BS - trigger_point;
963 packet.payload = la8->final_buf + (block * BS) + trigger_point;
964 sr_session_bus(la8->session_id, &packet);
968 static int receive_data(int fd, int revents, void *user_data)
971 struct sr_device_instance *sdi;
974 /* Avoid compiler errors. */
978 if (!(sdi = user_data)) {
979 sr_err("la8: %s: user_data was NULL", __func__);
983 if (!(la8 = sdi->priv)) {
984 sr_err("la8: %s: sdi->priv was NULL", __func__);
988 /* Get one block of data. */
989 if ((ret = la8_read_block(la8)) < 0) {
990 sr_err("la8: %s: la8_read_block error: %d", __func__, ret);
991 hw_stop_acquisition(sdi->index, user_data);
995 /* We need to get exactly NUM_BLOCKS blocks (i.e. 8MB) of data. */
996 if (la8->block_counter != (NUM_BLOCKS - 1)) {
997 la8->block_counter++;
1001 sr_dbg("la8: sampling finished, sending data to session bus now");
1003 /* All data was received and demangled, send it to the session bus. */
1004 for (i = 0; i < NUM_BLOCKS; i++)
1005 send_block_to_session_bus(la8, i);
1007 hw_stop_acquisition(sdi->index, user_data);
1009 // return FALSE; /* FIXME? */
1013 static int hw_start_acquisition(int device_index, gpointer session_device_id)
1015 struct sr_device_instance *sdi;
1017 struct sr_datafeed_packet packet;
1018 struct sr_datafeed_header header;
1022 sr_spew("la8: entering %s", __func__);
1024 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
1025 sr_err("la8: %s: sdi was NULL", __func__);
1026 return SR_ERR; /* TODO: SR_ERR_ARG? */
1029 if (!(la8 = sdi->priv)) {
1030 sr_err("la8: %s: sdi->priv was NULL", __func__);
1031 return SR_ERR; /* TODO: SR_ERR_ARG? */
1035 sr_err("la8: %s: la8->ftdic was NULL", __func__);
1039 la8->divcount = samplerate_to_divcount(la8->cur_samplerate);
1040 if (la8->divcount == 0xff) {
1041 sr_err("la8: %s: invalid divcount/samplerate", __func__);
1045 /* Fill acquisition parameters into buf[]. */
1046 buf[0] = la8->divcount;
1047 buf[1] = 0xff; /* This byte must always be 0xff. */
1048 buf[2] = la8->trigger_pattern;
1049 buf[3] = la8->trigger_mask;
1051 /* Start acquisition. */
1052 bytes_written = la8_write(la8, buf, 4);
1054 if (bytes_written < 0) {
1055 sr_err("la8: acquisition failed to start");
1057 } else if (bytes_written != 4) {
1058 sr_err("la8: acquisition failed to start");
1059 return SR_ERR; /* TODO: Other error and return code? */
1062 sr_dbg("la8: acquisition started successfully");
1064 la8->session_id = session_device_id;
1066 /* Send header packet to the session bus. */
1067 sr_dbg("la8: %s: sending SR_DF_HEADER", __func__);
1068 packet.type = SR_DF_HEADER;
1069 packet.length = sizeof(struct sr_datafeed_header);
1070 packet.unitsize = 0;
1071 packet.payload = &header;
1072 header.feed_version = 1;
1073 gettimeofday(&header.starttime, NULL);
1074 header.samplerate = la8->cur_samplerate;
1075 header.protocol_id = SR_PROTO_RAW;
1076 header.num_logic_probes = NUM_PROBES;
1077 header.num_analog_probes = 0;
1078 sr_session_bus(session_device_id, &packet);
1080 /* Time when we should be done (for detecting trigger timeouts). */
1081 la8->done = (la8->divcount + 1) * 0.08388608 + time(NULL)
1082 + la8->trigger_timeout;
1083 la8->block_counter = 0;
1084 la8->trigger_found = 0;
1086 /* Hook up a dummy handler to receive data from the LA8. */
1087 sr_source_add(-1, G_IO_IN, 0, receive_data, sdi);
1092 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
1094 struct sr_device_instance *sdi;
1096 struct sr_datafeed_packet packet;
1098 sr_dbg("la8: stopping acquisition");
1100 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
1101 sr_err("la8: %s: sdi was NULL", __func__);
1105 if (!(la8 = sdi->priv)) {
1106 sr_err("la8: %s: sdi->priv was NULL", __func__);
1110 /* Send end packet to the session bus. */
1111 sr_dbg("la8: %s: sending SR_DF_END", __func__);
1112 packet.type = SR_DF_END;
1114 packet.unitsize = 0;
1115 packet.payload = NULL;
1116 sr_session_bus(session_device_id, &packet);
1119 struct sr_device_plugin chronovu_la8_plugin_info = {
1120 .name = "chronovu-la8",
1121 .longname = "ChronoVu LA8",
1124 .cleanup = hw_cleanup,
1125 .opendev = hw_opendev,
1126 .closedev = hw_closedev,
1127 .get_device_info = hw_get_device_info,
1128 .get_status = hw_get_status,
1129 .get_capabilities = hw_get_capabilities,
1130 .set_configuration = hw_set_configuration,
1131 .start_acquisition = hw_start_acquisition,
1132 .stop_acquisition = hw_stop_acquisition,