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>
35 #include "iio_utils.h"
38 * enum autochan - state for the automatic channel enabling mechanism
41 AUTOCHANNELS_DISABLED
,
47 * size_from_channelarray() - calculate the storage size of a scan
48 * @channels: the channel info array
49 * @num_channels: number of channels
51 * Has the side effect of filling the channels[i].location values used
52 * in processing the buffer output.
54 int size_from_channelarray(struct iio_channel_info
*channels
, int num_channels
)
59 while (i
< num_channels
) {
60 if (bytes
% channels
[i
].bytes
== 0)
61 channels
[i
].location
= bytes
;
63 channels
[i
].location
= bytes
- bytes
% channels
[i
].bytes
66 bytes
= channels
[i
].location
+ channels
[i
].bytes
;
73 void print1byte(uint8_t input
, struct iio_channel_info
*info
)
76 * Shift before conversion to avoid sign extension
77 * of left aligned data
79 input
>>= info
->shift
;
81 if (info
->is_signed
) {
82 int8_t val
= (int8_t)(input
<< (8 - info
->bits_used
)) >>
83 (8 - info
->bits_used
);
84 printf("%05f ", ((float)val
+ info
->offset
) * info
->scale
);
86 printf("%05f ", ((float)input
+ info
->offset
) * info
->scale
);
90 void print2byte(uint16_t input
, struct iio_channel_info
*info
)
92 /* First swap if incorrect endian */
94 input
= be16toh(input
);
96 input
= le16toh(input
);
99 * Shift before conversion to avoid sign extension
100 * of left aligned data
102 input
>>= info
->shift
;
104 if (info
->is_signed
) {
105 int16_t val
= (int16_t)(input
<< (16 - info
->bits_used
)) >>
106 (16 - info
->bits_used
);
107 printf("%05f ", ((float)val
+ info
->offset
) * info
->scale
);
109 printf("%05f ", ((float)input
+ info
->offset
) * info
->scale
);
113 void print4byte(uint32_t input
, struct iio_channel_info
*info
)
115 /* First swap if incorrect endian */
117 input
= be32toh(input
);
119 input
= le32toh(input
);
122 * Shift before conversion to avoid sign extension
123 * of left aligned data
125 input
>>= info
->shift
;
127 if (info
->is_signed
) {
128 int32_t val
= (int32_t)(input
<< (32 - info
->bits_used
)) >>
129 (32 - info
->bits_used
);
130 printf("%05f ", ((float)val
+ info
->offset
) * info
->scale
);
132 printf("%05f ", ((float)input
+ info
->offset
) * info
->scale
);
136 void print8byte(uint64_t input
, struct iio_channel_info
*info
)
138 /* First swap if incorrect endian */
140 input
= be64toh(input
);
142 input
= le64toh(input
);
145 * Shift before conversion to avoid sign extension
146 * of left aligned data
148 input
>>= info
->shift
;
150 if (info
->is_signed
) {
151 int64_t val
= (int64_t)(input
<< (64 - info
->bits_used
)) >>
152 (64 - info
->bits_used
);
153 /* special case for timestamp */
154 if (info
->scale
== 1.0f
&& info
->offset
== 0.0f
)
155 printf("%" PRId64
" ", val
);
158 ((float)val
+ info
->offset
) * info
->scale
);
160 printf("%05f ", ((float)input
+ info
->offset
) * info
->scale
);
165 * process_scan() - print out the values in SI units
166 * @data: pointer to the start of the scan
167 * @channels: information about the channels.
168 * Note: size_from_channelarray must have been called first
169 * to fill the location offsets.
170 * @num_channels: number of channels
172 void process_scan(char *data
,
173 struct iio_channel_info
*channels
,
178 for (k
= 0; k
< num_channels
; k
++)
179 switch (channels
[k
].bytes
) {
180 /* only a few cases implemented so far */
182 print1byte(*(uint8_t *)(data
+ channels
[k
].location
),
186 print2byte(*(uint16_t *)(data
+ channels
[k
].location
),
190 print4byte(*(uint32_t *)(data
+ channels
[k
].location
),
194 print8byte(*(uint64_t *)(data
+ channels
[k
].location
),
203 static int enable_disable_all_channels(char *dev_dir_name
, int enable
)
205 const struct dirent
*ent
;
206 char scanelemdir
[256];
210 snprintf(scanelemdir
, sizeof(scanelemdir
),
211 FORMAT_SCAN_ELEMENTS_DIR
, dev_dir_name
);
212 scanelemdir
[sizeof(scanelemdir
)-1] = '\0';
214 dp
= opendir(scanelemdir
);
216 fprintf(stderr
, "Enabling/disabling channels: can't open %s\n",
222 while (ent
= readdir(dp
), ent
) {
223 if (iioutils_check_suffix(ent
->d_name
, "_en")) {
224 printf("%sabling: %s\n",
225 enable
? "En" : "Dis",
227 ret
= write_sysfs_int(ent
->d_name
, scanelemdir
,
230 fprintf(stderr
, "Failed to enable/disable %s\n",
235 if (closedir(dp
) == -1) {
236 perror("Enabling/disabling channels: "
237 "Failed to close directory");
243 void print_usage(void)
245 fprintf(stderr
, "Usage: generic_buffer [options]...\n"
246 "Capture, convert and output data from IIO device buffer\n"
247 " -a Auto-activate all available channels\n"
248 " -c <n> Do n conversions\n"
249 " -e Disable wait for event (new data)\n"
250 " -g Use trigger-less mode\n"
251 " -l <n> Set buffer length to n samples\n"
252 " -n <name> Set device name (mandatory)\n"
253 " -t <name> Set trigger name\n"
254 " -w <n> Set delay between reads in us (event-less mode)\n");
257 int main(int argc
, char **argv
)
259 unsigned long num_loops
= 2;
260 unsigned long timedelay
= 1000000;
261 unsigned long buf_len
= 128;
263 int ret
, c
, i
, j
, toread
;
267 char *trigger_name
= NULL
, *device_name
= NULL
;
268 char *dev_dir_name
, *buf_dir_name
;
270 int datardytrigger
= 1;
273 int dev_num
, trig_num
;
278 enum autochan autochannels
= AUTOCHANNELS_DISABLED
;
281 struct iio_channel_info
*channels
;
283 while ((c
= getopt(argc
, argv
, "ac:egl:n:t:w:")) != -1) {
286 autochannels
= AUTOCHANNELS_ENABLED
;
290 num_loops
= strtoul(optarg
, &dummy
, 10);
303 buf_len
= strtoul(optarg
, &dummy
, 10);
309 device_name
= optarg
;
312 trigger_name
= optarg
;
317 timedelay
= strtoul(optarg
, &dummy
, 10);
328 fprintf(stderr
, "Device name not set\n");
333 /* Find the device requested */
334 dev_num
= find_type_by_name(device_name
, "iio:device");
336 fprintf(stderr
, "Failed to find the %s\n", device_name
);
340 printf("iio device number being used is %d\n", dev_num
);
342 ret
= asprintf(&dev_dir_name
, "%siio:device%d", iio_dir
, dev_num
);
349 * Build the trigger name. If it is device associated
350 * its name is <device_name>_dev[n] where n matches
351 * the device number found above.
353 ret
= asprintf(&trigger_name
,
354 "%s-dev%d", device_name
, dev_num
);
357 goto error_free_dev_dir_name
;
361 /* Look for this "-devN" trigger */
362 trig_num
= find_type_by_name(trigger_name
, "trigger");
364 /* OK try the simpler "-trigger" suffix instead */
366 ret
= asprintf(&trigger_name
,
367 "%s-trigger", device_name
);
370 goto error_free_dev_dir_name
;
374 trig_num
= find_type_by_name(trigger_name
, "trigger");
376 fprintf(stderr
, "Failed to find the trigger %s\n",
379 goto error_free_triggername
;
382 printf("iio trigger number being used is %d\n", trig_num
);
384 printf("trigger-less mode selected\n");
388 * Parse the files in scan_elements to identify what channels are
391 ret
= build_channel_array(dev_dir_name
, &channels
, &num_channels
);
393 fprintf(stderr
, "Problem reading scan element information\n"
394 "diag %s\n", dev_dir_name
);
395 goto error_free_triggername
;
397 if (num_channels
&& autochannels
== AUTOCHANNELS_ENABLED
) {
398 fprintf(stderr
, "Auto-channels selected but some channels "
399 "are already activated in sysfs\n");
400 fprintf(stderr
, "Proceeding without activating any channels\n");
403 if (!num_channels
&& autochannels
== AUTOCHANNELS_ENABLED
) {
405 "No channels are enabled, enabling all channels\n");
407 ret
= enable_disable_all_channels(dev_dir_name
, 1);
409 fprintf(stderr
, "Failed to enable all channels\n");
410 goto error_free_triggername
;
413 /* This flags that we need to disable the channels again */
414 autochannels
= AUTOCHANNELS_ACTIVE
;
416 ret
= build_channel_array(dev_dir_name
, &channels
,
419 fprintf(stderr
, "Problem reading scan element "
421 "diag %s\n", dev_dir_name
);
422 goto error_disable_channels
;
425 fprintf(stderr
, "Still no channels after "
426 "auto-enabling, giving up\n");
427 goto error_disable_channels
;
431 if (!num_channels
&& autochannels
== AUTOCHANNELS_DISABLED
) {
433 "No channels are enabled, we have nothing to scan.\n");
434 fprintf(stderr
, "Enable channels manually in "
435 FORMAT_SCAN_ELEMENTS_DIR
436 "/*_en or pass -a to autoenable channels and "
437 "try again.\n", dev_dir_name
);
439 goto error_free_triggername
;
443 * Construct the directory name for the associated buffer.
444 * As we know that the lis3l02dq has only one buffer this may
445 * be built rather than found.
447 ret
= asprintf(&buf_dir_name
,
448 "%siio:device%d/buffer", iio_dir
, dev_num
);
451 goto error_free_channels
;
455 printf("%s %s\n", dev_dir_name
, trigger_name
);
457 * Set the device trigger to be the data ready trigger found
460 ret
= write_sysfs_string_and_verify("trigger/current_trigger",
465 "Failed to write current_trigger file\n");
466 goto error_free_buf_dir_name
;
470 /* Setup ring buffer parameters */
471 ret
= write_sysfs_int("length", buf_dir_name
, buf_len
);
473 goto error_free_buf_dir_name
;
475 /* Enable the buffer */
476 ret
= write_sysfs_int("enable", buf_dir_name
, 1);
479 "Failed to enable buffer: %s\n", strerror(-ret
));
480 goto error_free_buf_dir_name
;
483 scan_size
= size_from_channelarray(channels
, num_channels
);
484 data
= malloc(scan_size
* buf_len
);
487 goto error_free_buf_dir_name
;
490 ret
= asprintf(&buffer_access
, "/dev/iio:device%d", dev_num
);
493 goto error_free_data
;
496 /* Attempt to open non blocking the access dev */
497 fp
= open(buffer_access
, O_RDONLY
| O_NONBLOCK
);
498 if (fp
== -1) { /* TODO: If it isn't there make the node */
500 fprintf(stderr
, "Failed to open %s\n", buffer_access
);
501 goto error_free_buffer_access
;
504 for (j
= 0; j
< num_loops
; j
++) {
506 struct pollfd pfd
= {
511 ret
= poll(&pfd
, 1, -1);
514 goto error_close_buffer_access
;
515 } else if (ret
== 0) {
525 read_size
= read(fp
, data
, toread
* scan_size
);
527 if (errno
== EAGAIN
) {
528 fprintf(stderr
, "nothing available\n");
534 for (i
= 0; i
< read_size
/ scan_size
; i
++)
535 process_scan(data
+ scan_size
* i
, channels
,
539 /* Stop the buffer */
540 ret
= write_sysfs_int("enable", buf_dir_name
, 0);
542 goto error_close_buffer_access
;
545 /* Disconnect the trigger - just write a dummy name. */
546 ret
= write_sysfs_string("trigger/current_trigger",
547 dev_dir_name
, "NULL");
549 fprintf(stderr
, "Failed to write to %s\n",
552 error_close_buffer_access
:
554 perror("Failed to close buffer");
556 error_free_buffer_access
:
560 error_free_buf_dir_name
:
563 for (i
= num_channels
- 1; i
>= 0; i
--) {
564 free(channels
[i
].name
);
565 free(channels
[i
].generic_name
);
568 error_free_triggername
:
571 error_disable_channels
:
572 if (autochannels
== AUTOCHANNELS_ACTIVE
) {
573 ret
= enable_disable_all_channels(dev_dir_name
, 0);
575 fprintf(stderr
, "Failed to disable all channels\n");
577 error_free_dev_dir_name
: