LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-01-2010, 06:46 PM   #1
jayhorizon
LQ Newbie
 
Registered: Oct 2010
Posts: 17

Rep: Reputation: 0
Pulse counter or frequency of incoming pulse train


Hello,
I am trying to determine the frequency of a signal coming into an imx27 processor running linux os 2.6.22 If i were using a micro i would set up a timer for a set period of time and then count up the rising edges coming in. However I am not sure how to do this with C code in linux. Any thoughts would be appreciated.
Thanks!
 
Old 10-03-2010, 06:42 AM   #2
jf.argentino
Member
 
Registered: Apr 2008
Location: Toulon (France)
Distribution: FEDORA CORE
Posts: 493

Rep: Reputation: 50
imx27 have timers / counters, maybe a kernel driver handle them, if not write one
 
Old 10-03-2010, 02:11 PM   #3
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.

I agree with jf.argentino. Look if the platform provides timer drivers (it should) and then use the driver. You'll have to write another one to count the edges. I guess the signal comes on GPIO?
 
Old 10-03-2010, 02:21 PM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by jayhorizon View Post
Hello,
... However I am not sure how to do this with C code in linux. Any thoughts would be appreciated.
Thanks!
In addition to what's been said - you need to somehow register each incoming pulse/edge. For example, it can cause a HW interrupt (if your HW allows to hook it up/configure it this), and the interrupt service routine will increment a program counter implemented as a global or static variable.
 
Old 10-04-2010, 08:18 AM   #5
jayhorizon
LQ Newbie
 
Registered: Oct 2010
Posts: 17

Original Poster
Rep: Reputation: 0
Would I be able to use setitimer() and then just count the rising edges? Then i could use that alarm to stop the counting?
 
Old 10-04-2010, 08:21 AM   #6
jayhorizon
LQ Newbie
 
Registered: Oct 2010
Posts: 17

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Maria
I guess the signal comes on GPIO?
Yes it is coming in on a GPIO pin
 
Old 10-21-2010, 03:50 PM   #7
jayhorizon
LQ Newbie
 
Registered: Oct 2010
Posts: 17

Original Poster
Rep: Reputation: 0
I tried this code but it sometimes hangs and other times gives different results. I should get around the same number each time this is run.

struct timeval start;
struct timeval currnt;

int reg1 = 1;

int reg2 = 1;

u16 count;

int g_count = 0;

int i;


for(i = 0; i < loops;i++)

{

count = 0;

reg1 = mxc_get_gpio_datain(GLOSS_SENSE);

while((reg1 != 1) && (reg2 != 0))
{
reg2 = reg1;

reg1 = mxc_get_gpio_datain(GLOSS_SENSE);
}


do_gettimeofday(&start);
do_gettimeofday(&currnt);

while((currnt.tv_usec-start.tv_usec) < 100000)//100 ms

{

if((reg1 == 0) && (reg2 == 1))

count++;


reg2 = reg1;

reg1 = mxc_get_gpio_datain(GLOSS_SENSE);
do_gettimeofday(&currnt);

}

g_count += count;

printk("sp2000: count, %d.\n", count);

}
 
Old 10-22-2010, 10:37 AM   #8
jayhorizon
LQ Newbie
 
Registered: Oct 2010
Posts: 17

Original Poster
Rep: Reputation: 0
i have modified the above code and implemented a timer (setup_timer and mod_timer) so that i am counting for 100ms. i still am having an issue though with teh actual count. I don't htink i am counting the edges properly. I am still doing what i did above in the part where i look at teh pin and register it in reg1 and reg2 and then count like that but i don't think it is realiable.
Any advice would be appreciated.
Thanks,
Jen
 
Old 10-26-2010, 03:50 PM   #9
jayhorizon
LQ Newbie
 
Registered: Oct 2010
Posts: 17

Original Poster
Rep: Reputation: 0
I have a timer set up and interrupts enabled but now my problem is that the interrupts are happeneing so often that the processor can't pay attention to the timer and goes off in lala land counting interrupts and looses track of time.
Any thoughts please?
 
Old 10-26-2010, 09:27 PM   #10
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by jayhorizon View Post
I have a timer set up and interrupts enabled but now my problem is that the interrupts are happeneing so often that the processor can't pay attention to the timer and goes off in lala land counting interrupts and looses track of time.
Any thoughts please?
Lower the frequency of interrupts.
 
Old 10-27-2010, 08:41 AM   #11
jayhorizon
LQ Newbie
 
Registered: Oct 2010
Posts: 17

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Sergei Steshenko View Post
Lower the frequency of interrupts.
The problem is that I can't adjust the freqency. I have to measure between 10kHz and 500kHz signals.
Does anyone know a way i can do this?
 
Old 10-27-2010, 09:33 AM   #12
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by jayhorizon View Post
The problem is that I can't adjust the freqency. I have to measure between 10kHz and 500kHz signals.
Does anyone know a way i can do this?
If your system can't cope with 500k interrupts per second, then it can't. I.e. your system architect (the one responsible for bot HW and SW) made a poor choice of low performing HW.

Still, why do you need to process 500k interrupts per second ? I.e. which kind if real life events in your case requires 2us resolution ?
 
Old 10-27-2010, 09:45 AM   #13
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Quote:
Originally Posted by Sergei Steshenko View Post
In addition to what's been said - you need to somehow register each incoming pulse/edge. For example, it can cause a HW interrupt (if your HW allows to hook it up/configure it this), and the interrupt service routine will increment a program counter implemented as a global or static variable.
I think the notion of interrupting on each pulse/edge is the wrong approach in this case. The application will dictate the real solution, of course, and tempered by the capability of the hardware. Since you want to measure frequency, there is somewhat of an implied integration period, and this would be determined by the requirements. Given that, it would seem to make sense to let the hardware count pulses (and evidently, your hardware has this capability), and then take a periodic reading of the pulse count. If you trigger a reading by software once per second, then your reading of the counter will be in Hz. You need to be careful that the counter doesn't overflow, and might need to reset it on each reading; these are implementation details.

--- rod
 
Old 10-27-2010, 11:05 AM   #14
jayhorizon
LQ Newbie
 
Registered: Oct 2010
Posts: 17

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Sergei Steshenko View Post
If your system can't cope with 500k interrupts per second, then it can't. I.e. your system architect (the one responsible for bot HW and SW) made a poor choice of low performing HW.

Still, why do you need to process 500k interrupts per second ? I.e. which kind if real life events in your case requires 2us resolution ?

I am reading in a light detector on the gpio. My problem is the layout guys didn't bring in the signal on the pin that had a counter on it. So i am making my own. i am seeing that just counting edges is my best bet at this point instead of using interrupts. I am still not where i want to be in repeatablity but i am working on it.
I am using a timer that usses msecs_to_jiffies(100). Is that the most accurate way to get a 100 ms time period?
 
Old 10-28-2010, 12:07 AM   #15
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by jayhorizon View Post
... My problem is the layout guys didn't bring in the signal on the pin that had a counter on it. ...
So, use a wire and a soldering iron.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Problem with Pulse Audio codeman1234 Linux - Software 15 01-13-2012 11:00 AM
pulse audio sulekha Ubuntu 1 02-10-2009 05:48 PM
how can i get pulse audio to work i xfce deathalele Linux - Software 0 08-30-2008 07:02 PM
probléme with pulse dialing djnux Linux - Networking 1 04-07-2005 01:21 PM
Pulse Dialing chrisharkness Linux - General 2 11-18-2002 08:59 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration