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.
28 #include <linux/types.h>
30 #include "iio_utils.h"
33 * size_from_channelarray() - calculate the storage size of a scan
34 * @channels: the channel info array
35 * @num_channels: size of the channel info array
37 * Has the side effect of filling the channels[i].location values used
38 * in processing the buffer output.
40 int size_from_channelarray(struct iio_channel_info
*channels
, int num_channels
)
44 while (i
< num_channels
) {
45 if (bytes
% channels
[i
].bytes
== 0)
46 channels
[i
].location
= bytes
;
48 channels
[i
].location
= bytes
- bytes
%channels
[i
].bytes
50 bytes
= channels
[i
].location
+ channels
[i
].bytes
;
57 * process_scan() - print out the values in SI units
58 * @data: pointer to the start of the scan
59 * @infoarray: information about the channels. Note
60 * size_from_channelarray must have been called first to fill the
62 * @num_channels: the number of active channels
64 void process_scan(char *data
,
65 struct iio_channel_info
*infoarray
,
69 for (k
= 0; k
< num_channels
; k
++)
70 switch (infoarray
[k
].bytes
) {
71 /* only a few cases implemented so far */
73 if (infoarray
[k
].is_signed
) {
74 int16_t val
= *(int16_t *)
76 + infoarray
[k
].location
);
77 if ((val
>> infoarray
[k
].bits_used
) & 1)
78 val
= (val
& infoarray
[k
].mask
) |
80 printf("%05f ", ((float)val
+
84 uint16_t val
= *(uint16_t *)
86 infoarray
[k
].location
);
87 val
= (val
& infoarray
[k
].mask
);
88 printf("%05f ", ((float)val
+
94 if (infoarray
[k
].is_signed
) {
95 int64_t val
= *(int64_t *)
97 infoarray
[k
].location
);
98 if ((val
>> infoarray
[k
].bits_used
) & 1)
99 val
= (val
& infoarray
[k
].mask
) |
101 /* special case for timestamp */
102 if (infoarray
[k
].scale
== 1.0f
&&
103 infoarray
[k
].offset
== 0.0f
)
104 printf(" %lld", val
);
106 printf("%05f ", ((float)val
+
107 infoarray
[k
].offset
)*
117 int main(int argc
, char **argv
)
119 unsigned long num_loops
= 2;
120 unsigned long timedelay
= 1000000;
121 unsigned long buf_len
= 128;
124 int ret
, c
, i
, j
, toread
;
130 char *trigger_name
= NULL
, *device_name
= NULL
;
131 char *dev_dir_name
, *buf_dir_name
;
133 int datardytrigger
= 1;
136 struct iio_event_data dat
;
137 int dev_num
, trig_num
;
138 char *buffer_access
, *buffer_event
;
143 struct iio_channel_info
*infoarray
;
145 while ((c
= getopt(argc
, argv
, "l:w:c:et:n:")) != -1) {
148 device_name
= optarg
;
151 trigger_name
= optarg
;
158 num_loops
= strtoul(optarg
, &dummy
, 10);
161 timedelay
= strtoul(optarg
, &dummy
, 10);
164 buf_len
= strtoul(optarg
, &dummy
, 10);
171 if (device_name
== NULL
)
174 /* Find the device requested */
175 dev_num
= find_type_by_name(device_name
, "device");
177 printf("Failed to find the %s\n", device_name
);
181 printf("iio device number being used is %d\n", dev_num
);
183 asprintf(&dev_dir_name
, "%sdevice%d", iio_dir
, dev_num
);
184 if (trigger_name
== NULL
) {
186 * Build the trigger name. If it is device associated it's
187 * name is <device_name>_dev[n] where n matches the device
190 ret
= asprintf(&trigger_name
,
191 "%s-dev%d", device_name
, dev_num
);
198 /* Verify the trigger exists */
199 trig_num
= find_type_by_name(trigger_name
, "trigger");
201 printf("Failed to find the trigger %s\n", trigger_name
);
203 goto error_free_triggername
;
205 printf("iio trigger number being used is %d\n", trig_num
);
208 * Parse the files in scan_elements to identify what channels are
211 ret
= build_channel_array(dev_dir_name
, &infoarray
, &num_channels
);
213 printf("Problem reading scan element information \n");
214 goto error_free_triggername
;
218 * Construct the directory name for the associated buffer.
219 * As we know that the lis3l02dq has only one buffer this may
220 * be built rather than found.
222 ret
= asprintf(&buf_dir_name
, "%sdevice%d:buffer0", iio_dir
, dev_num
);
225 goto error_free_triggername
;
227 printf("%s %s\n", dev_dir_name
, trigger_name
);
228 /* Set the device trigger to be the data rdy trigger found above */
229 ret
= write_sysfs_string_and_verify("trigger/current_trigger",
233 printf("Failed to write current_trigger file\n");
234 goto error_free_buf_dir_name
;
237 /* Setup ring buffer parameters */
238 ret
= write_sysfs_int("length", buf_dir_name
, buf_len
);
240 goto error_free_buf_dir_name
;
242 /* Enable the buffer */
243 ret
= write_sysfs_int("enable", buf_dir_name
, 1);
245 goto error_free_buf_dir_name
;
246 scan_size
= size_from_channelarray(infoarray
, num_channels
);
247 data
= malloc(scan_size
*buf_len
);
250 goto error_free_buf_dir_name
;
253 ret
= asprintf(&buffer_access
,
254 "/dev/device%d:buffer0:access0",
258 goto error_free_data
;
261 ret
= asprintf(&buffer_event
, "/dev/device%d:buffer0:event0", dev_num
);
264 goto error_free_buffer_access
;
266 /* Attempt to open non blocking the access dev */
267 fp
= open(buffer_access
, O_RDONLY
| O_NONBLOCK
);
268 if (fp
== -1) { /*If it isn't there make the node */
269 printf("Failed to open %s\n", buffer_access
);
271 goto error_free_buffer_event
;
273 /* Attempt to open the event access dev (blocking this time) */
274 fp_ev
= fopen(buffer_event
, "rb");
276 printf("Failed to open %s\n", buffer_event
);
278 goto error_close_buffer_access
;
281 /* Wait for events 10 times */
282 for (j
= 0; j
< num_loops
; j
++) {
284 read_size
= fread(&dat
,
286 sizeof(struct iio_event_data
),
289 case IIO_EVENT_CODE_RING_100_FULL
:
292 case IIO_EVENT_CODE_RING_75_FULL
:
293 toread
= buf_len
*3/4;
295 case IIO_EVENT_CODE_RING_50_FULL
:
299 printf("Unexpecteded event code\n");
310 if (read_size
== -EAGAIN
) {
311 printf("nothing available\n");
314 for (i
= 0; i
< read_size
/scan_size
; i
++)
315 process_scan(data
+ scan_size
*i
,
320 /* Stop the ring buffer */
321 ret
= write_sysfs_int("enable", buf_dir_name
, 0);
323 goto error_close_buffer_event
;
325 /* Disconnect from the trigger - just write a dummy name.*/
326 write_sysfs_string("trigger/current_trigger",
327 dev_dir_name
, "NULL");
329 error_close_buffer_event
:
331 error_close_buffer_access
:
335 error_free_buffer_access
:
337 error_free_buffer_event
:
339 error_free_buf_dir_name
:
341 error_free_triggername
: