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)));
219 static void acq_send_measurement(struct sr_dev_inst *sdi)
221 double hires_measurement;
222 int zero_digits, num_digits;
223 struct sr_datafeed_packet packet;
224 struct sr_datafeed_analog analog;
225 struct sr_analog_encoding encoding;
226 struct sr_analog_meaning meaning;
227 struct sr_analog_spec spec;
228 struct dev_context *devc = sdi->priv;
230 hires_measurement = devc->base_measurement;
231 if (is_highres_enabled(devc))
232 hires_measurement += devc->hires_register;
234 /* Figure out how many of the digits are significant. */
235 num_digits = is_highres_enabled(devc) ? 8 : 7;
236 zero_digits = calculate_num_zero_digits(hires_measurement,
237 devc->measurement_range);
238 num_digits = num_digits - zero_digits;
240 packet.type = SR_DF_ANALOG;
241 packet.payload = &analog;
243 sr_analog_init(&analog, &encoding, &meaning, &spec, num_digits);
244 encoding.unitsize = sizeof(double);
246 meaning.channels = sdi->channels;
248 analog.num_samples = 1;
249 analog.data = &hires_measurement;
251 meaning.mq = devc->measurement_mq;
252 meaning.unit = devc->measurement_unit;
254 sr_session_send(sdi, &packet);
257 SR_PRIV int hp_3457a_receive_data(int fd, int revents, void *cb_data)
260 struct sr_scpi_dev_inst *scpi;
261 struct dev_context *devc;
262 struct sr_dev_inst *sdi = cb_data;
267 if (!(sdi = cb_data))
270 if (!(devc = sdi->priv))
275 switch (devc->acq_state) {
276 case ACQ_TRIGGERED_MEASUREMENT:
277 ret = sr_scpi_get_double(scpi, NULL, &devc->base_measurement);
279 retrigger_measurement(scpi, devc);
283 if (is_highres_enabled(devc))
284 request_hires(scpi, devc);
286 request_range(scpi, devc);
289 case ACQ_REQUESTED_HIRES:
290 ret = sr_scpi_get_double(scpi, NULL, &devc->hires_register);
292 retrigger_measurement(scpi, devc);
295 request_range(scpi, devc);
297 case ACQ_REQUESTED_RANGE:
298 ret = sr_scpi_get_double(scpi, NULL, &devc->measurement_range);
300 retrigger_measurement(scpi, devc);
303 devc->acq_state = ACQ_GOT_MEASUREMENT;
309 if (devc->acq_state == ACQ_GOT_MEASUREMENT)
310 acq_send_measurement(sdi);
312 if (devc->limit_samples && (devc->num_samples >= devc->limit_samples)) {
313 sdi->driver->dev_acquisition_stop(sdi, cb_data);
317 /* Got more to go. */
318 if (devc->acq_state == ACQ_GOT_MEASUREMENT) {
321 retrigger_measurement(scpi, devc);