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>
32 #include "iio_utils.h"
35 * size_from_channelarray() - calculate the storage size of a scan
36 * @channels: the channel info array
37 * @num_channels: size of the channel info array
39 * Has the side effect of filling the channels[i].location values used
40 * in processing the buffer output.
42 int size_from_channelarray(struct iio_channel_info *channels, int num_channels)
46 while (i < num_channels) {
47 if (bytes % channels[i].bytes == 0)
48 channels[i].location = bytes;
50 channels[i].location = bytes - bytes%channels[i].bytes
52 bytes = channels[i].location + channels[i].bytes;
58 void print2byte(int input, struct iio_channel_info *info)
60 /* First swap if incorrect endian */
63 input = be16toh((uint16_t)input);
65 input = le16toh((uint16_t)input);
67 /* shift before conversion to avoid sign extension
68 of left aligned data */
69 input = input >> info->shift;
70 if (info->is_signed) {
72 val &= (1 << info->bits_used) - 1;
73 val = (int16_t)(val << (16 - info->bits_used)) >>
74 (16 - info->bits_used);
76 (float)(val + info->offset)*info->scale);
79 val &= (1 << info->bits_used) - 1;
80 printf("%05f ", ((float)val + info->offset)*info->scale);
84 * process_scan() - print out the values in SI units
85 * @data: pointer to the start of the scan
86 * @infoarray: information about the channels. Note
87 * size_from_channelarray must have been called first to fill the
89 * @num_channels: the number of active channels
91 void process_scan(char *data,
92 struct iio_channel_info *infoarray,
96 for (k = 0; k < num_channels; k++)
97 switch (infoarray[k].bytes) {
98 /* only a few cases implemented so far */
100 print2byte(*(uint16_t *)(data + infoarray[k].location),
104 if (infoarray[k].is_signed) {
105 int64_t val = *(int64_t *)
107 infoarray[k].location);
108 if ((val >> infoarray[k].bits_used) & 1)
109 val = (val & infoarray[k].mask) |
111 /* special case for timestamp */
112 if (infoarray[k].scale == 1.0f &&
113 infoarray[k].offset == 0.0f)
114 printf(" %lld", val);
116 printf("%05f ", ((float)val +
117 infoarray[k].offset)*
127 int main(int argc, char **argv)
129 unsigned long num_loops = 2;
130 unsigned long timedelay = 1000000;
131 unsigned long buf_len = 128;
134 int ret, c, i, j, toread;
140 char *trigger_name = NULL, *device_name = NULL;
141 char *dev_dir_name, *buf_dir_name;
143 int datardytrigger = 1;
146 int dev_num, trig_num;
152 struct iio_channel_info *infoarray;
154 while ((c = getopt(argc, argv, "l:w:c:et:n:")) != -1) {
157 device_name = optarg;
160 trigger_name = optarg;
167 num_loops = strtoul(optarg, &dummy, 10);
170 timedelay = strtoul(optarg, &dummy, 10);
173 buf_len = strtoul(optarg, &dummy, 10);
180 if (device_name == NULL)
183 /* Find the device requested */
184 dev_num = find_type_by_name(device_name, "iio:device");
186 printf("Failed to find the %s\n", device_name);
190 printf("iio device number being used is %d\n", dev_num);
192 asprintf(&dev_dir_name, "%siio:device%d", iio_dir, dev_num);
193 if (trigger_name == NULL) {
195 * Build the trigger name. If it is device associated it's
196 * name is <device_name>_dev[n] where n matches the device
199 ret = asprintf(&trigger_name,
200 "%s-dev%d", device_name, dev_num);
207 /* Verify the trigger exists */
208 trig_num = find_type_by_name(trigger_name, "trigger");
210 printf("Failed to find the trigger %s\n", trigger_name);
212 goto error_free_triggername;
214 printf("iio trigger number being used is %d\n", trig_num);
217 * Parse the files in scan_elements to identify what channels are
220 ret = build_channel_array(dev_dir_name, &infoarray, &num_channels);
222 printf("Problem reading scan element information\n");
223 printf("diag %s\n", dev_dir_name);
224 goto error_free_triggername;
228 * Construct the directory name for the associated buffer.
229 * As we know that the lis3l02dq has only one buffer this may
230 * be built rather than found.
232 ret = asprintf(&buf_dir_name,
233 "%siio:device%d/buffer", iio_dir, dev_num);
236 goto error_free_triggername;
238 printf("%s %s\n", dev_dir_name, trigger_name);
239 /* Set the device trigger to be the data rdy trigger found above */
240 ret = write_sysfs_string_and_verify("trigger/current_trigger",
244 printf("Failed to write current_trigger file\n");
245 goto error_free_buf_dir_name;
248 /* Setup ring buffer parameters */
249 ret = write_sysfs_int("length", buf_dir_name, buf_len);
251 goto error_free_buf_dir_name;
253 /* Enable the buffer */
254 ret = write_sysfs_int("enable", buf_dir_name, 1);
256 goto error_free_buf_dir_name;
257 scan_size = size_from_channelarray(infoarray, num_channels);
258 data = malloc(scan_size*buf_len);
261 goto error_free_buf_dir_name;
264 ret = asprintf(&buffer_access, "/dev/iio:device%d", dev_num);
267 goto error_free_data;
270 /* Attempt to open non blocking the access dev */
271 fp = open(buffer_access, O_RDONLY | O_NONBLOCK);
272 if (fp == -1) { /*If it isn't there make the node */
273 printf("Failed to open %s\n", buffer_access);
275 goto error_free_buffer_access;
278 /* Wait for events 10 times */
279 for (j = 0; j < num_loops; j++) {
281 struct pollfd pfd = {
297 if (read_size == -EAGAIN) {
298 printf("nothing available\n");
301 for (i = 0; i < read_size/scan_size; i++)
302 process_scan(data + scan_size*i,
307 /* Stop the ring buffer */
308 ret = write_sysfs_int("enable", buf_dir_name, 0);
310 goto error_close_buffer_access;
312 /* Disconnect from the trigger - just write a dummy name.*/
313 write_sysfs_string("trigger/current_trigger",
314 dev_dir_name, "NULL");
316 error_close_buffer_access:
320 error_free_buffer_access:
322 error_free_buf_dir_name:
324 error_free_triggername: