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