1 /* Industrialio ring buffer with a lis3l02dq accelerometer
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.
18 #include <linux/types.h>
19 #include "iio_utils.h"
21 const char *device_name
= "lis3l02dq";
22 const char *trigger_name_base
= "lis3l02dq-dev";
23 const int num_vals
= 3;
24 const int scan_ts
= 1;
25 const int buf_len
= 128;
26 const int num_loops
= 10;
29 * Could get this from ring bps, but only after starting the ring
30 * which is a bit late for it to be useful.
32 * Todo: replace with much more generic version based on scan_elements
35 int size_from_scanmode(int num_vals
, int timestamp
)
37 if (num_vals
&& timestamp
)
45 int main(int argc
, char **argv
)
52 char *trigger_name
, *dev_dir_name
, *buf_dir_name
;
55 struct iio_event_data dat
;
56 int dev_num
, trig_num
;
58 char *buffer_access
, *buffer_event
;
59 const char *iio_dir
= "/sys/bus/iio/devices/";
64 /* Find out which iio device is the accelerometer. */
65 dev_num
= find_type_by_name(device_name
, "device");
67 printf("Failed to find the %s\n", device_name
);
71 printf("iio device number being used is %d\n", dev_num
);
72 asprintf(&dev_dir_name
, "%sdevice%d", iio_dir
, dev_num
);
75 * Build the trigger name.
76 * In this case we want the lis3l02dq's data ready trigger
77 * for this lis3l02dq. The naming is lis3l02dq_dev[n], where
78 * n matches the device number found above.
80 ret
= asprintf(&trigger_name
, "%s%d", trigger_name_base
, dev_num
);
83 goto error_free_dev_dir_name
;
87 * Find the trigger by name.
88 * This is techically unecessary here as we only need to
89 * refer to the trigger by name and that name is already
92 trig_num
= find_type_by_name(trigger_name
, "trigger");
94 printf("Failed to find the %s\n", trigger_name
);
96 goto error_free_triggername
;
98 printf("iio trigger number being used is %d\n", trig_num
);
101 * Read in the scale value - in a more generic case, first
102 * check for accel_scale, then the indivual channel scales
104 ret
= read_sysfs_float("accel_scale", dev_dir_name
, &gain
);
106 goto error_free_triggername
;;
109 * Construct the directory name for the associated buffer.
110 * As we know that the lis3l02dq has only one buffer this may
111 * be built rather than found.
113 ret
= asprintf(&buf_dir_name
, "%sdevice%d:buffer0", iio_dir
, dev_num
);
116 goto error_free_triggername
;
118 /* Set the device trigger to be the data rdy trigger found above */
119 ret
= write_sysfs_string_and_verify("trigger/current_trigger",
123 printf("Failed to write current_trigger file\n");
124 goto error_free_buf_dir_name
;
127 /* Setup ring buffer parameters */
128 ret
= write_sysfs_int("length", buf_dir_name
, buf_len
);
130 goto error_free_buf_dir_name
;
132 /* Enable the buffer */
133 ret
= write_sysfs_int("ring_enable", buf_dir_name
, 1);
135 goto error_free_buf_dir_name
;
137 data
= malloc(size_from_scanmode(num_vals
, scan_ts
)*buf_len
);
140 goto error_free_buf_dir_name
;
143 ret
= asprintf(&buffer_access
,
144 "/dev/device%d:buffer0:access0",
148 goto error_free_data
;
151 ret
= asprintf(&buffer_event
, "/dev/device%d:buffer0:event0", dev_num
);
154 goto error_free_data
;
156 /* Attempt to open non blocking the access dev */
157 fp
= open(buffer_access
, O_RDONLY
| O_NONBLOCK
);
158 if (fp
== -1) { /*If it isn't there make the node */
159 printf("Failed to open %s\n", buffer_access
);
161 goto error_free_buffer_event
;
163 /* Attempt to open the event access dev (blocking this time) */
164 fp_ev
= fopen(buffer_event
, "rb");
166 printf("Failed to open %s\n", buffer_event
);
168 goto error_close_buffer_access
;
171 /* Wait for events 10 times */
172 for (j
= 0; j
< num_loops
; j
++) {
173 read_size
= fread(&dat
, 1, sizeof(struct iio_event_data
),
176 case IIO_EVENT_CODE_RING_100_FULL
:
179 case IIO_EVENT_CODE_RING_75_FULL
:
180 toread
= buf_len
*3/4;
182 case IIO_EVENT_CODE_RING_50_FULL
:
186 printf("Unexpecteded event code\n");
191 toread
*size_from_scanmode(num_vals
, scan_ts
));
192 if (read_size
== -EAGAIN
) {
193 printf("nothing available\n");
196 scan_size
= size_from_scanmode(num_vals
, scan_ts
);
197 for (i
= 0; i
< read_size
/scan_size
; i
++) {
198 for (k
= 0; k
< num_vals
; k
++) {
199 __s16 val
= *(__s16
*)(&data
[i
*scan_size
201 printf("%05f ", (float)val
*gain
);
204 *(__s64
*)(&data
[(i
+ 1)
205 *size_from_scanmode(num_vals
,
211 /* Stop the ring buffer */
212 ret
= write_sysfs_int("ring_enable", buf_dir_name
, 0);
214 goto error_close_buffer_event
;
216 /* Disconnect from the trigger - just write a dummy name.*/
217 write_sysfs_string("trigger/current_trigger",
218 dev_dir_name
, "NULL");
220 error_close_buffer_event
:
222 error_close_buffer_access
:
226 error_free_buffer_access
:
228 error_free_buffer_event
:
230 error_free_buf_dir_name
:
232 error_free_triggername
:
234 error_free_dev_dir_name
: