]> sigrok.org Git - libsigrok.git/blobdiff - src/hardware/fx2lafw/api.c
Minor cosmetics, cleanups.
[libsigrok.git] / src / hardware / fx2lafw / api.c
index 41f71d1bb2ab86d5585d82e029dbd1e327f7697c..5e1f66432298363178399e9031a940beb3ed4a83 100644 (file)
@@ -19,6 +19,7 @@
  */
 
 #include "protocol.h"
+#include "dslogic.h"
 
 static const struct fx2lafw_profile supported_fx2[] = {
        /*
@@ -44,6 +45,20 @@ static const struct fx2lafw_profile supported_fx2[] = {
                FIRMWARE_DIR "/fx2lafw-cwav-usbeesx.fw",
                0, NULL, NULL},
 
+       /*
+        * DreamSourceLab DSLogic (before FW upload)
+        */
+       { 0x2a0e, 0x0001, "DreamSourceLab", "DSLogic", NULL,
+               FIRMWARE_DIR "/dreamsourcelab-dslogic-fx2.fw",
+               DEV_CAPS_16BIT, NULL, NULL},
+
+       /*
+        * DreamSourceLab DSLogic (after FW upload)
+        */
+       { 0x2a0e, 0x0001, "DreamSourceLab", "DSLogic", NULL,
+               FIRMWARE_DIR "/dreamsourcelab-dslogic-fx2.fw",
+               DEV_CAPS_16BIT, "DreamSourceLab", "DSLogic"},
+
        /*
         * Saleae Logic
         * EE Electronics ESLA100
@@ -73,17 +88,21 @@ static const struct fx2lafw_profile supported_fx2[] = {
        { 0, 0, 0, 0, 0, 0, 0, 0, 0 }
 };
 
+static const uint32_t drvopts[] = {
+       SR_CONF_LOGIC_ANALYZER,
+};
+
 static const uint32_t scanopts[] = {
        SR_CONF_CONN,
 };
 
 static const uint32_t devopts[] = {
-       SR_CONF_LOGIC_ANALYZER,
-       SR_CONF_CONTINUOUS,
+       SR_CONF_CONTINUOUS | SR_CONF_SET,
+       SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
        SR_CONF_CONN | SR_CONF_GET,
        SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
-       SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
        SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
+       SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET,
 };
 
 static const char *channel_names[] = {
@@ -119,24 +138,42 @@ static const uint64_t samplerates[] = {
        SR_MHZ(24),
 };
 
+static const uint64_t dslogic_samplerates[] = {
+       SR_KHZ(10),
+       SR_KHZ(20),
+       SR_KHZ(50),
+       SR_KHZ(100),
+       SR_KHZ(200),
+       SR_KHZ(500),
+       SR_MHZ(1),
+       SR_MHZ(2),
+       SR_MHZ(5),
+       SR_MHZ(10),
+       SR_MHZ(20),
+       SR_MHZ(25),
+       SR_MHZ(50),
+       SR_MHZ(100),
+       SR_MHZ(200),
+       SR_MHZ(400),
+};
+
 SR_PRIV struct sr_dev_driver fx2lafw_driver_info;
-static struct sr_dev_driver *di = &fx2lafw_driver_info;
 
-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 drv_context *drvc;
        struct dev_context *devc;
        struct sr_dev_inst *sdi;
        struct sr_usb_dev_inst *usb;
-       struct sr_channel *ch;
        struct sr_config *src;
        const struct fx2lafw_profile *prof;
        GSList *l, *devices, *conn_devices;
+       gboolean has_firmware;
        struct libusb_device_descriptor des;
        libusb_device **devlist;
        struct libusb_device_handle *hdl;
@@ -238,7 +275,7 @@ static GSList *scan(GSList *options)
                if (!prof)
                        continue;
 
-               sdi = sr_dev_inst_new();
+               sdi = g_malloc0(sizeof(struct sr_dev_inst));
                sdi->status = SR_ST_INITIALIZING;
                sdi->vendor = g_strdup(prof->vendor);
                sdi->model = g_strdup(prof->model);
@@ -249,12 +286,9 @@ static GSList *scan(GSList *options)
 
                /* Fill in channellist according to this device's profile. */
                num_logic_channels = prof->dev_caps & DEV_CAPS_16BIT ? 16 : 8;
-               for (j = 0; j < num_logic_channels; j++) {
-                       if (!(ch = sr_channel_new(j, SR_CHANNEL_LOGIC, TRUE,
-                                       channel_names[j])))
-                               return NULL;
-                       sdi->channels = g_slist_append(sdi->channels, ch);
-               }
+               for (j = 0; j < num_logic_channels; j++)
+                       sr_channel_new(sdi, j, SR_CHANNEL_LOGIC, TRUE,
+                                       channel_names[j]);
 
                devc = fx2lafw_dev_new();
                devc->profile = prof;
@@ -263,7 +297,21 @@ static GSList *scan(GSList *options)
                drvc->instances = g_slist_append(drvc->instances, sdi);
                devices = g_slist_append(devices, sdi);
 
-               if (fx2lafw_check_conf_profile(devlist[i])) {
+               if (strcmp(prof->model, "DSLogic")) {
+                       devc->dslogic = FALSE;
+                       devc->samplerates = samplerates;
+                       devc->num_samplerates = ARRAY_SIZE(samplerates);
+                       has_firmware = match_manuf_prod(devlist[i],
+                                       "sigrok", "fx2lafw");
+               } else {
+                       devc->dslogic = TRUE;
+                       devc->samplerates = dslogic_samplerates;
+                       devc->num_samplerates = ARRAY_SIZE(dslogic_samplerates);
+                       has_firmware = match_manuf_prod(devlist[i],
+                                       "DreamSourceLab", "DSLogic");
+               }
+
+               if (has_firmware) {
                        /* Already has the firmware, so fix the new address. */
                        sr_dbg("Found an fx2lafw device.");
                        sdi->status = SR_ST_INACTIVE;
@@ -291,13 +339,14 @@ static GSList *scan(GSList *options)
        return devices;
 }
 
-static GSList *dev_list(void)
+static GSList *dev_list(const struct sr_dev_driver *di)
 {
        return ((struct drv_context *)(di->priv))->instances;
 }
 
 static int dev_open(struct sr_dev_inst *sdi)
 {
+       struct sr_dev_driver *di = sdi->driver;
        struct sr_usb_dev_inst *usb;
        struct dev_context *devc;
        int ret;
@@ -359,9 +408,15 @@ static int dev_open(struct sr_dev_inst *sdi)
                return SR_ERR;
        }
 
+       if (devc->dslogic) {
+               if ((ret = dslogic_fpga_firmware_upload(sdi,
+                               DSLOGIC_FPGA_FIRMWARE)) != SR_OK)
+                       return ret;
+       }
+
        if (devc->cur_samplerate == 0) {
                /* Samplerate hasn't been set; default to the slowest one. */
-               devc->cur_samplerate = samplerates[0];
+               devc->cur_samplerate = devc->samplerates[0];
        }
 
        return SR_OK;
@@ -385,7 +440,7 @@ 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)
 {
        int ret;
        struct drv_context *drvc;
@@ -396,7 +451,6 @@ static int cleanup(void)
        ret = std_dev_clear(di, NULL);
 
        g_free(drvc);
-       di->priv = NULL;
 
        return ret;
 }
@@ -433,6 +487,9 @@ static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *s
        case SR_CONF_SAMPLERATE:
                *data = g_variant_new_uint64(devc->cur_samplerate);
                break;
+       case SR_CONF_CAPTURE_RATIO:
+               *data = g_variant_new_uint64(devc->capture_ratio);
+               break;
        default:
                return SR_ERR_NA;
        }
