hp-3457a: Implement basic configuration and sampling

KNOWN ISSUES:
- When sampling with 100 NPLC, the poll function will timeout a few
times before the first sample is acquired. Increasing the timeout
passed to sr_scpi_source_add() will cause all the other commands to
be processed slowly, producing a sampling rate of about one sample
every ten seconds.
- Support for plug-in cards (44491A and 44492A) is not implemented.
- Support for AC, AC+DC and four-wire resistance measurements is not
implemented.
- Support for configuring the frequency measurement source is not
implemented.
This commit is contained in:
Alexandru Gagniuc 2016-02-28 10:55:18 -08:00 committed by Uwe Hermann
parent fadb19ac96
commit db23af7fc2
3 changed files with 550 additions and 46 deletions

View File

@ -18,10 +18,121 @@
*/
#include <config.h>
#include <scpi.h>
#include <string.h>
#include "protocol.h"
static const uint32_t scanopts[] = {
SR_CONF_CONN,
};
static const uint32_t drvopts[] = {
SR_CONF_MULTIMETER,
};
static const uint32_t devopts[] = {
SR_CONF_CONTINUOUS | SR_CONF_SET,
SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
SR_CONF_MEASURED_QUANTITY | SR_CONF_SET,
SR_CONF_ADC_POWERLINE_CYCLES | SR_CONF_SET | SR_CONF_GET,
};
SR_PRIV struct sr_dev_driver hp_3457a_driver_info;
static int create_front_channel(struct sr_dev_inst *sdi, int chan_idx)
{
struct sr_channel *channel;
struct sr_channel_group *front;
channel = sr_channel_new(sdi, chan_idx++, SR_CHANNEL_ANALOG,
TRUE, "Front");
front = g_malloc0(sizeof(*front));
front->name = g_strdup("Front");
front->channels = g_slist_append(front->channels, channel);
sdi->channel_groups = g_slist_append(sdi->channel_groups, front);
return chan_idx;
}
static int create_rear_channels(struct sr_dev_inst *sdi, int chan_idx,
const struct rear_card_info *card)
{
(void) sdi;
/* When card is NULL, we couldn't identify the type of card. */
if (!card)
return chan_idx;
/* TODO: Create channel descriptor for plug-in cards here. */
return chan_idx;
}
static gchar *get_revision(struct sr_scpi_dev_inst *scpi)
{
int ret, major, minor;
GArray *rev_numbers;
/* Report a version of '0.0' if we can't parse the response. */
major = minor = 0;
ret = sr_scpi_get_floatv(scpi, "REV?", &rev_numbers);
if ((ret == SR_OK) && (rev_numbers->len >= 2)) {
major = (int)g_array_index(rev_numbers, float, 0);
minor = (int)g_array_index(rev_numbers, float, 1);
}
g_array_free(rev_numbers, TRUE);
return g_strdup_printf("%d.%d", major, minor);
}
static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
{
int ret, idx;
char *response;
struct sr_dev_inst *sdi;
struct dev_context *devc;
/*
* This command ensures we receive an EOI after every response, so that
* we don't wait the entire timeout after the response is received.
*/
if (sr_scpi_send(scpi, "END ALWAYS") != SR_OK)
return NULL;
ret = sr_scpi_get_string(scpi, "ID?", &response);
if ((ret != SR_OK) || !response)
return NULL;
if (strcmp(response, "HP3457A"))
return NULL;
g_free(response);
devc = g_malloc0(sizeof(struct dev_context));
sdi = g_malloc0(sizeof(struct sr_dev_inst));
sdi->vendor = g_strdup("Hewlett-Packard");
sdi->model = g_strdup("3457A");
sdi->version = get_revision(scpi);
sdi->conn = scpi;
sdi->driver = &hp_3457a_driver_info;
sdi->inst_type = SR_INST_SCPI;
sdi->priv = devc;
/* There is no way to probe the measurement mode. It must be set. */
devc->measurement_mq = 0;
devc->measurement_unit = 0;
/* Probe rear card option and create channels accordingly (TODO). */
devc->rear_card = hp_3457a_probe_rear_card(scpi);
idx = 0;
idx = create_front_channel(sdi, idx);
create_rear_channels(sdi, idx, devc->rear_card);
return sdi;
}
static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
{
return std_init(sr_ctx, di, LOG_PREFIX);
@ -29,19 +140,7 @@ static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
static GSList *scan(struct sr_dev_driver *di, GSList *options)
{
struct drv_context *drvc;
GSList *devices;
(void)options;
devices = NULL;
drvc = di->context;
drvc->instances = NULL;
/* TODO: scan for devices, either based on a SR_CONF_CONN option
* or on a USB scan. */
return devices;
return sr_scpi_scan(di->context, options, probe_device);
}
static GSList *dev_list(const struct sr_dev_driver *di)
@ -54,11 +153,39 @@ static int dev_clear(const struct sr_dev_driver *di)
return std_dev_clear(di, NULL);
}
/*
* We need to set the HP 3457A to a known state, and there are quite a number
* of knobs to tweak. Here's a brief explanation of what's going on. For more
* details, print out and consult the user manual.
* PRESET
* Set the instrument to a pre-determined state. This is easier and faster
* than sending a few dozen commands. Some of the PRESET defaults include
* ASCII output format, and synchronous triggering. See user manual for
* more details.
*
* After the PRESET command, the instrument is in a known state, and only those
* parameters for which the default is unsuitable are modified:
* INBUF ON
* Enable the HP-IB input buffer. This allows the instrument to release the
* HP-IB bus before processing the command, and increases throughput on
* GPIB buses with more than one device.
* TRIG HOLD
* Do not trigger new measurements until instructed to do so.
*/
static int dev_open(struct sr_dev_inst *sdi)
{
(void)sdi;
struct sr_scpi_dev_inst *scpi = sdi->conn;
struct dev_context *devc;
/* TODO: get handle from sdi->conn and open it. */
if (sr_scpi_open(scpi) != SR_OK)
return SR_ERR;
devc=sdi->priv;
sr_scpi_send(scpi, "PRESET");
sr_scpi_send(scpi, "INBUF ON");
sr_scpi_send(scpi, "TRIG HOLD");
sr_scpi_get_float(scpi, "NPLC?", &devc->nplc);
sdi->status = SR_ST_ACTIVE;
@ -67,9 +194,15 @@ static int dev_open(struct sr_dev_inst *sdi)
static int dev_close(struct sr_dev_inst *sdi)
{
(void)sdi;
struct sr_scpi_dev_inst *scpi = sdi->conn;
/* TODO: get handle from sdi->conn and close it. */
if (sdi->status != SR_ST_ACTIVE)
return SR_ERR_DEV_CLOSED;
/* Switch back to auto-triggering. */
sr_scpi_send(scpi, "TRIG AUTO");
sr_scpi_close(scpi);
sdi->status = SR_ST_INACTIVE;
@ -78,25 +211,24 @@ static int dev_close(struct sr_dev_inst *sdi)
static int cleanup(const struct sr_dev_driver *di)
{
dev_clear(di);
/* TODO: free other driver resources, if any. */
return SR_OK;
return dev_clear(di);
}
static int config_get(uint32_t key, GVariant **data,
const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
{
int ret;
struct dev_context *devc;
(void)sdi;
(void)data;
(void)cg;
devc = sdi->priv;
ret = SR_OK;
switch (key) {
/* TODO */
case SR_CONF_ADC_POWERLINE_CYCLES:
*data = g_variant_new_double(devc->nplc);
break;
default:
return SR_ERR_NA;
}
@ -105,19 +237,35 @@ static int config_get(uint32_t key, GVariant **data,
}
static int config_set(uint32_t key, GVariant *data,
const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
const struct sr_dev_inst *sdi,
const struct sr_channel_group *cg)
{
int ret;
enum sr_mq mq;
struct dev_context *devc;
GVariant *tuple_child;
(void)data;
(void)cg;
if (sdi->status != SR_ST_ACTIVE)
return SR_ERR_DEV_CLOSED;
devc = sdi->priv;
ret = SR_OK;
switch (key) {
/* TODO */
case SR_CONF_LIMIT_SAMPLES:
devc->limit_samples = g_variant_get_uint64(data);
break;
case SR_CONF_MEASURED_QUANTITY:
tuple_child = g_variant_get_child_value(data, 0);
mq = g_variant_get_uint32(tuple_child);
ret = hp_3457a_set_mq(sdi, mq);
g_variant_unref(tuple_child);
break;
case SR_CONF_ADC_POWERLINE_CYCLES:
ret = hp_3457a_set_nplc(sdi, g_variant_get_double(data));
break;
default:
ret = SR_ERR_NA;
}
@ -130,30 +278,72 @@ static int config_list(uint32_t key, GVariant **data,
{
int ret;
(void)sdi;
(void)data;
(void)cg;
if (key == SR_CONF_SCAN_OPTIONS) {
*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;
} else if ((key == SR_CONF_DEVICE_OPTIONS) && !cg) {
*data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
return SR_OK;
}
/* From here on, we're only concerned with channel group config. */
if (!cg)
return SR_ERR_NA;
/*
* TODO: Implement channel group configuration when adding support for
* plug-in cards.
*/
ret = SR_OK;
switch (key) {
/* TODO */
default:
return SR_ERR_NA;
ret = SR_ERR_NA;
}
return ret;
}
/*
* TRIG SGL
* Trigger the first measurement, then hold. We can't let the instrument
* auto-trigger because we read several registers to make a complete
* reading. If the instrument were auto-triggering, we could get the
* reading for sample N, but a new measurement is made and when we read the
* HIRES register, it contains data for sample N+1. This would produce
* wrong readings.
*/
static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
{
(void)sdi;
int ret;
struct sr_scpi_dev_inst *scpi;
struct dev_context *devc;
(void)cb_data;
if (sdi->status != SR_ST_ACTIVE)
return SR_ERR_DEV_CLOSED;
/* TODO: configure hardware, reset acquisition state, set up
* callbacks and send header packet. */
scpi = sdi->conn;
devc = sdi->priv;
ret = sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 100,
hp_3457a_receive_data, (void *)sdi);
if (ret != SR_OK)
return ret;
std_session_send_df_header(sdi, LOG_PREFIX);
/* Start first measurement. */
sr_scpi_send(scpi, "TRIG SGL");
devc->acq_state = ACQ_TRIGGERED_MEASUREMENT;
devc->num_samples = 0;
return SR_OK;
}
@ -165,8 +355,6 @@ static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
if (sdi->status != SR_ST_ACTIVE)
return SR_ERR_DEV_CLOSED;
/* TODO: stop acquisition. */
return SR_OK;
}

View File

@ -18,14 +18,251 @@
*/
#include <config.h>
#include <math.h>
#include <scpi.h>
#include "protocol.h"
/*
* Currently, only DC voltage and current are supported, as switching to AC or
* AC+DC requires mq flags, which is not yet implemented.
* Four-wire resistance measurements are not implemented (See "OHMF" command).
* The source for the frequency measurement can be either AC voltage, AC+DC
* voltage, AC current, or AC+DC current. Configuring this is not yet
* supported. For details, see "FSOURCE" command.
*/
static const struct {
enum sr_mq mq;
enum sr_unit unit;
const char *cmd;
} sr_mq_to_cmd_map[] = {
{ SR_MQ_VOLTAGE, SR_UNIT_VOLT, "DCV" },
{ SR_MQ_CURRENT, SR_UNIT_AMPERE, "DCI" },
{ SR_MQ_RESISTANCE, SR_UNIT_OHM, "OHM" },
{ SR_MQ_FREQUENCY, SR_UNIT_HERTZ, "FREQ" },
};
static const struct rear_card_info rear_card_parameters[] = {
{
.type = REAR_TERMINALS,
.card_id = 0,
.name = "Rear terminals",
.cg_name = "rear",
}, {
.type = HP_44491A,
.card_id = 44491,
.name = "44491A Armature Relay Multiplexer",
.cg_name = "44491a",
}, {
.type = HP_44492A,
.card_id = 44492,
.name = "44492A Reed Relay Multiplexer",
.cg_name = "44492a",
}
};
SR_PRIV int hp_3457a_set_mq(const struct sr_dev_inst *sdi, enum sr_mq mq)
{
int ret;
size_t i;
struct sr_scpi_dev_inst *scpi = sdi->conn;
struct dev_context *devc = sdi->priv;
for (i = 0; i < ARRAY_SIZE(sr_mq_to_cmd_map); i++) {
if (sr_mq_to_cmd_map[i].mq != mq)
continue;
ret = sr_scpi_send(scpi, sr_mq_to_cmd_map[i].cmd);
if (ret == SR_OK) {
devc->measurement_mq = sr_mq_to_cmd_map[i].mq;
devc->measurement_unit = sr_mq_to_cmd_map[i].unit;
}
return ret;
}
return SR_ERR_NA;
}
SR_PRIV const struct rear_card_info *hp_3457a_probe_rear_card(struct sr_scpi_dev_inst *scpi)
{
size_t i;
float card_fval;
unsigned int card_id;
const struct rear_card_info *rear_card = NULL;
if (sr_scpi_get_float(scpi, "OPT?", &card_fval) != SR_OK)
return NULL;
card_id = (unsigned int)card_fval;
for (i = 0; i < ARRAY_SIZE(rear_card_parameters); i++) {
if (rear_card_parameters[i].card_id == card_id) {
rear_card = rear_card_parameters + i;
break;
}
}
if (!rear_card)
return NULL;
sr_info("Found %s.", rear_card->name);
return rear_card;
}
SR_PRIV int hp_3457a_set_nplc(const struct sr_dev_inst *sdi, float nplc)
{
int ret;
struct sr_scpi_dev_inst *scpi = sdi->conn;
struct dev_context *devc = sdi->priv;
if ((nplc < 1E-6) || (nplc > 100))
return SR_ERR_ARG;
/* Only need one digit of precision here. */
ret = sr_scpi_send(scpi, "NPLC %.0E", nplc);
/*
* The instrument only has a few valid NPLC setting, so get back the
* one which was selected.
*/
sr_scpi_get_float(scpi, "NPLC?", &devc->nplc);
return ret;
}
/* HIRES register only contains valid data with 10 or more powerline cycles. */
static int is_highres_enabled(struct dev_context *devc)
{
return (devc->nplc >= 10.0);
}
static void retrigger_measurement(struct sr_scpi_dev_inst *scpi,
struct dev_context *devc)
{
sr_scpi_send(scpi, "?");
devc->acq_state = ACQ_TRIGGERED_MEASUREMENT;
}
static void request_hires(struct sr_scpi_dev_inst *scpi,
struct dev_context *devc)
{
sr_scpi_send(scpi, "RMATH HIRES");
devc->acq_state = ACQ_REQUESTED_HIRES;
}
static void request_range(struct sr_scpi_dev_inst *scpi,
struct dev_context *devc)
{
sr_scpi_send(scpi, "RANGE?");
devc->acq_state = ACQ_REQUESTED_RANGE;
}
/*
* Calculate the number of leading zeroes in the measurement.
*
* Depending on the range and measurement, a reading may not have eight digits
* of resolution. For example, on a 30V range:
* : 10.000000 V has 8 significant digits
* : 9.999999 V has 7 significant digits
* : 0.999999 V has 6 significant digits
*
* The number of significant digits is determined based on the range in which
* the measurement was taken:
* 1. By taking the base 10 logarithm of the range, and converting that to
* an integer, we can get the minimum reading which has a full resolution
* reading. Raising 10 to the integer power gives the full resolution.
* Ex: For 30 V range, a full resolution reading is 10.000000.
* 2. A ratio is taken between the full resolution reading and the
* measurement. Since the full resolution reading is a power of 10,
* for every leading zero, this ratio will be slightly higher than a
* power of 10. For example, for 10 V full resolution:
* : 10.000000 V, ratio = 1.0000000
* : 9.999999 V, ratio = 1.0000001
* : 0.999999 V, ratio = 10.000001
* 3. The ratio is rounded up to prevent loss of precision in the next step.
* 4. The base 10 logarithm of the ratio is taken, then rounded up. This
* gives the number of leading zeroes in the measurement.
* For example, for 10 V full resolution:
* : 10.000000 V, ceil(1.0000000) = 1, log10 = 0.00; 0 leading zeroes
* : 9.999999 V, ceil(1.0000001) = 2, log10 = 0.30; 1 leading zero
* : 0.999999 V, ceil(10.000001) = 11, log10 = 1.04, 2 leading zeroes
* 5. The number of leading zeroes is subtracted from the maximum number of
* significant digits, 8, at 7 1/2 digits resolution.
* For a 10 V full resolution reading, this gives:
* : 10.000000 V, 0 leading zeroes => 8 significant digits
* : 9.999999 V, 1 leading zero => 7 significant digits
* : 0.999999 V, 2 leading zeroes => 6 significant digits
*
* Single precision floating point numbers can achieve about 16 million counts,
* but in high resolution mode we can get as much as 30 million counts. As a
* result, these calculations must be done with double precision
* (the HP 3457A is a very precise instrument).
*/
static int calculate_num_zero_digits(double measurement, double range)
{
int zero_digits;
double min_full_res_reading, log10_range, full_res_ratio;
log10_range = log10(range);
min_full_res_reading = pow(10, (int)log10_range);
if (measurement > min_full_res_reading) {
zero_digits = 0;
} else if (measurement == 0.0) {
zero_digits = 0;
} else {
full_res_ratio = min_full_res_reading / measurement;
zero_digits = ceil(log10(ceil(full_res_ratio)));
}
return zero_digits;
}
static void acq_send_measurement(struct sr_dev_inst *sdi)
{
double hires_measurement;
int zero_digits, num_digits;
struct sr_datafeed_packet packet;
struct sr_datafeed_analog analog;
struct sr_analog_encoding encoding;
struct sr_analog_meaning meaning;
struct sr_analog_spec spec;
struct dev_context *devc = sdi->priv;
hires_measurement = devc->base_measurement;
if (is_highres_enabled(devc))
hires_measurement += devc->hires_register;
/* Figure out how many of the digits are significant. */
num_digits = is_highres_enabled(devc) ? 8 : 7;
zero_digits = calculate_num_zero_digits(hires_measurement,
devc->measurement_range);
num_digits = num_digits - zero_digits;
packet.type = SR_DF_ANALOG;
packet.payload = &analog;
sr_analog_init(&analog, &encoding, &meaning, &spec, num_digits);
encoding.unitsize = sizeof(double);
meaning.channels = sdi->channels;
analog.num_samples = 1;
analog.data = &hires_measurement;
meaning.mq = devc->measurement_mq;
meaning.unit = devc->measurement_unit;
sr_session_send(sdi, &packet);
}
SR_PRIV int hp_3457a_receive_data(int fd, int revents, void *cb_data)
{
const struct sr_dev_inst *sdi;
int ret;
struct sr_scpi_dev_inst *scpi;
struct dev_context *devc;
struct sr_dev_inst *sdi = cb_data;
(void)fd;
(void)revents;
if (!(sdi = cb_data))
return TRUE;
@ -33,8 +270,55 @@ SR_PRIV int hp_3457a_receive_data(int fd, int revents, void *cb_data)
if (!(devc = sdi->priv))
return TRUE;
if (revents == G_IO_IN) {
/* TODO */
scpi = sdi->conn;
switch (devc->acq_state) {
case ACQ_TRIGGERED_MEASUREMENT:
ret = sr_scpi_get_double(scpi, NULL, &devc->base_measurement);
if (ret != SR_OK) {
retrigger_measurement(scpi, devc);
return TRUE;
}
if (is_highres_enabled(devc))
request_hires(scpi, devc);
else
request_range(scpi, devc);
break;
case ACQ_REQUESTED_HIRES:
ret = sr_scpi_get_double(scpi, NULL, &devc->hires_register);
if (ret != SR_OK) {
retrigger_measurement(scpi, devc);
return TRUE;
}
request_range(scpi, devc);
break;
case ACQ_REQUESTED_RANGE:
ret = sr_scpi_get_double(scpi, NULL, &devc->measurement_range);
if (ret != SR_OK) {
retrigger_measurement(scpi, devc);
return TRUE;
}
devc->acq_state = ACQ_GOT_MEASUREMENT;
break;
default:
return FALSE;
}
if (devc->acq_state == ACQ_GOT_MEASUREMENT)
acq_send_measurement(sdi);
if (devc->limit_samples && (devc->num_samples >= devc->limit_samples)) {
sdi->driver->dev_acquisition_stop(sdi, cb_data);
return FALSE;
}
/* Got more to go. */
if (devc->acq_state == ACQ_GOT_MEASUREMENT) {
/* Retrigger */
devc->num_samples++;
retrigger_measurement(scpi, devc);
}
return TRUE;

View File

@ -21,24 +21,56 @@
#define LIBSIGROK_HARDWARE_HP_3457A_PROTOCOL_H
#include <stdint.h>
#include <glib.h>
#include <libsigrok/libsigrok.h>
#include "libsigrok-internal.h"
#define LOG_PREFIX "hp-3457a"
/* Information about the rear card option currently installed. */
enum card_type {
CARD_UNKNOWN,
REAR_TERMINALS,
HP_44491A,
HP_44492A,
};
struct rear_card_info {
unsigned int card_id;
enum card_type type;
const char *name;
const char *cg_name;
};
/* Possible states in an acquisition. */
enum acquisition_state {
ACQ_TRIGGERED_MEASUREMENT,
ACQ_REQUESTED_HIRES,
ACQ_REQUESTED_RANGE,
ACQ_GOT_MEASUREMENT,
};
/** Private, per-device-instance driver context. */
struct dev_context {
/* Model-specific information */
/* Information about rear card option, or NULL if unknown */
const struct rear_card_info *rear_card;
/* Acquisition settings */
enum sr_mq measurement_mq;
enum sr_unit measurement_unit;
uint64_t limit_samples;
float nplc;
/* Operational state */
/* Temporary state across callbacks */
enum acquisition_state acq_state;
uint64_t num_samples;
double base_measurement;
double hires_register;
double measurement_range;
};
SR_PRIV const struct rear_card_info *hp_3457a_probe_rear_card(struct sr_scpi_dev_inst *scpi);
SR_PRIV int hp_3457a_receive_data(int fd, int revents, void *cb_data);
SR_PRIV int hp_3457a_set_mq(const struct sr_dev_inst *sdi, enum sr_mq mq);
SR_PRIV int hp_3457a_set_nplc(const struct sr_dev_inst *sdi, float nplc);
#endif