Commit 9d8ae6c8 authored by Jonathan Cameron's avatar Jonathan Cameron Committed by Greg Kroah-Hartman

staging:iio:Documentation: Rewrite example for new abi.

Signed-off-by: default avatarJonathan Cameron <jic23@cam.ac.uk>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@suse.de>
parent e9124afa
...@@ -7,140 +7,173 @@ ...@@ -7,140 +7,173 @@
* the Free Software Foundation. * the Free Software Foundation.
*/ */
/* Made up value to limit allocation sizes */
#include <string.h>
#include <stdlib.h>
#define IIO_MAX_NAME_LENGTH 30
#define IIO_EVENT_CODE_RING_50_FULL 200 #define IIO_EVENT_CODE_RING_50_FULL 200
#define IIO_EVENT_CODE_RING_75_FULL 201 #define IIO_EVENT_CODE_RING_75_FULL 201
#define IIO_EVENT_CODE_RING_100_FULL 202 #define IIO_EVENT_CODE_RING_100_FULL 202
const char *iio_dir = "/sys/bus/iio/devices/";
struct iio_event_data { struct iio_event_data {
int id; int id;
__s64 timestamp; __s64 timestamp;
}; };
/**
inline char *find_ring_subelement(const char *directory, const char *subelement) * find_type_by_name() - function to match top level types by name
{ * @name: top level type instance name
DIR *dp; * @type: the type of top level instance being sort
const struct dirent *ent; *
int pos; * Typical types this is used for are device and trigger.
char temp[100]; **/
char *returnstring; inline int find_type_by_name(const char *name, const char *type)
dp = opendir(directory);
if (dp == NULL) {
printf("could not directory: %s\n", directory);
return NULL;
}
while (ent = readdir(dp), ent != NULL) {
if (strcmp(ent->d_name, ".") != 0 &&
strcmp(ent->d_name, "..") != 0) {
if (strncmp(ent->d_name, subelement, strlen(subelement)) == 0) {
int length = sprintf(temp, "%s%s%s", directory, ent->d_name, "/");
returnstring = malloc(length+1);
strncpy(returnstring, temp, length+1);
return returnstring;
}
}
}
return 0;
}
char *find_type_by_name(const char *name, const char *type)
{ {
const char *iio_dir = "/sys/bus/iio/devices/";
const struct dirent *ent; const struct dirent *ent;
int cnt, pos, pos2; int number, numstrlen;
FILE *nameFile; FILE *nameFile;
DIR *dp; DIR *dp;
char thisname[100]; char thisname[IIO_MAX_NAME_LENGTH];
char temp[100]; char *filename;
char *returnstring = NULL;
struct stat Stat; struct stat Stat;
pos = sprintf(temp, "%s", iio_dir);
dp = opendir(iio_dir); dp = opendir(iio_dir);
if (dp == NULL) { if (dp == NULL) {
printf("No industrialio devices available"); printf("No industrialio devices available");
return NULL; return -ENODEV;
} }
while (ent = readdir(dp), ent != NULL) { while (ent = readdir(dp), ent != NULL) {
cnt++;
/*reject . and .. */
if (strcmp(ent->d_name, ".") != 0 && if (strcmp(ent->d_name, ".") != 0 &&
strcmp(ent->d_name, "..") != 0) { strcmp(ent->d_name, "..") != 0 &&
/*make sure it isn't a trigger!*/ strlen(ent->d_name) > strlen(type) &&
if (strncmp(ent->d_name, type, strlen(type)) == 0) { strncmp(ent->d_name, type, strlen(type)) == 0) {
/* build full path to new file */ numstrlen = sscanf(ent->d_name + strlen(type),
pos2 = pos + sprintf(temp + pos, "%s/", ent->d_name); "%d",
sprintf(temp + pos2, "name"); &number);
printf("search location %s\n", temp); /* verify the next character is not a colon */
nameFile = fopen(temp, "r"); if (strncmp(ent->d_name + strlen(type) + numstrlen,
if (!nameFile) { ":",
sprintf(temp + pos2, "modalias", ent->d_name); 1) != 0) {
nameFile = fopen(temp, "r"); filename = malloc(strlen(iio_dir)
if (!nameFile) { + strlen(type)
printf("Failed to find a name for device\n"); + 1
return NULL; + numstrlen
} + 1);
} if (filename == NULL)
return -ENOMEM;
sprintf(filename, "%s%s%d/name",
iio_dir,
type,
number);
nameFile = fopen(filename, "r");
if (!nameFile)
continue;
free(filename);
fscanf(nameFile, "%s", thisname); fscanf(nameFile, "%s", thisname);
if (strcmp(name, thisname) == 0) { if (strcmp(name, thisname) == 0)
returnstring = malloc(strlen(temp) + 1); return number;
sprintf(temp + pos2, "");
strcpy(returnstring, temp);
return returnstring;
}
fclose(nameFile); fclose(nameFile);
} }
} }
} }
return -ENODEV;
} }
int write_sysfs_int(char *filename, char *basedir, int val) inline int _write_sysfs_int(char *filename, char *basedir, int val, int verify)
{ {
int ret; int ret;
FILE *sysfsfp; FILE *sysfsfp;
char temp[100]; int test;
sprintf(temp, "%s%s", basedir, filename); char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
if (temp == NULL)
return -ENOMEM;
sprintf(temp, "%s/%s", basedir, filename);
sysfsfp = fopen(temp, "w"); sysfsfp = fopen(temp, "w");
if (sysfsfp == NULL) if (sysfsfp == NULL) {
return -1; printf("failed to open %s\n", temp);
ret = -errno;
goto error_free;
}
fprintf(sysfsfp, "%d", val); fprintf(sysfsfp, "%d", val);
fclose(sysfsfp); fclose(sysfsfp);
return 0; if (verify) {
sysfsfp = fopen(temp, "r");
if (sysfsfp == NULL) {
printf("failed to open %s\n", temp);
ret = -errno;
goto error_free;
}
fscanf(sysfsfp, "%d", &test);
if (test != val) {
printf("Possible failure in int write %d to %s%s\n",
val,
basedir,
filename);
ret = -1;
}
}
error_free:
free(temp);
return ret;
}
int write_sysfs_int(char *filename, char *basedir, int val)
{
return _write_sysfs_int(filename, basedir, val, 0);
} }
int write_sysfs_int_and_verify(char *filename, char *basedir, int val) int write_sysfs_int_and_verify(char *filename, char *basedir, int val)
{
return _write_sysfs_int(filename, basedir, val, 1);
}
int _write_sysfs_string(char *filename, char *basedir, char *val, int verify)
{ {
int ret; int ret;
FILE *sysfsfp; FILE *sysfsfp;
char temp[100]; char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
int test; if (temp == NULL) {
printf("Memory allocation failed\n");
sprintf(temp, "%s%s", basedir, filename); return -ENOMEM;
}
sprintf(temp, "%s/%s", basedir, filename);
sysfsfp = fopen(temp, "w"); sysfsfp = fopen(temp, "w");
if (sysfsfp == NULL) if (sysfsfp == NULL) {
return -1; printf("Could not open %s\n", temp);
fprintf(sysfsfp, "%d", val); ret = -errno;
goto error_free;
}
fprintf(sysfsfp, "%s", val);
fclose(sysfsfp); fclose(sysfsfp);
if (verify) {
sysfsfp = fopen(temp, "r"); sysfsfp = fopen(temp, "r");
if (sysfsfp == NULL) if (sysfsfp == NULL) {
return -1; ret = -errno;
fscanf(sysfsfp, "%d", &test); goto error_free;
if (test != val) { }
printf("Possible failure in int write %d to %s%s\n", fscanf(sysfsfp, "%s", temp);
val, if (strcmp(temp, val) != 0) {
basedir, printf("Possible failure in string write of %s "
filename); "Should be %s "
return -1; "writen to %s\%s\n",
temp,
val,
basedir,
filename);
ret = -1;
}
} }
error_free:
free(temp);
return 0; return ret;
} }
/** /**
* write_sysfs_string_and_verify() - string write, readback and verify * write_sysfs_string_and_verify() - string write, readback and verify
* @filename: name of file to write to * @filename: name of file to write to
...@@ -149,40 +182,54 @@ int write_sysfs_int_and_verify(char *filename, char *basedir, int val) ...@@ -149,40 +182,54 @@ int write_sysfs_int_and_verify(char *filename, char *basedir, int val)
**/ **/
int write_sysfs_string_and_verify(char *filename, char *basedir, char *val) int write_sysfs_string_and_verify(char *filename, char *basedir, char *val)
{ {
int ret; return _write_sysfs_string(filename, basedir, val, 1);
FILE *sysfsfp; }
char temp[100];
sprintf(temp, "%s%s", basedir, filename);
sysfsfp = fopen(temp, "w");
if (sysfsfp == NULL)
return -1;
fprintf(sysfsfp, "%s", val);
fclose(sysfsfp);
sysfsfp = fopen(temp, "r"); int write_sysfs_string(char *filename, char *basedir, char *val)
if (sysfsfp == NULL) {
return -1; return _write_sysfs_string(filename, basedir, val, 0);
fscanf(sysfsfp, "%s", temp);
if (strcmp(temp, val) != 0) {
printf("Possible failure in string write %s to %s%s \n",
val,
basedir,
filename);
return -1;
}
return 0;
} }
int read_sysfs_posint(char *filename, char *basedir) int read_sysfs_posint(char *filename, char *basedir)
{ {
int ret; int ret;
FILE *sysfsfp; FILE *sysfsfp;
char temp[100]; char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
sprintf(temp, "%s%s", basedir, filename); if (temp == NULL) {
printf("Memory allocation failed");
return -ENOMEM;
}
sprintf(temp, "%s/%s", basedir, filename);
sysfsfp = fopen(temp, "r"); sysfsfp = fopen(temp, "r");
if (sysfsfp == NULL) if (sysfsfp == NULL) {
return -1; ret = -errno;
goto error_free;
}
fscanf(sysfsfp, "%d\n", &ret); fscanf(sysfsfp, "%d\n", &ret);
fclose(sysfsfp); fclose(sysfsfp);
error_free:
free(temp);
return ret;
}
int read_sysfs_float(char *filename, char *basedir, float *val)
{
float ret = 0;
FILE *sysfsfp;
char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
if (temp == NULL) {
printf("Memory allocation failed");
return -ENOMEM;
}
sprintf(temp, "%s/%s", basedir, filename);
sysfsfp = fopen(temp, "r");
if (sysfsfp == NULL) {
ret = -errno;
goto error_free;
}
fscanf(sysfsfp, "%f\n", val);
fclose(sysfsfp);
error_free:
free(temp);
return ret; return ret;
} }
/* Industrialio test ring buffer with a lis3l02dq acceleromter /* Industrialio ring buffer with a lis3l02dq accelerometer
* *
* Copyright (c) 2008 Jonathan Cameron * Copyright (c) 2008 Jonathan Cameron
* *
...@@ -6,125 +6,181 @@ ...@@ -6,125 +6,181 @@
* under the terms of the GNU General Public License version 2 as published by * under the terms of the GNU General Public License version 2 as published by
* the Free Software Foundation. * the Free Software Foundation.
* *
* Assumes suitable udev rules are used to create the dev nodes as named here. * This program is primarily intended as an example application.
*/ */
#include <dirent.h> #include <dirent.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h> #include <stdio.h>
#include <errno.h> #include <errno.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/dir.h> #include <sys/dir.h>
#include <linux/types.h> #include <linux/types.h>
#include "iio_utils.h" #include "iio_utils.h"
static const char *ring_access = "/dev/iio/lis3l02dq_ring_access"; const char *device_name = "lis3l02dq";
static const char *ring_event = "/dev/iio/lis3l02dq_ring_event"; const char *trigger_name_base = "lis3l02dq-dev";
static const char *device_name = "lis3l02dq"; const int num_vals = 3;
static const char *trigger_name = "lis3l02dq-dev0"; const int scan_ts = 1;
static int NumVals = 3; const int buf_len = 128;
static int scan_ts = 1; const int num_loops = 10;
static int RingLength = 128;
/* /*
* Could get this from ring bps, but only after starting the ring * Could get this from ring bps, but only after starting the ring
* which is a bit late for it to be useful * which is a bit late for it to be useful.
*
* Todo: replace with much more generic version based on scan_elements
* directory.
*/ */
int size_from_scanmode(int numVals, int timestamp) int size_from_scanmode(int num_vals, int timestamp)
{ {
if (numVals && timestamp) if (num_vals && timestamp)
return 16; return 16;
else if (timestamp) else if (timestamp)
return 8; return 8;
else else
return numVals*2; return num_vals*2;
} }
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int ret;
int i, j, k, toread; int i, j, k, toread;
FILE *fp_ev; FILE *fp_ev;
int fp; int fp;
char *trigger_name, *dev_dir_name, *buf_dir_name;
char *data; char *data;
size_t read_size; size_t read_size;
struct iio_event_data dat; struct iio_event_data dat;
int dev_num, trig_num;
char *buffer_access, *buffer_event;
const char *iio_dir = "/sys/bus/iio/devices/";
int scan_size;
float gain = 1;
char *BaseDirectoryName,
*TriggerDirectoryName,
*RingBufferDirectoryName;
BaseDirectoryName = find_type_by_name(device_name, "device"); /* Find out which iio device is the accelerometer. */
if (BaseDirectoryName == NULL) { dev_num = find_type_by_name(device_name, "device");
printf("Failed to find the %s \n", device_name); if (dev_num < 0) {
return -1; printf("Failed to find the %s\n", device_name);
ret = -ENODEV;
goto error_ret;
} }
TriggerDirectoryName = find_type_by_name(trigger_name, "trigger"); printf("iio device number being used is %d\n", dev_num);
if (TriggerDirectoryName == NULL) { asprintf(&dev_dir_name, "%sdevice%d", iio_dir, dev_num);
/*
* Build the trigger name.
* In this case we want the lis3l02dq's data ready trigger
* for this lis3l02dq. The naming is lis3l02dq_dev[n], where
* n matches the device number found above.
*/
ret = asprintf(&trigger_name, "%s%d", trigger_name_base, dev_num);
if (ret < 0) {
ret = -ENOMEM;
goto error_free_dev_dir_name;
}
/*
* Find the trigger by name.
* This is techically unecessary here as we only need to
* refer to the trigger by name and that name is already
* known.
*/
trig_num = find_type_by_name(trigger_name, "trigger");
if (trig_num < 0) {
printf("Failed to find the %s\n", trigger_name); printf("Failed to find the %s\n", trigger_name);
return -1; ret = -ENODEV;
goto error_free_triggername;
} }
RingBufferDirectoryName = find_ring_subelement(BaseDirectoryName, printf("iio trigger number being used is %d\n", trig_num);
"ring_buffer");
if (RingBufferDirectoryName == NULL) { /*
printf("Failed to find ring buffer\n"); * Read in the scale value - in a more generic case, first
return -1; * check for accel_scale, then the indivual channel scales
*/
ret = read_sysfs_float("accel_scale", dev_dir_name, &gain);
if (ret)
goto error_free_triggername;;
/*
* Construct the directory name for the associated buffer.
* As we know that the lis3l02dq has only one buffer this may
* be built rather than found.
*/
ret = asprintf(&buf_dir_name, "%sdevice%d:buffer0", iio_dir, dev_num);
if (ret < 0) {
ret = -ENOMEM;
goto error_free_triggername;
} }
/* Set the device trigger to be the data rdy trigger found above */
if (write_sysfs_string_and_verify("trigger/current_trigger", ret = write_sysfs_string_and_verify("trigger/current_trigger",
BaseDirectoryName, dev_dir_name,
(char *)trigger_name) < 0) { trigger_name);
printf("Failed to write current_trigger file \n"); if (ret < 0) {
return -1; printf("Failed to write current_trigger file\n");
goto error_free_buf_dir_name;
} }
/* Setup ring buffer parameters */ /* Setup ring buffer parameters */
if (write_sysfs_int("length", RingBufferDirectoryName, ret = write_sysfs_int("length", buf_dir_name, buf_len);
RingLength) < 0) { if (ret < 0)
printf("Failed to open the ring buffer length file \n"); goto error_free_buf_dir_name;
return -1;
}
/* Enable the ring buffer */ /* Enable the buffer */
if (write_sysfs_int("ring_enable", RingBufferDirectoryName, 1) < 0) { ret = write_sysfs_int("ring_enable", buf_dir_name, 1);
printf("Failed to open the ring buffer control file \n"); if (ret < 0)
return -1; goto error_free_buf_dir_name;
};
data = malloc(size_from_scanmode(NumVals, scan_ts)*RingLength); data = malloc(size_from_scanmode(num_vals, scan_ts)*buf_len);
if (!data) { if (!data) {
printf("Could not allocate space for usespace data store\n"); ret = -ENOMEM;
return -1; goto error_free_buf_dir_name;
}
ret = asprintf(&buffer_access,
"/dev/device%d:buffer0:access0",
dev_num);
if (ret < 0) {
ret = -ENOMEM;
goto error_free_data;
} }
ret = asprintf(&buffer_event, "/dev/device%d:buffer0:event0", dev_num);
if (ret < 0) {
ret = -ENOMEM;
goto error_free_data;
}
/* Attempt to open non blocking the access dev */ /* Attempt to open non blocking the access dev */
fp = open(ring_access, O_RDONLY | O_NONBLOCK); fp = open(buffer_access, O_RDONLY | O_NONBLOCK);
if (fp == -1) { /*If it isn't there make the node */ if (fp == -1) { /*If it isn't there make the node */
printf("Failed to open %s\n", ring_access); printf("Failed to open %s\n", buffer_access);
return -1; ret = -errno;
goto error_free_buffer_event;
} }
/* Attempt to open the event access dev (blocking this time) */ /* Attempt to open the event access dev (blocking this time) */
fp_ev = fopen(ring_event, "rb"); fp_ev = fopen(buffer_event, "rb");
if (fp_ev == NULL) { if (fp_ev == NULL) {
printf("Failed to open %s\n", ring_event); printf("Failed to open %s\n", buffer_event);
return -1; ret = -errno;
goto error_close_buffer_access;
} }
/* Wait for events 10 times */ /* Wait for events 10 times */
for (j = 0; j < 10; j++) { for (j = 0; j < num_loops; j++) {
read_size = fread(&dat, 1, sizeof(struct iio_event_data), read_size = fread(&dat, 1, sizeof(struct iio_event_data),
fp_ev); fp_ev);
switch (dat.id) { switch (dat.id) {
case IIO_EVENT_CODE_RING_100_FULL: case IIO_EVENT_CODE_RING_100_FULL:
toread = RingLength; toread = buf_len;
break; break;
case IIO_EVENT_CODE_RING_75_FULL: case IIO_EVENT_CODE_RING_75_FULL:
toread = RingLength*3/4; toread = buf_len*3/4;
break; break;
case IIO_EVENT_CODE_RING_50_FULL: case IIO_EVENT_CODE_RING_50_FULL:
toread = RingLength/2; toread = buf_len/2;
break; break;
default: default:
printf("Unexpecteded event code\n"); printf("Unexpecteded event code\n");
...@@ -132,39 +188,51 @@ int main(int argc, char **argv) ...@@ -132,39 +188,51 @@ int main(int argc, char **argv)
} }
read_size = read(fp, read_size = read(fp,
data, data,
toread*size_from_scanmode(NumVals, scan_ts)); toread*size_from_scanmode(num_vals, scan_ts));
if (read_size == -EAGAIN) { if (read_size == -EAGAIN) {
printf("nothing available \n"); printf("nothing available\n");
continue; continue;
} }
scan_size = size_from_scanmode(num_vals, scan_ts);
for (i = 0; for (i = 0; i < read_size/scan_size; i++) {
i < read_size/size_from_scanmode(NumVals, scan_ts); for (k = 0; k < num_vals; k++) {
i++) { __s16 val = *(__s16 *)(&data[i*scan_size
for (k = 0; k < NumVals; k++) {
__s16 val = *(__s16 *)(&data[i*size_from_scanmode(NumVals, scan_ts)
+ (k)*2]); + (k)*2]);
printf("%05d ", val); printf("%05f ", (float)val*gain);
} }
printf(" %lld\n", printf(" %lld\n",
*(__s64 *)(&data[(i+1)*size_from_scanmode(NumVals, scan_ts) *(__s64 *)(&data[(i + 1)
*size_from_scanmode(num_vals,
scan_ts)
- sizeof(__s64)])); - sizeof(__s64)]));
} }
} }
/* Stop the ring buffer */ /* Stop the ring buffer */
if (write_sysfs_int("ring_enable", RingBufferDirectoryName, 0) < 0) { ret = write_sysfs_int("ring_enable", buf_dir_name, 0);
printf("Failed to open the ring buffer control file \n"); if (ret < 0)
return -1; goto error_close_buffer_event;
};
/* Disconnect from the trigger - just write a dummy name.*/
/* Disconnect from the trigger - writing something that doesn't exist.*/ write_sysfs_string("trigger/current_trigger",
write_sysfs_string_and_verify("trigger/current_trigger", dev_dir_name, "NULL");
BaseDirectoryName, "NULL");
free(BaseDirectoryName); error_close_buffer_event:
free(TriggerDirectoryName); fclose(fp_ev);
free(RingBufferDirectoryName); error_close_buffer_access:
close(fp);
error_free_data:
free(data); free(data);
error_free_buffer_access:
return 0; free(buffer_access);
error_free_buffer_event:
free(buffer_event);
error_free_buf_dir_name:
free(buf_dir_name);
error_free_triggername:
free(trigger_name);
error_free_dev_dir_name:
free(dev_dir_name);
error_ret:
return ret;
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment