X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Flcr%2Fes51919.c;h=06d05d0ff22e2fec3357c16904fedf6bedf0d05c;hb=b94dd07b0823ae1607ce8159681a31833a01e199;hp=b8773493ac6c49d7f16cc551782aaec601e6af9c;hpb=a42a39ac378534782d44f166b879e84cefa66ed6;p=libsigrok.git diff --git a/src/lcr/es51919.c b/src/lcr/es51919.c index b8773493..06d05d0f 100644 --- a/src/lcr/es51919.c +++ b/src/lcr/es51919.c @@ -41,11 +41,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 +100,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; } @@ -308,7 +305,6 @@ static int send_config_update_key(struct sr_dev_inst *sdi, uint32_t key, sr_config_free(cfg); return ret; - } /* @@ -406,7 +402,7 @@ static int send_config_update_key(struct sr_dev_inst *sdi, uint32_t key, #define PACKET_SIZE 17 -static const uint64_t frequencies[] = { +static const double frequencies[] = { 100, 120, 1000, 10000, 100000, 0, }; @@ -435,8 +431,8 @@ 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; @@ -470,7 +466,7 @@ static int parse_mq(const uint8_t *pkt, int is_secondary, int is_parallel) 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 ? @@ -610,7 +606,6 @@ static unsigned int parse_model(const uint8_t *pkt) return MODEL_PAR; else return MODEL_SER; - } static gboolean packet_valid(const uint8_t *pkt) @@ -641,7 +636,7 @@ static int do_config_update(struct sr_dev_inst *sdi, uint32_t key, 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_uint64(frequencies[freq])); + g_variant_new_double(frequencies[freq])); } static int send_quant1_update(struct sr_dev_inst *sdi, unsigned int quant) @@ -669,7 +664,7 @@ static void handle_packet(struct sr_dev_inst *sdi, const uint8_t *pkt) struct dev_context *devc; unsigned int val; float floatval; - int count; + gboolean frame; devc = sdi->priv; @@ -705,34 +700,50 @@ static void handle_packet(struct sr_dev_inst *sdi, const uint8_t *pkt) return; } - count = 0; + 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(devc->cb_data, &packet); + frame = TRUE; + } + + packet.type = SR_DF_ANALOG; + packet.payload = &analog; + + sr_session_send(devc->cb_data, &packet); } 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(devc->cb_data, &packet); + frame = TRUE; + } + + packet.type = SR_DF_ANALOG; + packet.payload = &analog; + + sr_session_send(devc->cb_data, &packet); } - if (count > 0) - dev_sample_counter_inc(&devc->sample_count); + if (frame) { + packet.type = SR_DF_FRAME_END; + sr_session_send(devc->cb_data, &packet); + dev_limit_counter_inc(&devc->frame_count); + } } static int handle_new_data(struct sr_dev_inst *sdi) @@ -772,25 +783,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); 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) @@ -800,11 +799,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; } @@ -843,20 +839,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; - + 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) @@ -887,7 +877,7 @@ 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_MEASURED_QUANTITY: *data = g_variant_new_string(quantities1[devc->quant1]); @@ -924,10 +914,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); @@ -945,7 +935,7 @@ 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_MEASURED_QUANTITY | SR_CONF_GET | SR_CONF_LIST, @@ -970,8 +960,8 @@ 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_MEASURED_QUANTITY: *data = g_variant_new_strv(list_quantities1, @@ -1006,7 +996,7 @@ SR_PRIV int es51919_serial_acquisition_start(const struct sr_dev_inst *sdi, 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. */