]>
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 | */ | |
98fec29e | 118 | if (!sdi->channel_groups) |
380ee96f BG |
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; | |
6b80b80d BG |
231 | break; |
232 | case SR_CONF_LIMIT_MSEC: | |
233 | devc->limit_msec = g_variant_get_uint64(data) * 1000; | |
234 | devc->limit_samples = 0; | |
6b80b80d BG |
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; | |
6b80b80d | 244 | break; |
61f2b7f7 BG |
245 | case SR_CONF_PROBE_FACTOR: |
246 | if (!cg) | |
247 | return SR_ERR_CHANNEL_GROUP; | |
248 | ret = bl_acme_set_shunt(cg, g_variant_get_uint64(data)); | |
249 | break; | |
740ad48a BG |
250 | case SR_CONF_POWER_OFF: |
251 | if (!cg) | |
252 | return SR_ERR_CHANNEL_GROUP; | |
253 | ret = bl_acme_set_power_off(cg, g_variant_get_boolean(data)); | |
254 | break; | |
dfaee1de BG |
255 | default: |
256 | ret = SR_ERR_NA; | |
257 | } | |
258 | ||
259 | return ret; | |
260 | } | |
261 | ||
6b80b80d BG |
262 | static int config_list(uint32_t key, GVariant **data, |
263 | const struct sr_dev_inst *sdi, | |
264 | const struct sr_channel_group *cg) | |
dfaee1de | 265 | { |
6b80b80d BG |
266 | GVariant *gvar; |
267 | GVariantBuilder gvb; | |
dfaee1de BG |
268 | int ret; |
269 | ||
270 | (void)sdi; | |
dfaee1de BG |
271 | (void)cg; |
272 | ||
273 | ret = SR_OK; | |
5fabeeac BG |
274 | if (!cg) { |
275 | switch (key) { | |
276 | case SR_CONF_DEVICE_OPTIONS: | |
277 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
278 | devopts, ARRAY_SIZE(devopts), sizeof(uint32_t)); | |
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"), | |
283 | samplerates, ARRAY_SIZE(samplerates), sizeof(uint64_t)); | |
284 | g_variant_builder_add(&gvb, "{sv}", | |
285 | "samplerate-steps", gvar); | |
286 | *data = g_variant_builder_end(&gvb); | |
287 | break; | |
288 | default: | |
289 | return SR_ERR_NA; | |
290 | } | |
291 | } else { | |
292 | switch (key) { | |
293 | case SR_CONF_DEVICE_OPTIONS: | |
294 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
295 | devopts_cg, ARRAY_SIZE(devopts_cg), sizeof(uint32_t)); | |
296 | break; | |
297 | default: | |
298 | return SR_ERR_NA; | |
299 | } | |
dfaee1de BG |
300 | } |
301 | ||
302 | return ret; | |
303 | } | |
304 | ||
6b80b80d | 305 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) |
dfaee1de | 306 | { |
6b80b80d BG |
307 | struct dev_context *devc; |
308 | ||
dfaee1de BG |
309 | (void)cb_data; |
310 | ||
311 | if (sdi->status != SR_ST_ACTIVE) | |
312 | return SR_ERR_DEV_CLOSED; | |
313 | ||
6b80b80d BG |
314 | devc = sdi->priv; |
315 | devc->samples_read = 0; | |
316 | ||
317 | if (pipe(devc->pipe_fds)) { | |
318 | sr_err("Error setting up pipe"); | |
319 | return SR_ERR; | |
320 | } | |
321 | ||
322 | devc->channel = g_io_channel_unix_new(devc->pipe_fds[0]); | |
323 | g_io_channel_set_flags(devc->channel, G_IO_FLAG_NONBLOCK, NULL); | |
324 | g_io_channel_set_encoding(devc->channel, NULL, NULL); | |
325 | g_io_channel_set_buffered(devc->channel, FALSE); | |
326 | ||
327 | sr_session_source_add_channel(sdi->session, devc->channel, | |
391e23a9 | 328 | G_IO_IN | G_IO_ERR, 1, bl_acme_receive_data, (void *)sdi); |
6b80b80d BG |
329 | |
330 | /* Send header packet to the session bus. */ | |
331 | std_session_send_df_header(sdi, LOG_PREFIX); | |
332 | devc->start_time = g_get_monotonic_time(); | |
dfaee1de BG |
333 | |
334 | return SR_OK; | |
335 | } | |
336 | ||
337 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) | |
338 | { | |
6b80b80d BG |
339 | struct sr_datafeed_packet packet; |
340 | struct dev_context *devc; | |
341 | ||
dfaee1de BG |
342 | (void)cb_data; |
343 | ||
6b80b80d BG |
344 | devc = sdi->priv; |
345 | ||
dfaee1de BG |
346 | if (sdi->status != SR_ST_ACTIVE) |
347 | return SR_ERR_DEV_CLOSED; | |
348 | ||
6b80b80d BG |
349 | sr_session_source_remove_channel(sdi->session, devc->channel); |
350 | g_io_channel_shutdown(devc->channel, FALSE, NULL); | |
351 | g_io_channel_unref(devc->channel); | |
352 | devc->channel = NULL; | |
353 | ||
354 | /* Send last packet. */ | |
355 | packet.type = SR_DF_END; | |
356 | sr_session_send(sdi, &packet); | |
dfaee1de BG |
357 | |
358 | return SR_OK; | |
359 | } | |
360 | ||
361 | SR_PRIV struct sr_dev_driver baylibre_acme_driver_info = { | |
362 | .name = "baylibre-acme", | |
6b80b80d | 363 | .longname = "BayLibre ACME (Another Cute Measurement Equipment)", |
dfaee1de BG |
364 | .api_version = 1, |
365 | .init = init, | |
366 | .cleanup = cleanup, | |
367 | .scan = scan, | |
368 | .dev_list = dev_list, | |
369 | .dev_clear = dev_clear, | |
370 | .config_get = config_get, | |
371 | .config_set = config_set, | |
372 | .config_list = config_list, | |
373 | .dev_open = dev_open, | |
374 | .dev_close = dev_close, | |
375 | .dev_acquisition_start = dev_acquisition_start, | |
376 | .dev_acquisition_stop = dev_acquisition_stop, | |
377 | .priv = NULL, | |
378 | }; |