OSDev.org

The Place to Start for Operating System Developers
It is currently Tue Aug 20, 2019 2:33 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Wait function
PostPosted: Sat Apr 27, 2019 8:14 am 
Offline
Member
Member

Joined: Mon Jun 04, 2018 8:10 am
Posts: 44
Hi, I would like to write a wait function for my OS.
Just a simple one that adds a delay with a loop. I tried like this :

Code:

// library.c

extern void wait_ms(uint32_t ms)
{
   const uint32_t BEG = ms + timer_get_tick();
   while (BEG < timer_get_tick());
}

// timer.c

static volatile uint32_t tick = 0;

extern volatile uint32_t timer_get_tick(void)
{
   return tick;
}



but it does not work, it does not even enter the while loop (I tested with a printf). I think this might be related to GCC optimization (correct me if I am wrong).

How can I change to make this function work?
Regards


Top
 Profile  
 
 Post subject: Re: Wait function
PostPosted: Sat Apr 27, 2019 8:47 am 
Offline
Member
Member

Joined: Fri Aug 26, 2016 1:41 pm
Posts: 466
Your logic is wrong. You set BEG in the future (by taking that original timer_get_tick() value and adding MS to it. Maybe you meant something like:
Code:
   const uint32_t END = ms + timer_get_tick();
   while (timer_get_tick() < END);


Top
 Profile  
 
 Post subject: Re: Wait function
PostPosted: Sat Apr 27, 2019 8:52 am 
Offline
Member
Member

Joined: Mon Jun 04, 2018 8:10 am
Posts: 44
Yes, that is exactly what I meant, now it works :)
I thought this was a compiler optimization problem but my code was just wrong, thank you!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: quadrant and 11 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group