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

Have some feedback, questions, suggestions, or just fancy a chat? Pop it in here.
vmiceli
Posts: 5
Joined: Tue Mar 16, 2021 11:56 am

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

Post by vmiceli »

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

Code: Select all

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:

Code: Select all

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:

Code: Select all

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

Code: Select all

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:

Code: Select all

#!/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

Code: Select all

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
Last edited by vmiceli on Wed Mar 17, 2021 10:13 pm, edited 1 time in total.
User avatar
Joulinar
Posts: 5116
Joined: Sat Nov 16, 2019 12:49 am

Re: How do I use the DS3231 RTC on NanoPi NEO3 with DietPi 7.0.2?

Post by Joulinar »

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/issu ... -798903973
Pls let us know if a solution is working. This could help others if they hit by similar situation. Your DietPi Team
User avatar
MichaIng
Site Admin
Posts: 3097
Joined: Sat Nov 18, 2017 6:21 pm

Re: How do I use the DS3231 RTC on NanoPi NEO3 with DietPi 7.0.2?

Post by MichaIng »

Code: Select all

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

Code: Select all

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 ... 369ae0a83f
User avatar
Joulinar
Posts: 5116
Joined: Sat Nov 16, 2019 12:49 am

Re: How do I use the DS3231 RTC on NanoPi NEO3 with DietPi 7.0.2?

Post by Joulinar »

@MichaIng
But the NanoPi is not running with RPi kernel, isn't it?
Pls let us know if a solution is working. This could help others if they hit by similar situation. Your DietPi Team
vmiceli
Posts: 5
Joined: Tue Mar 16, 2021 11:56 am

Re: How do I use the DS3231 RTC on NanoPi NEO3 with DietPi 7.0.2?

Post by vmiceli »

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

Cheers,

Enzo
User avatar
MichaIng
Site Admin
Posts: 3097
Joined: Sat Nov 18, 2017 6:21 pm

Re: How do I use the DS3231 RTC on NanoPi NEO3 with DietPi 7.0.2?

Post by MichaIng »

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/issu ... -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.
vmiceli
Posts: 5
Joined: Tue Mar 16, 2021 11:56 am

Re: How do I use the DS3231 RTC on NanoPi NEO3 with DietPi 7.0.2?

Post by vmiceli »

Further developement (getting closer but not just yet...)

I added the

Code: Select all

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

Code: Select all

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:

Code: Select all

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
User avatar
MichaIng
Site Admin
Posts: 3097
Joined: Sat Nov 18, 2017 6:21 pm

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

Post by MichaIng »

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

Code: Select all

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.
User avatar
Joulinar
Posts: 5116
Joined: Sat Nov 16, 2019 12:49 am

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

Post by Joulinar »

@MichaIng
Issue was marked as solved by OT and a solution was update on first post
Pls let us know if a solution is working. This could help others if they hit by similar situation. Your DietPi Team
User avatar
MichaIng
Site Admin
Posts: 3097
Joined: Sat Nov 18, 2017 6:21 pm

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

Post by MichaIng »

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

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 ;).

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