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