X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Fhardware%2Fhp-3478a%2Fprotocol.c;h=7842f2f4e25992ca81a9d72e2f6d63a783274256;hb=e4204b1757459a03c0a70849a659f27387edc295;hp=3ddce5884aa1bddd72a199f4823bae3e2cde5f2c;hpb=8686b747cd40695c36f998603f6e853ee5eea883;p=libsigrok.git diff --git a/src/hardware/hp-3478a/protocol.c b/src/hardware/hp-3478a/protocol.c index 3ddce588..7842f2f4 100644 --- a/src/hardware/hp-3478a/protocol.c +++ b/src/hardware/hp-3478a/protocol.c @@ -89,19 +89,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 +153,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 +173,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 +187,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 +228,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 +248,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 +305,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 +456,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 +470,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 +481,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);