]> sigrok.org Git - libsigrok.git/blob - hardware/common/dmm/fs9721.c
ccf16a3250b80192726b915ff4b4af94b5c4411b
[libsigrok.git] / hardware / common / dmm / fs9721.c
1 /*
2  * This file is part of the sigrok 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 /* Message logging helpers with driver-specific prefix string. */
42 #define DRIVER_LOG_DOMAIN "fs9721: "
43 #define sr_log(l, s, args...) sr_log(l, DRIVER_LOG_DOMAIN s, ## args)
44 #define sr_spew(s, args...) sr_spew(DRIVER_LOG_DOMAIN s, ## args)
45 #define sr_dbg(s, args...) sr_dbg(DRIVER_LOG_DOMAIN s, ## args)
46 #define sr_info(s, args...) sr_info(DRIVER_LOG_DOMAIN s, ## args)
47 #define sr_warn(s, args...) sr_warn(DRIVER_LOG_DOMAIN s, ## args)
48 #define sr_err(s, args...) sr_err(DRIVER_LOG_DOMAIN s, ## args)
49
50 static int parse_digit(uint8_t b)
51 {
52         switch (b) {
53         case 0x7d:
54                 return 0;
55         case 0x05:
56                 return 1;
57         case 0x5b:
58                 return 2;
59         case 0x1f:
60                 return 3;
61         case 0x27:
62                 return 4;
63         case 0x3e:
64                 return 5;
65         case 0x7e:
66                 return 6;
67         case 0x15:
68                 return 7;
69         case 0x7f:
70                 return 8;
71         case 0x3f:
72                 return 9;
73         default:
74                 sr_err("Invalid digit byte: 0x%02x.", b);
75                 return -1;
76         }
77 }
78
79 static gboolean sync_nibbles_valid(const uint8_t *buf)
80 {
81         int i;
82
83         /* Check the synchronization nibbles, and make sure they all match. */
84         for (i = 0; i < FS9721_PACKET_SIZE; i++) {
85                 if (((buf[i] >> 4) & 0x0f) != (i + 1)) {
86                         sr_err("Sync nibble in byte %d (0x%02x) is invalid.",
87                                i, buf[i]);
88                         return FALSE;
89                 }
90         }
91
92         return TRUE;
93 }
94
95 static gboolean flags_valid(const struct fs9721_info *info)
96 {
97         int count;
98
99         /* Does the packet have more than one multiplier? */
100         count = 0;
101         count += (info->is_nano) ? 1 : 0;
102         count += (info->is_micro) ? 1 : 0;
103         count += (info->is_milli) ? 1 : 0;
104         count += (info->is_kilo) ? 1 : 0;
105         count += (info->is_mega) ? 1 : 0;
106         if (count > 1) {
107                 sr_err("More than one multiplier detected in packet.");
108                 return FALSE;
109         }
110
111         /* Does the packet "measure" more than one type of value? */
112         count = 0;
113         count += (info->is_hz) ? 1 : 0;
114         count += (info->is_ohm) ? 1 : 0;
115         count += (info->is_farad) ? 1 : 0;
116         count += (info->is_ampere) ? 1 : 0;
117         count += (info->is_volt) ? 1 : 0;
118         count += (info->is_percent) ? 1 : 0;
119         if (count > 1) {
120                 sr_err("More than one measurement type detected in packet.");
121                 return FALSE;
122         }
123
124         /* Both AC and DC set? */
125         if (info->is_ac && info->is_dc) {
126                 sr_err("Both AC and DC flags detected in packet.");
127                 return FALSE;
128         }
129
130         /* RS232 flag not set? */
131         if (!info->is_rs232) {
132                 sr_err("No RS232 flag detected in packet.");
133                 return FALSE;
134         }
135
136         return TRUE;
137 }
138
139 static int parse_value(const uint8_t *buf, float *result)
140 {
141         int i, sign, intval = 0, digits[4];
142         uint8_t digit_bytes[4];
143         float floatval;
144
145         /* Byte 1: LCD SEG2 */
146         sign = ((buf[1] & (1 << 3)) != 0) ? -1 : 1;
147
148         /*
149          * Bytes 1-8: Value (4 decimal digits, sign, decimal point)
150          *
151          * Over limit: "0L" (LCD), 0x00 0x7d 0x68 0x00 (digit bytes).
152          */
153
154         /* Merge the two nibbles for a digit into one byte. */
155         for (i = 0; i < 4; i++) {
156                 digit_bytes[i] = ((buf[1 + (i * 2)] & 0x0f) << 4);
157                 digit_bytes[i] |= (buf[1 + (i * 2) + 1] & 0x0f);
158
159                 /* Bit 7 in the byte is not part of the digit. */
160                 digit_bytes[i] &= ~(1 << 7);
161         }
162
163         /* Check for "OL". */
164         if (digit_bytes[0] == 0x00 && digit_bytes[1] == 0x7d &&
165             digit_bytes[2] == 0x68 && digit_bytes[3] == 0x00) {
166                 sr_spew("Over limit.");
167                 *result = INFINITY;
168                 return SR_OK;
169         }
170
171         /* Parse the digits. */
172         for (i = 0; i < 4; i++)
173                 digits[i] = parse_digit(digit_bytes[i]);
174         sr_spew("Digits: %02x %02x %02x %02x (%d%d%d%d).",
175                 digit_bytes[0], digit_bytes[1], digit_bytes[2], digit_bytes[3],
176                 digits[0], digits[1], digits[2], digits[3]);
177
178         /* Merge all digits into an integer value. */
179         for (i = 0; i < 4; i++) {
180                 intval *= 10;
181                 intval += digits[i];
182         }
183
184         floatval = (float)intval;
185
186         /* Decimal point position. */
187         if ((buf[3] & (1 << 3)) != 0) {
188                 floatval /= 1000;
189                 sr_spew("Decimal point after first digit.");
190         } else if ((buf[5] & (1 << 3)) != 0) {
191                 floatval /= 100;
192                 sr_spew("Decimal point after second digit.");
193         } else if ((buf[7] & (1 << 3)) != 0) {
194                 floatval /= 10;
195                 sr_spew("Decimal point after third digit.");
196         } else {
197                 sr_spew("No decimal point in the number.");
198         }
199
200         /* Apply sign. */
201         floatval *= sign;
202
203         sr_spew("The display value is %f.", floatval);
204
205         *result = floatval;
206
207         return SR_OK;
208 }
209
210 static void parse_flags(const uint8_t *buf, struct fs9721_info *info)
211 {
212         /* Byte 0: LCD SEG1 */
213         info->is_ac         = (buf[0] & (1 << 3)) != 0;
214         info->is_dc         = (buf[0] & (1 << 2)) != 0;
215         info->is_auto       = (buf[0] & (1 << 1)) != 0;
216         info->is_rs232      = (buf[0] & (1 << 0)) != 0;
217
218         /* Byte 1: LCD SEG2 */
219         info->is_sign       = (buf[1] & (1 << 3)) != 0;
220
221         /* Byte 9: LCD SEG10 */
222         info->is_micro      = (buf[9] & (1 << 3)) != 0;
223         info->is_nano       = (buf[9] & (1 << 2)) != 0;
224         info->is_kilo       = (buf[9] & (1 << 1)) != 0;
225         info->is_diode      = (buf[9] & (1 << 0)) != 0;
226
227         /* Byte 10: LCD SEG11 */
228         info->is_milli      = (buf[10] & (1 << 3)) != 0;
229         info->is_percent    = (buf[10] & (1 << 2)) != 0;
230         info->is_mega       = (buf[10] & (1 << 1)) != 0;
231         info->is_beep       = (buf[10] & (1 << 0)) != 0;
232
233         /* Byte 11: LCD SEG12 */
234         info->is_farad      = (buf[11] & (1 << 3)) != 0;
235         info->is_ohm        = (buf[11] & (1 << 2)) != 0;
236         info->is_rel        = (buf[11] & (1 << 1)) != 0;
237         info->is_hold       = (buf[11] & (1 << 0)) != 0;
238
239         /* Byte 12: LCD SEG13 */
240         info->is_ampere     = (buf[12] & (1 << 3)) != 0;
241         info->is_volt       = (buf[12] & (1 << 2)) != 0;
242         info->is_hz         = (buf[12] & (1 << 1)) != 0;
243         info->is_bat        = (buf[12] & (1 << 0)) != 0;
244
245         /* Byte 13: LCD SEG14 */
246         info->is_c2c1_11    = (buf[13] & (1 << 3)) != 0;
247         info->is_c2c1_10    = (buf[13] & (1 << 2)) != 0;
248         info->is_c2c1_01    = (buf[13] & (1 << 1)) != 0;
249         info->is_c2c1_00    = (buf[13] & (1 << 0)) != 0;
250 }
251
252 static void handle_flags(struct sr_datafeed_analog *analog, float *floatval,
253                          const struct fs9721_info *info)
254 {
255         /* Factors */
256         if (info->is_nano)
257                 *floatval /= 1000000000;
258         if (info->is_micro)
259                 *floatval /= 1000000;
260         if (info->is_milli)
261                 *floatval /= 1000;
262         if (info->is_kilo)
263                 *floatval *= 1000;
264         if (info->is_mega)
265                 *floatval *= 1000000;
266
267         /* Measurement modes */
268         if (info->is_volt) {
269                 analog->mq = SR_MQ_VOLTAGE;
270                 analog->unit = SR_UNIT_VOLT;
271         }
272         if (info->is_ampere) {
273                 analog->mq = SR_MQ_CURRENT;
274                 analog->unit = SR_UNIT_AMPERE;
275         }
276         if (info->is_ohm) {
277                 analog->mq = SR_MQ_RESISTANCE;
278                 analog->unit = SR_UNIT_OHM;
279         }
280         if (info->is_hz) {
281                 analog->mq = SR_MQ_FREQUENCY;
282                 analog->unit = SR_UNIT_HERTZ;
283         }
284         if (info->is_farad) {
285                 analog->mq = SR_MQ_CAPACITANCE;
286                 analog->unit = SR_UNIT_FARAD;
287         }
288         if (info->is_beep) {
289                 analog->mq = SR_MQ_CONTINUITY;
290                 analog->unit = SR_UNIT_BOOLEAN;
291                 *floatval = (*floatval == INFINITY) ? 0.0 : 1.0;
292         }
293         if (info->is_diode) {
294                 analog->mq = SR_MQ_VOLTAGE;
295                 analog->unit = SR_UNIT_VOLT;
296         }
297         if (info->is_percent) {
298                 analog->mq = SR_MQ_DUTY_CYCLE;
299                 analog->unit = SR_UNIT_PERCENTAGE;
300         }
301
302         /* Measurement related flags */
303         if (info->is_ac)
304                 analog->mqflags |= SR_MQFLAG_AC;
305         if (info->is_dc)
306                 analog->mqflags |= SR_MQFLAG_DC;
307         if (info->is_auto)
308                 analog->mqflags |= SR_MQFLAG_AUTORANGE;
309         if (info->is_hold)
310                 analog->mqflags |= SR_MQFLAG_HOLD;
311         if (info->is_rel)
312                 analog->mqflags |= SR_MQFLAG_RELATIVE;
313
314         /* Other flags */
315         if (info->is_rs232)
316                 sr_spew("RS232 enabled.");
317         if (info->is_bat)
318                 sr_spew("Battery is low.");
319         if (info->is_c2c1_00)
320                 sr_spew("User-defined LCD symbol 0 is active.");
321         if (info->is_c2c1_01)
322                 sr_spew("User-defined LCD symbol 1 is active.");
323         if (info->is_c2c1_10)
324                 sr_spew("User-defined LCD symbol 2 is active.");
325         if (info->is_c2c1_11)
326                 sr_spew("User-defined LCD symbol 3 is active.");
327 }
328
329 SR_PRIV gboolean sr_fs9721_is_packet_start(uint8_t b)
330 {
331         return (((b >> 4) & 0x0f) == 0x01);
332 }
333
334 SR_PRIV gboolean sr_fs9721_packet_valid(const uint8_t *buf)
335 {
336         struct fs9721_info info;
337
338         parse_flags(buf, &info);
339
340         return (sync_nibbles_valid(buf) && flags_valid(&info));
341 }
342
343 /**
344  * Parse a protocol packet.
345  *
346  * @param buf Buffer containing the 14-byte protocol packet. Must not be NULL.
347  * @param floatval Pointer to a float variable. That variable will contain the
348  *                 result value upon parsing success. Mut not be NULL.
349  * @param analog Pointer to a struct sr_datafeed_analog. The struct will be
350  *               filled with data according to the protocol packet.
351  *               Must not be NULL.
352  * @param info Pointer to a struct fs9721_info. The struct will be filled
353  *             with data according to the protocol packet. Must not be NULL.
354  *
355  * @return SR_OK upon success, SR_ERR upon failure. Upon errors, the
356  *         'analog' variable contents are undefined and should not be used.
357  */
358 SR_PRIV int sr_fs9721_parse(const uint8_t *buf, float *floatval,
359                             struct sr_datafeed_analog *analog, void *info)
360 {
361         int ret;
362         struct fs9721_info *info_local;
363
364         info_local = (struct fs9721_info *)info;
365
366         if ((ret = parse_value(buf, floatval)) != SR_OK) {
367                 sr_err("Error parsing value: %d.", ret);
368                 return ret;
369         }
370
371         parse_flags(buf, info_local);
372         handle_flags(analog, floatval, info_local);
373
374         return SR_OK;
375 }