2 * This file is part of the sigrok project.
4 * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com>
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 3 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, see <http://www.gnu.org/licenses/>.
21 * RadioShack 22-812 protocol parser.
23 * This protocol is currently encountered on the RadioShack 22-812 DMM.
24 * It is a 9-byte packet representing a 1:1 mapping of the LCD segments, hence
27 * The chip is a bare die covered by a plastic blob. It is unclear if this chip
28 * and protocol is used on any other device.
35 #include "libsigrok.h"
36 #include "libsigrok-internal.h"
38 /* Message logging helpers with driver-specific prefix string. */
39 #define DRIVER_LOG_DOMAIN "rs9lcd: "
40 #define sr_log(l, s, args...) sr_log(l, DRIVER_LOG_DOMAIN s, ## args)
41 #define sr_spew(s, args...) sr_spew(DRIVER_LOG_DOMAIN s, ## args)
42 #define sr_dbg(s, args...) sr_dbg(DRIVER_LOG_DOMAIN s, ## args)
43 #define sr_info(s, args...) sr_info(DRIVER_LOG_DOMAIN s, ## args)
44 #define sr_warn(s, args...) sr_warn(DRIVER_LOG_DOMAIN s, ## args)
45 #define sr_err(s, args...) sr_err(DRIVER_LOG_DOMAIN s, ## args)
47 /* Byte 1 of the packet, and the modes it represents */
50 #define IND1_KILO 0x20
51 #define IND1_MEGA 0x10
52 #define IND1_FARAD 0x08
54 #define IND1_VOLT 0x02
55 #define IND1_MILI 0x01
56 /* Byte 2 of the packet, and the modes it represents */
57 #define IND2_MICRO 0x80
58 #define IND2_NANO 0x40
61 #define IND2_DUTY 0x08
65 /* Byte 7 of the packet, and the modes it represents */
66 #define INFO_BEEP 0x80
67 #define INFO_DIODE 0x30
69 #define INFO_HOLD 0x10
72 #define INFO_RS232 0x02
73 #define INFO_AUTO 0x01
74 /* Instead of a decimal point, digit 4 carries the MAX flag */
76 /* Mask to remove the decimal point from a digit */
79 /* What the LCD values represent */
119 MODE_VOLT_WIDTH = 17,
136 struct rs9lcd_packet {
148 static gboolean checksum_valid(const struct rs9lcd_packet *rs_packet)
154 raw = (void *)rs_packet;
156 for (i = 0; i < RS_22_812_PACKET_SIZE - 1; i++)
159 /* This is just a funky constant added to the checksum. */
161 sum -= rs_packet->checksum;
165 static gboolean selection_good(const struct rs9lcd_packet *rs_packet)
169 /* Does the packet have more than one multiplier? */
171 count += (rs_packet->indicatrix1 & IND1_KILO) ? 1 : 0;
172 count += (rs_packet->indicatrix1 & IND1_MEGA) ? 1 : 0;
173 count += (rs_packet->indicatrix1 & IND1_MILI) ? 1 : 0;
174 count += (rs_packet->indicatrix2 & IND2_MICRO) ? 1 : 0;
175 count += (rs_packet->indicatrix2 & IND2_NANO) ? 1 : 0;
177 sr_err("More than one multiplier detected in packet.");
181 /* Does the packet "measure" more than one type of value? */
183 count += (rs_packet->indicatrix1 & IND1_HZ) ? 1 : 0;
184 count += (rs_packet->indicatrix1 & IND1_OHM) ? 1 : 0;
185 count += (rs_packet->indicatrix1 & IND1_FARAD) ? 1 : 0;
186 count += (rs_packet->indicatrix1 & IND1_AMP) ? 1 : 0;
187 count += (rs_packet->indicatrix1 & IND1_VOLT) ? 1 : 0;
188 count += (rs_packet->indicatrix2 & IND2_DBM) ? 1 : 0;
189 count += (rs_packet->indicatrix2 & IND2_SEC) ? 1 : 0;
190 count += (rs_packet->indicatrix2 & IND2_DUTY) ? 1 : 0;
191 count += (rs_packet->indicatrix2 & IND2_HFE) ? 1 : 0;
193 sr_err("More than one measurement type detected in packet.");
201 * Since the 22-812 does not identify itself in any way, shape, or form,
202 * we really don't know for sure who is sending the data. We must use every
203 * possible check to filter out bad packets, especially since detection of the
204 * 22-812 depends on how well we can filter the packets.
206 SR_PRIV gboolean sr_rs9lcd_packet_valid(const uint8_t *buf)
208 const struct rs9lcd_packet *rs_packet = (void *)buf;
211 * Check for valid mode first, before calculating the checksum. No
212 * point calculating the checksum, if we know we'll reject the packet.
214 if (!(rs_packet->mode < MODE_INVALID))
217 if (!checksum_valid(rs_packet)) {
218 sr_spew("Packet with invalid checksum. Discarding.");
222 if (!selection_good(rs_packet)) {
223 sr_spew("Packet with invalid selection bits. Discarding.");
230 static uint8_t decode_digit(uint8_t raw_digit)
232 /* Take out the decimal point, so we can use a simple switch(). */
233 raw_digit &= ~DP_MASK;
258 sr_err("Invalid digit byte: 0x%02x.", raw_digit);
263 static double lcd_to_double(const struct rs9lcd_packet *rs_packet, int type)
265 double rawval, multiplier = 1;
266 uint8_t digit, raw_digit;
267 gboolean dp_reached = FALSE;
270 /* end = 1: Don't parse last digit. end = 0: Parse all digits. */
271 end = (type == READ_TEMP) ? 1 : 0;
273 /* We have 4 digits, and we start from the most significant. */
274 for (i = 3; i >= end; i--) {
275 raw_digit = *(&(rs_packet->digit4) + i);
276 digit = decode_digit(raw_digit);
282 * Digit 1 does not have a decimal point. Instead, the decimal
283 * point is used to indicate MAX, so we must avoid testing it.
285 if ((i < 3) && (raw_digit & DP_MASK))
289 rawval = rawval * 10 + digit;
291 rawval *= multiplier;
292 if (rs_packet->info & INFO_NEG)
295 /* See if we need to multiply our raw value by anything. */
296 if (rs_packet->indicatrix1 & IND2_NANO)
298 else if (rs_packet->indicatrix2 & IND2_MICRO)
300 else if (rs_packet->indicatrix1 & IND1_MILI)
302 else if (rs_packet->indicatrix1 & IND1_KILO)
304 else if (rs_packet->indicatrix1 & IND1_MEGA)
310 static gboolean is_celsius(const struct rs9lcd_packet *rs_packet)
312 return ((rs_packet->digit4 & ~DP_MASK) == LCD_C);
315 static gboolean is_shortcirc(const struct rs9lcd_packet *rs_packet)
317 return ((rs_packet->digit2 & ~DP_MASK) == LCD_h);
320 static gboolean is_logic_high(const struct rs9lcd_packet *rs_packet)
322 sr_spew("Digit 2: 0x%02x.", rs_packet->digit2 & ~DP_MASK);
323 return ((rs_packet->digit2 & ~DP_MASK) == LCD_H);
326 SR_PRIV int sr_rs9lcd_parse(const uint8_t *buf, float *floatval,
327 struct sr_datafeed_analog *analog, void *info)
329 const struct rs9lcd_packet *rs_packet = (void *)buf;
334 rawval = lcd_to_double(rs_packet, READ_ALL);
336 switch (rs_packet->mode) {
338 analog->mq = SR_MQ_VOLTAGE;
339 analog->unit = SR_UNIT_VOLT;
340 analog->mqflags |= SR_MQFLAG_DC;
343 analog->mq = SR_MQ_VOLTAGE;
344 analog->unit = SR_UNIT_VOLT;
345 analog->mqflags |= SR_MQFLAG_AC;
350 analog->mq = SR_MQ_CURRENT;
351 analog->unit = SR_UNIT_AMPERE;
352 analog->mqflags |= SR_MQFLAG_DC;
357 analog->mq = SR_MQ_CURRENT;
358 analog->unit = SR_UNIT_AMPERE;
359 analog->mqflags |= SR_MQFLAG_AC;
362 analog->mq = SR_MQ_RESISTANCE;
363 analog->unit = SR_UNIT_OHM;
366 analog->mq = SR_MQ_CAPACITANCE;
367 analog->unit = SR_UNIT_FARAD;
370 analog->mq = SR_MQ_CONTINUITY;
371 analog->unit = SR_UNIT_BOOLEAN;
372 *analog->data = is_shortcirc(rs_packet);
375 analog->mq = SR_MQ_VOLTAGE;
376 analog->unit = SR_UNIT_VOLT;
377 analog->mqflags |= SR_MQFLAG_DIODE | SR_MQFLAG_DC;
382 analog->mq = SR_MQ_FREQUENCY;
383 analog->unit = SR_UNIT_HERTZ;
387 * No matter whether or not we have an actual voltage reading,
388 * we are measuring voltage, so we set our MQ as VOLTAGE.
390 analog->mq = SR_MQ_VOLTAGE;
391 if (!isnan(rawval)) {
392 /* We have an actual voltage. */
393 analog->unit = SR_UNIT_VOLT;
395 /* We have either HI or LOW. */
396 analog->unit = SR_UNIT_BOOLEAN;
397 *analog->data = is_logic_high(rs_packet);
401 analog->mq = SR_MQ_GAIN;
402 analog->unit = SR_UNIT_UNITLESS;
407 analog->mq = SR_MQ_DUTY_CYCLE;
408 analog->unit = SR_UNIT_PERCENTAGE;
411 case MODE_VOLT_WIDTH:
413 analog->mq = SR_MQ_PULSE_WIDTH;
414 analog->unit = SR_UNIT_SECOND;
416 analog->mq = SR_MQ_TEMPERATURE;
417 /* We need to reparse. */
418 *analog->data = lcd_to_double(rs_packet, READ_TEMP);
419 analog->unit = is_celsius(rs_packet) ?
420 SR_UNIT_CELSIUS : SR_UNIT_FAHRENHEIT;
423 analog->mq = SR_MQ_POWER;
424 analog->unit = SR_UNIT_DECIBEL_MW;
425 analog->mqflags |= SR_MQFLAG_AC;
428 sr_err("Unknown mode: %d.", rs_packet->mode);
432 if (rs_packet->info & INFO_HOLD)
433 analog->mqflags |= SR_MQFLAG_HOLD;
434 if (rs_packet->digit4 & DIG4_MAX)
435 analog->mqflags |= SR_MQFLAG_MAX;
436 if (rs_packet->indicatrix2 & IND2_MIN)
437 analog->mqflags |= SR_MQFLAG_MIN;
438 if (rs_packet->info & INFO_AUTO)
439 analog->mqflags |= SR_MQFLAG_AUTORANGE;