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