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