1 /* Industrialio buffer test code.
3 * Copyright (c) 2008 Jonathan Cameron
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
9 * This program is primarily intended as an example application.
10 * Reads the current buffer setup from sysfs and starts a short capture
11 * from the specified device, pretty printing the result after appropriate
14 * Command line parameters
15 * generic_buffer -n <device_name> -t <trigger_name>
16 * If trigger name is not specified the program assumes you want a dataready
17 * trigger associated with the device and goes looking for it.
29 #include <linux/types.h>
37 #include "iio_utils.h"
40 * enum autochan - state for the automatic channel enabling mechanism
43 AUTOCHANNELS_DISABLED
,
49 * size_from_channelarray() - calculate the storage size of a scan
50 * @channels: the channel info array
51 * @num_channels: number of channels
53 * Has the side effect of filling the channels[i].location values used
54 * in processing the buffer output.
56 int size_from_channelarray(struct iio_channel_info
*channels
, int num_channels
)
61 while (i
< num_channels
) {
62 if (bytes
% channels
[i
].bytes
== 0)
63 channels
[i
].location
= bytes
;
65 channels
[i
].location
= bytes
- bytes
% channels
[i
].bytes
68 bytes
= channels
[i
].location
+ channels
[i
].bytes
;
75 void print1byte(uint8_t input
, struct iio_channel_info
*info
)
78 * Shift before conversion to avoid sign extension
79 * of left aligned data
81 input
>>= info
->shift
;
83 if (info
->is_signed
) {
84 int8_t val
= (int8_t)(input
<< (8 - info
->bits_used
)) >>
85 (8 - info
->bits_used
);
86 printf("%05f ", ((float)val
+ info
->offset
) * info
->scale
);
88 printf("%05f ", ((float)input
+ info
->offset
) * info
->scale
);
92 void print2byte(uint16_t input
, struct iio_channel_info
*info
)
94 /* First swap if incorrect endian */
96 input
= be16toh(input
);
98 input
= le16toh(input
);
101 * Shift before conversion to avoid sign extension
102 * of left aligned data
104 input
>>= info
->shift
;
106 if (info
->is_signed
) {
107 int16_t val
= (int16_t)(input
<< (16 - info
->bits_used
)) >>
108 (16 - info
->bits_used
);
109 printf("%05f ", ((float)val
+ info
->offset
) * info
->scale
);
111 printf("%05f ", ((float)input
+ info
->offset
) * info
->scale
);
115 void print4byte(uint32_t input
, struct iio_channel_info
*info
)
117 /* First swap if incorrect endian */
119 input
= be32toh(input
);
121 input
= le32toh(input
);
124 * Shift before conversion to avoid sign extension
125 * of left aligned data
127 input
>>= info
->shift
;
129 if (info
->is_signed
) {
130 int32_t val
= (int32_t)(input
<< (32 - info
->bits_used
)) >>
131 (32 - info
->bits_used
);
132 printf("%05f ", ((float)val
+ info
->offset
) * info
->scale
);
134 printf("%05f ", ((float)input
+ info
->offset
) * info
->scale
);
138 void print8byte(uint64_t input
, struct iio_channel_info
*info
)
140 /* First swap if incorrect endian */
142 input
= be64toh(input
);
144 input
= le64toh(input
);
147 * Shift before conversion to avoid sign extension
148 * of left aligned data
150 input
>>= info
->shift
;
152 if (info
->is_signed
) {
153 int64_t val
= (int64_t)(input
<< (64 - info
->bits_used
)) >>
154 (64 - info
->bits_used
);
155 /* special case for timestamp */
156 if (info
->scale
== 1.0f
&& info
->offset
== 0.0f
)
157 printf("%" PRId64
" ", val
);
160 ((float)val
+ info
->offset
) * info
->scale
);
162 printf("%05f ", ((float)input
+ info
->offset
) * info
->scale
);
167 * process_scan() - print out the values in SI units
168 * @data: pointer to the start of the scan
169 * @channels: information about the channels.
170 * Note: size_from_channelarray must have been called first
171 * to fill the location offsets.
172 * @num_channels: number of channels
174 void process_scan(char *data
,
175 struct iio_channel_info
*channels
,
180 for (k
= 0; k
< num_channels
; k
++)
181 switch (channels
[k
].bytes
) {
182 /* only a few cases implemented so far */
184 print1byte(*(uint8_t *)(data
+ channels
[k
].location
),
188 print2byte(*(uint16_t *)(data
+ channels
[k
].location
),
192 print4byte(*(uint32_t *)(data
+ channels
[k
].location
),
196 print8byte(*(uint64_t *)(data
+ channels
[k
].location
),
205 static int enable_disable_all_channels(char *dev_dir_name
, int enable
)
207 const struct dirent
*ent
;
208 char scanelemdir
[256];
212 snprintf(scanelemdir
, sizeof(scanelemdir
),
213 FORMAT_SCAN_ELEMENTS_DIR
, dev_dir_name
);
214 scanelemdir
[sizeof(scanelemdir
)-1] = '\0';
216 dp
= opendir(scanelemdir
);
218 fprintf(stderr
, "Enabling/disabling channels: can't open %s\n",
224 while (ent
= readdir(dp
), ent
) {
225 if (iioutils_check_suffix(ent
->d_name
, "_en")) {
226 printf("%sabling: %s\n",
227 enable
? "En" : "Dis",
229 ret
= write_sysfs_int(ent
->d_name
, scanelemdir
,
232 fprintf(stderr
, "Failed to enable/disable %s\n",
237 if (closedir(dp
) == -1) {
238 perror("Enabling/disabling channels: "
239 "Failed to close directory");
245 void print_usage(void)
247 fprintf(stderr
, "Usage: generic_buffer [options]...\n"
248 "Capture, convert and output data from IIO device buffer\n"
249 " -a Auto-activate all available channels\n"
250 " -A Force-activate ALL channels\n"
251 " -c <n> Do n conversions\n"
252 " -e Disable wait for event (new data)\n"
253 " -g Use trigger-less mode\n"
254 " -l <n> Set buffer length to n samples\n"
255 " --device-name -n <name>\n"
256 " --device-num -N <num>\n"
257 " Set device by name or number (mandatory)\n"
258 " --trigger-name -t <name>\n"
259 " --trigger-num -T <num>\n"
260 " Set trigger by name or number\n"
261 " -w <n> Set delay between reads in us (event-less mode)\n");
264 enum autochan autochannels
= AUTOCHANNELS_DISABLED
;
265 char *dev_dir_name
= NULL
;
266 char *buf_dir_name
= NULL
;
267 bool current_trigger_set
= false;
273 /* Disable trigger */
274 if (dev_dir_name
&& current_trigger_set
) {
275 /* Disconnect the trigger - just write a dummy name. */
276 ret
= write_sysfs_string("trigger/current_trigger",
277 dev_dir_name
, "NULL");
279 fprintf(stderr
, "Failed to disable trigger: %s\n",
281 current_trigger_set
= false;
286 ret
= write_sysfs_int("enable", buf_dir_name
, 0);
288 fprintf(stderr
, "Failed to disable buffer: %s\n",
292 /* Disable channels if auto-enabled */
293 if (dev_dir_name
&& autochannels
== AUTOCHANNELS_ACTIVE
) {
294 ret
= enable_disable_all_channels(dev_dir_name
, 0);
296 fprintf(stderr
, "Failed to disable all channels\n");
297 autochannels
= AUTOCHANNELS_DISABLED
;
301 void sig_handler(int signum
)
303 fprintf(stderr
, "Caught signal %d\n", signum
);
308 void register_cleanup(void)
310 struct sigaction sa
= { .sa_handler
= sig_handler
};
311 const int signums
[] = { SIGINT
, SIGTERM
, SIGABRT
};
314 for (i
= 0; i
< ARRAY_SIZE(signums
); ++i
) {
315 ret
= sigaction(signums
[i
], &sa
, NULL
);
317 perror("Failed to register signal handler");
323 static const struct option longopts
[] = {
324 { "device-name", 1, 0, 'n' },
325 { "device-num", 1, 0, 'N' },
326 { "trigger-name", 1, 0, 't' },
327 { "trigger-num", 1, 0, 'T' },
331 int main(int argc
, char **argv
)
333 unsigned long num_loops
= 2;
334 unsigned long timedelay
= 1000000;
335 unsigned long buf_len
= 128;
337 int ret
, c
, i
, j
, toread
;
340 int num_channels
= 0;
341 char *trigger_name
= NULL
, *device_name
= NULL
;
345 int dev_num
= -1, trig_num
= -1;
346 char *buffer_access
= NULL
;
351 bool force_autochannels
= false;
353 struct iio_channel_info
*channels
= NULL
;
357 while ((c
= getopt_long(argc
, argv
, "aAc:egl:n:N:t:T:w:?", longopts
,
361 autochannels
= AUTOCHANNELS_ENABLED
;
364 autochannels
= AUTOCHANNELS_ENABLED
;
365 force_autochannels
= true;
369 num_loops
= strtoul(optarg
, &dummy
, 10);
384 buf_len
= strtoul(optarg
, &dummy
, 10);
392 device_name
= strdup(optarg
);
396 dev_num
= strtoul(optarg
, &dummy
, 10);
403 trigger_name
= strdup(optarg
);
407 trig_num
= strtoul(optarg
, &dummy
, 10);
413 timedelay
= strtoul(optarg
, &dummy
, 10);
426 /* Find the device requested */
427 if (dev_num
< 0 && !device_name
) {
428 fprintf(stderr
, "Device not set\n");
432 } else if (dev_num
>= 0 && device_name
) {
433 fprintf(stderr
, "Only one of --device-num or --device-name needs to be set\n");
437 } else if (dev_num
< 0) {
438 dev_num
= find_type_by_name(device_name
, "iio:device");
440 fprintf(stderr
, "Failed to find the %s\n", device_name
);
445 printf("iio device number being used is %d\n", dev_num
);
447 ret
= asprintf(&dev_dir_name
, "%siio:device%d", iio_dir
, dev_num
);
450 /* Fetch device_name if specified by number */
452 device_name
= malloc(IIO_MAX_NAME_LENGTH
);
457 ret
= read_sysfs_string("name", dev_dir_name
, device_name
);
459 fprintf(stderr
, "Failed to read name of device %d\n", dev_num
);
465 printf("trigger-less mode selected\n");
466 } else if (trig_num
>= 0) {
468 ret
= asprintf(&trig_dev_name
, "%strigger%d", iio_dir
, trig_num
);
472 trigger_name
= malloc(IIO_MAX_NAME_LENGTH
);
473 ret
= read_sysfs_string("name", trig_dev_name
, trigger_name
);
476 fprintf(stderr
, "Failed to read trigger%d name from\n", trig_num
);
479 printf("iio trigger number being used is %d\n", trig_num
);
483 * Build the trigger name. If it is device associated
484 * its name is <device_name>_dev[n] where n matches
485 * the device number found above.
487 ret
= asprintf(&trigger_name
,
488 "%s-dev%d", device_name
, dev_num
);
495 /* Look for this "-devN" trigger */
496 trig_num
= find_type_by_name(trigger_name
, "trigger");
498 /* OK try the simpler "-trigger" suffix instead */
500 ret
= asprintf(&trigger_name
,
501 "%s-trigger", device_name
);
508 trig_num
= find_type_by_name(trigger_name
, "trigger");
510 fprintf(stderr
, "Failed to find the trigger %s\n",
516 printf("iio trigger number being used is %d\n", trig_num
);
520 * Parse the files in scan_elements to identify what channels are
523 ret
= build_channel_array(dev_dir_name
, &channels
, &num_channels
);
525 fprintf(stderr
, "Problem reading scan element information\n"
526 "diag %s\n", dev_dir_name
);
529 if (num_channels
&& autochannels
== AUTOCHANNELS_ENABLED
&&
530 !force_autochannels
) {
531 fprintf(stderr
, "Auto-channels selected but some channels "
532 "are already activated in sysfs\n");
533 fprintf(stderr
, "Proceeding without activating any channels\n");
536 if ((!num_channels
&& autochannels
== AUTOCHANNELS_ENABLED
) ||
537 (autochannels
== AUTOCHANNELS_ENABLED
&& force_autochannels
)) {
538 fprintf(stderr
, "Enabling all channels\n");
540 ret
= enable_disable_all_channels(dev_dir_name
, 1);
542 fprintf(stderr
, "Failed to enable all channels\n");
546 /* This flags that we need to disable the channels again */
547 autochannels
= AUTOCHANNELS_ACTIVE
;
549 ret
= build_channel_array(dev_dir_name
, &channels
,
552 fprintf(stderr
, "Problem reading scan element "
554 "diag %s\n", dev_dir_name
);
558 fprintf(stderr
, "Still no channels after "
559 "auto-enabling, giving up\n");
564 if (!num_channels
&& autochannels
== AUTOCHANNELS_DISABLED
) {
566 "No channels are enabled, we have nothing to scan.\n");
567 fprintf(stderr
, "Enable channels manually in "
568 FORMAT_SCAN_ELEMENTS_DIR
569 "/*_en or pass -a to autoenable channels and "
570 "try again.\n", dev_dir_name
);
576 * Construct the directory name for the associated buffer.
577 * As we know that the lis3l02dq has only one buffer this may
578 * be built rather than found.
580 ret
= asprintf(&buf_dir_name
,
581 "%siio:device%d/buffer", iio_dir
, dev_num
);
588 printf("%s %s\n", dev_dir_name
, trigger_name
);
590 * Set the device trigger to be the data ready trigger found
593 ret
= write_sysfs_string_and_verify("trigger/current_trigger",
598 "Failed to write current_trigger file\n");
603 /* Setup ring buffer parameters */
604 ret
= write_sysfs_int("length", buf_dir_name
, buf_len
);
608 /* Enable the buffer */
609 ret
= write_sysfs_int("enable", buf_dir_name
, 1);
612 "Failed to enable buffer: %s\n", strerror(-ret
));
616 scan_size
= size_from_channelarray(channels
, num_channels
);
617 data
= malloc(scan_size
* buf_len
);
623 ret
= asprintf(&buffer_access
, "/dev/iio:device%d", dev_num
);
629 /* Attempt to open non blocking the access dev */
630 fp
= open(buffer_access
, O_RDONLY
| O_NONBLOCK
);
631 if (fp
== -1) { /* TODO: If it isn't there make the node */
633 fprintf(stderr
, "Failed to open %s\n", buffer_access
);
637 for (j
= 0; j
< num_loops
; j
++) {
639 struct pollfd pfd
= {
644 ret
= poll(&pfd
, 1, -1);
648 } else if (ret
== 0) {
658 read_size
= read(fp
, data
, toread
* scan_size
);
660 if (errno
== EAGAIN
) {
661 fprintf(stderr
, "nothing available\n");
667 for (i
= 0; i
< read_size
/ scan_size
; i
++)
668 process_scan(data
+ scan_size
* i
, channels
,
675 if (fp
>= 0 && close(fp
) == -1)
676 perror("Failed to close buffer");
680 for (i
= num_channels
- 1; i
>= 0; i
--) {
681 free(channels
[i
].name
);
682 free(channels
[i
].generic_name
);