X-Git-Url: http://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Fhardware%2Fbaylibre-acme%2Fapi.c;h=a1b860e3621fb91a1655d97fda085a66fb339e08;hb=f9c6b5cfd3c0f6cb737f6a68f69034ce6819f36d;hp=b4ef69ddf3c1372f84fe809295e2decb4bccccff;hpb=5fabeeac6af940b52f67ac8c925952accc9b39b3;p=libsigrok.git
diff --git a/src/hardware/baylibre-acme/api.c b/src/hardware/baylibre-acme/api.c
index b4ef69dd..a1b860e3 100644
--- a/src/hardware/baylibre-acme/api.c
+++ b/src/hardware/baylibre-acme/api.c
@@ -17,22 +17,31 @@
* along with this program. If not, see .
*/
+#include
#include "protocol.h"
-
-SR_PRIV struct sr_dev_driver baylibre_acme_driver_info;
-static struct sr_dev_driver *di = &baylibre_acme_driver_info;
+#include
+#include
static const uint32_t devopts[] = {
- SR_CONF_CONTINUOUS | SR_CONF_SET,
+ SR_CONF_CONTINUOUS,
SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
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 */
@@ -42,25 +51,15 @@ static const uint64_t samplerates[] = {
SR_HZ(1),
};
-static int init(struct sr_context *sr_ctx)
+static GSList *scan(struct sr_dev_driver *di, GSList *options)
{
- return std_init(sr_ctx, di, LOG_PREFIX);
-}
-
-static GSList *scan(GSList *options)
-{
- struct drv_context *drvc;
struct dev_context *devc;
struct sr_dev_inst *sdi;
- GSList *devices;
gboolean status;
int i;
(void)options;
- drvc = di->priv;
- devices = NULL;
-
devc = g_malloc0(sizeof(struct dev_context));
devc->samplerate = SR_HZ(10);
@@ -68,7 +67,6 @@ static GSList *scan(GSList *options)
sdi->status = SR_ST_INACTIVE;
sdi->vendor = g_strdup("BayLibre");
sdi->model = g_strdup("ACME");
- sdi->driver = di;
sdi->priv = devc;
status = bl_acme_is_sane();
@@ -116,13 +114,10 @@ static GSList *scan(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);
- drvc->instances = g_slist_append(drvc->instances, sdi);
-
- return devices;
+ return std_scan_complete(di, g_slist_append(NULL, sdi));
err_out:
g_free(devc);
@@ -131,21 +126,10 @@ err_out:
return NULL;
}
-static GSList *dev_list(void)
-{
- return ((struct drv_context *)(di->priv))->instances;
-}
-
-static int dev_clear(void)
-{
- return std_dev_clear(di, NULL);
-}
-
static int dev_open(struct sr_dev_inst *sdi)
{
(void)sdi;
- /* Nothing to do here. */
sdi->status = SR_ST_ACTIVE;
return SR_OK;
@@ -155,19 +139,11 @@ static int dev_close(struct sr_dev_inst *sdi)
{
(void)sdi;
- /* Nothing to do here. */
sdi->status = SR_ST_INACTIVE;
return SR_OK;
}
-static int cleanup(void)
-{
- dev_clear();
-
- return SR_OK;
-}
-
static int config_get(uint32_t key, GVariant **data,
const struct sr_dev_inst *sdi,
const struct sr_channel_group *cg)
@@ -182,10 +158,8 @@ static int config_get(uint32_t key, GVariant **data,
ret = SR_OK;
switch (key) {
case SR_CONF_LIMIT_SAMPLES:
- *data = g_variant_new_uint64(devc->limit_samples);
- break;
case SR_CONF_LIMIT_MSEC:
- *data = g_variant_new_uint64(devc->limit_msec);
+ ret = sr_sw_limits_config_get(&devc->limits, key, data);
break;
case SR_CONF_SAMPLERATE:
*data = g_variant_new_uint64(devc->samplerate);
@@ -227,14 +201,8 @@ static int config_set(uint32_t key, GVariant *data,
ret = SR_OK;
switch (key) {
case SR_CONF_LIMIT_SAMPLES:
- devc->limit_samples = g_variant_get_uint64(data);
- devc->limit_msec = 0;
- sr_dbg("Setting sample limit to %" PRIu64, devc->limit_samples);
- break;
case SR_CONF_LIMIT_MSEC:
- devc->limit_msec = g_variant_get_uint64(data) * 1000;
- devc->limit_samples = 0;
- sr_dbg("Setting time limit to %" PRIu64"ms", devc->limit_msec);
+ ret = sr_sw_limits_config_set(&devc->limits, key, data);
break;
case SR_CONF_SAMPLERATE:
samplerate = g_variant_get_uint64(data);
@@ -244,7 +212,7 @@ static int config_set(uint32_t key, GVariant *data,
break;
}
devc->samplerate = samplerate;
- sr_dbg("Setting samplerate to %" PRIu64, devc->samplerate);
+ bl_acme_maybe_set_update_interval(sdi, samplerate);
break;
case SR_CONF_PROBE_FACTOR:
if (!cg)
@@ -267,9 +235,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;
@@ -295,8 +264,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;
@@ -306,71 +280,111 @@ static int config_list(uint32_t key, GVariant **data,
return ret;
}
-static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
+static void dev_acquisition_close(const struct sr_dev_inst *sdi)
{
- struct dev_context *devc;
+ 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;
+ }
+ }
- (void)cb_data;
+ return 0;
+}
+
+static int dev_acquisition_start(const struct sr_dev_inst *sdi)
+{
+ struct dev_context *devc;
+ struct itimerspec tspec = {
+ .it_interval = { 0, 0 },
+ .it_value = { 0, 0 }
+ };
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;
+ }
+
+ tspec.it_interval.tv_sec = 0;
+ tspec.it_interval.tv_nsec = SR_HZ_TO_NS(devc->samplerate);
+ tspec.it_value = tspec.it_interval;
- if (pipe(devc->pipe_fds)) {
- sr_err("Error setting up pipe");
+ 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();
+ std_session_send_df_header(sdi);
+ sr_sw_limits_acquisition_start(&devc->limits);
return SR_OK;
}
-static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
+static int dev_acquisition_stop(struct sr_dev_inst *sdi)
{
- struct sr_datafeed_packet packet;
struct dev_context *devc;
- (void)cb_data;
-
devc = sdi->priv;
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);
+
+ if (devc->samples_missed > 0)
+ sr_warn("%" PRIu64 " samples missed", devc->samples_missed);
return SR_OK;
}
-SR_PRIV struct sr_dev_driver baylibre_acme_driver_info = {
+static struct sr_dev_driver baylibre_acme_driver_info = {
.name = "baylibre-acme",
.longname = "BayLibre ACME (Another Cute Measurement Equipment)",
.api_version = 1,
- .init = init,
- .cleanup = cleanup,
+ .init = std_init,
+ .cleanup = std_cleanup,
.scan = scan,
- .dev_list = dev_list,
- .dev_clear = dev_clear,
+ .dev_list = std_dev_list,
.config_get = config_get,
.config_set = config_set,
.config_list = config_list,
@@ -378,5 +392,6 @@ 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,
};
+SR_REGISTER_DEV_DRIVER(baylibre_acme_driver_info);