Pulse counter or frequency of incoming pulse train
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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!
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?
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.
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
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?
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?
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 ?
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.
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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.