]> sigrok.org Git - libsigrok.git/blob - src/dmm/fs9721.c
aa7746ef3be30d22c8d5f190784606ca11308a2e
[libsigrok.git] / src / dmm / fs9721.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
5  * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com>
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
34 #include <string.h>
35 #include <ctype.h>
36 #include <math.h>
37 #include <glib.h>
38 #include "libsigrok.h"
39 #include "libsigrok-internal.h"
40
41 #define LOG_PREFIX "fs9721"
42
43 static 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:
67                 sr_dbg("Invalid digit byte: 0x%02x.", b);
68                 return -1;
69         }
70 }
71
72 static 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)) {
79                         sr_dbg("Sync nibble in byte %d (0x%02x) is invalid.",
80                                i, buf[i]);
81                         return FALSE;
82                 }
83         }
84
85         return TRUE;
86 }
87
88 static 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) {
100                 sr_dbg("More than one multiplier detected in packet.");
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) {
113                 sr_dbg("More than one measurement type detected in packet.");
114                 return FALSE;
115         }
116
117         /* Both AC and DC set? */
118         if (info->is_ac && info->is_dc) {
119                 sr_dbg("Both AC and DC flags detected in packet.");
120                 return FALSE;
121         }
122
123         /* RS232 flag not set? */
124         if (!info->is_rs232) {
125                 sr_dbg("No RS232 flag detected in packet.");
126                 return FALSE;
127         }
128
129         return TRUE;
130 }
131
132 static int parse_value(const uint8_t *buf, float *result)
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
177         floatval = (float)intval;
178
179         /* Decimal point position. */
180         if ((buf[3] & (1 << 3)) != 0) {
181                 floatval /= 1000;
182                 sr_spew("Decimal point after first digit.");
183         } else if ((buf[5] & (1 << 3)) != 0) {
184                 floatval /= 100;
185                 sr_spew("Decimal point after second digit.");
186         } else if ((buf[7] & (1 << 3)) != 0) {
187                 floatval /= 10;
188                 sr_spew("Decimal point after third digit.");
189         } else {
190                 sr_spew("No decimal point in the number.");
191         }
192
193         /* Apply sign. */
194         floatval *= sign;
195
196         sr_spew("The display value is %f.", floatval);
197
198         *result = floatval;
199
200         return SR_OK;
201 }
202
203 static void parse_flags(const uint8_t *buf, struct fs9721_info *info)
204 {
205         /* Byte 0: LCD SEG1 */
206         info->is_ac         = (buf[0] & (1 << 3)) != 0;
207         info->is_dc         = (buf[0] & (1 << 2)) != 0;
208         info->is_auto       = (buf[0] & (1 << 1)) != 0;
209         info->is_rs232      = (buf[0] & (1 << 0)) != 0;
210
211         /* Byte 1: LCD SEG2 */
212         info->is_sign       = (buf[1] & (1 << 3)) != 0;
213
214         /* Byte 9: LCD SEG10 */
215         info->is_micro      = (buf[9] & (1 << 3)) != 0;
216         info->is_nano       = (buf[9] & (1 << 2)) != 0;
217         info->is_kilo       = (buf[9] & (1 << 1)) != 0;
218         info->is_diode      = (buf[9] & (1 << 0)) != 0;
219
220         /* Byte 10: LCD SEG11 */
221         info->is_milli      = (buf[10] & (1 << 3)) != 0;
222         info->is_percent    = (buf[10] & (1 << 2)) != 0;
223         info->is_mega       = (buf[10] & (1 << 1)) != 0;
224         info->is_beep       = (buf[10] & (1 << 0)) != 0;
225
226         /* Byte 11: LCD SEG12 */
227         info->is_farad      = (buf[11] & (1 << 3)) != 0;
228         info->is_ohm        = (buf[11] & (1 << 2)) != 0;
229         info->is_rel        = (buf[11] & (1 << 1)) != 0;
230         info->is_hold       = (buf[11] & (1 << 0)) != 0;
231
232         /* Byte 12: LCD SEG13 */
233         info->is_ampere     = (buf[12] & (1 << 3)) != 0;
234         info->is_volt       = (buf[12] & (1 << 2)) != 0;
235         info->is_hz         = (buf[12] & (1 << 1)) != 0;
236         info->is_bat        = (buf[12] & (1 << 0)) != 0;
237
238         /* Byte 13: LCD SEG14 */
239         info->is_c2c1_11    = (buf[13] & (1 << 3)) != 0;
240         info->is_c2c1_10    = (buf[13] & (1 << 2)) != 0;
241         info->is_c2c1_01    = (buf[13] & (1 << 1)) != 0;
242         info->is_c2c1_00    = (buf[13] & (1 << 0)) != 0;
243 }
244
245 static void handle_flags(struct sr_datafeed_analog *analog, float *floatval,
246                          const struct fs9721_info *info)
247 {
248         /* Factors */
249         if (info->is_nano)
250                 *floatval /= 1000000000;
251         if (info->is_micro)
252                 *floatval /= 1000000;
253         if (info->is_milli)
254                 *floatval /= 1000;
255         if (info->is_kilo)
256                 *floatval *= 1000;
257         if (info->is_mega)
258                 *floatval *= 1000000;
259
260         /* Measurement modes */
261         if (info->is_volt) {
262                 analog->mq = SR_MQ_VOLTAGE;
263                 analog->unit = SR_UNIT_VOLT;
264         }
265         if (info->is_ampere) {
266                 analog->mq = SR_MQ_CURRENT;
267                 analog->unit = SR_UNIT_AMPERE;
268         }
269         if (info->is_ohm) {
270                 analog->mq = SR_MQ_RESISTANCE;
271                 analog->unit = SR_UNIT_OHM;
272         }
273         if (info->is_hz) {
274                 analog->mq = SR_MQ_FREQUENCY;
275                 analog->unit = SR_UNIT_HERTZ;
276         }
277         if (info->is_farad) {
278                 analog->mq = SR_MQ_CAPACITANCE;
279                 analog->unit = SR_UNIT_FARAD;
280         }
281         if (info->is_beep) {
282                 analog->mq = SR_MQ_CONTINUITY;
283                 analog->unit = SR_UNIT_BOOLEAN;
284                 *floatval = (*floatval == INFINITY) ? 0.0 : 1.0;
285         }
286         if (info->is_diode) {
287                 analog->mq = SR_MQ_VOLTAGE;
288                 analog->unit = SR_UNIT_VOLT;
289         }
290         if (info->is_percent) {
291                 analog->mq = SR_MQ_DUTY_CYCLE;
292                 analog->unit = SR_UNIT_PERCENTAGE;
293         }
294
295         /* Measurement related flags */
296         if (info->is_ac)
297                 analog->mqflags |= SR_MQFLAG_AC;
298         if (info->is_dc)
299                 analog->mqflags |= SR_MQFLAG_DC;
300         if (info->is_auto)
301                 analog->mqflags |= SR_MQFLAG_AUTORANGE;
302         if (info->is_diode)
303                 analog->mqflags |= SR_MQFLAG_DIODE;
304         if (info->is_hold)
305                 analog->mqflags |= SR_MQFLAG_HOLD;
306         if (info->is_rel)
307                 analog->mqflags |= SR_MQFLAG_RELATIVE;
308
309         /* Other flags */
310         if (info->is_rs232)
311                 sr_spew("RS232 enabled.");
312         if (info->is_bat)
313                 sr_spew("Battery is low.");
314         if (info->is_c2c1_00)
315                 sr_spew("User-defined LCD symbol 0 is active.");
316         if (info->is_c2c1_01)
317                 sr_spew("User-defined LCD symbol 1 is active.");
318         if (info->is_c2c1_10)
319                 sr_spew("User-defined LCD symbol 2 is active.");
320         if (info->is_c2c1_11)
321                 sr_spew("User-defined LCD symbol 3 is active.");
322 }
323
324 SR_PRIV gboolean sr_fs9721_packet_valid(const uint8_t *buf)
325 {
326         struct fs9721_info info;
327
328         parse_flags(buf, &info);
329
330         return (sync_nibbles_valid(buf) && flags_valid(&info));
331 }
332
333 /**
334  * Parse a protocol packet.
335  *
336  * @param buf Buffer containing the 14-byte protocol packet. Must not be NULL.
337  * @param floatval Pointer to a float variable. That variable will contain the
338  *                 result value upon parsing success. Must not be NULL.
339  * @param analog Pointer to a struct sr_datafeed_analog. The struct will be
340  *               filled with data according to the protocol packet.
341  *               Must not be NULL.
342  * @param info Pointer to a struct fs9721_info. The struct will be filled
343  *             with data according to the protocol packet. Must not be NULL.
344  *
345  * @return SR_OK upon success, SR_ERR upon failure. Upon errors, the
346  *         'analog' variable contents are undefined and should not be used.
347  */
348 SR_PRIV int sr_fs9721_parse(const uint8_t *buf, float *floatval,
349                             struct sr_datafeed_analog *analog, void *info)
350 {
351         int ret;
352         struct fs9721_info *info_local;
353
354         info_local = (struct fs9721_info *)info;
355
356         if ((ret = parse_value(buf, floatval)) != SR_OK) {
357                 sr_dbg("Error parsing value: %d.", ret);
358                 return ret;
359         }
360
361         parse_flags(buf, info_local);
362         handle_flags(analog, floatval, info_local);
363
364         return SR_OK;
365 }
366
367 SR_PRIV void sr_fs9721_00_temp_c(struct sr_datafeed_analog *analog, void *info)
368 {
369         struct fs9721_info *info_local;
370
371         info_local = (struct fs9721_info *)info;
372
373         /* User-defined FS9721_LP3 flag 'c2c1_00' means temperature (C). */
374         if (info_local->is_c2c1_00) {
375                 analog->mq = SR_MQ_TEMPERATURE;
376                 analog->unit = SR_UNIT_CELSIUS;
377         }
378 }
379
380 SR_PRIV void sr_fs9721_01_temp_c(struct sr_datafeed_analog *analog, void *info)
381 {
382         struct fs9721_info *info_local;
383
384         info_local = (struct fs9721_info *)info;
385
386         /* User-defined FS9721_LP3 flag 'c2c1_01' means temperature (C). */
387         if (info_local->is_c2c1_01) {
388                 analog->mq = SR_MQ_TEMPERATURE;
389                 analog->unit = SR_UNIT_CELSIUS;
390         }
391 }
392
393 SR_PRIV void sr_fs9721_10_temp_c(struct sr_datafeed_analog *analog, void *info)
394 {
395         struct fs9721_info *info_local;
396
397         info_local = (struct fs9721_info *)info;
398
399         /* User-defined FS9721_LP3 flag 'c2c1_10' means temperature (C). */
400         if (info_local->is_c2c1_10) {
401                 analog->mq = SR_MQ_TEMPERATURE;
402                 analog->unit = SR_UNIT_CELSIUS;
403         }
404 }
405
406 SR_PRIV void sr_fs9721_01_10_temp_f_c(struct sr_datafeed_analog *analog, void *info)
407 {
408         struct fs9721_info *info_local;
409
410         info_local = (struct fs9721_info *)info;
411
412         /* User-defined FS9721_LP3 flag 'c2c1_01' means temperature (F). */
413         if (info_local->is_c2c1_01) {
414                 analog->mq = SR_MQ_TEMPERATURE;
415                 analog->unit = SR_UNIT_FAHRENHEIT;
416         }
417
418         /* User-defined FS9721_LP3 flag 'c2c1_10' means temperature (C). */
419         if (info_local->is_c2c1_10) {
420                 analog->mq = SR_MQ_TEMPERATURE;
421                 analog->unit = SR_UNIT_CELSIUS;
422         }
423 }
424
425 SR_PRIV void sr_fs9721_max_c_min(struct sr_datafeed_analog *analog, void *info)
426 {
427         struct fs9721_info *info_local;
428
429         info_local = (struct fs9721_info *)info;
430
431         /* User-defined FS9721_LP3 flag 'c2c1_00' means MAX. */
432         if (info_local->is_c2c1_00)
433                 analog->mqflags |= SR_MQFLAG_MAX;
434
435         /* User-defined FS9721_LP3 flag 'c2c1_01' means temperature (C). */
436         if (info_local->is_c2c1_01) {
437                 analog->mq = SR_MQ_TEMPERATURE;
438                 analog->unit = SR_UNIT_CELSIUS;
439         }
440
441         /* User-defined FS9721_LP3 flag 'c2c1_11' means MIN. */
442         if (info_local->is_c2c1_11)
443                 analog->mqflags |= SR_MQFLAG_MIN;
444
445 }