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