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