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