Re: ad7793 iio-ring test read issue - Reg

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, 23 Aug 2011 13:58:34 +0100, Jonathan Cameron wrote
> On 08/23/11 13:43, malas wrote:
> > YES : Gregs updated kernel taken from link below:
> > 
> > http://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging-2.6.git
> Right now that's pretty much mainline.
> 
> See comments below and please reply inline as it makes this sort
> of discussion much easier to follow.
> > 
> > 
> > On Tue, 23 Aug 2011 13:39:12 +0100, Jonathan Cameron wrote
> >> On 08/23/11 13:17, malas wrote:
> >>>
> >>>
> >>> Sorry, I did not explicitly mention that. I did use the new staging kernel referred by
> >>> Michael Hennerich and integrated with 2010RC5 uclinux dist .
> >>
> >> Hmm. As in current contents of Greg KH's staging/staging-next?
> >>
> >> I'll assume so... See below.
> >>
> >>>
> >>> On Tue, 23 Aug 2011 12:39:53 +0100, Hennerich, Michael wrote
> >>>> malas wrote on 2011-08-23:
> >>>>>
> >>>>>
> >>>>> Dear Jonathan Cameron,
> >>>>>
> >>>>> The kernel version I am using is :
> >>>>
> >>>> Didn't you say that you use the staging kernel?
> >>>>
> >>>>
> >>>
> >
http://blackfin.uclinux.org/gf/project/uclinux-dist/forum/?_forum_action=MessageReply&message_id=102839&action=ForumBrowse
> >>>>
> >>>> Or copied drivers/staging/iio over to your Blackfin kernel tree?
> >>>>
> >>>>>
> >>>>> ---------------------------------------------------------------------
> >>>>> midas@ubuntu:~/Mala/Telemedicine/uclinux_source/uc2010_source/uclinux/2
> >>>>> 010R1$ svn info Path: . URL:
> >>>>> svn://blackfin.uclinux.org/uclinux-dist/branches/2010R1 Repository Root:
> >>>>> svn://blackfin.uclinux.org/uclinux-dist Repository UUID:
> >>>>> 3e2a5524-39e8-4cb9-84a9-c49da69cba3a Revision: 10161 Node Kind:
> >>>>> directory Schedule: normal Last Changed Author: vapier Last Changed Rev:
> >>>>> 10136 Last Changed Date: 2011-04-02 22:36:15 +0530 (Sat, 02 Apr 2011)
> >>>>> ---------------------------------------------------------------------
> >>>>>
> >>>>> Regards
> >>>>> Mala
> >>>>>
> >>>>> On Tue, 23 Aug 2011 12:21:25 +0100, Jonathan Cameron wrote
> >>>>>> On 08/23/11 09:18, malas wrote:
> >>>>>>>
> >>>>>>> Dear Michael Hennerich,
> >>>>>>>
> >>>>>>>
> >>>>>>> I shall post iio quesris in this mail list as per your suggestions.
> >>>>>>>
> >>>>>>> AD7793 driver is built into kernel. The chip gets configured and am
> >>>>>>> able to read data on continuous mode via spi interface using ad7793
> >>>>>>> driver at kernel level.
> >>>>>>>
> >>>>>>> While testing userspace test iio-ring to read data from ring buffer,
> >>>>>>> generic_buffer code was executed, it  proceeds till opening
> >>>>>>> /sys/bus/iio/devices/device0:buffer0 and Read call fails.
> >>>>>> Question 1: What's the kernel and where did you get it from?
> >>>>>> (Interfaces are moving fast so it's plausible you've fallen in a hole
> >>>>>> where userspace wasn't matching kernelspace).
> >>>>>>>
> >>>>>>> In fs I can list the iio device ad7793 with following properties
> >>>>>>> get/set.
> >>>>>>>
> >>>>>>> root:/> ls /sys/bus/iio/devices/device0\:buffer0/
> >>>>>>> bytes_per_datum  enable           power            subsystem
> >>>>>>> dev              length           scan_elements    uevent
> >>>>>>> root:/>
> >>>>>>>
> >>>>>>> I find access0 and event0 missing in this list. Is that the reason
> >>>>> for read() fails.
> >>>>>> Yes.  Question is why and without kernel version I don't know where to
> >>>>>> look I'm afraid.
> >> Ah, this does look like a missmatch in interfaces between userspace and kernel.
> >>
> >> At this point in time we have scrapped the event0 chrdev (done via polling on
> >> the access one) and the access one has be flattened into device0\:buffer0/
> >> (read dev).
> >>
> >> Looks like the generic_buffer.c example does line up though. Where did the
> >> 'test iio-ring' come from? Is that one yours Michael? 


iio-ring.c under uclinux2010/user/blkfin-test/iio-ring-test. The file is in the
attached. I did not use that since i didn't find access0 or event0 files under
/sys/bus/iio/devices/device0:buffer0.

I have tested with the generic_buffer.c example (under iio/documentaion folder) to get
Ad7793 received samples to user space from ring buffer.
The app opens: /dev/device0:buffer0 for poll and reading samples.
Is the device name for the open() correct.


> >>>>>>
> >>>>>>>
> >>>>>>> Please help.
> >>>>>>>
> >>>>>>> Thanks
> >>>>>>> Mala
> >>>>>>> --
> >>>>>>> Open WebMail Project (http://openwebmail.org)
> >>>
> > 
> > 
> > --
> > Open WebMail Project (http://openwebmail.org)
> > 
> > 
> > 
> >


--
Open WebMail Project (http://openwebmail.org)



/*
 * Analog Devices IIO devices ring test program
 *
 * Author:       Barry Song
 * based on:
 * 	Jonathan Cameron's Industrialio ring buffer with a lis3l02dq accelerometer
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published by
 * the Free Software Foundation.
 *
 * This program is primarily intended as an example application.
 */

#include <dirent.h>
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/dir.h>
#include <linux/types.h>
#include <unistd.h>

#include "iio_utils.h"

#define IIO_TST_DBG

char device_name[IIO_MAX_NAME_LENGTH];
char trigger_name_base[IIO_MAX_NAME_LENGTH];
int num_vals;
int num_loops;
const int scan_ts = 1;
const int buf_len = 128;

char* argv0;
/*
 * Could get this from ring bps, but only after starting the ring
 * 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 num_vals, int timestamp)
{
	if (num_vals && timestamp)
		return (((num_vals*sizeof(__s16))+sizeof(__s64)-1) & ~(sizeof(__s64)-1))+sizeof(__s64);
	else if (timestamp)
		return 8;
	else
		return num_vals*sizeof(__s16);
}

void usage(void){
	fprintf(stderr, "usage:%s -n devicename -c valuenumber -l looptimes\n", argv0);
	exit(1);
}

int main(int argc, char **argv)
{
	int ret;
	int i, j, k, toread;
	FILE *fp_ev;
	int fp;

	char *trigger_name, *dev_dir_name, *buf_dir_name;
	char *data;
	size_t read_size;
	struct iio_event_data dat;
	int dev_num, trig_num;
	int c;

	char *buffer_access, *buffer_event;
	const char *iio_dir = "/sys/bus/iio/devices/";
	int scan_size;

	argv0= argv[0];

	while( (c = getopt(argc, argv, "n:c:l:")) != -1 ) {
		switch (c) {
		case 'n':
			strcpy(device_name, optarg);
			sprintf(trigger_name_base,"%s-dev", device_name);
			break;
		case 'c': num_vals = atoi(optarg); break;
		case 'l': num_loops = atoi(optarg); break;
		default: usage();
		}
	}
	if (!sizeof(device_name) || !num_vals || !num_loops)
		usage();

	/* Find out which iio device is the accelerometer. */
	dev_num = find_type_by_name(device_name, "device");
	if (dev_num < 0) {
		printf("Failed to find the %s\n", device_name);
		ret = -ENODEV;
		goto error_ret;
	}
	printf("iio device number being used is %d\n", dev_num);
	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);
		ret = -ENODEV;
		goto error_free_triggername;
	}
	printf("iio trigger number being used is %d\n", trig_num);

	/*
	 * 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 */
	ret = write_sysfs_string_and_verify("trigger/current_trigger",
					dev_dir_name,
					trigger_name);
	if (ret < 0) {
		printf("Failed to write current_trigger file\n");
		goto error_free_buf_dir_name;
	}
#ifdef IIO_TST_DBG
	printf("Going to setup ring parameters, length\n");
#endif	

	/* Setup ring buffer parameters */
	ret = write_sysfs_int("length", buf_dir_name, buf_len);
	if (ret < 0)
		goto error_free_buf_dir_name;


#ifdef IIO_TST_DBG
	printf("Going to setup ring parameters, ring_enable\n");
#endif	


	/* Enable the buffer */
//	ret = write_sysfs_int("ring_enable", buf_dir_name, 1); // Re: Mala changing to enable
	ret = write_sysfs_int("enable", buf_dir_name, 1);
	if (ret < 0)
		goto error_free_buf_dir_name;

	data = malloc(size_from_scanmode(num_vals, scan_ts)*buf_len);
	if (!data) {
		ret = -ENOMEM;
		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;
	}
#ifdef IIO_TST_DBG
	printf("Going to open buffer_access in nblk \n");
#endif	

	
	/* Attempt to open non blocking the access dev */
	fp = open(buffer_access, O_RDONLY | O_NONBLOCK);
	if (fp == -1) { /*If it isn't there make the node */
		printf("Failed to open %s\n", buffer_access);
		ret = -errno;
		goto error_free_buffer_event;
	}

#ifdef IIO_TST_DBG
	printf("Going to setup opening buffer-event in blocking mode\n");
#endif	

	/* Attempt to open the event access dev (blocking this time) */
	fp_ev = fopen(buffer_event, "rb");
	if (fp_ev == NULL) {
		printf("Failed to open %s\n", buffer_event);
		ret = -errno;
		goto error_close_buffer_access;
	}

#ifdef IIO_TST_DBG
	printf("Starting to wait for events\n");
#endif	

	/* Wait for events 10 times */
	for (j = 0; j < num_loops; j++) {
		read_size = fread(&dat, 1, sizeof(struct iio_event_data),
				  fp_ev);
		switch (dat.id) {
		case IIO_EVENT_CODE_RING_100_FULL:
			toread = buf_len;
			break;
		case IIO_EVENT_CODE_RING_75_FULL:
			toread = buf_len*3/4;
			break;
		case IIO_EVENT_CODE_RING_50_FULL:
			toread = buf_len/2;
			break;
		default:
			printf("Unexpecteded event code\n");
			continue;
		}
#ifdef IIO_TST_DBG
	printf("Entering blocked read\n");
#endif	

		read_size = read(fp,
				 data,
				 toread*size_from_scanmode(num_vals, scan_ts));
		if (read_size == -EAGAIN) {
			printf("nothing available\n");
			continue;
		}

#ifdef IIO_TST_DBG
	printf("Read %d samples\n",read_size);
#endif	

		scan_size = size_from_scanmode(num_vals, scan_ts);
		for (i = 0; i < read_size/scan_size; i++) {
			for (k = 0; k < num_vals; k++) {
				__s16 val = *(__s16 *)(&data[i*scan_size
							     + (k)*2]);
				printf("%04x ", (unsigned short)val);
			}
			printf(" %lld\n",
			       *(__s64 *)(&data[(i + 1)
						*size_from_scanmode(num_vals,
								    scan_ts)
						- sizeof(__s64)]));
		}
	}

#ifdef IIO_TST_DBG
	printf("Finished data acquisition, stopping ring buffer\n");
#endif	


	/* Stop the ring buffer */
	ret = write_sysfs_int("ring_enable", buf_dir_name, 0);
	if (ret < 0)
		goto error_close_buffer_event;

	/* Disconnect from the trigger - just write a dummy name.*/
	write_sysfs_string("trigger/current_trigger",
			dev_dir_name, "NULL");

error_close_buffer_event:
	fclose(fp_ev);
error_close_buffer_access:
	close(fp);
error_free_data:
	free(data);
error_free_buffer_access:
	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;
}

[Index of Archives]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Input]     [Linux Kernel]     [Linux SCSI]     [X.org]

  Powered by Linux