]>
Commit | Line | Data |
---|---|---|
b84c13d7 BV |
1 | /* |
2 | * This file is part of the sigrok project. | |
3 | * | |
4 | * Copyright (C) 2012 Bert Vermeulen <bert@biot.com> | |
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 | ||
20 | #include "libsigrok.h" | |
21 | #include "libsigrok-internal.h" | |
22 | #include "genericdmm.h" | |
23 | #include <libusb.h> | |
24 | #include <string.h> | |
25 | #include <math.h> | |
26 | ||
2980cc24 UH |
27 | /* Message logging helpers with driver-specific prefix string. */ |
28 | #define DRIVER_LOG_DOMAIN "victor-dmm: " | |
29 | #define sr_log(l, s, args...) sr_log(l, DRIVER_LOG_DOMAIN s, ## args) | |
30 | #define sr_spew(s, args...) sr_spew(DRIVER_LOG_DOMAIN s, ## args) | |
31 | #define sr_dbg(s, args...) sr_dbg(DRIVER_LOG_DOMAIN s, ## args) | |
32 | #define sr_info(s, args...) sr_info(DRIVER_LOG_DOMAIN s, ## args) | |
33 | #define sr_warn(s, args...) sr_warn(DRIVER_LOG_DOMAIN s, ## args) | |
34 | #define sr_err(s, args...) sr_err(DRIVER_LOG_DOMAIN s, ## args) | |
b84c13d7 | 35 | |
2980cc24 | 36 | #define DMM_DATA_SIZE 14 |
b84c13d7 BV |
37 | |
38 | /* Reverse the high nibble into the low nibble */ | |
39 | static uint8_t decode_digit(uint8_t in) | |
40 | { | |
41 | uint8_t out, i; | |
42 | ||
43 | out = 0; | |
44 | in >>= 4; | |
45 | for (i = 0x08; i; i >>= 1) { | |
46 | out >>= 1; | |
47 | if (in & i) | |
48 | out |= 0x08; | |
49 | } | |
50 | ||
51 | return out; | |
52 | } | |
53 | ||
54 | static void decode_buf(struct dev_context *devc, unsigned char *data) | |
55 | { | |
56 | struct sr_datafeed_packet packet; | |
57 | struct sr_datafeed_analog analog; | |
58 | long factor, ivalue; | |
59 | uint8_t digits[4]; | |
2980cc24 UH |
60 | gboolean is_duty, is_continuity, is_diode, is_ac, is_dc, is_auto; |
61 | gboolean is_hold, is_max, is_min, is_relative, minus; | |
b84c13d7 BV |
62 | float fvalue; |
63 | ||
64 | digits[0] = decode_digit(data[12]); | |
65 | digits[1] = decode_digit(data[11]); | |
66 | digits[2] = decode_digit(data[10]); | |
67 | digits[3] = decode_digit(data[9]); | |
69a74024 | 68 | |
b84c13d7 BV |
69 | if (digits[0] == 0x0f && digits[1] == 0x00 && digits[2] == 0x0a && |
70 | digits[3] == 0x0f) | |
71 | /* The "over limit" (OL) display comes through like this */ | |
72 | ivalue = -1; | |
73 | else if (digits[0] > 9 || digits[1] > 9 || digits[2] > 9 || digits[3] > 9) | |
74 | /* An invalid digit in any position denotes no value. */ | |
75 | ivalue = -2; | |
76 | else { | |
77 | ivalue = digits[0] * 1000; | |
78 | ivalue += digits[1] * 100; | |
79 | ivalue += digits[2] * 10; | |
80 | ivalue += digits[3]; | |
81 | } | |
82 | ||
83 | /* Decimal point position */ | |
84 | switch (data[7] >> 4) { | |
85 | case 0x00: | |
86 | factor = 0; | |
87 | break; | |
88 | case 0x02: | |
89 | factor = 1; | |
90 | break; | |
91 | case 0x04: | |
92 | factor = 2; | |
93 | break; | |
94 | case 0x08: | |
95 | factor = 3; | |
96 | break; | |
97 | default: | |
2980cc24 | 98 | sr_err("Unknown decimal point value %.2x.", data[7]); |
b84c13d7 BV |
99 | } |
100 | ||
101 | /* Minus flag */ | |
102 | minus = data[2] & 0x01; | |
103 | ||
104 | /* Mode detail symbols on the right side of the digits */ | |
105 | is_duty = is_continuity = is_diode = FALSE; | |
106 | switch (data[4]) { | |
107 | case 0x00: | |
108 | /* None. */ | |
109 | break; | |
110 | case 0x01: | |
111 | /* Micro */ | |
112 | factor += 6; | |
113 | break; | |
114 | case 0x02: | |
115 | /* Milli */ | |
116 | factor += 3; | |
117 | break; | |
118 | case 0x04: | |
119 | /* Kilo */ | |
120 | ivalue *= 1000; | |
121 | break; | |
122 | case 0x08: | |
123 | /* Mega */ | |
124 | ivalue *= 1000000; | |
125 | break; | |
126 | case 0x10: | |
127 | /* Continuity shows up as Ohm + this bit */ | |
128 | is_continuity = TRUE; | |
129 | break; | |
130 | case 0x20: | |
131 | /* Diode tester is Volt + this bit */ | |
132 | is_diode = TRUE; | |
133 | break; | |
134 | case 0x40: | |
135 | is_duty = TRUE; | |
136 | break; | |
137 | case 0x80: | |
138 | /* Never seen */ | |
2980cc24 | 139 | sr_dbg("Unknown mode right detail %.2x.", data[4]); |
b84c13d7 BV |
140 | break; |
141 | default: | |
2980cc24 | 142 | sr_dbg("Unknown/invalid mode right detail %.2x.", data[4]); |
b84c13d7 BV |
143 | } |
144 | ||
145 | /* Scale flags on the right, continued */ | |
146 | is_max = is_min = TRUE; | |
147 | if (data[5] & 0x04) | |
148 | is_max = TRUE; | |
149 | if (data[5] & 0x08) | |
150 | is_min = TRUE; | |
151 | if (data[5] & 0x40) | |
152 | /* Nano */ | |
153 | factor += 9; | |
154 | ||
155 | /* Mode detail symbols on the left side of the digits */ | |
156 | is_auto = is_dc = is_ac = is_hold = is_relative = FALSE; | |
157 | if (data[6] & 0x04) | |
158 | is_auto = TRUE; | |
159 | if (data[6] & 0x08) | |
160 | is_dc = TRUE; | |
161 | if (data[6] & 0x10) | |
162 | is_ac = TRUE; | |
163 | if (data[6] & 0x20) | |
164 | is_relative = TRUE; | |
165 | if (data[6] & 0x40) | |
166 | is_hold = TRUE; | |
167 | ||
168 | fvalue = (float)ivalue / pow(10, factor); | |
169 | if (minus) | |
170 | fvalue = -fvalue; | |
171 | ||
172 | memset(&analog, 0, sizeof(struct sr_datafeed_analog)); | |
173 | ||
174 | /* Measurement mode */ | |
175 | analog.mq = -1; | |
176 | switch (data[3]) { | |
177 | case 0x00: | |
178 | if (is_duty) { | |
179 | analog.mq = SR_MQ_DUTY_CYCLE; | |
180 | analog.unit = SR_UNIT_PERCENTAGE; | |
181 | } else | |
2980cc24 | 182 | sr_dbg("Unknown measurement mode %.2x.", data[3]); |
b84c13d7 BV |
183 | break; |
184 | case 0x01: | |
185 | if (is_diode) { | |
186 | analog.mq = SR_MQ_VOLTAGE; | |
187 | analog.unit = SR_UNIT_VOLT; | |
188 | analog.mqflags |= SR_MQFLAG_DIODE; | |
189 | if (ivalue < 0) | |
190 | fvalue = NAN; | |
191 | } else { | |
192 | if (ivalue < 0) | |
193 | break; | |
194 | analog.mq = SR_MQ_VOLTAGE; | |
195 | analog.unit = SR_UNIT_VOLT; | |
196 | if (is_ac) | |
197 | analog.mqflags |= SR_MQFLAG_AC; | |
198 | if (is_dc) | |
199 | analog.mqflags |= SR_MQFLAG_DC; | |
200 | } | |
201 | break; | |
202 | case 0x02: | |
203 | analog.mq = SR_MQ_CURRENT; | |
204 | analog.unit = SR_UNIT_AMPERE; | |
205 | if (is_ac) | |
206 | analog.mqflags |= SR_MQFLAG_AC; | |
207 | if (is_dc) | |
208 | analog.mqflags |= SR_MQFLAG_DC; | |
209 | break; | |
210 | case 0x04: | |
211 | if (is_continuity) { | |
212 | analog.mq = SR_MQ_CONTINUITY; | |
213 | analog.unit = SR_UNIT_BOOLEAN; | |
214 | fvalue = ivalue < 0 ? 0.0 : 1.0; | |
215 | } else { | |
216 | analog.mq = SR_MQ_RESISTANCE; | |
217 | analog.unit = SR_UNIT_OHM; | |
69a74024 BV |
218 | if (ivalue < 0) |
219 | fvalue = INFINITY; | |
b84c13d7 BV |
220 | } |
221 | break; | |
222 | case 0x08: | |
223 | /* Never seen */ | |
2980cc24 | 224 | sr_dbg("Unknown measurement mode %.2x.", data[3]); |
b84c13d7 BV |
225 | break; |
226 | case 0x10: | |
227 | analog.mq = SR_MQ_FREQUENCY; | |
69a74024 | 228 | analog.unit = SR_UNIT_HERTZ; |
b84c13d7 BV |
229 | break; |
230 | case 0x20: | |
231 | analog.mq = SR_MQ_CAPACITANCE; | |
232 | analog.unit = SR_UNIT_FARAD; | |
233 | break; | |
234 | case 0x40: | |
235 | analog.mq = SR_MQ_TEMPERATURE; | |
236 | analog.unit = SR_UNIT_CELSIUS; | |
237 | break; | |
238 | case 0x80: | |
239 | analog.mq = SR_MQ_TEMPERATURE; | |
240 | analog.unit = SR_UNIT_FAHRENHEIT; | |
241 | break; | |
242 | default: | |
2980cc24 | 243 | sr_dbg("Unknown/invalid measurement mode %.2x.", data[3]); |
b84c13d7 BV |
244 | } |
245 | if (analog.mq == -1) | |
246 | return; | |
247 | ||
248 | if (is_auto) | |
249 | analog.mqflags |= SR_MQFLAG_AUTORANGE; | |
250 | if (is_hold) | |
251 | analog.mqflags |= SR_MQFLAG_HOLD; | |
252 | if (is_max) | |
253 | analog.mqflags |= SR_MQFLAG_MAX; | |
254 | if (is_min) | |
255 | analog.mqflags |= SR_MQFLAG_MIN; | |
256 | if (is_relative) | |
257 | analog.mqflags |= SR_MQFLAG_RELATIVE; | |
258 | ||
259 | analog.num_samples = 1; | |
260 | analog.data = &fvalue; | |
261 | packet.type = SR_DF_ANALOG; | |
262 | packet.payload = &analog; | |
263 | sr_session_send(devc->cb_data, &packet); | |
264 | ||
265 | devc->num_samples++; | |
b84c13d7 BV |
266 | } |
267 | ||
268 | static int victor70c_data(struct sr_dev_inst *sdi) | |
269 | { | |
270 | struct dev_context *devc; | |
271 | GString *dbg; | |
272 | int len, ret, i; | |
273 | unsigned char buf[DMM_DATA_SIZE], data[DMM_DATA_SIZE]; | |
274 | unsigned char obfuscation[DMM_DATA_SIZE] = "jodenxunickxia"; | |
275 | unsigned char shuffle[DMM_DATA_SIZE] = { | |
276 | 6, 13, 5, 11, 2, 7, 9, 8, 3, 10, 12, 0, 4, 1 | |
277 | }; | |
278 | ||
279 | devc = sdi->priv; | |
280 | ||
281 | if (sdi->status == SR_ST_INACTIVE) { | |
282 | /* First time through. */ | |
f6b8ffa6 BV |
283 | if (libusb_kernel_driver_active(devc->usb->devhdl, 0) == 1) { |
284 | if (libusb_detach_kernel_driver(devc->usb->devhdl, 0) < 0) { | |
2980cc24 | 285 | sr_err("Failed to detach kernel driver."); |
f6b8ffa6 BV |
286 | return SR_ERR; |
287 | } | |
288 | } | |
b84c13d7 BV |
289 | |
290 | if (libusb_claim_interface(devc->usb->devhdl, 0)) { | |
2980cc24 | 291 | sr_err("Failed to claim interface 0."); |
b84c13d7 BV |
292 | return SR_ERR; |
293 | } | |
294 | sdi->status = SR_ST_ACTIVE; | |
295 | } | |
296 | ||
297 | ret = libusb_interrupt_transfer(devc->usb->devhdl, 0x81, buf, DMM_DATA_SIZE, | |
298 | &len, 100); | |
299 | if (ret != 0) { | |
2980cc24 | 300 | sr_err("Failed to get data: libusb error %d.", ret); |
b84c13d7 BV |
301 | return SR_ERR; |
302 | } | |
303 | ||
304 | if (len != DMM_DATA_SIZE) { | |
2980cc24 UH |
305 | sr_dbg("Short packet: received %d/%d bytes.", |
306 | len, DMM_DATA_SIZE); | |
b84c13d7 BV |
307 | return SR_ERR; |
308 | } | |
309 | ||
310 | for (i = 0; i < DMM_DATA_SIZE && buf[i] == 0; i++); | |
311 | if (i == DMM_DATA_SIZE) { | |
312 | /* This DMM outputs all zeroes from time to time, just ignore it. */ | |
2980cc24 | 313 | sr_dbg("Received all zeroes."); |
b84c13d7 BV |
314 | return SR_OK; |
315 | } | |
316 | ||
317 | /* Deobfuscate and reorder data. */ | |
318 | for (i = 0; i < DMM_DATA_SIZE; i++) | |
319 | data[shuffle[i]] = (buf[i] - obfuscation[i]) & 0xff; | |
320 | ||
321 | if (sr_log_loglevel_get() >= SR_LOG_SPEW) { | |
322 | dbg = g_string_sized_new(128); | |
2980cc24 | 323 | g_string_printf(dbg, "Deobfuscated."); |
b84c13d7 BV |
324 | for (i = 0; i < DMM_DATA_SIZE; i++) |
325 | g_string_append_printf(dbg, " %.2x", data[i]); | |
326 | sr_spew("%s", dbg->str); | |
327 | g_string_free(dbg, TRUE); | |
328 | } | |
329 | ||
330 | decode_buf(devc, data); | |
331 | ||
332 | return SR_OK; | |
333 | } | |
334 | ||
b84c13d7 BV |
335 | SR_PRIV struct dmmchip dmmchip_victor70c = { |
336 | .data = victor70c_data, | |
337 | }; |