]>
Commit | Line | Data |
---|---|---|
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 <config.h> | |
21 | #include "protocol.h" | |
22 | #include <time.h> | |
23 | #include <sys/timerfd.h> | |
24 | ||
25 | SR_PRIV struct sr_dev_driver baylibre_acme_driver_info; | |
26 | ||
27 | static const uint32_t devopts[] = { | |
28 | SR_CONF_CONTINUOUS | SR_CONF_SET, | |
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, | |
32 | }; | |
33 | ||
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) | |
47 | ||
48 | #define MAX_SAMPLE_RATE 500 /* In Hz */ | |
49 | ||
50 | static const uint64_t samplerates[] = { | |
51 | SR_HZ(1), | |
52 | SR_HZ(MAX_SAMPLE_RATE), | |
53 | SR_HZ(1), | |
54 | }; | |
55 | ||
56 | static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx) | |
57 | { | |
58 | return std_init(sr_ctx, di, LOG_PREFIX); | |
59 | } | |
60 | ||
61 | static GSList *scan(struct sr_dev_driver *di, GSList *options) | |
62 | { | |
63 | struct drv_context *drvc; | |
64 | struct dev_context *devc; | |
65 | struct sr_dev_inst *sdi; | |
66 | GSList *devices; | |
67 | gboolean status; | |
68 | int i; | |
69 | ||
70 | (void)options; | |
71 | ||
72 | drvc = di->context; | |
73 | devices = NULL; | |
74 | ||
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; | |
80 | sdi->vendor = g_strdup("BayLibre"); | |
81 | sdi->model = g_strdup("ACME"); | |
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), | |
100 | PROBE_NUM(i), ENRG_PROBE_NAME); | |
101 | if (status) { | |
102 | /* Energy probe detected. */ | |
103 | status = bl_acme_register_probe(sdi, PROBE_ENRG, | |
104 | bl_acme_get_enrg_addr(i), PROBE_NUM(i)); | |
105 | if (!status) { | |
106 | sr_err("Error registering power probe %d", | |
107 | PROBE_NUM(i)); | |
108 | continue; | |
109 | } | |
110 | } else if (i >= TEMP_PRB_START_INDEX) { | |
111 | status = bl_acme_detect_probe(bl_acme_get_temp_addr(i), | |
112 | PROBE_NUM(i), TEMP_PROBE_NAME); | |
113 | if (status) { | |
114 | /* Temperature probe detected. */ | |
115 | status = bl_acme_register_probe(sdi,PROBE_TEMP, | |
116 | bl_acme_get_temp_addr(i), PROBE_NUM(i)); | |
117 | if (!status) { | |
118 | sr_err("Error registering temp " | |
119 | "probe %d", PROBE_NUM(i)); | |
120 | continue; | |
121 | } | |
122 | } | |
123 | } | |
124 | } | |
125 | ||
126 | /* | |
127 | * Let's assume there's no ACME device present if no probe | |
128 | * has been registered. | |
129 | */ | |
130 | if (!sdi->channel_groups) | |
131 | goto err_out; | |
132 | ||
133 | devices = g_slist_append(devices, sdi); | |
134 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
135 | ||
136 | return devices; | |
137 | ||
138 | err_out: | |
139 | g_free(devc); | |
140 | sr_dev_inst_free(sdi); | |
141 | ||
142 | return NULL; | |
143 | } | |
144 | ||
145 | static GSList *dev_list(const struct sr_dev_driver *di) | |
146 | { | |
147 | return ((struct drv_context *)(di->context))->instances; | |
148 | } | |
149 | ||
150 | static int dev_clear(const struct sr_dev_driver *di) | |
151 | { | |
152 | return std_dev_clear(di, NULL); | |
153 | } | |
154 | ||
155 | static int dev_open(struct sr_dev_inst *sdi) | |
156 | { | |
157 | (void)sdi; | |
158 | ||
159 | /* Nothing to do here. */ | |
160 | sdi->status = SR_ST_ACTIVE; | |
161 | ||
162 | return SR_OK; | |
163 | } | |
164 | ||
165 | static int dev_close(struct sr_dev_inst *sdi) | |
166 | { | |
167 | (void)sdi; | |
168 | ||
169 | /* Nothing to do here. */ | |
170 | sdi->status = SR_ST_INACTIVE; | |
171 | ||
172 | return SR_OK; | |
173 | } | |
174 | ||
175 | static int cleanup(const struct sr_dev_driver *di) | |
176 | { | |
177 | dev_clear(di); | |
178 | ||
179 | return SR_OK; | |
180 | } | |
181 | ||
182 | static int config_get(uint32_t key, GVariant **data, | |
183 | const struct sr_dev_inst *sdi, | |
184 | const struct sr_channel_group *cg) | |
185 | { | |
186 | struct dev_context *devc; | |
187 | int ret; | |
188 | uint64_t shunt; | |
189 | gboolean power_off; | |
190 | ||
191 | devc = sdi->priv; | |
192 | ||
193 | ret = SR_OK; | |
194 | switch (key) { | |
195 | case SR_CONF_LIMIT_SAMPLES: | |
196 | *data = g_variant_new_uint64(devc->limit_samples); | |
197 | break; | |
198 | case SR_CONF_LIMIT_MSEC: | |
199 | *data = g_variant_new_uint64(devc->limit_msec); | |
200 | break; | |
201 | case SR_CONF_SAMPLERATE: | |
202 | *data = g_variant_new_uint64(devc->samplerate); | |
203 | break; | |
204 | case SR_CONF_PROBE_FACTOR: | |
205 | if (!cg) | |
206 | return SR_ERR_CHANNEL_GROUP; | |
207 | ret = bl_acme_get_shunt(cg, &shunt); | |
208 | if (ret == SR_OK) | |
209 | *data = g_variant_new_uint64(shunt); | |
210 | break; | |
211 | case SR_CONF_POWER_OFF: | |
212 | if (!cg) | |
213 | return SR_ERR_CHANNEL_GROUP; | |
214 | ret = bl_acme_read_power_state(cg, &power_off); | |
215 | if (ret == SR_OK) | |
216 | *data = g_variant_new_boolean(power_off); | |
217 | break; | |
218 | default: | |
219 | return SR_ERR_NA; | |
220 | } | |
221 | ||
222 | return ret; | |
223 | } | |
224 | ||
225 | static int config_set(uint32_t key, GVariant *data, | |
226 | const struct sr_dev_inst *sdi, | |
227 | const struct sr_channel_group *cg) | |
228 | { | |
229 | struct dev_context *devc; | |
230 | uint64_t samplerate; | |
231 | int ret; | |
232 | ||
233 | if (sdi->status != SR_ST_ACTIVE) | |
234 | return SR_ERR_DEV_CLOSED; | |
235 | ||
236 | devc = sdi->priv; | |
237 | ||
238 | ret = SR_OK; | |
239 | switch (key) { | |
240 | case SR_CONF_LIMIT_SAMPLES: | |
241 | devc->limit_samples = g_variant_get_uint64(data); | |
242 | devc->limit_msec = 0; | |
243 | break; | |
244 | case SR_CONF_LIMIT_MSEC: | |
245 | devc->limit_msec = g_variant_get_uint64(data) * 1000; | |
246 | devc->limit_samples = 0; | |
247 | break; | |
248 | case SR_CONF_SAMPLERATE: | |
249 | samplerate = g_variant_get_uint64(data); | |
250 | if (samplerate > MAX_SAMPLE_RATE) { | |
251 | sr_err("Maximum sample rate is %d", MAX_SAMPLE_RATE); | |
252 | ret = SR_ERR_SAMPLERATE; | |
253 | break; | |
254 | } | |
255 | devc->samplerate = samplerate; | |
256 | bl_acme_maybe_set_update_interval(sdi, samplerate); | |
257 | break; | |
258 | case SR_CONF_PROBE_FACTOR: | |
259 | if (!cg) | |
260 | return SR_ERR_CHANNEL_GROUP; | |
261 | ret = bl_acme_set_shunt(cg, g_variant_get_uint64(data)); | |
262 | break; | |
263 | case SR_CONF_POWER_OFF: | |
264 | if (!cg) | |
265 | return SR_ERR_CHANNEL_GROUP; | |
266 | ret = bl_acme_set_power_off(cg, g_variant_get_boolean(data)); | |
267 | break; | |
268 | default: | |
269 | ret = SR_ERR_NA; | |
270 | } | |
271 | ||
272 | return ret; | |
273 | } | |
274 | ||
275 | static int config_list(uint32_t key, GVariant **data, | |
276 | const struct sr_dev_inst *sdi, | |
277 | const struct sr_channel_group *cg) | |
278 | { | |
279 | uint32_t devopts_cg[MAX_DEVOPTS_CG]; | |
280 | GVariant *gvar; | |
281 | GVariantBuilder gvb; | |
282 | int ret, num_devopts_cg = 0; | |
283 | ||
284 | (void)sdi; | |
285 | (void)cg; | |
286 | ||
287 | ret = SR_OK; | |
288 | if (!cg) { | |
289 | switch (key) { | |
290 | case SR_CONF_DEVICE_OPTIONS: | |
291 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
292 | devopts, ARRAY_SIZE(devopts), sizeof(uint32_t)); | |
293 | break; | |
294 | case SR_CONF_SAMPLERATE: | |
295 | g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}")); | |
296 | gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"), | |
297 | samplerates, ARRAY_SIZE(samplerates), sizeof(uint64_t)); | |
298 | g_variant_builder_add(&gvb, "{sv}", | |
299 | "samplerate-steps", gvar); | |
300 | *data = g_variant_builder_end(&gvb); | |
301 | break; | |
302 | default: | |
303 | return SR_ERR_NA; | |
304 | } | |
305 | } else { | |
306 | switch (key) { | |
307 | case SR_CONF_DEVICE_OPTIONS: | |
308 | if (bl_acme_get_probe_type(cg) == PROBE_ENRG) | |
309 | devopts_cg[num_devopts_cg++] = HAS_PROBE_FACTOR; | |
310 | if (bl_acme_probe_has_pws(cg)) | |
311 | devopts_cg[num_devopts_cg++] = HAS_POWER_OFF; | |
312 | ||
313 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
314 | devopts_cg, num_devopts_cg, sizeof(uint32_t)); | |
315 | break; | |
316 | default: | |
317 | return SR_ERR_NA; | |
318 | } | |
319 | } | |
320 | ||
321 | return ret; | |
322 | } | |
323 | ||
324 | static void dev_acquisition_close(const struct sr_dev_inst *sdi) | |
325 | { | |
326 | GSList *chl; | |
327 | struct sr_channel *ch; | |
328 | ||
329 | for (chl = sdi->channels; chl; chl = chl->next) { | |
330 | ch = chl->data; | |
331 | bl_acme_close_channel(ch); | |
332 | } | |
333 | } | |
334 | ||
335 | static int dev_acquisition_open(const struct sr_dev_inst *sdi) | |
336 | { | |
337 | GSList *chl; | |
338 | struct sr_channel *ch; | |
339 | ||
340 | for (chl = sdi->channels; chl; chl = chl->next) { | |
341 | ch = chl->data; | |
342 | if (bl_acme_open_channel(ch)) { | |
343 | sr_err("Error opening channel %s", ch->name); | |
344 | dev_acquisition_close(sdi); | |
345 | return SR_ERR; | |
346 | } | |
347 | } | |
348 | ||
349 | return 0; | |
350 | } | |
351 | ||
352 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) | |
353 | { | |
354 | struct dev_context *devc; | |
355 | struct itimerspec tspec = { | |
356 | .it_interval = { 0, 0 }, | |
357 | .it_value = { 0, 0 } | |
358 | }; | |
359 | ||
360 | (void)cb_data; | |
361 | ||
362 | if (sdi->status != SR_ST_ACTIVE) | |
363 | return SR_ERR_DEV_CLOSED; | |
364 | ||
365 | if (dev_acquisition_open(sdi)) | |
366 | return SR_ERR; | |
367 | ||
368 | devc = sdi->priv; | |
369 | devc->samples_read = 0; | |
370 | devc->samples_missed = 0; | |
371 | devc->timer_fd = timerfd_create(CLOCK_MONOTONIC, 0); | |
372 | if (devc->timer_fd < 0) { | |
373 | sr_err("Error creating timer fd"); | |
374 | return SR_ERR; | |
375 | } | |
376 | ||
377 | tspec.it_interval.tv_sec = 0; | |
378 | tspec.it_interval.tv_nsec = SR_HZ_TO_NS(devc->samplerate); | |
379 | tspec.it_value = tspec.it_interval; | |
380 | ||
381 | if (timerfd_settime(devc->timer_fd, 0, &tspec, NULL)) { | |
382 | sr_err("Failed to set timer"); | |
383 | close(devc->timer_fd); | |
384 | return SR_ERR; | |
385 | } | |
386 | ||
387 | devc->channel = g_io_channel_unix_new(devc->timer_fd); | |
388 | g_io_channel_set_flags(devc->channel, G_IO_FLAG_NONBLOCK, NULL); | |
389 | g_io_channel_set_encoding(devc->channel, NULL, NULL); | |
390 | g_io_channel_set_buffered(devc->channel, FALSE); | |
391 | ||
392 | sr_session_source_add_channel(sdi->session, devc->channel, | |
393 | G_IO_IN | G_IO_ERR, 1000, bl_acme_receive_data, (void *)sdi); | |
394 | ||
395 | std_session_send_df_header(sdi, LOG_PREFIX); | |
396 | devc->start_time = g_get_monotonic_time(); | |
397 | ||
398 | return SR_OK; | |
399 | } | |
400 | ||
401 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) | |
402 | { | |
403 | struct dev_context *devc; | |
404 | ||
405 | (void)cb_data; | |
406 | ||
407 | devc = sdi->priv; | |
408 | ||
409 | if (sdi->status != SR_ST_ACTIVE) | |
410 | return SR_ERR_DEV_CLOSED; | |
411 | ||
412 | dev_acquisition_close(sdi); | |
413 | sr_session_source_remove_channel(sdi->session, devc->channel); | |
414 | g_io_channel_shutdown(devc->channel, FALSE, NULL); | |
415 | g_io_channel_unref(devc->channel); | |
416 | devc->channel = NULL; | |
417 | ||
418 | std_session_send_df_end(sdi, LOG_PREFIX); | |
419 | ||
420 | if (devc->samples_missed > 0) | |
421 | sr_warn("%" PRIu64 " samples missed", devc->samples_missed); | |
422 | ||
423 | return SR_OK; | |
424 | } | |
425 | ||
426 | SR_PRIV struct sr_dev_driver baylibre_acme_driver_info = { | |
427 | .name = "baylibre-acme", | |
428 | .longname = "BayLibre ACME (Another Cute Measurement Equipment)", | |
429 | .api_version = 1, | |
430 | .init = init, | |
431 | .cleanup = cleanup, | |
432 | .scan = scan, | |
433 | .dev_list = dev_list, | |
434 | .dev_clear = dev_clear, | |
435 | .config_get = config_get, | |
436 | .config_set = config_set, | |
437 | .config_list = config_list, | |
438 | .dev_open = dev_open, | |
439 | .dev_close = dev_close, | |
440 | .dev_acquisition_start = dev_acquisition_start, | |
441 | .dev_acquisition_stop = dev_acquisition_stop, | |
442 | .context = NULL, | |
443 | }; |