]> sigrok.org Git - libsigrok.git/blobdiff - src/lcr/es51919.c
std_serial_dev_acquisition_stop(): Drop unneeded parameter.
[libsigrok.git] / src / lcr / es51919.c
index a99db214f91a5e014ac84a58718f924b633b7883..f020de8398d850839d66a00af895c092a3813943 100644 (file)
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <config.h>
 #include <stdint.h>
 #include <string.h>
 #include <math.h>
 #include <glib.h>
-#include "libsigrok.h"
+#include <libsigrok/libsigrok.h>
 #include "libsigrok-internal.h"
 
 #define LOG_PREFIX "es51919"
@@ -41,11 +42,7 @@ static struct dev_buffer *dev_buffer_new(size_t size)
 {
        struct dev_buffer *dbuf;
 
-       if (!(dbuf = g_try_malloc(sizeof(struct dev_buffer) + size))) {
-               sr_err("Dev buffer malloc failed (size=%zu).", size);
-               return NULL;
-       }
-
+       dbuf = g_malloc0(sizeof(struct dev_buffer) + size);
        dbuf->size = size;
        dbuf->len = 0;
        dbuf->offset = 0;
@@ -104,32 +101,33 @@ static uint8_t *dev_buffer_packet_find(struct dev_buffer *dbuf,
        return NULL;
 }
 
-struct dev_sample_counter {
-       /** The current number of already received samples. */
+struct dev_limit_counter {
+       /** The current number of received samples/frames/etc. */
        uint64_t count;
-       /** The current sampling limit (in number of samples). */
+       /** The limit (in number of samples/frames/etc.). */
        uint64_t limit;
 };
 
-static void dev_sample_counter_start(struct dev_sample_counter *cnt)
+static void dev_limit_counter_start(struct dev_limit_counter *cnt)
 {
        cnt->count = 0;
 }
 
-static void dev_sample_counter_inc(struct dev_sample_counter *cnt)
+static void dev_limit_counter_inc(struct dev_limit_counter *cnt)
 {
        cnt->count++;
 }
 
-static void dev_sample_limit_set(struct dev_sample_counter *cnt, uint64_t limit)
+static void dev_limit_counter_limit_set(struct dev_limit_counter *cnt,
+                                       uint64_t limit)
 {
        cnt->limit = limit;
 }
 
-static gboolean dev_sample_limit_reached(struct dev_sample_counter *cnt)
+static gboolean dev_limit_counter_limit_reached(struct dev_limit_counter *cnt)
 {
        if (cnt->limit && cnt->count >= cnt->limit) {
-               sr_info("Requested sample limit reached.");
+               sr_info("Requested counter limit reached.");
                return TRUE;
        }
 
@@ -294,6 +292,22 @@ static int send_config_update(struct sr_dev_inst *sdi, struct sr_config *cfg)
        return sr_session_send(sdi, &packet);
 }
 
+static int send_config_update_key(struct sr_dev_inst *sdi, uint32_t key,
+                                 GVariant *var)
+{
+       struct sr_config *cfg;
+       int ret;
+
+       cfg = sr_config_new(key, var);
+       if (!cfg)
+               return SR_ERR;
+
+       ret = send_config_update(sdi, cfg);
+       sr_config_free(cfg);
+
+       return ret;
+}
+
 /*
  * Cyrustek ES51919 LCR chipset host protocol.
  *
@@ -366,7 +380,7 @@ static int send_config_update(struct sr_dev_inst *sdi, struct sr_config *cfg)
  *                       0 = normal (measurement shown)
  *                       1 = blank (nothing shown)
  *                       2 = lines ("----")
- *                       3 = ouside limits ("OL")
+ *                       3 = outside limits ("OL")
  *                       7 = pass ("PASS")
  *                       8 = fail ("FAIL")
  *                       9 = open ("OPEn")
@@ -389,17 +403,20 @@ static int send_config_update(struct sr_dev_inst *sdi, struct sr_config *cfg)
 
 #define PACKET_SIZE 17
 
-static const uint64_t frequencies[] = {
+static const double frequencies[] = {
        100, 120, 1000, 10000, 100000, 0,
 };
 
+enum { MODEL_NONE, MODEL_PAR, MODEL_SER, MODEL_AUTO, };
+
+static const char *const models[] = {
+       "NONE", "PARALLEL", "SERIES", "AUTO",
+};
+
 /** Private, per-device-instance driver context. */
 struct dev_context {
-       /** Opaque pointer passed in by the frontend. */
-       void *cb_data;
-
-       /** The number of samples. */
-       struct dev_sample_counter sample_count;
+       /** The number of frames. */
+       struct dev_limit_counter frame_count;
 
        /** The time limit counter. */
        struct dev_time_counter time_count;
@@ -409,13 +426,25 @@ struct dev_context {
 
        /** The frequency of the test signal (index to frequencies[]). */
        unsigned int freq;
+
+       /** Equivalent circuit model (index to models[]). */
+       unsigned int model;
 };
 
-static int parse_mq(const uint8_t *buf, int is_secondary, int is_parallel)
+static const uint8_t *pkt_to_buf(const uint8_t *pkt, int is_secondary)
 {
+       return is_secondary ? pkt + 10 : pkt + 5;
+}
+
+static int parse_mq(const uint8_t *pkt, int is_secondary, int is_parallel)
+{
+       const uint8_t *buf;
+
+       buf = pkt_to_buf(pkt, is_secondary);
+
        switch (is_secondary << 8 | buf[0]) {
        case 0x001:
-               return is_parallel ?
+               return is_parallel ?
                        SR_MQ_PARALLEL_INDUCTANCE : SR_MQ_SERIES_INDUCTANCE;
        case 0x002:
                return is_parallel ?
@@ -453,7 +482,7 @@ static float parse_value(const uint8_t *buf)
 }
 
 static void parse_measurement(const uint8_t *pkt, float *floatval,
-                             struct sr_datafeed_analog *analog,
+                             struct sr_datafeed_analog_old *analog,
                              int is_secondary)
 {
        static const struct {
@@ -479,7 +508,7 @@ static void parse_measurement(const uint8_t *pkt, float *floatval,
        const uint8_t *buf;
        int state;
 
-       buf = pkt + (is_secondary ? 10 : 5);
+       buf = pkt_to_buf(pkt, is_secondary);
 
        analog->mq = -1;
        analog->mqflags = 0;
@@ -499,16 +528,12 @@ static void parse_measurement(const uint8_t *pkt, float *floatval,
                        analog->mqflags |= SR_MQFLAG_HOLD;
                if (pkt[2] & 0x02)
                        analog->mqflags |= SR_MQFLAG_REFERENCE;
-               if (pkt[2] & 0x20)
-                       analog->mqflags |= SR_MQFLAG_AUTOMQ;
-               if (pkt[2] & 0x40)
-                       analog->mqflags |= SR_MQFLAG_AUTOMODEL;
        } else {
                if (pkt[2] & 0x04)
                        analog->mqflags |= SR_MQFLAG_RELATIVE;
        }
 
-       if ((analog->mq = parse_mq(buf, is_secondary, pkt[2] & 0x80)) < 0)
+       if ((analog->mq = parse_mq(pkt, is_secondary, pkt[2] & 0x80)) < 0)
                return;
 
        if ((buf[3] >> 3) >= ARRAY_SIZE(units)) {
@@ -523,7 +548,7 @@ static void parse_measurement(const uint8_t *pkt, float *floatval,
        *floatval *= (state == 0) ? units[buf[3] >> 3].mult : INFINITY;
 }
 
-static unsigned int parse_frequency(const uint8_t *pkt)
+static unsigned int parse_freq(const uint8_t *pkt)
 {
        unsigned int freq;
 
@@ -537,6 +562,18 @@ static unsigned int parse_frequency(const uint8_t *pkt)
        return freq;
 }
 
+static unsigned int parse_model(const uint8_t *pkt)
+{
+       if (pkt[2] & 0x40)
+               return MODEL_AUTO;
+       else if (parse_mq(pkt, 0, 0) == SR_MQ_RESISTANCE)
+               return MODEL_NONE;
+       else if (pkt[2] & 0x80)
+               return MODEL_PAR;
+       else
+               return MODEL_SER;
+}
+
 static gboolean packet_valid(const uint8_t *pkt)
 {
        /*
@@ -552,73 +589,98 @@ static gboolean packet_valid(const uint8_t *pkt)
        return FALSE;
 }
 
-static int send_frequency_update(struct sr_dev_inst *sdi, unsigned int freq)
+static int do_config_update(struct sr_dev_inst *sdi, uint32_t key,
+                           GVariant *var)
 {
-       struct sr_config *cfg;
-       struct dev_context *devc;
-       int ret;
-
-       devc = sdi->priv;
-
-       cfg = sr_config_new(SR_CONF_OUTPUT_FREQUENCY,
-                           g_variant_new_uint64(frequencies[freq]));
-
-       if (!cfg)
-               return SR_ERR;
+       return send_config_update_key(sdi, key, var);
+}
 
-       ret = send_config_update(devc->cb_data, cfg);
-       sr_config_free(cfg);
+static int send_freq_update(struct sr_dev_inst *sdi, unsigned int freq)
+{
+       return do_config_update(sdi, SR_CONF_OUTPUT_FREQUENCY,
+                               g_variant_new_double(frequencies[freq]));
+}
 
-       return ret;
+static int send_model_update(struct sr_dev_inst *sdi, unsigned int model)
+{
+       return do_config_update(sdi, SR_CONF_EQUIV_CIRCUIT_MODEL,
+                               g_variant_new_string(models[model]));
 }
 
 static void handle_packet(struct sr_dev_inst *sdi, const uint8_t *pkt)
 {
        struct sr_datafeed_packet packet;
-       struct sr_datafeed_analog analog;
+       struct sr_datafeed_analog_old analog;
        struct dev_context *devc;
-       unsigned int freq;
+       unsigned int val;
        float floatval;
-       int count;
+       gboolean frame;
 
        devc = sdi->priv;
 
-       freq = parse_frequency(pkt);
-       if (freq != devc->freq) {
-               if (send_frequency_update(sdi, freq) == SR_OK)
-                       devc->freq = freq;
+       val = parse_freq(pkt);
+       if (val != devc->freq) {
+               if (send_freq_update(sdi, val) == SR_OK)
+                       devc->freq = val;
                else
                        return;
        }
 
-       count = 0;
+       val = parse_model(pkt);
+       if (val != devc->model) {
+               if (send_model_update(sdi, val) == SR_OK)
+                       devc->model = val;
+               else
+                       return;
+       }
+
+       frame = FALSE;
 
        memset(&analog, 0, sizeof(analog));
 
        analog.num_samples = 1;
        analog.data = &floatval;
 
-       packet.type = SR_DF_ANALOG;
-       packet.payload = &analog;
-
        analog.channels = g_slist_append(NULL, sdi->channels->data);
 
        parse_measurement(pkt, &floatval, &analog, 0);
        if (analog.mq >= 0) {
-               if (sr_session_send(devc->cb_data, &packet) == SR_OK)
-                       count++;
+               if (!frame) {
+                       packet.type = SR_DF_FRAME_BEGIN;
+                       sr_session_send(sdi, &packet);
+                       frame = TRUE;
+               }
+
+               packet.type = SR_DF_ANALOG_OLD;
+               packet.payload = &analog;
+
+               sr_session_send(sdi, &packet);
        }
 
+       g_slist_free(analog.channels);
        analog.channels = g_slist_append(NULL, sdi->channels->next->data);
 
        parse_measurement(pkt, &floatval, &analog, 1);
        if (analog.mq >= 0) {
-               if (sr_session_send(devc->cb_data, &packet) == SR_OK)
-                       count++;
+               if (!frame) {
+                       packet.type = SR_DF_FRAME_BEGIN;
+                       sr_session_send(sdi, &packet);
+                       frame = TRUE;
+               }
+
+               packet.type = SR_DF_ANALOG_OLD;
+               packet.payload = &analog;
+
+               sr_session_send(sdi, &packet);
        }
 
-       if (count > 0)
-               dev_sample_counter_inc(&devc->sample_count);
+       g_slist_free(analog.channels);
+
+       if (frame) {
+               packet.type = SR_DF_FRAME_END;
+               sr_session_send(sdi, &packet);
+               dev_limit_counter_inc(&devc->frame_count);
+       }
 }
 
 static int handle_new_data(struct sr_dev_inst *sdi)
@@ -658,25 +720,13 @@ static int receive_data(int fd, int revents, void *cb_data)
                handle_new_data(sdi);
        }
 
-       if (dev_sample_limit_reached(&devc->sample_count) ||
+       if (dev_limit_counter_limit_reached(&devc->frame_count) ||
            dev_time_limit_reached(&devc->time_count))
-               sdi->driver->dev_acquisition_stop(sdi, cb_data);
+               sdi->driver->dev_acquisition_stop(sdi);
 
        return TRUE;
 }
 
-static int add_channel(struct sr_dev_inst *sdi, const char *name)
-{
-       struct sr_channel *ch;
-
-       if (!(ch = sr_channel_new(0, SR_CHANNEL_ANALOG, TRUE, name)))
-               return SR_ERR;
-
-       sdi->channels = g_slist_append(sdi->channels, ch);
-
-       return SR_OK;
-}
-
 static const char *const channel_names[] = { "P1", "P2" };
 
 static int setup_channels(struct sr_dev_inst *sdi)
@@ -686,11 +736,8 @@ static int setup_channels(struct sr_dev_inst *sdi)
 
        ret = SR_ERR_BUG;
 
-       for (i = 0; i < ARRAY_SIZE(channel_names); i++) {
-               ret = add_channel(sdi, channel_names[i]);
-               if (ret != SR_OK)
-                       break;
-       }
+       for (i = 0; i < ARRAY_SIZE(channel_names); i++)
+               sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE, channel_names[i]);
 
        return ret;
 }
@@ -729,22 +776,14 @@ SR_PRIV struct sr_dev_inst *es51919_serial_scan(GSList *options,
 
        sr_info("Found device on port %s.", serial->port);
 
-       if (!(sdi = sr_dev_inst_new(SR_ST_INACTIVE, vendor, model, NULL)))
-               goto scan_cleanup;
-
-       if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
-               sr_err("Device context malloc failed.");
-               goto scan_cleanup;
-       }
-
-       if (!(devc->buf = dev_buffer_new(PACKET_SIZE * 8)))
-               goto scan_cleanup;
-
-       devc->freq = -1;
-
+       sdi = g_malloc0(sizeof(struct sr_dev_inst));
+       sdi->status = SR_ST_INACTIVE;
+       sdi->vendor = g_strdup(vendor);
+       sdi->model = g_strdup(model);
+       devc = g_malloc0(sizeof(struct dev_context));
+       devc->buf = dev_buffer_new(PACKET_SIZE * 8);
        sdi->inst_type = SR_INST_SERIAL;
        sdi->conn = serial;
-
        sdi->priv = devc;
 
        if (setup_channels(sdi) != SR_OK)
@@ -775,7 +814,10 @@ SR_PRIV int es51919_serial_config_get(uint32_t key, GVariant **data,
 
        switch (key) {
        case SR_CONF_OUTPUT_FREQUENCY:
-               *data = g_variant_new_uint64(frequencies[devc->freq]);
+               *data = g_variant_new_double(frequencies[devc->freq]);
+               break;
+       case SR_CONF_EQUIV_CIRCUIT_MODEL:
+               *data = g_variant_new_string(models[devc->model]);
                break;
        default:
                sr_spew("%s: Unsupported key %u", __func__, key);
@@ -803,10 +845,10 @@ SR_PRIV int es51919_serial_config_set(uint32_t key, GVariant *data,
                dev_time_limit_set(&devc->time_count, val);
                sr_dbg("Setting time limit to %" PRIu64 ".", val);
                break;
-       case SR_CONF_LIMIT_SAMPLES:
+       case SR_CONF_LIMIT_FRAMES:
                val = g_variant_get_uint64(data);
-               dev_sample_limit_set(&devc->sample_count, val);
-               sr_dbg("Setting sample limit to %" PRIu64 ".", val);
+               dev_limit_counter_limit_set(&devc->frame_count, val);
+               sr_dbg("Setting frame limit to %" PRIu64 ".", val);
                break;
        default:
                sr_spew("%s: Unsupported key %u", __func__, key);
@@ -824,9 +866,10 @@ static const uint32_t scanopts[] = {
 static const uint32_t devopts[] = {
        SR_CONF_LCRMETER,
        SR_CONF_CONTINUOUS,
-       SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
+       SR_CONF_LIMIT_FRAMES | SR_CONF_SET,
        SR_CONF_LIMIT_MSEC | SR_CONF_SET,
        SR_CONF_OUTPUT_FREQUENCY | SR_CONF_GET | SR_CONF_LIST,
+       SR_CONF_EQUIV_CIRCUIT_MODEL | SR_CONF_GET | SR_CONF_LIST,
 };
 
 static const struct std_opt_desc opts = {
@@ -846,8 +889,11 @@ SR_PRIV int es51919_serial_config_list(uint32_t key, GVariant **data,
 
        switch (key) {
        case SR_CONF_OUTPUT_FREQUENCY:
-               *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT64,
-                       frequencies, ARRAY_SIZE(frequencies), sizeof(uint64_t));
+               *data = g_variant_new_fixed_array(G_VARIANT_TYPE_DOUBLE,
+                       frequencies, ARRAY_SIZE(frequencies), sizeof(double));
+               break;
+       case SR_CONF_EQUIV_CIRCUIT_MODEL:
+               *data = g_variant_new_strv(models, ARRAY_SIZE(models));
                break;
        default:
                sr_spew("%s: Unsupported key %u", __func__, key);
@@ -857,8 +903,7 @@ SR_PRIV int es51919_serial_config_list(uint32_t key, GVariant **data,
        return SR_OK;
 }
 
-SR_PRIV int es51919_serial_acquisition_start(const struct sr_dev_inst *sdi,
-                                            void *cb_data)
+SR_PRIV int es51919_serial_acquisition_start(const struct sr_dev_inst *sdi)
 {
        struct dev_context *devc;
        struct sr_serial_dev_inst *serial;
@@ -869,13 +914,10 @@ SR_PRIV int es51919_serial_acquisition_start(const struct sr_dev_inst *sdi,
        if (!(devc = sdi->priv))
                return SR_ERR_BUG;
 
-       devc->cb_data = cb_data;
-
-       dev_sample_counter_start(&devc->sample_count);
+       dev_limit_counter_start(&devc->frame_count);
        dev_time_counter_start(&devc->time_count);
 
-       /* Send header packet to the session bus. */
-       std_session_send_df_header(cb_data, LOG_PREFIX);
+       std_session_send_df_header(sdi, LOG_PREFIX);
 
        /* Poll every 50ms, or whenever some data comes in. */
        serial = sdi->conn;
@@ -885,9 +927,8 @@ SR_PRIV int es51919_serial_acquisition_start(const struct sr_dev_inst *sdi,
        return SR_OK;
 }
 
-SR_PRIV int es51919_serial_acquisition_stop(struct sr_dev_inst *sdi,
-                                           void *cb_data)
+SR_PRIV int es51919_serial_acquisition_stop(struct sr_dev_inst *sdi)
 {
-       return std_serial_dev_acquisition_stop(sdi, cb_data,
+       return std_serial_dev_acquisition_stop(sdi,
                        std_serial_dev_close, sdi->conn, LOG_PREFIX);
 }