]> sigrok.org Git - libsigrok.git/blobdiff - hardware/asix-sigma/asix-sigma.c
ASIX Sigma: Fix firmware loading bug.
[libsigrok.git] / hardware / asix-sigma / asix-sigma.c
index de89f7acc947e91abeef0ff864039e00cb9b2981..3cd83d358a7b7a94e6228a1cb5e6ec6dc1ddca21 100644 (file)
  * ASIX Sigma Logic Analyzer Driver
  */
 
+#include "config.h"
+#include <glib.h>
+#include <glib/gstdio.h>
 #include <ftdi.h>
 #include <string.h>
 #include <zlib.h>
 #include <sigrok.h>
-#include <config.h>
 #include "asix-sigma.h"
 
 #define USB_VENDOR                     0xa600
 static GSList *device_instances = NULL;
 
 static uint64_t supported_samplerates[] = {
-       KHZ(200),
-       KHZ(250),
-       KHZ(500),
-       MHZ(1),
-       MHZ(5),
-       MHZ(10),
-       MHZ(25),
-       MHZ(50),
-       MHZ(100),
-       MHZ(200),
+       SR_KHZ(200),
+       SR_KHZ(250),
+       SR_KHZ(500),
+       SR_MHZ(1),
+       SR_MHZ(5),
+       SR_MHZ(10),
+       SR_MHZ(25),
+       SR_MHZ(50),
+       SR_MHZ(100),
+       SR_MHZ(200),
        0,
 };
 
