]>
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 | ||
6ec6c43b | 20 | #include <config.h> |
dfaee1de | 21 | #include "protocol.h" |
a0648b1a DL |
22 | #include <time.h> |
23 | #include <sys/timerfd.h> | |
dfaee1de | 24 | |
05199c0a UH |
25 | static const uint32_t drvopts[] = { |
26 | SR_CONF_THERMOMETER, | |
c8e789fa | 27 | SR_CONF_POWERMETER, |
05199c0a UH |
28 | }; |
29 | ||
6b80b80d | 30 | static const uint32_t devopts[] = { |
e91bb0a6 | 31 | SR_CONF_CONTINUOUS, |
6b80b80d BG |
32 | SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET, |
33 | SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET, | |
34 | SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
5fabeeac BG |
35 | }; |
36 | ||
1fe04eb8 BG |
37 | /* |
38 | * Currently there are two channel-group/probe options for ACME: | |
39 | * - SR_CONF_PROBE_FACTOR - allows to modify current shunt resistance | |
40 | * calibration | |
41 | * - SR_CONF_POWER_OFF - allows to remotely cut-off/restore power to | |
42 | * measured devices | |
43 | * | |
44 | * They are not static - we have to check each probe's capabilities in | |
45 | * config_list(). | |
46 | */ | |
47 | #define MAX_DEVOPTS_CG 2 | |
48 | #define HAS_PROBE_FACTOR (SR_CONF_PROBE_FACTOR | SR_CONF_GET | SR_CONF_SET) | |
49 | #define HAS_POWER_OFF (SR_CONF_POWER_OFF | SR_CONF_GET | SR_CONF_SET) | |
6b80b80d | 50 | |
391e23a9 | 51 | #define MAX_SAMPLE_RATE 500 /* In Hz */ |
6b80b80d BG |
52 | |
53 | static const uint64_t samplerates[] = { | |
54 | SR_HZ(1), | |
55 | SR_HZ(MAX_SAMPLE_RATE), | |
56 | SR_HZ(1), | |
57 | }; | |
58 | ||
4f840ce9 | 59 | static GSList *scan(struct sr_dev_driver *di, GSList *options) |
dfaee1de | 60 | { |
6b80b80d BG |
61 | struct dev_context *devc; |
62 | struct sr_dev_inst *sdi; | |
6b80b80d BG |
63 | gboolean status; |
64 | int i; | |
dfaee1de BG |
65 | |
66 | (void)options; | |
67 | ||
6b80b80d BG |
68 | devc = g_malloc0(sizeof(struct dev_context)); |
69 | devc->samplerate = SR_HZ(10); | |
70 | ||
71 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
72 | sdi->status = SR_ST_INACTIVE; | |
3d14ce49 UH |
73 | sdi->vendor = g_strdup("BayLibre"); |
74 | sdi->model = g_strdup("ACME"); | |
6b80b80d BG |
75 | sdi->priv = devc; |
76 | ||
77 | status = bl_acme_is_sane(); | |
78 | if (!status) | |
79 | goto err_out; | |
80 | ||
81 | /* | |
82 | * Iterate over all ACME connectors and check if any probes | |
83 | * are present. | |
84 | */ | |
85 | for (i = 0; i < MAX_PROBES; i++) { | |
86 | /* | |
87 | * First check if there's an energy probe on this connector. If | |
88 | * not, and we're already at the fifth probe - see if we can | |
89 | * detect a temperature probe. | |
90 | */ | |
91 | status = bl_acme_detect_probe(bl_acme_get_enrg_addr(i), | |
391e23a9 | 92 | PROBE_NUM(i), ENRG_PROBE_NAME); |
6b80b80d BG |
93 | if (status) { |
94 | /* Energy probe detected. */ | |
391e23a9 UH |
95 | status = bl_acme_register_probe(sdi, PROBE_ENRG, |
96 | bl_acme_get_enrg_addr(i), PROBE_NUM(i)); | |
6b80b80d BG |
97 | if (!status) { |
98 | sr_err("Error registering power probe %d", | |
99 | PROBE_NUM(i)); | |
100 | continue; | |
101 | } | |
6b80b80d BG |
102 | } else if (i >= TEMP_PRB_START_INDEX) { |
103 | status = bl_acme_detect_probe(bl_acme_get_temp_addr(i), | |
391e23a9 | 104 | PROBE_NUM(i), TEMP_PROBE_NAME); |
6b80b80d BG |
105 | if (status) { |
106 | /* Temperature probe detected. */ | |
107 | status = bl_acme_register_probe(sdi,PROBE_TEMP, | |
391e23a9 | 108 | bl_acme_get_temp_addr(i), PROBE_NUM(i)); |
6b80b80d BG |
109 | if (!status) { |
110 | sr_err("Error registering temp " | |
111 | "probe %d", PROBE_NUM(i)); | |
112 | continue; | |
113 | } | |
114 | } | |
6b80b80d BG |
115 | } |
116 | } | |
117 | ||
380ee96f BG |
118 | /* |
119 | * Let's assume there's no ACME device present if no probe | |
120 | * has been registered. | |
121 | */ | |
98fec29e | 122 | if (!sdi->channel_groups) |
380ee96f BG |
123 | goto err_out; |
124 | ||
43376f33 | 125 | return std_scan_complete(di, g_slist_append(NULL, sdi)); |
6b80b80d BG |
126 | |
127 | err_out: | |
128 | g_free(devc); | |
129 | sr_dev_inst_free(sdi); | |
130 | ||
131 | return NULL; | |
dfaee1de BG |
132 | } |
133 | ||
6b80b80d | 134 | static int config_get(uint32_t key, GVariant **data, |
dd7a72ea | 135 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) |
dfaee1de | 136 | { |
6b80b80d | 137 | struct dev_context *devc; |
dfaee1de | 138 | int ret; |
61f2b7f7 | 139 | uint64_t shunt; |
740ad48a | 140 | gboolean power_off; |
dfaee1de | 141 | |
6b80b80d BG |
142 | devc = sdi->priv; |
143 | ||
dfaee1de BG |
144 | ret = SR_OK; |
145 | switch (key) { | |
6b80b80d | 146 | case SR_CONF_LIMIT_SAMPLES: |
6b80b80d | 147 | case SR_CONF_LIMIT_MSEC: |
d54a7c42 | 148 | ret = sr_sw_limits_config_get(&devc->limits, key, data); |
6b80b80d BG |
149 | break; |
150 | case SR_CONF_SAMPLERATE: | |
151 | *data = g_variant_new_uint64(devc->samplerate); | |
152 | break; | |
61f2b7f7 BG |
153 | case SR_CONF_PROBE_FACTOR: |
154 | if (!cg) | |
155 | return SR_ERR_CHANNEL_GROUP; | |
156 | ret = bl_acme_get_shunt(cg, &shunt); | |
157 | if (ret == SR_OK) | |
158 | *data = g_variant_new_uint64(shunt); | |
159 | break; | |
740ad48a BG |
160 | case SR_CONF_POWER_OFF: |
161 | if (!cg) | |
162 | return SR_ERR_CHANNEL_GROUP; | |
163 | ret = bl_acme_read_power_state(cg, &power_off); | |
164 | if (ret == SR_OK) | |
165 | *data = g_variant_new_boolean(power_off); | |
166 | break; | |
dfaee1de BG |
167 | default: |
168 | return SR_ERR_NA; | |
169 | } | |
170 | ||
171 | return ret; | |
172 | } | |
173 | ||
6b80b80d | 174 | static int config_set(uint32_t key, GVariant *data, |
dd7a72ea | 175 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) |
dfaee1de | 176 | { |
6b80b80d BG |
177 | struct dev_context *devc; |
178 | uint64_t samplerate; | |
dfaee1de BG |
179 | int ret; |
180 | ||
6b80b80d BG |
181 | devc = sdi->priv; |
182 | ||
dfaee1de BG |
183 | ret = SR_OK; |
184 | switch (key) { | |
6b80b80d | 185 | case SR_CONF_LIMIT_SAMPLES: |
6b80b80d | 186 | case SR_CONF_LIMIT_MSEC: |
d54a7c42 | 187 | ret = sr_sw_limits_config_set(&devc->limits, key, data); |
6b80b80d BG |
188 | break; |
189 | case SR_CONF_SAMPLERATE: | |
190 | samplerate = g_variant_get_uint64(data); | |
191 | if (samplerate > MAX_SAMPLE_RATE) { | |
192 | sr_err("Maximum sample rate is %d", MAX_SAMPLE_RATE); | |
193 | ret = SR_ERR_SAMPLERATE; | |
194 | break; | |
195 | } | |
196 | devc->samplerate = samplerate; | |
7c91c22a | 197 | bl_acme_maybe_set_update_interval(sdi, samplerate); |
6b80b80d | 198 | break; |
61f2b7f7 BG |
199 | case SR_CONF_PROBE_FACTOR: |
200 | if (!cg) | |
201 | return SR_ERR_CHANNEL_GROUP; | |
202 | ret = bl_acme_set_shunt(cg, g_variant_get_uint64(data)); | |
203 | break; | |
740ad48a BG |
204 | case SR_CONF_POWER_OFF: |
205 | if (!cg) | |
206 | return SR_ERR_CHANNEL_GROUP; | |
207 | ret = bl_acme_set_power_off(cg, g_variant_get_boolean(data)); | |
208 | break; | |
dfaee1de BG |
209 | default: |
210 | ret = SR_ERR_NA; | |
211 | } | |
212 | ||
213 | return ret; | |
214 | } | |
215 | ||
6b80b80d | 216 | static int config_list(uint32_t key, GVariant **data, |
dd7a72ea | 217 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) |
dfaee1de | 218 | { |
1fe04eb8 | 219 | uint32_t devopts_cg[MAX_DEVOPTS_CG]; |
a9010323 | 220 | int num_devopts_cg = 0; |
dfaee1de | 221 | |
5fabeeac BG |
222 | if (!cg) { |
223 | switch (key) { | |
224 | case SR_CONF_DEVICE_OPTIONS: | |
05199c0a | 225 | return STD_CONFIG_LIST(key, data, sdi, cg, NULL, drvopts, devopts); |
5fabeeac | 226 | case SR_CONF_SAMPLERATE: |
53012da6 | 227 | *data = std_gvar_samplerates_steps(ARRAY_AND_SIZE(samplerates)); |
5fabeeac BG |
228 | break; |
229 | default: | |
230 | return SR_ERR_NA; | |
231 | } | |
232 | } else { | |
233 | switch (key) { | |
234 | case SR_CONF_DEVICE_OPTIONS: | |
1fe04eb8 BG |
235 | if (bl_acme_get_probe_type(cg) == PROBE_ENRG) |
236 | devopts_cg[num_devopts_cg++] = HAS_PROBE_FACTOR; | |
237 | if (bl_acme_probe_has_pws(cg)) | |
238 | devopts_cg[num_devopts_cg++] = HAS_POWER_OFF; | |
239 | ||
105df674 | 240 | *data = std_gvar_array_u32(devopts_cg, num_devopts_cg); |
5fabeeac BG |
241 | break; |
242 | default: | |
243 | return SR_ERR_NA; | |
244 | } | |
dfaee1de BG |
245 | } |
246 | ||
a9010323 | 247 | return SR_OK; |
dfaee1de BG |
248 | } |
249 | ||
4e88b86c DL |
250 | static void dev_acquisition_close(const struct sr_dev_inst *sdi) |
251 | { | |
252 | GSList *chl; | |
253 | struct sr_channel *ch; | |
254 | ||
255 | for (chl = sdi->channels; chl; chl = chl->next) { | |
256 | ch = chl->data; | |
257 | bl_acme_close_channel(ch); | |
258 | } | |
259 | } | |
260 | ||
261 | static int dev_acquisition_open(const struct sr_dev_inst *sdi) | |
262 | { | |
263 | GSList *chl; | |
264 | struct sr_channel *ch; | |
265 | ||
266 | for (chl = sdi->channels; chl; chl = chl->next) { | |
267 | ch = chl->data; | |
268 | if (bl_acme_open_channel(ch)) { | |
269 | sr_err("Error opening channel %s", ch->name); | |
270 | dev_acquisition_close(sdi); | |
271 | return SR_ERR; | |
272 | } | |
273 | } | |
274 | ||
275 | return 0; | |
276 | } | |
277 | ||
695dc859 | 278 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) |
dfaee1de | 279 | { |
6b80b80d | 280 | struct dev_context *devc; |
a0648b1a DL |
281 | struct itimerspec tspec = { |
282 | .it_interval = { 0, 0 }, | |
283 | .it_value = { 0, 0 } | |
284 | }; | |
6b80b80d | 285 | |
4e88b86c DL |
286 | if (dev_acquisition_open(sdi)) |
287 | return SR_ERR; | |
288 | ||
6b80b80d | 289 | devc = sdi->priv; |
a0648b1a DL |
290 | devc->samples_missed = 0; |
291 | devc->timer_fd = timerfd_create(CLOCK_MONOTONIC, 0); | |
292 | if (devc->timer_fd < 0) { | |
293 | sr_err("Error creating timer fd"); | |
294 | return SR_ERR; | |
295 | } | |
6b80b80d | 296 | |
a0648b1a | 297 | tspec.it_interval.tv_sec = 0; |
7a1a3a36 | 298 | tspec.it_interval.tv_nsec = SR_HZ_TO_NS(devc->samplerate); |
a0648b1a DL |
299 | tspec.it_value = tspec.it_interval; |
300 | ||
301 | if (timerfd_settime(devc->timer_fd, 0, &tspec, NULL)) { | |
302 | sr_err("Failed to set timer"); | |
303 | close(devc->timer_fd); | |
6b80b80d BG |
304 | return SR_ERR; |
305 | } | |
306 | ||
a0648b1a | 307 | devc->channel = g_io_channel_unix_new(devc->timer_fd); |
6b80b80d BG |
308 | g_io_channel_set_flags(devc->channel, G_IO_FLAG_NONBLOCK, NULL); |
309 | g_io_channel_set_encoding(devc->channel, NULL, NULL); | |
310 | g_io_channel_set_buffered(devc->channel, FALSE); | |
311 | ||
312 | sr_session_source_add_channel(sdi->session, devc->channel, | |
a0648b1a | 313 | G_IO_IN | G_IO_ERR, 1000, bl_acme_receive_data, (void *)sdi); |
6b80b80d | 314 | |
bee2b016 | 315 | std_session_send_df_header(sdi); |
d54a7c42 | 316 | sr_sw_limits_acquisition_start(&devc->limits); |
dfaee1de BG |
317 | |
318 | return SR_OK; | |
319 | } | |
320 | ||
695dc859 | 321 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) |
dfaee1de | 322 | { |
6b80b80d BG |
323 | struct dev_context *devc; |
324 | ||
6b80b80d BG |
325 | devc = sdi->priv; |
326 | ||
f9b0ab6b | 327 | dev_acquisition_close(sdi); |
6b80b80d BG |
328 | sr_session_source_remove_channel(sdi->session, devc->channel); |
329 | g_io_channel_shutdown(devc->channel, FALSE, NULL); | |
330 | g_io_channel_unref(devc->channel); | |
331 | devc->channel = NULL; | |
332 | ||
bee2b016 | 333 | std_session_send_df_end(sdi); |
dfaee1de | 334 | |
a0648b1a | 335 | if (devc->samples_missed > 0) |
6433156c | 336 | sr_warn("%" PRIu64 " samples missed", devc->samples_missed); |
a0648b1a | 337 | |
dfaee1de BG |
338 | return SR_OK; |
339 | } | |
340 | ||
dd5c48a6 | 341 | static struct sr_dev_driver baylibre_acme_driver_info = { |
dfaee1de | 342 | .name = "baylibre-acme", |
6b80b80d | 343 | .longname = "BayLibre ACME (Another Cute Measurement Equipment)", |
dfaee1de | 344 | .api_version = 1, |
c2fdcc25 | 345 | .init = std_init, |
700d6b64 | 346 | .cleanup = std_cleanup, |
dfaee1de | 347 | .scan = scan, |
c01bf34c | 348 | .dev_list = std_dev_list, |
f778bf02 | 349 | .dev_clear = std_dev_clear, |
dfaee1de BG |
350 | .config_get = config_get, |
351 | .config_set = config_set, | |
352 | .config_list = config_list, | |
4d67b9d9 UH |
353 | .dev_open = std_dummy_dev_open, |
354 | .dev_close = std_dummy_dev_close, | |
dfaee1de BG |
355 | .dev_acquisition_start = dev_acquisition_start, |
356 | .dev_acquisition_stop = dev_acquisition_stop, | |
41812aca | 357 | .context = NULL, |
dfaee1de | 358 | }; |
dd5c48a6 | 359 | SR_REGISTER_DEV_DRIVER(baylibre_acme_driver_info); |