]> sigrok.org Git - libsigrok.git/blobdiff - hardware/asix-sigma/asix-sigma.c
update plugins and cli to use new DF_HEADER
[libsigrok.git] / hardware / asix-sigma / asix-sigma.c
index 0dbd38eb744308e21b3515f9ff02d5438010597c..d4d9fddfdaf6a765ffc50c52d134c832b8008549 100644 (file)
@@ -48,14 +48,8 @@ static int cur_firmware = -1;
 static int num_probes = 0;
 static int samples_per_event = 0;
 static int capture_ratio = 50;
-
-/* Single-pin trigger support (100 and 200 MHz).*/
-static uint8_t triggerpin = 1;
-static uint8_t triggerfall = 0;
-
-/* Simple trigger support (<= 50 MHz). */
-static uint16_t triggermask = 1;
-static uint16_t triggervalue = 1;
+static struct sigma_trigger trigger;
+static struct sigma_state sigma;
 
 static uint64_t supported_samplerates[] = {
        KHZ(200),
@@ -112,6 +106,8 @@ static const char *firmware_files[] = {
        "asix-sigma-phasor.fw", /* Frequency counter */
 };
 
+static void hw_stop_acquisition(int device_index, gpointer session_device_id);
+
 static int sigma_read(void *buf, size_t size)
 {
        int ret;
@@ -249,7 +245,7 @@ static int sigma_read_dram(uint16_t startchunk, size_t numchunks, uint8_t *data)
        return sigma_read(data, numchunks * CHUNK_SIZE);
 }
 
-/* Upload trigger look-up tables to Sigma */
+/* Upload trigger look-up tables to Sigma. */
 static int sigma_write_trigger_lut(struct triggerlut *lut)
 {
        int i;
@@ -536,7 +532,8 @@ static int hw_opendev(int device_index)
        return SIGROK_OK;
 }
 
-static int set_samplerate(struct sigrok_device_instance *sdi, uint64_t samplerate)
+static int set_samplerate(struct sigrok_device_instance *sdi,
+                         uint64_t samplerate)
 {
        int i, ret;
 
@@ -564,39 +561,49 @@ static int set_samplerate(struct sigrok_device_instance *sdi, uint64_t samplerat
 
        cur_samplerate = samplerate;
        samples_per_event = 16 / num_probes;
+       sigma.state = SIGMA_IDLE;
 
        g_message("Firmware uploaded");
 
        return ret;
 }
 
-/* Only trigger on single pin supported (in 100-200 MHz modes). */
+/*
+ * In 100 and 200 MHz mode, only a single pin rising/falling can be
+ * set as trigger. In other modes, two rising/falling triggers can be set,
+ * in addition to value/mask trigger for any number of probes.
+ *
+ * The Sigma supports complex triggers using boolean expressions, but this
+ * has not been implemented yet.
+ */
 static int configure_probes(GSList *probes)
 {
        struct probe *probe;
        GSList *l;
        int trigger_set = 0;
+       int probebit;
 
-       triggermask = 0;
-       triggervalue = 0;
+       memset(&trigger, 0, sizeof(struct sigma_trigger));
 
        for (l = probes; l; l = l->next) {
                probe = (struct probe *)l->data;
+               probebit = 1 << (probe->index - 1);
 
                if (!probe->enabled || !probe->trigger)
                        continue;
 
                if (cur_samplerate >= MHZ(100)) {
-                       /* Fast trigger support */
+                       /* Fast trigger support. */
                        if (trigger_set) {
-                               g_warning("Asix Sigma only supports a single pin trigger "
-                                               "in 100 and 200 MHz mode.");
+                               g_warning("Asix Sigma only supports a single "
+                                               "pin trigger in 100 and 200 "
+                                               "MHz mode.");
                                return SIGROK_ERR;
                        }
                        if (probe->trigger[0] == 'f')
-                               triggerfall = 1;
+                               trigger.fallingmask |= probebit;
                        else if (probe->trigger[0] == 'r')
-                               triggerfall = 0;
+                               trigger.risingmask |= probebit;
                        else {
                                g_warning("Asix Sigma only supports "
                                          "rising/falling trigger in 100 "
@@ -604,25 +611,32 @@ static int configure_probes(GSList *probes)
                                return SIGROK_ERR;
                        }
 
-                       triggerpin = probe->index - 1;
+                       ++trigger_set;
                } else {
-                       /* Normal trigger support */
-                       triggermask |= 1 << (probe->index - 1);
+                       /* Simple trigger support (event). */
+                       if (probe->trigger[0] == '1') {
+                               trigger.simplevalue |= probebit;
+                               trigger.simplemask |= probebit;
+                       }
+                       else if (probe->trigger[0] == '0') {
+                               trigger.simplevalue &= ~probebit;
+                               trigger.simplemask |= probebit;
+                       }
+                       else if (probe->trigger[0] == 'f') {
+                               trigger.fallingmask |= probebit;
+                               ++trigger_set;
+                       }
+                       else if (probe->trigger[0] == 'r') {
+                               trigger.risingmask |= probebit;
+                               ++trigger_set;
+                       }
 
-                       if (probe->trigger[0] == '1')
-                               triggervalue |= 1 << (probe->index - 1);
-                       else if (probe->trigger[0] == '0')
-                               triggervalue |= 0 << (probe->index - 1);
-                       else {
-                               g_warning("Asix Sigma only supports "
-                                         "trigger values in <= 50"
-                                         " MHz mode.");
+                       if (trigger_set > 2) {
+                               g_warning("Asix Sigma only supports 2 rising/"
+                                         "falling triggers.");
                                return SIGROK_ERR;
                        }
-
                }
-
-               ++trigger_set;
        }
 
        return SIGROK_OK;
@@ -713,6 +727,37 @@ static int hw_set_configuration(int device_index, int capability, void *value)
        return ret;
 }
 
+/* Software trigger to determine exact trigger position. */
+static int get_trigger_offset(uint16_t *samples, uint16_t last_sample,
+                             struct sigma_trigger *t)
+{
+       int i;
+
+       for (i = 0; i < 8; ++i) {
+               if (i > 0)
+                       last_sample = samples[i-1];
+
+               /* Simple triggers. */
+               if ((samples[i] & t->simplemask) != t->simplevalue)
+                       continue;
+
+               /* Rising edge. */
+               if ((last_sample & t->risingmask) != 0 || (samples[i] &
+                   t->risingmask) != t->risingmask)
+                       continue;
+
+               /* Falling edge. */
+               if ((last_sample & t->fallingmask) != t->fallingmask ||
+                   (samples[i] & t->fallingmask) != 0)
+                       continue;
+
+               break;
+       }
+
+       /* If we did not match, return original trigger pos. */
+       return i & 0x7;
+}
+
 /*
  * Decode chunk of 1024 bytes, 64 clusters, 7 events per cluster.
  * Each event is 20ns apart, and can contain multiple samples.
@@ -734,13 +779,11 @@ static int decode_chunk_ts(uint8_t *buf, uint16_t *lastts,
        uint16_t *event;
        uint16_t cur_sample;
        int triggerts = -1;
-       int triggeroff = 0;
 
+       /* Check if trigger is in this chunk. */
        if (triggerpos != -1) {
                if (cur_samplerate <= MHZ(50))
-                       triggerpos -= EVENTS_PER_CLUSTER;
-               else
-                       triggeroff = 3;
+                       triggerpos -= EVENTS_PER_CLUSTER - 1;
 
                if (triggerpos < 0)
                        triggerpos = 0;
@@ -769,8 +812,9 @@ static int decode_chunk_ts(uint8_t *buf, uint16_t *lastts,
                while (sent < n) {
                        tosend = MIN(2048, n - sent);
 
-                       packet.type = DF_LOGIC16;
+                       packet.type = DF_LOGIC;
                        packet.length = tosend * sizeof(uint16_t);
+                       packet.unitsize = 2;
                        packet.payload = samples + sent;
                        session_bus(user_data, &packet);
 
@@ -802,16 +846,18 @@ static int decode_chunk_ts(uint8_t *buf, uint16_t *lastts,
                sent = 0;
                if (i == triggerts) {
                        /*
-                        * Trigger is presumptively only accurate to event, i.e.
-                        * for 100 and 200 MHz, where multiple samples are coded
-                        * in a single event, the trigger does not match the
-                        * exact sample.
+                        * Trigger is not always accurate to sample because of
+                        * pipeline delay. However, it always triggers before
+                        * the actual event. We therefore look at the next
+                        * samples to pinpoint the exact position of the trigger.
                         */
-                       tosend = (triggerpos % 7) - triggeroff;
+                       tosend = get_trigger_offset(samples, *lastsample,
+                                                   &trigger);
 
                        if (tosend > 0) {
-                               packet.type = DF_LOGIC16;
+                               packet.type = DF_LOGIC;
                                packet.length = tosend * sizeof(uint16_t);
+                               packet.unitsize = 2;
                                packet.payload = samples;
                                session_bus(user_data, &packet);
 
@@ -827,8 +873,9 @@ static int decode_chunk_ts(uint8_t *buf, uint16_t *lastts,
                /* Send rest of the chunk to sigrok. */
                tosend = n - sent;
 
-               packet.type = DF_LOGIC16;
+               packet.type = DF_LOGIC;
                packet.length = tosend * sizeof(uint16_t);
+               packet.unitsize = 2;
                packet.payload = samples + sent;
                session_bus(user_data, &packet);
 
@@ -843,99 +890,86 @@ static int receive_data(int fd, int revents, void *user_data)
        struct datafeed_packet packet;
        const int chunks_per_read = 32;
        unsigned char buf[chunks_per_read * CHUNK_SIZE];
-       int bufsz, numchunks, curchunk, i, newchunks;
-       uint32_t triggerpos, stoppos, running_msec;
+       int bufsz, numchunks, i, newchunks;
+       uint32_t running_msec;
        struct timeval tv;
-       uint16_t lastts = 0;
-       uint16_t lastsample = 0;
-       uint8_t modestatus;
-       int triggerchunk = -1;
 
        fd = fd;
        revents = revents;
 
-       /* Get the current position. */
-       sigma_read_pos(&stoppos, &triggerpos);
-       numchunks = stoppos / 512;
-
-       /* Check if the has expired, or memory is full. */
-       gettimeofday(&tv, 0);
-       running_msec = (tv.tv_sec - start_tv.tv_sec) * 1000 +
-                      (tv.tv_usec - start_tv.tv_usec) / 1000;
+       numchunks = sigma.stoppos / 512;
 
-       if (running_msec < limit_msec && numchunks < 32767)
+       if (sigma.state == SIGMA_IDLE)
                return FALSE;
 
-       /* Stop acqusition. */
-       sigma_set_register(WRITE_MODE, 0x11);
+       if (sigma.state == SIGMA_CAPTURE) {
 
-       /* Set SDRAM Read Enable. */
-       sigma_set_register(WRITE_MODE, 0x02);
+               /* Check if the timer has expired, or memory is full. */
+               gettimeofday(&tv, 0);
+               running_msec = (tv.tv_sec - start_tv.tv_sec) * 1000 +
+                       (tv.tv_usec - start_tv.tv_usec) / 1000;
 
-       /* Get the current position. */
-       sigma_read_pos(&stoppos, &triggerpos);
+               if (running_msec < limit_msec && numchunks < 32767)
+                       return FALSE;
 
-       /* Check if trigger has fired. */
-       modestatus = sigma_get_register(READ_MODE);
-       if (modestatus & 0x20) {
-               triggerchunk = triggerpos / 512;
-       }
+               hw_stop_acquisition(-1, user_data);
+
+               return FALSE;
+
+       } else if (sigma.state == SIGMA_DOWNLOAD) {
+               if (sigma.chunks_downloaded >= numchunks) {
+                       /* End of samples. */
+                       packet.type = DF_END;
+                       packet.length = 0;
+                       session_bus(user_data, &packet);
 
-       /* Download sample data. */
-       for (curchunk = 0; curchunk < numchunks;) {
-               newchunks = MIN(chunks_per_read, numchunks - curchunk);
+                       sigma.state = SIGMA_IDLE;
+
+                       return TRUE;
+               }
+
+               newchunks = MIN(chunks_per_read,
+                               numchunks - sigma.chunks_downloaded);
 
                g_message("Downloading sample data: %.0f %%",
-                         100.0 * curchunk / numchunks);
+                         100.0 * sigma.chunks_downloaded / numchunks);
 
-               bufsz = sigma_read_dram(curchunk, newchunks, buf);
+               bufsz = sigma_read_dram(sigma.chunks_downloaded,
+                                       newchunks, buf);
 
                /* Find first ts. */
-               if (curchunk == 0)
-                       lastts = *(uint16_t *) buf - 1;
+               if (sigma.chunks_downloaded == 0) {
+                       sigma.lastts = *(uint16_t *) buf - 1;
+                       sigma.lastsample = 0;
+               }
 
                /* Decode chunks and send them to sigrok. */
                for (i = 0; i < newchunks; ++i) {
-                       if (curchunk + i == triggerchunk)
+                       if (sigma.chunks_downloaded + i == sigma.triggerchunk)
                                decode_chunk_ts(buf + (i * CHUNK_SIZE),
-                                               &lastts, &lastsample,
-                                               triggerpos & 0x1ff, user_data);
+                                               &sigma.lastts, &sigma.lastsample,
+                                               sigma.triggerpos & 0x1ff,
+                                               user_data);
                        else
                                decode_chunk_ts(buf + (i * CHUNK_SIZE),
-                                               &lastts, &lastsample,
+                                               &sigma.lastts, &sigma.lastsample,
                                                -1, user_data);
                }
 
-               curchunk += newchunks;
+               sigma.chunks_downloaded += newchunks;
        }
 
-       /* End of data. */
-       packet.type = DF_END;
-       packet.length = 0;
-       session_bus(user_data, &packet);
-
        return TRUE;
 }
 
-/*
- * Build trigger LUTs used by 50 MHz and lower sample rates for supporting
- * simple pin change and state triggers. Only two transitions (rise/fall) can be
- * set at any time, but a full mask and value can be set (0/1).
- */
-static int build_basic_trigger(struct triggerlut *lut)
+/* Build a LUT entry used by the trigger functions. */
+static void build_lut_entry(uint16_t value, uint16_t mask, uint16_t *entry)
 {
        int i, j, k, bit;
 
-       memset(lut, 0, sizeof(struct triggerlut));
-
-       /* Unknown */
-       lut->m4 = 0xa000;
-
-       /* Set the LUT for controlling value/maske trigger */
-
        /* For each quad probe. */
        for (i = 0; i < 4; ++i) {
-               lut->m2d[i] = 0xffff;
+               entry[i] = 0xffff;
 
                /* For each bit in LUT. */
                for (j = 0; j < 16; ++j)
@@ -944,17 +978,140 @@ static int build_basic_trigger(struct triggerlut *lut)
                        for (k = 0; k < 4; ++k) {
                                bit = 1 << (i * 4 + k);
 
-                               if ((triggermask & bit) &&
-                                   ((!(triggervalue & bit)) !=
-                                    (!(j & (1 << k)))))
-                                       lut->m2d[i] &= ~(1 << j);
+                               /* Set bit in entry */
+                               if ((mask & bit) &&
+                                   ((!(value & bit)) !=
+                                   (!(j & (1 << k)))))
+                                       entry[i] &= ~(1 << j);
+                       }
+       }
+}
+
+/* Add a logical function to LUT mask. */
+static void add_trigger_function(enum triggerop oper, enum triggerfunc func,
+                                int index, int neg, uint16_t *mask)
+{
+       int i, j;
+       int x[2][2], tmp, a, b, aset, bset, rset;
+
+       memset(x, 0, 4 * sizeof(int));
+
+       /* Trigger detect condition. */
+       switch (oper) {
+       case OP_LEVEL:
+               x[0][1] = 1;
+               x[1][1] = 1;
+               break;
+       case OP_NOT:
+               x[0][0] = 1;
+               x[1][0] = 1;
+               break;
+       case OP_RISE:
+               x[0][1] = 1;
+               break;
+       case OP_FALL:
+               x[1][0] = 1;
+               break;
+       case OP_RISEFALL:
+               x[0][1] = 1;
+               x[1][0] = 1;
+               break;
+       case OP_NOTRISE:
+               x[1][1] = 1;
+               x[0][0] = 1;
+               x[1][0] = 1;
+               break;
+       case OP_NOTFALL:
+               x[1][1] = 1;
+               x[0][0] = 1;
+               x[0][1] = 1;
+               break;
+       case OP_NOTRISEFALL:
+               x[1][1] = 1;
+               x[0][0] = 1;
+               break;
+       }
+
+       /* Transpose if neg is set. */
+       if (neg) {
+               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;
                        }
        }
 
-       /* Unused when not triggering on transitions */
-       lut->m3 = 0xffff;
+       /* Update mask with function. */
+       for (i = 0; i < 16; ++i) {
+               a = (i >> (2 * index + 0)) & 1;
+               b = (i >> (2 * index + 1)) & 1;
+
+               aset = (*mask >> i) & 1;
+               bset = x[b][a];
+
+               if (func == FUNC_AND || func == FUNC_NAND)
+                       rset = aset & bset;
+               else if (func == FUNC_OR || func == FUNC_NOR)
+                       rset = aset | bset;
+               else if (func == FUNC_XOR || func == FUNC_NXOR)
+                       rset = aset ^ bset;
+
+               if (func == FUNC_NAND || func == FUNC_NOR || func == FUNC_NXOR)
+                       rset = !rset;
+
+               *mask &= ~(1 << i);
+
+               if (rset)
+                       *mask |= 1 << i;
+       }
+}
+
+/*
+ * Build trigger LUTs used by 50 MHz and lower sample rates for supporting
+ * simple pin change and state triggers. Only two transitions (rise/fall) can be
+ * set at any time, but a full mask and value can be set (0/1).
+ */
+static int build_basic_trigger(struct triggerlut *lut)
+{
+       int i,j;
+       uint16_t masks[2] = { 0, 0 };
+
+       memset(lut, 0, sizeof(struct triggerlut));
+
+       /* Contant for simple triggers. */
+       lut->m4 = 0xa000;
+
+       /* Value/mask trigger support. */
+       build_lut_entry(trigger.simplevalue, trigger.simplemask, lut->m2d);
+
+       /* Rise/fall trigger support. */
+       for (i = 0, j = 0; i < 16; ++i) {
+               if (trigger.risingmask & (1 << i) ||
+                   trigger.fallingmask & (1 << i))
+                       masks[j++] = 1 << i;
+       }
+
+       build_lut_entry(masks[0], masks[0], lut->m0d);
+       build_lut_entry(masks[1], masks[1], lut->m1d);
+
+       /* Add glue logic */
+       if (masks[0] || masks[1]) {
+               /* Transition trigger. */
+               if (masks[0] & trigger.risingmask)
+                       add_trigger_function(OP_RISE, FUNC_OR, 0, 0, &lut->m3);
+               if (masks[0] & trigger.fallingmask)
+                       add_trigger_function(OP_FALL, FUNC_OR, 0, 0, &lut->m3);
+               if (masks[1] & trigger.risingmask)
+                       add_trigger_function(OP_RISE, FUNC_OR, 1, 0, &lut->m3);
+               if (masks[1] & trigger.fallingmask)
+                       add_trigger_function(OP_FALL, FUNC_OR, 1, 0, &lut->m3);
+       } else {
+               /* Only value/mask trigger. */
+               lut->m3 = 0xffff;
+       }
 
-       /* Triggertype: event */
+       /* Triggertype: event. */
        lut->params.selres = 3;
 
        return SIGROK_OK;
@@ -970,6 +1127,7 @@ static int hw_start_acquisition(int device_index, gpointer session_device_id)
        uint8_t triggerselect;
        struct triggerinout triggerinout_conf;
        struct triggerlut lut;
+       int triggerpin;
 
        session_device_id = session_device_id;
 
@@ -989,8 +1147,18 @@ static int hw_start_acquisition(int device_index, gpointer session_device_id)
        if (cur_samplerate >= MHZ(100)) {
                sigma_set_register(WRITE_TRIGGER_SELECT1, 0x81);
 
-               triggerselect = (1 << LEDSEL1) | (triggerfall << 3) |
-                                       (triggerpin & 0x7);
+               /* Find which pin to trigger on from mask. */
+               for (triggerpin = 0; triggerpin < 8; ++triggerpin)
+                       if ((trigger.risingmask | trigger.fallingmask) &
+                           (1 << triggerpin))
+                               break;
+
+               /* Set trigger pin and light LED on trigger. */
+               triggerselect = (1 << LEDSEL1) | (triggerpin & 0x7);
+
+               /* Default rising edge. */
+               if (trigger.fallingmask)
+                       triggerselect |= 1 << 3;
 
        /* All other modes. */
        } else if (cur_samplerate <= MHZ(50)) {
@@ -1037,7 +1205,7 @@ static int hw_start_acquisition(int device_index, gpointer session_device_id)
        }
 
        /* Setup maximum post trigger time. */
-       sigma_set_register(WRITE_POST_TRIGGER, (capture_ratio * 256) / 100);
+       sigma_set_register(WRITE_POST_TRIGGER, (capture_ratio * 255) / 100);
 
        /* Start acqusition. */
        gettimeofday(&start_tv, 0);
@@ -1051,25 +1219,45 @@ static int hw_start_acquisition(int device_index, gpointer session_device_id)
        gettimeofday(&header.starttime, NULL);
        header.samplerate = cur_samplerate;
        header.protocol_id = PROTO_RAW;
-       header.num_probes = num_probes;
+       header.num_logic_probes = num_probes;
+       header.num_analog_probes = 0;
        session_bus(session_device_id, &packet);
 
        /* Add capture source. */
        source_add(0, G_IO_IN, 10, receive_data, session_device_id);
 
+       sigma.state = SIGMA_CAPTURE;
+
        return SIGROK_OK;
 }
 
 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
 {
+       uint8_t modestatus;
+
        device_index = device_index;
        session_device_id = session_device_id;
 
        /* Stop acquisition. */
        sigma_set_register(WRITE_MODE, 0x11);
 
-       // XXX Set some state to indicate that data should be sent to sigrok
-       //     Now, we just wait for timeout
+       /* Set SDRAM Read Enable. */
+       sigma_set_register(WRITE_MODE, 0x02);
+
+       /* Get the current position. */
+       sigma_read_pos(&sigma.stoppos, &sigma.triggerpos);
+
+       /* Check if trigger has fired. */
+       modestatus = sigma_get_register(READ_MODE);
+       if (modestatus & 0x20) {
+               sigma.triggerchunk = sigma.triggerpos / 512;
+
+       } else
+               sigma.triggerchunk = -1;
+
+       sigma.chunks_downloaded = 0;
+
+       sigma.state = SIGMA_DOWNLOAD;
 }
 
 struct device_plugin asix_sigma_plugin_info = {