[SOLVED] How do I use the DS3231 RTC on NanoPi NEO3 with DietPi 7.0.2?

Hello folks,

I’m converting an NTP server (GPS controled) from a rpi zero with SPI ethernet (10Mb/s) running raspbian lite to a nanopi neo3 with DietPi. I want to use the onboard 1GBps, as that will reduce the local LAN NTP delay from 2msec to 0.5msec from my tests.

The first step of this process is to get the RTC DS3231 (I2C) to work with DietPi.
I was successful in the I2C connection and

sudo i2cdetect -y 0

correctly shows the DS3231 at address 68.

From my searches around the forums I then need to execute the following in order to create the new device:

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

By the way, even with sudo the command gives permission error, I can only run that as root.
After this the new hardware clock is available as it can be tested by:

sudo hwclock -r -f /dev/rtc1

I also remove the fake-clock by using the dietpi-config → RTC hardware.

Questions:

  1. Is this enough to have the DS3231 become the true and only hardware clock of the system?
  2. After reboot this is all lost. On the pi I would edit the rc.local file but that file is not present in my installation and even if I create it and add the following
sudo echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-0/new_device
hwclock -s

it still wouldn’t work (the new I2C device is not created).
So, what is the proper way to run the two command above at boot time on dietpi?

BTW I’m using DietPi v7.0.2.

Thanks a million!!!

Enzo


[SOLVED]

/lib/udev/hwclock-set became:

#!/bin/sh
# Reset the System Clock to UTC if the hardware clock from which it
# was copied by the kernel was in localtime.

dev=$1

if [ -e /run/systemd/system ] ; then
#    exit 0
echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-0/new_device

fi

#if [ -e /run/udev/hwclock-set ]; then
#    exit 0
#fi

.
.
.
HCTOSYS_DEVICE=rtc1

This will attach /dev/rtc1 to the ds3231.

In order to have the system time set from the DS3231 at boot add the following line to /etc/rc.local

sudo hwclock -s -f /dev/rtc1

If your rc.local is not being executed at boot, enable the service:
Check it with: sudo systemctl status rc.local.service
restart it with sudo systemctl restart rc.local.service

Hi,

not sure if this is fitting but there was a similar issues on GitHub. At least it was as well some challenges on RTC https://github.com/MichaIng/DietPi/issues/3393#issuecomment-798903973

G_CONFIG_INJECT 'dtoverlay=i2c-rtc,ds3231' 'dtoverlay=i2c-rtc,ds3231' /boot/config.txt
reboot

Try it with this first, adding the required device tree overlay (if you didn’t do that yet).

If that along does not make it work, try to load the related kernel module, which seems to not happen automatically (anymore):

modprobe rtc-ds1307

If that does help, we found a bug with newest firmware, I seems. But probably that got fixed already with latest master/development branch, at least there is some changelog related to this overlay: https://github.com/raspberrypi/firmware/commit/8717598c7eb443c39c28a4b3a66b4d369ae0a83f

MichaIng
But the NanoPi is not running with RPi kernel, isn’t it?

MichaIng

I don’t have a /boot/config.txt on the NanoPi Neo3. I do have two other files though:

dietpi.txt and armbianEnv.txt

Should I add ‘dtoverlay=i2c-rtc,ds3231’ into one of these files?

Joulinar
Thanks for you github link. very interesting but seems to be related to an rpi 3B, and my current experience is that the nanopi uses a different setup :frowning:

Cheers,

Enzo

Ah sorry, since on GitHub someone was asking for exactly the same RTC on RPi, I missed to have a closer look: https://github.com/MichaIng/DietPi/issues/3393#issuecomment-798903973

Not sure how it works on NEO3. There are some device tree overlays available as well: /boot/dtb/rockchip/overlay/
Check out the readme, additional I2C interfaces are available.

And check out the kernel modules found in /lib/modules/$(uname -r)/kernel/drivers/rtc/ if you find the matching one. rtc-ds1307 is not present there (on the older Linux 5.7 at least), but probably another one matches. modinfo <module_name> can be used to get more info, and modprobe <module_name> to load it.

Further developement (getting closer but not just yet…)

I added the

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

into /lib/udev/hwclock-set and now the ds3231 is available once the NanoPi boots.

I also changed HCTOSYS_DEVICE=rtc0 to HCTOSYS_DEVICE=rtc1 in /lib/udev/hwclock-set.

if I do

ls /dev/RTC*

I get: /dev/rtc, /dev/rtc0 and /dev/rtc1. /dev/rtc and /dev/rtc0 are default rtc in the system while /dev/rtc1 is the proper ds3231 RTC.

The problem: At the moment, after reboot, the system time is always taken from /dev/rtc0:

dietpi@DietPiNTP:~$ dmesg | grep rtc
[    3.342126] rk808-rtc rk808-rtc: registered as rtc0
[    3.343968] rk808-rtc rk808-rtc: setting system clock to 2020-07-10T00:44:55 UTC (1594341895)
[   10.403642] rtc-ds1307 0-0068: registered as rtc1
dietpi@DietPiNTP:~$

The question is, how can I make /dev/rtc1 the default RTC? It is registered after the system time is set via /dev/rtc0 so maybe it should be made available earlier? Or how can I issue “sudo hwclock -s -f /dev/rtc1” at boot once /dev/rtc1 has become available?

Cheers,

Enzo

The following will change the /dev/rtc symlink one rtc1 is detected:

echo 'KERNEL=="rtc1", SUBSYSTEM=="rtc", ACTION="add", SYMLINK="rtc", MODE="0666"' > /etc/udev/rules.d/rtc1.rules

But I’m not sure whether this triggers an additional sync as well.

Since at a later boot stage network time sync updates the system clock anyway, do you need the RTC for an offline device and it can sync at a later boot stage, or does it need to be early?

A script in /var/lib/dietpi/postboot.d/ will be executes at late boot stage, otherwise a systemd service would be required.

MichaIng
Issue was marked as solved by OT and a solution was update on first post

Ah whoops, that is the issue when browsing the forum with a small mobile phone display :slight_smile:.

Little note if someone fails the use rc.local: It needs to be executable: chmod +x /etc/rc.local
The service runs automatically then. It cannot be enabled since it’s a static service without any install definition :wink:.

/var/lib/dietpi/postboot.d/ is the very similar alternative that allows to add individual scripts.

Hi,

Yes I am the guy who asked the question on the RTC DS3231 + RPi on github and thanks to MichaIng I succeed in connecting my DS3231 to the Rpi but it was not enough to automatically update the system time at startup before updating it via ntp.

To make it fully functional on my RPi I had to do the same kind of thing that vmiceli did.
If it can be useful for other RPi user, I followed the tutorial here : https://learn.adafruit.com/adding-a-real-time-clock-to-raspberry-pi/set-rtc-time