After trying many things to get my I2C RTC working on a Orange Pi PC plus with a builtin RTC, I've managed (a nasty way) to have it working.
- Find out the path to the built-in rtc:
#>find /sys | egrep 'rtc$'
/sys/bus/platform/drivers/sun6i-rtc/1f00000.rtc
You'll probably get many lines, just focus on the ones with "/sys/bus..."
- Create a udev to manage the creation of the device and device links
#>nano /etc/udev/rules.d/99-rtc1.rules
Paste this content:
KERNEL=="rtc1", SUBSYSTEM=="rtc", DRIVER=="", ATTR{name}=="rtc-ds1307 0-0068", ATTR{hctosys}=="0", SYMLINK+="rtc", SYMLINK+="rtc0", MODE="0666"
- Now, you have to create a systemd service:
#>nano /etc/systemd/system/rtc.service
The below content has to be edited according to your rtc0 built-in and rtc1 i2c devices on the line ExecStart
:
[Unit]
Description=Initialize ds1307 RTC and sincronize system clock
DefaultDependencies=no
Requires=systemd-modules-load.service
After=systemd-modules-load.service
#Before=sysvinit.target
ConditionPathExists=/sys/class/i2c-adapter
#Conflicts=shutdown.target
[Service]
Type=oneshot
RemainAfterExit=yes
User=root
WorkingDirectory=/root
ExecStart=/bin/sh -c "echo -n "1f00000.rtc" > /sys/bus/platform/drivers/sun6i-rtc/unbind && echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-0/new_device && hwclock --rtc /dev/rtc --hctosys --utc"
[Install]
WantedBy=multi-user.target
- Enable the service:
#>systemctl enable rtc.service
- Start the service:
#>systemctl start rtc.service
- You may check if the
/dev/rtc*
devices have changed accordingly using ls -lah /dev/rtc*
you should have something like this:
lrwxrwxrwx 1 root root 4 Apr 3 18:19 /dev/rtc -> rtc1
lrwxrwxrwx 1 root root 4 Apr 3 18:19 /dev/rtc0 -> rtc1
crw-rw-rw- 1 root root 253, 1 Apr 3 18:19 /dev/rtc1
Now rtc and rtc0 are pointing to rtc1!
- Check if hwclock is working with
hwclock --verbose
hwclock from util-linux 2.37.2
System Time: 1680548612.161304
Trying to open: /dev/rtc0
Using the rtc interface to the clock.
Last drift adjustment done at 1680540088 seconds after 1969
Last calibration done at 1680540088 seconds after 1969
Hardware clock is on UTC time
Assuming hardware clock is kept in UTC time.
Waiting for clock tick...
ioctl(4, RTC_UIE_ON, 0): Invalid argument
Waiting in loop for time from /dev/rtc0 to change
...got clock tick
Time read from Hardware Clock: 2023/04/03 19:03:33
Hw clock time : 2023/04/03 19:03:33 = 1680548613 seconds since 1969
Time since last adjustment is 8525 seconds
Calculated Hardware Clock drift is 0.000000 seconds
2023-04-03 19:03:32.766066+00:00
To update your clock with system time just use hwclock -w
to update from rtc use hwclock -s
You must add your module to the /etc/modules
echo "rtc-ds1307" >> /etc/modules