]> sigrok.org Git - libsigrok.git/blobdiff - src/hardware/asix-sigma/asix-sigma.c
Java: Remove explicit constructor deletes since SWIG 2 doesn't like them
[libsigrok.git] / src / hardware / asix-sigma / asix-sigma.c
index 24c786da0d65b9f420934d70831ea1c6477b5e7d..9db557b3f69379f714025aeeb74e88e75300f8ca 100644 (file)
  * ASIX SIGMA/SIGMA2 logic analyzer driver
  */
 
+#include <config.h>
 #include <glib.h>
 #include <glib/gstdio.h>
 #include <ftdi.h>
 #include <string.h>
 #include <unistd.h>
-#include "libsigrok.h"
+#include <libsigrok/libsigrok.h>
 #include "libsigrok-internal.h"
 #include "asix-sigma.h"
 
@@ -39,7 +40,6 @@
 #define USB_MODEL_NAME                 "SIGMA"
 
 SR_PRIV struct sr_dev_driver asix_sigma_driver_info;
-static struct sr_dev_driver *di = &asix_sigma_driver_info;
 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data);
 
 /*
@@ -71,13 +71,13 @@ static const char *channel_names[] = {
        "9", "10", "11", "12", "13", "14", "15", "16",
 };
 
-static const uint32_t devopts[] = {
+static const uint32_t drvopts[] = {
        SR_CONF_LOGIC_ANALYZER,
-       SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
-       SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
 };
 
-static const uint32_t devopts_global[] = {
+static const uint32_t devopts[] = {
+       SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
+       SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
        SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
        SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
        SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET,
@@ -90,17 +90,17 @@ static const int32_t trigger_matches[] = {
        SR_TRIGGER_FALLING,
 };
 
-static const char *sigma_firmware_files[] = {
+static const char sigma_firmware_files[][24] = {
        /* 50 MHz, supports 8 bit fractions */
-       FIRMWARE_DIR "/asix-sigma-50.fw",
+       "asix-sigma-50.fw",
        /* 100 MHz */
-       FIRMWARE_DIR "/asix-sigma-100.fw",
+       "asix-sigma-100.fw",
        /* 200 MHz */
-       FIRMWARE_DIR "/asix-sigma-200.fw",
+       "asix-sigma-200.fw",
        /* Synchronous clock from pin */
-       FIRMWARE_DIR "/asix-sigma-50sync.fw",
+       "asix-sigma-50sync.fw",
        /* Frequency counter */
-       FIRMWARE_DIR "/asix-sigma-phasor.fw",
+       "asix-sigma-phasor.fw",
 };
 
 static int sigma_read(void *buf, size_t size, struct dev_context *devc)
@@ -131,13 +131,23 @@ static int sigma_write(void *buf, size_t size, struct dev_context *devc)
        return ret;
 }
 
+/*
+ * NOTE: We chose the buffer size to be large enough to hold any write to the
+ * device. We still print a message just in case.
+ */
 static int sigma_write_register(uint8_t reg, uint8_t *data, size_t len,
                                struct dev_context *devc)
 {
        size_t i;
-       uint8_t buf[len + 2];
+       uint8_t buf[80];
        int idx = 0;
 
+       if ((len + 2) > sizeof(buf)) {
+               sr_err("Attempted to write %zu bytes, but buffer is too small.",
+                      len + 2);
+               return SR_ERR_BUG;
+       }
+
        buf[idx++] = REG_ADDR_LOW | (reg & 0xf);
        buf[idx++] = REG_ADDR_HIGH | (reg >> 4);
 
@@ -312,20 +322,19 @@ static void clear_helper(void *priv)
        ftdi_deinit(&devc->ftdic);
 }
 
-static int dev_clear(void)
+static int dev_clear(const struct sr_dev_driver *di)
 {
        return std_dev_clear(di, clear_helper);
 }
 
-static int init(struct sr_context *sr_ctx)
+static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
 {
        return std_init(sr_ctx, di, LOG_PREFIX);
 }
 
