Rework FS9721 parser.

Merge parts of the tekpower-dmm code (the chip of the TekPower
TP4000ZC seems to be an FS9721_LP3 too) and rework parts of the functions.

Adapt the tekpower-dmm and uni-t-dmm code accordingly.
This commit is contained in:
Uwe Hermann 2012-11-01 15:58:09 +01:00
parent be5c1d3b52
commit 8c1adf3738
7 changed files with 178 additions and 117 deletions

View File

@ -2,6 +2,7 @@
* This file is part of the sigrok project.
*
* Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
* Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -75,16 +76,66 @@ static int parse_digit(uint8_t b)
}
}
/**
* Parse the numerical value from a protocol packet.
*
* @param buf Buffer containing the 14-byte protocol packet.
* @param result Pointer to a float variable. That variable will contain the
* result value upon parsing success.
*
* @return SR_OK upon success, SR_ERR upon failure. Upon errors, the result
* variable contents are undefined and should not be used.
*/
static gboolean sync_nibbles_valid(const uint8_t *buf)
{
int i;
/* Check the synchronization nibbles, and make sure they all match. */
for (i = 0; i < FS9721_PACKET_SIZE; i++) {
if (((buf[i] >> 4) & 0x0f) != (i + 1)) {
sr_err("Sync nibble in byte %d (0x%02x) is invalid.",
i, buf[i]);
return FALSE;
}
}
return TRUE;
}
static gboolean flags_valid(const struct fs9721_info *info)
{
int count;
/* Does the packet have more than one multiplier? */
count = 0;
count += (info->is_nano) ? 1 : 0;
count += (info->is_micro) ? 1 : 0;
count += (info->is_milli) ? 1 : 0;
count += (info->is_kilo) ? 1 : 0;
count += (info->is_mega) ? 1 : 0;
if (count > 1) {
sr_err("More than one multiplier detected in packet.");
return FALSE;
}
/* Does the packet "measure" more than one type of value? */
count = 0;
count += (info->is_hz) ? 1 : 0;
count += (info->is_ohm) ? 1 : 0;
count += (info->is_farad) ? 1 : 0;
count += (info->is_ampere) ? 1 : 0;
count += (info->is_volt) ? 1 : 0;
count += (info->is_percent) ? 1 : 0;
if (count > 1) {
sr_err("More than one measurement type detected in packet.");
return FALSE;
}
/* Both AC and DC set? */
if (info->is_ac && info->is_dc) {
sr_err("Both AC and DC flags detected in packet.");
return FALSE;
}
/* RS232 flag not set? */
if (!info->is_rs232) {
sr_err("No RS232 flag detected in packet.");
return FALSE;
}
return TRUE;
}
static int parse_value(const uint8_t *buf, float *result)
{
int i, sign, intval = 0, digits[4];
@ -130,7 +181,6 @@ static int parse_value(const uint8_t *buf, float *result)
intval += digits[i];
}
/* Store the value in a float variable. */
floatval = (float)intval;
/* Decimal point position. */
@ -157,154 +207,156 @@ static int parse_value(const uint8_t *buf, float *result)
return SR_OK;
}
/**
* Parse various flags in a protocol packet.
*
* @param buf Buffer containing the 14-byte protocol packet.
* @param floatval Pointer to a float variable which should contain the value
* parsed using parse_value(). That variable will be modified
* in-place depending on the flags in the protocol packet.
* @param analog Pointer to a struct sr_datafeed_analog. The struct will be
* filled with the relevant data according to the flags in the
* protocol packet.
*
* @return SR_OK upon success, SR_ERR upon failure. Upon errors, the 'floatval'
* and 'analog' variable contents are undefined and should not be used.
*/
static int parse_flags(const uint8_t *buf, float *floatval,
struct sr_datafeed_analog *analog)
static void parse_flags(const uint8_t *buf, struct fs9721_info *info)
{
gboolean is_ac, is_dc, is_auto, is_rs232, is_micro, is_nano, is_kilo;
gboolean is_diode, is_milli, is_percent, is_mega, is_beep, is_farad;
gboolean is_ohm, is_rel, is_hold, is_ampere, is_volt, is_hz, is_bat;
gboolean is_c2c1_11, is_c2c1_10, is_c2c1_01, is_c2c1_00;
/* Byte 0: LCD SEG1 */
is_ac = (buf[0] & (1 << 3)) != 0;
is_dc = (buf[0] & (1 << 2)) != 0;
is_auto = (buf[0] & (1 << 1)) != 0;
is_rs232 = (buf[0] & (1 << 0)) != 0;
info->is_ac = (buf[0] & (1 << 3)) != 0;
info->is_dc = (buf[0] & (1 << 2)) != 0;
info->is_auto = (buf[0] & (1 << 1)) != 0;
info->is_rs232 = (buf[0] & (1 << 0)) != 0;
/* Byte 1: LCD SEG2 */
info->is_sign = (buf[1] & (1 << 3)) != 0;
/* Byte 9: LCD SEG10 */
is_micro = (buf[9] & (1 << 3)) != 0;
is_nano = (buf[9] & (1 << 2)) != 0;
is_kilo = (buf[9] & (1 << 1)) != 0;
is_diode = (buf[9] & (1 << 0)) != 0;
info->is_micro = (buf[9] & (1 << 3)) != 0;
info->is_nano = (buf[9] & (1 << 2)) != 0;
info->is_kilo = (buf[9] & (1 << 1)) != 0;
info->is_diode = (buf[9] & (1 << 0)) != 0;
/* Byte 10: LCD SEG11 */
is_milli = (buf[10] & (1 << 3)) != 0;
is_percent = (buf[10] & (1 << 2)) != 0;
is_mega = (buf[10] & (1 << 1)) != 0;
is_beep = (buf[10] & (1 << 0)) != 0;
info->is_milli = (buf[10] & (1 << 3)) != 0;
info->is_percent = (buf[10] & (1 << 2)) != 0;
info->is_mega = (buf[10] & (1 << 1)) != 0;
info->is_beep = (buf[10] & (1 << 0)) != 0;
/* Byte 11: LCD SEG12 */
is_farad = (buf[11] & (1 << 3)) != 0;
is_ohm = (buf[11] & (1 << 2)) != 0;
is_rel = (buf[11] & (1 << 1)) != 0;
is_hold = (buf[11] & (1 << 0)) != 0;
info->is_farad = (buf[11] & (1 << 3)) != 0;
info->is_ohm = (buf[11] & (1 << 2)) != 0;
info->is_rel = (buf[11] & (1 << 1)) != 0;
info->is_hold = (buf[11] & (1 << 0)) != 0;
/* Byte 12: LCD SEG13 */
is_ampere = (buf[12] & (1 << 3)) != 0;
is_volt = (buf[12] & (1 << 2)) != 0;
is_hz = (buf[12] & (1 << 1)) != 0;
is_bat = (buf[12] & (1 << 0)) != 0;
info->is_ampere = (buf[12] & (1 << 3)) != 0;
info->is_volt = (buf[12] & (1 << 2)) != 0;
info->is_hz = (buf[12] & (1 << 1)) != 0;
info->is_bat = (buf[12] & (1 << 0)) != 0;
/* Byte 13: LCD SEG14 */
is_c2c1_11 = (buf[13] & (1 << 3)) != 0;
is_c2c1_10 = (buf[13] & (1 << 2)) != 0;
is_c2c1_01 = (buf[13] & (1 << 1)) != 0;
is_c2c1_00 = (buf[13] & (1 << 0)) != 0;
info->is_c2c1_11 = (buf[13] & (1 << 3)) != 0;
info->is_c2c1_10 = (buf[13] & (1 << 2)) != 0;
info->is_c2c1_01 = (buf[13] & (1 << 1)) != 0;
info->is_c2c1_00 = (buf[13] & (1 << 0)) != 0;
}
static void handle_flags(struct sr_datafeed_analog *analog, float *floatval,
const struct fs9721_info *info)
{
/* Factors */
if (is_nano)
if (info->is_nano)
*floatval /= 1000000000;
if (is_micro)
if (info->is_micro)
*floatval /= 1000000;
if (is_milli)
if (info->is_milli)
*floatval /= 1000;
if (is_kilo)
if (info->is_kilo)
*floatval *= 1000;
if (is_mega)
if (info->is_mega)
*floatval *= 1000000;
/* Measurement modes */
if (is_volt) {
if (info->is_volt) {
analog->mq = SR_MQ_VOLTAGE;
analog->unit = SR_UNIT_VOLT;
}
if (is_ampere) {
if (info->is_ampere) {
analog->mq = SR_MQ_CURRENT;
analog->unit = SR_UNIT_AMPERE;
}
if (is_ohm) {
if (info->is_ohm) {
analog->mq = SR_MQ_RESISTANCE;
analog->unit = SR_UNIT_OHM;
}
if (is_hz) {
if (info->is_hz) {
analog->mq = SR_MQ_FREQUENCY;
analog->unit = SR_UNIT_HERTZ;
}
if (is_farad) {
if (info->is_farad) {
analog->mq = SR_MQ_CAPACITANCE;
analog->unit = SR_UNIT_FARAD;
}
if (is_beep) {
if (info->is_beep) {
analog->mq = SR_MQ_CONTINUITY;
analog->unit = SR_UNIT_BOOLEAN;
*floatval = (*floatval < 0.0) ? 0.0 : 1.0;
*floatval = (*floatval == INFINITY) ? 0.0 : 1.0;
}
if (is_diode) {
if (info->is_diode) {
analog->mq = SR_MQ_VOLTAGE;
analog->unit = SR_UNIT_VOLT;
}
if (is_percent) {
if (info->is_percent) {
analog->mq = SR_MQ_DUTY_CYCLE;
analog->unit = SR_UNIT_PERCENTAGE;
}
/* Measurement related flags */
if (is_ac)
if (info->is_ac)
analog->mqflags |= SR_MQFLAG_AC;
if (is_dc)
if (info->is_dc)
analog->mqflags |= SR_MQFLAG_DC;
if (is_auto)
if (info->is_auto)
analog->mqflags |= SR_MQFLAG_AUTORANGE;
if (is_hold)
if (info->is_hold)
analog->mqflags |= SR_MQFLAG_HOLD;
if (is_rel)
if (info->is_rel)
analog->mqflags |= SR_MQFLAG_RELATIVE;
/* Other flags */
if (is_rs232)
if (info->is_rs232)
sr_spew("RS232 enabled.");
if (is_bat)
if (info->is_bat)
sr_spew("Battery is low.");
if (is_c2c1_00)
if (info->is_c2c1_00)
sr_spew("User-defined LCD symbol 0 is active.");
if (is_c2c1_01)
if (info->is_c2c1_01)
sr_spew("User-defined LCD symbol 1 is active.");
if (is_c2c1_10)
if (info->is_c2c1_10)
sr_spew("User-defined LCD symbol 2 is active.");
if (is_c2c1_11)
if (info->is_c2c1_11)
sr_spew("User-defined LCD symbol 3 is active.");
}
return SR_OK;
SR_PRIV gboolean sr_fs9721_is_packet_start(uint8_t b)
{
return (((b >> 4) & 0x0f) == 0x01);
}
SR_PRIV gboolean sr_fs9721_packet_valid(const uint8_t *buf)
{
struct fs9721_info info;
parse_flags(buf, &info);
return (sync_nibbles_valid(buf) && flags_valid(&info));
}
/**
* Parse a protocol packet.
*
* @param buf Buffer containing the 14-byte protocol packet.
* @param floatval Pointer to a float variable. That variable will be modified
* in-place depending on the protocol packet.
* @param floatval Pointer to a float variable. That variable will contain the
* result value upon parsing success.
* @param analog Pointer to a struct sr_datafeed_analog. The struct will be
* filled with data according to the protocol packet.
* @param info Pointer to a struct fs9721_info. The struct will be filled
* with data according to the protocol packet.
*
* @return SR_OK upon success, SR_ERR upon failure. Upon errors, the
* 'analog' variable contents are undefined and should not be used.
*/
SR_PRIV int sr_dmm_parse_fs9721(const uint8_t *buf, float *floatval,
struct sr_datafeed_analog *analog)
SR_PRIV int sr_fs9721_parse(const uint8_t *buf, float *floatval,
struct sr_datafeed_analog *analog,
struct fs9721_info *info)
{
int ret;
@ -313,10 +365,8 @@ SR_PRIV int sr_dmm_parse_fs9721(const uint8_t *buf, float *floatval,
return ret;
}
if ((ret = parse_flags(buf, floatval, analog)) != SR_OK) {
sr_err("Error parsing flags: %d.", ret);
return ret;
}
parse_flags(buf, info);
handle_flags(analog, floatval, info);
return SR_OK;
}

View File

@ -95,10 +95,9 @@ static GSList *lcd14_scan(const char *conn, const char *serialcomm)
struct drv_context *drvc;
struct dev_context *devc;
struct sr_probe *probe;
struct fs9721_packet *packet;
GSList *devices;
int i, len, fd, retry, good_packets = 0, dropped, ret;
char buf[128], *b;
uint8_t buf[128], *b;
if ((fd = serial_open(conn, O_RDONLY | O_NONBLOCK)) == -1) {
sr_err("Unable to open %s: %s.", conn, strerror(errno));
@ -127,7 +126,7 @@ static GSList *lcd14_scan(const char *conn, const char *serialcomm)
/* Let's get a bit of data and see if we can find a packet. */
len = sizeof(buf);
serial_readline(fd, &b, &len, 500);
serial_readline(fd, (char **)&b, &len, 500);
if ((len == 0) || (len < FS9721_PACKET_SIZE)) {
/* Not enough data received, is the DMM connected? */
continue;
@ -136,8 +135,7 @@ static GSList *lcd14_scan(const char *conn, const char *serialcomm)
/* Let's treat our buffer like a stream, and find any
* valid packets */
for (i = 0; i < len - FS9721_PACKET_SIZE + 1;) {
packet = (void *)(&buf[i]);
if (!fs9721_is_packet_valid(packet, NULL)) {
if (!sr_fs9721_packet_valid(&buf[i])) {
i++;
continue;
}

View File

@ -26,21 +26,25 @@
#include "libsigrok-internal.h"
#include "protocol.h"
/* User-defined FS9721_LP3 flag 'c2c1_10' means temperature on this DMM. */
#define is_temperature info.is_c2c1_10
/* Now see what the value means, and pass that on. */
static void fs9721_serial_handle_packet(const struct fs9721_data *data,
static void fs9721_serial_handle_packet(const uint8_t *buf,
struct dev_context *devc)
{
float rawval;
float floatval;
struct sr_datafeed_packet packet;
struct sr_datafeed_analog *analog;
struct fs9721_info info;
if (!(analog = g_try_malloc0(sizeof(struct sr_datafeed_analog)))) {
sr_err("Failed to malloc packet.");
sr_err("Analog packet malloc failed.");
return;
}
if (!(analog->data = g_try_malloc(sizeof(float)))) {
sr_err("Failed to malloc data.");
sr_err("Analog value malloc failed.");
g_free(analog);
return;
}
@ -48,16 +52,15 @@ static void fs9721_serial_handle_packet(const struct fs9721_data *data,
analog->num_samples = 1;
analog->mq = -1;
sr_dmm_smart_parse_fs9721(data, &rawval, analog);
*analog->data = rawval;
sr_fs9721_parse(buf, &floatval, analog, &info);
*analog->data = floatval;
if (data->flags & FLAG_TEMP_CELSIUS) {
if (is_temperature) {
analog->mq = SR_MQ_TEMPERATURE;
/* No Kelvin or Fahrenheit from the device, just Celsius. */
analog->unit = SR_UNIT_CELSIUS;
}
if (analog->mq != -1) {
/* Got a measurement. */
packet.type = SR_DF_ANALOG;
@ -73,8 +76,6 @@ static void fs9721_serial_handle_packet(const struct fs9721_data *data,
static void handle_new_data(struct dev_context *devc, int fd)
{
int len, i, offset = 0;
struct fs9721_packet *packet;
struct fs9721_data data;
/* Try to get as much data as the buffer can hold. */
len = DMM_BUFSIZE - devc->buflen;
@ -87,9 +88,8 @@ static void handle_new_data(struct dev_context *devc, int fd)
/* Now look for packets in that data. */
while ((devc->buflen - offset) >= FS9721_PACKET_SIZE) {
packet = (void *)(devc->buf + offset);
if (fs9721_is_packet_valid(packet, &data)) {
fs9721_serial_handle_packet(&data, devc);
if (sr_fs9721_packet_valid(devc->buf + offset)) {
fs9721_serial_handle_packet(devc->buf + offset, devc);
offset += FS9721_PACKET_SIZE;
} else {
offset++;

View File

@ -20,8 +20,6 @@
#ifndef LIBSIGROK_HARDWARE_TEKPOWER_DMM_PROTOCOL_H
#define LIBSIGROK_HARDWARE_TEKPOWER_DMM_PROTOCOL_H
#include "hardware/common/dmm/fs9721.h"
/* Message logging helpers with driver-specific prefix string. */
#define DRIVER_LOG_DOMAIN "tekpower-dmm: "
#define sr_log(l, s, args...) sr_log(l, DRIVER_LOG_DOMAIN s, ## args)
@ -33,8 +31,6 @@
#define DMM_BUFSIZE 256
#define FLAG_TEMP_CELSIUS FS9721_USR2
/** Private, per-device-instance driver context. */
struct dev_context {
/** The current sampling limit (in number of samples). */

View File

@ -172,7 +172,7 @@ static int hw_init(int dmm)
di = di_ut61d;
else if (dmm == VOLTCRAFT_VC820)
di = di_vc820;
sr_dbg("Selected '%s' driver.", di->name);
sr_dbg("Selected '%s' subdriver.", di->name);
di->priv = drvc;

View File

@ -70,6 +70,7 @@ static void decode_packet(struct dev_context *devc, int dmm, const uint8_t *buf)
{
struct sr_datafeed_packet packet;
struct sr_datafeed_analog analog;
struct fs9721_info info;
float floatval;
int ret;
@ -79,7 +80,7 @@ static void decode_packet(struct dev_context *devc, int dmm, const uint8_t *buf)
if (dmm == UNI_T_UT61D)
ret = sr_dmm_parse_fs9922(buf, &floatval, &analog);
else if (dmm == VOLTCRAFT_VC820)
ret = sr_dmm_parse_fs9721(buf, &floatval, &analog);
ret = sr_fs9721_parse(buf, &floatval, &analog, &info);
if (ret != SR_OK) {
sr_err("Invalid DMM packet, ignoring.");
return;
@ -236,7 +237,7 @@ static int uni_t_dmm_receive_data(int fd, int revents, int dmm, void *cb_data)
return TRUE;
} else if (dmm == VOLTCRAFT_VC820) {
/* Valid packets have 0x1 as high nibble. */
if ((buf[1] & 0xf0) != 0x10)
if (!sr_fs9721_is_packet_start(buf[1]))
return TRUE;
}
synced_on_first_packet = TRUE;
@ -251,6 +252,10 @@ static int uni_t_dmm_receive_data(int fd, int revents, int dmm, void *cb_data)
if (data_byte_counter == NUM_DATA_BYTES) {
log_dmm_packet(pbuf);
data_byte_counter = 0;
if (!sr_fs9721_packet_valid(pbuf)) {
sr_err("Invalid packet.");
return TRUE;
}
decode_packet(devc, dmm, pbuf);
memset(pbuf, 0x00, NUM_DATA_BYTES);
}

View File

@ -155,7 +155,19 @@ SR_PRIV int sr_dmm_parse_fs9922(const uint8_t *buf, float *floatval,
/*--- hardware/common/dmm/fs9721.c ------------------------------------------*/
SR_PRIV int sr_dmm_parse_fs9721(const uint8_t *buf, float *floatval,
struct sr_datafeed_analog *analog);
#define FS9721_PACKET_SIZE 14
struct fs9721_info {
gboolean is_ac, is_dc, is_auto, is_rs232, is_micro, is_nano, is_kilo;
gboolean is_diode, is_milli, is_percent, is_mega, is_beep, is_farad;
gboolean is_ohm, is_rel, is_hold, is_ampere, is_volt, is_hz, is_bat;
gboolean is_c2c1_11, is_c2c1_10, is_c2c1_01, is_c2c1_00, is_sign;
};
SR_PRIV gboolean sr_fs9721_is_packet_start(uint8_t b);
SR_PRIV gboolean sr_fs9721_packet_valid(const uint8_t *buf);
SR_PRIV int sr_fs9721_parse(const uint8_t *buf, float *floatval,
struct sr_datafeed_analog *analog,
struct fs9721_info *info);
#endif