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.
29 #include <linux/types.h>
35 #include "iio_utils.h"
38 * size_from_channelarray() - calculate the storage size of a scan
39 * @channels: the channel info array
40 * @num_channels: number of channels
42 * Has the side effect of filling the channels[i].location values used
43 * in processing the buffer output.
45 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(uint16_t input, struct iio_channel_info *info)
64 /* First swap if incorrect endian */
66 input = be16toh(input);
68 input = le16toh(input);
71 * Shift before conversion to avoid sign extension
72 * of left aligned data
74 input >>= info->shift;
76 if (info->is_signed) {
77 int16_t val = (int16_t)(input << (16 - info->bits_used)) >>
78 (16 - info->bits_used);
79 printf("%05f ", ((float)val + info->offset) * info->scale);
81 printf("%05f ", ((float)input + info->offset) * info->scale);
85 void print4byte(uint32_t input, struct iio_channel_info *info)
87 /* First swap if incorrect endian */
89 input = be32toh(input);
91 input = le32toh(input);
94 * Shift before conversion to avoid sign extension
95 * of left aligned data
97 input >>= info->shift;
99 if (info->is_signed) {
100 int32_t val = (int32_t)(input << (32 - info->bits_used)) >>
101 (32 - info->bits_used);
102 printf("%05f ", ((float)val + info->offset) * info->scale);
104 printf("%05f ", ((float)input + info->offset) * info->scale);
108 void print8byte(uint64_t input, struct iio_channel_info *info)
110 /* First swap if incorrect endian */
112 input = be64toh(input);
114 input = le64toh(input);
117 * Shift before conversion to avoid sign extension
118 * of left aligned data
120 input >>= info->shift;
122 if (info->is_signed) {
123 int64_t val = (int64_t)(input << (64 - info->bits_used)) >>
124 (64 - info->bits_used);
125 /* special case for timestamp */
126 if (info->scale == 1.0f && info->offset == 0.0f)
127 printf("%" PRId64 " ", val);
130 ((float)val + info->offset) * info->scale);
132 printf("%05f ", ((float)input + info->offset) * info->scale);
137 * process_scan() - print out the values in SI units
138 * @data: pointer to the start of the scan
139 * @channels: information about the channels. Note
140 * size_from_channelarray must have been called first to fill the
142 * @num_channels: number of channels
144 void process_scan(char *data,
145 struct iio_channel_info *channels,
150 for (k = 0; k < num_channels; k++)
151 switch (channels[k].bytes) {
152 /* only a few cases implemented so far */
154 print2byte(*(uint16_t *)(data + channels[k].location),
158 print4byte(*(uint32_t *)(data + channels[k].location),
162 print8byte(*(uint64_t *)(data + channels[k].location),
171 int main(int argc, char **argv)
173 unsigned long num_loops = 2;
174 unsigned long timedelay = 1000000;
175 unsigned long buf_len = 128;
177 int ret, c, i, j, toread;
181 char *trigger_name = NULL, *device_name = NULL;
182 char *dev_dir_name, *buf_dir_name;
184 int datardytrigger = 1;
187 int dev_num, trig_num;
194 struct iio_channel_info *channels;
196 while ((c = getopt(argc, argv, "l:w:c:et:n:g")) != -1) {
199 device_name = optarg;
202 trigger_name = optarg;
209 num_loops = strtoul(optarg, &dummy, 10);
212 timedelay = strtoul(optarg, &dummy, 10);
215 buf_len = strtoul(optarg, &dummy, 10);
225 if (device_name == NULL)
228 /* Find the device requested */
229 dev_num = find_type_by_name(device_name, "iio:device");
231 printf("Failed to find the %s\n", device_name);
235 printf("iio device number being used is %d\n", dev_num);
237 ret = asprintf(&dev_dir_name, "%siio:device%d", iio_dir, dev_num);
242 if (trigger_name == NULL) {
244 * Build the trigger name. If it is device associated
245 * its name is <device_name>_dev[n] where n matches
246 * the device number found above.
248 ret = asprintf(&trigger_name,
249 "%s-dev%d", device_name, dev_num);
252 goto error_free_dev_dir_name;
256 /* Verify the trigger exists */
257 trig_num = find_type_by_name(trigger_name, "trigger");
259 printf("Failed to find the trigger %s\n", trigger_name);
261 goto error_free_triggername;
263 printf("iio trigger number being used is %d\n", trig_num);
265 printf("trigger-less mode selected\n");
268 * Parse the files in scan_elements to identify what channels are
271 ret = build_channel_array(dev_dir_name, &channels, &num_channels);
273 printf("Problem reading scan element information\n");
274 printf("diag %s\n", dev_dir_name);
275 goto error_free_triggername;
279 * Construct the directory name for the associated buffer.
280 * As we know that the lis3l02dq has only one buffer this may
281 * be built rather than found.
283 ret = asprintf(&buf_dir_name,
284 "%siio:device%d/buffer", iio_dir, dev_num);
287 goto error_free_channels;
291 printf("%s %s\n", dev_dir_name, trigger_name);
292 /* Set the device trigger to be the data ready trigger found
294 ret = write_sysfs_string_and_verify("trigger/current_trigger",
298 printf("Failed to write current_trigger file\n");
299 goto error_free_buf_dir_name;
303 /* Setup ring buffer parameters */
304 ret = write_sysfs_int("length", buf_dir_name, buf_len);
306 goto error_free_buf_dir_name;
308 /* Enable the buffer */
309 ret = write_sysfs_int("enable", buf_dir_name, 1);
311 goto error_free_buf_dir_name;
312 scan_size = size_from_channelarray(channels, num_channels);
313 data = malloc(scan_size*buf_len);
316 goto error_free_buf_dir_name;
319 ret = asprintf(&buffer_access, "/dev/iio:device%d", dev_num);
322 goto error_free_data;
325 /* Attempt to open non blocking the access dev */
326 fp = open(buffer_access, O_RDONLY | O_NONBLOCK);
327 if (fp == -1) { /* If it isn't there make the node */
329 printf("Failed to open %s\n", buffer_access);
330 goto error_free_buffer_access;
333 /* Wait for events 10 times */
334 for (j = 0; j < num_loops; j++) {
336 struct pollfd pfd = {
353 if (errno == EAGAIN) {
354 printf("nothing available\n");
359 for (i = 0; i < read_size/scan_size; i++)
360 process_scan(data + scan_size*i,
365 /* Stop the buffer */
366 ret = write_sysfs_int("enable", buf_dir_name, 0);
368 goto error_close_buffer_access;
371 /* Disconnect the trigger - just write a dummy name. */
372 write_sysfs_string("trigger/current_trigger",
373 dev_dir_name, "NULL");
375 error_close_buffer_access:
377 error_free_buffer_access:
381 error_free_buf_dir_name:
384 for (i = num_channels - 1; i >= 0; i--) {
385 free(channels[i].name);
386 free(channels[i].generic_name);
389 error_free_triggername:
392 error_free_dev_dir_name: