]> sigrok.org Git - libsigrok.git/blobdiff - src/hardware/fx2lafw/api.c
Drop unneeded std_session_send_df_header() comments.
[libsigrok.git] / src / hardware / fx2lafw / api.c
index 16ab9be01a6846d6c41c0222ca5dd89e978da168..976cfbf9d28d735628c841b08b17849ecb8ad178 100644 (file)
@@ -29,48 +29,48 @@ static const struct fx2lafw_profile supported_fx2[] = {
         * ARMFLY AX-Pro
         */
        { 0x08a9, 0x0014, "CWAV", "USBee AX", NULL,
-               FIRMWARE_DIR "/fx2lafw-cwav-usbeeax.fw",
-               0, NULL, NULL},
+               "fx2lafw-cwav-usbeeax.fw",
+               DEV_CAPS_AX_ANALOG, NULL, NULL},
        /*
         * CWAV USBee DX
         * XZL-Studio DX
         */
        { 0x08a9, 0x0015, "CWAV", "USBee DX", NULL,
-               FIRMWARE_DIR "/fx2lafw-cwav-usbeedx.fw",
+               "fx2lafw-cwav-usbeedx.fw",
                DEV_CAPS_16BIT, NULL, NULL },
 
        /*
         * CWAV USBee SX
         */
        { 0x08a9, 0x0009, "CWAV", "USBee SX", NULL,
-               FIRMWARE_DIR "/fx2lafw-cwav-usbeesx.fw",
+               "fx2lafw-cwav-usbeesx.fw",
                0, NULL, NULL},
 
        /* DreamSourceLab DSLogic (before FW upload) */
        { 0x2a0e, 0x0001, "DreamSourceLab", "DSLogic", NULL,
-               FIRMWARE_DIR "/dreamsourcelab-dslogic-fx2.fw",
+               "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",
+               "dreamsourcelab-dslogic-fx2.fw",
                DEV_CAPS_16BIT, "DreamSourceLab", "DSLogic"},
 
        /* DreamSourceLab DSCope (before FW upload) */
        { 0x2a0e, 0x0002, "DreamSourceLab", "DSCope", NULL,
-               FIRMWARE_DIR "/dreamsourcelab-dscope-fx2.fw",
+               "dreamsourcelab-dscope-fx2.fw",
                DEV_CAPS_16BIT, NULL, NULL},
        /* DreamSourceLab DSCope (after FW upload) */
        { 0x2a0e, 0x0002, "DreamSourceLab", "DSCope", NULL,
-               FIRMWARE_DIR "/dreamsourcelab-dscope-fx2.fw",
+               "dreamsourcelab-dscope-fx2.fw",
                DEV_CAPS_16BIT, "DreamSourceLab", "DSCope"},
 
        /* DreamSourceLab DSLogic Pro (before FW upload) */
        { 0x2a0e, 0x0003, "DreamSourceLab", "DSLogic Pro", NULL,
-               FIRMWARE_DIR "/dreamsourcelab-dslogic-pro-fx2.fw",
+               "dreamsourcelab-dslogic-pro-fx2.fw",
                DEV_CAPS_16BIT, NULL, NULL},
        /* DreamSourceLab DSLogic Pro (after FW upload) */
        { 0x2a0e, 0x0003, "DreamSourceLab", "DSLogic Pro", NULL,
-               FIRMWARE_DIR "/dreamsourcelab-dslogic-pro-fx2.fw",
+               "dreamsourcelab-dslogic-pro-fx2.fw",
                DEV_CAPS_16BIT, "DreamSourceLab", "DSLogic"},
 
        /*
@@ -80,7 +80,7 @@ static const struct fx2lafw_profile supported_fx2[] = {
         * Robomotic BugLogic 3
         */
        { 0x0925, 0x3881, "Saleae", "Logic", NULL,
-               FIRMWARE_DIR "/fx2lafw-saleae-logic.fw",
+               "fx2lafw-saleae-logic.fw",
                0, NULL, NULL},
 
        /*
@@ -89,17 +89,31 @@ static const struct fx2lafw_profile supported_fx2[] = {
         * Braintechnology USB Interface V2.x
         */
        { 0x04B4, 0x8613, "Cypress", "FX2", NULL,
-               FIRMWARE_DIR "/fx2lafw-cypress-fx2.fw",
+               "fx2lafw-cypress-fx2.fw",
                DEV_CAPS_16BIT, NULL, NULL },
 
        /*
         * Braintechnology USB-LPS
         */
        { 0x16d0, 0x0498, "Braintechnology", "USB-LPS", NULL,
-               FIRMWARE_DIR "/fx2lafw-braintechnology-usb-lps.fw",
+               "fx2lafw-braintechnology-usb-lps.fw",
                DEV_CAPS_16BIT, NULL, NULL },
 
