]> sigrok.org Git - libsigrok.git/blame - hardware/common/dmm/es51922.c
es51922/fs9721/fs9922/metex14: Use diode MQFLAG.
[libsigrok.git] / hardware / common / dmm / es51922.c
CommitLineData
fe0c0b98 1/*
50985c20 2 * This file is part of the libsigrok project.
fe0c0b98
UH
3 *
4 * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
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 2 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, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21/*
22 * Cyrustek ES51922 protocol parser.
23 *
24 * Communication parameters: Unidirectional, 19230/7o1
25 */
26
27#include <string.h>
28#include <ctype.h>
29#include <math.h>
30#include <glib.h>
31#include "libsigrok.h"
32#include "libsigrok-internal.h"
33
29a27196
UH
34/* Message logging helpers with subsystem-specific prefix string. */
35#define LOG_PREFIX "es51922: "
36#define sr_log(l, s, args...) sr_log(l, LOG_PREFIX s, ## args)
37#define sr_spew(s, args...) sr_spew(LOG_PREFIX s, ## args)
38#define sr_dbg(s, args...) sr_dbg(LOG_PREFIX s, ## args)
39#define sr_info(s, args...) sr_info(LOG_PREFIX s, ## args)
40#define sr_warn(s, args...) sr_warn(LOG_PREFIX s, ## args)
41#define sr_err(s, args...) sr_err(LOG_PREFIX s, ## args)
fe0c0b98
UH
42
43/* Factors for the respective measurement mode (0 means "invalid"). */
44static const float factors[8][8] = {
45 {1e-4, 1e-3, 1e-2, 1e-1, 1e-5, 0, 0, 0}, /* V */
46 {1e-8, 1e-7, 0, 0, 0, 0, 0, 0}, /* uA */
47 {1e-6, 1e-5, 0, 0, 0, 0, 0, 0}, /* mA */
48 {1e-3, 0, 0, 0, 0, 0, 0, 0}, /* 22A */
49 {1e-4, 1e-3, 1e-2, 1e-1, 1, 0, 0, 0}, /* Manual A */
50 {1e-2, 1e-1, 1, 1e1, 1e2, 1e3, 1e4, 0}, /* Resistance */
51 {1e-2, 1e-1, 0, 1, 1e1, 1e2, 1e3, 1e4}, /* Frequency */
52 {1e-12, 1e-11, 1e-10, 1e-9, 1e-8, 1e-7, 1e-6, 1e-5}, /* Capacitance */
53};
54
55static int parse_value(const uint8_t *buf, float *result)
56{
57 int sign, intval;
58 float floatval;
59
60 /*
61 * Bytes 1-5: Value (5 decimal digits)
62 *
63 * Over limit: "0L." on the display, "22580" as protocol "digits".
64 * (chip max. value is 22000, so 22580 is out of range)
65 *
66 * Example: "OL.", auto-range mega-ohm mode
67 * Hex: 36 32 32 35 38 30 33 31 30 30 32 30 0d 0a
68 * ASCII: 2 2 5 8 0
69 */
70 if (!strncmp((const char *)&buf[1], "22580", 5)) {
71 sr_spew("Over limit.");
72 *result = INFINITY;
73 return SR_OK;
74 } else if (!isdigit(buf[1]) || !isdigit(buf[2]) ||
75 !isdigit(buf[3]) || !isdigit(buf[4]) || !isdigit(buf[5])) {
76 sr_err("Value contained invalid digits: %02x %02x %02x %02x "
77 "%02x (%c %c %c %c %c).",
78 buf[1], buf[2], buf[3], buf[4], buf[5]);
79 return SR_ERR;
80 }
81 intval = 0;
82 intval += (buf[1] - '0') * 10000;
83 intval += (buf[2] - '0') * 1000;
84 intval += (buf[3] - '0') * 100;
85 intval += (buf[4] - '0') * 10;
86 intval += (buf[5] - '0') * 1;
87
88 floatval = (float)intval;
89
90 /* Note: The decimal point position will be parsed later. */
91
92 /* Byte 7: Sign bit (and other stuff) */
93 sign = ((buf[7] & (1 << 2)) != 0) ? -1 : 1;
94
95 /* Apply sign. */
96 floatval *= sign;
97
98 sr_spew("The display value is %f.", floatval);
99
100 *result = floatval;
101
102 return SR_OK;
103}
104
105static int parse_range(uint8_t b, float *floatval,
106 const struct es51922_info *info)
107{
108 int idx, mode;
109
110 idx = b - '0';
111
112 if (!(idx >= 0 && idx <= 7)) {
113 sr_dbg("Invalid range byte / index: 0x%02x / 0x%02x.", b, idx);
114 return SR_ERR;
115 }
116
117 /* Parse range byte (depends on the measurement mode). */
118 if (info->is_voltage)
119 mode = 0; /* V */
120 else if (info->is_current && info->is_micro)
121 mode = 1; /* uA */
122 else if (info->is_current && info->is_milli)
123 mode = 2; /* mA */
124 else if (info->is_current && !info->is_micro && !info->is_milli)
125 mode = 3; /* 22A */
126 else if (info->is_current && !info->is_auto)
127 mode = 4; /* Manual A */
128 else if (info->is_resistance)
129 mode = 5; /* Resistance */
130 else if (info->is_frequency)
131 mode = 6; /* Frequency */
132 else if (info->is_capacitance)
133 mode = 7; /* Capacitance */
134 else {
135 sr_dbg("Invalid mode, range byte was: 0x%02x.", b);
136 return SR_ERR;
137 }
138
139 if (factors[mode][idx] == 0) {
140 sr_dbg("Invalid factor for range byte: 0x%02x.", b);
141 return SR_ERR;
142 }
143
144 /* Apply respective factor (mode-dependent) on the value. */
145 *floatval *= factors[mode][idx];
146 sr_dbg("Applying factor %f, new value is %f.",
147 factors[mode][idx], *floatval);
148
149 return SR_OK;
150}
151
152static void parse_flags(const uint8_t *buf, struct es51922_info *info)
153{
154 /* Get is_judge and is_vbar early on, we'll need it. */
155 info->is_judge = (buf[7] & (1 << 3)) != 0;
156 info->is_vbar = (buf[11] & (1 << 2)) != 0;
157
158 /* Byte 6: Function */
159 switch (buf[6]) {
160 case 0x3b: /* V */
161 info->is_voltage = TRUE;
162 break;
163 case 0x3d: /* uA */
164 info->is_auto = info->is_micro = info->is_current = TRUE;
165 break;
166 case 0x3f: /* mA */
167 info->is_auto = info->is_milli = info->is_current = TRUE;
168 break;
169 case 0x30: /* 22A */
170 info->is_current = TRUE;
171 break;
172 case 0x39: /* Manual A */
173 info->is_auto = FALSE; /* Manual mode */
174 info->is_current = TRUE;
175 break;
176 case 0x33: /* Resistance */
177 info->is_resistance = TRUE;
178 break;
179 case 0x35: /* Continuity */
180 info->is_continuity = TRUE;
181 break;
182 case 0x31: /* Diode */
183 info->is_diode = TRUE;
184 break;
185 case 0x32: /* Frequency / duty cycle */
186 if (info->is_judge)
187 info->is_frequency = TRUE;
188 else
189 info->is_duty_cycle = TRUE;
190 break;
191 case 0x36: /* Capacitance */
192 info->is_capacitance = TRUE;
193 break;
194 case 0x34: /* Temperature */
195 info->is_temperature = TRUE;
196 if (info->is_judge)
197 info->is_celsius = TRUE;
198 else
199 info->is_fahrenheit = TRUE;
200 /* IMPORTANT: The digits always represent Celsius! */
201 break;
202 case 0x3e: /* ADP */
203 info->is_adp = TRUE;
204 break;
205 default:
206 sr_err("Invalid function byte: 0x%02x.", buf[6]);
207 break;
208 }
209
210 /* Byte 7: Status */
211 /* Bits [6:4]: Always 0b011 */
212 info->is_judge = (buf[7] & (1 << 3)) != 0;
213 info->is_sign = (buf[7] & (1 << 2)) != 0;
214 info->is_batt = (buf[7] & (1 << 1)) != 0; /* Battery low */
215 info->is_ol = (buf[7] & (1 << 0)) != 0; /* Input overflow */
216
217 /* Byte 8: Option 1 */
218 /* Bits [6:4]: Always 0b011 */
219 info->is_max = (buf[8] & (1 << 3)) != 0;
220 info->is_min = (buf[8] & (1 << 2)) != 0;
221 info->is_rel = (buf[8] & (1 << 1)) != 0;
222 info->is_rmr = (buf[8] & (1 << 0)) != 0;
223
224 /* Byte 9: Option 2 */
225 /* Bits [6:4]: Always 0b011 */
226 info->is_ul = (buf[9] & (1 << 3)) != 0;
227 info->is_pmax = (buf[9] & (1 << 2)) != 0; /* Max. peak value */
228 info->is_pmin = (buf[9] & (1 << 1)) != 0; /* Min. peak value */
229 /* Bit 0: Always 0 */
230
231 /* Byte 10: Option 3 */
232 /* Bits [6:4]: Always 0b011 */
233 info->is_dc = (buf[10] & (1 << 3)) != 0;
234 info->is_ac = (buf[10] & (1 << 2)) != 0;
235 info->is_auto = (buf[10] & (1 << 1)) != 0;
236 info->is_vahz = (buf[10] & (1 << 0)) != 0;
237
238 /* Byte 11: Option 4 */
239 /* Bits [6:3]: Always 0b0110 */
240 info->is_vbar = (buf[11] & (1 << 2)) != 0;
241 info->is_hold = (buf[11] & (1 << 1)) != 0;
242 info->is_lpf = (buf[11] & (1 << 0)) != 0; /* Low pass filter on */
243
244 /* Byte 12: Always '\r' (carriage return, 0x0d, 13) */
245
246 /* Byte 13: Always '\n' (newline, 0x0a, 10) */
247}
248
249static void handle_flags(struct sr_datafeed_analog *analog,
250 float *floatval, const struct es51922_info *info)
251{
252 /*
253 * Note: is_micro etc. are not used directly to multiply/divide
254 * floatval, this is handled via parse_range() and factors[][].
255 */
256
257 /* Measurement modes */
258 if (info->is_voltage) {
259 analog->mq = SR_MQ_VOLTAGE;
260 analog->unit = SR_UNIT_VOLT;
261 }
262 if (info->is_current) {
263 analog->mq = SR_MQ_CURRENT;
264 analog->unit = SR_UNIT_AMPERE;
265 }
266 if (info->is_resistance) {
267 analog->mq = SR_MQ_RESISTANCE;
268 analog->unit = SR_UNIT_OHM;
269 }
270 if (info->is_frequency) {
271 analog->mq = SR_MQ_FREQUENCY;
272 analog->unit = SR_UNIT_HERTZ;
273 }
274 if (info->is_capacitance) {
275 analog->mq = SR_MQ_CAPACITANCE;
276 analog->unit = SR_UNIT_FARAD;
277 }
278 if (info->is_temperature && info->is_celsius) {
279 analog->mq = SR_MQ_TEMPERATURE;
280 analog->unit = SR_UNIT_CELSIUS;
281 }
282 if (info->is_temperature && info->is_fahrenheit) {
283 analog->mq = SR_MQ_TEMPERATURE;
284 analog->unit = SR_UNIT_FAHRENHEIT;
285 }
286 if (info->is_continuity) {
287 analog->mq = SR_MQ_CONTINUITY;
288 analog->unit = SR_UNIT_BOOLEAN;
289 *floatval = (*floatval < 0.0) ? 0.0 : 1.0;
290 }
291 if (info->is_diode) {
292 analog->mq = SR_MQ_VOLTAGE;
293 analog->unit = SR_UNIT_VOLT;
294 }
295 if (info->is_duty_cycle) {
296 analog->mq = SR_MQ_DUTY_CYCLE;
297 analog->unit = SR_UNIT_PERCENTAGE;
298 }
299
300 /* Measurement related flags */
301 if (info->is_ac)
302 analog->mqflags |= SR_MQFLAG_AC;
303 if (info->is_dc)
304 analog->mqflags |= SR_MQFLAG_DC;
305 if (info->is_auto)
306 analog->mqflags |= SR_MQFLAG_AUTORANGE;
a6ed50f4
UH
307 if (info->is_diode)
308 analog->mqflags |= SR_MQFLAG_DIODE;
fe0c0b98
UH
309 if (info->is_hold)
310 /*
311 * Note: HOLD only affects the number displayed on the LCD,
312 * but not the value sent via the protocol! It also does not
313 * affect the bargraph on the LCD.
314 */
315 analog->mqflags |= SR_MQFLAG_HOLD;
316 if (info->is_max)
317 analog->mqflags |= SR_MQFLAG_MAX;
318 if (info->is_min)
319 analog->mqflags |= SR_MQFLAG_MIN;
320 if (info->is_rel)
321 analog->mqflags |= SR_MQFLAG_RELATIVE;
322
323 /* Other flags */
324 if (info->is_judge)
325 sr_spew("Judge bit is set.");
326 if (info->is_batt)
327 sr_spew("Battery is low.");
328 if (info->is_ol)
329 sr_spew("Input overflow.");
330 if (info->is_pmax)
331 sr_spew("pMAX active, LCD shows max. peak value.");
332 if (info->is_pmin)
333 sr_spew("pMIN active, LCD shows min. peak value.");
334 if (info->is_vahz)
335 sr_spew("VAHZ active.");
336 if (info->is_vbar)
337 sr_spew("VBAR active.");
338 if (info->is_lpf)
339 sr_spew("Low-pass filter feature is active.");
340}
341
342static gboolean flags_valid(const struct es51922_info *info)
343{
344 int count;
345
346 /* Does the packet have more than one multiplier? */
347 count = 0;
348 count += (info->is_nano) ? 1 : 0;
349 count += (info->is_micro) ? 1 : 0;
350 count += (info->is_milli) ? 1 : 0;
351 /* Note: No 'kilo' or 'mega' bits per se in this protocol. */
352 if (count > 1) {
353 sr_err("More than one multiplier detected in packet.");
354 return FALSE;
355 }
356
357 /* Does the packet "measure" more than one type of value? */
358 count = 0;
359 count += (info->is_voltage) ? 1 : 0;
360 count += (info->is_current) ? 1 : 0;
361 count += (info->is_resistance) ? 1 : 0;
362 count += (info->is_frequency) ? 1 : 0;
363 count += (info->is_capacitance) ? 1 : 0;
364 count += (info->is_temperature) ? 1 : 0;
365 count += (info->is_continuity) ? 1 : 0;
366 count += (info->is_diode) ? 1 : 0;
367 count += (info->is_duty_cycle) ? 1 : 0;
368 if (count > 1) {
369 sr_err("More than one measurement type detected in packet.");
370 return FALSE;
371 }
372
373 /* Both AC and DC set? */
374 if (info->is_ac && info->is_dc) {
375 sr_err("Both AC and DC flags detected in packet.");
376 return FALSE;
377 }
378
379 return TRUE;
380}
381
382SR_PRIV gboolean sr_es51922_packet_valid(const uint8_t *buf)
383{
384 struct es51922_info info;
385
386 memset(&info, 0x00, sizeof(struct es51922_info));
387 parse_flags(buf, &info);
388
389 if (!flags_valid(&info))
390 return FALSE;
391
392 if (buf[12] != '\r' || buf[13] != '\n') {
393 sr_spew("Packet doesn't end with \\r\\n.");
394 return FALSE;
395 }
396
397 return TRUE;
398}
399
400/**
401 * Parse a protocol packet.
402 *
403 * @param buf Buffer containing the protocol packet. Must not be NULL.
404 * @param floatval Pointer to a float variable. That variable will contain the
405 * result value upon parsing success. Must not be NULL.
406 * @param analog Pointer to a struct sr_datafeed_analog. The struct will be
407 * filled with data according to the protocol packet.
408 * Must not be NULL.
409 * @param info Pointer to a struct es51922_info. The struct will be filled
410 * with data according to the protocol packet. Must not be NULL.
411 *
412 * @return SR_OK upon success, SR_ERR upon failure. Upon errors, the
413 * 'analog' variable contents are undefined and should not be used.
414 */
415SR_PRIV int sr_es51922_parse(const uint8_t *buf, float *floatval,
416 struct sr_datafeed_analog *analog, void *info)
417{
418 int ret;
419 struct es51922_info *info_local;
420
421 info_local = (struct es51922_info *)info;
422
423 if ((ret = parse_value(buf, floatval)) != SR_OK) {
424 sr_err("Error parsing value: %d.", ret);
425 return ret;
426 }
427
428 memset(info_local, 0x00, sizeof(struct es51922_info));
429 parse_flags(buf, info_local);
430 handle_flags(analog, floatval, info_local);
431
432 return parse_range(buf[0], floatval, info_local);
433}