1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Industrialio buffer test code.
4 * Copyright (c) 2008 Jonathan Cameron
6 * This program is primarily intended as an example application.
7 * Reads the current buffer setup from sysfs and starts a short capture
8 * from the specified device, pretty printing the result after appropriate
11 * Command line parameters
12 * generic_buffer -n <device_name> -t <trigger_name>
13 * If trigger name is not specified the program assumes you want a dataready
14 * trigger associated with the device and goes looking for it.
25 #include <linux/types.h>
33 #include "iio_utils.h"
36 * enum autochan - state for the automatic channel enabling mechanism
39 AUTOCHANNELS_DISABLED,
45 * size_from_channelarray() - calculate the storage size of a scan
46 * @channels: the channel info array
47 * @num_channels: number of channels
49 * Has the side effect of filling the channels[i].location values used
50 * in processing the buffer output.
52 static unsigned int size_from_channelarray(struct iio_channel_info *channels, int num_channels)
54 unsigned int bytes = 0;
56 unsigned int misalignment;
58 while (i < num_channels) {
59 if (channels[i].bytes > max)
60 max = channels[i].bytes;
61 if (bytes % channels[i].bytes == 0)
62 channels[i].location = bytes;
64 channels[i].location = bytes - bytes % channels[i].bytes
67 bytes = channels[i].location + channels[i].bytes;
71 * We want the data in next sample to also be properly aligned so
72 * we'll add padding at the end if needed. Adding padding only
73 * works for channel data which size is 2^n bytes.
75 misalignment = bytes % max;
77 bytes += max - misalignment;
82 static void print1byte(uint8_t input, struct iio_channel_info *info)
85 * Shift before conversion to avoid sign extension
86 * of left aligned data
88 input >>= info->shift;
90 if (info->is_signed) {
91 int8_t val = (int8_t)(input << (8 - info->bits_used)) >>
92 (8 - info->bits_used);
93 printf("%05f ", ((float)val + info->offset) * info->scale);
95 printf("%05f ", ((float)input + info->offset) * info->scale);
99 static void print2byte(uint16_t input, struct iio_channel_info *info)
101 /* First swap if incorrect endian */
103 input = be16toh(input);
105 input = le16toh(input);
108 * Shift before conversion to avoid sign extension
109 * of left aligned data
111 input >>= info->shift;
113 if (info->is_signed) {
114 int16_t val = (int16_t)(input << (16 - info->bits_used)) >>
115 (16 - info->bits_used);
116 printf("%05f ", ((float)val + info->offset) * info->scale);
118 printf("%05f ", ((float)input + info->offset) * info->scale);
122 static void print4byte(uint32_t input, struct iio_channel_info *info)
124 /* First swap if incorrect endian */
126 input = be32toh(input);
128 input = le32toh(input);
131 * Shift before conversion to avoid sign extension
132 * of left aligned data
134 input >>= info->shift;
136 if (info->is_signed) {
137 int32_t val = (int32_t)(input << (32 - info->bits_used)) >>
138 (32 - info->bits_used);
139 printf("%05f ", ((float)val + info->offset) * info->scale);
141 printf("%05f ", ((float)input + info->offset) * info->scale);
145 static void print8byte(uint64_t input, struct iio_channel_info *info)
147 /* First swap if incorrect endian */
149 input = be64toh(input);
151 input = le64toh(input);
154 * Shift before conversion to avoid sign extension
155 * of left aligned data
157 input >>= info->shift;
159 if (info->is_signed) {
160 int64_t val = (int64_t)(input << (64 - info->bits_used)) >>
161 (64 - info->bits_used);
162 /* special case for timestamp */
163 if (info->scale == 1.0f && info->offset == 0.0f)
164 printf("%" PRId64 " ", val);
167 ((float)val + info->offset) * info->scale);
169 printf("%05f ", ((float)input + info->offset) * info->scale);
174 * process_scan() - print out the values in SI units
175 * @data: pointer to the start of the scan
176 * @channels: information about the channels.
177 * Note: size_from_channelarray must have been called first
178 * to fill the location offsets.
179 * @num_channels: number of channels
181 static void process_scan(char *data, struct iio_channel_info *channels,
186 for (k = 0; k < num_channels; k++)
187 switch (channels[k].bytes) {
188 /* only a few cases implemented so far */
190 print1byte(*(uint8_t *)(data + channels[k].location),
194 print2byte(*(uint16_t *)(data + channels[k].location),
198 print4byte(*(uint32_t *)(data + channels[k].location),
202 print8byte(*(uint64_t *)(data + channels[k].location),
211 static int enable_disable_all_channels(char *dev_dir_name, int enable)
213 const struct dirent *ent;
214 char scanelemdir[256];
218 snprintf(scanelemdir, sizeof(scanelemdir),
219 FORMAT_SCAN_ELEMENTS_DIR, dev_dir_name);
220 scanelemdir[sizeof(scanelemdir)-1] = '\0';
222 dp = opendir(scanelemdir);
224 fprintf(stderr, "Enabling/disabling channels: can't open %s\n",
230 while (ent = readdir(dp), ent) {
231 if (iioutils_check_suffix(ent->d_name, "_en")) {
232 printf("%sabling: %s\n",
233 enable ? "En" : "Dis",
235 ret = write_sysfs_int(ent->d_name, scanelemdir,
238 fprintf(stderr, "Failed to enable/disable %s\n",
243 if (closedir(dp) == -1) {
244 perror("Enabling/disabling channels: "
245 "Failed to close directory");
251 static void print_usage(void)
253 fprintf(stderr, "Usage: generic_buffer [options]...\n"
254 "Capture, convert and output data from IIO device buffer\n"
255 " -a Auto-activate all available channels\n"
256 " -A Force-activate ALL channels\n"
257 " -c <n> Do n conversions, or loop forever if n < 0\n"
258 " -e Disable wait for event (new data)\n"
259 " -g Use trigger-less mode\n"
260 " -l <n> Set buffer length to n samples\n"
261 " --device-name -n <name>\n"
262 " --device-num -N <num>\n"
263 " Set device by name or number (mandatory)\n"
264 " --trigger-name -t <name>\n"
265 " --trigger-num -T <num>\n"
266 " Set trigger by name or number\n"
267 " -w <n> Set delay between reads in us (event-less mode)\n");
270 static enum autochan autochannels = AUTOCHANNELS_DISABLED;
271 static char *dev_dir_name = NULL;
272 static char *buf_dir_name = NULL;
273 static bool current_trigger_set = false;
275 static void cleanup(void)
279 /* Disable trigger */
280 if (dev_dir_name && current_trigger_set) {
281 /* Disconnect the trigger - just write a dummy name. */
282 ret = write_sysfs_string("trigger/current_trigger",
283 dev_dir_name, "NULL");
285 fprintf(stderr, "Failed to disable trigger: %s\n",
287 current_trigger_set = false;
292 ret = write_sysfs_int("enable", buf_dir_name, 0);
294 fprintf(stderr, "Failed to disable buffer: %s\n",
298 /* Disable channels if auto-enabled */
299 if (dev_dir_name && autochannels == AUTOCHANNELS_ACTIVE) {
300 ret = enable_disable_all_channels(dev_dir_name, 0);
302 fprintf(stderr, "Failed to disable all channels\n");
303 autochannels = AUTOCHANNELS_DISABLED;
307 static void sig_handler(int signum)
309 fprintf(stderr, "Caught signal %d\n", signum);
314 static void register_cleanup(void)
316 struct sigaction sa = { .sa_handler = sig_handler };
317 const int signums[] = { SIGINT, SIGTERM, SIGABRT };
320 for (i = 0; i < ARRAY_SIZE(signums); ++i) {
321 ret = sigaction(signums[i], &sa, NULL);
323 perror("Failed to register signal handler");
329 static const struct option longopts[] = {
330 { "device-name", 1, 0, 'n' },
331 { "device-num", 1, 0, 'N' },
332 { "trigger-name", 1, 0, 't' },
333 { "trigger-num", 1, 0, 'T' },
337 int main(int argc, char **argv)
339 long long num_loops = 2;
340 unsigned long timedelay = 1000000;
341 unsigned long buf_len = 128;
344 unsigned long long j;
345 unsigned long toread;
349 int num_channels = 0;
350 char *trigger_name = NULL, *device_name = NULL;
354 int dev_num = -1, trig_num = -1;
355 char *buffer_access = NULL;
356 unsigned int scan_size;
360 bool force_autochannels = false;
362 struct iio_channel_info *channels = NULL;
366 while ((c = getopt_long(argc, argv, "aAc:egl:n:N:t:T:w:?", longopts,
370 autochannels = AUTOCHANNELS_ENABLED;
373 autochannels = AUTOCHANNELS_ENABLED;
374 force_autochannels = true;
378 num_loops = strtoll(optarg, &dummy, 10);
393 buf_len = strtoul(optarg, &dummy, 10);
401 device_name = strdup(optarg);
405 dev_num = strtoul(optarg, &dummy, 10);
412 trigger_name = strdup(optarg);
416 trig_num = strtoul(optarg, &dummy, 10);
422 timedelay = strtoul(optarg, &dummy, 10);
435 /* Find the device requested */
436 if (dev_num < 0 && !device_name) {
437 fprintf(stderr, "Device not set\n");
441 } else if (dev_num >= 0 && device_name) {
442 fprintf(stderr, "Only one of --device-num or --device-name needs to be set\n");
446 } else if (dev_num < 0) {
447 dev_num = find_type_by_name(device_name, "iio:device");
449 fprintf(stderr, "Failed to find the %s\n", device_name);
454 printf("iio device number being used is %d\n", dev_num);
456 ret = asprintf(&dev_dir_name, "%siio:device%d", iio_dir, dev_num);
459 /* Fetch device_name if specified by number */
461 device_name = malloc(IIO_MAX_NAME_LENGTH);
466 ret = read_sysfs_string("name", dev_dir_name, device_name);
468 fprintf(stderr, "Failed to read name of device %d\n", dev_num);
474 printf("trigger-less mode selected\n");
475 } else if (trig_num >= 0) {
477 ret = asprintf(&trig_dev_name, "%strigger%d", iio_dir, trig_num);
481 trigger_name = malloc(IIO_MAX_NAME_LENGTH);
482 ret = read_sysfs_string("name", trig_dev_name, trigger_name);
485 fprintf(stderr, "Failed to read trigger%d name from\n", trig_num);
488 printf("iio trigger number being used is %d\n", trig_num);
492 * Build the trigger name. If it is device associated
493 * its name is <device_name>_dev[n] where n matches
494 * the device number found above.
496 ret = asprintf(&trigger_name,
497 "%s-dev%d", device_name, dev_num);
504 /* Look for this "-devN" trigger */
505 trig_num = find_type_by_name(trigger_name, "trigger");
507 /* OK try the simpler "-trigger" suffix instead */
509 ret = asprintf(&trigger_name,
510 "%s-trigger", device_name);
517 trig_num = find_type_by_name(trigger_name, "trigger");
519 fprintf(stderr, "Failed to find the trigger %s\n",
525 printf("iio trigger number being used is %d\n", trig_num);
529 * Parse the files in scan_elements to identify what channels are
532 ret = build_channel_array(dev_dir_name, &channels, &num_channels);
534 fprintf(stderr, "Problem reading scan element information\n"
535 "diag %s\n", dev_dir_name);
538 if (num_channels && autochannels == AUTOCHANNELS_ENABLED &&
539 !force_autochannels) {
540 fprintf(stderr, "Auto-channels selected but some channels "
541 "are already activated in sysfs\n");
542 fprintf(stderr, "Proceeding without activating any channels\n");
545 if ((!num_channels && autochannels == AUTOCHANNELS_ENABLED) ||
546 (autochannels == AUTOCHANNELS_ENABLED && force_autochannels)) {
547 fprintf(stderr, "Enabling all channels\n");
549 ret = enable_disable_all_channels(dev_dir_name, 1);
551 fprintf(stderr, "Failed to enable all channels\n");
555 /* This flags that we need to disable the channels again */
556 autochannels = AUTOCHANNELS_ACTIVE;
558 ret = build_channel_array(dev_dir_name, &channels,
561 fprintf(stderr, "Problem reading scan element "
563 "diag %s\n", dev_dir_name);
567 fprintf(stderr, "Still no channels after "
568 "auto-enabling, giving up\n");
573 if (!num_channels && autochannels == AUTOCHANNELS_DISABLED) {
575 "No channels are enabled, we have nothing to scan.\n");
576 fprintf(stderr, "Enable channels manually in "
577 FORMAT_SCAN_ELEMENTS_DIR
578 "/*_en or pass -a to autoenable channels and "
579 "try again.\n", dev_dir_name);
585 * Construct the directory name for the associated buffer.
586 * As we know that the lis3l02dq has only one buffer this may
587 * be built rather than found.
589 ret = asprintf(&buf_dir_name,
590 "%siio:device%d/buffer", iio_dir, dev_num);
597 printf("%s %s\n", dev_dir_name, trigger_name);
599 * Set the device trigger to be the data ready trigger found
602 ret = write_sysfs_string_and_verify("trigger/current_trigger",
607 "Failed to write current_trigger file\n");
612 /* Setup ring buffer parameters */
613 ret = write_sysfs_int("length", buf_dir_name, buf_len);
617 /* Enable the buffer */
618 ret = write_sysfs_int("enable", buf_dir_name, 1);
621 "Failed to enable buffer: %s\n", strerror(-ret));
625 scan_size = size_from_channelarray(channels, num_channels);
627 size_t total_buf_len = scan_size * buf_len;
629 if (scan_size > 0 && total_buf_len / scan_size != buf_len) {
631 perror("Integer overflow happened when calculate scan_size * buf_len");
635 data = malloc(total_buf_len);
641 ret = asprintf(&buffer_access, "/dev/iio:device%d", dev_num);
647 /* Attempt to open non blocking the access dev */
648 fp = open(buffer_access, O_RDONLY | O_NONBLOCK);
649 if (fp == -1) { /* TODO: If it isn't there make the node */
651 fprintf(stderr, "Failed to open %s\n", buffer_access);
655 for (j = 0; j < num_loops || num_loops < 0; j++) {
657 struct pollfd pfd = {
662 ret = poll(&pfd, 1, -1);
666 } else if (ret == 0) {
676 read_size = read(fp, data, toread * scan_size);
678 if (errno == EAGAIN) {
679 fprintf(stderr, "nothing available\n");
685 for (i = 0; i < read_size / scan_size; i++)
686 process_scan(data + scan_size * i, channels,
693 if (fp >= 0 && close(fp) == -1)
694 perror("Failed to close buffer");
698 for (i = num_channels - 1; i >= 0; i--) {
699 free(channels[i].name);
700 free(channels[i].generic_name);