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
)
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
= input
>> info
->shift
;
75 if (info
->is_signed
) {
77 val
&= (1 << info
->bits_used
) - 1;
78 val
= (int16_t)(val
<< (16 - info
->bits_used
)) >>
79 (16 - info
->bits_used
);
80 printf("%05f ", ((float)val
+ info
->offset
)*info
->scale
);
83 val
&= (1 << info
->bits_used
) - 1;
84 printf("%05f ", ((float)val
+ info
->offset
)*info
->scale
);
88 * process_scan() - print out the values in SI units
89 * @data: pointer to the start of the scan
90 * @channels: information about the channels. Note
91 * size_from_channelarray must have been called first to fill the
93 * @num_channels: number of channels
95 void process_scan(char *data
,
96 struct iio_channel_info
*channels
,
100 for (k
= 0; k
< num_channels
; k
++)
101 switch (channels
[k
].bytes
) {
102 /* only a few cases implemented so far */
104 print2byte(*(uint16_t *)(data
+ channels
[k
].location
),
108 if (!channels
[k
].is_signed
) {
109 uint32_t val
= *(uint32_t *)
110 (data
+ channels
[k
].location
);
111 printf("%05f ", ((float)val
+
118 if (channels
[k
].is_signed
) {
119 int64_t val
= *(int64_t *)
121 channels
[k
].location
);
122 if ((val
>> channels
[k
].bits_used
) & 1)
123 val
= (val
& channels
[k
].mask
) |
125 /* special case for timestamp */
126 if (channels
[k
].scale
== 1.0f
&&
127 channels
[k
].offset
== 0.0f
)
128 printf("%" PRId64
" ", val
);
130 printf("%05f ", ((float)val
+
141 int main(int argc
, char **argv
)
143 unsigned long num_loops
= 2;
144 unsigned long timedelay
= 1000000;
145 unsigned long buf_len
= 128;
147 int ret
, c
, i
, j
, toread
;
151 char *trigger_name
= NULL
, *device_name
= NULL
;
152 char *dev_dir_name
, *buf_dir_name
;
154 int datardytrigger
= 1;
157 int dev_num
, trig_num
;
163 struct iio_channel_info
*channels
;
165 while ((c
= getopt(argc
, argv
, "l:w:c:et:n:")) != -1) {
168 device_name
= optarg
;
171 trigger_name
= optarg
;
178 num_loops
= strtoul(optarg
, &dummy
, 10);
181 timedelay
= strtoul(optarg
, &dummy
, 10);
184 buf_len
= strtoul(optarg
, &dummy
, 10);
191 if (device_name
== NULL
)
194 /* Find the device requested */
195 dev_num
= find_type_by_name(device_name
, "iio:device");
197 printf("Failed to find the %s\n", device_name
);
201 printf("iio device number being used is %d\n", dev_num
);
203 asprintf(&dev_dir_name
, "%siio:device%d", iio_dir
, dev_num
);
204 if (trigger_name
== NULL
) {
206 * Build the trigger name. If it is device associated its
207 * name is <device_name>_dev[n] where n matches the device
210 ret
= asprintf(&trigger_name
,
211 "%s-dev%d", device_name
, dev_num
);
218 /* Verify the trigger exists */
219 trig_num
= find_type_by_name(trigger_name
, "trigger");
221 printf("Failed to find the trigger %s\n", trigger_name
);
223 goto error_free_triggername
;
225 printf("iio trigger number being used is %d\n", trig_num
);
228 * Parse the files in scan_elements to identify what channels are
231 ret
= build_channel_array(dev_dir_name
, &channels
, &num_channels
);
233 printf("Problem reading scan element information\n");
234 printf("diag %s\n", dev_dir_name
);
235 goto error_free_triggername
;
239 * Construct the directory name for the associated buffer.
240 * As we know that the lis3l02dq has only one buffer this may
241 * be built rather than found.
243 ret
= asprintf(&buf_dir_name
,
244 "%siio:device%d/buffer", iio_dir
, dev_num
);
247 goto error_free_triggername
;
249 printf("%s %s\n", dev_dir_name
, trigger_name
);
250 /* Set the device trigger to be the data ready trigger found above */
251 ret
= write_sysfs_string_and_verify("trigger/current_trigger",
255 printf("Failed to write current_trigger file\n");
256 goto error_free_buf_dir_name
;
259 /* Setup ring buffer parameters */
260 ret
= write_sysfs_int("length", buf_dir_name
, buf_len
);
262 goto error_free_buf_dir_name
;
264 /* Enable the buffer */
265 ret
= write_sysfs_int("enable", buf_dir_name
, 1);
267 goto error_free_buf_dir_name
;
268 scan_size
= size_from_channelarray(channels
, num_channels
);
269 data
= malloc(scan_size
*buf_len
);
272 goto error_free_buf_dir_name
;
275 ret
= asprintf(&buffer_access
, "/dev/iio:device%d", dev_num
);
278 goto error_free_data
;
281 /* Attempt to open non blocking the access dev */
282 fp
= open(buffer_access
, O_RDONLY
| O_NONBLOCK
);
283 if (fp
== -1) { /* If it isn't there make the node */
284 printf("Failed to open %s\n", buffer_access
);
286 goto error_free_buffer_access
;
289 /* Wait for events 10 times */
290 for (j
= 0; j
< num_loops
; j
++) {
292 struct pollfd pfd
= {
308 if (read_size
== -EAGAIN
) {
309 printf("nothing available\n");
312 for (i
= 0; i
< read_size
/scan_size
; i
++)
313 process_scan(data
+ scan_size
*i
,
318 /* Stop the buffer */
319 ret
= write_sysfs_int("enable", buf_dir_name
, 0);
321 goto error_close_buffer_access
;
323 /* Disconnect the trigger - just write a dummy name. */
324 write_sysfs_string("trigger/current_trigger",
325 dev_dir_name
, "NULL");
327 error_close_buffer_access
:
331 error_free_buffer_access
:
333 error_free_buf_dir_name
:
335 error_free_triggername
: