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.
31 #include <linux/types.h>
37 #include "iio_utils.h"
40 * size_from_channelarray() - calculate the storage size of a scan
41 * @channels: the channel info array
42 * @num_channels: number of channels
44 * Has the side effect of filling the channels[i].location values used
45 * in processing the buffer output.
47 int size_from_channelarray(struct iio_channel_info *channels, int num_channels)
52 while (i < num_channels) {
53 if (bytes % channels[i].bytes == 0)
54 channels[i].location = bytes;
56 channels[i].location = bytes - bytes%channels[i].bytes
58 bytes = channels[i].location + channels[i].bytes;
64 void print2byte(int input, struct iio_channel_info *info)
66 /* First swap if incorrect endian */
68 input = be16toh((uint16_t)input);
70 input = le16toh((uint16_t)input);
73 * Shift before conversion to avoid sign extension
74 * of left aligned data
76 input = input >> info->shift;
77 if (info->is_signed) {
80 val &= (1 << info->bits_used) - 1;
81 val = (int16_t)(val << (16 - info->bits_used)) >>
82 (16 - info->bits_used);
83 printf("%05f ", ((float)val + info->offset)*info->scale);
87 val &= (1 << info->bits_used) - 1;
88 printf("%05f ", ((float)val + info->offset)*info->scale);
92 * process_scan() - print out the values in SI units
93 * @data: pointer to the start of the scan
94 * @channels: information about the channels. Note
95 * size_from_channelarray must have been called first to fill the
97 * @num_channels: number of channels
99 void process_scan(char *data,
100 struct iio_channel_info *channels,
105 for (k = 0; k < num_channels; k++)
106 switch (channels[k].bytes) {
107 /* only a few cases implemented so far */
109 print2byte(*(uint16_t *)(data + channels[k].location),
113 if (!channels[k].is_signed) {
114 uint32_t val = *(uint32_t *)
115 (data + channels[k].location);
116 printf("%05f ", ((float)val +
123 if (channels[k].is_signed) {
124 int64_t val = *(int64_t *)
126 channels[k].location);
127 if ((val >> channels[k].bits_used) & 1)
128 val = (val & channels[k].mask) |
130 /* special case for timestamp */
131 if (channels[k].scale == 1.0f &&
132 channels[k].offset == 0.0f)
133 printf("%" PRId64 " ", val);
135 printf("%05f ", ((float)val +
146 int main(int argc, char **argv)
148 unsigned long num_loops = 2;
149 unsigned long timedelay = 1000000;
150 unsigned long buf_len = 128;
152 int ret, c, i, j, toread;
156 char *trigger_name = NULL, *device_name = NULL;
157 char *dev_dir_name, *buf_dir_name;
159 int datardytrigger = 1;
162 int dev_num, trig_num;
169 struct iio_channel_info *channels;
171 while ((c = getopt(argc, argv, "l:w:c:et:n:g")) != -1) {
174 device_name = optarg;
177 trigger_name = optarg;
184 num_loops = strtoul(optarg, &dummy, 10);
187 timedelay = strtoul(optarg, &dummy, 10);
190 buf_len = strtoul(optarg, &dummy, 10);
200 if (device_name == NULL)
203 /* Find the device requested */
204 dev_num = find_type_by_name(device_name, "iio:device");
206 printf("Failed to find the %s\n", device_name);
210 printf("iio device number being used is %d\n", dev_num);
212 asprintf(&dev_dir_name, "%siio:device%d", iio_dir, dev_num);
215 if (trigger_name == NULL) {
217 * Build the trigger name. If it is device associated
218 * its name is <device_name>_dev[n] where n matches
219 * the device number found above.
221 ret = asprintf(&trigger_name,
222 "%s-dev%d", device_name, dev_num);
229 /* Verify the trigger exists */
230 trig_num = find_type_by_name(trigger_name, "trigger");
232 printf("Failed to find the trigger %s\n", trigger_name);
234 goto error_free_triggername;
236 printf("iio trigger number being used is %d\n", trig_num);
238 printf("trigger-less mode selected\n");
241 * Parse the files in scan_elements to identify what channels are
244 ret = build_channel_array(dev_dir_name, &channels, &num_channels);
246 printf("Problem reading scan element information\n");
247 printf("diag %s\n", dev_dir_name);
248 goto error_free_triggername;
252 * Construct the directory name for the associated buffer.
253 * As we know that the lis3l02dq has only one buffer this may
254 * be built rather than found.
256 ret = asprintf(&buf_dir_name,
257 "%siio:device%d/buffer", iio_dir, dev_num);
260 goto error_free_triggername;
264 printf("%s %s\n", dev_dir_name, trigger_name);
265 /* Set the device trigger to be the data ready trigger found
267 ret = write_sysfs_string_and_verify("trigger/current_trigger",
271 printf("Failed to write current_trigger file\n");
272 goto error_free_buf_dir_name;
276 /* Setup ring buffer parameters */
277 ret = write_sysfs_int("length", buf_dir_name, buf_len);
279 goto error_free_buf_dir_name;
281 /* Enable the buffer */
282 ret = write_sysfs_int("enable", buf_dir_name, 1);
284 goto error_free_buf_dir_name;
285 scan_size = size_from_channelarray(channels, num_channels);
286 data = malloc(scan_size*buf_len);
289 goto error_free_buf_dir_name;
292 ret = asprintf(&buffer_access, "/dev/iio:device%d", dev_num);
295 goto error_free_data;
298 /* Attempt to open non blocking the access dev */
299 fp = open(buffer_access, O_RDONLY | O_NONBLOCK);
300 if (fp == -1) { /* If it isn't there make the node */
301 printf("Failed to open %s\n", buffer_access);
303 goto error_free_buffer_access;
306 /* Wait for events 10 times */
307 for (j = 0; j < num_loops; j++) {
309 struct pollfd pfd = {
326 if (errno == -EAGAIN) {
327 printf("nothing available\n");
332 for (i = 0; i < read_size/scan_size; i++)
333 process_scan(data + scan_size*i,
338 /* Stop the buffer */
339 ret = write_sysfs_int("enable", buf_dir_name, 0);
341 goto error_close_buffer_access;
344 /* Disconnect the trigger - just write a dummy name. */
345 write_sysfs_string("trigger/current_trigger",
346 dev_dir_name, "NULL");
348 error_close_buffer_access:
352 error_free_buffer_access:
354 error_free_buf_dir_name:
356 error_free_triggername: