X-Git-Url: http://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Fhardware%2Fscpi-pps%2Fapi.c;h=57bdcb81b7df9339c033e787ce1c95958c797fb3;hb=eac48b34917cf19c2b5ba52ccea85ec94bb5830f;hp=cf6bc65b6f0d25fbbd8dd0e13e5d4a611f3c3507;hpb=c1603f4574c8ce9648b51ea587b99cfb0bb02420;p=libsigrok.git
diff --git a/src/hardware/scpi-pps/api.c b/src/hardware/scpi-pps/api.c
index cf6bc65b..57bdcb81 100644
--- a/src/hardware/scpi-pps/api.c
+++ b/src/hardware/scpi-pps/api.c
@@ -17,11 +17,14 @@
* along with this program. If not, see .
*/
+#include
#include
#include
+#include "scpi.h"
#include "protocol.h"
-SR_PRIV struct sr_dev_driver scpi_pps_driver_info;
+static struct sr_dev_driver scpi_pps_driver_info;
+static struct sr_dev_driver hp_ib_pps_driver_info;
static const uint32_t scanopts[] = {
SR_CONF_CONN,
@@ -39,12 +42,9 @@ static const struct pps_channel_instance pci[] = {
{ SR_MQ_FREQUENCY, SCPI_CMD_GET_MEAS_FREQUENCY, "F" },
};
-static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
-{
- return std_init(sr_ctx, di, LOG_PREFIX);
-}
-
-static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
+static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi,
+ int (*get_hw_id)(struct sr_scpi_dev_inst *scpi,
+ struct sr_scpi_hw_info **scpi_response))
{
struct dev_context *devc;
struct sr_dev_inst *sdi;
@@ -65,15 +65,15 @@ static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
const char *vendor;
char ch_name[16];
- if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
+ if (get_hw_id(scpi, &hw_info) != SR_OK) {
sr_info("Couldn't get IDN response.");
return NULL;
}
device = NULL;
for (i = 0; i < num_pps_profiles; i++) {
- vendor = get_vendor(hw_info->manufacturer);
- if (strcasecmp(vendor, pps_profiles[i].vendor))
+ vendor = sr_vendor_alias(hw_info->manufacturer);
+ if (g_ascii_strcasecmp(vendor, pps_profiles[i].vendor))
continue;
model_re = g_regex_new(pps_profiles[i].model, 0, 0, NULL);
if (g_regex_match(model_re, hw_info->model, 0, &model_mi))
@@ -89,7 +89,6 @@ static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
}
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);
@@ -128,7 +127,7 @@ static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
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))
+ if (!scpi_cmd_get(devc->device->commands, pci[i].command))
continue;
g_snprintf(ch_name, 16, "%s%s", pci[i].prefix,
channels[ch_num].name);
@@ -165,25 +164,85 @@ static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
sr_scpi_hw_info_free(hw_info);
hw_info = NULL;
- scpi_cmd(sdi, SCPI_CMD_LOCAL);
- sr_scpi_close(scpi);
+ scpi_cmd(sdi, devc->device->commands, SCPI_CMD_LOCAL);
return sdi;
}
-static GSList *scan(struct sr_dev_driver *di, GSList *options)
+static gchar *hpib_get_revision(struct sr_scpi_dev_inst *scpi)
+{
+ int ret;
+ gboolean matches;
+ char *response;
+ GRegex *version_regex;
+
+ ret = sr_scpi_get_string(scpi, "ROM?", &response);
+ if (ret != SR_OK && !response)
+ return NULL;
+
+ /* Example version string: "B01 B01" */
+ version_regex = g_regex_new("[A-Z][0-9]{2} [A-Z][0-9]{2}", 0, 0, NULL);
+ matches = g_regex_match(version_regex, response, 0, NULL);
+ g_regex_unref(version_regex);
+
+ if (!matches) {
+ /* Not a valid version string. Ignore it. */
+ g_free(response);
+ response = NULL;
+ } else {
+ /* Replace space with dot. */
+ response[3] = '.';
+ }
+
+ return response;
+}
+
+/*
+ * This function assumes the response is in the form "HP"
+ *
+ * HP made many GPIB (then called HP-IB) instruments before the SCPI command
+ * set was introduced into the standard. We haven't seen any non-HP instruments
+ * which respond to the "ID?" query, so assume all are HP for now.
+ */
+static int hpib_get_hw_id(struct sr_scpi_dev_inst *scpi,
+ struct sr_scpi_hw_info **scpi_response)
+{
+ int ret;
+ char *response;
+ struct sr_scpi_hw_info *hw_info;
+
+ ret = sr_scpi_get_string(scpi, "ID?", &response);
+ if ((ret != SR_OK) || !response)
+ return SR_ERR;
+
+ hw_info = g_malloc0(sizeof(struct sr_scpi_hw_info));
+
+ *scpi_response = hw_info;
+ hw_info->model = response;
+ hw_info->firmware_version = hpib_get_revision(scpi);
+ hw_info->manufacturer = g_strdup("HP");
+
+ return SR_OK;
+}
+
+static struct sr_dev_inst *probe_scpi_pps_device(struct sr_scpi_dev_inst *scpi)
{
- return sr_scpi_scan(di->priv, options, probe_device);
+ return probe_device(scpi, sr_scpi_get_hw_id);
}
-static GSList *dev_list(const struct sr_dev_driver *di)
+static struct sr_dev_inst *probe_hpib_pps_device(struct sr_scpi_dev_inst *scpi)
{
- return ((struct drv_context *)(di->priv))->instances;
+ return probe_device(scpi, hpib_get_hw_id);
}
-static int dev_clear(const struct sr_dev_driver *di)
+static GSList *scan_scpi_pps(struct sr_dev_driver *di, GSList *options)
+{
+ return sr_scpi_scan(di->context, options, probe_scpi_pps_device);
+}
+
+static GSList *scan_hpib_pps(struct sr_dev_driver *di, GSList *options)
{
- return std_dev_clear(di, NULL);
+ return sr_scpi_scan(di->context, options, probe_hpib_pps_device);
}
static int dev_open(struct sr_dev_inst *sdi)
@@ -201,13 +260,14 @@ static int dev_open(struct sr_dev_inst *sdi)
sdi->status = SR_ST_ACTIVE;
- scpi_cmd(sdi, SCPI_CMD_REMOTE);
devc = sdi->priv;
+ scpi_cmd(sdi, devc->device->commands, SCPI_CMD_REMOTE);
devc->beeper_was_set = FALSE;
- if (scpi_cmd_resp(sdi, &beeper, G_VARIANT_TYPE_BOOLEAN, SCPI_CMD_BEEPER) == SR_OK) {
+ if (scpi_cmd_resp(sdi, devc->device->commands, &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);
+ scpi_cmd(sdi, devc->device->commands, SCPI_CMD_BEEPER_DISABLE);
}
g_variant_unref(beeper);
}
@@ -227,8 +287,8 @@ static int dev_close(struct sr_dev_inst *sdi)
scpi = sdi->conn;
if (scpi) {
if (devc->beeper_was_set)
- scpi_cmd(sdi, SCPI_CMD_BEEPER_ENABLE);
- scpi_cmd(sdi, SCPI_CMD_LOCAL);
+ scpi_cmd(sdi, devc->device->commands, SCPI_CMD_BEEPER_ENABLE);
+ scpi_cmd(sdi, devc->device->commands, SCPI_CMD_LOCAL);
sr_scpi_close(scpi);
sdi->status = SR_ST_INACTIVE;
}
@@ -246,7 +306,7 @@ static void clear_helper(void *priv)
g_free(devc);
}
-static int cleanup(const struct sr_dev_driver *di)
+static int dev_clear(const struct sr_dev_driver *di)
{
return std_dev_clear(di, clear_helper);
}
@@ -258,6 +318,7 @@ 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;
@@ -287,15 +348,15 @@ static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *s
gvtype = NULL;
cmd = -1;
switch (key) {
- case SR_CONF_OUTPUT_ENABLED:
+ case SR_CONF_ENABLED:
gvtype = G_VARIANT_TYPE_BOOLEAN;
cmd = SCPI_CMD_GET_OUTPUT_ENABLED;
break;
- case SR_CONF_OUTPUT_VOLTAGE:
+ case SR_CONF_VOLTAGE:
gvtype = G_VARIANT_TYPE_DOUBLE;
cmd = SCPI_CMD_GET_MEAS_VOLTAGE;
break;
- case SR_CONF_OUTPUT_VOLTAGE_TARGET:
+ case SR_CONF_VOLTAGE_TARGET:
gvtype = G_VARIANT_TYPE_DOUBLE;
cmd = SCPI_CMD_GET_VOLTAGE_TARGET;
break;
@@ -307,11 +368,11 @@ static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *s
gvtype = G_VARIANT_TYPE_DOUBLE;
cmd = SCPI_CMD_GET_FREQUENCY_TARGET;
break;
- case SR_CONF_OUTPUT_CURRENT:
+ case SR_CONF_CURRENT:
gvtype = G_VARIANT_TYPE_DOUBLE;
cmd = SCPI_CMD_GET_MEAS_CURRENT;
break;
- case SR_CONF_OUTPUT_CURRENT_LIMIT:
+ case SR_CONF_CURRENT_LIMIT:
gvtype = G_VARIANT_TYPE_DOUBLE;
cmd = SCPI_CMD_GET_CURRENT_LIMIT;
break;
@@ -343,16 +404,38 @@ static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *s
gvtype = G_VARIANT_TYPE_BOOLEAN;
cmd = SCPI_CMD_GET_OVER_TEMPERATURE_PROTECTION;
break;
- case SR_CONF_OUTPUT_REGULATION:
+ case SR_CONF_REGULATION:
gvtype = G_VARIANT_TYPE_STRING;
cmd = SCPI_CMD_GET_OUTPUT_REGULATION;
}
- if (gvtype) {
- if (cg)
- select_channel(sdi, cg->channels->data);
- ret = scpi_cmd_resp(sdi, data, gvtype, cmd);
- } else
- ret = SR_ERR_NA;
+ if (!gvtype)
+ return SR_ERR_NA;
+
+ if (cg)
+ select_channel(sdi, cg->channels->data);
+ ret = scpi_cmd_resp(sdi, devc->device->commands, data, gvtype, cmd);
+
+ if (cmd == SCPI_CMD_GET_OUTPUT_REGULATION) {
+ /*
+ * The Rigol DP800 series return CV/CC/UR, Philips PM2800
+ * return VOLT/CURR. We always return a GVariant string in
+ * the Rigol notation.
+ */
+ s = g_variant_get_string(*data, NULL);
+ if (!strcmp(s, "VOLT")) {
+ g_variant_unref(*data);
+ *data = g_variant_new_string("CV");
+ } else if (!strcmp(s, "CURR")) {
+ g_variant_unref(*data);
+ *data = g_variant_new_string("CC");
+ }
+
+ s = g_variant_get_string(*data, NULL);
+ if (strcmp(s, "CV") && strcmp(s, "CC") && strcmp(s, "UR")) {
+ sr_dbg("Unknown response to SCPI_CMD_GET_OUTPUT_REGULATION: %s", s);
+ ret = SR_ERR_DATA;
+ }
+ }
return ret;
}
@@ -360,6 +443,7 @@ static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *s
static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
const struct sr_channel_group *cg)
{
+ struct dev_context *devc;
double d;
int ret;
@@ -373,51 +457,65 @@ static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sd
/* Channel group specified. */
select_channel(sdi, cg->channels->data);
- ret = SR_OK;
+ devc = sdi->priv;
+
switch (key) {
- case SR_CONF_OUTPUT_ENABLED:
+ case SR_CONF_ENABLED:
if (g_variant_get_boolean(data))
- ret = scpi_cmd(sdi, SCPI_CMD_SET_OUTPUT_ENABLE);
+ ret = scpi_cmd(sdi, devc->device->commands,
+ SCPI_CMD_SET_OUTPUT_ENABLE);
else
- ret = scpi_cmd(sdi, SCPI_CMD_SET_OUTPUT_DISABLE);
+ ret = scpi_cmd(sdi, devc->device->commands,
+ SCPI_CMD_SET_OUTPUT_DISABLE);
break;
- case SR_CONF_OUTPUT_VOLTAGE_TARGET:
+ case SR_CONF_VOLTAGE_TARGET:
d = g_variant_get_double(data);
- ret = scpi_cmd(sdi, SCPI_CMD_SET_VOLTAGE_TARGET, d);
+ ret = scpi_cmd(sdi, devc->device->commands,
+ SCPI_CMD_SET_VOLTAGE_TARGET, d);
break;
case SR_CONF_OUTPUT_FREQUENCY_TARGET:
d = g_variant_get_double(data);
- ret = scpi_cmd(sdi, SCPI_CMD_SET_FREQUENCY_TARGET, d);
+ ret = scpi_cmd(sdi, devc->device->commands,
+ SCPI_CMD_SET_FREQUENCY_TARGET, d);
break;
- case SR_CONF_OUTPUT_CURRENT_LIMIT:
+ case SR_CONF_CURRENT_LIMIT:
d = g_variant_get_double(data);
- ret = scpi_cmd(sdi, SCPI_CMD_SET_CURRENT_LIMIT, d);
+ ret = scpi_cmd(sdi, devc->device->commands,
+ 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);
+ ret = scpi_cmd(sdi, devc->device->commands,
+ SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_ENABLE);
else
- ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_DISABLE);
+ ret = scpi_cmd(sdi, devc->device->commands,
+ 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);
+ ret = scpi_cmd(sdi, devc->device->commands,
+ 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);
+ ret = scpi_cmd(sdi, devc->device->commands,
+ SCPI_CMD_SET_OVER_CURRENT_PROTECTION_ENABLE);
else
- ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_CURRENT_PROTECTION_DISABLE);
+ ret = scpi_cmd(sdi, devc->device->commands,
+ 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);
+ ret = scpi_cmd(sdi, devc->device->commands,
+ 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);
+ ret = scpi_cmd(sdi, devc->device->commands,
+ SCPI_CMD_SET_OVER_TEMPERATURE_PROTECTION_ENABLE);
else
- ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_TEMPERATURE_PROTECTION_DISABLE);
+ ret = scpi_cmd(sdi, devc->device->commands,
+ SCPI_CMD_SET_OVER_TEMPERATURE_PROTECTION_DISABLE);
break;
default:
ret = SR_ERR_NA;
@@ -461,7 +559,7 @@ static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *
devc->device->devopts, devc->device->num_devopts,
sizeof(uint32_t));
break;
- case SR_CONF_OUTPUT_CHANNEL_CONFIG:
+ case SR_CONF_CHANNEL_CONFIG:
/* Not used. */
i = 0;
if (devc->device->features & PPS_INDEPENDENT)
@@ -498,7 +596,7 @@ static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *
devc->device->devopts_cg, devc->device->num_devopts_cg,
sizeof(uint32_t));
break;
- case SR_CONF_OUTPUT_VOLTAGE_TARGET:
+ case SR_CONF_VOLTAGE_TARGET:
ch_spec = &(devc->device->channels[ch->index]);
g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
/* Min, max, write resolution. */
@@ -518,7 +616,7 @@ static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *
}
*data = g_variant_builder_end(&gvb);
break;
- case SR_CONF_OUTPUT_CURRENT_LIMIT:
+ case SR_CONF_CURRENT_LIMIT:
g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
/* Min, max, step. */
for (i = 0; i < 3; i++) {
@@ -536,8 +634,7 @@ static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *
return ret;
}
-static int dev_acquisition_start(const struct sr_dev_inst *sdi,
- void *cb_data)
+static int dev_acquisition_start(const struct sr_dev_inst *sdi)
{
struct dev_context *devc;
struct sr_scpi_dev_inst *scpi;
@@ -550,15 +647,14 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi,
devc = sdi->priv;
scpi = sdi->conn;
- devc->cb_data = cb_data;
if ((ret = sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 10,
scpi_pps_receive_data, (void *)sdi)) != SR_OK)
return ret;
- std_session_send_df_header(sdi, LOG_PREFIX);
+ std_session_send_df_header(sdi);
/* Prime the pipe with the first channel's fetch. */
- ch = next_enabled_channel(sdi, NULL);
+ ch = sr_next_enabled_channel(sdi, NULL);
pch = ch->priv;
if ((ret = select_channel(sdi, ch)) < 0)
return ret;
@@ -572,19 +668,16 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi,
cmd = SCPI_CMD_GET_MEAS_POWER;
else
return SR_ERR;
- scpi_cmd(sdi, cmd, pch->hwname);
+ scpi_cmd(sdi, devc->device->commands, cmd, pch->hwname);
return SR_OK;
}
-static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
+static int dev_acquisition_stop(struct sr_dev_inst *sdi)
{
- struct sr_datafeed_packet packet;
struct sr_scpi_dev_inst *scpi;
float f;
- (void)cb_data;
-
if (sdi->status != SR_ST_ACTIVE)
return SR_ERR_DEV_CLOSED;
@@ -598,20 +691,38 @@ 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);
+ std_session_send_df_end(sdi);
return SR_OK;
}
-SR_PRIV struct sr_dev_driver scpi_pps_driver_info = {
+static struct sr_dev_driver scpi_pps_driver_info = {
.name = "scpi-pps",
.longname = "SCPI PPS",
.api_version = 1,
- .init = init,
- .cleanup = cleanup,
- .scan = scan,
- .dev_list = dev_list,
+ .init = std_init,
+ .cleanup = std_cleanup,
+ .scan = scan_scpi_pps,
+ .dev_list = std_dev_list,
+ .dev_clear = dev_clear,
+ .config_get = config_get,
+ .config_set = config_set,
+ .config_list = config_list,
+ .dev_open = dev_open,
+ .dev_close = dev_close,
+ .dev_acquisition_start = dev_acquisition_start,
+ .dev_acquisition_stop = dev_acquisition_stop,
+ .context = NULL,
+};
+
+static struct sr_dev_driver hp_ib_pps_driver_info = {
+ .name = "hpib-pps",
+ .longname = "HP-IB PPS",
+ .api_version = 1,
+ .init = std_init,
+ .cleanup = std_cleanup,
+ .scan = scan_hpib_pps,
+ .dev_list = std_dev_list,
.dev_clear = dev_clear,
.config_get = config_get,
.config_set = config_set,
@@ -620,5 +731,7 @@ SR_PRIV struct sr_dev_driver scpi_pps_driver_info = {
.dev_close = dev_close,
.dev_acquisition_start = dev_acquisition_start,
.dev_acquisition_stop = dev_acquisition_stop,
- .priv = NULL,
+ .context = NULL,
};
+SR_REGISTER_DEV_DRIVER(scpi_pps_driver_info);
+SR_REGISTER_DEV_DRIVER(hp_ib_pps_driver_info);