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