-static struct samplerates samplerates = {
-       KHZ(200),
-       MHZ(200),
-       0,
+static struct sr_samplerates samplerates = {
+       SR_KHZ(200),
+       SR_MHZ(200),
+       SR_HZ(0),
        supported_samplerates,
 };
 
 static int capabilities[] = {
-       HWCAP_LOGIC_ANALYZER,
-       HWCAP_SAMPLERATE,
-       HWCAP_CAPTURE_RATIO,
-       HWCAP_PROBECONFIG,
+       SR_HWCAP_LOGIC_ANALYZER,
+       SR_HWCAP_SAMPLERATE,
+       SR_HWCAP_CAPTURE_RATIO,
+       SR_HWCAP_PROBECONFIG,
 
-       HWCAP_LIMIT_MSEC,
+       SR_HWCAP_LIMIT_MSEC,
        0,
 };
 
@@ -312,9 +314,9 @@ static int bin2bitbang(const char *filename,
        int c, ret, bit, v;
        uint32_t imm = 0x3f6df2ab;
 
-       f = fopen(filename, "r");
+       f = g_fopen(filename, "rb");
        if (!f) {
-               g_warning("fopen(\"%s\", \"r\")", filename);
+               g_warning("g_fopen(\"%s\", \"rb\")", filename);
                return -1;
        }
 
@@ -385,7 +387,7 @@ static int bin2bitbang(const char *filename,
        return 0;
 }
 
-static int hw_init(char *deviceinfo)
+static int hw_init(const char *deviceinfo)
 {
        struct sr_device_instance *sdi;
        struct sigma *sigma = g_malloc(sizeof(struct sigma));
@@ -411,7 +413,7 @@ static int hw_init(char *deviceinfo)
        sigma->use_triggers = 0;
 
        /* Register SIGMA device. */
-       sdi = sr_device_instance_new(0, ST_INITIALIZING,
+       sdi = sr_device_instance_new(0, SR_ST_INITIALIZING,
                        USB_VENDOR_NAME, USB_MODEL_NAME, USB_MODEL_VERSION);
        if (!sdi)
                goto free;
@@ -541,7 +543,7 @@ static int hw_opendev(int device_index)
                return 0;
        }
 
-       sdi->status = ST_ACTIVE;
+       sdi->status = SR_ST_ACTIVE;
 
        return SR_OK;
 }
@@ -559,15 +561,15 @@ static int set_samplerate(struct sr_device_instance *sdi,
        if (supported_samplerates[i] == 0)
                return SR_ERR_SAMPLERATE;
 
-       if (samplerate <= MHZ(50)) {
+       if (samplerate <= SR_MHZ(50)) {
                ret = upload_firmware(0, sigma);
                sigma->num_probes = 16;
        }
-       if (samplerate == MHZ(100)) {
+       if (samplerate == SR_MHZ(100)) {
                ret = upload_firmware(1, sigma);
                sigma->num_probes = 8;
        }
-       else if (samplerate == MHZ(200)) {
+       else if (samplerate == SR_MHZ(200)) {
                ret = upload_firmware(2, sigma);
                sigma->num_probes = 4;
        }
@@ -592,7 +594,7 @@ static int set_samplerate(struct sr_device_instance *sdi,
 static int configure_probes(struct sr_device_instance *sdi, GSList *probes)
 {
        struct sigma *sigma = sdi->priv;
-       struct probe *probe;
+       struct sr_probe *probe;
        GSList *l;
        int trigger_set = 0;
        int probebit;
@@ -600,13 +602,13 @@ static int configure_probes(struct sr_device_instance *sdi, GSList *probes)
        memset(&sigma->trigger, 0, sizeof(struct sigma_trigger));
 
        for (l = probes; l; l = l->next) {
-               probe = (struct probe *)l->data;
+               probe = (struct sr_probe *)l->data;
                probebit = 1 << (probe->index - 1);
 
                if (!probe->enabled || !probe->trigger)
                        continue;
 
-               if (sigma->cur_samplerate >= MHZ(100)) {
+               if (sigma->cur_samplerate >= SR_MHZ(100)) {
                        /* Fast trigger support. */
                        if (trigger_set) {
                                g_warning("Asix Sigma only supports a single "
@@ -672,10 +674,10 @@ static void hw_closedev(int device_index)
        if ((sdi = sr_get_device_instance(device_instances, device_index)))
        {
                sigma = sdi->priv;
-               if (sdi->status == ST_ACTIVE)
+               if (sdi->status == SR_ST_ACTIVE)
                        ftdi_usb_close(&sigma->ftdic);
 
-               sdi->status = ST_INACTIVE;
+               sdi->status = SR_ST_INACTIVE;
        }
 }
 
@@ -709,19 +711,19 @@ static void *hw_get_device_info(int device_index, int device_info_id)
        sigma = sdi->priv;
 
        switch (device_info_id) {
-       case DI_INSTANCE:
+       case SR_DI_INSTANCE:
                info = sdi;
                break;
-       case DI_NUM_PROBES:
+       case SR_DI_NUM_PROBES:
                info = GINT_TO_POINTER(16);
                break;
-       case DI_SAMPLERATES:
+       case SR_DI_SAMPLERATES:
                info = &samplerates;
                break;
-       case DI_TRIGGER_TYPES:
+       case SR_DI_TRIGGER_TYPES:
                info = (char *)TRIGGER_TYPES;
                break;
-       case DI_CUR_SAMPLERATE:
+       case SR_DI_CUR_SAMPLERATE:
                info = &sigma->cur_samplerate;
                break;
        }
@@ -737,7 +739,7 @@ static int hw_get_status(int device_index)
        if (sdi)
                return sdi->status;
        else
-               return ST_NOT_FOUND;
+               return SR_ST_NOT_FOUND;
 }
 
 static int *hw_get_capabilities(void)
@@ -756,17 +758,17 @@ static int hw_set_configuration(int device_index, int capability, void *value)
 
        sigma = sdi->priv;
 
-       if (capability == HWCAP_SAMPLERATE) {
+       if (capability == SR_HWCAP_SAMPLERATE) {
                ret = set_samplerate(sdi, *(uint64_t*) value);
-       } else if (capability == HWCAP_PROBECONFIG) {
+       } else if (capability == SR_HWCAP_PROBECONFIG) {
                ret = configure_probes(sdi, value);
-       } else if (capability == HWCAP_LIMIT_MSEC) {
+       } else if (capability == SR_HWCAP_LIMIT_MSEC) {
                sigma->limit_msec = *(uint64_t*) value;
                if (sigma->limit_msec > 0)
                        ret = SR_OK;
                else
                        ret = SR_ERR;
-       } else if (capability == HWCAP_CAPTURE_RATIO) {
+       } else if (capability == SR_HWCAP_CAPTURE_RATIO) {
                sigma->capture_ratio = *(uint64_t*) value;
                if (sigma->capture_ratio < 0 || sigma->capture_ratio > 100)
                        ret = SR_ERR;
@@ -837,7 +839,7 @@ static int decode_chunk_ts(uint8_t *buf, uint16_t *lastts,
 
        /* Check if trigger is in this chunk. */
        if (triggerpos != -1) {
-               if (sigma->cur_samplerate <= MHZ(50))
+               if (sigma->cur_samplerate <= SR_MHZ(50))
                        triggerpos -= EVENTS_PER_CLUSTER - 1;
 
                if (triggerpos < 0)
@@ -871,11 +873,11 @@ static int decode_chunk_ts(uint8_t *buf, uint16_t *lastts,
                while (sent < n) {
                        tosend = MIN(2048, n - sent);
 
-                       packet.type = DF_LOGIC;
+                       packet.type = SR_DF_LOGIC;
                        packet.length = tosend * sizeof(uint16_t);
                        packet.unitsize = 2;
                        packet.payload = samples + sent;
-                       session_bus(sigma->session_id, &packet);
+                       sr_session_bus(sigma->session_id, &packet);
 
                        sent += tosend;
                }
@@ -915,21 +917,21 @@ static int decode_chunk_ts(uint8_t *buf, uint16_t *lastts,
                                                    &sigma->trigger);
 
                        if (tosend > 0) {
-                               packet.type = DF_LOGIC;
+                               packet.type = SR_DF_LOGIC;
                                packet.length = tosend * sizeof(uint16_t);
                                packet.unitsize = 2;
                                packet.payload = samples;
-                               session_bus(sigma->session_id, &packet);
+                               sr_session_bus(sigma->session_id, &packet);
 
                                sent += tosend;
                        }
 
                        /* Only send trigger if explicitly enabled. */
                        if (sigma->use_triggers) {
-                               packet.type = DF_TRIGGER;
+                               packet.type = SR_DF_TRIGGER;
                                packet.length = 0;
                                packet.payload = 0;
-                               session_bus(sigma->session_id, &packet);
+                               sr_session_bus(sigma->session_id, &packet);
                        }
                }
 
@@ -937,11 +939,11 @@ static int decode_chunk_ts(uint8_t *buf, uint16_t *lastts,
                tosend = n - sent;
 
                if (tosend > 0) {
-                       packet.type = DF_LOGIC;
+                       packet.type = SR_DF_LOGIC;
                        packet.length = tosend * sizeof(uint16_t);
                        packet.unitsize = 2;
                        packet.payload = samples + sent;
-                       session_bus(sigma->session_id, &packet);
+                       sr_session_bus(sigma->session_id, &packet);
                }
 
                *lastsample = samples[n - 1];
@@ -986,9 +988,9 @@ static int receive_data(int fd, int revents, void *user_data)
        } else if (sigma->state.state == SIGMA_DOWNLOAD) {
                if (sigma->state.chunks_downloaded >= numchunks) {
                        /* End of samples. */
-                       packet.type = DF_END;
+                       packet.type = SR_DF_END;
                        packet.length = 0;
-                       session_bus(sigma->session_id, &packet);
+                       sr_session_bus(sigma->session_id, &packet);
 
                        sigma->state.state = SIGMA_IDLE;
 
@@ -1203,11 +1205,10 @@ static int hw_start_acquisition(int device_index, gpointer session_device_id)
        struct sr_datafeed_packet packet;
        struct sr_datafeed_header header;
        struct clockselect_50 clockselect;
-       int frac;
+       int frac, triggerpin, ret;
        uint8_t triggerselect;
        struct triggerinout triggerinout_conf;
        struct triggerlut lut;
-       int triggerpin;
 
        session_device_id = session_device_id;
 
@@ -1217,14 +1218,16 @@ static int hw_start_acquisition(int device_index, gpointer session_device_id)
        sigma = sdi->priv;
 
        /* If the samplerate has not been set, default to 200 KHz. */
-       if (sigma->cur_firmware == -1)
-               set_samplerate(sdi, KHZ(200));
+       if (sigma->cur_firmware == -1) {
+               if ((ret = set_samplerate(sdi, SR_KHZ(200))) != SR_OK)
+                       return ret;
+       }
 
        /* Enter trigger programming mode. */
        sigma_set_register(WRITE_TRIGGER_SELECT1, 0x20, sigma);
 
        /* 100 and 200 MHz mode. */
-       if (sigma->cur_samplerate >= MHZ(100)) {
+       if (sigma->cur_samplerate >= SR_MHZ(100)) {
                sigma_set_register(WRITE_TRIGGER_SELECT1, 0x81, sigma);
 
                /* Find which pin to trigger on from mask. */
@@ -1241,7 +1244,7 @@ static int hw_start_acquisition(int device_index, gpointer session_device_id)
                        triggerselect |= 1 << 3;
 
        /* All other modes. */
-       } else if (sigma->cur_samplerate <= MHZ(50)) {
+       } else if (sigma->cur_samplerate <= SR_MHZ(50)) {
                build_basic_trigger(&lut, sigma);
 
                sigma_write_trigger_lut(&lut, sigma);
@@ -1262,10 +1265,10 @@ static int hw_start_acquisition(int device_index, gpointer session_device_id)
        sigma_set_register(WRITE_TRIGGER_SELECT1, triggerselect, sigma);
 
        /* Set clock select register. */
-       if (sigma->cur_samplerate == MHZ(200))
+       if (sigma->cur_samplerate == SR_MHZ(200))
                /* Enable 4 probes. */
                sigma_set_register(WRITE_CLOCK_SELECT, 0xf0, sigma);
-       else if (sigma->cur_samplerate == MHZ(100))
+       else if (sigma->cur_samplerate == SR_MHZ(100))
                /* Enable 8 probes. */
                sigma_set_register(WRITE_CLOCK_SELECT, 0x00, sigma);
        else {
@@ -1273,7 +1276,7 @@ static int hw_start_acquisition(int device_index, gpointer session_device_id)
                 * 50 MHz mode (or fraction thereof). Any fraction down to
                 * 50 MHz / 256 can be used, but is not supported by sigrok API.
                 */
-               frac = MHZ(50) / sigma->cur_samplerate - 1;
+               frac = SR_MHZ(50) / sigma->cur_samplerate - 1;
 
                clockselect.async = 0;
                clockselect.fraction = frac;
@@ -1295,19 +1298,19 @@ static int hw_start_acquisition(int device_index, gpointer session_device_id)
        sigma->session_id = session_device_id;
 
        /* Send header packet to the session bus. */
-       packet.type = DF_HEADER;
+       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 = PROTO_RAW;
+       header.protocol_id = SR_PROTO_RAW;
        header.num_logic_probes = sigma->num_probes;
        header.num_analog_probes = 0;
-       session_bus(session_device_id, &packet);
+       sr_session_bus(session_device_id, &packet);
 
        /* Add capture source. */
-       source_add(0, G_IO_IN, 10, receive_data, sdi);
+       sr_source_add(0, G_IO_IN, 10, receive_data, sdi);
 
        sigma->state.state = SIGMA_CAPTURE;
 
@@ -1349,7 +1352,7 @@ static void hw_stop_acquisition(int device_index, gpointer session_device_id)
        sigma->state.state = SIGMA_DOWNLOAD;
 }
 
-struct device_plugin asix_sigma_plugin_info = {
+struct sr_device_plugin asix_sigma_plugin_info = {
        "asix-sigma",
        "ASIX SIGMA",
        1,