]> sigrok.org Git - libsigrok.git/blobdiff - hardware/sysclk-lwla/protocol.c
sysclk-lwla: Load bitstream files in RBF format.
[libsigrok.git] / hardware / sysclk-lwla / protocol.c
index 7c8cc80a13d45c84c48abf0e7aaced164c3acc37..82c1055c197dae1306707c56ef39568d577a6b30 100644 (file)
@@ -20,9 +20,6 @@
 #include "protocol.h"
 #include <string.h>
 
-/* Bit mask covering all 34 channels. */
-#define ALL_CHANNELS_MASK (((uint64_t)1 << NUM_PROBES) - 1)
-
 /* Bit mask for the RLE repeat-count-follows flag. */
 #define RLE_FLAG_LEN_FOLLOWS ((uint64_t)1 << 35)
 
 
 /* The bitstream filenames are indexed by the clock source enumeration.
  */
-static const char *const bitstream_map[] = {
-       FIRMWARE_DIR "/sysclk-lwla1034-off.bitstream",
-       FIRMWARE_DIR "/sysclk-lwla1034-int.bitstream",
-       FIRMWARE_DIR "/sysclk-lwla1034-extpos.bitstream",
-       FIRMWARE_DIR "/sysclk-lwla1034-extneg.bitstream",
+static const char bitstream_map[][32] = {
+       "sysclk-lwla1034-off.rbf",
+       "sysclk-lwla1034-int.rbf",
+       "sysclk-lwla1034-extpos.rbf",
+       "sysclk-lwla1034-extneg.rbf",
 };
 
 /* Submit an already filled-in USB transfer.
@@ -238,8 +235,8 @@ static void issue_read_start(const struct sr_dev_inst *sdi)
        acq->mem_addr_next = 4;
        acq->mem_addr_stop = acq->mem_addr_fill;
 
-       /* Byte offset into the packet output buffer. */
-       acq->out_offset = 0;
+       /* Sample position in the packet output buffer. */
+       acq->out_index = 0;
 
        regvals = devc->reg_write_seq;
 
@@ -292,8 +289,7 @@ static void process_capture_length(const struct sr_dev_inst *sdi)
        }
        acq->mem_addr_fill = LWLA_READ32(acq->xfer_buf_in);
 
-       sr_dbg("%lu words in capture buffer.",
-              (unsigned long)acq->mem_addr_fill);
+       sr_dbg("%zu words in capture buffer.", acq->mem_addr_fill);
 
        if (acq->mem_addr_fill > 0 && sdi->status == SR_ST_ACTIVE)
                issue_read_start(sdi);
@@ -349,6 +345,8 @@ static void process_capture_status(const struct sr_dev_inst *sdi)
        uint64_t duration;
        struct dev_context *devc;
        struct acquisition_state *acq;
+       unsigned int mem_fill;
+       unsigned int flags;
 
        devc = sdi->priv;
        acq  = devc->acquisition;
@@ -364,10 +362,9 @@ static void process_capture_status(const struct sr_dev_inst *sdi)
         * in the FPGA.  These fields are definitely less than 64 bit wide
         * internally, and the unused bits occasionally even contain garbage.
         */
-       acq->mem_addr_fill = LWLA_READ32(&acq->xfer_buf_in[0]);
-       duration           = LWLA_READ32(&acq->xfer_buf_in[8]);
-       acq->capture_flags = LWLA_READ32(&acq->xfer_buf_in[16])
-                               & STATUS_FLAG_MASK;
+       mem_fill = LWLA_READ32(&acq->xfer_buf_in[0]);
+       duration = LWLA_READ32(&acq->xfer_buf_in[8]);
+       flags    = LWLA_READ32(&acq->xfer_buf_in[16]) & STATUS_FLAG_MASK;
 
        /* The LWLA1034 runs at 125 MHz if the clock divider is bypassed.
         * However, the time base used for the duration is apparently not
@@ -381,10 +378,16 @@ static void process_capture_status(const struct sr_dev_inst *sdi)
        else
                acq->duration_now = duration;
 
-       sr_spew("Captured %zu words, %" PRIu64 " ms, flags 0x%02X",
-               acq->mem_addr_fill, acq->duration_now, acq->capture_flags);
+       sr_spew("Captured %u words, %" PRIu64 " ms, flags 0x%02X.",
+               mem_fill, acq->duration_now, flags);
+
+       if ((flags & STATUS_TRIGGERED) > (acq->capture_flags & STATUS_TRIGGERED))
+               sr_info("Capture triggered.");
+
+       acq->capture_flags = flags;
 
        if (acq->duration_now >= acq->duration_max) {
+               sr_dbg("Time limit reached, stopping capture.");
                issue_stop_capture(sdi);
                return;
        }
@@ -434,49 +437,6 @@ static void request_read_mem(const struct sr_dev_inst *sdi)
        }
 }
 
-/* Send a packet of logic samples to the session bus.  The payload is taken
- * from the acquisition state.  The return value indicates whether to stop
- * reading more samples.
- */
-static gboolean send_logic_packet(const struct sr_dev_inst *sdi)
-{
-       uint64_t samples;
-       struct dev_context *devc;
-       struct acquisition_state *acq;
-       struct sr_datafeed_packet packet;
-       struct sr_datafeed_logic  logic;
-       int last;
-
-       devc = sdi->priv;
-       acq  = devc->acquisition;
-
-       if (acq->samples_done >= acq->samples_max)
-               return TRUE;
-
-       packet.type    = SR_DF_LOGIC;
-       packet.payload = &logic;
-       logic.unitsize = UNIT_SIZE;
-       logic.data     = acq->out_packet;
-       logic.length   = acq->out_offset;
-
-       samples = acq->out_offset / UNIT_SIZE;
-       last = FALSE;
-
-       /* Cut the packet short if necessary. */
-       if (acq->samples_done + samples >= acq->samples_max) {
-               samples = acq->samples_max - acq->samples_done;
-               logic.length = samples * UNIT_SIZE;
-               last = TRUE;
-       }
-       acq->samples_done += samples;
-       acq->out_offset = 0;
-
-       /* Send off logic datafeed packet. */
-       sr_session_send(sdi, &packet);
-
-       return last;
-}
-
 /* Demangle and decompress incoming sample data from the capture buffer.
  * The data chunk is taken from the acquisition state, and is expected to
  * contain a multiple of 8 device words.
@@ -487,15 +447,19 @@ static gboolean send_logic_packet(const struct sr_dev_inst *sdi)
 static int process_sample_data(const struct sr_dev_inst *sdi)
 {
        uint64_t sample;
-       uint64_t run_len;
        uint64_t high_nibbles;
        uint64_t word;
        struct dev_context *devc;
        struct acquisition_state *acq;
        uint8_t *out_p;
        uint16_t *slice;
+       struct sr_datafeed_packet packet;
+       struct sr_datafeed_logic logic;
        size_t expect_len;
        size_t actual_len;
+       size_t out_max_samples;
+       size_t out_run_samples;
+       size_t ri;
        size_t in_words_left;
        size_t si;
 
@@ -512,33 +476,55 @@ static int process_sample_data(const struct sr_dev_inst *sdi)
        actual_len = acq->xfer_in->actual_length;
 
        if (actual_len != expect_len) {
-               sr_err("Received size %lu does not match expected size %lu.",
-                      (unsigned long)actual_len, (unsigned long)expect_len);
+               sr_err("Received size %zu does not match expected size %zu.",
+                      actual_len, expect_len);
                devc->transfer_error = TRUE;
                return SR_ERR;
        }
        acq->mem_addr_done += in_words_left;
+
+       /* Prepare session packet. */
+       packet.type    = SR_DF_LOGIC;
+       packet.payload = &logic;
+       logic.unitsize = UNIT_SIZE;
+       logic.data     = acq->out_packet;
+
        slice = acq->xfer_buf_in;
        si = 0; /* word index within slice */
 
        for (;;) {
-               sample = acq->sample;
+               /* Calculate number of samples to write into packet. */
+               out_max_samples = MIN(acq->samples_max - acq->samples_done,
+                                     PACKET_LENGTH - acq->out_index);
+               out_run_samples = MIN(acq->run_len, out_max_samples);
+
                /* Expand run-length samples into session packet. */
-               for (run_len = acq->run_len; run_len > 0; --run_len) {
-                       out_p = &acq->out_packet[acq->out_offset];
+               sample = acq->sample;
+               out_p = &acq->out_packet[acq->out_index * UNIT_SIZE];
+
+               for (ri = 0; ri < out_run_samples; ++ri) {
                        out_p[0] =  sample        & 0xFF;
                        out_p[1] = (sample >>  8) & 0xFF;
                        out_p[2] = (sample >> 16) & 0xFF;
                        out_p[3] = (sample >> 24) & 0xFF;
                        out_p[4] = (sample >> 32) & 0xFF;
-                       acq->out_offset += UNIT_SIZE;
-
-                       /* Send out packet if it is full. */
-                       if (acq->out_offset > PACKET_SIZE - UNIT_SIZE)
-                               if (send_logic_packet(sdi))
-                                       return SR_OK; /* sample limit reached */
+                       out_p += UNIT_SIZE;
+               }
+               acq->run_len -= out_run_samples;
+               acq->out_index += out_run_samples;
+               acq->samples_done += out_run_samples;
+
+               /* Packet full or sample count limit reached? */
+               if (out_run_samples == out_max_samples) {
+                       logic.length = acq->out_index * UNIT_SIZE;
+                       sr_session_send(sdi, &packet);
+                       acq->out_index = 0;
+
+                       if (acq->samples_done >= acq->samples_max)
+                               return SR_OK; /* sample limit reached */
+                       if (acq->run_len > 0)
+                               continue; /* need another packet */
                }
-               acq->run_len = 0;
 
                if (in_words_left == 0)
                        break; /* done with current chunk */
@@ -566,10 +552,12 @@ static int process_sample_data(const struct sr_dev_inst *sdi)
                --in_words_left;
        }
 
-       /* Send out partially filled packet if it is the last one. */
-       if (acq->mem_addr_done >= acq->mem_addr_stop && acq->out_offset > 0)
-               send_logic_packet(sdi);
-
+       /* Send out partially filled packet if this was the last chunk. */
+       if (acq->mem_addr_done >= acq->mem_addr_stop && acq->out_index > 0) {
+               logic.length = acq->out_index * UNIT_SIZE;
+               sr_session_send(sdi, &packet);
+               acq->out_index = 0;
+       }
        return SR_OK;
 }
 
