]>
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> |
6b80b80d | 21 | #include <string.h> |
391e23a9 | 22 | #include <stdlib.h> |
6b80b80d | 23 | #include <errno.h> |
391e23a9 | 24 | #include <fcntl.h> |
61f2b7f7 | 25 | #include <glib/gstdio.h> |
dfaee1de | 26 | #include "protocol.h" |
740ad48a | 27 | #include "gpio.h" |
dfaee1de | 28 | |
6b80b80d BG |
29 | struct channel_group_priv { |
30 | int hwmon_num; | |
61f2b7f7 | 31 | int probe_type; |
740ad48a | 32 | int index; |
6b80b80d BG |
33 | }; |
34 | ||
35 | struct channel_priv { | |
36 | int ch_type; | |
4e88b86c | 37 | int fd; |
7e5a048f | 38 | float val; |
6b80b80d BG |
39 | struct channel_group_priv *probe; |
40 | }; | |
41 | ||
42 | static const uint8_t enrg_i2c_addrs[] = { | |
43 | 0x40, 0x41, 0x44, 0x45, 0x42, 0x43, 0x46, 0x47, | |
44 | }; | |
45 | ||
46 | static const uint8_t temp_i2c_addrs[] = { | |
47 | 0x0, 0x0, 0x0, 0x0, 0x4c, 0x49, 0x4f, 0x4b, | |
48 | }; | |
49 | ||
740ad48a | 50 | static const uint32_t pws_gpios[] = { |
391e23a9 | 51 | 486, 498, 502, 482, 478, 506, 510, 474, |
740ad48a BG |
52 | }; |
53 | ||
54 | static const uint32_t pws_info_gpios[] = { | |
55 | 487, 499, 503, 483, 479, 507, 511, 475, | |
56 | }; | |
57 | ||
61f2b7f7 BG |
58 | #define MOHM_TO_UOHM(x) ((x) * 1000) |
59 | #define UOHM_TO_MOHM(x) ((x) / 1000) | |
60 | ||
6b80b80d BG |
61 | SR_PRIV uint8_t bl_acme_get_enrg_addr(int index) |
62 | { | |
63 | return enrg_i2c_addrs[index]; | |
64 | } | |
65 | ||
66 | SR_PRIV uint8_t bl_acme_get_temp_addr(int index) | |
67 | { | |
68 | return temp_i2c_addrs[index]; | |
69 | } | |
70 | ||
71 | SR_PRIV gboolean bl_acme_is_sane(void) | |
72 | { | |
73 | gboolean status; | |
74 | ||
75 | /* | |
76 | * We expect sysfs to be present and mounted at /sys, ina226 and | |
77 | * tmp435 sensors detected by the system and their appropriate | |
78 | * drivers loaded and functional. | |
79 | */ | |
80 | status = g_file_test("/sys", G_FILE_TEST_IS_DIR); | |
81 | if (!status) { | |
82 | sr_err("/sys/ directory not found - sysfs not mounted?"); | |
83 | return FALSE; | |
84 | } | |
85 | ||
86 | return TRUE; | |
87 | } | |
88 | ||
89 | static void probe_name_path(unsigned int addr, GString *path) | |
90 | { | |
91 | g_string_printf(path, | |
92 | "/sys/class/i2c-adapter/i2c-1/1-00%02x/name", addr); | |
93 | } | |
94 | ||
95 | /* | |
96 | * For given address fill buf with the path to appropriate hwmon entry. | |
97 | */ | |
98 | static void probe_hwmon_path(unsigned int addr, GString *path) | |
99 | { | |
100 | g_string_printf(path, | |
101 | "/sys/class/i2c-adapter/i2c-1/1-00%02x/hwmon", addr); | |
102 | } | |
103 | ||
104 | SR_PRIV gboolean bl_acme_detect_probe(unsigned int addr, | |
105 | int prb_num, const char *prb_name) | |
106 | { | |
107 | gboolean ret = FALSE, status; | |
108 | char *buf = NULL; | |
109 | GString *path = g_string_sized_new(64); | |
110 | GError *err = NULL; | |
111 | gsize size; | |
112 | ||
113 | probe_name_path(addr, path); | |
114 | status = g_file_get_contents(path->str, &buf, &size, &err); | |
115 | if (!status) { | |
116 | sr_dbg("Name for probe %d can't be read: %s", | |
117 | prb_num, err->message); | |
118 | g_string_free(path, TRUE); | |
119 | return ret; | |
6b80b80d BG |
120 | } |
121 | ||
391e23a9 | 122 | if (!strncmp(buf, prb_name, strlen(prb_name))) { |
6b80b80d BG |
123 | /* |
124 | * Correct driver registered on this address - but is | |
125 | * there an actual probe connected? | |
126 | */ | |
127 | probe_hwmon_path(addr, path); | |
128 | status = g_file_test(path->str, G_FILE_TEST_IS_DIR); | |
129 | if (status) { | |
130 | /* We have found an ACME probe. */ | |
131 | ret = TRUE; | |
132 | } | |
133 | } | |
134 | ||
135 | g_free(buf); | |
136 | g_string_free(path, TRUE); | |
137 | ||
138 | return ret; | |
139 | } | |
140 | ||
141 | static int get_hwmon_index(unsigned int addr) | |
142 | { | |
143 | int status, hwmon; | |
144 | GString *path = g_string_sized_new(64); | |
145 | GError *err = NULL; | |
146 | GDir *dir; | |
147 | ||
148 | probe_hwmon_path(addr, path); | |
149 | dir = g_dir_open(path->str, 0, &err); | |
391e23a9 | 150 | if (!dir) { |
6b80b80d BG |
151 | sr_err("Error opening %s: %s", path->str, err->message); |
152 | g_string_free(path, TRUE); | |
153 | return -1; | |
154 | } | |
155 | ||
156 | g_string_free(path, TRUE); | |
157 | ||
158 | /* | |
159 | * The directory should contain a single file named hwmonX where X | |
160 | * is the hwmon index. | |
161 | */ | |
162 | status = sscanf(g_dir_read_name(dir), "hwmon%d", &hwmon); | |
163 | g_dir_close(dir); | |
164 | if (status != 1) { | |
165 | sr_err("Unable to determine the hwmon entry"); | |
166 | return -1; | |
167 | } | |
168 | ||
169 | return hwmon; | |
170 | } | |
171 | ||
391e23a9 | 172 | static void append_channel(struct sr_dev_inst *sdi, struct sr_channel_group *cg, |
6b80b80d BG |
173 | int index, int type) |
174 | { | |
175 | struct channel_priv *cp; | |
176 | struct dev_context *devc; | |
177 | struct sr_channel *ch; | |
178 | char *name; | |
179 | ||
180 | devc = sdi->priv; | |
181 | ||
182 | switch (type) { | |
183 | case ENRG_PWR: | |
184 | name = g_strdup_printf("P%d_ENRG_PWR", index); | |
185 | break; | |
186 | case ENRG_CURR: | |
187 | name = g_strdup_printf("P%d_ENRG_CURR", index); | |
188 | break; | |
189 | case ENRG_VOL: | |
190 | name = g_strdup_printf("P%d_ENRG_VOL", index); | |
191 | break; | |
192 | case TEMP_IN: | |
193 | name = g_strdup_printf("P%d_TEMP_IN", index); | |
194 | break; | |
195 | case TEMP_OUT: | |
196 | name = g_strdup_printf("P%d_TEMP_OUT", index); | |
197 | break; | |
198 | default: | |
391e23a9 | 199 | sr_err("Invalid channel type: %d.", type); |
6b80b80d BG |
200 | return; |
201 | } | |
202 | ||
203 | cp = g_malloc0(sizeof(struct channel_priv)); | |
204 | cp->ch_type = type; | |
205 | cp->probe = cg->priv; | |
206 | ||
5e23fcab | 207 | ch = sr_channel_new(sdi, devc->num_channels++, |
6b80b80d BG |
208 | SR_CHANNEL_ANALOG, TRUE, name); |
209 | g_free(name); | |
210 | ||
211 | ch->priv = cp; | |
212 | cg->channels = g_slist_append(cg->channels, ch); | |
6b80b80d BG |
213 | } |
214 | ||
215 | SR_PRIV gboolean bl_acme_register_probe(struct sr_dev_inst *sdi, int type, | |
216 | unsigned int addr, int prb_num) | |
217 | { | |
218 | struct sr_channel_group *cg; | |
219 | struct channel_group_priv *cgp; | |
220 | int hwmon; | |
221 | ||
222 | /* Obtain the hwmon index. */ | |
223 | hwmon = get_hwmon_index(addr); | |
224 | if (hwmon < 0) | |
225 | return FALSE; | |
226 | ||
227 | cg = g_malloc0(sizeof(struct sr_channel_group)); | |
228 | cgp = g_malloc0(sizeof(struct channel_group_priv)); | |
229 | cgp->hwmon_num = hwmon; | |
61f2b7f7 | 230 | cgp->probe_type = type; |
740ad48a | 231 | cgp->index = prb_num - 1; |
6b80b80d BG |
232 | cg->name = g_strdup_printf("Probe_%d", prb_num); |
233 | cg->priv = cgp; | |
234 | ||
235 | if (type == PROBE_ENRG) { | |
236 | append_channel(sdi, cg, prb_num, ENRG_PWR); | |
237 | append_channel(sdi, cg, prb_num, ENRG_CURR); | |
238 | append_channel(sdi, cg, prb_num, ENRG_VOL); | |
239 | } else if (type == PROBE_TEMP) { | |
240 | append_channel(sdi, cg, prb_num, TEMP_IN); | |
241 | append_channel(sdi, cg, prb_num, TEMP_OUT); | |
242 | } else { | |
391e23a9 | 243 | sr_err("Invalid probe type: %d.", type); |
6b80b80d BG |
244 | } |
245 | ||
246 | sdi->channel_groups = g_slist_append(sdi->channel_groups, cg); | |
247 | ||
248 | return TRUE; | |
249 | } | |
250 | ||
1fe04eb8 BG |
251 | SR_PRIV int bl_acme_get_probe_type(const struct sr_channel_group *cg) |
252 | { | |
253 | struct channel_group_priv *cgp = cg->priv; | |
254 | ||
255 | return cgp->probe_type; | |
256 | } | |
257 | ||
258 | SR_PRIV int bl_acme_probe_has_pws(const struct sr_channel_group *cg) | |
259 | { | |
260 | struct channel_group_priv *cgp = cg->priv; | |
261 | ||
262 | return sr_gpio_getval_export(pws_info_gpios[cgp->index]); | |
263 | } | |
264 | ||
61f2b7f7 BG |
265 | /* |
266 | * Sets path to the hwmon attribute if this channel group | |
267 | * supports shunt resistance setting. The caller has to supply | |
268 | * a valid GString. | |
269 | */ | |
270 | static int get_shunt_path(const struct sr_channel_group *cg, GString *path) | |
271 | { | |
272 | struct channel_group_priv *cgp; | |
273 | int ret = SR_OK, status; | |
274 | ||
275 | cgp = cg->priv; | |
276 | ||
277 | if (cgp->probe_type != PROBE_ENRG) { | |
278 | sr_err("Probe doesn't support shunt resistance setting"); | |
279 | return SR_ERR_ARG; | |
280 | } | |
281 | ||
282 | g_string_append_printf(path, | |
283 | "/sys/class/hwmon/hwmon%d/shunt_resistor", | |
284 | cgp->hwmon_num); | |
285 | ||
286 | /* | |
287 | * The shunt_resistor sysfs attribute is available | |
288 | * in the Linux kernel since version 3.20. We have | |
391e23a9 | 289 | * to notify the user if this attribute is not present. |
61f2b7f7 BG |
290 | */ |
291 | status = g_file_test(path->str, G_FILE_TEST_EXISTS); | |
292 | if (!status) { | |
391e23a9 | 293 | sr_err("shunt_resistance attribute not present, please update " |
61f2b7f7 BG |
294 | "your kernel to version >=3.20"); |
295 | return SR_ERR_NA; | |
296 | } | |
297 | ||
298 | return ret; | |
299 | } | |
300 | ||
7c91c22a BG |
301 | /* |
302 | * Try setting the update_interval sysfs attribute for each probe according | |
303 | * to samplerate. | |
304 | */ | |
305 | SR_PRIV void bl_acme_maybe_set_update_interval(const struct sr_dev_inst *sdi, | |
306 | uint64_t samplerate) | |
307 | { | |
308 | struct sr_channel_group *cg; | |
309 | struct channel_group_priv *cgp; | |
310 | GString *hwmon; | |
311 | GSList *l; | |
312 | FILE *fd; | |
313 | ||
314 | for (l = sdi->channel_groups; l != NULL; l = l->next) { | |
315 | cg = l->data; | |
316 | cgp = cg->priv; | |
317 | ||
318 | hwmon = g_string_sized_new(64); | |
319 | g_string_append_printf(hwmon, | |
320 | "/sys/class/hwmon/hwmon%d/update_interval", | |
321 | cgp->hwmon_num); | |
322 | ||
323 | if (g_file_test(hwmon->str, G_FILE_TEST_EXISTS)) { | |
324 | fd = g_fopen(hwmon->str, "w"); | |
325 | if (!fd) { | |
326 | g_string_free(hwmon, TRUE); | |
327 | continue; | |
328 | } | |
329 | ||
330 | g_fprintf(fd, "%" PRIu64 "\n", 1000 / samplerate); | |
331 | fclose(fd); | |
332 | } | |
333 | ||
334 | g_string_free(hwmon, TRUE); | |
335 | } | |
336 | } | |
337 | ||
61f2b7f7 BG |
338 | SR_PRIV int bl_acme_get_shunt(const struct sr_channel_group *cg, |
339 | uint64_t *shunt) | |
340 | { | |
341 | GString *path = g_string_sized_new(64); | |
342 | gchar *contents; | |
343 | int status, ret = SR_OK; | |
344 | GError *err = NULL; | |
345 | ||
346 | status = get_shunt_path(cg, path); | |
347 | if (status != SR_OK) { | |
348 | ret = status; | |
349 | goto out; | |
350 | } | |
351 | ||
352 | status = g_file_get_contents(path->str, &contents, NULL, &err); | |
353 | if (!status) { | |
354 | sr_err("Error reading shunt resistance: %s", err->message); | |
355 | ret = SR_ERR_IO; | |
356 | goto out; | |
357 | } | |
358 | ||
359 | *shunt = UOHM_TO_MOHM(strtol(contents, NULL, 10)); | |
360 | ||
361 | out: | |
362 | g_string_free(path, TRUE); | |
363 | return ret; | |
364 | } | |
365 | ||
391e23a9 | 366 | SR_PRIV int bl_acme_set_shunt(const struct sr_channel_group *cg, uint64_t shunt) |
61f2b7f7 BG |
367 | { |
368 | GString *path = g_string_sized_new(64);; | |
369 | int status, ret = SR_OK; | |
370 | FILE *fd; | |
371 | ||
372 | status = get_shunt_path(cg, path); | |
373 | if (status != SR_OK) { | |
374 | ret = status; | |
375 | goto out; | |
376 | } | |
377 | ||
378 | /* | |
379 | * Can't use g_file_set_contents() here, as it calls open() with | |
380 | * O_EXEC flag in a sysfs directory thus failing with EACCES. | |
381 | */ | |
382 | fd = g_fopen(path->str, "w"); | |
383 | if (!fd) { | |
7237e912 | 384 | sr_err("Error opening %s: %s", path->str, g_strerror(errno)); |
350b8b07 BG |
385 | ret = SR_ERR_IO; |
386 | goto out; | |
61f2b7f7 BG |
387 | } |
388 | ||
34527854 | 389 | g_fprintf(fd, "%" PRIu64 "\n", MOHM_TO_UOHM(shunt)); |
61f2b7f7 BG |
390 | fclose(fd); |
391 | ||
392 | out: | |
393 | g_string_free(path, TRUE); | |
394 | return ret; | |
395 | } | |
396 | ||
740ad48a BG |
397 | SR_PRIV int bl_acme_read_power_state(const struct sr_channel_group *cg, |
398 | gboolean *off) | |
399 | { | |
400 | struct channel_group_priv *cgp; | |
401 | int val; | |
402 | ||
403 | cgp = cg->priv; | |
404 | ||
1fe04eb8 | 405 | if (!bl_acme_probe_has_pws(cg)) { |
740ad48a BG |
406 | sr_err("Probe has no power-switch"); |
407 | return SR_ERR_ARG; | |
408 | } | |
409 | ||
410 | val = sr_gpio_getval_export(pws_gpios[cgp->index]); | |
411 | *off = val ? FALSE : TRUE; | |
412 | ||
413 | return SR_OK; | |
414 | } | |
415 | ||
416 | SR_PRIV int bl_acme_set_power_off(const struct sr_channel_group *cg, | |
417 | gboolean off) | |
418 | { | |
419 | struct channel_group_priv *cgp; | |
09d217a4 | 420 | int val; |
740ad48a BG |
421 | |
422 | cgp = cg->priv; | |
423 | ||
1fe04eb8 | 424 | if (!bl_acme_probe_has_pws(cg)) { |
740ad48a BG |
425 | sr_err("Probe has no power-switch"); |
426 | return SR_ERR_ARG; | |
427 | } | |
428 | ||
09d217a4 | 429 | val = sr_gpio_setval_export(pws_gpios[cgp->index], off ? 0 : 1); |
192d37e7 BG |
430 | if (val < 0) { |
431 | sr_err("Error setting power-off state: gpio: %d", | |
432 | pws_gpios[cgp->index]); | |
433 | return SR_ERR_IO; | |
434 | } | |
740ad48a BG |
435 | |
436 | return SR_OK; | |
437 | } | |
438 | ||
6b80b80d | 439 | static int channel_to_mq(struct sr_channel *ch) |
dfaee1de | 440 | { |
6b80b80d BG |
441 | struct channel_priv *chp; |
442 | ||
443 | chp = ch->priv; | |
444 | ||
445 | switch (chp->ch_type) { | |
446 | case ENRG_PWR: | |
447 | return SR_MQ_POWER; | |
448 | case ENRG_CURR: | |
449 | return SR_MQ_CURRENT; | |
450 | case ENRG_VOL: | |
451 | return SR_MQ_VOLTAGE; | |
391e23a9 | 452 | case TEMP_IN: /* Fallthrough */ |
6b80b80d BG |
453 | case TEMP_OUT: |
454 | return SR_MQ_TEMPERATURE; | |
455 | default: | |
456 | return -1; | |
457 | } | |
458 | } | |
459 | ||
460 | static int channel_to_unit(struct sr_channel *ch) | |
461 | { | |
462 | struct channel_priv *chp; | |
463 | ||
464 | chp = ch->priv; | |
465 | ||
466 | switch (chp->ch_type) { | |
467 | case ENRG_PWR: | |
468 | return SR_UNIT_WATT; | |
469 | case ENRG_CURR: | |
470 | return SR_UNIT_AMPERE; | |
471 | case ENRG_VOL: | |
472 | return SR_UNIT_VOLT; | |
391e23a9 | 473 | case TEMP_IN: /* Fallthrough */ |
6b80b80d BG |
474 | case TEMP_OUT: |
475 | return SR_UNIT_CELSIUS; | |
476 | default: | |
477 | return -1; | |
478 | } | |
479 | } | |
480 | ||
481 | /* We need to scale measurements down from the units used by the drivers. */ | |
482 | static float adjust_data(int val, int type) | |
483 | { | |
484 | switch (type) { | |
485 | case ENRG_PWR: | |
486 | return ((float)val) / 1000000.0; | |
391e23a9 UH |
487 | case ENRG_CURR: /* Fallthrough */ |
488 | case ENRG_VOL: /* Fallthrough */ | |
489 | case TEMP_IN: /* Fallthrough */ | |
6b80b80d BG |
490 | case TEMP_OUT: |
491 | return ((float)val) / 1000.0; | |
492 | default: | |
493 | return 0.0; | |
494 | } | |
495 | } | |
496 | ||
497 | static float read_sample(struct sr_channel *ch) | |
498 | { | |
499 | struct channel_priv *chp; | |
4e88b86c | 500 | char buf[16]; |
6b80b80d BG |
501 | ssize_t len; |
502 | int fd; | |
503 | ||
4e88b86c DL |
504 | chp = ch->priv; |
505 | fd = chp->fd; | |
506 | ||
507 | lseek(fd, 0, SEEK_SET); | |
508 | ||
509 | len = read(fd, buf, sizeof(buf)); | |
510 | if (len < 0) { | |
6433156c | 511 | sr_err("Error reading from channel %s (hwmon: %d): %s", |
7237e912 | 512 | ch->name, chp->probe->hwmon_num, g_strerror(errno)); |
4e88b86c DL |
513 | ch->enabled = FALSE; |
514 | return -1.0; | |
515 | } | |
516 | ||
517 | return adjust_data(strtol(buf, NULL, 10), chp->ch_type); | |
518 | } | |
519 | ||
520 | SR_PRIV int bl_acme_open_channel(struct sr_channel *ch) | |
521 | { | |
522 | struct channel_priv *chp; | |
523 | char path[64], *file; | |
524 | int fd; | |
525 | ||
6b80b80d BG |
526 | chp = ch->priv; |
527 | ||
528 | switch (chp->ch_type) { | |
529 | case ENRG_PWR: file = "power1_input"; break; | |
530 | case ENRG_CURR: file = "curr1_input"; break; | |
531 | case ENRG_VOL: file = "in1_input"; break; | |
532 | case TEMP_IN: file = "temp1_input"; break; | |
533 | case TEMP_OUT: file = "temp2_input"; break; | |
534 | default: | |
391e23a9 | 535 | sr_err("Invalid channel type: %d.", chp->ch_type); |
4e88b86c | 536 | return SR_ERR; |
6b80b80d BG |
537 | } |
538 | ||
391e23a9 | 539 | snprintf(path, sizeof(path), "/sys/class/hwmon/hwmon%d/%s", |
6b80b80d | 540 | chp->probe->hwmon_num, file); |
4e88b86c | 541 | |
6b80b80d BG |
542 | fd = open(path, O_RDONLY); |
543 | if (fd < 0) { | |
7237e912 | 544 | sr_err("Error opening %s: %s", path, g_strerror(errno)); |
6b80b80d | 545 | ch->enabled = FALSE; |
4e88b86c | 546 | return SR_ERR; |
6b80b80d BG |
547 | } |
548 | ||
4e88b86c | 549 | chp->fd = fd; |
6b80b80d | 550 | |
4e88b86c DL |
551 | return 0; |
552 | } | |
553 | ||
554 | SR_PRIV void bl_acme_close_channel(struct sr_channel *ch) | |
555 | { | |
556 | struct channel_priv *chp; | |
557 | ||
558 | chp = ch->priv; | |
559 | close(chp->fd); | |
560 | chp->fd = -1; | |
6b80b80d BG |
561 | } |
562 | ||
563 | SR_PRIV int bl_acme_receive_data(int fd, int revents, void *cb_data) | |
564 | { | |
a0648b1a DL |
565 | uint32_t cur_time, elapsed_time; |
566 | uint64_t nrexpiration; | |
6b80b80d BG |
567 | struct sr_datafeed_packet packet, framep; |
568 | struct sr_datafeed_analog analog; | |
569 | struct sr_dev_inst *sdi; | |
570 | struct sr_channel *ch; | |
7e5a048f | 571 | struct channel_priv *chp; |
dfaee1de | 572 | struct dev_context *devc; |
6b80b80d | 573 | GSList *chl, chonly; |
7e5a048f | 574 | unsigned i; |
dfaee1de BG |
575 | |
576 | (void)fd; | |
6b80b80d BG |
577 | (void)revents; |
578 | ||
579 | sdi = cb_data; | |
580 | if (!sdi) | |
581 | return TRUE; | |
dfaee1de | 582 | |
6b80b80d BG |
583 | devc = sdi->priv; |
584 | if (!devc) | |
dfaee1de BG |
585 | return TRUE; |
586 | ||
6b80b80d BG |
587 | packet.type = SR_DF_ANALOG; |
588 | packet.payload = &analog; | |
d0148a50 | 589 | memset(&analog, 0, sizeof(struct sr_datafeed_analog)); |
6b80b80d | 590 | |
a0648b1a DL |
591 | if (read(devc->timer_fd, &nrexpiration, sizeof(nrexpiration)) < 0) { |
592 | sr_warn("Failed to read timer information"); | |
593 | return TRUE; | |
594 | } | |
595 | ||
6b80b80d | 596 | /* |
a0648b1a DL |
597 | * We were not able to process the previous timer expiration, we are |
598 | * overloaded. | |
6b80b80d | 599 | */ |
a0648b1a DL |
600 | if (nrexpiration > 1) |
601 | devc->samples_missed += nrexpiration - 1; | |
6b80b80d | 602 | |
6b80b80d | 603 | /* |
7e5a048f BG |
604 | * XXX This is a nasty workaround... |
605 | * | |
606 | * At high sampling rates and maximum channels we are not able to | |
607 | * acquire samples fast enough, even though frontends still think | |
608 | * that samples arrive on time. This causes shifts in frontend | |
609 | * plots. | |
610 | * | |
611 | * To compensate for the delay we check if any clock events were | |
612 | * missed and - if so - don't really read the next value, but send | |
613 | * the same sample as fast as possible. We do it until we are back | |
614 | * on schedule. | |
615 | * | |
616 | * At high sampling rate this doesn't seem to visibly reduce the | |
617 | * accuracy. | |
6b80b80d | 618 | */ |
7e5a048f BG |
619 | for (i = 0; i < nrexpiration; i++) { |
620 | framep.type = SR_DF_FRAME_BEGIN; | |
621 | sr_session_send(cb_data, &framep); | |
622 | ||
623 | /* | |
624 | * Due to different units used in each channel we're sending | |
625 | * samples one-by-one. | |
626 | */ | |
627 | for (chl = sdi->channels; chl; chl = chl->next) { | |
628 | ch = chl->data; | |
629 | chp = ch->priv; | |
6b80b80d | 630 | |
7e5a048f BG |
631 | if (!ch->enabled) |
632 | continue; | |
633 | chonly.next = NULL; | |
634 | chonly.data = ch; | |
635 | analog.channels = &chonly; | |
636 | analog.num_samples = 1; | |
637 | analog.mq = channel_to_mq(chl->data); | |
638 | analog.unit = channel_to_unit(ch); | |
639 | ||
640 | if (i < 1) | |
641 | chp->val = read_sample(ch); | |
642 | ||
643 | analog.data = &chp->val; | |
644 | sr_session_send(cb_data, &packet); | |
645 | } | |
646 | ||
647 | framep.type = SR_DF_FRAME_END; | |
648 | sr_session_send(cb_data, &framep); | |
649 | } | |
6b80b80d BG |
650 | |
651 | devc->samples_read++; | |
652 | if (devc->limit_samples > 0 && | |
653 | devc->samples_read >= devc->limit_samples) { | |
654 | sr_info("Requested number of samples reached."); | |
655 | sdi->driver->dev_acquisition_stop(sdi, cb_data); | |
656 | devc->last_sample_fin = g_get_monotonic_time(); | |
dfaee1de | 657 | return TRUE; |
6b80b80d BG |
658 | } else if (devc->limit_msec > 0) { |
659 | cur_time = g_get_monotonic_time(); | |
660 | elapsed_time = cur_time - devc->start_time; | |
dfaee1de | 661 | |
6b80b80d BG |
662 | if (elapsed_time >= devc->limit_msec) { |
663 | sr_info("Sampling time limit reached."); | |
664 | sdi->driver->dev_acquisition_stop(sdi, cb_data); | |
665 | devc->last_sample_fin = g_get_monotonic_time(); | |
666 | return TRUE; | |
667 | } | |
dfaee1de BG |
668 | } |
669 | ||
6b80b80d | 670 | devc->last_sample_fin = g_get_monotonic_time(); |
dfaee1de BG |
671 | return TRUE; |
672 | } |