This is the mail archive of the c++-embedded@sourceware.cygnus.com mailing list .


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

timer idioms in embedded system


I'm trying to come up with a good timer idiom that doesn't use much
interrupt time.

My current code (single-threaded, no RTOS) uses an array of words to
represent global timers.  The timer interrupt decrements any non-zero
timers. Code that wants to use a timer sets it to a non-zero tick count
and then waits for it to decrement to zero with a simple "while (timer)
;".

I'd like to change this to use a single counter incremented by the
interrupt. Client code would wait for the counter to increment to a
desired value. Example wait code might be

 unsigned long expire_time = clock() + delay;
 while (clock() < expire_time) /* wait */;

How should I handle rollover? I expect the counter to be 32-bit, and the
interrupt tick to be 1 millisecond. The rollover should occur about
every 46 days.

It seems like I could declare the 32-bit values as signed and do
something like

 long clock();
 long expire_time = clock() + delay;
 while ((clock() - expire_time) < 0) /* wait */;

Is this reasonable?

-- 
Ken
mailto:shiva@well.com
mailto:shiva@CompuServe.COM
http://www.well.com/user/shiva/
http://sewnsurf.home.ml.org/
http://www.e-scrub.com/cgi-bin/wpoison/wpoison.cgi (Death to Spam!)






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