blob: 4eebb6616e5ca33aca62a036db63c4a38a5fad61 [file] [log] [blame]
Jonathan Camerone58537c2010-10-08 12:14:14 +01001/* Industrialio buffer test code.
2 *
3 * Copyright (c) 2008 Jonathan Cameron
4 *
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.
8 *
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
12 * conversion.
13 *
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.
18 *
19 */
20
21#include <unistd.h>
Roberta Dobrescubdcb31d2015-02-26 10:49:24 +020022#include <stdlib.h>
Jonathan Camerone58537c2010-10-08 12:14:14 +010023#include <dirent.h>
24#include <fcntl.h>
25#include <stdio.h>
26#include <errno.h>
27#include <sys/stat.h>
28#include <sys/dir.h>
29#include <linux/types.h>
Jonathan Cameron30268a32011-02-11 13:09:12 +000030#include <string.h>
Jonathan Cameron52615d42011-05-18 14:41:19 +010031#include <poll.h>
Jonathan Cameron117cf8b2011-12-04 19:10:59 +000032#include <endian.h>
Peter Meerwaldbb233782012-06-25 23:12:17 +020033#include <getopt.h>
Peter Meerwald1bcdfbc2012-06-25 23:12:16 +020034#include <inttypes.h>
Jonathan Camerone58537c2010-10-08 12:14:14 +010035#include "iio_utils.h"
36
Jonathan Camerone58537c2010-10-08 12:14:14 +010037/**
38 * size_from_channelarray() - calculate the storage size of a scan
Peter Meerwaldd8da0ee2012-06-25 23:12:14 +020039 * @channels: the channel info array
40 * @num_channels: number of channels
Jonathan Camerone58537c2010-10-08 12:14:14 +010041 *
42 * Has the side effect of filling the channels[i].location values used
43 * in processing the buffer output.
44 **/
45int size_from_channelarray(struct iio_channel_info *channels, int num_channels)
46{
47 int bytes = 0;
48 int i = 0;
Melike Yurtogluff39a252014-10-03 23:34:50 +030049
Jonathan Camerone58537c2010-10-08 12:14:14 +010050 while (i < num_channels) {
51 if (bytes % channels[i].bytes == 0)
52 channels[i].location = bytes;
53 else
54 channels[i].location = bytes - bytes%channels[i].bytes
55 + channels[i].bytes;
56 bytes = channels[i].location + channels[i].bytes;
57 i++;
58 }
59 return bytes;
60}
61
Hartmut Knaack8e926132015-05-31 14:39:58 +020062void print2byte(uint16_t input, struct iio_channel_info *info)
Jonathan Cameron52615d42011-05-18 14:41:19 +010063{
Jonathan Cameron117cf8b2011-12-04 19:10:59 +000064 /* First swap if incorrect endian */
Jonathan Cameron117cf8b2011-12-04 19:10:59 +000065 if (info->be)
Hartmut Knaack8e926132015-05-31 14:39:58 +020066 input = be16toh(input);
Jonathan Cameron117cf8b2011-12-04 19:10:59 +000067 else
Hartmut Knaack8e926132015-05-31 14:39:58 +020068 input = le16toh(input);
Jonathan Cameron117cf8b2011-12-04 19:10:59 +000069
Peter Meerwaldd8da0ee2012-06-25 23:12:14 +020070 /*
71 * Shift before conversion to avoid sign extension
72 * of left aligned data
73 */
Aya Mahfouz1525ecf2015-02-26 11:45:26 +020074 input >>= info->shift;
Hartmut Knaack8e926132015-05-31 14:39:58 +020075 input &= info->mask;
Jonathan Cameron52615d42011-05-18 14:41:19 +010076 if (info->is_signed) {
Hartmut Knaack8e926132015-05-31 14:39:58 +020077 int16_t val = (int16_t)(input << (16 - info->bits_used)) >>
78 (16 - info->bits_used);
79 printf("%05f ", ((float)val + info->offset) * info->scale);
Jonathan Cameron52615d42011-05-18 14:41:19 +010080 } else {
Hartmut Knaack8e926132015-05-31 14:39:58 +020081 printf("%05f ", ((float)input + info->offset) * info->scale);
Jonathan Cameron52615d42011-05-18 14:41:19 +010082 }
83}
Hartmut Knaack8e926132015-05-31 14:39:58 +020084
85void print4byte(uint32_t input, struct iio_channel_info *info)
86{
87 /* First swap if incorrect endian */
88 if (info->be)
89 input = be32toh(input);
90 else
91 input = le32toh(input);
92
93 /*
94 * Shift before conversion to avoid sign extension
95 * of left aligned data
96 */
97 input >>= info->shift;
98 input &= info->mask;
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);
103 } else {
104 printf("%05f ", ((float)input + info->offset) * info->scale);
105 }
106}
107
108void print8byte(uint64_t input, struct iio_channel_info *info)
109{
110 /* First swap if incorrect endian */
111 if (info->be)
112 input = be64toh(input);
113 else
114 input = le64toh(input);
115
116 /*
117 * Shift before conversion to avoid sign extension
118 * of left aligned data
119 */
120 input >>= info->shift;
121 input &= info->mask;
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);
128 else
129 printf("%05f ",
130 ((float)val + info->offset) * info->scale);
131 } else {
132 printf("%05f ", ((float)input + info->offset) * info->scale);
133 }
134}
135
Jonathan Camerone58537c2010-10-08 12:14:14 +0100136/**
137 * process_scan() - print out the values in SI units
138 * @data: pointer to the start of the scan
Peter Meerwaldd8da0ee2012-06-25 23:12:14 +0200139 * @channels: information about the channels. Note
Jonathan Camerone58537c2010-10-08 12:14:14 +0100140 * size_from_channelarray must have been called first to fill the
141 * location offsets.
Peter Meerwaldd8da0ee2012-06-25 23:12:14 +0200142 * @num_channels: number of channels
Jonathan Camerone58537c2010-10-08 12:14:14 +0100143 **/
144void process_scan(char *data,
Peter Meerwaldd8da0ee2012-06-25 23:12:14 +0200145 struct iio_channel_info *channels,
Jonathan Camerone58537c2010-10-08 12:14:14 +0100146 int num_channels)
147{
148 int k;
Melike Yurtogluff39a252014-10-03 23:34:50 +0300149
Jonathan Camerone58537c2010-10-08 12:14:14 +0100150 for (k = 0; k < num_channels; k++)
Peter Meerwaldd8da0ee2012-06-25 23:12:14 +0200151 switch (channels[k].bytes) {
Jonathan Camerone58537c2010-10-08 12:14:14 +0100152 /* only a few cases implemented so far */
153 case 2:
Peter Meerwaldd8da0ee2012-06-25 23:12:14 +0200154 print2byte(*(uint16_t *)(data + channels[k].location),
155 &channels[k]);
Jonathan Camerone58537c2010-10-08 12:14:14 +0100156 break;
Marek Vasut6cffc1f2012-08-12 16:21:00 +0100157 case 4:
Hartmut Knaack8e926132015-05-31 14:39:58 +0200158 print4byte(*(uint32_t *)(data + channels[k].location),
159 &channels[k]);
Marek Vasut6cffc1f2012-08-12 16:21:00 +0100160 break;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100161 case 8:
Hartmut Knaack8e926132015-05-31 14:39:58 +0200162 print8byte(*(uint64_t *)(data + channels[k].location),
163 &channels[k]);
Jonathan Camerone58537c2010-10-08 12:14:14 +0100164 break;
165 default:
166 break;
167 }
168 printf("\n");
169}
170
Hartmut Knaacke06e3d72015-05-31 14:40:22 +0200171void print_usage(void)
172{
173 printf("Usage: generic_buffer [options]...\n"
174 "Capture, convert and output data from IIO device buffer\n"
175 " -c <n> Do n conversions\n"
176 " -e Disable wait for event (new data)\n"
177 " -g Use trigger-less mode\n"
178 " -l <n> Set buffer length to n samples\n"
179 " -n <name> Set device name (mandatory)\n"
180 " -t <name> Set trigger name\n"
181 " -w <n> Set delay between reads in us (event-less mode)\n");
182}
183
Jonathan Camerone58537c2010-10-08 12:14:14 +0100184int main(int argc, char **argv)
185{
Jonathan Cameron96df9792011-02-11 13:09:13 +0000186 unsigned long num_loops = 2;
187 unsigned long timedelay = 1000000;
188 unsigned long buf_len = 128;
189
Jonathan Camerone58537c2010-10-08 12:14:14 +0100190 int ret, c, i, j, toread;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100191 int fp;
192
193 int num_channels;
194 char *trigger_name = NULL, *device_name = NULL;
195 char *dev_dir_name, *buf_dir_name;
196
197 int datardytrigger = 1;
198 char *data;
Jonathan Cameronc77b3812011-04-15 18:56:00 +0100199 ssize_t read_size;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100200 int dev_num, trig_num;
Jonathan Cameron52615d42011-05-18 14:41:19 +0100201 char *buffer_access;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100202 int scan_size;
Jonathan Cameron30268a32011-02-11 13:09:12 +0000203 int noevents = 0;
Karol Wronab6d5be572014-11-04 15:29:39 +0100204 int notrigger = 0;
Jonathan Cameron96df9792011-02-11 13:09:13 +0000205 char *dummy;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100206
Peter Meerwaldd8da0ee2012-06-25 23:12:14 +0200207 struct iio_channel_info *channels;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100208
Hartmut Knaacke06e3d72015-05-31 14:40:22 +0200209 while ((c = getopt(argc, argv, "c:egl:n:t:w:")) != -1) {
Jonathan Camerone58537c2010-10-08 12:14:14 +0100210 switch (c) {
Jonathan Cameron96df9792011-02-11 13:09:13 +0000211 case 'c':
Hartmut Knaackc8ce9902015-05-31 14:40:03 +0200212 errno = 0;
Jonathan Cameron96df9792011-02-11 13:09:13 +0000213 num_loops = strtoul(optarg, &dummy, 10);
Hartmut Knaackc8ce9902015-05-31 14:40:03 +0200214 if (errno)
215 return -errno;
Jonathan Cameron96df9792011-02-11 13:09:13 +0000216 break;
Hartmut Knaacke06e3d72015-05-31 14:40:22 +0200217 case 'e':
218 noevents = 1;
219 break;
220 case 'g':
221 notrigger = 1;
Jonathan Cameron96df9792011-02-11 13:09:13 +0000222 break;
223 case 'l':
Hartmut Knaackc8ce9902015-05-31 14:40:03 +0200224 errno = 0;
Jonathan Cameron96df9792011-02-11 13:09:13 +0000225 buf_len = strtoul(optarg, &dummy, 10);
Hartmut Knaackc8ce9902015-05-31 14:40:03 +0200226 if (errno)
227 return -errno;
Jonathan Cameron96df9792011-02-11 13:09:13 +0000228 break;
Hartmut Knaacke06e3d72015-05-31 14:40:22 +0200229 case 'n':
230 device_name = optarg;
231 break;
232 case 't':
233 trigger_name = optarg;
234 datardytrigger = 0;
235 break;
236 case 'w':
237 errno = 0;
238 timedelay = strtoul(optarg, &dummy, 10);
239 if (errno)
240 return -errno;
Karol Wronab6d5be572014-11-04 15:29:39 +0100241 break;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100242 case '?':
Hartmut Knaacke06e3d72015-05-31 14:40:22 +0200243 print_usage();
Jonathan Camerone58537c2010-10-08 12:14:14 +0100244 return -1;
245 }
246 }
247
Hartmut Knaacke06e3d72015-05-31 14:40:22 +0200248 if (device_name == NULL) {
249 printf("Device name not set\n");
250 print_usage();
Michael Hennerich065896e2011-02-24 16:34:50 +0100251 return -1;
Hartmut Knaacke06e3d72015-05-31 14:40:22 +0200252 }
Michael Hennerich065896e2011-02-24 16:34:50 +0100253
Jonathan Camerone58537c2010-10-08 12:14:14 +0100254 /* Find the device requested */
Jonathan Cameron1aa04272011-08-30 12:32:47 +0100255 dev_num = find_type_by_name(device_name, "iio:device");
Jonathan Camerone58537c2010-10-08 12:14:14 +0100256 if (dev_num < 0) {
257 printf("Failed to find the %s\n", device_name);
Hartmut Knaack0e799872015-05-31 14:40:17 +0200258 return dev_num;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100259 }
260 printf("iio device number being used is %d\n", dev_num);
261
Hartmut Knaacke9e45b42015-05-31 14:40:02 +0200262 ret = asprintf(&dev_dir_name, "%siio:device%d", iio_dir, dev_num);
263 if (ret < 0)
264 return -ENOMEM;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100265
Karol Wronab6d5be572014-11-04 15:29:39 +0100266 if (!notrigger) {
267 if (trigger_name == NULL) {
268 /*
269 * Build the trigger name. If it is device associated
270 * its name is <device_name>_dev[n] where n matches
271 * the device number found above.
272 */
273 ret = asprintf(&trigger_name,
274 "%s-dev%d", device_name, dev_num);
275 if (ret < 0) {
276 ret = -ENOMEM;
Hartmut Knaackd3ccfc42015-05-31 14:39:42 +0200277 goto error_free_dev_dir_name;
Karol Wronab6d5be572014-11-04 15:29:39 +0100278 }
279 }
280
281 /* Verify the trigger exists */
282 trig_num = find_type_by_name(trigger_name, "trigger");
283 if (trig_num < 0) {
284 printf("Failed to find the trigger %s\n", trigger_name);
Hartmut Knaacke83a47c2015-05-31 14:39:57 +0200285 ret = trig_num;
Karol Wronab6d5be572014-11-04 15:29:39 +0100286 goto error_free_triggername;
287 }
288 printf("iio trigger number being used is %d\n", trig_num);
289 } else
290 printf("trigger-less mode selected\n");
Jonathan Camerone58537c2010-10-08 12:14:14 +0100291
292 /*
293 * Parse the files in scan_elements to identify what channels are
294 * present
295 */
Peter Meerwaldd8da0ee2012-06-25 23:12:14 +0200296 ret = build_channel_array(dev_dir_name, &channels, &num_channels);
Jonathan Camerone58537c2010-10-08 12:14:14 +0100297 if (ret) {
Jonathan Cameron52615d42011-05-18 14:41:19 +0100298 printf("Problem reading scan element information\n");
Jonathan Cameron1aa04272011-08-30 12:32:47 +0100299 printf("diag %s\n", dev_dir_name);
Jonathan Camerone58537c2010-10-08 12:14:14 +0100300 goto error_free_triggername;
301 }
302
303 /*
304 * Construct the directory name for the associated buffer.
305 * As we know that the lis3l02dq has only one buffer this may
306 * be built rather than found.
307 */
Jonathan Cameron1aa04272011-08-30 12:32:47 +0100308 ret = asprintf(&buf_dir_name,
309 "%siio:device%d/buffer", iio_dir, dev_num);
Jonathan Camerone58537c2010-10-08 12:14:14 +0100310 if (ret < 0) {
311 ret = -ENOMEM;
Hartmut Knaack63f05c82015-05-31 14:39:44 +0200312 goto error_free_channels;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100313 }
Karol Wronab6d5be572014-11-04 15:29:39 +0100314
315 if (!notrigger) {
316 printf("%s %s\n", dev_dir_name, trigger_name);
317 /* Set the device trigger to be the data ready trigger found
318 * above */
319 ret = write_sysfs_string_and_verify("trigger/current_trigger",
320 dev_dir_name,
321 trigger_name);
322 if (ret < 0) {
323 printf("Failed to write current_trigger file\n");
324 goto error_free_buf_dir_name;
325 }
Jonathan Camerone58537c2010-10-08 12:14:14 +0100326 }
327
328 /* Setup ring buffer parameters */
329 ret = write_sysfs_int("length", buf_dir_name, buf_len);
330 if (ret < 0)
331 goto error_free_buf_dir_name;
332
333 /* Enable the buffer */
334 ret = write_sysfs_int("enable", buf_dir_name, 1);
335 if (ret < 0)
336 goto error_free_buf_dir_name;
Peter Meerwaldd8da0ee2012-06-25 23:12:14 +0200337 scan_size = size_from_channelarray(channels, num_channels);
Jonathan Camerone58537c2010-10-08 12:14:14 +0100338 data = malloc(scan_size*buf_len);
339 if (!data) {
340 ret = -ENOMEM;
341 goto error_free_buf_dir_name;
342 }
343
Jonathan Cameron1aa04272011-08-30 12:32:47 +0100344 ret = asprintf(&buffer_access, "/dev/iio:device%d", dev_num);
Jonathan Camerone58537c2010-10-08 12:14:14 +0100345 if (ret < 0) {
346 ret = -ENOMEM;
347 goto error_free_data;
348 }
349
Jonathan Camerone58537c2010-10-08 12:14:14 +0100350 /* Attempt to open non blocking the access dev */
351 fp = open(buffer_access, O_RDONLY | O_NONBLOCK);
Peter Meerwaldd8da0ee2012-06-25 23:12:14 +0200352 if (fp == -1) { /* If it isn't there make the node */
Jonathan Camerone58537c2010-10-08 12:14:14 +0100353 ret = -errno;
Hartmut Knaack2b6a6e62015-05-31 14:39:48 +0200354 printf("Failed to open %s\n", buffer_access);
Jonathan Cameron52615d42011-05-18 14:41:19 +0100355 goto error_free_buffer_access;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100356 }
357
358 /* Wait for events 10 times */
359 for (j = 0; j < num_loops; j++) {
Jonathan Cameron30268a32011-02-11 13:09:12 +0000360 if (!noevents) {
Jonathan Cameron52615d42011-05-18 14:41:19 +0100361 struct pollfd pfd = {
362 .fd = fp,
363 .events = POLLIN,
364 };
365
Hartmut Knaack6bb7cac2015-05-31 14:40:04 +0200366 ret = poll(&pfd, 1, -1);
367 if (ret < 0) {
368 ret = -errno;
369 goto error_close_buffer_access;
370 } else if (ret == 0) {
371 continue;
372 }
373
Jonathan Cameron52615d42011-05-18 14:41:19 +0100374 toread = buf_len;
375
Jonathan Cameron30268a32011-02-11 13:09:12 +0000376 } else {
Jonathan Cameron96df9792011-02-11 13:09:13 +0000377 usleep(timedelay);
Jonathan Cameron30268a32011-02-11 13:09:12 +0000378 toread = 64;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100379 }
Jonathan Cameron30268a32011-02-11 13:09:12 +0000380
Jonathan Camerone58537c2010-10-08 12:14:14 +0100381 read_size = read(fp,
382 data,
383 toread*scan_size);
Peter Meerwald97b603a2014-12-06 06:00:00 +0000384 if (read_size < 0) {
Hartmut Knaack87499482015-05-31 14:39:56 +0200385 if (errno == EAGAIN) {
Peter Meerwald97b603a2014-12-06 06:00:00 +0000386 printf("nothing available\n");
387 continue;
388 } else
389 break;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100390 }
391 for (i = 0; i < read_size/scan_size; i++)
392 process_scan(data + scan_size*i,
Peter Meerwaldd8da0ee2012-06-25 23:12:14 +0200393 channels,
Jonathan Camerone58537c2010-10-08 12:14:14 +0100394 num_channels);
395 }
396
Peter Meerwaldd8da0ee2012-06-25 23:12:14 +0200397 /* Stop the buffer */
Jonathan Camerone58537c2010-10-08 12:14:14 +0100398 ret = write_sysfs_int("enable", buf_dir_name, 0);
399 if (ret < 0)
Jonathan Cameron52615d42011-05-18 14:41:19 +0100400 goto error_close_buffer_access;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100401
Karol Wronab6d5be572014-11-04 15:29:39 +0100402 if (!notrigger)
403 /* Disconnect the trigger - just write a dummy name. */
Hartmut Knaack6bb7cac2015-05-31 14:40:04 +0200404 ret = write_sysfs_string("trigger/current_trigger",
405 dev_dir_name, "NULL");
406 if (ret < 0)
407 printf("Failed to write to %s\n", dev_dir_name);
Jonathan Camerone58537c2010-10-08 12:14:14 +0100408
Jonathan Camerone58537c2010-10-08 12:14:14 +0100409error_close_buffer_access:
Hartmut Knaack6bb7cac2015-05-31 14:40:04 +0200410 if (close(fp) == -1)
411 perror("Failed to close buffer");
Jonathan Camerone58537c2010-10-08 12:14:14 +0100412error_free_buffer_access:
413 free(buffer_access);
Hartmut Knaacka71bfb42015-05-31 14:39:41 +0200414error_free_data:
415 free(data);
Jonathan Camerone58537c2010-10-08 12:14:14 +0100416error_free_buf_dir_name:
417 free(buf_dir_name);
Hartmut Knaack63f05c82015-05-31 14:39:44 +0200418error_free_channels:
419 for (i = num_channels - 1; i >= 0; i--) {
420 free(channels[i].name);
421 free(channels[i].generic_name);
422 }
423 free(channels);
Jonathan Camerone58537c2010-10-08 12:14:14 +0100424error_free_triggername:
425 if (datardytrigger)
426 free(trigger_name);
Hartmut Knaackd3ccfc42015-05-31 14:39:42 +0200427error_free_dev_dir_name:
428 free(dev_dir_name);
Hartmut Knaack0e799872015-05-31 14:40:17 +0200429
Jonathan Camerone58537c2010-10-08 12:14:14 +0100430 return ret;
431}