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.
30 #include <linux/types.h>
36 #include "iio_utils.h"
39 * size_from_channelarray() - calculate the storage size of a scan
40 * @channels: the channel info array
41 * @num_channels: number of channels
43 * Has the side effect of filling the channels[i].location values used
44 * in processing the buffer output.
46 int size_from_channelarray(struct iio_channel_info
*channels
, int num_channels
)
51 while (i
< num_channels
) {
52 if (bytes
% channels
[i
].bytes
== 0)
53 channels
[i
].location
= bytes
;
55 channels
[i
].location
= bytes
- bytes
%channels
[i
].bytes
57 bytes
= channels
[i
].location
+ channels
[i
].bytes
;
63 void print2byte(int input
, struct iio_channel_info
*info
)
65 /* First swap if incorrect endian */
67 input
= be16toh((uint16_t)input
);
69 input
= le16toh((uint16_t)input
);
72 * Shift before conversion to avoid sign extension
73 * of left aligned data
75 input
= input
>> info
->shift
;
76 if (info
->is_signed
) {
79 val
&= (1 << info
->bits_used
) - 1;
80 val
= (int16_t)(val
<< (16 - info
->bits_used
)) >>
81 (16 - info
->bits_used
);
82 printf("%05f ", ((float)val
+ info
->offset
)*info
->scale
);
86 val
&= (1 << info
->bits_used
) - 1;
87 printf("%05f ", ((float)val
+ info
->offset
)*info
->scale
);
91 * process_scan() - print out the values in SI units
92 * @data: pointer to the start of the scan
93 * @channels: information about the channels. Note
94 * size_from_channelarray must have been called first to fill the
96 * @num_channels: number of channels
98 void process_scan(char *data
,
99 struct iio_channel_info
*channels
,
104 for (k
= 0; k
< num_channels
; k
++)
105 switch (channels
[k
].bytes
) {
106 /* only a few cases implemented so far */
108 print2byte(*(uint16_t *)(data
+ channels
[k
].location
),
112 if (!channels
[k
].is_signed
) {
113 uint32_t val
= *(uint32_t *)
114 (data
+ channels
[k
].location
);
115 printf("%05f ", ((float)val
+
122 if (channels
[k
].is_signed
) {
123 int64_t val
= *(int64_t *)
125 channels
[k
].location
);
126 if ((val
>> channels
[k
].bits_used
) & 1)
127 val
= (val
& channels
[k
].mask
) |
129 /* special case for timestamp */
130 if (channels
[k
].scale
== 1.0f
&&
131 channels
[k
].offset
== 0.0f
)
132 printf("%" PRId64
" ", val
);
134 printf("%05f ", ((float)val
+
145 int main(int argc
, char **argv
)
147 unsigned long num_loops
= 2;
148 unsigned long timedelay
= 1000000;
149 unsigned long buf_len
= 128;
151 int ret
, c
, i
, j
, toread
;
155 char *trigger_name
= NULL
, *device_name
= NULL
;
156 char *dev_dir_name
, *buf_dir_name
;
158 int datardytrigger
= 1;
161 int dev_num
, trig_num
;
168 struct iio_channel_info
*channels
;
170 while ((c
= getopt(argc
, argv
, "l:w:c:et:n:g")) != -1) {
173 device_name
= optarg
;
176 trigger_name
= optarg
;
183 num_loops
= strtoul(optarg
, &dummy
, 10);
186 timedelay
= strtoul(optarg
, &dummy
, 10);
189 buf_len
= strtoul(optarg
, &dummy
, 10);
199 if (device_name
== NULL
)
202 /* Find the device requested */
203 dev_num
= find_type_by_name(device_name
, "iio:device");
205 printf("Failed to find the %s\n", device_name
);
209 printf("iio device number being used is %d\n", dev_num
);
211 asprintf(&dev_dir_name
, "%siio:device%d", iio_dir
, dev_num
);
214 if (trigger_name
== NULL
) {
216 * Build the trigger name. If it is device associated
217 * its name is <device_name>_dev[n] where n matches
218 * the device number found above.
220 ret
= asprintf(&trigger_name
,
221 "%s-dev%d", device_name
, dev_num
);
228 /* Verify the trigger exists */
229 trig_num
= find_type_by_name(trigger_name
, "trigger");
231 printf("Failed to find the trigger %s\n", trigger_name
);
233 goto error_free_triggername
;
235 printf("iio trigger number being used is %d\n", trig_num
);
237 printf("trigger-less mode selected\n");
240 * Parse the files in scan_elements to identify what channels are
243 ret
= build_channel_array(dev_dir_name
, &channels
, &num_channels
);
245 printf("Problem reading scan element information\n");
246 printf("diag %s\n", dev_dir_name
);
247 goto error_free_triggername
;
251 * Construct the directory name for the associated buffer.
252 * As we know that the lis3l02dq has only one buffer this may
253 * be built rather than found.
255 ret
= asprintf(&buf_dir_name
,
256 "%siio:device%d/buffer", iio_dir
, dev_num
);
259 goto error_free_triggername
;
263 printf("%s %s\n", dev_dir_name
, trigger_name
);
264 /* Set the device trigger to be the data ready trigger found
266 ret
= write_sysfs_string_and_verify("trigger/current_trigger",
270 printf("Failed to write current_trigger file\n");
271 goto error_free_buf_dir_name
;
275 /* Setup ring buffer parameters */
276 ret
= write_sysfs_int("length", buf_dir_name
, buf_len
);
278 goto error_free_buf_dir_name
;
280 /* Enable the buffer */
281 ret
= write_sysfs_int("enable", buf_dir_name
, 1);
283 goto error_free_buf_dir_name
;
284 scan_size
= size_from_channelarray(channels
, num_channels
);
285 data
= malloc(scan_size
*buf_len
);
288 goto error_free_buf_dir_name
;
291 ret
= asprintf(&buffer_access
, "/dev/iio:device%d", dev_num
);
294 goto error_free_data
;
297 /* Attempt to open non blocking the access dev */
298 fp
= open(buffer_access
, O_RDONLY
| O_NONBLOCK
);
299 if (fp
== -1) { /* If it isn't there make the node */
300 printf("Failed to open %s\n", buffer_access
);
302 goto error_free_buffer_access
;
305 /* Wait for events 10 times */
306 for (j
= 0; j
< num_loops
; j
++) {
308 struct pollfd pfd
= {
325 if (errno
== -EAGAIN
) {
326 printf("nothing available\n");
331 for (i
= 0; i
< read_size
/scan_size
; i
++)
332 process_scan(data
+ scan_size
*i
,
337 /* Stop the buffer */
338 ret
= write_sysfs_int("enable", buf_dir_name
, 0);
340 goto error_close_buffer_access
;
343 /* Disconnect the trigger - just write a dummy name. */
344 write_sysfs_string("trigger/current_trigger",
345 dev_dir_name
, "NULL");
347 error_close_buffer_access
:
351 error_free_buffer_access
:
353 error_free_buf_dir_name
:
355 error_free_triggername
: