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