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 * size_from_channelarray() - calculate the storage size of a scan
39 * @channels: the channel info array
40 * @num_channels: number of channels
42 * Has the side effect of filling the channels[i].location values used
43 * in processing the buffer output.
45 int size_from_channelarray(struct iio_channel_info
*channels
, int num_channels
)
50 while (i
< num_channels
) {
51 if (bytes
% channels
[i
].bytes
== 0)
52 channels
[i
].location
= bytes
;
54 channels
[i
].location
= bytes
- bytes
%channels
[i
].bytes
56 bytes
= channels
[i
].location
+ channels
[i
].bytes
;
62 void print2byte(int input
, struct iio_channel_info
*info
)
64 /* First swap if incorrect endian */
66 input
= be16toh((uint16_t)input
);
68 input
= le16toh((uint16_t)input
);
71 * Shift before conversion to avoid sign extension
72 * of left aligned data
74 input
>>= info
->shift
;
75 if (info
->is_signed
) {
78 val
&= (1 << info
->bits_used
) - 1;
79 val
= (int16_t)(val
<< (16 - info
->bits_used
)) >>
80 (16 - info
->bits_used
);
81 printf("%05f ", ((float)val
+ info
->offset
)*info
->scale
);
85 val
&= (1 << info
->bits_used
) - 1;
86 printf("%05f ", ((float)val
+ info
->offset
)*info
->scale
);
90 * process_scan() - print out the values in SI units
91 * @data: pointer to the start of the scan
92 * @channels: information about the channels. Note
93 * size_from_channelarray must have been called first to fill the
95 * @num_channels: number of channels
97 void process_scan(char *data
,
98 struct iio_channel_info
*channels
,
103 for (k
= 0; k
< num_channels
; k
++)
104 switch (channels
[k
].bytes
) {
105 /* only a few cases implemented so far */
107 print2byte(*(uint16_t *)(data
+ channels
[k
].location
),
111 if (!channels
[k
].is_signed
) {
112 uint32_t val
= *(uint32_t *)
113 (data
+ channels
[k
].location
);
114 printf("%05f ", ((float)val
+
121 if (channels
[k
].is_signed
) {
122 int64_t val
= *(int64_t *)
124 channels
[k
].location
);
125 if ((val
>> channels
[k
].bits_used
) & 1)
126 val
= (val
& channels
[k
].mask
) |
128 /* special case for timestamp */
129 if (channels
[k
].scale
== 1.0f
&&
130 channels
[k
].offset
== 0.0f
)
131 printf("%" PRId64
" ", val
);
133 printf("%05f ", ((float)val
+
144 int main(int argc
, char **argv
)
146 unsigned long num_loops
= 2;
147 unsigned long timedelay
= 1000000;
148 unsigned long buf_len
= 128;
150 int ret
, c
, i
, j
, toread
;
154 char *trigger_name
= NULL
, *device_name
= NULL
;
155 char *dev_dir_name
, *buf_dir_name
;
157 int datardytrigger
= 1;
160 int dev_num
, trig_num
;
167 struct iio_channel_info
*channels
;
169 while ((c
= getopt(argc
, argv
, "l:w:c:et:n:g")) != -1) {
172 device_name
= optarg
;
175 trigger_name
= optarg
;
182 num_loops
= strtoul(optarg
, &dummy
, 10);
185 timedelay
= strtoul(optarg
, &dummy
, 10);
188 buf_len
= strtoul(optarg
, &dummy
, 10);
198 if (device_name
== NULL
)
201 /* Find the device requested */
202 dev_num
= find_type_by_name(device_name
, "iio:device");
204 printf("Failed to find the %s\n", device_name
);
208 printf("iio device number being used is %d\n", dev_num
);
210 asprintf(&dev_dir_name
, "%siio:device%d", iio_dir
, dev_num
);
213 if (trigger_name
== NULL
) {
215 * Build the trigger name. If it is device associated
216 * its name is <device_name>_dev[n] where n matches
217 * the device number found above.
219 ret
= asprintf(&trigger_name
,
220 "%s-dev%d", device_name
, dev_num
);
227 /* Verify the trigger exists */
228 trig_num
= find_type_by_name(trigger_name
, "trigger");
230 printf("Failed to find the trigger %s\n", trigger_name
);
232 goto error_free_triggername
;
234 printf("iio trigger number being used is %d\n", trig_num
);
236 printf("trigger-less mode selected\n");
239 * Parse the files in scan_elements to identify what channels are
242 ret
= build_channel_array(dev_dir_name
, &channels
, &num_channels
);
244 printf("Problem reading scan element information\n");
245 printf("diag %s\n", dev_dir_name
);
246 goto error_free_triggername
;
250 * Construct the directory name for the associated buffer.
251 * As we know that the lis3l02dq has only one buffer this may
252 * be built rather than found.
254 ret
= asprintf(&buf_dir_name
,
255 "%siio:device%d/buffer", iio_dir
, dev_num
);
258 goto error_free_triggername
;
262 printf("%s %s\n", dev_dir_name
, trigger_name
);
263 /* Set the device trigger to be the data ready trigger found
265 ret
= write_sysfs_string_and_verify("trigger/current_trigger",
269 printf("Failed to write current_trigger file\n");
270 goto error_free_buf_dir_name
;
274 /* Setup ring buffer parameters */
275 ret
= write_sysfs_int("length", buf_dir_name
, buf_len
);
277 goto error_free_buf_dir_name
;
279 /* Enable the buffer */
280 ret
= write_sysfs_int("enable", buf_dir_name
, 1);
282 goto error_free_buf_dir_name
;
283 scan_size
= size_from_channelarray(channels
, num_channels
);
284 data
= malloc(scan_size
*buf_len
);
287 goto error_free_buf_dir_name
;
290 ret
= asprintf(&buffer_access
, "/dev/iio:device%d", dev_num
);
293 goto error_free_data
;
296 /* Attempt to open non blocking the access dev */
297 fp
= open(buffer_access
, O_RDONLY
| O_NONBLOCK
);
298 if (fp
== -1) { /* If it isn't there make the node */
299 printf("Failed to open %s\n", buffer_access
);
301 goto error_free_buffer_access
;
304 /* Wait for events 10 times */
305 for (j
= 0; j
< num_loops
; j
++) {
307 struct pollfd pfd
= {
324 if (errno
== -EAGAIN
) {
325 printf("nothing available\n");
330 for (i
= 0; i
< read_size
/scan_size
; i
++)
331 process_scan(data
+ scan_size
*i
,
336 /* Stop the buffer */
337 ret
= write_sysfs_int("enable", buf_dir_name
, 0);
339 goto error_close_buffer_access
;
342 /* Disconnect the trigger - just write a dummy name. */
343 write_sysfs_string("trigger/current_trigger",
344 dev_dir_name
, "NULL");
346 error_close_buffer_access
:
350 error_free_buffer_access
:
352 error_free_buf_dir_name
:
354 error_free_triggername
: