]> sigrok.org Git - libsigrok.git/blobdiff - hardware/saleae-logic16/protocol.c
Add struct sr_session parameter to all session source backends.
[libsigrok.git] / hardware / saleae-logic16 / protocol.c
index 4a4aee125356acc2b71ec658ece2819ad8ff47a9..785833432749f2f71f772a841699197e5d56da41 100644 (file)
@@ -576,28 +576,37 @@ SR_PRIV int logic16_init_device(const struct sr_dev_inst *sdi)
        return SR_OK;
 }
 
-static void finish_acquisition(struct dev_context *devc)
+static void finish_acquisition(struct sr_dev_inst *sdi)
 {
        struct sr_datafeed_packet packet;
+       struct dev_context *devc;
+
+       devc = sdi->priv;
 
        /* Terminate session. */
        packet.type = SR_DF_END;
        sr_session_send(devc->cb_data, &packet);
 
        /* Remove fds from polling. */
-       usb_source_remove(devc->ctx);
+       usb_source_remove(sdi->session, devc->ctx);
 
        devc->num_transfers = 0;
        g_free(devc->transfers);
        g_free(devc->convbuffer);
+       if (devc->stl) {
+               soft_trigger_logic_free(devc->stl);
+               devc->stl = NULL;
+       }
 }
 
 static void free_transfer(struct libusb_transfer *transfer)
 {
+       struct sr_dev_inst *sdi;
        struct dev_context *devc;
        unsigned int i;
 
-       devc = transfer->user_data;
+       sdi = transfer->user_data;
+       devc = sdi->priv;
 
        g_free(transfer->buffer);
        transfer->buffer = NULL;
@@ -612,7 +621,7 @@ static void free_transfer(struct libusb_transfer *transfer)
 
        devc->submitted_transfers--;
        if (devc->submitted_transfers == 0)
-               finish_acquisition(devc);
+               finish_acquisition(sdi);
 }
 
 static void resubmit_transfer(struct libusb_transfer *transfer)
@@ -628,9 +637,8 @@ static void resubmit_transfer(struct libusb_transfer *transfer)
        sr_err("%s: %s", __func__, libusb_error_name(ret));
 }
 
