Using the Real-Time Clock

RPi900 includes a PCF8523 real-time clock chip, connected to the I²C bus of the Raspberry Pi. The chip maintains system time in the absence of a network connection and, with backup battery, in the absence of power.

Using the Clock

The Raspberry Pi Arch Linux ARM distribution incorporates a driver module for the PCF8523 chip. The rpi900-rtc package enables the driver, causing the chip to automatically load as the rtc0 device. System time will also synchronised from the chip automatically during the boot sequence. Build and install the package as follows:

$cd packages/rpi900-rtc
$makepkg --clean
$sudo pacman -U rpi900-rtc-1.0-1-armv6h.pkg.tar.xz

Then reboot to load the /dev/rtc0 clock device. Initially you will need to set time on the real-time clock from system time via NTP:

$sudo ntpd -q             # make sure system clock is set from network time
$sudo hwclock --systohc   # set rtc from the system clock
$sudo hwclock --show      # check rtc was set
Sat May 10 17:36:50 2014  -0.671045 seconds

Along with the hwclock command, you can also read the hardware clock using its sysfs attributes, although results are in UTC:

$cat /sys/class/rtc/rtc0/date
2013-06-27
$cat /sys/class/rtc/rtc0/time
23:58:12
$cat /sys/class/rtc/rtc0/since_epoch
1372377510

Compensating for Clock Drift

Like any other electronic clock, the RPi900 real-time clock is subject to drift. The RPi900 clock crystal tolerance is specified as ±20 ppm, corresponding to a drift of up to 1.7 seconds per day. Other factors such as temperature and aging also affect drift.

Linux can compensate for systematic clock drift. One simple way is to issue a second hwclock --systohc command after several weeks. (You’ll need network access for the correct time while you do this.) As described in man hwclock, this command compares system time to clock time and records an estimate of drift. Subsequent boots will incorporate this estimate when reading from the clock.

The PCF8523 also has an on-board offset register for drift compensation. Although this feature is not currently implemented in the driver, you could set the value yourself. Consult the datasheet (page 28) for information on the register, then use i2c-tools to manually set the value according to your measurements.

With or without a network connection, the Network Time Protocol daemon (ntpd) will manage your system time in the best way, using the drift calibration you have set up.