/*
* This file is part of the libsigrok project.
*
- * Copyright (C) 2017-2018 Frank Stettner <frank-stettner@gmx.net>
+ * Copyright (C) 2017-2021 Frank Stettner <frank-stettner@gmx.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
struct sr_scpi_dev_inst *scpi = sdi->conn;
struct dev_context *devc = sdi->priv;
- /* No need to send command if we're not changing measurement type. */
- if (devc->measurement_mq == mq &&
- ((devc->measurement_mq_flags & mq_flags) == mq_flags))
+ /* No need to send a command if we're not changing the measurement type. */
+ if (devc->measurement_mq == mq && devc->measurement_mq_flag == mq_flags)
return SR_OK;
for (i = 0; i < ARRAY_SIZE(sr_mq_to_cmd_map); i++) {
return SR_ERR_NA;
}
+SR_PRIV int hp_3478a_set_range(const struct sr_dev_inst *sdi, int range_exp)
+{
+ int ret;
+ struct sr_scpi_dev_inst *scpi = sdi->conn;
+ struct dev_context *devc = sdi->priv;
+
+ /* No need to send command if we're not changing the range. */
+ if (devc->range_exp == range_exp)
+ return SR_OK;
+
+ /* -99 is a dummy exponent for auto ranging. */
+ if (range_exp == -99)
+ ret = sr_scpi_send(scpi, "RA");
+ else
+ ret = sr_scpi_send(scpi, "R%i", range_exp);
+ if (ret != SR_OK)
+ return ret;
+
+ return hp_3478a_get_status_bytes(sdi);
+}
+
+SR_PRIV int hp_3478a_set_digits(const struct sr_dev_inst *sdi, uint8_t digits)
+{
+ int ret;
+ struct sr_scpi_dev_inst *scpi = sdi->conn;
+ struct dev_context *devc = sdi->priv;
+
+ /* No need to send command if we're not changing the range. */
+ if (devc->spec_digits == digits)
+ return SR_OK;
+
+ /* digits are based on devc->spec_digits, so we have to substract 1 */
+ ret = sr_scpi_send(scpi, "N%i", digits-1);
+ if (ret != SR_OK)
+ return ret;
+
+ return hp_3478a_get_status_bytes(sdi);
+}
+
static int parse_range_vdc(struct dev_context *devc, uint8_t range_byte)
{
- if ((range_byte & SB1_RANGE_BLOCK) == RANGE_VDC_30MV)
+ if ((range_byte & SB1_RANGE_BLOCK) == RANGE_VDC_30MV) {
+ devc->range_exp = -2;
devc->enc_digits = devc->spec_digits - 2;
- else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_VDC_300MV)
+ } else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_VDC_300MV) {
+ devc->range_exp = -1;
devc->enc_digits = devc->spec_digits - 3;
- else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_VDC_3V)
+ } else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_VDC_3V) {
+ devc->range_exp = 0;
devc->enc_digits = devc->spec_digits - 1;
- else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_VDC_30V)
+ } else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_VDC_30V) {
+ devc->range_exp = 1;
devc->enc_digits = devc->spec_digits - 2;
- else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_VDC_300V)
+ } else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_VDC_300V) {
+ devc->range_exp = 2;
devc->enc_digits = devc->spec_digits - 3;
- else
+ } else
return SR_ERR_DATA;
return SR_OK;
static int parse_range_vac(struct dev_context *devc, uint8_t range_byte)
{
- if ((range_byte & SB1_RANGE_BLOCK) == RANGE_VAC_300MV)
+ if ((range_byte & SB1_RANGE_BLOCK) == RANGE_VAC_300MV) {
+ devc->range_exp = -1;
devc->enc_digits = devc->spec_digits - 3;
- else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_VAC_3V)
+ } else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_VAC_3V) {
+ devc->range_exp = 0;
devc->enc_digits = devc->spec_digits - 1;
- else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_VAC_30V)
+ } else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_VAC_30V) {
+ devc->range_exp = 1;
devc->enc_digits = devc->spec_digits - 2;
- else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_VAC_300V)
+ } else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_VAC_300V) {
+ devc->range_exp = 2;
devc->enc_digits = devc->spec_digits - 3;
- else
+ } else
return SR_ERR_DATA;
return SR_OK;
static int parse_range_a(struct dev_context *devc, uint8_t range_byte)
{
- if ((range_byte & SB1_RANGE_BLOCK) == RANGE_A_300MA)
+ if ((range_byte & SB1_RANGE_BLOCK) == RANGE_A_300MA) {
+ devc->range_exp = -1;
devc->enc_digits = devc->spec_digits - 3;
- else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_A_3A)
+ } else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_A_3A) {
+ devc->range_exp = 0;
devc->enc_digits = devc->spec_digits - 1;
- else
+ } else
return SR_ERR_DATA;
return SR_OK;
static int parse_range_ohm(struct dev_context *devc, uint8_t range_byte)
{
- if ((range_byte & SB1_RANGE_BLOCK) == RANGE_OHM_30R)
+ if ((range_byte & SB1_RANGE_BLOCK) == RANGE_OHM_30R) {
+ devc->range_exp = 1;
devc->enc_digits = devc->spec_digits - 2;
- else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_OHM_300R)
+ } else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_OHM_300R) {
+ devc->range_exp = 2;
devc->enc_digits = devc->spec_digits - 3;
- else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_OHM_3KR)
+ } else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_OHM_3KR) {
+ devc->range_exp = 3;
devc->enc_digits = devc->spec_digits - 1;
- else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_OHM_30KR)
+ } else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_OHM_30KR) {
+ devc->range_exp = 4;
devc->enc_digits = devc->spec_digits - 2;
- else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_OHM_300KR)
+ } else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_OHM_300KR) {
+ devc->range_exp = 5;
devc->enc_digits = devc->spec_digits - 3;
- else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_OHM_3MR)
+ } else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_OHM_3MR) {
+ devc->range_exp = 6;
devc->enc_digits = devc->spec_digits - 1;
- else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_OHM_30MR)
+ } else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_OHM_30MR) {
+ devc->range_exp = 7;
devc->enc_digits = devc->spec_digits - 2;
- else
+ } else
return SR_ERR_DATA;
return SR_OK;
return SR_ERR_DATA;
/* Function + Range */
- devc->measurement_mq_flags = 0;
+ devc->measurement_mq_flag = 0;
+ devc->acquisition_mq_flags = 0;
if ((function_byte & SB1_FUNCTION_BLOCK) == FUNCTION_VDC) {
devc->measurement_mq = SR_MQ_VOLTAGE;
- devc->measurement_mq_flags |= SR_MQFLAG_DC;
+ devc->measurement_mq_flag = SR_MQFLAG_DC;
+ devc->acquisition_mq_flags |= SR_MQFLAG_DC;
devc->measurement_unit = SR_UNIT_VOLT;
parse_range_vdc(devc, function_byte);
} else if ((function_byte & SB1_FUNCTION_BLOCK) == FUNCTION_VAC) {
devc->measurement_mq = SR_MQ_VOLTAGE;
- devc->measurement_mq_flags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
+ devc->measurement_mq_flag = SR_MQFLAG_AC;
+ devc->acquisition_mq_flags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
devc->measurement_unit = SR_UNIT_VOLT;
parse_range_vac(devc, function_byte);
} else if ((function_byte & SB1_FUNCTION_BLOCK) == FUNCTION_2WR) {
parse_range_ohm(devc, function_byte);
} else if ((function_byte & SB1_FUNCTION_BLOCK) == FUNCTION_4WR) {
devc->measurement_mq = SR_MQ_RESISTANCE;
- devc->measurement_mq_flags |= SR_MQFLAG_FOUR_WIRE;
+ devc->measurement_mq_flag = SR_MQFLAG_FOUR_WIRE;
+ devc->acquisition_mq_flags |= SR_MQFLAG_FOUR_WIRE;
devc->measurement_unit = SR_UNIT_OHM;
parse_range_ohm(devc, function_byte);
} else if ((function_byte & SB1_FUNCTION_BLOCK) == FUNCTION_ADC) {
devc->measurement_mq = SR_MQ_CURRENT;
- devc->measurement_mq_flags |= SR_MQFLAG_DC;
+ devc->measurement_mq_flag = SR_MQFLAG_DC;
+ devc->acquisition_mq_flags |= SR_MQFLAG_DC;
devc->measurement_unit = SR_UNIT_AMPERE;
parse_range_a(devc, function_byte);
} else if ((function_byte & SB1_FUNCTION_BLOCK) == FUNCTION_AAC) {
devc->measurement_mq = SR_MQ_CURRENT;
- devc->measurement_mq_flags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
+ devc->measurement_mq_flag = SR_MQFLAG_AC;
+ devc->acquisition_mq_flags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
devc->measurement_unit = SR_UNIT_AMPERE;
parse_range_a(devc, function_byte);
} else if ((function_byte & SB1_FUNCTION_BLOCK) == FUNCTION_EXR) {
devc->auto_zero = FALSE;
/* Auto-Range */
- if ((status_byte & STATUS_AUTO_RANGE) == STATUS_AUTO_RANGE)
- devc->measurement_mq_flags |= SR_MQFLAG_AUTORANGE;
- else
- devc->measurement_mq_flags &= ~SR_MQFLAG_AUTORANGE;
+ if ((status_byte & STATUS_AUTO_RANGE) == STATUS_AUTO_RANGE) {
+ devc->acquisition_mq_flags |= SR_MQFLAG_AUTORANGE;
+ devc->range_exp = -99;
+ } else
+ devc->acquisition_mq_flags &= ~SR_MQFLAG_AUTORANGE;
/* Internal trigger */
if ((status_byte & STATUS_INT_TRIGGER) == STATUS_INT_TRIGGER)
encoding.digits = devc->enc_digits;
meaning.mq = devc->measurement_mq;
- meaning.mqflags = devc->measurement_mq_flags;
+ meaning.mqflags = devc->acquisition_mq_flags;
meaning.unit = devc->measurement_unit;
meaning.channels = sdi->channels;
struct sr_scpi_dev_inst *scpi;
struct sr_dev_inst *sdi;
struct dev_context *devc;
+ char status_register;
(void)fd;
(void)revents;
scpi = sdi->conn;
/*
- * This is necessary to get the actual range for the encoding digits.
- * When SPoll is implemmented, this can be done via SPoll.
+ * TODO: Wait for SRQ from the DMM when a new measurement is available.
+ * For now, we don't wait for a SRQ, but just do a SPoll and
+ * check the Data Ready bit (0x01).
+ * This is necessary, because (1) reading a value will block the
+ * bus until a measurement is available and (2) when switching
+ * ranges, there could be a timeout.
*/
- if (hp_3478a_get_status_bytes(sdi) != SR_OK)
+ if (sr_scpi_gpib_spoll(scpi, &status_register) != SR_OK)
+ return FALSE;
+ if (!(((uint8_t)status_register) & 0x01))
+ return TRUE;
+
+ /* Get a reading from the DMM. */
+ if (sr_scpi_get_double(scpi, NULL, &devc->measurement) != SR_OK)
return FALSE;
+ /* Check for overflow. */
+ if (devc->measurement >= 9.998e+9)
+ devc->measurement = INFINITY;
+
/*
- * TODO: Implement GPIB-SPoll, to get notified by a SRQ when a new
- * measurement is available. This is necessary, because when
- * switching ranges, there could be a timeout.
+ * This is necessary to get the actual range for the encoding digits.
+ * Must be called after reading the value, because it resets the
+ * status register!
*/
- if (sr_scpi_get_double(scpi, NULL, &devc->measurement) != SR_OK)
+ if (hp_3478a_get_status_bytes(sdi) != SR_OK)
return FALSE;
acq_send_measurement(sdi);