1 /* Industrialio test ring buffer with a lis3l02dq acceleromter
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 * Assumes suitable udev rules are used to create the dev nodes as named here.
17 #include <sys/types.h>
21 #include <linux/types.h>
25 static const char *ring_access
= "/dev/iio/lis3l02dq_ring_access";
26 static const char *ring_event
= "/dev/iio/lis3l02dq_ring_event";
27 static const char *device_name
= "lis3l02dq";
28 static const char *trigger_name
= "lis3l02dq-dev0";
29 static int NumVals
= 3;
30 static int scan_ts
= 1;
31 static int RingLength
= 128;
34 * Could get this from ring bps, but only after starting the ring
35 * which is a bit late for it to be useful
37 int size_from_scanmode(int numVals
, int timestamp
)
39 if (numVals
&& timestamp
)
47 int main(int argc
, char **argv
)
54 struct iio_event_data dat
;
56 char *BaseDirectoryName
,
57 *TriggerDirectoryName
,
58 *RingBufferDirectoryName
;
60 BaseDirectoryName
= find_type_by_name(device_name
, "device");
61 if (BaseDirectoryName
== NULL
) {
62 printf("Failed to find the %s \n", device_name
);
65 TriggerDirectoryName
= find_type_by_name(trigger_name
, "trigger");
66 if (TriggerDirectoryName
== NULL
) {
67 printf("Failed to find the %s\n", trigger_name
);
70 RingBufferDirectoryName
= find_ring_subelement(BaseDirectoryName
,
72 if (RingBufferDirectoryName
== NULL
) {
73 printf("Failed to find ring buffer\n");
77 if (write_sysfs_string_and_verify("trigger/current_trigger",
79 (char *)trigger_name
) < 0) {
80 printf("Failed to write current_trigger file \n");
84 /* Setup ring buffer parameters */
85 if (write_sysfs_int("length", RingBufferDirectoryName
,
87 printf("Failed to open the ring buffer length file \n");
91 /* Enable the ring buffer */
92 if (write_sysfs_int("ring_enable", RingBufferDirectoryName
, 1) < 0) {
93 printf("Failed to open the ring buffer control file \n");
97 data
= malloc(size_from_scanmode(NumVals
, scan_ts
)*RingLength
);
99 printf("Could not allocate space for usespace data store\n");
103 /* Attempt to open non blocking the access dev */
104 fp
= open(ring_access
, O_RDONLY
| O_NONBLOCK
);
105 if (fp
== -1) { /*If it isn't there make the node */
106 printf("Failed to open %s\n", ring_access
);
109 /* Attempt to open the event access dev (blocking this time) */
110 fp_ev
= fopen(ring_event
, "rb");
112 printf("Failed to open %s\n", ring_event
);
116 /* Wait for events 10 times */
117 for (j
= 0; j
< 10; j
++) {
118 read_size
= fread(&dat
, 1, sizeof(struct iio_event_data
),
121 case IIO_EVENT_CODE_RING_100_FULL
:
124 case IIO_EVENT_CODE_RING_75_FULL
:
125 toread
= RingLength
*3/4;
127 case IIO_EVENT_CODE_RING_50_FULL
:
128 toread
= RingLength
/2;
131 printf("Unexpecteded event code\n");
136 toread
*size_from_scanmode(NumVals
, scan_ts
));
137 if (read_size
== -EAGAIN
) {
138 printf("nothing available \n");
143 i
< read_size
/size_from_scanmode(NumVals
, scan_ts
);
145 for (k
= 0; k
< NumVals
; k
++) {
146 __s16 val
= *(__s16
*)(&data
[i
*size_from_scanmode(NumVals
, scan_ts
)
148 printf("%05d ", val
);
151 *(__s64
*)(&data
[(i
+1)*size_from_scanmode(NumVals
, scan_ts
)
156 /* Stop the ring buffer */
157 if (write_sysfs_int("ring_enable", RingBufferDirectoryName
, 0) < 0) {
158 printf("Failed to open the ring buffer control file \n");
162 /* Disconnect from the trigger - writing something that doesn't exist.*/
163 write_sysfs_string_and_verify("trigger/current_trigger",
164 BaseDirectoryName
, "NULL");
165 free(BaseDirectoryName
);
166 free(TriggerDirectoryName
);
167 free(RingBufferDirectoryName
);