2008-06-10 03:54:28     How to calculate the time elapsed precisely?

Document created by Aaronwu Employee on Aug 6, 2013
Version 1Show Document
  • View in full screen mode

2008-06-10 03:54:28     How to calculate the time elapsed precisely?

Zhi Qiang Zhang (CHINA)

Message: 56896   

 

How to calculate the time elapsed precisely? The precision should be in 1/10000 second.

 

 

 

Regards,

 

ZhangZQ

QuoteReplyEditDelete

 

 

2008-06-10 08:05:31     Re: How to calculate the time elapsed precisely?

Robin Getz (UNITED STATES)

Message: 56926   

 

ZhangZQ:

 

Where are you trying to do this? In a driver?

 

In a user space application use gettimeofday - measures in microseconds, or 1/100000 seconds resolution.

 

-Robin

 

 

QuoteReplyEditDelete

 

 

2008-06-10 10:12:18     Re: How to calculate the time elapsed precisely?

Zhi Qiang Zhang (CHINA)

Message: 56930   

 

Robin,

 

I will use it in user space application not in driver, gettimeofday can measure in 1/1000 second, but I want 1/10000, what can do? Thanks.

 

 

 

Regards,

 

ZhangZQ

QuoteReplyEditDelete

 

 

2008-06-10 10:45:34     Re: How to calculate the time elapsed precisely?

Mike Frysinger (UNITED STATES)

Message: 56932   

 

gettimeofday is accurate to microseconds (like Robin said) which means it is accurate enough according to your needs

QuoteReplyEditDelete

 

 

2008-06-11 01:54:08     Re: How to calculate the time elapsed precisely?

Zhi Qiang Zhang (CHINA)

Message: 56960   

 

Microsecond = 1/1000 second, but I want the 1/10000 second.

 

 

 

Regards,

 

ZhangZQ

QuoteReplyEditDelete

 

 

2008-06-11 02:14:54     Re: How to calculate the time elapsed precisely?

Mike Frysinger (UNITED STATES)

Message: 56962   

 

sorry, but that's incorrect.  milli = 10^3, micro = 10^6.

QuoteReplyEditDelete

 

 

2008-06-11 04:06:29     Re: How to calculate the time elapsed precisely?

Zhi Qiang Zhang (CHINA)

Message: 56968   

 

yes, timeval.tv_usec is 10^6, but the lower 10^3 should be zero, for example, I used gettimeofday in bf531 board running the most updated version(svn) of uclinux, and I print the tv_usec, the values should be

 

123000

 

12000

 

899000

 

so I can't get the precise time elapsed.

 

 

 

Regards,

 

ZhangZQ

QuoteReplyEditDelete

 

 

2008-06-11 04:31:36     Re: How to calculate the time elapsed precisely?

Mike Frysinger (UNITED STATES)

Message: 56988   

 

that has nothing to do with the interface ... gettimeofday is the thing to use

 

your kernel tick is probably set to 1000HZ ... you could try setting it faster (like 100HZ)

QuoteReplyEditDelete

 

 

2008-06-11 09:36:12     Re: How to calculate the time elapsed precisely?

Graham Davies (UNITED STATES)

Message: 56996   

 

I had a requirement to do exactly what you're asking, i.e. measure elapsed time to a precision of 0.1 ms.  There does not seem to be a ready-made uClinux feature capable of this.  I ended up writing a kernel driver with a user-space interface to get the value of a free-running counter updated by a 10,000 Hz interrupt.

 

We've been through all this discussion of gettimeofday before with someone else who had a similar problem, though the suggestion to set the kernel tick "faster" from 1000Hz to 100Hz, is new, I must admit.  If we guess that gettimeofday has a *precision* (or accuracy) equal to the period of the kernel tick (although, as Robin says, it has a *resolution* of one microsecond, hence all the zeros), you'd need to set the tick rate up to 10,000 Hz to get what you want.  This may or may not be a good idea, depending on what happens on a kernel tick, which I don't know.  Rather than try to get these questions answered, I found it easier to just write my own driver.  This had the additional benefit that my software continues to work when someone adjusts the time of day.

 

