This is the mail archive of the ecos-discuss@sources.redhat.com 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]

Re: Functions to consume CPU time in x86


Cristiano Ligieri Pereira <cpereira@ics.uci.edu> writes:

> And of course I want to be able to specify that I want to keep the CPU
> busy for specific X (mili/nano)seconds and not only keep it busy for some
> randon amount of time.
> 
> I looking for functions like this.
> 
> consume10ns() or consume1ns()
> 
> or consume1ms() or consume10ms()
> 
> Thanks again,
> 

Nanosecond delays are a bit difficult since you have to do it with
calibrated loops or use CPU specific cycle counters. Microsecond
delays are easier since you can use the timer. Here is some code that
does that:

void 
hal_delay_us(int us)
{
    while( us > 0 )
    {
        cyg_uint32 us1 = us;
        cyg_int32 ticks;
        cyg_uint32 cval1, cval2;

        // Wait in bursts of 1s to avoid overflow problems with the
        // multiply by 1000 below.
        
        if( us1 > 1000000 )
            us1 = 1000000;

        us -= us1;
        
        // The PC clock ticks at 838ns per tick. So we convert the us
        // value we were given to clock ticks and wait for that many
        // to pass.

        ticks = (us1 * 1000UL) / 838UL;

        HAL_CLOCK_READ( &cval1 );

        // We just loop, waiting for clock ticks to happen,
        // and subtracting them from ticks when they do.
        
        while( ticks > 0 )
        {
            cyg_int32 diff;
            HAL_CLOCK_READ( &cval2 );

            diff = cval2 - cval1;

            // Cope with counter wrap-around.
            if( diff < 0 )
                diff += CYGNUM_HAL_RTC_PERIOD;

            ticks -= diff;
            cval1 = cval2;

        }
    }
}


-- 
Nick Garnett, eCos Kernel Architect
Red Hat, Cambridge, UK


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