]> sigrok.org Git - libsigrok.git/blame - src/hardware/hp-3457a/protocol.c
scpi-pps: Add a missing "break" in config_get().
[libsigrok.git] / src / hardware / hp-3457a / protocol.c
CommitLineData
00b2a092
AG
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>
db23af7f
AG
21#include <math.h>
22#include <scpi.h>
00b2a092
AG
23#include "protocol.h"
24
2c04f943
AG
25static int set_mq_volt(struct sr_scpi_dev_inst *scpi, enum sr_mqflag flags);
26static int set_mq_amp(struct sr_scpi_dev_inst *scpi, enum sr_mqflag flags);
27static int set_mq_ohm(struct sr_scpi_dev_inst *scpi, enum sr_mqflag flags);
ca314e06 28
db23af7f 29/*
db23af7f
AG
30 * The source for the frequency measurement can be either AC voltage, AC+DC
31 * voltage, AC current, or AC+DC current. Configuring this is not yet
32 * supported. For details, see "FSOURCE" command.
2c04f943
AG
33 * The set_mode function is optional and can be set to NULL, but in that case
34 * a cmd string must be provided.
db23af7f
AG
35 */
36static const struct {
37 enum sr_mq mq;
38 enum sr_unit unit;
39 const char *cmd;
2c04f943 40 int (*set_mode)(struct sr_scpi_dev_inst *scpi, enum sr_mqflag flags);
db23af7f 41} sr_mq_to_cmd_map[] = {
2c04f943
AG
42 { SR_MQ_VOLTAGE, SR_UNIT_VOLT, "DCV", set_mq_volt },
43 { SR_MQ_CURRENT, SR_UNIT_AMPERE, "DCI", set_mq_amp },
44 { SR_MQ_RESISTANCE, SR_UNIT_OHM, "OHM", set_mq_ohm },
45 { SR_MQ_FREQUENCY, SR_UNIT_HERTZ, "FREQ", NULL },
db23af7f
AG
46};
47
48static const struct rear_card_info rear_card_parameters[] = {
49 {
50 .type = REAR_TERMINALS,
51 .card_id = 0,
52 .name = "Rear terminals",
53 .cg_name = "rear",
9a093be9 54 .num_channels = 1,
db23af7f
AG
55 }, {
56 .type = HP_44491A,
57 .card_id = 44491,
58 .name = "44491A Armature Relay Multiplexer",
59 .cg_name = "44491a",
9a093be9 60 .num_channels = 14,
db23af7f
AG
61 }, {
62 .type = HP_44492A,
63 .card_id = 44492,
64 .name = "44492A Reed Relay Multiplexer",
65 .cg_name = "44492a",
9a093be9 66 .num_channels = 10,
db23af7f
AG
67 }
68};
69
2c04f943 70static int send_mq_ac_dc(struct sr_scpi_dev_inst *scpi, const char *mode,
d9251a2c 71 enum sr_mqflag flags)
2c04f943
AG
72{
73 const char *ac_flag, *dc_flag;
74
75 if (flags & ~(SR_MQFLAG_AC | SR_MQFLAG_DC))
76 return SR_ERR_NA;
77
78 ac_flag = (flags & SR_MQFLAG_AC) ? "AC" : "";
79 dc_flag = "";
80 /* Must specify DC measurement when AC flag is not given. */
81 if ((flags & SR_MQFLAG_DC) || !(flags & SR_MQFLAG_AC))
82 dc_flag = "DC";
83
84 return sr_scpi_send(scpi, "%s%s%s", ac_flag, dc_flag, mode);
85}
86
87static int set_mq_volt(struct sr_scpi_dev_inst *scpi, enum sr_mqflag flags)
88{
89 return send_mq_ac_dc(scpi, "V", flags);
90}
91
92static int set_mq_amp(struct sr_scpi_dev_inst *scpi, enum sr_mqflag flags)
93{
94 return send_mq_ac_dc(scpi, "I", flags);
95}
96
97static int set_mq_ohm(struct sr_scpi_dev_inst *scpi, enum sr_mqflag flags)
98{
99 const char *ohm_flag;
100
101 if (flags & ~(SR_MQFLAG_FOUR_WIRE))
102 return SR_ERR_NA;
103
104 ohm_flag = (flags & SR_MQFLAG_FOUR_WIRE) ? "F" : "";
105 return sr_scpi_send(scpi, "OHM%s", ohm_flag);
106}
107
108SR_PRIV int hp_3457a_set_mq(const struct sr_dev_inst *sdi, enum sr_mq mq,
109 enum sr_mqflag mq_flags)
db23af7f
AG
110{
111 int ret;
112 size_t i;
113 struct sr_scpi_dev_inst *scpi = sdi->conn;
114 struct dev_context *devc = sdi->priv;
115
9a093be9
AG
116 /* No need to send command if we're not changing measurement type. */
117 if (devc->measurement_mq == mq)
118 return SR_OK;
119
db23af7f
AG
120 for (i = 0; i < ARRAY_SIZE(sr_mq_to_cmd_map); i++) {
121 if (sr_mq_to_cmd_map[i].mq != mq)
122 continue;
2c04f943
AG
123 if (sr_mq_to_cmd_map[i].set_mode) {
124 ret = sr_mq_to_cmd_map[i].set_mode(scpi, mq_flags);
125 } else {
126 ret = sr_scpi_send(scpi, sr_mq_to_cmd_map[i].cmd);
127 }
db23af7f
AG
128 if (ret == SR_OK) {
129 devc->measurement_mq = sr_mq_to_cmd_map[i].mq;
2c04f943 130 devc->measurement_mq_flags = mq_flags;
db23af7f
AG
131 devc->measurement_unit = sr_mq_to_cmd_map[i].unit;
132 }
133 return ret;
134 }
135
136 return SR_ERR_NA;
137}
138
139SR_PRIV const struct rear_card_info *hp_3457a_probe_rear_card(struct sr_scpi_dev_inst *scpi)
140{
141 size_t i;
142 float card_fval;
143 unsigned int card_id;
144 const struct rear_card_info *rear_card = NULL;
145
146 if (sr_scpi_get_float(scpi, "OPT?", &card_fval) != SR_OK)
147 return NULL;
148
149 card_id = (unsigned int)card_fval;
150
151 for (i = 0; i < ARRAY_SIZE(rear_card_parameters); i++) {
152 if (rear_card_parameters[i].card_id == card_id) {
153 rear_card = rear_card_parameters + i;
154 break;
155 }
156 }
157
158 if (!rear_card)
159 return NULL;
160
161 sr_info("Found %s.", rear_card->name);
162
163 return rear_card;
164}
165
166SR_PRIV int hp_3457a_set_nplc(const struct sr_dev_inst *sdi, float nplc)
167{
168 int ret;
169 struct sr_scpi_dev_inst *scpi = sdi->conn;
170 struct dev_context *devc = sdi->priv;
171
172 if ((nplc < 1E-6) || (nplc > 100))
173 return SR_ERR_ARG;
174
175 /* Only need one digit of precision here. */
176 ret = sr_scpi_send(scpi, "NPLC %.0E", nplc);
177
178 /*
179 * The instrument only has a few valid NPLC setting, so get back the
180 * one which was selected.
181 */
182 sr_scpi_get_float(scpi, "NPLC?", &devc->nplc);
183
184 return ret;
185}
186
9a093be9
AG
187SR_PRIV int hp_3457a_select_input(const struct sr_dev_inst *sdi,
188 enum channel_conn loc)
189{
190 int ret;
191 struct sr_scpi_dev_inst *scpi = sdi->conn;
192 struct dev_context *devc = sdi->priv;
193
194 if (devc->input_loc == loc)
195 return SR_OK;
196
197 ret = sr_scpi_send(scpi, "TERM %s", (loc == CONN_FRONT) ? "FRONT": "REAR");
198 if (ret == SR_OK)
199 devc->input_loc = loc;
200
201 return ret;
202}
203
204SR_PRIV int hp_3457a_send_scan_list(const struct sr_dev_inst *sdi,
205 unsigned int *channels, size_t len)
206{
207 size_t i;
208 char chan[16], list_str[64] = "";
209
210 for (i = 0; i < len; i++) {
211 g_snprintf(chan, sizeof(chan), ",%u", channels[i]);
212 g_strlcat(list_str, chan, sizeof(list_str));
213 }
214
215 return sr_scpi_send(sdi->conn, "SLIST %s", list_str);
216}
217
db23af7f
AG
218/* HIRES register only contains valid data with 10 or more powerline cycles. */
219static int is_highres_enabled(struct dev_context *devc)
220{
221 return (devc->nplc >= 10.0);
222}
223
9a093be9
AG
224static void activate_next_channel(struct dev_context *devc)
225{
226 GSList *list_elem;
227 struct sr_channel *chan;
228
229 list_elem = g_slist_find(devc->active_channels, devc->current_channel);
230 if (list_elem)
231 list_elem = list_elem->next;
232 if (!list_elem)
233 list_elem = devc->active_channels;
234
235 chan = list_elem->data;
236
237 devc->current_channel = chan;
238}
239
db23af7f
AG
240static void retrigger_measurement(struct sr_scpi_dev_inst *scpi,
241 struct dev_context *devc)
242{
243 sr_scpi_send(scpi, "?");
244 devc->acq_state = ACQ_TRIGGERED_MEASUREMENT;
245}
246
247static void request_hires(struct sr_scpi_dev_inst *scpi,
248 struct dev_context *devc)
249{
250 sr_scpi_send(scpi, "RMATH HIRES");
251 devc->acq_state = ACQ_REQUESTED_HIRES;
252}
253
254static void request_range(struct sr_scpi_dev_inst *scpi,
255 struct dev_context *devc)
256{
257 sr_scpi_send(scpi, "RANGE?");
258 devc->acq_state = ACQ_REQUESTED_RANGE;
259}
260
9a093be9
AG
261static void request_current_channel(struct sr_scpi_dev_inst *scpi,
262 struct dev_context *devc)
263{
264 sr_scpi_send(scpi, "CHAN?");
265 devc->acq_state = ACQ_REQUESTED_CHANNEL_SYNC;
266}
267
db23af7f
AG
268/*
269 * Calculate the number of leading zeroes in the measurement.
270 *
271 * Depending on the range and measurement, a reading may not have eight digits
272 * of resolution. For example, on a 30V range:
273 * : 10.000000 V has 8 significant digits
274 * : 9.999999 V has 7 significant digits
275 * : 0.999999 V has 6 significant digits
276 *
277 * The number of significant digits is determined based on the range in which
278 * the measurement was taken:
279 * 1. By taking the base 10 logarithm of the range, and converting that to
280 * an integer, we can get the minimum reading which has a full resolution
281 * reading. Raising 10 to the integer power gives the full resolution.
282 * Ex: For 30 V range, a full resolution reading is 10.000000.
283 * 2. A ratio is taken between the full resolution reading and the
284 * measurement. Since the full resolution reading is a power of 10,
285 * for every leading zero, this ratio will be slightly higher than a
286 * power of 10. For example, for 10 V full resolution:
287 * : 10.000000 V, ratio = 1.0000000
288 * : 9.999999 V, ratio = 1.0000001
289 * : 0.999999 V, ratio = 10.000001
290 * 3. The ratio is rounded up to prevent loss of precision in the next step.
291 * 4. The base 10 logarithm of the ratio is taken, then rounded up. This
292 * gives the number of leading zeroes in the measurement.
293 * For example, for 10 V full resolution:
294 * : 10.000000 V, ceil(1.0000000) = 1, log10 = 0.00; 0 leading zeroes
295 * : 9.999999 V, ceil(1.0000001) = 2, log10 = 0.30; 1 leading zero
296 * : 0.999999 V, ceil(10.000001) = 11, log10 = 1.04, 2 leading zeroes
297 * 5. The number of leading zeroes is subtracted from the maximum number of
298 * significant digits, 8, at 7 1/2 digits resolution.
299 * For a 10 V full resolution reading, this gives:
300 * : 10.000000 V, 0 leading zeroes => 8 significant digits
301 * : 9.999999 V, 1 leading zero => 7 significant digits
302 * : 0.999999 V, 2 leading zeroes => 6 significant digits
303 *
304 * Single precision floating point numbers can achieve about 16 million counts,
305 * but in high resolution mode we can get as much as 30 million counts. As a
306 * result, these calculations must be done with double precision
307 * (the HP 3457A is a very precise instrument).
308 */
309static int calculate_num_zero_digits(double measurement, double range)
310{
311 int zero_digits;
312 double min_full_res_reading, log10_range, full_res_ratio;
313
314 log10_range = log10(range);
315 min_full_res_reading = pow(10, (int)log10_range);
316 if (measurement > min_full_res_reading) {
317 zero_digits = 0;
318 } else if (measurement == 0.0) {
319 zero_digits = 0;
320 } else {
321 full_res_ratio = min_full_res_reading / measurement;
322 zero_digits = ceil(log10(ceil(full_res_ratio)));
323 }
324
325 return zero_digits;
326}
327
625430bf
AG
328/*
329 * Until the output modules understand double precision data, we need to send
330 * the measurement as floats instead of doubles, hence, the dance with
331 * measurement_workaround double to float conversion.
332 * See bug #779 for details.
333 * The workaround should be removed once the output modules are fixed.
334 */
db23af7f
AG
335static void acq_send_measurement(struct sr_dev_inst *sdi)
336{
337 double hires_measurement;
625430bf 338 float measurement_workaround;
db23af7f
AG
339 int zero_digits, num_digits;
340 struct sr_datafeed_packet packet;
341 struct sr_datafeed_analog analog;
342 struct sr_analog_encoding encoding;
343 struct sr_analog_meaning meaning;
344 struct sr_analog_spec spec;
345 struct dev_context *devc = sdi->priv;
346
347 hires_measurement = devc->base_measurement;
348 if (is_highres_enabled(devc))
349 hires_measurement += devc->hires_register;
350
351 /* Figure out how many of the digits are significant. */
352 num_digits = is_highres_enabled(devc) ? 8 : 7;
353 zero_digits = calculate_num_zero_digits(hires_measurement,
354 devc->measurement_range);
355 num_digits = num_digits - zero_digits;
356
357 packet.type = SR_DF_ANALOG;
358 packet.payload = &analog;
359
360 sr_analog_init(&analog, &encoding, &meaning, &spec, num_digits);
625430bf 361 encoding.unitsize = sizeof(float);
db23af7f 362
9a093be9 363 meaning.channels = g_slist_append(NULL, devc->current_channel);
db23af7f 364
625430bf 365 measurement_workaround = hires_measurement;
db23af7f 366 analog.num_samples = 1;
625430bf 367 analog.data = &measurement_workaround;
db23af7f
AG
368
369 meaning.mq = devc->measurement_mq;
2c04f943 370 meaning.mqflags = devc->measurement_mq_flags;
db23af7f
AG
371 meaning.unit = devc->measurement_unit;
372
373 sr_session_send(sdi, &packet);
9a093be9
AG
374
375 g_slist_free(meaning.channels);
db23af7f
AG
376}
377
9a093be9
AG
378/*
379 * The scan-advance channel sync -- call to request_current_channel() -- is not
380 * necessarily needed. It is done in case we have a communication error and the
381 * DMM advances the channel without having sent the reading. The DMM only
382 * advances the channel when it thinks it sent the reading over HP-IB. Thus, on
383 * most errors we can retrigger the measurement and still be in sync. This
384 * check is done to make sure we don't fall out of sync due to obscure errors.
385 */
00b2a092
AG
386SR_PRIV int hp_3457a_receive_data(int fd, int revents, void *cb_data)
387{
db23af7f
AG
388 int ret;
389 struct sr_scpi_dev_inst *scpi;
00b2a092 390 struct dev_context *devc;
9a093be9 391 struct channel_context *chanc;
695dc859 392 struct sr_dev_inst *sdi;
00b2a092
AG
393
394 (void)fd;
db23af7f 395 (void)revents;
00b2a092
AG
396
397 if (!(sdi = cb_data))
398 return TRUE;
399
400 if (!(devc = sdi->priv))
401 return TRUE;
402
db23af7f
AG
403 scpi = sdi->conn;
404
405 switch (devc->acq_state) {
406 case ACQ_TRIGGERED_MEASUREMENT:
407 ret = sr_scpi_get_double(scpi, NULL, &devc->base_measurement);
408 if (ret != SR_OK) {
409 retrigger_measurement(scpi, devc);
410 return TRUE;
411 }
412
413 if (is_highres_enabled(devc))
414 request_hires(scpi, devc);
415 else
416 request_range(scpi, devc);
417
418 break;
419 case ACQ_REQUESTED_HIRES:
420 ret = sr_scpi_get_double(scpi, NULL, &devc->hires_register);
421 if (ret != SR_OK) {
422 retrigger_measurement(scpi, devc);
423 return TRUE;
424 }
425 request_range(scpi, devc);
426 break;
427 case ACQ_REQUESTED_RANGE:
428 ret = sr_scpi_get_double(scpi, NULL, &devc->measurement_range);
429 if (ret != SR_OK) {
430 retrigger_measurement(scpi, devc);
431 return TRUE;
432 }
433 devc->acq_state = ACQ_GOT_MEASUREMENT;
434 break;
9a093be9
AG
435 case ACQ_REQUESTED_CHANNEL_SYNC:
436 ret = sr_scpi_get_double(scpi, NULL, &devc->last_channel_sync);
437 if (ret != SR_OK) {
438 sr_err("Cannot check channel synchronization.");
d2f7c417 439 sr_dev_acquisition_stop(sdi);
9a093be9
AG
440 return FALSE;
441 }
442 devc->acq_state = ACQ_GOT_CHANNEL_SYNC;
443 break;
db23af7f
AG
444 default:
445 return FALSE;
446 }
447
e2626373 448 if (devc->acq_state == ACQ_GOT_MEASUREMENT) {
db23af7f 449 acq_send_measurement(sdi);
e2626373
AG
450 devc->num_samples++;
451 }
db23af7f 452
9a093be9
AG
453 if (devc->acq_state == ACQ_GOT_CHANNEL_SYNC) {
454 chanc = devc->current_channel->priv;
455 if (chanc->index != devc->last_channel_sync) {
456 sr_err("Current channel and scan advance out of sync.");
457 sr_err("Expected channel %u, but device says %u",
458 chanc->index,
459 (unsigned int)devc->last_channel_sync);
d2f7c417 460 sr_dev_acquisition_stop(sdi);
9a093be9
AG
461 return FALSE;
462 }
463 /* All is good. Back to business. */
464 retrigger_measurement(scpi, devc);
465 }
466
db23af7f 467 if (devc->limit_samples && (devc->num_samples >= devc->limit_samples)) {
d2f7c417 468 sr_dev_acquisition_stop(sdi);
db23af7f
AG
469 return FALSE;
470 }
471
472 /* Got more to go. */
473 if (devc->acq_state == ACQ_GOT_MEASUREMENT) {
9a093be9
AG
474 activate_next_channel(devc);
475 /* Retrigger, or check if scan-advance is in sync. */
476 if (((devc->num_samples % 10) == 9)
477 && (devc->num_active_channels > 1)) {
478 request_current_channel(scpi, devc);
479 } else {
480 retrigger_measurement(scpi, devc);
481 }
00b2a092
AG
482 }
483
484 return TRUE;
485}