On Tue, Nov 06, 2007 at 02:33:53AM -0800, akpm@linux-foundation.org wrote:
[mmotm.git] / drivers / staging / iio / Documentation / lis3l02dqbuffersimple.c
blob2b5cfc58d78d12f15bca20f0759ad83f9878f4d5
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.
12 #include <dirent.h>
13 #include <fcntl.h>
14 #include <stdio.h>
15 #include <errno.h>
16 #include <stdint.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <sys/dir.h>
21 #include <linux/types.h>
22 #include <dirent.h>
23 #include "iio_util.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)
40 return 16;
41 else if (timestamp)
42 return 8;
43 else
44 return numVals*2;
47 int main(int argc, char **argv)
49 int i, j, k, toread;
50 FILE *fp_ev;
51 int fp;
52 char *data;
53 size_t read_size;
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);
63 return -1;
65 TriggerDirectoryName = find_type_by_name(trigger_name, "trigger");
66 if (TriggerDirectoryName == NULL) {
67 printf("Failed to find the %s\n", trigger_name);
68 return -1;
70 RingBufferDirectoryName = find_ring_subelement(BaseDirectoryName,
71 "ring_buffer");
72 if (RingBufferDirectoryName == NULL) {
73 printf("Failed to find ring buffer\n");
74 return -1;
77 if (write_sysfs_string_and_verify("trigger/current_trigger",
78 BaseDirectoryName,
79 (char *)trigger_name) < 0) {
80 printf("Failed to write current_trigger file \n");
81 return -1;
84 /* Setup ring buffer parameters */
85 if (write_sysfs_int("length", RingBufferDirectoryName,
86 RingLength) < 0) {
87 printf("Failed to open the ring buffer length file \n");
88 return -1;
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");
94 return -1;
97 data = malloc(size_from_scanmode(NumVals, scan_ts)*RingLength);
98 if (!data) {
99 printf("Could not allocate space for usespace data store\n");
100 return -1;
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);
107 return -1;
109 /* Attempt to open the event access dev (blocking this time) */
110 fp_ev = fopen(ring_event, "rb");
111 if (fp_ev == NULL) {
112 printf("Failed to open %s\n", ring_event);
113 return -1;
116 /* Wait for events 10 times */
117 for (j = 0; j < 10; j++) {
118 read_size = fread(&dat, 1, sizeof(struct iio_event_data),
119 fp_ev);
120 switch (dat.id) {
121 case IIO_EVENT_CODE_RING_100_FULL:
122 toread = RingLength;
123 break;
124 case IIO_EVENT_CODE_RING_75_FULL:
125 toread = RingLength*3/4;
126 break;
127 case IIO_EVENT_CODE_RING_50_FULL:
128 toread = RingLength/2;
129 break;
130 default:
131 printf("Unexpecteded event code\n");
132 continue;
134 read_size = read(fp,
135 data,
136 toread*size_from_scanmode(NumVals, scan_ts));
137 if (read_size == -EAGAIN) {
138 printf("nothing available \n");
139 continue;
142 for (i = 0;
143 i < read_size/size_from_scanmode(NumVals, scan_ts);
144 i++) {
145 for (k = 0; k < NumVals; k++) {
146 __s16 val = *(__s16 *)(&data[i*size_from_scanmode(NumVals, scan_ts)
147 + (k)*2]);
148 printf("%05d ", val);
150 printf(" %lld\n",
151 *(__s64 *)(&data[(i+1)*size_from_scanmode(NumVals, scan_ts)
152 - sizeof(__s64)]));
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");
159 return -1;
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);
168 free(data);
170 return 0;