]> sigrok.org Git - libsigrok.git/blobdiff - src/hardware/sysclk-lwla/lwla1034.c
drivers: Factor out std_gvar_samplerates{,_steps}().
[libsigrok.git] / src / hardware / sysclk-lwla / lwla1034.c
index 62de549a639b2bd4f2ccec66f14aa7a8fbbc7429..63ca46f58065474c86136f5923a43e66e0236c30 100644 (file)
 #include "lwla.h"
 #include "protocol.h"
 
-/* Number of logic channels.
- */
+/* Number of logic channels. */
 #define NUM_CHANNELS   34
 
-/* Bit mask covering all logic channels.
- */
+/* Bit mask covering all logic channels. */
 #define ALL_CHANNELS_MASK      ((UINT64_C(1) << NUM_CHANNELS) - 1)
 
-/* Unit size for the sigrok logic datafeed.
- */
+/* Unit size for the sigrok logic datafeed. */
 #define UNIT_SIZE      ((NUM_CHANNELS + 7) / 8)
 
-/* Size of the acquisition buffer in device memory units.
- */
+/* Size of the acquisition buffer in device memory units. */
 #define MEMORY_DEPTH   (256 * 1024)    /* 256k x 36 bit */
 
-/* Capture memory read start address.
- */
+/* Capture memory read start address. */
 #define READ_START_ADDR        4
 
 /* Number of device memory units (36 bit) to read at a time. Slices of 8
@@ -56,8 +51,7 @@
  */
 #define READ_CHUNK_LEN (28 * 8)
 
