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