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/>.
26 * Currently, only DC voltage and current are supported, as switching to AC or
27 * AC+DC requires mq flags, which is not yet implemented.
28 * Four-wire resistance measurements are not implemented (See "OHMF" command).
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.
37 } sr_mq_to_cmd_map[] = {
38 { SR_MQ_VOLTAGE, SR_UNIT_VOLT, "DCV" },
39 { SR_MQ_CURRENT, SR_UNIT_AMPERE, "DCI" },
40 { SR_MQ_RESISTANCE, SR_UNIT_OHM, "OHM" },
41 { SR_MQ_FREQUENCY, SR_UNIT_HERTZ, "FREQ" },
44 static const struct rear_card_info rear_card_parameters[] = {
46 .type = REAR_TERMINALS,
48 .name = "Rear terminals",
53 .name = "44491A Armature Relay Multiplexer",
58 .name = "44492A Reed Relay Multiplexer",
63 SR_PRIV int hp_3457a_set_mq(const struct sr_dev_inst *sdi, enum sr_mq mq)
67 struct sr_scpi_dev_inst *scpi = sdi->conn;
68 struct dev_context *devc = sdi->priv;
70 for (i = 0; i < ARRAY_SIZE(sr_mq_to_cmd_map); i++) {
71 if (sr_mq_to_cmd_map[i].mq != mq)
73 ret = sr_scpi_send(scpi, sr_mq_to_cmd_map[i].cmd);
75 devc->measurement_mq = sr_mq_to_cmd_map[i].mq;
76 devc->measurement_unit = sr_mq_to_cmd_map[i].unit;
84 SR_PRIV const struct rear_card_info *hp_3457a_probe_rear_card(struct sr_scpi_dev_inst *scpi)
89 const struct rear_card_info *rear_card = NULL;
91 if (sr_scpi_get_float(scpi, "OPT?", &card_fval) != SR_OK)
94 card_id = (unsigned int)card_fval;
96 for (i = 0; i < ARRAY_SIZE(rear_card_parameters); i++) {
97 if (rear_card_parameters[i].card_id == card_id) {
98 rear_card = rear_card_parameters + i;
106 sr_info("Found %s.", rear_card->name);
111 SR_PRIV int hp_3457a_set_nplc(const struct sr_dev_inst *sdi, float nplc)
114 struct sr_scpi_dev_inst *scpi = sdi->conn;
115 struct dev_context *devc = sdi->priv;
117 if ((nplc < 1E-6) || (nplc > 100))
120 /* Only need one digit of precision here. */
121 ret = sr_scpi_send(scpi, "NPLC %.0E", nplc);
124 * The instrument only has a few valid NPLC setting, so get back the
125 * one which was selected.
127 sr_scpi_get_float(scpi, "NPLC?", &devc->nplc);
132 /* HIRES register only contains valid data with 10 or more powerline cycles. */
133 static int is_highres_enabled(struct dev_context *devc)
135 return (devc->nplc >= 10.0);
138 static void retrigger_measurement(struct sr_scpi_dev_inst *scpi,
139 struct dev_context *devc)
141 sr_scpi_send(scpi, "?");
142 devc->acq_state = ACQ_TRIGGERED_MEASUREMENT;
145 static void request_hires(struct sr_scpi_dev_inst *scpi,
146 struct dev_context *devc)
148 sr_scpi_send(scpi, "RMATH HIRES");
149 devc->acq_state = ACQ_REQUESTED_HIRES;
152 static void request_range(struct sr_scpi_dev_inst *scpi,
153 struct dev_context *devc)
155 sr_scpi_send(scpi, "RANGE?");
156 devc->acq_state = ACQ_REQUESTED_RANGE;
160 * Calculate the number of leading zeroes in the measurement.
162 * Depending on the range and measurement, a reading may not have eight digits
163 * of resolution. For example, on a 30V range:
164 * : 10.000000 V has 8 significant digits
165 * : 9.999999 V has 7 significant digits
166 * : 0.999999 V has 6 significant digits
168 * The number of significant digits is determined based on the range in which
169 * the measurement was taken:
170 * 1. By taking the base 10 logarithm of the range, and converting that to
171 * an integer, we can get the minimum reading which has a full resolution
172 * reading. Raising 10 to the integer power gives the full resolution.
173 * Ex: For 30 V range, a full resolution reading is 10.000000.
174 * 2. A ratio is taken between the full resolution reading and the
175 * measurement. Since the full resolution reading is a power of 10,
176 * for every leading zero, this ratio will be slightly higher than a
177 * power of 10. For example, for 10 V full resolution:
178 * : 10.000000 V, ratio = 1.0000000
179 * : 9.999999 V, ratio = 1.0000001
180 * : 0.999999 V, ratio = 10.000001
181 * 3. The ratio is rounded up to prevent loss of precision in the next step.
182 * 4. The base 10 logarithm of the ratio is taken, then rounded up. This
183 * gives the number of leading zeroes in the measurement.
184 * For example, for 10 V full resolution:
185 * : 10.000000 V, ceil(1.0000000) = 1, log10 = 0.00; 0 leading zeroes
186 * : 9.999999 V, ceil(1.0000001) = 2, log10 = 0.30; 1 leading zero
187 * : 0.999999 V, ceil(10.000001) = 11, log10 = 1.04, 2 leading zeroes
188 * 5. The number of leading zeroes is subtracted from the maximum number of
189 * significant digits, 8, at 7 1/2 digits resolution.
190 * For a 10 V full resolution reading, this gives:
191 * : 10.000000 V, 0 leading zeroes => 8 significant digits
192 * : 9.999999 V, 1 leading zero => 7 significant digits
193 * : 0.999999 V, 2 leading zeroes => 6 significant digits
195 * Single precision floating point numbers can achieve about 16 million counts,
196 * but in high resolution mode we can get as much as 30 million counts. As a
197 * result, these calculations must be done with double precision
198 * (the HP 3457A is a very precise instrument).
200 static int calculate_num_zero_digits(double measurement, double range)
203 double min_full_res_reading, log10_range, full_res_ratio;
205 log10_range = log10(range);
206 min_full_res_reading = pow(10, (int)log10_range);
207 if (measurement > min_full_res_reading) {
209 } else if (measurement == 0.0) {
212 full_res_ratio = min_full_res_reading / measurement;
213 zero_digits = ceil(log10(ceil(full_res_ratio)));
220 * Until the output modules understand double precision data, we need to send
221 * the measurement as floats instead of doubles, hence, the dance with
222 * measurement_workaround double to float conversion.
223 * See bug #779 for details.
224 * The workaround should be removed once the output modules are fixed.
226 static void acq_send_measurement(struct sr_dev_inst *sdi)
228 double hires_measurement;
229 float measurement_workaround;
230 int zero_digits, num_digits;
231 struct sr_datafeed_packet packet;
232 struct sr_datafeed_analog analog;
233 struct sr_analog_encoding encoding;
234 struct sr_analog_meaning meaning;
235 struct sr_analog_spec spec;
236 struct dev_context *devc = sdi->priv;
238 hires_measurement = devc->base_measurement;
239 if (is_highres_enabled(devc))
240 hires_measurement += devc->hires_register;
242 /* Figure out how many of the digits are significant. */
243 num_digits = is_highres_enabled(devc) ? 8 : 7;
244 zero_digits = calculate_num_zero_digits(hires_measurement,
245 devc->measurement_range);
246 num_digits = num_digits - zero_digits;
248 packet.type = SR_DF_ANALOG;
249 packet.payload = &analog;
251 sr_analog_init(&analog, &encoding, &meaning, &spec, num_digits);
252 encoding.unitsize = sizeof(float);
254 meaning.channels = sdi->channels;
256 measurement_workaround = hires_measurement;
257 analog.num_samples = 1;
258 analog.data = &measurement_workaround;
260 meaning.mq = devc->measurement_mq;
261 meaning.unit = devc->measurement_unit;
263 sr_session_send(sdi, &packet);
266 SR_PRIV int hp_3457a_receive_data(int fd, int revents, void *cb_data)
269 struct sr_scpi_dev_inst *scpi;
270 struct dev_context *devc;
271 struct sr_dev_inst *sdi = cb_data;
276 if (!(sdi = cb_data))
279 if (!(devc = sdi->priv))
284 switch (devc->acq_state) {
285 case ACQ_TRIGGERED_MEASUREMENT:
286 ret = sr_scpi_get_double(scpi, NULL, &devc->base_measurement);
288 retrigger_measurement(scpi, devc);
292 if (is_highres_enabled(devc))
293 request_hires(scpi, devc);
295 request_range(scpi, devc);
298 case ACQ_REQUESTED_HIRES:
299 ret = sr_scpi_get_double(scpi, NULL, &devc->hires_register);
301 retrigger_measurement(scpi, devc);
304 request_range(scpi, devc);
306 case ACQ_REQUESTED_RANGE:
307 ret = sr_scpi_get_double(scpi, NULL, &devc->measurement_range);
309 retrigger_measurement(scpi, devc);
312 devc->acq_state = ACQ_GOT_MEASUREMENT;
318 if (devc->acq_state == ACQ_GOT_MEASUREMENT)
319 acq_send_measurement(sdi);
321 if (devc->limit_samples && (devc->num_samples >= devc->limit_samples)) {
322 sdi->driver->dev_acquisition_stop(sdi, cb_data);
326 /* Got more to go. */
327 if (devc->acq_state == ACQ_GOT_MEASUREMENT) {
330 retrigger_measurement(scpi, devc);