-static size_t convert_sample_data_16(struct dev_context *devc,
-                                    uint8_t *dest, size_t destcnt,
-                                    const uint8_t *src, size_t srccnt)
+static size_t convert_sample_data(struct dev_context *devc,
+               uint8_t *dest, size_t destcnt, const uint8_t *src, size_t srccnt)
 {
        uint16_t *channel_data;
        int i, cur_channel;
@@ -671,75 +679,24 @@ static size_t convert_sample_data_16(struct dev_context *devc,
        return ret;
 }
 
-static size_t convert_sample_data_8(struct dev_context *devc,
-                                   uint8_t *dest, size_t destcnt,
-                                   const uint8_t *src, size_t srccnt)
-{
-       uint8_t *channel_data;
-       int i, cur_channel;
-       size_t ret = 0;
-       uint16_t sample;
-       uint8_t channel_mask;
-
-       srccnt /= 2;
-
-       channel_data = (uint8_t *)devc->channel_data;
-       cur_channel = devc->cur_channel;
-
-       while (srccnt--) {
-               sample = src[0] | (src[1] << 8);
-               src += 2;
-
-               channel_mask = devc->channel_masks[cur_channel];
-
-               for (i = 15; i >= 0; --i, sample >>= 1)
-                       if (sample & 1)
-                               channel_data[i] |= channel_mask;
-
-               if (++cur_channel == devc->num_channels) {
-                       cur_channel = 0;
-                       if (destcnt < 16) {
-                               sr_err("Conversion buffer too small!");
-                               break;
-                       }
-                       memcpy(dest, channel_data, 16);
-                       memset(channel_data, 0, 16);
-                       dest += 16;
-                       ret += 16;
-                       destcnt -= 16;
-               }
-       }
-
-       devc->cur_channel = cur_channel;
-
-       return ret;
-}
-
-static size_t convert_sample_data(struct dev_context *devc,
-                                 uint8_t *dest, size_t destcnt,
-                                 const uint8_t *src, size_t srccnt,
-                                 int unitsize)
-{
-       return (unitsize == 2?
-               convert_sample_data_16(devc, dest, destcnt, src, srccnt) :
-               convert_sample_data_8(devc, dest, destcnt, src, srccnt));
-}
-
 SR_PRIV void logic16_receive_transfer(struct libusb_transfer *transfer)
 {
        gboolean packet_has_error = FALSE;
        struct sr_datafeed_packet packet;
        struct sr_datafeed_logic logic;
+       struct sr_dev_inst *sdi;
        struct dev_context *devc;
-       size_t converted_length;
+       size_t new_samples, num_samples;
+       int trigger_offset;
 
-       devc = transfer->user_data;
+       sdi = transfer->user_data;
+       devc = sdi->priv;
 
        /*
         * If acquisition has already ended, just free any queued up
         * transfer that come in.
         */
-       if (devc->num_samples < 0) {
+       if (devc->sent_samples < 0) {
                free_transfer(transfer);
                return;
        }
@@ -749,7 +706,7 @@ SR_PRIV void logic16_receive_transfer(struct libusb_transfer *transfer)
 
        switch (transfer->status) {
        case LIBUSB_TRANSFER_NO_DEVICE:
-               devc->num_samples = -2;
+               devc->sent_samples = -2;
                free_transfer(transfer);
                return;
        case LIBUSB_TRANSFER_COMPLETED:
@@ -775,7 +732,7 @@ SR_PRIV void logic16_receive_transfer(struct libusb_transfer *transfer)
                         * The FX2 gave up. End the acquisition, the frontend
                         * will work out that the samplecount is short.
                         */
-                       devc->num_samples = -2;
+                       devc->sent_samples = -2;
                        free_transfer(transfer);
                } else {
                        resubmit_transfer(transfer);
@@ -785,31 +742,45 @@ SR_PRIV void logic16_receive_transfer(struct libusb_transfer *transfer)
                devc->empty_transfer_count = 0;
        }
 
-       converted_length = convert_sample_data(devc, devc->convbuffer,
-                               devc->convbuffer_size, transfer->buffer,
-                               transfer->actual_length, devc->unitsize);
-
-       if (converted_length > 0) {
-               /* Cap sample count if needed */
-               if (devc->limit_samples &&
-                   (uint64_t)devc->num_samples + converted_length
-                   > devc->limit_samples) {
-                       converted_length =
-                               devc->limit_samples - devc->num_samples;
+       new_samples = convert_sample_data(devc, devc->convbuffer,
+                       devc->convbuffer_size, transfer->buffer, transfer->actual_length);
+
+       if (new_samples > 0) {
+               if (devc->trigger_fired) {
+                       /* Send the incoming transfer to the session bus. */
+                       packet.type = SR_DF_LOGIC;
+                       packet.payload = &logic;
+                       if (devc->limit_samples &&
+                                       new_samples > devc->limit_samples - devc->sent_samples)
+                               new_samples = devc->limit_samples - devc->sent_samples;
+                       logic.length = new_samples * 2;
+                       logic.unitsize = 2;
+                       logic.data = devc->convbuffer;
+                       sr_session_send(devc->cb_data, &packet);
+                       devc->sent_samples += new_samples;
+               } else {
+                       trigger_offset = soft_trigger_logic_check(devc->stl,
+                                       devc->convbuffer, new_samples * 2);
+                       if (trigger_offset > -1) {
+                               packet.type = SR_DF_LOGIC;
+                               packet.payload = &logic;
+                               num_samples = new_samples - trigger_offset;
+                               if (devc->limit_samples &&
+                                               num_samples > devc->limit_samples - devc->sent_samples)
+                                       num_samples = devc->limit_samples - devc->sent_samples;
+                               logic.length = num_samples * 2;
+                               logic.unitsize = 2;
+                               logic.data = devc->convbuffer + trigger_offset * 2;
+                               sr_session_send(devc->cb_data, &packet);
+                               devc->sent_samples += num_samples;
+
+                               devc->trigger_fired = TRUE;
+                       }
                }
 
-               /* Send the incoming transfer to the session bus. */
-               packet.type = SR_DF_LOGIC;
-               packet.payload = &logic;
-               logic.length = converted_length * devc->unitsize;
-               logic.unitsize = devc->unitsize;
-               logic.data = devc->convbuffer;
-               sr_session_send(devc->cb_data, &packet);
-
-               devc->num_samples += converted_length;
                if (devc->limit_samples &&
-                   (uint64_t)devc->num_samples >= devc->limit_samples) {
-                       devc->num_samples = -2;
+                               (uint64_t)devc->sent_samples >= devc->limit_samples) {
+                       devc->sent_samples = -2;
                        free_transfer(transfer);
                        return;
                }