Graham.

 

 

QuoteReplyEditDelete

 

 

2008-06-11 10:26:52     Re: How to calculate the time elapsed precisely?

Robin Getz (UNITED STATES)

Message: 56998   

 

Graham/ZhangZQ:

 

If gettimeofday is not returning the right values (accurate to 10^-6) - it may be a math bug (overflow, which limits resolution) in the kernel that someone introduced - as it should be able to do this... (at least I thought it did last time I touched things

 

What specific kernel version are you using? What specific kernel setup do you have? (can you look in your ./linux-2.6.x/.config)

 

#

# Automatically generated make config: don't edit

# Linux kernel version: 2.6.24.7

#

# Kernel Timer/Scheduler

#

# CONFIG_HZ_100 is not set

CONFIG_HZ_250=y

# CONFIG_HZ_300 is not set

# CONFIG_HZ_1000 is not set

CONFIG_HZ=250

CONFIG_GENERIC_TIME=y

CONFIG_GENERIC_CLOCKEVENTS=y

# CONFIG_CYCLES_CLOCKSOURCE is not set

# CONFIG_TICK_ONESHOT is not set

# CONFIG_NO_HZ is not set

# CONFIG_HIGH_RES_TIMERS is not set

CONFIG_GENERIC_CLOCKEVENTS_BUILD=y

 

 

Thanks

 

 

QuoteReplyEditDelete

 

 

2008-06-11 18:51:08     Re: How to calculate the time elapsed precisely?

Zhi Qiang Zhang (CHINA)

Message: 57048   

 

Robin,

 

The lower 10^3 of tv_usec are all zero, I am using this kernel,

 

kernel:    Linux release 2.6.24.7-ADI-2008R2-pre-svn4814, build #126 PREEMPT Wed Jun 11 22:58:34 HKT 2008

toolchain: bfin-uclinux-gcc release gcc version 4.1.2 (ADI svn)

user-dist: release svn-6753, build #324 Wed Jun 11 22:28:57 HKT 2008

 

 

 

and here is my setting.

 

#

# Kernel Timer/Scheduler

#

# CONFIG_HZ_100 is not set

CONFIG_HZ_250=y

# CONFIG_HZ_300 is not set

# CONFIG_HZ_1000 is not set

CONFIG_HZ=250

CONFIG_GENERIC_TIME=y

CONFIG_GENERIC_CLOCKEVENTS=y

# CONFIG_CYCLES_CLOCKSOURCE is not set

# CONFIG_TICK_ONESHOT is not set

# CONFIG_NO_HZ is not set

# CONFIG_HIGH_RES_TIMERS is not set

CONFIG_GENERIC_CLOCKEVENTS_BUILD=y

 

 

 

Regards,

 

ZhangZQ

QuoteReplyEditDelete

 

 

2008-06-11 18:56:21     Re: How to calculate the time elapsed precisely?

Zhi Qiang Zhang (CHINA)

Message: 57050   

 

Graham,

 

I also try to make a driver to read the jiffies as described in chap7 of LDD, and I made a program to check the precision,

 

void main(void)

{

        int fd = open("/dev/jif", O_RDONLY);

 

        unsigned long jif, tmp;

        int diff;

        int i;

 

        diff = 0;

        tmp = jif = 0;

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

        {

                read(fd, &jif, sizeof(long));

                if (tmp != jif)

                {

                        diff++;

                        tmp = jif;

                }

        }

 

        printf("%d\r\n", diff);

 

        close(fd);

}

 

/dev/jif is the driver simply to read the jiffies, this program loops 100000 times, got 100 different jiffies values, but don't know the time unit of each jiffies. How do you do in your driver? also simply read jiffies?

 

 

 

Regards,

 

ZhangZQ

QuoteReplyEditDelete

 

 

2008-06-12 10:05:45     Re: How to calculate the time elapsed precisely?

Graham Davies (UNITED STATES)

Message: 57139   

 

ZhangZQ,

 

No, I do not read jiffies to measure elapsed time.  I have a custom driver.  Here is a post I made on the subject:

 

http://blackfin.uclinux.org/gf/project/uclinux-dist/forum/?action=ForumBrowse&_forum_action=MessageReply&message_id=53168

 

In fact, you might want to read the entire thread.

 

I have watched all discussion on this subject carefully as it's hard for me to believe that uClinux is put forward as a real-time operating system and yet does not have a user-space facility for accessing a fast, free-running tick counter.  Even Windows has it.  So far, I've not seen anything to suggest that I was wrong to write my own tick counter facility.

 

The main reason I rejected the use of gettimeofday() is that my product allows the user to set the time of day, which, until I realized that another developer on the project used gettimeofday() as a tick count, cause the software to freak out.  The lack of precision would also be a problem, but because of the first problem I've not bothered to find out what limits the precision of gettimeofday().

 

The reason I don't use jiffies is that I don't know what all goes on inside uClinux when the jiffies counter is bumped.  My setting, like yours, is 250 Hz.  There is evidence that setting it to 1000 Hz is sensible, but, again like you, I need 0.1 ms resolution.  That's ten times faster.  It may not be a good idea to set the jiffies rate to 10,000 Hz.

 

All-in-all, writing my own tick counter was easier than trying to puzzle out why there isn't one already available.

 

Graham.

 

 

QuoteReplyEditDelete

 

 

2008-06-12 19:27:04     Re: How to calculate the time elapsed precisely?

Zhi Qiang Zhang (CHINA)

Message: 57185   

 

Graham,

 

How to make your own tick counter driver? Now I can only try another way, that is to calculate the time elapsed by another MCU, and send the result to bf531.

 

 

 

Regards,

 

ZhangZQ

QuoteReplyEditDelete

 

 

2008-06-13 10:45:22     Re: How to calculate the time elapsed precisely?

Graham Davies (UNITED STATES)

Message: 57240   

 

My tick counter driver just uses the Blackfin hardware timers in a very obvious way.  I'm pretty sure I found something in the distribution on which to base it and a lot of the details have leaked out of my head now.  Look for files in the drivers/char directory that #include gptimers.h.  My interrupt service routine increments a volatile 32-bit unsigned integer TickCount.  The driver ioctl call copies the value of TickCount into a user-space variable.  It's as simple as that.  In fact, it's probably pretty close to a "Hello World!" driver that anyone attempting to use uClinux for the Blackfin in a real-time application should alltempt before going on to the hard stuff.  If you need to measure elapsed time to 0.1 ms, I assume you have a real-time application.  If the foregoing looks too hard, maybe you should really think about whether uClinux is right for you.

 

Graham.

 

 

QuoteReplyEditDelete

 

 

2008-06-13 20:07:14     Re: How to calculate the time elapsed precisely?

Zhi Qiang Zhang (CHINA)

Message: 57277   

 

Graham,

 

Thank you very much for your information!

 

I am making a product that there is a wheel, and there are 75 sawtooth in the wheel per round, the MAX RPM of this wheel is 20. This wheel is controlled by anther MCU(AVR), the communication between MCU and BFIN through UART in 115200, when the wheel turned around the MCU will tell the BFIN on each movement of sawtooth. I need the time elapsed between each sawtooth for further processing.

 

 

 

Regards,

 

ZhangZQ

QuoteReplyEditDelete

 

 

2008-06-18 06:36:01     Re: How to calculate the time elapsed precisely?

Frans Klaver (NETHERLANDS)

Message: 57512   

 

Don't rely on the UART for accurate timing measurement, you may not notice it, but Linux' UART implementation isn't the fastest around with the flip buffer and all. If you really need accurate timing information, either have the AVR count clock ticks and send the results to the blackfin, or write a driver that captures a timer value when an input is toggled (or set for that matter). Let the AVR toggle the input of the Blackfin when a rotation has been made

 

Cheers,

 

Frans

Attachments

    Outcomes