2 * This file is part of the libsigrok project.
4 * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
5 * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com>
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.
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.
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
23 * Fortune Semiconductor FS9721_LP3/FS9721B protocol parser.
25 * FS9721_LP3: 4000 counts (3 3/4 digits)
26 * FS9721B/Q100: 2400 counts (3 2/3 digits)
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.
39 #include <libsigrok/libsigrok.h>
40 #include "libsigrok-internal.h"
42 #define LOG_PREFIX "fs9721"
44 static int parse_digit(uint8_t b)
68 sr_dbg("Invalid digit byte: 0x%02x.", b);
73 static gboolean sync_nibbles_valid(const uint8_t *buf)
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)) {
80 sr_dbg("Sync nibble in byte %d (0x%02x) is invalid.",
89 static gboolean flags_valid(const struct fs9721_info *info)
93 /* Does the packet have more than one multiplier? */
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;
101 sr_dbg("More than one multiplier detected in packet.");
105 /* Does the packet "measure" more than one type of value? */
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;
114 sr_dbg("More than one measurement type detected in packet.");
118 /* Both AC and DC set? */
119 if (info->is_ac && info->is_dc) {
120 sr_dbg("Both AC and DC flags detected in packet.");
124 /* RS232 flag not set? */
125 if (!info->is_rs232) {
126 sr_dbg("No RS232 flag detected in packet.");
133 static int parse_value(const uint8_t *buf, float *result)
135 int i, sign, intval = 0, digits[4];
136 uint8_t digit_bytes[4];
139 /* Byte 1: LCD SEG2 */
140 sign = ((buf[1] & (1 << 3)) != 0) ? -1 : 1;
143 * Bytes 1-8: Value (4 decimal digits, sign, decimal point)
145 * Over limit: "0L" (LCD), 0x00 0x7d 0x68 0x00 (digit bytes).
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);
153 /* Bit 7 in the byte is not part of the digit. */
154 digit_bytes[i] &= ~(1 << 7);
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.");
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]);
172 /* Merge all digits into an integer value. */
173 for (i = 0; i < 4; i++) {
178 floatval = (float)intval;
180 /* Decimal point position. */
181 if ((buf[3] & (1 << 3)) != 0) {
183 sr_spew("Decimal point after first digit.");
184 } else if ((buf[5] & (1 << 3)) != 0) {
186 sr_spew("Decimal point after second digit.");
187 } else if ((buf[7] & (1 << 3)) != 0) {
189 sr_spew("Decimal point after third digit.");
191 sr_spew("No decimal point in the number.");
197 sr_spew("The display value is %f.", floatval);
204 static void parse_flags(const uint8_t *buf, struct fs9721_info *info)
206 /* Byte 0: LCD SEG1 */
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;
212 /* Byte 1: LCD SEG2 */
213 info->is_sign = (buf[1] & (1 << 3)) != 0;
215 /* Byte 9: LCD SEG10 */
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;
221 /* Byte 10: LCD SEG11 */
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;
227 /* Byte 11: LCD SEG12 */
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;
233 /* Byte 12: LCD SEG13 */
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;
239 /* Byte 13: LCD SEG14 */
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;
246 static void handle_flags(struct sr_datafeed_analog_old *analog, float *floatval,
247 const struct fs9721_info *info)
251 *floatval /= 1000000000;
253 *floatval /= 1000000;
259 *floatval *= 1000000;
261 /* Measurement modes */
263 analog->mq = SR_MQ_VOLTAGE;
264 analog->unit = SR_UNIT_VOLT;
266 if (info->is_ampere) {
267 analog->mq = SR_MQ_CURRENT;
268 analog->unit = SR_UNIT_AMPERE;
271 analog->mq = SR_MQ_RESISTANCE;
272 analog->unit = SR_UNIT_OHM;
275 analog->mq = SR_MQ_FREQUENCY;
276 analog->unit = SR_UNIT_HERTZ;
278 if (info->is_farad) {
279 analog->mq = SR_MQ_CAPACITANCE;
280 analog->unit = SR_UNIT_FARAD;
283 analog->mq = SR_MQ_CONTINUITY;
284 analog->unit = SR_UNIT_BOOLEAN;
285 *floatval = (*floatval == INFINITY) ? 0.0 : 1.0;
287 if (info->is_diode) {
288 analog->mq = SR_MQ_VOLTAGE;
289 analog->unit = SR_UNIT_VOLT;
291 if (info->is_percent) {
292 analog->mq = SR_MQ_DUTY_CYCLE;
293 analog->unit = SR_UNIT_PERCENTAGE;
296 /* Measurement related flags */
298 analog->mqflags |= SR_MQFLAG_AC;
300 analog->mqflags |= SR_MQFLAG_DC;
302 analog->mqflags |= SR_MQFLAG_AUTORANGE;
304 analog->mqflags |= SR_MQFLAG_DIODE;
306 analog->mqflags |= SR_MQFLAG_HOLD;
308 analog->mqflags |= SR_MQFLAG_RELATIVE;
312 sr_spew("RS232 enabled.");
314 sr_spew("Battery is low.");
315 if (info->is_c2c1_00)
316 sr_spew("User-defined LCD symbol 0 is active.");
317 if (info->is_c2c1_01)
318 sr_spew("User-defined LCD symbol 1 is active.");
319 if (info->is_c2c1_10)
320 sr_spew("User-defined LCD symbol 2 is active.");
321 if (info->is_c2c1_11)
322 sr_spew("User-defined LCD symbol 3 is active.");
325 SR_PRIV gboolean sr_fs9721_packet_valid(const uint8_t *buf)
327 struct fs9721_info info;
329 parse_flags(buf, &info);
331 return (sync_nibbles_valid(buf) && flags_valid(&info));
335 * Parse a protocol packet.
337 * @param buf Buffer containing the 14-byte protocol packet. Must not be NULL.
338 * @param floatval Pointer to a float variable. That variable will contain the
339 * result value upon parsing success. Must not be NULL.
340 * @param analog Pointer to a struct sr_datafeed_analog_old. The struct will be
341 * filled with data according to the protocol packet.
343 * @param info Pointer to a struct fs9721_info. The struct will be filled
344 * with data according to the protocol packet. Must not be NULL.
346 * @return SR_OK upon success, SR_ERR upon failure. Upon errors, the
347 * 'analog' variable contents are undefined and should not be used.
349 SR_PRIV int sr_fs9721_parse(const uint8_t *buf, float *floatval,
350 struct sr_datafeed_analog_old *analog, void *info)
353 struct fs9721_info *info_local;
355 info_local = (struct fs9721_info *)info;
357 if ((ret = parse_value(buf, floatval)) != SR_OK) {
358 sr_dbg("Error parsing value: %d.", ret);
362 parse_flags(buf, info_local);
363 handle_flags(analog, floatval, info_local);
368 SR_PRIV void sr_fs9721_00_temp_c(struct sr_datafeed_analog_old *analog, void *info)
370 struct fs9721_info *info_local;
372 info_local = (struct fs9721_info *)info;
374 /* User-defined FS9721_LP3 flag 'c2c1_00' means temperature (C). */
375 if (info_local->is_c2c1_00) {
376 analog->mq = SR_MQ_TEMPERATURE;
377 analog->unit = SR_UNIT_CELSIUS;
381 SR_PRIV void sr_fs9721_01_temp_c(struct sr_datafeed_analog_old *analog, void *info)
383 struct fs9721_info *info_local;
385 info_local = (struct fs9721_info *)info;
387 /* User-defined FS9721_LP3 flag 'c2c1_01' means temperature (C). */
388 if (info_local->is_c2c1_01) {
389 analog->mq = SR_MQ_TEMPERATURE;
390 analog->unit = SR_UNIT_CELSIUS;
394 SR_PRIV void sr_fs9721_10_temp_c(struct sr_datafeed_analog_old *analog, void *info)
396 struct fs9721_info *info_local;
398 info_local = (struct fs9721_info *)info;
400 /* User-defined FS9721_LP3 flag 'c2c1_10' means temperature (C). */
401 if (info_local->is_c2c1_10) {
402 analog->mq = SR_MQ_TEMPERATURE;
403 analog->unit = SR_UNIT_CELSIUS;
407 SR_PRIV void sr_fs9721_01_10_temp_f_c(struct sr_datafeed_analog_old *analog, void *info)
409 struct fs9721_info *info_local;
411 info_local = (struct fs9721_info *)info;
413 /* User-defined FS9721_LP3 flag 'c2c1_01' means temperature (F). */
414 if (info_local->is_c2c1_01) {
415 analog->mq = SR_MQ_TEMPERATURE;
416 analog->unit = SR_UNIT_FAHRENHEIT;
419 /* User-defined FS9721_LP3 flag 'c2c1_10' means temperature (C). */
420 if (info_local->is_c2c1_10) {
421 analog->mq = SR_MQ_TEMPERATURE;
422 analog->unit = SR_UNIT_CELSIUS;
426 SR_PRIV void sr_fs9721_max_c_min(struct sr_datafeed_analog_old *analog, void *info)
428 struct fs9721_info *info_local;
430 info_local = (struct fs9721_info *)info;
432 /* User-defined FS9721_LP3 flag 'c2c1_00' means MAX. */
433 if (info_local->is_c2c1_00)
434 analog->mqflags |= SR_MQFLAG_MAX;
436 /* User-defined FS9721_LP3 flag 'c2c1_01' means temperature (C). */
437 if (info_local->is_c2c1_01) {
438 analog->mq = SR_MQ_TEMPERATURE;
439 analog->unit = SR_UNIT_CELSIUS;
442 /* User-defined FS9721_LP3 flag 'c2c1_11' means MIN. */
443 if (info_local->is_c2c1_11)
444 analog->mqflags |= SR_MQFLAG_MIN;