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,
31 SR_CONF_POWER_OFF | SR_CONF_GET | SR_CONF_SET,
34 #define MAX_SAMPLE_RATE 500 /* In Hz */
36 static const uint64_t samplerates[] = {
38 SR_HZ(MAX_SAMPLE_RATE),
42 static int init(struct sr_context *sr_ctx)
44 return std_init(sr_ctx, di, LOG_PREFIX);
47 static GSList *scan(GSList *options)
49 struct drv_context *drvc;
50 struct dev_context *devc;
51 struct sr_dev_inst *sdi;
61 devc = g_malloc0(sizeof(struct dev_context));
62 devc->samplerate = SR_HZ(10);
64 sdi = g_malloc0(sizeof(struct sr_dev_inst));
65 sdi->status = SR_ST_INACTIVE;
66 sdi->vendor = g_strdup("BayLibre");
67 sdi->model = g_strdup("ACME");
71 status = bl_acme_is_sane();
76 * Iterate over all ACME connectors and check if any probes
79 for (i = 0; i < MAX_PROBES; i++) {
81 * First check if there's an energy probe on this connector. If
82 * not, and we're already at the fifth probe - see if we can
83 * detect a temperature probe.
85 status = bl_acme_detect_probe(bl_acme_get_enrg_addr(i),
86 PROBE_NUM(i), ENRG_PROBE_NAME);
88 /* Energy probe detected. */
89 status = bl_acme_register_probe(sdi, PROBE_ENRG,
90 bl_acme_get_enrg_addr(i), PROBE_NUM(i));
92 sr_err("Error registering power probe %d",
96 } else if (i >= TEMP_PRB_START_INDEX) {
97 status = bl_acme_detect_probe(bl_acme_get_temp_addr(i),
98 PROBE_NUM(i), TEMP_PROBE_NAME);
100 /* Temperature probe detected. */
101 status = bl_acme_register_probe(sdi,PROBE_TEMP,
102 bl_acme_get_temp_addr(i), PROBE_NUM(i));
104 sr_err("Error registering temp "
105 "probe %d", PROBE_NUM(i));
112 devices = g_slist_append(devices, sdi);
113 drvc->instances = g_slist_append(drvc->instances, sdi);
119 sr_dev_inst_free(sdi);
124 static GSList *dev_list(void)
126 return ((struct drv_context *)(di->priv))->instances;
129 static int dev_clear(void)
131 return std_dev_clear(di, NULL);
134 static int dev_open(struct sr_dev_inst *sdi)
138 /* Nothing to do here. */
139 sdi->status = SR_ST_ACTIVE;
144 static int dev_close(struct sr_dev_inst *sdi)
148 /* Nothing to do here. */
149 sdi->status = SR_ST_INACTIVE;
154 static int cleanup(void)
161 static int config_get(uint32_t key, GVariant **data,
162 const struct sr_dev_inst *sdi,
163 const struct sr_channel_group *cg)
165 struct dev_context *devc;
174 case SR_CONF_LIMIT_SAMPLES:
175 *data = g_variant_new_uint64(devc->limit_samples);
177 case SR_CONF_LIMIT_MSEC:
178 *data = g_variant_new_uint64(devc->limit_msec);
180 case SR_CONF_SAMPLERATE:
181 *data = g_variant_new_uint64(devc->samplerate);
183 case SR_CONF_PROBE_FACTOR:
185 return SR_ERR_CHANNEL_GROUP;
186 ret = bl_acme_get_shunt(cg, &shunt);
188 *data = g_variant_new_uint64(shunt);
190 case SR_CONF_POWER_OFF:
192 return SR_ERR_CHANNEL_GROUP;
193 ret = bl_acme_read_power_state(cg, &power_off);
195 *data = g_variant_new_boolean(power_off);
204 static int config_set(uint32_t key, GVariant *data,
205 const struct sr_dev_inst *sdi,
206 const struct sr_channel_group *cg)
208 struct dev_context *devc;
212 if (sdi->status != SR_ST_ACTIVE)
213 return SR_ERR_DEV_CLOSED;
219 case SR_CONF_LIMIT_SAMPLES:
220 devc->limit_samples = g_variant_get_uint64(data);
221 devc->limit_msec = 0;
222 sr_dbg("Setting sample limit to %" PRIu64, devc->limit_samples);
224 case SR_CONF_LIMIT_MSEC:
225 devc->limit_msec = g_variant_get_uint64(data) * 1000;
226 devc->limit_samples = 0;
227 sr_dbg("Setting time limit to %" PRIu64"ms", devc->limit_msec);
229 case SR_CONF_SAMPLERATE:
230 samplerate = g_variant_get_uint64(data);
231 if (samplerate > MAX_SAMPLE_RATE) {
232 sr_err("Maximum sample rate is %d", MAX_SAMPLE_RATE);
233 ret = SR_ERR_SAMPLERATE;
236 devc->samplerate = samplerate;
237 sr_dbg("Setting samplerate to %" PRIu64, devc->samplerate);
239 case SR_CONF_PROBE_FACTOR:
241 return SR_ERR_CHANNEL_GROUP;
242 ret = bl_acme_set_shunt(cg, g_variant_get_uint64(data));
244 case SR_CONF_POWER_OFF:
246 return SR_ERR_CHANNEL_GROUP;
247 ret = bl_acme_set_power_off(cg, g_variant_get_boolean(data));
256 static int config_list(uint32_t key, GVariant **data,
257 const struct sr_dev_inst *sdi,
258 const struct sr_channel_group *cg)
269 case SR_CONF_DEVICE_OPTIONS:
270 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
271 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
273 case SR_CONF_SAMPLERATE:
274 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
275 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
276 samplerates, ARRAY_SIZE(samplerates), sizeof(uint64_t));
277 g_variant_builder_add(&gvb, "{sv}", "samplerate-steps", gvar);
278 *data = g_variant_builder_end(&gvb);
287 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
289 struct dev_context *devc;
293 if (sdi->status != SR_ST_ACTIVE)
294 return SR_ERR_DEV_CLOSED;
297 devc->samples_read = 0;
299 if (pipe(devc->pipe_fds)) {
300 sr_err("Error setting up pipe");
304 devc->channel = g_io_channel_unix_new(devc->pipe_fds[0]);
305 g_io_channel_set_flags(devc->channel, G_IO_FLAG_NONBLOCK, NULL);
306 g_io_channel_set_encoding(devc->channel, NULL, NULL);
307 g_io_channel_set_buffered(devc->channel, FALSE);
309 sr_session_source_add_channel(sdi->session, devc->channel,
310 G_IO_IN | G_IO_ERR, 1, bl_acme_receive_data, (void *)sdi);
312 /* Send header packet to the session bus. */
313 std_session_send_df_header(sdi, LOG_PREFIX);
314 devc->start_time = g_get_monotonic_time();
319 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
321 struct sr_datafeed_packet packet;
322 struct dev_context *devc;
328 if (sdi->status != SR_ST_ACTIVE)
329 return SR_ERR_DEV_CLOSED;
331 sr_session_source_remove_channel(sdi->session, devc->channel);
332 g_io_channel_shutdown(devc->channel, FALSE, NULL);
333 g_io_channel_unref(devc->channel);
334 devc->channel = NULL;
336 /* Send last packet. */
337 packet.type = SR_DF_END;
338 sr_session_send(sdi, &packet);
343 SR_PRIV struct sr_dev_driver baylibre_acme_driver_info = {
344 .name = "baylibre-acme",
345 .longname = "BayLibre ACME (Another Cute Measurement Equipment)",
350 .dev_list = dev_list,
351 .dev_clear = dev_clear,
352 .config_get = config_get,
353 .config_set = config_set,
354 .config_list = config_list,
355 .dev_open = dev_open,
356 .dev_close = dev_close,
357 .dev_acquisition_start = dev_acquisition_start,
358 .dev_acquisition_stop = dev_acquisition_stop,