]> sigrok.org Git - libsigrok.git/blame - src/hardware/baylibre-acme/api.c
SR_CONF_PROBE_FACTOR: New option.
[libsigrok.git] / src / hardware / baylibre-acme / api.c
CommitLineData
dfaee1de
BG
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2015 Bartosz Golaszewski <bgolaszewski@baylibre.com>
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
20#include "protocol.h"
21
22SR_PRIV struct sr_dev_driver baylibre_acme_driver_info;
23static struct sr_dev_driver *di = &baylibre_acme_driver_info;
24
6b80b80d
BG
25static 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};
31
32#define MAX_SAMPLE_RATE 500 /* In Hz */
33
34static const uint64_t samplerates[] = {
35 SR_HZ(1),
36 SR_HZ(MAX_SAMPLE_RATE),
37 SR_HZ(1),
38};
39
dfaee1de
BG
40static int init(struct sr_context *sr_ctx)
41{
42 return std_init(sr_ctx, di, LOG_PREFIX);
43}
44
45static GSList *scan(GSList *options)
46{
47 struct drv_context *drvc;
6b80b80d
BG
48 struct dev_context *devc;
49 struct sr_dev_inst *sdi;
dfaee1de 50 GSList *devices;
6b80b80d
BG
51 gboolean status;
52 int i;
dfaee1de
BG
53
54 (void)options;
55
dfaee1de 56 drvc = di->priv;
6b80b80d 57 devices = NULL;
dfaee1de 58
6b80b80d
BG
59 devc = g_malloc0(sizeof(struct dev_context));
60 devc->samplerate = SR_HZ(10);
61
62 sdi = g_malloc0(sizeof(struct sr_dev_inst));
63 sdi->status = SR_ST_INACTIVE;
64 sdi->model = g_strdup("Acme cape");
65 sdi->driver = di;
66 sdi->priv = devc;
67
68 status = bl_acme_is_sane();
69 if (!status)
70 goto err_out;
71
72 /*
73 * Iterate over all ACME connectors and check if any probes
74 * are present.
75 */
76 for (i = 0; i < MAX_PROBES; i++) {
77 /*
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.
81 */
82 status = bl_acme_detect_probe(bl_acme_get_enrg_addr(i),
83 PROBE_NUM(i),
84 ENRG_PROBE_NAME);
85 if (status) {
86 /* Energy probe detected. */
87 status = bl_acme_register_probe(sdi,
88 PROBE_ENRG,
89 bl_acme_get_enrg_addr(i),
90 PROBE_NUM(i));
91 if (!status) {
92 sr_err("Error registering power probe %d",
93 PROBE_NUM(i));
94 continue;
95 }
96
97 } else if (i >= TEMP_PRB_START_INDEX) {
98 status = bl_acme_detect_probe(bl_acme_get_temp_addr(i),
99 PROBE_NUM(i),
100 TEMP_PROBE_NAME);
101 if (status) {
102 /* Temperature probe detected. */
103 status = bl_acme_register_probe(sdi,PROBE_TEMP,
104 bl_acme_get_temp_addr(i),
105 PROBE_NUM(i));
106 if (!status) {
107 sr_err("Error registering temp "
108 "probe %d", PROBE_NUM(i));
109 continue;
110 }
111 }
112
113 }
114 }
115
116 devices = g_slist_append(devices, sdi);
117 drvc->instances = g_slist_append(drvc->instances, sdi);
dfaee1de
BG
118
119 return devices;
6b80b80d
BG
120
121err_out:
122 g_free(devc);
123 sr_dev_inst_free(sdi);
124
125 return NULL;
dfaee1de
BG
126}
127
128static GSList *dev_list(void)
129{
130 return ((struct drv_context *)(di->priv))->instances;
131}
132
133static int dev_clear(void)
134{
135 return std_dev_clear(di, NULL);
136}
137
138static int dev_open(struct sr_dev_inst *sdi)
139{
140 (void)sdi;
141
6b80b80d 142 /* Nothing to do here. */
dfaee1de
BG
143 sdi->status = SR_ST_ACTIVE;
144
145 return SR_OK;
146}
147
148static int dev_close(struct sr_dev_inst *sdi)
149{
150 (void)sdi;
151
6b80b80d 152 /* Nothing to do here. */
dfaee1de
BG
153 sdi->status = SR_ST_INACTIVE;
154
155 return SR_OK;
156}
157
158static int cleanup(void)
159{
160 dev_clear();
161
dfaee1de
BG
162 return SR_OK;
163}
164
6b80b80d
BG
165static int config_get(uint32_t key, GVariant **data,
166 const struct sr_dev_inst *sdi,
167 const struct sr_channel_group *cg)
dfaee1de 168{
6b80b80d 169 struct dev_context *devc;
dfaee1de
BG
170 int ret;
171
dfaee1de
BG
172 (void)cg;
173
6b80b80d
BG
174 devc = sdi->priv;
175
dfaee1de
BG
176 ret = SR_OK;
177 switch (key) {
6b80b80d
BG
178 case SR_CONF_LIMIT_SAMPLES:
179 *data = g_variant_new_uint64(devc->limit_samples);
180 break;
181 case SR_CONF_LIMIT_MSEC:
182 *data = g_variant_new_uint64(devc->limit_msec);
183 break;
184 case SR_CONF_SAMPLERATE:
185 *data = g_variant_new_uint64(devc->samplerate);
186 break;
dfaee1de
BG
187 default:
188 return SR_ERR_NA;
189 }
190
191 return ret;
192}
193
6b80b80d
BG
194static int config_set(uint32_t key, GVariant *data,
195 const struct sr_dev_inst *sdi,
196 const struct sr_channel_group *cg)
dfaee1de 197{
6b80b80d
BG
198 struct dev_context *devc;
199 uint64_t samplerate;
dfaee1de
BG
200 int ret;
201
dfaee1de
BG
202 (void)cg;
203
204 if (sdi->status != SR_ST_ACTIVE)
205 return SR_ERR_DEV_CLOSED;
206
6b80b80d
BG
207 devc = sdi->priv;
208
dfaee1de
BG
209 ret = SR_OK;
210 switch (key) {
6b80b80d
BG
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);
216 break;
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);
221 break;
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;
227 break;
228 }
229 devc->samplerate = samplerate;
230 sr_dbg("Setting samplerate to %" PRIu64, devc->samplerate);
231 break;
dfaee1de
BG
232 default:
233 ret = SR_ERR_NA;
234 }
235
236 return ret;
237}
238
6b80b80d
BG
239static int config_list(uint32_t key, GVariant **data,
240 const struct sr_dev_inst *sdi,
241 const struct sr_channel_group *cg)
dfaee1de 242{
6b80b80d
BG
243 GVariant *gvar;
244 GVariantBuilder gvb;
dfaee1de
BG
245 int ret;
246
247 (void)sdi;
dfaee1de
BG
248 (void)cg;
249
250 ret = SR_OK;
251 switch (key) {
6b80b80d
BG
252 case SR_CONF_DEVICE_OPTIONS:
253 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
254 devopts,
255 ARRAY_SIZE(devopts),
256 sizeof(uint32_t));
257 break;
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"),
261 samplerates,
262 ARRAY_SIZE(samplerates),
263 sizeof(uint64_t));
264 g_variant_builder_add(&gvb, "{sv}", "samplerate-steps", gvar);
265 *data = g_variant_builder_end(&gvb);
266 break;
dfaee1de
BG
267 default:
268 return SR_ERR_NA;
269 }
270
271 return ret;
272}
273
6b80b80d 274static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
dfaee1de 275{
6b80b80d
BG
276 struct dev_context *devc;
277
dfaee1de
BG
278 (void)cb_data;
279
280 if (sdi->status != SR_ST_ACTIVE)
281 return SR_ERR_DEV_CLOSED;
282
6b80b80d
BG
283 devc = sdi->priv;
284 devc->samples_read = 0;
285
286 if (pipe(devc->pipe_fds)) {
287 sr_err("Error setting up pipe");
288 return SR_ERR;
289 }
290
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);
295
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);
299
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();
dfaee1de
BG
303
304 return SR_OK;
305}
306
307static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
308{
6b80b80d
BG
309 struct sr_datafeed_packet packet;
310 struct dev_context *devc;
311
dfaee1de
BG
312 (void)cb_data;
313
6b80b80d
BG
314 devc = sdi->priv;
315
dfaee1de
BG
316 if (sdi->status != SR_ST_ACTIVE)
317 return SR_ERR_DEV_CLOSED;
318
6b80b80d
BG
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;
323
324 /* Send last packet. */
325 packet.type = SR_DF_END;
326 sr_session_send(sdi, &packet);
dfaee1de
BG
327
328 return SR_OK;
329}
330
331SR_PRIV struct sr_dev_driver baylibre_acme_driver_info = {
332 .name = "baylibre-acme",
6b80b80d 333 .longname = "BayLibre ACME (Another Cute Measurement Equipment)",
dfaee1de
BG
334 .api_version = 1,
335 .init = init,
336 .cleanup = cleanup,
337 .scan = scan,
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,
347 .priv = NULL,
348};