blob: 0befcb89ea3a43ca0fb7d6a6d5aea931a56c75fa [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>
22#include <dirent.h>
23#include <fcntl.h>
24#include <stdio.h>
25#include <errno.h>
26#include <sys/stat.h>
27#include <sys/dir.h>
28#include <linux/types.h>
Jonathan Cameron30268a32011-02-11 13:09:12 +000029#include <string.h>
Jonathan Camerone58537c2010-10-08 12:14:14 +010030#include "iio_utils.h"
31
32const int buf_len = 128;
33const int num_loops = 2;
34
35/**
36 * size_from_channelarray() - calculate the storage size of a scan
37 * @channels: the channel info array
38 * @num_channels: size of the channel info array
39 *
40 * Has the side effect of filling the channels[i].location values used
41 * in processing the buffer output.
42 **/
43int size_from_channelarray(struct iio_channel_info *channels, int num_channels)
44{
45 int bytes = 0;
46 int i = 0;
47 while (i < num_channels) {
48 if (bytes % channels[i].bytes == 0)
49 channels[i].location = bytes;
50 else
51 channels[i].location = bytes - bytes%channels[i].bytes
52 + channels[i].bytes;
53 bytes = channels[i].location + channels[i].bytes;
54 i++;
55 }
56 return bytes;
57}
58
59/**
60 * process_scan() - print out the values in SI units
61 * @data: pointer to the start of the scan
62 * @infoarray: information about the channels. Note
63 * size_from_channelarray must have been called first to fill the
64 * location offsets.
65 * @num_channels: the number of active channels
66 **/
67void process_scan(char *data,
68 struct iio_channel_info *infoarray,
69 int num_channels)
70{
71 int k;
72 for (k = 0; k < num_channels; k++)
73 switch (infoarray[k].bytes) {
74 /* only a few cases implemented so far */
75 case 2:
76 if (infoarray[k].is_signed) {
77 int16_t val = *(int16_t *)
78 (data
79 + infoarray[k].location);
80 if ((val >> infoarray[k].bits_used) & 1)
81 val = (val & infoarray[k].mask) |
82 ~infoarray[k].mask;
83 printf("%05f ", ((float)val +
84 infoarray[k].offset)*
85 infoarray[k].scale);
86 } else {
87 uint16_t val = *(uint16_t *)
88 (data +
89 infoarray[k].location);
90 val = (val & infoarray[k].mask);
91 printf("%05f ", ((float)val +
92 infoarray[k].offset)*
93 infoarray[k].scale);
94 }
95 break;
96 case 8:
97 if (infoarray[k].is_signed) {
98 int64_t val = *(int64_t *)
99 (data +
100 infoarray[k].location);
101 if ((val >> infoarray[k].bits_used) & 1)
102 val = (val & infoarray[k].mask) |
103 ~infoarray[k].mask;
104 /* special case for timestamp */
105 if (infoarray[k].scale == 1.0f &&
106 infoarray[k].offset == 0.0f)
107 printf(" %lld", val);
108 else
109 printf("%05f ", ((float)val +
110 infoarray[k].offset)*
111 infoarray[k].scale);
112 }
113 break;
114 default:
115 break;
116 }
117 printf("\n");
118}
119
120int main(int argc, char **argv)
121{
122 int ret, c, i, j, toread;
123
124 FILE *fp_ev;
125 int fp;
126
127 int num_channels;
128 char *trigger_name = NULL, *device_name = NULL;
129 char *dev_dir_name, *buf_dir_name;
130
131 int datardytrigger = 1;
132 char *data;
133 size_t read_size;
134 struct iio_event_data dat;
135 int dev_num, trig_num;
136 char *buffer_access, *buffer_event;
137 int scan_size;
Jonathan Cameron30268a32011-02-11 13:09:12 +0000138 int noevents = 0;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100139
140 struct iio_channel_info *infoarray;
141
Jonathan Cameron30268a32011-02-11 13:09:12 +0000142 while ((c = getopt(argc, argv, "et:n:")) != -1) {
Jonathan Camerone58537c2010-10-08 12:14:14 +0100143 switch (c) {
144 case 'n':
145 device_name = optarg;
146 break;
147 case 't':
148 trigger_name = optarg;
149 datardytrigger = 0;
150 break;
Jonathan Cameron30268a32011-02-11 13:09:12 +0000151 case 'e':
152 noevents = 1;
153 break;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100154 case '?':
155 return -1;
156 }
157 }
158
159 /* Find the device requested */
160 dev_num = find_type_by_name(device_name, "device");
161 if (dev_num < 0) {
162 printf("Failed to find the %s\n", device_name);
163 ret = -ENODEV;
164 goto error_ret;
165 }
166 printf("iio device number being used is %d\n", dev_num);
167
168 asprintf(&dev_dir_name, "%sdevice%d", iio_dir, dev_num);
169 if (trigger_name == NULL) {
170 /*
171 * Build the trigger name. If it is device associated it's
172 * name is <device_name>_dev[n] where n matches the device
173 * number found above
174 */
175 ret = asprintf(&trigger_name,
176 "%s-dev%d", device_name, dev_num);
177 if (ret < 0) {
178 ret = -ENOMEM;
179 goto error_ret;
180 }
181 }
182
183 /* Verify the trigger exists */
184 trig_num = find_type_by_name(trigger_name, "trigger");
185 if (trig_num < 0) {
186 printf("Failed to find the trigger %s\n", trigger_name);
187 ret = -ENODEV;
188 goto error_free_triggername;
189 }
190 printf("iio trigger number being used is %d\n", trig_num);
191
192 /*
193 * Parse the files in scan_elements to identify what channels are
194 * present
195 */
196 ret = build_channel_array(dev_dir_name, &infoarray, &num_channels);
197 if (ret) {
198 printf("Problem reading scan element information \n");
199 goto error_free_triggername;
200 }
201
202 /*
203 * Construct the directory name for the associated buffer.
204 * As we know that the lis3l02dq has only one buffer this may
205 * be built rather than found.
206 */
207 ret = asprintf(&buf_dir_name, "%sdevice%d:buffer0", iio_dir, dev_num);
208 if (ret < 0) {
209 ret = -ENOMEM;
210 goto error_free_triggername;
211 }
212 printf("%s %s\n", dev_dir_name, trigger_name);
213 /* Set the device trigger to be the data rdy trigger found above */
214 ret = write_sysfs_string_and_verify("trigger/current_trigger",
215 dev_dir_name,
216 trigger_name);
217 if (ret < 0) {
218 printf("Failed to write current_trigger file\n");
219 goto error_free_buf_dir_name;
220 }
221
222 /* Setup ring buffer parameters */
223 ret = write_sysfs_int("length", buf_dir_name, buf_len);
224 if (ret < 0)
225 goto error_free_buf_dir_name;
226
227 /* Enable the buffer */
228 ret = write_sysfs_int("enable", buf_dir_name, 1);
229 if (ret < 0)
230 goto error_free_buf_dir_name;
231 scan_size = size_from_channelarray(infoarray, num_channels);
232 data = malloc(scan_size*buf_len);
233 if (!data) {
234 ret = -ENOMEM;
235 goto error_free_buf_dir_name;
236 }
237
238 ret = asprintf(&buffer_access,
239 "/dev/device%d:buffer0:access0",
240 dev_num);
241 if (ret < 0) {
242 ret = -ENOMEM;
243 goto error_free_data;
244 }
245
246 ret = asprintf(&buffer_event, "/dev/device%d:buffer0:event0", dev_num);
247 if (ret < 0) {
248 ret = -ENOMEM;
249 goto error_free_buffer_access;
250 }
251 /* Attempt to open non blocking the access dev */
252 fp = open(buffer_access, O_RDONLY | O_NONBLOCK);
253 if (fp == -1) { /*If it isn't there make the node */
254 printf("Failed to open %s\n", buffer_access);
255 ret = -errno;
256 goto error_free_buffer_event;
257 }
258 /* Attempt to open the event access dev (blocking this time) */
259 fp_ev = fopen(buffer_event, "rb");
260 if (fp_ev == NULL) {
261 printf("Failed to open %s\n", buffer_event);
262 ret = -errno;
263 goto error_close_buffer_access;
264 }
265
266 /* Wait for events 10 times */
267 for (j = 0; j < num_loops; j++) {
Jonathan Cameron30268a32011-02-11 13:09:12 +0000268 if (!noevents) {
269 read_size = fread(&dat,
270 1,
271 sizeof(struct iio_event_data),
272 fp_ev);
273 switch (dat.id) {
274 case IIO_EVENT_CODE_RING_100_FULL:
275 toread = buf_len;
276 break;
277 case IIO_EVENT_CODE_RING_75_FULL:
278 toread = buf_len*3/4;
279 break;
280 case IIO_EVENT_CODE_RING_50_FULL:
281 toread = buf_len/2;
282 break;
283 default:
284 printf("Unexpecteded event code\n");
285 continue;
286 }
287 } else {
288 usleep(1000);
289 toread = 64;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100290 }
Jonathan Cameron30268a32011-02-11 13:09:12 +0000291
Jonathan Camerone58537c2010-10-08 12:14:14 +0100292 read_size = read(fp,
293 data,
294 toread*scan_size);
295 if (read_size == -EAGAIN) {
296 printf("nothing available\n");
297 continue;
298 }
299 for (i = 0; i < read_size/scan_size; i++)
300 process_scan(data + scan_size*i,
301 infoarray,
302 num_channels);
303 }
304
305 /* Stop the ring buffer */
306 ret = write_sysfs_int("enable", buf_dir_name, 0);
307 if (ret < 0)
308 goto error_close_buffer_event;
309
310 /* Disconnect from the trigger - just write a dummy name.*/
311 write_sysfs_string("trigger/current_trigger",
312 dev_dir_name, "NULL");
313
314error_close_buffer_event:
315 fclose(fp_ev);
316error_close_buffer_access:
317 close(fp);
318error_free_data:
319 free(data);
320error_free_buffer_access:
321 free(buffer_access);
322error_free_buffer_event:
323 free(buffer_event);
324error_free_buf_dir_name:
325 free(buf_dir_name);
326error_free_triggername:
327 if (datardytrigger)
328 free(trigger_name);
329error_ret:
330 return ret;
331}