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,
30 SR_CONF_PROBE_FACTOR | SR_CONF_GET | SR_CONF_SET,
33 #define MAX_SAMPLE_RATE 500 /* In Hz */
35 static const uint64_t samplerates[] = {
37 SR_HZ(MAX_SAMPLE_RATE),
41 static int init(struct sr_context *sr_ctx)
43 return std_init(sr_ctx, di, LOG_PREFIX);
46 static GSList *scan(GSList *options)
48 struct drv_context *drvc;
49 struct dev_context *devc;
50 struct sr_dev_inst *sdi;
60 devc = g_malloc0(sizeof(struct dev_context));
61 devc->samplerate = SR_HZ(10);
63 sdi = g_malloc0(sizeof(struct sr_dev_inst));
64 sdi->status = SR_ST_INACTIVE;
65 sdi->model = g_strdup("Acme cape");
69 status = bl_acme_is_sane();
74 * Iterate over all ACME connectors and check if any probes
77 for (i = 0; i < MAX_PROBES; i++) {
79 * First check if there's an energy probe on this connector. If
80 * not, and we're already at the fifth probe - see if we can
81 * detect a temperature probe.
83 status = bl_acme_detect_probe(bl_acme_get_enrg_addr(i),
87 /* Energy probe detected. */
88 status = bl_acme_register_probe(sdi,
90 bl_acme_get_enrg_addr(i),
93 sr_err("Error registering power probe %d",
98 } else if (i >= TEMP_PRB_START_INDEX) {
99 status = bl_acme_detect_probe(bl_acme_get_temp_addr(i),
103 /* Temperature probe detected. */
104 status = bl_acme_register_probe(sdi,PROBE_TEMP,
105 bl_acme_get_temp_addr(i),
108 sr_err("Error registering temp "
109 "probe %d", PROBE_NUM(i));
117 devices = g_slist_append(devices, sdi);
118 drvc->instances = g_slist_append(drvc->instances, sdi);
124 sr_dev_inst_free(sdi);
129 static GSList *dev_list(void)
131 return ((struct drv_context *)(di->priv))->instances;
134 static int dev_clear(void)
136 return std_dev_clear(di, NULL);
139 static int dev_open(struct sr_dev_inst *sdi)
143 /* Nothing to do here. */
144 sdi->status = SR_ST_ACTIVE;
149 static int dev_close(struct sr_dev_inst *sdi)
153 /* Nothing to do here. */
154 sdi->status = SR_ST_INACTIVE;
159 static int cleanup(void)
166 static int config_get(uint32_t key, GVariant **data,
167 const struct sr_dev_inst *sdi,
168 const struct sr_channel_group *cg)
170 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);
187 case SR_CONF_PROBE_FACTOR:
189 return SR_ERR_CHANNEL_GROUP;
190 ret = bl_acme_get_shunt(cg, &shunt);
192 *data = g_variant_new_uint64(shunt);
201 static int config_set(uint32_t key, GVariant *data,
202 const struct sr_dev_inst *sdi,
203 const struct sr_channel_group *cg)
205 struct dev_context *devc;
209 if (sdi->status != SR_ST_ACTIVE)
210 return SR_ERR_DEV_CLOSED;
216 case SR_CONF_LIMIT_SAMPLES:
217 devc->limit_samples = g_variant_get_uint64(data);
218 devc->limit_msec = 0;
219 sr_dbg("Setting sample limit to %" PRIu64,
220 devc->limit_samples);
222 case SR_CONF_LIMIT_MSEC:
223 devc->limit_msec = g_variant_get_uint64(data) * 1000;
224 devc->limit_samples = 0;
225 sr_dbg("Setting time limit to %" PRIu64"ms", devc->limit_msec);
227 case SR_CONF_SAMPLERATE:
228 samplerate = g_variant_get_uint64(data);
229 if (samplerate > MAX_SAMPLE_RATE) {
230 sr_err("Maximum sample rate is %d", MAX_SAMPLE_RATE);
231 ret = SR_ERR_SAMPLERATE;
234 devc->samplerate = samplerate;
235 sr_dbg("Setting samplerate to %" PRIu64, devc->samplerate);
237 case SR_CONF_PROBE_FACTOR:
239 return SR_ERR_CHANNEL_GROUP;
240 ret = bl_acme_set_shunt(cg, g_variant_get_uint64(data));
249 static int config_list(uint32_t key, GVariant **data,
250 const struct sr_dev_inst *sdi,
251 const struct sr_channel_group *cg)
262 case SR_CONF_DEVICE_OPTIONS:
263 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
268 case SR_CONF_SAMPLERATE:
269 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
270 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
272 ARRAY_SIZE(samplerates),
274 g_variant_builder_add(&gvb, "{sv}", "samplerate-steps", gvar);
275 *data = g_variant_builder_end(&gvb);
284 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
286 struct dev_context *devc;
290 if (sdi->status != SR_ST_ACTIVE)
291 return SR_ERR_DEV_CLOSED;
294 devc->samples_read = 0;
296 if (pipe(devc->pipe_fds)) {
297 sr_err("Error setting up pipe");
301 devc->channel = g_io_channel_unix_new(devc->pipe_fds[0]);
302 g_io_channel_set_flags(devc->channel, G_IO_FLAG_NONBLOCK, NULL);
303 g_io_channel_set_encoding(devc->channel, NULL, NULL);
304 g_io_channel_set_buffered(devc->channel, FALSE);
306 sr_session_source_add_channel(sdi->session, devc->channel,
307 G_IO_IN | G_IO_ERR, 1,
308 bl_acme_receive_data, (void *)sdi);
310 /* Send header packet to the session bus. */
311 std_session_send_df_header(sdi, LOG_PREFIX);
312 devc->start_time = g_get_monotonic_time();
317 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
319 struct sr_datafeed_packet packet;
320 struct dev_context *devc;
326 if (sdi->status != SR_ST_ACTIVE)
327 return SR_ERR_DEV_CLOSED;
329 sr_session_source_remove_channel(sdi->session, devc->channel);
330 g_io_channel_shutdown(devc->channel, FALSE, NULL);
331 g_io_channel_unref(devc->channel);
332 devc->channel = NULL;
334 /* Send last packet. */
335 packet.type = SR_DF_END;
336 sr_session_send(sdi, &packet);
341 SR_PRIV struct sr_dev_driver baylibre_acme_driver_info = {
342 .name = "baylibre-acme",
343 .longname = "BayLibre ACME (Another Cute Measurement Equipment)",
348 .dev_list = dev_list,
349 .dev_clear = dev_clear,
350 .config_get = config_get,
351 .config_set = config_set,
352 .config_list = config_list,
353 .dev_open = dev_open,
354 .dev_close = dev_close,
355 .dev_acquisition_start = dev_acquisition_start,
356 .dev_acquisition_stop = dev_acquisition_stop,