X-Git-Url: https://sigrok.org/gitweb/?p=libsigrok.git;a=blobdiff_plain;f=src%2Fhardware%2Fscpi-pps%2Fapi.c;h=fc06e906370372729d44f79fe46a661af8b445ff;hp=5b64f4b622e9b4a5f788b14a00f03f61e56bc620;hb=4a471029c230a7e2e0b15528d22bf30fb7cf2472;hpb=fdedbfcdef0b0d0b36855df3f118efb219048368 diff --git a/src/hardware/scpi-pps/api.c b/src/hardware/scpi-pps/api.c index 5b64f4b6..fc06e906 100644 --- a/src/hardware/scpi-pps/api.c +++ b/src/hardware/scpi-pps/api.c @@ -25,7 +25,7 @@ static struct sr_dev_driver *di = &scpi_pps_driver_info; extern unsigned int num_pps_profiles; extern const struct scpi_pps pps_profiles[]; -static const int32_t scanopts[] = { +static const uint32_t scanopts[] = { SR_CONF_CONN, SR_CONF_SERIALCOMM, }; @@ -50,13 +50,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, old_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,44 +85,59 @@ static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi) return NULL; } - sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, vendor, hw_info->model, + sdi = sr_dev_inst_new(SR_ST_ACTIVE, vendor, hw_info->model, hw_info->firmware_version); sdi->conn = scpi; sdi->driver = di; 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 = device->channels; + num_channels = device->num_channels; + channel_groups = 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. */ - old_idx = ch_idx; 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); + channels[ch_num].name); ch = sr_channel_new(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); } - if (ch_idx == old_idx) { - /* - * Didn't create any channels for this hardware output. - * This can happen if the device has no measurement capability. - */ - g_free(pch); - continue; - } } - 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) { @@ -139,8 +156,10 @@ static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi) sdi->channel_groups = g_slist_append(sdi->channel_groups, cg); } - /* SCPI devices commonly lock the panel keys when accessed remotely. */ - scpi_cmd(sdi, SCPI_CMD_KEY_UNLOCK); + sr_scpi_hw_info_free(hw_info); + hw_info = NULL; + + scpi_cmd(sdi, SCPI_CMD_LOCAL); sr_scpi_close(scpi); return sdi; @@ -163,7 +182,9 @@ static int dev_clear(void) 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) return SR_ERR; @@ -174,19 +195,34 @@ 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; } 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) { - scpi_cmd(sdi, SCPI_CMD_KEY_UNLOCK); + 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; } @@ -194,28 +230,34 @@ static int dev_close(struct sr_dev_inst *sdi) return SR_OK; } +static void clear_helper(void *priv) +{ + struct dev_context *devc; + + devc = priv; + g_free(devc->channels); + g_free(devc->channel_groups); + g_free(devc); +} + static int cleanup(void) { - return SR_OK; + return std_dev_clear(di, clear_helper); } -static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi, +static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) { struct dev_context *devc; - struct sr_scpi_dev_inst *scpi; - struct sr_channel *ch; - struct pps_channel *pch; const GVariantType *gvtype; unsigned int i; int cmd, ret; - char *s; + const char *s; if (!sdi) return SR_ERR_ARG; devc = sdi->priv; - scpi = sdi->conn; if (cg) { /* @@ -235,9 +277,6 @@ static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi, break; } } - - ch = cg->channels->data; - pch = ch->priv; } gvtype = NULL; @@ -251,17 +290,17 @@ static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi, gvtype = G_VARIANT_TYPE_DOUBLE; cmd = SCPI_CMD_GET_MEAS_VOLTAGE; break; - case SR_CONF_OUTPUT_VOLTAGE_MAX: + case SR_CONF_OUTPUT_VOLTAGE_TARGET: gvtype = G_VARIANT_TYPE_DOUBLE; - cmd = SCPI_CMD_GET_VOLTAGE_MAX; + cmd = SCPI_CMD_GET_VOLTAGE_TARGET; break; case SR_CONF_OUTPUT_CURRENT: gvtype = G_VARIANT_TYPE_DOUBLE; cmd = SCPI_CMD_GET_MEAS_CURRENT; break; - case SR_CONF_OUTPUT_CURRENT_MAX: + case SR_CONF_OUTPUT_CURRENT_LIMIT: gvtype = G_VARIANT_TYPE_DOUBLE; - cmd = SCPI_CMD_GET_CURRENT_MAX; + cmd = SCPI_CMD_GET_CURRENT_LIMIT; break; case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED: gvtype = G_VARIANT_TYPE_BOOLEAN; @@ -291,30 +330,32 @@ static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi, gvtype = G_VARIANT_TYPE_BOOLEAN; cmd = SCPI_CMD_GET_OVER_TEMPERATURE_PROTECTION; break; + case SR_CONF_OUTPUT_REGULATION: + gvtype = G_VARIANT_TYPE_STRING; + cmd = SCPI_CMD_GET_OUTPUT_REGULATION; } if (gvtype) { if (cg) - ret = scpi_cmd_resp(sdi, data, gvtype, cmd, pch->hwname); - else - ret = scpi_cmd_resp(sdi, data, gvtype, cmd); - } else if (cg) { - switch (key) { - case SR_CONF_OUTPUT_REGULATION: - ret = SR_ERR; - if (scpi_cmd(sdi, SCPI_CMD_GET_OUTPUT_REGULATION, pch->hwname) == SR_OK) { - if (sr_scpi_get_string(scpi, NULL, &s) == SR_OK) { - if (strcmp(s, "CC") && strcmp(s, "CV") && strcmp(s, "UR")) { - sr_dbg("Unknown response to SCPI_CMD_GET_OUTPUT_REGULATION: %s", s); - } else { - *data = g_variant_new_string(s); - g_free(s); - ret = SR_OK; - } + 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; } - break; - default: - ret = SR_ERR_NA; } } else ret = SR_ERR_NA; @@ -322,14 +363,11 @@ static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi, return ret; } -static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi, +static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) { - struct sr_channel *ch; - struct pps_channel *pch; double d; int ret; - const char *s; if (!sdi) return SR_ERR_ARG; @@ -337,75 +375,60 @@ static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi, if (sdi->status != SR_ST_ACTIVE) return SR_ERR_DEV_CLOSED; - ret = SR_OK; - if (!cg) { - switch (key) { - /* No channel group: global options. */ - case SR_CONF_OUTPUT_ENABLED: - s = g_variant_get_boolean(data) ? "ON" : "OFF"; - ret = scpi_cmd(sdi, SCPI_CMD_SET_OUTPUT_ENABLED, s); - break; - case SR_CONF_OUTPUT_VOLTAGE_MAX: - d = g_variant_get_double(data); - ret = scpi_cmd(sdi, SCPI_CMD_SET_VOLTAGE_MAX, d); - break; - case SR_CONF_OUTPUT_CURRENT_MAX: - d = g_variant_get_double(data); - ret = scpi_cmd(sdi, SCPI_CMD_SET_CURRENT_MAX, d); - break; - case SR_CONF_OVER_TEMPERATURE_PROTECTION: - s = g_variant_get_boolean(data) ? "ON" : "OFF"; - ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_TEMPERATURE_PROTECTION, s); - break; - default: - ret = SR_ERR_NA; - } - } else { + if (cg) /* Channel group specified. */ - ch = cg->channels->data; - pch = ch->priv; - switch (key) { - case SR_CONF_OUTPUT_ENABLED: - s = g_variant_get_boolean(data) ? "ON" : "OFF"; - ret = scpi_cmd(sdi, SCPI_CMD_SET_OUTPUT_ENABLED, pch->hwname, s); - break; - case SR_CONF_OUTPUT_VOLTAGE_MAX: - d = g_variant_get_double(data); - ret = scpi_cmd(sdi, SCPI_CMD_SET_VOLTAGE_MAX, pch->hwname, d); - break; - case SR_CONF_OUTPUT_CURRENT_MAX: - d = g_variant_get_double(data); - ret = scpi_cmd(sdi, SCPI_CMD_SET_CURRENT_MAX, pch->hwname, d); - break; - case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED: - s = g_variant_get_boolean(data) ? "ON" : "OFF"; - ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_ENABLED, - pch->hwname, s); - break; - case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD: - d = g_variant_get_double(data); - ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_THRESHOLD, - pch->hwname, d); - break; - case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED: - s = g_variant_get_boolean(data) ? "ON" : "OFF"; - ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_CURRENT_PROTECTION_ENABLED, - pch->hwname, s); - break; - case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD: - d = g_variant_get_double(data); - ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_CURRENT_PROTECTION_THRESHOLD, - pch->hwname, d); - break; - default: - ret = SR_ERR_NA; - } + select_channel(sdi, cg->channels->data); + + ret = SR_OK; + switch (key) { + case SR_CONF_OUTPUT_ENABLED: + if (g_variant_get_boolean(data)) + ret = scpi_cmd(sdi, SCPI_CMD_SET_OUTPUT_ENABLE); + else + ret = scpi_cmd(sdi, SCPI_CMD_SET_OUTPUT_DISABLE); + break; + case SR_CONF_OUTPUT_VOLTAGE_TARGET: + d = g_variant_get_double(data); + ret = scpi_cmd(sdi, SCPI_CMD_SET_VOLTAGE_TARGET, d); + break; + case SR_CONF_OUTPUT_CURRENT_LIMIT: + d = g_variant_get_double(data); + ret = scpi_cmd(sdi, SCPI_CMD_SET_CURRENT_LIMIT, d); + break; + case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED: + if (g_variant_get_boolean(data)) + ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_ENABLE); + else + ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_DISABLE); + break; + case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD: + d = g_variant_get_double(data); + ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_THRESHOLD, d); + break; + case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED: + if (g_variant_get_boolean(data)) + ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_CURRENT_PROTECTION_ENABLE); + else + ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_CURRENT_PROTECTION_DISABLE); + break; + case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD: + d = g_variant_get_double(data); + ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_CURRENT_PROTECTION_THRESHOLD, d); + break; + case SR_CONF_OVER_TEMPERATURE_PROTECTION: + if (g_variant_get_boolean(data)) + ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_TEMPERATURE_PROTECTION_ENABLE); + else + ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_TEMPERATURE_PROTECTION_DISABLE); + break; + default: + ret = SR_ERR_NA; } return ret; } -static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi, +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; @@ -418,8 +441,8 @@ static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi, /* Always available, even without sdi. */ if (key == SR_CONF_SCAN_OPTIONS) { - *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, - scanopts, ARRAY_SIZE(scanopts), sizeof(int32_t)); + *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, + scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t)); return SR_OK; } @@ -432,9 +455,9 @@ static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi, /* No channel group: global options. */ switch (key) { case SR_CONF_DEVICE_OPTIONS: - *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, + *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, devc->device->devopts, devc->device->num_devopts, - sizeof(int32_t)); + sizeof(uint32_t)); break; case SR_CONF_OUTPUT_CHANNEL_CONFIG: /* Not used. */ @@ -469,11 +492,11 @@ static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi, switch (key) { case SR_CONF_DEVICE_OPTIONS: - *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, + *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, devc->device->devopts_cg, devc->device->num_devopts_cg, - sizeof(int32_t)); + sizeof(uint32_t)); break; - case SR_CONF_OUTPUT_VOLTAGE_MAX: + case SR_CONF_OUTPUT_VOLTAGE_TARGET: ch_spec = &(devc->device->channels[ch->index]); g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); /* Min, max, write resolution. */ @@ -483,7 +506,7 @@ static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi, } *data = g_variant_builder_end(&gvb); break; - case SR_CONF_OUTPUT_CURRENT_MAX: + case SR_CONF_OUTPUT_CURRENT_LIMIT: g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); /* Min, max, step. */ for (i = 0; i < 3; i++) { @@ -523,9 +546,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; - devc->cur_channel = 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) @@ -541,6 +565,7 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) { + struct sr_datafeed_packet packet; struct sr_scpi_dev_inst *scpi; float f; @@ -559,6 +584,9 @@ static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) sr_scpi_get_float(scpi, NULL, &f); sr_scpi_source_remove(sdi->session, scpi); + packet.type = SR_DF_END; + sr_session_send(sdi, &packet); + return SR_OK; }