linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Remko van der Vossen <remko.van.der.vossen@cmg.nl>
To: "'linux-kernel@vger.kernel.org'" <linux-kernel@vger.kernel.org>
Subject: Re: RTC !!
Date: Thu, 12 Apr 2001 14:33:50 +0200	[thread overview]
Message-ID: <B569A4D2254ED2119FE500104BC1D5CD01294156@NL-EIN-MAIL01> (raw)

Hi Guys,

Niraj wrote:
> The RTC interrupt  is programmable from 2 Hz to 8192 Hz, in 
> powers of 2. So the interrupts that you could get are one
> of the following:      0.122ms, .244ms, .488ms, .977ms,
> 1.953ms, 3.906ms, 7.813ms, and so on.    Is there any  
> workaround , so that i can use RTC for meeting my
> requirement of an interrupt every 1.666..ms!!  
> ( I know that i can use UTIME or #define HZ 600, but i want
> to know if i can use RTC for this purpose )

It's pretty simple actually, the finest granularity of the 
RTC interrupt is 0.122 ms, so if you want to use a 600 Hz 
timer you'd just have to see if that fits...
1.666/0.122 = 13.656 so it doesn't fit, the best you can do 
is 1000/(0.122*14) = 585.48 Hz you could implement this 
pretty simple, just make a counter, and increment this 
counter every time you receive an interrupt, afther 
incrementing the counter you simply check to see if it is 14, 
if so reset it to 0 and call the real interrupt handler... 
That's the best you can do with a granularity of 0.122 ms, as 
simple as that...
Ofcourse this timer would operate at an increasing time 
difference if you really need a 600Hz timer so it wouldn't be 
much good...
The thing you could do is use a timer operating at a 
frequency of half the allowed tolerance, ie in you case that 
would be a timer of max 0.4ms (half the frequency is twice 
the period) so we'd have to use the 0.244 timer here. Now 
every time you get this interrupt you request the actual time 
from the RTC and if this time is withing half the duration of 
your period (0.122 ms in this case) from your targetted time 
then you call the actual interrupt... You could optimize this 
just a bit by adding a counter so you don't have to call on 
the RTC at every interrupt, I think it'd be a bit to costly...

So, after this whole probably terribly confusing story I 
think I'll give a scenario...

here's how it should go:

inttime expect  diff     action
t0.000  t0.000  t 0.000  ==> interrupt call
t0.244  t1.666  t-1.422
t0.488  t1.666  t-1.178
t0.732  t1.666  t-0.934
t0.976  t1.666  t-0.690
t1.220  t1.666  t-0.446
t1.464  t1.666  t-0.202
t1.708  t1.666  t 0.042  ==> interrupt call
t1.952  t3.333  t-1.381
t2.196  t3.333  t-1.137
t2.440  t3.333  t-0.893
t2.684  t3.333  t-0.649
t2.928  t3.333  t-0.405
t3.172  t3.333  t-0.161
t3.416  t3.333  t 0.083  ==> interrupt call
t3.660  t5.000  t-1.340
t3.904  t5.000  t-1.096
t4.148  t5.000  t-0.852
t4.392  t5.000  t-0.608
t4.636  t5.000  t-0.364
t4.880  t5.000  t-0.120  ==> interrupt call
t5.124  t6.666  t-1.542

you can see that the difference at the point of interrupt 
trigger slightly increases every interrupt, at a certain 
point this difference will be more than 0.122 ms and then the 
interrupt before that one will be actually triggering the 
interrupt as that one is then closer to the targetted 
interrupt time, this way you are ensured that the maximum 
difference between interrupt trigger time and actual time is 
no more than 0.122 ms. The drawback in a straigtforward 
implementation of this is that the RTC is accessed every time 
an interrupt occurs, this can be optimized by adding a timer. 
I'll demonstrate in the following (pseudo) code:


assumptions:
  -gettime returns the current time in microseconds
  -there are bound te be errors in this code as I wrote it off
   the top of my head

typedef void (*tcb)( void );  //Timer callback function prototype

int32 ttime; //targetted time  (microseconds)
int32 tfreq; //timer freq      (microseconds)
int32 ttol;  //timer tolerance (microseconds)
int32 httf;  //half timer tick frequency (microseconds)
int32 mtick; //minimum RTC ticks to next timer event
int32 etick; //elapsed RTC ticks since last timer event
tcb   tmrcb; //Timer callback function

void rtcinthandler( void ) {
  //increase etick and check if a possible timer event should
  //be investigated
  if (++etick > mtick) {
    int32 ctime = gettime();
    //check for a timer event
    if ((ctime - ttime + httf) < (httf * 2)) {
      //We've got a winner, reinit variable and call
      //callback handler
      etick = 0;
      //We recalculate ttime to prevent increasing rounding errors
      ttime = (ctime * tfreq / 1000000 + 1) * 1000000 / tfreq;
      tmrcb();
    }
  }
}

void inittimer(tcb acallback, int32 atfreq, int32 attol) {
  int32 tempval;
  int32 prevttime; //previous targetted time
  int32 ctime = gettime(); //current time

  /*todo: disable the timer if it was already running*/
  tfreq = atfreq;
  ttol = attol;
  tmrcb = acallback;
  //this is probably a very difficult way to calculate the
  //ttf, but at the moment I can't come up with anything
  //better
  httf = 0;
  tempval = ttol / 112
  while (tempval / 2)
    httf++;
  //calculate the targetted time of the next tick..
  //the next statements may seem weird but if we require
  //the integer rounding to get the right times...
  prevttime = ctime * tfreq / 1000000 * 1000000 / tfreq;
  ttime = (ctime * tfreq / 1000000 + 1) * 1000000 / tfreq;
  //calculate the elapsed ticks since last ttime
  etick = (ctime - prevttime + httf) / (httf * 2);
  /*todo: start the actual timer*/
}

Bye,

Remko van der Vossen
CMG Eindhoven
Remko.van.der.Vossen@cmg.nl

             reply	other threads:[~2001-04-12 12:31 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-04-12 12:33 Remko van der Vossen [this message]
  -- strict thread matches above, loose matches on Subject: below --
2001-04-21 10:49 RTC ! npunmia
2001-04-20 11:18 npunmia
2001-04-20 12:38 ` Jesper Juhl
2001-04-12 13:05 RTC !! Remko van der Vossen
2001-04-12  9:20 npunmia
2001-04-12 21:39 ` David Woodhouse

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=B569A4D2254ED2119FE500104BC1D5CD01294156@NL-EIN-MAIL01 \
    --to=remko.van.der.vossen@cmg.nl \
    --cc=linux-kernel@vger.kernel.org \
    /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 NNTP newsgroup(s).