2 * This file is part of the libsigrok project.
4 * Copyright (C) 2016 Alexandru Gagniuc <mr.nuke.me@gmail.com>
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/>.
25 static int set_mq_volt(struct sr_scpi_dev_inst *scpi, enum sr_mqflag flags);
26 static int set_mq_amp(struct sr_scpi_dev_inst *scpi, enum sr_mqflag flags);
27 static int set_mq_ohm(struct sr_scpi_dev_inst *scpi, enum sr_mqflag flags);
29 * The source for the frequency measurement can be either AC voltage, AC+DC
30 * voltage, AC current, or AC+DC current. Configuring this is not yet
31 * supported. For details, see "FSOURCE" command.
32 * The set_mode function is optional and can be set to NULL, but in that case
33 * a cmd string must be provided.
39 int (*set_mode)(struct sr_scpi_dev_inst *scpi, enum sr_mqflag flags);
40 } sr_mq_to_cmd_map[] = {
41 { SR_MQ_VOLTAGE, SR_UNIT_VOLT, "DCV", set_mq_volt },
42 { SR_MQ_CURRENT, SR_UNIT_AMPERE, "DCI", set_mq_amp },
43 { SR_MQ_RESISTANCE, SR_UNIT_OHM, "OHM", set_mq_ohm },
44 { SR_MQ_FREQUENCY, SR_UNIT_HERTZ, "FREQ", NULL },
47 static const struct rear_card_info rear_card_parameters[] = {
49 .type = REAR_TERMINALS,
51 .name = "Rear terminals",
57 .name = "44491A Armature Relay Multiplexer",
63 .name = "44492A Reed Relay Multiplexer",
69 static int send_mq_ac_dc(struct sr_scpi_dev_inst *scpi, const char *mode,
72 const char *ac_flag, *dc_flag;
74 if (flags & ~(SR_MQFLAG_AC | SR_MQFLAG_DC))
77 ac_flag = (flags & SR_MQFLAG_AC) ? "AC" : "";
79 /* Must specify DC measurement when AC flag is not given. */
80 if ((flags & SR_MQFLAG_DC) || !(flags & SR_MQFLAG_AC))
83 return sr_scpi_send(scpi, "%s%s%s", ac_flag, dc_flag, mode);
86 static int set_mq_volt(struct sr_scpi_dev_inst *scpi, enum sr_mqflag flags)
88 return send_mq_ac_dc(scpi, "V", flags);
91 static int set_mq_amp(struct sr_scpi_dev_inst *scpi, enum sr_mqflag flags)
93 return send_mq_ac_dc(scpi, "I", flags);
96 static int set_mq_ohm(struct sr_scpi_dev_inst *scpi, enum sr_mqflag flags)
100 if (flags & ~(SR_MQFLAG_FOUR_WIRE))
103 ohm_flag = (flags & SR_MQFLAG_FOUR_WIRE) ? "F" : "";
104 return sr_scpi_send(scpi, "OHM%s", ohm_flag);
107 SR_PRIV int hp_3457a_set_mq(const struct sr_dev_inst *sdi, enum sr_mq mq,
108 enum sr_mqflag mq_flags)
112 struct sr_scpi_dev_inst *scpi = sdi->conn;
113 struct dev_context *devc = sdi->priv;
115 /* No need to send command if we're not changing measurement type. */
116 if (devc->measurement_mq == mq)
119 for (i = 0; i < ARRAY_SIZE(sr_mq_to_cmd_map); i++) {
120 if (sr_mq_to_cmd_map[i].mq != mq)
122 if (sr_mq_to_cmd_map[i].set_mode) {
123 ret = sr_mq_to_cmd_map[i].set_mode(scpi, mq_flags);
125 ret = sr_scpi_send(scpi, sr_mq_to_cmd_map[i].cmd);
128 devc->measurement_mq = sr_mq_to_cmd_map[i].mq;
129 devc->measurement_mq_flags = mq_flags;
130 devc->measurement_unit = sr_mq_to_cmd_map[i].unit;
138 SR_PRIV const struct rear_card_info *hp_3457a_probe_rear_card(struct sr_scpi_dev_inst *scpi)
142 unsigned int card_id;
143 const struct rear_card_info *rear_card = NULL;
145 if (sr_scpi_get_float(scpi, "OPT?", &card_fval) != SR_OK)
148 card_id = (unsigned int)card_fval;
150 for (i = 0; i < ARRAY_SIZE(rear_card_parameters); i++) {
151 if (rear_card_parameters[i].card_id == card_id) {
152 rear_card = rear_card_parameters + i;
160 sr_info("Found %s.", rear_card->name);
165 SR_PRIV int hp_3457a_set_nplc(const struct sr_dev_inst *sdi, float nplc)
168 struct sr_scpi_dev_inst *scpi = sdi->conn;
169 struct dev_context *devc = sdi->priv;
171 if ((nplc < 1E-6) || (nplc > 100))
174 /* Only need one digit of precision here. */
175 ret = sr_scpi_send(scpi, "NPLC %.0E", nplc);
178 * The instrument only has a few valid NPLC setting, so get back the
179 * one which was selected.
181 sr_scpi_get_float(scpi, "NPLC?", &devc->nplc);
186 SR_PRIV int hp_3457a_select_input(const struct sr_dev_inst *sdi,
187 enum channel_conn loc)
190 struct sr_scpi_dev_inst *scpi = sdi->conn;
191 struct dev_context *devc = sdi->priv;
193 if (devc->input_loc == loc)
196 ret = sr_scpi_send(scpi, "TERM %s", (loc == CONN_FRONT) ? "FRONT": "REAR");
198 devc->input_loc = loc;
203 SR_PRIV int hp_3457a_send_scan_list(const struct sr_dev_inst *sdi,
204 unsigned int *channels, size_t len)
207 char chan[16], list_str[64] = "";
209 for (i = 0; i < len; i++) {
210 g_snprintf(chan, sizeof(chan), ",%u", channels[i]);
211 g_strlcat(list_str, chan, sizeof(list_str));
214 return sr_scpi_send(sdi->conn, "SLIST %s", list_str);
217 /* HIRES register only contains valid data with 10 or more powerline cycles. */
218 static int is_highres_enabled(struct dev_context *devc)
220 return (devc->nplc >= 10.0);
223 static void activate_next_channel(struct dev_context *devc)
226 struct sr_channel *chan;
228 list_elem = g_slist_find(devc->active_channels, devc->current_channel);
230 list_elem = list_elem->next;
232 list_elem = devc->active_channels;
234 chan = list_elem->data;
236 devc->current_channel = chan;
239 static void retrigger_measurement(struct sr_scpi_dev_inst *scpi,
240 struct dev_context *devc)
242 sr_scpi_send(scpi, "?");
243 devc->acq_state = ACQ_TRIGGERED_MEASUREMENT;
246 static void request_hires(struct sr_scpi_dev_inst *scpi,
247 struct dev_context *devc)
249 sr_scpi_send(scpi, "RMATH HIRES");
250 devc->acq_state = ACQ_REQUESTED_HIRES;
253 static void request_range(struct sr_scpi_dev_inst *scpi,
254 struct dev_context *devc)
256 sr_scpi_send(scpi, "RANGE?");
257 devc->acq_state = ACQ_REQUESTED_RANGE;
260 static void request_current_channel(struct sr_scpi_dev_inst *scpi,
261 struct dev_context *devc)
263 sr_scpi_send(scpi, "CHAN?");
264 devc->acq_state = ACQ_REQUESTED_CHANNEL_SYNC;
268 * Calculate the number of leading zeroes in the measurement.
270 * Depending on the range and measurement, a reading may not have eight digits
271 * of resolution. For example, on a 30V range:
272 * : 10.000000 V has 8 significant digits
273 * : 9.999999 V has 7 significant digits
274 * : 0.999999 V has 6 significant digits
276 * The number of significant digits is determined based on the range in which
277 * the measurement was taken:
278 * 1. By taking the base 10 logarithm of the range, and converting that to
279 * an integer, we can get the minimum reading which has a full resolution
280 * reading. Raising 10 to the integer power gives the full resolution.
281 * Ex: For 30 V range, a full resolution reading is 10.000000.
282 * 2. A ratio is taken between the full resolution reading and the
283 * measurement. Since the full resolution reading is a power of 10,
284 * for every leading zero, this ratio will be slightly higher than a
285 * power of 10. For example, for 10 V full resolution:
286 * : 10.000000 V, ratio = 1.0000000
287 * : 9.999999 V, ratio = 1.0000001
288 * : 0.999999 V, ratio = 10.000001
289 * 3. The ratio is rounded up to prevent loss of precision in the next step.
290 * 4. The base 10 logarithm of the ratio is taken, then rounded up. This
291 * gives the number of leading zeroes in the measurement.
292 * For example, for 10 V full resolution:
293 * : 10.000000 V, ceil(1.0000000) = 1, log10 = 0.00; 0 leading zeroes
294 * : 9.999999 V, ceil(1.0000001) = 2, log10 = 0.30; 1 leading zero
295 * : 0.999999 V, ceil(10.000001) = 11, log10 = 1.04, 2 leading zeroes
296 * 5. The number of leading zeroes is subtracted from the maximum number of
297 * significant digits, 8, at 7 1/2 digits resolution.
298 * For a 10 V full resolution reading, this gives:
299 * : 10.000000 V, 0 leading zeroes => 8 significant digits
300 * : 9.999999 V, 1 leading zero => 7 significant digits
301 * : 0.999999 V, 2 leading zeroes => 6 significant digits
303 * Single precision floating point numbers can achieve about 16 million counts,
304 * but in high resolution mode we can get as much as 30 million counts. As a
305 * result, these calculations must be done with double precision
306 * (the HP 3457A is a very precise instrument).
308 static int calculate_num_zero_digits(double measurement, double range)
311 double min_full_res_reading, log10_range, full_res_ratio;
313 log10_range = log10(range);
314 min_full_res_reading = pow(10, (int)log10_range);
315 if (measurement > min_full_res_reading) {
317 } else if (measurement == 0.0) {
320 full_res_ratio = min_full_res_reading / measurement;
321 zero_digits = ceil(log10(ceil(full_res_ratio)));
328 * Until the output modules understand double precision data, we need to send
329 * the measurement as floats instead of doubles, hence, the dance with
330 * measurement_workaround double to float conversion.
331 * See bug #779 for details.
332 * The workaround should be removed once the output modules are fixed.
334 static void acq_send_measurement(struct sr_dev_inst *sdi)
336 double hires_measurement;
337 float measurement_workaround;
338 int zero_digits, num_digits;
339 struct sr_datafeed_packet packet;
340 struct sr_datafeed_analog analog;
341 struct sr_analog_encoding encoding;
342 struct sr_analog_meaning meaning;
343 struct sr_analog_spec spec;
344 struct dev_context *devc = sdi->priv;
346 hires_measurement = devc->base_measurement;
347 if (is_highres_enabled(devc))
348 hires_measurement += devc->hires_register;
350 /* Figure out how many of the digits are significant. */
351 num_digits = is_highres_enabled(devc) ? 8 : 7;
352 zero_digits = calculate_num_zero_digits(hires_measurement,
353 devc->measurement_range);
354 num_digits = num_digits - zero_digits;
356 packet.type = SR_DF_ANALOG;
357 packet.payload = &analog;
359 sr_analog_init(&analog, &encoding, &meaning, &spec, num_digits);
360 encoding.unitsize = sizeof(float);
362 meaning.channels = g_slist_append(NULL, devc->current_channel);
364 measurement_workaround = hires_measurement;
365 analog.num_samples = 1;
366 analog.data = &measurement_workaround;
368 meaning.mq = devc->measurement_mq;
369 meaning.mqflags = devc->measurement_mq_flags;
370 meaning.unit = devc->measurement_unit;
372 sr_session_send(sdi, &packet);
374 g_slist_free(meaning.channels);
378 * The scan-advance channel sync -- call to request_current_channel() -- is not
379 * necessarily needed. It is done in case we have a communication error and the
380 * DMM advances the channel without having sent the reading. The DMM only
381 * advances the channel when it thinks it sent the reading over HP-IB. Thus, on
382 * most errors we can retrigger the measurement and still be in sync. This
383 * check is done to make sure we don't fall out of sync due to obscure errors.
385 SR_PRIV int hp_3457a_receive_data(int fd, int revents, void *cb_data)
388 struct sr_scpi_dev_inst *scpi;
389 struct dev_context *devc;
390 struct channel_context *chanc;
391 struct sr_dev_inst *sdi;
396 if (!(sdi = cb_data))
399 if (!(devc = sdi->priv))
404 switch (devc->acq_state) {
405 case ACQ_TRIGGERED_MEASUREMENT:
406 ret = sr_scpi_get_double(scpi, NULL, &devc->base_measurement);
408 retrigger_measurement(scpi, devc);
412 if (is_highres_enabled(devc))
413 request_hires(scpi, devc);
415 request_range(scpi, devc);
418 case ACQ_REQUESTED_HIRES:
419 ret = sr_scpi_get_double(scpi, NULL, &devc->hires_register);
421 retrigger_measurement(scpi, devc);
424 request_range(scpi, devc);
426 case ACQ_REQUESTED_RANGE:
427 ret = sr_scpi_get_double(scpi, NULL, &devc->measurement_range);
429 retrigger_measurement(scpi, devc);
432 devc->acq_state = ACQ_GOT_MEASUREMENT;
434 case ACQ_REQUESTED_CHANNEL_SYNC:
435 ret = sr_scpi_get_double(scpi, NULL, &devc->last_channel_sync);
437 sr_err("Cannot check channel synchronization.");
438 sdi->driver->dev_acquisition_stop(sdi);
441 devc->acq_state = ACQ_GOT_CHANNEL_SYNC;
447 if (devc->acq_state == ACQ_GOT_MEASUREMENT) {
448 acq_send_measurement(sdi);
452 if (devc->acq_state == ACQ_GOT_CHANNEL_SYNC) {
453 chanc = devc->current_channel->priv;
454 if (chanc->index != devc->last_channel_sync) {
455 sr_err("Current channel and scan advance out of sync.");
456 sr_err("Expected channel %u, but device says %u",
458 (unsigned int)devc->last_channel_sync);
459 sdi->driver->dev_acquisition_stop(sdi);
462 /* All is good. Back to business. */
463 retrigger_measurement(scpi, devc);
466 if (devc->limit_samples && (devc->num_samples >= devc->limit_samples)) {
467 sdi->driver->dev_acquisition_stop(sdi);
471 /* Got more to go. */
472 if (devc->acq_state == ACQ_GOT_MEASUREMENT) {
473 activate_next_channel(devc);
474 /* Retrigger, or check if scan-advance is in sync. */
475 if (((devc->num_samples % 10) == 9)
476 && (devc->num_active_channels > 1)) {
477 request_current_channel(scpi, devc);
479 retrigger_measurement(scpi, devc);