RTEMS: Counting time, again... (help)
sashti srinivasan
svasn_rtems at yahoo.com
Sat Mar 27 09:21:36 UTC 2004
Hello Alex,
If your BSP is PC386,..., here is the source code I
use for my purpose. It takes a timeval structure(with
resolution of CONFIGURE_MICROSECONDS_PER_TICK)
obtained from gettimeofday and updates it by reading
the hardware register. The macros used here are
defined in <bsp.h> and you have to include that. If
you find it incorrect or has bugs, please let me know.
Note: The routine HardwareUpdate(), is accurate upto
1 Microsecond. Also note that this routine assumes
Microseconds per tick is set at 10 milliseconds. If
it is different, change the source accordingly.
int globalAddress;
char globalValue;
struct timeval tempTime;
struct timezone noUse;
/* Routine to write a byte to PC IO port
* CS02M040 Srinivasan, IIT(M)
*/
void WriteByte(int address,char value)
{
globalAddress=address;
globalValue=value;
asm("movl globalAddress,%edx");
asm("movb globalValue,%al");
asm("outb %al,%dx");
}
/* Routine to read a byte from PC IO port
* CS02M040 Srinivasan, IIT(M)
*/
char ReadByte(int address)
{
char value;
globalAddress=address;
asm("movl globalAddress,%edx");
asm("movb %dl,globalValue");
asm("inb %dx,%al");
asm("movb %al,globalValue");
value=globalValue;
return value;
}
/* Routine that returns time with 1 microsecond
* accuracy(On PC hardware). This routine calls
* gettimeofday again.
* CS02M040 S Srinivasan, IIT(M)
*/
void HardwareUpdate(struct timeval *inp)
{
unsigned long currentValue;
unsigned char lsb,msb;
unsigned long nextValue;
currentValue=inp->tv_usec;
WriteByte(TIMER_MODE,TIMER_SEL0|TIMER_LATCH);
lsb=ReadByte(TIMER_CNTR0);
msb=ReadByte(TIMER_CNTR0);
gettimeofday(&tempTime,&noUse);
if(tempTime.tv_usec!=inp->tv_usec)
{
*inp=tempTime;
currentValue=inp->tv_usec;
WriteByte(TIMER_MODE,TIMER_SEL0|TIMER_LATCH);
lsb=ReadByte(TIMER_CNTR0);
msb=ReadByte(TIMER_CNTR0);
}
nextValue=((unsigned long)lsb) + ((unsigned
long)msb)*256;
nextValue=TICK_TO_US(nextValue);
nextValue=10000-nextValue;
if(nextValue>=10005)
{
printf("Error in low level time functions %d
\n",nextValue);
exit(0);
}
if(nextValue+currentValue>=1000000)
{
nextValue=(nextValue+currentValue)%1000000;
inp->tv_sec++;
}
else
{
nextValue=nextValue+currentValue;
}
inp->tv_usec=nextValue;
}
Regards
Srinivasan
--- Alex <kbyte at iol.pt> wrote:
> Hi,
>
> I know that will be some users that will say to me
> that this doubt is already explain in the mailling
> list, but before sent this mail I search in the
> mailling list and I saw nothings...
>
> Weel, I need to measure time in microseconds (or
> nanoseconds, this will be great). Using the posix
> calls the maximum resolution I get is 20
> milliseconds. That doesnt feet my needs... :-(
>
> So, I thinking to use an osciloscope or some device
> like that to measure those times.
> Another idea is to access the hardware ticks.
>
> Which is the best way to measure those times?
> External counter device or access hardware ticker?
>
> Is there any free source code to access the hardware
> ticks?
>
> Thanks in advance...
>
> BSP: i686
>
> Alex
>
>
>
___________________________________________________________________________________________
> IOL Flash. A net normal em versao acelerada.
> Precisa de uma caixa de correio maior? Com o IOL
> Flash pode ter uma caixa com 21MB de espaco.
> Saiba mais sobre o IOL Flash:
> http://www.iol.pt/central_utilizador/iol_flash/
>
__________________________________
Do you Yahoo!?
Yahoo! Finance Tax Center - File online. File on time.
http://taxes.yahoo.com/filing.html
More information about the users
mailing list