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