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