]> sigrok.org Git - libsigrok.git/blame - src/hardware/baylibre-acme/api.c
Construct driver array at runtime, from an array of per-file arrays.
[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,
5fabeeac
BG
30};
31
32static const uint32_t devopts_cg[] = {
61f2b7f7 33 SR_CONF_PROBE_FACTOR | SR_CONF_GET | SR_CONF_SET,
740ad48a 34 SR_CONF_POWER_OFF | SR_CONF_GET | SR_CONF_SET,
6b80b80d
BG
35};
36
391e23a9 37#define MAX_SAMPLE_RATE 500 /* In Hz */
6b80b80d
BG
38
39static const uint64_t samplerates[] = {
40 SR_HZ(1),
41 SR_HZ(MAX_SAMPLE_RATE),
42 SR_HZ(1),
43};
44
dfaee1de
BG
45static int init(struct sr_context *sr_ctx)
46{
47 return std_init(sr_ctx, di, LOG_PREFIX);
48}
49
50static GSList *scan(GSList *options)
51{
52 struct drv_context *drvc;
6b80b80d
BG
53 struct dev_context *devc;
54 struct sr_dev_inst *sdi;
dfaee1de 55 GSList *devices;
6b80b80d
BG
56 gboolean status;
57 int i;
dfaee1de
BG
58
59 (void)options;
60
dfaee1de 61 drvc = di->priv;
6b80b80d 62 devices = NULL;
dfaee1de 63
6b80b80d
BG
64 devc = g_malloc0(sizeof(struct dev_context));
65 devc->samplerate = SR_HZ(10);
66
67 sdi = g_malloc0(sizeof(struct sr_dev_inst));
68 sdi->status = SR_ST_INACTIVE;
3d14ce49
UH
69 sdi->vendor = g_strdup("BayLibre");
70 sdi->model = g_strdup("ACME");
6b80b80d
BG
71 sdi->driver = di;
72 sdi->priv = devc;
73
74 status = bl_acme_is_sane();
75 if (!status)
76 goto err_out;
77
78 /*
79 * Iterate over all ACME connectors and check if any probes
80 * are present.
81 */
82 for (i = 0; i < MAX_PROBES; i++) {
83 /*
84 * First check if there's an energy probe on this connector. If
85 * not, and we're already at the fifth probe - see if we can
86 * detect a temperature probe.
87 */
88 status = bl_acme_detect_probe(bl_acme_get_enrg_addr(i),
391e23a9 89 PROBE_NUM(i), ENRG_PROBE_NAME);
6b80b80d
BG
90 if (status) {
91 /* Energy probe detected. */
391e23a9
UH
92 status = bl_acme_register_probe(sdi, PROBE_ENRG,
93 bl_acme_get_enrg_addr(i), PROBE_NUM(i));
6b80b80d
BG
94 if (!status) {
95 sr_err("Error registering power probe %d",
96 PROBE_NUM(i));
97 continue;
98 }
6b80b80d
BG
99 } else if (i >= TEMP_PRB_START_INDEX) {
100 status = bl_acme_detect_probe(bl_acme_get_temp_addr(i),
391e23a9 101 PROBE_NUM(i), TEMP_PROBE_NAME);
6b80b80d
BG
102 if (status) {
103 /* Temperature probe detected. */
104 status = bl_acme_register_probe(sdi,PROBE_TEMP,
391e23a9 105 bl_acme_get_temp_addr(i), PROBE_NUM(i));
6b80b80d
BG
106 if (!status) {
107 sr_err("Error registering temp "
108 "probe %d", PROBE_NUM(i));
109 continue;
110 }
111 }
6b80b80d
BG
112 }
113 }
114
380ee96f
BG
115 /*
116 * Let's assume there's no ACME device present if no probe
117 * has been registered.
118 */
119 if (sdi->channel_groups == NULL)
120 goto err_out;
121
6b80b80d
BG
122 devices = g_slist_append(devices, sdi);
123 drvc->instances = g_slist_append(drvc->instances, sdi);
dfaee1de
BG
124
125 return devices;
6b80b80d
BG
126
127err_out:
128 g_free(devc);
129 sr_dev_inst_free(sdi);
130
131 return NULL;
dfaee1de
BG
132}
133
134static GSList *dev_list(void)
135{
136 return ((struct drv_context *)(di->priv))->instances;
137}
138
139static int dev_clear(void)
140{
141 return std_dev_clear(di, NULL);
142}
143
144static int dev_open(struct sr_dev_inst *sdi)
145{
146 (void)sdi;
147
6b80b80d 148 /* Nothing to do here. */
dfaee1de
BG
149 sdi->status = SR_ST_ACTIVE;
150
151 return SR_OK;
152}
153
154static int dev_close(struct sr_dev_inst *sdi)
155{
156 (void)sdi;
157
6b80b80d 158 /* Nothing to do here. */
dfaee1de
BG
159 sdi->status = SR_ST_INACTIVE;
160
161 return SR_OK;
162}
163
164static int cleanup(void)
165{
166 dev_clear();
167
dfaee1de
BG
168 return SR_OK;
169}
170
6b80b80d
BG
171static int config_get(uint32_t key, GVariant **data,
172 const struct sr_dev_inst *sdi,
173 const struct sr_channel_group *cg)
dfaee1de 174{
6b80b80d 175 struct dev_context *devc;
dfaee1de 176 int ret;
61f2b7f7 177 uint64_t shunt;
740ad48a 178 gboolean power_off;
dfaee1de 179
6b80b80d
BG
180 devc = sdi->priv;
181
dfaee1de
BG
182 ret = SR_OK;
183 switch (key) {
6b80b80d
BG
184 case SR_CONF_LIMIT_SAMPLES:
185 *data = g_variant_new_uint64(devc->limit_samples);
186 break;
187 case SR_CONF_LIMIT_MSEC:
188 *data = g_variant_new_uint64(devc->limit_msec);
189 break;
190 case SR_CONF_SAMPLERATE:
191 *data = g_variant_new_uint64(devc->samplerate);
192 break;
61f2b7f7
BG
193 case SR_CONF_PROBE_FACTOR:
194 if (!cg)
195 return SR_ERR_CHANNEL_GROUP;
196 ret = bl_acme_get_shunt(cg, &shunt);
197 if (ret == SR_OK)
198 *data = g_variant_new_uint64(shunt);
199 break;
740ad48a
BG
200 case SR_CONF_POWER_OFF:
201 if (!cg)
202 return SR_ERR_CHANNEL_GROUP;
203 ret = bl_acme_read_power_state(cg, &power_off);
204 if (ret == SR_OK)
205 *data = g_variant_new_boolean(power_off);
206 break;
dfaee1de
BG
207 default:
208 return SR_ERR_NA;
209 }
210
211 return ret;
212}
213
6b80b80d
BG
214static int config_set(uint32_t key, GVariant *data,
215 const struct sr_dev_inst *sdi,
216 const struct sr_channel_group *cg)
dfaee1de 217{
6b80b80d
BG
218 struct dev_context *devc;
219 uint64_t samplerate;
dfaee1de
BG
220 int ret;
221
dfaee1de
BG
222 if (sdi->status != SR_ST_ACTIVE)
223 return SR_ERR_DEV_CLOSED;
224
6b80b80d
BG
225 devc = sdi->priv;
226
dfaee1de
BG
227 ret = SR_OK;
228 switch (key) {
6b80b80d
BG
229 case SR_CONF_LIMIT_SAMPLES:
230 devc->limit_samples = g_variant_get_uint64(data);
231 devc->limit_msec = 0;
391e23a9 232 sr_dbg("Setting sample limit to %" PRIu64, devc->limit_samples);
6b80b80d
BG
233 break;
234 case SR_CONF_LIMIT_MSEC:
235 devc->limit_msec = g_variant_get_uint64(data) * 1000;
236 devc->limit_samples = 0;
237 sr_dbg("Setting time limit to %" PRIu64"ms", devc->limit_msec);
238 break;
239 case SR_CONF_SAMPLERATE:
240 samplerate = g_variant_get_uint64(data);
241 if (samplerate > MAX_SAMPLE_RATE) {
242 sr_err("Maximum sample rate is %d", MAX_SAMPLE_RATE);
243 ret = SR_ERR_SAMPLERATE;
244 break;
245 }
246 devc->samplerate = samplerate;
247 sr_dbg("Setting samplerate to %" PRIu64, devc->samplerate);
248 break;
61f2b7f7
BG
249 case SR_CONF_PROBE_FACTOR:
250 if (!cg)
251 return SR_ERR_CHANNEL_GROUP;
252 ret = bl_acme_set_shunt(cg, g_variant_get_uint64(data));
253 break;
740ad48a
BG
254 case SR_CONF_POWER_OFF:
255 if (!cg)
256 return SR_ERR_CHANNEL_GROUP;
257 ret = bl_acme_set_power_off(cg, g_variant_get_boolean(data));
258 break;
dfaee1de
BG
259 default:
260 ret = SR_ERR_NA;
261 }
262
263 return ret;
264}
265
6b80b80d
BG
266static int config_list(uint32_t key, GVariant **data,
267 const struct sr_dev_inst *sdi,
268 const struct sr_channel_group *cg)
dfaee1de 269{
6b80b80d
BG
270 GVariant *gvar;
271 GVariantBuilder gvb;
dfaee1de
BG
272 int ret;
273
274 (void)sdi;
dfaee1de
BG
275 (void)cg;
276
277 ret = SR_OK;
5fabeeac
BG
278 if (!cg) {
279 switch (key) {
280 case SR_CONF_DEVICE_OPTIONS:
281 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
282 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
283 break;
284 case SR_CONF_SAMPLERATE:
285 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
286 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
287 samplerates, ARRAY_SIZE(samplerates), sizeof(uint64_t));
288 g_variant_builder_add(&gvb, "{sv}",
289 "samplerate-steps", gvar);
290 *data = g_variant_builder_end(&gvb);
291 break;
292 default:
293 return SR_ERR_NA;
294 }
295 } else {
296 switch (key) {
297 case SR_CONF_DEVICE_OPTIONS:
298 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
299 devopts_cg, ARRAY_SIZE(devopts_cg), sizeof(uint32_t));
300 break;
301 default:
302 return SR_ERR_NA;
303 }
dfaee1de
BG
304 }
305
306 return ret;
307}
308
6b80b80d 309static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
dfaee1de 310{
6b80b80d
BG
311 struct dev_context *devc;
312
dfaee1de
BG
313 (void)cb_data;
314
315 if (sdi->status != SR_ST_ACTIVE)
316 return SR_ERR_DEV_CLOSED;
317
6b80b80d
BG
318 devc = sdi->priv;
319 devc->samples_read = 0;
320
321 if (pipe(devc->pipe_fds)) {
322 sr_err("Error setting up pipe");
323 return SR_ERR;
324 }
325
326 devc->channel = g_io_channel_unix_new(devc->pipe_fds[0]);
327 g_io_channel_set_flags(devc->channel, G_IO_FLAG_NONBLOCK, NULL);
328 g_io_channel_set_encoding(devc->channel, NULL, NULL);
329 g_io_channel_set_buffered(devc->channel, FALSE);
330
331 sr_session_source_add_channel(sdi->session, devc->channel,
391e23a9 332 G_IO_IN | G_IO_ERR, 1, bl_acme_receive_data, (void *)sdi);
6b80b80d
BG
333
334 /* Send header packet to the session bus. */
335 std_session_send_df_header(sdi, LOG_PREFIX);
336 devc->start_time = g_get_monotonic_time();
dfaee1de
BG
337
338 return SR_OK;
339}
340
341static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
342{
6b80b80d
BG
343 struct sr_datafeed_packet packet;
344 struct dev_context *devc;
345
dfaee1de
BG
346 (void)cb_data;
347
6b80b80d
BG
348 devc = sdi->priv;
349
dfaee1de
BG
350 if (sdi->status != SR_ST_ACTIVE)
351 return SR_ERR_DEV_CLOSED;
352
6b80b80d
BG
353 sr_session_source_remove_channel(sdi->session, devc->channel);
354 g_io_channel_shutdown(devc->channel, FALSE, NULL);
355 g_io_channel_unref(devc->channel);
356 devc->channel = NULL;
357
358 /* Send last packet. */
359 packet.type = SR_DF_END;
360 sr_session_send(sdi, &packet);
dfaee1de
BG
361
362 return SR_OK;
363}
364
365SR_PRIV struct sr_dev_driver baylibre_acme_driver_info = {
366 .name = "baylibre-acme",
6b80b80d 367 .longname = "BayLibre ACME (Another Cute Measurement Equipment)",
dfaee1de
BG
368 .api_version = 1,
369 .init = init,
370 .cleanup = cleanup,
371 .scan = scan,
372 .dev_list = dev_list,
373 .dev_clear = dev_clear,
374 .config_get = config_get,
375 .config_set = config_set,
376 .config_list = config_list,
377 .dev_open = dev_open,
378 .dev_close = dev_close,
379 .dev_acquisition_start = dev_acquisition_start,
380 .dev_acquisition_stop = dev_acquisition_stop,
381 .priv = NULL,
382};