Page 1 of 1
Help with timer
Posted: Mon Mar 21, 2005 12:00 am
by Tjaalie
Hello,
For my OS i made i timer with a wait function but its hangs.
The code for the function is like this:
Code: Select all
void TimerWait(int ticks)
{
//Vars
int WaitTime = Timer + ticks;
//Kijk of we niet te ver gaan
if (WaitTime >= 1000)
{
WaitTime -= 1000;
}
//Wait
while (Timer < WaitTime);
}
I dont know why it hangs its just so n00b but can anybody tell me why?[/code]
Re: Help with timer
Posted: Mon Mar 21, 2005 12:00 am
by rexlunae
Tjaalie wrote:I dont know why it hangs its just so n00b but can anybody tell me why?
It probably hangs because of that loop at the end. The code you gave us does not contain anything that causes that loop to end. What is Time? Is it a global variable updated on an interrupt?
Re: Help with timer
Posted: Mon Mar 21, 2005 12:00 am
by Tjaalie
The timer variable works like:
Code: Select all
void HandleTimer(struct regs* r)
{
Timer++;
if (Timer >= 1000)
{
Timer = 0;
}
}
So can you tell me whats wrong with the loop?
The i know the HanleTimer function get called cause when i put an print function in it it prints the text many times so its works. What do i do wrong?
Re: Help with timer
Posted: Tue Mar 22, 2005 12:00 am
by Anton
Tjaalie wrote:
So can you tell me whats wrong with the loop?
Do you have optimization turned on? If you do turn it off.
Also make the variable Timer volatile
Tjaalie wrote:
The i know the HanleTimer function get called cause when i put an print function in it it prints the text many times so its works.
This is wrong, since if you remove an print function, the compiler might optimize out the code.
Anton.
Re: Help with timer
Posted: Tue Mar 22, 2005 12:00 am
by rexlunae
Anton wrote:Do you have optimization turned on? If you do turn it off.
Also make the variable Timer volatile
All that should be necessary is declaring it volatile. Turning off optimizations should not be necessary. I wonder what the interrupt flag is set to when he enters the loop though. If it is not allowing interrupts, the Timer variable won't get changed.
Re: Help with timer
Posted: Tue Mar 22, 2005 12:00 am
by Tjaalie
thanx i only had to volatile it but what does
volatile do. can any body explain that why its working
now and not without it?
Re: Help with timer
Posted: Tue Mar 22, 2005 12:00 am
by bregma
Tjaalie wrote:thanx i only had to volatile it but what does
volatile do. can any body explain that why its working
now and not without it?
"volatile" effectively tells the compiler that the memory location may be changed without the compiler's knowledge, so it won't try to use a cached value in a register but instead always retrieve it from memory.
--
smw
Re: Help with timer
Posted: Tue Mar 22, 2005 12:00 am
by Legend
Or more general perhaps, it protectes the variable from optimizations.
Re: Help with timer
Posted: Sat Mar 26, 2005 12:00 am
by jcmatias
How do you use this code?
Is this a ISR ?
If is the case , remember that the ISR return is IRET.
Re: Help with timer
Posted: Mon Mar 28, 2005 12:00 am
by DruG5t0r3
That Timer function is far from being reliable. Lets say you have a 1mhz CPU and another one with 2 billion teraflops....(OK i'm exagerating)... Which one will go out of the loop first?...
I recommend to rely on IRQ 0 , I believe it's the only reliable timer you have on a x86 system.
Re: Help with timer
Posted: Wed Mar 30, 2005 12:00 am
by Tjaalie
this timer uses the irq 0