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