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