tools:iio:iio_utils: mark private function static
[deliverable/linux.git] / tools / iio / generic_buffer.c
CommitLineData
e58537cc
JC
1/* 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>
bdcb31d0 22#include <stdlib.h>
e58537cc
JC
23#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>
30268a3d 30#include <string.h>
52615d47 31#include <poll.h>
117cf8b7 32#include <endian.h>
bb23378c 33#include <getopt.h>
1bcdfbcf 34#include <inttypes.h>
e58537cc
JC
35#include "iio_utils.h"
36
e58537cc
JC
37/**
38 * size_from_channelarray() - calculate the storage size of a scan
d8da0eee
PM
39 * @channels: the channel info array
40 * @num_channels: number of channels
e58537cc
JC
41 *
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;
ff39a252 49
e58537cc
JC
50 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
8e926134 62void print2byte(uint16_t input, struct iio_channel_info *info)
52615d47 63{
117cf8b7 64 /* First swap if incorrect endian */
117cf8b7 65 if (info->be)
8e926134 66 input = be16toh(input);
117cf8b7 67 else
8e926134 68 input = le16toh(input);
117cf8b7 69
d8da0eee
PM
70 /*
71 * Shift before conversion to avoid sign extension
72 * of left aligned data
73 */
1525ecfc 74 input >>= info->shift;
8e926134 75 input &= info->mask;
52615d47 76 if (info->is_signed) {
8e926134
HK
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);
80 } else {
81 printf("%05f ", ((float)input + info->offset) * info->scale);
82 }
83}
ff39a252 84
8e926134
HK
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);
52615d47 103 } else {
8e926134
HK
104 printf("%05f ", ((float)input + info->offset) * info->scale);
105 }
106}
ff39a252 107
8e926134
HK
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);
52615d47
JC
133 }
134}
8e926134 135
e58537cc
JC
136/**
137 * process_scan() - print out the values in SI units
138 * @data: pointer to the start of the scan
d8da0eee 139 * @channels: information about the channels. Note
e58537cc
JC
140 * size_from_channelarray must have been called first to fill the
141 * location offsets.
d8da0eee 142 * @num_channels: number of channels
e58537cc
JC
143 **/
144void process_scan(char *data,
d8da0eee 145 struct iio_channel_info *channels,
e58537cc
JC
146 int num_channels)
147{
148 int k;
ff39a252 149
e58537cc 150 for (k = 0; k < num_channels; k++)
d8da0eee 151 switch (channels[k].bytes) {
e58537cc
JC
152 /* only a few cases implemented so far */
153 case 2:
d8da0eee
PM
154 print2byte(*(uint16_t *)(data + channels[k].location),
155 &channels[k]);
e58537cc 156 break;
6cffc1f8 157 case 4:
8e926134
HK
158 print4byte(*(uint32_t *)(data + channels[k].location),
159 &channels[k]);
6cffc1f8 160 break;
e58537cc 161 case 8:
8e926134
HK
162 print8byte(*(uint64_t *)(data + channels[k].location),
163 &channels[k]);
e58537cc
JC
164 break;
165 default:
166 break;
167 }
168 printf("\n");
169}
170
171int main(int argc, char **argv)
172{
96df9799
JC
173 unsigned long num_loops = 2;
174 unsigned long timedelay = 1000000;
175 unsigned long buf_len = 128;
176
e58537cc 177 int ret, c, i, j, toread;
e58537cc
JC
178 int fp;
179
180 int num_channels;
181 char *trigger_name = NULL, *device_name = NULL;
182 char *dev_dir_name, *buf_dir_name;
183
184 int datardytrigger = 1;
185 char *data;
c77b3810 186 ssize_t read_size;
e58537cc 187 int dev_num, trig_num;
52615d47 188 char *buffer_access;
e58537cc 189 int scan_size;
30268a3d 190 int noevents = 0;
b6d5be57 191 int notrigger = 0;
96df9799 192 char *dummy;
e58537cc 193
d8da0eee 194 struct iio_channel_info *channels;
e58537cc 195
b6d5be57 196 while ((c = getopt(argc, argv, "l:w:c:et:n:g")) != -1) {
e58537cc
JC
197 switch (c) {
198 case 'n':
199 device_name = optarg;
200 break;
201 case 't':
202 trigger_name = optarg;
203 datardytrigger = 0;
204 break;
30268a3d
JC
205 case 'e':
206 noevents = 1;
207 break;
96df9799
JC
208 case 'c':
209 num_loops = strtoul(optarg, &dummy, 10);
210 break;
211 case 'w':
212 timedelay = strtoul(optarg, &dummy, 10);
213 break;
214 case 'l':
215 buf_len = strtoul(optarg, &dummy, 10);
216 break;
b6d5be57
KW
217 case 'g':
218 notrigger = 1;
219 break;
e58537cc
JC
220 case '?':
221 return -1;
222 }
223 }
224
065896e9
MH
225 if (device_name == NULL)
226 return -1;
227
e58537cc 228 /* Find the device requested */
1aa04278 229 dev_num = find_type_by_name(device_name, "iio:device");
e58537cc
JC
230 if (dev_num < 0) {
231 printf("Failed to find the %s\n", device_name);
e83a47cf 232 ret = dev_num;
e58537cc
JC
233 goto error_ret;
234 }
235 printf("iio device number being used is %d\n", dev_num);
236
1aa04278 237 asprintf(&dev_dir_name, "%siio:device%d", iio_dir, dev_num);
b6d5be57
KW
238
239 if (!notrigger) {
240 if (trigger_name == NULL) {
241 /*
242 * Build the trigger name. If it is device associated
243 * its name is <device_name>_dev[n] where n matches
244 * the device number found above.
245 */
246 ret = asprintf(&trigger_name,
247 "%s-dev%d", device_name, dev_num);
248 if (ret < 0) {
249 ret = -ENOMEM;
d3ccfc41 250 goto error_free_dev_dir_name;
b6d5be57 251 }
e58537cc 252 }
e58537cc 253
b6d5be57
KW
254 /* Verify the trigger exists */
255 trig_num = find_type_by_name(trigger_name, "trigger");
256 if (trig_num < 0) {
257 printf("Failed to find the trigger %s\n", trigger_name);
e83a47cf 258 ret = trig_num;
b6d5be57
KW
259 goto error_free_triggername;
260 }
261 printf("iio trigger number being used is %d\n", trig_num);
262 } else
263 printf("trigger-less mode selected\n");
e58537cc
JC
264
265 /*
266 * Parse the files in scan_elements to identify what channels are
267 * present
268 */
d8da0eee 269 ret = build_channel_array(dev_dir_name, &channels, &num_channels);
e58537cc 270 if (ret) {
52615d47 271 printf("Problem reading scan element information\n");
1aa04278 272 printf("diag %s\n", dev_dir_name);
e58537cc
JC
273 goto error_free_triggername;
274 }
275
276 /*
277 * Construct the directory name for the associated buffer.
278 * As we know that the lis3l02dq has only one buffer this may
279 * be built rather than found.
280 */
1aa04278
JC
281 ret = asprintf(&buf_dir_name,
282 "%siio:device%d/buffer", iio_dir, dev_num);
e58537cc
JC
283 if (ret < 0) {
284 ret = -ENOMEM;
63f05c85 285 goto error_free_channels;
e58537cc 286 }
b6d5be57
KW
287
288 if (!notrigger) {
289 printf("%s %s\n", dev_dir_name, trigger_name);
290 /* Set the device trigger to be the data ready trigger found
291 * above */
292 ret = write_sysfs_string_and_verify("trigger/current_trigger",
293 dev_dir_name,
294 trigger_name);
295 if (ret < 0) {
296 printf("Failed to write current_trigger file\n");
297 goto error_free_buf_dir_name;
298 }
e58537cc
JC
299 }
300
301 /* Setup ring buffer parameters */
302 ret = write_sysfs_int("length", buf_dir_name, buf_len);
303 if (ret < 0)
304 goto error_free_buf_dir_name;
305
306 /* Enable the buffer */
307 ret = write_sysfs_int("enable", buf_dir_name, 1);
308 if (ret < 0)
309 goto error_free_buf_dir_name;
d8da0eee 310 scan_size = size_from_channelarray(channels, num_channels);
e58537cc
JC
311 data = malloc(scan_size*buf_len);
312 if (!data) {
313 ret = -ENOMEM;
314 goto error_free_buf_dir_name;
315 }
316
1aa04278 317 ret = asprintf(&buffer_access, "/dev/iio:device%d", dev_num);
e58537cc
JC
318 if (ret < 0) {
319 ret = -ENOMEM;
320 goto error_free_data;
321 }
322
e58537cc
JC
323 /* Attempt to open non blocking the access dev */
324 fp = open(buffer_access, O_RDONLY | O_NONBLOCK);
d8da0eee 325 if (fp == -1) { /* If it isn't there make the node */
e58537cc 326 ret = -errno;
2b6a6e67 327 printf("Failed to open %s\n", buffer_access);
52615d47 328 goto error_free_buffer_access;
e58537cc
JC
329 }
330
331 /* Wait for events 10 times */
332 for (j = 0; j < num_loops; j++) {
30268a3d 333 if (!noevents) {
52615d47
JC
334 struct pollfd pfd = {
335 .fd = fp,
336 .events = POLLIN,
337 };
338
339 poll(&pfd, 1, -1);
340 toread = buf_len;
341
30268a3d 342 } else {
96df9799 343 usleep(timedelay);
30268a3d 344 toread = 64;
e58537cc 345 }
30268a3d 346
e58537cc
JC
347 read_size = read(fp,
348 data,
349 toread*scan_size);
97b603a4 350 if (read_size < 0) {
8749948a 351 if (errno == EAGAIN) {
97b603a4
PM
352 printf("nothing available\n");
353 continue;
354 } else
355 break;
e58537cc
JC
356 }
357 for (i = 0; i < read_size/scan_size; i++)
358 process_scan(data + scan_size*i,
d8da0eee 359 channels,
e58537cc
JC
360 num_channels);
361 }
362
d8da0eee 363 /* Stop the buffer */
e58537cc
JC
364 ret = write_sysfs_int("enable", buf_dir_name, 0);
365 if (ret < 0)
52615d47 366 goto error_close_buffer_access;
e58537cc 367
b6d5be57
KW
368 if (!notrigger)
369 /* Disconnect the trigger - just write a dummy name. */
370 write_sysfs_string("trigger/current_trigger",
371 dev_dir_name, "NULL");
e58537cc 372
e58537cc
JC
373error_close_buffer_access:
374 close(fp);
e58537cc
JC
375error_free_buffer_access:
376 free(buffer_access);
a71bfb4a
HK
377error_free_data:
378 free(data);
e58537cc
JC
379error_free_buf_dir_name:
380 free(buf_dir_name);
63f05c85
HK
381error_free_channels:
382 for (i = num_channels - 1; i >= 0; i--) {
383 free(channels[i].name);
384 free(channels[i].generic_name);
385 }
386 free(channels);
e58537cc
JC
387error_free_triggername:
388 if (datardytrigger)
389 free(trigger_name);
d3ccfc41
HK
390error_free_dev_dir_name:
391 free(dev_dir_name);
e58537cc
JC
392error_ret:
393 return ret;
394}
This page took 0.795481 seconds and 5 git commands to generate.