]>
Commit | Line | Data |
---|---|---|
bfd48770 MH |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2013 Matthias Heidbrink <m-sigrok@heidbrink.biz> | |
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 | ||
d9251a2c UH |
20 | /** |
21 | * @file | |
22 | * | |
23 | * Norma DM9x0/Siemens B102x DMMs driver. | |
24 | * | |
25 | * @internal | |
a4e435eb MH |
26 | */ |
27 | ||
6ec6c43b | 28 | #include <config.h> |
bfd48770 MH |
29 | #include "protocol.h" |
30 | ||
07ffa5b3 UH |
31 | #define LINE_LENGTH 20 |
32 | ||
e790bd5c UH |
33 | SR_PRIV const struct nmadmm_req nmadmm_requests[] = { |
34 | { NMADMM_REQ_IDN, "IDN?" }, | |
35 | { NMADMM_REQ_IDN, "STATUS?" }, | |
1b4aedc0 | 36 | ALL_ZERO |
e790bd5c UH |
37 | }; |
38 | ||
39 | static int nma_send_req(const struct sr_dev_inst *sdi, int req, char *params) | |
40 | { | |
41 | struct sr_serial_dev_inst *serial; | |
42 | struct dev_context *devc; | |
43 | char buf[NMADMM_BUFSIZE]; | |
44 | int len; | |
45 | ||
46 | if (!sdi || !(serial = sdi->conn) || !(devc = sdi->priv)) | |
47 | return SR_ERR_BUG; | |
f8e76e2e | 48 | |
e790bd5c UH |
49 | len = snprintf(buf, sizeof(buf), "%s%s\r\n", |
50 | nmadmm_requests[req].req_str, params ? params : ""); | |
f8e76e2e | 51 | |
e790bd5c | 52 | sr_spew("Sending request: '%s'.", buf); |
f8e76e2e | 53 | |
e790bd5c UH |
54 | devc->last_req = req; |
55 | devc->last_req_pending = TRUE; | |
f8e76e2e | 56 | |
d545c81c UH |
57 | if (serial_write_blocking(serial, buf, len, |
58 | serial_timeout(serial, len)) < 0) { | |
92714255 | 59 | sr_err("Unable to send request."); |
e790bd5c UH |
60 | devc->last_req_pending = FALSE; |
61 | return SR_ERR; | |
62 | } | |
63 | ||
a4e435eb MH |
64 | devc->req_sent_at = g_get_monotonic_time(); |
65 | ||
e790bd5c UH |
66 | return SR_OK; |
67 | } | |
68 | ||
69 | /** | |
70 | * Convert hexadecimal digit to int. | |
71 | * | |
72 | * @param[in] xgit Hexadecimal digit to convert. | |
73 | * @return Int value of xgit (0 on invalid xgit). | |
f8e76e2e MH |
74 | */ |
75 | SR_PRIV int xgittoint(char xgit) | |
76 | { | |
77 | if ((xgit >= '0') && (xgit <= '9')) | |
78 | return xgit - '0'; | |
79 | xgit = tolower(xgit); | |
80 | if ((xgit >= 'a') && (xgit <= 'f')) | |
81 | return xgit - 'a'; | |
82 | return 0; | |
83 | } | |
84 | ||
e790bd5c | 85 | /** |
04cb9157 | 86 | * Process received line. It consists of 20 hex digits + \\r\\n, |
e790bd5c UH |
87 | * e.g. '08100400018100400000'. |
88 | */ | |
f8e76e2e MH |
89 | static void nma_process_line(const struct sr_dev_inst *sdi) |
90 | { | |
91 | struct dev_context *devc; | |
92 | int pos, flags; | |
93 | int vt, range; /* Measurement value type, range in device format */ | |
94 | int mmode, devstat; /* Measuring mode, device status */ | |
95 | float value; /* Measured value */ | |
96 | float scale; /* Scaling factor depending on range and function */ | |
55bee166 UH |
97 | struct sr_datafeed_analog analog; |
98 | struct sr_analog_encoding encoding; | |
99 | struct sr_analog_meaning meaning; | |
100 | struct sr_analog_spec spec; | |
f8e76e2e MH |
101 | struct sr_datafeed_packet packet; |
102 | ||
103 | devc = sdi->priv; | |
104 | ||
07ffa5b3 | 105 | devc->buf[LINE_LENGTH] = '\0'; |
f8e76e2e MH |
106 | |
107 | sr_spew("Received line '%s'.", devc->buf); | |
108 | ||
e790bd5c | 109 | /* Check line. */ |
07ffa5b3 | 110 | if (strlen((const char *)devc->buf) != LINE_LENGTH) { |
e790bd5c UH |
111 | sr_err("line: Invalid status '%s', must be 20 hex digits.", |
112 | devc->buf); | |
f8e76e2e MH |
113 | devc->buflen = 0; |
114 | return; | |
115 | } | |
e790bd5c | 116 | |
07ffa5b3 | 117 | for (pos = 0; pos < LINE_LENGTH; pos++) { |
f8e76e2e MH |
118 | if (!isxdigit(devc->buf[pos])) { |
119 | sr_err("line: Expected hex digit in '%s' at pos %d!", | |
120 | devc->buf, pos); | |
121 | devc->buflen = 0; | |
122 | return; | |
123 | } | |
e790bd5c | 124 | } |
f8e76e2e MH |
125 | |
126 | /* Start decoding. */ | |
127 | value = 0.0; | |
128 | scale = 1.0; | |
7dcaddd3 UH |
129 | /* TODO: Use proper 'digits' value for this device (and its modes). */ |
130 | sr_analog_init(&analog, &encoding, &meaning, &spec, 2); | |
f8e76e2e | 131 | |
e790bd5c UH |
132 | /* |
133 | * The numbers are hex digits, starting from 0. | |
134 | * 0: Keyboard status, currently not interesting. | |
135 | * 1: Central switch status, currently not interesting. | |
136 | * 2: Type of measured value. | |
137 | */ | |
f8e76e2e MH |
138 | vt = xgittoint(devc->buf[2]); |
139 | switch (vt) { | |
e790bd5c | 140 | case 0: |
55bee166 | 141 | analog.meaning->mq = SR_MQ_VOLTAGE; |
f8e76e2e | 142 | break; |
e790bd5c | 143 | case 1: |
55bee166 | 144 | analog.meaning->mq = SR_MQ_CURRENT; /* 2A */ |
f8e76e2e | 145 | break; |
e790bd5c | 146 | case 2: |
55bee166 | 147 | analog.meaning->mq = SR_MQ_RESISTANCE; |
f8e76e2e | 148 | break; |
e790bd5c | 149 | case 3: |
55bee166 | 150 | analog.meaning->mq = SR_MQ_CAPACITANCE; |
f8e76e2e | 151 | break; |
e790bd5c | 152 | case 4: |
55bee166 | 153 | analog.meaning->mq = SR_MQ_TEMPERATURE; |
f8e76e2e | 154 | break; |
e790bd5c | 155 | case 5: |
55bee166 | 156 | analog.meaning->mq = SR_MQ_FREQUENCY; |
f8e76e2e | 157 | break; |
e790bd5c | 158 | case 6: |
55bee166 | 159 | analog.meaning->mq = SR_MQ_CURRENT; /* 10A */ |
f8e76e2e | 160 | break; |
e790bd5c | 161 | case 7: |
55bee166 | 162 | analog.meaning->mq = SR_MQ_GAIN; /* TODO: Scale factor */ |
f8e76e2e | 163 | break; |
e790bd5c | 164 | case 8: |
55bee166 | 165 | analog.meaning->mq = SR_MQ_GAIN; /* Percentage */ |
f8e76e2e MH |
166 | scale /= 100.0; |
167 | break; | |
e790bd5c | 168 | case 9: |
55bee166 | 169 | analog.meaning->mq = SR_MQ_GAIN; /* dB */ |
f8e76e2e MH |
170 | scale /= 100.0; |
171 | break; | |
e790bd5c UH |
172 | default: |
173 | sr_err("Unknown value type: 0x%02x.", vt); | |
174 | break; | |
f8e76e2e | 175 | } |
e790bd5c | 176 | |
f8e76e2e MH |
177 | /* 3: Measurement range for measured value */ |
178 | range = xgittoint(devc->buf[3]); | |
179 | switch (vt) { | |
e790bd5c | 180 | case 0: /* V */ |
f8e76e2e MH |
181 | scale *= pow(10.0, range - 5); |
182 | break; | |
e790bd5c | 183 | case 1: /* A */ |
f8e76e2e MH |
184 | scale *= pow(10.0, range - 7); |
185 | break; | |
e790bd5c | 186 | case 2: /* Ω */ |
f8e76e2e MH |
187 | scale *= pow(10.0, range - 2); |
188 | break; | |
e790bd5c | 189 | case 3: /* F */ |
f8e76e2e MH |
190 | scale *= pow(10.0, range - 12); |
191 | break; | |
e790bd5c | 192 | case 4: /* °C */ |
f8e76e2e MH |
193 | scale *= pow(10.0, range - 1); |
194 | break; | |
e790bd5c | 195 | case 5: /* Hz */ |
f8e76e2e MH |
196 | scale *= pow(10.0, range - 2); |
197 | break; | |
e790bd5c | 198 | // No default, other value types have fixed display format. |
f8e76e2e MH |
199 | } |
200 | ||
201 | /* 5: Sign and 1st digit */ | |
202 | flags = xgittoint(devc->buf[5]); | |
203 | value = (flags & 0x03); | |
e790bd5c UH |
204 | if (flags & 0x04) |
205 | scale *= -1; | |
206 | ||
f8e76e2e MH |
207 | /* 6-9: 2nd-4th digit */ |
208 | for (pos = 6; pos < 10; pos++) | |
209 | value = value * 10 + xgittoint(devc->buf[pos]); | |
210 | value *= scale; | |
211 | ||
212 | /* 10: Display counter */ | |
213 | mmode = xgittoint(devc->buf[10]); | |
214 | switch (mmode) { | |
e790bd5c | 215 | case 0: /* Frequency */ |
55bee166 | 216 | analog.meaning->unit = SR_UNIT_HERTZ; |
f8e76e2e | 217 | break; |
e790bd5c | 218 | case 1: /* V TRMS, only type 5 */ |
55bee166 UH |
219 | analog.meaning->unit = SR_UNIT_VOLT; |
220 | analog.meaning->mqflags |= (SR_MQFLAG_AC | SR_MQFLAG_DC | SR_MQFLAG_RMS); | |
f8e76e2e | 221 | break; |
e790bd5c | 222 | case 2: /* V AC */ |
55bee166 UH |
223 | analog.meaning->unit = SR_UNIT_VOLT; |
224 | analog.meaning->mqflags |= SR_MQFLAG_AC; | |
e790bd5c | 225 | if (devc->type >= 3) |
55bee166 | 226 | analog.meaning->mqflags |= SR_MQFLAG_RMS; |
f8e76e2e | 227 | break; |
e790bd5c | 228 | case 3: /* V DC */ |
55bee166 UH |
229 | analog.meaning->unit = SR_UNIT_VOLT; |
230 | analog.meaning->mqflags |= SR_MQFLAG_DC; | |
f8e76e2e | 231 | break; |
e790bd5c | 232 | case 4: /* Ohm */ |
55bee166 | 233 | analog.meaning->unit = SR_UNIT_OHM; |
f8e76e2e | 234 | break; |
e790bd5c | 235 | case 5: /* Continuity */ |
55bee166 UH |
236 | analog.meaning->unit = SR_UNIT_BOOLEAN; |
237 | analog.meaning->mq = SR_MQ_CONTINUITY; | |
e790bd5c | 238 | /* TODO: Continuity handling is a bit odd in libsigrok. */ |
f8e76e2e MH |
239 | break; |
240 | case 6: /* Degree Celsius */ | |
55bee166 | 241 | analog.meaning->unit = SR_UNIT_CELSIUS; |
f8e76e2e | 242 | break; |
e790bd5c | 243 | case 7: /* Capacity */ |
55bee166 | 244 | analog.meaning->unit = SR_UNIT_FARAD; |
f8e76e2e MH |
245 | break; |
246 | case 8: /* Current DC */ | |
55bee166 UH |
247 | analog.meaning->unit = SR_UNIT_AMPERE; |
248 | analog.meaning->mqflags |= SR_MQFLAG_DC; | |
f8e76e2e | 249 | break; |
e790bd5c | 250 | case 9: /* Current AC */ |
55bee166 UH |
251 | analog.meaning->unit = SR_UNIT_AMPERE; |
252 | analog.meaning->mqflags |= SR_MQFLAG_AC; | |
e790bd5c | 253 | if (devc->type >= 3) |
55bee166 | 254 | analog.meaning->mqflags |= SR_MQFLAG_RMS; |
f8e76e2e | 255 | break; |
e790bd5c | 256 | case 0xa: /* Current TRMS, only type 5 */ |
55bee166 UH |
257 | analog.meaning->unit = SR_UNIT_AMPERE; |
258 | analog.meaning->mqflags |= (SR_MQFLAG_AC | SR_MQFLAG_DC | SR_MQFLAG_RMS); | |
f8e76e2e | 259 | break; |
e790bd5c | 260 | case 0xb: /* Diode */ |
55bee166 UH |
261 | analog.meaning->unit = SR_UNIT_VOLT; |
262 | analog.meaning->mqflags |= (SR_MQFLAG_DIODE | SR_MQFLAG_DC); | |
f8e76e2e MH |
263 | break; |
264 | default: | |
e790bd5c | 265 | sr_err("Unknown mmode: 0x%02x.", mmode); |
f8e76e2e MH |
266 | break; |
267 | } | |
268 | ||
269 | /* 11: Device status */ | |
270 | devstat = xgittoint(devc->buf[11]); | |
271 | ||
272 | switch (devstat) { | |
e790bd5c | 273 | case 1: /* Normal measurement */ |
f8e76e2e | 274 | break; |
e790bd5c | 275 | case 2: /* Input loop (limit, reference values) */ |
f8e76e2e | 276 | break; |
e790bd5c | 277 | case 3: /* TRANS/SENS */ |
f8e76e2e | 278 | break; |
e790bd5c UH |
279 | case 4: /* Error */ |
280 | sr_err("Device error. Fuse?"); /* TODO: Really abort? */ | |
f8e76e2e | 281 | devc->buflen = 0; |
e790bd5c | 282 | return; /* Cannot continue. */ |
f8e76e2e | 283 | default: |
e790bd5c | 284 | sr_err("Unknown device status: 0x%02x", devstat); |
f8e76e2e MH |
285 | break; |
286 | } | |
287 | ||
288 | /* 12-19: Flags and display symbols */ | |
289 | /* 12, 13 */ | |
290 | flags = (xgittoint(devc->buf[12]) << 8) | xgittoint(devc->buf[13]); | |
291 | /* 0x80: PRINT TODO: Stop polling when discovered? */ | |
e790bd5c | 292 | /* 0x40: EXTR */ |
55bee166 | 293 | if (analog.meaning->mq == SR_MQ_CONTINUITY) { |
e790bd5c UH |
294 | if (flags & 0x20) |
295 | value = 1.0; /* Beep */ | |
296 | else | |
297 | value = 0.0; | |
f8e76e2e MH |
298 | } |
299 | /* 0x10: AVG */ | |
300 | /* 0x08: Diode */ | |
301 | if (flags & 0x04) /* REL */ | |
55bee166 | 302 | analog.meaning->mqflags |= SR_MQFLAG_RELATIVE; |
f8e76e2e MH |
303 | /* 0x02: SHIFT */ |
304 | if (flags & 0x01) /* % */ | |
55bee166 | 305 | analog.meaning->unit = SR_UNIT_PERCENTAGE; |
f8e76e2e MH |
306 | |
307 | /* 14, 15 */ | |
308 | flags = (xgittoint(devc->buf[14]) << 8) | xgittoint(devc->buf[15]); | |
e790bd5c | 309 | if (!(flags & 0x80)) /* MAN: Manual range */ |
55bee166 | 310 | analog.meaning->mqflags |= SR_MQFLAG_AUTORANGE; |
e790bd5c | 311 | if (flags & 0x40) /* LOBATT1: Low battery, measurement still within specs */ |
f8e76e2e | 312 | devc->lowbatt = 1; |
e790bd5c UH |
313 | /* 0x20: PEAK */ |
314 | /* 0x10: COUNT */ | |
315 | if (flags & 0x08) /* HOLD */ | |
55bee166 | 316 | analog.meaning->mqflags |= SR_MQFLAG_HOLD; |
f8e76e2e | 317 | /* 0x04: LIMIT */ |
e790bd5c | 318 | if (flags & 0x02) /* MAX */ |
55bee166 | 319 | analog.meaning->mqflags |= SR_MQFLAG_MAX; |
e790bd5c | 320 | if (flags & 0x01) /* MIN */ |
55bee166 | 321 | analog.meaning->mqflags |= SR_MQFLAG_MIN; |
f8e76e2e MH |
322 | |
323 | /* 16, 17 */ | |
324 | flags = (xgittoint(devc->buf[16]) << 8) | xgittoint(devc->buf[17]); | |
325 | /* 0xe0: undefined */ | |
e790bd5c | 326 | if (flags & 0x10) { /* LOBATT2: Low battery, measurement inaccurate */ |
f8e76e2e MH |
327 | devc->lowbatt = 2; |
328 | sr_warn("Low battery, measurement quality degraded!"); | |
329 | } | |
330 | /* 0x08: SCALED */ | |
f3f19d11 | 331 | /* 0x04: RATE (=lower resolution, allows higher data rate up to 10/s. */ |
e790bd5c UH |
332 | /* 0x02: Current clamp */ |
333 | if (flags & 0x01) { /* dB */ | |
334 | /* | |
335 | * TODO: The Norma has an adjustable dB reference value. If | |
336 | * changed from default, this is not correct. | |
337 | */ | |
55bee166 UH |
338 | if (analog.meaning->unit == SR_UNIT_VOLT) |
339 | analog.meaning->unit = SR_UNIT_DECIBEL_VOLT; | |
f8e76e2e | 340 | else |
55bee166 | 341 | analog.meaning->unit = SR_UNIT_UNITLESS; |
f8e76e2e MH |
342 | } |
343 | ||
344 | /* 18, 19 */ | |
e790bd5c | 345 | /* flags = (xgittoint(devc->buf[18]) << 8) | xgittoint(devc->buf[19]); */ |
f8e76e2e MH |
346 | /* 0x80: Undefined. */ |
347 | /* 0x40: Remote mode, keyboard locked */ | |
348 | /* 0x38: Undefined. */ | |
e790bd5c | 349 | /* 0x04: MIN > MAX */ |
f8e76e2e MH |
350 | /* 0x02: Measured value < Min */ |
351 | /* 0x01: Measured value > Max */ | |
352 | ||
e790bd5c | 353 | /* 4: Flags. Evaluating this after setting value! */ |
f8e76e2e | 354 | flags = xgittoint(devc->buf[4]); |
e790bd5c | 355 | if (flags & 0x04) /* Invalid value */ |
d9251a2c | 356 | value = NAN; |
e790bd5c | 357 | else if (flags & 0x01) /* Overload */ |
d9251a2c | 358 | value = INFINITY; |
e790bd5c | 359 | if (flags & 0x02) { /* Duplicate value, has been sent before. */ |
d9251a2c UH |
360 | sr_spew("Duplicate value, dismissing!"); |
361 | devc->buflen = 0; | |
362 | return; | |
f8e76e2e MH |
363 | } |
364 | ||
e790bd5c UH |
365 | sr_spew("range=%d/scale=%f/value=%f", range, |
366 | (double)scale, (double)value); | |
f8e76e2e | 367 | |
e790bd5c | 368 | /* Finish and send packet. */ |
55bee166 | 369 | analog.meaning->channels = sdi->channels; |
f8e76e2e MH |
370 | analog.num_samples = 1; |
371 | analog.data = &value; | |
372 | ||
d0148a50 | 373 | memset(&packet, 0, sizeof(struct sr_datafeed_packet)); |
55bee166 | 374 | packet.type = SR_DF_ANALOG; |
f8e76e2e | 375 | packet.payload = &analog; |
695dc859 | 376 | sr_session_send(sdi, &packet); |
f8e76e2e | 377 | |
e790bd5c | 378 | /* Finish processing. */ |
37dbffd1 | 379 | sr_sw_limits_update_samples_read(&devc->limits, 1); |
f8e76e2e MH |
380 | devc->buflen = 0; |
381 | } | |
382 | ||
bfd48770 MH |
383 | SR_PRIV int norma_dmm_receive_data(int fd, int revents, void *cb_data) |
384 | { | |
f8e76e2e | 385 | struct sr_dev_inst *sdi; |
bfd48770 | 386 | struct dev_context *devc; |
f8e76e2e MH |
387 | struct sr_serial_dev_inst *serial; |
388 | int len; | |
bfd48770 MH |
389 | |
390 | (void)fd; | |
391 | ||
392 | if (!(sdi = cb_data)) | |
393 | return TRUE; | |
394 | ||
395 | if (!(devc = sdi->priv)) | |
396 | return TRUE; | |
397 | ||
f8e76e2e | 398 | serial = sdi->conn; |
bfd48770 | 399 | if (revents == G_IO_IN) { |
f8e76e2e | 400 | /* Serial data arrived. */ |
e790bd5c | 401 | while (NMADMM_BUFSIZE - devc->buflen - 1 > 0) { |
32950cc0 | 402 | len = serial_read_nonblocking(serial, devc->buf + devc->buflen, 1); |
f8e76e2e MH |
403 | if (len < 1) |
404 | break; | |
405 | devc->buflen += len; | |
406 | *(devc->buf + devc->buflen) = '\0'; | |
407 | if (*(devc->buf + devc->buflen - 1) == '\n') { | |
e790bd5c UH |
408 | /* |
409 | * TODO: According to specs, should be \r, but | |
410 | * then we'd have to get rid of the \n. | |
411 | */ | |
f8e76e2e MH |
412 | devc->last_req_pending = FALSE; |
413 | nma_process_line(sdi); | |
414 | break; | |
415 | } | |
416 | } | |
417 | } | |
418 | ||
37dbffd1 | 419 | if (sr_sw_limits_check(&devc->limits)) { |
695dc859 | 420 | sdi->driver->dev_acquisition_stop(sdi); |
37dbffd1 LPC |
421 | } else { |
422 | /* Request next package. */ | |
a4e435eb MH |
423 | if (devc->last_req_pending) { |
424 | gint64 elapsed_us = g_get_monotonic_time() - devc->req_sent_at; | |
425 | if (elapsed_us > NMADMM_TIMEOUT_MS * 1000) {/* Timeout! */ | |
426 | sr_spew("Request timeout!"); | |
427 | devc->last_req_pending = FALSE; | |
428 | } | |
429 | } | |
430 | if (!devc->last_req_pending) { | |
431 | if (nma_send_req(sdi, NMADMM_REQ_STATUS, NULL) != SR_OK) | |
432 | return FALSE; | |
433 | } | |
bfd48770 MH |
434 | } |
435 | ||
436 | return TRUE; | |
437 | } |