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