Jump to content

NAS kit w/ NEO LTS: RTC not surviving reboots


Go to solution Solved by Makda Mujji,

Recommended Posts

Posted

The 1-bay NAS kit v1.2 has an RTC with option to add CR2032 button cell to keep HW time when system is shut down. But running `timedatectl` when device is booted in offline mode shows RTC is set to 1970-01-01 00:00:00 which defeats the purpose of having the option of CR2032. And yes, the button cell have juice; I've bought it a week ago and tested it before putting it in the kit.

Posted (edited)

Allwinner H3 CPU has an in-built RTC which is mounted at /dev/rtc0. This is the reason the RTC doesn't save current time when CPU powers off. The RTC present on the NAS kit (DS1307) needs to be accessed using i2c interface which itself needs to be enabled before doing anything else, and all time sync done with DS1307.

 

Enable i2c (i2c-tools is present in Armbian bullseye by default) by editing /boot/armbianEnv.txt file:

sudo nano /etc/armbianEnv.txt

in the line

 overlays=...

where "..." means there are already some overlays mentioned, append " i2c" (notice a space before i2c) so that the line should look like

overlays=... i2c

Save and close. Next, make sure DS1307 module is present in your kernel (it is present in Armbian bullseye by default)

sudo modinfo rtc-ds1307

If you see some description and a signature, it is present. Load the module on system start up by adding line to /etc/modules:

sudo echo rtc-ds1307 >> /etc/modules

 Run the following command:

sudo i2cdetect -y 0

If you see something like this:

     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: -- -- -- -- -- -- -- --

then you can see rtc1 in /dev:

sudo ls -al /dev/ | grep rtc

You need to initialize rtc1 (one time):

sudo hwclock --rtc /dev/rtc1 --systohc

Create a new file called rtc_ds1307 and add following text (copied from here) by following command:

sudo cat <<EOF > rtc && sudo mv rtc /etc/init.d/rtc_ds1307 && sudo chmod +x /etc/init.d/rtc_1307
#! /bin/sh
### BEGIN INIT INFO
# Provides: rtc_ds1307
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: DS1307 real-time clock usage script
# Description: This file should be used to construct scripts to be
# placed in /etc/init.d.
### END INIT INFO
# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="ds1307_rtc maintenance service"
do_start() {
	echo "Selecting correct RTC instance"
    	echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-0/new_device
    	sudo ln -f -s /dev/rtc1 /dev/rtc
	echo "Syncing system time to RTC"
    	#You need to sync from rtc1 instead of default (rtc0)
	sudo hwclock -s -f /dev/rtc1
}
do_stop() {
	echo "Syncing RTC to system time"
	sudo hwclock -w
} 
case "\$1" in 
	start) 
		do_start 
		;; 
	stop) 
		do_stop
		;; 
	status) 
		echo "RTC time:" hwclock -r 
		echo "System time:" date
		;; 
	restart|force-reload)
		do_stop 
		;; 
	*) 
echo "Usage: rtc_ds1307 {start|stop|status|restart}" >&2 exit 3 
;; 
esac
EOF

Update rc.d then reboot:

sudo update-rc.d rtc_ds1307 defaults
sudo reboot now

Check your system time by following command:

timedatectl

 

Edited by Makda Mujji
Added code blocks
Posted

Few changes to above answer:

Edit armbianEnv.txt by

sudo nano /boot/armbianEnv.txt

And append i2c0 instead of i2c

overlays=... i2c0

install i2c-tools

sudo apt install i2c-tools

If your "sudo i2cdetect -y 0" returns 68, run the following commands first:

Load rtc-ds1307 by

sudo modprobe rtc-ds1307
sudo echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-0/new_device

 

  • Solution
Posted (edited)

Starting from Trixie, some paths have changed. Here is the complete walkthrough for the process:

 

Install necessary tools (i2c-tools and hwclock are not present by default in Armbian trixie):

sudo apt install i2c-tools util-linux-extra

Edit armbianEnv.txt:

sudo nano /boot/armbianEnv.txt

Append i2c0 to the line that says overlays=...:

overlays=... i2c0

("..." denotes some overlays already written, do not delete them).

Reboot (compulsory to load i2c module):

sudo reboot now

Inquire availability of module rtc-ds1307 in the kernel (present in trixie):

sudo modinfo rtc-ds1307

If it returns some description, the module is present. If not, then install its driver from GitHub.

Open modules file:

sudo nano /etc/modules

Add line rtc-ds1307 in it. Save and close.

Run the following command:

sudo i2cdetect -y 0

It should return 68 in the grid:

     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: -- -- -- -- -- -- -- -- 

Then add module to start up:

sudo modprobe rtc-ds1307
sudo echo ds1307 0x68 > /sys/class/i2c-dev/i2c-0/device/new_device

The DS1307 should now be available at /dev/rtc1, which you can check:

sudo ls /dev/ | grep rtc

Initialize rtc1 (once)

sudo hwclock --rtc /dev/rtc1 --systohc

Create a new file rtc_ds1307.sh:

sudo nano /usr/local/bin/rtc_ds1307.sh

Add following lines in it:

#!/bin/bash
echo "Creating entry for rtc_1307 service" | systemd-cat -p info
sudo echo ds1307 0x68 > /sys/class/i2c-dev/i2c-0/device/new_device
echo "Done" | systemd-cat -p info
echo "Symlinking /dev/rtc1 to /dev/rtc" | systemd-cat -p info
sudo ln -f -s /dev/rtc1 /dev/rtc
echo "Done"
echo "Syncing RTC time to system time" | systemd-cat -p info
sudo hwclock --hctosys --noadjfile --utc -f /dev/rtc1
echo "Done"

Save and exit. Make this file executable:

sudo chmod +x /usr/local/bin/rtc_ds1307.sh

Create a systemd service to run this script at startup:

sudo nano /etc/systemd/system/rtc_ds1307.service

Add following lines in it:

[Unit]
Description=Synchronize system clock to RTC
Requires=systemd-modules-load.service
After=systemd-modules-load.service
ConditionPathExists=/sys/class/i2c-dev
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/local/bin/rtc_ds1307.sh
[Install]
WantedBy=multi-user.target

Save and exit. Enable the service:

sudo chmod 644 /etc/systemd/system/rtc_ds1307.service
sudo systemctl enable rtc_ds1307.service

Reboot and check system date time by running:

timedatectl

 

Edited by Makda Mujji
Minor typos and corrections.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...

Important Information

Terms of Use - Privacy Policy - Guidelines