@@ -475,6 +532,14 @@ static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sd
                case SR_CONF_LIMIT_SAMPLES:
                        devc->limit_samples = g_variant_get_uint64(data);
                        break;
+               case SR_CONF_CAPTURE_RATIO:
+                       devc->capture_ratio = g_variant_get_uint64(data);
+                       if (devc->capture_ratio > 100) {
+                               devc->capture_ratio = 0;
+                               ret = SR_ERR;
+                       } else
+                               ret = SR_OK;
+                       break;
                default:
                        ret = SR_ERR_NA;
        }
@@ -485,10 +550,10 @@ static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sd
 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
                const struct sr_channel_group *cg)
 {
+       struct dev_context *devc;
        GVariant *gvar;
        GVariantBuilder gvb;
 
-       (void)sdi;
        (void)cg;
 
        switch (key) {
@@ -497,13 +562,20 @@ static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *
                                scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
                break;
        case SR_CONF_DEVICE_OPTIONS:
-               *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
-                               devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
+               if (!sdi)
+                       *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
+                                       drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
+               else
+                       *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
+                                       devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
                break;
        case SR_CONF_SAMPLERATE:
+               if (!sdi->priv)
+                       return SR_ERR_ARG;
+               devc = sdi->priv;
                g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
-               gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"), samplerates,
-                               ARRAY_SIZE(samplerates), sizeof(uint64_t));
+               gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"), devc->samplerates,
+                               devc->num_samplerates, sizeof(uint64_t));
                g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
                *data = g_variant_builder_end(&gvb);
                break;
