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