This is the mail archive of the ecos-discuss@sourceware.org mailing list for the eCos project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: ecos and gettimeofday()


Alexander Neundorf <neundorf@kde.org> writes:

> IMHO according to 
> http://sources.redhat.com/ecos/docs-latest/ref/hal-clocks-and-timers.html 
> also this version is not completely correct:
> 
> "CYGNUM_HAL_RTC_NUMERATOR and CYGNUM_HAL_RTC_DENOMINATOR specify the 
> resolution of the clock interrupt. [...] The result of dividing the numerator 
> by the denominator should correspond to the number of nanoseconds between 
> clock interrupts. For example a numerator of 1000000000 and a denominator of 
> 100 means that there are 10000000 nanoseconds (or 10 milliseconds) between 
> clock interrupts."
> 
> While the calculation in ppp/ returns the correct result for the numbers as 
> given in the documentation, if I understand the documentation correctly, the 
> same 10 ms tick could also be specified e.g. with 
> CYGNUM_HAL_RTC_NUMERATOR=10000000 and CYGNUM_HAL_RTC_DENOMINATOR=1. The 
> documentation doesn't mention that CYGNUM_HAL_RTC_NUMERATOR has always to be 
> 1,000,000,000.
>

Yes. The PPP stack, like the rest of the BSD stack, tends to assume
that clock ticks run at 100Hz. So a calculation of this sort is
generally acceptable.


> The version I implemented takes both, NUMERATOR and DENOMINATOR into account. 
> The 64 bit of cyg_tick_count_t shouldn't overflow too fast.
> 
> int gettimeofday(struct timeval* tv, struct timezone* tz)
> {
>    cyg_tick_count_t time_us = cyg_current_time() * 
> (CYGNUM_HAL_RTC_NUMERATOR/CYGNUM_HAL_RTC_DENOMINATOR/1000);
>    tv->tv_sec =  time_us / 1000000;
>    tv->tv_usec = time_us % 1000000;
>    return 0;
> }
> 
> So, let me know what you think and I'll prepare a complete patch.


An alternative implementation that I have used is:

int gettimeofday(struct timeval *tv, struct timezone *tz)
{
    int ticks_per_second = 1000000000/(CYGNUM_HAL_RTC_NUMERATOR/CYGNUM_HAL_RTC_DENOMINATOR);    
    cyg_tick_count_t cur_time = cyg_current_time();
    int tix;
    tv->tv_sec = cur_time / ticks_per_second;
    tix = cur_time % ticks_per_second;
    tv->tv_usec = (tix * 1000000)/ticks_per_second;
    return 0;
}

-- 
Nick Garnett                                eCos Kernel Architect
http://www.ecoscentric.com           The eCos and RedBoot experts
>>>> Visit us at stand 230 at The Embedded Systems Show 2005 <<<<
>>>> Oct 19-20 NEC, Birmingham, UK http://www.embedded.co.uk <<<<


-- 
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]