]> sigrok.org Git - libsigrok.git/blobdiff - hardware/common/dmm/es519xx.c
es519xx: fix switching between frequency and duty cycle mode on 14 bytes chips
[libsigrok.git] / hardware / common / dmm / es519xx.c
index 0a0a2231e64723c83f657a55794676559d06af58..838592ed4a253e1f96e0ba14856f4e873a32e29a 100644 (file)
 #include "libsigrok.h"
 #include "libsigrok-internal.h"
 
-/* Message logging helpers with subsystem-specific prefix string. */
-#define LOG_PREFIX "es519xx: "
-#define sr_log(l, s, args...) sr_log(l, LOG_PREFIX s, ## args)
-#define sr_spew(s, args...) sr_spew(LOG_PREFIX s, ## args)
-#define sr_dbg(s, args...) sr_dbg(LOG_PREFIX s, ## args)
-#define sr_info(s, args...) sr_info(LOG_PREFIX s, ## args)
-#define sr_warn(s, args...) sr_warn(LOG_PREFIX s, ## args)
-#define sr_err(s, args...) sr_err(LOG_PREFIX s, ## args)
+#define LOG_PREFIX "es519xx"
 
 /* Factors for the respective measurement mode (0 means "invalid"). */
-static const float factors_2400_11b[8][8] = {
+static const float factors_2400_11b[9][8] = {
        {1e-4,  1e-3,  1e-2,  1e-1, 1,    0,    0,    0   }, /* V */
        {1e-7,  1e-6,  0,     0,    0,    0,    0,    0   }, /* uA */
        {1e-5,  1e-4,  0,     0,    0,    0,    0,    0   }, /* mA */
@@ -51,8 +44,9 @@ static const float factors_2400_11b[8][8] = {
        {1e-1,  1,     1e1,   1e2,  1e3,  1e4,  0,    0   }, /* Resistance */
        {1,     1e1,   1e2,   1e3,  1e4,  1e5,  0,    0   }, /* Frequency */
        {1e-12, 1e-11, 1e-10, 1e-9, 1e-8, 1e-7, 1e-6, 1e-5}, /* Capacitance */
+       {1e-3,  0,     0,     0,    0,    0,    0,    0   }, /* Diode */
 };
-static const float factors_19200_11b_5digits[8][8] = {
+static const float factors_19200_11b_5digits[9][8] = {
        {1e-4,  1e-3,  1e-2,  1e-1, 1e-5, 0,    0,    0},    /* V */
        {1e-8,  1e-7,  0,     0,    0,    0,    0,    0},    /* uA */
        {1e-6,  1e-5,  0,     0,    0,    0,    0,    0},    /* mA */
@@ -61,8 +55,9 @@ static const float factors_19200_11b_5digits[8][8] = {
        {1e-2,  1e-1,  1,     1e1,  1e2,  1e3,  1e4,  0},    /* Resistance */
        {1e-1,  0,     1,     1e1,  1e2,  1e3,  1e4,  0},    /* Frequency */
        {1e-12, 1e-11, 1e-10, 1e-9, 1e-8, 1e-7, 1e-6, 1e-5}, /* Capacitance */
+       {1e-4,  0,     0,     0,    0,    0,    0,    0   }, /* Diode */
 };
-static const float factors_19200_11b_clampmeter[8][8] = {
+static const float factors_19200_11b_clampmeter[9][8] = {
        {1e-3,  1e-2,  1e-1,  1,    1e-4, 0,    0,    0},    /* V */
        {1e-7,  1e-6,  0,     0,    0,    0,    0,    0},    /* uA */
        {1e-5,  1e-4,  0,     0,    0,    0,    0,    0},    /* mA */
@@ -71,8 +66,9 @@ static const float factors_19200_11b_clampmeter[8][8] = {
        {1e-1,  1,     1e1,   1e2,  1e3,  1e4,  0,    0},    /* Resistance */
        {1e-1,  0,     1,     1e1,  1e2,  1e3,  1e4,  0},    /* Frequency */
        {1e-12, 1e-11, 1e-10, 1e-9, 1e-8, 1e-7, 1e-6, 1e-5}, /* Capacitance */
+       {1e-3,  0,     0,     0,    0,    0,    0,    0   }, /* Diode */
 };
-static const float factors_19200_11b[8][8] = {
+static const float factors_19200_11b[9][8] = {
        {1e-3,  1e-2,  1e-1,  1,    1e-4, 0,    0,    0},    /* V */
        {1e-7,  1e-6,  0,     0,    0,    0,    0,    0},    /* uA */
        {1e-5,  1e-4,  0,     0,    0,    0,    0,    0},    /* mA */
@@ -81,8 +77,9 @@ static const float factors_19200_11b[8][8] = {
        {1e-1,  1,     1e1,   1e2,  1e3,  1e4,  0,    0},    /* Resistance */
        {1,     1e1,   1e2,   1e3,  1e4,  0,    0,    0},    /* Frequency */
        {1e-12, 1e-11, 1e-10, 1e-9, 1e-8, 1e-7, 1e-6, 0},    /* Capacitance */
+       {1e-3,  0,     0,     0,    0,    0,    0,    0},    /* Diode */
 };
-static const float factors_19200_14b[8][8] = {
+static const float factors_19200_14b[9][8] = {
        {1e-4,  1e-3,  1e-2,  1e-1, 1e-5, 0,    0,    0},    /* V */
        {1e-8,  1e-7,  0,     0,    0,    0,    0,    0},    /* uA */
        {1e-6,  1e-5,  0,     0,    0,    0,    0,    0},    /* mA */
@@ -91,6 +88,7 @@ static const float factors_19200_14b[8][8] = {
        {1e-2,  1e-1,  1,     1e1,  1e2,  1e3,  1e4,  0},    /* Resistance */
        {1e-2,  1e-1,  0,     1,    1e1,  1e2,  1e3,  1e4},  /* Frequency */
        {1e-12, 1e-11, 1e-10, 1e-9, 1e-8, 1e-7, 1e-6, 1e-5}, /* Capacitance */
+       {1e-4,  0,     0,     0,    0,    0,    0,    0   }, /* Diode */
 };
 
 static int parse_value(const uint8_t *buf, struct es519xx_info *info,
@@ -163,12 +161,14 @@ static int parse_range(uint8_t b, float *floatval,
        else if (info->is_rpm)
                /* Not a typo, it's really index 4 in factors_2400_11b[][]. */
                mode = 4; /* RPM */
-       else if (info->is_resistance)
+       else if (info->is_resistance || info->is_continuity)
                mode = 5; /* Resistance */
        else if (info->is_frequency)
                mode = 6; /* Frequency */
        else if (info->is_capacitance)
                mode = 7; /* Capacitance */
+       else if (info->is_diode)
+               mode = 8; /* Diode */
        else {
                sr_dbg("Invalid mode, range byte was: 0x%02x.", b);
                return SR_ERR;
@@ -369,9 +369,9 @@ static void parse_flags(const uint8_t *buf, struct es519xx_info *info)
                case 0x32: /* Frequency / RPM / duty cycle */
                        if (info->packet_size == 14) {
                                if (info->is_judge)
-                                       info->is_frequency = TRUE;
-                               else
                                        info->is_duty_cycle = TRUE;
+                               else
+                                       info->is_frequency = TRUE;
                        } else {
                                if (info->is_judge)
                                        info->is_rpm = TRUE;
@@ -462,7 +462,7 @@ static void handle_flags(struct sr_datafeed_analog *analog,
        if (info->is_continuity) {
                analog->mq = SR_MQ_CONTINUITY;
                analog->unit = SR_UNIT_BOOLEAN;
-               *floatval = (*floatval < 0.0) ? 0.0 : 1.0;
+               *floatval = (*floatval < 0.0 || *floatval > 25.0) ? 0.0 : 1.0;
        }
        if (info->is_diode) {
                analog->mq = SR_MQ_VOLTAGE;
@@ -595,9 +595,11 @@ static int sr_es519xx_parse(const uint8_t *buf, float *floatval,
                return ret;
        }
 
-       handle_flags(analog, floatval, info);
+       if ((ret = parse_range(buf[0], floatval, info)) != SR_OK)
+               return ret;
 
-       return parse_range(buf[0], floatval, info);
+       handle_flags(analog, floatval, info);
+       return SR_OK;
 }
 
 /*
@@ -606,7 +608,7 @@ static int sr_es519xx_parse(const uint8_t *buf, float *floatval,
  */
 SR_PRIV gboolean sr_es519xx_2400_11b_packet_valid(const uint8_t *buf)
 {
-       struct es519xx_info info;
+       struct es519xx_info info = { 0 };
 
        info.baudrate = 2400;
        info.packet_size = 11;
@@ -620,6 +622,7 @@ SR_PRIV int sr_es519xx_2400_11b_parse(const uint8_t *buf, float *floatval,
        struct es519xx_info *info_local;
 
        info_local = info;
+       memset(info_local, 0, sizeof(struct es519xx_info));
        info_local->baudrate = 2400;
        info_local->packet_size = 11;
 
@@ -632,7 +635,7 @@ SR_PRIV int sr_es519xx_2400_11b_parse(const uint8_t *buf, float *floatval,
  */
 SR_PRIV gboolean sr_es519xx_2400_11b_altfn_packet_valid(const uint8_t *buf)
 {
-       struct es519xx_info info;
+       struct es519xx_info info = { 0 };
 
        info.baudrate = 2400;
        info.packet_size = 11;
@@ -647,6 +650,7 @@ SR_PRIV int sr_es519xx_2400_11b_altfn_parse(const uint8_t *buf,
        struct es519xx_info *info_local;
 
        info_local = info;
+       memset(info_local, 0, sizeof(struct es519xx_info));
        info_local->baudrate = 2400;
        info_local->packet_size = 11;
        info_local->alt_functions = TRUE;
@@ -660,7 +664,7 @@ SR_PRIV int sr_es519xx_2400_11b_altfn_parse(const uint8_t *buf,
  */
 SR_PRIV gboolean sr_es519xx_19200_11b_5digits_packet_valid(const uint8_t *buf)
 {
-       struct es519xx_info info;
+       struct es519xx_info info = { 0 };
 
        info.baudrate = 19200;
        info.packet_size = 11;
@@ -675,6 +679,7 @@ SR_PRIV int sr_es519xx_19200_11b_5digits_parse(const uint8_t *buf,
        struct es519xx_info *info_local;
 
        info_local = info;
+       memset(info_local, 0, sizeof(struct es519xx_info));
        info_local->baudrate = 19200;
        info_local->packet_size = 11;
        info_local->fivedigits = TRUE;
@@ -688,7 +693,7 @@ SR_PRIV int sr_es519xx_19200_11b_5digits_parse(const uint8_t *buf,
  */
 SR_PRIV gboolean sr_es519xx_19200_11b_clamp_packet_valid(const uint8_t *buf)
 {
-       struct es519xx_info info;
+       struct es519xx_info info = { 0 };
 
        info.baudrate = 19200;
        info.packet_size = 11;
@@ -703,6 +708,7 @@ SR_PRIV int sr_es519xx_19200_11b_clamp_parse(const uint8_t *buf,
        struct es519xx_info *info_local;
 
        info_local = info;
+       memset(info_local, 0, sizeof(struct es519xx_info));
        info_local->baudrate = 19200;
        info_local->packet_size = 11;
        info_local->clampmeter = TRUE;
@@ -716,7 +722,7 @@ SR_PRIV int sr_es519xx_19200_11b_clamp_parse(const uint8_t *buf,
  */
 SR_PRIV gboolean sr_es519xx_19200_11b_packet_valid(const uint8_t *buf)
 {
-       struct es519xx_info info;
+       struct es519xx_info info = { 0 };
 
        info.baudrate = 19200;
        info.packet_size = 11;
@@ -730,6 +736,7 @@ SR_PRIV int sr_es519xx_19200_11b_parse(const uint8_t *buf, float *floatval,
        struct es519xx_info *info_local;
 
        info_local = info;
+       memset(info_local, 0, sizeof(struct es519xx_info));
        info_local->baudrate = 19200;
        info_local->packet_size = 11;
 
@@ -742,7 +749,7 @@ SR_PRIV int sr_es519xx_19200_11b_parse(const uint8_t *buf, float *floatval,
  */
 SR_PRIV gboolean sr_es519xx_19200_14b_packet_valid(const uint8_t *buf)
 {
-       struct es519xx_info info;
+       struct es519xx_info info = { 0 };
 
        info.baudrate = 19200;
        info.packet_size = 14;
@@ -756,6 +763,7 @@ SR_PRIV int sr_es519xx_19200_14b_parse(const uint8_t *buf, float *floatval,
        struct es519xx_info *info_local;
 
        info_local = info;
+       memset(info_local, 0, sizeof(struct es519xx_info));
        info_local->baudrate = 19200;
        info_local->packet_size = 14;
 
@@ -768,7 +776,7 @@ SR_PRIV int sr_es519xx_19200_14b_parse(const uint8_t *buf, float *floatval,
  */
 SR_PRIV gboolean sr_es519xx_19200_14b_sel_lpf_packet_valid(const uint8_t *buf)
 {
-       struct es519xx_info info;
+       struct es519xx_info info = { 0 };
 
        info.baudrate = 19200;
        info.packet_size = 14;
@@ -783,6 +791,7 @@ SR_PRIV int sr_es519xx_19200_14b_sel_lpf_parse(const uint8_t *buf,
        struct es519xx_info *info_local;
 
        info_local = info;
+       memset(info_local, 0, sizeof(struct es519xx_info));
        info_local->baudrate = 19200;
        info_local->packet_size = 14;
        info_local->selectable_lpf = TRUE;