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