kemonine
/
lollipopcloud
Archived
1
0
Fork 0
This repository has been archived on 2022-08-05. You can view files and clone it, but cannot push or open issues or pull requests.
lollipopcloud/hardware/rtc.md

4.1 KiB

RTC (Real Time Clock)

Setup a good time source. The built in RTC for Allwinner chips lacks a battery on the Orange Pi boards and has a higher drain than others. Go with the others for the better power use

Inspiration / Further Reading

Clocks

Suggestions

  • Use cheapest for ultra portable builds
  • Use most accurate for mostly permanent builds (RV/similar)
  • Use most common rarely used (unless you have good reason, don't)

Hardware

Key Note

This guide assumes the PCF8523. You'll need to adjust lightly for the DS3231 or DS1307.

Initial Setup

armbian-config # enable i2c busses
systemctl reboot # reboot to enable bus(es)
apt install i2c-tools # install tools
i2cdetect -l # check to ensure busses are present
i2cdetect -y 1 # 0 for orange pi zero ; 1 for recent pi's
modprobe rtc-pcf8523 # load the proper rtc module (adafruit cheap model)
echo pcf8523 0x68 > /sys/class/i2c-adapter/i2c-1/new_device # setup device so it's seen
dmesg | grep rtc # verify the kernel sees the rtc
ls /dev/rtc* # should have rtc1
hwclock -r -f /dev/rtc1 # See if anything is on the rtc
hwclock -f /dev/rtc1 --systohc -D --noadjfile --utc # set / init the rtc
hwclock -r -f /dev/rtc1 # read the value from the rtc
hwclock -w -f /dev/rtc1 # write the current time to the rtc
apt remove --purge fake-hwclock # purge the fake hwclock as we have a real one now
systemctl disable fake-hwclock # Disable any dangling services

Udev Rules

# setup udev rule to setup the new rtc as the primary for the board
# `udevadm info -a -p /sys/class/rtc/rtc1` gets the details for the below entries
cat > /etc/udev/rules.d/99-rtc1.rules <<EOF
KERNEL=="rtc1", SUBSYSTEM=="rtc", DRIVER=="", ATTR{name}=="rtc-pcf8523 0-0068", SYMLINK="rtc", MODE="0666"
EOF

External RTC On Boot

A systemd unit that brings the external RTC online during boot


cat > /etc/systemd/system/rtc1-online.service <<EOF
[Unit]
Description=Setup rtc1 as primary clock
DefaultDependencies=no
Before=time-sync.target

[Service]
Type=oneshot
ExecStart=/bin/sh -c '/sbin/modprobe rtc-pcf8523  \
&& echo pcf8523 0x68 > /sys/class/i2c-adapter/i2c-1/new_device \
&& /sbin/hwclock -s -f /dev/rtc1'

[Install]
WantedBy=time-sync.target
EOF

systemctl daemon-reload
systemctl enable rtc1-online

Set RTC on shutdown

A systemd unit that sets the current time to the rtc on shutdown to minimize clock drift during next boot


cat > /etc/systemd/system/rtc1-shutdown-fix.service <<EOF
[Unit]
Description=Set rtc1 on shutdown
DefaultDependencies=no
Before=shutdown.target reboot.target halt.target

[Service]
Type=oneshot
ExecStart=/sbin/hwclock --systohc -D --noadjfile --utc -f /dev/rtc1

[Install]
WantedBy=halt.target reboot.target shutdown.target
EOF

systemctl daemon-reload
systemctl enable rtc1-shutdown-fix

Misc Notes


date # Wait for this to get sync'd via whatever ntp you're doing
hwclock -r -f /dev/rtc1 # Verify this isn't right
hwclock -w -f /dev/rtc1 # Write ntp system time -> rtc
hwclock -r -f /dev/rtc1 # Verify the write worked
ls -l /dev/rtc* # Check rtc dev node is present
hwclock -r -f /dev/rtc1 # read time from external rtc
hwclock -r -f /dev/rtc0 # read time from internal rtc