]> sigrok.org Git - libsigrok.git/blob - src/hardware/baylibre-acme/api.c
baylibre-acme: Add support for probe factor setting.
[libsigrok.git] / src / hardware / baylibre-acme / api.c
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
22 SR_PRIV struct sr_dev_driver baylibre_acme_driver_info;
23 static struct sr_dev_driver *di = &baylibre_acme_driver_info;
24
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 };
32
33 #define MAX_SAMPLE_RATE         500 /* In Hz */
34
35 static const uint64_t samplerates[] = {
36         SR_HZ(1),
37         SR_HZ(MAX_SAMPLE_RATE),
38         SR_HZ(1),
39 };
40
41 static int init(struct sr_context *sr_ctx)
42 {
43         return std_init(sr_ctx, di, LOG_PREFIX);
44 }
45
46 static GSList *scan(GSList *options)
47 {
48         struct drv_context *drvc;
49         struct dev_context *devc;
50         struct sr_dev_inst *sdi;
51         GSList *devices;
52         gboolean status;
53         int i;
54
55         (void)options;
56
57         drvc = di->priv;
58         devices = NULL;
59
60         devc = g_malloc0(sizeof(struct dev_context));
61         devc->samplerate = SR_HZ(10);
62
63         sdi = g_malloc0(sizeof(struct sr_dev_inst));
64         sdi->status = SR_ST_INACTIVE;
65         sdi->model = g_strdup("Acme cape");
66         sdi->driver = di;
67         sdi->priv = devc;
68
69         status = bl_acme_is_sane();
70         if (!status)
71                 goto err_out;
72
73         /*
74          * Iterate over all ACME connectors and check if any probes
75          * are present.
76          */
77         for (i = 0; i < MAX_PROBES; i++) {
78                 /*
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.
82                  */
83                 status = bl_acme_detect_probe(bl_acme_get_enrg_addr(i),
84                                               PROBE_NUM(i),
85                                               ENRG_PROBE_NAME);
86                 if (status) {
87                         /* Energy probe detected. */
88                         status = bl_acme_register_probe(sdi,
89                                                        PROBE_ENRG,
90                                                        bl_acme_get_enrg_addr(i),
91                                                        PROBE_NUM(i));
92                         if (!status) {
93                                 sr_err("Error registering power probe %d",
94                                        PROBE_NUM(i));
95                                 continue;
96                         }
97
98                 } else if (i >= TEMP_PRB_START_INDEX) {
99                         status = bl_acme_detect_probe(bl_acme_get_temp_addr(i),
100                                                       PROBE_NUM(i),
101                                                       TEMP_PROBE_NAME);
102                         if (status) {
103                                 /* Temperature probe detected. */
104                                 status = bl_acme_register_probe(sdi,PROBE_TEMP,
105                                                 bl_acme_get_temp_addr(i),
106                                                 PROBE_NUM(i));
107                                 if (!status) {
108                                         sr_err("Error registering temp "
109                                                "probe %d", PROBE_NUM(i));
110                                         continue;
111                                 }
112                         }
113
114                 }
115         }
116
117         devices = g_slist_append(devices, sdi);
118         drvc->instances = g_slist_append(drvc->instances, sdi);
119
120         return devices;
121
122 err_out:
123         g_free(devc);
124         sr_dev_inst_free(sdi);
125
126         return NULL;
127 }
128
129 static GSList *dev_list(void)
130 {
131         return ((struct drv_context *)(di->priv))->instances;
132 }
133
134 static int dev_clear(void)
135 {
136         return std_dev_clear(di, NULL);
137 }
138
139 static int dev_open(struct sr_dev_inst *sdi)
140 {
141         (void)sdi;
142
143         /* Nothing to do here. */
144         sdi->status = SR_ST_ACTIVE;
145
146         return SR_OK;
147 }
148
149 static int dev_close(struct sr_dev_inst *sdi)
150 {
151         (void)sdi;
152
153         /* Nothing to do here. */
154         sdi->status = SR_ST_INACTIVE;
155
156         return SR_OK;
157 }
158
159 static int cleanup(void)
160 {
161         dev_clear();
162
163         return SR_OK;
164 }
165
166 static int config_get(uint32_t key, GVariant **data,
167                       const struct sr_dev_inst *sdi,
168                       const struct sr_channel_group *cg)
169 {
170         struct dev_context *devc;
171         int ret;
172         uint64_t shunt;
173
174         devc = sdi->priv;
175
176         ret = SR_OK;
177         switch (key) {
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;
187         case SR_CONF_PROBE_FACTOR:
188                 if (!cg)
189                         return SR_ERR_CHANNEL_GROUP;
190                 ret = bl_acme_get_shunt(cg, &shunt);
191                 if (ret == SR_OK)
192                         *data = g_variant_new_uint64(shunt);
193                 break;
194         default:
195                 return SR_ERR_NA;
196         }
197
198         return ret;
199 }
200
201 static int config_set(uint32_t key, GVariant *data,
202                       const struct sr_dev_inst *sdi,
203                       const struct sr_channel_group *cg)
204 {
205         struct dev_context *devc;
206         uint64_t samplerate;
207         int ret;
208
209         if (sdi->status != SR_ST_ACTIVE)
210                 return SR_ERR_DEV_CLOSED;
211
212         devc = sdi->priv;
213
214         ret = SR_OK;
215         switch (key) {
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);
221                 break;
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);
226                 break;
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;
232                         break;
233                 }
234                 devc->samplerate = samplerate;
235                 sr_dbg("Setting samplerate to %" PRIu64, devc->samplerate);
236                 break;
237         case SR_CONF_PROBE_FACTOR:
238                 if (!cg)
239                         return SR_ERR_CHANNEL_GROUP;
240                 ret = bl_acme_set_shunt(cg, g_variant_get_uint64(data));
241                 break;
242         default:
243                 ret = SR_ERR_NA;
244         }
245
246         return ret;
247 }
248
249 static int config_list(uint32_t key, GVariant **data,
250                        const struct sr_dev_inst *sdi,
251                        const struct sr_channel_group *cg)
252 {
253         GVariant *gvar;
254         GVariantBuilder gvb;
255         int ret;
256
257         (void)sdi;
258         (void)cg;
259
260         ret = SR_OK;
261         switch (key) {
262         case SR_CONF_DEVICE_OPTIONS:
263                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
264                                                   devopts,
265                                                   ARRAY_SIZE(devopts),
266                                                   sizeof(uint32_t));
267                 break;
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"),
271                                                  samplerates,
272                                                  ARRAY_SIZE(samplerates),
273                                                  sizeof(uint64_t));
274                 g_variant_builder_add(&gvb, "{sv}", "samplerate-steps", gvar);
275                 *data = g_variant_builder_end(&gvb);
276                 break;
277         default:
278                 return SR_ERR_NA;
279         }
280
281         return ret;
282 }
283
284 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
285 {
286         struct dev_context *devc;
287
288         (void)cb_data;
289
290         if (sdi->status != SR_ST_ACTIVE)
291                 return SR_ERR_DEV_CLOSED;
292
293         devc = sdi->priv;
294         devc->samples_read = 0;
295
296         if (pipe(devc->pipe_fds)) {
297                 sr_err("Error setting up pipe");
298                 return SR_ERR;
299         }
300
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);
305
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);
309
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();
313
314         return SR_OK;
315 }
316
317 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
318 {
319         struct sr_datafeed_packet packet;
320         struct dev_context *devc;
321
322         (void)cb_data;
323
324         devc = sdi->priv;
325
326         if (sdi->status != SR_ST_ACTIVE)
327                 return SR_ERR_DEV_CLOSED;
328
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;
333
334         /* Send last packet. */
335         packet.type = SR_DF_END;
336         sr_session_send(sdi, &packet);
337
338         return SR_OK;
339 }
340
341 SR_PRIV struct sr_dev_driver baylibre_acme_driver_info = {
342         .name = "baylibre-acme",
343         .longname = "BayLibre ACME (Another Cute Measurement Equipment)",
344         .api_version = 1,
345         .init = init,
346         .cleanup = cleanup,
347         .scan = scan,
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,
357         .priv = NULL,
358 };