]> sigrok.org Git - libsigrok.git/blobdiff - hardware/asix-sigma/asix-sigma.c
libsigrok: Use sr_err() et al instead of printf.
[libsigrok.git] / hardware / asix-sigma / asix-sigma.c
index 598d0b065ad22c0ee68524a05cf31892d07b3da2..71cb2fddcdd7b9d41eeb4e2325a0e0e2a6b9e914 100644 (file)
@@ -20,7 +20,7 @@
  */
 
 /*
- * ASIX Sigma Logic Analyzer Driver
+ * ASIX SIGMA Logic Analyzer Driver
  */
 
 #include "config.h"
@@ -98,7 +98,7 @@ static const char *firmware_files[] = {
        "asix-sigma-phasor.fw", /* Frequency counter */
 };
 
-static void hw_stop_acquisition(int device_index, gpointer session_device_id);
+static void hw_stop_acquisition(int device_index, gpointer session_data);
 
 static int sigma_read(void *buf, size_t size, struct sigma *sigma)
 {
@@ -332,12 +332,15 @@ static int bin2bitbang(const char *filename,
        fseek(f, 0, SEEK_SET);
 
        if (!(compressed_buf = g_try_malloc(file_size))) {
-               sr_err("asix: %s: compressed_buf malloc failed", __func__);
+               sr_err("sigma: %s: compressed_buf malloc failed", __func__);
+               fclose(f);
                return SR_ERR_MALLOC;
        }
 
        if (!(firmware = g_try_malloc(buffer_size))) {
-               sr_err("asix: %s: firmware malloc failed", __func__);
+               sr_err("sigma: %s: firmware malloc failed", __func__);
+               fclose(f);
+               g_free(compressed_buf);
                return SR_ERR_MALLOC;
        }
 
@@ -363,7 +366,9 @@ static int bin2bitbang(const char *filename,
 
        *buf = p = (unsigned char *)g_try_malloc(*buf_size);
        if (!p) {
-               sr_err("asix: %s: buf/p malloc failed", __func__);
+               sr_err("sigma: %s: buf/p malloc failed", __func__);
+               g_free(compressed_buf);
+               g_free(firmware);
                return SR_ERR_MALLOC;
        }
 
@@ -398,7 +403,7 @@ static int hw_init(const char *deviceinfo)
        deviceinfo = deviceinfo;
 
        if (!(sigma = g_try_malloc(sizeof(struct sigma)))) {
-               sr_err("asix: %s: sigma malloc failed", __func__);
+               sr_err("sigma: %s: sigma malloc failed", __func__);
                return 0; /* FIXME: Should be SR_ERR_MALLOC. */
        }
 
@@ -410,6 +415,7 @@ static int hw_init(const char *deviceinfo)
                goto free;
 
        sigma->cur_samplerate = 0;
+       sigma->period_ps = 0;
        sigma->limit_msec = 0;
        sigma->cur_firmware = -1;
        sigma->num_probes = 0;
@@ -432,7 +438,7 @@ static int hw_init(const char *deviceinfo)
 
        return 1;
 free:
-       free(sigma);
+       g_free(sigma);
        return 0;
 }
 
@@ -580,6 +586,7 @@ static int set_samplerate(struct sr_device_instance *sdi,
        }
 
        sigma->cur_samplerate = samplerate;
+       sigma->period_ps = 1000000000000 / samplerate;
        sigma->samples_per_event = 16 / sigma->num_probes;
        sigma->state.state = SIGMA_IDLE;
 
@@ -616,7 +623,7 @@ static int configure_probes(struct sr_device_instance *sdi, GSList *probes)
                if (sigma->cur_samplerate >= SR_MHZ(100)) {
                        /* Fast trigger support. */
                        if (trigger_set) {
-                               sr_warn("Asix Sigma only supports a single "
+                               sr_warn("ASIX SIGMA only supports a single "
                                        "pin trigger in 100 and 200MHz mode.");
                                return SR_ERR;
                        }
@@ -625,7 +632,7 @@ static int configure_probes(struct sr_device_instance *sdi, GSList *probes)
                        else if (probe->trigger[0] == 'r')
                                sigma->trigger.risingmask |= probebit;
                        else {
-                               sr_warn("Asix Sigma only supports "
+                               sr_warn("ASIX SIGMA only supports "
                                        "rising/falling trigger in 100 "
                                        "and 200MHz mode.");
                                return SR_ERR;
@@ -657,7 +664,7 @@ static int configure_probes(struct sr_device_instance *sdi, GSList *probes)
                          * does not permit ORed triggers.
                          */
                        if (trigger_set > 1) {
-                               sr_warn("Asix Sigma only supports 1 rising/"
+                               sr_warn("ASIX SIGMA only supports 1 rising/"
                                        "falling triggers.");
                                return SR_ERR;
                        }
@@ -670,19 +677,28 @@ static int configure_probes(struct sr_device_instance *sdi, GSList *probes)
        return SR_OK;
 }
 
-static void hw_closedev(int device_index)
+static int hw_closedev(int device_index)
 {
        struct sr_device_instance *sdi;
        struct sigma *sigma;
 
-       if ((sdi = sr_get_device_instance(device_instances, device_index)))
-       {
-               sigma = sdi->priv;
-               if (sdi->status == SR_ST_ACTIVE)
-                       ftdi_usb_close(&sigma->ftdic);
+       if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
+               sr_err("sigma: %s: sdi was NULL", __func__);
+               return SR_ERR; /* TODO: SR_ERR_ARG? */
+       }
 
-               sdi->status = SR_ST_INACTIVE;
+       if (!(sigma = sdi->priv)) {
+               sr_err("sigma: %s: sdi->priv was NULL", __func__);
+               return SR_ERR; /* TODO: SR_ERR_ARG? */
        }
+
+       /* TODO */
+       if (sdi->status == SR_ST_ACTIVE)
+               ftdi_usb_close(&sigma->ftdic);
+
+       sdi->status = SR_ST_INACTIVE;
+
+       return SR_OK;
 }
 
 static void hw_cleanup(void)
@@ -708,7 +724,7 @@ static void *hw_get_device_info(int device_index, int device_info_id)
        void *info = NULL;
 
        if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
-               fprintf(stderr, "It's NULL.\n");
+               sr_err("It's NULL.\n");
                return NULL;
        }
 
@@ -827,13 +843,14 @@ static int get_trigger_offset(uint16_t *samples, uint16_t last_sample,
  */
 static int decode_chunk_ts(uint8_t *buf, uint16_t *lastts,
                           uint16_t *lastsample, int triggerpos,
-                          uint16_t limit_chunk, void *user_data)
+                          uint16_t limit_chunk, void *session_data)
 {
-       struct sr_device_instance *sdi = user_data;
+       struct sr_device_instance *sdi = session_data;
        struct sigma *sigma = sdi->priv;
        uint16_t tsdiff, ts;
        uint16_t samples[65536 * sigma->samples_per_event];
        struct sr_datafeed_packet packet;
+       struct sr_datafeed_logic logic;
        int i, j, k, l, numpad, tosend;
        size_t n = 0, sent = 0;
        int clustersize = EVENTS_PER_CLUSTER * sigma->samples_per_event;
@@ -878,9 +895,13 @@ static int decode_chunk_ts(uint8_t *buf, uint16_t *lastts,
                        tosend = MIN(2048, n - sent);
 
                        packet.type = SR_DF_LOGIC;
-                       packet.length = tosend * sizeof(uint16_t);
-                       packet.unitsize = 2;
-                       packet.payload = samples + sent;
+                       /* TODO: fill in timeoffset and duration */
+                       packet.timeoffset = 0;
+                       packet.duration = 0;
+                       packet.payload = &logic;
+                       logic.length = tosend * sizeof(uint16_t);
+                       logic.unitsize = 2;
+                       logic.data = samples + sent;
                        sr_session_bus(sigma->session_id, &packet);
 
                        sent += tosend;
@@ -922,9 +943,13 @@ static int decode_chunk_ts(uint8_t *buf, uint16_t *lastts,
 
                        if (tosend > 0) {
                                packet.type = SR_DF_LOGIC;
-                               packet.length = tosend * sizeof(uint16_t);
-                               packet.unitsize = 2;
-                               packet.payload = samples;
+                               /* TODO: fill in timeoffset and duration */
+                               packet.timeoffset = 0;
+                               packet.duration = 0;
+                               packet.payload = &logic;
+                               logic.length = tosend * sizeof(uint16_t);
+                               logic.unitsize = 2;
+                               logic.data = samples;
                                sr_session_bus(sigma->session_id, &packet);
 
                                sent += tosend;
@@ -933,8 +958,9 @@ static int decode_chunk_ts(uint8_t *buf, uint16_t *lastts,
                        /* Only send trigger if explicitly enabled. */
                        if (sigma->use_triggers) {
                                packet.type = SR_DF_TRIGGER;
-                               packet.length = 0;
-                               packet.payload = 0;
+                               /* TODO: fill in timeoffset only */
+                               packet.timeoffset = 0;
+                               packet.duration = 0;
                                sr_session_bus(sigma->session_id, &packet);
                        }
                }
@@ -944,9 +970,13 @@ static int decode_chunk_ts(uint8_t *buf, uint16_t *lastts,
 
                if (tosend > 0) {
                        packet.type = SR_DF_LOGIC;
-                       packet.length = tosend * sizeof(uint16_t);
-                       packet.unitsize = 2;
-                       packet.payload = samples + sent;
+                       /* TODO: fill in timeoffset and duration */
+                       packet.timeoffset = 0;
+                       packet.duration = 0;
+                       packet.payload = &logic;
+                       logic.length = tosend * sizeof(uint16_t);
+                       logic.unitsize = 2;
+                       logic.data = samples + sent;
                        sr_session_bus(sigma->session_id, &packet);
                }
 
@@ -956,9 +986,9 @@ static int decode_chunk_ts(uint8_t *buf, uint16_t *lastts,
        return SR_OK;
 }
 
-static int receive_data(int fd, int revents, void *user_data)
+static int receive_data(int fd, int revents, void *session_data)
 {
-       struct sr_device_instance *sdi = user_data;
+       struct sr_device_instance *sdi = session_data;
        struct sigma *sigma = sdi->priv;
        struct sr_datafeed_packet packet;
        const int chunks_per_read = 32;
@@ -985,7 +1015,7 @@ static int receive_data(int fd, int revents, void *user_data)
                if (running_msec < sigma->limit_msec && numchunks < 32767)
                        return FALSE;
 
-               hw_stop_acquisition(sdi->index, user_data);
+               hw_stop_acquisition(sdi->index, session_data);
 
                return FALSE;
 
@@ -993,7 +1023,6 @@ static int receive_data(int fd, int revents, void *user_data)
                if (sigma->state.chunks_downloaded >= numchunks) {
                        /* End of samples. */
                        packet.type = SR_DF_END;
-                       packet.length = 0;
                        sr_session_bus(sigma->session_id, &packet);
 
                        sigma->state.state = SIGMA_IDLE;
@@ -1009,6 +1038,8 @@ static int receive_data(int fd, int revents, void *user_data)
 
                bufsz = sigma_read_dram(sigma->state.chunks_downloaded,
                                        newchunks, buf, sigma);
+               /* TODO: Check bufsz. For now, just avoid compiler warnings. */
+               (void)bufsz;
 
                /* Find first ts. */
                if (sigma->state.chunks_downloaded == 0) {
@@ -1032,12 +1063,12 @@ static int receive_data(int fd, int revents, void *user_data)
                                                &sigma->state.lastts,
                                                &sigma->state.lastsample,
                                                sigma->state.triggerpos & 0x1ff,
-                                               limit_chunk, user_data);
+                                               limit_chunk, session_data);
                        else
                                decode_chunk_ts(buf + (i * CHUNK_SIZE),
                                                &sigma->state.lastts,
                                                &sigma->state.lastsample,
-                                               -1, limit_chunk, user_data);
+                                               -1, limit_chunk, session_data);
 
                        ++sigma->state.chunks_downloaded;
                }
@@ -1202,7 +1233,7 @@ static int build_basic_trigger(struct triggerlut *lut, struct sigma *sigma)
        return SR_OK;
 }
 
-static int hw_start_acquisition(int device_index, gpointer session_device_id)
+static int hw_start_acquisition(int device_index, gpointer session_data)
 {
        struct sr_device_instance *sdi;
        struct sigma *sigma;
@@ -1214,7 +1245,7 @@ static int hw_start_acquisition(int device_index, gpointer session_device_id)
        struct triggerinout triggerinout_conf;
        struct triggerlut lut;
 
-       session_device_id = session_device_id;
+       session_data = session_data;
 
        if (!(sdi = sr_get_device_instance(device_instances, device_index)))
                return SR_ERR;
@@ -1299,19 +1330,17 @@ static int hw_start_acquisition(int device_index, gpointer session_device_id)
        gettimeofday(&sigma->start_tv, 0);
        sigma_set_register(WRITE_MODE, 0x0d, sigma);
 
-       sigma->session_id = session_device_id;
+       sigma->session_id = session_data;
 
        /* Send header packet to the session bus. */
        packet.type = SR_DF_HEADER;
-       packet.length = sizeof(struct sr_datafeed_header);
        packet.payload = &header;
        header.feed_version = 1;
        gettimeofday(&header.starttime, NULL);
        header.samplerate = sigma->cur_samplerate;
-       header.protocol_id = SR_PROTO_RAW;
        header.num_logic_probes = sigma->num_probes;
        header.num_analog_probes = 0;
-       sr_session_bus(session_device_id, &packet);
+       sr_session_bus(session_data, &packet);
 
        /* Add capture source. */
        sr_source_add(0, G_IO_IN, 10, receive_data, sdi);
@@ -1321,7 +1350,7 @@ static int hw_start_acquisition(int device_index, gpointer session_device_id)
        return SR_OK;
 }
 
-static void hw_stop_acquisition(int device_index, gpointer session_device_id)
+static void hw_stop_acquisition(int device_index, gpointer session_data)
 {
        struct sr_device_instance *sdi;
        struct sigma *sigma;
@@ -1332,7 +1361,7 @@ static void hw_stop_acquisition(int device_index, gpointer session_device_id)
 
        sigma = sdi->priv;
 
-       session_device_id = session_device_id;
+       session_data = session_data;
 
        /* Stop acquisition. */
        sigma_set_register(WRITE_MODE, 0x11, sigma);
@@ -1357,17 +1386,17 @@ static void hw_stop_acquisition(int device_index, gpointer session_device_id)
 }
 
 struct sr_device_plugin asix_sigma_plugin_info = {
-       "asix-sigma",
-       "ASIX SIGMA",
-       1,
-       hw_init,
-       hw_cleanup,
-       hw_opendev,
-       hw_closedev,
-       hw_get_device_info,
-       hw_get_status,
-       hw_get_capabilities,
-       hw_set_configuration,
-       hw_start_acquisition,
-       hw_stop_acquisition,
+       .name = "asix-sigma",
+       .longname = "ASIX SIGMA",
+       .api_version = 1,
+       .init = hw_init,
+       .cleanup = hw_cleanup,
+       .opendev = hw_opendev,
+       .closedev = hw_closedev,
+       .get_device_info = hw_get_device_info,
+       .get_status = hw_get_status,
+       .get_capabilities = hw_get_capabilities,
+       .set_configuration = hw_set_configuration,
+       .start_acquisition = hw_start_acquisition,
+       .stop_acquisition = hw_stop_acquisition,
 };