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