-       { 0, 0, 0, 0, 0, 0, 0, 0, 0 }
+       /*
+        * sigrok FX2 based 8-channel logic analyzer
+        */
+       { 0x1d50, 0x608c, "sigrok", "FX2 LA (8ch)", NULL,
+               "fx2lafw-sigrok-fx2-8ch.fw",
+               0, NULL, NULL},
+
+       /*
+        * sigrok FX2 based 16-channel logic analyzer
+        */
+       { 0x1d50, 0x608d, "sigrok", "FX2 LA (16ch)", NULL,
+               "fx2lafw-sigrok-fx2-16ch.fw",
+               DEV_CAPS_16BIT, NULL, NULL },
+
+       ALL_ZERO
 };
 
 static const uint32_t drvopts[] = {
@@ -119,11 +133,6 @@ static const uint32_t devopts[] = {
        SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET,
 };
 
-static const char *channel_names[] = {
-       "0", "1", "2", "3", "4", "5", "6", "7",
-       "8", "9", "10", "11", "12", "13", "14", "15",
-};
-
 static const int32_t soft_trigger_matches[] = {
        SR_TRIGGER_ZERO,
        SR_TRIGGER_ONE,
@@ -183,6 +192,8 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
        struct dev_context *devc;
        struct sr_dev_inst *sdi;
        struct sr_usb_dev_inst *usb;
+       struct sr_channel *ch;
+       struct sr_channel_group *cg;
        struct sr_config *src;
        const struct fx2lafw_profile *prof;
        GSList *l, *devices, *conn_devices;
@@ -190,9 +201,11 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
        struct libusb_device_descriptor des;
        libusb_device **devlist;
        struct libusb_device_handle *hdl;
-       int num_logic_channels, ret, i, j;
+       int ret, i, j;
+       int num_logic_channels = 0, num_analog_channels = 0;
        const char *conn;
        char manufacturer[64], product[64], serial_num[64], connection_id[64];
+       char channel_name[16];
 
        drvc = di->context;
 
@@ -295,13 +308,35 @@ static GSList *scan(struct sr_dev_driver *di, 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++)
-                       sr_channel_new(sdi, j, SR_CHANNEL_LOGIC, TRUE,
-                                       channel_names[j]);
+               num_analog_channels = prof->dev_caps & DEV_CAPS_AX_ANALOG ? 1 : 0;
+
+               /* Logic channels, all in one channel group. */
+               cg = g_malloc0(sizeof(struct sr_channel_group));
+               cg->name = g_strdup("Logic");
+               for (j = 0; j < num_logic_channels; j++) {
+                       sprintf(channel_name, "D%d", j);
+                       ch = sr_channel_new(sdi, j, SR_CHANNEL_LOGIC,
+                                               TRUE, channel_name);
+                       cg->channels = g_slist_append(cg->channels, ch);
+               }
+               sdi->channel_groups = g_slist_append(NULL, cg);
+
+               for (j = 0; j < num_analog_channels; j++) {
+                       snprintf(channel_name, 16, "A%d", j);
+                       ch = sr_channel_new(sdi, j + num_logic_channels,
+                                       SR_CHANNEL_ANALOG, TRUE, channel_name);
+
+                       /* Every analog channel gets its own channel group. */
+                       cg = g_malloc0(sizeof(struct sr_channel_group));
+                       cg->name = g_strdup(channel_name);
+                       cg->channels = g_slist_append(NULL, ch);
+                       sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
+               }
 
                devc = fx2lafw_dev_new();
                devc->profile = prof;
-               devc->sample_wide = (prof->dev_caps & DEV_CAPS_16BIT) != 0;
+               if ((prof->dev_caps & DEV_CAPS_16BIT) || (prof->dev_caps & DEV_CAPS_AX_ANALOG))
+                       devc->sample_wide = TRUE;
                sdi->priv = devc;
                drvc->instances = g_slist_append(drvc->instances, sdi);
                devices = g_slist_append(devices, sdi);
@@ -330,8 +365,8 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
                        sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
                                        libusb_get_device_address(devlist[i]), NULL);
                } else {
-                       if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION,
-                               prof->firmware) == SR_OK)
+                       if (ezusb_upload_firmware(drvc->sr_ctx, devlist[i],
+                                       USB_CONFIGURATION, prof->firmware) == SR_OK)
                                /* Store when this device's FW was updated. */
                                devc->fw_updated = g_get_monotonic_time();
                        else
