public inbox for c++-embedded@sourceware.org
 help / color / mirror / Atom feed
From: Dave Hansen <dave.hansen@btree.com>
To: shiva@compuserve.com, c++-embedded@cygnus.com, shiva@well.com
Subject: Re: timer idioms in embedded system
Date: Fri, 26 Jun 1998 07:13:00 -0000	[thread overview]
Message-ID: <s593648f.047@btree.com> (raw)

If you stay in unsigned integer math, you can pretty much ignore 
rollover.  Signed math may bite you since it's not as well defined.

Change your client code to work something like:

   unsigned long start = clock();
   while ((clock()-start) < delay) wait();

For example, if start==0xFFFFFFF0 and clock() returns 4, the result
of the subtraction will be 4 - 0xFFFFFFF0 or 0x00000014

HTH.  Regards,

     -=Dave

>>> Ken <shiva@compuserve.com> 06/25/98 10:41PM >>>
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!)




                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             !
                                   
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             !
                                   
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      

             reply	other threads:[~1998-06-26  7:13 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-06-26  7:13 Dave Hansen [this message]
  -- strict thread matches above, loose matches on Subject: below --
1998-06-25 21:10 Ken
     [not found] ` <359B0170.D922E2B9@agt.net>
1998-07-03  0:49   ` Ken

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=s593648f.047@btree.com \
    --to=dave.hansen@btree.com \
    --cc=c++-embedded@cygnus.com \
    --cc=shiva@compuserve.com \
    --cc=shiva@well.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).