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