128 lines
4.1 KiB
Markdown
128 lines
4.1 KiB
Markdown
# 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
|
|
|
|
- [https://learn.adafruit.com/adding-a-real-time-clock-to-raspberry-pi?view=all](https://learn.adafruit.com/adding-a-real-time-clock-to-raspberry-pi?view=all)
|
|
- [https://www.raspberrypi.org/forums/viewtopic.php?t=20619](https://www.raspberrypi.org/forums/viewtopic.php?t=20619)
|
|
- [https://www.raspberrypi-spy.co.uk/2015/05/adding-a-ds3231-real-time-clock-to-the-raspberry-pi/](https://www.raspberrypi-spy.co.uk/2015/05/adding-a-ds3231-real-time-clock-to-the-raspberry-pi/)
|
|
- [https://unix.stackexchange.com/questions/246605/how-can-the-link-target-of-dev-rtc-be-changed](https://unix.stackexchange.com/questions/246605/how-can-the-link-target-of-dev-rtc-be-changed)
|
|
- [https://www.raspberrypi.org/forums/viewtopic.php?t=85683](https://www.raspberrypi.org/forums/viewtopic.php?t=85683)
|
|
|
|
## 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
|
|
|
|
- [DS1307 (most common | Link)](https://www.adafruit.com/product/3296)
|
|
- [PCF8523 (cheapest | Link)](https://www.adafruit.com/product/3295)
|
|
- [DS3231 (most accurate | Link](https://www.adafruit.com/product/3013)
|
|
|
|
### Key Note
|
|
|
|
This guide assumes the PCF8523. You'll need to adjust lightly for the DS3231 or DS1307.
|
|
|
|
## Initial Setup
|
|
|
|
``` bash
|
|
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
|
|
|
|
``` bash
|
|
# 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
|
|
|
|
``` bash
|
|
|
|
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
|
|
|
|
``` bash
|
|
|
|
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
|
|
|
|
``` bash
|
|
|
|
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
|
|
|
|
```
|