]> sigrok.org Git - libsigrok.git/blob - src/hardware/baylibre-acme/protocol.c
output/csv: use intermediate time_t var, silence compiler warning
[libsigrok.git] / src / hardware / baylibre-acme / protocol.c
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 <config.h>
21 #include <math.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <arpa/inet.h>
27 #include <glib/gstdio.h>
28 #include "protocol.h"
29 #include "gpio.h"
30
31 #define ACME_REV_A              1
32 #define ACME_REV_B              2
33
34 enum channel_type {
35         ENRG_PWR = 1,
36         ENRG_CURR,
37         ENRG_VOL,
38         TEMP_IN,
39         TEMP_OUT,
40 };
41
42 struct channel_group_priv {
43         uint8_t rev;
44         int hwmon_num;
45         int probe_type;
46         int index;
47         int has_pws;
48         uint32_t pws_gpio;
49 };
50
51 struct channel_priv {
52         int ch_type;
53         int fd;
54         int digits;
55         float val;
56         struct channel_group_priv *probe;
57 };
58
59 #define EEPROM_SERIAL_SIZE              16
60 #define EEPROM_TAG_SIZE                 32
61
62 #define EEPROM_PROBE_TYPE_USB           1
63 #define EEPROM_PROBE_TYPE_JACK          2
64 #define EEPROM_PROBE_TYPE_HE10          3
65
66 struct probe_eeprom {
67         uint32_t type;
68         uint32_t rev;
69         uint32_t shunt;
70         uint8_t pwr_sw;
71         uint8_t serial[EEPROM_SERIAL_SIZE];
72         int8_t tag[EEPROM_TAG_SIZE];
73 };
74
75 #define EEPROM_SIZE (3 * sizeof(uint32_t) + 1 + EEPROM_SERIAL_SIZE + EEPROM_TAG_SIZE)
76
77 #define EEPROM_OFF_TYPE         0
78 #define EEPROM_OFF_REV          sizeof(uint32_t)
79 #define EEPROM_OFF_SHUNT        (2 * sizeof(uint32_t))
80 #define EEPROM_OFF_PWR_SW       (3 * sizeof(uint32_t))
81 #define EEPROM_OFF_SERIAL       (3 * sizeof(uint32_t) + 1)
82 #define EEPROM_OFF_TAG          (EEPROM_OFF_SERIAL + EEPROM_SERIAL_SIZE)
83
84 static const uint8_t enrg_i2c_addrs[] = {
85         0x40, 0x41, 0x44, 0x45, 0x42, 0x43, 0x46, 0x47,
86 };
87
88 static const uint8_t temp_i2c_addrs[] = {
89         0x0, 0x0, 0x0, 0x0, 0x4c, 0x49, 0x4f, 0x4b,
90 };
91
92 static const uint32_t revA_pws_gpios[] = {
93         486, 498, 502, 482, 478, 506, 510, 474,
94 };
95
96 static const uint32_t revA_pws_info_gpios[] = {
97         487, 499, 503, 483, 479, 507, 511, 475,
98 };
99
100 static const uint32_t revB_pws_gpios[] = {
101         489, 491, 493, 495, 497, 499, 501, 503,
102 };
103
104 #define MOHM_TO_UOHM(x) ((x) * 1000)
105 #define UOHM_TO_MOHM(x) ((x) / 1000)
106
107 SR_PRIV uint8_t bl_acme_get_enrg_addr(int index)
108 {
109         return enrg_i2c_addrs[index];
110 }
111
112 SR_PRIV uint8_t bl_acme_get_temp_addr(int index)
113 {
114         return temp_i2c_addrs[index];
115 }
116
117 SR_PRIV gboolean bl_acme_is_sane(void)
118 {
119         gboolean status;
120
121         /*
122          * We expect sysfs to be present and mounted at /sys, ina226 and
123          * tmp435 sensors detected by the system and their appropriate
124          * drivers loaded and functional.
125          */
126         status = g_file_test("/sys", G_FILE_TEST_IS_DIR);
127         if (!status) {
128                 sr_err("/sys/ directory not found - sysfs not mounted?");
129                 return FALSE;
130         }
131
132         return TRUE;
133 }
134
135 static void probe_name_path(unsigned int addr, GString *path)
136 {
137         g_string_printf(path,
138                         "/sys/class/i2c-adapter/i2c-1/1-00%02x/name", addr);
139 }
140
141 /*
142  * For given address fill buf with the path to appropriate hwmon entry.
143  */
144 static void probe_hwmon_path(unsigned int addr, GString *path)
145 {
146         g_string_printf(path,
147                         "/sys/class/i2c-adapter/i2c-1/1-00%02x/hwmon", addr);
148 }
149
150 static void probe_eeprom_path(unsigned int addr, GString *path)
151 {
152         g_string_printf(path,
153                         "/sys/class/i2c-dev/i2c-1/device/1-00%02x/eeprom",
154                         addr + 0x10);
155 }
156
157 SR_PRIV gboolean bl_acme_detect_probe(unsigned int addr,
158                                       int prb_num, const char *prb_name)
159 {
160         gboolean ret = FALSE, status;
161         char *buf = NULL;
162         GString *path = g_string_sized_new(64);
163         GError *err = NULL;
164         gsize size;
165
166         probe_name_path(addr, path);
167         status = g_file_get_contents(path->str, &buf, &size, &err);
168         if (!status) {
169                 /* Don't log "No such file or directory" messages. */
170                 if (err->code != G_FILE_ERROR_NOENT)
171                         sr_dbg("Name for probe %d can't be read (%d): %s",
172                                prb_num, err->code, err->message);
173                 g_string_free(path, TRUE);
174                 g_error_free(err);
175                 return ret;
176         }
177
178         if (!strncmp(buf, prb_name, strlen(prb_name))) {
179                 /*
180                  * Correct driver registered on this address - but is
181                  * there an actual probe connected?
182                  */
183                 probe_hwmon_path(addr, path);
184                 status = g_file_test(path->str, G_FILE_TEST_IS_DIR);
185                 if (status) {
186                         /* We have found an ACME probe. */
187                         ret = TRUE;
188                 }
189         }
190
191         g_free(buf);
192         g_string_free(path, TRUE);
193
194         return ret;
195 }
196
197 static int get_hwmon_index(unsigned int addr)
198 {
199         int status, hwmon;
200         GString *path = g_string_sized_new(64);
201         GError *err = NULL;
202         GDir *dir;
203
204         probe_hwmon_path(addr, path);
205         dir = g_dir_open(path->str, 0, &err);
206         if (!dir) {
207                 sr_err("Error opening %s: %s", path->str, err->message);
208                 g_string_free(path, TRUE);
209                 g_error_free(err);
210                 return -1;
211         }
212
213         g_string_free(path, TRUE);
214
215         /*
216          * The directory should contain a single file named hwmonX where X
217          * is the hwmon index.
218          */
219         status = sscanf(g_dir_read_name(dir), "hwmon%d", &hwmon);
220         g_dir_close(dir);
221         if (status != 1) {
222                 sr_err("Unable to determine the hwmon entry");
223                 return -1;
224         }
225
226         return hwmon;
227 }
228
229 static void append_channel(struct sr_dev_inst *sdi, struct sr_channel_group *cg,
230                            int index, int type)
231 {
232         struct channel_priv *cp;
233         struct dev_context *devc;
234         struct sr_channel *ch;
235         char *name;
236
237         devc = sdi->priv;
238
239         switch (type) {
240         case ENRG_PWR:
241                 name = g_strdup_printf("P%d_ENRG_PWR", index);
242                 break;
243         case ENRG_CURR:
244                 name = g_strdup_printf("P%d_ENRG_CURR", index);
245                 break;
246         case ENRG_VOL:
247                 name = g_strdup_printf("P%d_ENRG_VOL", index);
248                 break;
249         case TEMP_IN:
250                 name = g_strdup_printf("P%d_TEMP_IN", index);
251                 break;
252         case TEMP_OUT:
253                 name = g_strdup_printf("P%d_TEMP_OUT", index);
254                 break;
255         default:
256                 sr_err("Invalid channel type: %d.", type);
257                 return;
258         }
259
260         cp = g_malloc0(sizeof(struct channel_priv));
261         cp->ch_type = type;
262         cp->probe = cg->priv;
263
264         ch = sr_channel_new(sdi, devc->num_channels++,
265                             SR_CHANNEL_ANALOG, TRUE, name);
266         g_free(name);
267
268         ch->priv = cp;
269         cg->channels = g_slist_append(cg->channels, ch);
270 }
271
272 static int read_probe_eeprom(unsigned int addr, struct probe_eeprom *eeprom)
273 {
274         GString *path = g_string_sized_new(64);
275         char eeprom_buf[EEPROM_SIZE];
276         ssize_t rd;
277         int fd;
278
279         probe_eeprom_path(addr, path);
280         fd = g_open(path->str, O_RDONLY);
281         g_string_free(path, TRUE);
282         if (fd < 0)
283                 return -1;
284
285         rd = read(fd, eeprom_buf, EEPROM_SIZE);
286         close(fd);
287         if (rd != EEPROM_SIZE)
288                 return -1;
289
290         eeprom->type = RB32(eeprom_buf + EEPROM_OFF_TYPE);
291         eeprom->rev = RB32(eeprom_buf + EEPROM_OFF_REV);
292         eeprom->shunt = RB32(eeprom_buf + EEPROM_OFF_SHUNT);
293         eeprom->pwr_sw = R8(eeprom_buf + EEPROM_OFF_PWR_SW);
294         /* Don't care about the serial number and tag for now. */
295
296         /* Check if we have some sensible values. */
297         if (eeprom->rev != 'B')
298                 /* 'B' is the only supported revision with EEPROM for now. */
299                 return -1;
300
301         if (eeprom->type != EEPROM_PROBE_TYPE_USB &&
302             eeprom->type != EEPROM_PROBE_TYPE_JACK &&
303             eeprom->type != EEPROM_PROBE_TYPE_HE10)
304                 return -1;
305
306         return 0;
307 }
308
309 /* Some i2c slave addresses on revision B probes differ from revision A. */
310 static int revB_addr_to_num(unsigned int addr)
311 {
312         switch (addr) {
313         case 0x44:      return 5;
314         case 0x45:      return 6;
315         case 0x42:      return 3;
316         case 0x43:      return 4;
317         default:        return addr - 0x3f;
318         }
319 }
320
321 SR_PRIV gboolean bl_acme_register_probe(struct sr_dev_inst *sdi, int type,
322                                         unsigned int addr, int prb_num)
323 {
324         struct sr_channel_group *cg;
325         struct channel_group_priv *cgp;
326         struct probe_eeprom eeprom;
327         int hwmon, status;
328         uint32_t gpio;
329
330         /* Obtain the hwmon index. */
331         hwmon = get_hwmon_index(addr);
332         if (hwmon < 0)
333                 return FALSE;
334
335         cg = g_malloc0(sizeof(struct sr_channel_group));
336         cgp = g_malloc0(sizeof(struct channel_group_priv));
337         cg->priv = cgp;
338
339         /*
340          * See if we can read the EEPROM contents. If not, assume it's
341          * a revision A probe.
342          */
343         memset(&eeprom, 0, sizeof(struct probe_eeprom));
344         status = read_probe_eeprom(addr, &eeprom);
345         cgp->rev = status < 0 ? ACME_REV_A : ACME_REV_B;
346
347         prb_num = cgp->rev == ACME_REV_A ? prb_num : revB_addr_to_num(addr);
348
349         cgp->hwmon_num = hwmon;
350         cgp->probe_type = type;
351         cgp->index = prb_num - 1;
352         cg->name = g_strdup_printf("Probe_%d", prb_num);
353
354         if (cgp->rev == ACME_REV_A) {
355                 gpio = revA_pws_info_gpios[cgp->index];
356                 cgp->has_pws = sr_gpio_getval_export(gpio);
357                 cgp->pws_gpio = revA_pws_gpios[cgp->index];
358         } else {
359                 cgp->has_pws = eeprom.pwr_sw;
360                 cgp->pws_gpio = revB_pws_gpios[cgp->index];
361
362                 /*
363                  * For revision B we can already try to set the shunt
364                  * resistance according to the EEPROM contents.
365                  *
366                  * Keep the default value if shunt in EEPROM == 0.
367                  */
368                 if (eeprom.shunt > 0)
369                         bl_acme_set_shunt(cg, UOHM_TO_MOHM(eeprom.shunt));
370         }
371
372         if (type == PROBE_ENRG) {
373                 append_channel(sdi, cg, prb_num, ENRG_PWR);
374                 append_channel(sdi, cg, prb_num, ENRG_CURR);
375                 append_channel(sdi, cg, prb_num, ENRG_VOL);
376         } else if (type == PROBE_TEMP) {
377                 append_channel(sdi, cg, prb_num, TEMP_IN);
378                 append_channel(sdi, cg, prb_num, TEMP_OUT);
379         } else {
380                 sr_err("Invalid probe type: %d.", type);
381         }
382
383         sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
384
385         return TRUE;
386 }
387
388 SR_PRIV int bl_acme_get_probe_type(const struct sr_channel_group *cg)
389 {
390         struct channel_group_priv *cgp = cg->priv;
391
392         return cgp->probe_type;
393 }
394
395 SR_PRIV int bl_acme_probe_has_pws(const struct sr_channel_group *cg)
396 {
397         struct channel_group_priv *cgp = cg->priv;
398
399         return cgp->has_pws;
400 }
401
402 /*
403  * Sets path to the hwmon attribute if this channel group
404  * supports shunt resistance setting. The caller has to supply
405  * a valid GString.
406  */
407 static int get_shunt_path(const struct sr_channel_group *cg, GString *path)
408 {
409         struct channel_group_priv *cgp;
410         int ret = SR_OK, status;
411
412         cgp = cg->priv;
413
414         if (cgp->probe_type != PROBE_ENRG) {
415                 sr_err("Probe doesn't support shunt resistance setting");
416                 return SR_ERR_ARG;
417         }
418
419         g_string_append_printf(path,
420                                "/sys/class/hwmon/hwmon%d/shunt_resistor",
421                                cgp->hwmon_num);
422
423         /*
424          * The shunt_resistor sysfs attribute is available
425          * in the Linux kernel since version 3.20. We have
426          * to notify the user if this attribute is not present.
427          */
428         status = g_file_test(path->str, G_FILE_TEST_EXISTS);
429         if (!status) {
430                 sr_err("shunt_resistance attribute not present, please update "
431                        "your kernel to version >=3.20");
432                 return SR_ERR_NA;
433         }
434
435         return ret;
436 }
437
438 /*
439  * Try setting the update_interval sysfs attribute for each probe according
440  * to samplerate.
441  */
442 SR_PRIV void bl_acme_maybe_set_update_interval(const struct sr_dev_inst *sdi,
443                                                uint64_t samplerate)
444 {
445         struct sr_channel_group *cg;
446         struct channel_group_priv *cgp;
447         GString *hwmon;
448         GSList *l;
449         FILE *fd;
450
451         for (l = sdi->channel_groups; l != NULL; l = l->next) {
452                 cg = l->data;
453                 cgp = cg->priv;
454
455                 hwmon = g_string_sized_new(64);
456                 g_string_append_printf(hwmon,
457                                 "/sys/class/hwmon/hwmon%d/update_interval",
458                                 cgp->hwmon_num);
459
460                 if (g_file_test(hwmon->str, G_FILE_TEST_EXISTS)) {
461                         fd = g_fopen(hwmon->str, "w");
462                         if (!fd) {
463                                 g_string_free(hwmon, TRUE);
464                                 continue;
465                         }
466
467                         g_fprintf(fd, "%" PRIu64 "\n", 1000 / samplerate);
468                         fclose(fd);
469                 }
470
471                 g_string_free(hwmon, TRUE);
472         }
473 }
474
475 SR_PRIV int bl_acme_get_shunt(const struct sr_channel_group *cg,
476                               uint64_t *shunt)
477 {
478         GString *path = g_string_sized_new(64);
479         gchar *contents;
480         int status, ret = SR_OK;
481         GError *err = NULL;
482
483         status = get_shunt_path(cg, path);
484         if (status != SR_OK) {
485                 ret = status;
486                 goto out;
487         }
488
489         status = g_file_get_contents(path->str, &contents, NULL, &err);
490         if (!status) {
491                 sr_err("Error reading shunt resistance: %s", err->message);
492                 ret = SR_ERR_IO;
493                 g_error_free(err);
494                 goto out;
495         }
496
497         *shunt = UOHM_TO_MOHM(strtol(contents, NULL, 10));
498
499 out:
500         g_string_free(path, TRUE);
501         return ret;
502 }
503
504 SR_PRIV int bl_acme_set_shunt(const struct sr_channel_group *cg, uint64_t shunt)
505 {
506         GString *path = g_string_sized_new(64);;
507         int status, ret = SR_OK;
508         FILE *fd;
509
510         status = get_shunt_path(cg, path);
511         if (status != SR_OK) {
512                 ret = status;
513                 goto out;
514         }
515
516         /*
517          * Can't use g_file_set_contents() here, as it calls open() with
518          * O_EXEC flag in a sysfs directory thus failing with EACCES.
519          */
520         fd = g_fopen(path->str, "w");
521         if (!fd) {
522                 sr_err("Error opening %s: %s", path->str, g_strerror(errno));
523                 ret = SR_ERR_IO;
524                 goto out;
525         }
526
527         g_fprintf(fd, "%" PRIu64 "\n", MOHM_TO_UOHM(shunt));
528         fclose(fd);
529
530 out:
531         g_string_free(path, TRUE);
532         return ret;
533 }
534
535 SR_PRIV int bl_acme_read_power_state(const struct sr_channel_group *cg,
536                                      gboolean *off)
537 {
538         struct channel_group_priv *cgp;
539         int val;
540
541         cgp = cg->priv;
542
543         if (!bl_acme_probe_has_pws(cg)) {
544                 sr_err("Probe has no power-switch");
545                 return SR_ERR_ARG;
546         }
547
548         val = sr_gpio_getval_export(cgp->pws_gpio);
549         *off = val ? FALSE : TRUE;
550
551         return SR_OK;
552 }
553
554 SR_PRIV int bl_acme_set_power_off(const struct sr_channel_group *cg,
555                                   gboolean off)
556 {
557         struct channel_group_priv *cgp;
558         int val;
559
560         cgp = cg->priv;
561
562         if (!bl_acme_probe_has_pws(cg)) {
563                 sr_err("Probe has no power-switch");
564                 return SR_ERR_ARG;
565         }
566
567         val = sr_gpio_setval_export(cgp->pws_gpio, off ? 0 : 1);
568         if (val < 0) {
569                 sr_err("Error setting power-off state: gpio: %d",
570                        cgp->pws_gpio);
571                 return SR_ERR_IO;
572         }
573
574         return SR_OK;
575 }
576
577 static int channel_to_mq(struct sr_channel *ch)
578 {
579         struct channel_priv *chp;
580
581         chp = ch->priv;
582
583         switch (chp->ch_type) {
584         case ENRG_PWR:
585                 return SR_MQ_POWER;
586         case ENRG_CURR:
587                 return SR_MQ_CURRENT;
588         case ENRG_VOL:
589                 return SR_MQ_VOLTAGE;
590         case TEMP_IN: /* Fallthrough */
591         case TEMP_OUT:
592                 return SR_MQ_TEMPERATURE;
593         default:
594                 return -1;
595         }
596 }
597
598 static int channel_to_unit(struct sr_channel *ch)
599 {
600         struct channel_priv *chp;
601
602         chp = ch->priv;
603
604         switch (chp->ch_type) {
605         case ENRG_PWR:
606                 return SR_UNIT_WATT;
607         case ENRG_CURR:
608                 return SR_UNIT_AMPERE;
609         case ENRG_VOL:
610                 return SR_UNIT_VOLT;
611         case TEMP_IN: /* Fallthrough */
612         case TEMP_OUT:
613                 return SR_UNIT_CELSIUS;
614         default:
615                 return -1;
616         }
617 }
618
619 /* We need to scale measurements down from the units used by the drivers. */
620 static int type_digits(int type)
621 {
622         switch (type) {
623         case ENRG_PWR:
624                 return 6;
625         case ENRG_CURR: /* Fallthrough */
626         case ENRG_VOL: /* Fallthrough */
627         case TEMP_IN: /* Fallthrough */
628         case TEMP_OUT:
629                 return 3;
630         default:
631                 return 0;
632         }
633 }
634
635 static float read_sample(struct sr_channel *ch)
636 {
637         struct channel_priv *chp;
638         char buf[16];
639         ssize_t len;
640         int fd;
641
642         chp = ch->priv;
643         fd = chp->fd;
644
645         lseek(fd, 0, SEEK_SET);
646
647         len = read(fd, buf, sizeof(buf));
648         if (len < 0) {
649                 sr_err("Error reading from channel %s (hwmon: %d): %s",
650                         ch->name, chp->probe->hwmon_num, g_strerror(errno));
651                 ch->enabled = FALSE;
652                 return -1.0;
653         }
654
655         chp->digits = type_digits(chp->ch_type);
656         return strtol(buf, NULL, 10) * powf(10, -chp->digits);
657 }
658
659 SR_PRIV int bl_acme_open_channel(struct sr_channel *ch)
660 {
661         struct channel_priv *chp;
662         char path[64];
663         const char *file;
664         int fd;
665
666         chp = ch->priv;
667
668         switch (chp->ch_type) {
669         case ENRG_PWR:  file = "power1_input";  break;
670         case ENRG_CURR: file = "curr1_input";   break;
671         case ENRG_VOL:  file = "in1_input";     break;
672         case TEMP_IN:   file = "temp1_input";   break;
673         case TEMP_OUT:  file = "temp2_input";   break;
674         default:
675                 sr_err("Invalid channel type: %d.", chp->ch_type);
676                 return SR_ERR;
677         }
678
679         snprintf(path, sizeof(path), "/sys/class/hwmon/hwmon%d/%s",
680                  chp->probe->hwmon_num, file);
681
682         fd = open(path, O_RDONLY);
683         if (fd < 0) {
684                 sr_err("Error opening %s: %s", path, g_strerror(errno));
685                 ch->enabled = FALSE;
686                 return SR_ERR;
687         }
688
689         chp->fd = fd;
690
691         return 0;
692 }
693
694 SR_PRIV void bl_acme_close_channel(struct sr_channel *ch)
695 {
696         struct channel_priv *chp;
697
698         chp = ch->priv;
699         close(chp->fd);
700         chp->fd = -1;
701 }
702
703 SR_PRIV int bl_acme_receive_data(int fd, int revents, void *cb_data)
704 {
705         uint64_t nrexpiration;
706         struct sr_datafeed_packet packet, framep;
707         struct sr_datafeed_analog analog;
708         struct sr_analog_encoding encoding;
709         struct sr_analog_meaning meaning;
710         struct sr_analog_spec spec;
711         struct sr_dev_inst *sdi;
712         struct sr_channel *ch;
713         struct channel_priv *chp;
714         struct dev_context *devc;
715         GSList *chl, chonly;
716         unsigned i;
717
718         (void)fd;
719         (void)revents;
720
721         sdi = cb_data;
722         if (!sdi)
723                 return TRUE;
724
725         devc = sdi->priv;
726         if (!devc)
727                 return TRUE;
728
729         packet.type = SR_DF_ANALOG;
730         packet.payload = &analog;
731         sr_analog_init(&analog, &encoding, &meaning, &spec, 0);
732
733         if (read(devc->timer_fd, &nrexpiration, sizeof(nrexpiration)) < 0) {
734                 sr_warn("Failed to read timer information");
735                 return TRUE;
736         }
737
738         /*
739          * We were not able to process the previous timer expiration, we are
740          * overloaded.
741          */
742         if (nrexpiration > 1)
743                 devc->samples_missed += nrexpiration - 1;
744
745         /*
746          * XXX This is a nasty workaround...
747          *
748          * At high sampling rates and maximum channels we are not able to
749          * acquire samples fast enough, even though frontends still think
750          * that samples arrive on time. This causes shifts in frontend
751          * plots.
752          *
753          * To compensate for the delay we check if any clock events were
754          * missed and - if so - don't really read the next value, but send
755          * the same sample as fast as possible. We do it until we are back
756          * on schedule.
757          *
758          * At high sampling rate this doesn't seem to visibly reduce the
759          * accuracy.
760          */
761         for (i = 0; i < nrexpiration; i++) {
762                 framep.type = SR_DF_FRAME_BEGIN;
763                 sr_session_send(sdi, &framep);
764
765                 /*
766                  * Due to different units used in each channel we're sending
767                  * samples one-by-one.
768                  */
769                 for (chl = sdi->channels; chl; chl = chl->next) {
770                         ch = chl->data;
771                         chp = ch->priv;
772
773                         if (!ch->enabled)
774                                 continue;
775                         chonly.next = NULL;
776                         chonly.data = ch;
777                         analog.num_samples = 1;
778                         analog.meaning->channels = &chonly;
779                         analog.meaning->mq = channel_to_mq(chl->data);
780                         analog.meaning->unit = channel_to_unit(ch);
781
782                         if (i < 1)
783                                 chp->val = read_sample(ch);
784
785                         analog.encoding->digits  = chp->digits;
786                         analog.spec->spec_digits = chp->digits;
787                         analog.data = &chp->val;
788                         sr_session_send(sdi, &packet);
789                 }
790
791                 framep.type = SR_DF_FRAME_END;
792                 sr_session_send(sdi, &framep);
793         }
794
795         sr_sw_limits_update_samples_read(&devc->limits, 1);
796
797         if (sr_sw_limits_check(&devc->limits)) {
798                 sr_dev_acquisition_stop(sdi);
799                 return TRUE;
800         }
801
802         return TRUE;
803 }