]>
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) | |
6ddedf5b | 289 | sr_spew("Power On SRQ or clear msg received"); |
d2c1730a FS |
290 | |
291 | /* Cal failed SRQ */ | |
292 | if ((sqr_byte & SRQ_CAL_FAILED) == SRQ_CAL_FAILED) | |
6ddedf5b | 293 | sr_spew("CAL failed SRQ"); |
d2c1730a FS |
294 | |
295 | /* Keyboard SRQ */ | |
296 | if ((sqr_byte & SRQ_KEYBORD) == SRQ_KEYBORD) | |
6ddedf5b | 297 | sr_spew("Keyboard SRQ"); |
d2c1730a FS |
298 | |
299 | /* Hardware error SRQ */ | |
300 | if ((sqr_byte & SRQ_HARDWARE_ERR) == SRQ_HARDWARE_ERR) | |
6ddedf5b | 301 | sr_spew("Hardware error SRQ"); |
d2c1730a FS |
302 | |
303 | /* Syntax error SRQ */ | |
304 | if ((sqr_byte & SRQ_SYNTAX_ERR) == SRQ_SYNTAX_ERR) | |
6ddedf5b | 305 | sr_spew("Syntax error SRQ"); |
d2c1730a FS |
306 | |
307 | /* Every reading is available to the bus SRQ */ | |
308 | if ((sqr_byte & SRQ_BUS_AVAIL) == SRQ_BUS_AVAIL) | |
6ddedf5b | 309 | sr_spew("Every reading is available to the bus SRQ"); |
d2c1730a FS |
310 | #endif |
311 | ||
312 | return SR_OK; | |
313 | } | |
314 | ||
315 | static int parse_error_byte(uint8_t error_byte) | |
316 | { | |
317 | int ret; | |
318 | ||
319 | ret = SR_OK; | |
320 | ||
321 | /* A/D link */ | |
322 | if ((error_byte & ERROR_AD_LINK) == ERROR_AD_LINK) { | |
6ddedf5b | 323 | sr_err("Failure in the A/D link"); |
d2c1730a FS |
324 | ret = SR_ERR; |
325 | } | |
326 | ||
327 | /* A/D Self Test */ | |
328 | if ((error_byte & ERROR_AD_SELF_TEST) == ERROR_AD_SELF_TEST) { | |
6ddedf5b | 329 | sr_err("A/D has failed its internal Self Test"); |
d2c1730a FS |
330 | ret = SR_ERR; |
331 | } | |
332 | ||
333 | /* A/D slope error */ | |
334 | if ((error_byte & ERROR_AD_SLOPE) == ERROR_AD_SLOPE) { | |
6ddedf5b | 335 | sr_err("There has been an A/D slope error"); |
d2c1730a FS |
336 | ret = SR_ERR; |
337 | } | |
338 | ||
339 | /* ROM Selt Test */ | |
340 | if ((error_byte & ERROR_ROM_SELF_TEST) == ERROR_ROM_SELF_TEST) { | |
6ddedf5b | 341 | sr_err("The ROM Self Test has failed"); |
d2c1730a FS |
342 | ret = SR_ERR; |
343 | } | |
344 | ||
345 | /* RAM Selt Test */ | |
346 | if ((error_byte & ERROR_RAM_SELF_TEST) == ERROR_RAM_SELF_TEST) { | |
6ddedf5b | 347 | sr_err("The RAM Self Test has failed"); |
d2c1730a FS |
348 | ret = SR_ERR; |
349 | } | |
350 | ||
351 | /* Selt Test */ | |
352 | if ((error_byte & ERROR_SELF_TEST) == ERROR_SELF_TEST) { | |
6ddedf5b UH |
353 | sr_err("Self Test: Any of the CAL RAM locations have bad " |
354 | "checksums, or a range with a bad checksum is selected"); | |
d2c1730a FS |
355 | ret = SR_ERR; |
356 | } | |
357 | ||
358 | return ret; | |
359 | } | |
360 | ||
361 | SR_PRIV int hp_3478a_get_status_bytes(const struct sr_dev_inst *sdi) | |
362 | { | |
363 | int ret; | |
364 | char *response; | |
365 | uint8_t function_byte, status_byte, srq_byte, error_byte; | |
366 | struct sr_scpi_dev_inst *scpi = sdi->conn; | |
367 | struct dev_context *devc = sdi->priv; | |
368 | ||
369 | ret = sr_scpi_get_string(scpi, "B", &response); | |
370 | if (ret != SR_OK) | |
371 | return ret; | |
372 | ||
373 | if (!response) | |
374 | return SR_ERR; | |
375 | ||
376 | function_byte = (uint8_t)response[0]; | |
377 | status_byte = (uint8_t)response[1]; | |
378 | srq_byte = (uint8_t)response[2]; | |
379 | error_byte = (uint8_t)response[3]; | |
380 | ||
381 | g_free(response); | |
382 | ||
383 | parse_function_byte(devc, function_byte); | |
384 | parse_status_byte(devc, status_byte); | |
385 | parse_srq_byte(srq_byte); | |
386 | ret = parse_error_byte(error_byte); | |
387 | ||
388 | return ret; | |
389 | } | |
390 | ||
391 | static void acq_send_measurement(struct sr_dev_inst *sdi) | |
392 | { | |
393 | struct sr_datafeed_packet packet; | |
394 | struct sr_datafeed_analog analog; | |
395 | struct sr_analog_encoding encoding; | |
396 | struct sr_analog_meaning meaning; | |
397 | struct sr_analog_spec spec; | |
398 | struct dev_context *devc; | |
399 | float f; | |
400 | ||
401 | devc = sdi->priv; | |
402 | ||
403 | packet.type = SR_DF_ANALOG; | |
404 | packet.payload = &analog; | |
405 | ||
406 | sr_analog_init(&analog, &encoding, &meaning, &spec, devc->enc_digits); | |
407 | ||
408 | /* TODO: Implement NAN, depending on counts, range and value. */ | |
409 | f = devc->measurement; | |
410 | analog.num_samples = 1; | |
411 | analog.data = &f; | |
412 | ||
413 | encoding.unitsize = sizeof(float); | |
414 | encoding.is_float = TRUE; | |
415 | encoding.digits = devc->enc_digits; | |
416 | ||
417 | meaning.mq = devc->measurement_mq; | |
418 | meaning.mqflags = devc->measurement_mq_flags; | |
419 | meaning.unit = devc->measurement_unit; | |
420 | meaning.channels = sdi->channels; | |
421 | ||
422 | spec.spec_digits = devc->spec_digits; | |
423 | ||
424 | sr_session_send(sdi, &packet); | |
425 | } | |
426 | ||
1d9eebf4 FS |
427 | SR_PRIV int hp_3478a_receive_data(int fd, int revents, void *cb_data) |
428 | { | |
d2c1730a FS |
429 | struct sr_scpi_dev_inst *scpi; |
430 | struct sr_dev_inst *sdi; | |
1d9eebf4 FS |
431 | struct dev_context *devc; |
432 | ||
433 | (void)fd; | |
d2c1730a | 434 | (void)revents; |
1d9eebf4 | 435 | |
d2c1730a | 436 | if (!(sdi = cb_data) || !(devc = sdi->priv)) |
1d9eebf4 FS |
437 | return TRUE; |
438 | ||
d2c1730a | 439 | scpi = sdi->conn; |
1d9eebf4 | 440 | |
d2c1730a FS |
441 | /* |
442 | * This is necessary to get the actual range for the encoding digits. | |
443 | * When SPoll is implemmented, this can be done via SPoll. | |
444 | */ | |
445 | if (hp_3478a_get_status_bytes(sdi) != SR_OK) | |
446 | return FALSE; | |
447 | ||
448 | /* | |
449 | * TODO: Implement GPIB-SPoll, to get notified by a SRQ when a new | |
450 | * measurement is available. This is necessary, because when | |
451 | * switching ranges, there could be a timeout. | |
452 | */ | |
453 | if (sr_scpi_get_double(scpi, NULL, &devc->measurement) != SR_OK) | |
454 | return FALSE; | |
455 | ||
456 | acq_send_measurement(sdi); | |
457 | sr_sw_limits_update_samples_read(&devc->limits, 1); | |
458 | ||
459 | if (sr_sw_limits_check(&devc->limits)) | |
460 | sr_dev_acquisition_stop(sdi); | |
1d9eebf4 FS |
461 | |
462 | return TRUE; | |
463 | } |