]>
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 | 118 | { |
f4bd8a17 | 119 | sr_spew("decode_rs_16(%d) scale = %d", rs, devc->scale); |
f5792417 | 120 | |
1d4a2839 | 121 | if (rs & 0x04) /* Sign */ |
f4bd8a17 | 122 | devc->scale = -devc->scale; |
f5792417 MH |
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 */ |
f4bd8a17 | 134 | devc->scale += -1; |
f5792417 | 135 | else if (devc->mq == SR_MQ_CURRENT) /* 000.0 µA */ |
f4bd8a17 | 136 | devc->scale += -5; |
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; | |
f4bd8a17 | 142 | devc->scale += -2; |
42f2f8a5 | 143 | } else if (devc->scale1000 == 2) { |
f5792417 | 144 | /* 16I Iso 500/1000V 3 GOhm */ |
f4bd8a17 | 145 | devc->scale += -1; |
f5792417 MH |
146 | } |
147 | } | |
148 | break; | |
149 | case 1: | |
f4bd8a17 | 150 | devc->scale += -4; |
f5792417 MH |
151 | break; |
152 | case 2: | |
f4bd8a17 | 153 | devc->scale += -3; |
f5792417 MH |
154 | break; |
155 | case 3: | |
f4bd8a17 | 156 | devc->scale += -2; |
f5792417 MH |
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))) |
f4bd8a17 | 270 | devc->scale = -devc->scale; |
f5792417 MH |
271 | |
272 | /* Range */ | |
273 | range = rs & 0x07; | |
274 | switch (devc->mq) { | |
275 | case SR_MQ_VOLTAGE: | |
276 | if (devc->unit == SR_UNIT_DECIBEL_VOLT) { | |
f4bd8a17 | 277 | devc->scale += -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 | 286 | else |
f4bd8a17 | 287 | devc->scale += range - 5; |
f5792417 MH |
288 | break; |
289 | case SR_MQ_CURRENT: | |
290 | if (devc->scale1000 == -1) | |
f4bd8a17 | 291 | devc->scale += range - 5; |
f5792417 | 292 | else |
f4bd8a17 | 293 | devc->scale += range - 4; |
f5792417 MH |
294 | break; |
295 | case SR_MQ_RESISTANCE: | |
f4bd8a17 | 296 | devc->scale += range - 2; |
f5792417 MH |
297 | break; |
298 | case SR_MQ_FREQUENCY: | |
f4bd8a17 | 299 | devc->scale += range - 2; |
f5792417 MH |
300 | break; |
301 | case SR_MQ_TEMPERATURE: | |
f4bd8a17 | 302 | devc->scale += range - 2; |
f5792417 MH |
303 | break; |
304 | case SR_MQ_CAPACITANCE: | |
f4bd8a17 | 305 | devc->scale += 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; | |
f4bd8a17 | 397 | devc->scale += -1; |
f5792417 | 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) */ | |
f4bd8a17 | 415 | devc->scale += -1; |
6392d599 | 416 | /* Fall through! */ |
f5792417 | 417 | case 0x0e: /* 01110 W on power, A range (29S only) */ |
f4bd8a17 | 418 | devc->scale += -1; |
5791bdf6 | 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 | 431 | devc->mq = SR_MQ_CONTINUITY; |
f4bd8a17 | 432 | devc->scale += -5; |
f5792417 MH |
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))) |
f4bd8a17 | 524 | devc->scale = -devc->scale; |
f5792417 MH |
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) |
f4bd8a17 | 531 | devc->scale += -3; |
c90beca7 | 532 | else |
f4bd8a17 | 533 | devc->scale += 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!*/ | |
f4bd8a17 | 538 | devc->scale += range - 6; |
f5792417 MH |
539 | break; |
540 | case SR_MQ_RESISTANCE: | |
f4bd8a17 | 541 | devc->scale += range - 3; |
f5792417 MH |
542 | break; |
543 | case SR_MQ_FREQUENCY: | |
f4bd8a17 | 544 | devc->scale += range - 3; |
f5792417 MH |
545 | break; |
546 | case SR_MQ_TEMPERATURE: | |
fc348b77 | 547 | if (range == 4) /* Indicator for °F */ |
f5792417 | 548 | devc->unit = SR_UNIT_FAHRENHEIT; |
f4bd8a17 | 549 | devc->scale += -2; |
f5792417 MH |
550 | break; |
551 | case SR_MQ_CAPACITANCE: | |
6392d599 MH |
552 | if (range == 7) |
553 | range -= 1; /* Same value as range 6 */ | |
f4bd8a17 | 554 | devc->scale += range - 13; |
f5792417 MH |
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 */ | |
f4bd8a17 | 580 | devc->scale += -6; |
c90beca7 | 581 | break; |
93b118da UH |
582 | case 2: |
583 | case 3: /* 1, 3 mA */ | |
f4bd8a17 | 584 | devc->scale += -5; |
c90beca7 | 585 | break; |
93b118da UH |
586 | case 4: |
587 | case 5: /* 10, 30 mA */ | |
f4bd8a17 | 588 | devc->scale += -4; |
c90beca7 | 589 | break; |
93b118da UH |
590 | case 6: |
591 | case 7: /* 100, 300 mA */ | |
f4bd8a17 | 592 | devc->scale += -3; |
c90beca7 MH |
593 | break; |
594 | } | |
595 | else /* A */ | |
0c5f2abc | 596 | switch (range) { |
93b118da UH |
597 | case 0: |
598 | case 1: /* 1, 3 A */ | |
f4bd8a17 | 599 | devc->scale += -5; |
c90beca7 MH |
600 | break; |
601 | case 2: /* 10 A */ | |
f4bd8a17 | 602 | devc->scale += -4; |
c90beca7 MH |
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))) | |
f4bd8a17 | 614 | devc->scale = -devc->scale; |
c90beca7 MH |
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; | |
f4bd8a17 | 644 | devc->scale = 0; |
f5792417 MH |
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 | 665 | struct sr_datafeed_packet packet; |
f4bd8a17 | 666 | int digits; |
f5792417 MH |
667 | |
668 | devc = sdi->priv; | |
669 | ||
f4bd8a17 AJ |
670 | digits = -(devc->scale + 3 * devc->scale1000); |
671 | sr_analog_init(&analog, &encoding, &meaning, &spec, digits); | |
f0748225 | 672 | analog.meaning->channels = sdi->channels; |
f5792417 | 673 | analog.num_samples = 1; |
f0748225 UH |
674 | analog.meaning->mq = devc->mq; |
675 | analog.meaning->unit = devc->unit; | |
676 | analog.meaning->mqflags = devc->mqflags; | |
f5792417 MH |
677 | analog.data = &devc->value; |
678 | ||
d0148a50 | 679 | memset(&packet, 0, sizeof(struct sr_datafeed_packet)); |
f0748225 | 680 | packet.type = SR_DF_ANALOG; |
f5792417 | 681 | packet.payload = &analog; |
695dc859 | 682 | sr_session_send(sdi, &packet); |
f5792417 | 683 | |
3c413879 | 684 | sr_sw_limits_update_samples_read(&devc->limits, 1); |
f5792417 MH |
685 | } |
686 | ||
6392d599 | 687 | /** Process 6-byte data message, Metrahit 1x/2x send mode. */ |
f5792417 MH |
688 | static void process_msg_dta_6(struct sr_dev_inst *sdi) |
689 | { | |
690 | struct dev_context *devc; | |
691 | int cnt; | |
fc348b77 | 692 | uint8_t dgt; |
f5792417 MH |
693 | |
694 | devc = sdi->priv; | |
695 | clean_rs_v(devc); | |
696 | ||
697 | /* Byte 0, range and sign */ | |
873e0c12 | 698 | if (devc->model <= METRAHIT_16X) |
f5792417 | 699 | decode_rs_16(bc(devc->buf[0]), devc); |
873e0c12 | 700 | else if (devc->model < METRAHIT_2X) |
f5792417 | 701 | decode_rs_18(bc(devc->buf[0]), devc); |
c90beca7 | 702 | else { |
f5792417 | 703 | decode_rs_2x(bc(devc->buf[0]), devc); |
f4bd8a17 | 704 | devc->scale += 1; /* Compensate for format having only 5 digits, decode_rs_2x() assumes 6. */ |
c90beca7 | 705 | } |
f5792417 MH |
706 | |
707 | /* Bytes 1-5, digits (ls first). */ | |
708 | for (cnt = 0; cnt < 5; cnt++) { | |
709 | dgt = bc(devc->buf[1 + cnt]); | |
710 | if (dgt >= 10) { | |
711 | /* 10 Overload; on model <= 16X also 11 possible. */ | |
712 | devc->value = NAN; | |
f4bd8a17 | 713 | devc->scale = 0; |
f5792417 MH |
714 | break; |
715 | } | |
716 | devc->value += pow(10.0, cnt) * dgt; | |
717 | } | |
6392d599 | 718 | |
f4bd8a17 | 719 | sr_spew("process_msg_dta_6() value=%f scale=%d scale1000=%d", |
f5792417 MH |
720 | devc->value, devc->scale, devc->scale1000); |
721 | if (devc->value != NAN) | |
f4bd8a17 | 722 | devc->value *= pow(10.0, devc->scale) * pow(1000.0, devc->scale1000); |
f5792417 MH |
723 | |
724 | /* Create and send packet. */ | |
725 | send_value(sdi); | |
726 | } | |
727 | ||
728 | /** Process 5-byte info message, Metrahit 1x/2x. */ | |
729 | static void process_msg_inf_5(struct sr_dev_inst *sdi) | |
730 | { | |
731 | struct dev_context *devc; | |
732 | enum model model; | |
733 | ||
734 | devc = sdi->priv; | |
735 | ||
736 | clean_ctmv_rs_v(devc); | |
737 | ||
738 | /* Process byte 0 */ | |
873e0c12 | 739 | model = gmc_decode_model_sm(bc(devc->buf[0])); |
f5792417 MH |
740 | if (model != devc->model) { |
741 | sr_warn("Model mismatch in data: Detected %s, now %s", | |
873e0c12 | 742 | gmc_model_str(devc->model), gmc_model_str(model)); |
f5792417 MH |
743 | } |
744 | ||
745 | /* Process bytes 1-4 */ | |
873e0c12 | 746 | if (devc->model <= METRAHIT_16X) { |
f5792417 MH |
747 | decode_ctmv_16(bc(devc->buf[1]), devc); |
748 | decode_spc_16(bc(devc->buf[2]) | (bc(devc->buf[3]) << 4), devc); | |
749 | decode_rs_16(bc(devc->buf[4]), devc); | |
873e0c12 | 750 | } else if (devc->model <= METRAHIT_18S) { |
f5792417 MH |
751 | decode_ctmv_18(bc(devc->buf[1]), devc); |
752 | decode_spc_18(bc(devc->buf[2]) | (bc(devc->buf[3]) << 4), devc); | |
753 | decode_rs_18(bc(devc->buf[4]), devc); | |
fc348b77 | 754 | } else { /* Must be Metrahit 2x */ |
f5792417 MH |
755 | decode_ctmv_2x(bc(devc->buf[1]), devc); |
756 | decode_spc_2x(bc(devc->buf[2]) | (bc(devc->buf[3]) << 4), devc); | |
757 | decode_rs_2x(bc(devc->buf[4]), devc); | |
758 | } | |
759 | } | |
760 | ||
761 | /** Process 10-byte info/data message, Metrahit 15+. */ | |
762 | static void process_msg_inf_10(struct sr_dev_inst *sdi) | |
763 | { | |
764 | struct dev_context *devc; | |
765 | int cnt; | |
fc348b77 | 766 | uint8_t dgt; |
f5792417 MH |
767 | |
768 | devc = sdi->priv; | |
769 | ||
770 | process_msg_inf_5(sdi); | |
771 | ||
772 | /* Now decode numbers */ | |
773 | for (cnt = 0; cnt < 5; cnt++) { | |
774 | dgt = bc(devc->buf[5 + cnt]); | |
c90beca7 MH |
775 | if (dgt == 11) { /* Empty digit */ |
776 | dgt = 0; | |
777 | } | |
778 | else if (dgt >= 12) { /* Overload */ | |
f5792417 | 779 | devc->value = NAN; |
f4bd8a17 | 780 | devc->scale = 0; |
f5792417 MH |
781 | break; |
782 | } | |
783 | devc->value += pow(10.0, cnt) * dgt; | |
784 | } | |
f4bd8a17 | 785 | sr_spew("process_msg_inf_10() value=%f scale=%d scalet=%d", |
c90beca7 | 786 | devc->value, devc->scale, devc->scale1000); |
f5792417 MH |
787 | |
788 | if (devc->value != NAN) | |
f4bd8a17 | 789 | devc->value *= pow(10, devc->scale) * pow(1000.0, devc->scale1000); |
f5792417 MH |
790 | |
791 | /* Create and send packet. */ | |
792 | send_value(sdi); | |
793 | } | |
794 | ||
795 | /** Decode send interval (Metrahit 2x only). */ | |
fc348b77 | 796 | static const char *decode_send_interval(uint8_t si) |
f5792417 MH |
797 | { |
798 | switch (si) { | |
fc348b77 UH |
799 | case 0x00: |
800 | return "0.05"; | |
801 | case 0x01: | |
802 | return "0.1"; | |
803 | case 0x02: | |
804 | return "0.2"; | |
805 | case 0x03: | |
806 | return "0.5"; | |
807 | case 0x04: | |
808 | return "00:01"; | |
809 | case 0x05: | |
810 | return "00:02"; | |
811 | case 0x06: | |
812 | return "00:05"; | |
813 | case 0x07: | |
814 | return "00:10"; | |
815 | case 0x08: | |
816 | return "00:20"; | |
817 | case 0x09: | |
818 | return "00:30"; | |
819 | case 0x0a: | |
820 | return "01:00"; | |
821 | case 0x0b: | |
822 | return "02:00"; | |
823 | case 0x0c: | |
824 | return "05:00"; | |
825 | case 0x0d: | |
826 | return "10:00"; | |
827 | case 0x0e: | |
828 | return "----"; | |
829 | case 0x0f: | |
830 | return "data"; | |
831 | default: | |
832 | return "Unknown value"; | |
f5792417 MH |
833 | } |
834 | } | |
835 | ||
836 | /** Process 13-byte info/data message, Metrahit 2x. */ | |
837 | static void process_msg_inf_13(struct sr_dev_inst *sdi) | |
838 | { | |
839 | struct dev_context *devc; | |
840 | enum model model; | |
841 | int cnt; | |
fc348b77 | 842 | uint8_t dgt; |
f5792417 MH |
843 | |
844 | devc = sdi->priv; | |
845 | ||
846 | clean_ctmv_rs_v(devc); | |
847 | ||
848 | /* Byte 0, model. */ | |
873e0c12 | 849 | model = gmc_decode_model_sm(bc(devc->buf[0])); |
f5792417 MH |
850 | if (model != devc->model) { |
851 | sr_warn("Model mismatch in data: Detected %s, now %s", | |
873e0c12 | 852 | gmc_model_str(devc->model), gmc_model_str(model)); |
f5792417 MH |
853 | } |
854 | ||
855 | /* Bytes 1-4, 11. */ | |
856 | decode_ctmv_2x(bc(devc->buf[1]) | (bc(devc->buf[11]) << 4), devc); | |
857 | decode_spc_2x(bc(devc->buf[2]) | (bc(devc->buf[3]) << 4), devc); | |
858 | decode_rs_2x(bc(devc->buf[4]), devc); | |
859 | ||
860 | /* Bytes 5-10, digits (ls first). */ | |
861 | for (cnt = 0; cnt < 6; cnt++) { | |
862 | dgt = bc(devc->buf[5 + cnt]); | |
863 | if (dgt == 10) { /* Overload */ | |
864 | devc->value = NAN; | |
f4bd8a17 | 865 | devc->scale = 0; |
f5792417 MH |
866 | break; |
867 | } | |
868 | devc->value += pow(10.0, cnt) * dgt; | |
869 | } | |
f4bd8a17 | 870 | sr_spew("process_msg_inf_13() value=%f scale=%d scale1000=%d mq=%d " |
6433156c | 871 | "unit=%d mqflags=0x%02" PRIx64, devc->value, devc->scale, |
f0748225 | 872 | devc->scale1000, devc->mq, devc->unit, (uint64_t)devc->mqflags); |
f5792417 | 873 | if (devc->value != NAN) |
f4bd8a17 | 874 | devc->value *= pow(10, devc->scale) * pow(1000.0, devc->scale1000); |
f5792417 MH |
875 | |
876 | /* Byte 12, Send Interval */ | |
877 | sr_spew("Send interval: %s", decode_send_interval(bc(devc->buf[12]))); | |
878 | ||
879 | /* Create and send packet. */ | |
880 | send_value(sdi); | |
881 | } | |
882 | ||
c90beca7 MH |
883 | /** Dump contents of 14-byte message. |
884 | * @param buf Pointer to array of 14 data bytes. | |
885 | * @param[in] raw Write only data bytes, no interpretation. | |
886 | */ | |
bc48ec37 | 887 | static void dump_msg14(guchar *buf, gboolean raw) |
c90beca7 MH |
888 | { |
889 | if (!buf) | |
890 | return; | |
891 | ||
892 | if (raw) | |
893 | sr_spew("msg14: 0x %02x %02x %02x %02x %02x %02x %02x %02x " | |
894 | "%02x %02x %02x %02x %02x %02x", | |
895 | buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], | |
896 | buf[7], buf[8], buf[9], buf[10], buf[11], buf[12], | |
897 | buf[13]); | |
898 | else | |
899 | sr_spew("msg14: 0x a=%d c1=%02x c2=%02x cmd=%02x dta=%02x " | |
900 | "%02x %02x %02x %02x %02x %02x %02x %02x chs=%02x", | |
901 | buf[1] == 0x2b?buf[0] >> 2:buf[0] % 0x0f, buf[1], buf[2], buf[3], buf[4], buf[5], | |
902 | buf[6], buf[7], buf[8], buf[9], buf[10], buf[11], | |
903 | buf[12], buf[13]); | |
904 | } | |
905 | ||
906 | /** Calc checksum for 14 byte message type. | |
907 | * | |
908 | * @param[in] dta Pointer to array of 13 data bytes. | |
909 | * @return Checksum. | |
910 | */ | |
c442ffda | 911 | static guchar calc_chksum_14(guchar *dta) |
c90beca7 MH |
912 | { |
913 | guchar cnt, chs; | |
914 | ||
07ffa5b3 | 915 | for (chs = 0, cnt = 0; cnt < (GMC_REPLY_SIZE - 1); cnt++) |
c90beca7 MH |
916 | chs += dta[cnt]; |
917 | ||
918 | return (64 - chs) & MASK_6BITS; | |
919 | } | |
920 | ||
921 | /** Check 14-byte message, Metrahit 2x. */ | |
922 | static int chk_msg14(struct sr_dev_inst *sdi) | |
923 | { | |
924 | struct dev_context *devc; | |
925 | int retc; | |
926 | gboolean isreq; /* Message is request to multimeter (otherwise response) */ | |
927 | uint8_t addr; /* Adaptor address */ | |
928 | ||
929 | retc = SR_OK; | |
930 | ||
931 | /* Check parameters and message */ | |
932 | if (!sdi || !(devc = sdi->priv)) | |
933 | return SR_ERR_ARG; | |
934 | ||
935 | if (devc->buflen != 14) { | |
936 | sr_err("process_msg_14(): Msg len 14 expected!"); | |
937 | return SR_ERR_ARG; | |
938 | } | |
939 | ||
940 | isreq = devc->buf[1] == 0x2b; | |
941 | if (isreq) | |
942 | addr = devc->buf[0] >> 2; | |
943 | else | |
944 | addr = devc->buf[0] & 0x0f; | |
945 | ||
946 | if ((devc->addr != addr) && !(isreq && (addr == 0))) { | |
947 | sr_err("process_msg_14(): Address mismatch, msg for other device!"); | |
948 | retc = SR_ERR_ARG; | |
949 | } | |
950 | ||
951 | if (devc->buf[1] == 0) { /* Error msg from device! */ | |
c90beca7 MH |
952 | switch (devc->buf[2]) { |
953 | case 1: /* Not used */ | |
954 | sr_err("Device: Illegal error code!"); | |
955 | break; | |
956 | case 2: /* Incorrect check sum of received block */ | |
957 | sr_err("Device: Incorrect checksum in cmd!"); | |
958 | break; | |
959 | case 3: /* Incorrect length of received block */ | |
960 | sr_err("Device: Incorrect block length in cmd!"); | |
961 | break; | |
962 | case 4: /* Incorrect 2nd or 3rd byte */ | |
963 | sr_err("Device: Incorrect byte 2 or 3 in cmd!"); | |
964 | break; | |
965 | case 5: /* Parameter out of range */ | |
966 | sr_err("Device: Parameter out of range!"); | |
967 | break; | |
968 | default: | |
969 | sr_err("Device: Unknown error code!"); | |
970 | } | |
971 | retc = SR_ERR_ARG; | |
972 | } | |
973 | else if (!isreq && ((devc->buf[1] != 0x27) || (devc->buf[2] != 0x3f))) { | |
974 | sr_err("process_msg_14(): byte 1/2 unexpected!"); | |
975 | retc = SR_ERR_ARG; | |
976 | } | |
977 | ||
978 | if (calc_chksum_14(devc->buf) != devc->buf[13]) { | |
979 | sr_err("process_msg_14(): Invalid checksum!"); | |
980 | retc = SR_ERR_ARG; | |
981 | } | |
982 | ||
983 | if (retc != SR_OK) | |
984 | dump_msg14(devc->buf, TRUE); | |
985 | ||
986 | return retc; | |
987 | } | |
988 | ||
989 | /** Check 14-byte message, Metrahit 2x. */ | |
990 | SR_PRIV int process_msg14(struct sr_dev_inst *sdi) | |
991 | { | |
992 | struct dev_context *devc; | |
993 | int retc; | |
994 | uint8_t addr; | |
995 | uint8_t cnt, dgt; | |
996 | ||
997 | if ((retc = chk_msg14(sdi)) != SR_OK) | |
998 | return retc; | |
999 | ||
1000 | devc = sdi->priv; | |
1001 | ||
1002 | clean_ctmv_rs_v(devc); | |
1003 | addr = devc->buf[0] & MASK_6BITS; | |
1004 | if (addr != devc->addr) | |
1005 | sr_info("Device address mismatch %d/%d!", addr, devc->addr); | |
1006 | ||
1007 | switch (devc->buf[3]) { /* That's the command this reply is for */ | |
1008 | /* 0 cannot occur, the respective message is not a 14-byte message */ | |
1009 | case 1: /* Read first free and occupied address */ | |
1010 | sr_spew("Cmd %d unimplemented!", devc->buf[3]); | |
1011 | break; | |
1012 | case 2: /* Clear all RAM in multimeter */ | |
1013 | sr_spew("Cmd %d unimplemented!", devc->buf[3]); | |
1014 | break; | |
1015 | case 3: /* Read firmware version and status */ | |
6433156c | 1016 | sr_spew("Cmd 3, Read firmware and status"); |
c90beca7 MH |
1017 | switch (devc->cmd_idx) { |
1018 | case 0: | |
1019 | devc->fw_ver_maj = devc->buf[5]; | |
1020 | devc->fw_ver_min = devc->buf[4]; | |
1021 | sr_spew("Firmware version %d.%d", (int)devc->fw_ver_maj, (int)devc->fw_ver_min); | |
1022 | sr_spew("Rotary Switch Position (1..10): %d", (int)devc->buf[6]); | |
1023 | /** Docs say values 0..9, but that's not true */ | |
1024 | sr_spew("Measurement Function: %d ", (int)devc->buf[7]); | |
1025 | decode_ctmv_2x(devc->buf[7], devc); | |
1026 | sr_spew("Range: 0x%x", devc->buf[8]); | |
1027 | decode_rs_2x_TR2(devc->buf[8] & 0x0f, devc); /* Docs wrong, uses conversion table TR_2! */ | |
1028 | devc->autorng = (devc->buf[8] & 0x20) == 0; | |
1029 | // TODO 9, 10: 29S special functions | |
1030 | devc->ubatt = 0.1 * (float)devc->buf[11]; | |
1031 | devc->model = gmc_decode_model_bd(devc->buf[12]); | |
1032 | sr_spew("Model=%s, battery voltage=%2.1f V", gmc_model_str(devc->model), (double)devc->ubatt); | |
1033 | break; | |
1034 | case 1: | |
1035 | sr_spew("Internal version %d.%d", (int)devc->buf[5], (int)devc->buf[4]); | |
1036 | sr_spew("Comm mode: 0x%x", (int)devc->buf[6]); | |
1037 | sr_spew("Block cnt%%64: %d", (int)devc->buf[7]); | |
1038 | sr_spew("drpCi: %d drpCh: %d", (int)devc->buf[8], (int)devc->buf[9]); | |
1039 | // Semantics undocumented. Possibly Metrahit 29S dropouts stuff? | |
1040 | break; | |
1041 | default: | |
1042 | sr_spew("Cmd 3: Unknown cmd_idx=%d", devc->cmd_idx); | |
1043 | break; | |
1044 | } | |
1045 | break; | |
1046 | case 4: /* Set real time, date, sample rate, trigger, ... */ | |
1047 | sr_spew("Cmd %d unimplemented!", devc->buf[3]); | |
1048 | break; | |
1049 | case 5: /* Read real time, date, sample rate, trigger... */ | |
1050 | sr_spew("Cmd %d unimplemented!", devc->buf[3]); | |
1051 | break; | |
1052 | case 6: /* Set modes or power off */ | |
1053 | sr_spew("Cmd %d unimplemented!", devc->buf[3]); | |
1054 | break; | |
1055 | case 7: /* Set measurement function, range, autom/man. */ | |
1056 | sr_spew("Cmd %d unimplemented!", devc->buf[3]); | |
1057 | break; | |
1058 | case 8: /* Get one measurement value */ | |
1059 | sr_spew("Cmd 8, get one measurement value"); | |
1060 | sr_spew("Measurement Function: %d ", (int)devc->buf[5]); | |
1061 | decode_ctmv_2x(devc->buf[5], devc); | |
1062 | if (!(devc->buf[6] & 0x10)) /* If bit4=0, old data. */ | |
1063 | return SR_OK; | |
1064 | ||
1065 | decode_rs_2x_TR2(devc->buf[6] & 0x0f, devc); // The docs say conversion table TR_3, but that does not work | |
1066 | setmqf(devc, SR_MQFLAG_AUTORANGE, devc->autorng); | |
1067 | /* 6 digits */ | |
1068 | for (cnt = 0; cnt < 6; cnt++) { | |
1069 | dgt = bc(devc->buf[7 + cnt]); | |
1070 | if (dgt == 10) { /* Overload */ | |
1071 | devc->value = NAN; | |
f4bd8a17 | 1072 | devc->scale = 0; |
c90beca7 MH |
1073 | break; |
1074 | } | |
1075 | else if (dgt == 13) { /* FUSE */ | |
1076 | sr_err("FUSE!"); | |
1077 | } | |
1078 | else if (dgt == 14) { /* Function recognition mode, OPEN */ | |
1079 | sr_info("Function recognition mode, OPEN!"); | |
1080 | devc->value = NAN; | |
f4bd8a17 | 1081 | devc->scale = 0; |
c90beca7 MH |
1082 | break; |
1083 | } | |
1084 | devc->value += pow(10.0, cnt) * dgt; | |
1085 | } | |
f4bd8a17 | 1086 | sr_spew("process_msg14() value=%f scale=%d scale1000=%d mq=%d " |
6433156c | 1087 | "unit=%d mqflags=0x%02" PRIx64, devc->value, devc->scale, |
f0748225 | 1088 | devc->scale1000, devc->mq, devc->unit, (uint64_t)devc->mqflags); |
c90beca7 | 1089 | if (devc->value != NAN) |
f4bd8a17 | 1090 | devc->value *= pow(10, devc->scale) * pow(1000.0, devc->scale1000); |
c90beca7 MH |
1091 | |
1092 | send_value(sdi); | |
1093 | ||
1094 | break; | |
1095 | default: | |
1096 | sr_spew("Unknown cmd %d!", devc->buf[3]); | |
1097 | break; | |
1098 | } | |
1099 | ||
1100 | return SR_OK; | |
1101 | } | |
1102 | ||
1103 | /** Data reception callback function. */ | |
7b4edcb6 MH |
1104 | SR_PRIV int gmc_mh_1x_2x_receive_data(int fd, int revents, void *cb_data) |
1105 | { | |
f5792417 | 1106 | struct sr_dev_inst *sdi; |
7b4edcb6 | 1107 | struct dev_context *devc; |
f5792417 | 1108 | struct sr_serial_dev_inst *serial; |
fc348b77 | 1109 | uint8_t buf, msgt; |
f5792417 | 1110 | int len; |
7b4edcb6 MH |
1111 | |
1112 | (void)fd; | |
1113 | ||
1114 | if (!(sdi = cb_data)) | |
1115 | return TRUE; | |
1116 | ||
1117 | if (!(devc = sdi->priv)) | |
1118 | return TRUE; | |
1119 | ||
f5792417 MH |
1120 | serial = sdi->conn; |
1121 | ||
1122 | if (revents == G_IO_IN) { /* Serial data arrived. */ | |
1123 | while (GMC_BUFSIZE - devc->buflen - 1 > 0) { | |
4a0eda2b | 1124 | len = serial_read_nonblocking(serial, devc->buf + devc->buflen, 1); |
f5792417 MH |
1125 | if (len < 1) |
1126 | break; | |
1127 | buf = *(devc->buf + devc->buflen); | |
1128 | sr_spew("read 0x%02x/%d/%d", buf, buf, buf & MSGC_MASK); | |
1129 | devc->buflen += len; | |
1130 | if (!devc->settings_ok) { | |
fc348b77 UH |
1131 | /* |
1132 | * If no device type/settings record processed | |
1133 | * yet, wait for one. | |
1134 | */ | |
f5792417 MH |
1135 | if ((devc->buf[0] & MSGID_MASK) != MSGID_INF) { |
1136 | devc->buflen = 0; | |
1137 | continue; | |
1138 | } | |
1139 | devc->settings_ok = TRUE; | |
1140 | } | |
1141 | ||
1142 | msgt = devc->buf[0] & MSGID_MASK; | |
1143 | switch (msgt) { | |
1144 | case MSGID_INF: | |
1145 | if (devc->buflen == 13) { | |
1146 | process_msg_inf_13(sdi); | |
1147 | devc->buflen = 0; | |
1148 | continue; | |
fc348b77 | 1149 | } else if ((devc->buflen == 10) && |
c90beca7 | 1150 | (devc->model <= METRAHIT_18S)) { |
f5792417 MH |
1151 | process_msg_inf_10(sdi); |
1152 | devc->buflen = 0; | |
1153 | continue; | |
1154 | } | |
1155 | else if ((devc->buflen >= 5) && | |
c90beca7 MH |
1156 | (devc->buf[devc->buflen - 1] & |
1157 | MSGID_MASK) != MSGID_DATA) { | |
fc348b77 UH |
1158 | /* |
1159 | * Char just received is beginning | |
1160 | * of next message. | |
1161 | */ | |
f5792417 | 1162 | process_msg_inf_5(sdi); |
fc348b77 | 1163 | devc->buf[0] = |
c90beca7 | 1164 | devc->buf[devc->buflen - 1]; |
f5792417 MH |
1165 | devc->buflen = 1; |
1166 | continue; | |
1167 | } | |
1168 | break; | |
1169 | case MSGID_DTA: | |
1170 | case MSGID_D10: | |
1171 | if (devc->buflen == 6) { | |
1172 | process_msg_dta_6(sdi); | |
1173 | devc->buflen = 0; | |
1174 | } | |
1175 | break; | |
1176 | case MSGID_DATA: | |
1177 | sr_err("Comm error, unexpected data byte!"); | |
1178 | devc->buflen = 0; | |
1179 | break; | |
1180 | } | |
1181 | } | |
1182 | } | |
1183 | ||
3c413879 | 1184 | if (sr_sw_limits_check(&devc->limits)) |
695dc859 | 1185 | sdi->driver->dev_acquisition_stop(sdi); |
f5792417 | 1186 | |
7b4edcb6 MH |
1187 | return TRUE; |
1188 | } | |
f5792417 | 1189 | |
c90beca7 MH |
1190 | SR_PRIV int gmc_mh_2x_receive_data(int fd, int revents, void *cb_data) |
1191 | { | |
1192 | struct sr_dev_inst *sdi; | |
1193 | struct dev_context *devc; | |
1194 | struct sr_serial_dev_inst *serial; | |
1195 | uint8_t buf; | |
1196 | int len; | |
c90beca7 MH |
1197 | |
1198 | (void)fd; | |
1199 | ||
1200 | if (!(sdi = cb_data)) | |
1201 | return TRUE; | |
1202 | ||
1203 | if (!(devc = sdi->priv)) | |
1204 | return TRUE; | |
1205 | ||
1206 | serial = sdi->conn; | |
1207 | ||
1208 | if (revents == G_IO_IN) { /* Serial data arrived. */ | |
1209 | while (GMC_BUFSIZE - devc->buflen - 1 > 0) { | |
4a0eda2b | 1210 | len = serial_read_nonblocking(serial, devc->buf + devc->buflen, 1); |
c90beca7 MH |
1211 | if (len < 1) |
1212 | break; | |
1213 | buf = *(devc->buf + devc->buflen); | |
1214 | sr_spew("read 0x%02x/%d/%d", buf, buf, buf & MASK_6BITS); | |
1215 | devc->buf[devc->buflen] &= MASK_6BITS; | |
1216 | devc->buflen += len; | |
1217 | ||
1218 | if (devc->buflen == 14) { | |
1219 | devc->response_pending = FALSE; | |
1220 | sr_spew("gmc_mh_2x_receive_data processing msg"); | |
1221 | process_msg14(sdi); | |
1222 | devc->buflen = 0; | |
1223 | } | |
1224 | } | |
1225 | } | |
1226 | ||
3c413879 | 1227 | if (sr_sw_limits_check(&devc->limits)) |
695dc859 | 1228 | sdi->driver->dev_acquisition_stop(sdi); |
c90beca7 | 1229 | |
c90beca7 MH |
1230 | /* Request next data set, if required */ |
1231 | if (sdi->status == SR_ST_ACTIVE) { | |
1232 | if (devc->response_pending) { | |
1233 | gint64 elapsed_us = g_get_monotonic_time() - devc->req_sent_at; | |
1a46cc62 | 1234 | if (elapsed_us > (1 * 1000 * 1000)) /* Timeout! */ |
c90beca7 MH |
1235 | devc->response_pending = FALSE; |
1236 | } | |
1237 | if (!devc->response_pending) { | |
1238 | devc->cmd_seq++; | |
1239 | if (devc->cmd_seq % 10 == 0) { | |
1240 | if (req_stat14(sdi, FALSE) != SR_OK) | |
1241 | return FALSE; | |
1242 | } | |
1243 | else if (req_meas14(sdi) != SR_OK) | |
1244 | return FALSE; | |
1245 | } | |
1246 | } | |
1247 | ||
1248 | return TRUE; | |
1249 | } | |
1250 | ||
1251 | /** Create 14 (42) byte command for Metrahit 2x multimeter in bidir mode. | |
1252 | * | |
1253 | * Actually creates 42 bytes due to the encoding method used. | |
1254 | * @param[in] addr Device address (0=adapter, 1..15 multimeter; for byte 0). | |
1255 | * @param[in] func Function code (byte 3). | |
1256 | * @param[in] params Further parameters (9 bytes) | |
1257 | * @param[out] buf Buffer to create msg in (42 bytes). | |
1258 | */ | |
bc48ec37 | 1259 | static void create_cmd_14(guchar addr, guchar func, guchar *params, guchar *buf) |
c90beca7 | 1260 | { |
07ffa5b3 | 1261 | uint8_t dta[GMC_REPLY_SIZE]; /* Unencoded message */ |
c90beca7 MH |
1262 | int cnt; |
1263 | ||
1264 | if (!params || !buf) | |
1265 | return; | |
1266 | ||
1267 | /* 0: Address */ | |
1268 | dta[0] = ((addr << 2) | 0x03) & MASK_6BITS; | |
1269 | ||
1270 | /* 1-3: Set command header */ | |
1271 | dta[1] = 0x2b; | |
1272 | dta[2] = 0x3f; | |
1273 | dta[3] = func; | |
1274 | ||
1275 | /* 4-12: Copy further parameters */ | |
1276 | for (cnt = 0; cnt < 9; cnt++) | |
07ffa5b3 | 1277 | dta[cnt + 4] = (params[cnt] & MASK_6BITS); |
c90beca7 MH |
1278 | |
1279 | /* 13: Checksum (b complement) */ | |
1280 | dta[13] = calc_chksum_14(dta); | |
1281 | ||
1282 | /* The whole message is packed into 3 bytes per byte now (lower 6 bits only) the most | |
1283 | * peculiar way I have ever seen. Possibly to improve IR communication? */ | |
07ffa5b3 UH |
1284 | for (cnt = 0; cnt < GMC_REPLY_SIZE; cnt++) { |
1285 | buf[(3 * cnt) + 0] = (dta[cnt] & 0x01 ? 0x0f : 0) | (dta[cnt] & 0x02 ? 0xf0 : 0); | |
1286 | buf[(3 * cnt) + 1] = (dta[cnt] & 0x04 ? 0x0f : 0) | (dta[cnt] & 0x08 ? 0xf0 : 0); | |
1287 | buf[(3 * cnt) + 2] = (dta[cnt] & 0x10 ? 0x0f : 0) | (dta[cnt] & 0x20 ? 0xf0 : 0); | |
c90beca7 MH |
1288 | } |
1289 | } | |
1290 | ||
1291 | /** Request one measurement from 2x multimeter (msg 8). | |
1292 | * | |
1293 | */ | |
1294 | int req_meas14(const struct sr_dev_inst *sdi) | |
1295 | { | |
1296 | struct dev_context *devc; | |
1297 | struct sr_serial_dev_inst *serial; | |
1298 | uint8_t params[9]; | |
1299 | uint8_t msg[42]; | |
1300 | ||
1301 | if (!sdi || !(devc = sdi->priv) || !(serial = sdi->conn)) | |
1302 | return SR_ERR; | |
1303 | ||
1304 | memset(params, 0, sizeof(params)); | |
1305 | params[0] = 0; | |
1306 | devc->cmd_idx = 0; | |
1307 | create_cmd_14(devc->addr, 8, params, msg); | |
1308 | devc->req_sent_at = g_get_monotonic_time(); | |
936b1c00 UH |
1309 | if (serial_write_blocking(serial, msg, sizeof(msg), |
1310 | serial_timeout(serial, sizeof(msg))) < (int)sizeof(msg)) { | |
c90beca7 MH |
1311 | return SR_ERR; |
1312 | } | |
1313 | ||
1314 | devc->response_pending = TRUE; | |
1315 | ||
1316 | return SR_OK; | |
1317 | } | |
1318 | ||
1319 | /** Request status from 2x multimeter (msg 3). | |
1320 | * @param[in] power_on Try to power on powered off multimeter by sending additional messages. | |
1321 | */ | |
1322 | int req_stat14(const struct sr_dev_inst *sdi, gboolean power_on) | |
1323 | { | |
1324 | struct dev_context *devc; | |
1325 | struct sr_serial_dev_inst *serial; | |
1326 | uint8_t params[9]; | |
1327 | uint8_t msg[42]; | |
1328 | ||
1329 | if (!sdi || !(devc = sdi->priv) || !(serial = sdi->conn)) | |
1330 | return SR_ERR; | |
1331 | ||
1332 | memset(params, 0, sizeof(params)); | |
1333 | params[0] = 0; | |
1334 | devc->cmd_idx = 0; | |
1335 | create_cmd_14(devc->addr, 3, params, msg); | |
1336 | ||
1337 | if (power_on) { | |
1338 | sr_info("Write some data and wait 3s to turn on powered off device..."); | |
936b1c00 UH |
1339 | if (serial_write_blocking(serial, msg, sizeof(msg), |
1340 | serial_timeout(serial, sizeof(msg))) < 0) | |
c90beca7 | 1341 | return SR_ERR; |
1a46cc62 | 1342 | g_usleep(1 * 1000 * 1000); |
936b1c00 UH |
1343 | if (serial_write_blocking(serial, msg, sizeof(msg), |
1344 | serial_timeout(serial, sizeof(msg))) < 0) | |
fadd0707 | 1345 | return SR_ERR; |
1a46cc62 | 1346 | g_usleep(1 * 1000 * 1000); |
936b1c00 UH |
1347 | if (serial_write_blocking(serial, msg, sizeof(msg), |
1348 | serial_timeout(serial, sizeof(msg))) < 0) | |
fadd0707 | 1349 | return SR_ERR; |
1a46cc62 | 1350 | g_usleep(1 * 1000 * 1000); |
c90beca7 MH |
1351 | serial_flush(serial); |
1352 | } | |
1353 | ||
1354 | /* Write message and wait for reply */ | |
1355 | devc->req_sent_at = g_get_monotonic_time(); | |
936b1c00 UH |
1356 | if (serial_write_blocking(serial, msg, sizeof(msg), |
1357 | serial_timeout(serial, sizeof(msg))) < (int)sizeof(msg)) { | |
c90beca7 MH |
1358 | return SR_ERR; |
1359 | } | |
1360 | ||
1361 | devc->response_pending = TRUE; | |
1362 | ||
1363 | return SR_OK; | |
1364 | } | |
1365 | ||
1366 | /** Decode model in "send mode". | |
1367 | * | |
1368 | * @param[in] mcode Model code. | |
1369 | * @return Model code. | |
1370 | */ | |
873e0c12 | 1371 | SR_PRIV int gmc_decode_model_sm(uint8_t mcode) |
f5792417 MH |
1372 | { |
1373 | if (mcode > 0xf) { | |
1374 | sr_err("decode_model(%d): Model code 0..15 expected!", mcode); | |
873e0c12 | 1375 | return METRAHIT_NONE; |
f5792417 MH |
1376 | } |
1377 | ||
0c5f2abc | 1378 | switch (mcode) { |
fc348b77 | 1379 | case 0x04: /* 0100b */ |
873e0c12 | 1380 | return METRAHIT_12S; |
fc348b77 | 1381 | case 0x08: /* 1000b */ |
873e0c12 | 1382 | return METRAHIT_13S14A; |
fc348b77 | 1383 | case 0x09: /* 1001b */ |
873e0c12 | 1384 | return METRAHIT_14S; |
fc348b77 | 1385 | case 0x0A: /* 1010b */ |
873e0c12 | 1386 | return METRAHIT_15S; |
fc348b77 | 1387 | case 0x0B: /* 1011b */ |
873e0c12 | 1388 | return METRAHIT_16S; |
fc348b77 | 1389 | case 0x06: /* 0110b (undocumented by GMC!) */ |
873e0c12 | 1390 | return METRAHIT_16I; |
2c1a012e MH |
1391 | case 0x07: /* 0111b (undocumented by GMC!) */ |
1392 | return METRAHIT_16T; | |
fc348b77 | 1393 | case 0x0D: /* 1101b */ |
873e0c12 | 1394 | return METRAHIT_18S; |
fc348b77 | 1395 | case 0x02: /* 0010b */ |
873e0c12 | 1396 | return METRAHIT_22SM; |
fc348b77 | 1397 | case 0x03: /* 0011b */ |
873e0c12 | 1398 | return METRAHIT_23S; |
2c1a012e | 1399 | case 0x0F: /* 1111b */ |
873e0c12 | 1400 | return METRAHIT_24S; |
fc348b77 | 1401 | case 0x05: /* 0101b */ |
c90beca7 | 1402 | return METRAHIT_25S; |
fc348b77 | 1403 | case 0x01: /* 0001b */ |
c90beca7 | 1404 | return METRAHIT_26SM; |
2c1a012e | 1405 | case 0x0C: /* 1100b */ |
873e0c12 | 1406 | return METRAHIT_28S; |
2c1a012e | 1407 | case 0x0E: /* 1110b */ |
873e0c12 | 1408 | return METRAHIT_29S; |
f5792417 MH |
1409 | default: |
1410 | sr_err("Unknown model code %d!", mcode); | |
873e0c12 | 1411 | return METRAHIT_NONE; |
f5792417 MH |
1412 | } |
1413 | } | |
1414 | ||
6392d599 | 1415 | /** Convert GMC model code in bidirectional mode to sigrok-internal one. |
fc348b77 | 1416 | * |
6392d599 | 1417 | * @param[in] mcode Model code. |
fc348b77 | 1418 | * |
6392d599 | 1419 | * @return Model code. |
fc348b77 | 1420 | */ |
6392d599 | 1421 | SR_PRIV int gmc_decode_model_bd(uint8_t mcode) |
f5792417 | 1422 | { |
6392d599 | 1423 | switch (mcode & 0x1f) { |
fc348b77 | 1424 | case 2: |
c90beca7 MH |
1425 | if (mcode & 0x20) |
1426 | return METRAHIT_22M; | |
1427 | else | |
1428 | return METRAHIT_22S; | |
fc348b77 | 1429 | case 3: |
873e0c12 | 1430 | return METRAHIT_23S; |
fc348b77 | 1431 | case 4: |
873e0c12 | 1432 | return METRAHIT_24S; |
fc348b77 | 1433 | case 5: |
6392d599 | 1434 | return METRAHIT_25S; |
fc348b77 | 1435 | case 1: |
c90beca7 MH |
1436 | if (mcode & 0x20) |
1437 | return METRAHIT_26M; | |
1438 | else | |
1439 | return METRAHIT_26S; | |
fc348b77 | 1440 | case 12: |
873e0c12 | 1441 | return METRAHIT_28S; |
fc348b77 | 1442 | case 14: |
873e0c12 | 1443 | return METRAHIT_29S; |
f5792417 MH |
1444 | default: |
1445 | sr_err("Unknown model code %d!", mcode); | |
873e0c12 | 1446 | return METRAHIT_NONE; |
f5792417 MH |
1447 | } |
1448 | } | |
1449 | ||
6392d599 MH |
1450 | /** Convert sigrok-internal model code to string. |
1451 | * | |
1452 | * @param[in] mcode Model code. | |
1453 | * | |
1454 | * @return Model code string. | |
1455 | */ | |
873e0c12 | 1456 | SR_PRIV const char *gmc_model_str(enum model mcode) |
f5792417 MH |
1457 | { |
1458 | switch (mcode) { | |
873e0c12 | 1459 | case METRAHIT_NONE: |
f5792417 | 1460 | return "-uninitialized model variable-"; |
873e0c12 | 1461 | case METRAHIT_12S: |
f5792417 | 1462 | return "METRAHit 12S"; |
873e0c12 | 1463 | case METRAHIT_13S14A: |
f5792417 | 1464 | return "METRAHit 13S/14A"; |
873e0c12 | 1465 | case METRAHIT_14S: |
f5792417 | 1466 | return "METRAHit 14S"; |
873e0c12 | 1467 | case METRAHIT_15S: |
f5792417 | 1468 | return "METRAHit 15S"; |
873e0c12 | 1469 | case METRAHIT_16S: |
f5792417 | 1470 | return "METRAHit 16S"; |
873e0c12 | 1471 | case METRAHIT_16I: |
2c1a012e MH |
1472 | return "METRAHit 16I/16L"; |
1473 | case METRAHIT_16T: | |
1474 | return "METRAHit 16T/16U/KMM2002"; | |
873e0c12 | 1475 | case METRAHIT_18S: |
f5792417 | 1476 | return "METRAHit 18S"; |
873e0c12 | 1477 | case METRAHIT_22SM: |
f5792417 | 1478 | return "METRAHit 22S/M"; |
c90beca7 MH |
1479 | case METRAHIT_22S: |
1480 | return "METRAHit 22S"; | |
1481 | case METRAHIT_22M: | |
1482 | return "METRAHit 22M"; | |
873e0c12 | 1483 | case METRAHIT_23S: |
f5792417 | 1484 | return "METRAHit 23S"; |
873e0c12 | 1485 | case METRAHIT_24S: |
f5792417 | 1486 | return "METRAHit 24S"; |
6392d599 MH |
1487 | case METRAHIT_25S: |
1488 | return "METRAHit 25S"; | |
1489 | case METRAHIT_26SM: | |
1490 | return "METRAHit 26S/M"; | |
c90beca7 MH |
1491 | case METRAHIT_26S: |
1492 | return "METRAHit 26S"; | |
1493 | case METRAHIT_26M: | |
1494 | return "METRAHit 26M"; | |
873e0c12 | 1495 | case METRAHIT_28S: |
f5792417 | 1496 | return "METRAHit 28S"; |
873e0c12 | 1497 | case METRAHIT_29S: |
f5792417 MH |
1498 | return "METRAHit 29S"; |
1499 | default: | |
1500 | return "Unknown model code"; | |
1501 | } | |
1502 | } | |
c90beca7 | 1503 | |
dcd438ee | 1504 | /** @copydoc sr_dev_driver.config_set */ |
584560f1 BV |
1505 | SR_PRIV int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, |
1506 | const struct sr_channel_group *cg) | |
c90beca7 MH |
1507 | { |
1508 | struct dev_context *devc; | |
1509 | uint8_t params[9]; | |
1510 | uint8_t msg[42]; | |
1511 | ||
53b4680f | 1512 | (void)cg; |
c90beca7 MH |
1513 | |
1514 | if (sdi->status != SR_ST_ACTIVE) | |
1515 | return SR_ERR_DEV_CLOSED; | |
1516 | ||
b0baddef | 1517 | devc = sdi->priv; |
c90beca7 MH |
1518 | |
1519 | switch (key) { | |
1520 | case SR_CONF_POWER_OFF: | |
1521 | if (devc->model < METRAHIT_2X) | |
1522 | return SR_ERR_NA; | |
1523 | if (!g_variant_get_boolean(data)) | |
1524 | return SR_ERR; | |
1525 | sr_info("Powering device off."); | |
1526 | ||
1527 | memset(params, 0, sizeof(params)); | |
1528 | params[0] = 5; | |
1529 | params[1] = 5; | |
1530 | create_cmd_14(devc->addr, 6, params, msg); | |
936b1c00 UH |
1531 | if (serial_write_blocking(sdi->conn, msg, sizeof(msg), |
1532 | serial_timeout(sdi->conn, sizeof(msg))) < 0) | |
c90beca7 MH |
1533 | return SR_ERR; |
1534 | else | |
1a46cc62 | 1535 | g_usleep(2 * 1000 * 1000); /* Wait to ensure transfer before interface switched off. */ |
c90beca7 | 1536 | break; |
c90beca7 | 1537 | case SR_CONF_LIMIT_SAMPLES: |
3c413879 LPC |
1538 | case SR_CONF_LIMIT_MSEC: |
1539 | return sr_sw_limits_config_set(&devc->limits, key, data); | |
c90beca7 MH |
1540 | default: |
1541 | return SR_ERR_NA; | |
1542 | } | |
1543 | ||
1544 | return SR_OK; | |
1545 | } |