@@ -526,9 +598,8 @@ static int receive_data(int fd, int revents, void *cb_data)
 
        (void)fd;
        (void)revents;
-       (void)cb_data;
 
-       drvc = di->priv;
+       drvc = (struct drv_context *)cb_data;
 
        tv.tv_sec = tv.tv_usec = 0;
        libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
@@ -536,37 +607,37 @@ static int receive_data(int fd, int revents, void *cb_data)
        return TRUE;
 }
 
-static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
+static int start_transfers(const struct sr_dev_inst *sdi)
 {
        struct dev_context *devc;
-       struct drv_context *drvc;
        struct sr_usb_dev_inst *usb;
        struct sr_trigger *trigger;
        struct libusb_transfer *transfer;
-       unsigned int i, timeout, num_transfers;
-       int ret;
+       unsigned int i, num_transfers;
+       int endpoint, timeout, ret;
        unsigned char *buf;
        size_t size;
 
-       if (sdi->status != SR_ST_ACTIVE)
-               return SR_ERR_DEV_CLOSED;
-
-       drvc = di->priv;
        devc = sdi->priv;
        usb = sdi->conn;
 
-       devc->cb_data = cb_data;
        devc->sent_samples = 0;
        devc->acq_aborted = FALSE;
        devc->empty_transfer_count = 0;
 
        if ((trigger = sr_session_trigger_get(sdi->session))) {
-               devc->stl = soft_trigger_logic_new(sdi, trigger);
+               int pre_trigger_samples = 0;
+               if (devc->limit_samples > 0)
+                       pre_trigger_samples = devc->capture_ratio * devc->limit_samples/100;
+               devc->stl = soft_trigger_logic_new(sdi, trigger, pre_trigger_samples);
+               if (devc->stl == NULL)
+                       return SR_ERR_MALLOC;
                devc->trigger_fired = FALSE;
        } else
                devc->trigger_fired = TRUE;
 
        timeout = fx2lafw_get_timeout(devc);
+
        num_transfers = fx2lafw_get_number_of_transfers(devc);
        size = fx2lafw_get_buffer_size(devc);
        devc->submitted_transfers = 0;
@@ -577,6 +648,8 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
                return SR_ERR_MALLOC;
        }
 
