[ntp:questions] Reading the RTC

Spoon root at localhost.invalid
Tue Sep 25 16:25:16 UTC 2007


Hello,

Consider an x86 PC running a recent Linux 2.6.22 kernel.

I'm using the standard real time clock driver through /dev/rtc

http://lxr.linux.no/source/drivers/char/rtc.c

I've written two small programs: systohw and readrtc

systohw reads the current system time, then sleeps until the next whole
second, then writes that date to the RTC.

readrtc sets up the RTC to raise an IRQ at 1 Hz, then sits in a loop
waiting for the IRQ, printing the system time every time the OS catches
an IRQ.

I expected the RTC to raise the IRQ every time it counts a whole second,
i.e. when there is no fraction of a second.

However, it seems like the RTC raises the IRQ when the fraction of a
second is 0.5, not when it is 0.

# ./systohw ; echo +++++ ; ./readrtc
1190736224.656947347
tm_sec = 45, tm_min = 3, tm_hour = 16, tm_mday = 25, tm_mon = 8,tm_year
= 107, tm_wday = 2, tm_yday = 267, tm_isdst = 0
1190736224.657174984
1190736225.000003988
1190736225.000089568
+++++
val=464 1190736225.502137106
val=464 1190736226.502125051
val=464 1190736227.502124153
val=464 1190736228.502123868
val=464 1190736229.502123776
val=464 1190736230.502122866

systohw reads the current system time = 1190736224.657174984
then sleeps until 1190736225 (it wakes up at 1190736225.000003988)
then sets the RTC to 1190736225
at which point the current system time is 1190736225.000089568
(setting the RTC took ~86 µs)

readrtc configures the RTC to raise an IRQ once every second (??)
=> ioctl(fd, RTC_UIE_ON, 0);
then waits for IRQs which come in at
1190736225.502137106
1190736226.502125051
1190736227.502124153

(There's a small uncorrected frequency offset on the system clock, and
probably one too on the RTC.)

Unless I missed something, the RTC raises the IRQ when the fraction of a
second is 0.5 NOT when it is 0.

Or is something perhaps delaying the IRQ by 500 ms every time?

In other words, when I receive the IRQ, and the RTC tells me the date,
it is, in fact, 500 ms later than this date?

Can somebody please clear my confusion?

Regards.




More information about the questions mailing list