]> sigrok.org Git - libsigrok.git/blobdiff - src/hardware/hp-3457a/protocol.c
hp-3457a: Implement AC, ACDC, and four-wire resistance modes
[libsigrok.git] / src / hardware / hp-3457a / protocol.c
index 320d8d0abfe114c657ebbf627809365b8a80c9dc..bfc7902575bc92699a59f2ea7991968ed3b35435 100644 (file)
 #include <scpi.h>
 #include "protocol.h"
 
+static int set_mq_volt(struct sr_scpi_dev_inst *scpi, enum sr_mqflag flags);
+static int set_mq_amp(struct sr_scpi_dev_inst *scpi, enum sr_mqflag flags);
+static int set_mq_ohm(struct sr_scpi_dev_inst *scpi, enum sr_mqflag flags);
 /*
- * Currently, only DC voltage and current are supported, as switching to AC or
- * AC+DC requires mq flags, which is not yet implemented.
- * Four-wire resistance measurements are not implemented (See "OHMF" command).
  * The source for the frequency measurement can be either AC voltage, AC+DC
  * voltage, AC current, or AC+DC current. Configuring this is not yet
  * supported. For details, see "FSOURCE" command.
+ * The set_mode function is optional and can be set to NULL, but in that case
+ * a cmd string must be provided.
  */
 static const struct {
        enum sr_mq mq;
        enum sr_unit unit;
        const char *cmd;
+       int (*set_mode)(struct sr_scpi_dev_inst *scpi, enum sr_mqflag flags);
 } sr_mq_to_cmd_map[] = {
-       { SR_MQ_VOLTAGE, SR_UNIT_VOLT, "DCV" },
-       { SR_MQ_CURRENT, SR_UNIT_AMPERE, "DCI" },
-       { SR_MQ_RESISTANCE, SR_UNIT_OHM, "OHM" },
-       { SR_MQ_FREQUENCY, SR_UNIT_HERTZ, "FREQ" },
+       { SR_MQ_VOLTAGE, SR_UNIT_VOLT, "DCV", set_mq_volt },
+       { SR_MQ_CURRENT, SR_UNIT_AMPERE, "DCI", set_mq_amp },
+       { SR_MQ_RESISTANCE, SR_UNIT_OHM, "OHM", set_mq_ohm },
+       { SR_MQ_FREQUENCY, SR_UNIT_HERTZ, "FREQ", NULL },
 };
 
 static const struct rear_card_info rear_card_parameters[] = {
@@ -60,7 +63,46 @@ static const struct rear_card_info rear_card_parameters[] = {
        }
 };
 
-SR_PRIV int hp_3457a_set_mq(const struct sr_dev_inst *sdi, enum sr_mq mq)
+static int send_mq_ac_dc(struct sr_scpi_dev_inst *scpi, const char *mode,
+                          enum sr_mqflag flags)
+{
+       const char *ac_flag, *dc_flag;
+
+       if (flags & ~(SR_MQFLAG_AC | SR_MQFLAG_DC))
+               return SR_ERR_NA;
+
+       ac_flag = (flags & SR_MQFLAG_AC) ? "AC" : "";
+       dc_flag = "";
+       /* Must specify DC measurement when AC flag is not given. */
+       if ((flags & SR_MQFLAG_DC) || !(flags & SR_MQFLAG_AC))
+               dc_flag = "DC";
+
+       return sr_scpi_send(scpi, "%s%s%s", ac_flag, dc_flag, mode);
+}
+
+static int set_mq_volt(struct sr_scpi_dev_inst *scpi, enum sr_mqflag flags)
+{
+       return send_mq_ac_dc(scpi, "V", flags);
+}
+
+static int set_mq_amp(struct sr_scpi_dev_inst *scpi, enum sr_mqflag flags)
+{
+       return send_mq_ac_dc(scpi, "I", flags);
+}
+
+static int set_mq_ohm(struct sr_scpi_dev_inst *scpi, enum sr_mqflag flags)
+{
+       const char *ohm_flag;
+
+       if (flags & ~(SR_MQFLAG_FOUR_WIRE))
+               return SR_ERR_NA;
+
+       ohm_flag = (flags & SR_MQFLAG_FOUR_WIRE) ? "F" : "";
+       return sr_scpi_send(scpi, "OHM%s", ohm_flag);
+}
+
+SR_PRIV int hp_3457a_set_mq(const struct sr_dev_inst *sdi, enum sr_mq mq,
+                           enum sr_mqflag mq_flags)
 {
        int ret;
        size_t i;
@@ -70,9 +112,14 @@ SR_PRIV int hp_3457a_set_mq(const struct sr_dev_inst *sdi, enum sr_mq mq)
        for (i = 0; i < ARRAY_SIZE(sr_mq_to_cmd_map); i++) {
                if (sr_mq_to_cmd_map[i].mq != mq)
                        continue;
-               ret = sr_scpi_send(scpi, sr_mq_to_cmd_map[i].cmd);
+               if (sr_mq_to_cmd_map[i].set_mode) {
+                       ret = sr_mq_to_cmd_map[i].set_mode(scpi, mq_flags);
+               } else {
+                       ret = sr_scpi_send(scpi, sr_mq_to_cmd_map[i].cmd);
+               }
                if (ret == SR_OK) {
                        devc->measurement_mq = sr_mq_to_cmd_map[i].mq;
+                       devc->measurement_mq_flags = mq_flags;
                        devc->measurement_unit = sr_mq_to_cmd_map[i].unit;
                }
                return ret;
@@ -216,9 +263,17 @@ static int calculate_num_zero_digits(double measurement, double range)
        return zero_digits;
 }
 
+/*
+ * Until the output modules understand double precision data, we need to send
+ * the measurement as floats instead of doubles, hence, the dance with
+ * measurement_workaround double to float conversion.
+ * See bug #779 for details.
+ * The workaround should be removed once the output modules are fixed.
+ */
 static void acq_send_measurement(struct sr_dev_inst *sdi)
 {
        double hires_measurement;
+       float measurement_workaround;
        int zero_digits, num_digits;
        struct sr_datafeed_packet packet;
        struct sr_datafeed_analog analog;
@@ -241,14 +296,16 @@ static void acq_send_measurement(struct sr_dev_inst *sdi)
        packet.payload = &analog;
 
        sr_analog_init(&analog, &encoding, &meaning, &spec, num_digits);
-       encoding.unitsize = sizeof(double);
+       encoding.unitsize = sizeof(float);
 
        meaning.channels = sdi->channels;
 
+       measurement_workaround = hires_measurement;
        analog.num_samples = 1;
-       analog.data = &hires_measurement;
+       analog.data = &measurement_workaround;
 
        meaning.mq = devc->measurement_mq;
+       meaning.mqflags = devc->measurement_mq_flags;
        meaning.unit = devc->measurement_unit;
 
        sr_session_send(sdi, &packet);
@@ -306,8 +363,10 @@ SR_PRIV int hp_3457a_receive_data(int fd, int revents, void *cb_data)
                return FALSE;
        }
 
-       if (devc->acq_state == ACQ_GOT_MEASUREMENT)
+       if (devc->acq_state == ACQ_GOT_MEASUREMENT) {
                acq_send_measurement(sdi);
+               devc->num_samples++;
+       }
 
        if (devc->limit_samples && (devc->num_samples >= devc->limit_samples)) {
                sdi->driver->dev_acquisition_stop(sdi, cb_data);
@@ -317,7 +376,6 @@ SR_PRIV int hp_3457a_receive_data(int fd, int revents, void *cb_data)
        /* Got more to go. */
        if (devc->acq_state == ACQ_GOT_MEASUREMENT) {
                /* Retrigger */
-               devc->num_samples++;
                retrigger_measurement(scpi, devc);
        }