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