2 * This file is part of the libsigrok project.
4 * Copyright (C) 2015 Bartosz Golaszewski <bgolaszewski@baylibre.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 SR_PRIV struct sr_dev_driver baylibre_acme_driver_info;
23 static struct sr_dev_driver *di = &baylibre_acme_driver_info;
25 static const uint32_t devopts[] = {
26 SR_CONF_CONTINUOUS | SR_CONF_SET,
27 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
28 SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
29 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
32 #define MAX_SAMPLE_RATE 500 /* In Hz */
34 static const uint64_t samplerates[] = {
36 SR_HZ(MAX_SAMPLE_RATE),
40 static int init(struct sr_context *sr_ctx)
42 return std_init(sr_ctx, di, LOG_PREFIX);
45 static GSList *scan(GSList *options)
47 struct drv_context *drvc;
48 struct dev_context *devc;
49 struct sr_dev_inst *sdi;
59 devc = g_malloc0(sizeof(struct dev_context));
60 devc->samplerate = SR_HZ(10);
62 sdi = g_malloc0(sizeof(struct sr_dev_inst));
63 sdi->status = SR_ST_INACTIVE;
64 sdi->model = g_strdup("Acme cape");
68 status = bl_acme_is_sane();
73 * Iterate over all ACME connectors and check if any probes
76 for (i = 0; i < MAX_PROBES; i++) {
78 * First check if there's an energy probe on this connector. If
79 * not, and we're already at the fifth probe - see if we can
80 * detect a temperature probe.
82 status = bl_acme_detect_probe(bl_acme_get_enrg_addr(i),
86 /* Energy probe detected. */
87 status = bl_acme_register_probe(sdi,
89 bl_acme_get_enrg_addr(i),
92 sr_err("Error registering power probe %d",
97 } else if (i >= TEMP_PRB_START_INDEX) {
98 status = bl_acme_detect_probe(bl_acme_get_temp_addr(i),
102 /* Temperature probe detected. */
103 status = bl_acme_register_probe(sdi,PROBE_TEMP,
104 bl_acme_get_temp_addr(i),
107 sr_err("Error registering temp "
108 "probe %d", PROBE_NUM(i));
116 devices = g_slist_append(devices, sdi);
117 drvc->instances = g_slist_append(drvc->instances, sdi);
123 sr_dev_inst_free(sdi);
128 static GSList *dev_list(void)
130 return ((struct drv_context *)(di->priv))->instances;
133 static int dev_clear(void)
135 return std_dev_clear(di, NULL);
138 static int dev_open(struct sr_dev_inst *sdi)
142 /* Nothing to do here. */
143 sdi->status = SR_ST_ACTIVE;
148 static int dev_close(struct sr_dev_inst *sdi)
152 /* Nothing to do here. */
153 sdi->status = SR_ST_INACTIVE;
158 static int cleanup(void)
165 static int config_get(uint32_t key, GVariant **data,
166 const struct sr_dev_inst *sdi,
167 const struct sr_channel_group *cg)
169 struct dev_context *devc;
178 case SR_CONF_LIMIT_SAMPLES:
179 *data = g_variant_new_uint64(devc->limit_samples);
181 case SR_CONF_LIMIT_MSEC:
182 *data = g_variant_new_uint64(devc->limit_msec);
184 case SR_CONF_SAMPLERATE:
185 *data = g_variant_new_uint64(devc->samplerate);
194 static int config_set(uint32_t key, GVariant *data,
195 const struct sr_dev_inst *sdi,
196 const struct sr_channel_group *cg)
198 struct dev_context *devc;
204 if (sdi->status != SR_ST_ACTIVE)
205 return SR_ERR_DEV_CLOSED;
211 case SR_CONF_LIMIT_SAMPLES:
212 devc->limit_samples = g_variant_get_uint64(data);
213 devc->limit_msec = 0;
214 sr_dbg("Setting sample limit to %" PRIu64,
215 devc->limit_samples);
217 case SR_CONF_LIMIT_MSEC:
218 devc->limit_msec = g_variant_get_uint64(data) * 1000;
219 devc->limit_samples = 0;
220 sr_dbg("Setting time limit to %" PRIu64"ms", devc->limit_msec);
222 case SR_CONF_SAMPLERATE:
223 samplerate = g_variant_get_uint64(data);
224 if (samplerate > MAX_SAMPLE_RATE) {
225 sr_err("Maximum sample rate is %d", MAX_SAMPLE_RATE);
226 ret = SR_ERR_SAMPLERATE;
229 devc->samplerate = samplerate;
230 sr_dbg("Setting samplerate to %" PRIu64, devc->samplerate);
239 static int config_list(uint32_t key, GVariant **data,
240 const struct sr_dev_inst *sdi,
241 const struct sr_channel_group *cg)
252 case SR_CONF_DEVICE_OPTIONS:
253 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
258 case SR_CONF_SAMPLERATE:
259 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
260 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
262 ARRAY_SIZE(samplerates),
264 g_variant_builder_add(&gvb, "{sv}", "samplerate-steps", gvar);
265 *data = g_variant_builder_end(&gvb);
274 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
276 struct dev_context *devc;
280 if (sdi->status != SR_ST_ACTIVE)
281 return SR_ERR_DEV_CLOSED;
284 devc->samples_read = 0;
286 if (pipe(devc->pipe_fds)) {
287 sr_err("Error setting up pipe");
291 devc->channel = g_io_channel_unix_new(devc->pipe_fds[0]);
292 g_io_channel_set_flags(devc->channel, G_IO_FLAG_NONBLOCK, NULL);
293 g_io_channel_set_encoding(devc->channel, NULL, NULL);
294 g_io_channel_set_buffered(devc->channel, FALSE);
296 sr_session_source_add_channel(sdi->session, devc->channel,
297 G_IO_IN | G_IO_ERR, 1,
298 bl_acme_receive_data, (void *)sdi);
300 /* Send header packet to the session bus. */
301 std_session_send_df_header(sdi, LOG_PREFIX);
302 devc->start_time = g_get_monotonic_time();
307 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
309 struct sr_datafeed_packet packet;
310 struct dev_context *devc;
316 if (sdi->status != SR_ST_ACTIVE)
317 return SR_ERR_DEV_CLOSED;
319 sr_session_source_remove_channel(sdi->session, devc->channel);
320 g_io_channel_shutdown(devc->channel, FALSE, NULL);
321 g_io_channel_unref(devc->channel);
322 devc->channel = NULL;
324 /* Send last packet. */
325 packet.type = SR_DF_END;
326 sr_session_send(sdi, &packet);
331 SR_PRIV struct sr_dev_driver baylibre_acme_driver_info = {
332 .name = "baylibre-acme",
333 .longname = "BayLibre ACME (Another Cute Measurement Equipment)",
338 .dev_list = dev_list,
339 .dev_clear = dev_clear,
340 .config_get = config_get,
341 .config_set = config_set,
342 .config_list = config_list,
343 .dev_open = dev_open,
344 .dev_close = dev_close,
345 .dev_acquisition_start = dev_acquisition_start,
346 .dev_acquisition_stop = dev_acquisition_stop,