X-Git-Url: https://sigrok.org/gitweb/?p=libsigrok.git;a=blobdiff_plain;f=src%2Fhardware%2Fhp-3478a%2Fprotocol.c;h=2e9130b022a9ab01832701ebdae41eb0c1533c47;hp=3ddce5884aa1bddd72a199f4823bae3e2cde5f2c;hb=569165c0e44323ce8a4baf1da4d540e8976e9d33;hpb=94cf02d0c24580322fdf9238a96a563205b943d2 diff --git a/src/hardware/hp-3478a/protocol.c b/src/hardware/hp-3478a/protocol.c index 3ddce588..2e9130b0 100644 --- a/src/hardware/hp-3478a/protocol.c +++ b/src/hardware/hp-3478a/protocol.c @@ -1,7 +1,7 @@ /* * This file is part of the libsigrok project. * - * Copyright (C) 2017-2018 Frank Stettner + * Copyright (C) 2017-2021 Frank Stettner * * 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 @@ -69,9 +69,8 @@ SR_PRIV int hp_3478a_set_mq(const struct sr_dev_inst *sdi, enum sr_mq mq, 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++) { @@ -89,19 +88,63 @@ 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); +} + +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; @@ -109,15 +152,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 +172,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 +186,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; @@ -170,15 +226,18 @@ static int parse_function_byte(struct dev_context *devc, uint8_t function_byte) 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) { @@ -187,17 +246,20 @@ static int parse_function_byte(struct dev_context *devc, uint8_t function_byte) 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) { @@ -242,10 +304,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 +455,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; @@ -406,6 +469,7 @@ SR_PRIV int hp_3478a_receive_data(int fd, int revents, void *cb_data) struct sr_scpi_dev_inst *scpi; struct sr_dev_inst *sdi; struct dev_context *devc; + char status_register; (void)fd; (void)revents; @@ -416,18 +480,32 @@ SR_PRIV int hp_3478a_receive_data(int fd, int revents, void *cb_data) 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);