RTEMS - rtems_clock_get_uptime() fails during timer tick

Sebastian Huber sebastian.huber at embedded-brains.de
Thu Feb 24 08:47:02 UTC 2011

Hello Rolf,

this looks like a general problem with the
_Watchdog_Nanoseconds_since_tick_handler() mechanic.  Lets suppose we have a
hardware timer with an automatic reload capability.  It counts from 0 to N,
then goes back to 0, and so on.  It triggers an interrupt during the N to 0
transition.  The _Watchdog_Nanoseconds_since_tick_handler() returns the current
hardware timer ticks mapped to nanoseconds.  Lets have a look at this function.

void _TOD_Get_uptime(
  Timestamp_Control *uptime
  ISR_Level         level;
  Timestamp_Control offset;
  Timestamp_Control up;
  long              nanoseconds;

  /* assume time checked for NULL by caller */

  /* _TOD_Uptime is in native timestamp format */
  _ISR_Disable( level );
    up = _TOD_Uptime;
    nanoseconds = (*_Watchdog_Nanoseconds_since_tick_handler)();
  _ISR_Enable( level );

  _Timestamp_Set( &offset, 0, nanoseconds );
  _Timestamp_Add_to( &up, &offset );
  *uptime = up;

Now consider the following sequence.  We are right before the hardware timer N
to 0 transition.  We disable the interrupts in the above function.  The
hardware timer goes to 0.  Since interrupts are disabled, the _TOD_Uptime is
not up to date.  We call _Watchdog_Nanoseconds_since_tick_handler() and get the
time since the last hardware (!) timer tick.  Now we use the out of date uptime
with the new nanoseconds.

A solution may be to not disable interrupts in _TOD_Get_uptime() (and all
calling contexts) and use something like this:

do {
} while (up0 != up1)

Have a nice day!

Sebastian Huber, embedded brains GmbH

Address : Obere Lagerstr. 30, D-82178 Puchheim, Germany
Phone   : +49 89 18 90 80 79-6
Fax     : +49 89 18 90 80 79-9
E-Mail  : sebastian.huber at embedded-brains.de
PGP     : Public key available on request.

Diese Nachricht ist keine geschäftliche Mitteilung im Sinne des EHUG.

More information about the users mailing list