]> sigrok.org Git - libsigrok.git/blobdiff - hardware/asix-sigma/asix-sigma.c
Rename SR_HWOPT_* and SR_HWCAP_* to SR_CONF_*
[libsigrok.git] / hardware / asix-sigma / asix-sigma.c
index e6515dd255eb5cdf126f89ccd24a6254fca1f75d..828121ae121bb6614aaa813904e79ce15e462f06 100644 (file)
@@ -41,7 +41,8 @@
 #define NUM_PROBES                     16
 
 SR_PRIV struct sr_dev_driver asix_sigma_driver_info;
-static struct sr_dev_driver *adi = &asix_sigma_driver_info;
+static struct sr_dev_driver *di = &asix_sigma_driver_info;
+static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data);
 
 static const uint64_t supported_samplerates[] = {
        SR_KHZ(200),
@@ -63,22 +64,8 @@ static const uint64_t supported_samplerates[] = {
  * (the cable has two additional GND pins, and a TI and TO pin)
  */
 static const char *probe_names[NUM_PROBES + 1] = {
-       "1",
-       "2",
-       "3",
-       "4",
-       "5",
-       "6",
-       "7",
-       "8",
-       "9",
-       "10",
-       "11",
-       "12",
-       "13",
-       "14",
-       "15",
-       "16",
+       "1", "2", "3", "4", "5", "6", "7", "8",
+       "9", "10", "11", "12", "13", "14", "15", "16",
        NULL,
 };
 
@@ -90,12 +77,11 @@ static const struct sr_samplerates samplerates = {
 };
 
 static const int hwcaps[] = {
-       SR_HWCAP_LOGIC_ANALYZER,
-       SR_HWCAP_SAMPLERATE,
-       SR_HWCAP_CAPTURE_RATIO,
-       SR_HWCAP_PROBECONFIG,
+       SR_CONF_LOGIC_ANALYZER,
+       SR_CONF_SAMPLERATE,
+       SR_CONF_CAPTURE_RATIO,
 
-       SR_HWCAP_LIMIT_MSEC,
+       SR_CONF_LIMIT_MSEC,
        0,
 };
 
@@ -123,16 +109,13 @@ static const char *firmware_files[] = {
        "asix-sigma-phasor.fw", /* Frequency counter */
 };
 
-static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
-               void *cb_data);
-
 static int sigma_read(void *buf, size_t size, struct dev_context *devc)
 {
        int ret;
 
        ret = ftdi_read_data(&devc->ftdic, (unsigned char *)buf, size);
        if (ret < 0) {
-               sr_err("sigma: ftdi_read_data failed: %s",
+               sr_err("ftdi_read_data failed: %s",
                       ftdi_get_error_string(&devc->ftdic));
        }
 
@@ -145,10 +128,10 @@ static int sigma_write(void *buf, size_t size, struct dev_context *devc)
 
        ret = ftdi_write_data(&devc->ftdic, (unsigned char *)buf, size);
        if (ret < 0) {
-               sr_err("sigma: ftdi_write_data failed: %s",
+               sr_err("ftdi_write_data failed: %s",
                       ftdi_get_error_string(&devc->ftdic));
        } else if ((size_t) ret != size) {
-               sr_err("sigma: ftdi_write_data did not complete write.");
+               sr_err("ftdi_write_data did not complete write.");
        }
 
        return ret;
@@ -196,7 +179,7 @@ static uint8_t sigma_get_register(uint8_t reg, struct dev_context *devc)
        uint8_t value;
 
        if (1 != sigma_read_register(reg, &value, 1, devc)) {
-               sr_err("sigma: sigma_get_register: 1 byte expected");
+               sr_err("sigma_get_register: 1 byte expected");
                return 0;
        }
 
@@ -343,12 +326,12 @@ static int bin2bitbang(const char *filename,
 
        f = g_fopen(filename, "rb");
        if (!f) {
-               sr_err("sigma: g_fopen(\"%s\", \"rb\")", filename);
+               sr_err("g_fopen(\"%s\", \"rb\")", filename);
                return SR_ERR;
        }
 
        if (-1 == fseek(f, 0, SEEK_END)) {
-               sr_err("sigma: fseek on %s failed", filename);
+               sr_err("fseek on %s failed", filename);
                fclose(f);
                return SR_ERR;
        }
@@ -358,7 +341,7 @@ static int bin2bitbang(const char *filename,
        fseek(f, 0, SEEK_SET);
 
        if (!(firmware = g_try_malloc(buffer_size))) {
-               sr_err("sigma: %s: firmware malloc failed", __func__);
+               sr_err("%s: firmware malloc failed", __func__);
                fclose(f);
                return SR_ERR_MALLOC;
        }
@@ -370,7 +353,7 @@ static int bin2bitbang(const char *filename,
        fclose(f);
 
        if(fwsize != file_size) {
-           sr_err("sigma: %s: Error reading firmware", filename);
+           sr_err("%s: Error reading firmware", filename);
            fclose(f);
            g_free(firmware);
            return SR_ERR;
@@ -380,7 +363,7 @@ static int bin2bitbang(const char *filename,
 
        *buf = p = (unsigned char *)g_try_malloc(*buf_size);
        if (!p) {
-               sr_err("sigma: %s: buf/p malloc failed", __func__);
+               sr_err("%s: buf/p malloc failed", __func__);
                g_free(firmware);
                return SR_ERR_MALLOC;
        }
@@ -397,7 +380,7 @@ static int bin2bitbang(const char *filename,
 
        if (offset != *buf_size) {
                g_free(*buf);
-               sr_err("sigma: Error reading firmware %s "
+               sr_err("Error reading firmware %s "
                       "offset=%ld, file_size=%ld, buf_size=%zd.",
                       filename, offset, file_size, *buf_size);
 
@@ -407,43 +390,44 @@ static int bin2bitbang(const char *filename,
        return SR_OK;
 }
 
-static void clear_instances(void)
+static int clear_instances(void)
 {
        GSList *l;
        struct sr_dev_inst *sdi;
        struct drv_context *drvc;
        struct dev_context *devc;
 
-       drvc = adi->priv;
+       drvc = di->priv;
 
        /* Properly close all devices. */
        for (l = drvc->instances; l; l = l->next) {
                if (!(sdi = l->data)) {
                        /* Log error, but continue cleaning up the rest. */
-                       sr_err("sigma: %s: sdi was NULL, continuing", __func__);
+                       sr_err("%s: sdi was NULL, continuing", __func__);
                        continue;
                }
                if (sdi->priv) {
                        devc = sdi->priv;
                        ftdi_free(&devc->ftdic);
-                       g_free(devc);
                }
                sr_dev_inst_free(sdi);
        }
        g_slist_free(drvc->instances);
        drvc->instances = NULL;
 
+       return SR_OK;
 }
 
-static int hw_init(void)
+static int hw_init(struct sr_context *sr_ctx)
 {
        struct drv_context *drvc;
 
        if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
-               sr_err("asix-sigma: driver context malloc failed.");
-               return SR_ERR;
+               sr_err("Driver context malloc failed.");
+               return SR_ERR_MALLOC;
        }
-       adi->priv = drvc;
+       drvc->sr_ctx = sr_ctx;
+       di->priv = drvc;
 
        return SR_OK;
 }
@@ -461,12 +445,13 @@ static GSList *hw_scan(GSList *options)
        int ret, i;
 
        (void)options;
-       drvc = adi->priv;
+
+       drvc = di->priv;
        devices = NULL;
        clear_instances();
 
        if (!(devc = g_try_malloc(sizeof(struct dev_context)))) {
-               sr_err("sigma: %s: devc malloc failed", __func__);
+               sr_err("%s: devc malloc failed", __func__);
                return NULL;
        }
 
@@ -487,8 +472,8 @@ static GSList *hw_scan(GSList *options)
        sscanf(serial_txt, "%x", &serial);
 
        if (serial < 0xa6010000 || serial > 0xa602ffff) {
-               sr_err("sigma: Only SIGMA and SIGMA2 are supported "
-                      "in this version of sigrok.");
+               sr_err("Only SIGMA and SIGMA2 are supported "
+                      "in this version of libsigrok.");
                goto free;
        }
 
@@ -506,13 +491,13 @@ static GSList *hw_scan(GSList *options)
        /* Register SIGMA device. */
        if (!(sdi = sr_dev_inst_new(0, SR_ST_INITIALIZING, USB_VENDOR_NAME,
                                    USB_MODEL_NAME, USB_MODEL_VERSION))) {
-               sr_err("sigma: %s: sdi was NULL", __func__);
+               sr_err("%s: sdi was NULL", __func__);
                goto free;
        }
-       sdi->driver = adi;
+       sdi->driver = di;
 
        for (i = 0; probe_names[i]; i++) {
-               if (!(probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE,
+               if (!(probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE,
                                probe_names[i])))
                        return NULL;
                sdi->probes = g_slist_append(sdi->probes, probe);
@@ -533,6 +518,15 @@ free:
        return NULL;
 }
 
+static GSList *hw_dev_list(void)
+{
+       struct drv_context *drvc;
+
+       drvc = di->priv;
+
+       return drvc->instances;
+}
+
 static int upload_firmware(int firmware_idx, struct dev_context *devc)
 {
        int ret;
@@ -545,20 +539,20 @@ static int upload_firmware(int firmware_idx, struct dev_context *devc)
        /* Make sure it's an ASIX SIGMA. */
        if ((ret = ftdi_usb_open_desc(&devc->ftdic,
                USB_VENDOR, USB_PRODUCT, USB_DESCRIPTION, NULL)) < 0) {
-               sr_err("sigma: ftdi_usb_open failed: %s",
+               sr_err("ftdi_usb_open failed: %s",
                       ftdi_get_error_string(&devc->ftdic));
                return 0;
        }
 
        if ((ret = ftdi_set_bitmode(&devc->ftdic, 0xdf, BITMODE_BITBANG)) < 0) {
-               sr_err("sigma: ftdi_set_bitmode failed: %s",
+               sr_err("ftdi_set_bitmode failed: %s",
                       ftdi_get_error_string(&devc->ftdic));
                return 0;
        }
 
        /* Four times the speed of sigmalogan - Works well. */
        if ((ret = ftdi_set_baudrate(&devc->ftdic, 750000)) < 0) {
-               sr_err("sigma: ftdi_set_baudrate failed: %s",
+               sr_err("ftdi_set_baudrate failed: %s",
                       ftdi_get_error_string(&devc->ftdic));
                return 0;
        }
@@ -586,19 +580,19 @@ static int upload_firmware(int firmware_idx, struct dev_context *devc)
                 firmware_files[firmware_idx]);
 
        if ((ret = bin2bitbang(firmware_path, &buf, &buf_size)) != SR_OK) {
-               sr_err("sigma: An error occured while reading the firmware: %s",
+               sr_err("An error occured while reading the firmware: %s",
                       firmware_path);
                return ret;
        }
 
        /* Upload firmare. */
-       sr_info("sigma: Uploading firmware %s", firmware_files[firmware_idx]);
+       sr_info("Uploading firmware file '%s'.", firmware_files[firmware_idx]);
        sigma_write(buf, buf_size, devc);
 
        g_free(buf);
 
        if ((ret = ftdi_set_bitmode(&devc->ftdic, 0x00, BITMODE_RESET)) < 0) {
-               sr_err("sigma: ftdi_set_bitmode failed: %s",
+               sr_err("ftdi_set_bitmode failed: %s",
                       ftdi_get_error_string(&devc->ftdic));
                return SR_ERR;
        }
@@ -616,13 +610,13 @@ static int upload_firmware(int firmware_idx, struct dev_context *devc)
        ret = sigma_read(result, 3, devc);
        if (ret != 3 ||
            result[0] != 0xa6 || result[1] != 0x55 || result[2] != 0xaa) {
-               sr_err("sigma: Configuration failed. Invalid reply received.");
+               sr_err("Configuration failed. Invalid reply received.");
                return SR_ERR;
        }
 
        devc->cur_firmware = firmware_idx;
 
-       sr_info("sigma: Firmware uploaded");
+       sr_info("Firmware uploaded.");
 
        return SR_OK;
 }
@@ -638,7 +632,7 @@ static int hw_dev_open(struct sr_dev_inst *sdi)
        if ((ret = ftdi_usb_open_desc(&devc->ftdic,
                USB_VENDOR, USB_PRODUCT, USB_DESCRIPTION, NULL)) < 0) {
 
-               sr_err("sigma: ftdi_usb_open failed: %s",
+               sr_err("ftdi_usb_open failed: %s",
                       ftdi_get_error_string(&devc->ftdic));
 
                return 0;
@@ -654,6 +648,8 @@ static int set_samplerate(const struct sr_dev_inst *sdi, uint64_t samplerate)
        int i, ret;
        struct dev_context *devc = sdi->priv;
 
+       ret = SR_OK;
+
        for (i = 0; supported_samplerates[i]; i++) {
                if (supported_samplerates[i] == samplerate)
                        break;
@@ -675,7 +671,7 @@ static int set_samplerate(const struct sr_dev_inst *sdi, uint64_t samplerate)
        }
 
        devc->cur_samplerate = samplerate;
-       devc->period_ps = 1000000000000 / samplerate;
+       devc->period_ps = 1000000000000ULL / samplerate;
        devc->samples_per_event = 16 / devc->num_probes;
        devc->state.state = SIGMA_IDLE;
 
@@ -690,7 +686,7 @@ static int set_samplerate(const struct sr_dev_inst *sdi, uint64_t samplerate)
  * The Sigma supports complex triggers using boolean expressions, but this
  * has not been implemented yet.
  */
-static int configure_probes(const struct sr_dev_inst *sdi, const GSList *probes)
+static int configure_probes(const struct sr_dev_inst *sdi)
 {
        struct dev_context *devc = sdi->priv;
        const struct sr_probe *probe;
@@ -700,7 +696,7 @@ static int configure_probes(const struct sr_dev_inst *sdi, const GSList *probes)
 
        memset(&devc->trigger, 0, sizeof(struct sigma_trigger));
 
-       for (l = probes; l; l = l->next) {
+       for (l = sdi->probes; l; l = l->next) {
                probe = (struct sr_probe *)l->data;
                probebit = 1 << (probe->index);
 
@@ -710,8 +706,8 @@ static int configure_probes(const struct sr_dev_inst *sdi, const GSList *probes)
                if (devc->cur_samplerate >= SR_MHZ(100)) {
                        /* Fast trigger support. */
                        if (trigger_set) {
-                               sr_err("sigma: ASIX SIGMA only supports a single "
-                                      "pin trigger in 100 and 200MHz mode.");
+                               sr_err("Only a single pin trigger in 100 and "
+                                      "200MHz mode is supported.");
                                return SR_ERR;
                        }
                        if (probe->trigger[0] == 'f')
@@ -719,9 +715,8 @@ static int configure_probes(const struct sr_dev_inst *sdi, const GSList *probes)
                        else if (probe->trigger[0] == 'r')
                                devc->trigger.risingmask |= probebit;
                        else {
-                               sr_err("sigma: ASIX SIGMA only supports "
-                                      "rising/falling trigger in 100 "
-                                      "and 200MHz mode.");
+                               sr_err("Only rising/falling trigger in 100 "
+                                      "and 200MHz mode is supported.");
                                return SR_ERR;
                        }
 
@@ -751,8 +746,8 @@ static int configure_probes(const struct sr_dev_inst *sdi, const GSList *probes)
                         * does not permit ORed triggers.
                         */
                        if (trigger_set > 1) {
-                               sr_err("sigma: ASIX SIGMA only supports 1 "
-                                      "rising/falling triggers.");
+                               sr_err("Only 1 rising/falling trigger "
+                                      "is supported.");
                                return SR_ERR;
                        }
                }
@@ -769,7 +764,7 @@ static int hw_dev_close(struct sr_dev_inst *sdi)
        struct dev_context *devc;
 
        if (!(devc = sdi->priv)) {
-               sr_err("sigma: %s: sdi->priv was NULL", __func__);
+               sr_err("%s: sdi->priv was NULL", __func__);
                return SR_ERR_BUG;
        }
 
@@ -784,8 +779,7 @@ static int hw_dev_close(struct sr_dev_inst *sdi)
 
 static int hw_cleanup(void)
 {
-
-       if (!adi->priv)
+       if (!di->priv)
                return SR_OK;
 
        clear_instances();
@@ -794,7 +788,7 @@ static int hw_cleanup(void)
 }
 
 static int hw_info_get(int info_id, const void **data,
-       const struct sr_dev_inst *sdi)
+                      const struct sr_dev_inst *sdi)
 {
        struct dev_context *devc;
 
@@ -802,12 +796,6 @@ static int hw_info_get(int info_id, const void **data,
        case SR_DI_HWCAPS:
                *data = hwcaps;
                break;
-       case SR_DI_NUM_PROBES:
-               *data = GINT_TO_POINTER(NUM_PROBES);
-               break;
-       case SR_DI_PROBE_NAMES:
-               *data = probe_names;
-               break;
        case SR_DI_SAMPLERATES:
                *data = &samplerates;
                break;
@@ -829,24 +817,22 @@ static int hw_info_get(int info_id, const void **data,
 }
 
 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
-               const void *value)
+                            const void *value)
 {
        struct dev_context *devc;
        int ret;
 
        devc = sdi->priv;
 
-       if (hwcap == SR_HWCAP_SAMPLERATE) {
+       if (hwcap == SR_CONF_SAMPLERATE) {
                ret = set_samplerate(sdi, *(const uint64_t *)value);
-       } else if (hwcap == SR_HWCAP_PROBECONFIG) {
-               ret = configure_probes(sdi, value);
-       } else if (hwcap == SR_HWCAP_LIMIT_MSEC) {
+       } else if (hwcap == SR_CONF_LIMIT_MSEC) {
                devc->limit_msec = *(const uint64_t *)value;
                if (devc->limit_msec > 0)
                        ret = SR_OK;
                else
                        ret = SR_ERR;
-       } else if (hwcap == SR_HWCAP_CAPTURE_RATIO) {
+       } else if (hwcap == SR_CONF_CAPTURE_RATIO) {
                devc->capture_ratio = *(const uint64_t *)value;
                if (devc->capture_ratio < 0 || devc->capture_ratio > 100)
                        ret = SR_ERR;
@@ -1041,7 +1027,6 @@ static int receive_data(int fd, int revents, void *cb_data)
        uint64_t running_msec;
        struct timeval tv;
 
-       /* Avoid compiler warnings. */
        (void)fd;
        (void)revents;
 
@@ -1064,7 +1049,9 @@ static int receive_data(int fd, int revents, void *cb_data)
                else
                        hw_dev_acquisition_stop(sdi, sdi);
 
-       } else if (devc->state.state == SIGMA_DOWNLOAD) {
+       }
+
+       if (devc->state.state == SIGMA_DOWNLOAD) {
                if (devc->state.chunks_downloaded >= numchunks) {
                        /* End of samples. */
                        packet.type = SR_DF_END;
@@ -1078,7 +1065,7 @@ static int receive_data(int fd, int revents, void *cb_data)
                newchunks = MIN(chunks_per_read,
                                numchunks - devc->state.chunks_downloaded);
 
-               sr_info("sigma: Downloading sample data: %.0f %%",
+               sr_info("Downloading sample data: %.0f %%.",
                        100.0 * devc->state.chunks_downloaded / numchunks);
 
                bufsz = sigma_read_dram(devc->state.chunks_downloaded,
@@ -1284,15 +1271,19 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
        struct dev_context *devc;
        struct sr_datafeed_packet *packet;
        struct sr_datafeed_header *header;
-       struct sr_datafeed_meta_logic meta;
        struct clockselect_50 clockselect;
        int frac, triggerpin, ret;
-       uint8_t triggerselect;
+       uint8_t triggerselect = 0;
        struct triggerinout triggerinout_conf;
        struct triggerlut lut;
 
        devc = sdi->priv;
 
+       if (configure_probes(sdi) != SR_OK) {
+               sr_err("Failed to configure probes.");
+               return SR_ERR;
+       }
+
        /* If the samplerate has not been set, default to 200 kHz. */
        if (devc->cur_firmware == -1) {
                if ((ret = set_samplerate(sdi, SR_KHZ(200))) != SR_OK)
@@ -1374,12 +1365,12 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
        devc->session_dev_id = cb_data;
 
        if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
-               sr_err("sigma: %s: packet malloc failed.", __func__);
+               sr_err("%s: packet malloc failed.", __func__);
                return SR_ERR_MALLOC;
        }
 
        if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
-               sr_err("sigma: %s: header malloc failed.", __func__);
+               sr_err("%s: header malloc failed.", __func__);
                return SR_ERR_MALLOC;
        }
 
@@ -1390,13 +1381,6 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
        gettimeofday(&header->starttime, NULL);
        sr_session_send(devc->session_dev_id, packet);
 
-       /* Send metadata about the SR_DF_LOGIC packets to come. */
-       packet->type = SR_DF_META_LOGIC;
-       packet->payload = &meta;
-       meta.samplerate = devc->cur_samplerate;
-       meta.num_probes = devc->num_probes;
-       sr_session_send(devc->session_dev_id, packet);
-
        /* Add capture source. */
        sr_source_add(0, G_IO_IN, 10, receive_data, (void *)sdi);
 
@@ -1408,19 +1392,17 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
        return SR_OK;
 }
 
-static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
-               void *cb_data)
+static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
 {
        struct dev_context *devc;
        uint8_t modestatus;
 
-       /* Avoid compiler warnings. */
        (void)cb_data;
 
        sr_source_remove(0);
 
        if (!(devc = sdi->priv)) {
-               sr_err("sigma: %s: sdi->priv was NULL", __func__);
+               sr_err("%s: sdi->priv was NULL", __func__);
                return SR_ERR_BUG;
        }
 
@@ -1454,6 +1436,8 @@ SR_PRIV struct sr_dev_driver asix_sigma_driver_info = {
        .init = hw_init,
        .cleanup = hw_cleanup,
        .scan = hw_scan,
+       .dev_list = hw_dev_list,
+       .dev_clear = clear_instances,
        .dev_open = hw_dev_open,
        .dev_close = hw_dev_close,
        .info_get = hw_info_get,