]> sigrok.org Git - libsigrok.git/blame - src/hardware/baylibre-acme/api.c
Drop SR_CONF_SET flag from SR_CONF_CONTINUOUS options
[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
6ec6c43b 20#include <config.h>
dfaee1de 21#include "protocol.h"
a0648b1a
DL
22#include <time.h>
23#include <sys/timerfd.h>
dfaee1de
BG
24
25SR_PRIV struct sr_dev_driver baylibre_acme_driver_info;
dfaee1de 26
6b80b80d 27static const uint32_t devopts[] = {
e91bb0a6 28 SR_CONF_CONTINUOUS,
6b80b80d
BG
29 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
30 SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
31 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
5fabeeac
BG
32};
33
1fe04eb8
BG
34/*
35 * Currently there are two channel-group/probe options for ACME:
36 * - SR_CONF_PROBE_FACTOR - allows to modify current shunt resistance
37 * calibration
38 * - SR_CONF_POWER_OFF - allows to remotely cut-off/restore power to
39 * measured devices
40 *
41 * They are not static - we have to check each probe's capabilities in
42 * config_list().
43 */
44#define MAX_DEVOPTS_CG 2
45#define HAS_PROBE_FACTOR (SR_CONF_PROBE_FACTOR | SR_CONF_GET | SR_CONF_SET)
46#define HAS_POWER_OFF (SR_CONF_POWER_OFF | SR_CONF_GET | SR_CONF_SET)
6b80b80d 47
391e23a9 48#define MAX_SAMPLE_RATE 500 /* In Hz */
6b80b80d
BG
49
50static const uint64_t samplerates[] = {
51 SR_HZ(1),
52 SR_HZ(MAX_SAMPLE_RATE),
53 SR_HZ(1),
54};
55
4f840ce9 56static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
dfaee1de
BG
57{
58 return std_init(sr_ctx, di, LOG_PREFIX);
59}
60
4f840ce9 61static GSList *scan(struct sr_dev_driver *di, GSList *options)
dfaee1de
BG
62{
63 struct drv_context *drvc;
6b80b80d
BG
64 struct dev_context *devc;
65 struct sr_dev_inst *sdi;
dfaee1de 66 GSList *devices;
6b80b80d
BG
67 gboolean status;
68 int i;
dfaee1de
BG
69
70 (void)options;
71
41812aca 72 drvc = di->context;
6b80b80d 73 devices = NULL;
dfaee1de 74
6b80b80d
BG
75 devc = g_malloc0(sizeof(struct dev_context));
76 devc->samplerate = SR_HZ(10);
77
78 sdi = g_malloc0(sizeof(struct sr_dev_inst));
79 sdi->status = SR_ST_INACTIVE;
3d14ce49
UH
80 sdi->vendor = g_strdup("BayLibre");
81 sdi->model = g_strdup("ACME");
6b80b80d
BG
82 sdi->driver = di;
83 sdi->priv = devc;
84
85 status = bl_acme_is_sane();
86 if (!status)
87 goto err_out;
88
89 /*
90 * Iterate over all ACME connectors and check if any probes
91 * are present.
92 */
93 for (i = 0; i < MAX_PROBES; i++) {
94 /*
95 * First check if there's an energy probe on this connector. If
96 * not, and we're already at the fifth probe - see if we can
97 * detect a temperature probe.
98 */
99 status = bl_acme_detect_probe(bl_acme_get_enrg_addr(i),
391e23a9 100 PROBE_NUM(i), ENRG_PROBE_NAME);
6b80b80d
BG
101 if (status) {
102 /* Energy probe detected. */
391e23a9
UH
103 status = bl_acme_register_probe(sdi, PROBE_ENRG,
104 bl_acme_get_enrg_addr(i), PROBE_NUM(i));
6b80b80d
BG
105 if (!status) {
106 sr_err("Error registering power probe %d",
107 PROBE_NUM(i));
108 continue;
109 }
6b80b80d
BG
110 } else if (i >= TEMP_PRB_START_INDEX) {
111 status = bl_acme_detect_probe(bl_acme_get_temp_addr(i),
391e23a9 112 PROBE_NUM(i), TEMP_PROBE_NAME);
6b80b80d
BG
113 if (status) {
114 /* Temperature probe detected. */
115 status = bl_acme_register_probe(sdi,PROBE_TEMP,
391e23a9 116 bl_acme_get_temp_addr(i), PROBE_NUM(i));
6b80b80d
BG
117 if (!status) {
118 sr_err("Error registering temp "
119 "probe %d", PROBE_NUM(i));
120 continue;
121 }
122 }
6b80b80d
BG
123 }
124 }
125
380ee96f
BG
126 /*
127 * Let's assume there's no ACME device present if no probe
128 * has been registered.
129 */
98fec29e 130 if (!sdi->channel_groups)
380ee96f
BG
131 goto err_out;
132
6b80b80d
BG
133 devices = g_slist_append(devices, sdi);
134 drvc->instances = g_slist_append(drvc->instances, sdi);
dfaee1de
BG
135
136 return devices;
6b80b80d
BG
137
138err_out:
139 g_free(devc);
140 sr_dev_inst_free(sdi);
141
142 return NULL;
dfaee1de
BG
143}
144
dfaee1de
BG
145static int dev_open(struct sr_dev_inst *sdi)
146{
147 (void)sdi;
148
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
dfaee1de
BG
158 sdi->status = SR_ST_INACTIVE;
159
160 return SR_OK;
161}
162
6b80b80d
BG
163static int config_get(uint32_t key, GVariant **data,
164 const struct sr_dev_inst *sdi,
165 const struct sr_channel_group *cg)
dfaee1de 166{
6b80b80d 167 struct dev_context *devc;
dfaee1de 168 int ret;
61f2b7f7 169 uint64_t shunt;
740ad48a 170 gboolean power_off;
dfaee1de 171
6b80b80d
BG
172 devc = sdi->priv;
173
dfaee1de
BG
174 ret = SR_OK;
175 switch (key) {
6b80b80d
BG
176 case SR_CONF_LIMIT_SAMPLES:
177 *data = g_variant_new_uint64(devc->limit_samples);
178 break;
179 case SR_CONF_LIMIT_MSEC:
180 *data = g_variant_new_uint64(devc->limit_msec);
181 break;
182 case SR_CONF_SAMPLERATE:
183 *data = g_variant_new_uint64(devc->samplerate);
184 break;
61f2b7f7
BG
185 case SR_CONF_PROBE_FACTOR:
186 if (!cg)
187 return SR_ERR_CHANNEL_GROUP;
188 ret = bl_acme_get_shunt(cg, &shunt);
189 if (ret == SR_OK)
190 *data = g_variant_new_uint64(shunt);
191 break;
740ad48a
BG
192 case SR_CONF_POWER_OFF:
193 if (!cg)
194 return SR_ERR_CHANNEL_GROUP;
195 ret = bl_acme_read_power_state(cg, &power_off);
196 if (ret == SR_OK)
197 *data = g_variant_new_boolean(power_off);
198 break;
dfaee1de
BG
199 default:
200 return SR_ERR_NA;
201 }
202
203 return ret;
204}
205
6b80b80d
BG
206static int config_set(uint32_t key, GVariant *data,
207 const struct sr_dev_inst *sdi,
208 const struct sr_channel_group *cg)
dfaee1de 209{
6b80b80d
BG
210 struct dev_context *devc;
211 uint64_t samplerate;
dfaee1de
BG
212 int ret;
213
dfaee1de
BG
214 if (sdi->status != SR_ST_ACTIVE)
215 return SR_ERR_DEV_CLOSED;
216
6b80b80d
BG
217 devc = sdi->priv;
218
dfaee1de
BG
219 ret = SR_OK;
220 switch (key) {
6b80b80d
BG
221 case SR_CONF_LIMIT_SAMPLES:
222 devc->limit_samples = g_variant_get_uint64(data);
223 devc->limit_msec = 0;
6b80b80d
BG
224 break;
225 case SR_CONF_LIMIT_MSEC:
226 devc->limit_msec = g_variant_get_uint64(data) * 1000;
227 devc->limit_samples = 0;
6b80b80d
BG
228 break;
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;
234 break;
235 }
236 devc->samplerate = samplerate;
7c91c22a 237 bl_acme_maybe_set_update_interval(sdi, samplerate);
6b80b80d 238 break;
61f2b7f7
BG
239 case SR_CONF_PROBE_FACTOR:
240 if (!cg)
241 return SR_ERR_CHANNEL_GROUP;
242 ret = bl_acme_set_shunt(cg, g_variant_get_uint64(data));
243 break;
740ad48a
BG
244 case SR_CONF_POWER_OFF:
245 if (!cg)
246 return SR_ERR_CHANNEL_GROUP;
247 ret = bl_acme_set_power_off(cg, g_variant_get_boolean(data));
248 break;
dfaee1de
BG
249 default:
250 ret = SR_ERR_NA;
251 }
252
253 return ret;
254}
255
6b80b80d
BG
256static int config_list(uint32_t key, GVariant **data,
257 const struct sr_dev_inst *sdi,
258 const struct sr_channel_group *cg)
dfaee1de 259{
1fe04eb8 260 uint32_t devopts_cg[MAX_DEVOPTS_CG];
6b80b80d
BG
261 GVariant *gvar;
262 GVariantBuilder gvb;
1fe04eb8 263 int ret, num_devopts_cg = 0;
dfaee1de
BG
264
265 (void)sdi;
dfaee1de
BG
266 (void)cg;
267
268 ret = SR_OK;
5fabeeac
BG
269 if (!cg) {
270 switch (key) {
271 case SR_CONF_DEVICE_OPTIONS:
272 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
273 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
274 break;
275 case SR_CONF_SAMPLERATE:
276 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
277 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
278 samplerates, ARRAY_SIZE(samplerates), sizeof(uint64_t));
279 g_variant_builder_add(&gvb, "{sv}",
280 "samplerate-steps", gvar);
281 *data = g_variant_builder_end(&gvb);
282 break;
283 default:
284 return SR_ERR_NA;
285 }
286 } else {
287 switch (key) {
288 case SR_CONF_DEVICE_OPTIONS:
1fe04eb8
BG
289 if (bl_acme_get_probe_type(cg) == PROBE_ENRG)
290 devopts_cg[num_devopts_cg++] = HAS_PROBE_FACTOR;
291 if (bl_acme_probe_has_pws(cg))
292 devopts_cg[num_devopts_cg++] = HAS_POWER_OFF;
293
5fabeeac 294 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
1fe04eb8 295 devopts_cg, num_devopts_cg, sizeof(uint32_t));
5fabeeac
BG
296 break;
297 default:
298 return SR_ERR_NA;
299 }
dfaee1de
BG
300 }
301
302 return ret;
303}
304
4e88b86c
DL
305static void dev_acquisition_close(const struct sr_dev_inst *sdi)
306{
307 GSList *chl;
308 struct sr_channel *ch;
309
310 for (chl = sdi->channels; chl; chl = chl->next) {
311 ch = chl->data;
312 bl_acme_close_channel(ch);
313 }
314}
315
316static int dev_acquisition_open(const struct sr_dev_inst *sdi)
317{
318 GSList *chl;
319 struct sr_channel *ch;
320
321 for (chl = sdi->channels; chl; chl = chl->next) {
322 ch = chl->data;
323 if (bl_acme_open_channel(ch)) {
324 sr_err("Error opening channel %s", ch->name);
325 dev_acquisition_close(sdi);
326 return SR_ERR;
327 }
328 }
329
330 return 0;
331}
332
695dc859 333static int dev_acquisition_start(const struct sr_dev_inst *sdi)
dfaee1de 334{
6b80b80d 335 struct dev_context *devc;
a0648b1a
DL
336 struct itimerspec tspec = {
337 .it_interval = { 0, 0 },
338 .it_value = { 0, 0 }
339 };
6b80b80d 340
dfaee1de
BG
341 if (sdi->status != SR_ST_ACTIVE)
342 return SR_ERR_DEV_CLOSED;
343
4e88b86c
DL
344 if (dev_acquisition_open(sdi))
345 return SR_ERR;
346
6b80b80d
BG
347 devc = sdi->priv;
348 devc->samples_read = 0;
a0648b1a
DL
349 devc->samples_missed = 0;
350 devc->timer_fd = timerfd_create(CLOCK_MONOTONIC, 0);
351 if (devc->timer_fd < 0) {
352 sr_err("Error creating timer fd");
353 return SR_ERR;
354 }
6b80b80d 355
a0648b1a 356 tspec.it_interval.tv_sec = 0;
7a1a3a36 357 tspec.it_interval.tv_nsec = SR_HZ_TO_NS(devc->samplerate);
a0648b1a
DL
358 tspec.it_value = tspec.it_interval;
359
360 if (timerfd_settime(devc->timer_fd, 0, &tspec, NULL)) {
361 sr_err("Failed to set timer");
362 close(devc->timer_fd);
6b80b80d
BG
363 return SR_ERR;
364 }
365
a0648b1a 366 devc->channel = g_io_channel_unix_new(devc->timer_fd);
6b80b80d
BG
367 g_io_channel_set_flags(devc->channel, G_IO_FLAG_NONBLOCK, NULL);
368 g_io_channel_set_encoding(devc->channel, NULL, NULL);
369 g_io_channel_set_buffered(devc->channel, FALSE);
370
371 sr_session_source_add_channel(sdi->session, devc->channel,
a0648b1a 372 G_IO_IN | G_IO_ERR, 1000, bl_acme_receive_data, (void *)sdi);
6b80b80d 373
6b80b80d
BG
374 std_session_send_df_header(sdi, LOG_PREFIX);
375 devc->start_time = g_get_monotonic_time();
dfaee1de
BG
376
377 return SR_OK;
378}
379
695dc859 380static int dev_acquisition_stop(struct sr_dev_inst *sdi)
dfaee1de 381{
6b80b80d
BG
382 struct dev_context *devc;
383
6b80b80d
BG
384 devc = sdi->priv;
385
dfaee1de
BG
386 if (sdi->status != SR_ST_ACTIVE)
387 return SR_ERR_DEV_CLOSED;
388
f9b0ab6b 389 dev_acquisition_close(sdi);
6b80b80d
BG
390 sr_session_source_remove_channel(sdi->session, devc->channel);
391 g_io_channel_shutdown(devc->channel, FALSE, NULL);
392 g_io_channel_unref(devc->channel);
393 devc->channel = NULL;
394
3be42bc2 395 std_session_send_df_end(sdi, LOG_PREFIX);
dfaee1de 396
a0648b1a 397 if (devc->samples_missed > 0)
6433156c 398 sr_warn("%" PRIu64 " samples missed", devc->samples_missed);
a0648b1a 399
dfaee1de
BG
400 return SR_OK;
401}
402
403SR_PRIV struct sr_dev_driver baylibre_acme_driver_info = {
404 .name = "baylibre-acme",
6b80b80d 405 .longname = "BayLibre ACME (Another Cute Measurement Equipment)",
dfaee1de
BG
406 .api_version = 1,
407 .init = init,
700d6b64 408 .cleanup = std_cleanup,
dfaee1de 409 .scan = scan,
c01bf34c 410 .dev_list = std_dev_list,
dfaee1de
BG
411 .config_get = config_get,
412 .config_set = config_set,
413 .config_list = config_list,
414 .dev_open = dev_open,
415 .dev_close = dev_close,
416 .dev_acquisition_start = dev_acquisition_start,
417 .dev_acquisition_stop = dev_acquisition_stop,
41812aca 418 .context = NULL,
dfaee1de 419};