RTC in RPi3: set system time by kernel (PCF8523 and similar)

Guides written by the community, for the community, and only guides!

RTC in RPi3: set system time by kernel (PCF8523 and similar)

Postby alexeicolin » Thu Jul 13, 2017 4:57 pm

This quick guide is for setting up an external I2C RTC (such as PCF8523 https://www.adafruit.com/product/3386) with Rasbperry Pi 3.

Preferably, the system time should be set by the kernel during early boot, not by the userspace (udev/systemd), as some tutorials propose (hence I'm posting this one). In particular, it should be set before the file systems are mounted (if not, then a file system check may be wrongly triggered on every boot due to superblock timestamp being in the future).

1. Rebuild the kernel with the HCTOSYS and the RTC driver as built-in (not as module, because HCTOSYS does not work with modules), substitute the driver for your RTC model instead of the last line:
$this->bbcode_second_pass_code('', 'CONFIG_RTC_HCTOSYS=y
CONFIG_RTC_HCTOSYS_DEVICE="rtc0"
CONFIG_RTC_DRV_PCF8523=y
')

2. add the RTC device to DTB (decompile, edit, compile):
$this->bbcode_second_pass_code('', '
pacman -S dtc # or use linux-4.12/scripts/dtc/dtc
dtc -I dtb -O dts /boot/dtbs/broadcom/bcm2837-rpi-3-b.dtb > /boot/dtbs/broadcom/bcm2837-rpi-3-b.dts
')
Edit bcm2837-rpi-3-b.dts to add rtc node as a child within i2c@7e804000 node, substitute your RTC's driver name in the "compatible" line and its I2C address in the reg line and after the '@' in the node name:
$this->bbcode_second_pass_code('', '
i2c@7e904000 {
....
rtc: rtc@68 {
compatible = "pcf8523";
#address-cells = <1>;
#size-cells = <0>;
reg = <0x68>;
};
}
')
$this->bbcode_second_pass_code('', '
dtc -I dts -O dtb /boot/dtbs/broadcom/bcm2837-rpi-3-b.dts > /boot/dtbs/broadcom/bcm2837-rpi-3-b.dtb
')

Now, time is set from the RTC by the kernel early during boot without any userspace stuff (like systemd, udev) at all: dmesg | grep -i rtc
$this->bbcode_second_pass_code('', '
[ 2.926635] rtc-pcf8523 1-0068: rtc core: registered rtc-pcf8523 as rtc0
[ 3.525986] rtc-pcf8523 1-0068: setting system clock to 2017-07-13 16:10:35 UTC (1499962235)')

Troubleshooting:

If hwclock fails with 'Invalid argument', try 'hwclock --noadjfile --utc ...'
It should succeed and it will also print a message about what args to give it to have the /tmp/adjtime file created -- once created, the Invalid argument went away for me.
alexeicolin
 
Posts: 4
Joined: Mon Dec 29, 2014 6:26 am

Return to Community Guides

Who is online

Users browsing this forum: No registered users and 3 guests