From: Uwe Hermann Date: Tue, 4 Jun 2013 13:32:20 +0000 (+0200) Subject: ikalogic-scanaplus: Initial driver implementation. X-Git-Tag: libsigrok-0.2.1~36 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=ab4bb6eb7c6d7ab2eb212088a764fb696726c8dc;p=libsigrok.git ikalogic-scanaplus: Initial driver implementation. --- diff --git a/hardware/ikalogic-scanaplus/api.c b/hardware/ikalogic-scanaplus/api.c index 2a1df8c1..7f1109a8 100644 --- a/hardware/ikalogic-scanaplus/api.c +++ b/hardware/ikalogic-scanaplus/api.c @@ -20,26 +20,146 @@ #include "protocol.h" +#define USB_VENDOR_ID 0x0403 +#define USB_DEVICE_ID 0x6014 +#define USB_VENDOR_NAME "Ikalogic" +#define USB_MODEL_NAME "ScanaPLUS" +#define USB_IPRODUCT "SCANAPLUS" + +#define SAMPLE_BUF_SIZE (8 * 1024 * 1024) + +static const int32_t hwcaps[] = { + SR_CONF_LOGIC_ANALYZER, + SR_CONF_SAMPLERATE, + SR_CONF_LIMIT_MSEC, + SR_CONF_LIMIT_SAMPLES, + SR_CONF_CONTINUOUS, // TODO? +}; + +/* Probes are numbered 1-9. */ +static const char *probe_names[] = { + "1", "2", "3", "4", "5", "6", "7", "8", "9", + NULL, +}; + +/* Note: The Ikalogic ScanaPLUS always samples at 100MHz. */ +static uint64_t samplerates[1] = { SR_MHZ(100) }; + SR_PRIV struct sr_dev_driver ikalogic_scanaplus_driver_info; static struct sr_dev_driver *di = &ikalogic_scanaplus_driver_info; +static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data); + +static void clear_helper(void *priv) +{ + struct dev_context *devc; + + devc = priv; + + ftdi_free(devc->ftdic); + g_free(devc->compressed_buf); + g_free(devc->sample_buf); +} + +static int dev_clear(void) +{ + return std_dev_clear(di, clear_helper); +} + static int init(struct sr_context *sr_ctx) { - return std_hw_init(sr_ctx, di, LOG_PREFIX); + return std_init(sr_ctx, di, LOG_PREFIX); } static GSList *scan(GSList *options) { + struct sr_dev_inst *sdi; + struct sr_probe *probe; struct drv_context *drvc; + struct dev_context *devc; GSList *devices; + unsigned int i; + int ret; (void)options; - devices = NULL; drvc = di->priv; - drvc->instances = NULL; + + devices = NULL; + + /* Allocate memory for our private device context. */ + if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) { + sr_err("Device context malloc failed."); + goto err_free_nothing; + } + + /* Allocate memory for the incoming compressed samples. */ + if (!(devc->compressed_buf = g_try_malloc0(COMPRESSED_BUF_SIZE))) { + sr_err("compressed_buf malloc failed."); + goto err_free_devc; + } + + /* Allocate memory for the uncompressed samples. */ + if (!(devc->sample_buf = g_try_malloc0(SAMPLE_BUF_SIZE))) { + sr_err("sample_buf malloc failed."); + goto err_free_compressed_buf; + } + + /* Allocate memory for the FTDI context (ftdic) and initialize it. */ + if (!(devc->ftdic = ftdi_new())) { + sr_err("Failed to initialize libftdi."); + goto err_free_sample_buf; + } + + /* Check for the device and temporarily open it. */ + ret = ftdi_usb_open_desc(devc->ftdic, USB_VENDOR_ID, USB_DEVICE_ID, + USB_IPRODUCT, NULL); + if (ret < 0) { + /* Log errors, except for -3 ("device not found"). */ + if (ret != -3) + sr_err("Failed to open device (%d): %s", ret, + ftdi_get_error_string(devc->ftdic)); + goto err_free_ftdic; + } + + /* Register the device with libsigrok. */ + sdi = sr_dev_inst_new(0, SR_ST_INITIALIZING, + USB_VENDOR_NAME, USB_MODEL_NAME, NULL); + if (!sdi) { + sr_err("Failed to create device instance."); + goto err_close_ftdic; + } + sdi->driver = di; + sdi->priv = devc; + + for (i = 0; probe_names[i]; i++) { + if (!(probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE, + probe_names[i]))) + return NULL; + sdi->probes = g_slist_append(sdi->probes, probe); + } + + devices = g_slist_append(devices, sdi); + drvc->instances = g_slist_append(drvc->instances, sdi); + + /* Close device. We'll reopen it again when we need it. */ + scanaplus_close(devc); return devices; + +err_close_ftdic: + scanaplus_close(devc); +err_free_ftdic: + ftdi_free(devc->ftdic); /* NOT free() or g_free()! */ +err_free_sample_buf: + g_free(devc->sample_buf); +err_free_compressed_buf: + g_free(devc->compressed_buf); +err_free_devc: + g_free(devc); +err_free_nothing: + + return NULL; } static GSList *dev_list(void) @@ -47,27 +167,111 @@ static GSList *dev_list(void) return ((struct drv_context *)(di->priv))->instances; } -static int dev_clear(void) -{ - return std_dev_clear(di, NULL); -} - static int dev_open(struct sr_dev_inst *sdi) { - (void)sdi; + struct dev_context *devc; + int ret; + + devc = sdi->priv; + + /* Select interface A, otherwise communication will fail. */ + ret = ftdi_set_interface(devc->ftdic, INTERFACE_A); + if (ret < 0) { + sr_err("Failed to set FTDI interface A (%d): %s", ret, + ftdi_get_error_string(devc->ftdic)); + return SR_ERR; + } + sr_dbg("FTDI chip interface A set successfully."); + + /* Open the device. */ + ret = ftdi_usb_open_desc(devc->ftdic, USB_VENDOR_ID, USB_DEVICE_ID, + USB_IPRODUCT, NULL); + if (ret < 0) { + sr_err("Failed to open device (%d): %s", ret, + ftdi_get_error_string(devc->ftdic)); + return SR_ERR; + } + sr_dbg("FTDI device opened successfully."); + + /* Purge RX/TX buffers in the FTDI chip. */ + if ((ret = ftdi_usb_purge_buffers(devc->ftdic)) < 0) { + sr_err("Failed to purge FTDI RX/TX buffers (%d): %s.", + ret, ftdi_get_error_string(devc->ftdic)); + goto err_dev_open_close_ftdic; + } + sr_dbg("FTDI chip buffers purged successfully."); + + /* Reset the FTDI bitmode. */ + ret = ftdi_set_bitmode(devc->ftdic, 0xff, BITMODE_RESET); + if (ret < 0) { + sr_err("Failed to reset the FTDI chip bitmode (%d): %s.", + ret, ftdi_get_error_string(devc->ftdic)); + goto err_dev_open_close_ftdic; + } + sr_dbg("FTDI chip bitmode reset successfully."); + + /* Set FTDI bitmode to "sync FIFO". */ + ret = ftdi_set_bitmode(devc->ftdic, 0xff, BITMODE_SYNCFF); + if (ret < 0) { + sr_err("Failed to put FTDI chip into sync FIFO mode (%d): %s.", + ret, ftdi_get_error_string(devc->ftdic)); + goto err_dev_open_close_ftdic; + } + sr_dbg("FTDI chip sync FIFO mode entered successfully."); + + /* Set the FTDI latency timer to 2. */ + ret = ftdi_set_latency_timer(devc->ftdic, 2); + if (ret < 0) { + sr_err("Failed to set FTDI latency timer (%d): %s.", + ret, ftdi_get_error_string(devc->ftdic)); + goto err_dev_open_close_ftdic; + } + sr_dbg("FTDI chip latency timer set successfully."); + + /* Set the FTDI read data chunk size to 64kB. */ + ret = ftdi_read_data_set_chunksize(devc->ftdic, 64 * 1024); + if (ret < 0) { + sr_err("Failed to set FTDI read data chunk size (%d): %s.", + ret, ftdi_get_error_string(devc->ftdic)); + goto err_dev_open_close_ftdic; + } + sr_dbg("FTDI chip read data chunk size set successfully."); + + /* Get the ScanaPLUS device ID from the FTDI EEPROM. */ + if ((ret = scanaplus_get_device_id(devc)) < 0) { + sr_err("Failed to get ScanaPLUS device ID: %d.", ret); + goto err_dev_open_close_ftdic; + } + sr_dbg("Received ScanaPLUS device ID successfully: %02x %02x %02x.", + devc->devid[0], devc->devid[1], devc->devid[2]); sdi->status = SR_ST_ACTIVE; return SR_OK; + +err_dev_open_close_ftdic: + scanaplus_close(devc); + return SR_ERR; } static int dev_close(struct sr_dev_inst *sdi) { - (void)sdi; + int ret; + struct dev_context *devc; + + ret = SR_OK; + devc = sdi->priv; + + if (sdi->status == SR_ST_ACTIVE) { + sr_dbg("Status ACTIVE, closing device."); + ret = scanaplus_close(devc); + } else { + sr_spew("Status not ACTIVE, nothing to do."); + } sdi->status = SR_ST_INACTIVE; - return SR_OK; + return ret; } static int cleanup(void) @@ -75,73 +279,134 @@ static int cleanup(void) return dev_clear(); } -static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi) +static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi) { - int ret; - (void)sdi; - (void)data; - ret = SR_OK; - switch (key) { + switch (id) { + case SR_CONF_SAMPLERATE: + /* The ScanaPLUS samplerate is 100MHz and can't be changed. */ + *data = g_variant_new_uint64(SR_MHZ(100)); + break; default: return SR_ERR_NA; } - return ret; + return SR_OK; } -static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi) +static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi) { - int ret; - - (void)data; + struct dev_context *devc; if (sdi->status != SR_ST_ACTIVE) return SR_ERR_DEV_CLOSED; - ret = SR_OK; - switch (key) { + devc = sdi->priv; + + switch (id) { + case SR_CONF_SAMPLERATE: + if (g_variant_get_uint64(data) != SR_MHZ(100)) { + sr_err("ScanaPLUS only supports samplerate = 100MHz."); + return SR_ERR_ARG; + } + /* Nothing to do, the ScanaPLUS samplerate is always 100MHz. */ + break; + case SR_CONF_LIMIT_MSEC: + if (g_variant_get_uint64(data) == 0) + return SR_ERR_ARG; + devc->limit_msec = g_variant_get_uint64(data); + break; + case SR_CONF_LIMIT_SAMPLES: + if (g_variant_get_uint64(data) == 0) + return SR_ERR_ARG; + devc->limit_samples = g_variant_get_uint64(data); + break; default: - ret = SR_ERR_NA; + return SR_ERR_NA; } - return ret; + return SR_OK; } static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi) { - int ret; + GVariant *gvar; + GVariantBuilder gvb; (void)sdi; - (void)data; - ret = SR_OK; switch (key) { + case SR_CONF_DEVICE_OPTIONS: + *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, + hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t)); + break; + case SR_CONF_SAMPLERATE: + g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}")); + gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"), + samplerates, ARRAY_SIZE(samplerates), + sizeof(uint64_t)); + g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar); + *data = g_variant_builder_end(&gvb); + break; default: return SR_ERR_NA; } - return ret; + return SR_OK; } static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) { - (void)sdi; - (void)cb_data; + int ret; + struct dev_context *devc; if (sdi->status != SR_ST_ACTIVE) return SR_ERR_DEV_CLOSED; + if (!(devc = sdi->priv)) + return SR_ERR_BUG; + + if (!devc->ftdic) + return SR_ERR_BUG; + + /* TODO: Configure probes later (thresholds etc.). */ + + devc->cb_data = cb_data; + + /* Properly reset internal variables before every new acquisition. */ + devc->compressed_bytes_ignored = 0; + devc->samples_sent = 0; + devc->bytes_received = 0; + + if ((ret = scanaplus_init(devc)) < 0) + return ret; + + if ((ret = scanaplus_start_acquisition(devc)) < 0) + return ret; + + /* Send header packet to the session bus. */ + std_session_send_df_header(cb_data, LOG_PREFIX); + + /* Hook up a dummy handler to receive data from the device. */ + sr_source_add(-1, G_IO_IN, 0, scanaplus_receive_data, (void *)sdi); + return SR_OK; } static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) { - (void)cb_data; + struct sr_datafeed_packet packet; - if (sdi->status != SR_ST_ACTIVE) - return SR_ERR_DEV_CLOSED; + (void)sdi; + + sr_dbg("Stopping acquisition."); + sr_source_remove(-1); + + /* Send end packet to the session bus. */ + sr_dbg("Sending SR_DF_END."); + packet.type = SR_DF_END; + sr_session_send(cb_data, &packet); return SR_OK; } diff --git a/hardware/ikalogic-scanaplus/protocol.c b/hardware/ikalogic-scanaplus/protocol.c index 2513153d..86597850 100644 --- a/hardware/ikalogic-scanaplus/protocol.c +++ b/hardware/ikalogic-scanaplus/protocol.c @@ -20,12 +20,286 @@ #include "protocol.h" -SR_PRIV int ikalogic_scanaplus_receive_data(int fd, int revents, void *cb_data) +/* + * Logic level thresholds. + * + * For each of the two channel groups (1-4 and 5-9), the logic level + * threshold can be set independently. + * + * The threshold can be set to values that are usable for systems with + * different voltage levels, e.g. for 1.8V or 3.3V systems. + * + * The actual threshold value is always the middle of the values below. + * E.g. for a system voltage level of 1.8V, the threshold is at 0.9V. That + * means that values <= 0.9V are considered to be a logic 0/low, and + * values > 0.9V are considered to be a logic 1/high. + * + * - 1.2V system: threshold = 0.6V + * - 1.5V system: threshold = 0.75V + * - 1.8V system: threshold = 0.9V + * - 2.8V system: threshold = 1.4V + * - 3.3V system: threshold = 1.65V + */ +#define THRESHOLD_1_2V_SYSTEM 0x2e +#define THRESHOLD_1_5V_SYSTEM 0x39 +#define THRESHOLD_1_8V_SYSTEM 0x45 +#define THRESHOLD_2_8V_SYSTEM 0x6c +#define THRESHOLD_3_3V_SYSTEM 0x7f + +static int scanaplus_write(struct dev_context *devc, uint8_t *buf, int size) +{ + int i, bytes_written; + GString *s; + + /* Note: Caller checks devc, devc->ftdic, buf, size. */ + + s = g_string_sized_new(100); + g_string_printf(s, "Writing %d bytes: ", size); + for (i = 0; i < size; i++) + g_string_append_printf(s, "0x%02x ", buf[i]); + sr_spew("%s", s->str); + g_string_free(s, TRUE); + + bytes_written = ftdi_write_data(devc->ftdic, buf, size); + if (bytes_written < 0) { + sr_err("Failed to write FTDI data (%d): %s.", + bytes_written, ftdi_get_error_string(devc->ftdic)); + } else if (bytes_written != size) { + sr_err("FTDI write error, only %d/%d bytes written: %s.", + bytes_written, size, ftdi_get_error_string(devc->ftdic)); + } + + return bytes_written; +} + +SR_PRIV int scanaplus_close(struct dev_context *devc) +{ + int ret; + + /* Note: Caller checks devc and devc->ftdic. */ + + if ((ret = ftdi_usb_close(devc->ftdic)) < 0) { + sr_err("Failed to close FTDI device (%d): %s.", + ret, ftdi_get_error_string(devc->ftdic)); + return SR_ERR; + } + + return SR_OK; +} + +static void scanaplus_uncompress_block(struct dev_context *devc, + uint64_t num_bytes) { - const struct sr_dev_inst *sdi; + uint64_t i, j; + uint8_t num_samples, low, high; + + for (i = 0; i < num_bytes; i += 2) { + num_samples = devc->compressed_buf[i + 0] >> 1; + + low = devc->compressed_buf[i + 0] & (1 << 0); + high = devc->compressed_buf[i + 1]; + + for (j = 0; j < num_samples; j++) { + devc->sample_buf[devc->bytes_received++] = high; + devc->sample_buf[devc->bytes_received++] = low; + } + } +} + +static void send_samples(struct dev_context *devc, uint64_t samples_to_send) +{ + struct sr_datafeed_packet packet; + struct sr_datafeed_logic logic; + + sr_spew("Sending %" PRIu64 " samples.", samples_to_send); + + packet.type = SR_DF_LOGIC; + packet.payload = &logic; + logic.length = samples_to_send * 2; + logic.unitsize = 2; /* We need 2 bytes for 9 probes. */ + logic.data = devc->sample_buf; + sr_session_send(devc->cb_data, &packet); + + devc->samples_sent += samples_to_send; + devc->bytes_received -= samples_to_send * 2; +} + +SR_PRIV int scanaplus_get_device_id(struct dev_context *devc) +{ + int ret; + uint16_t val1, val2; + + /* FTDI EEPROM indices 16+17 contain the 3 device ID bytes. */ + if ((ret = ftdi_read_eeprom_location(devc->ftdic, 16, &val1)) < 0) { + sr_err("Failed to read EEPROM index 16 (%d): %s.", + ret, ftdi_get_error_string(devc->ftdic)); + return SR_ERR; + } + if ((ret = ftdi_read_eeprom_location(devc->ftdic, 17, &val2)) < 0) { + sr_err("Failed to read EEPROM index 17 (%d): %s.", + ret, ftdi_get_error_string(devc->ftdic)); + return SR_ERR; + } + + /* + * Note: Bit 7 of the three bytes must not be used, apparently. + * + * Even though the three bits can be either 0 or 1 (we've seen both + * in actual ScanaPLUS devices), the device ID as sent to the FPGA + * has bit 7 of each byte zero'd out. + * + * It is unknown whether bit 7 of these bytes has any meaning, + * whether it's used somewhere, or whether it can be simply ignored. + */ + devc->devid[0] = ((val1 >> 0) & 0xff) & ~(1 << 7); + devc->devid[1] = ((val1 >> 8) & 0xff) & ~(1 << 7); + devc->devid[2] = ((val2 >> 0) & 0xff) & ~(1 << 7); + + return SR_OK; +} + +static int scanaplus_clear_device_id(struct dev_context *devc) +{ + uint8_t buf[2]; + + buf[0] = 0x8c; + buf[1] = 0x00; + if (scanaplus_write(devc, (uint8_t *)&buf, 2) < 0) + return SR_ERR; + + buf[0] = 0x8e; + buf[1] = 0x00; + if (scanaplus_write(devc, (uint8_t *)&buf, 2) < 0) + return SR_ERR; + + buf[0] = 0x8f; + buf[1] = 0x00; + if (scanaplus_write(devc, (uint8_t *)&buf, 2) < 0) + return SR_ERR; + + return SR_OK; +} + +static int scanaplus_send_device_id(struct dev_context *devc) +{ + uint8_t buf[2]; + + buf[0] = 0x8c; + buf[1] = devc->devid[0]; + if (scanaplus_write(devc, (uint8_t *)&buf, 2) < 0) + return SR_ERR; + + buf[0] = 0x8e; + buf[1] = devc->devid[1]; + if (scanaplus_write(devc, (uint8_t *)&buf, 2) < 0) + return SR_ERR; + + buf[0] = 0x8f; + buf[1] = devc->devid[2]; + if (scanaplus_write(devc, (uint8_t *)&buf, 2) < 0) + return SR_ERR; + + return SR_OK; +} + +SR_PRIV int scanaplus_init(struct dev_context *devc) +{ + int i; + uint8_t buf[8]; + + buf[0] = 0x88; + buf[1] = 0x41; + if (scanaplus_write(devc, (uint8_t *)&buf, 2) < 0) + return SR_ERR; + + buf[0] = 0x89; + buf[1] = 0x64; + buf[2] = 0x8a; + buf[3] = 0x64; + if (scanaplus_write(devc, (uint8_t *)&buf, 4) < 0) + return SR_ERR; + + buf[0] = 0x88; + buf[1] = 0x41; + if (scanaplus_write(devc, (uint8_t *)&buf, 2) < 0) + return SR_ERR; + + buf[0] = 0x88; + buf[1] = 0x40; + if (scanaplus_write(devc, (uint8_t *)&buf, 2) < 0) + return SR_ERR; + + buf[0] = 0x8d; + buf[1] = 0x01; + buf[2] = 0x8d; + buf[3] = 0x05; + buf[4] = 0x8d; + buf[5] = 0x01; + buf[6] = 0x8d; + buf[7] = 0x02; + if (scanaplus_write(devc, (uint8_t *)&buf, 8) < 0) + return SR_ERR; + + for (i = 0; i < 57; i++) { + buf[0] = 0x8d; + buf[1] = 0x06; + if (scanaplus_write(devc, (uint8_t *)&buf, 2) < 0) + return SR_ERR; + + buf[0] = 0x8d; + buf[1] = 0x02; + if (scanaplus_write(devc, (uint8_t *)&buf, 2) < 0) + return SR_ERR; + } + + if (scanaplus_send_device_id(devc) < 0) + return SR_ERR; + + buf[0] = 0x88; + buf[1] = 0x40; + if (scanaplus_write(devc, (uint8_t *)&buf, 2) < 0) + return SR_ERR; + + return SR_OK; +} + +SR_PRIV int scanaplus_start_acquisition(struct dev_context *devc) +{ + uint8_t buf[4]; + + /* Threshold and differential probe settings not yet implemented. */ + + buf[0] = 0x89; + buf[1] = 0x7f; /* Logic level threshold for probes 1-4. */ + buf[2] = 0x8a; + buf[3] = 0x7f; /* Logic level threshold for probes 5-9. */ + if (scanaplus_write(devc, (uint8_t *)&buf, 4) < 0) + return SR_ERR; + + buf[0] = 0x88; + buf[1] = 0x40; /* Special config of probes 5/6 and 7/8. */ + /* 0x40: normal, 0x50: ch56 diff, 0x48: ch78 diff, 0x58: ch5678 diff */ + if (scanaplus_write(devc, (uint8_t *)&buf, 2) < 0) + return SR_ERR; + + if (scanaplus_clear_device_id(devc) < 0) + return SR_ERR; + + if (scanaplus_send_device_id(devc) < 0) + return SR_ERR; + + return SR_OK; +} + +SR_PRIV int scanaplus_receive_data(int fd, int revents, void *cb_data) +{ + int bytes_read; + struct sr_dev_inst *sdi; struct dev_context *devc; + uint64_t max, n; (void)fd; + (void)revents; if (!(sdi = cb_data)) return TRUE; @@ -33,8 +307,63 @@ SR_PRIV int ikalogic_scanaplus_receive_data(int fd, int revents, void *cb_data) if (!(devc = sdi->priv)) return TRUE; - if (revents == G_IO_IN) { - /* TODO */ + if (!devc->ftdic) + return TRUE; + + /* Get a block of data. */ + bytes_read = ftdi_read_data(devc->ftdic, devc->compressed_buf, + COMPRESSED_BUF_SIZE); + if (bytes_read < 0) { + sr_err("Failed to read FTDI data (%d): %s.", + bytes_read, ftdi_get_error_string(devc->ftdic)); + sdi->driver->dev_acquisition_stop(sdi, sdi); + return FALSE; + } + if (bytes_read == 0) { + sr_spew("Received 0 bytes, nothing to do."); + return TRUE; + } + + /* + * After a ScanaPLUS acquisition starts, a bunch of samples will be + * returned as all-zero, no matter which signals are actually present + * on the probes. This is probably due to the FPGA reconfiguring some + * of its internal state/config during this time. + * + * As far as we know there is apparently no way for the PC-side to + * know when this "reconfiguration" starts or ends. The FTDI chip + * will return all-zero "dummy" samples during this time, which is + * indistinguishable from actual all-zero samples. + * + * We currently simply ignore the first 64kB of data after an + * acquisition starts. Empirical tests have shown that the + * "reconfigure" time is a lot less than that usually. + */ + if (devc->compressed_bytes_ignored < COMPRESSED_BUF_SIZE) { + /* Ignore the first 64kB of data of every acquisition. */ + sr_spew("Ignoring first 64kB chunk of data."); + devc->compressed_bytes_ignored += COMPRESSED_BUF_SIZE; + return TRUE; + } + + /* TODO: Handle bytes_read which is not a multiple of 2? */ + scanaplus_uncompress_block(devc, bytes_read); + + n = devc->samples_sent + (devc->bytes_received / 2); + max = (SR_MHZ(100) / 1000) * devc->limit_msec; + + if (devc->limit_samples && (n >= devc->limit_samples)) { + send_samples(devc, devc->limit_samples - devc->samples_sent); + sr_info("Requested number of samples reached."); + sdi->driver->dev_acquisition_stop(sdi, cb_data); + return TRUE; + } else if (devc->limit_msec && (n >= max)) { + send_samples(devc, max - devc->samples_sent); + sr_info("Requested time limit reached."); + sdi->driver->dev_acquisition_stop(sdi, cb_data); + return TRUE; + } else { + send_samples(devc, devc->bytes_received / 2); } return TRUE; diff --git a/hardware/ikalogic-scanaplus/protocol.h b/hardware/ikalogic-scanaplus/protocol.h index 578a0110..e736f838 100644 --- a/hardware/ikalogic-scanaplus/protocol.h +++ b/hardware/ikalogic-scanaplus/protocol.h @@ -22,12 +22,14 @@ #define LIBSIGROK_HARDWARE_IKALOGIC_SCANAPLUS_PROTOCOL_H #include +#include #include +#include #include "libsigrok.h" #include "libsigrok-internal.h" /* Message logging helpers with subsystem-specific prefix string. */ -#define LOG_PREFIX "ikalogic-scanaplus: " +#define LOG_PREFIX "scanaplus: " #define sr_log(l, s, args...) sr_log(l, LOG_PREFIX s, ## args) #define sr_spew(s, args...) sr_spew(LOG_PREFIX s, ## args) #define sr_dbg(s, args...) sr_dbg(LOG_PREFIX s, ## args) @@ -35,10 +37,35 @@ #define sr_warn(s, args...) sr_warn(LOG_PREFIX s, ## args) #define sr_err(s, args...) sr_err(LOG_PREFIX s, ## args) -/** Private, per-device-instance driver context. */ +#define COMPRESSED_BUF_SIZE (64 * 1024) + +/* Private, per-device-instance driver context. */ struct dev_context { + /** FTDI device context (used by libftdi). */ + struct ftdi_context *ftdic; + + /** The current sampling limit (in ms). */ + uint64_t limit_msec; + + /** The current sampling limit (in number of samples). */ + uint64_t limit_samples; + + void *cb_data; + + uint8_t *compressed_buf; + uint64_t compressed_bytes_ignored; + uint8_t *sample_buf; + uint64_t bytes_received; + uint64_t samples_sent; + + /** ScanaPLUS unique device ID (3 bytes). */ + uint8_t devid[3]; }; -SR_PRIV int ikalogic_scanaplus_receive_data(int fd, int revents, void *cb_data); +SR_PRIV int scanaplus_close(struct dev_context *devc); +SR_PRIV int scanaplus_get_device_id(struct dev_context *devc); +SR_PRIV int scanaplus_init(struct dev_context *devc); +SR_PRIV int scanaplus_start_acquisition(struct dev_context *devc); +SR_PRIV int scanaplus_receive_data(int fd, int revents, void *cb_data); #endif