]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2017-2018 Frank Stettner <frank-stettner@gmx.net> | |
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 <stdlib.h> | |
23 | #include "scpi.h" | |
24 | #include "protocol.h" | |
25 | ||
26 | static int set_mq_volt(struct sr_scpi_dev_inst *scpi, enum sr_mqflag flags); | |
27 | static int set_mq_amp(struct sr_scpi_dev_inst *scpi, enum sr_mqflag flags); | |
28 | static int set_mq_ohm(struct sr_scpi_dev_inst *scpi, enum sr_mqflag flags); | |
29 | ||
30 | static const struct { | |
31 | enum sr_mq mq; | |
32 | int (*set_mode)(struct sr_scpi_dev_inst *scpi, enum sr_mqflag flags); | |
33 | } sr_mq_to_cmd_map[] = { | |
34 | { SR_MQ_VOLTAGE, set_mq_volt }, | |
35 | { SR_MQ_CURRENT, set_mq_amp }, | |
36 | { SR_MQ_RESISTANCE, set_mq_ohm }, | |
37 | }; | |
38 | ||
39 | static int set_mq_volt(struct sr_scpi_dev_inst *scpi, enum sr_mqflag flags) | |
40 | { | |
41 | if ((flags & SR_MQFLAG_AC) != SR_MQFLAG_AC && | |
42 | (flags & SR_MQFLAG_DC) != SR_MQFLAG_DC) | |
43 | return SR_ERR_NA; | |
44 | ||
45 | return sr_scpi_send(scpi, "%s", | |
46 | ((flags & SR_MQFLAG_AC) == SR_MQFLAG_AC) ? "F2" : "F1"); | |
47 | } | |
48 | ||
49 | static int set_mq_amp(struct sr_scpi_dev_inst *scpi, enum sr_mqflag flags) | |
50 | { | |
51 | if ((flags & SR_MQFLAG_AC) != SR_MQFLAG_AC && | |
52 | (flags & SR_MQFLAG_DC) != SR_MQFLAG_DC) | |
53 | return SR_ERR_NA; | |
54 | ||
55 | return sr_scpi_send(scpi, "%s", (flags & SR_MQFLAG_AC) ? "F6" : "F5"); | |
56 | } | |
57 | ||
58 | static int set_mq_ohm(struct sr_scpi_dev_inst *scpi, enum sr_mqflag flags) | |
59 | { | |
60 | return sr_scpi_send(scpi, "%s", | |
61 | (flags & SR_MQFLAG_FOUR_WIRE) ? "F4" : "F3"); | |
62 | } | |
63 | ||
64 | SR_PRIV int hp_3478a_set_mq(const struct sr_dev_inst *sdi, enum sr_mq mq, | |
65 | enum sr_mqflag mq_flags) | |
66 | { | |
67 | int ret; | |
68 | size_t i; | |
69 | struct sr_scpi_dev_inst *scpi = sdi->conn; | |
70 | struct dev_context *devc = sdi->priv; | |
71 | ||
72 | /* No need to send command if we're not changing measurement type. */ | |
73 | if (devc->measurement_mq == mq && | |
74 | ((devc->measurement_mq_flags & mq_flags) == mq_flags)) | |
75 | return SR_OK; | |
76 | ||
77 | for (i = 0; i < ARRAY_SIZE(sr_mq_to_cmd_map); i++) { | |
78 | if (sr_mq_to_cmd_map[i].mq != mq) | |
79 | continue; | |
80 | ||
81 | ret = sr_mq_to_cmd_map[i].set_mode(scpi, mq_flags); | |
82 | if (ret != SR_OK) | |
83 | return ret; | |
84 | ||
85 | ret = hp_3478a_get_status_bytes(sdi); | |
86 | return ret; | |
87 | } | |
88 | ||
89 | return SR_ERR_NA; | |
90 | } | |
91 | ||
92 | static int parse_range_vdc(struct dev_context *devc, uint8_t range_byte) | |
93 | { | |
94 | if ((range_byte & SB1_RANGE_BLOCK) == RANGE_VDC_30MV) | |
95 | devc->enc_digits = devc->spec_digits - 2; | |
96 | else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_VDC_300MV) | |
97 | devc->enc_digits = devc->spec_digits - 3; | |
98 | else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_VDC_3V) | |
99 | devc->enc_digits = devc->spec_digits - 1; | |
100 | else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_VDC_30V) | |
101 | devc->enc_digits = devc->spec_digits - 2; | |
102 | else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_VDC_300V) | |
103 | devc->enc_digits = devc->spec_digits - 3; | |
104 | else | |
105 | return SR_ERR_DATA; | |
106 | ||
107 | return SR_OK; | |
108 | } | |
109 | ||
110 | static int parse_range_vac(struct dev_context *devc, uint8_t range_byte) | |
111 | { | |
112 | if ((range_byte & SB1_RANGE_BLOCK) == RANGE_VAC_300MV) | |
113 | devc->enc_digits = devc->spec_digits - 3; | |
114 | else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_VAC_3V) | |
115 | devc->enc_digits = devc->spec_digits - 1; | |
116 | else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_VAC_30V) | |
117 | devc->enc_digits = devc->spec_digits - 2; | |
118 | else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_VAC_300V) | |
119 | devc->enc_digits = devc->spec_digits - 3; | |
120 | else | |
121 | return SR_ERR_DATA; | |
122 | ||
123 | return SR_OK; | |
124 | } | |
125 | ||
126 | static int parse_range_a(struct dev_context *devc, uint8_t range_byte) | |
127 | { | |
128 | if ((range_byte & SB1_RANGE_BLOCK) == RANGE_A_300MA) | |
129 | devc->enc_digits = devc->spec_digits - 3; | |
130 | else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_A_3A) | |
131 | devc->enc_digits = devc->spec_digits - 1; | |
132 | else | |
133 | return SR_ERR_DATA; | |
134 | ||
135 | return SR_OK; | |
136 | } | |
137 | ||
138 | static int parse_range_ohm(struct dev_context *devc, uint8_t range_byte) | |
139 | { | |
140 | if ((range_byte & SB1_RANGE_BLOCK) == RANGE_OHM_30R) | |
141 | devc->enc_digits = devc->spec_digits - 2; | |
142 | else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_OHM_300R) | |
143 | devc->enc_digits = devc->spec_digits - 3; | |
144 | else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_OHM_3KR) | |
145 | devc->enc_digits = devc->spec_digits - 1; | |
146 | else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_OHM_30KR) | |
147 | devc->enc_digits = devc->spec_digits - 2; | |
148 | else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_OHM_300KR) | |
149 | devc->enc_digits = devc->spec_digits - 3; | |
150 | else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_OHM_3MR) | |
151 | devc->enc_digits = devc->spec_digits - 1; | |
152 | else if ((range_byte & SB1_RANGE_BLOCK) == RANGE_OHM_30MR) | |
153 | devc->enc_digits = devc->spec_digits - 2; | |
154 | else | |
155 | return SR_ERR_DATA; | |
156 | ||
157 | return SR_OK; | |
158 | } | |
159 | ||
160 | static int parse_function_byte(struct dev_context *devc, uint8_t function_byte) | |
161 | { | |
162 | /* Digits / Resolution (spec_digits must be set before range parsing) */ | |
163 | if ((function_byte & SB1_DIGITS_BLOCK) == DIGITS_5_5) | |
164 | devc->spec_digits = 6; | |
165 | else if ((function_byte & SB1_DIGITS_BLOCK) == DIGITS_4_5) | |
166 | devc->spec_digits = 5; | |
167 | else if ((function_byte & SB1_DIGITS_BLOCK) == DIGITS_3_5) | |
168 | devc->spec_digits = 4; | |
169 | else | |
170 | return SR_ERR_DATA; | |
171 | ||
172 | /* Function + Range */ | |
173 | devc->measurement_mq_flags = 0; | |
174 | if ((function_byte & SB1_FUNCTION_BLOCK) == FUNCTION_VDC) { | |
175 | devc->measurement_mq = SR_MQ_VOLTAGE; | |
176 | devc->measurement_mq_flags |= SR_MQFLAG_DC; | |
177 | devc->measurement_unit = SR_UNIT_VOLT; | |
178 | parse_range_vdc(devc, function_byte); | |
179 | } else if ((function_byte & SB1_FUNCTION_BLOCK) == FUNCTION_VAC) { | |
180 | devc->measurement_mq = SR_MQ_VOLTAGE; | |
181 | devc->measurement_mq_flags |= SR_MQFLAG_AC | SR_MQFLAG_RMS; | |
182 | devc->measurement_unit = SR_UNIT_VOLT; | |
183 | parse_range_vac(devc, function_byte); | |
184 | } else if ((function_byte & SB1_FUNCTION_BLOCK) == FUNCTION_2WR) { | |
185 | devc->measurement_mq = SR_MQ_RESISTANCE; | |
186 | devc->measurement_unit = SR_UNIT_OHM; | |
187 | parse_range_ohm(devc, function_byte); | |
188 | } else if ((function_byte & SB1_FUNCTION_BLOCK) == FUNCTION_4WR) { | |
189 | devc->measurement_mq = SR_MQ_RESISTANCE; | |
190 | devc->measurement_mq_flags |= SR_MQFLAG_FOUR_WIRE; | |
191 | devc->measurement_unit = SR_UNIT_OHM; | |
192 | parse_range_ohm(devc, function_byte); | |
193 | } else if ((function_byte & SB1_FUNCTION_BLOCK) == FUNCTION_ADC) { | |
194 | devc->measurement_mq = SR_MQ_CURRENT; | |
195 | devc->measurement_mq_flags |= SR_MQFLAG_DC; | |
196 | devc->measurement_unit = SR_UNIT_AMPERE; | |
197 | parse_range_a(devc, function_byte); | |
198 | } else if ((function_byte & SB1_FUNCTION_BLOCK) == FUNCTION_AAC) { | |
199 | devc->measurement_mq = SR_MQ_CURRENT; | |
200 | devc->measurement_mq_flags |= SR_MQFLAG_AC | SR_MQFLAG_RMS; | |
201 | devc->measurement_unit = SR_UNIT_AMPERE; | |
202 | parse_range_a(devc, function_byte); | |
203 | } else if ((function_byte & SB1_FUNCTION_BLOCK) == FUNCTION_EXR) { | |
204 | devc->measurement_mq = SR_MQ_RESISTANCE; | |
205 | devc->measurement_unit = SR_UNIT_OHM; | |
206 | parse_range_ohm(devc, function_byte); | |
207 | } | |
208 | ||
209 | return SR_OK; | |
210 | } | |
211 | ||
212 | static int parse_status_byte(struct dev_context *devc, uint8_t status_byte) | |
213 | { | |
214 | devc->trigger = TRIGGER_UNDEFINED; | |
215 | ||
216 | /* External Trigger */ | |
217 | if ((status_byte & STATUS_EXT_TRIGGER) == STATUS_EXT_TRIGGER) | |
218 | devc->trigger = TRIGGER_EXTERNAL; | |
219 | ||
220 | /* Cal RAM */ | |
221 | if ((status_byte & STATUS_CAL_RAM) == STATUS_CAL_RAM) | |
222 | devc->calibration = TRUE; | |
223 | else | |
224 | devc->calibration = FALSE; | |
225 | ||
226 | /* Front/Rear terminals */ | |
227 | if ((status_byte & STATUS_FRONT_TERMINAL) == STATUS_FRONT_TERMINAL) | |
228 | devc->terminal = TERMINAL_FRONT; | |
229 | else | |
230 | devc->terminal = TERMINAL_REAR; | |
231 | ||
232 | /* 50Hz / 60Hz */ | |
233 | if ((status_byte & STATUS_50HZ) == STATUS_50HZ) | |
234 | devc->line = LINE_50HZ; | |
235 | else | |
236 | devc->line = LINE_60HZ; | |
237 | ||
238 | /* Auto-Zero */ | |
239 | if ((status_byte & STATUS_AUTO_ZERO) == STATUS_AUTO_ZERO) | |
240 | devc->auto_zero = TRUE; | |
241 | else | |
242 | devc->auto_zero = FALSE; | |
243 | ||
244 | /* Auto-Range */ | |
245 | if ((status_byte & STATUS_AUTO_RANGE) == STATUS_AUTO_RANGE) | |
246 | devc->measurement_mq_flags |= SR_MQFLAG_AUTORANGE; | |
247 | else | |
248 | devc->measurement_mq_flags &= ~SR_MQFLAG_AUTORANGE; | |
249 | ||
250 | /* Internal trigger */ | |
251 | if ((status_byte & STATUS_INT_TRIGGER) == STATUS_INT_TRIGGER) | |
252 | devc->trigger = TRIGGER_INTERNAL; | |
253 | ||
254 | return SR_OK; | |
255 | } | |
256 | ||
257 | static int parse_srq_byte(uint8_t sqr_byte) | |
258 | { | |
259 | (void)sqr_byte; | |
260 | ||
261 | #if 0 | |
262 | /* The ServiceReQuest register isn't used at the moment. */ | |
263 | ||
264 | /* PON SRQ */ | |
265 | if ((sqr_byte & SRQ_POWER_ON) == SRQ_POWER_ON) | |
266 | sr_spew("Power On SRQ or clear msg received"); | |
267 | ||
268 | /* Cal failed SRQ */ | |
269 | if ((sqr_byte & SRQ_CAL_FAILED) == SRQ_CAL_FAILED) | |
270 | sr_spew("CAL failed SRQ"); | |
271 | ||
272 | /* Keyboard SRQ */ | |
273 | if ((sqr_byte & SRQ_KEYBORD) == SRQ_KEYBORD) | |
274 | sr_spew("Keyboard SRQ"); | |
275 | ||
276 | /* Hardware error SRQ */ | |
277 | if ((sqr_byte & SRQ_HARDWARE_ERR) == SRQ_HARDWARE_ERR) | |
278 | sr_spew("Hardware error SRQ"); | |
279 | ||
280 | /* Syntax error SRQ */ | |
281 | if ((sqr_byte & SRQ_SYNTAX_ERR) == SRQ_SYNTAX_ERR) | |
282 | sr_spew("Syntax error SRQ"); | |
283 | ||
284 | /* Every reading is available to the bus SRQ */ | |
285 | if ((sqr_byte & SRQ_BUS_AVAIL) == SRQ_BUS_AVAIL) | |
286 | sr_spew("Every reading is available to the bus SRQ"); | |
287 | #endif | |
288 | ||
289 | return SR_OK; | |
290 | } | |
291 | ||
292 | static int parse_error_byte(uint8_t error_byte) | |
293 | { | |
294 | int ret; | |
295 | ||
296 | ret = SR_OK; | |
297 | ||
298 | /* A/D link */ | |
299 | if ((error_byte & ERROR_AD_LINK) == ERROR_AD_LINK) { | |
300 | sr_err("Failure in the A/D link"); | |
301 | ret = SR_ERR; | |
302 | } | |
303 | ||
304 | /* A/D Self Test */ | |
305 | if ((error_byte & ERROR_AD_SELF_TEST) == ERROR_AD_SELF_TEST) { | |
306 | sr_err("A/D has failed its internal Self Test"); | |
307 | ret = SR_ERR; | |
308 | } | |
309 | ||
310 | /* A/D slope error */ | |
311 | if ((error_byte & ERROR_AD_SLOPE) == ERROR_AD_SLOPE) { | |
312 | sr_err("There has been an A/D slope error"); | |
313 | ret = SR_ERR; | |
314 | } | |
315 | ||
316 | /* ROM Selt Test */ | |
317 | if ((error_byte & ERROR_ROM_SELF_TEST) == ERROR_ROM_SELF_TEST) { | |
318 | sr_err("The ROM Self Test has failed"); | |
319 | ret = SR_ERR; | |
320 | } | |
321 | ||
322 | /* RAM Selt Test */ | |
323 | if ((error_byte & ERROR_RAM_SELF_TEST) == ERROR_RAM_SELF_TEST) { | |
324 | sr_err("The RAM Self Test has failed"); | |
325 | ret = SR_ERR; | |
326 | } | |
327 | ||
328 | /* Selt Test */ | |
329 | if ((error_byte & ERROR_SELF_TEST) == ERROR_SELF_TEST) { | |
330 | sr_err("Self Test: Any of the CAL RAM locations have bad " | |
331 | "checksums, or a range with a bad checksum is selected"); | |
332 | ret = SR_ERR; | |
333 | } | |
334 | ||
335 | return ret; | |
336 | } | |
337 | ||
338 | SR_PRIV int hp_3478a_get_status_bytes(const struct sr_dev_inst *sdi) | |
339 | { | |
340 | int ret; | |
341 | char *response; | |
342 | uint8_t function_byte, status_byte, srq_byte, error_byte; | |
343 | struct sr_scpi_dev_inst *scpi = sdi->conn; | |
344 | struct dev_context *devc = sdi->priv; | |
345 | ||
346 | ret = sr_scpi_get_string(scpi, "B", &response); | |
347 | if (ret != SR_OK) | |
348 | return ret; | |
349 | ||
350 | if (!response) | |
351 | return SR_ERR; | |
352 | ||
353 | function_byte = (uint8_t)response[0]; | |
354 | status_byte = (uint8_t)response[1]; | |
355 | srq_byte = (uint8_t)response[2]; | |
356 | error_byte = (uint8_t)response[3]; | |
357 | ||
358 | g_free(response); | |
359 | ||
360 | parse_function_byte(devc, function_byte); | |
361 | parse_status_byte(devc, status_byte); | |
362 | parse_srq_byte(srq_byte); | |
363 | ret = parse_error_byte(error_byte); | |
364 | ||
365 | return ret; | |
366 | } | |
367 | ||
368 | static void acq_send_measurement(struct sr_dev_inst *sdi) | |
369 | { | |
370 | struct sr_datafeed_packet packet; | |
371 | struct sr_datafeed_analog analog; | |
372 | struct sr_analog_encoding encoding; | |
373 | struct sr_analog_meaning meaning; | |
374 | struct sr_analog_spec spec; | |
375 | struct dev_context *devc; | |
376 | float f; | |
377 | ||
378 | devc = sdi->priv; | |
379 | ||
380 | packet.type = SR_DF_ANALOG; | |
381 | packet.payload = &analog; | |
382 | ||
383 | sr_analog_init(&analog, &encoding, &meaning, &spec, devc->enc_digits); | |
384 | ||
385 | /* TODO: Implement NAN, depending on counts, range and value. */ | |
386 | f = devc->measurement; | |
387 | analog.num_samples = 1; | |
388 | analog.data = &f; | |
389 | ||
390 | encoding.unitsize = sizeof(float); | |
391 | encoding.is_float = TRUE; | |
392 | encoding.digits = devc->enc_digits; | |
393 | ||
394 | meaning.mq = devc->measurement_mq; | |
395 | meaning.mqflags = devc->measurement_mq_flags; | |
396 | meaning.unit = devc->measurement_unit; | |
397 | meaning.channels = sdi->channels; | |
398 | ||
399 | spec.spec_digits = devc->spec_digits; | |
400 | ||
401 | sr_session_send(sdi, &packet); | |
402 | } | |
403 | ||
404 | SR_PRIV int hp_3478a_receive_data(int fd, int revents, void *cb_data) | |
405 | { | |
406 | struct sr_scpi_dev_inst *scpi; | |
407 | struct sr_dev_inst *sdi; | |
408 | struct dev_context *devc; | |
409 | ||
410 | (void)fd; | |
411 | (void)revents; | |
412 | ||
413 | if (!(sdi = cb_data) || !(devc = sdi->priv)) | |
414 | return TRUE; | |
415 | ||
416 | scpi = sdi->conn; | |
417 | ||
418 | /* | |
419 | * This is necessary to get the actual range for the encoding digits. | |
420 | * When SPoll is implemmented, this can be done via SPoll. | |
421 | */ | |
422 | if (hp_3478a_get_status_bytes(sdi) != SR_OK) | |
423 | return FALSE; | |
424 | ||
425 | /* | |
426 | * TODO: Implement GPIB-SPoll, to get notified by a SRQ when a new | |
427 | * measurement is available. This is necessary, because when | |
428 | * switching ranges, there could be a timeout. | |
429 | */ | |
430 | if (sr_scpi_get_double(scpi, NULL, &devc->measurement) != SR_OK) | |
431 | return FALSE; | |
432 | ||
433 | acq_send_measurement(sdi); | |
434 | sr_sw_limits_update_samples_read(&devc->limits, 1); | |
435 | ||
436 | if (sr_sw_limits_check(&devc->limits)) | |
437 | sr_dev_acquisition_stop(sdi); | |
438 | ||
439 | return TRUE; | |
440 | } |