X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;ds=inline;f=src%2Fhardware%2Fhp-3478a%2Fprotocol.c;h=6767bf0a03b4e7ff8b1b4b260c05a3fbe7abdc16;hb=ccf68765aaf76e1ef89b8c81bf3a8d4a8161488f;hp=2871db767c38cf806a688b428df106fa2745445a;hpb=6ddedf5bacd642f6450df15e961147919687a47d;p=libsigrok.git diff --git a/src/hardware/hp-3478a/protocol.c b/src/hardware/hp-3478a/protocol.c index 2871db76..6767bf0a 100644 --- a/src/hardware/hp-3478a/protocol.c +++ b/src/hardware/hp-3478a/protocol.c @@ -38,46 +38,27 @@ static const struct { static int set_mq_volt(struct sr_scpi_dev_inst *scpi, enum sr_mqflag flags) { - const char *cmd; - if ((flags & SR_MQFLAG_AC) != SR_MQFLAG_AC && (flags & SR_MQFLAG_DC) != SR_MQFLAG_DC) return SR_ERR_NA; - if ((flags & SR_MQFLAG_AC) == SR_MQFLAG_AC) - cmd = "F2"; - else - cmd = "F1"; - - return sr_scpi_send(scpi, "%s", cmd); + return sr_scpi_send(scpi, "%s", + ((flags & SR_MQFLAG_AC) == SR_MQFLAG_AC) ? "F2" : "F1"); } static int set_mq_amp(struct sr_scpi_dev_inst *scpi, enum sr_mqflag flags) { - const char *cmd; - if ((flags & SR_MQFLAG_AC) != SR_MQFLAG_AC && (flags & SR_MQFLAG_DC) != SR_MQFLAG_DC) return SR_ERR_NA; - if (flags & SR_MQFLAG_AC) - cmd = "F6"; - else - cmd = "F5"; - - return sr_scpi_send(scpi, "%s", cmd); + return sr_scpi_send(scpi, "%s", (flags & SR_MQFLAG_AC) ? "F6" : "F5"); } static int set_mq_ohm(struct sr_scpi_dev_inst *scpi, enum sr_mqflag flags) { - const char *cmd; - - if (flags & SR_MQFLAG_FOUR_WIRE) - cmd = "F4"; - else - cmd = "F3"; - - return sr_scpi_send(scpi, "%s", cmd); + return sr_scpi_send(scpi, "%s", + (flags & SR_MQFLAG_FOUR_WIRE) ? "F4" : "F3"); } SR_PRIV int hp_3478a_set_mq(const struct sr_dev_inst *sdi, enum sr_mq mq, @@ -110,81 +91,86 @@ SR_PRIV int hp_3478a_set_mq(const struct sr_dev_inst *sdi, enum sr_mq mq, 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->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->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->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->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->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->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->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->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->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->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->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->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->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->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->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->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->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->enc_digits = devc->spec_digits - 2; - } else { + else return SR_ERR_DATA; - } return SR_OK; } static int parse_function_byte(struct dev_context *devc, uint8_t function_byte) { - devc->measurement_mq_flags = 0; + /* Digits / Resolution (spec_digits must be set before range parsing) */ + if ((function_byte & SB1_DIGITS_BLOCK) == DIGITS_5_5) + devc->spec_digits = 6; + else if ((function_byte & SB1_DIGITS_BLOCK) == DIGITS_4_5) + devc->spec_digits = 5; + else if ((function_byte & SB1_DIGITS_BLOCK) == DIGITS_3_5) + devc->spec_digits = 4; + else + return SR_ERR_DATA; /* Function + Range */ + devc->measurement_mq_flags = 0; if ((function_byte & SB1_FUNCTION_BLOCK) == FUNCTION_VDC) { devc->measurement_mq = SR_MQ_VOLTAGE; devc->measurement_mq_flags |= SR_MQFLAG_DC; @@ -220,15 +206,6 @@ static int parse_function_byte(struct dev_context *devc, uint8_t function_byte) parse_range_ohm(devc, function_byte); } - /* Digits / Resolution */ - if ((function_byte & SB1_DIGITS_BLOCK) == DIGITS_5_5) { - devc->spec_digits = 5; - } else if ((function_byte & SB1_DIGITS_BLOCK) == DIGITS_4_5) { - devc->spec_digits = 4; - } else if ((function_byte & SB1_DIGITS_BLOCK) == DIGITS_3_5) { - devc->spec_digits = 3; - } - return SR_OK; } @@ -429,6 +406,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; @@ -439,18 +417,28 @@ 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; /* - * 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);