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