]> sigrok.org Git - libsigrok.git/blame - src/hardware/norma-dmm/protocol.c
norma-dmm: Remove unused version field from state struct
[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 */
5faebab2 94 struct sr_datafeed_analog_old analog;
f8e76e2e
MH
95 struct sr_datafeed_packet packet;
96
97 devc = sdi->priv;
98
07ffa5b3 99 devc->buf[LINE_LENGTH] = '\0';
f8e76e2e
MH
100
101 sr_spew("Received line '%s'.", devc->buf);
102
e790bd5c 103 /* Check line. */
07ffa5b3 104 if (strlen((const char *)devc->buf) != LINE_LENGTH) {
e790bd5c
UH
105 sr_err("line: Invalid status '%s', must be 20 hex digits.",
106 devc->buf);
f8e76e2e
MH
107 devc->buflen = 0;
108 return;
109 }
e790bd5c 110
07ffa5b3 111 for (pos = 0; pos < LINE_LENGTH; pos++) {
f8e76e2e
MH
112 if (!isxdigit(devc->buf[pos])) {
113 sr_err("line: Expected hex digit in '%s' at pos %d!",
114 devc->buf, pos);
115 devc->buflen = 0;
116 return;
117 }
e790bd5c 118 }
f8e76e2e
MH
119
120 /* Start decoding. */
121 value = 0.0;
122 scale = 1.0;
5faebab2 123 memset(&analog, 0, sizeof(struct sr_datafeed_analog_old));
f8e76e2e 124
e790bd5c
UH
125 /*
126 * The numbers are hex digits, starting from 0.
127 * 0: Keyboard status, currently not interesting.
128 * 1: Central switch status, currently not interesting.
129 * 2: Type of measured value.
130 */
f8e76e2e
MH
131 vt = xgittoint(devc->buf[2]);
132 switch (vt) {
e790bd5c
UH
133 case 0:
134 analog.mq = SR_MQ_VOLTAGE;
f8e76e2e 135 break;
e790bd5c
UH
136 case 1:
137 analog.mq = SR_MQ_CURRENT; /* 2A */
f8e76e2e 138 break;
e790bd5c
UH
139 case 2:
140 analog.mq = SR_MQ_RESISTANCE;
f8e76e2e 141 break;
e790bd5c
UH
142 case 3:
143 analog.mq = SR_MQ_CAPACITANCE;
f8e76e2e 144 break;
e790bd5c
UH
145 case 4:
146 analog.mq = SR_MQ_TEMPERATURE;
f8e76e2e 147 break;
e790bd5c
UH
148 case 5:
149 analog.mq = SR_MQ_FREQUENCY;
f8e76e2e 150 break;
e790bd5c
UH
151 case 6:
152 analog.mq = SR_MQ_CURRENT; /* 10A */
f8e76e2e 153 break;
e790bd5c
UH
154 case 7:
155 analog.mq = SR_MQ_GAIN; /* TODO: Scale factor */
f8e76e2e 156 break;
e790bd5c
UH
157 case 8:
158 analog.mq = SR_MQ_GAIN; /* Percentage */
f8e76e2e
MH
159 scale /= 100.0;
160 break;
e790bd5c
UH
161 case 9:
162 analog.mq = SR_MQ_GAIN; /* dB */
f8e76e2e
MH
163 scale /= 100.0;
164 break;
e790bd5c
UH
165 default:
166 sr_err("Unknown value type: 0x%02x.", vt);
167 break;
f8e76e2e 168 }
e790bd5c 169
f8e76e2e
MH
170 /* 3: Measurement range for measured value */
171 range = xgittoint(devc->buf[3]);
172 switch (vt) {
e790bd5c 173 case 0: /* V */
f8e76e2e
MH
174 scale *= pow(10.0, range - 5);
175 break;
e790bd5c 176 case 1: /* A */
f8e76e2e
MH
177 scale *= pow(10.0, range - 7);
178 break;
e790bd5c 179 case 2: /* Ω */
f8e76e2e
MH
180 scale *= pow(10.0, range - 2);
181 break;
e790bd5c 182 case 3: /* F */
f8e76e2e
MH
183 scale *= pow(10.0, range - 12);
184 break;
e790bd5c 185 case 4: /* °C */
f8e76e2e
MH
186 scale *= pow(10.0, range - 1);
187 break;
e790bd5c 188 case 5: /* Hz */
f8e76e2e
MH
189 scale *= pow(10.0, range - 2);
190 break;
e790bd5c 191 // No default, other value types have fixed display format.
f8e76e2e
MH
192 }
193
194 /* 5: Sign and 1st digit */
195 flags = xgittoint(devc->buf[5]);
196 value = (flags & 0x03);
e790bd5c
UH
197 if (flags & 0x04)
198 scale *= -1;
199
f8e76e2e
MH
200 /* 6-9: 2nd-4th digit */
201 for (pos = 6; pos < 10; pos++)
202 value = value * 10 + xgittoint(devc->buf[pos]);
203 value *= scale;
204
205 /* 10: Display counter */
206 mmode = xgittoint(devc->buf[10]);
207 switch (mmode) {
e790bd5c 208 case 0: /* Frequency */
f8e76e2e
MH
209 analog.unit = SR_UNIT_HERTZ;
210 break;
e790bd5c 211 case 1: /* V TRMS, only type 5 */
f8e76e2e
MH
212 analog.unit = SR_UNIT_VOLT;
213 analog.mqflags |= (SR_MQFLAG_AC | SR_MQFLAG_DC | SR_MQFLAG_RMS);
214 break;
e790bd5c 215 case 2: /* V AC */
f8e76e2e
MH
216 analog.unit = SR_UNIT_VOLT;
217 analog.mqflags |= SR_MQFLAG_AC;
e790bd5c
UH
218 if (devc->type >= 3)
219 analog.mqflags |= SR_MQFLAG_RMS;
f8e76e2e 220 break;
e790bd5c 221 case 3: /* V DC */
f8e76e2e
MH
222 analog.unit = SR_UNIT_VOLT;
223 analog.mqflags |= SR_MQFLAG_DC;
224 break;
e790bd5c 225 case 4: /* Ohm */
f8e76e2e
MH
226 analog.unit = SR_UNIT_OHM;
227 break;
e790bd5c 228 case 5: /* Continuity */
f8e76e2e
MH
229 analog.unit = SR_UNIT_BOOLEAN;
230 analog.mq = SR_MQ_CONTINUITY;
e790bd5c 231 /* TODO: Continuity handling is a bit odd in libsigrok. */
f8e76e2e
MH
232 break;
233 case 6: /* Degree Celsius */
234 analog.unit = SR_UNIT_CELSIUS;
235 break;
e790bd5c 236 case 7: /* Capacity */
f8e76e2e
MH
237 analog.unit = SR_UNIT_FARAD;
238 break;
239 case 8: /* Current DC */
240 analog.unit = SR_UNIT_AMPERE;
241 analog.mqflags |= SR_MQFLAG_DC;
242 break;
e790bd5c 243 case 9: /* Current AC */
f8e76e2e
MH
244 analog.unit = SR_UNIT_AMPERE;
245 analog.mqflags |= SR_MQFLAG_AC;
e790bd5c
UH
246 if (devc->type >= 3)
247 analog.mqflags |= SR_MQFLAG_RMS;
f8e76e2e 248 break;
e790bd5c 249 case 0xa: /* Current TRMS, only type 5 */
f8e76e2e
MH
250 analog.unit = SR_UNIT_AMPERE;
251 analog.mqflags |= (SR_MQFLAG_AC | SR_MQFLAG_DC | SR_MQFLAG_RMS);
252 break;
e790bd5c 253 case 0xb: /* Diode */
f8e76e2e
MH
254 analog.unit = SR_UNIT_VOLT;
255 analog.mqflags |= (SR_MQFLAG_DIODE | SR_MQFLAG_DC);
256 break;
257 default:
e790bd5c 258 sr_err("Unknown mmode: 0x%02x.", mmode);
f8e76e2e
MH
259 break;
260 }
261
262 /* 11: Device status */
263 devstat = xgittoint(devc->buf[11]);
264
265 switch (devstat) {
e790bd5c 266 case 1: /* Normal measurement */
f8e76e2e 267 break;
e790bd5c 268 case 2: /* Input loop (limit, reference values) */
f8e76e2e 269 break;
e790bd5c 270 case 3: /* TRANS/SENS */
f8e76e2e 271 break;
e790bd5c
UH
272 case 4: /* Error */
273 sr_err("Device error. Fuse?"); /* TODO: Really abort? */
f8e76e2e 274 devc->buflen = 0;
e790bd5c 275 return; /* Cannot continue. */
f8e76e2e 276 default:
e790bd5c 277 sr_err("Unknown device status: 0x%02x", devstat);
f8e76e2e
MH
278 break;
279 }
280
281 /* 12-19: Flags and display symbols */
282 /* 12, 13 */
283 flags = (xgittoint(devc->buf[12]) << 8) | xgittoint(devc->buf[13]);
284 /* 0x80: PRINT TODO: Stop polling when discovered? */
e790bd5c 285 /* 0x40: EXTR */
f8e76e2e 286 if (analog.mq == SR_MQ_CONTINUITY) {
e790bd5c
UH
287 if (flags & 0x20)
288 value = 1.0; /* Beep */
289 else
290 value = 0.0;
f8e76e2e
MH
291 }
292 /* 0x10: AVG */
293 /* 0x08: Diode */
294 if (flags & 0x04) /* REL */
295 analog.mqflags |= SR_MQFLAG_RELATIVE;
296 /* 0x02: SHIFT */
297 if (flags & 0x01) /* % */
298 analog.unit = SR_UNIT_PERCENTAGE;
299
300 /* 14, 15 */
301 flags = (xgittoint(devc->buf[14]) << 8) | xgittoint(devc->buf[15]);
e790bd5c 302 if (!(flags & 0x80)) /* MAN: Manual range */
f8e76e2e 303 analog.mqflags |= SR_MQFLAG_AUTORANGE;
e790bd5c 304 if (flags & 0x40) /* LOBATT1: Low battery, measurement still within specs */
f8e76e2e 305 devc->lowbatt = 1;
e790bd5c
UH
306 /* 0x20: PEAK */
307 /* 0x10: COUNT */
308 if (flags & 0x08) /* HOLD */
f8e76e2e
MH
309 analog.mqflags |= SR_MQFLAG_HOLD;
310 /* 0x04: LIMIT */
e790bd5c 311 if (flags & 0x02) /* MAX */
f8e76e2e 312 analog.mqflags |= SR_MQFLAG_MAX;
e790bd5c 313 if (flags & 0x01) /* MIN */
f8e76e2e
MH
314 analog.mqflags |= SR_MQFLAG_MIN;
315
316 /* 16, 17 */
317 flags = (xgittoint(devc->buf[16]) << 8) | xgittoint(devc->buf[17]);
318 /* 0xe0: undefined */
e790bd5c 319 if (flags & 0x10) { /* LOBATT2: Low battery, measurement inaccurate */
f8e76e2e
MH
320 devc->lowbatt = 2;
321 sr_warn("Low battery, measurement quality degraded!");
322 }
323 /* 0x08: SCALED */
f3f19d11 324 /* 0x04: RATE (=lower resolution, allows higher data rate up to 10/s. */
e790bd5c
UH
325 /* 0x02: Current clamp */
326 if (flags & 0x01) { /* dB */
327 /*
328 * TODO: The Norma has an adjustable dB reference value. If
329 * changed from default, this is not correct.
330 */
f8e76e2e
MH
331 if (analog.unit == SR_UNIT_VOLT)
332 analog.unit = SR_UNIT_DECIBEL_VOLT;
f8e76e2e
MH
333 else
334 analog.unit = SR_UNIT_UNITLESS;
335 }
336
337 /* 18, 19 */
e790bd5c 338 /* flags = (xgittoint(devc->buf[18]) << 8) | xgittoint(devc->buf[19]); */
f8e76e2e
MH
339 /* 0x80: Undefined. */
340 /* 0x40: Remote mode, keyboard locked */
341 /* 0x38: Undefined. */
e790bd5c 342 /* 0x04: MIN > MAX */
f8e76e2e
MH
343 /* 0x02: Measured value < Min */
344 /* 0x01: Measured value > Max */
345
e790bd5c 346 /* 4: Flags. Evaluating this after setting value! */
f8e76e2e 347 flags = xgittoint(devc->buf[4]);
e790bd5c 348 if (flags & 0x04) /* Invalid value */
f8e76e2e 349 value = NAN;
e790bd5c 350 else if (flags & 0x01) /* Overload */
f8e76e2e 351 value = INFINITY;
e790bd5c 352 if (flags & 0x02) { /* Duplicate value, has been sent before. */
f8e76e2e
MH
353 sr_spew("Duplicate value, dismissing!");
354 devc->buflen = 0;
355 return;
356 }
357
e790bd5c
UH
358 sr_spew("range=%d/scale=%f/value=%f", range,
359 (double)scale, (double)value);
f8e76e2e 360
e790bd5c 361 /* Finish and send packet. */
ba7dd8bb 362 analog.channels = sdi->channels;
f8e76e2e
MH
363 analog.num_samples = 1;
364 analog.data = &value;
365
d0148a50 366 memset(&packet, 0, sizeof(struct sr_datafeed_packet));
5faebab2 367 packet.type = SR_DF_ANALOG_OLD;
f8e76e2e 368 packet.payload = &analog;
695dc859 369 sr_session_send(sdi, &packet);
f8e76e2e 370
e790bd5c 371 /* Finish processing. */
37dbffd1 372 sr_sw_limits_update_samples_read(&devc->limits, 1);
f8e76e2e
MH
373 devc->buflen = 0;
374}
375
bfd48770
MH
376SR_PRIV int norma_dmm_receive_data(int fd, int revents, void *cb_data)
377{
f8e76e2e 378 struct sr_dev_inst *sdi;
bfd48770 379 struct dev_context *devc;
f8e76e2e
MH
380 struct sr_serial_dev_inst *serial;
381 int len;
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
37dbffd1 412 if (sr_sw_limits_check(&devc->limits)) {
695dc859 413 sdi->driver->dev_acquisition_stop(sdi);
37dbffd1
LPC
414 } else {
415 /* Request next package. */
a4e435eb
MH
416 if (devc->last_req_pending) {
417 gint64 elapsed_us = g_get_monotonic_time() - devc->req_sent_at;
418 if (elapsed_us > NMADMM_TIMEOUT_MS * 1000) {/* Timeout! */
419 sr_spew("Request timeout!");
420 devc->last_req_pending = FALSE;
421 }
422 }
423 if (!devc->last_req_pending) {
424 if (nma_send_req(sdi, NMADMM_REQ_STATUS, NULL) != SR_OK)
425 return FALSE;
426 }
bfd48770
MH
427 }
428
429 return TRUE;
430}