X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Fhardware%2Fbaylibre-acme%2Fapi.c;h=caafc7037fd65408eae48c559e491554653ff8a7;hb=1c47e0da8f2571bc34dbdc368c3c1f55318c3aa0;hp=4bcf772dd3c110b9ad9d7dacf60616d128eb0a07;hpb=dcd438ee3523098201c7937e75e55775da3b506f;p=libsigrok.git diff --git a/src/hardware/baylibre-acme/api.c b/src/hardware/baylibre-acme/api.c index 4bcf772d..caafc703 100644 --- a/src/hardware/baylibre-acme/api.c +++ b/src/hardware/baylibre-acme/api.c @@ -17,7 +17,10 @@ * along with this program. If not, see . */ +#include #include "protocol.h" +#include +#include SR_PRIV struct sr_dev_driver baylibre_acme_driver_info; @@ -28,10 +31,19 @@ static const uint32_t devopts[] = { SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, }; -static const uint32_t devopts_cg[] = { - SR_CONF_PROBE_FACTOR | SR_CONF_GET | SR_CONF_SET, - SR_CONF_POWER_OFF | SR_CONF_GET | SR_CONF_SET, -}; +/* + * Currently there are two channel-group/probe options for ACME: + * - SR_CONF_PROBE_FACTOR - allows to modify current shunt resistance + * calibration + * - SR_CONF_POWER_OFF - allows to remotely cut-off/restore power to + * measured devices + * + * They are not static - we have to check each probe's capabilities in + * config_list(). + */ +#define MAX_DEVOPTS_CG 2 +#define HAS_PROBE_FACTOR (SR_CONF_PROBE_FACTOR | SR_CONF_GET | SR_CONF_SET) +#define HAS_POWER_OFF (SR_CONF_POWER_OFF | SR_CONF_GET | SR_CONF_SET) #define MAX_SAMPLE_RATE 500 /* In Hz */ @@ -57,7 +69,7 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) (void)options; - drvc = di->priv; + drvc = di->context; devices = NULL; devc = g_malloc0(sizeof(struct dev_context)); @@ -115,7 +127,7 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) * Let's assume there's no ACME device present if no probe * has been registered. */ - if (sdi->channel_groups == NULL) + if (!sdi->channel_groups) goto err_out; devices = g_slist_append(devices, sdi); @@ -132,7 +144,7 @@ err_out: static GSList *dev_list(const struct sr_dev_driver *di) { - return ((struct drv_context *)(di->priv))->instances; + return ((struct drv_context *)(di->context))->instances; } static int dev_clear(const struct sr_dev_driver *di) @@ -241,6 +253,7 @@ static int config_set(uint32_t key, GVariant *data, break; } devc->samplerate = samplerate; + bl_acme_maybe_set_update_interval(sdi, samplerate); break; case SR_CONF_PROBE_FACTOR: if (!cg) @@ -263,9 +276,10 @@ static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) { + uint32_t devopts_cg[MAX_DEVOPTS_CG]; GVariant *gvar; GVariantBuilder gvb; - int ret; + int ret, num_devopts_cg = 0; (void)sdi; (void)cg; @@ -291,8 +305,13 @@ static int config_list(uint32_t key, GVariant **data, } else { switch (key) { case SR_CONF_DEVICE_OPTIONS: + if (bl_acme_get_probe_type(cg) == PROBE_ENRG) + devopts_cg[num_devopts_cg++] = HAS_PROBE_FACTOR; + if (bl_acme_probe_has_pws(cg)) + devopts_cg[num_devopts_cg++] = HAS_POWER_OFF; + *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, - devopts_cg, ARRAY_SIZE(devopts_cg), sizeof(uint32_t)); + devopts_cg, num_devopts_cg, sizeof(uint32_t)); break; default: return SR_ERR_NA; @@ -302,32 +321,77 @@ static int config_list(uint32_t key, GVariant **data, return ret; } +static void dev_acquisition_close(const struct sr_dev_inst *sdi) +{ + GSList *chl; + struct sr_channel *ch; + + for (chl = sdi->channels; chl; chl = chl->next) { + ch = chl->data; + bl_acme_close_channel(ch); + } +} + +static int dev_acquisition_open(const struct sr_dev_inst *sdi) +{ + GSList *chl; + struct sr_channel *ch; + + for (chl = sdi->channels; chl; chl = chl->next) { + ch = chl->data; + if (bl_acme_open_channel(ch)) { + sr_err("Error opening channel %s", ch->name); + dev_acquisition_close(sdi); + return SR_ERR; + } + } + + return 0; +} + static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) { struct dev_context *devc; + struct itimerspec tspec = { + .it_interval = { 0, 0 }, + .it_value = { 0, 0 } + }; (void)cb_data; if (sdi->status != SR_ST_ACTIVE) return SR_ERR_DEV_CLOSED; + if (dev_acquisition_open(sdi)) + return SR_ERR; + devc = sdi->priv; devc->samples_read = 0; + devc->samples_missed = 0; + devc->timer_fd = timerfd_create(CLOCK_MONOTONIC, 0); + if (devc->timer_fd < 0) { + sr_err("Error creating timer fd"); + return SR_ERR; + } - if (pipe(devc->pipe_fds)) { - sr_err("Error setting up pipe"); + tspec.it_interval.tv_sec = 0; + tspec.it_interval.tv_nsec = SR_HZ_TO_NS(devc->samplerate); + tspec.it_value = tspec.it_interval; + + if (timerfd_settime(devc->timer_fd, 0, &tspec, NULL)) { + sr_err("Failed to set timer"); + close(devc->timer_fd); return SR_ERR; } - devc->channel = g_io_channel_unix_new(devc->pipe_fds[0]); + devc->channel = g_io_channel_unix_new(devc->timer_fd); g_io_channel_set_flags(devc->channel, G_IO_FLAG_NONBLOCK, NULL); g_io_channel_set_encoding(devc->channel, NULL, NULL); g_io_channel_set_buffered(devc->channel, FALSE); sr_session_source_add_channel(sdi->session, devc->channel, - G_IO_IN | G_IO_ERR, 1, bl_acme_receive_data, (void *)sdi); + G_IO_IN | G_IO_ERR, 1000, bl_acme_receive_data, (void *)sdi); - /* Send header packet to the session bus. */ std_session_send_df_header(sdi, LOG_PREFIX); devc->start_time = g_get_monotonic_time(); @@ -336,7 +400,6 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) { - struct sr_datafeed_packet packet; struct dev_context *devc; (void)cb_data; @@ -346,14 +409,16 @@ static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) if (sdi->status != SR_ST_ACTIVE) return SR_ERR_DEV_CLOSED; + dev_acquisition_close(sdi); sr_session_source_remove_channel(sdi->session, devc->channel); g_io_channel_shutdown(devc->channel, FALSE, NULL); g_io_channel_unref(devc->channel); devc->channel = NULL; - /* Send last packet. */ - packet.type = SR_DF_END; - sr_session_send(sdi, &packet); + std_session_send_df_end(sdi, LOG_PREFIX); + + if (devc->samples_missed > 0) + sr_warn("%" PRIu64 " samples missed", devc->samples_missed); return SR_OK; } @@ -374,5 +439,5 @@ SR_PRIV struct sr_dev_driver baylibre_acme_driver_info = { .dev_close = dev_close, .dev_acquisition_start = dev_acquisition_start, .dev_acquisition_stop = dev_acquisition_stop, - .priv = NULL, + .context = NULL, };