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
57 bytes = channels[i].location + channels[i].bytes;
64 void print1byte(uint8_t input, struct iio_channel_info *info)
67 * Shift before conversion to avoid sign extension
68 * of left aligned data
70 input >>= info->shift;
72 if (info->is_signed) {
73 int8_t val = (int8_t)(input << (8 - info->bits_used)) >>
74 (8 - info->bits_used);
75 printf("%05f ", ((float)val + info->offset) * info->scale);
77 printf("%05f ", ((float)input + info->offset) * info->scale);
81 void print2byte(uint16_t input, struct iio_channel_info *info)
83 /* First swap if incorrect endian */
85 input = be16toh(input);
87 input = le16toh(input);
90 * Shift before conversion to avoid sign extension
91 * of left aligned data
93 input >>= info->shift;
95 if (info->is_signed) {
96 int16_t val = (int16_t)(input << (16 - info->bits_used)) >>
97 (16 - info->bits_used);
98 printf("%05f ", ((float)val + info->offset) * info->scale);
100 printf("%05f ", ((float)input + info->offset) * info->scale);
104 void print4byte(uint32_t input, struct iio_channel_info *info)
106 /* First swap if incorrect endian */
108 input = be32toh(input);
110 input = le32toh(input);
113 * Shift before conversion to avoid sign extension
114 * of left aligned data
116 input >>= info->shift;
118 if (info->is_signed) {
119 int32_t val = (int32_t)(input << (32 - info->bits_used)) >>
120 (32 - info->bits_used);
121 printf("%05f ", ((float)val + info->offset) * info->scale);
123 printf("%05f ", ((float)input + info->offset) * info->scale);
127 void print8byte(uint64_t input, struct iio_channel_info *info)
129 /* First swap if incorrect endian */
131 input = be64toh(input);
133 input = le64toh(input);
136 * Shift before conversion to avoid sign extension
137 * of left aligned data
139 input >>= info->shift;
141 if (info->is_signed) {
142 int64_t val = (int64_t)(input << (64 - info->bits_used)) >>
143 (64 - info->bits_used);
144 /* special case for timestamp */
145 if (info->scale == 1.0f && info->offset == 0.0f)
146 printf("%" PRId64 " ", val);
149 ((float)val + info->offset) * info->scale);
151 printf("%05f ", ((float)input + info->offset) * info->scale);
156 * process_scan() - print out the values in SI units
157 * @data: pointer to the start of the scan
158 * @channels: information about the channels.
159 * Note: size_from_channelarray must have been called first
160 * to fill the location offsets.
161 * @num_channels: number of channels
163 void process_scan(char *data,
164 struct iio_channel_info *channels,
169 for (k = 0; k < num_channels; k++)
170 switch (channels[k].bytes) {
171 /* only a few cases implemented so far */
173 print1byte(*(uint8_t *)(data + channels[k].location),
177 print2byte(*(uint16_t *)(data + channels[k].location),
181 print4byte(*(uint32_t *)(data + channels[k].location),
185 print8byte(*(uint64_t *)(data + channels[k].location),
194 void print_usage(void)
196 fprintf(stderr, "Usage: generic_buffer [options]...\n"
197 "Capture, convert and output data from IIO device buffer\n"
198 " -c <n> Do n conversions\n"
199 " -e Disable wait for event (new data)\n"
200 " -g Use trigger-less mode\n"
201 " -l <n> Set buffer length to n samples\n"
202 " -n <name> Set device name (mandatory)\n"
203 " -t <name> Set trigger name\n"
204 " -w <n> Set delay between reads in us (event-less mode)\n");
207 int main(int argc, char **argv)
209 unsigned long num_loops = 2;
210 unsigned long timedelay = 1000000;
211 unsigned long buf_len = 128;
213 int ret, c, i, j, toread;
217 char *trigger_name = NULL, *device_name = NULL;
218 char *dev_dir_name, *buf_dir_name;
220 int datardytrigger = 1;
223 int dev_num, trig_num;
230 struct iio_channel_info *channels;
232 while ((c = getopt(argc, argv, "c:egl:n:t:w:")) != -1) {
236 num_loops = strtoul(optarg, &dummy, 10);
249 buf_len = strtoul(optarg, &dummy, 10);
255 device_name = optarg;
258 trigger_name = optarg;
263 timedelay = strtoul(optarg, &dummy, 10);
274 fprintf(stderr, "Device name not set\n");
279 /* Find the device requested */
280 dev_num = find_type_by_name(device_name, "iio:device");
282 fprintf(stderr, "Failed to find the %s\n", device_name);
286 printf("iio device number being used is %d\n", dev_num);
288 ret = asprintf(&dev_dir_name, "%siio:device%d", iio_dir, dev_num);
295 * Build the trigger name. If it is device associated
296 * its name is <device_name>_dev[n] where n matches
297 * the device number found above.
299 ret = asprintf(&trigger_name,
300 "%s-dev%d", device_name, dev_num);
303 goto error_free_dev_dir_name;
307 /* Verify the trigger exists */
308 trig_num = find_type_by_name(trigger_name, "trigger");
310 fprintf(stderr, "Failed to find the trigger %s\n",
313 goto error_free_triggername;
316 printf("iio trigger number being used is %d\n", trig_num);
318 printf("trigger-less mode selected\n");
322 * Parse the files in scan_elements to identify what channels are
325 ret = build_channel_array(dev_dir_name, &channels, &num_channels);
327 fprintf(stderr, "Problem reading scan element information\n"
328 "diag %s\n", dev_dir_name);
329 goto error_free_triggername;
333 "No channels are enabled, we have nothing to scan.\n");
334 fprintf(stderr, "Enable channels manually in "
335 FORMAT_SCAN_ELEMENTS_DIR
336 "/*_en and try again.\n", dev_dir_name);
338 goto error_free_triggername;
342 * Construct the directory name for the associated buffer.
343 * As we know that the lis3l02dq has only one buffer this may
344 * be built rather than found.
346 ret = asprintf(&buf_dir_name,
347 "%siio:device%d/buffer", iio_dir, dev_num);
350 goto error_free_channels;
354 printf("%s %s\n", dev_dir_name, trigger_name);
356 * Set the device trigger to be the data ready trigger found
359 ret = write_sysfs_string_and_verify("trigger/current_trigger",
364 "Failed to write current_trigger file\n");
365 goto error_free_buf_dir_name;
369 /* Setup ring buffer parameters */
370 ret = write_sysfs_int("length", buf_dir_name, buf_len);
372 goto error_free_buf_dir_name;
374 /* Enable the buffer */
375 ret = write_sysfs_int("enable", buf_dir_name, 1);
378 "Failed to enable buffer: %s\n", strerror(-ret));
379 goto error_free_buf_dir_name;
382 scan_size = size_from_channelarray(channels, num_channels);
383 data = malloc(scan_size * buf_len);
386 goto error_free_buf_dir_name;
389 ret = asprintf(&buffer_access, "/dev/iio:device%d", dev_num);
392 goto error_free_data;
395 /* Attempt to open non blocking the access dev */
396 fp = open(buffer_access, O_RDONLY | O_NONBLOCK);
397 if (fp == -1) { /* TODO: If it isn't there make the node */
399 fprintf(stderr, "Failed to open %s\n", buffer_access);
400 goto error_free_buffer_access;
403 for (j = 0; j < num_loops; j++) {
405 struct pollfd pfd = {
410 ret = poll(&pfd, 1, -1);
413 goto error_close_buffer_access;
414 } else if (ret == 0) {
424 read_size = read(fp, data, toread * scan_size);
426 if (errno == EAGAIN) {
427 fprintf(stderr, "nothing available\n");
433 for (i = 0; i < read_size / scan_size; i++)
434 process_scan(data + scan_size * i, channels,
438 /* Stop the buffer */
439 ret = write_sysfs_int("enable", buf_dir_name, 0);
441 goto error_close_buffer_access;
444 /* Disconnect the trigger - just write a dummy name. */
445 ret = write_sysfs_string("trigger/current_trigger",
446 dev_dir_name, "NULL");
448 fprintf(stderr, "Failed to write to %s\n",
451 error_close_buffer_access:
453 perror("Failed to close buffer");
455 error_free_buffer_access:
459 error_free_buf_dir_name:
462 for (i = num_channels - 1; i >= 0; i--) {
463 free(channels[i].name);
464 free(channels[i].generic_name);
467 error_free_triggername:
471 error_free_dev_dir_name: