]> sigrok.org Git - libsigrok.git/blob - src/hardware/hp-3457a/protocol.c
320d8d0abfe114c657ebbf627809365b8a80c9dc
[libsigrok.git] / src / hardware / hp-3457a / protocol.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2016 Alexandru Gagniuc <mr.nuke.me@gmail.com>
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #include <config.h>
21 #include <math.h>
22 #include <scpi.h>
23 #include "protocol.h"
24
25 /*
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.
32  */
33 static const struct {
34         enum sr_mq mq;
35         enum sr_unit unit;
36         const char *cmd;
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" },
42 };
43
44 static const struct rear_card_info rear_card_parameters[] = {
45         {
46                 .type = REAR_TERMINALS,
47                 .card_id = 0,
48                 .name = "Rear terminals",
49                 .cg_name = "rear",
50         }, {
51                 .type = HP_44491A,
52                 .card_id = 44491,
53                 .name = "44491A Armature Relay Multiplexer",
54                 .cg_name = "44491a",
55         }, {
56                 .type = HP_44492A,
57                 .card_id = 44492,
58                 .name = "44492A Reed Relay Multiplexer",
59                 .cg_name = "44492a",
60         }
61 };
62
63 SR_PRIV int hp_3457a_set_mq(const struct sr_dev_inst *sdi, enum sr_mq mq)
64 {
65         int ret;
66         size_t i;
67         struct sr_scpi_dev_inst *scpi = sdi->conn;
68         struct dev_context *devc = sdi->priv;
69
70         for (i = 0; i < ARRAY_SIZE(sr_mq_to_cmd_map); i++) {
71                 if (sr_mq_to_cmd_map[i].mq != mq)
72                         continue;
73                 ret = sr_scpi_send(scpi, sr_mq_to_cmd_map[i].cmd);
74                 if (ret == SR_OK) {
75                         devc->measurement_mq = sr_mq_to_cmd_map[i].mq;
76                         devc->measurement_unit = sr_mq_to_cmd_map[i].unit;
77                 }
78                 return ret;
79         }
80
81         return SR_ERR_NA;
82 }
83
84 SR_PRIV const struct rear_card_info *hp_3457a_probe_rear_card(struct sr_scpi_dev_inst *scpi)
85 {
86         size_t i;
87         float card_fval;
88         unsigned int card_id;
89         const struct rear_card_info *rear_card = NULL;
90
91         if (sr_scpi_get_float(scpi, "OPT?", &card_fval) != SR_OK)
92                 return NULL;
93
94         card_id = (unsigned int)card_fval;
95
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;
99                         break;
100                 }
101         }
102
103         if (!rear_card)
104                 return NULL;
105
106         sr_info("Found %s.", rear_card->name);
107
108         return rear_card;
109 }
110
111 SR_PRIV int hp_3457a_set_nplc(const struct sr_dev_inst *sdi, float nplc)
112 {
113         int ret;
114         struct sr_scpi_dev_inst *scpi = sdi->conn;
115         struct dev_context *devc = sdi->priv;
116
117         if ((nplc < 1E-6) || (nplc > 100))
118                 return SR_ERR_ARG;
119
120         /* Only need one digit of precision here. */
121         ret = sr_scpi_send(scpi, "NPLC %.0E", nplc);
122
123         /*
124          * The instrument only has a few valid NPLC setting, so get back the
125          * one which was selected.
126          */
127         sr_scpi_get_float(scpi, "NPLC?", &devc->nplc);
128
129         return ret;
130 }
131
132 /* HIRES register only contains valid data with 10 or more powerline cycles. */
133 static int is_highres_enabled(struct dev_context *devc)
134 {
135         return (devc->nplc >= 10.0);
136 }
137
138 static void retrigger_measurement(struct sr_scpi_dev_inst *scpi,
139                                   struct dev_context *devc)
140 {
141         sr_scpi_send(scpi, "?");
142         devc->acq_state = ACQ_TRIGGERED_MEASUREMENT;
143 }
144
145 static void request_hires(struct sr_scpi_dev_inst *scpi,
146                           struct dev_context *devc)
147 {
148         sr_scpi_send(scpi, "RMATH HIRES");
149         devc->acq_state = ACQ_REQUESTED_HIRES;
150 }
151
152 static void request_range(struct sr_scpi_dev_inst *scpi,
153                           struct dev_context *devc)
154 {
155         sr_scpi_send(scpi, "RANGE?");
156         devc->acq_state = ACQ_REQUESTED_RANGE;
157 }
158
159 /*
160  * Calculate the number of leading zeroes in the measurement.
161  *
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
167  *
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
194  *
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).
199  */
200 static int calculate_num_zero_digits(double measurement, double range)
201 {
202         int zero_digits;
203         double min_full_res_reading, log10_range, full_res_ratio;
204
205         log10_range = log10(range);
206         min_full_res_reading = pow(10, (int)log10_range);
207         if (measurement > min_full_res_reading) {
208                 zero_digits = 0;
209         } else if (measurement == 0.0) {
210                 zero_digits = 0;
211         } else {
212                 full_res_ratio = min_full_res_reading / measurement;
213                 zero_digits = ceil(log10(ceil(full_res_ratio)));
214         }
215
216         return zero_digits;
217 }
218
219 static void acq_send_measurement(struct sr_dev_inst *sdi)
220 {
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;
229
230         hires_measurement = devc->base_measurement;
231         if (is_highres_enabled(devc))
232                 hires_measurement += devc->hires_register;
233
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;
239
240         packet.type = SR_DF_ANALOG;
241         packet.payload = &analog;
242
243         sr_analog_init(&analog, &encoding, &meaning, &spec, num_digits);
244         encoding.unitsize = sizeof(double);
245
246         meaning.channels = sdi->channels;
247
248         analog.num_samples = 1;
249         analog.data = &hires_measurement;
250
251         meaning.mq = devc->measurement_mq;
252         meaning.unit = devc->measurement_unit;
253
254         sr_session_send(sdi, &packet);
255 }
256
257 SR_PRIV int hp_3457a_receive_data(int fd, int revents, void *cb_data)
258 {
259         int ret;
260         struct sr_scpi_dev_inst *scpi;
261         struct dev_context *devc;
262         struct sr_dev_inst *sdi = cb_data;
263
264         (void)fd;
265         (void)revents;
266
267         if (!(sdi = cb_data))
268                 return TRUE;
269
270         if (!(devc = sdi->priv))
271                 return TRUE;
272
273         scpi = sdi->conn;
274
275         switch (devc->acq_state) {
276         case ACQ_TRIGGERED_MEASUREMENT:
277                 ret = sr_scpi_get_double(scpi, NULL, &devc->base_measurement);
278                 if (ret != SR_OK) {
279                         retrigger_measurement(scpi, devc);
280                         return TRUE;
281                 }
282
283                 if (is_highres_enabled(devc))
284                         request_hires(scpi, devc);
285                 else
286                         request_range(scpi, devc);
287
288                 break;
289         case ACQ_REQUESTED_HIRES:
290                 ret = sr_scpi_get_double(scpi, NULL, &devc->hires_register);
291                 if (ret != SR_OK) {
292                         retrigger_measurement(scpi, devc);
293                         return TRUE;
294                 }
295                 request_range(scpi, devc);
296                 break;
297         case ACQ_REQUESTED_RANGE:
298                 ret = sr_scpi_get_double(scpi, NULL, &devc->measurement_range);
299                 if (ret != SR_OK) {
300                         retrigger_measurement(scpi, devc);
301                         return TRUE;
302                 }
303                 devc->acq_state = ACQ_GOT_MEASUREMENT;
304                 break;
305         default:
306                 return FALSE;
307         }
308
309         if (devc->acq_state == ACQ_GOT_MEASUREMENT)
310                 acq_send_measurement(sdi);
311
312         if (devc->limit_samples && (devc->num_samples >= devc->limit_samples)) {
313                 sdi->driver->dev_acquisition_stop(sdi, cb_data);
314                 return FALSE;
315         }
316
317         /* Got more to go. */
318         if (devc->acq_state == ACQ_GOT_MEASUREMENT) {
319                 /* Retrigger */
320                 devc->num_samples++;
321                 retrigger_measurement(scpi, devc);
322         }
323
324         return TRUE;
325 }