0

I am trying to measure the time period of a square wave on a Beaglebone running Angstrom OS. I have written a kernel driver to register an ISR in which I'm timing the pulses. Everything is working fine, but the time interval being measured is completely wrong. I'm using do_gettimeofday() function to measure the time. When I do the same in userspace program using poll() function, I'm able to achieve correct values (it shows around 1007 us for a 1000us wave), but when I use the driver to measure the pulse, I get the interval as 1923us. I have no idea why the time interval in the kernel is higher than that in user space. I have attached my code below. I would be grateful if someone can find the mistake in my program.

kernel ISR:

static irqreturn_t ISR ( int irq, void *dev_id)
{

prev = c;
do_gettimeofday(&c);

printk(KERN_ALERT "%ld", (c.tv_usec - prev.tv_usec));
return IRQ_HANDLED;
}

userspace prog:

while(1){
    prev = start;
    gettimeofday(&start, NULL);
    rc = poll(&fdset, 1, 20000);
    if(rc < 0){
        printf("Error in rc\n");
        return -1;
    }

    if(rc == 0){
        printf("Timed out\n");
        return -1;
    }

    if (fdset.revents & POLLPRI) {
        len = read(fdset.fd, buf, 2);
        printf("%ld\n", (start.tv_usec - prev.tv_usec));
    }

}
  • [Link to another question about different clock/time functions.](http://stackoverflow.com/questions/12392278/getrusage-vs-clock-gettime-vs-clock-vs-gettimeofday) – sawdust Mar 21 '13 at 02:18
  • The time interval might not be "wrong" as you claim. Your code is trying to acquire a lock (by calling `do_gettimeofday()`)in an ISR. – sawdust Mar 21 '13 at 02:33
  • Thanks for the reply. But I don't understand how acquiring a lock results in this kind of behavior. Could you please explain? – user1971707 Mar 21 '13 at 06:17
  • Trying to acquire a lock in an ISR is dangerous if that lock could be held by another ISR. There's the potential for a deadlock, if not unexpected delays. – sawdust Mar 21 '13 at 09:02
  • Hi. How would u design the interrupt so that it measures the pulse width. I have to measure pulse widths as low as 250us. Currently using this approach I can correctly measure pulse widths if it is higher, around 10ms. – user1971707 Mar 22 '13 at 23:13

1 Answers1

1

For profiling interrupt latency, I find it quite useful to be lazy and to set a GPIO pin then measure the time with an oscilloscope. Probably not the answer you want, but it might help you over a hurdle quickly.

Jadon
  • 51
  • 3