+       timeout = fx2lafw_get_timeout(devc);
+       endpoint = devc->dslogic ? 6 : 2;
        devc->num_transfers = num_transfers;
        for (i = 0; i < num_transfers; i++) {
                if (!(buf = g_try_malloc(size))) {
@@ -585,7 +658,7 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
                }
                transfer = libusb_alloc_transfer(0);
                libusb_fill_bulk_transfer(transfer, usb->devhdl,
-                               2 | LIBUSB_ENDPOINT_IN, buf, size,
+                               endpoint | LIBUSB_ENDPOINT_IN, buf, size,
                                fx2lafw_receive_transfer, (void *)sdi, timeout);
                if ((ret = libusb_submit_transfer(transfer)) != 0) {
                        sr_err("Failed to submit transfer: %s.",
@@ -599,16 +672,95 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
                devc->submitted_transfers++;
        }
 
-       devc->ctx = drvc->sr_ctx;
+       /* Send header packet to the session bus. */
+       std_session_send_df_header(sdi, LOG_PREFIX);
 
-       usb_source_add(sdi->session, devc->ctx, timeout, receive_data, NULL);
+       return SR_OK;
+}
 
-       /* Send header packet to the session bus. */
-       std_session_send_df_header(cb_data, LOG_PREFIX);
+static void dslogic_trigger_receive(struct libusb_transfer *transfer)
+{
+       const struct sr_dev_inst *sdi;
+       struct dslogic_trigger_pos *tpos;
+
+       sdi = transfer->user_data;
+
+       if (transfer->status == LIBUSB_TRANSFER_COMPLETED
+                       && transfer->actual_length == sizeof(struct dslogic_trigger_pos)) {
+               tpos = (struct dslogic_trigger_pos *)transfer->buffer;
+               sr_dbg("tpos real_pos %.8x ram_saddr %.8x", tpos->real_pos, tpos->ram_saddr);
+               g_free(tpos);
+               start_transfers(sdi);
+       }
+
+       libusb_free_transfer(transfer);
+
+}
+
+static int dslogic_trigger_request(const struct sr_dev_inst *sdi)
+{
+       struct sr_usb_dev_inst *usb;
+       struct libusb_transfer *transfer;
+       struct dslogic_trigger_pos *tpos;
+       int ret;
+
+       usb = sdi->conn;
+
+       if ((ret = dslogic_stop_acquisition(sdi)) != SR_OK)
+               return ret;
 
-       if ((ret = fx2lafw_command_start_acquisition(sdi)) != SR_OK) {
-               fx2lafw_abort_acquisition(devc);
+       if ((ret = dslogic_fpga_configure(sdi)) != SR_OK)
                return ret;
+
+       if ((ret = dslogic_start_acquisition(sdi)) != SR_OK)
+               return ret;
+
+       sr_dbg("Getting trigger.");
+       tpos = g_malloc(sizeof(struct dslogic_trigger_pos));
+       transfer = libusb_alloc_transfer(0);
+       libusb_fill_bulk_transfer(transfer, usb->devhdl, 6 | LIBUSB_ENDPOINT_IN,
+                       (unsigned char *)tpos, sizeof(struct dslogic_trigger_pos),
+                       dslogic_trigger_receive, (void *)sdi, 0);
+       if ((ret = libusb_submit_transfer(transfer)) < 0) {
+               sr_err("Failed to request trigger: %s.", libusb_error_name(ret));
+               libusb_free_transfer(transfer);
+               g_free(tpos);
+               return SR_ERR;
+       }
+
+       return ret;
+}
+
+static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
+{
+       struct sr_dev_driver *di;
+       struct drv_context *drvc;
+       struct dev_context *devc;
+       int timeout, ret;
+
+       if (sdi->status != SR_ST_ACTIVE)
+               return SR_ERR_DEV_CLOSED;
+
+       di = sdi->driver;
+       drvc = di->priv;
+       devc = sdi->priv;
+
+       devc->ctx = drvc->sr_ctx;
+       devc->cb_data = cb_data;
+       devc->sent_samples = 0;
+       devc->empty_transfer_count = 0;
+       devc->acq_aborted = FALSE;
+
+       timeout = fx2lafw_get_timeout(devc);
+       usb_source_add(sdi->session, devc->ctx, timeout, receive_data, drvc);
+
+       if (devc->dslogic) {
+               dslogic_trigger_request(sdi);
+       }
+       else {
+               if ((ret = fx2lafw_command_start_acquisition(sdi)) != SR_OK)
+                       return ret;
+               start_transfers(sdi);
        }
 
        return SR_OK;