-static GSList *scan(GSList *options)
+static GSList *scan(struct sr_dev_driver *di, GSList *options)
 {
        struct sr_dev_inst *sdi;
-       struct sr_channel *ch;
        struct drv_context *drvc;
        struct dev_context *devc;
        GSList *devices;
@@ -337,14 +346,11 @@ static GSList *scan(GSList *options)
 
        (void)options;
 
-       drvc = di->priv;
+       drvc = di->context;
 
        devices = NULL;
 
-       if (!(devc = g_try_malloc(sizeof(struct dev_context)))) {
-               sr_err("%s: devc malloc failed", __func__);
-               return NULL;
-       }
+       devc = g_malloc0(sizeof(struct dev_context));
 
        ftdi_init(&devc->ftdic);
 
@@ -380,19 +386,14 @@ static GSList *scan(GSList *options)
        devc->use_triggers = 0;
 
        /* Register SIGMA device. */
-       sdi = sr_dev_inst_new();
+       sdi = g_malloc0(sizeof(struct sr_dev_inst));
        sdi->status = SR_ST_INITIALIZING;
        sdi->vendor = g_strdup(USB_VENDOR_NAME);
        sdi->model = g_strdup(USB_MODEL_NAME);
        sdi->driver = di;
 
-       for (i = 0; i < ARRAY_SIZE(channel_names); i++) {
-               ch = sr_channel_new(i, SR_CHANNEL_LOGIC, TRUE,
-                                   channel_names[i]);
-               if (!ch)
-                       return NULL;
-               sdi->channels = g_slist_append(sdi->channels, ch);
-       }
+       for (i = 0; i < ARRAY_SIZE(channel_names); i++)
+               sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, channel_names[i]);
 
        devices = g_slist_append(devices, sdi);
        drvc->instances = g_slist_append(drvc->instances, sdi);
@@ -409,9 +410,9 @@ free:
        return NULL;
 }
 
-static GSList *dev_list(void)
+static GSList *dev_list(const struct sr_dev_driver *di)
 {
-       return ((struct drv_context *)(di->priv))->instances;
+       return ((struct drv_context *)(di->context))->instances;
 }
 
 /*
@@ -429,7 +430,7 @@ static int sigma_fpga_init_bitbang(struct dev_context *devc)
                0x01, 0x03, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01,
                0x01, 0x01,
        };
-       int i, ret, timeout = 10000;
+       int i, ret, timeout = (10 * 1000);
        uint8_t data;
 
        /* Section 2. part 1), do the FPGA suicide. */
@@ -451,7 +452,7 @@ static int sigma_fpga_init_bitbang(struct dev_context *devc)
                if (data & (1 << 5))
                        return 0;
                /* The D6 was not asserted yet, wait a bit. */
-               usleep(10000);
+               g_usleep(10 * 1000);
        }
 
        return SR_ERR_TIMEOUT;
@@ -507,31 +508,20 @@ err:
  * pulses used to program the FPGA. Note that the *bb_cmd must be free()'d
  * by the caller of this function.
  */
-static int sigma_fw_2_bitbang(const char *filename,
+static int sigma_fw_2_bitbang(struct sr_context *ctx, const char *name,
                              uint8_t **bb_cmd, gsize *bb_cmd_size)
 {
-       GMappedFile *file;
-       GError *error;
-       gsize i, file_size, bb_size;
-       gchar *firmware;
+       size_t i, file_size, bb_size;
+       char *firmware;
        uint8_t *bb_stream, *bbs;
        uint32_t imm;
        int bit, v;
        int ret = SR_OK;
 
-       /*
-        * Map the file and make the mapped buffer writable.
-        * NOTE: Using writable=TRUE does _NOT_ mean that file that is mapped
-        *       will be modified. It will not be modified until someone uses
-        *       g_file_set_contents() on it.
-        */
-       error = NULL;
-       file = g_mapped_file_new(filename, TRUE, &error);
-       g_assert_no_error(error);
-
-       file_size = g_mapped_file_get_length(file);
-       firmware = g_mapped_file_get_contents(file);
-       g_assert(firmware);
+       firmware = sr_resource_load(ctx, SR_RESOURCE_FIRMWARE,
+                       name, &file_size, 256 * 1024);
+       if (!firmware)
+               return SR_ERR;
 
        /* Weird magic transformation below, I have no idea what it does. */
        imm = 0x3f6df2ab;
@@ -570,11 +560,12 @@ static int sigma_fw_2_bitbang(const char *filename,
        *bb_cmd_size = bb_size;
 
 exit:
-       g_mapped_file_unref(file);
+       g_free(firmware);
        return ret;
 }
 
-static int upload_firmware(int firmware_idx, struct dev_context *devc)
+static int upload_firmware(struct sr_context *ctx,
+               int firmware_idx, struct dev_context *devc)
 {
        int ret;
        unsigned char *buf;
@@ -600,7 +591,7 @@ static int upload_firmware(int firmware_idx, struct dev_context *devc)
        }
 
        /* Four times the speed of sigmalogan - Works well. */
-       ret = ftdi_set_baudrate(ftdic, 750000);
+       ret = ftdi_set_baudrate(ftdic, 750 * 1000);
        if (ret < 0) {
                sr_err("ftdi_set_baudrate failed: %s",
                       ftdi_get_error_string(ftdic));
@@ -613,14 +604,14 @@ static int upload_firmware(int firmware_idx, struct dev_context *devc)
                return ret;
 
        /* Prepare firmware. */
-       ret = sigma_fw_2_bitbang(firmware, &buf, &buf_size);
+       ret = sigma_fw_2_bitbang(ctx, firmware, &buf, &buf_size);
        if (ret != SR_OK) {
-               sr_err("An error occured while reading the firmware: %s",
+               sr_err("An error occurred while reading the firmware: %s",
                       firmware);
                return ret;
        }
 
-       /* Upload firmare. */
+       /* Upload firmware. */
        sr_info("Uploading firmware file '%s'.", firmware);
        sigma_write(buf, buf_size, devc);
 
@@ -676,10 +667,12 @@ static int dev_open(struct sr_dev_inst *sdi)
 static int set_samplerate(const struct sr_dev_inst *sdi, uint64_t samplerate)
 {
        struct dev_context *devc;
+       struct drv_context *drvc;
        unsigned int i;
        int ret;
 
        devc = sdi->priv;
+       drvc = sdi->driver->context;
        ret = SR_OK;
 
        for (i = 0; i < ARRAY_SIZE(samplerates); i++) {
@@ -690,13 +683,13 @@ static int set_samplerate(const struct sr_dev_inst *sdi, uint64_t samplerate)
                return SR_ERR_SAMPLERATE;
 
        if (samplerate <= SR_MHZ(50)) {
-               ret = upload_firmware(0, devc);
+               ret = upload_firmware(drvc->sr_ctx, 0, devc);
                devc->num_channels = 16;
        } else if (samplerate == SR_MHZ(100)) {
-               ret = upload_firmware(1, devc);
+               ret = upload_firmware(drvc->sr_ctx, 1, devc);
                devc->num_channels = 8;
        } else if (samplerate == SR_MHZ(200)) {
-               ret = upload_firmware(2, devc);
+               ret = upload_firmware(drvc->sr_ctx, 2, devc);
                devc->num_channels = 4;
        }
 
@@ -792,7 +785,6 @@ static int convert_trigger(const struct sr_dev_inst *sdi)
                }
        }
 
-
        return SR_OK;
 }
 
@@ -811,9 +803,9 @@ static int dev_close(struct sr_dev_inst *sdi)
        return SR_OK;
 }
 
-static int cleanup(void)
+static int cleanup(const struct sr_dev_driver *di)
 {
-       return dev_clear();
+       return dev_clear(di);
 }
 
 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
@@ -894,17 +886,16 @@ static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *
        GVariant *gvar;
        GVariantBuilder gvb;
 
-       (void)sdi;
        (void)cg;
 
        switch (key) {
        case SR_CONF_DEVICE_OPTIONS:
                if (!sdi)
                        *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
-                                       devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
+                                       drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
                else
                        *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
-                                       devopts_global, ARRAY_SIZE(devopts_global), sizeof(uint32_t));
+                                       devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
                break;
        case SR_CONF_SAMPLERATE:
                g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
@@ -958,7 +949,6 @@ static int get_trigger_offset(uint8_t *samples, uint16_t last_sample,
        return i & 0x7;
 }
 
-
 /*
  * Return the timestamp of "DRAM cluster".
  */
@@ -1093,7 +1083,7 @@ static int decode_chunk_ts(struct sigma_dram_line *dram_line,
                                             trigger_event);
                }
 
-               /* Find in which cluster the trigger occured. */
+               /* Find in which cluster the trigger occurred. */
                trigger_cluster = trigger_event / EVENTS_PER_CLUSTER;
        }
 
@@ -1378,7 +1368,7 @@ static int build_basic_trigger(struct triggerlut *lut, struct dev_context *devc)
 
        memset(lut, 0, sizeof(struct triggerlut));
 
-       /* Contant for simple triggers. */
+       /* Constant for simple triggers. */
        lut->m4 = 0xa000;
 
        /* Value/mask trigger support. */
@@ -1520,7 +1510,7 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
        std_session_send_df_header(sdi, LOG_PREFIX);
 
        /* Add capture source. */
-       sr_session_source_add(sdi->session, 0, G_IO_IN, 10, receive_data, (void *)sdi);
+       sr_session_source_add(sdi->session, -1, 0, 10, receive_data, (void *)sdi);
 
        devc->state.state = SIGMA_CAPTURE;
 
@@ -1536,7 +1526,7 @@ static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
        devc = sdi->priv;
        devc->state.state = SIGMA_IDLE;
 
-       sr_session_source_remove(sdi->session, 0);
+       sr_session_source_remove(sdi->session, -1);
 
        return SR_OK;
 }
@@ -1557,5 +1547,5 @@ SR_PRIV struct sr_dev_driver asix_sigma_driver_info = {
        .dev_close = dev_close,
        .dev_acquisition_start = dev_acquisition_start,
        .dev_acquisition_stop = dev_acquisition_stop,
-       .priv = NULL,
+       .context = NULL,
 };