Enable i2c RTC using systemd.
A udev rules triggers an I2C systemd service on I2C kernel module adding.
Service then loads I2C driver adding a new I2C device to /sys
Then kernel loads I2C RTC driver and adds an RTC device
It triggers a udev rules that updates hardware clock
-
enable I2C in config.txt, adding
device_tree_param=i2c1=on
-
enable I2C driver loading at boot, refer
i2c.conf
-
reboot raspberry
-
check an I2C bus is found :
i2cdetect -l i2c-1 unknown bcm2835 I2C adapter N/A
-
check I2C rtc is detected :
i2cdetect -y 1 0 1 2 3 4 5 6 7 8 9 a b c d e f 00: -- -- -- -- -- -- -- -- -- -- -- -- -- 10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- -- 70: -- -- -- -- -- -- -- --
-
create
/etc/udev/rules.d/50-i2c_start.rules
-
create
/etc/conf.d/rtc-i2c
-
create
/etc/systemd/system/i2c_rtc.service
-
create
/etc/udev/rules.d//etc/udev/rules.d/60-rtc-i2c.rules
-
test it:
hwclock -r 2000-01-02 01:25:04.301281+01:00
-
when an NTP service is started, it should set hwclock to a valid time, if not you must set a valid system clock and then set hwclock to system clock
hwclock --systohc
-
reboot
-
system clock should be fine, even if system is not connected to outside world
That's a lot of file and rule. Most of it is not needed anymore. You don't have to manually load module and and set the i2c thing
Here is my setup.
I have a mcp7941x chip for the RTC.
The dtoverlay stuff takes care of module loading and configure the i2c stuff too. We only need to load i2c-dev.
Then the udev rule trigger a systemd service that calls hwclock. That's it.
Note: I use a systemd service because it seems udev has not the right to access
/dev/rtc
and then hwclock fails ?? Needs to be confirmed.