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