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