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