]> sigrok.org Git - libsigrok.git/blobdiff - src/hardware/sysclk-lwla/api.c
sysclk-lwla: Simplify trigger mask generation
[libsigrok.git] / src / hardware / sysclk-lwla / api.c
index d093430d50dfec6027c27ca03f8467c5e58a0261..ba9ca4fa7dd60ae275f094c1336c0cd3f6d18d91 100644 (file)
@@ -248,6 +248,40 @@ static int dev_clear(const struct sr_dev_driver *di)
        return std_dev_clear(di, &clear_dev_context);
 }
 
+/* Drain any pending data from the USB transfer buffers on the device.
+ * This may be necessary e.g. after a crash or generally to clean up after
+ * an abnormal condition.
+ */
+static int drain_usb(struct sr_usb_dev_inst *usb, unsigned int endpoint)
+{
+       int drained, xfer_len;
+       int ret;
+       unsigned char buf[512];
+
+       const unsigned int drain_timeout_ms = 10;
+
+       drained = 0;
+       do {
+               xfer_len = 0;
+               ret = libusb_bulk_transfer(usb->devhdl, endpoint,
+                                          buf, sizeof buf, &xfer_len,
+                                          drain_timeout_ms);
+               drained += xfer_len;
+       } while (ret == LIBUSB_SUCCESS && xfer_len != 0);
+
+       if (ret != LIBUSB_SUCCESS && ret != LIBUSB_ERROR_TIMEOUT) {
+               sr_err("Failed to drain USB endpoint %u: %s.",
+                      endpoint & (LIBUSB_ENDPOINT_IN - 1),
+                      libusb_error_name(ret));
+               return SR_ERR;
+       }
+       if (drained > 0) {
+               sr_warn("Drained %d bytes from USB endpoint %u.",
+                       drained, endpoint & (LIBUSB_ENDPOINT_IN - 1));
+       }
+       return SR_OK;
+}
+
 /* Open and initialize device.
  */
 static int dev_open(struct sr_dev_inst *sdi)
@@ -274,12 +308,8 @@ static int dev_open(struct sr_dev_inst *sdi)
        if (ret != SR_OK)
                return ret;
 
-       /* Set the configuration twice to trigger a lightweight reset.
-        */
        ret = libusb_set_configuration(usb->devhdl, USB_CONFIG);
-       if (ret == 0)
-               ret = libusb_set_configuration(usb->devhdl, USB_CONFIG);
-       if (ret != 0) {
+       if (ret != LIBUSB_SUCCESS) {
                sr_err("Failed to set USB configuration: %s.",
                        libusb_error_name(ret));
                sr_usb_close(usb);
@@ -287,13 +317,17 @@ static int dev_open(struct sr_dev_inst *sdi)
        }
 
        ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
-       if (ret < 0) {
+       if (ret != LIBUSB_SUCCESS) {
                sr_err("Failed to claim interface: %s.",
                        libusb_error_name(ret));
                sr_usb_close(usb);
                return SR_ERR;
        }
 
+       ret = drain_usb(usb, EP_REPLY);
+       if (ret != SR_OK)
+               return ret;
+
        sdi->status = SR_ST_ACTIVE;
 
        devc->active_fpga_config = FPGA_NOCONF;
@@ -560,13 +594,14 @@ static int prepare_trigger_masks(const struct sr_dev_inst *sdi)
        uint64_t trigger_mask;
        uint64_t trigger_values;
        uint64_t trigger_edge_mask;
-       uint64_t channel_bit;
+       uint64_t level_bit, type_bit;
        struct dev_context *devc;
        struct sr_trigger *trigger;
        struct sr_trigger_stage *stage;
        struct sr_trigger_match *match;
        const GSList *node;
        int idx;
+       enum sr_trigger_matches trg;
 
        devc = sdi->priv;
 
@@ -591,30 +626,27 @@ static int prepare_trigger_masks(const struct sr_dev_inst *sdi)
                        continue; /* ignore disabled channel */
 
                idx = match->channel->index;
+               trg = match->match;
 
                if (idx < 0 || idx >= devc->model->num_channels) {
                        sr_err("Channel index %d out of range.", idx);
                        return SR_ERR_BUG; /* should not happen */
                }
-               channel_bit = UINT64_C(1) << idx;
-               trigger_mask |= channel_bit;
-
-               switch (match->match) {
-               case SR_TRIGGER_ZERO:
-                       break;
-               case SR_TRIGGER_ONE:
-                       trigger_values |= channel_bit;
-                       break;
-               case SR_TRIGGER_RISING:
-                       trigger_values |= channel_bit;
-                       /* Fall through for edge mask. */
-               case SR_TRIGGER_FALLING:
-                       trigger_edge_mask |= channel_bit;
-                       break;
-               default:
+               if (trg != SR_TRIGGER_ZERO
+                               && trg != SR_TRIGGER_ONE
+                               && trg != SR_TRIGGER_RISING
+                               && trg != SR_TRIGGER_FALLING) {
                        sr_err("Unsupported trigger match for CH%d.", idx + 1);
                        return SR_ERR_ARG;
                }
+               level_bit = (trg == SR_TRIGGER_ONE
+                       || trg == SR_TRIGGER_RISING) ? 1 : 0;
+               type_bit = (trg == SR_TRIGGER_RISING
+                       || trg == SR_TRIGGER_FALLING) ? 1 : 0;
+
+               trigger_mask |= UINT64_C(1) << idx;
+               trigger_values |= level_bit << idx;
+               trigger_edge_mask |= type_bit << idx;
        }
        devc->trigger_mask = trigger_mask;
        devc->trigger_values = trigger_values;