From: Frank Stettner Date: Fri, 13 Dec 2019 17:23:09 +0000 (+0100) Subject: hp-3478a: Add get/set/list of measurement ranges. X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=e5137b9343563b27c8ba791360aa408264482153;p=libsigrok.git hp-3478a: Add get/set/list of measurement ranges. --- diff --git a/src/hardware/hp-3478a/api.c b/src/hardware/hp-3478a/api.c index 5174ee14..d4e40338 100644 --- a/src/hardware/hp-3478a/api.c +++ b/src/hardware/hp-3478a/api.c @@ -34,6 +34,7 @@ static const uint32_t devopts[] = { SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET, SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET, SR_CONF_MEASURED_QUANTITY | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, + SR_CONF_RANGE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, }; static const struct { @@ -41,17 +42,59 @@ static const struct { enum sr_mqflag mqflag; } mqopts[] = { {SR_MQ_VOLTAGE, SR_MQFLAG_DC}, - {SR_MQ_VOLTAGE, SR_MQFLAG_DC | SR_MQFLAG_AUTORANGE}, - {SR_MQ_VOLTAGE, SR_MQFLAG_AC | SR_MQFLAG_RMS}, - {SR_MQ_VOLTAGE, SR_MQFLAG_AC | SR_MQFLAG_RMS | SR_MQFLAG_AUTORANGE}, + {SR_MQ_VOLTAGE, SR_MQFLAG_AC}, {SR_MQ_CURRENT, SR_MQFLAG_DC}, - {SR_MQ_CURRENT, SR_MQFLAG_DC | SR_MQFLAG_AUTORANGE}, - {SR_MQ_CURRENT, SR_MQFLAG_AC | SR_MQFLAG_RMS}, - {SR_MQ_CURRENT, SR_MQFLAG_AC | SR_MQFLAG_RMS | SR_MQFLAG_AUTORANGE}, + {SR_MQ_CURRENT, SR_MQFLAG_AC}, {SR_MQ_RESISTANCE, 0}, - {SR_MQ_RESISTANCE, 0 | SR_MQFLAG_AUTORANGE}, {SR_MQ_RESISTANCE, SR_MQFLAG_FOUR_WIRE}, - {SR_MQ_RESISTANCE, SR_MQFLAG_FOUR_WIRE | SR_MQFLAG_AUTORANGE}, +}; + + +static const struct { + enum sr_mq mq; + enum sr_mqflag mqflag; + int range_exp; + const char *range_str; +} rangeopts[] = { + /* -99 is a dummy exponent for auto ranging. */ + {SR_MQ_VOLTAGE, SR_MQFLAG_DC, -99, "Auto"}, + {SR_MQ_VOLTAGE, SR_MQFLAG_DC, -2, "30mV"}, + {SR_MQ_VOLTAGE, SR_MQFLAG_DC, -1, "300mV"}, + {SR_MQ_VOLTAGE, SR_MQFLAG_DC, 0, "3V"}, + {SR_MQ_VOLTAGE, SR_MQFLAG_DC, 1, "30V"}, + {SR_MQ_VOLTAGE, SR_MQFLAG_DC, 2, "300V"}, + /* -99 is a dummy exponent for auto ranging. */ + {SR_MQ_VOLTAGE, SR_MQFLAG_AC, -99, "Auto"}, + {SR_MQ_VOLTAGE, SR_MQFLAG_AC, -1, "300mV"}, + {SR_MQ_VOLTAGE, SR_MQFLAG_AC, 0, "3V"}, + {SR_MQ_VOLTAGE, SR_MQFLAG_AC, 1, "30V"}, + {SR_MQ_VOLTAGE, SR_MQFLAG_AC, 2, "300V"}, + /* -99 is a dummy exponent for auto ranging. */ + {SR_MQ_CURRENT, SR_MQFLAG_DC, -99, "Auto"}, + {SR_MQ_CURRENT, SR_MQFLAG_DC, -1, "300mV"}, + {SR_MQ_CURRENT, SR_MQFLAG_DC, 0, "3V"}, + /* -99 is a dummy exponent for auto ranging. */ + {SR_MQ_CURRENT, SR_MQFLAG_AC, -99, "Auto"}, + {SR_MQ_CURRENT, SR_MQFLAG_AC, -1, "300mV"}, + {SR_MQ_CURRENT, SR_MQFLAG_AC, 0, "3V"}, + /* -99 is a dummy exponent for auto ranging. */ + {SR_MQ_RESISTANCE, 0, -99, "Auto"}, + {SR_MQ_RESISTANCE, 0, 1, "30"}, + {SR_MQ_RESISTANCE, 0, 2, "300"}, + {SR_MQ_RESISTANCE, 0, 3, "3k"}, + {SR_MQ_RESISTANCE, 0, 4, "30k"}, + {SR_MQ_RESISTANCE, 0, 5, "300k"}, + {SR_MQ_RESISTANCE, 0, 6, "3M"}, + {SR_MQ_RESISTANCE, 0, 7, "30M"}, + /* -99 is a dummy exponent for auto ranging. */ + {SR_MQ_RESISTANCE, SR_MQFLAG_FOUR_WIRE, -99, "Auto"}, + {SR_MQ_RESISTANCE, SR_MQFLAG_FOUR_WIRE, 1, "30R"}, + {SR_MQ_RESISTANCE, SR_MQFLAG_FOUR_WIRE, 2, "300R"}, + {SR_MQ_RESISTANCE, SR_MQFLAG_FOUR_WIRE, 3, "3kR"}, + {SR_MQ_RESISTANCE, SR_MQFLAG_FOUR_WIRE, 4, "30kR"}, + {SR_MQ_RESISTANCE, SR_MQFLAG_FOUR_WIRE, 5, "300kR"}, + {SR_MQ_RESISTANCE, SR_MQFLAG_FOUR_WIRE, 6, "3MR"}, + {SR_MQ_RESISTANCE, SR_MQFLAG_FOUR_WIRE, 7, "30MR"}, }; static struct sr_dev_driver hp_3478a_driver_info; @@ -118,6 +161,8 @@ static int config_get(uint32_t key, GVariant **data, struct dev_context *devc; int ret; GVariant *arr[2]; + unsigned int i; + const char *range_str; (void)cg; @@ -135,6 +180,21 @@ static int config_get(uint32_t key, GVariant **data, arr[1] = g_variant_new_uint64(devc->measurement_mq_flags); *data = g_variant_new_tuple(arr, 2); break; + case SR_CONF_RANGE: + ret = hp_3478a_get_status_bytes(sdi); + if (ret != SR_OK) + return ret; + range_str = "Auto"; + for (i = 0; i < ARRAY_SIZE(rangeopts); i++) { + if (rangeopts[i].mq == devc->measurement_mq && + rangeopts[i].mqflag == devc->measurement_mq_flags && + rangeopts[i].range_exp == devc->range_exp) { + range_str = rangeopts[i].range_str; + break; + } + } + *data = g_variant_new_string(range_str); + break; default: return SR_ERR_NA; } @@ -149,6 +209,8 @@ static int config_set(uint32_t key, GVariant *data, enum sr_mq mq; enum sr_mqflag mq_flags; GVariant *tuple_child; + unsigned int i; + const char *range_str; (void)cg; @@ -165,6 +227,16 @@ static int config_set(uint32_t key, GVariant *data, mq_flags = g_variant_get_uint64(tuple_child); g_variant_unref(tuple_child); return hp_3478a_set_mq(sdi, mq, mq_flags); + case SR_CONF_RANGE: + range_str = g_variant_get_string(data, NULL); + for (i = 0; i < ARRAY_SIZE(rangeopts); i++) { + if (rangeopts[i].mq == devc->measurement_mq && + rangeopts[i].mqflag == devc->measurement_mq_flags && + g_strcmp0(rangeopts[i].range_str, range_str) == 0) { + return hp_3478a_set_range(sdi, rangeopts[i].range_exp); + } + } + return SR_ERR_NA; default: return SR_ERR_NA; } @@ -175,10 +247,14 @@ static int config_set(uint32_t key, GVariant *data, 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; + int ret; unsigned int i; GVariant *gvar, *arr[2]; GVariantBuilder gvb; + devc = sdi->priv; + switch (key) { case SR_CONF_SCAN_OPTIONS: case SR_CONF_DEVICE_OPTIONS: @@ -197,6 +273,19 @@ static int config_list(uint32_t key, GVariant **data, } *data = g_variant_builder_end(&gvb); break; + case SR_CONF_RANGE: + ret = hp_3478a_get_status_bytes(sdi); + if (ret != SR_OK) + return ret; + g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); + for (i = 0; i < ARRAY_SIZE(rangeopts); i++) { + if (rangeopts[i].mq == devc->measurement_mq && + rangeopts[i].mqflag == devc->measurement_mq_flags) { + g_variant_builder_add(&gvb, "s", rangeopts[i].range_str); + } + } + *data = g_variant_builder_end(&gvb); + break; default: return SR_ERR_NA; } diff --git a/src/hardware/hp-3478a/protocol.c b/src/hardware/hp-3478a/protocol.c index 6767bf0a..da200e7a 100644 --- a/src/hardware/hp-3478a/protocol.c +++ b/src/hardware/hp-3478a/protocol.c @@ -89,19 +89,45 @@ SR_PRIV int hp_3478a_set_mq(const struct sr_dev_inst *sdi, enum sr_mq mq, 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); +} + 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; @@ -109,15 +135,19 @@ static int parse_range_vdc(struct dev_context *devc, uint8_t range_byte) 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; @@ -125,11 +155,13 @@ static int parse_range_vac(struct dev_context *devc, uint8_t range_byte) 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; @@ -137,21 +169,28 @@ static int parse_range_a(struct dev_context *devc, uint8_t range_byte) 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; @@ -171,14 +210,17 @@ static int parse_function_byte(struct dev_context *devc, uint8_t function_byte) /* Function + Range */ devc->measurement_mq_flags = 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->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_flags |= 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) { @@ -188,16 +230,19 @@ static int parse_function_byte(struct dev_context *devc, uint8_t 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->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->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_flags |= 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) { @@ -242,10 +287,11 @@ static int parse_status_byte(struct dev_context *devc, uint8_t status_byte) 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) @@ -392,7 +438,7 @@ static void acq_send_measurement(struct sr_dev_inst *sdi) 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; diff --git a/src/hardware/hp-3478a/protocol.h b/src/hardware/hp-3478a/protocol.h index baf622bd..1b7f5cb1 100644 --- a/src/hardware/hp-3478a/protocol.h +++ b/src/hardware/hp-3478a/protocol.h @@ -138,8 +138,12 @@ struct dev_context { double measurement; enum sr_mq measurement_mq; + /** The measurement mq flags only contain flags for AC, DC and 4-wire. */ enum sr_mqflag measurement_mq_flags; + /** The acquisition mq flags also contain flags for autoranging and RMS. */ + enum sr_mqflag acquisition_mq_flags; enum sr_unit measurement_unit; + int range_exp; uint8_t enc_digits; uint8_t spec_digits; @@ -157,6 +161,7 @@ struct channel_context { SR_PRIV int hp_3478a_set_mq(const struct sr_dev_inst *sdi, enum sr_mq mq, enum sr_mqflag mq_flags); +SR_PRIV int hp_3478a_set_range(const struct sr_dev_inst *sdi, int range_exp); SR_PRIV int hp_3478a_get_status_bytes(const struct sr_dev_inst *sdi); SR_PRIV int hp_3478a_receive_data(int fd, int revents, void *cb_data);