]> sigrok.org Git - libsigrok.git/blame - src/dmm/fs9721.c
device.c: Whitespace/cosmetics and typo fixes.
[libsigrok.git] / src / dmm / fs9721.c
CommitLineData
6c701476 1/*
50985c20 2 * This file is part of the libsigrok project.
6c701476
UH
3 *
4 * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
8c1adf37 5 * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com>
6c701476
UH
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22/*
23 * Fortune Semiconductor FS9721_LP3/FS9721B protocol parser.
24 *
25 * FS9721_LP3: 4000 counts (3 3/4 digits)
26 * FS9721B/Q100: 2400 counts (3 2/3 digits)
27 *
28 * Same for both chips:
29 * - Packages: Bare die (78 pins) or QFP-100
30 * - Communication parameters: Unidirectional, 2400/8n1
31 * - The protocol seems to be exactly the same.
32 */
33
6ec6c43b 34#include <config.h>
6c701476
UH
35#include <string.h>
36#include <ctype.h>
37#include <math.h>
38#include <glib.h>
c1aae900 39#include <libsigrok/libsigrok.h>
6c701476
UH
40#include "libsigrok-internal.h"
41
3544f848 42#define LOG_PREFIX "fs9721"
6c701476
UH
43
44static int parse_digit(uint8_t b)
45{
46 switch (b) {
47 case 0x7d:
48 return 0;
49 case 0x05:
50 return 1;
51 case 0x5b:
52 return 2;
53 case 0x1f:
54 return 3;
55 case 0x27:
56 return 4;
57 case 0x3e:
58 return 5;
59 case 0x7e:
60 return 6;
61 case 0x15:
62 return 7;
63 case 0x7f:
64 return 8;
65 case 0x3f:
66 return 9;
67 default:
ec5186f9 68 sr_dbg("Invalid digit byte: 0x%02x.", b);
6c701476
UH
69 return -1;
70 }
71}
72
8c1adf37
UH
73static gboolean sync_nibbles_valid(const uint8_t *buf)
74{
75 int i;
76
77 /* Check the synchronization nibbles, and make sure they all match. */
78 for (i = 0; i < FS9721_PACKET_SIZE; i++) {
79 if (((buf[i] >> 4) & 0x0f) != (i + 1)) {
ec5186f9 80 sr_dbg("Sync nibble in byte %d (0x%02x) is invalid.",
8c1adf37
UH
81 i, buf[i]);
82 return FALSE;
83 }
84 }
85
86 return TRUE;
87}
88
89static gboolean flags_valid(const struct fs9721_info *info)
90{
91 int count;
92
93 /* Does the packet have more than one multiplier? */
94 count = 0;
95 count += (info->is_nano) ? 1 : 0;
96 count += (info->is_micro) ? 1 : 0;
97 count += (info->is_milli) ? 1 : 0;
98 count += (info->is_kilo) ? 1 : 0;
99 count += (info->is_mega) ? 1 : 0;
100 if (count > 1) {
ec5186f9 101 sr_dbg("More than one multiplier detected in packet.");
8c1adf37
UH
102 return FALSE;
103 }
104
105 /* Does the packet "measure" more than one type of value? */
106 count = 0;
107 count += (info->is_hz) ? 1 : 0;
108 count += (info->is_ohm) ? 1 : 0;
109 count += (info->is_farad) ? 1 : 0;
110 count += (info->is_ampere) ? 1 : 0;
111 count += (info->is_volt) ? 1 : 0;
112 count += (info->is_percent) ? 1 : 0;
113 if (count > 1) {
ec5186f9 114 sr_dbg("More than one measurement type detected in packet.");
8c1adf37
UH
115 return FALSE;
116 }
117
118 /* Both AC and DC set? */
119 if (info->is_ac && info->is_dc) {
ec5186f9 120 sr_dbg("Both AC and DC flags detected in packet.");
8c1adf37
UH
121 return FALSE;
122 }
123
124 /* RS232 flag not set? */
125 if (!info->is_rs232) {
ec5186f9 126 sr_dbg("No RS232 flag detected in packet.");
8c1adf37
UH
127 return FALSE;
128 }
129
130 return TRUE;
131}
132
70e28e78 133static int parse_value(const uint8_t *buf, float *result, int *exponent)
6c701476
UH
134{
135 int i, sign, intval = 0, digits[4];
136 uint8_t digit_bytes[4];
137 float floatval;
138
139 /* Byte 1: LCD SEG2 */
140 sign = ((buf[1] & (1 << 3)) != 0) ? -1 : 1;
141
142 /*
143 * Bytes 1-8: Value (4 decimal digits, sign, decimal point)
144 *
145 * Over limit: "0L" (LCD), 0x00 0x7d 0x68 0x00 (digit bytes).
146 */
147
148 /* Merge the two nibbles for a digit into one byte. */
149 for (i = 0; i < 4; i++) {
150 digit_bytes[i] = ((buf[1 + (i * 2)] & 0x0f) << 4);
151 digit_bytes[i] |= (buf[1 + (i * 2) + 1] & 0x0f);
152
153 /* Bit 7 in the byte is not part of the digit. */
154 digit_bytes[i] &= ~(1 << 7);
155 }
156
157 /* Check for "OL". */
158 if (digit_bytes[0] == 0x00 && digit_bytes[1] == 0x7d &&
159 digit_bytes[2] == 0x68 && digit_bytes[3] == 0x00) {
160 sr_spew("Over limit.");
161 *result = INFINITY;
162 return SR_OK;
163 }
164
165 /* Parse the digits. */
166 for (i = 0; i < 4; i++)
167 digits[i] = parse_digit(digit_bytes[i]);
168 sr_spew("Digits: %02x %02x %02x %02x (%d%d%d%d).",
169 digit_bytes[0], digit_bytes[1], digit_bytes[2], digit_bytes[3],
170 digits[0], digits[1], digits[2], digits[3]);
171
172 /* Merge all digits into an integer value. */
173 for (i = 0; i < 4; i++) {
174 intval *= 10;
175 intval += digits[i];
176 }
177
6c701476
UH
178 floatval = (float)intval;
179
180 /* Decimal point position. */
181 if ((buf[3] & (1 << 3)) != 0) {
70e28e78 182 *exponent = -3;
6c701476
UH
183 sr_spew("Decimal point after first digit.");
184 } else if ((buf[5] & (1 << 3)) != 0) {
70e28e78 185 *exponent = -2;
6c701476
UH
186 sr_spew("Decimal point after second digit.");
187 } else if ((buf[7] & (1 << 3)) != 0) {
70e28e78 188 *exponent = -1;
6c701476
UH
189 sr_spew("Decimal point after third digit.");
190 } else {
70e28e78 191 *exponent = 0;
6c701476
UH
192 sr_spew("No decimal point in the number.");
193 }
194
195 /* Apply sign. */
196 floatval *= sign;
197
198 sr_spew("The display value is %f.", floatval);
199
200 *result = floatval;
201
202 return SR_OK;
203}
204
8c1adf37 205static void parse_flags(const uint8_t *buf, struct fs9721_info *info)
6c701476 206{
6c701476 207 /* Byte 0: LCD SEG1 */
8c1adf37
UH
208 info->is_ac = (buf[0] & (1 << 3)) != 0;
209 info->is_dc = (buf[0] & (1 << 2)) != 0;
210 info->is_auto = (buf[0] & (1 << 1)) != 0;
211 info->is_rs232 = (buf[0] & (1 << 0)) != 0;
212
213 /* Byte 1: LCD SEG2 */
214 info->is_sign = (buf[1] & (1 << 3)) != 0;
6c701476
UH
215
216 /* Byte 9: LCD SEG10 */
8c1adf37
UH
217 info->is_micro = (buf[9] & (1 << 3)) != 0;
218 info->is_nano = (buf[9] & (1 << 2)) != 0;
219 info->is_kilo = (buf[9] & (1 << 1)) != 0;
220 info->is_diode = (buf[9] & (1 << 0)) != 0;
6c701476
UH
221
222 /* Byte 10: LCD SEG11 */
8c1adf37
UH
223 info->is_milli = (buf[10] & (1 << 3)) != 0;
224 info->is_percent = (buf[10] & (1 << 2)) != 0;
225 info->is_mega = (buf[10] & (1 << 1)) != 0;
226 info->is_beep = (buf[10] & (1 << 0)) != 0;
6c701476
UH
227
228 /* Byte 11: LCD SEG12 */
8c1adf37
UH
229 info->is_farad = (buf[11] & (1 << 3)) != 0;
230 info->is_ohm = (buf[11] & (1 << 2)) != 0;
231 info->is_rel = (buf[11] & (1 << 1)) != 0;
232 info->is_hold = (buf[11] & (1 << 0)) != 0;
6c701476
UH
233
234 /* Byte 12: LCD SEG13 */
8c1adf37
UH
235 info->is_ampere = (buf[12] & (1 << 3)) != 0;
236 info->is_volt = (buf[12] & (1 << 2)) != 0;
237 info->is_hz = (buf[12] & (1 << 1)) != 0;
238 info->is_bat = (buf[12] & (1 << 0)) != 0;
6c701476
UH
239
240 /* Byte 13: LCD SEG14 */
8c1adf37
UH
241 info->is_c2c1_11 = (buf[13] & (1 << 3)) != 0;
242 info->is_c2c1_10 = (buf[13] & (1 << 2)) != 0;
243 info->is_c2c1_01 = (buf[13] & (1 << 1)) != 0;
244 info->is_c2c1_00 = (buf[13] & (1 << 0)) != 0;
245}
6c701476 246
b02bb45f 247static void handle_flags(struct sr_datafeed_analog *analog, float *floatval,
70e28e78 248 int *exponent, const struct fs9721_info *info)
8c1adf37 249{
6c701476 250 /* Factors */
8c1adf37 251 if (info->is_nano)
70e28e78 252 *exponent -= 9;
8c1adf37 253 if (info->is_micro)
70e28e78 254 *exponent -= 6;
8c1adf37 255 if (info->is_milli)
70e28e78 256 *exponent -= 3;
8c1adf37 257 if (info->is_kilo)
70e28e78 258 *exponent += 3;
8c1adf37 259 if (info->is_mega)
70e28e78
AJ
260 *exponent += 6;
261 *floatval *= powf(10, *exponent);
6c701476
UH
262
263 /* Measurement modes */
8c1adf37 264 if (info->is_volt) {
b02bb45f
UH
265 analog->meaning->mq = SR_MQ_VOLTAGE;
266 analog->meaning->unit = SR_UNIT_VOLT;
6c701476 267 }
8c1adf37 268 if (info->is_ampere) {
b02bb45f
UH
269 analog->meaning->mq = SR_MQ_CURRENT;
270 analog->meaning->unit = SR_UNIT_AMPERE;
6c701476 271 }
8c1adf37 272 if (info->is_ohm) {
b02bb45f
UH
273 analog->meaning->mq = SR_MQ_RESISTANCE;
274 analog->meaning->unit = SR_UNIT_OHM;
6c701476 275 }
8c1adf37 276 if (info->is_hz) {
b02bb45f
UH
277 analog->meaning->mq = SR_MQ_FREQUENCY;
278 analog->meaning->unit = SR_UNIT_HERTZ;
6c701476 279 }
8c1adf37 280 if (info->is_farad) {
b02bb45f
UH
281 analog->meaning->mq = SR_MQ_CAPACITANCE;
282 analog->meaning->unit = SR_UNIT_FARAD;
6c701476 283 }
8c1adf37 284 if (info->is_beep) {
b02bb45f
UH
285 analog->meaning->mq = SR_MQ_CONTINUITY;
286 analog->meaning->unit = SR_UNIT_BOOLEAN;
8c1adf37 287 *floatval = (*floatval == INFINITY) ? 0.0 : 1.0;
6c701476 288 }
8c1adf37 289 if (info->is_diode) {
b02bb45f
UH
290 analog->meaning->mq = SR_MQ_VOLTAGE;
291 analog->meaning->unit = SR_UNIT_VOLT;
6c701476 292 }
8c1adf37 293 if (info->is_percent) {
b02bb45f
UH
294 analog->meaning->mq = SR_MQ_DUTY_CYCLE;
295 analog->meaning->unit = SR_UNIT_PERCENTAGE;
6c701476
UH
296 }
297
298 /* Measurement related flags */
8c1adf37 299 if (info->is_ac)
b02bb45f 300 analog->meaning->mqflags |= SR_MQFLAG_AC;
8c1adf37 301 if (info->is_dc)
b02bb45f 302 analog->meaning->mqflags |= SR_MQFLAG_DC;
8c1adf37 303 if (info->is_auto)
b02bb45f 304 analog->meaning->mqflags |= SR_MQFLAG_AUTORANGE;
a6ed50f4 305 if (info->is_diode)
b02bb45f 306 analog->meaning->mqflags |= SR_MQFLAG_DIODE;
8c1adf37 307 if (info->is_hold)
b02bb45f 308 analog->meaning->mqflags |= SR_MQFLAG_HOLD;
8c1adf37 309 if (info->is_rel)
b02bb45f 310 analog->meaning->mqflags |= SR_MQFLAG_RELATIVE;
6c701476
UH
311
312 /* Other flags */
8c1adf37 313 if (info->is_rs232)
6c701476 314 sr_spew("RS232 enabled.");
8c1adf37 315 if (info->is_bat)
6c701476 316 sr_spew("Battery is low.");
8c1adf37 317 if (info->is_c2c1_00)
6c701476 318 sr_spew("User-defined LCD symbol 0 is active.");
8c1adf37 319 if (info->is_c2c1_01)
6c701476 320 sr_spew("User-defined LCD symbol 1 is active.");
8c1adf37 321 if (info->is_c2c1_10)
6c701476 322 sr_spew("User-defined LCD symbol 2 is active.");
8c1adf37 323 if (info->is_c2c1_11)
6c701476 324 sr_spew("User-defined LCD symbol 3 is active.");
8c1adf37 325}
6c701476 326
8c1adf37
UH
327SR_PRIV gboolean sr_fs9721_packet_valid(const uint8_t *buf)
328{
329 struct fs9721_info info;
330
331 parse_flags(buf, &info);
332
333 return (sync_nibbles_valid(buf) && flags_valid(&info));
6c701476
UH
334}
335
336/**
337 * Parse a protocol packet.
338 *
93357bc3 339 * @param buf Buffer containing the 14-byte protocol packet. Must not be NULL.
8c1adf37 340 * @param floatval Pointer to a float variable. That variable will contain the
f3f19d11 341 * result value upon parsing success. Must not be NULL.
b02bb45f 342 * @param analog Pointer to a struct sr_datafeed_analog. The struct will be
6c701476 343 * filled with data according to the protocol packet.
93357bc3 344 * Must not be NULL.
8c1adf37 345 * @param info Pointer to a struct fs9721_info. The struct will be filled
93357bc3 346 * with data according to the protocol packet. Must not be NULL.
6c701476
UH
347 *
348 * @return SR_OK upon success, SR_ERR upon failure. Upon errors, the
349 * 'analog' variable contents are undefined and should not be used.
350 */
8c1adf37 351SR_PRIV int sr_fs9721_parse(const uint8_t *buf, float *floatval,
b02bb45f 352 struct sr_datafeed_analog *analog, void *info)
6c701476 353{
70e28e78 354 int ret, exponent = 0;
93357bc3
UH
355 struct fs9721_info *info_local;
356
357 info_local = (struct fs9721_info *)info;
6c701476 358
70e28e78 359 if ((ret = parse_value(buf, floatval, &exponent)) != SR_OK) {
ec5186f9 360 sr_dbg("Error parsing value: %d.", ret);
6c701476
UH
361 return ret;
362 }
363
93357bc3 364 parse_flags(buf, info_local);
70e28e78
AJ
365 handle_flags(analog, floatval, &exponent, info_local);
366
367 analog->encoding->digits = -exponent;
368 analog->spec->spec_digits = -exponent;
6c701476
UH
369
370 return SR_OK;
371}
48535594 372
b02bb45f 373SR_PRIV void sr_fs9721_00_temp_c(struct sr_datafeed_analog *analog, void *info)
48535594
UH
374{
375 struct fs9721_info *info_local;
376
377 info_local = (struct fs9721_info *)info;
378
379 /* User-defined FS9721_LP3 flag 'c2c1_00' means temperature (C). */
380 if (info_local->is_c2c1_00) {
b02bb45f
UH
381 analog->meaning->mq = SR_MQ_TEMPERATURE;
382 analog->meaning->unit = SR_UNIT_CELSIUS;
48535594
UH
383 }
384}
2451a20f 385
b02bb45f 386SR_PRIV void sr_fs9721_01_temp_c(struct sr_datafeed_analog *analog, void *info)
2451a20f
UH
387{
388 struct fs9721_info *info_local;
389
390 info_local = (struct fs9721_info *)info;
391
392 /* User-defined FS9721_LP3 flag 'c2c1_01' means temperature (C). */
393 if (info_local->is_c2c1_01) {
b02bb45f
UH
394 analog->meaning->mq = SR_MQ_TEMPERATURE;
395 analog->meaning->unit = SR_UNIT_CELSIUS;
2451a20f
UH
396 }
397}
398
b02bb45f 399SR_PRIV void sr_fs9721_10_temp_c(struct sr_datafeed_analog *analog, void *info)
2451a20f
UH
400{
401 struct fs9721_info *info_local;
402
403 info_local = (struct fs9721_info *)info;
404
405 /* User-defined FS9721_LP3 flag 'c2c1_10' means temperature (C). */
406 if (info_local->is_c2c1_10) {
b02bb45f
UH
407 analog->meaning->mq = SR_MQ_TEMPERATURE;
408 analog->meaning->unit = SR_UNIT_CELSIUS;
2451a20f
UH
409 }
410}
411
b02bb45f 412SR_PRIV void sr_fs9721_01_10_temp_f_c(struct sr_datafeed_analog *analog, void *info)
2451a20f
UH
413{
414 struct fs9721_info *info_local;
415
416 info_local = (struct fs9721_info *)info;
417
418 /* User-defined FS9721_LP3 flag 'c2c1_01' means temperature (F). */
419 if (info_local->is_c2c1_01) {
b02bb45f
UH
420 analog->meaning->mq = SR_MQ_TEMPERATURE;
421 analog->meaning->unit = SR_UNIT_FAHRENHEIT;
2451a20f
UH
422 }
423
424 /* User-defined FS9721_LP3 flag 'c2c1_10' means temperature (C). */
425 if (info_local->is_c2c1_10) {
b02bb45f
UH
426 analog->meaning->mq = SR_MQ_TEMPERATURE;
427 analog->meaning->unit = SR_UNIT_CELSIUS;
2451a20f
UH
428 }
429}
d327972b 430
b02bb45f 431SR_PRIV void sr_fs9721_max_c_min(struct sr_datafeed_analog *analog, void *info)
d327972b
UH
432{
433 struct fs9721_info *info_local;
434
435 info_local = (struct fs9721_info *)info;
436
437 /* User-defined FS9721_LP3 flag 'c2c1_00' means MAX. */
438 if (info_local->is_c2c1_00)
b02bb45f 439 analog->meaning->mqflags |= SR_MQFLAG_MAX;
d327972b
UH
440
441 /* User-defined FS9721_LP3 flag 'c2c1_01' means temperature (C). */
442 if (info_local->is_c2c1_01) {
b02bb45f
UH
443 analog->meaning->mq = SR_MQ_TEMPERATURE;
444 analog->meaning->unit = SR_UNIT_CELSIUS;
d327972b
UH
445 }
446
447 /* User-defined FS9721_LP3 flag 'c2c1_11' means MIN. */
448 if (info_local->is_c2c1_11)
b02bb45f 449 analog->meaning->mqflags |= SR_MQFLAG_MIN;
d327972b
UH
450
451}