]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/iio/Documentation/generic_buffer.c
01266c2556da995c6fe4a8d5ba4c95b42cbb7df0
[karo-tx-linux.git] / drivers / staging / iio / Documentation / generic_buffer.c
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 #define _GNU_SOURCE
22
23 #include <unistd.h>
24 #include <stdlib.h>
25 #include <dirent.h>
26 #include <fcntl.h>
27 #include <stdio.h>
28 #include <errno.h>
29 #include <sys/stat.h>
30 #include <sys/dir.h>
31 #include <linux/types.h>
32 #include <string.h>
33 #include <poll.h>
34 #include <endian.h>
35 #include <getopt.h>
36 #include <inttypes.h>
37 #include "iio_utils.h"
38
39 /**
40  * size_from_channelarray() - calculate the storage size of a scan
41  * @channels:           the channel info array
42  * @num_channels:       number of channels
43  *
44  * Has the side effect of filling the channels[i].location values used
45  * in processing the buffer output.
46  **/
47 int size_from_channelarray(struct iio_channel_info *channels, int num_channels)
48 {
49         int bytes = 0;
50         int i = 0;
51
52         while (i < num_channels) {
53                 if (bytes % channels[i].bytes == 0)
54                         channels[i].location = bytes;
55                 else
56                         channels[i].location = bytes - bytes%channels[i].bytes
57                                 + channels[i].bytes;
58                 bytes = channels[i].location + channels[i].bytes;
59                 i++;
60         }
61         return bytes;
62 }
63
64 void print2byte(int input, struct iio_channel_info *info)
65 {
66         /* First swap if incorrect endian */
67         if (info->be)
68                 input = be16toh((uint16_t)input);
69         else
70                 input = le16toh((uint16_t)input);
71
72         /*
73          * Shift before conversion to avoid sign extension
74          * of left aligned data
75          */
76         input = input >> info->shift;
77         if (info->is_signed) {
78                 int16_t val = input;
79
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);
84         } else {
85                 uint16_t val = input;
86
87                 val &= (1 << info->bits_used) - 1;
88                 printf("%05f ", ((float)val + info->offset)*info->scale);
89         }
90 }
91 /**
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
96  *  location offsets.
97  * @num_channels:       number of channels
98  **/
99 void process_scan(char *data,
100                   struct iio_channel_info *channels,
101                   int num_channels)
102 {
103         int k;
104
105         for (k = 0; k < num_channels; k++)
106                 switch (channels[k].bytes) {
107                         /* only a few cases implemented so far */
108                 case 2:
109                         print2byte(*(uint16_t *)(data + channels[k].location),
110                                    &channels[k]);
111                         break;
112                 case 4:
113                         if (!channels[k].is_signed) {
114                                 uint32_t val = *(uint32_t *)
115                                         (data + channels[k].location);
116                                 printf("%05f ", ((float)val +
117                                                  channels[k].offset)*
118                                        channels[k].scale);
119
120                         }
121                         break;
122                 case 8:
123                         if (channels[k].is_signed) {
124                                 int64_t val = *(int64_t *)
125                                         (data +
126                                          channels[k].location);
127                                 if ((val >> channels[k].bits_used) & 1)
128                                         val = (val & channels[k].mask) |
129                                                 ~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);
134                                 else
135                                         printf("%05f ", ((float)val +
136                                                          channels[k].offset)*
137                                                channels[k].scale);
138                         }
139                         break;
140                 default:
141                         break;
142                 }
143         printf("\n");
144 }
145
146 int main(int argc, char **argv)
147 {
148         unsigned long num_loops = 2;
149         unsigned long timedelay = 1000000;
150         unsigned long buf_len = 128;
151
152         int ret, c, i, j, toread;
153         int fp;
154
155         int num_channels;
156         char *trigger_name = NULL, *device_name = NULL;
157         char *dev_dir_name, *buf_dir_name;
158
159         int datardytrigger = 1;
160         char *data;
161         ssize_t read_size;
162         int dev_num, trig_num;
163         char *buffer_access;
164         int scan_size;
165         int noevents = 0;
166         int notrigger = 0;
167         char *dummy;
168
169         struct iio_channel_info *channels;
170
171         while ((c = getopt(argc, argv, "l:w:c:et:n:g")) != -1) {
172                 switch (c) {
173                 case 'n':
174                         device_name = optarg;
175                         break;
176                 case 't':
177                         trigger_name = optarg;
178                         datardytrigger = 0;
179                         break;
180                 case 'e':
181                         noevents = 1;
182                         break;
183                 case 'c':
184                         num_loops = strtoul(optarg, &dummy, 10);
185                         break;
186                 case 'w':
187                         timedelay = strtoul(optarg, &dummy, 10);
188                         break;
189                 case 'l':
190                         buf_len = strtoul(optarg, &dummy, 10);
191                         break;
192                 case 'g':
193                         notrigger = 1;
194                         break;
195                 case '?':
196                         return -1;
197                 }
198         }
199
200         if (device_name == NULL)
201                 return -1;
202
203         /* Find the device requested */
204         dev_num = find_type_by_name(device_name, "iio:device");
205         if (dev_num < 0) {
206                 printf("Failed to find the %s\n", device_name);
207                 ret = -ENODEV;
208                 goto error_ret;
209         }
210         printf("iio device number being used is %d\n", dev_num);
211
212         asprintf(&dev_dir_name, "%siio:device%d", iio_dir, dev_num);
213
214         if (!notrigger) {
215                 if (trigger_name == NULL) {
216                         /*
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.
220                          */
221                         ret = asprintf(&trigger_name,
222                                        "%s-dev%d", device_name, dev_num);
223                         if (ret < 0) {
224                                 ret = -ENOMEM;
225                                 goto error_ret;
226                         }
227                 }
228
229                 /* Verify the trigger exists */
230                 trig_num = find_type_by_name(trigger_name, "trigger");
231                 if (trig_num < 0) {
232                         printf("Failed to find the trigger %s\n", trigger_name);
233                         ret = -ENODEV;
234                         goto error_free_triggername;
235                 }
236                 printf("iio trigger number being used is %d\n", trig_num);
237         } else
238                 printf("trigger-less mode selected\n");
239
240         /*
241          * Parse the files in scan_elements to identify what channels are
242          * present
243          */
244         ret = build_channel_array(dev_dir_name, &channels, &num_channels);
245         if (ret) {
246                 printf("Problem reading scan element information\n");
247                 printf("diag %s\n", dev_dir_name);
248                 goto error_free_triggername;
249         }
250
251         /*
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.
255          */
256         ret = asprintf(&buf_dir_name,
257                        "%siio:device%d/buffer", iio_dir, dev_num);
258         if (ret < 0) {
259                 ret = -ENOMEM;
260                 goto error_free_triggername;
261         }
262
263         if (!notrigger) {
264                 printf("%s %s\n", dev_dir_name, trigger_name);
265                 /* Set the device trigger to be the data ready trigger found
266                  * above */
267                 ret = write_sysfs_string_and_verify("trigger/current_trigger",
268                                                     dev_dir_name,
269                                                     trigger_name);
270                 if (ret < 0) {
271                         printf("Failed to write current_trigger file\n");
272                         goto error_free_buf_dir_name;
273                 }
274         }
275
276         /* Setup ring buffer parameters */
277         ret = write_sysfs_int("length", buf_dir_name, buf_len);
278         if (ret < 0)
279                 goto error_free_buf_dir_name;
280
281         /* Enable the buffer */
282         ret = write_sysfs_int("enable", buf_dir_name, 1);
283         if (ret < 0)
284                 goto error_free_buf_dir_name;
285         scan_size = size_from_channelarray(channels, num_channels);
286         data = malloc(scan_size*buf_len);
287         if (!data) {
288                 ret = -ENOMEM;
289                 goto error_free_buf_dir_name;
290         }
291
292         ret = asprintf(&buffer_access, "/dev/iio:device%d", dev_num);
293         if (ret < 0) {
294                 ret = -ENOMEM;
295                 goto error_free_data;
296         }
297
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);
302                 ret = -errno;
303                 goto error_free_buffer_access;
304         }
305
306         /* Wait for events 10 times */
307         for (j = 0; j < num_loops; j++) {
308                 if (!noevents) {
309                         struct pollfd pfd = {
310                                 .fd = fp,
311                                 .events = POLLIN,
312                         };
313
314                         poll(&pfd, 1, -1);
315                         toread = buf_len;
316
317                 } else {
318                         usleep(timedelay);
319                         toread = 64;
320                 }
321
322                 read_size = read(fp,
323                                  data,
324                                  toread*scan_size);
325                 if (read_size < 0) {
326                         if (errno == -EAGAIN) {
327                                 printf("nothing available\n");
328                                 continue;
329                         } else
330                                 break;
331                 }
332                 for (i = 0; i < read_size/scan_size; i++)
333                         process_scan(data + scan_size*i,
334                                      channels,
335                                      num_channels);
336         }
337
338         /* Stop the buffer */
339         ret = write_sysfs_int("enable", buf_dir_name, 0);
340         if (ret < 0)
341                 goto error_close_buffer_access;
342
343         if (!notrigger)
344                 /* Disconnect the trigger - just write a dummy name. */
345                 write_sysfs_string("trigger/current_trigger",
346                                    dev_dir_name, "NULL");
347
348 error_close_buffer_access:
349         close(fp);
350 error_free_data:
351         free(data);
352 error_free_buffer_access:
353         free(buffer_access);
354 error_free_buf_dir_name:
355         free(buf_dir_name);
356 error_free_triggername:
357         if (datardytrigger)
358                 free(trigger_name);
359 error_ret:
360         return ret;
361 }