@@ -725,21 +713,21 @@ SR_PRIV int lwla_init_device(const struct sr_dev_inst *sdi)
        ret = lwla_read_reg(sdi->conn, REG_CMD_CTRL1, &value);
        if (ret != SR_OK)
                return ret;
-       sr_info("Received test word 0x%08X back.", value);
+       sr_dbg("Received test word 0x%08X back.", value);
        if (value != 0x12345678)
                return SR_ERR;
 
        ret = lwla_read_reg(sdi->conn, REG_CMD_CTRL4, &value);
        if (ret != SR_OK)
                return ret;
-       sr_info("Received test word 0x%08X back.", value);
+       sr_dbg("Received test word 0x%08X back.", value);
        if (value != 0x12345678)
                return SR_ERR;
 
        ret = lwla_read_reg(sdi->conn, REG_CMD_CTRL3, &value);
        if (ret != SR_OK)
                return ret;
-       sr_info("Received test word 0x%08X back.", value);
+       sr_dbg("Received test word 0x%08X back.", value);
        if (value != 0x87654321)
                return SR_ERR;
 
@@ -788,14 +776,24 @@ SR_PRIV int lwla_setup_acquisition(const struct sr_dev_inst *sdi)
        usb  = sdi->conn;
        acq  = devc->acquisition;
 
-       /* By default, run virtually unlimited. */
-       acq->duration_max = (devc->limit_msec > 0)
-               ? devc->limit_msec : MAX_LIMIT_MSEC;
-       acq->samples_max = (devc->limit_samples > 0)
-               ? devc->limit_samples : MAX_LIMIT_SAMPLES;
+       if (devc->limit_msec > 0) {
+               acq->duration_max = devc->limit_msec;
+               sr_info("Acquisition time limit %" PRIu64 " ms.",
+                       devc->limit_msec);
+       } else
+               acq->duration_max = MAX_LIMIT_MSEC;
+
+       if (devc->limit_samples > 0) {
+               acq->samples_max = devc->limit_samples;
+               sr_info("Acquisition sample count limit %" PRIu64 ".",
+                       devc->limit_samples);
+       } else
+               acq->samples_max = MAX_LIMIT_SAMPLES;
 
        switch (devc->cur_clock_source) {
        case CLOCK_SOURCE_INT:
+               sr_info("Internal clock, samplerate %" PRIu64 ".",
+                       devc->samplerate);
                if (devc->samplerate == 0)
                        return SR_ERR_BUG;
                /* At 125 MHz, the clock divider is bypassed. */
@@ -810,7 +808,11 @@ SR_PRIV int lwla_setup_acquisition(const struct sr_dev_inst *sdi)
                                        * devc->samplerate / 1000;
                break;
        case CLOCK_SOURCE_EXT_FALL:
+               sr_info("External clock, falling edge.");
+               acq->bypass_clockdiv = TRUE;
+               break;
        case CLOCK_SOURCE_EXT_RISE:
+               sr_info("External clock, rising edge.");
                acq->bypass_clockdiv = TRUE;
                break;
        default:
@@ -861,7 +863,9 @@ SR_PRIV int lwla_start_acquisition(const struct sr_dev_inst *sdi)
        usb  = sdi->conn;
        acq  = devc->acquisition;
 
-       acq->duration_now = 0;
+       acq->duration_now  = 0;
+       acq->mem_addr_fill = 0;
+       acq->capture_flags = 0;
 
        libusb_fill_bulk_transfer(acq->xfer_out, usb->devhdl, EP_COMMAND,
                                  (unsigned char *)acq->xfer_buf_out, 0,