]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2013, 2014 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 | ||
20 | /** | |
21 | * @file | |
22 | * | |
23 | * Gossen Metrawatt Metrahit 1x/2x drivers | |
24 | * | |
25 | * @internal | |
26 | */ | |
27 | ||
28 | #include <config.h> | |
29 | #include <math.h> | |
30 | #include <string.h> | |
31 | #include "protocol.h" | |
32 | ||
33 | /* Internal Headers */ | |
34 | static guchar calc_chksum_14(guchar *dta); | |
35 | static int chk_msg14(struct sr_dev_inst *sdi); | |
36 | ||
37 | /** Set or clear flags in devc->mqflags. */ | |
38 | static void setmqf(struct dev_context *devc, uint64_t flags, gboolean set) | |
39 | { | |
40 | if (set) | |
41 | devc->mqflags |= flags; | |
42 | else | |
43 | devc->mqflags &= ~flags; | |
44 | } | |
45 | ||
46 | /** Decode current type and measured value, Metrahit 12-16. */ | |
47 | static void decode_ctmv_16(uint8_t ctmv, struct dev_context *devc) | |
48 | { | |
49 | devc->mq = 0; | |
50 | devc->unit = 0; | |
51 | devc->mqflags = 0; | |
52 | ||
53 | switch (ctmv) { | |
54 | case 0x00: /* 0000 - */ | |
55 | break; | |
56 | case 0x01: /* 0001 mV DC */ | |
57 | devc->scale1000 = -1; /* Fall through */ | |
58 | case 0x02: /* 0010 V DC */ | |
59 | case 0x03: /* 0011 V AC+DC */ | |
60 | case 0x04: /* 0100 V AC */ | |
61 | devc->mq = SR_MQ_VOLTAGE; | |
62 | devc->unit = SR_UNIT_VOLT; | |
63 | if (ctmv <= 0x03) | |
64 | devc->mqflags |= SR_MQFLAG_DC; | |
65 | if (ctmv >= 0x03) { | |
66 | devc->mqflags |= SR_MQFLAG_AC; | |
67 | if (devc->model >= METRAHIT_16S) | |
68 | devc->mqflags |= SR_MQFLAG_RMS; | |
69 | } | |
70 | break; | |
71 | case 0x05: /* 0101 Hz (15S/16S only) */ | |
72 | case 0x06: /* 0110 kHz (15S/16S only) */ | |
73 | devc->mq = SR_MQ_FREQUENCY; | |
74 | devc->unit = SR_UNIT_HERTZ; | |
75 | if (ctmv == 0x06) | |
76 | devc->scale1000 = 1; | |
77 | break; | |
78 | case 0x07: /* 0111 % (15S/16S only) */ | |
79 | devc->mq = SR_MQ_DUTY_CYCLE; | |
80 | devc->unit = SR_UNIT_PERCENTAGE; | |
81 | break; | |
82 | case 0x08: /* 1000 Diode */ | |
83 | devc->mq = SR_MQ_VOLTAGE; | |
84 | devc->unit = SR_UNIT_VOLT; | |
85 | devc->mqflags |= SR_MQFLAG_DIODE; | |
86 | break; | |
87 | case 0x09: /* 1001 Ohm, °C */ | |
88 | case 0x0a: /* 1010 kOhm */ | |
89 | case 0x0b: /* 1011 MOhm */ | |
90 | devc->mq = SR_MQ_RESISTANCE; /* Changed to temp. later if req.*/ | |
91 | devc->unit = SR_UNIT_OHM; | |
92 | devc->scale1000 = ctmv - 0x09; | |
93 | break; | |
94 | case 0x0c: /* 1100 nF (15S/16S only) */ | |
95 | case 0x0d: /* 1101 µF (15S/16S only) */ | |
96 | devc->mq = SR_MQ_CAPACITANCE; | |
97 | devc->unit = SR_UNIT_FARAD; | |
98 | devc->scale1000 = (ctmv == 0x0c) ? -3 : -2; | |
99 | break; | |
100 | case 0x0e: /* mA, µA */ | |
101 | devc->scale1000 = -1; /* Fall through. */ | |
102 | case 0x0f: /* A */ | |
103 | devc->mq = SR_MQ_CURRENT; | |
104 | devc->unit = SR_UNIT_AMPERE; | |
105 | if (devc->model == METRAHIT_16S) | |
106 | devc->mqflags |= SR_MQFLAG_RMS; | |
107 | /* 16I A only with clamp, RMS questionable. */ | |
108 | break; | |
109 | } | |
110 | } | |
111 | ||
112 | /** | |
113 | * Decode range/sign/acdc byte special chars (Metrahit 12-16). | |
114 | * | |
115 | * @param[in] rs Range and sign byte. | |
116 | */ | |
117 | static void decode_rs_16(uint8_t rs, struct dev_context *devc) | |
118 | { | |
119 | sr_spew("decode_rs_16(%d) scale = %d", rs, devc->scale); | |
120 | ||
121 | if (rs & 0x04) /* Sign */ | |
122 | devc->scale = -devc->scale; | |
123 | ||
124 | if (devc->mq == SR_MQ_CURRENT) | |
125 | devc->mqflags |= (rs & 0x08) ? SR_MQFLAG_AC : SR_MQFLAG_DC; | |
126 | ||
127 | switch (rs & 0x03) { | |
128 | case 0: | |
129 | if (devc->mq == SR_MQ_VOLTAGE) /* V */ | |
130 | devc->scale += -1; | |
131 | else if (devc->mq == SR_MQ_CURRENT) /* 000.0 µA */ | |
132 | devc->scale += -5; | |
133 | else if (devc->mq == SR_MQ_RESISTANCE) { | |
134 | if (devc->buflen >= 10) { | |
135 | /* °C with 10 byte msg type, otherwise GOhm. */ | |
136 | devc->mq = SR_MQ_TEMPERATURE; | |
137 | devc->unit = SR_UNIT_CELSIUS; | |
138 | devc->scale += -2; | |
139 | } else if (devc->scale1000 == 2) { | |
140 | /* 16I Iso 500/1000V 3 GOhm */ | |
141 | devc->scale += -1; | |
142 | } | |
143 | } | |
144 | break; | |
145 | case 1: | |
146 | devc->scale += -4; | |
147 | break; | |
148 | case 2: | |
149 | devc->scale += -3; | |
150 | break; | |
151 | case 3: | |
152 | devc->scale += -2; | |
153 | break; | |
154 | } | |
155 | } | |
156 | ||
157 | /** | |
158 | * Decode special chars, Metrahit 12-16. | |
159 | * | |
160 | * @param[in] spc Special characters 1 and 2 (s1 | (s2 << 4)). | |
161 | */ | |
162 | static void decode_spc_16(uint8_t spc, struct dev_context *devc) | |
163 | { | |
164 | /* xxxx1xxx ON */ | |
165 | /* TODO: What does that mean? Power on? The 16I sets this. */ | |
166 | /* xxxxx1xx BEEP */ | |
167 | /* xxxxxx1x Low battery */ | |
168 | /* xxxxxxx1 FUSE */ | |
169 | /* 1xxxxxxx MIN */ | |
170 | setmqf(devc, SR_MQFLAG_MIN, spc & 0x80); | |
171 | ||
172 | /* x1xxxxxx MAN */ | |
173 | setmqf(devc, SR_MQFLAG_AUTORANGE, !(spc & 0x40)); | |
174 | ||
175 | /* xx1xxxxx DATA */ | |
176 | setmqf(devc, SR_MQFLAG_HOLD, spc & 0x20); | |
177 | ||
178 | /* xxx1xxxx MAX */ | |
179 | setmqf(devc, SR_MQFLAG_MAX, spc & 0x10); | |
180 | } | |
181 | ||
182 | /** Decode current type and measured value, Metrahit 18. */ | |
183 | static void decode_ctmv_18(uint8_t ctmv, struct dev_context *devc) | |
184 | { | |
185 | devc->mq = 0; | |
186 | devc->unit = 0; | |
187 | devc->mqflags = 0; | |
188 | ||
189 | switch (ctmv) { | |
190 | case 0x00: /* 0000 - */ | |
191 | break; | |
192 | case 0x01: /* 0001 V AC */ | |
193 | case 0x02: /* 0010 V AC+DC */ | |
194 | case 0x03: /* 0011 V DC */ | |
195 | devc->mq = SR_MQ_VOLTAGE; | |
196 | devc->unit = SR_UNIT_VOLT; | |
197 | if (ctmv <= 0x02) | |
198 | devc->mqflags |= (SR_MQFLAG_AC | SR_MQFLAG_RMS); | |
199 | if (ctmv >= 0x02) | |
200 | devc->mqflags |= SR_MQFLAG_DC; | |
201 | break; | |
202 | case 0x04: /* 0100 Ohm/Ohm with buzzer */ | |
203 | devc->mq = SR_MQ_RESISTANCE; | |
204 | devc->unit = SR_UNIT_OHM; | |
205 | break; | |
206 | case 0x05: /* 0101 Diode/Diode with buzzer */ | |
207 | devc->mq = SR_MQ_VOLTAGE; | |
208 | devc->unit = SR_UNIT_VOLT; | |
209 | devc->mqflags |= SR_MQFLAG_DIODE; | |
210 | break; | |
211 | case 0x06: /* 0110 °C */ | |
212 | devc->mq = SR_MQ_TEMPERATURE; | |
213 | devc->unit = SR_UNIT_CELSIUS; | |
214 | break; | |
215 | case 0x07: /* 0111 F */ | |
216 | devc->mq = SR_MQ_CAPACITANCE; | |
217 | devc->unit = SR_UNIT_FARAD; | |
218 | break; | |
219 | case 0x08: /* 1000 mA DC */ | |
220 | case 0x09: /* 1001 A DC */ | |
221 | case 0x0a: /* 1010 mA AC+DC */ | |
222 | case 0x0b: /* 1011 A AC+DC */ | |
223 | devc->mq = SR_MQ_CURRENT; | |
224 | devc->unit = SR_UNIT_AMPERE; | |
225 | devc->mqflags |= SR_MQFLAG_DC; | |
226 | if (ctmv >= 0x0a) | |
227 | devc->mqflags |= (SR_MQFLAG_AC | SR_MQFLAG_RMS); | |
228 | if ((ctmv == 0x08) || (ctmv == 0x0a)) | |
229 | devc->scale1000 = -1; | |
230 | break; | |
231 | case 0x0c: /* 1100 Hz */ | |
232 | devc->mq = SR_MQ_FREQUENCY; | |
233 | devc->unit = SR_UNIT_HERTZ; | |
234 | break; | |
235 | case 0x0d: /* 1101 dB */ | |
236 | devc->mq = SR_MQ_VOLTAGE; | |
237 | devc->unit = SR_UNIT_DECIBEL_VOLT; | |
238 | devc->mqflags |= SR_MQFLAG_AC; /* dB available for AC only */ | |
239 | break; | |
240 | case 0x0e: /* 1110 Events AC, Events AC+DC. Actually delivers just | |
241 | * current voltage via IR, nothing more. */ | |
242 | devc->mq = SR_MQ_VOLTAGE; | |
243 | devc->unit = SR_UNIT_VOLT; | |
244 | devc->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_DC | SR_MQFLAG_RMS; | |
245 | break; | |
246 | case 0x0f: /* 1111 Clock */ | |
247 | devc->mq = SR_MQ_TIME; | |
248 | devc->unit = SR_UNIT_SECOND; | |
249 | devc->mqflags |= SR_MQFLAG_DURATION; | |
250 | break; | |
251 | } | |
252 | } | |
253 | ||
254 | /** | |
255 | * Decode range/sign/acdc byte special chars, Metrahit 18. | |
256 | * | |
257 | * @param[in] rs Range/sign byte. | |
258 | */ | |
259 | static void decode_rs_18(uint8_t rs, struct dev_context *devc) | |
260 | { | |
261 | int range; | |
262 | ||
263 | /* Sign */ | |
264 | if (((devc->scale > 0) && (rs & 0x08)) || | |
265 | ((devc->scale < 0) && !(rs & 0x08))) | |
266 | devc->scale = -devc->scale; | |
267 | ||
268 | /* Range */ | |
269 | range = rs & 0x07; | |
270 | switch (devc->mq) { | |
271 | case SR_MQ_VOLTAGE: | |
272 | if (devc->unit == SR_UNIT_DECIBEL_VOLT) { | |
273 | devc->scale += -2; | |
274 | /* | |
275 | * When entering relative mode, the device switches | |
276 | * from 10 byte to 6 byte msg format. Unfortunately | |
277 | * it switches back to 10 byte when the second value | |
278 | * is measured, so that's not sufficient to | |
279 | * identify relative mode. | |
280 | */ | |
281 | } | |
282 | else | |
283 | devc->scale += range - 5; | |
284 | break; | |
285 | case SR_MQ_CURRENT: | |
286 | devc->scale += (devc->scale1000 == -1) ? range - 5 : range - 4; | |
287 | break; | |
288 | case SR_MQ_RESISTANCE: | |
289 | devc->scale += range - 2; | |
290 | break; | |
291 | case SR_MQ_FREQUENCY: | |
292 | devc->scale += range - 2; | |
293 | break; | |
294 | case SR_MQ_TEMPERATURE: | |
295 | devc->scale += range - 2; | |
296 | break; | |
297 | case SR_MQ_CAPACITANCE: | |
298 | devc->scale += range - 13; | |
299 | break; | |
300 | /* TODO: 29S Mains measurements. */ | |
301 | default: | |
302 | /* Avoid certain compiler warnings due to (-Wswitch). */ | |
303 | break; | |
304 | } | |
305 | } | |
306 | ||
307 | /** | |
308 | * Decode special chars, Metrahit 18. | |
309 | * | |
310 | * @param[in] spc Special characters 1 and 2 (s1 | (s2 << 4)). | |
311 | */ | |
312 | static void decode_spc_18(uint8_t spc, struct dev_context *devc) | |
313 | { | |
314 | /* xxxx1xxx ZERO */ | |
315 | /* xxxxx1xx BEEP */ | |
316 | /* xxxxxx1x Low battery */ | |
317 | /* xxxxxxx1 Fuse */ | |
318 | ||
319 | if (devc->mq == SR_MQ_TIME) { | |
320 | /* xxx1xxxx Clock running: 1; stop: 0 */ | |
321 | sr_spew("Clock running: %d", spc >> 4); | |
322 | } else { | |
323 | /* 1xxxxxxx MAN */ | |
324 | setmqf(devc, SR_MQFLAG_AUTORANGE, !(spc & 0x80)); | |
325 | ||
326 | /* x1xxxxxx MIN */ | |
327 | setmqf(devc, SR_MQFLAG_MIN, spc & 0x40); | |
328 | ||
329 | /* xx1xxxxx MAX */ | |
330 | setmqf(devc, SR_MQFLAG_MAX, spc & 0x20); | |
331 | ||
332 | /* xxx1xxxx DATA */ | |
333 | setmqf(devc, SR_MQFLAG_HOLD, spc & 0x10); | |
334 | } | |
335 | } | |
336 | ||
337 | /** | |
338 | * Decode current type and measured value, Metrahit 2x. | |
339 | * | |
340 | * @param[in] ctmv Current type and measured value (v1 | (v2 << 4)). | |
341 | */ | |
342 | static void decode_ctmv_2x(uint8_t ctmv, struct dev_context *devc) | |
343 | { | |
344 | if ((ctmv > 0x20) || (!devc)) { | |
345 | sr_err("decode_ctmv_2x(0x%x): invalid param(s)!", ctmv); | |
346 | return; | |
347 | } | |
348 | ||
349 | devc->mq = 0; | |
350 | devc->unit = 0; | |
351 | devc->mqflags = 0; | |
352 | ||
353 | switch (ctmv) { | |
354 | /* 00000 unused */ | |
355 | case 0x01: /* 00001 V DC */ | |
356 | case 0x02: /* 00010 V AC+DC */ | |
357 | case 0x03: /* 00011 V AC */ | |
358 | devc->mq = SR_MQ_VOLTAGE; | |
359 | devc->unit = SR_UNIT_VOLT; | |
360 | if (ctmv <= 0x02) | |
361 | devc->mqflags |= SR_MQFLAG_DC; | |
362 | if (ctmv >= 0x02) { | |
363 | devc->mqflags |= SR_MQFLAG_AC; | |
364 | if (devc->model >= METRAHIT_24S) | |
365 | devc->mqflags |= SR_MQFLAG_RMS; | |
366 | } | |
367 | break; | |
368 | case 0x04: /* 00100 mA DC */ | |
369 | case 0x05: /* 00101 mA AC+DC */ | |
370 | devc->scale1000 = -1; | |
371 | /* Fall through! */ | |
372 | case 0x06: /* 00110 A DC */ | |
373 | case 0x07: /* 00111 A AC+DC */ | |
374 | devc->mq = SR_MQ_CURRENT; | |
375 | devc->unit = SR_UNIT_AMPERE; | |
376 | devc->mqflags |= SR_MQFLAG_DC; | |
377 | if ((ctmv == 0x05) || (ctmv == 0x07)) { | |
378 | devc->mqflags |= SR_MQFLAG_AC; | |
379 | if (devc->model >= METRAHIT_24S) | |
380 | devc->mqflags |= SR_MQFLAG_RMS; | |
381 | } | |
382 | break; | |
383 | case 0x08: /* 01000 Ohm */ | |
384 | devc->mq = SR_MQ_RESISTANCE; | |
385 | devc->unit = SR_UNIT_OHM; | |
386 | break; | |
387 | case 0x09: /* 01001 F */ | |
388 | devc->mq = SR_MQ_CAPACITANCE; | |
389 | devc->unit = SR_UNIT_FARAD; | |
390 | devc->scale += -1; | |
391 | break; | |
392 | case 0x0a: /* 01010 V dB */ | |
393 | devc->mq = SR_MQ_VOLTAGE; | |
394 | devc->unit = SR_UNIT_DECIBEL_VOLT; | |
395 | devc->mqflags |= SR_MQFLAG_AC; | |
396 | if (devc->model >= METRAHIT_24S) | |
397 | devc->mqflags |= SR_MQFLAG_RMS; | |
398 | break; | |
399 | case 0x0b: /* 01011 Hz U ACDC */ | |
400 | case 0x0c: /* 01100 Hz U AC */ | |
401 | devc->mq = SR_MQ_FREQUENCY; | |
402 | devc->unit = SR_UNIT_HERTZ; | |
403 | devc->mqflags |= SR_MQFLAG_AC; | |
404 | if (ctmv <= 0x0b) | |
405 | devc->mqflags |= SR_MQFLAG_DC; | |
406 | break; | |
407 | case 0x0d: /* 01101 W on power, mA range (29S only) */ | |
408 | devc->scale += -1; | |
409 | /* Fall through! */ | |
410 | case 0x0e: /* 01110 W on power, A range (29S only) */ | |
411 | devc->scale += -1; | |
412 | devc->scale1000 = -1; | |
413 | devc->mq = SR_MQ_POWER; | |
414 | devc->unit = SR_UNIT_WATT; | |
415 | devc->mqflags |= (SR_MQFLAG_AC | SR_MQFLAG_DC | SR_MQFLAG_RMS); | |
416 | break; | |
417 | case 0x0f: /* 01111 Diode */ | |
418 | case 0x10: /* 10000 Diode with buzzer (actually cont. with voltage) */ | |
419 | devc->unit = SR_UNIT_VOLT; | |
420 | if (ctmv == 0x0f) { | |
421 | devc->mq = SR_MQ_VOLTAGE; | |
422 | devc->mqflags |= SR_MQFLAG_DIODE; | |
423 | } else { | |
424 | devc->mq = SR_MQ_CONTINUITY; | |
425 | devc->scale += -5; | |
426 | } | |
427 | devc->unit = SR_UNIT_VOLT; | |
428 | break; | |
429 | case 0x11: /* 10001 Ohm with buzzer */ | |
430 | devc->mq = SR_MQ_CONTINUITY; | |
431 | devc->unit = SR_UNIT_OHM; | |
432 | devc->scale1000 = -1; | |
433 | break; | |
434 | case 0x12: /* 10010 Temperature */ | |
435 | devc->mq = SR_MQ_TEMPERATURE; | |
436 | devc->unit = SR_UNIT_CELSIUS; | |
437 | /* This can be Fahrenheit. That is detected by range=4 later. */ | |
438 | break; | |
439 | /* 0x13 10011 unused */ | |
440 | /* 0x14 10100 unused */ | |
441 | case 0x15: /* 10101 Press (29S only) */ | |
442 | /* TODO: What does that mean? Possibly phase shift? | |
443 | Then we need a unit/flag for it. */ | |
444 | devc->mq = SR_MQ_GAIN; | |
445 | devc->unit = SR_UNIT_PERCENTAGE; | |
446 | break; | |
447 | case 0x16: /* 10110 Pulse W (29S only) */ | |
448 | /* TODO: Own unit and flag for this! */ | |
449 | devc->mq = SR_MQ_POWER; | |
450 | devc->unit = SR_UNIT_WATT; | |
451 | break; | |
452 | case 0x17: /* 10111 TRMS V on mains (29S only) */ | |
453 | devc->mq = SR_MQ_VOLTAGE; | |
454 | devc->unit = SR_UNIT_VOLT; | |
455 | devc->mqflags |= (SR_MQFLAG_AC | SR_MQFLAG_DC | SR_MQFLAG_RMS); | |
456 | break; | |
457 | case 0x18: /* 11000 Counter (zero crossings of a signal) */ | |
458 | devc->mq = SR_MQ_VOLTAGE; | |
459 | devc->unit = SR_UNIT_UNITLESS; | |
460 | break; | |
461 | case 0x19: /* 11001 Events U ACDC */ | |
462 | case 0x1a: /* 11010 Events U AC */ | |
463 | /* TODO: No unit or flags for this yet! */ | |
464 | devc->mq = SR_MQ_VOLTAGE; | |
465 | devc->unit = SR_UNIT_UNITLESS; | |
466 | devc->mqflags |= SR_MQFLAG_AC; | |
467 | if (ctmv <= 0x19) | |
468 | devc->mqflags |= SR_MQFLAG_DC; | |
469 | break; | |
470 | case 0x1b: /* 11011 Milliamperes in power mode (29S only); error in docs, "pulse on mains" */ | |
471 | devc->mq = SR_MQ_CURRENT; | |
472 | devc->unit = SR_UNIT_AMPERE; | |
473 | devc->mqflags |= (SR_MQFLAG_AC | SR_MQFLAG_DC | SR_MQFLAG_RMS); | |
474 | devc->scale1000 = -1; | |
475 | break; | |
476 | case 0x1c: /* 11100 dropout on mains (29S only) */ | |
477 | /* TODO: No unit or flags for this yet! */ | |
478 | devc->mq = SR_MQ_VOLTAGE; | |
479 | devc->unit = SR_UNIT_UNITLESS; | |
480 | devc->mqflags |= SR_MQFLAG_AC; | |
481 | break; | |
482 | case 0x1d: /* 11101 Voltage in power mode (29S); undocumented! */ | |
483 | devc->mq = SR_MQ_VOLTAGE; | |
484 | devc->unit = SR_UNIT_VOLT; | |
485 | devc->mqflags |= (SR_MQFLAG_AC | SR_MQFLAG_DC | SR_MQFLAG_RMS); | |
486 | break; | |
487 | /* 0x1e: 11110 Undocumented */ | |
488 | case 0x1f: /* 11111 25S in stopwatch mode; undocumented! | |
489 | The value is voltage, not time, so treat it such. */ | |
490 | devc->mq = SR_MQ_VOLTAGE; | |
491 | devc->unit = SR_UNIT_VOLT; | |
492 | devc->mqflags |= SR_MQFLAG_DC; | |
493 | break; | |
494 | case 0x20: /* 100000 25S in event count mode; undocumented! | |
495 | Value is 0 anyway. */ | |
496 | devc->mq = SR_MQ_VOLTAGE; | |
497 | devc->unit = SR_UNIT_UNITLESS; | |
498 | break; | |
499 | default: | |
500 | sr_err("decode_ctmv_2x(%d, ...): Unknown ctmv!", ctmv); | |
501 | break; | |
502 | } | |
503 | } | |
504 | ||
505 | /** | |
506 | * Decode range/sign/acdc byte special chars, Metrahit 2x, table TR. | |
507 | * | |
508 | * @param[in] rs Range/sign byte. | |
509 | */ | |
510 | static void decode_rs_2x(uint8_t rs, struct dev_context *devc) | |
511 | { | |
512 | int range; | |
513 | ||
514 | /* Sign */ | |
515 | if (((devc->scale > 0) && (rs & 0x08)) || | |
516 | ((devc->scale < 0) && !(rs & 0x08))) | |
517 | devc->scale = -devc->scale; | |
518 | ||
519 | /* Range */ | |
520 | range = rs & 0x07; | |
521 | switch (devc->mq) { | |
522 | case SR_MQ_VOLTAGE: | |
523 | devc->scale += (devc->unit == SR_UNIT_DECIBEL_VOLT) ? -3 : range - 6; | |
524 | break; | |
525 | case SR_MQ_CURRENT: | |
526 | if (devc->scale1000 != -1) /* uA, mA */ | |
527 | range += 1;/* mA and A ranges differ by 10^4, not 10^3!*/ | |
528 | devc->scale += range - 6; | |
529 | break; | |
530 | case SR_MQ_RESISTANCE: | |
531 | devc->scale += range - 3; | |
532 | break; | |
533 | case SR_MQ_FREQUENCY: | |
534 | devc->scale += range - 3; | |
535 | break; | |
536 | case SR_MQ_TEMPERATURE: | |
537 | if (range == 4) /* Indicator for °F */ | |
538 | devc->unit = SR_UNIT_FAHRENHEIT; | |
539 | devc->scale += -2; | |
540 | break; | |
541 | case SR_MQ_CAPACITANCE: | |
542 | if (range == 7) | |
543 | range -= 1; /* Same value as range 6 */ | |
544 | devc->scale += range - 13; | |
545 | break; | |
546 | /* TODO: 29S Mains measurements. */ | |
547 | default: | |
548 | /* Avoid certain compiler warnings due to (-Wswitch). */ | |
549 | break; | |
550 | } | |
551 | } | |
552 | ||
553 | /** | |
554 | * Decode range/sign/acdc byte special chars, Metrahit 2x, table TR 2. | |
555 | * | |
556 | * @param[in] rs Range/sign byte. | |
557 | */ | |
558 | static void decode_rs_2x_TR2(uint8_t rs, struct dev_context *devc) | |
559 | { | |
560 | int range; | |
561 | ||
562 | /* Range */ | |
563 | range = rs & 0x07; | |
564 | switch (devc->mq) { | |
565 | case SR_MQ_CURRENT: | |
566 | if (devc->scale1000 == -1) /* mA */ | |
567 | switch (range) { | |
568 | case 0: | |
569 | case 1: /* 100, 300 µA */ | |
570 | devc->scale += -6; | |
571 | break; | |
572 | case 2: | |
573 | case 3: /* 1, 3 mA */ | |
574 | devc->scale += -5; | |
575 | break; | |
576 | case 4: | |
577 | case 5: /* 10, 30 mA */ | |
578 | devc->scale += -4; | |
579 | break; | |
580 | case 6: | |
581 | case 7: /* 100, 300 mA */ | |
582 | devc->scale += -3; | |
583 | break; | |
584 | } | |
585 | else /* A */ | |
586 | switch (range) { | |
587 | case 0: | |
588 | case 1: /* 1, 3 A */ | |
589 | devc->scale += -5; | |
590 | break; | |
591 | case 2: /* 10 A */ | |
592 | devc->scale += -4; | |
593 | break; | |
594 | } | |
595 | break; | |
596 | default: | |
597 | decode_rs_2x(rs, devc); | |
598 | return; | |
599 | } | |
600 | ||
601 | /* Sign */ | |
602 | if (((devc->scale > 0) && (rs & 0x08)) || | |
603 | ((devc->scale < 0) && !(rs & 0x08))) | |
604 | devc->scale = -devc->scale; | |
605 | } | |
606 | ||
607 | /** | |
608 | * Decode special chars (Metrahit 2x). | |
609 | * | |
610 | * @param[in] spc Special characters 1 and 2 (s1 | (s2 << 4)). | |
611 | */ | |
612 | static void decode_spc_2x(uint8_t spc, struct dev_context *devc) | |
613 | { | |
614 | /* xxxxxxx1 Fuse */ | |
615 | ||
616 | /* xxxxxx1x Low battery */ | |
617 | ||
618 | /* xxxxx1xx BEEP */ | |
619 | ||
620 | /* xxxx1xxx ZERO */ | |
621 | ||
622 | /* xxx1xxxx DATA */ | |
623 | setmqf(devc, SR_MQFLAG_HOLD, spc & 0x10); | |
624 | ||
625 | /* x11xxxxx unused */ | |
626 | /* 1xxxxxxx MAN */ | |
627 | setmqf(devc, SR_MQFLAG_AUTORANGE, !(spc & 0x80)); | |
628 | } | |
629 | ||
630 | /** Clean range and sign. */ | |
631 | static void clean_rs_v(struct dev_context *devc) | |
632 | { | |
633 | devc->value = 0.0; | |
634 | devc->scale = 0; | |
635 | } | |
636 | ||
637 | /** Clean current type, measured variable, range and sign. */ | |
638 | static void clean_ctmv_rs_v(struct dev_context *devc) | |
639 | { | |
640 | devc->mq = 0; | |
641 | devc->unit = 0; | |
642 | devc->mqflags = 0; | |
643 | devc->scale1000 = 0; | |
644 | clean_rs_v(devc); | |
645 | } | |
646 | ||
647 | /** Send prepared value. */ | |
648 | static void send_value(struct sr_dev_inst *sdi) | |
649 | { | |
650 | struct dev_context *devc; | |
651 | struct sr_datafeed_analog analog; | |
652 | struct sr_analog_encoding encoding; | |
653 | struct sr_analog_meaning meaning; | |
654 | struct sr_analog_spec spec; | |
655 | struct sr_datafeed_packet packet; | |
656 | int digits; | |
657 | ||
658 | devc = sdi->priv; | |
659 | ||
660 | digits = -(devc->scale + 3 * devc->scale1000); | |
661 | sr_analog_init(&analog, &encoding, &meaning, &spec, digits); | |
662 | analog.meaning->channels = sdi->channels; | |
663 | analog.num_samples = 1; | |
664 | analog.meaning->mq = devc->mq; | |
665 | analog.meaning->unit = devc->unit; | |
666 | analog.meaning->mqflags = devc->mqflags; | |
667 | analog.data = &devc->value; | |
668 | ||
669 | memset(&packet, 0, sizeof(struct sr_datafeed_packet)); | |
670 | packet.type = SR_DF_ANALOG; | |
671 | packet.payload = &analog; | |
672 | sr_session_send(sdi, &packet); | |
673 | ||
674 | sr_sw_limits_update_samples_read(&devc->limits, 1); | |
675 | } | |
676 | ||
677 | /** Process 6-byte data message, Metrahit 1x/2x send mode. */ | |
678 | static void process_msg_dta_6(struct sr_dev_inst *sdi) | |
679 | { | |
680 | struct dev_context *devc; | |
681 | int cnt; | |
682 | uint8_t dgt; | |
683 | ||
684 | devc = sdi->priv; | |
685 | clean_rs_v(devc); | |
686 | ||
687 | /* Byte 0, range and sign */ | |
688 | if (devc->model <= METRAHIT_16X) | |
689 | decode_rs_16(bc(devc->buf[0]), devc); | |
690 | else if (devc->model < METRAHIT_2X) | |
691 | decode_rs_18(bc(devc->buf[0]), devc); | |
692 | else { | |
693 | decode_rs_2x(bc(devc->buf[0]), devc); | |
694 | devc->scale += 1; /* Compensate for format having only 5 digits, decode_rs_2x() assumes 6. */ | |
695 | } | |
696 | ||
697 | /* Bytes 1-5, digits (ls first). */ | |
698 | for (cnt = 0; cnt < 5; cnt++) { | |
699 | dgt = bc(devc->buf[1 + cnt]); | |
700 | if (dgt >= 10) { | |
701 | /* 10 Overload; on model <= 16X also 11 possible. */ | |
702 | devc->value = NAN; | |
703 | devc->scale = 0; | |
704 | break; | |
705 | } | |
706 | devc->value += pow(10.0, cnt) * dgt; | |
707 | } | |
708 | ||
709 | sr_spew("process_msg_dta_6() value=%f scale=%d scale1000=%d", | |
710 | devc->value, devc->scale, devc->scale1000); | |
711 | if (devc->value != NAN) | |
712 | devc->value *= pow(10.0, devc->scale) * pow(1000.0, devc->scale1000); | |
713 | ||
714 | /* Create and send packet. */ | |
715 | send_value(sdi); | |
716 | } | |
717 | ||
718 | /** Process 5-byte info message, Metrahit 1x/2x. */ | |
719 | static void process_msg_inf_5(struct sr_dev_inst *sdi) | |
720 | { | |
721 | struct dev_context *devc; | |
722 | enum model model; | |
723 | ||
724 | devc = sdi->priv; | |
725 | ||
726 | clean_ctmv_rs_v(devc); | |
727 | ||
728 | /* Process byte 0 */ | |
729 | model = gmc_decode_model_sm(bc(devc->buf[0])); | |
730 | if (model != devc->model) { | |
731 | sr_warn("Model mismatch in data: Detected %s, now %s", | |
732 | gmc_model_str(devc->model), gmc_model_str(model)); | |
733 | } | |
734 | ||
735 | /* Process bytes 1-4 */ | |
736 | if (devc->model <= METRAHIT_16X) { | |
737 | decode_ctmv_16(bc(devc->buf[1]), devc); | |
738 | decode_spc_16(bc(devc->buf[2]) | (bc(devc->buf[3]) << 4), devc); | |
739 | decode_rs_16(bc(devc->buf[4]), devc); | |
740 | } else if (devc->model <= METRAHIT_18S) { | |
741 | decode_ctmv_18(bc(devc->buf[1]), devc); | |
742 | decode_spc_18(bc(devc->buf[2]) | (bc(devc->buf[3]) << 4), devc); | |
743 | decode_rs_18(bc(devc->buf[4]), devc); | |
744 | } else { /* Must be Metrahit 2x */ | |
745 | decode_ctmv_2x(bc(devc->buf[1]), devc); | |
746 | decode_spc_2x(bc(devc->buf[2]) | (bc(devc->buf[3]) << 4), devc); | |
747 | decode_rs_2x(bc(devc->buf[4]), devc); | |
748 | } | |
749 | } | |
750 | ||
751 | /** Process 10-byte info/data message, Metrahit 15+. */ | |
752 | static void process_msg_inf_10(struct sr_dev_inst *sdi) | |
753 | { | |
754 | struct dev_context *devc; | |
755 | int cnt; | |
756 | uint8_t dgt; | |
757 | ||
758 | devc = sdi->priv; | |
759 | ||
760 | process_msg_inf_5(sdi); | |
761 | ||
762 | /* Now decode numbers */ | |
763 | for (cnt = 0; cnt < 5; cnt++) { | |
764 | dgt = bc(devc->buf[5 + cnt]); | |
765 | if (dgt == 11) { /* Empty digit */ | |
766 | dgt = 0; | |
767 | } | |
768 | else if (dgt >= 12) { /* Overload */ | |
769 | devc->value = NAN; | |
770 | devc->scale = 0; | |
771 | break; | |
772 | } | |
773 | devc->value += pow(10.0, cnt) * dgt; | |
774 | } | |
775 | sr_spew("process_msg_inf_10() value=%f scale=%d scalet=%d", | |
776 | devc->value, devc->scale, devc->scale1000); | |
777 | ||
778 | if (devc->value != NAN) | |
779 | devc->value *= pow(10, devc->scale) * pow(1000.0, devc->scale1000); | |
780 | ||
781 | /* Create and send packet. */ | |
782 | send_value(sdi); | |
783 | } | |
784 | ||
785 | /** Decode send interval (Metrahit 2x only). */ | |
786 | static const char *decode_send_interval(uint8_t si) | |
787 | { | |
788 | switch (si) { | |
789 | case 0x00: | |
790 | return "0.05"; | |
791 | case 0x01: | |
792 | return "0.1"; | |
793 | case 0x02: | |
794 | return "0.2"; | |
795 | case 0x03: | |
796 | return "0.5"; | |
797 | case 0x04: | |
798 | return "00:01"; | |
799 | case 0x05: | |
800 | return "00:02"; | |
801 | case 0x06: | |
802 | return "00:05"; | |
803 | case 0x07: | |
804 | return "00:10"; | |
805 | case 0x08: | |
806 | return "00:20"; | |
807 | case 0x09: | |
808 | return "00:30"; | |
809 | case 0x0a: | |
810 | return "01:00"; | |
811 | case 0x0b: | |
812 | return "02:00"; | |
813 | case 0x0c: | |
814 | return "05:00"; | |
815 | case 0x0d: | |
816 | return "10:00"; | |
817 | case 0x0e: | |
818 | return "----"; | |
819 | case 0x0f: | |
820 | return "data"; | |
821 | default: | |
822 | return "Unknown value"; | |
823 | } | |
824 | } | |
825 | ||
826 | /** Process 13-byte info/data message, Metrahit 2x. */ | |
827 | static void process_msg_inf_13(struct sr_dev_inst *sdi) | |
828 | { | |
829 | struct dev_context *devc; | |
830 | enum model model; | |
831 | int cnt; | |
832 | uint8_t dgt; | |
833 | ||
834 | devc = sdi->priv; | |
835 | ||
836 | clean_ctmv_rs_v(devc); | |
837 | ||
838 | /* Byte 0, model. */ | |
839 | model = gmc_decode_model_sm(bc(devc->buf[0])); | |
840 | if (model != devc->model) { | |
841 | sr_warn("Model mismatch in data: Detected %s, now %s", | |
842 | gmc_model_str(devc->model), gmc_model_str(model)); | |
843 | } | |
844 | ||
845 | /* Bytes 1-4, 11. */ | |
846 | decode_ctmv_2x(bc(devc->buf[1]) | (bc(devc->buf[11]) << 4), devc); | |
847 | decode_spc_2x(bc(devc->buf[2]) | (bc(devc->buf[3]) << 4), devc); | |
848 | decode_rs_2x(bc(devc->buf[4]), devc); | |
849 | ||
850 | /* Bytes 5-10, digits (ls first). */ | |
851 | for (cnt = 0; cnt < 6; cnt++) { | |
852 | dgt = bc(devc->buf[5 + cnt]); | |
853 | if (dgt == 10) { /* Overload */ | |
854 | devc->value = NAN; | |
855 | devc->scale = 0; | |
856 | break; | |
857 | } | |
858 | devc->value += pow(10.0, cnt) * dgt; | |
859 | } | |
860 | sr_spew("process_msg_inf_13() value=%f scale=%d scale1000=%d mq=%d " | |
861 | "unit=%d mqflags=0x%02" PRIx64, devc->value, devc->scale, | |
862 | devc->scale1000, devc->mq, devc->unit, (uint64_t)devc->mqflags); | |
863 | if (devc->value != NAN) | |
864 | devc->value *= pow(10, devc->scale) * pow(1000.0, devc->scale1000); | |
865 | ||
866 | /* Byte 12, Send Interval */ | |
867 | sr_spew("Send interval: %s", decode_send_interval(bc(devc->buf[12]))); | |
868 | ||
869 | /* Create and send packet. */ | |
870 | send_value(sdi); | |
871 | } | |
872 | ||
873 | /** | |
874 | * Dump contents of 14-byte message. | |
875 | * | |
876 | * @param buf Pointer to array of 14 data bytes. | |
877 | * @param[in] raw Write only data bytes, no interpretation. | |
878 | */ | |
879 | static void dump_msg14(guchar *buf, gboolean raw) | |
880 | { | |
881 | if (!buf) | |
882 | return; | |
883 | ||
884 | if (raw) | |
885 | sr_spew("msg14: 0x %02x %02x %02x %02x %02x %02x %02x %02x " | |
886 | "%02x %02x %02x %02x %02x %02x", | |
887 | buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], | |
888 | buf[7], buf[8], buf[9], buf[10], buf[11], buf[12], | |
889 | buf[13]); | |
890 | else | |
891 | sr_spew("msg14: 0x a=%d c1=%02x c2=%02x cmd=%02x dta=%02x " | |
892 | "%02x %02x %02x %02x %02x %02x %02x %02x chs=%02x", | |
893 | buf[1] == 0x2b?buf[0] >> 2:buf[0] % 0x0f, buf[1], buf[2], buf[3], buf[4], buf[5], | |
894 | buf[6], buf[7], buf[8], buf[9], buf[10], buf[11], | |
895 | buf[12], buf[13]); | |
896 | } | |
897 | ||
898 | /** | |
899 | * Calc checksum for 14 byte message type. | |
900 | * | |
901 | * @param[in] dta Pointer to array of 13 data bytes. | |
902 | * @return Checksum. | |
903 | */ | |
904 | static guchar calc_chksum_14(guchar *dta) | |
905 | { | |
906 | guchar cnt, chs; | |
907 | ||
908 | for (chs = 0, cnt = 0; cnt < (GMC_REPLY_SIZE - 1); cnt++) | |
909 | chs += dta[cnt]; | |
910 | ||
911 | return (64 - chs) & MASK_6BITS; | |
912 | } | |
913 | ||
914 | /** Check 14-byte message, Metrahit 2x. */ | |
915 | static int chk_msg14(struct sr_dev_inst *sdi) | |
916 | { | |
917 | struct dev_context *devc; | |
918 | int retc; | |
919 | gboolean isreq; /* Message is request to multimeter (otherwise response) */ | |
920 | uint8_t addr; /* Adaptor address */ | |
921 | ||
922 | retc = SR_OK; | |
923 | ||
924 | /* Check parameters and message */ | |
925 | if (!sdi || !(devc = sdi->priv)) | |
926 | return SR_ERR_ARG; | |
927 | ||
928 | if (devc->buflen != 14) { | |
929 | sr_err("process_msg_14(): Msg len 14 expected!"); | |
930 | return SR_ERR_ARG; | |
931 | } | |
932 | ||
933 | isreq = devc->buf[1] == 0x2b; | |
934 | if (isreq) | |
935 | addr = devc->buf[0] >> 2; | |
936 | else | |
937 | addr = devc->buf[0] & 0x0f; | |
938 | ||
939 | if ((devc->addr != addr) && !(isreq && (addr == 0))) { | |
940 | sr_err("process_msg_14(): Address mismatch, msg for other device!"); | |
941 | retc = SR_ERR_ARG; | |
942 | } | |
943 | ||
944 | if (devc->buf[1] == 0) { /* Error msg from device! */ | |
945 | switch (devc->buf[2]) { | |
946 | case 1: /* Not used */ | |
947 | sr_err("Device: Illegal error code!"); | |
948 | break; | |
949 | case 2: /* Incorrect check sum of received block */ | |
950 | sr_err("Device: Incorrect checksum in cmd!"); | |
951 | break; | |
952 | case 3: /* Incorrect length of received block */ | |
953 | sr_err("Device: Incorrect block length in cmd!"); | |
954 | break; | |
955 | case 4: /* Incorrect 2nd or 3rd byte */ | |
956 | sr_err("Device: Incorrect byte 2 or 3 in cmd!"); | |
957 | break; | |
958 | case 5: /* Parameter out of range */ | |
959 | sr_err("Device: Parameter out of range!"); | |
960 | break; | |
961 | default: | |
962 | sr_err("Device: Unknown error code!"); | |
963 | } | |
964 | retc = SR_ERR_ARG; | |
965 | } | |
966 | else if (!isreq && ((devc->buf[1] != 0x27) || (devc->buf[2] != 0x3f))) { | |
967 | sr_err("process_msg_14(): byte 1/2 unexpected!"); | |
968 | retc = SR_ERR_ARG; | |
969 | } | |
970 | ||
971 | if (calc_chksum_14(devc->buf) != devc->buf[13]) { | |
972 | sr_err("process_msg_14(): Invalid checksum!"); | |
973 | retc = SR_ERR_ARG; | |
974 | } | |
975 | ||
976 | if (retc != SR_OK) | |
977 | dump_msg14(devc->buf, TRUE); | |
978 | ||
979 | return retc; | |
980 | } | |
981 | ||
982 | /** Check 14-byte message, Metrahit 2x. */ | |
983 | SR_PRIV int process_msg14(struct sr_dev_inst *sdi) | |
984 | { | |
985 | struct dev_context *devc; | |
986 | int retc; | |
987 | uint8_t addr; | |
988 | uint8_t cnt, dgt; | |
989 | ||
990 | if ((retc = chk_msg14(sdi)) != SR_OK) | |
991 | return retc; | |
992 | ||
993 | devc = sdi->priv; | |
994 | ||
995 | clean_ctmv_rs_v(devc); | |
996 | addr = devc->buf[0] & MASK_6BITS; | |
997 | if (addr != devc->addr) | |
998 | sr_info("Device address mismatch %d/%d!", addr, devc->addr); | |
999 | ||
1000 | switch (devc->buf[3]) { /* That's the command this reply is for */ | |
1001 | /* 0 cannot occur, the respective message is not a 14-byte message */ | |
1002 | case 1: /* Read first free and occupied address */ | |
1003 | sr_spew("Cmd %d unimplemented!", devc->buf[3]); | |
1004 | break; | |
1005 | case 2: /* Clear all RAM in multimeter */ | |
1006 | sr_spew("Cmd %d unimplemented!", devc->buf[3]); | |
1007 | break; | |
1008 | case 3: /* Read firmware version and status */ | |
1009 | sr_spew("Cmd 3, Read firmware and status"); | |
1010 | switch (devc->cmd_idx) { | |
1011 | case 0: | |
1012 | devc->fw_ver_maj = devc->buf[5]; | |
1013 | devc->fw_ver_min = devc->buf[4]; | |
1014 | sr_spew("Firmware version %d.%d", (int)devc->fw_ver_maj, (int)devc->fw_ver_min); | |
1015 | sr_spew("Rotary Switch Position (1..10): %d", (int)devc->buf[6]); | |
1016 | /** Docs say values 0..9, but that's not true */ | |
1017 | sr_spew("Measurement Function: %d ", (int)devc->buf[7]); | |
1018 | decode_ctmv_2x(devc->buf[7], devc); | |
1019 | sr_spew("Range: 0x%x", devc->buf[8]); | |
1020 | decode_rs_2x_TR2(devc->buf[8] & 0x0f, devc); /* Docs wrong, uses conversion table TR_2! */ | |
1021 | devc->autorng = (devc->buf[8] & 0x20) == 0; | |
1022 | // TODO 9, 10: 29S special functions | |
1023 | devc->ubatt = 0.1 * (float)devc->buf[11]; | |
1024 | devc->model = gmc_decode_model_bd(devc->buf[12]); | |
1025 | sr_spew("Model=%s, battery voltage=%2.1f V", gmc_model_str(devc->model), (double)devc->ubatt); | |
1026 | break; | |
1027 | case 1: | |
1028 | sr_spew("Internal version %d.%d", (int)devc->buf[5], (int)devc->buf[4]); | |
1029 | sr_spew("Comm mode: 0x%x", (int)devc->buf[6]); | |
1030 | sr_spew("Block cnt%%64: %d", (int)devc->buf[7]); | |
1031 | sr_spew("drpCi: %d drpCh: %d", (int)devc->buf[8], (int)devc->buf[9]); | |
1032 | // Semantics undocumented. Possibly Metrahit 29S dropouts stuff? | |
1033 | break; | |
1034 | default: | |
1035 | sr_spew("Cmd 3: Unknown cmd_idx=%d", devc->cmd_idx); | |
1036 | break; | |
1037 | } | |
1038 | break; | |
1039 | case 4: /* Set real time, date, sample rate, trigger, ... */ | |
1040 | sr_spew("Cmd %d unimplemented!", devc->buf[3]); | |
1041 | break; | |
1042 | case 5: /* Read real time, date, sample rate, trigger... */ | |
1043 | sr_spew("Cmd %d unimplemented!", devc->buf[3]); | |
1044 | break; | |
1045 | case 6: /* Set modes or power off */ | |
1046 | sr_spew("Cmd %d unimplemented!", devc->buf[3]); | |
1047 | break; | |
1048 | case 7: /* Set measurement function, range, autom/man. */ | |
1049 | sr_spew("Cmd %d unimplemented!", devc->buf[3]); | |
1050 | break; | |
1051 | case 8: /* Get one measurement value */ | |
1052 | sr_spew("Cmd 8, get one measurement value"); | |
1053 | sr_spew("Measurement Function: %d ", (int)devc->buf[5]); | |
1054 | decode_ctmv_2x(devc->buf[5], devc); | |
1055 | if (!(devc->buf[6] & 0x10)) /* If bit4=0, old data. */ | |
1056 | return SR_OK; | |
1057 | ||
1058 | decode_rs_2x_TR2(devc->buf[6] & 0x0f, devc); // The docs say conversion table TR_3, but that does not work | |
1059 | setmqf(devc, SR_MQFLAG_AUTORANGE, devc->autorng); | |
1060 | /* 6 digits */ | |
1061 | for (cnt = 0; cnt < 6; cnt++) { | |
1062 | dgt = bc(devc->buf[7 + cnt]); | |
1063 | if (dgt == 10) { /* Overload */ | |
1064 | devc->value = NAN; | |
1065 | devc->scale = 0; | |
1066 | break; | |
1067 | } | |
1068 | else if (dgt == 13) { /* FUSE */ | |
1069 | sr_err("FUSE!"); | |
1070 | } | |
1071 | else if (dgt == 14) { /* Function recognition mode, OPEN */ | |
1072 | sr_info("Function recognition mode, OPEN!"); | |
1073 | devc->value = NAN; | |
1074 | devc->scale = 0; | |
1075 | break; | |
1076 | } | |
1077 | devc->value += pow(10.0, cnt) * dgt; | |
1078 | } | |
1079 | sr_spew("process_msg14() value=%f scale=%d scale1000=%d mq=%d " | |
1080 | "unit=%d mqflags=0x%02" PRIx64, devc->value, devc->scale, | |
1081 | devc->scale1000, devc->mq, devc->unit, (uint64_t)devc->mqflags); | |
1082 | if (devc->value != NAN) | |
1083 | devc->value *= pow(10, devc->scale) * pow(1000.0, devc->scale1000); | |
1084 | ||
1085 | send_value(sdi); | |
1086 | ||
1087 | break; | |
1088 | default: | |
1089 | sr_spew("Unknown cmd %d!", devc->buf[3]); | |
1090 | break; | |
1091 | } | |
1092 | ||
1093 | return SR_OK; | |
1094 | } | |
1095 | ||
1096 | /** Data reception callback function. */ | |
1097 | SR_PRIV int gmc_mh_1x_2x_receive_data(int fd, int revents, void *cb_data) | |
1098 | { | |
1099 | struct sr_dev_inst *sdi; | |
1100 | struct dev_context *devc; | |
1101 | struct sr_serial_dev_inst *serial; | |
1102 | uint8_t buf, msgt; | |
1103 | int len; | |
1104 | ||
1105 | (void)fd; | |
1106 | ||
1107 | if (!(sdi = cb_data)) | |
1108 | return TRUE; | |
1109 | ||
1110 | if (!(devc = sdi->priv)) | |
1111 | return TRUE; | |
1112 | ||
1113 | serial = sdi->conn; | |
1114 | ||
1115 | if (revents == G_IO_IN) { /* Serial data arrived. */ | |
1116 | while (GMC_BUFSIZE - devc->buflen - 1 > 0) { | |
1117 | len = serial_read_nonblocking(serial, devc->buf + devc->buflen, 1); | |
1118 | if (len < 1) | |
1119 | break; | |
1120 | buf = *(devc->buf + devc->buflen); | |
1121 | sr_spew("read 0x%02x/%d/%d", buf, buf, buf & MSGC_MASK); | |
1122 | devc->buflen += len; | |
1123 | if (!devc->settings_ok) { | |
1124 | /* | |
1125 | * If no device type/settings record processed | |
1126 | * yet, wait for one. | |
1127 | */ | |
1128 | if ((devc->buf[0] & MSGID_MASK) != MSGID_INF) { | |
1129 | devc->buflen = 0; | |
1130 | continue; | |
1131 | } | |
1132 | devc->settings_ok = TRUE; | |
1133 | } | |
1134 | ||
1135 | msgt = devc->buf[0] & MSGID_MASK; | |
1136 | switch (msgt) { | |
1137 | case MSGID_INF: | |
1138 | if (devc->buflen == 13) { | |
1139 | process_msg_inf_13(sdi); | |
1140 | devc->buflen = 0; | |
1141 | continue; | |
1142 | } else if ((devc->buflen == 10) && | |
1143 | (devc->model <= METRAHIT_18S)) { | |
1144 | process_msg_inf_10(sdi); | |
1145 | devc->buflen = 0; | |
1146 | continue; | |
1147 | } | |
1148 | else if ((devc->buflen >= 5) && | |
1149 | (devc->buf[devc->buflen - 1] & | |
1150 | MSGID_MASK) != MSGID_DATA) { | |
1151 | /* | |
1152 | * Char just received is beginning | |
1153 | * of next message. | |
1154 | */ | |
1155 | process_msg_inf_5(sdi); | |
1156 | devc->buf[0] = | |
1157 | devc->buf[devc->buflen - 1]; | |
1158 | devc->buflen = 1; | |
1159 | continue; | |
1160 | } | |
1161 | break; | |
1162 | case MSGID_DTA: | |
1163 | case MSGID_D10: | |
1164 | if (devc->buflen == 6) { | |
1165 | process_msg_dta_6(sdi); | |
1166 | devc->buflen = 0; | |
1167 | } | |
1168 | break; | |
1169 | case MSGID_DATA: | |
1170 | sr_err("Comm error, unexpected data byte!"); | |
1171 | devc->buflen = 0; | |
1172 | break; | |
1173 | } | |
1174 | } | |
1175 | } | |
1176 | ||
1177 | if (sr_sw_limits_check(&devc->limits)) | |
1178 | sdi->driver->dev_acquisition_stop(sdi); | |
1179 | ||
1180 | return TRUE; | |
1181 | } | |
1182 | ||
1183 | SR_PRIV int gmc_mh_2x_receive_data(int fd, int revents, void *cb_data) | |
1184 | { | |
1185 | struct sr_dev_inst *sdi; | |
1186 | struct dev_context *devc; | |
1187 | struct sr_serial_dev_inst *serial; | |
1188 | uint8_t buf; | |
1189 | int len; | |
1190 | ||
1191 | (void)fd; | |
1192 | ||
1193 | if (!(sdi = cb_data)) | |
1194 | return TRUE; | |
1195 | ||
1196 | if (!(devc = sdi->priv)) | |
1197 | return TRUE; | |
1198 | ||
1199 | serial = sdi->conn; | |
1200 | ||
1201 | if (revents == G_IO_IN) { /* Serial data arrived. */ | |
1202 | while (GMC_BUFSIZE - devc->buflen - 1 > 0) { | |
1203 | len = serial_read_nonblocking(serial, devc->buf + devc->buflen, 1); | |
1204 | if (len < 1) | |
1205 | break; | |
1206 | buf = *(devc->buf + devc->buflen); | |
1207 | sr_spew("read 0x%02x/%d/%d", buf, buf, buf & MASK_6BITS); | |
1208 | devc->buf[devc->buflen] &= MASK_6BITS; | |
1209 | devc->buflen += len; | |
1210 | ||
1211 | if (devc->buflen == 14) { | |
1212 | devc->response_pending = FALSE; | |
1213 | sr_spew("gmc_mh_2x_receive_data processing msg"); | |
1214 | process_msg14(sdi); | |
1215 | devc->buflen = 0; | |
1216 | } | |
1217 | } | |
1218 | } | |
1219 | ||
1220 | if (sr_sw_limits_check(&devc->limits)) | |
1221 | sdi->driver->dev_acquisition_stop(sdi); | |
1222 | ||
1223 | /* Request next data set, if required */ | |
1224 | if (sdi->status == SR_ST_ACTIVE) { | |
1225 | if (devc->response_pending) { | |
1226 | gint64 elapsed_us = g_get_monotonic_time() - devc->req_sent_at; | |
1227 | if (elapsed_us > (1 * 1000 * 1000)) /* Timeout! */ | |
1228 | devc->response_pending = FALSE; | |
1229 | } | |
1230 | if (!devc->response_pending) { | |
1231 | devc->cmd_seq++; | |
1232 | if (devc->cmd_seq % 10 == 0) { | |
1233 | if (req_stat14(sdi, FALSE) != SR_OK) | |
1234 | return FALSE; | |
1235 | } | |
1236 | else if (req_meas14(sdi) != SR_OK) | |
1237 | return FALSE; | |
1238 | } | |
1239 | } | |
1240 | ||
1241 | return TRUE; | |
1242 | } | |
1243 | ||
1244 | /** | |
1245 | * Create 14 (42) byte command for Metrahit 2x multimeter in bidir mode. | |
1246 | * | |
1247 | * Actually creates 42 bytes due to the encoding method used. | |
1248 | * | |
1249 | * @param[in] addr Device address (0=adapter, 1..15 multimeter; for byte 0). | |
1250 | * @param[in] func Function code (byte 3). | |
1251 | * @param[in] params Further parameters (9 bytes) | |
1252 | * @param[out] buf Buffer to create msg in (42 bytes). | |
1253 | */ | |
1254 | static void create_cmd_14(guchar addr, guchar func, guchar *params, guchar *buf) | |
1255 | { | |
1256 | uint8_t dta[GMC_REPLY_SIZE]; /* Unencoded message */ | |
1257 | int cnt; | |
1258 | ||
1259 | if (!params || !buf) | |
1260 | return; | |
1261 | ||
1262 | /* 0: Address */ | |
1263 | dta[0] = ((addr << 2) | 0x03) & MASK_6BITS; | |
1264 | ||
1265 | /* 1-3: Set command header */ | |
1266 | dta[1] = 0x2b; | |
1267 | dta[2] = 0x3f; | |
1268 | dta[3] = func; | |
1269 | ||
1270 | /* 4-12: Copy further parameters */ | |
1271 | for (cnt = 0; cnt < 9; cnt++) | |
1272 | dta[cnt + 4] = (params[cnt] & MASK_6BITS); | |
1273 | ||
1274 | /* 13: Checksum (b complement) */ | |
1275 | dta[13] = calc_chksum_14(dta); | |
1276 | ||
1277 | /* The whole message is packed into 3 bytes per byte now (lower 6 bits only) the most | |
1278 | * peculiar way I have ever seen. Possibly to improve IR communication? */ | |
1279 | for (cnt = 0; cnt < GMC_REPLY_SIZE; cnt++) { | |
1280 | buf[(3 * cnt) + 0] = (dta[cnt] & 0x01 ? 0x0f : 0) | (dta[cnt] & 0x02 ? 0xf0 : 0); | |
1281 | buf[(3 * cnt) + 1] = (dta[cnt] & 0x04 ? 0x0f : 0) | (dta[cnt] & 0x08 ? 0xf0 : 0); | |
1282 | buf[(3 * cnt) + 2] = (dta[cnt] & 0x10 ? 0x0f : 0) | (dta[cnt] & 0x20 ? 0xf0 : 0); | |
1283 | } | |
1284 | } | |
1285 | ||
1286 | /** Request one measurement from 2x multimeter (msg 8). | |
1287 | * | |
1288 | */ | |
1289 | int req_meas14(const struct sr_dev_inst *sdi) | |
1290 | { | |
1291 | struct dev_context *devc; | |
1292 | struct sr_serial_dev_inst *serial; | |
1293 | uint8_t params[9]; | |
1294 | uint8_t msg[42]; | |
1295 | ||
1296 | if (!sdi || !(devc = sdi->priv) || !(serial = sdi->conn)) | |
1297 | return SR_ERR; | |
1298 | ||
1299 | memset(params, 0, sizeof(params)); | |
1300 | params[0] = 0; | |
1301 | devc->cmd_idx = 0; | |
1302 | create_cmd_14(devc->addr, 8, params, msg); | |
1303 | devc->req_sent_at = g_get_monotonic_time(); | |
1304 | if (serial_write_blocking(serial, msg, sizeof(msg), | |
1305 | serial_timeout(serial, sizeof(msg))) < (int)sizeof(msg)) { | |
1306 | return SR_ERR; | |
1307 | } | |
1308 | ||
1309 | devc->response_pending = TRUE; | |
1310 | ||
1311 | return SR_OK; | |
1312 | } | |
1313 | ||
1314 | /** | |
1315 | * Request status from 2x multimeter (msg 3). | |
1316 | * | |
1317 | * @param[in] power_on Try to power on powered off multimeter by sending | |
1318 | * additional messages. | |
1319 | */ | |
1320 | int req_stat14(const struct sr_dev_inst *sdi, gboolean power_on) | |
1321 | { | |
1322 | struct dev_context *devc; | |
1323 | struct sr_serial_dev_inst *serial; | |
1324 | uint8_t params[9]; | |
1325 | uint8_t msg[42]; | |
1326 | ||
1327 | if (!sdi || !(devc = sdi->priv) || !(serial = sdi->conn)) | |
1328 | return SR_ERR; | |
1329 | ||
1330 | memset(params, 0, sizeof(params)); | |
1331 | params[0] = 0; | |
1332 | devc->cmd_idx = 0; | |
1333 | create_cmd_14(devc->addr, 3, params, msg); | |
1334 | ||
1335 | if (power_on) { | |
1336 | sr_info("Write some data and wait 3s to turn on powered off device..."); | |
1337 | if (serial_write_blocking(serial, msg, sizeof(msg), | |
1338 | serial_timeout(serial, sizeof(msg))) < 0) | |
1339 | return SR_ERR; | |
1340 | g_usleep(1 * 1000 * 1000); | |
1341 | if (serial_write_blocking(serial, msg, sizeof(msg), | |
1342 | serial_timeout(serial, sizeof(msg))) < 0) | |
1343 | return SR_ERR; | |
1344 | g_usleep(1 * 1000 * 1000); | |
1345 | if (serial_write_blocking(serial, msg, sizeof(msg), | |
1346 | serial_timeout(serial, sizeof(msg))) < 0) | |
1347 | return SR_ERR; | |
1348 | g_usleep(1 * 1000 * 1000); | |
1349 | serial_flush(serial); | |
1350 | } | |
1351 | ||
1352 | /* Write message and wait for reply */ | |
1353 | devc->req_sent_at = g_get_monotonic_time(); | |
1354 | if (serial_write_blocking(serial, msg, sizeof(msg), | |
1355 | serial_timeout(serial, sizeof(msg))) < (int)sizeof(msg)) { | |
1356 | return SR_ERR; | |
1357 | } | |
1358 | ||
1359 | devc->response_pending = TRUE; | |
1360 | ||
1361 | return SR_OK; | |
1362 | } | |
1363 | ||
1364 | /** Decode model in "send mode". | |
1365 | * | |
1366 | * @param[in] mcode Model code. | |
1367 | * @return Model code. | |
1368 | */ | |
1369 | SR_PRIV int gmc_decode_model_sm(uint8_t mcode) | |
1370 | { | |
1371 | if (mcode > 0xf) { | |
1372 | sr_err("decode_model(%d): Model code 0..15 expected!", mcode); | |
1373 | return METRAHIT_NONE; | |
1374 | } | |
1375 | ||
1376 | switch (mcode) { | |
1377 | case 0x04: /* 0100b */ | |
1378 | return METRAHIT_12S; | |
1379 | case 0x08: /* 1000b */ | |
1380 | return METRAHIT_13S14A; | |
1381 | case 0x09: /* 1001b */ | |
1382 | return METRAHIT_14S; | |
1383 | case 0x0A: /* 1010b */ | |
1384 | return METRAHIT_15S; | |
1385 | case 0x0B: /* 1011b */ | |
1386 | return METRAHIT_16S; | |
1387 | case 0x06: /* 0110b (undocumented by GMC!) */ | |
1388 | return METRAHIT_16I; | |
1389 | case 0x07: /* 0111b (undocumented by GMC!) */ | |
1390 | return METRAHIT_16T; | |
1391 | case 0x0D: /* 1101b */ | |
1392 | return METRAHIT_18S; | |
1393 | case 0x02: /* 0010b */ | |
1394 | return METRAHIT_22SM; | |
1395 | case 0x03: /* 0011b */ | |
1396 | return METRAHIT_23S; | |
1397 | case 0x0F: /* 1111b */ | |
1398 | return METRAHIT_24S; | |
1399 | case 0x05: /* 0101b */ | |
1400 | return METRAHIT_25S; | |
1401 | case 0x01: /* 0001b */ | |
1402 | return METRAHIT_26SM; | |
1403 | case 0x0C: /* 1100b */ | |
1404 | return METRAHIT_28S; | |
1405 | case 0x0E: /* 1110b */ | |
1406 | return METRAHIT_29S; | |
1407 | default: | |
1408 | sr_err("Unknown model code %d!", mcode); | |
1409 | return METRAHIT_NONE; | |
1410 | } | |
1411 | } | |
1412 | ||
1413 | /** | |
1414 | * Convert GMC model code in bidirectional mode to sigrok-internal one. | |
1415 | * | |
1416 | * @param[in] mcode Model code. | |
1417 | * | |
1418 | * @return Model code. | |
1419 | */ | |
1420 | SR_PRIV int gmc_decode_model_bd(uint8_t mcode) | |
1421 | { | |
1422 | switch (mcode & 0x1f) { | |
1423 | case 2: | |
1424 | return (mcode & 0x20) ? METRAHIT_22M : METRAHIT_22S; | |
1425 | case 3: | |
1426 | return METRAHIT_23S; | |
1427 | case 4: | |
1428 | return METRAHIT_24S; | |
1429 | case 5: | |
1430 | return METRAHIT_25S; | |
1431 | case 1: | |
1432 | return (mcode & 0x20) ? METRAHIT_26M : METRAHIT_26S; | |
1433 | case 12: | |
1434 | return METRAHIT_28S; | |
1435 | case 14: | |
1436 | return METRAHIT_29S; | |
1437 | default: | |
1438 | sr_err("Unknown model code %d!", mcode); | |
1439 | return METRAHIT_NONE; | |
1440 | } | |
1441 | } | |
1442 | ||
1443 | /** | |
1444 | * Convert sigrok-internal model code to string. | |
1445 | * | |
1446 | * @param[in] mcode Model code. | |
1447 | * | |
1448 | * @return Model code string. | |
1449 | */ | |
1450 | SR_PRIV const char *gmc_model_str(enum model mcode) | |
1451 | { | |
1452 | switch (mcode) { | |
1453 | case METRAHIT_NONE: | |
1454 | return "-uninitialized model variable-"; | |
1455 | case METRAHIT_12S: | |
1456 | return "METRAHit 12S"; | |
1457 | case METRAHIT_13S14A: | |
1458 | return "METRAHit 13S/14A"; | |
1459 | case METRAHIT_14S: | |
1460 | return "METRAHit 14S"; | |
1461 | case METRAHIT_15S: | |
1462 | return "METRAHit 15S"; | |
1463 | case METRAHIT_16S: | |
1464 | return "METRAHit 16S"; | |
1465 | case METRAHIT_16I: | |
1466 | return "METRAHit 16I/16L"; | |
1467 | case METRAHIT_16T: | |
1468 | return "METRAHit 16T/16U/KMM2002"; | |
1469 | case METRAHIT_18S: | |
1470 | return "METRAHit 18S"; | |
1471 | case METRAHIT_22SM: | |
1472 | return "METRAHit 22S/M"; | |
1473 | case METRAHIT_22S: | |
1474 | return "METRAHit 22S"; | |
1475 | case METRAHIT_22M: | |
1476 | return "METRAHit 22M"; | |
1477 | case METRAHIT_23S: | |
1478 | return "METRAHit 23S"; | |
1479 | case METRAHIT_24S: | |
1480 | return "METRAHit 24S"; | |
1481 | case METRAHIT_25S: | |
1482 | return "METRAHit 25S"; | |
1483 | case METRAHIT_26SM: | |
1484 | return "METRAHit 26S/M"; | |
1485 | case METRAHIT_26S: | |
1486 | return "METRAHit 26S"; | |
1487 | case METRAHIT_26M: | |
1488 | return "METRAHit 26M"; | |
1489 | case METRAHIT_28S: | |
1490 | return "METRAHit 28S"; | |
1491 | case METRAHIT_29S: | |
1492 | return "METRAHit 29S"; | |
1493 | default: | |
1494 | return "Unknown model code"; | |
1495 | } | |
1496 | } | |
1497 | ||
1498 | /** @copydoc sr_dev_driver.config_set */ | |
1499 | SR_PRIV int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, | |
1500 | const struct sr_channel_group *cg) | |
1501 | { | |
1502 | struct dev_context *devc; | |
1503 | uint8_t params[9]; | |
1504 | uint8_t msg[42]; | |
1505 | ||
1506 | (void)cg; | |
1507 | ||
1508 | if (sdi->status != SR_ST_ACTIVE) | |
1509 | return SR_ERR_DEV_CLOSED; | |
1510 | ||
1511 | devc = sdi->priv; | |
1512 | ||
1513 | switch (key) { | |
1514 | case SR_CONF_POWER_OFF: | |
1515 | if (devc->model < METRAHIT_2X) | |
1516 | return SR_ERR_NA; | |
1517 | if (!g_variant_get_boolean(data)) | |
1518 | return SR_ERR; | |
1519 | sr_info("Powering device off."); | |
1520 | ||
1521 | memset(params, 0, sizeof(params)); | |
1522 | params[0] = 5; | |
1523 | params[1] = 5; | |
1524 | create_cmd_14(devc->addr, 6, params, msg); | |
1525 | if (serial_write_blocking(sdi->conn, msg, sizeof(msg), | |
1526 | serial_timeout(sdi->conn, sizeof(msg))) < 0) | |
1527 | return SR_ERR; | |
1528 | else | |
1529 | g_usleep(2 * 1000 * 1000); /* Wait to ensure transfer before interface switched off. */ | |
1530 | break; | |
1531 | case SR_CONF_LIMIT_SAMPLES: | |
1532 | case SR_CONF_LIMIT_MSEC: | |
1533 | return sr_sw_limits_config_set(&devc->limits, key, data); | |
1534 | default: | |
1535 | return SR_ERR_NA; | |
1536 | } | |
1537 | ||
1538 | return SR_OK; | |
1539 | } |