@@ -350,6 +385,20 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
        return devices;
 }
 
+static void clear_dev_context(void *priv)
+{
+       struct dev_context *devc;
+
+       devc = priv;
+       g_slist_free(devc->enabled_analog_channels);
+       g_free(devc);
+}
+
+static int dev_clear(const struct sr_dev_driver *di)
+{
+       return std_dev_clear(di, clear_dev_context);
+}
+
 static GSList *dev_list(const struct sr_dev_driver *di)
 {
        return ((struct drv_context *)(di->context))->instances;
@@ -360,7 +409,7 @@ 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;
-       char *fpga_firmware = NULL;
+       const char *fpga_firmware = NULL;
        int ret;
        int64_t timediff_us, timediff_ms;
 
@@ -553,11 +602,7 @@ static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sd
                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;
+               ret = (devc->capture_ratio > 100) ? SR_ERR : SR_OK;
                break;
        default:
                ret = SR_ERR_NA;
@@ -655,8 +700,6 @@ static int start_transfers(const struct sr_dev_inst *sdi)
        } 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;
@@ -691,7 +734,11 @@ static int start_transfers(const struct sr_dev_inst *sdi)
                devc->submitted_transfers++;
        }
 
-       /* Send header packet to the session bus. */
+       if (devc->profile->dev_caps & DEV_CAPS_AX_ANALOG)
+               devc->send_data_proc = mso_send_data_proc;
+       else
+               devc->send_data_proc = la_send_data_proc;
+
        std_session_send_df_header(sdi, LOG_PREFIX);
 
        return SR_OK;
@@ -701,10 +748,22 @@ static void LIBUSB_CALL dslogic_trigger_receive(struct libusb_transfer *transfer
 {
        const struct sr_dev_inst *sdi;
        struct dslogic_trigger_pos *tpos;
+       struct dev_context *devc;
 
        sdi = transfer->user_data;
-
-       if (transfer->status == LIBUSB_TRANSFER_COMPLETED
+       devc = sdi->priv;
+       if (transfer->status == LIBUSB_TRANSFER_CANCELLED) {
+               sr_dbg("Trigger transfer canceled.");
+               /* Terminate session. */
+               std_session_send_df_end(sdi, LOG_PREFIX);
+               usb_source_remove(sdi->session, devc->ctx);
+               devc->num_transfers = 0;
+               g_free(devc->transfers);
+               if (devc->stl) {
+                       soft_trigger_logic_free(devc->stl);
+                       devc->stl = NULL;
+               }
+       } else 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);
@@ -713,7 +772,6 @@ static void LIBUSB_CALL dslogic_trigger_receive(struct libusb_transfer *transfer
        }
 
        libusb_free_transfer(transfer);
-
 }
 
 static int dslogic_trigger_request(const struct sr_dev_inst *sdi)
@@ -721,9 +779,11 @@ 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;
+       struct dev_context *devc;
        int ret;
 
        usb = sdi->conn;
+       devc = sdi->priv;
 
        if ((ret = dslogic_stop_acquisition(sdi)) != SR_OK)
                return ret;
@@ -747,15 +807,50 @@ static int dslogic_trigger_request(const struct sr_dev_inst *sdi)
                return SR_ERR;
        }
 
