[patch] Re: Clock setting for SPARC/LEON
Aaron J. Grier
aaron at frye.com
Thu Aug 19 21:33:38 UTC 2004
On Thu, Aug 19, 2004 at 04:30:47PM -0400, Jerry Needell wrote:
> Also can anyone show me where the code actually gets the frequency
> information. I cannot find a place where
> BSP_Configuration.microseconds_per_tick is actually initialized.
>
> I am using a Gaisler Research GR-PCI-XC2V development board.
I've run into a similar issue with my 68k system. the tick rate is
8MHz/(13*3*8), very close to 10ms, but I couldn't depend on it for
accurate time-keeping.
attached is a patch against 4.5.0; I assume it could be ported to newer
versions without a much difficulty.
--
Aaron J. Grier | Frye Electronics, Tigard, OR | aaron at frye.com
-------------- next part --------------
Index: 0.31/c/src/exec/score/macros/rtems/score/tod.inl
--- 0.31/c/src/exec/score/macros/rtems/score/tod.inl Fri, 29 Sep 2000 20:11:24 -0700 kaben (rtems/L/b/27_tod.inl 1.1 664)
+++ 0.32/c/src/exec/score/macros/rtems/score/tod.inl Sat, 25 May 2002 17:15:34 -0700 aaron (rtems/L/b/27_tod.inl 1.2 664)
@@ -24,7 +24,8 @@
#define _TOD_Tickle_ticks() \
_TOD_Current.ticks++; \
- _Watchdog_Ticks_since_boot++
+ _Watchdog_Ticks_since_boot++; \
+ _TOD_Cumulative_slop += _TOD_Slop_per_tick;
/*PAGE
*
Index: 0.31/c/src/exec/score/inline/rtems/score/tod.inl
--- 0.31/c/src/exec/score/inline/rtems/score/tod.inl Fri, 29 Sep 2000 20:11:24 -0700 kaben (rtems/M/b/3_tod.inl 1.1 664)
+++ 0.32/c/src/exec/score/inline/rtems/score/tod.inl Sat, 25 May 2002 17:15:34 -0700 aaron (rtems/M/b/3_tod.inl 1.2 664)
@@ -30,6 +30,7 @@
{
_TOD_Current.ticks += 1;
_Watchdog_Ticks_since_boot += 1;
+ _TOD_Cumulative_slop += _TOD_Slop_per_tick;
}
/*PAGE
Index: 0.31/c/src/exec/score/include/rtems/score/tod.h
--- 0.31/c/src/exec/score/include/rtems/score/tod.h Fri, 29 Sep 2000 20:11:24 -0700 kaben (rtems/M/b/32_tod.h 1.1 664)
+++ 0.32/c/src/exec/score/include/rtems/score/tod.h Sat, 25 May 2002 17:15:34 -0700 aaron (rtems/M/b/32_tod.h 1.2 664)
@@ -101,9 +101,15 @@
* per clock tick, this value will contain only the integer portion
* of the division. This means that the interval between clock ticks
* can be a source of error in the current time of day.
+ *
+ * To help alleviate this problem, we keep a running error (in
+ * microseconds) and adjust the scheduling of the seconds watchdog
+ * accordingly.
*/
SCORE_EXTERN unsigned32 _TOD_Ticks_per_second;
+SCORE_EXTERN signed32 _TOD_Slop_per_tick;
+SCORE_EXTERN signed32 _TOD_Cumulative_slop;
/*
* This is the control structure for the watchdog timer which
Index: 0.31/c/src/exec/score/src/coretodtickle.c
--- 0.31/c/src/exec/score/src/coretodtickle.c Fri, 29 Sep 2000 20:11:24 -0700 kaben (rtems/O/b/36_coretodtic 1.1 664)
+++ 0.32/c/src/exec/score/src/coretodtickle.c Sat, 25 May 2002 17:15:34 -0700 aaron (rtems/O/b/36_coretodtic 1.2 664)
@@ -62,6 +62,31 @@
}
}
+ /* tickle the seconds timer */
_Watchdog_Tickle_seconds();
- _Watchdog_Insert_ticks( &_TOD_Seconds_watchdog, _TOD_Ticks_per_second );
+
+ /* prepare to schedule the next tick */
+ if (_TOD_Slop_per_tick == 0)
+ _Watchdog_Insert_ticks( &_TOD_Seconds_watchdog, _TOD_Ticks_per_second );
+ else if (_TOD_Slop_per_tick > 0) { /* running fast */
+ if (_TOD_Cumulative_slop > _TOD_Microseconds_per_tick) {
+ _TOD_Cumulative_slop%=_TOD_Microseconds_per_tick;
+ _Watchdog_Insert_ticks(
+ &_TOD_Seconds_watchdog, _TOD_Ticks_per_second + 1
+ );
+ } else _Watchdog_Insert_ticks(
+ &_TOD_Seconds_watchdog, _TOD_Ticks_per_second
+ );
+ } else { /* running slow */
+ if (_TOD_Cumulative_slop < (-1 * _TOD_Microseconds_per_tick)) {
+ /* C doesn't really do modular arithmetic, so we fake it */
+ _TOD_Cumulative_slop%=_TOD_Microseconds_per_tick;
+ _TOD_Cumulative_slop+=_TOD_Microseconds_per_tick;
+ _Watchdog_Insert_ticks(
+ &_TOD_Seconds_watchdog, _TOD_Ticks_per_second - 1
+ );
+ } else _Watchdog_Insert_ticks(
+ &_TOD_Seconds_watchdog, _TOD_Ticks_per_second
+ );
+ }
}
Index: 0.31/c/src/exec/score/src/coretod.c
--- 0.31/c/src/exec/score/src/coretod.c Fri, 29 Sep 2000 20:11:24 -0700 kaben (rtems/O/b/38_coretod.c 1.1 664)
+++ 0.32/c/src/exec/score/src/coretod.c Sat, 25 May 2002 17:15:34 -0700 aaron (rtems/O/b/38_coretod.c 1.2 664)
@@ -48,9 +48,14 @@
if ( microseconds_per_tick == 0 )
_TOD_Ticks_per_second = 0;
- else
+ else {
_TOD_Ticks_per_second =
TOD_MICROSECONDS_PER_SECOND / microseconds_per_tick;
+ _TOD_Slop_per_tick =
+ TOD_MICROSECONDS_PER_SECOND / _TOD_Ticks_per_second
+ - microseconds_per_tick;
+ _TOD_Cumulative_slop = 0;
+ }
_Watchdog_Initialize( &_TOD_Seconds_watchdog, _TOD_Tickle, 0, NULL );
More information about the users
mailing list