X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2Fhardware%2Fgmc-mh-1x-2x%2Fprotocol.c;h=53efdf4255f7500d60c4f07ef36ac6e537f4dff3;hb=396cdca0c3edbf619a9f03379486bbedc56cfdac;hp=6f96d504601c1a65f227b7d63853997dbba40b46;hpb=93b118da4fec6976df924eb77121f07b361b8330;p=libsigrok.git
diff --git a/src/hardware/gmc-mh-1x-2x/protocol.c b/src/hardware/gmc-mh-1x-2x/protocol.c
index 6f96d504..53efdf42 100644
--- a/src/hardware/gmc-mh-1x-2x/protocol.c
+++ b/src/hardware/gmc-mh-1x-2x/protocol.c
@@ -17,17 +17,12 @@
* along with this program. If not, see .
*/
-/** @file
- * Gossen Metrawatt Metrahit 1x/2x drivers
- * @internal
- */
-
+#include
#include
#include
#include "protocol.h"
-/* Internal Headers */
-static guchar calc_chksum_14(guchar* dta);
+static guchar calc_chksum_14(guchar *dta);
static int chk_msg14(struct sr_dev_inst *sdi);
/** Set or clear flags in devc->mqflags. */
@@ -91,10 +86,7 @@ static void decode_ctmv_16(uint8_t ctmv, struct dev_context *devc)
case 0x0d: /* 1101 µF (15S/16S only) */
devc->mq = SR_MQ_CAPACITANCE;
devc->unit = SR_UNIT_FARAD;
- if (ctmv == 0x0c)
- devc->scale1000 = -3;
- else
- devc->scale1000 = -2;
+ devc->scale1000 = (ctmv == 0x0c) ? -3 : -2;
break;
case 0x0e: /* mA, µA */
devc->scale1000 = -1; /* Fall through. */
@@ -115,44 +107,40 @@ static void decode_ctmv_16(uint8_t ctmv, struct dev_context *devc)
*/
static void decode_rs_16(uint8_t rs, struct dev_context *devc)
{
- sr_spew("decode_rs_16(%d) scale = %f", rs, devc->scale);
+ sr_spew("decode_rs_16(%d) scale = %d", rs, devc->scale);
if (rs & 0x04) /* Sign */
- devc->scale *= -1.0;
+ devc->scale = -devc->scale;
- if (devc->mq == SR_MQ_CURRENT) {
- if (rs & 0x08) /* Current is AC */
- devc->mqflags |= SR_MQFLAG_AC;
- else
- devc->mqflags |= SR_MQFLAG_DC;
- }
+ if (devc->mq == SR_MQ_CURRENT)
+ devc->mqflags |= (rs & 0x08) ? SR_MQFLAG_AC : SR_MQFLAG_DC;
switch (rs & 0x03) {
case 0:
if (devc->mq == SR_MQ_VOLTAGE) /* V */
- devc->scale *= 0.1;
+ devc->scale += -1;
else if (devc->mq == SR_MQ_CURRENT) /* 000.0 µA */
- devc->scale *= 0.00001;
+ devc->scale += -5;
else if (devc->mq == SR_MQ_RESISTANCE) {
if (devc->buflen >= 10) {
/* °C with 10 byte msg type, otherwise GOhm. */
devc->mq = SR_MQ_TEMPERATURE;
devc->unit = SR_UNIT_CELSIUS;
- devc->scale *= 0.01;
+ devc->scale += -2;
} else if (devc->scale1000 == 2) {
/* 16I Iso 500/1000V 3 GOhm */
- devc->scale *= 0.1;
+ devc->scale += -1;
}
}
break;
case 1:
- devc->scale *= 0.0001;
+ devc->scale += -4;
break;
case 2:
- devc->scale *= 0.001;
+ devc->scale += -3;
break;
case 3:
- devc->scale *= 0.01;
+ devc->scale += -2;
break;
}
}
@@ -257,7 +245,7 @@ static void decode_ctmv_18(uint8_t ctmv, struct dev_context *devc)
/**
* Decode range/sign/acdc byte special chars, Metrahit 18.
*
- * @param[in] rs Rance/sign byte.
+ * @param[in] rs Range/sign byte.
*/
static void decode_rs_18(uint8_t rs, struct dev_context *devc)
{
@@ -266,14 +254,14 @@ static void decode_rs_18(uint8_t rs, struct dev_context *devc)
/* Sign */
if (((devc->scale > 0) && (rs & 0x08)) ||
((devc->scale < 0) && !(rs & 0x08)))
- devc->scale *= -1.0;
+ devc->scale = -devc->scale;
/* Range */
range = rs & 0x07;
switch (devc->mq) {
case SR_MQ_VOLTAGE:
if (devc->unit == SR_UNIT_DECIBEL_VOLT) {
- devc->scale *= pow(10.0, -2);
+ devc->scale += -2;
/*
* When entering relative mode, the device switches
* from 10 byte to 6 byte msg format. Unfortunately
@@ -283,27 +271,27 @@ static void decode_rs_18(uint8_t rs, struct dev_context *devc)
*/
}
else
- devc->scale *= pow(10.0, range - 5);
+ devc->scale += range - 5;
break;
case SR_MQ_CURRENT:
- if (devc->scale1000 == -1)
- devc->scale *= pow(10.0, range - 5);
- else
- devc->scale *= pow(10.0, range - 4);
+ devc->scale += (devc->scale1000 == -1) ? range - 5 : range - 4;
break;
case SR_MQ_RESISTANCE:
- devc->scale *= pow(10.0, range - 2);
+ devc->scale += range - 2;
break;
case SR_MQ_FREQUENCY:
- devc->scale *= pow(10.0, range - 2);
+ devc->scale += range - 2;
break;
case SR_MQ_TEMPERATURE:
- devc->scale *= pow(10.0, range - 2);
+ devc->scale += range - 2;
break;
case SR_MQ_CAPACITANCE:
- devc->scale *= pow(10.0, range - 13);
+ devc->scale += range - 13;
break;
/* TODO: 29S Mains measurements. */
+ default:
+ /* Avoid certain compiler warnings due to (-Wswitch). */
+ break;
}
}
@@ -390,7 +378,7 @@ static void decode_ctmv_2x(uint8_t ctmv, struct dev_context *devc)
case 0x09: /* 01001 F */
devc->mq = SR_MQ_CAPACITANCE;
devc->unit = SR_UNIT_FARAD;
- devc->scale *= 0.1;
+ devc->scale += -1;
break;
case 0x0a: /* 01010 V dB */
devc->mq = SR_MQ_VOLTAGE;
@@ -408,10 +396,10 @@ static void decode_ctmv_2x(uint8_t ctmv, struct dev_context *devc)
devc->mqflags |= SR_MQFLAG_DC;
break;
case 0x0d: /* 01101 W on power, mA range (29S only) */
- devc->scale *= 0.1;
+ devc->scale += -1;
/* Fall through! */
case 0x0e: /* 01110 W on power, A range (29S only) */
- devc->scale *= 0.1;
+ devc->scale += -1;
devc->scale1000 = -1;
devc->mq = SR_MQ_POWER;
devc->unit = SR_UNIT_WATT;
@@ -425,7 +413,7 @@ static void decode_ctmv_2x(uint8_t ctmv, struct dev_context *devc)
devc->mqflags |= SR_MQFLAG_DIODE;
} else {
devc->mq = SR_MQ_CONTINUITY;
- devc->scale *= 0.00001;
+ devc->scale += -5;
}
devc->unit = SR_UNIT_VOLT;
break;
@@ -517,39 +505,39 @@ static void decode_rs_2x(uint8_t rs, struct dev_context *devc)
/* Sign */
if (((devc->scale > 0) && (rs & 0x08)) ||
((devc->scale < 0) && !(rs & 0x08)))
- devc->scale *= -1.0;
+ devc->scale = -devc->scale;
/* Range */
range = rs & 0x07;
switch (devc->mq) {
case SR_MQ_VOLTAGE:
- if (devc->unit == SR_UNIT_DECIBEL_VOLT)
- devc->scale *= pow(10.0, -3);
- else
- devc->scale *= pow(10.0, range - 6);
+ devc->scale += (devc->unit == SR_UNIT_DECIBEL_VOLT) ? -3 : range - 6;
break;
case SR_MQ_CURRENT:
if (devc->scale1000 != -1) /* uA, mA */
range += 1;/* mA and A ranges differ by 10^4, not 10^3!*/
- devc->scale *= pow(10.0, range - 6);
+ devc->scale += range - 6;
break;
case SR_MQ_RESISTANCE:
- devc->scale *= pow(10.0, range - 3);
+ devc->scale += range - 3;
break;
case SR_MQ_FREQUENCY:
- devc->scale *= pow(10.0, range - 3);
+ devc->scale += range - 3;
break;
case SR_MQ_TEMPERATURE:
if (range == 4) /* Indicator for °F */
devc->unit = SR_UNIT_FAHRENHEIT;
- devc->scale *= pow(10.0, - 2);
+ devc->scale += -2;
break;
case SR_MQ_CAPACITANCE:
if (range == 7)
range -= 1; /* Same value as range 6 */
- devc->scale *= pow(10.0, range - 13);
+ devc->scale += range - 13;
break;
/* TODO: 29S Mains measurements. */
+ default:
+ /* Avoid certain compiler warnings due to (-Wswitch). */
+ break;
}
}
@@ -570,29 +558,29 @@ static void decode_rs_2x_TR2(uint8_t rs, struct dev_context *devc)
switch (range) {
case 0:
case 1: /* 100, 300 µA */
- devc->scale *= pow(10.0, -6);
+ devc->scale += -6;
break;
case 2:
case 3: /* 1, 3 mA */
- devc->scale *= pow(10.0, -5);
+ devc->scale += -5;
break;
case 4:
case 5: /* 10, 30 mA */
- devc->scale *= pow(10.0, -4);
+ devc->scale += -4;
break;
case 6:
case 7: /* 100, 300 mA */
- devc->scale *= pow(10.0, -3);
+ devc->scale += -3;
break;
}
else /* A */
switch (range) {
case 0:
case 1: /* 1, 3 A */
- devc->scale *= pow(10.0, -5);
+ devc->scale += -5;
break;
case 2: /* 10 A */
- devc->scale *= pow(10.0, -4);
+ devc->scale += -4;
break;
}
break;
@@ -604,7 +592,7 @@ static void decode_rs_2x_TR2(uint8_t rs, struct dev_context *devc)
/* Sign */
if (((devc->scale > 0) && (rs & 0x08)) ||
((devc->scale < 0) && !(rs & 0x08)))
- devc->scale *= -1.0;
+ devc->scale = -devc->scale;
}
/**
@@ -634,7 +622,7 @@ static void decode_spc_2x(uint8_t spc, struct dev_context *devc)
static void clean_rs_v(struct dev_context *devc)
{
devc->value = 0.0;
- devc->scale = 1.0;
+ devc->scale = 0;
}
/** Clean current type, measured variable, range and sign. */
@@ -652,24 +640,29 @@ static void send_value(struct sr_dev_inst *sdi)
{
struct dev_context *devc;
struct sr_datafeed_analog analog;
+ struct sr_analog_encoding encoding;
+ struct sr_analog_meaning meaning;
+ struct sr_analog_spec spec;
struct sr_datafeed_packet packet;
+ int digits;
devc = sdi->priv;
- memset(&analog, 0, sizeof(struct sr_datafeed_analog));
- analog.channels = sdi->channels;
+ digits = -(devc->scale + 3 * devc->scale1000);
+ sr_analog_init(&analog, &encoding, &meaning, &spec, digits);
+ analog.meaning->channels = sdi->channels;
analog.num_samples = 1;
- analog.mq = devc->mq;
- analog.unit = devc->unit;
- analog.mqflags = devc->mqflags;
+ analog.meaning->mq = devc->mq;
+ analog.meaning->unit = devc->unit;
+ analog.meaning->mqflags = devc->mqflags;
analog.data = &devc->value;
memset(&packet, 0, sizeof(struct sr_datafeed_packet));
packet.type = SR_DF_ANALOG;
packet.payload = &analog;
- sr_session_send(devc->cb_data, &packet);
+ sr_session_send(sdi, &packet);
- devc->num_samples++;
+ sr_sw_limits_update_samples_read(&devc->limits, 1);
}
/** Process 6-byte data message, Metrahit 1x/2x send mode. */
@@ -689,7 +682,7 @@ static void process_msg_dta_6(struct sr_dev_inst *sdi)
decode_rs_18(bc(devc->buf[0]), devc);
else {
decode_rs_2x(bc(devc->buf[0]), devc);
- devc->scale *= 10; /* Compensate for format having only 5 digits, decode_rs_2x() assumes 6. */
+ devc->scale += 1; /* Compensate for format having only 5 digits, decode_rs_2x() assumes 6. */
}
/* Bytes 1-5, digits (ls first). */
@@ -698,16 +691,16 @@ static void process_msg_dta_6(struct sr_dev_inst *sdi)
if (dgt >= 10) {
/* 10 Overload; on model <= 16X also 11 possible. */
devc->value = NAN;
- devc->scale = 1.0;
+ devc->scale = 0;
break;
}
devc->value += pow(10.0, cnt) * dgt;
}
- sr_spew("process_msg_dta_6() value=%f scale=%f scale1000=%d",
+ sr_spew("process_msg_dta_6() value=%f scale=%d scale1000=%d",
devc->value, devc->scale, devc->scale1000);
if (devc->value != NAN)
- devc->value *= devc->scale * pow(1000.0, devc->scale1000);
+ devc->value *= pow(10.0, devc->scale) * pow(1000.0, devc->scale1000);
/* Create and send packet. */
send_value(sdi);
@@ -765,16 +758,16 @@ static void process_msg_inf_10(struct sr_dev_inst *sdi)
}
else if (dgt >= 12) { /* Overload */
devc->value = NAN;
- devc->scale = 1.0;
+ devc->scale = 0;
break;
}
devc->value += pow(10.0, cnt) * dgt;
}
- sr_spew("process_msg_inf_10() value=%f scale=%f scalet=%d",
- devc->value, devc->scale, devc->scale1000);
+ sr_spew("process_msg_inf_10() value=%f scale=%d scalet=%d",
+ devc->value, devc->scale, devc->scale1000);
if (devc->value != NAN)
- devc->value *= devc->scale * pow(1000.0, devc->scale1000);
+ devc->value *= pow(10, devc->scale) * pow(1000.0, devc->scale1000);
/* Create and send packet. */
send_value(sdi);
@@ -850,16 +843,16 @@ static void process_msg_inf_13(struct sr_dev_inst *sdi)
dgt = bc(devc->buf[5 + cnt]);
if (dgt == 10) { /* Overload */
devc->value = NAN;
- devc->scale = 1.0;
+ devc->scale = 0;
break;
}
devc->value += pow(10.0, cnt) * dgt;
}
- sr_spew("process_msg_inf_13() value=%f scale=%f scale1000=%d mq=%d "
- "unit=%d mqflags=0x%02llx", devc->value, devc->scale,
- devc->scale1000, devc->mq, devc->unit, devc->mqflags);
+ sr_spew("process_msg_inf_13() value=%f scale=%d scale1000=%d mq=%d "
+ "unit=%d mqflags=0x%02" PRIx64, devc->value, devc->scale,
+ devc->scale1000, devc->mq, devc->unit, (uint64_t)devc->mqflags);
if (devc->value != NAN)
- devc->value *= devc->scale * pow(1000.0, devc->scale1000);
+ devc->value *= pow(10, devc->scale) * pow(1000.0, devc->scale1000);
/* Byte 12, Send Interval */
sr_spew("Send interval: %s", decode_send_interval(bc(devc->buf[12])));
@@ -868,11 +861,13 @@ static void process_msg_inf_13(struct sr_dev_inst *sdi)
send_value(sdi);
}
-/** Dump contents of 14-byte message.
- * @param buf Pointer to array of 14 data bytes.
- * @param[in] raw Write only data bytes, no interpretation.
+/**
+ * Dump contents of 14-byte message.
+ *
+ * @param buf Pointer to array of 14 data bytes.
+ * @param[in] raw Write only data bytes, no interpretation.
*/
-void dump_msg14(guchar* buf, gboolean raw)
+static void dump_msg14(guchar *buf, gboolean raw)
{
if (!buf)
return;
@@ -891,16 +886,17 @@ void dump_msg14(guchar* buf, gboolean raw)
buf[12], buf[13]);
}
-/** Calc checksum for 14 byte message type.
+/**
+ * Calc checksum for 14 byte message type.
*
- * @param[in] dta Pointer to array of 13 data bytes.
- * @return Checksum.
+ * @param[in] dta Pointer to array of 13 data bytes.
+ * @return Checksum.
*/
-static guchar calc_chksum_14(guchar* dta)
+static guchar calc_chksum_14(guchar *dta)
{
guchar cnt, chs;
- for (chs = 0, cnt = 0; cnt < 13; cnt++)
+ for (chs = 0, cnt = 0; cnt < (GMC_REPLY_SIZE - 1); cnt++)
chs += dta[cnt];
return (64 - chs) & MASK_6BITS;
@@ -912,7 +908,7 @@ static int chk_msg14(struct sr_dev_inst *sdi)
struct dev_context *devc;
int retc;
gboolean isreq; /* Message is request to multimeter (otherwise response) */
- uint8_t addr; /* Adaptor address */
+ uint8_t addr; /* Adaptor address */
retc = SR_OK;
@@ -937,7 +933,6 @@ static int chk_msg14(struct sr_dev_inst *sdi)
}
if (devc->buf[1] == 0) { /* Error msg from device! */
- retc = SR_ERR_ARG;
switch (devc->buf[2]) {
case 1: /* Not used */
sr_err("Device: Illegal error code!");
@@ -1002,7 +997,7 @@ SR_PRIV int process_msg14(struct sr_dev_inst *sdi)
sr_spew("Cmd %d unimplemented!", devc->buf[3]);
break;
case 3: /* Read firmware version and status */
- sr_spew("Cmd 3, Read firmware and status", devc->buf[3]);
+ sr_spew("Cmd 3, Read firmware and status");
switch (devc->cmd_idx) {
case 0:
devc->fw_ver_maj = devc->buf[5];
@@ -1013,7 +1008,7 @@ SR_PRIV int process_msg14(struct sr_dev_inst *sdi)
sr_spew("Measurement Function: %d ", (int)devc->buf[7]);
decode_ctmv_2x(devc->buf[7], devc);
sr_spew("Range: 0x%x", devc->buf[8]);
- decode_rs_2x_TR2(devc->buf[8] & 0x0f, devc); /* Docs wrong, uses conversion table TR_2! */
+ decode_rs_2x_TR2(devc->buf[8] & 0x0f, devc); /* Docs wrong, uses conversion table TR_2! */
devc->autorng = (devc->buf[8] & 0x20) == 0;
// TODO 9, 10: 29S special functions
devc->ubatt = 0.1 * (float)devc->buf[11];
@@ -1024,7 +1019,7 @@ SR_PRIV int process_msg14(struct sr_dev_inst *sdi)
sr_spew("Internal version %d.%d", (int)devc->buf[5], (int)devc->buf[4]);
sr_spew("Comm mode: 0x%x", (int)devc->buf[6]);
sr_spew("Block cnt%%64: %d", (int)devc->buf[7]);
- sr_spew("drpCi: %d drpCh: %d", (int)devc->buf[8], (int)devc->buf[9]);
+ sr_spew("drpCi: %d drpCh: %d", (int)devc->buf[8], (int)devc->buf[9]);
// Semantics undocumented. Possibly Metrahit 29S dropouts stuff?
break;
default:
@@ -1058,7 +1053,7 @@ SR_PRIV int process_msg14(struct sr_dev_inst *sdi)
dgt = bc(devc->buf[7 + cnt]);
if (dgt == 10) { /* Overload */
devc->value = NAN;
- devc->scale = 1.0;
+ devc->scale = 0;
break;
}
else if (dgt == 13) { /* FUSE */
@@ -1067,16 +1062,16 @@ SR_PRIV int process_msg14(struct sr_dev_inst *sdi)
else if (dgt == 14) { /* Function recognition mode, OPEN */
sr_info("Function recognition mode, OPEN!");
devc->value = NAN;
- devc->scale = 1.0;
+ devc->scale = 0;
break;
}
devc->value += pow(10.0, cnt) * dgt;
}
- sr_spew("process_msg14() value=%f scale=%f scale1000=%d mq=%d "
- "unit=%d mqflags=0x%02llx", devc->value, devc->scale,
- devc->scale1000, devc->mq, devc->unit, devc->mqflags);
+ sr_spew("process_msg14() value=%f scale=%d scale1000=%d mq=%d "
+ "unit=%d mqflags=0x%02" PRIx64, devc->value, devc->scale,
+ devc->scale1000, devc->mq, devc->unit, (uint64_t)devc->mqflags);
if (devc->value != NAN)
- devc->value *= devc->scale * pow(1000.0, devc->scale1000);
+ devc->value *= pow(10, devc->scale) * pow(1000.0, devc->scale1000);
send_value(sdi);
@@ -1097,7 +1092,6 @@ SR_PRIV int gmc_mh_1x_2x_receive_data(int fd, int revents, void *cb_data)
struct sr_serial_dev_inst *serial;
uint8_t buf, msgt;
int len;
- gdouble elapsed_s;
(void)fd;
@@ -1171,15 +1165,8 @@ SR_PRIV int gmc_mh_1x_2x_receive_data(int fd, int revents, void *cb_data)
}
}
- /* If number of samples or time limit reached, stop acquisition. */
- if (devc->limit_samples && (devc->num_samples >= devc->limit_samples))
- sdi->driver->dev_acquisition_stop(sdi, cb_data);
-
- if (devc->limit_msec) {
- elapsed_s = g_timer_elapsed(devc->elapsed_msec, NULL);
- if ((elapsed_s * 1000) >= devc->limit_msec)
- sdi->driver->dev_acquisition_stop(sdi, cb_data);
- }
+ if (sr_sw_limits_check(&devc->limits))
+ sr_dev_acquisition_stop(sdi);
return TRUE;
}
@@ -1191,7 +1178,6 @@ SR_PRIV int gmc_mh_2x_receive_data(int fd, int revents, void *cb_data)
struct sr_serial_dev_inst *serial;
uint8_t buf;
int len;
- gdouble elapsed_s;
(void)fd;
@@ -1222,21 +1208,14 @@ SR_PRIV int gmc_mh_2x_receive_data(int fd, int revents, void *cb_data)
}
}
- /* If number of samples or time limit reached, stop acquisition. */
- if (devc->limit_samples && (devc->num_samples >= devc->limit_samples))
- sdi->driver->dev_acquisition_stop(sdi, cb_data);
-
- if (devc->limit_msec) {
- elapsed_s = g_timer_elapsed(devc->elapsed_msec, NULL);
- if ((elapsed_s * 1000) >= devc->limit_msec)
- sdi->driver->dev_acquisition_stop(sdi, cb_data);
- }
+ if (sr_sw_limits_check(&devc->limits))
+ sr_dev_acquisition_stop(sdi);
/* Request next data set, if required */
if (sdi->status == SR_ST_ACTIVE) {
if (devc->response_pending) {
gint64 elapsed_us = g_get_monotonic_time() - devc->req_sent_at;
- if (elapsed_us > 1*1000*1000) /* Timeout! */
+ if (elapsed_us > (1 * 1000 * 1000)) /* Timeout! */
devc->response_pending = FALSE;
}
if (!devc->response_pending) {
@@ -1253,17 +1232,19 @@ SR_PRIV int gmc_mh_2x_receive_data(int fd, int revents, void *cb_data)
return TRUE;
}
-/** Create 14 (42) byte command for Metrahit 2x multimeter in bidir mode.
+/**
+ * Create 14 (42) byte command for Metrahit 2x multimeter in bidir mode.
+ *
+ * Actually creates 42 bytes due to the encoding method used.
*
- * Actually creates 42 bytes due to the encoding method used.
- * @param[in] addr Device address (0=adapter, 1..15 multimeter; for byte 0).
- * @param[in] func Function code (byte 3).
- * @param[in] params Further parameters (9 bytes)
- * @param[out] buf Buffer to create msg in (42 bytes).
+ * @param[in] addr Device address (0=adapter, 1..15 multimeter; for byte 0).
+ * @param[in] func Function code (byte 3).
+ * @param[in] params Further parameters (9 bytes)
+ * @param[out] buf Buffer to create msg in (42 bytes).
*/
-void create_cmd_14(guchar addr, guchar func, guchar* params, guchar* buf)
+static void create_cmd_14(guchar addr, guchar func, guchar *params, guchar *buf)
{
- uint8_t dta[14]; /* Unencoded message */
+ uint8_t dta[GMC_REPLY_SIZE]; /* Unencoded message */
int cnt;
if (!params || !buf)
@@ -1279,23 +1260,21 @@ void create_cmd_14(guchar addr, guchar func, guchar* params, guchar* buf)
/* 4-12: Copy further parameters */
for (cnt = 0; cnt < 9; cnt++)
- dta[cnt+4] = (params[cnt] & MASK_6BITS);
+ dta[cnt + 4] = (params[cnt] & MASK_6BITS);
/* 13: Checksum (b complement) */
dta[13] = calc_chksum_14(dta);
/* The whole message is packed into 3 bytes per byte now (lower 6 bits only) the most
* peculiar way I have ever seen. Possibly to improve IR communication? */
- for (cnt = 0; cnt < 14; cnt++) {
- buf[3*cnt] = (dta[cnt] & 0x01 ? 0x0f : 0) | (dta[cnt] & 0x02 ? 0xf0 : 0);
- buf[3*cnt + 1] = (dta[cnt] & 0x04 ? 0x0f : 0) | (dta[cnt] & 0x08 ? 0xf0 : 0);
- buf[3*cnt + 2] = (dta[cnt] & 0x10 ? 0x0f : 0) | (dta[cnt] & 0x20 ? 0xf0 : 0);
+ for (cnt = 0; cnt < GMC_REPLY_SIZE; cnt++) {
+ buf[(3 * cnt) + 0] = (dta[cnt] & 0x01 ? 0x0f : 0) | (dta[cnt] & 0x02 ? 0xf0 : 0);
+ buf[(3 * cnt) + 1] = (dta[cnt] & 0x04 ? 0x0f : 0) | (dta[cnt] & 0x08 ? 0xf0 : 0);
+ buf[(3 * cnt) + 2] = (dta[cnt] & 0x10 ? 0x0f : 0) | (dta[cnt] & 0x20 ? 0xf0 : 0);
}
}
-/** Request one measurement from 2x multimeter (msg 8).
- *
- */
+/** Request one measurement from 2x multimeter (msg 8). */
int req_meas14(const struct sr_dev_inst *sdi)
{
struct dev_context *devc;
@@ -1311,7 +1290,8 @@ int req_meas14(const struct sr_dev_inst *sdi)
devc->cmd_idx = 0;
create_cmd_14(devc->addr, 8, params, msg);
devc->req_sent_at = g_get_monotonic_time();
- if (serial_write_blocking(serial, msg, sizeof(msg), 0) < (int)sizeof(msg)) {
+ if (serial_write_blocking(serial, msg, sizeof(msg),
+ serial_timeout(serial, sizeof(msg))) < (int)sizeof(msg)) {
return SR_ERR;
}
@@ -1320,8 +1300,11 @@ int req_meas14(const struct sr_dev_inst *sdi)
return SR_OK;
}
-/** Request status from 2x multimeter (msg 3).
- * @param[in] power_on Try to power on powered off multimeter by sending additional messages.
+/**
+ * Request status from 2x multimeter (msg 3).
+ *
+ * @param[in] power_on Try to power on powered off multimeter by sending
+ * additional messages.
*/
int req_stat14(const struct sr_dev_inst *sdi, gboolean power_on)
{
@@ -1340,21 +1323,25 @@ int req_stat14(const struct sr_dev_inst *sdi, gboolean power_on)
if (power_on) {
sr_info("Write some data and wait 3s to turn on powered off device...");
- if (serial_write_blocking(serial, msg, sizeof(msg), 0) < 0)
+ if (serial_write_blocking(serial, msg, sizeof(msg),
+ serial_timeout(serial, sizeof(msg))) < 0)
return SR_ERR;
- g_usleep(1*1000*1000);
- if (serial_write_blocking(serial, msg, sizeof(msg), 0) < 0)
+ g_usleep(1 * 1000 * 1000);
+ if (serial_write_blocking(serial, msg, sizeof(msg),
+ serial_timeout(serial, sizeof(msg))) < 0)
return SR_ERR;
- g_usleep(1*1000*1000);
- if (serial_write_blocking(serial, msg, sizeof(msg), 0) < 0)
+ g_usleep(1 * 1000 * 1000);
+ if (serial_write_blocking(serial, msg, sizeof(msg),
+ serial_timeout(serial, sizeof(msg))) < 0)
return SR_ERR;
- g_usleep(1*1000*1000);
+ g_usleep(1 * 1000 * 1000);
serial_flush(serial);
}
/* Write message and wait for reply */
devc->req_sent_at = g_get_monotonic_time();
- if (serial_write_blocking(serial, msg, sizeof(msg), 0) < (int)sizeof(msg)) {
+ if (serial_write_blocking(serial, msg, sizeof(msg),
+ serial_timeout(serial, sizeof(msg))) < (int)sizeof(msg)) {
return SR_ERR;
}
@@ -1363,7 +1350,8 @@ int req_stat14(const struct sr_dev_inst *sdi, gboolean power_on)
return SR_OK;
}
-/** Decode model in "send mode".
+/**
+ * Decode model in "send mode".
*
* @param[in] mcode Model code.
* @return Model code.
@@ -1412,20 +1400,18 @@ SR_PRIV int gmc_decode_model_sm(uint8_t mcode)
}
}
-/** Convert GMC model code in bidirectional mode to sigrok-internal one.
+/**
+ * Convert GMC model code in bidirectional mode to sigrok-internal one.
*
- * @param[in] mcode Model code.
+ * @param[in] mcode Model code.
*
- * @return Model code.
+ * @return Model code.
*/
SR_PRIV int gmc_decode_model_bd(uint8_t mcode)
{
switch (mcode & 0x1f) {
case 2:
- if (mcode & 0x20)
- return METRAHIT_22M;
- else
- return METRAHIT_22S;
+ return (mcode & 0x20) ? METRAHIT_22M : METRAHIT_22S;
case 3:
return METRAHIT_23S;
case 4:
@@ -1433,10 +1419,7 @@ SR_PRIV int gmc_decode_model_bd(uint8_t mcode)
case 5:
return METRAHIT_25S;
case 1:
- if (mcode & 0x20)
- return METRAHIT_26M;
- else
- return METRAHIT_26S;
+ return (mcode & 0x20) ? METRAHIT_26M : METRAHIT_26S;
case 12:
return METRAHIT_28S;
case 14:
@@ -1447,11 +1430,12 @@ SR_PRIV int gmc_decode_model_bd(uint8_t mcode)
}
}
-/** Convert sigrok-internal model code to string.
+/**
+ * Convert sigrok-internal model code to string.
*
- * @param[in] mcode Model code.
+ * @param[in] mcode Model code.
*
- * @return Model code string.
+ * @return Model code string.
*/
SR_PRIV const char *gmc_model_str(enum model mcode)
{
@@ -1511,13 +1495,7 @@ SR_PRIV int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *s
(void)cg;
- if (sdi->status != SR_ST_ACTIVE)
- return SR_ERR_DEV_CLOSED;
-
- if (!(devc = sdi->priv)) {
- sr_err("sdi->priv was NULL.");
- return SR_ERR_BUG;
- }
+ devc = sdi->priv;
switch (key) {
case SR_CONF_POWER_OFF:
@@ -1531,17 +1509,15 @@ SR_PRIV int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *s
params[0] = 5;
params[1] = 5;
create_cmd_14(devc->addr, 6, params, msg);
- if (serial_write_blocking(sdi->conn, msg, sizeof(msg), 0) < 0)
+ if (serial_write_blocking(sdi->conn, msg, sizeof(msg),
+ serial_timeout(sdi->conn, sizeof(msg))) < 0)
return SR_ERR;
else
- g_usleep(2000000); /* Wait to ensure transfer before interface switched off. */
- break;
- case SR_CONF_LIMIT_MSEC:
- devc->limit_msec = g_variant_get_uint64(data);
+ g_usleep(2 * 1000 * 1000); /* Wait to ensure transfer before interface switched off. */
break;
case SR_CONF_LIMIT_SAMPLES:
- devc->limit_samples = g_variant_get_uint64(data);
- break;
+ case SR_CONF_LIMIT_MSEC:
+ return sr_sw_limits_config_set(&devc->limits, key, data);
default:
return SR_ERR_NA;
}