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