]> sigrok.org Git - libsigrok.git/blobdiff - hardware/openbench-logic-sniffer/protocol.c
ols: Use sr_dev_inst to store connection handle
[libsigrok.git] / hardware / openbench-logic-sniffer / protocol.c
index ff2af5850c0191560204f68a61f7969349969c23..68b9f0c4a64d2e3acd614e7f555bad0546ae08ae 100644 (file)
@@ -133,15 +133,19 @@ SR_PRIV struct dev_context *ols_dev_new(void)
 {
        struct dev_context *devc;
 
-       if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
+       if (!(devc = g_try_malloc(sizeof(struct dev_context)))) {
                sr_err("Device context malloc failed.");
                return NULL;
        }
 
+       /* Device-specific settings */
+       devc->max_samples = devc->max_samplerate = devc->protocol_version = 0;
+
+       /* Acquisition settings */
+       devc->limit_samples = devc->capture_ratio = 0;
        devc->trigger_at = -1;
        devc->probe_mask = 0xffffffff;
-       devc->cur_samplerate = SR_KHZ(200);
-       devc->serial = NULL;
+       devc->flag_reg = 0;
 
        return devc;
 }
@@ -284,16 +288,12 @@ SR_PRIV struct sr_dev_inst *get_metadata(struct sr_serial_dev_inst *serial)
 }
 
 SR_PRIV int ols_set_samplerate(const struct sr_dev_inst *sdi,
-                              uint64_t samplerate,
-                              const struct sr_samplerates *samplerates)
+               const uint64_t samplerate)
 {
        struct dev_context *devc;
 
        devc = sdi->priv;
-       if (devc->max_samplerate) {
-               if (samplerate > devc->max_samplerate)
-                       return SR_ERR_SAMPLERATE;
-       } else if (samplerate < samplerates->low || samplerate > samplerates->high)
+       if (devc->max_samplerate && samplerate > devc->max_samplerate)
                return SR_ERR_SAMPLERATE;
 
        if (samplerate > CLOCK_RATE) {
@@ -311,7 +311,7 @@ SR_PRIV int ols_set_samplerate(const struct sr_dev_inst *sdi,
        if (devc->flag_reg & FLAG_DEMUX)
                devc->cur_samplerate *= 2;
        if (devc->cur_samplerate != samplerate)
-               sr_err("Can't match samplerate %" PRIu64 ", using %"
+               sr_info("Can't match samplerate %" PRIu64 ", using %"
                       PRIu64 ".", samplerate, devc->cur_samplerate);
 
        return SR_OK;
@@ -320,10 +320,10 @@ SR_PRIV int ols_set_samplerate(const struct sr_dev_inst *sdi,
 SR_PRIV void abort_acquisition(const struct sr_dev_inst *sdi)
 {
        struct sr_datafeed_packet packet;
-       struct dev_context *devc;
+       struct sr_serial_dev_inst *serial;
 
-       devc = sdi->priv;
-       sr_source_remove(devc->serial->fd);
+       serial = sdi->conn;
+       sr_source_remove(serial->fd);
 
        /* Terminate session */
        packet.type = SR_DF_END;
@@ -332,12 +332,14 @@ SR_PRIV void abort_acquisition(const struct sr_dev_inst *sdi)
 
 SR_PRIV int ols_receive_data(int fd, int revents, void *cb_data)
 {
+       struct drv_context *drvc;
+       struct dev_context *devc;
+       struct sr_serial_dev_inst *serial;
        struct sr_datafeed_packet packet;
        struct sr_datafeed_logic logic;
        struct sr_dev_inst *sdi;
-       struct drv_context *drvc;
-       struct dev_context *devc;
        GSList *l;
+       uint32_t sample;
        int num_channels, offset, i, j;
        unsigned char byte;
 
@@ -348,7 +350,8 @@ SR_PRIV int ols_receive_data(int fd, int revents, void *cb_data)
        for (l = drvc->instances; l; l = l->next) {
                sdi = l->data;
                devc = sdi->priv;
-               if (devc->serial->fd == fd)
+               serial = sdi->conn;
+               if (serial->fd == fd)
                        break;
                devc = NULL;
        }
@@ -381,7 +384,7 @@ SR_PRIV int ols_receive_data(int fd, int revents, void *cb_data)
        }
 
        if (revents == G_IO_IN) {
-               if (serial_read(devc->serial, &byte, 1) != 1)
+               if (serial_read(serial, &byte, 1) != 1)
                        return FALSE;
 
                /* Ignore it if we've read enough. */
@@ -392,8 +395,9 @@ SR_PRIV int ols_receive_data(int fd, int revents, void *cb_data)
                sr_dbg("Received byte 0x%.2x.", byte);
                if (devc->num_bytes == num_channels) {
                        /* Got a full sample. */
-                       sr_dbg("Received sample 0x%.*x.",
-                              devc->num_bytes * 2, *(int *)devc->sample);
+                       sample = devc->sample[0] | (devc->sample[1] << 8) \
+                                       | (devc->sample[2] << 16) | (devc->sample[3] << 24);
+                       sr_dbg("Received sample 0x%.*x.", devc->num_bytes * 2, sample);
                        if (devc->flag_reg & FLAG_RLE) {
                                /*
                                 * In RLE mode -1 should never come in as a
@@ -405,7 +409,7 @@ SR_PRIV int ols_receive_data(int fd, int revents, void *cb_data)
                                         * FIXME: This will only work on
                                         * little-endian systems.
                                         */
-                                       devc->rle_count = *(int *)(devc->sample);
+                                       devc->rle_count = sample;
                                        sr_dbg("RLE count: %d.", devc->rle_count);
                                        devc->num_bytes = 0;
                                        return TRUE;
@@ -441,7 +445,7 @@ SR_PRIV int ols_receive_data(int fd, int revents, void *cb_data)
                                        }
                                }
                                memcpy(devc->sample, devc->tmp_sample, 4);
-                               sr_dbg("Full sample: 0x%.8x.", *(int *)devc->sample);
+                               sr_dbg("Full sample: 0x%.8x.", sample);
                        }
 
                        /* the OLS sends its sample buffer backwards.
@@ -502,9 +506,9 @@ SR_PRIV int ols_receive_data(int fd, int revents, void *cb_data)
                }
                g_free(devc->raw_sample_buf);
 
-               serial_flush(devc->serial);
+               serial_flush(serial);
                abort_acquisition(sdi);
-               serial_close(devc->serial);
+               serial_close(serial);
        }
 
        return TRUE;