X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Fhardware%2Fscpi-pps%2Fapi.c;h=9da525792625267db897a6158776257169f9e0ee;hb=329733d92c5004f0fe308eff26b9537fded2cdf3;hp=8bb0ac0ce4450bc70ab45a84f74d064674cfee83;hpb=bf48ccebeed885652312188770ec893f7425cb43;p=libsigrok.git diff --git a/src/hardware/scpi-pps/api.c b/src/hardware/scpi-pps/api.c index 8bb0ac0c..9da52579 100644 --- a/src/hardware/scpi-pps/api.c +++ b/src/hardware/scpi-pps/api.c @@ -21,7 +21,6 @@ #include "protocol.h" SR_PRIV struct sr_dev_driver scpi_pps_driver_info; -static struct sr_dev_driver *di = &scpi_pps_driver_info; extern unsigned int num_pps_profiles; extern const struct scpi_pps pps_profiles[]; @@ -30,13 +29,17 @@ static const uint32_t scanopts[] = { SR_CONF_SERIALCOMM, }; -static struct pps_channel_instance pci[] = { +static const uint32_t drvopts[] = { + SR_CONF_POWER_SUPPLY, +}; + +static const struct pps_channel_instance pci[] = { { SR_MQ_VOLTAGE, SCPI_CMD_GET_MEAS_VOLTAGE, "V" }, { SR_MQ_CURRENT, SCPI_CMD_GET_MEAS_CURRENT, "I" }, { SR_MQ_POWER, SCPI_CMD_GET_MEAS_POWER, "P" }, }; -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); } @@ -50,13 +53,15 @@ static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi) struct sr_channel *ch; const struct scpi_pps *device; struct pps_channel *pch; - const struct channel_group_spec *cgs; + struct channel_spec *channels; + struct channel_group_spec *channel_groups, *cgs; struct pps_channel_group *pcg; GRegex *model_re; GMatchInfo *model_mi; GSList *l; uint64_t mask; - unsigned int ch_num, ch_idx, i, j; + unsigned int num_channels, num_channel_groups, ch_num, ch_idx, i, j; + int ret; const char *vendor; char ch_name[16]; @@ -83,35 +88,62 @@ static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi) return NULL; } - sdi = sr_dev_inst_new(SR_ST_ACTIVE, vendor, hw_info->model, - hw_info->firmware_version); + sdi = g_malloc0(sizeof(struct sr_dev_inst)); + sdi->status = SR_ST_INACTIVE; + sdi->vendor = g_strdup(vendor); + sdi->model = g_strdup(hw_info->model); + sdi->version = g_strdup(hw_info->firmware_version); sdi->conn = scpi; - sdi->driver = di; + sdi->driver = &scpi_pps_driver_info; sdi->inst_type = SR_INST_SCPI; + sdi->serial_num = g_strdup(hw_info->serial_number); + devc = g_malloc0(sizeof(struct dev_context)); devc->device = device; sdi->priv = devc; + if (device->num_channels) { + /* Static channels and groups. */ + channels = (struct channel_spec *)device->channels; + num_channels = device->num_channels; + channel_groups = (struct channel_group_spec *)device->channel_groups; + num_channel_groups = device->num_channel_groups; + } else { + /* Channels and groups need to be probed. */ + ret = device->probe_channels(sdi, hw_info, &channels, &num_channels, + &channel_groups, &num_channel_groups); + if (ret != SR_OK) { + sr_err("Failed to probe for channels."); + return NULL; + } + /* + * Since these were dynamically allocated, we'll need to free them + * later. + */ + devc->channels = channels; + devc->channel_groups = channel_groups; + } + ch_idx = 0; - for (ch_num = 0; ch_num < device->num_channels; ch_num++) { + for (ch_num = 0; ch_num < num_channels; ch_num++) { /* Create one channel per measurable output unit. */ for (i = 0; i < ARRAY_SIZE(pci); i++) { if (!scpi_cmd_get(sdi, pci[i].command)) continue; g_snprintf(ch_name, 16, "%s%s", pci[i].prefix, - device->channels[ch_num].name); - ch = sr_channel_new(ch_idx++, SR_CHANNEL_ANALOG, TRUE, ch_name); + channels[ch_num].name); + ch = sr_channel_new(sdi, ch_idx++, SR_CHANNEL_ANALOG, TRUE, + ch_name); pch = g_malloc0(sizeof(struct pps_channel)); pch->hw_output_idx = ch_num; - pch->hwname = device->channels[ch_num].name; + pch->hwname = channels[ch_num].name; pch->mq = pci[i].mq; ch->priv = pch; - sdi->channels = g_slist_append(sdi->channels, ch); } } - for (i = 0; i < device->num_channel_groups; i++) { - cgs = &device->channel_groups[i]; + for (i = 0; i < num_channel_groups; i++) { + cgs = &channel_groups[i]; cg = g_malloc0(sizeof(struct sr_channel_group)); cg->name = g_strdup(cgs->name); for (j = 0, mask = 1; j < 64; j++, mask <<= 1) { @@ -130,32 +162,37 @@ static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi) sdi->channel_groups = g_slist_append(sdi->channel_groups, cg); } + sr_scpi_hw_info_free(hw_info); + hw_info = NULL; + scpi_cmd(sdi, SCPI_CMD_LOCAL); sr_scpi_close(scpi); return sdi; } -static GSList *scan(GSList *options) +static GSList *scan(struct sr_dev_driver *di, GSList *options) { return sr_scpi_scan(di->priv, options, probe_device); } -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_clear(void) +static int dev_clear(const struct sr_dev_driver *di) { return std_dev_clear(di, NULL); } static int dev_open(struct sr_dev_inst *sdi) { + struct dev_context *devc; struct sr_scpi_dev_inst *scpi; + GVariant *beeper; - if (sdi->status != SR_ST_ACTIVE) + if (sdi->status != SR_ST_INACTIVE) return SR_ERR; scpi = sdi->conn; @@ -165,6 +202,15 @@ static int dev_open(struct sr_dev_inst *sdi) sdi->status = SR_ST_ACTIVE; scpi_cmd(sdi, SCPI_CMD_REMOTE); + devc = sdi->priv; + devc->beeper_was_set = FALSE; + if (scpi_cmd_resp(sdi, &beeper, G_VARIANT_TYPE_BOOLEAN, SCPI_CMD_BEEPER) == SR_OK) { + if (g_variant_get_boolean(beeper)) { + devc->beeper_was_set = TRUE; + scpi_cmd(sdi, SCPI_CMD_BEEPER_DISABLE); + } + g_variant_unref(beeper); + } return SR_OK; } @@ -172,12 +218,16 @@ static int dev_open(struct sr_dev_inst *sdi) static int dev_close(struct sr_dev_inst *sdi) { struct sr_scpi_dev_inst *scpi; + struct dev_context *devc; if (sdi->status != SR_ST_ACTIVE) return SR_ERR_DEV_CLOSED; + devc = sdi->priv; scpi = sdi->conn; if (scpi) { + if (devc->beeper_was_set) + scpi_cmd(sdi, SCPI_CMD_BEEPER_ENABLE); scpi_cmd(sdi, SCPI_CMD_LOCAL); sr_scpi_close(scpi); sdi->status = SR_ST_INACTIVE; @@ -186,9 +236,19 @@ static int dev_close(struct sr_dev_inst *sdi) return SR_OK; } -static int cleanup(void) +static void clear_helper(void *priv) { - return std_dev_clear(di, NULL); + struct dev_context *devc; + + devc = priv; + g_free(devc->channels); + g_free(devc->channel_groups); + g_free(devc); +} + +static int cleanup(const struct sr_dev_driver *di) +{ + return std_dev_clear(di, clear_helper); } static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, @@ -198,7 +258,6 @@ static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *s const GVariantType *gvtype; unsigned int i; int cmd, ret; - const char *s; if (!sdi) return SR_ERR_ARG; @@ -284,25 +343,6 @@ static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *s if (cg) select_channel(sdi, cg->channels->data); ret = scpi_cmd_resp(sdi, data, gvtype, cmd); - - if (gvtype == G_VARIANT_TYPE_STRING && ret == SR_OK) { - /* Non-standard data type responses. */ - switch (key) { - case SCPI_CMD_GET_OUTPUT_REGULATION: - /* - * This is specific to the Rigol DP800 series. - * We return the same string for now until more - * models with this key are supported. Do a check - * just for the hell of it. - */ - s = g_variant_get_string(*data, NULL); - if (strcmp(s, "CC") && strcmp(s, "CV") && strcmp(s, "UR")) { - sr_dbg("Unknown response to SCPI_CMD_GET_OUTPUT_REGULATION: %s", s); - ret = SR_ERR_DATA; - } - break; - } - } } else ret = SR_ERR_NA; @@ -379,7 +419,7 @@ static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst * { struct dev_context *devc; struct sr_channel *ch; - struct channel_spec *ch_spec; + const struct channel_spec *ch_spec; GVariant *gvar; GVariantBuilder gvb; int ret, i; @@ -390,6 +430,10 @@ static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst * *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t)); return SR_OK; + } else if (key == SR_CONF_DEVICE_OPTIONS && !sdi) { + *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, + drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t)); + return SR_OK; } if (!sdi) @@ -492,9 +536,10 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, std_session_send_df_header(sdi, LOG_PREFIX); /* Prime the pipe with the first channel's fetch. */ - ch = sdi->channels->data; + ch = next_enabled_channel(sdi, NULL); pch = ch->priv; - select_channel(sdi, ch); + if ((ret = select_channel(sdi, ch)) != SR_OK) + return ret; if (pch->mq == SR_MQ_VOLTAGE) cmd = SCPI_CMD_GET_MEAS_VOLTAGE; else if (pch->mq == SR_MQ_CURRENT)