]>
Commit | Line | Data |
---|---|---|
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 | /* | |
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. | |
225 | */ | |
226 | static void acq_send_measurement(struct sr_dev_inst *sdi) | |
227 | { | |
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; | |
237 | ||
238 | hires_measurement = devc->base_measurement; | |
239 | if (is_highres_enabled(devc)) | |
240 | hires_measurement += devc->hires_register; | |
241 | ||
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; | |
247 | ||
248 | packet.type = SR_DF_ANALOG; | |
249 | packet.payload = &analog; | |
250 | ||
251 | sr_analog_init(&analog, &encoding, &meaning, &spec, num_digits); | |
252 | encoding.unitsize = sizeof(float); | |
253 | ||
254 | meaning.channels = sdi->channels; | |
255 | ||
256 | measurement_workaround = hires_measurement; | |
257 | analog.num_samples = 1; | |
258 | analog.data = &measurement_workaround; | |
259 | ||
260 | meaning.mq = devc->measurement_mq; | |
261 | meaning.unit = devc->measurement_unit; | |
262 | ||
263 | sr_session_send(sdi, &packet); | |
264 | } | |
265 | ||
266 | SR_PRIV int hp_3457a_receive_data(int fd, int revents, void *cb_data) | |
267 | { | |
268 | int ret; | |
269 | struct sr_scpi_dev_inst *scpi; | |
270 | struct dev_context *devc; | |
271 | struct sr_dev_inst *sdi = cb_data; | |
272 | ||
273 | (void)fd; | |
274 | (void)revents; | |
275 | ||
276 | if (!(sdi = cb_data)) | |
277 | return TRUE; | |
278 | ||
279 | if (!(devc = sdi->priv)) | |
280 | return TRUE; | |
281 | ||
282 | scpi = sdi->conn; | |
283 | ||
284 | switch (devc->acq_state) { | |
285 | case ACQ_TRIGGERED_MEASUREMENT: | |
286 | ret = sr_scpi_get_double(scpi, NULL, &devc->base_measurement); | |
287 | if (ret != SR_OK) { | |
288 | retrigger_measurement(scpi, devc); | |
289 | return TRUE; | |
290 | } | |
291 | ||
292 | if (is_highres_enabled(devc)) | |
293 | request_hires(scpi, devc); | |
294 | else | |
295 | request_range(scpi, devc); | |
296 | ||
297 | break; | |
298 | case ACQ_REQUESTED_HIRES: | |
299 | ret = sr_scpi_get_double(scpi, NULL, &devc->hires_register); | |
300 | if (ret != SR_OK) { | |
301 | retrigger_measurement(scpi, devc); | |
302 | return TRUE; | |
303 | } | |
304 | request_range(scpi, devc); | |
305 | break; | |
306 | case ACQ_REQUESTED_RANGE: | |
307 | ret = sr_scpi_get_double(scpi, NULL, &devc->measurement_range); | |
308 | if (ret != SR_OK) { | |
309 | retrigger_measurement(scpi, devc); | |
310 | return TRUE; | |
311 | } | |
312 | devc->acq_state = ACQ_GOT_MEASUREMENT; | |
313 | break; | |
314 | default: | |
315 | return FALSE; | |
316 | } | |
317 | ||
318 | if (devc->acq_state == ACQ_GOT_MEASUREMENT) | |
319 | acq_send_measurement(sdi); | |
320 | ||
321 | if (devc->limit_samples && (devc->num_samples >= devc->limit_samples)) { | |
322 | sdi->driver->dev_acquisition_stop(sdi, cb_data); | |
323 | return FALSE; | |
324 | } | |
325 | ||
326 | /* Got more to go. */ | |
327 | if (devc->acq_state == ACQ_GOT_MEASUREMENT) { | |
328 | /* Retrigger */ | |
329 | devc->num_samples++; | |
330 | retrigger_measurement(scpi, devc); | |
331 | } | |
332 | ||
333 | return TRUE; | |
334 | } |