]> sigrok.org Git - libsigrok.git/blobdiff - hardware/common/dmm/es519xx.c
build: Portability fixes.
[libsigrok.git] / hardware / common / dmm / es519xx.c
index 3340be98c48ac20c13700f333e161c0eece9b302..075587ce40fcc37fcde278f84ec7976ea5a94ee7 100644 (file)
@@ -35,7 +35,7 @@
 #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 */
@@ -44,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 */
@@ -54,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 */
@@ -64,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 */
@@ -74,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 */
@@ -84,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,
@@ -106,7 +111,7 @@ static int parse_value(const uint8_t *buf, struct es519xx_info *info,
        } else if (!isdigit(buf[1]) || !isdigit(buf[2]) ||
                   !isdigit(buf[3]) || !isdigit(buf[4]) ||
                   (num_digits == 5 && !isdigit(buf[5]))) {
-               sr_err("Value contained invalid digits: %02x %02x %02x %02x "
+               sr_dbg("Value contained invalid digits: %02x %02x %02x %02x "
                       "(%c %c %c %c).", buf[1], buf[2], buf[3], buf[4],
                       buf[1], buf[2], buf[3], buf[4]);
                return SR_ERR;
@@ -162,6 +167,10 @@ static int parse_range(uint8_t b, float *floatval,
                mode = 6; /* Frequency */
        else if (info->is_capacitance)
                mode = 7; /* Capacitance */
+       else if (info->is_diode)
+               mode = 8; /* Diode */
+       else if (info->is_duty_cycle)
+               mode = 0; /* Dummy, unused */
        else {
                sr_dbg("Invalid mode, range byte was: 0x%02x.", b);
                return SR_ERR;
@@ -173,6 +182,8 @@ static int parse_range(uint8_t b, float *floatval,
                else if (info->is_milli)
                        factor = (const float[]){1e-2, 1e-1}[idx];
        }
+       else if (info->is_duty_cycle)
+               factor = 1e-1;
        else if (info->baudrate == 2400)
                factor = factors_2400_11b[mode][idx];
        else if (info->fivedigits)
@@ -328,7 +339,7 @@ static void parse_flags(const uint8_t *buf, struct es519xx_info *info)
                        info->is_adp3 = TRUE;
                        break;
                default:
-                       sr_err("Invalid function byte: 0x%02x.", buf[function]);
+                       sr_dbg("Invalid function byte: 0x%02x.", buf[function]);
                        break;
                }
        } else {
@@ -362,9 +373,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;
@@ -396,11 +407,28 @@ static void parse_flags(const uint8_t *buf, struct es519xx_info *info)
                        info->is_adp3 = TRUE;
                        break;
                default:
-                       sr_err("Invalid function byte: 0x%02x.", buf[function]);
+                       sr_dbg("Invalid function byte: 0x%02x.", buf[function]);
                        break;
                }
        }
 
+       if (info->is_vahz && (info->is_voltage || info->is_current)) {
+               info->is_voltage = FALSE;
+               info->is_current = FALSE;
+               info->is_milli = info->is_micro = FALSE;
+               if (info->packet_size == 14) {
+                       if (info->is_judge)
+                               info->is_duty_cycle = TRUE;
+                       else
+                               info->is_frequency = TRUE;
+               } else {
+                       if (info->is_judge)
+                               info->is_rpm = TRUE;
+                       else
+                               info->is_frequency = TRUE;
+               }
+       }
+
        if (info->is_current && (info->is_micro || info->is_milli) && info->is_vasel) {
                info->is_current = info->is_auto = FALSE;
                info->is_voltage = TRUE;
@@ -525,7 +553,7 @@ static gboolean flags_valid(const struct es519xx_info *info)
        count  = (info->is_micro) ? 1 : 0;
        count += (info->is_milli) ? 1 : 0;
        if (count > 1) {
-               sr_err("More than one multiplier detected in packet.");
+               sr_dbg("More than one multiplier detected in packet.");
                return FALSE;
        }
 
@@ -540,13 +568,13 @@ static gboolean flags_valid(const struct es519xx_info *info)
        count += (info->is_diode) ? 1 : 0;
        count += (info->is_rpm) ? 1 : 0;
        if (count > 1) {
-               sr_err("More than one measurement type detected in packet.");
+               sr_dbg("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.");
+               sr_dbg("Both AC and DC flags detected in packet.");
                return FALSE;
        }
 
@@ -584,7 +612,7 @@ static int sr_es519xx_parse(const uint8_t *buf, float *floatval,
                return SR_ERR;
 
        if ((ret = parse_value(buf, info, floatval)) != SR_OK) {
-               sr_err("Error parsing value: %d.", ret);
+               sr_dbg("Error parsing value: %d.", ret);
                return ret;
        }
 
@@ -601,8 +629,9 @@ 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 = { 0 };
+       struct es519xx_info info;
 
+       memset(&info, 0, sizeof(struct es519xx_info));
        info.baudrate = 2400;
        info.packet_size = 11;
 
@@ -628,8 +657,9 @@ 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 = { 0 };
+       struct es519xx_info info;
 
+       memset(&info, 0, sizeof(struct es519xx_info));
        info.baudrate = 2400;
        info.packet_size = 11;
        info.alt_functions = TRUE;
@@ -657,8 +687,9 @@ 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 = { 0 };
+       struct es519xx_info info;
 
+       memset(&info, 0, sizeof(struct es519xx_info));
        info.baudrate = 19200;
        info.packet_size = 11;
        info.fivedigits = TRUE;
@@ -686,8 +717,9 @@ 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 = { 0 };
+       struct es519xx_info info;
 
+       memset(&info, 0, sizeof(struct es519xx_info));
        info.baudrate = 19200;
        info.packet_size = 11;
        info.clampmeter = TRUE;
@@ -715,8 +747,9 @@ 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 = { 0 };
+       struct es519xx_info info;
 
+       memset(&info, 0, sizeof(struct es519xx_info));
        info.baudrate = 19200;
        info.packet_size = 11;
 
@@ -742,8 +775,9 @@ 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 = { 0 };
+       struct es519xx_info info;
 
+       memset(&info, 0, sizeof(struct es519xx_info));
        info.baudrate = 19200;
        info.packet_size = 14;
 
@@ -769,8 +803,9 @@ 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 = { 0 };
+       struct es519xx_info info;
 
+       memset(&info, 0, sizeof(struct es519xx_info));
        info.baudrate = 19200;
        info.packet_size = 14;
        info.selectable_lpf = TRUE;