+       devc->transfers = g_try_malloc0(sizeof(*devc->transfers));
+       if (!devc->transfers) {
+               sr_err("USB trigger_pos transfer malloc failed.");
+               return SR_ERR_MALLOC;
+       }
+       devc->num_transfers = 1;
+       devc->submitted_transfers++;
+       devc->transfers[0] = transfer;
+
        return ret;
 }
 
+static int configure_channels(const struct sr_dev_inst *sdi)
+{
+       struct dev_context *devc;
+       const GSList *l;
+       int p;
+       struct sr_channel *ch;
+
+       devc = sdi->priv;
+
+       g_slist_free(devc->enabled_analog_channels);
+       devc->enabled_analog_channels = NULL;
+       memset(devc->ch_enabled, 0, sizeof(devc->ch_enabled));
+
+       for (l = sdi->channels, p = 0; l; l = l->next, p++) {
+               ch = l->data;
+               if ((p <= NUM_CHANNELS) && (ch->type == SR_CHANNEL_ANALOG)) {
+                       devc->ch_enabled[p] = ch->enabled;
+                       devc->enabled_analog_channels =
+                           g_slist_append(devc->enabled_analog_channels, ch);
+               }
+       }
+
+       return SR_OK;
+}
+
 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;
+       size_t size;
 
        if (sdi->status != SR_ST_ACTIVE)
                return SR_ERR_DEV_CLOSED;
@@ -770,12 +865,25 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
        devc->empty_transfer_count = 0;
        devc->acq_aborted = FALSE;
 
+       if (configure_channels(sdi) != SR_OK) {
+               sr_err("Failed to configure channels.");
+               return SR_ERR;
+       }
+
        timeout = fx2lafw_get_timeout(devc);
        usb_source_add(sdi->session, devc->ctx, timeout, receive_data, drvc);
 
        if (devc->dslogic) {
                dslogic_trigger_request(sdi);
        } else {
+               size = fx2lafw_get_buffer_size(devc);
+               /* Prepare for analog sampling. */
+               if (devc->profile->dev_caps & DEV_CAPS_AX_ANALOG) {
+                       /* We need a buffer half the size of a transfer. */
+                       devc->logic_buffer = g_try_malloc(size / 2);
+                       devc->analog_buffer = g_try_malloc(
+                               sizeof(float) * size / 2);
+               }
                start_transfers(sdi);
                if ((ret = fx2lafw_command_start_acquisition(sdi)) != SR_OK) {
                        fx2lafw_abort_acquisition(devc);
@@ -788,8 +896,15 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
 
 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
 {
+       struct dev_context *devc;
+
        (void)cb_data;
 
+       devc = sdi->priv;
+
+       if (devc->dslogic)
+               dslogic_stop_acquisition(sdi);
+
        fx2lafw_abort_acquisition(sdi->priv);
 
        return SR_OK;
@@ -803,7 +918,7 @@ SR_PRIV struct sr_dev_driver fx2lafw_driver_info = {
        .cleanup = cleanup,
        .scan = scan,
        .dev_list = dev_list,
-       .dev_clear = NULL,
+       .dev_clear = dev_clear,
        .config_get = config_get,
        .config_set = config_set,
        .config_list = config_list,