X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Fhardware%2Fscpi-pps%2Fapi.c;h=5bce3af0d3f092bb35b861c06c5b1f60b9d5d6c4;hb=b02bb45f4cf6378520e5a5b82ff39013cfa270b6;hp=1fddeea05ac8984c7742902ebd3866f026db9453;hpb=34577da641d836dcf27433fee3c6384565ca7847;p=libsigrok.git
diff --git a/src/hardware/scpi-pps/api.c b/src/hardware/scpi-pps/api.c
index 1fddeea0..5bce3af0 100644
--- a/src/hardware/scpi-pps/api.c
+++ b/src/hardware/scpi-pps/api.c
@@ -17,12 +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,
@@ -40,12 +42,10 @@ 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;
@@ -66,7 +66,7 @@ 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;
}
@@ -90,7 +90,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);
@@ -167,24 +166,84 @@ static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
hw_info = NULL;
scpi_cmd(sdi, devc->device->commands, SCPI_CMD_LOCAL);
- sr_scpi_close(scpi);
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)
{
- return sr_scpi_scan(di->context, options, probe_device);
+ 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 GSList *dev_list(const struct sr_dev_driver *di)
+static struct sr_dev_inst *probe_scpi_pps_device(struct sr_scpi_dev_inst *scpi)
{
- return ((struct drv_context *)(di->context))->instances;
+ return probe_device(scpi, sr_scpi_get_hw_id);
}
-static int dev_clear(const struct sr_dev_driver *di)
+static struct sr_dev_inst *probe_hpib_pps_device(struct sr_scpi_dev_inst *scpi)
{
- return std_dev_clear(di, NULL);
+ return probe_device(scpi, hpib_get_hw_id);
+}
+
+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 sr_scpi_scan(di->context, options, probe_hpib_pps_device);
}
static int dev_open(struct sr_dev_inst *sdi)
@@ -204,7 +263,6 @@ static int dev_open(struct sr_dev_inst *sdi)
devc = sdi->priv;
scpi_cmd(sdi, devc->device->commands, SCPI_CMD_REMOTE);
- devc = sdi->priv;
devc->beeper_was_set = FALSE;
if (scpi_cmd_resp(sdi, devc->device->commands, &beeper,
G_VARIANT_TYPE_BOOLEAN, SCPI_CMD_BEEPER) == SR_OK) {
@@ -249,7 +307,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);
}
@@ -261,7 +319,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;
- char *s;
+ const char *s;
if (!sdi)
return SR_ERR_ARG;
@@ -364,19 +422,20 @@ static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *s
* return VOLT/CURR. We always return a GVariant string in
* the Rigol notation.
*/
- if ((ret = sr_scpi_get_string(sdi->conn, NULL, &s)) != SR_OK)
- return ret;
- if (!strcmp(s, "CV") || !strcmp(s, "VOLT")) {
+ 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, "CC") || !strcmp(s, "CURR")) {
+ } else if (!strcmp(s, "CURR")) {
+ g_variant_unref(*data);
*data = g_variant_new_string("CC");
- } else if (!strcmp(s, "UR")) {
- *data = g_variant_new_string("UR");
- } else {
+ }
+
+ 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;
}
- g_free(s);
}
return ret;
@@ -400,7 +459,7 @@ static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sd
select_channel(sdi, cg->channels->data);
devc = sdi->priv;
- ret = SR_OK;
+
switch (key) {
case SR_CONF_ENABLED:
if (g_variant_get_boolean(data))
@@ -576,7 +635,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;
@@ -589,12 +648,11 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
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 = sr_next_enabled_channel(sdi, NULL);
@@ -616,14 +674,11 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
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;
@@ -637,20 +692,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,
@@ -661,3 +734,5 @@ SR_PRIV struct sr_dev_driver scpi_pps_driver_info = {
.dev_acquisition_stop = dev_acquisition_stop,
.context = NULL,
};
+SR_REGISTER_DEV_DRIVER(scpi_pps_driver_info);
+SR_REGISTER_DEV_DRIVER(hp_ib_pps_driver_info);