Wait for specific amount of time

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
Nessphoro
Member
Member
Posts: 308
Joined: Sat Apr 30, 2011 12:50 am

Wait for specific amount of time

Post by Nessphoro »

Hello again,

In my wait function I want to wait for a specific amount of ticks.
void timer_wait(unsigned int t)
{
unsigned long long eticks=timer_ticks+t;
while(eticks>timer_ticks);
}

So I call this function and pass in lets say 18 (I didn't change the default PIC frequency)

As I see the IRQ is working and it increments timer_ticks

But nothing happens in wait. Kernel just gets stuck in an infinite lock.

So I tried to change the code to this to see if while works
void timer_wait(unsigned int t)
{
unsigned long long eticks=timer_ticks+t;
while(eticks>timer_ticks)
{
printf("Tick\n");
}
}
Okay I add this and it magically starts to work. I change it back again, and it doesn't.

Any thought on what might be wrong?
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Wait for specific amount of time

Post by Brendan »

Hi,
Nessphoro wrote:Okay I add this and it magically starts to work. I change it back again, and it doesn't.

Any thought on what might be wrong?
Did you declare "timer_ticks" as volatile? If you didn't the compiler probably caches "timer_ticks" in a register.


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
User avatar
Nessphoro
Member
Member
Posts: 308
Joined: Sat Apr 30, 2011 12:50 am

Re: Wait for specific amount of time

Post by Nessphoro »

You're right sir, I didn't.

It works now.

Thanks
Post Reply