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.
28 #include <linux/types.h>
29 #include "iio_utils.h"
31 const int buf_len = 128;
32 const int num_loops = 2;
35 * size_from_channelarray() - calculate the storage size of a scan
36 * @channels: the channel info array
37 * @num_channels: size of the channel info array
39 * Has the side effect of filling the channels[i].location values used
40 * in processing the buffer output.
42 int size_from_channelarray(struct iio_channel_info *channels, int num_channels)
46 while (i < num_channels) {
47 if (bytes % channels[i].bytes == 0)
48 channels[i].location = bytes;
50 channels[i].location = bytes - bytes%channels[i].bytes
52 bytes = channels[i].location + channels[i].bytes;
59 * process_scan() - print out the values in SI units
60 * @data: pointer to the start of the scan
61 * @infoarray: information about the channels. Note
62 * size_from_channelarray must have been called first to fill the
64 * @num_channels: the number of active channels
66 void process_scan(char *data,
67 struct iio_channel_info *infoarray,
71 for (k = 0; k < num_channels; k++)
72 switch (infoarray[k].bytes) {
73 /* only a few cases implemented so far */
75 if (infoarray[k].is_signed) {
76 int16_t val = *(int16_t *)
78 + infoarray[k].location);
79 if ((val >> infoarray[k].bits_used) & 1)
80 val = (val & infoarray[k].mask) |
82 printf("%05f ", ((float)val +
86 uint16_t val = *(uint16_t *)
88 infoarray[k].location);
89 val = (val & infoarray[k].mask);
90 printf("%05f ", ((float)val +
96 if (infoarray[k].is_signed) {
97 int64_t val = *(int64_t *)
99 infoarray[k].location);
100 if ((val >> infoarray[k].bits_used) & 1)
101 val = (val & infoarray[k].mask) |
103 /* special case for timestamp */
104 if (infoarray[k].scale == 1.0f &&
105 infoarray[k].offset == 0.0f)
106 printf(" %lld", val);
108 printf("%05f ", ((float)val +
109 infoarray[k].offset)*
119 int main(int argc, char **argv)
121 int ret, c, i, j, toread;
127 char *trigger_name = NULL, *device_name = NULL;
128 char *dev_dir_name, *buf_dir_name;
130 int datardytrigger = 1;
133 struct iio_event_data dat;
134 int dev_num, trig_num;
135 char *buffer_access, *buffer_event;
138 struct iio_channel_info *infoarray;
140 while ((c = getopt(argc, argv, "t:n:")) != -1) {
143 device_name = optarg;
146 trigger_name = optarg;
154 /* Find the device requested */
155 dev_num = find_type_by_name(device_name, "device");
157 printf("Failed to find the %s\n", device_name);
161 printf("iio device number being used is %d\n", dev_num);
163 asprintf(&dev_dir_name, "%sdevice%d", iio_dir, dev_num);
164 if (trigger_name == NULL) {
166 * Build the trigger name. If it is device associated it's
167 * name is <device_name>_dev[n] where n matches the device
170 ret = asprintf(&trigger_name,
171 "%s-dev%d", device_name, dev_num);
178 /* Verify the trigger exists */
179 trig_num = find_type_by_name(trigger_name, "trigger");
181 printf("Failed to find the trigger %s\n", trigger_name);
183 goto error_free_triggername;
185 printf("iio trigger number being used is %d\n", trig_num);
188 * Parse the files in scan_elements to identify what channels are
191 ret = build_channel_array(dev_dir_name, &infoarray, &num_channels);
193 printf("Problem reading scan element information \n");
194 goto error_free_triggername;
198 * Construct the directory name for the associated buffer.
199 * As we know that the lis3l02dq has only one buffer this may
200 * be built rather than found.
202 ret = asprintf(&buf_dir_name, "%sdevice%d:buffer0", iio_dir, dev_num);
205 goto error_free_triggername;
207 printf("%s %s\n", dev_dir_name, trigger_name);
208 /* Set the device trigger to be the data rdy trigger found above */
209 ret = write_sysfs_string_and_verify("trigger/current_trigger",
213 printf("Failed to write current_trigger file\n");
214 goto error_free_buf_dir_name;
217 /* Setup ring buffer parameters */
218 ret = write_sysfs_int("length", buf_dir_name, buf_len);
220 goto error_free_buf_dir_name;
222 /* Enable the buffer */
223 ret = write_sysfs_int("enable", buf_dir_name, 1);
225 goto error_free_buf_dir_name;
226 scan_size = size_from_channelarray(infoarray, num_channels);
227 data = malloc(scan_size*buf_len);
230 goto error_free_buf_dir_name;
233 ret = asprintf(&buffer_access,
234 "/dev/device%d:buffer0:access0",
238 goto error_free_data;
241 ret = asprintf(&buffer_event, "/dev/device%d:buffer0:event0", dev_num);
244 goto error_free_buffer_access;
246 /* Attempt to open non blocking the access dev */
247 fp = open(buffer_access, O_RDONLY | O_NONBLOCK);
248 if (fp == -1) { /*If it isn't there make the node */
249 printf("Failed to open %s\n", buffer_access);
251 goto error_free_buffer_event;
253 /* Attempt to open the event access dev (blocking this time) */
254 fp_ev = fopen(buffer_event, "rb");
256 printf("Failed to open %s\n", buffer_event);
258 goto error_close_buffer_access;
261 /* Wait for events 10 times */
262 for (j = 0; j < num_loops; j++) {
263 read_size = fread(&dat, 1, sizeof(struct iio_event_data),
266 case IIO_EVENT_CODE_RING_100_FULL:
269 case IIO_EVENT_CODE_RING_75_FULL:
270 toread = buf_len*3/4;
272 case IIO_EVENT_CODE_RING_50_FULL:
276 printf("Unexpecteded event code\n");
282 if (read_size == -EAGAIN) {
283 printf("nothing available\n");
286 for (i = 0; i < read_size/scan_size; i++)
287 process_scan(data + scan_size*i,
292 /* Stop the ring buffer */
293 ret = write_sysfs_int("enable", buf_dir_name, 0);
295 goto error_close_buffer_event;
297 /* Disconnect from the trigger - just write a dummy name.*/
298 write_sysfs_string("trigger/current_trigger",
299 dev_dir_name, "NULL");
301 error_close_buffer_event:
303 error_close_buffer_access:
307 error_free_buffer_access:
309 error_free_buffer_event:
311 error_free_buf_dir_name:
313 error_free_triggername: