]> sigrok.org Git - libsigrok.git/blobdiff - src/hardware/asix-sigma/protocol.c
asix-sigma: Stabilize channel assignment for different samplerates
[libsigrok.git] / src / hardware / asix-sigma / protocol.c
index 01dc1c18763c69382b522b8cc2638e5b3bc23677..f8289ba749d86491064bc94f75d0139a731c4851 100644 (file)
 #include <config.h>
 #include "protocol.h"
 
-#define USB_VENDOR                     0xa600
-#define USB_PRODUCT                    0xa000
-#define USB_DESCRIPTION                        "ASIX SIGMA"
-#define USB_VENDOR_NAME                        "ASIX"
-#define USB_MODEL_NAME                 "SIGMA"
-
 /*
  * The ASIX Sigma supports arbitrary integer frequency divider in
  * the 50MHz mode. The divider is in range 1...256 , allowing for
@@ -51,7 +45,7 @@ SR_PRIV const uint64_t samplerates[] = {
        SR_MHZ(200),    /* Special FW needed */
 };
 
-SR_PRIV const int SAMPLERATES_COUNT = ARRAY_SIZE(samplerates);
+SR_PRIV const size_t samplerates_count = ARRAY_SIZE(samplerates);
 
 static const char sigma_firmware_files[][24] = {
        /* 50 MHz, supports 8 bit fractions */
@@ -105,16 +99,16 @@ SR_PRIV int sigma_write_register(uint8_t reg, uint8_t *data, size_t len,
        uint8_t buf[80];
        int idx = 0;
 
-       if ((len + 2) > sizeof(buf)) {
+       if ((2 * len + 2) > sizeof(buf)) {
                sr_err("Attempted to write %zu bytes, but buffer is too small.",
-                      len + 2);
+                      len);
                return SR_ERR_BUG;
        }
 
        buf[idx++] = REG_ADDR_LOW | (reg & 0xf);
        buf[idx++] = REG_ADDR_HIGH | (reg >> 4);
 
-       for (i = 0; i < len; ++i) {
+       for (i = 0; i < len; i++) {
                buf[idx++] = REG_DATA_LOW | (data[i] & 0xf);
                buf[idx++] = REG_DATA_HIGH_WRITE | (data[i] >> 4);
        }
@@ -201,7 +195,7 @@ static int sigma_read_dram(uint16_t startchunk, size_t numchunks,
        buf[idx++] = REG_DRAM_BLOCK;
        buf[idx++] = REG_DRAM_WAIT_ACK;
 
-       for (i = 0; i < numchunks; ++i) {
+       for (i = 0; i < numchunks; i++) {
                /* Alternate bit to copy from DRAM to cache. */
                if (i != (numchunks - 1))
                        buf[idx++] = REG_DRAM_BLOCK | (((i + 1) % 2) << 4);
@@ -225,7 +219,7 @@ SR_PRIV int sigma_write_trigger_lut(struct triggerlut *lut, struct dev_context *
        uint16_t bit;
 
        /* Transpose the table and send to Sigma. */
-       for (i = 0; i < 16; ++i) {
+       for (i = 0; i < 16; i++) {
                bit = 1 << i;
 
                tmp[0] = tmp[1] = 0;
@@ -388,12 +382,13 @@ static int sigma_fw_2_bitbang(struct sr_context *ctx, const char *name,
        int bit, v;
        int ret = SR_OK;
 
+       /* Retrieve the on-disk firmware file content. */
        firmware = sr_resource_load(ctx, SR_RESOURCE_FIRMWARE,
                        name, &file_size, 256 * 1024);
        if (!firmware)
                return SR_ERR;
 
-       /* Weird magic transformation below, I have no idea what it does. */
+       /* Unscramble the file content (XOR with "random" sequence). */
        imm = 0x3f6df2ab;
        for (i = 0; i < file_size; i++) {
                imm = (imm + 0xa853753) % 177 + (imm * 0x8034052);
@@ -401,13 +396,20 @@ static int sigma_fw_2_bitbang(struct sr_context *ctx, const char *name,
        }
 
        /*
-        * Now that the firmware is "transformed", we will transcribe the
-        * firmware blob into a sequence of toggles of the Dx wires. This
-        * sequence will be fed directly into the Sigma, which must be in
-        * the FPGA bitbang programming mode.
+        * Generate a sequence of bitbang samples. With two samples per
+        * FPGA configuration bit, providing the level for the DIN signal
+        * as well as two edges for CCLK. See Xilinx UG332 for details
+        * ("slave serial" mode).
+        *
+        * Note that CCLK is inverted in hardware. That's why the
+        * respective bit is first set and then cleared in the bitbang
+        * sample sets. So that the DIN level will be stable when the
+        * data gets sampled at the rising CCLK edge, and the signals'
+        * setup time constraint will be met.
+        *
+        * The caller will put the FPGA into download mode, will send
+        * the bitbang samples, and release the allocated memory.
         */
-
-       /* Each bit of firmware is transcribed as two toggles of Dx wires. */
        bb_size = file_size * 8 * 2;
        bb_stream = (uint8_t *)g_try_malloc(bb_size);
        if (!bb_stream) {
@@ -415,7 +417,6 @@ static int sigma_fw_2_bitbang(struct sr_context *ctx, const char *name,
                ret = SR_ERR_MALLOC;
                goto exit;
        }
-
        bbs = bb_stream;
        for (i = 0; i < file_size; i++) {
                for (bit = 7; bit >= 0; bit--) {
@@ -512,24 +513,54 @@ static int upload_firmware(struct sr_context *ctx,
        return SR_OK;
 }
 
+/*
+ * Sigma doesn't support limiting the number of samples, so we have to
+ * translate the number and the samplerate to an elapsed time.
+ *
+ * In addition we need to ensure that the last data cluster has passed
+ * the hardware pipeline, and became available to the PC side. With RLE
+ * compression up to 327ms could pass before another cluster accumulates
+ * at 200kHz samplerate when input pins don't change.
+ */
+SR_PRIV uint64_t sigma_limit_samples_to_msec(const struct dev_context *devc,
+                                            uint64_t limit_samples)
+{
+       uint64_t limit_msec;
+       uint64_t worst_cluster_time_ms;
+
+       limit_msec = limit_samples * 1000 / devc->cur_samplerate;
+       worst_cluster_time_ms = 65536 * 1000 / devc->cur_samplerate;
+       /*
+        * One cluster time is not enough to flush pipeline when sampling
+        * grounded pins with 1 sample limit at 200kHz. Hence the 2* fix.
+        */
+       return limit_msec + 2 * worst_cluster_time_ms;
+}
+
 SR_PRIV int sigma_set_samplerate(const struct sr_dev_inst *sdi, uint64_t samplerate)
 {
        struct dev_context *devc;
        struct drv_context *drvc;
-       unsigned int i;
+       size_t i;
        int ret;
 
        devc = sdi->priv;
        drvc = sdi->driver->context;
        ret = SR_OK;
 
-       for (i = 0; i < ARRAY_SIZE(samplerates); i++) {
+       /* Reject rates that are not in the list of supported rates. */
+       for (i = 0; i < samplerates_count; i++) {
                if (samplerates[i] == samplerate)
                        break;
        }
-       if (samplerates[i] == 0)
+       if (i >= samplerates_count || samplerates[i] == 0)
                return SR_ERR_SAMPLERATE;
 
+       /*
+        * Depending on the samplerates of 200/100/50- MHz, specific
+        * firmware is required and higher rates might limit the set
+        * of available channels.
+        */
        if (samplerate <= SR_MHZ(50)) {
                ret = upload_firmware(drvc->sr_ctx, 0, devc);
                devc->num_channels = 16;
@@ -541,6 +572,11 @@ SR_PRIV int sigma_set_samplerate(const struct sr_dev_inst *sdi, uint64_t sampler
                devc->num_channels = 4;
        }
 
+       /*
+        * Derive the sample period from the sample rate as well as the
+        * number of samples that the device will communicate within
+        * an "event" (memory organization internal to the device).
+        */
        if (ret == SR_OK) {
                devc->cur_samplerate = samplerate;
                devc->period_ps = 1000000000000ULL / samplerate;
@@ -548,6 +584,18 @@ SR_PRIV int sigma_set_samplerate(const struct sr_dev_inst *sdi, uint64_t sampler
                devc->state.state = SIGMA_IDLE;
        }
 
+       /*
+        * Support for "limit_samples" is implemented by stopping
+        * acquisition after a corresponding period of time.
+        * Re-calculate that period of time, in case the limit is
+        * set first and the samplerate gets (re-)configured later.
+        */
+       if (ret == SR_OK && devc->limit_samples) {
+               uint64_t msecs;
+               msecs = sigma_limit_samples_to_msec(devc, devc->limit_samples);
+               devc->limit_msec = msecs;
+       }
+
        return ret;
 }
 
@@ -599,7 +647,7 @@ SR_PRIV int sigma_convert_trigger(const struct sr_dev_inst *sdi)
                                        return SR_ERR;
                                }
 
-                               ++trigger_set;
+                               trigger_set++;
                        } else {
                                /* Simple trigger support (event). */
                                if (match->match == SR_TRIGGER_ONE) {
@@ -612,11 +660,11 @@ SR_PRIV int sigma_convert_trigger(const struct sr_dev_inst *sdi)
                                }
                                else if (match->match == SR_TRIGGER_FALLING) {
                                        devc->trigger.fallingmask |= channelbit;
-                                       ++trigger_set;
+                                       trigger_set++;
                                }
                                else if (match->match == SR_TRIGGER_RISING) {
                                        devc->trigger.risingmask |= channelbit;
-                                       ++trigger_set;
+                                       trigger_set++;
                                }
 
                                /*
@@ -644,7 +692,7 @@ static int get_trigger_offset(uint8_t *samples, uint16_t last_sample,
        int i;
        uint16_t sample = 0;
 
-       for (i = 0; i < 8; ++i) {
+       for (i = 0; i < 8; i++) {
                if (i > 0)
                        last_sample = sample;
                sample = samples[2 * i] | (samples[2 * i + 1] << 8);
@@ -678,6 +726,26 @@ static uint16_t sigma_dram_cluster_ts(struct sigma_dram_cluster *cluster)
        return (cluster->timestamp_hi << 8) | cluster->timestamp_lo;
 }
 
+/*
+ * Return one 16bit data entity of a DRAM cluster at the specified index.
+ */
+static uint16_t sigma_dram_cluster_data(struct sigma_dram_cluster *cl, int idx)
+{
+       uint16_t sample;
+
+       sample = 0;
+       sample |= cl->samples[idx].sample_lo << 0;
+       sample |= cl->samples[idx].sample_hi << 8;
+       sample = (sample >> 8) | (sample << 8);
+       return sample;
+}
+
+static void store_sr_sample(uint8_t *samples, int idx, uint16_t data)
+{
+       samples[2 * idx + 0] = (data >> 0) & 0xff;
+       samples[2 * idx + 1] = (data >> 8) & 0xff;
+}
+
 static void sigma_decode_dram_cluster(struct sigma_dram_cluster *dram_cluster,
                                      unsigned int events_in_cluster,
                                      unsigned int triggered,
@@ -687,13 +755,13 @@ static void sigma_decode_dram_cluster(struct sigma_dram_cluster *dram_cluster,
        struct sigma_state *ss = &devc->state;
        struct sr_datafeed_packet packet;
        struct sr_datafeed_logic logic;
-       uint16_t tsdiff, ts;
+       uint16_t tsdiff, ts, sample;
        uint8_t samples[2048];
        unsigned int i;
 
        ts = sigma_dram_cluster_ts(dram_cluster);
        tsdiff = ts - ss->lastts;
-       ss->lastts = ts;
+       ss->lastts = ts + EVENTS_PER_CLUSTER;
 
        packet.type = SR_DF_LOGIC;
        packet.payload = &logic;
@@ -711,17 +779,16 @@ static void sigma_decode_dram_cluster(struct sigma_dram_cluster *dram_cluster,
         * sample in the cluster happens at the time of the timestamp
         * and the remaining samples happen at timestamp +1...+6 .
         */
-       for (ts = 0; ts < tsdiff - (EVENTS_PER_CLUSTER - 1); ts++) {
+       for (ts = 0; ts < tsdiff; ts++) {
                i = ts % 1024;
-               samples[2 * i + 0] = ss->lastsample & 0xff;
-               samples[2 * i + 1] = ss->lastsample >> 8;
+               store_sr_sample(samples, i, ss->lastsample);
 
                /*
                 * If we have 1024 samples ready or we're at the
                 * end of submitting the padding samples, submit
                 * the packet to Sigrok.
                 */
-               if ((i == 1023) || (ts == (tsdiff - EVENTS_PER_CLUSTER))) {
+               if ((i == 1023) || (ts == tsdiff - 1)) {
                        logic.length = (i + 1) * logic.unitsize;
                        sr_session_send(sdi, &packet);
                }
@@ -731,12 +798,17 @@ static void sigma_decode_dram_cluster(struct sigma_dram_cluster *dram_cluster,
         * Parse the samples in current cluster and prepare them
         * to be submitted to Sigrok.
         */
+       sample = 0;
        for (i = 0; i < events_in_cluster; i++) {
-               samples[2 * i + 1] = dram_cluster->samples[i].sample_lo;
-               samples[2 * i + 0] = dram_cluster->samples[i].sample_hi;
+               sample = sigma_dram_cluster_data(dram_cluster, i);
+               store_sr_sample(samples, i, sample);
        }
 
-       /* Send data up to trigger point (if triggered). */
+       /*
+        * If a trigger position applies, then provide the datafeed with
+        * the first part of data up to that position, then send the
+        * trigger marker.
+        */
        int trigger_offset = 0;
        if (triggered) {
                /*
@@ -762,6 +834,10 @@ static void sigma_decode_dram_cluster(struct sigma_dram_cluster *dram_cluster,
                }
        }
 
+       /*
+        * Send the data after the trigger, or all of the received data
+        * if no trigger position applies.
+        */
        if (events_in_cluster > 0) {
                packet.type = SR_DF_LOGIC;
                logic.length = events_in_cluster * logic.unitsize;
@@ -769,10 +845,7 @@ static void sigma_decode_dram_cluster(struct sigma_dram_cluster *dram_cluster,
                sr_session_send(sdi, &packet);
        }
 
-       ss->lastsample =
-               samples[2 * (events_in_cluster - 1) + 0] |
-               (samples[2 * (events_in_cluster - 1) + 1] << 8);
-
+       ss->lastsample = sample;
 }
 
 /*
@@ -835,7 +908,6 @@ static int download_capture(struct sr_dev_inst *sdi)
        struct sigma_dram_line *dram_line;
        int bufsz;
        uint32_t stoppos, triggerpos;
-       struct sr_datafeed_packet packet;
        uint8_t modestatus;
 
        uint32_t i;
@@ -907,11 +979,9 @@ static int download_capture(struct sr_dev_inst *sdi)
                dl_lines_done += dl_lines_curr;
        }
 
-       /* All done. */
-       packet.type = SR_DF_END;
-       sr_session_send(sdi, &packet);
+       std_session_send_df_end(sdi);
 
-       sdi->driver->dev_acquisition_stop(sdi, sdi);
+       sdi->driver->dev_acquisition_stop(sdi);
 
        g_free(dram_line);
 
@@ -976,20 +1046,19 @@ static void build_lut_entry(uint16_t value, uint16_t mask, uint16_t *entry)
        int i, j, k, bit;
 
        /* For each quad channel. */
-       for (i = 0; i < 4; ++i) {
+       for (i = 0; i < 4; i++) {
                entry[i] = 0xffff;
 
                /* For each bit in LUT. */
-               for (j = 0; j < 16; ++j)
+               for (j = 0; j < 16; j++)
 
                        /* For each channel in quad. */
-                       for (k = 0; k < 4; ++k) {
+                       for (k = 0; k < 4; k++) {
                                bit = 1 << (i * 4 + k);
 
                                /* Set bit in entry */
-                               if ((mask & bit) &&
-                                   ((!(value & bit)) !=
-                                   (!(j & (1 << k)))))
+                               if ((mask & bit) && ((!(value & bit)) !=
+                                                       (!(j & (1 << k)))))
                                        entry[i] &= ~(1 << j);
                        }
        }
@@ -1042,17 +1111,17 @@ static void add_trigger_function(enum triggerop oper, enum triggerfunc func,
 
        /* Transpose if neg is set. */
        if (neg) {
-               for (i = 0; i < 2; ++i) {
-                       for (j = 0; j < 2; ++j) {
+               for (i = 0; i < 2; i++) {
+                       for (j = 0; j < 2; j++) {
                                tmp = x[i][j];
-                               x[i][j] = x[1-i][1-j];
-                               x[1-i][1-j] = tmp;
+                               x[i][j] = x[1 - i][1 - j];
+                               x[1 - i][1 - j] = tmp;
                        }
                }
        }
 
        /* Update mask with function. */
-       for (i = 0; i < 16; ++i) {
+       for (i = 0; i < 16; i++) {
                a = (i >> (2 * index + 0)) & 1;
                b = (i >> (2 * index + 1)) & 1;
 
@@ -1097,7 +1166,7 @@ SR_PRIV int sigma_build_basic_trigger(struct triggerlut *lut, struct dev_context
                        lut->m2d);
 
        /* Rise/fall trigger support. */
-       for (i = 0, j = 0; i < 16; ++i) {
+       for (i = 0, j = 0; i < 16; i++) {
                if (devc->trigger.risingmask & (1 << i) ||
                    devc->trigger.fallingmask & (1 << i))
                        masks[j++] = 1 << i;