2 * This file is part of the sigrok project.
4 * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
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.
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.
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
22 * Fortune Semiconductor FS9721_LP3/FS9721B protocol parser.
24 * FS9721_LP3: 4000 counts (3 3/4 digits)
25 * FS9721B/Q100: 2400 counts (3 2/3 digits)
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.
37 #include "libsigrok.h"
38 #include "libsigrok-internal.h"
40 /* Message logging helpers with driver-specific prefix string. */
41 #define DRIVER_LOG_DOMAIN "fs9721: "
42 #define sr_log(l, s, args...) sr_log(l, DRIVER_LOG_DOMAIN s, ## args)
43 #define sr_spew(s, args...) sr_spew(DRIVER_LOG_DOMAIN s, ## args)
44 #define sr_dbg(s, args...) sr_dbg(DRIVER_LOG_DOMAIN s, ## args)
45 #define sr_info(s, args...) sr_info(DRIVER_LOG_DOMAIN s, ## args)
46 #define sr_warn(s, args...) sr_warn(DRIVER_LOG_DOMAIN s, ## args)
47 #define sr_err(s, args...) sr_err(DRIVER_LOG_DOMAIN s, ## args)
49 static int parse_digit(uint8_t b)
73 sr_err("Invalid digit byte: 0x%02x.", b);
79 * Parse the numerical value from a protocol packet.
81 * @param buf Buffer containing the 14-byte protocol packet.
82 * @param result Pointer to a float variable. That variable will contain the
83 * result value upon parsing success.
85 * @return SR_OK upon success, SR_ERR upon failure. Upon errors, the result
86 * variable contents are undefined and should not be used.
88 static int parse_value(const uint8_t *buf, float *result)
90 int i, sign, intval = 0, digits[4];
91 uint8_t digit_bytes[4];
94 /* Byte 1: LCD SEG2 */
95 sign = ((buf[1] & (1 << 3)) != 0) ? -1 : 1;
98 * Bytes 1-8: Value (4 decimal digits, sign, decimal point)
100 * Over limit: "0L" (LCD), 0x00 0x7d 0x68 0x00 (digit bytes).
103 /* Merge the two nibbles for a digit into one byte. */
104 for (i = 0; i < 4; i++) {
105 digit_bytes[i] = ((buf[1 + (i * 2)] & 0x0f) << 4);
106 digit_bytes[i] |= (buf[1 + (i * 2) + 1] & 0x0f);
108 /* Bit 7 in the byte is not part of the digit. */
109 digit_bytes[i] &= ~(1 << 7);
112 /* Check for "OL". */
113 if (digit_bytes[0] == 0x00 && digit_bytes[1] == 0x7d &&
114 digit_bytes[2] == 0x68 && digit_bytes[3] == 0x00) {
115 sr_spew("Over limit.");
120 /* Parse the digits. */
121 for (i = 0; i < 4; i++)
122 digits[i] = parse_digit(digit_bytes[i]);
123 sr_spew("Digits: %02x %02x %02x %02x (%d%d%d%d).",
124 digit_bytes[0], digit_bytes[1], digit_bytes[2], digit_bytes[3],
125 digits[0], digits[1], digits[2], digits[3]);
127 /* Merge all digits into an integer value. */
128 for (i = 0; i < 4; i++) {
133 /* Store the value in a float variable. */
134 floatval = (float)intval;
136 /* Decimal point position. */
137 if ((buf[3] & (1 << 3)) != 0) {
139 sr_spew("Decimal point after first digit.");
140 } else if ((buf[5] & (1 << 3)) != 0) {
142 sr_spew("Decimal point after second digit.");
143 } else if ((buf[7] & (1 << 3)) != 0) {
145 sr_spew("Decimal point after third digit.");
147 sr_spew("No decimal point in the number.");
153 sr_spew("The display value is %f.", floatval);
161 * Parse various flags in a protocol packet.
163 * @param buf Buffer containing the 14-byte protocol packet.
164 * @param floatval Pointer to a float variable which should contain the value
165 * parsed using parse_value(). That variable will be modified
166 * in-place depending on the flags in the protocol packet.
167 * @param analog Pointer to a struct sr_datafeed_analog. The struct will be
168 * filled with the relevant data according to the flags in the
171 * @return SR_OK upon success, SR_ERR upon failure. Upon errors, the 'floatval'
172 * and 'analog' variable contents are undefined and should not be used.
174 static int parse_flags(const uint8_t *buf, float *floatval,
175 struct sr_datafeed_analog *analog)
177 gboolean is_ac, is_dc, is_auto, is_rs232, is_micro, is_nano, is_kilo;
178 gboolean is_diode, is_milli, is_percent, is_mega, is_beep, is_farad;
179 gboolean is_ohm, is_rel, is_hold, is_ampere, is_volt, is_hz, is_bat;
180 gboolean is_c2c1_11, is_c2c1_10, is_c2c1_01, is_c2c1_00;
182 /* Byte 0: LCD SEG1 */
183 is_ac = (buf[0] & (1 << 3)) != 0;
184 is_dc = (buf[0] & (1 << 2)) != 0;
185 is_auto = (buf[0] & (1 << 1)) != 0;
186 is_rs232 = (buf[0] & (1 << 0)) != 0;
188 /* Byte 9: LCD SEG10 */
189 is_micro = (buf[9] & (1 << 3)) != 0;
190 is_nano = (buf[9] & (1 << 2)) != 0;
191 is_kilo = (buf[9] & (1 << 1)) != 0;
192 is_diode = (buf[9] & (1 << 0)) != 0;
194 /* Byte 10: LCD SEG11 */
195 is_milli = (buf[10] & (1 << 3)) != 0;
196 is_percent = (buf[10] & (1 << 2)) != 0;
197 is_mega = (buf[10] & (1 << 1)) != 0;
198 is_beep = (buf[10] & (1 << 0)) != 0;
200 /* Byte 11: LCD SEG12 */
201 is_farad = (buf[11] & (1 << 3)) != 0;
202 is_ohm = (buf[11] & (1 << 2)) != 0;
203 is_rel = (buf[11] & (1 << 1)) != 0;
204 is_hold = (buf[11] & (1 << 0)) != 0;
206 /* Byte 12: LCD SEG13 */
207 is_ampere = (buf[12] & (1 << 3)) != 0;
208 is_volt = (buf[12] & (1 << 2)) != 0;
209 is_hz = (buf[12] & (1 << 1)) != 0;
210 is_bat = (buf[12] & (1 << 0)) != 0;
212 /* Byte 13: LCD SEG14 */
213 is_c2c1_11 = (buf[13] & (1 << 3)) != 0;
214 is_c2c1_10 = (buf[13] & (1 << 2)) != 0;
215 is_c2c1_01 = (buf[13] & (1 << 1)) != 0;
216 is_c2c1_00 = (buf[13] & (1 << 0)) != 0;
220 *floatval /= 1000000000;
222 *floatval /= 1000000;
228 *floatval *= 1000000;
230 /* Measurement modes */
232 analog->mq = SR_MQ_VOLTAGE;
233 analog->unit = SR_UNIT_VOLT;
236 analog->mq = SR_MQ_CURRENT;
237 analog->unit = SR_UNIT_AMPERE;
240 analog->mq = SR_MQ_RESISTANCE;
241 analog->unit = SR_UNIT_OHM;
244 analog->mq = SR_MQ_FREQUENCY;
245 analog->unit = SR_UNIT_HERTZ;
248 analog->mq = SR_MQ_CAPACITANCE;
249 analog->unit = SR_UNIT_FARAD;
252 analog->mq = SR_MQ_CONTINUITY;
253 analog->unit = SR_UNIT_BOOLEAN;
254 *floatval = (*floatval < 0.0) ? 0.0 : 1.0;
257 analog->mq = SR_MQ_VOLTAGE;
258 analog->unit = SR_UNIT_VOLT;
261 analog->mq = SR_MQ_DUTY_CYCLE;
262 analog->unit = SR_UNIT_PERCENTAGE;
265 /* Measurement related flags */
267 analog->mqflags |= SR_MQFLAG_AC;
269 analog->mqflags |= SR_MQFLAG_DC;
271 analog->mqflags |= SR_MQFLAG_AUTORANGE;
273 analog->mqflags |= SR_MQFLAG_HOLD;
275 analog->mqflags |= SR_MQFLAG_RELATIVE;
279 sr_spew("RS232 enabled.");
281 sr_spew("Battery is low.");
283 sr_spew("User-defined LCD symbol 0 is active.");
285 sr_spew("User-defined LCD symbol 1 is active.");
287 sr_spew("User-defined LCD symbol 2 is active.");
289 sr_spew("User-defined LCD symbol 3 is active.");
295 * Parse a protocol packet.
297 * @param buf Buffer containing the 14-byte protocol packet.
298 * @param floatval Pointer to a float variable. That variable will be modified
299 * in-place depending on the protocol packet.
300 * @param analog Pointer to a struct sr_datafeed_analog. The struct will be
301 * filled with data according to the protocol packet.
303 * @return SR_OK upon success, SR_ERR upon failure. Upon errors, the
304 * 'analog' variable contents are undefined and should not be used.
306 SR_PRIV int sr_dmm_parse_fs9721(const uint8_t *buf, float *floatval,
307 struct sr_datafeed_analog *analog)
311 if ((ret = parse_value(buf, floatval)) != SR_OK) {
312 sr_err("Error parsing value: %d.", ret);
316 if ((ret = parse_flags(buf, floatval, analog)) != SR_OK) {
317 sr_err("Error parsing flags: %d.", ret);