-/* Bit mask for the RLE repeat-count-follows flag.
- */
+/* Bit mask for the RLE repeat-count-follows flag. */
 #define RLE_FLAG_LEN_FOLLOWS   (UINT64_C(1) << 35)
 
 /* Start index and count for bulk long register reads.
@@ -67,8 +61,7 @@
 #define READ_LREGS_START       LREG_MEM_FILL
 #define READ_LREGS_COUNT       (LREG_STATUS + 1 - READ_LREGS_START)
 
-/** LWLA1034 register addresses.
- */
+/** LWLA1034 register addresses. */
 enum reg_addr {
        REG_MEM_CTRL    = 0x1074, /* capture buffer control */
        REG_MEM_FILL    = 0x1078, /* capture buffer fill level */
@@ -82,15 +75,13 @@ enum reg_addr {
        REG_LONG_HIGH   = 0x10BC, /* long register high word */
 };
 
-/** Flag bits for REG_MEM_CTRL.
- */
+/** Flag bits for REG_MEM_CTRL. */
 enum mem_ctrl_flag {
        MEM_CTRL_WRITE   = 1 << 0, /* "wr1rd0" bit */
        MEM_CTRL_CLR_IDX = 1 << 1, /* "clr_idx" bit */
 };
 
-/* LWLA1034 long register addresses.
- */
+/* LWLA1034 long register addresses. */
 enum long_reg_addr {
        LREG_CHAN_MASK  = 0,    /* channel enable mask */
        LREG_DIV_COUNT  = 1,    /* clock divider max count */
@@ -107,8 +98,7 @@ enum long_reg_addr {
        LREG_TEST_ID    = 100,  /* constant test ID */
 };
 
-/** Flag bits for LREG_CAP_CTRL.
- */
+/** Flag bits for LREG_CAP_CTRL. */
 enum cap_ctrl_flag {
        CAP_CTRL_TRG_EN       = 1 << 0, /* "trg_en" bit */
        CAP_CTRL_CLR_TIMEBASE = 1 << 2, /* "do_clr_timebase" bit */
@@ -117,8 +107,7 @@ enum cap_ctrl_flag {
        CAP_CTRL_CLR_COUNTER  = 1 << 6, /* "clr_cntr0" bit */
 };
 
-/* Available FPGA configurations.
- */
+/* Available FPGA configurations. */
 enum fpga_config {
        FPGA_OFF = 0,   /* FPGA shutdown config */
        FPGA_INT,       /* internal clock config */
@@ -126,8 +115,7 @@ enum fpga_config {
        FPGA_EXTNEG,    /* external clock, falling edge config */
 };
 
-/* FPGA bitstream resource filenames.
- */
+/* FPGA bitstream resource filenames. */
 static const char bitstream_map[][32] = {
        [FPGA_OFF]      = "sysclk-lwla1034-off.rbf",
        [FPGA_INT]      = "sysclk-lwla1034-int.rbf",
@@ -135,8 +123,7 @@ static const char bitstream_map[][32] = {
        [FPGA_EXTNEG]   = "sysclk-lwla1034-extneg.rbf",
 };
 
-/* Read 64-bit long register.
- */
+/* Read 64-bit long register. */
 static int read_long_reg(const struct sr_usb_dev_inst *usb,
                         uint32_t addr, uint64_t *value)
 {
@@ -164,8 +151,7 @@ static int read_long_reg(const struct sr_usb_dev_inst *usb,
        return SR_OK;
 }
 
-/* Queue access sequence for a long register write.
- */
+/* Queue access sequence for a long register write. */
 static void queue_long_regval(struct acquisition_state *acq,
                              uint32_t addr, uint64_t value)
 {
@@ -175,8 +161,7 @@ static void queue_long_regval(struct acquisition_state *acq,
        lwla_queue_regval(acq, REG_LONG_STROBE, 0);
 }
 
-/* Helper to fill in the long register bulk write command.
- */
+/* Helper to fill in the long register bulk write command. */
 static inline void bulk_long_set(struct acquisition_state *acq,
                                 unsigned int idx, uint64_t value)
 {
@@ -186,8 +171,7 @@ static inline void bulk_long_set(struct acquisition_state *acq,
        acq->xfer_buf_out[4 * idx + 6] = LWLA_WORD_3(value);
 }
 
-/* Helper for dissecting the response to a long register bulk read.
- */
+/* Helper for dissecting the response to a long register bulk read. */
 static inline uint64_t bulk_long_get(const struct acquisition_state *acq,
                                     unsigned int idx)
 {
@@ -208,9 +192,7 @@ static void read_response(struct acquisition_state *acq)
        uint64_t sample, high_nibbles, word;
        uint32_t *slice;
        uint8_t *out_p;
-       unsigned int words_left;
-       unsigned int max_samples, run_samples;
-       unsigned int wi, ri, si;
+       unsigned int words_left, max_samples, run_samples, wi, ri, si;
 
        /* Number of 36-bit words remaining in the transfer buffer. */
        words_left = MIN(acq->mem_addr_next, acq->mem_addr_stop)
@@ -239,13 +221,13 @@ static void read_response(struct acquisition_state *acq)
                acq->samples_done += run_samples;
 
                if (run_samples == max_samples)
-                       break; /* packet full or sample limit reached */
+                       break; /* Packet full or sample limit reached. */
                if (wi >= words_left)
-                       break; /* done with current transfer */
+                       break; /* Done with current transfer. */
 
                /* Get the current slice of 8 packed 36-bit words. */
                slice = &acq->xfer_buf_in[(acq->in_index + wi) / 8 * 9];
-               si = (acq->in_index + wi) % 8; /* word index within slice */
+               si = (acq->in_index + wi) % 8; /* Word index within slice. */
 
                /* Extract the next 36-bit word. */
                high_nibbles = LWLA_TO_UINT32(slice[8]);
@@ -262,6 +244,7 @@ static void read_response(struct acquisition_state *acq)
                        acq->rle = RLE_STATE_DATA;
                }
        }
+
        acq->in_index += wi;
        acq->mem_addr_done += wi;
 }
@@ -279,15 +262,13 @@ static int detect_short_transfer_quirk(const struct sr_dev_inst *sdi)
 {
        struct dev_context *devc;
        struct sr_usb_dev_inst *usb;
-       int xfer_len;
-       int ret;
+       int xfer_len, ret;
        uint16_t command[3];
        unsigned char buf[512];
-
        const int lreg_count = 10;
 
        devc = sdi->priv;
-       usb  = sdi->conn;
+       usb = sdi->conn;
 
        command[0] = LWLA_WORD(CMD_READ_LREGS);
        command[1] = LWLA_WORD(0);
@@ -320,14 +301,12 @@ static int detect_short_transfer_quirk(const struct sr_dev_inst *sdi)
        return SR_ERR;
 }
 
-/* Select and transfer FPGA bitstream for the current configuration.
- */
+/* Select and transfer FPGA bitstream for the current configuration. */
 static int apply_fpga_config(const struct sr_dev_inst *sdi)
 {
        struct dev_context *devc;
        struct drv_context *drvc;
-       int config;
-       int ret;
+       int config, ret;
 
        devc = sdi->priv;
        drvc = sdi->driver->context;
@@ -342,7 +321,7 @@ static int apply_fpga_config(const struct sr_dev_inst *sdi)
                config = FPGA_EXTNEG;
 
        if (config == devc->active_fpga_config)
-               return SR_OK; /* no change */
+               return SR_OK; /* No change. */
 
        ret = lwla_send_bitstream(drvc->sr_ctx, sdi->conn,
                                  bitstream_map[config]);
@@ -351,16 +330,13 @@ static int apply_fpga_config(const struct sr_dev_inst *sdi)
        return ret;
 }
 
-/* Perform initialization self test.
- */
+/* Perform initialization self test. */
 static int device_init_check(const struct sr_dev_inst *sdi)
 {
        uint64_t value;
        int ret;
 
-       ret = read_long_reg(sdi->conn, LREG_TEST_ID, &value);
-       if (ret != SR_OK)
-               return ret;
+       read_long_reg(sdi->conn, LREG_TEST_ID, &value);
 
        /* Ignore the value returned by the first read. */
        ret = read_long_reg(sdi->conn, LREG_TEST_ID, &value);
@@ -375,36 +351,33 @@ static int device_init_check(const struct sr_dev_inst *sdi)
        return detect_short_transfer_quirk(sdi);
 }
 
-/* Set up the device in preparation for an acquisition session.
- */
+/* Set up the device in preparation for an acquisition session. */
 static int setup_acquisition(const struct sr_dev_inst *sdi)
 {
-       uint64_t divider_count;
-       uint64_t trigger_mask;
+       static const struct regval capture_init[] = {
+               {REG_MEM_CTRL,    MEM_CTRL_CLR_IDX},
+               {REG_MEM_CTRL,    MEM_CTRL_WRITE},
+               {REG_LONG_ADDR,   LREG_CAP_CTRL},
+               {REG_LONG_LOW,    CAP_CTRL_CLR_TIMEBASE | CAP_CTRL_FLUSH_FIFO |
+                                 CAP_CTRL_CLR_FIFOFULL | CAP_CTRL_CLR_COUNTER},
+               {REG_LONG_HIGH,   0},
+               {REG_LONG_STROBE, 0},
+       };
+       uint64_t divider_count, trigger_mask;
        struct dev_context *devc;
        struct sr_usb_dev_inst *usb;
        struct acquisition_state *acq;
        int ret;
 
        devc = sdi->priv;
-       usb  = sdi->conn;
-       acq  = devc->acquisition;
+       usb = sdi->conn;
+       acq = devc->acquisition;
 
-       acq->reg_seq_pos = 0;
-       acq->reg_seq_len = 0;
-
-       lwla_queue_regval(acq, REG_MEM_CTRL, MEM_CTRL_CLR_IDX);
-       lwla_queue_regval(acq, REG_MEM_CTRL, MEM_CTRL_WRITE);
-
-       queue_long_regval(acq, LREG_CAP_CTRL,
-               CAP_CTRL_CLR_TIMEBASE | CAP_CTRL_FLUSH_FIFO |
-               CAP_CTRL_CLR_FIFOFULL | CAP_CTRL_CLR_COUNTER);
-
-       lwla_queue_regval(acq, REG_CLK_BOOST, acq->clock_boost);
-
-       ret = lwla_write_regs(usb, acq->reg_sequence, acq->reg_seq_len);
-       acq->reg_seq_len = 0;
+       ret = lwla_write_regs(usb, capture_init, ARRAY_SIZE(capture_init));
+       if (ret != SR_OK)
+               return ret;
 
+       ret = lwla_write_reg(usb, REG_CLK_BOOST, acq->clock_boost);
        if (ret != SR_OK)
                return ret;
 
@@ -422,7 +395,7 @@ static int setup_acquisition(const struct sr_dev_inst *sdi)
 
        bulk_long_set(acq, LREG_DIV_COUNT, divider_count);
        bulk_long_set(acq, LREG_TRG_VALUE, devc->trigger_values);
-       bulk_long_set(acq, LREG_TRG_TYPE,  devc->trigger_edge_mask);
+       bulk_long_set(acq, LREG_TRG_TYPE, devc->trigger_edge_mask);
 
        trigger_mask = devc->trigger_mask;
 
@@ -525,7 +498,7 @@ static int handle_response(const struct sr_dev_inst *sdi)
        int expect_len;
 
        devc = sdi->priv;
-       acq  = devc->acquisition;
+       acq = devc->acquisition;
 
        switch (devc->state) {
        case STATE_STATUS_REQUEST:
@@ -535,7 +508,7 @@ static int handle_response(const struct sr_dev_inst *sdi)
                        return SR_ERR;
                }
                acq->mem_addr_fill = bulk_long_get(acq, LREG_MEM_FILL) & 0xFFFFFFFF;
-               acq->duration_now  = bulk_long_get(acq, LREG_DURATION);
+               acq->duration_now = bulk_long_get(acq, LREG_DURATION);
                /* Shift left by one so the bit positions match the LWLA1016. */
                acq->status = (bulk_long_get(acq, LREG_STATUS) & 0x3F) << 1;
                /*
@@ -573,8 +546,7 @@ static int handle_response(const struct sr_dev_inst *sdi)
        return SR_OK;
 }
 
-/** Model descriptor for the LWLA1034.
- */
+/** Model descriptor for the LWLA1034. */
 SR_PRIV const struct model_info lwla1034_info = {
        .name = "LWLA1034",
        .num_channels = NUM_CHANNELS,