2 * This file is part of the libsigrok project.
4 * Copyright (C) 2017-2021 Frank Stettner <frank-stettner@gmx.net>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 static int set_mq_volt(struct sr_scpi_dev_inst *scpi, enum sr_mqflag flags);
27 static int set_mq_amp(struct sr_scpi_dev_inst *scpi, enum sr_mqflag flags);
28 static int set_mq_ohm(struct sr_scpi_dev_inst *scpi, enum sr_mqflag flags);
32 int (*set_mode)(struct sr_scpi_dev_inst *scpi, enum sr_mqflag flags);
33 } sr_mq_to_cmd_map[] = {
34 { SR_MQ_VOLTAGE, set_mq_volt },
35 { SR_MQ_CURRENT, set_mq_amp },
36 { SR_MQ_RESISTANCE, set_mq_ohm },
39 static int set_mq_volt(struct sr_scpi_dev_inst *scpi, enum sr_mqflag flags)
41 if ((flags & SR_MQFLAG_AC) != SR_MQFLAG_AC &&
42 (flags & SR_MQFLAG_DC) != SR_MQFLAG_DC)
45 return sr_scpi_send(scpi, "%s",
46 ((flags & SR_MQFLAG_AC) == SR_MQFLAG_AC) ? "F2" : "F1");
49 static int set_mq_amp(struct sr_scpi_dev_inst *scpi, enum sr_mqflag flags)
51 if ((flags & SR_MQFLAG_AC) != SR_MQFLAG_AC &&
52 (flags & SR_MQFLAG_DC) != SR_MQFLAG_DC)
55 return sr_scpi_send(scpi, "%s", (flags & SR_MQFLAG_AC) ? "F6" : "F5");
58 static int set_mq_ohm(struct sr_scpi_dev_inst *scpi, enum sr_mqflag flags)
60 return sr_scpi_send(scpi, "%s",
61 (flags & SR_MQFLAG_FOUR_WIRE) ? "F4" : "F3");
64 SR_PRIV int hp_3478a_set_mq(const struct sr_dev_inst *sdi, enum sr_mq mq,
65 enum sr_mqflag mq_flags)
69 struct sr_scpi_dev_inst *scpi = sdi->conn;
70 struct dev_context *devc = sdi->priv;
72 /* No need to send a command if we're not changing the measurement type. */
73 if (devc->measurement_mq == mq && devc->measurement_mq_flag == mq_flags)
76 for (i = 0; i < ARRAY_SIZE(sr_mq_to_cmd_map); i++) {
77 if (sr_mq_to_cmd_map[i].mq != mq)
80 ret = sr_mq_to_cmd_map[i].set_mode(scpi, mq_flags);
84 ret = hp_3478a_get_status_bytes(sdi);
91 SR_PRIV int hp_3478a_set_range(const struct sr_dev_inst *sdi, int range_exp)
94 struct sr_scpi_dev_inst *scpi = sdi->conn;
95 struct dev_context *devc = sdi->priv;
97 /* No need to send command if we're not changing the range. */
98 if (devc->range_exp == range_exp)
101 /* -99 is a dummy exponent for auto ranging. */
102 if (range_exp == -99)
103 ret = sr_scpi_send(scpi, "RA");
105 ret = sr_scpi_send(scpi, "R%i", range_exp);
109 return hp_3478a_get_status_bytes(sdi);
112 SR_PRIV int hp_3478a_set_digits(const struct sr_dev_inst *sdi, uint8_t digits)
115 struct sr_scpi_dev_inst *scpi = sdi->conn;
116 struct dev_context *devc = sdi->priv;
118 /* No need to send command if we're not changing the resolution. */
119 if (devc->digits == digits)
122 /* digits are the total number of digits, so we have to substract 1 */
123 ret = sr_scpi_send(scpi, "N%i", digits-1);
127 return hp_3478a_get_status_bytes(sdi);
130 static int parse_range_vdc(struct dev_context *devc, uint8_t range_byte)
132 if ((range_byte & SB1_RANGE_BLOCK) == RANGE_VDC_30MV) {
133 devc->range_exp = -2;
134 devc->sr_digits = devc->digits + 1;
135 } else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_VDC_300MV) {
136 devc->range_exp = -1;
137 devc->sr_digits = devc->digits;
138 } else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_VDC_3V) {
140 devc->sr_digits = devc->digits - 1;
141 } else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_VDC_30V) {
143 devc->sr_digits = devc->digits - 2;
144 } else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_VDC_300V) {
146 devc->sr_digits = devc->digits - 3;
153 static int parse_range_vac(struct dev_context *devc, uint8_t range_byte)
155 if ((range_byte & SB1_RANGE_BLOCK) == RANGE_VAC_300MV) {
156 devc->range_exp = -1;
157 devc->sr_digits = devc->digits;
158 } else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_VAC_3V) {
160 devc->sr_digits = devc->digits - 1;
161 } else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_VAC_30V) {
163 devc->sr_digits = devc->digits - 2;
164 } else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_VAC_300V) {
166 devc->sr_digits = devc->digits - 3;
173 static int parse_range_a(struct dev_context *devc, uint8_t range_byte)
175 if ((range_byte & SB1_RANGE_BLOCK) == RANGE_A_300MA) {
176 devc->range_exp = -1;
177 devc->sr_digits = devc->digits;
178 } else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_A_3A) {
180 devc->sr_digits = devc->digits - 1;
187 static int parse_range_ohm(struct dev_context *devc, uint8_t range_byte)
189 if ((range_byte & SB1_RANGE_BLOCK) == RANGE_OHM_30R) {
191 devc->sr_digits = devc->digits - 2;
192 } else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_OHM_300R) {
194 devc->sr_digits = devc->digits - 3;
195 } else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_OHM_3KR) {
197 devc->sr_digits = devc->digits - 4;
198 } else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_OHM_30KR) {
200 devc->sr_digits = devc->digits - 5;
201 } else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_OHM_300KR) {
203 devc->sr_digits = devc->digits - 6;
204 } else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_OHM_3MR) {
206 devc->sr_digits = devc->digits - 7;
207 } else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_OHM_30MR) {
209 devc->sr_digits = devc->digits - 8;
216 static int parse_function_byte(struct dev_context *devc, uint8_t function_byte)
218 /* Digits / Resolution (digits must be set before range parsing) */
219 if ((function_byte & SB1_DIGITS_BLOCK) == DIGITS_5_5)
221 else if ((function_byte & SB1_DIGITS_BLOCK) == DIGITS_4_5)
223 else if ((function_byte & SB1_DIGITS_BLOCK) == DIGITS_3_5)
228 /* Function + Range */
229 devc->measurement_mq_flag = 0;
230 devc->acquisition_mq_flags = 0;
231 if ((function_byte & SB1_FUNCTION_BLOCK) == FUNCTION_VDC) {
232 devc->measurement_mq = SR_MQ_VOLTAGE;
233 devc->measurement_mq_flag = SR_MQFLAG_DC;
234 devc->acquisition_mq_flags |= SR_MQFLAG_DC;
235 devc->measurement_unit = SR_UNIT_VOLT;
236 parse_range_vdc(devc, function_byte);
237 } else if ((function_byte & SB1_FUNCTION_BLOCK) == FUNCTION_VAC) {
238 devc->measurement_mq = SR_MQ_VOLTAGE;
239 devc->measurement_mq_flag = SR_MQFLAG_AC;
240 devc->acquisition_mq_flags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
241 devc->measurement_unit = SR_UNIT_VOLT;
242 parse_range_vac(devc, function_byte);
243 } else if ((function_byte & SB1_FUNCTION_BLOCK) == FUNCTION_2WR) {
244 devc->measurement_mq = SR_MQ_RESISTANCE;
245 devc->measurement_unit = SR_UNIT_OHM;
246 parse_range_ohm(devc, function_byte);
247 } else if ((function_byte & SB1_FUNCTION_BLOCK) == FUNCTION_4WR) {
248 devc->measurement_mq = SR_MQ_RESISTANCE;
249 devc->measurement_mq_flag = SR_MQFLAG_FOUR_WIRE;
250 devc->acquisition_mq_flags |= SR_MQFLAG_FOUR_WIRE;
251 devc->measurement_unit = SR_UNIT_OHM;
252 parse_range_ohm(devc, function_byte);
253 } else if ((function_byte & SB1_FUNCTION_BLOCK) == FUNCTION_ADC) {
254 devc->measurement_mq = SR_MQ_CURRENT;
255 devc->measurement_mq_flag = SR_MQFLAG_DC;
256 devc->acquisition_mq_flags |= SR_MQFLAG_DC;
257 devc->measurement_unit = SR_UNIT_AMPERE;
258 parse_range_a(devc, function_byte);
259 } else if ((function_byte & SB1_FUNCTION_BLOCK) == FUNCTION_AAC) {
260 devc->measurement_mq = SR_MQ_CURRENT;
261 devc->measurement_mq_flag = SR_MQFLAG_AC;
262 devc->acquisition_mq_flags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
263 devc->measurement_unit = SR_UNIT_AMPERE;
264 parse_range_a(devc, function_byte);
265 } else if ((function_byte & SB1_FUNCTION_BLOCK) == FUNCTION_EXR) {
266 devc->measurement_mq = SR_MQ_RESISTANCE;
267 devc->measurement_unit = SR_UNIT_OHM;
268 parse_range_ohm(devc, function_byte);
274 static int parse_status_byte(struct dev_context *devc, uint8_t status_byte)
276 devc->trigger = TRIGGER_UNDEFINED;
278 /* External Trigger */
279 if ((status_byte & STATUS_EXT_TRIGGER) == STATUS_EXT_TRIGGER)
280 devc->trigger = TRIGGER_EXTERNAL;
283 if ((status_byte & STATUS_CAL_RAM) == STATUS_CAL_RAM)
284 devc->calibration = TRUE;
286 devc->calibration = FALSE;
288 /* Front/Rear terminals */
289 if ((status_byte & STATUS_FRONT_TERMINAL) == STATUS_FRONT_TERMINAL)
290 devc->terminal = TERMINAL_FRONT;
292 devc->terminal = TERMINAL_REAR;
295 if ((status_byte & STATUS_50HZ) == STATUS_50HZ)
296 devc->line = LINE_50HZ;
298 devc->line = LINE_60HZ;
301 if ((status_byte & STATUS_AUTO_ZERO) == STATUS_AUTO_ZERO)
302 devc->auto_zero = TRUE;
304 devc->auto_zero = FALSE;
307 if ((status_byte & STATUS_AUTO_RANGE) == STATUS_AUTO_RANGE) {
308 devc->acquisition_mq_flags |= SR_MQFLAG_AUTORANGE;
309 devc->range_exp = -99;
311 devc->acquisition_mq_flags &= ~SR_MQFLAG_AUTORANGE;
313 /* Internal trigger */
314 if ((status_byte & STATUS_INT_TRIGGER) == STATUS_INT_TRIGGER)
315 devc->trigger = TRIGGER_INTERNAL;
320 static int parse_srq_byte(uint8_t sqr_byte)
325 /* The ServiceReQuest register isn't used at the moment. */
328 if ((sqr_byte & SRQ_POWER_ON) == SRQ_POWER_ON)
329 sr_spew("Power On SRQ or clear msg received");
332 if ((sqr_byte & SRQ_CAL_FAILED) == SRQ_CAL_FAILED)
333 sr_spew("CAL failed SRQ");
336 if ((sqr_byte & SRQ_KEYBORD) == SRQ_KEYBORD)
337 sr_spew("Keyboard SRQ");
339 /* Hardware error SRQ */
340 if ((sqr_byte & SRQ_HARDWARE_ERR) == SRQ_HARDWARE_ERR)
341 sr_spew("Hardware error SRQ");
343 /* Syntax error SRQ */
344 if ((sqr_byte & SRQ_SYNTAX_ERR) == SRQ_SYNTAX_ERR)
345 sr_spew("Syntax error SRQ");
347 /* Every reading is available to the bus SRQ */
348 if ((sqr_byte & SRQ_BUS_AVAIL) == SRQ_BUS_AVAIL)
349 sr_spew("Every reading is available to the bus SRQ");
355 static int parse_error_byte(uint8_t error_byte)
362 if ((error_byte & ERROR_AD_LINK) == ERROR_AD_LINK) {
363 sr_err("Failure in the A/D link");
368 if ((error_byte & ERROR_AD_SELF_TEST) == ERROR_AD_SELF_TEST) {
369 sr_err("A/D has failed its internal Self Test");
373 /* A/D slope error */
374 if ((error_byte & ERROR_AD_SLOPE) == ERROR_AD_SLOPE) {
375 sr_err("There has been an A/D slope error");
380 if ((error_byte & ERROR_ROM_SELF_TEST) == ERROR_ROM_SELF_TEST) {
381 sr_err("The ROM Self Test has failed");
386 if ((error_byte & ERROR_RAM_SELF_TEST) == ERROR_RAM_SELF_TEST) {
387 sr_err("The RAM Self Test has failed");
392 if ((error_byte & ERROR_SELF_TEST) == ERROR_SELF_TEST) {
393 sr_err("Self Test: Any of the CAL RAM locations have bad "
394 "checksums, or a range with a bad checksum is selected");
401 SR_PRIV int hp_3478a_get_status_bytes(const struct sr_dev_inst *sdi)
405 uint8_t function_byte, status_byte, srq_byte, error_byte;
406 struct sr_scpi_dev_inst *scpi = sdi->conn;
407 struct dev_context *devc = sdi->priv;
409 ret = sr_scpi_get_string(scpi, "B", &response);
416 function_byte = (uint8_t)response[0];
417 status_byte = (uint8_t)response[1];
418 srq_byte = (uint8_t)response[2];
419 error_byte = (uint8_t)response[3];
423 parse_function_byte(devc, function_byte);
424 parse_status_byte(devc, status_byte);
425 parse_srq_byte(srq_byte);
426 ret = parse_error_byte(error_byte);
431 static void acq_send_measurement(struct sr_dev_inst *sdi)
433 struct sr_datafeed_packet packet;
434 struct sr_datafeed_analog analog;
435 struct sr_analog_encoding encoding;
436 struct sr_analog_meaning meaning;
437 struct sr_analog_spec spec;
438 struct dev_context *devc;
443 packet.type = SR_DF_ANALOG;
444 packet.payload = &analog;
446 sr_analog_init(&analog, &encoding, &meaning, &spec, devc->sr_digits);
448 /* TODO: Implement NAN, depending on counts, range and value. */
449 f = devc->measurement;
450 analog.num_samples = 1;
453 encoding.unitsize = sizeof(float);
454 encoding.is_float = TRUE;
455 encoding.digits = devc->sr_digits;
457 meaning.mq = devc->measurement_mq;
458 meaning.mqflags = devc->acquisition_mq_flags;
459 meaning.unit = devc->measurement_unit;
460 meaning.channels = sdi->channels;
462 spec.spec_digits = devc->sr_digits;
464 sr_session_send(sdi, &packet);
467 SR_PRIV int hp_3478a_receive_data(int fd, int revents, void *cb_data)
469 struct sr_scpi_dev_inst *scpi;
470 struct sr_dev_inst *sdi;
471 struct dev_context *devc;
472 char status_register;
477 if (!(sdi = cb_data) || !(devc = sdi->priv))
483 * TODO: Wait for SRQ from the DMM when a new measurement is available.
484 * For now, we don't wait for a SRQ, but just do a SPoll and
485 * check the Data Ready bit (0x01).
486 * This is necessary, because (1) reading a value will block the
487 * bus until a measurement is available and (2) when switching
488 * ranges, there could be a timeout.
490 if (sr_scpi_gpib_spoll(scpi, &status_register) != SR_OK)
492 if (!(((uint8_t)status_register) & SRQ_BUS_AVAIL))
495 /* Get a reading from the DMM. */
496 if (sr_scpi_get_double(scpi, NULL, &devc->measurement) != SR_OK)
499 /* Check for overflow. */
500 if (devc->measurement >= 9.998e+9)
501 devc->measurement = INFINITY;
504 * This is necessary to get the actual range for the encoding digits.
505 * Must be called after reading the value, because it resets the
508 if (hp_3478a_get_status_bytes(sdi) != SR_OK)
511 acq_send_measurement(sdi);
512 sr_sw_limits_update_samples_read(&devc->limits, 1);
514 if (sr_sw_limits_check(&devc->limits))
515 sr_dev_acquisition_stop(sdi);