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
26 #include "sigrok-internal.h"
28 #define USB_VENDOR_ID 0x0403
29 #define USB_PRODUCT_ID 0x6001
30 #define USB_DESCRIPTION "ChronoVu LA8"
31 #define USB_VENDOR_NAME "ChronoVu"
32 #define USB_MODEL_NAME "LA8"
33 #define USB_MODEL_VERSION ""
36 #define TRIGGER_TYPES "01"
37 #define SDRAM_SIZE (8 * 1024 * 1024)
38 #define MIN_NUM_SAMPLES 1
40 #define BS 4096 /* Block size */
41 #define NUM_BLOCKS 2048 /* Number of blocks */
43 static GSList *dev_insts = NULL;
45 static const char *probe_names[NUM_PROBES + 1] = {
57 /* Private, per-device-instance driver context. */
59 /** FTDI device context (used by libftdi). */
60 struct ftdi_context *ftdic;
62 /** The currently configured samplerate of the device. */
63 uint64_t cur_samplerate;
65 /** The current sampling limit (in ms). */
68 /** The current sampling limit (in number of samples). */
69 uint64_t limit_samples;
75 * A buffer containing some (mangled) samples from the device.
76 * Format: Pretty mangled-up (due to hardware reasons), see code.
78 uint8_t mangled_buf[BS];
81 * An 8MB buffer where we'll store the de-mangled samples.
82 * Format: Each sample is 1 byte, MSB is channel 7, LSB is channel 0.
87 * Trigger pattern (MSB = channel 7, LSB = channel 0).
88 * A 1 bit matches a high signal, 0 matches a low signal on a probe.
89 * Only low/high triggers (but not e.g. rising/falling) are supported.
91 uint8_t trigger_pattern;
94 * Trigger mask (MSB = channel 7, LSB = channel 0).
95 * A 1 bit means "must match trigger_pattern", 0 means "don't care".
99 /** Time (in seconds) before the trigger times out. */
100 uint64_t trigger_timeout;
102 /** Tells us whether an SR_DF_TRIGGER packet was already sent. */
108 /** Counter/index for the data block to be read. */
111 /** The divcount value (determines the sample period) for the LA8. */
115 /* This will be initialized via hw_dev_info_get()/SR_DI_SAMPLERATES. */
116 static uint64_t supported_samplerates[255 + 1] = { 0 };
119 * Min: 1 sample per 0.01us -> sample time is 0.084s, samplerate 100MHz
120 * Max: 1 sample per 2.55us -> sample time is 21.391s, samplerate 392.15kHz
122 static struct sr_samplerates samplerates = {
126 .list = supported_samplerates,
129 /* Note: Continuous sampling is not supported by the hardware. */
130 static int hwcaps[] = {
131 SR_HWCAP_LOGIC_ANALYZER,
133 SR_HWCAP_LIMIT_MSEC, /* TODO: Not yet implemented. */
134 SR_HWCAP_LIMIT_SAMPLES, /* TODO: Not yet implemented. */
138 /* Function prototypes. */
139 static int la8_close_usb_reset_sequencer(struct context *ctx);
140 static int hw_dev_acquisition_stop(int dev_index, gpointer session_data);
141 static int la8_reset(struct context *ctx);
143 static void fill_supported_samplerates_if_needed(void)
147 /* Do nothing if supported_samplerates[] is already filled. */
148 if (supported_samplerates[0] != 0)
151 /* Fill supported_samplerates[] with the proper values. */
152 for (i = 0; i < 255; i++)
153 supported_samplerates[254 - i] = SR_MHZ(100) / (i + 1);
154 supported_samplerates[255] = 0;
158 * Check if the given samplerate is supported by the LA8 hardware.
160 * @param samplerate The samplerate (in Hz) to check.
161 * @return 1 if the samplerate is supported/valid, 0 otherwise.
163 static int is_valid_samplerate(uint64_t samplerate)
167 fill_supported_samplerates_if_needed();
169 for (i = 0; i < 255; i++) {
170 if (supported_samplerates[i] == samplerate)
174 sr_err("la8: %s: invalid samplerate (%" PRIu64 "Hz)",
175 __func__, samplerate);
181 * Convert a samplerate (in Hz) to the 'divcount' value the LA8 wants.
183 * LA8 hardware: sample period = (divcount + 1) * 10ns.
184 * Min. value for divcount: 0x00 (10ns sample period, 100MHz samplerate).
185 * Max. value for divcount: 0xfe (2550ns sample period, 392.15kHz samplerate).
187 * @param samplerate The samplerate in Hz.
188 * @return The divcount value as needed by the hardware, or 0xff upon errors.
190 static uint8_t samplerate_to_divcount(uint64_t samplerate)
192 if (samplerate == 0) {
193 sr_err("la8: %s: samplerate was 0", __func__);
197 if (!is_valid_samplerate(samplerate)) {
198 sr_err("la8: %s: can't get divcount, samplerate invalid",
203 return (SR_MHZ(100) / samplerate) - 1;
207 * Write data of a certain length to the LA8's FTDI device.
209 * @param ctx The struct containing private per-device-instance data.
210 * @param buf The buffer containing the data to write.
211 * @param size The number of bytes to write.
212 * @return The number of bytes written, or a negative value upon errors.
214 static int la8_write(struct context *ctx, uint8_t *buf, int size)
219 sr_err("la8: %s: ctx was NULL", __func__);
224 sr_err("la8: %s: ctx->ftdic was NULL", __func__);
229 sr_err("la8: %s: buf was NULL", __func__);
234 sr_err("la8: %s: size was < 0", __func__);
238 bytes_written = ftdi_write_data(ctx->ftdic, buf, size);
240 if (bytes_written < 0) {
241 sr_err("la8: %s: ftdi_write_data: (%d) %s", __func__,
242 bytes_written, ftdi_get_error_string(ctx->ftdic));
243 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
244 } else if (bytes_written != size) {
245 sr_err("la8: %s: bytes to write: %d, bytes written: %d",
246 __func__, size, bytes_written);
247 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
250 return bytes_written;
254 * Read a certain amount of bytes from the LA8's FTDI device.
256 * @param ctx The struct containing private per-device-instance data.
257 * @param buf The buffer where the received data will be stored.
258 * @param size The number of bytes to read.
259 * @return The number of bytes read, or a negative value upon errors.
261 static int la8_read(struct context *ctx, uint8_t *buf, int size)
266 sr_err("la8: %s: ctx was NULL", __func__);
271 sr_err("la8: %s: ctx->ftdic was NULL", __func__);
276 sr_err("la8: %s: buf was NULL", __func__);
281 sr_err("la8: %s: size was <= 0", __func__);
285 bytes_read = ftdi_read_data(ctx->ftdic, buf, size);
287 if (bytes_read < 0) {
288 sr_err("la8: %s: ftdi_read_data: (%d) %s", __func__,
289 bytes_read, ftdi_get_error_string(ctx->ftdic));
290 } else if (bytes_read != size) {
291 // sr_err("la8: %s: bytes to read: %d, bytes read: %d",
292 // __func__, size, bytes_read);
298 static int la8_close(struct context *ctx)
303 sr_err("la8: %s: ctx was NULL", __func__);
308 sr_err("la8: %s: ctx->ftdic was NULL", __func__);
312 if ((ret = ftdi_usb_close(ctx->ftdic)) < 0) {
313 sr_err("la8: %s: ftdi_usb_close: (%d) %s",
314 __func__, ret, ftdi_get_error_string(ctx->ftdic));
321 * Close the ChronoVu LA8 USB port and reset the LA8 sequencer logic.
323 * @param ctx The struct containing private per-device-instance data.
324 * @return SR_OK upon success, SR_ERR upon failure.
326 static int la8_close_usb_reset_sequencer(struct context *ctx)
328 /* Magic sequence of bytes for resetting the LA8 sequencer logic. */
329 uint8_t buf[8] = {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};
333 sr_err("la8: %s: ctx was NULL", __func__);
338 sr_err("la8: %s: ctx->ftdic was NULL", __func__);
342 if (ctx->ftdic->usb_dev) {
343 /* Reset the LA8 sequencer logic, then wait 100ms. */
344 sr_dbg("la8: resetting sequencer logic");
345 (void) la8_write(ctx, buf, 8); /* Ignore errors. */
346 g_usleep(100 * 1000);
348 /* Purge FTDI buffers, then reset and close the FTDI device. */
349 sr_dbg("la8: purging buffers, resetting+closing FTDI device");
351 /* Log errors, but ignore them (i.e., don't abort). */
352 if ((ret = ftdi_usb_purge_buffers(ctx->ftdic)) < 0)
353 sr_err("la8: %s: ftdi_usb_purge_buffers: (%d) %s",
354 __func__, ret, ftdi_get_error_string(ctx->ftdic));
355 if ((ret = ftdi_usb_reset(ctx->ftdic)) < 0)
356 sr_err("la8: %s: ftdi_usb_reset: (%d) %s", __func__,
357 ret, ftdi_get_error_string(ctx->ftdic));
358 if ((ret = ftdi_usb_close(ctx->ftdic)) < 0)
359 sr_err("la8: %s: ftdi_usb_close: (%d) %s", __func__,
360 ret, ftdi_get_error_string(ctx->ftdic));
363 ftdi_free(ctx->ftdic); /* Returns void. */
370 * Reset the ChronoVu LA8.
372 * The LA8 must be reset after a failed read/write operation or upon timeouts.
374 * @param ctx The struct containing private per-device-instance data.
375 * @return SR_OK upon success, SR_ERR upon failure.
377 static int la8_reset(struct context *ctx)
384 sr_err("la8: %s: ctx was NULL", __func__);
389 sr_err("la8: %s: ctx->ftdic was NULL", __func__);
393 sr_dbg("la8: resetting the device");
396 * Purge pending read data from the FTDI hardware FIFO until
397 * no more data is left, or a timeout occurs (after 20s).
399 done = 20 + time(NULL);
401 /* TODO: Ignore errors? Check for < 0 at least! */
402 bytes_read = la8_read(ctx, (uint8_t *)&buf, BS);
404 } while ((done > now) && (bytes_read > 0));
406 /* Reset the LA8 sequencer logic and close the USB port. */
407 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
409 sr_dbg("la8: device reset finished");
414 static int configure_probes(struct context *ctx, GSList *probes)
416 struct sr_probe *probe;
421 ctx->trigger_pattern = 0;
422 ctx->trigger_mask = 0; /* Default to "don't care" for all probes. */
424 for (l = probes; l; l = l->next) {
425 probe = (struct sr_probe *)l->data;
428 sr_err("la8: %s: probe was NULL", __func__);
432 /* Skip disabled probes. */
436 /* Skip (enabled) probes with no configured trigger. */
440 /* Note: Must only be run if probe->trigger != NULL. */
441 if (probe->index < 0 || probe->index > 7) {
442 sr_err("la8: %s: invalid probe index %d, must be "
443 "between 0 and 7", __func__, probe->index);
447 probe_bit = (1 << (probe->index - 1));
449 /* Configure the probe's trigger mask and trigger pattern. */
450 for (tc = probe->trigger; tc && *tc; tc++) {
451 ctx->trigger_mask |= probe_bit;
453 /* Sanity check, LA8 only supports low/high trigger. */
454 if (*tc != '0' && *tc != '1') {
455 sr_err("la8: %s: invalid trigger '%c', only "
456 "'0'/'1' supported", __func__, *tc);
461 ctx->trigger_pattern |= probe_bit;
465 sr_dbg("la8: %s: trigger_mask = 0x%x, trigger_pattern = 0x%x",
466 __func__, ctx->trigger_mask, ctx->trigger_pattern);
471 static int hw_init(const char *devinfo)
474 struct sr_dev_inst *sdi;
477 /* Avoid compiler errors. */
480 /* Allocate memory for our private driver context. */
481 if (!(ctx = g_try_malloc(sizeof(struct context)))) {
482 sr_err("la8: %s: struct context malloc failed", __func__);
483 goto err_free_nothing;
486 /* Set some sane defaults. */
488 ctx->cur_samplerate = SR_MHZ(100); /* 100MHz == max. samplerate */
490 ctx->limit_samples = 0;
491 ctx->session_id = NULL;
492 memset(ctx->mangled_buf, 0, BS);
493 ctx->final_buf = NULL;
494 ctx->trigger_pattern = 0x00; /* Value irrelevant, see trigger_mask. */
495 ctx->trigger_mask = 0x00; /* All probes are "don't care". */
496 ctx->trigger_timeout = 10; /* Default to 10s trigger timeout. */
497 ctx->trigger_found = 0;
499 ctx->block_counter = 0;
500 ctx->divcount = 0; /* 10ns sample period == 100MHz samplerate */
502 /* Allocate memory where we'll store the de-mangled data. */
503 if (!(ctx->final_buf = g_try_malloc(SDRAM_SIZE))) {
504 sr_err("la8: %s: final_buf malloc failed", __func__);
508 /* Allocate memory for the FTDI context (ftdic) and initialize it. */
509 if (!(ctx->ftdic = ftdi_new())) {
510 sr_err("la8: %s: ftdi_new failed", __func__);
511 goto err_free_final_buf;
514 /* Check for the device and temporarily open it. */
515 if ((ret = ftdi_usb_open_desc(ctx->ftdic, USB_VENDOR_ID,
516 USB_PRODUCT_ID, USB_DESCRIPTION, NULL)) < 0) {
517 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
520 sr_dbg("la8: found device");
522 /* Register the device with libsigrok. */
523 sdi = sr_dev_inst_new(0, SR_ST_INITIALIZING,
524 USB_VENDOR_NAME, USB_MODEL_NAME, USB_MODEL_VERSION);
526 sr_err("la8: %s: sr_dev_inst_new failed", __func__);
527 goto err_close_ftdic;
532 dev_insts = g_slist_append(dev_insts, sdi);
534 sr_spew("la8: %s finished successfully", __func__);
536 /* Close device. We'll reopen it again when we need it. */
537 (void) la8_close(ctx); /* Log, but ignore errors. */
542 (void) la8_close(ctx); /* Log, but ignore errors. */
544 free(ctx->ftdic); /* NOT g_free()! */
546 g_free(ctx->final_buf);
554 static int hw_dev_open(int dev_index)
557 struct sr_dev_inst *sdi;
560 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
561 sr_err("la8: %s: sdi was NULL", __func__);
562 return SR_ERR; /* TODO: SR_ERR_ARG? */
565 if (!(ctx = 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(ctx->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(ctx->ftdic));
577 (void) la8_close_usb_reset_sequencer(ctx); /* 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(ctx->ftdic)) < 0) {
584 sr_err("la8: %s: ftdi_usb_purge_buffers: (%d) %s",
585 __func__, ret, ftdi_get_error_string(ctx->ftdic));
586 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
587 goto err_dev_open_close_ftdic;
589 sr_dbg("la8: FTDI buffers purged successfully");
591 /* Enable flow control in the FTDI chip. */
592 if ((ret = ftdi_setflowctrl(ctx->ftdic, SIO_RTS_CTS_HS)) < 0) {
593 sr_err("la8: %s: ftdi_setflowcontrol: (%d) %s",
594 __func__, ret, ftdi_get_error_string(ctx->ftdic));
595 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
596 goto err_dev_open_close_ftdic;
598 sr_dbg("la8: FTDI flow control enabled successfully");
601 g_usleep(100 * 1000);
603 sdi->status = SR_ST_ACTIVE;
607 err_dev_open_close_ftdic:
608 (void) la8_close(ctx); /* Log, but ignore errors. */
612 static int set_samplerate(struct sr_dev_inst *sdi, uint64_t samplerate)
617 sr_err("la8: %s: sdi was NULL", __func__);
621 if (!(ctx = 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 ctx->cur_samplerate = samplerate;
637 sr_dbg("la8: samplerate set to %" PRIu64 "Hz", ctx->cur_samplerate);
642 static int hw_dev_close(int dev_index)
644 struct sr_dev_inst *sdi;
647 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
648 sr_err("la8: %s: sdi was NULL", __func__);
649 return SR_ERR; /* TODO: SR_ERR_ARG? */
652 if (!(ctx = 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(ctx); /* 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(ctx->final_buf);
675 static int hw_cleanup(void)
678 struct sr_dev_inst *sdi;
681 /* Properly close all devices. */
682 for (l = dev_insts; l; l = l->next) {
683 if (!(sdi = l->data)) {
684 /* Log error, but continue cleaning up the rest. */
685 sr_err("la8: %s: sdi was NULL, continuing", __func__);
689 sr_dev_inst_free(sdi); /* Returns void. */
691 g_slist_free(dev_insts); /* Returns void. */
697 static void *hw_dev_info_get(int dev_index, int dev_info_id)
699 struct sr_dev_inst *sdi;
703 sr_spew("la8: entering %s", __func__);
705 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
706 sr_err("la8: %s: sdi was NULL", __func__);
710 if (!(ctx = sdi->priv)) {
711 sr_err("la8: %s: sdi->priv was NULL", __func__);
715 switch (dev_info_id) {
719 case SR_DI_NUM_PROBES:
720 info = GINT_TO_POINTER(NUM_PROBES);
722 case SR_DI_PROBE_NAMES:
725 case SR_DI_SAMPLERATES:
726 fill_supported_samplerates_if_needed();
729 case SR_DI_TRIGGER_TYPES:
730 info = (char *)TRIGGER_TYPES;
732 case SR_DI_CUR_SAMPLERATE:
733 info = &ctx->cur_samplerate;
736 /* Unknown device info ID, return NULL. */
737 sr_err("la8: %s: Unknown device info ID", __func__);
745 static int hw_dev_status_get(int dev_index)
747 struct sr_dev_inst *sdi;
749 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
750 sr_err("la8: %s: sdi was NULL, device not found", __func__);
751 return SR_ST_NOT_FOUND;
754 sr_dbg("la8: %s: returning status %d", __func__, sdi->status);
759 static int *hw_hwcap_get_all(void)
761 sr_spew("la8: entering %s", __func__);
766 static int hw_dev_config_set(int dev_index, int hwcap, void *value)
768 struct sr_dev_inst *sdi;
771 sr_spew("la8: entering %s", __func__);
773 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
774 sr_err("la8: %s: sdi was NULL", __func__);
775 return SR_ERR; /* TODO: SR_ERR_ARG? */
778 if (!(ctx = sdi->priv)) {
779 sr_err("la8: %s: sdi->priv was NULL", __func__);
780 return SR_ERR; /* TODO: SR_ERR_ARG? */
784 case SR_HWCAP_SAMPLERATE:
785 if (set_samplerate(sdi, *(uint64_t *)value) == SR_ERR)
787 sr_dbg("la8: SAMPLERATE = %" PRIu64, ctx->cur_samplerate);
789 case SR_HWCAP_PROBECONFIG:
790 if (configure_probes(ctx, (GSList *)value) != SR_OK) {
791 sr_err("la8: %s: probe config failed", __func__);
795 case SR_HWCAP_LIMIT_MSEC:
796 if (*(uint64_t *)value == 0) {
797 sr_err("la8: %s: LIMIT_MSEC can't be 0", __func__);
800 ctx->limit_msec = *(uint64_t *)value;
801 sr_dbg("la8: LIMIT_MSEC = %" PRIu64, ctx->limit_msec);
803 case SR_HWCAP_LIMIT_SAMPLES:
804 if (*(uint64_t *)value < MIN_NUM_SAMPLES) {
805 sr_err("la8: %s: LIMIT_SAMPLES too small", __func__);
808 ctx->limit_samples = *(uint64_t *)value;
809 sr_dbg("la8: LIMIT_SAMPLES = %" PRIu64, ctx->limit_samples);
812 /* Unknown capability, return SR_ERR. */
813 sr_err("la8: %s: Unknown capability", __func__);
822 * Get a block of data from the LA8.
824 * @param ctx The struct containing private per-device-instance data.
825 * @return SR_OK upon success, or SR_ERR upon errors.
827 static int la8_read_block(struct context *ctx)
829 int i, byte_offset, m, mi, p, index, bytes_read;
833 sr_err("la8: %s: ctx was NULL", __func__);
838 sr_err("la8: %s: ctx->ftdic was NULL", __func__);
842 sr_spew("la8: %s: reading block %d", __func__, ctx->block_counter);
844 bytes_read = la8_read(ctx, ctx->mangled_buf, BS);
846 /* If first block read got 0 bytes, retry until success or timeout. */
847 if ((bytes_read == 0) && (ctx->block_counter == 0)) {
849 sr_spew("la8: %s: reading block 0 again", __func__);
850 bytes_read = la8_read(ctx, ctx->mangled_buf, BS);
851 /* TODO: How to handle read errors here? */
853 } while ((ctx->done > now) && (bytes_read == 0));
856 /* Check if block read was successful or a timeout occured. */
857 if (bytes_read != BS) {
858 sr_err("la8: %s: trigger timed out", __func__);
859 (void) la8_reset(ctx); /* Ignore errors. */
863 /* De-mangle the data. */
864 sr_spew("la8: de-mangling samples of block %d", ctx->block_counter);
865 byte_offset = ctx->block_counter * BS;
866 m = byte_offset / (1024 * 1024);
867 mi = m * (1024 * 1024);
868 for (i = 0; i < BS; i++) {
870 index = m * 2 + (((byte_offset + i) - mi) / 2) * 16;
871 index += (ctx->divcount == 0) ? p : (1 - p);
872 ctx->final_buf[index] = ctx->mangled_buf[i];
878 static void send_block_to_session_bus(struct context *ctx, int block)
881 uint8_t sample, expected_sample;
882 struct sr_datafeed_packet packet;
883 struct sr_datafeed_logic logic;
884 int trigger_point; /* Relative trigger point (in this block). */
886 /* Note: No sanity checks on ctx/block, caller is responsible. */
888 /* Check if we can find the trigger condition in this block. */
890 expected_sample = ctx->trigger_pattern & ctx->trigger_mask;
891 for (i = 0; i < BS; i++) {
892 /* Don't continue if the trigger was found previously. */
893 if (ctx->trigger_found)
897 * Also, don't continue if triggers are "don't care", i.e. if
898 * no trigger conditions were specified by the user. In that
899 * case we don't want to send an SR_DF_TRIGGER packet at all.
901 if (ctx->trigger_mask == 0x00)
904 sample = *(ctx->final_buf + (block * BS) + i);
906 if ((sample & ctx->trigger_mask) == expected_sample) {
908 ctx->trigger_found = 1;
913 /* If no trigger was found, send one SR_DF_LOGIC packet. */
914 if (trigger_point == -1) {
915 /* Send an SR_DF_LOGIC packet to the session bus. */
916 sr_spew("la8: sending SR_DF_LOGIC packet (%d bytes) for "
917 "block %d", BS, block);
918 packet.type = SR_DF_LOGIC;
919 packet.payload = &logic;
922 logic.data = ctx->final_buf + (block * BS);
923 sr_session_bus(ctx->session_id, &packet);
928 * We found the trigger, so some special handling is needed. We have
929 * to send an SR_DF_LOGIC packet with the samples before the trigger
930 * (if any), then the SD_DF_TRIGGER packet itself, then another
931 * SR_DF_LOGIC packet with the samples after the trigger (if any).
934 /* TODO: Send SR_DF_TRIGGER packet before or after the actual sample? */
936 /* If at least one sample is located before the trigger... */
937 if (trigger_point > 0) {
938 /* Send pre-trigger SR_DF_LOGIC packet to the session bus. */
939 sr_spew("la8: sending pre-trigger SR_DF_LOGIC packet, "
940 "start = %d, length = %d", block * BS, trigger_point);
941 packet.type = SR_DF_LOGIC;
942 packet.payload = &logic;
943 logic.length = trigger_point;
945 logic.data = ctx->final_buf + (block * BS);
946 sr_session_bus(ctx->session_id, &packet);
949 /* Send the SR_DF_TRIGGER packet to the session bus. */
950 sr_spew("la8: sending SR_DF_TRIGGER packet, sample = %d",
951 (block * BS) + trigger_point);
952 packet.type = SR_DF_TRIGGER;
953 packet.payload = NULL;
954 sr_session_bus(ctx->session_id, &packet);
956 /* If at least one sample is located after the trigger... */
957 if (trigger_point < (BS - 1)) {
958 /* Send post-trigger SR_DF_LOGIC packet to the session bus. */
959 sr_spew("la8: sending post-trigger SR_DF_LOGIC packet, "
960 "start = %d, length = %d",
961 (block * BS) + trigger_point, BS - trigger_point);
962 packet.type = SR_DF_LOGIC;
963 packet.payload = &logic;
964 logic.length = BS - trigger_point;
966 logic.data = ctx->final_buf + (block * BS) + trigger_point;
967 sr_session_bus(ctx->session_id, &packet);
971 static int receive_data(int fd, int revents, void *session_data)
974 struct sr_dev_inst *sdi;
977 /* Avoid compiler errors. */
981 if (!(sdi = session_data)) {
982 sr_err("la8: %s: session_data was NULL", __func__);
986 if (!(ctx = sdi->priv)) {
987 sr_err("la8: %s: sdi->priv was NULL", __func__);
991 /* Get one block of data. */
992 if ((ret = la8_read_block(ctx)) < 0) {
993 sr_err("la8: %s: la8_read_block error: %d", __func__, ret);
994 hw_dev_acquisition_stop(sdi->index, session_data);
998 /* We need to get exactly NUM_BLOCKS blocks (i.e. 8MB) of data. */
999 if (ctx->block_counter != (NUM_BLOCKS - 1)) {
1000 ctx->block_counter++;
1004 sr_dbg("la8: sampling finished, sending data to session bus now");
1006 /* All data was received and demangled, send it to the session bus. */
1007 for (i = 0; i < NUM_BLOCKS; i++)
1008 send_block_to_session_bus(ctx, i);
1010 hw_dev_acquisition_stop(sdi->index, session_data);
1012 // return FALSE; /* FIXME? */
1016 static int hw_dev_acquisition_start(int dev_index, gpointer session_data)
1018 struct sr_dev_inst *sdi;
1019 struct context *ctx;
1020 struct sr_datafeed_packet packet;
1021 struct sr_datafeed_header header;
1025 sr_spew("la8: entering %s", __func__);
1027 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
1028 sr_err("la8: %s: sdi was NULL", __func__);
1029 return SR_ERR; /* TODO: SR_ERR_ARG? */
1032 if (!(ctx = sdi->priv)) {
1033 sr_err("la8: %s: sdi->priv was NULL", __func__);
1034 return SR_ERR; /* TODO: SR_ERR_ARG? */
1038 sr_err("la8: %s: ctx->ftdic was NULL", __func__);
1042 ctx->divcount = samplerate_to_divcount(ctx->cur_samplerate);
1043 if (ctx->divcount == 0xff) {
1044 sr_err("la8: %s: invalid divcount/samplerate", __func__);
1048 /* Fill acquisition parameters into buf[]. */
1049 buf[0] = ctx->divcount;
1050 buf[1] = 0xff; /* This byte must always be 0xff. */
1051 buf[2] = ctx->trigger_pattern;
1052 buf[3] = ctx->trigger_mask;
1054 /* Start acquisition. */
1055 bytes_written = la8_write(ctx, buf, 4);
1057 if (bytes_written < 0) {
1058 sr_err("la8: acquisition failed to start");
1060 } else if (bytes_written != 4) {
1061 sr_err("la8: acquisition failed to start");
1062 return SR_ERR; /* TODO: Other error and return code? */
1065 sr_dbg("la8: acquisition started successfully");
1067 ctx->session_id = session_data;
1069 /* Send header packet to the session bus. */
1070 sr_dbg("la8: %s: sending SR_DF_HEADER", __func__);
1071 packet.type = SR_DF_HEADER;
1072 packet.payload = &header;
1073 header.feed_version = 1;
1074 gettimeofday(&header.starttime, NULL);
1075 header.samplerate = ctx->cur_samplerate;
1076 header.num_logic_probes = NUM_PROBES;
1077 sr_session_bus(session_data, &packet);
1079 /* Time when we should be done (for detecting trigger timeouts). */
1080 ctx->done = (ctx->divcount + 1) * 0.08388608 + time(NULL)
1081 + ctx->trigger_timeout;
1082 ctx->block_counter = 0;
1083 ctx->trigger_found = 0;
1085 /* Hook up a dummy handler to receive data from the LA8. */
1086 sr_source_add(-1, G_IO_IN, 0, receive_data, sdi);
1091 static int hw_dev_acquisition_stop(int dev_index, gpointer session_data)
1093 struct sr_dev_inst *sdi;
1094 struct context *ctx;
1095 struct sr_datafeed_packet packet;
1097 sr_dbg("la8: stopping acquisition");
1099 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
1100 sr_err("la8: %s: sdi was NULL", __func__);
1104 if (!(ctx = sdi->priv)) {
1105 sr_err("la8: %s: sdi->priv was NULL", __func__);
1109 /* Send end packet to the session bus. */
1110 sr_dbg("la8: %s: sending SR_DF_END", __func__);
1111 packet.type = SR_DF_END;
1112 sr_session_bus(session_data, &packet);
1117 SR_PRIV struct sr_dev_plugin chronovu_la8_plugin_info = {
1118 .name = "chronovu-la8",
1119 .longname = "ChronoVu LA8",
1122 .cleanup = hw_cleanup,
1123 .dev_open = hw_dev_open,
1124 .dev_close = hw_dev_close,
1125 .dev_info_get = hw_dev_info_get,
1126 .dev_status_get = hw_dev_status_get,
1127 .hwcap_get_all = hw_hwcap_get_all,
1128 .dev_config_set = hw_dev_config_set,
1129 .dev_acquisition_start = hw_dev_acquisition_start,
1130 .dev_acquisition_stop = hw_dev_acquisition_stop,