]> sigrok.org Git - libsigrok.git/blobdiff - hardware/asix-sigma/asix-sigma.c
asix-sigma: Unify calling of decode_chunk_ts()
[libsigrok.git] / hardware / asix-sigma / asix-sigma.c
index c44bae7ab9bb3b7fde085a178795458ab9515abb..e57373248ec81d565d2ade7f349964956f2aa26f 100644 (file)
@@ -1073,13 +1073,24 @@ static int download_capture(struct sr_dev_inst *sdi)
 {
        struct dev_context *devc = sdi->priv;
        const int chunks_per_read = 32;
-       unsigned char buf[chunks_per_read * CHUNK_SIZE];
-       int bufsz, i, numchunks, newchunks;
+       struct sigma_dram_line *dram_line;
+       unsigned char *buf;
+       int bufsz;
        uint32_t stoppos, triggerpos;
-       int triggerchunk, chunks_downloaded;
        struct sr_datafeed_packet packet;
        uint8_t modestatus;
 
+       uint32_t i;
+       uint32_t dl_lines_total, dl_lines_curr, dl_lines_done;
+       uint32_t dl_trailing_events;
+       uint32_t trg_line = ~0;
+
+       dram_line = g_try_malloc0(chunks_per_read * sizeof(*dram_line));
+       if (!dram_line)
+               return FALSE;
+
+       buf = (unsigned char *)dram_line;
+
        sr_info("Downloading sample data.");
 
        /* Stop acquisition. */
@@ -1094,47 +1105,50 @@ static int download_capture(struct sr_dev_inst *sdi)
        /* Check if trigger has fired. */
        modestatus = sigma_get_register(READ_MODE, devc);
        if (modestatus & 0x20)
-               triggerchunk = triggerpos / 512;
-       else
-               triggerchunk = -1;
-
-       chunks_downloaded = 0;
-       numchunks = (stoppos + 511) / 512;
-       newchunks = MIN(chunks_per_read, numchunks - chunks_downloaded);
-
-       bufsz = sigma_read_dram(chunks_downloaded, newchunks, buf, devc);
-       /* TODO: Check bufsz. For now, just avoid compiler warnings. */
-       (void)bufsz;
-
-       /* Find first ts. */
-       if (chunks_downloaded == 0) {
-               devc->state.lastts = RL16(buf) - 1;
-               devc->state.lastsample = 0;
-       }
+               trg_line = triggerpos >> 9;
+
+       /*
+        * Determine how many 1024b "DRAM lines" do we need to read from the
+        * Sigma so we have a complete set of samples. Note that the last
+        * line can be only partial, containing less than 64 clusters.
+        */
+       dl_lines_total = (stoppos >> 9) + 1;
+       dl_trailing_events = stoppos & 0x1ff;
+
+       dl_lines_done = 0;
 
-       /* Decode chunks and send them to sigrok. */
-       for (i = 0; i < newchunks; ++i) {
-               int limit_chunk = 0;
+       while (dl_lines_total > dl_lines_done) {
+               /* We can download only up-to 32 DRAM lines in one go! */
+               dl_lines_curr = MIN(chunks_per_read, dl_lines_total);
 
-               /* The last chunk may potentially be only in part. */
-               if (chunks_downloaded == numchunks - 1) {
-                       /* Find the last valid timestamp */
-                       limit_chunk = stoppos % 512 + devc->state.lastts;
+               bufsz = sigma_read_dram(dl_lines_done, dl_lines_curr, buf, devc);
+               /* TODO: Check bufsz. For now, just avoid compiler warnings. */
+               (void)bufsz;
+
+               /* This is the first DRAM line, so find the initial timestamp. */
+               if (dl_lines_done == 0) {
+                       devc->state.lastts = RL16(buf) - 1;
+                       devc->state.lastsample = 0;
                }
 
-               if (chunks_downloaded + i == triggerchunk)
-                       decode_chunk_ts(buf + (i * CHUNK_SIZE),
-                                       &devc->state.lastts,
-                                       &devc->state.lastsample,
-                                       triggerpos & 0x1ff,
-                                       limit_chunk, sdi);
-               else
+               for (i = 0; i < dl_lines_curr; i++) {
+                       uint32_t dl_limit = 0;
+                       int trigger_line = -1;
+                       /* The last "DRAM line" can be only partially full. */
+                       if (dl_lines_done + i == dl_lines_total - 1)
+                               dl_limit = dl_trailing_events;
+
+                       /* Test if the trigger happened on this line. */
+                       if (dl_lines_done + i == trg_line)
+                               trigger_line = trg_line;
+
                        decode_chunk_ts(buf + (i * CHUNK_SIZE),
                                        &devc->state.lastts,
                                        &devc->state.lastsample,
-                                       -1, limit_chunk, sdi);
+                                       trigger_line, dl_limit, sdi);
+               }
 
-               ++chunks_downloaded;
+               dl_lines_done += dl_lines_curr;
        }
 
        /* All done. */
@@ -1143,6 +1157,8 @@ static int download_capture(struct sr_dev_inst *sdi)
 
        dev_acquisition_stop(sdi, sdi);
 
+       g_free(dram_line);
+
        return TRUE;
 }